From 87390db80df7223809357061056ab509e5de6975 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Mon, 6 Apr 2026 14:28:58 +0200 Subject: [PATCH] Adding more Window validation --- CMakeLists.txt | 29 +++++++++---------------- src/Engine/Graphics/Graphics.h | 2 +- src/Engine/Graphics/Vulkan/Graphics.cpp | 19 ++++++++++++++-- src/Engine/Graphics/Vulkan/Window.cpp | 23 +++++++++++++++++++- 4 files changed, 50 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d54de8..00ce42a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,7 +59,12 @@ target_include_directories(Engine PRIVATE src/Engine) target_link_libraries(Engine PUBLIC Vulkan::Vulkan) target_link_libraries(Engine PUBLIC GPUOpen::VulkanMemoryAllocator) if(NOT APPLE) - target_link_libraries(Engine PUBLIC slang::slang-glslang) + # slang-glslang is a MODULE library (plugin) - copy it instead of linking + add_custom_command(TARGET Engine POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + $ + $ + ) endif() target_link_libraries(Engine PUBLIC EnTT::EnTT) target_link_libraries(Engine PUBLIC glfw) @@ -121,31 +126,17 @@ if(APPLE) target_compile_options(Engine PUBLIC -Wno-gnu-anonymous-struct -Wno-nested-anon-types) target_compile_options(Editor PUBLIC -Wno-gnu-anonymous-struct -Wno-nested-anon-types) endif() -target_compile_options(Engine PUBLIC "$<$:-DENABLE_VALIDATION>") -target_compile_options(Engine PUBLIC "$<$:-DSEELE_DEBUG>") add_subdirectory(src/) #add_subdirectory(tests/) if(WIN32) - add_custom_target(dll_copy ALL - COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ - COMMAND_EXPAND_LISTS - DEPENDS Editor) -elseif(APPLE) - add_custom_target(dll_copy ALL - COMMAND ${CMAKE_COMMAND} -E true - COMMAND_EXPAND_LISTS - DEPENDS Editor) -else() - add_custom_target(dll_copy ALL - COMMAND ${CMAKE_COMMAND} -E true) + add_custom_command(TARGET Engine POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $) endif() -add_custom_target(SeeleEngine ALL - COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/res $ - COMMAND_EXPAND_LISTS - DEPENDS Editor dll_copy) +add_custom_command(TARGET Engine POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/res $) install( TARGETS diff --git a/src/Engine/Graphics/Graphics.h b/src/Engine/Graphics/Graphics.h index 6414fa8..adf6238 100644 --- a/src/Engine/Graphics/Graphics.h +++ b/src/Engine/Graphics/Graphics.h @@ -4,7 +4,7 @@ #include "MinimalEngine.h" #include "RenderTarget.h" #include "Resources.h" - +#define ENABLE_VALIDATION namespace Seele { namespace Gfx { diff --git a/src/Engine/Graphics/Vulkan/Graphics.cpp b/src/Engine/Graphics/Vulkan/Graphics.cpp index 6e519ec..d3c7540 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.cpp +++ b/src/Engine/Graphics/Vulkan/Graphics.cpp @@ -667,6 +667,17 @@ Array Graphics::getRequiredExtensions() { unsigned int glfwExtensionCount = 0; const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); + if (glfwExtensions == nullptr || glfwExtensionCount == 0) { + const char* glfwError = nullptr; + glfwGetError(&glfwError); + std::string message = "GLFW did not report required Vulkan instance extensions"; + if (glfwError != nullptr) { + message += ": "; + message += glfwError; + } + throw std::runtime_error(message); + } + for (unsigned int i = 0; i < glfwExtensionCount; i++) { extensions.add(glfwExtensions[i]); } @@ -677,8 +688,12 @@ Array Graphics::getRequiredExtensions() { } void Graphics::initInstance(GraphicsInitializer initInfo) { - glfwInit(); - assert(glfwVulkanSupported()); + if (!glfwInit()) { + throw std::runtime_error("Failed to initialize GLFW"); + } + if (!glfwVulkanSupported()) { + throw std::runtime_error("GLFW: Vulkan not supported"); + } VkApplicationInfo appInfo = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .pNext = nullptr, diff --git a/src/Engine/Graphics/Vulkan/Window.cpp b/src/Engine/Graphics/Vulkan/Window.cpp index 81295d2..4b5974f 100644 --- a/src/Engine/Graphics/Vulkan/Window.cpp +++ b/src/Engine/Graphics/Vulkan/Window.cpp @@ -4,6 +4,7 @@ #include "Graphics.h" #include "Resources.h" #include +#include using namespace Seele; using namespace Seele::Vulkan; @@ -61,6 +62,16 @@ Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo) glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); GLFWwindow* handle = glfwCreateWindow(createInfo.width / contentScaleX, createInfo.height / contentScaleY, createInfo.title, nullptr, nullptr); + if (handle == nullptr) { + const char* glfwError = nullptr; + glfwGetError(&glfwError); + std::string message = "Failed to create GLFW window"; + if (glfwError != nullptr) { + message += ": "; + message += glfwError; + } + throw std::runtime_error(message); + } windowHandle = handle; glfwSetWindowUserPointer(handle, this); @@ -73,7 +84,17 @@ Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo) glfwSetFramebufferSizeCallback(handle, &glfwFramebufferResizeCallback); // glfwSetWindowSizeCallback(handle, &glfwResizeCallback); - glfwCreateWindowSurface(instance, handle, nullptr, &surface); + VkResult surfaceResult = glfwCreateWindowSurface(instance, handle, nullptr, &surface); + if (surfaceResult != VK_SUCCESS || surface == VK_NULL_HANDLE) { + const char* glfwError = nullptr; + glfwGetError(&glfwError); + std::ostringstream oss; + oss << "Failed to create Vulkan surface via GLFW (VkResult=" << surfaceResult << ")"; + if (glfwError != nullptr) { + oss << ": " << glfwError; + } + throw std::runtime_error(oss.str()); + } querySurface(); chooseSwapSurfaceFormat();