working glfw support

This commit is contained in:
2024-03-26 00:11:38 +01:00
parent 2200863406
commit 4af38a2c7e
20 changed files with 81 additions and 276 deletions

View File

@@ -5,8 +5,8 @@ if [ -e a.out ]; then
fi fi
if [ $(uname -s) = 'Darwin' ]; then if [ $(uname -s) = 'Darwin' ]; then
clang main.c ../libmlx.dylib -L /opt/homebrew/lib -lSDL2 -g; clang main.c ../libmlx.dylib -L /opt/homebrew/lib -lglfw -g;
else else
clang main.c ../libmlx.so -lSDL2 -g -Wall -Wextra -Werror; clang main.c ../libmlx.so -lglfw -g -Wall -Wextra -Werror;
fi fi

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */ /* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:00:40 by maldavid ### ########.fr */ /* Updated: 2024/03/25 22:16:24 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -23,20 +23,14 @@
namespace mlx::core namespace mlx::core
{ {
static bool __drop_sdl_responsability = false;
Application::Application() : _fps(), _in(std::make_unique<Input>()) Application::Application() : _fps(), _in(std::make_unique<Input>())
{ {
_fps.init(); _fps.init();
__drop_sdl_responsability = SDL_WasInit(SDL_INIT_VIDEO); glfwSetErrorCallback([]([[maybe_unused]] int code, const char* desc)
if(__drop_sdl_responsability) // is case the mlx is running in a sandbox like MacroUnitTester where SDL is already init {
return; error::report(e_kind::fatal_error, "GLFW error : %s", desc);
SDL_SetMemoryFunctions(MemManager::malloc, MemManager::calloc, MemManager::realloc, MemManager::free); });
glfwInit();
/* Remove this comment if you want to prioritise Wayland over X11/XWayland, at your own risks */
//SDL_SetHint(SDL_HINT_VIDEODRIVER, "wayland,x11");
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
error::report(e_kind::fatal_error, "SDL error : unable to init all subsystems : %s", SDL_GetError());
} }
void Application::run() noexcept void Application::run() noexcept
@@ -111,9 +105,6 @@ namespace mlx::core
{ {
TextLibrary::get().clearLibrary(); TextLibrary::get().clearLibrary();
FontLibrary::get().clearLibrary(); FontLibrary::get().clearLibrary();
if(__drop_sdl_responsability) glfwTerminate();
return;
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS);
SDL_Quit();
} }
} }

View File

@@ -55,8 +55,6 @@ namespace mlx::core
error::report(e_kind::warning, "trying to move the mouse relative to a window that is targeting an image and not a real window, this is not allowed (move ignored)"); error::report(e_kind::warning, "trying to move the mouse relative to a window that is targeting an image and not a real window, this is not allowed (move ignored)");
return; return;
} }
SDL_WarpMouseInWindow(_graphics[*static_cast<int*>(win)]->getWindow()->getNativeWindow(), x, y);
SDL_PumpEvents();
} }
void Application::onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept void Application::onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept
@@ -73,10 +71,8 @@ namespace mlx::core
void Application::getScreenSize(void* win, int* w, int* h) noexcept void Application::getScreenSize(void* win, int* w, int* h) noexcept
{ {
CHECK_WINDOW_PTR(win); CHECK_WINDOW_PTR(win);
SDL_DisplayMode DM; *w = 0;
SDL_GetDesktopDisplayMode(SDL_GetWindowDisplayIndex(_graphics[*static_cast<int*>(win)]->getWindow()->getNativeWindow()), &DM); *h = 0;
*w = DM.w;
*h = DM.h;
} }
void Application::setFPSCap(std::uint32_t fps) noexcept void Application::setFPSCap(std::uint32_t fps) noexcept

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */ /* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:00:45 by maldavid ### ########.fr */ /* Updated: 2024/03/25 23:05:46 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -78,13 +78,11 @@ extern "C"
int mlx_mouse_show() int mlx_mouse_show()
{ {
SDL_ShowCursor(SDL_ENABLE);
return 0; return 0;
} }
int mlx_mouse_hide() int mlx_mouse_hide()
{ {
SDL_ShowCursor(SDL_DISABLE);
return 0; return 0;
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 14:56:17 by maldavid #+# #+# */ /* Created: 2024/01/18 14:56:17 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:00:54 by maldavid ### ########.fr */ /* Updated: 2024/03/25 22:59:13 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -18,9 +18,9 @@ namespace mlx
{ {
void FpsManager::init() void FpsManager::init()
{ {
_timer = SDL_GetTicks64(); _timer = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
_fps_before = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); _fps_before = _timer;
_fps_now = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); _fps_now = _timer;
} }
bool FpsManager::update() bool FpsManager::update()
@@ -28,8 +28,8 @@ namespace mlx
using namespace std::chrono_literals; using namespace std::chrono_literals;
_fps_now = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); _fps_now = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
if(SDL_GetTicks64() - _timer > 1000) if(std::chrono::duration<std::uint64_t>{_fps_now - _timer} >= 1s)
_timer += 1000; _timer += _fps_now;
_fps_elapsed_time = _fps_now - _fps_before; _fps_elapsed_time = _fps_now - _fps_before;
if(_fps_elapsed_time >= _ns) if(_fps_elapsed_time >= _ns)

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 14:53:30 by maldavid #+# #+# */ /* Created: 2024/01/18 14:53:30 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:13:16 by maldavid ### ########.fr */ /* Updated: 2024/03/25 22:58:32 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -28,9 +28,9 @@ namespace mlx
private: private:
double _ns = 1000000000.0 / 1'337'000.0; double _ns = 1000000000.0 / 1'337'000.0;
std::uint64_t _timer = 0; std::int64_t _fps_before = 0;
std::uint64_t _fps_before = 0; std::int64_t _fps_now = 0;
std::uint64_t _fps_now = 0; std::int64_t _timer = 0;
std::uint32_t _max_fps = 1'337'000; std::uint32_t _max_fps = 1'337'000;
std::uint32_t _fps_elapsed_time = 0; std::uint32_t _fps_elapsed_time = 0;
}; };

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */ /* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:00:58 by maldavid ### ########.fr */ /* Updated: 2024/03/25 23:02:43 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -71,11 +71,12 @@ namespace mlx
#ifdef GRAPHICS_MEMORY_DUMP #ifdef GRAPHICS_MEMORY_DUMP
// dump memory to file every two seconds // dump memory to file every two seconds
static std::uint64_t timer = SDL_GetTicks64(); using namespace std::chrono_literals;
if(SDL_GetTicks64() - timer > 2000) static std::int64_t timer = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
if(std::chrono::duration<std::uint64_t>{static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()) - timer} >= 1s)
{ {
Render_Core::get().getAllocator().dumpMemoryToJson(); Render_Core::get().getAllocator().dumpMemoryToJson();
timer += 2000; timer = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
} }
#endif #endif
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */ /* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:13:11 by maldavid ### ########.fr */ /* Updated: 2024/03/25 23:00:43 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */ /* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:01:09 by maldavid ### ########.fr */ /* Updated: 2024/03/25 23:13:16 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -24,130 +24,14 @@ namespace mlx
_xRel = 0; _xRel = 0;
_yRel = 0; _yRel = 0;
while(SDL_PollEvent(&_event)) static int i = 0;
{ i++;
if(_event.type == SDL_MOUSEMOTION) if(i >= 150)
{
_x = _event.motion.x;
_y = _event.motion.y;
_xRel = _event.motion.xrel;
_yRel = _event.motion.yrel;
}
std::uint32_t id = _event.window.windowID;
if(_events_hooks.find(id) == _events_hooks.end())
continue;
auto& hooks = _events_hooks[id];
switch(_event.type)
{
case SDL_KEYDOWN:
{
if(hooks[MLX_KEYDOWN].hook)
hooks[MLX_KEYDOWN].hook(_event.key.keysym.scancode, hooks[MLX_KEYDOWN].param);
break;
}
case SDL_KEYUP:
{
if(hooks[MLX_KEYUP].hook)
hooks[MLX_KEYUP].hook(_event.key.keysym.scancode, hooks[MLX_KEYUP].param);
break;
}
case SDL_MOUSEBUTTONDOWN:
{
if(hooks[MLX_MOUSEDOWN].hook)
hooks[MLX_MOUSEDOWN].hook(_event.button.button, hooks[MLX_MOUSEDOWN].param);
break;
}
case SDL_MOUSEBUTTONUP:
{
if(hooks[MLX_MOUSEUP].hook)
hooks[MLX_MOUSEUP].hook(_event.button.button, hooks[MLX_MOUSEUP].param);
break;
}
case SDL_MOUSEWHEEL:
{
if(hooks[MLX_MOUSEWHEEL].hook)
{
if(_event.wheel.y > 0) // scroll up
hooks[MLX_MOUSEWHEEL].hook(1, hooks[MLX_MOUSEWHEEL].param);
else if(_event.wheel.y < 0) // scroll down
hooks[MLX_MOUSEWHEEL].hook(2, hooks[MLX_MOUSEWHEEL].param);
if(_event.wheel.x > 0) // scroll right
hooks[MLX_MOUSEWHEEL].hook(3, hooks[MLX_MOUSEWHEEL].param);
else if(_event.wheel.x < 0) // scroll left
hooks[MLX_MOUSEWHEEL].hook(4, hooks[MLX_MOUSEWHEEL].param);
}
break;
}
case SDL_WINDOWEVENT:
{ {
auto& hooks = _events_hooks[0];
auto& win_hook = hooks[MLX_WINDOW_EVENT]; auto& win_hook = hooks[MLX_WINDOW_EVENT];
switch(_event.window.event)
{
case SDL_WINDOWEVENT_CLOSE:
{
if(win_hook.hook) if(win_hook.hook)
win_hook.hook(0, win_hook.param); win_hook.hook(0, win_hook.param);
break;
}
case SDL_WINDOWEVENT_MOVED:
{
if(win_hook.hook)
win_hook.hook(1, win_hook.param);
break;
}
case SDL_WINDOWEVENT_MINIMIZED:
{
if(win_hook.hook)
win_hook.hook(2, win_hook.param);
break;
}
case SDL_WINDOWEVENT_MAXIMIZED:
{
if(win_hook.hook)
win_hook.hook(3, win_hook.param);
break;
}
case SDL_WINDOWEVENT_ENTER:
{
if(win_hook.hook)
win_hook.hook(4, win_hook.param);
break;
}
case SDL_WINDOWEVENT_FOCUS_GAINED:
{
if(win_hook.hook)
win_hook.hook(5, win_hook.param);
break;
}
case SDL_WINDOWEVENT_LEAVE:
{
if(win_hook.hook)
win_hook.hook(6, win_hook.param);
break;
}
case SDL_WINDOWEVENT_FOCUS_LOST:
{
if(win_hook.hook)
win_hook.hook(7, win_hook.param);
break;
}
default : break;
}
break;
}
default: break;
}
} }
} }
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */ /* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:12:44 by maldavid ### ########.fr */ /* Updated: 2024/03/25 23:03:39 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -58,7 +58,6 @@ namespace mlx
private: private:
std::unordered_map<std::uint32_t, std::shared_ptr<MLX_Window>> _windows; std::unordered_map<std::uint32_t, std::shared_ptr<MLX_Window>> _windows;
std::unordered_map<std::uint32_t, std::array<Hook, 6>> _events_hooks; std::unordered_map<std::uint32_t, std::array<Hook, 6>> _events_hooks;
SDL_Event _event;
int _x = 0; int _x = 0;
int _y = 0; int _y = 0;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */ /* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:01:14 by maldavid ### ########.fr */ /* Updated: 2024/03/25 22:17:34 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -18,41 +18,23 @@
namespace mlx namespace mlx
{ {
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
constexpr const std::uint32_t rmask = 0xff000000;
constexpr const std::uint32_t gmask = 0x00ff0000;
constexpr const std::uint32_t bmask = 0x0000ff00;
constexpr const std::uint32_t amask = 0x000000ff;
#else
constexpr const std::uint32_t rmask = 0x000000ff;
constexpr const std::uint32_t gmask = 0x0000ff00;
constexpr const std::uint32_t bmask = 0x00ff0000;
constexpr const std::uint32_t amask = 0xff000000;
#endif
MLX_Window::MLX_Window(std::size_t w, std::size_t h, const std::string& title) : _width(w), _height(h) MLX_Window::MLX_Window(std::size_t w, std::size_t h, const std::string& title) : _width(w), _height(h)
{ {
static std::uint64_t ids = 0;
if(title.find("vvaas") != std::string::npos) if(title.find("vvaas") != std::string::npos)
core::error::report(e_kind::message, "vvaas est mauvais"); core::error::report(e_kind::message, "vvaas est mauvais");
_win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
if(!_win) _win = glfwCreateWindow(_width, _height, title.c_str(), NULL, NULL);;
core::error::report(e_kind::fatal_error, std::string("unable to open a new window, ") + SDL_GetError()); _id = ids++;
_id = SDL_GetWindowID(_win);
_icon = SDL_CreateRGBSurfaceFrom(static_cast<void*>(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, rmask, gmask, bmask, amask);
SDL_SetWindowIcon(_win, _icon);
} }
void MLX_Window::destroy() noexcept void MLX_Window::destroy() noexcept
{ {
if(_win != nullptr) if(_win != nullptr)
{ {
SDL_DestroyWindow(_win); glfwDestroyWindow(_win);
_win = nullptr; _win = nullptr;
} }
if(_icon != nullptr)
{
SDL_FreeSurface(_icon);
_icon = nullptr;
}
} }
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */ /* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:12:46 by maldavid ### ########.fr */ /* Updated: 2024/03/25 22:11:21 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -20,7 +20,7 @@ namespace mlx
public: public:
MLX_Window(std::size_t w, std::size_t h, const std::string& title); MLX_Window(std::size_t w, std::size_t h, const std::string& title);
inline SDL_Window* getNativeWindow() const noexcept { return _win; } inline GLFWwindow* getNativeWindow() const noexcept { return _win; }
inline int getWidth() const noexcept { return _width; } inline int getWidth() const noexcept { return _width; }
inline int getHeight() const noexcept { return _height; } inline int getHeight() const noexcept { return _height; }
inline std::uint32_t getID() const noexcept { return _id; } inline std::uint32_t getID() const noexcept { return _id; }
@@ -30,8 +30,8 @@ namespace mlx
~MLX_Window() = default; ~MLX_Window() = default;
private: private:
SDL_Surface* _icon = nullptr; GLFWimage _icon;
SDL_Window* _win = nullptr; GLFWwindow* _win = nullptr;
int _width = 0; int _width = 0;
int _height = 0; int _height = 0;
std::uint32_t _id = -1; std::uint32_t _id = -1;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/25 17:37:23 by maldavid #+# #+# */ /* Created: 2024/03/25 17:37:23 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:24:26 by maldavid ### ########.fr */ /* Updated: 2024/03/25 22:03:44 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -18,8 +18,8 @@
#include <mlx_profile.h> #include <mlx_profile.h>
#include <cstdio> #include <cstdio>
#include <iostream> #include <iostream>
#include <SDL2/SDL.h>
#include <volk.h> #include <volk.h>
#include <GLFW/glfw3.h>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <list> #include <list>
@@ -41,7 +41,6 @@
#include <cstring> #include <cstring>
#include <optional> #include <optional>
#include <set> #include <set>
#include <SDL2/SDL_vulkan.h>
#include <cstddef> #include <cstddef>
#include <cstdlib> #include <cstdlib>
#include <random> #include <random>

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:14:29 by maldavid #+# #+# */ /* Created: 2022/10/08 19:14:29 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:02:11 by maldavid ### ########.fr */ /* Updated: 2024/03/25 22:31:54 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -71,19 +71,11 @@ namespace mlx
std::vector<VkPhysicalDevice> devices(deviceCount); std::vector<VkPhysicalDevice> devices(deviceCount);
vkEnumeratePhysicalDevices(Render_Core::get().getInstance().get(), &deviceCount, devices.data()); vkEnumeratePhysicalDevices(Render_Core::get().getInstance().get(), &deviceCount, devices.data());
SDL_Window* window = SDL_CreateWindow("", 0, 0, 1, 1, SDL_WINDOW_VULKAN | SDL_WINDOW_HIDDEN);
if(!window)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create a window to pick physical device");
VkSurfaceKHR surface = VK_NULL_HANDLE;
if(SDL_Vulkan_CreateSurface(window, Render_Core::get().getInstance().get(), &surface) != SDL_TRUE)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create a surface to pick physical device");
std::multimap<int, VkPhysicalDevice> devices_score; std::multimap<int, VkPhysicalDevice> devices_score;
for(const auto& device : devices) for(const auto& device : devices)
{ {
int score = deviceScore(device, surface); int score = deviceScore(device);
devices_score.insert(std::make_pair(score, device)); devices_score.insert(std::make_pair(score, device));
} }
@@ -97,23 +89,17 @@ namespace mlx
vkGetPhysicalDeviceProperties(_physical_device, &props); vkGetPhysicalDeviceProperties(_physical_device, &props);
core::error::report(e_kind::message, "Vulkan : picked a physical device, %s", props.deviceName); core::error::report(e_kind::message, "Vulkan : picked a physical device, %s", props.deviceName);
#endif #endif
Render_Core::get().getQueue().findQueueFamilies(_physical_device, surface); // update queue indicies to current physical device Render_Core::get().getQueue().findQueueFamilies(_physical_device); // update queue indicies to current physical device
vkDestroySurfaceKHR(Render_Core::get().getInstance().get(), surface, nullptr);
SDL_DestroyWindow(window);
} }
int Device::deviceScore(VkPhysicalDevice device, VkSurfaceKHR surface) int Device::deviceScore(VkPhysicalDevice device)
{ {
Queues::QueueFamilyIndices indices = Render_Core::get().getQueue().findQueueFamilies(device, surface); Queues::QueueFamilyIndices indices = Render_Core::get().getQueue().findQueueFamilies(device);
bool extensionsSupported = checkDeviceExtensionSupport(device); bool extensionsSupported = checkDeviceExtensionSupport(device);
std::uint32_t formatCount = 0;
if(extensionsSupported)
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
VkPhysicalDeviceProperties props; VkPhysicalDeviceProperties props;
vkGetPhysicalDeviceProperties(device, &props); vkGetPhysicalDeviceProperties(device, &props);
if(!indices.isComplete() || !extensionsSupported || formatCount == 0) if(!indices.isComplete() || !extensionsSupported)
return -1; return -1;
VkPhysicalDeviceFeatures features; VkPhysicalDeviceFeatures features;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:13:42 by maldavid #+# #+# */ /* Created: 2022/10/08 19:13:42 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:11:56 by maldavid ### ########.fr */ /* Updated: 2024/03/25 22:31:46 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -29,7 +29,7 @@ namespace mlx
private: private:
void pickPhysicalDevice(); void pickPhysicalDevice();
bool checkDeviceExtensionSupport(VkPhysicalDevice device); bool checkDeviceExtensionSupport(VkPhysicalDevice device);
int deviceScore(VkPhysicalDevice device, VkSurfaceKHR surface); int deviceScore(VkPhysicalDevice device);
private: private:
VkPhysicalDevice _physical_device = VK_NULL_HANDLE; VkPhysicalDevice _physical_device = VK_NULL_HANDLE;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:04:21 by maldavid #+# #+# */ /* Created: 2022/10/08 19:04:21 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:02:18 by maldavid ### ########.fr */ /* Updated: 2024/03/25 23:10:37 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -59,34 +59,19 @@ namespace mlx
std::vector<const char*> Instance::getRequiredExtensions() std::vector<const char*> Instance::getRequiredExtensions()
{ {
std::vector<const char*> extensions; std::uint32_t glfw_extension_count = 0;
const char** glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_extension_count);
std::vector<const char*> extensions(glfw_extensions, glfw_extensions + glfw_extension_count);
extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
#ifdef VK_USE_PLATFORM_XCB_KHR
extensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_XLIB_KHR
extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_WAYLAND_KHR
extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_WIN32_KHR
extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_METAL_EXT
extensions.push_back(VK_EXT_METAL_SURFACE_EXTENSION_NAME);
#endif
if constexpr(enableValidationLayers) if constexpr(enableValidationLayers)
{ {
extensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); extensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
} }
return extensions; return extensions;
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:02:42 by maldavid #+# #+# */ /* Created: 2022/10/08 19:02:42 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:02:20 by maldavid ### ########.fr */ /* Updated: 2024/03/25 22:29:19 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -16,7 +16,7 @@
namespace mlx namespace mlx
{ {
Queues::QueueFamilyIndices Queues::findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface) Queues::QueueFamilyIndices Queues::findQueueFamilies(VkPhysicalDevice device)
{ {
std::uint32_t queueFamilyCount = 0; std::uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr); vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
@@ -31,10 +31,7 @@ namespace mlx
if(queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) if(queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)
_families->graphics_family = i; _families->graphics_family = i;
VkBool32 presentSupport = false; if(glfwGetPhysicalDevicePresentationSupport(Render_Core::get().getInstance().get(), device, i))
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
if(presentSupport)
_families->present_family = i; _families->present_family = i;
if(_families->isComplete()) if(_families->isComplete())
@@ -48,20 +45,7 @@ namespace mlx
void Queues::init() void Queues::init()
{ {
if(!_families.has_value()) if(!_families.has_value())
{ findQueueFamilies(Render_Core::get().getDevice().getPhysicalDevice());
SDL_Window* window = SDL_CreateWindow("", 0, 0, 1, 1, SDL_WINDOW_VULKAN | SDL_WINDOW_HIDDEN);
if(!window)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create a window to init queues");
VkSurfaceKHR surface = VK_NULL_HANDLE;
if(SDL_Vulkan_CreateSurface(window, Render_Core::get().getInstance().get(), &surface) != SDL_TRUE)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create a surface to init queues");
findQueueFamilies(Render_Core::get().getDevice().getPhysicalDevice(), surface);
vkDestroySurfaceKHR(Render_Core::get().getInstance().get(), surface, nullptr);
SDL_DestroyWindow(window);
}
vkGetDeviceQueue(Render_Core::get().getDevice().get(), _families->graphics_family.value(), 0, &_graphics_queue); vkGetDeviceQueue(Render_Core::get().getDevice().get(), _families->graphics_family.value(), 0, &_graphics_queue);
vkGetDeviceQueue(Render_Core::get().getDevice().get(), _families->present_family.value(), 0, &_present_queue); vkGetDeviceQueue(Render_Core::get().getDevice().get(), _families->present_family.value(), 0, &_present_queue);
#ifdef DEBUG #ifdef DEBUG

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:01:49 by maldavid #+# #+# */ /* Created: 2022/10/08 19:01:49 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:11:46 by maldavid ### ########.fr */ /* Updated: 2024/03/25 22:29:26 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -28,7 +28,7 @@ namespace mlx
inline bool isComplete() { return graphics_family.has_value() && present_family.has_value(); } inline bool isComplete() { return graphics_family.has_value() && present_family.has_value(); }
}; };
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface); QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
void init(); void init();

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 18:58:49 by maldavid #+# #+# */ /* Created: 2022/10/08 18:58:49 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:00:11 by maldavid ### ########.fr */ /* Updated: 2024/03/25 22:25:55 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -19,8 +19,8 @@ namespace mlx
{ {
void Surface::create(Renderer& renderer) void Surface::create(Renderer& renderer)
{ {
if(SDL_Vulkan_CreateSurface(renderer.getWindow()->getNativeWindow(), Render_Core::get().getInstance().get(), &_surface) != SDL_TRUE) if(glfwCreateWindowSurface(Render_Core::get().getInstance().get(), renderer.getWindow()->getNativeWindow(), NULL, &_surface) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create a surface : %s", SDL_GetError()); core::error::report(e_kind::fatal_error, "Vulkan : failed to create a surface");
#ifdef DEBUG #ifdef DEBUG
core::error::report(e_kind::message, "Vulkan : created new surface"); core::error::report(e_kind::message, "Vulkan : created new surface");
#endif #endif

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:22:28 by maldavid #+# #+# */ /* Created: 2022/10/06 18:22:28 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:03:41 by maldavid ### ########.fr */ /* Updated: 2024/03/25 23:09:33 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -33,7 +33,7 @@ namespace mlx
if(_swapchain_support.capabilities.maxImageCount > 0 && imageCount > _swapchain_support.capabilities.maxImageCount) if(_swapchain_support.capabilities.maxImageCount > 0 && imageCount > _swapchain_support.capabilities.maxImageCount)
imageCount = _swapchain_support.capabilities.maxImageCount; imageCount = _swapchain_support.capabilities.maxImageCount;
Queues::QueueFamilyIndices indices = Render_Core::get().getQueue().findQueueFamilies(Render_Core::get().getDevice().getPhysicalDevice(), renderer->getSurface().get()); Queues::QueueFamilyIndices indices = Render_Core::get().getQueue().findQueueFamilies(Render_Core::get().getDevice().getPhysicalDevice());
std::uint32_t queueFamilyIndices[] = { indices.graphics_family.value(), indices.present_family.value() }; std::uint32_t queueFamilyIndices[] = { indices.graphics_family.value(), indices.present_family.value() };
VkSwapchainCreateInfoKHR createInfo{}; VkSwapchainCreateInfoKHR createInfo{};
@@ -123,7 +123,7 @@ namespace mlx
return capabilities.currentExtent; return capabilities.currentExtent;
int width, height; int width, height;
SDL_Vulkan_GetDrawableSize(_renderer->getWindow()->getNativeWindow(), &width, &height); glfwGetFramebufferSize(_renderer->getWindow()->getNativeWindow(), &width, &height);
VkExtent2D actualExtent = { static_cast<std::uint32_t>(width), static_cast<std::uint32_t>(height) }; VkExtent2D actualExtent = { static_cast<std::uint32_t>(width), static_cast<std::uint32_t>(height) };