Browse Source

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.
master
AliMehrabani 7 days ago
parent
commit
8a3eb62d12
  1. 2
      VkTest.pro
  2. 6
      VkTest.pro.user
  3. 55
      VulkanTutorial1.0/ComputeAndGraphics.cpp
  4. 4
      VulkanTutorial1.0/ComputeAndGraphics.h
  5. 11
      VulkanTutorial1.0/VulkanWindow.cpp
  6. 3
      VulkanTutorial1.0/VulkanWindow.h
  7. 19
      main.cpp

2
VkTest.pro

@ -1,4 +1,4 @@
QT += core xml x11extras QT += core xml x11extras gui
CONFIG += c++17 CONFIG += c++17

6
VkTest.pro.user

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.10.1, 2024-11-18T17:14:40. --> <!-- Written by QtCreator 4.10.1, 2024-11-18T18:35:13. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
@ -623,8 +623,8 @@
<valuelist type="QVariantList" key="Android.PreStartShellCmdListKey"/> <valuelist type="QVariantList" key="Android.PreStartShellCmdListKey"/>
<value type="int" key="PE.EnvironmentAspect.Base">0</value> <value type="int" key="PE.EnvironmentAspect.Base">0</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/> <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">VkTest</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">VkTest</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.AndroidRunConfiguration:/home/ali-mehrabani/Qt_projects/VkTest/VkTest.pro</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.AndroidRunConfiguration:/home/ali-mehrabani/Qt_projects/VkTest/VkTest.pro</value>
<value type="QString" key="RunConfiguration.Arguments"></value> <value type="QString" key="RunConfiguration.Arguments"></value>
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value> <value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>

55
VulkanTutorial1.0/ComputeAndGraphics.cpp

@ -46,23 +46,27 @@ ComputeAndGraphics::ComputeAndGraphics()
void ComputeAndGraphics::run() void ComputeAndGraphics::run()
{ {
initWindow(); // initWindow();
initVulkan(); initVulkan();
mainLoop(); 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); // glfwSetWindowUserPointer(_window, this);
// glfwSetFramebufferSizeCallback(_window, framebufferResizeCallback); // glfwSetFramebufferSizeCallback(_window, framebufferResizeCallback);
// window = // VulkanWindow window;
window->resize(800, 600);
_windowQt = window;
} }
//void ComputeAndGraphics::framebufferResizeCallback(GLFWwindow* window, int /*width*/, int /*height*/) { //void ComputeAndGraphics::framebufferResizeCallback(GLFWwindow* window, int /*width*/, int /*height*/) {
@ -236,9 +240,11 @@ void ComputeAndGraphics::setupDebugMessenger() {
void ComputeAndGraphics::createSurface() void ComputeAndGraphics::createSurface()
{ {
if (glfwCreateWindowSurface(_instance, _window, nullptr, &_surface) != VK_SUCCESS) { // if (glfwCreateWindowSurface(_instance, _window, nullptr, &_surface) != VK_SUCCESS) {
throw std::runtime_error("failed to create window surface!"); // throw std::runtime_error("failed to create window surface!");
} // }
_windowQt->createVulkanSurface(_instance, _surface);
} }
void ComputeAndGraphics::pickPhysicalDevice() { void ComputeAndGraphics::pickPhysicalDevice() {
@ -569,7 +575,9 @@ VkExtent2D ComputeAndGraphics::chooseSwapExtent(const VkSurfaceCapabilitiesKHR&
return capabilities.currentExtent; return capabilities.currentExtent;
} else { } else {
int width, height; int width, height;
glfwGetFramebufferSize(_window, &width, &height); // glfwGetFramebufferSize(_window, &width, &height);
width = 800;
height = 600;
VkExtent2D actualExtent = { VkExtent2D actualExtent = {
static_cast<uint32_t>(width), static_cast<uint32_t>(width),
@ -1874,11 +1882,16 @@ void ComputeAndGraphics::createSyncObjects() {
void ComputeAndGraphics::mainLoop() void ComputeAndGraphics::mainLoop()
{ {
while (!glfwWindowShouldClose(_window)) { // while (!glfwWindowShouldClose(_window)) {
glfwPollEvents(); // glfwPollEvents();
// drawFrame();
// }
while (true) {
drawFrame(); drawFrame();
} }
vkDeviceWaitIdle(_device); vkDeviceWaitIdle(_device);
} }
@ -2151,12 +2164,12 @@ void ComputeAndGraphics::updateComputeUniformBuffer(uint32_t currentImage)
} }
void ComputeAndGraphics::recreateSwapChain() { void ComputeAndGraphics::recreateSwapChain() {
int width = 0, height = 0; int width = 800, height = 600;
glfwGetFramebufferSize(_window, &width, &height); // glfwGetFramebufferSize(_window, &width, &height);
while (width == 0 || height == 0) { // while (width == 0 || height == 0) {
glfwGetFramebufferSize(_window, &width, &height); // glfwGetFramebufferSize(_window, &width, &height);
glfwWaitEvents(); // glfwWaitEvents();
} // }
vkDeviceWaitIdle(_device); vkDeviceWaitIdle(_device);
@ -2248,9 +2261,9 @@ void ComputeAndGraphics::cleanBase()
vkDestroyInstance(_instance, nullptr); vkDestroyInstance(_instance, nullptr);
glfwDestroyWindow(_window); // glfwDestroyWindow(_window);
glfwTerminate(); // glfwTerminate();
} }
void ComputeAndGraphics::cleanDescriptors() void ComputeAndGraphics::cleanDescriptors()

4
VulkanTutorial1.0/ComputeAndGraphics.h

@ -90,6 +90,7 @@ class ComputeAndGraphics
public: public:
ComputeAndGraphics(); ComputeAndGraphics();
void run(); void run();
void initWindow(VulkanWindow* window);
// Message Callback // Message Callback
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT /*messageSeverity*/, static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT /*messageSeverity*/,
@ -105,7 +106,6 @@ public:
// static void framebufferResizeCallback(QQuickWindow* window, int width, int height); // static void framebufferResizeCallback(QQuickWindow* window, int width, int height);
private: private:
void initWindow();
void initVulkan(); void initVulkan();
void mainLoop(); void mainLoop();
void cleanup(); void cleanup();
@ -316,7 +316,7 @@ private:
bool framebufferResized = false; bool framebufferResized = false;
GLFWwindow *_window; GLFWwindow *_window;
// VulkanWindow _window; VulkanWindow* _windowQt;
VkInstance _instance; VkInstance _instance;
VkDebugUtilsMessengerEXT _debugMessenger; VkDebugUtilsMessengerEXT _debugMessenger;
VkPhysicalDevice _physicalDevice; VkPhysicalDevice _physicalDevice;

11
VulkanTutorial1.0/VulkanWindow.cpp

@ -7,15 +7,14 @@ VulkanWindow::VulkanWindow()
// qCritical() << "Failed to initialize Vulkan instance!"; // qCritical() << "Failed to initialize Vulkan instance!";
// exit(EXIT_FAILURE); // exit(EXIT_FAILURE);
// } // }
createVulkanSurface(); // createVulkanSurface();
} }
VulkanWindow::~VulkanWindow() { VulkanWindow::~VulkanWindow() {
// cleanup(); // cleanup();
} }
void VulkanWindow::createVulkanSurface() { VkSurfaceKHR VulkanWindow::createVulkanSurface(VkInstance instance, VkSurfaceKHR& surface) {
VkSurfaceKHR surface;
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
VkXcbSurfaceCreateInfoKHR surfaceCreateInfo = {}; VkXcbSurfaceCreateInfoKHR surfaceCreateInfo = {};
@ -23,7 +22,7 @@ void VulkanWindow::createVulkanSurface() {
surfaceCreateInfo.connection = QX11Info::connection(); surfaceCreateInfo.connection = QX11Info::connection();
surfaceCreateInfo.window = static_cast<unsigned int>(winId()); surfaceCreateInfo.window = static_cast<unsigned int>(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."; qCritical() << "Failed to create Vulkan surface on Linux.";
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -32,14 +31,14 @@ void VulkanWindow::createVulkanSurface() {
surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
surfaceCreateInfo.window = reinterpret_cast<ANativeWindow*>(winId()); surfaceCreateInfo.window = reinterpret_cast<ANativeWindow*>(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."; qCritical() << "Failed to create Vulkan surface on Android.";
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
#endif #endif
_surface = surface; _surface = surface;
qDebug() << "Vulkan surface created successfully."; qDebug() << "Vulkan surface created successfully.";
return _surface;
} }
void VulkanWindow::cleanup() { void VulkanWindow::cleanup() {

3
VulkanTutorial1.0/VulkanWindow.h

@ -26,8 +26,9 @@ public:
VulkanWindow(); VulkanWindow();
virtual ~VulkanWindow(); virtual ~VulkanWindow();
VkSurfaceKHR createVulkanSurface(VkInstance instance, VkSurfaceKHR& surface);
private: private:
void createVulkanSurface();
void cleanup(); void cleanup();
VkSurfaceKHR _surface; VkSurfaceKHR _surface;

19
main.cpp

@ -9,19 +9,28 @@
#include <stdexcept> #include <stdexcept>
#include <cstdlib> #include <cstdlib>
int main(int /*argc*/, char */*argv*/[]) { #include<QGuiApplication>
// HelloTriangleApplication app; int main(int argc, char *argv[]) {
ComputeAndGraphics app;
QGuiApplication app(argc, argv);
// HelloTriangleApplication vulkanApp;
ComputeAndGraphics vulkanApp;
VulkanWindow* window = new VulkanWindow();
vulkanApp.initWindow(window);
// VkMain vkMain; // VkMain vkMain;
try { try {
app.run(); window->show();
vulkanApp.run();
// vkMain.mainRun(); // vkMain.mainRun();
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
return EXIT_SUCCESS; // return EXIT_SUCCESS;
return app.exec();
} }

Loading…
Cancel
Save