From 8a3eb62d1231a002a41dbb97f289cab823243e18 Mon Sep 17 00:00:00 2001 From: AliMehrabani Date: Mon, 18 Nov 2024 18:43:56 +0330 Subject: [PATCH] Replace GLFW window with Qt window GLFW window is replaced with a custom QWindow but it just displays the image. resizability and cleanup are not implemented. --- VkTest.pro | 2 +- VkTest.pro.user | 6 +-- VulkanTutorial1.0/ComputeAndGraphics.cpp | 55 +++++++++++++++--------- VulkanTutorial1.0/ComputeAndGraphics.h | 4 +- VulkanTutorial1.0/VulkanWindow.cpp | 11 +++-- VulkanTutorial1.0/VulkanWindow.h | 3 +- main.cpp | 19 +++++--- 7 files changed, 61 insertions(+), 39 deletions(-) diff --git a/VkTest.pro b/VkTest.pro index de9dcda..3fc1a47 100644 --- a/VkTest.pro +++ b/VkTest.pro @@ -1,4 +1,4 @@ -QT += core xml x11extras +QT += core xml x11extras gui CONFIG += c++17 diff --git a/VkTest.pro.user b/VkTest.pro.user index 7914520..ae09adb 100644 --- a/VkTest.pro.user +++ b/VkTest.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -623,8 +623,8 @@ 0 - VkTest - VkTest + + Qt4ProjectManager.AndroidRunConfiguration:/home/ali-mehrabani/Qt_projects/VkTest/VkTest.pro 3768 diff --git a/VulkanTutorial1.0/ComputeAndGraphics.cpp b/VulkanTutorial1.0/ComputeAndGraphics.cpp index 15ad85f..e8626cc 100644 --- a/VulkanTutorial1.0/ComputeAndGraphics.cpp +++ b/VulkanTutorial1.0/ComputeAndGraphics.cpp @@ -46,23 +46,27 @@ ComputeAndGraphics::ComputeAndGraphics() void ComputeAndGraphics::run() { - initWindow(); +// initWindow(); + initVulkan(); mainLoop(); - cleanup(); +// cleanup(); } -void ComputeAndGraphics::initWindow() +void ComputeAndGraphics::initWindow(VulkanWindow* window) { - glfwInit(); +// glfwInit(); + +// glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); +// _window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); - _window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); // glfwSetWindowUserPointer(_window, this); // glfwSetFramebufferSizeCallback(_window, framebufferResizeCallback); -// window = +// VulkanWindow window; + window->resize(800, 600); + _windowQt = window; } //void ComputeAndGraphics::framebufferResizeCallback(GLFWwindow* window, int /*width*/, int /*height*/) { @@ -236,9 +240,11 @@ void ComputeAndGraphics::setupDebugMessenger() { void ComputeAndGraphics::createSurface() { - if (glfwCreateWindowSurface(_instance, _window, nullptr, &_surface) != VK_SUCCESS) { - throw std::runtime_error("failed to create window surface!"); - } +// if (glfwCreateWindowSurface(_instance, _window, nullptr, &_surface) != VK_SUCCESS) { +// throw std::runtime_error("failed to create window surface!"); +// } + + _windowQt->createVulkanSurface(_instance, _surface); } void ComputeAndGraphics::pickPhysicalDevice() { @@ -569,7 +575,9 @@ VkExtent2D ComputeAndGraphics::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& return capabilities.currentExtent; } else { int width, height; - glfwGetFramebufferSize(_window, &width, &height); +// glfwGetFramebufferSize(_window, &width, &height); + width = 800; + height = 600; VkExtent2D actualExtent = { static_cast(width), @@ -1874,11 +1882,16 @@ void ComputeAndGraphics::createSyncObjects() { void ComputeAndGraphics::mainLoop() { - while (!glfwWindowShouldClose(_window)) { - glfwPollEvents(); +// while (!glfwWindowShouldClose(_window)) { +// glfwPollEvents(); +// drawFrame(); +// } + + while (true) { drawFrame(); } + vkDeviceWaitIdle(_device); } @@ -2151,12 +2164,12 @@ void ComputeAndGraphics::updateComputeUniformBuffer(uint32_t currentImage) } void ComputeAndGraphics::recreateSwapChain() { - int width = 0, height = 0; - glfwGetFramebufferSize(_window, &width, &height); - while (width == 0 || height == 0) { - glfwGetFramebufferSize(_window, &width, &height); - glfwWaitEvents(); - } + int width = 800, height = 600; +// glfwGetFramebufferSize(_window, &width, &height); +// while (width == 0 || height == 0) { +// glfwGetFramebufferSize(_window, &width, &height); +// glfwWaitEvents(); +// } vkDeviceWaitIdle(_device); @@ -2248,9 +2261,9 @@ void ComputeAndGraphics::cleanBase() vkDestroyInstance(_instance, nullptr); - glfwDestroyWindow(_window); +// glfwDestroyWindow(_window); - glfwTerminate(); +// glfwTerminate(); } void ComputeAndGraphics::cleanDescriptors() diff --git a/VulkanTutorial1.0/ComputeAndGraphics.h b/VulkanTutorial1.0/ComputeAndGraphics.h index eb4cbda..5822185 100644 --- a/VulkanTutorial1.0/ComputeAndGraphics.h +++ b/VulkanTutorial1.0/ComputeAndGraphics.h @@ -90,6 +90,7 @@ class ComputeAndGraphics public: ComputeAndGraphics(); void run(); + void initWindow(VulkanWindow* window); // Message Callback static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT /*messageSeverity*/, @@ -105,7 +106,6 @@ public: // static void framebufferResizeCallback(QQuickWindow* window, int width, int height); private: - void initWindow(); void initVulkan(); void mainLoop(); void cleanup(); @@ -316,7 +316,7 @@ private: bool framebufferResized = false; GLFWwindow *_window; -// VulkanWindow _window; + VulkanWindow* _windowQt; VkInstance _instance; VkDebugUtilsMessengerEXT _debugMessenger; VkPhysicalDevice _physicalDevice; diff --git a/VulkanTutorial1.0/VulkanWindow.cpp b/VulkanTutorial1.0/VulkanWindow.cpp index 7547bfd..aa6deb3 100644 --- a/VulkanTutorial1.0/VulkanWindow.cpp +++ b/VulkanTutorial1.0/VulkanWindow.cpp @@ -7,15 +7,14 @@ VulkanWindow::VulkanWindow() // qCritical() << "Failed to initialize Vulkan instance!"; // exit(EXIT_FAILURE); // } - createVulkanSurface(); +// createVulkanSurface(); } VulkanWindow::~VulkanWindow() { // cleanup(); } -void VulkanWindow::createVulkanSurface() { - VkSurfaceKHR surface; +VkSurfaceKHR VulkanWindow::createVulkanSurface(VkInstance instance, VkSurfaceKHR& surface) { #ifdef Q_OS_LINUX VkXcbSurfaceCreateInfoKHR surfaceCreateInfo = {}; @@ -23,7 +22,7 @@ void VulkanWindow::createVulkanSurface() { surfaceCreateInfo.connection = QX11Info::connection(); surfaceCreateInfo.window = static_cast(winId()); - if (vkCreateXcbSurfaceKHR(_instance, &surfaceCreateInfo, nullptr, &surface) != VK_SUCCESS) { + if (vkCreateXcbSurfaceKHR(instance, &surfaceCreateInfo, nullptr, &surface) != VK_SUCCESS) { qCritical() << "Failed to create Vulkan surface on Linux."; exit(EXIT_FAILURE); } @@ -32,14 +31,14 @@ void VulkanWindow::createVulkanSurface() { surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; surfaceCreateInfo.window = reinterpret_cast(winId()); - if (vkCreateAndroidSurfaceKHR(_instance, &surfaceCreateInfo, nullptr, &surface) != VK_SUCCESS) { + if (vkCreateAndroidSurfaceKHR(instance, &surfaceCreateInfo, nullptr, &surface) != VK_SUCCESS) { qCritical() << "Failed to create Vulkan surface on Android."; exit(EXIT_FAILURE); } #endif _surface = surface; qDebug() << "Vulkan surface created successfully."; - + return _surface; } void VulkanWindow::cleanup() { diff --git a/VulkanTutorial1.0/VulkanWindow.h b/VulkanTutorial1.0/VulkanWindow.h index 308395d..ea17230 100644 --- a/VulkanTutorial1.0/VulkanWindow.h +++ b/VulkanTutorial1.0/VulkanWindow.h @@ -26,8 +26,9 @@ public: VulkanWindow(); virtual ~VulkanWindow(); + VkSurfaceKHR createVulkanSurface(VkInstance instance, VkSurfaceKHR& surface); + private: - void createVulkanSurface(); void cleanup(); VkSurfaceKHR _surface; diff --git a/main.cpp b/main.cpp index 113a599..3ea6406 100644 --- a/main.cpp +++ b/main.cpp @@ -9,19 +9,28 @@ #include #include -int main(int /*argc*/, char */*argv*/[]) { +#include -// HelloTriangleApplication app; - ComputeAndGraphics app; +int main(int argc, char *argv[]) { + + QGuiApplication app(argc, argv); + +// HelloTriangleApplication vulkanApp; + ComputeAndGraphics vulkanApp; + VulkanWindow* window = new VulkanWindow(); + vulkanApp.initWindow(window); // VkMain vkMain; try { - app.run(); + window->show(); + vulkanApp.run(); + // vkMain.mainRun(); } catch (const std::exception& e) { std::cerr << e.what() << std::endl; return EXIT_FAILURE; } - return EXIT_SUCCESS; +// return EXIT_SUCCESS; + return app.exec(); }