More windowing changes for renderdoc

This commit is contained in:
Dynamitos committed 2026-04-07 22:25:13 +02:00
1 parent 7af42b9bad
commit b6a449d8e3
3 files changed
+283 -62

No files matched your search

+116 -41
View File
@@ -3,12 +3,79 @@
#include "Enums.h"
#include "Graphics.h"
#include "Resources.h"
#define GLFW_EXPOSE_NATIVE_WAYLAND
#define GLFW_EXPOSE_NATIVE_X11
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#include <X11/Xlib.h>
#include <sstream>
#include <vulkan/vulkan_wayland.h>
#include <vulkan/vulkan_xlib.h>
using namespace Seele;
using namespace Seele::Vulkan;
namespace {
VkResult createSurfaceFromNativeHandles(VkInstance instance, GLFWwindow* handle, VkSurfaceKHR* surface, std::string& reason) {
#if defined(GLFW_PLATFORM_WAYLAND)
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND) {
auto createWaylandSurface = reinterpret_cast<PFN_vkCreateWaylandSurfaceKHR>(
vkGetInstanceProcAddr(instance, "vkCreateWaylandSurfaceKHR"));
if (createWaylandSurface == nullptr) {
reason = "vkCreateWaylandSurfaceKHR is unavailable on the Vulkan instance";
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
wl_display* display = glfwGetWaylandDisplay();
wl_surface* waylandSurface = glfwGetWaylandWindow(handle);
if (display == nullptr || waylandSurface == nullptr) {
reason = "GLFW did not provide valid Wayland native handles";
return VK_ERROR_INITIALIZATION_FAILED;
}
VkWaylandSurfaceCreateInfoKHR createInfo = {
.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR,
.pNext = nullptr,
.flags = 0,
.display = display,
.surface = waylandSurface,
};
return createWaylandSurface(instance, &createInfo, nullptr, surface);
}
#endif
#if defined(GLFW_PLATFORM_X11)
if (glfwGetPlatform() == GLFW_PLATFORM_X11) {
auto createXlibSurface = reinterpret_cast<PFN_vkCreateXlibSurfaceKHR>(
vkGetInstanceProcAddr(instance, "vkCreateXlibSurfaceKHR"));
if (createXlibSurface == nullptr) {
reason = "vkCreateXlibSurfaceKHR is unavailable on the Vulkan instance";
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
Display* display = glfwGetX11Display();
::Window x11Window = glfwGetX11Window(handle);
if (display == nullptr || x11Window == 0) {
reason = "GLFW did not provide valid X11 native handles";
return VK_ERROR_INITIALIZATION_FAILED;
}
VkXlibSurfaceCreateInfoKHR createInfo = {
.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
.pNext = nullptr,
.flags = 0,
.dpy = display,
.window = x11Window,
};
return createXlibSurface(instance, &createInfo, nullptr, surface);
}
#endif
reason = "Unsupported GLFW platform for manual Vulkan surface creation";
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
}
double currentFrameDelta = 0;
double Gfx::getCurrentFrameDelta() { return currentFrameDelta; }
@@ -23,41 +90,41 @@ void glfwKeyCallback(GLFWwindow* handle, int key, int, int action, int modifier)
if (key == -1) {
return;
}
Window* window = (Window*)glfwGetWindowUserPointer(handle);
window->keyPress((KeyCode)key, (InputAction)action, (KeyModifier)modifier);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->keyPress(static_cast<Seele::KeyCode>(key), static_cast<InputAction>(action), static_cast<KeyModifier>(modifier));
}
void glfwMouseMoveCallback(GLFWwindow* handle, double xpos, double ypos) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->mouseMove(xpos, ypos);
}
void glfwMouseButtonCallback(GLFWwindow* handle, int button, int action, int modifier) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
window->mouseButton((MouseButton)button, (InputAction)action, (KeyModifier)modifier);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->mouseButton(static_cast<MouseButton>(button), static_cast<InputAction>(action), static_cast<KeyModifier>(modifier));
}
void glfwScrollCallback(GLFWwindow* handle, double xoffset, double yoffset) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->scroll(xoffset, yoffset);
}
void glfwFileCallback(GLFWwindow* handle, int count, const char** paths) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->fileDrop(count, paths);
}
void glfwCloseCallback(GLFWwindow* handle) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->close();
}
void glfwFramebufferResizeCallback(GLFWwindow* handle, int width, int height) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->resize(width, height);
}
Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo)
Seele::Vulkan::Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo)
: graphics(graphics), preferences(createInfo), instance(graphics->getInstance()), swapchain(VK_NULL_HANDLE) {
glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &contentScaleX, &contentScaleY);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
@@ -90,12 +157,20 @@ Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo)
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;
std::string nativeReason;
surfaceResult = createSurfaceFromNativeHandles(instance, handle, &surface, nativeReason);
if (surfaceResult != VK_SUCCESS || surface == VK_NULL_HANDLE) {
std::ostringstream oss;
oss << "Failed to create Vulkan surface via GLFW (VkResult=" << surfaceResult << ")";
if (glfwError != nullptr) {
oss << ": " << glfwError;
}
if (!nativeReason.empty()) {
oss << ". Native fallback failed: " << nativeReason;
}
throw std::runtime_error(oss.str());
}
throw std::runtime_error(oss.str());
}
querySurface();
@@ -108,17 +183,17 @@ Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo)
createSwapChain();
}
Window::~Window() {
Seele::Vulkan::Window::~Window() {
vkDestroySwapchainKHR(graphics->getDevice(), swapchain, nullptr);
vkDestroySurfaceKHR(instance, surface, nullptr);
glfwDestroyWindow(static_cast<GLFWwindow*>(windowHandle));
}
void Window::pollInput() { glfwPollEvents(); }
void Seele::Vulkan::Window::pollInput() { glfwPollEvents(); }
void Window::show() { glfwShowWindow(static_cast<GLFWwindow*>(windowHandle)); }
void Seele::Vulkan::Window::show() { glfwShowWindow(static_cast<GLFWwindow*>(windowHandle)); }
void Window::beginFrame() {
void Seele::Vulkan::Window::beginFrame() {
imageAvailableFences[currentSemaphoreIndex]->reset();
imageAvailableSemaphores[currentSemaphoreIndex]->resolveSignal();
imageAvailableSemaphores[currentSemaphoreIndex]->rotateSemaphore();
@@ -139,7 +214,7 @@ void Window::beginFrame() {
start = end;
}
void Window::endFrame() {
void Seele::Vulkan::Window::endFrame() {
swapChainTextures[currentImageIndex]->changeLayout(Gfx::SE_IMAGE_LAYOUT_PRESENT_SRC_KHR, Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, Gfx::SE_ACCESS_MEMORY_READ_BIT,
Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
@@ -169,39 +244,39 @@ void Window::endFrame() {
// graphics->waitDeviceIdle();
}
bool Window::shouldClose() const {
bool Seele::Vulkan::Window::shouldClose() const {
return glfwWindowShouldClose((GLFWwindow*)windowHandle);
}
Gfx::PTexture2D Window::getBackBuffer() const { return PTexture2D(swapChainTextures[currentImageIndex]); }
Gfx::PTexture2D Seele::Vulkan::Window::getBackBuffer() const { return PTexture2D(swapChainTextures[currentImageIndex]); }
void Window::setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback) { keyCallback = callback; }
void Seele::Vulkan::Window::setKeyCallback(std::function<void(Seele::KeyCode, InputAction, KeyModifier)> callback) { keyCallback = callback; }
void Window::setMouseMoveCallback(std::function<void(double, double)> callback) { mouseMoveCallback = callback; }
void Seele::Vulkan::Window::setMouseMoveCallback(std::function<void(double, double)> callback) { mouseMoveCallback = callback; }
void Window::setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) { mouseButtonCallback = callback; }
void Seele::Vulkan::Window::setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) { mouseButtonCallback = callback; }
void Window::setScrollCallback(std::function<void(double, double)> callback) { scrollCallback = callback; }
void Seele::Vulkan::Window::setScrollCallback(std::function<void(double, double)> callback) { scrollCallback = callback; }
void Window::setFileCallback(std::function<void(int, const char**)> callback) { fileCallback = callback; }
void Seele::Vulkan::Window::setFileCallback(std::function<void(int, const char**)> callback) { fileCallback = callback; }
void Window::setCloseCallback(std::function<void()> callback) { closeCallback = callback; }
void Seele::Vulkan::Window::setCloseCallback(std::function<void()> callback) { closeCallback = callback; }
void Window::setResizeCallback(std::function<void(uint32, uint32)> callback) { resizeCallback = callback; }
void Seele::Vulkan::Window::setResizeCallback(std::function<void(uint32, uint32)> callback) { resizeCallback = callback; }
void Window::keyPress(KeyCode code, InputAction action, KeyModifier modifier) { keyCallback(code, action, modifier); }
void Seele::Vulkan::Window::keyPress(Seele::KeyCode code, InputAction action, KeyModifier modifier) { keyCallback(code, action, modifier); }
void Window::mouseMove(double x, double y) { mouseMoveCallback(x, y); }
void Seele::Vulkan::Window::mouseMove(double x, double y) { mouseMoveCallback(x, y); }
void Window::mouseButton(MouseButton button, InputAction action, KeyModifier modifier) { mouseButtonCallback(button, action, modifier); }
void Seele::Vulkan::Window::mouseButton(MouseButton button, InputAction action, KeyModifier modifier) { mouseButtonCallback(button, action, modifier); }
void Window::scroll(double x, double y) { scrollCallback(x, y); }
void Seele::Vulkan::Window::scroll(double x, double y) { scrollCallback(x, y); }
void Window::fileDrop(int num, const char** files) { fileCallback(num, files); }
void Seele::Vulkan::Window::fileDrop(int num, const char** files) { fileCallback(num, files); }
void Window::close() { closeCallback(); }
void Seele::Vulkan::Window::close() { closeCallback(); }
void Window::resize(int width, int height) {
void Seele::Vulkan::Window::resize(int width, int height) {
if (width == 0 || height == 0) {
paused = true;
return;
@@ -218,7 +293,7 @@ void Window::resize(int width, int height) {
resizeCallback(width, height);
}
void Window::querySurface() {
void Seele::Vulkan::Window::querySurface() {
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(graphics->getPhysicalDevice(), surface, &capabilities);
uint32 numFormats;
@@ -232,7 +307,7 @@ void Window::querySurface() {
vkGetPhysicalDeviceSurfacePresentModesKHR(graphics->getPhysicalDevice(), surface, &numPresentModes, supportedPresentModes.data());
}
void Window::chooseSwapSurfaceFormat() {
void Seele::Vulkan::Window::chooseSwapSurfaceFormat() {
for (const auto& supportedFormat : supportedFormats) {
if (supportedFormat.format == cast(preferences.preferredFormat)) {
format = supportedFormat;
@@ -248,7 +323,7 @@ void Window::chooseSwapSurfaceFormat() {
format = supportedFormats[0];
}
void Window::chooseSwapPresentMode() {
void Seele::Vulkan::Window::chooseSwapPresentMode() {
for (const auto& supportedPresentMode : supportedPresentModes) {
if (supportedPresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
presentMode = supportedPresentMode;
@@ -258,7 +333,7 @@ void Window::chooseSwapPresentMode() {
presentMode = VK_PRESENT_MODE_FIFO_KHR;
}
void Window::chooseSwapExtent() {
void Seele::Vulkan::Window::chooseSwapExtent() {
if (capabilities.currentExtent.width != std::numeric_limits<uint32>::max()) {
extent = capabilities.currentExtent;
return;
@@ -274,7 +349,7 @@ void Window::chooseSwapExtent() {
extent.height = std::clamp(extent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
}
void Window::createSwapChain() {
void Seele::Vulkan::Window::createSwapChain() {
graphics->waitDeviceIdle();
uint32 imageCount = Gfx::numFramesBuffered;
VkSwapchainCreateInfoKHR createInfo = {