From 181af9c9ea489d155076c37275bd66fd7dad112f Mon Sep 17 00:00:00 2001 From: AliMehrabani Date: Tue, 19 Nov 2024 16:39:03 +0330 Subject: [PATCH] update on adding android It works for linux right now. Be careful about the order of including XCB related headers. --- VkTest.pro | 43 +++++++---- VkTest.pro.user | 8 +- VulkanTutorial1.0/ComputeAndGraphics.cpp | 94 +++++++++++++----------- VulkanTutorial1.0/ComputeAndGraphics.h | 6 +- VulkanTutorial1.0/Types.h | 23 +++--- VulkanTutorial1.0/VulkanWindow.cpp | 27 ++++--- VulkanTutorial1.0/VulkanWindow.h | 14 ++-- 7 files changed, 120 insertions(+), 95 deletions(-) diff --git a/VkTest.pro b/VkTest.pro index 3fc1a47..b56d1f2 100644 --- a/VkTest.pro +++ b/VkTest.pro @@ -1,18 +1,23 @@ -QT += core xml x11extras gui +QT += core xml gui CONFIG += c++17 -INCLUDEPATH += /usr/include/vulkan \ +QMAKE_SPEC_T = $$[QMAKE_SPEC] + +INCLUDEPATH += \ +# /usr/include/vulkan \ $$PWD/third_party \ - $$PWD/third_party/vkbootstrap \ - $$PWD/third_party/vkbootstrap/build \ - $$PWD/third_party/fmt/include \ - $$PWD/third_party/SDL/build/include/SDL2 \ - $$PWD/third_party/SDL/build/include-config-/SDL2 \ - $$PWD/third_party/fmt/build - -LIBS += -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi \ - -L$$PWD/third_party/pugixml/build -lpugixml \ +# $$PWD/third_party/vkbootstrap \ +# $$PWD/third_party/vkbootstrap/build \ +# $$PWD/third_party/fmt/include \ +# $$PWD/third_party/SDL/build/include/SDL2 \ +# $$PWD/third_party/SDL/build/include-config-/SDL2 \ +# $$PWD/third_party/fmt/build + +LIBS += \ + -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi \ + -lvulkan \ +# -L$$PWD/third_party/pugixml/build -lpugixml \ # -lSDL2main -lSDL2 -lvk-bootstrap \ # -L$$PWD/third_party/fmt/build -lfmt @@ -21,13 +26,13 @@ SOURCES += \ VulkanTutorial1.0/VulkanWindow.cpp \ main.cpp -HEADERS += \ \ +HEADERS += \ VulkanTutorial1.0/ComputeAndGraphics.h \ VulkanTutorial1.0/Types.h \ VulkanTutorial1.0/VulkanWindow.h -DISTFILES += \ \ +DISTFILES += \ shaders/VulkanTutorial1.0/ComputeAndGraphics/compComp.spv \ shaders/VulkanTutorial1.0/ComputeAndGraphics/compile.sh \ shaders/VulkanTutorial1.0/ComputeAndGraphics/fragComp.spv \ @@ -42,10 +47,9 @@ DISTFILES += \ \ shaders/VulkanTutorial1.0/HelloTriangle/vert.spv -contains(ANDROID_TARGET_ARCH,arm64-v8a) { +contains(QMAKE_SPEC_T,.*android.*) { QT += - ANDROID_PACKAGE_SOURCE_DIR = \ - $$PWD/android + ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android OTHER_FILES += android/src/Vulkan \ android/AndroidManifest.xml \ android/build.gradle \ @@ -55,3 +59,10 @@ contains(ANDROID_TARGET_ARCH,arm64-v8a) { android/gradlew.bat \ android/res/values/libs.xml } +contains(QMAKE_SPEC_T,.*linux.*) { + QT += x11extras + + INCLUDEPATH += \ + /usr/include/vulkan \ +} + diff --git a/VkTest.pro.user b/VkTest.pro.user index ae09adb..5704080 100644 --- a/VkTest.pro.user +++ b/VkTest.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -8,7 +8,7 @@ ProjectExplorer.Project.ActiveTarget - 0 + 1 ProjectExplorer.Project.EditorSettings @@ -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 e8626cc..aae9fcd 100644 --- a/VulkanTutorial1.0/ComputeAndGraphics.cpp +++ b/VulkanTutorial1.0/ComputeAndGraphics.cpp @@ -146,10 +146,10 @@ VkApplicationInfo ComputeAndGraphics::createInstanceAppInfo() VkApplicationInfo appInfo{}; appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; appInfo.pApplicationName = "Vulkan App"; - appInfo.applicationVersion = VK_MAKE_VERSION(1, 3, 0); + appInfo.applicationVersion = VK_MAKE_VERSION(1, 1, 0); appInfo.pEngineName = "No Engine"; - appInfo.engineVersion = VK_MAKE_VERSION(1, 3, 0); - appInfo.apiVersion = VK_API_VERSION_1_3; + appInfo.engineVersion = VK_MAKE_VERSION(1, 1, 0); + appInfo.apiVersion = VK_API_VERSION_1_1; return appInfo; } @@ -211,10 +211,11 @@ std::vector ComputeAndGraphics::getRequiredExtensions() std::vector extensions; extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); -#ifdef Q_OS_LINUX - extensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME); -#elif Q_OS_ANDROID + +#if defined(Q_OS_ANDROID) extensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME); +#elif defined(Q_OS_LINUX) + extensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME); #endif if (enableValidationLayers) { @@ -387,11 +388,11 @@ void ComputeAndGraphics::createLogicalDevice() VkPhysicalDeviceFeatures deviceFeatures = createDeviceCoreFeatures(); - VkPhysicalDeviceVulkan12Features deviceFeatures12 = createDeviceVulkan12Features(nullptr); +// VkPhysicalDeviceVulkan12Features deviceFeatures12 = createDeviceVulkan12Features(nullptr); // VkPhysicalDevice8BitStorageFeaturesKHR device8BitStorageFeatures2Info = createDevice8BitStorageFeatures2Info(); // VkPhysicalDeviceFeatures2 deviceCoreFeatures2 = createDeviceCoreFeatures2(&device8BitStorageFeatures2Info); - VkDeviceCreateInfo createInfo = createLogicalDeviceInfo(queueCreateInfos, &deviceFeatures, &deviceFeatures12); + VkDeviceCreateInfo createInfo = createLogicalDeviceInfo(queueCreateInfos, &deviceFeatures, nullptr); if (vkCreateDevice(_physicalDevice, &createInfo, nullptr, &_device) != VK_SUCCESS) { throw std::runtime_error("failed to create logical device!"); @@ -420,48 +421,48 @@ VkPhysicalDeviceFeatures ComputeAndGraphics::createDeviceCoreFeatures() return deviceFeatures; } -VkPhysicalDeviceVulkan12Features ComputeAndGraphics::createDeviceVulkan12Features(void* pNext) -{ - VkPhysicalDeviceVulkan12Features deviceFeatures12{}; - deviceFeatures12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES; - deviceFeatures12.scalarBlockLayout = VK_TRUE; - deviceFeatures12.shaderInt8 = VK_TRUE; - deviceFeatures12.uniformAndStorageBuffer8BitAccess = VK_TRUE; -// deviceFeatures12.storageBuffer8BitAccess = VK_TRUE; -// deviceFeatures12.storagePushConstant8 = VK_TRUE; - deviceFeatures12.pNext = pNext; +//VkPhysicalDeviceVulkan12Features ComputeAndGraphics::createDeviceVulkan12Features(void* pNext) +//{ +// VkPhysicalDeviceVulkan12Features deviceFeatures12{}; +// deviceFeatures12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES; +// deviceFeatures12.scalarBlockLayout = VK_TRUE; +// deviceFeatures12.shaderInt8 = VK_TRUE; +// deviceFeatures12.uniformAndStorageBuffer8BitAccess = VK_TRUE; +//// deviceFeatures12.storageBuffer8BitAccess = VK_TRUE; +//// deviceFeatures12.storagePushConstant8 = VK_TRUE; +// deviceFeatures12.pNext = pNext; - return deviceFeatures12; -} +// return deviceFeatures12; +//} -VkPhysicalDeviceVulkan13Features ComputeAndGraphics::createDeviceVulkan13Features(void* pNext) -{ - VkPhysicalDeviceVulkan13Features deviceFeatures13{}; - deviceFeatures13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES; - deviceFeatures13.pNext = pNext; +//VkPhysicalDeviceVulkan13Features ComputeAndGraphics::createDeviceVulkan13Features(void* pNext) +//{ +// VkPhysicalDeviceVulkan13Features deviceFeatures13{}; +// deviceFeatures13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES; +// deviceFeatures13.pNext = pNext; - return deviceFeatures13; -} +// return deviceFeatures13; +//} -VkPhysicalDeviceFeatures2 ComputeAndGraphics::createDeviceCoreFeatures2(VkPhysicalDevice8BitStorageFeatures* pNext) -{ - VkPhysicalDeviceFeatures2 deviceFeatures2{}; - deviceFeatures2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; - deviceFeatures2.pNext = pNext; +//VkPhysicalDeviceFeatures2 ComputeAndGraphics::createDeviceCoreFeatures2(VkPhysicalDevice8BitStorageFeatures* pNext) +//{ +// VkPhysicalDeviceFeatures2 deviceFeatures2{}; +// deviceFeatures2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; +// deviceFeatures2.pNext = pNext; - return deviceFeatures2; -} +// return deviceFeatures2; +//} -VkPhysicalDevice8BitStorageFeaturesKHR ComputeAndGraphics::createDevice8BitStorageFeatures2Info() -{ - VkPhysicalDevice8BitStorageFeatures storage8BitFeatures{}; - storage8BitFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES; - storage8BitFeatures.uniformAndStorageBuffer8BitAccess = VK_TRUE; - storage8BitFeatures.storagePushConstant8 = VK_TRUE; - storage8BitFeatures.storageBuffer8BitAccess = VK_TRUE; +//VkPhysicalDevice8BitStorageFeaturesKHR ComputeAndGraphics::createDevice8BitStorageFeatures2Info() +//{ +// VkPhysicalDevice8BitStorageFeatures storage8BitFeatures{}; +// storage8BitFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES; +// storage8BitFeatures.uniformAndStorageBuffer8BitAccess = VK_TRUE; +// storage8BitFeatures.storagePushConstant8 = VK_TRUE; +// storage8BitFeatures.storageBuffer8BitAccess = VK_TRUE; - return storage8BitFeatures; -} +// return storage8BitFeatures; +//} VkDeviceCreateInfo ComputeAndGraphics::createLogicalDeviceInfo(std::vector& queueCreateInfos , VkPhysicalDeviceFeatures* deviceFeatures, void* pNext) @@ -2164,7 +2165,12 @@ void ComputeAndGraphics::updateComputeUniformBuffer(uint32_t currentImage) } void ComputeAndGraphics::recreateSwapChain() { - int width = 800, height = 600; + int width = 0, height = 0; + _windowQt->getWindowSize(width, height); + while (width == 0 || height == 0) { + _windowQt->getWindowSize(width, height); + } + // glfwGetFramebufferSize(_window, &width, &height); // while (width == 0 || height == 0) { // glfwGetFramebufferSize(_window, &width, &height); diff --git a/VulkanTutorial1.0/ComputeAndGraphics.h b/VulkanTutorial1.0/ComputeAndGraphics.h index 5822185..3bd77c0 100644 --- a/VulkanTutorial1.0/ComputeAndGraphics.h +++ b/VulkanTutorial1.0/ComputeAndGraphics.h @@ -200,8 +200,8 @@ private: VkDeviceQueueCreateInfo createQueueInfo(uint32_t queueFamily, uint32_t queueCount, float* queuePriority); VkPhysicalDeviceFeatures createDeviceCoreFeatures(); - VkPhysicalDeviceVulkan12Features createDeviceVulkan12Features(void* pNext); - VkPhysicalDeviceVulkan13Features createDeviceVulkan13Features(void* pNext); +// VkPhysicalDeviceVulkan12Features createDeviceVulkan12Features(void* pNext); +// VkPhysicalDeviceVulkan13Features createDeviceVulkan13Features(void* pNext); VkPhysicalDeviceFeatures2KHR createDeviceCoreFeatures2(VkPhysicalDevice8BitStorageFeaturesKHR* pNext); VkPhysicalDevice8BitStorageFeaturesKHR createDevice8BitStorageFeatures2Info(); VkDeviceCreateInfo createLogicalDeviceInfo(std::vector& queueCreateInfos, VkPhysicalDeviceFeatures* deviceFeatures @@ -315,7 +315,7 @@ private: uint32_t currentFrame = 0; bool framebufferResized = false; - GLFWwindow *_window; +// GLFWwindow *_window; VulkanWindow* _windowQt; VkInstance _instance; VkDebugUtilsMessengerEXT _debugMessenger; diff --git a/VulkanTutorial1.0/Types.h b/VulkanTutorial1.0/Types.h index d7578be..46db0ed 100644 --- a/VulkanTutorial1.0/Types.h +++ b/VulkanTutorial1.0/Types.h @@ -3,16 +3,23 @@ #pragma once +#if defined(Q_OS_ANDROID) +#define VK_USE_PLATFORM_ANDROID_KHR // Enable Vulkan support for Android +#elif defined(Q_OS_LINUX) +#define VK_USE_PLATFORM_XCB_KHR // Enable Vulkan support for Linux (XCB) +#include +#endif + + + #include #include -#pragma once - //#define VK_USE_PLATFORM_ -#define GLFW_INCLUDE_VULKAN -#include +//#define GLFW_INCLUDE_VULKAN +//#include //#define GLFW_EXPOSE_NATIVE_ -#include +//#include #define GLM_FORCE_RADIANS //#define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES @@ -25,12 +32,6 @@ #include -#ifdef Q_OS_ANDROID - const QString TARGET_PLATFORM = "Android"; -#else - const QString TARGET_PLATFORM = "Linux"; -#endif - struct SwapChainSupportDetails { VkSurfaceCapabilitiesKHR capabilities; std::vector formats; diff --git a/VulkanTutorial1.0/VulkanWindow.cpp b/VulkanTutorial1.0/VulkanWindow.cpp index aa6deb3..774d1cb 100644 --- a/VulkanTutorial1.0/VulkanWindow.cpp +++ b/VulkanTutorial1.0/VulkanWindow.cpp @@ -16,17 +16,8 @@ VulkanWindow::~VulkanWindow() { VkSurfaceKHR VulkanWindow::createVulkanSurface(VkInstance instance, VkSurfaceKHR& surface) { -#ifdef Q_OS_LINUX - VkXcbSurfaceCreateInfoKHR surfaceCreateInfo = {}; - surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; - surfaceCreateInfo.connection = QX11Info::connection(); - surfaceCreateInfo.window = static_cast(winId()); - if (vkCreateXcbSurfaceKHR(instance, &surfaceCreateInfo, nullptr, &surface) != VK_SUCCESS) { - qCritical() << "Failed to create Vulkan surface on Linux."; - exit(EXIT_FAILURE); - } -#elif Q_OS_ANDROID +#if defined(Q_OS_ANDROID) VkAndroidSurfaceCreateInfoKHR surfaceCreateInfo = {}; surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; surfaceCreateInfo.window = reinterpret_cast(winId()); @@ -35,12 +26,28 @@ VkSurfaceKHR VulkanWindow::createVulkanSurface(VkInstance instance, VkSurfaceKHR qCritical() << "Failed to create Vulkan surface on Android."; exit(EXIT_FAILURE); } +#elif defined(Q_OS_LINUX) + VkXcbSurfaceCreateInfoKHR surfaceCreateInfo = {}; + surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; + surfaceCreateInfo.connection = QX11Info::connection(); + surfaceCreateInfo.window = static_cast(winId()); + + if (vkCreateXcbSurfaceKHR(instance, &surfaceCreateInfo, nullptr, &surface) != VK_SUCCESS) { + qCritical() << "Failed to create Vulkan surface on Linux."; + exit(EXIT_FAILURE); + } #endif _surface = surface; qDebug() << "Vulkan surface created successfully."; return _surface; } +void VulkanWindow::getWindowSize(int &width, int &height) +{ + width = this->width(); + height = this->height(); +} + void VulkanWindow::cleanup() { if (_surface != VK_NULL_HANDLE) { vkDestroySurfaceKHR(_instance, _surface, nullptr); diff --git a/VulkanTutorial1.0/VulkanWindow.h b/VulkanTutorial1.0/VulkanWindow.h index ea17230..6f12feb 100644 --- a/VulkanTutorial1.0/VulkanWindow.h +++ b/VulkanTutorial1.0/VulkanWindow.h @@ -4,20 +4,19 @@ #include #include #include -#include #include "Types.h" -#ifdef Q_OS_ANDROID +#if defined(Q_OS_ANDROID) +#define VK_USE_PLATFORM_ANDROID_KHR // Enable Vulkan support for Android #include -#else +#elif defined(Q_OS_LINUX) +#include +#define VK_USE_PLATFORM_XCB_KHR // Enable Vulkan support for Linux (XCB) #include +#include #endif - -#define VK_USE_PLATFORM_XCB_KHR // Enable Vulkan support for Linux (XCB) -#define VK_USE_PLATFORM_ANDROID_KHR // Enable Vulkan support for Android - class VulkanWindow : public QWindow { Q_OBJECT @@ -27,6 +26,7 @@ public: virtual ~VulkanWindow(); VkSurfaceKHR createVulkanSurface(VkInstance instance, VkSurfaceKHR& surface); + void getWindowSize(int& width, int& height); private: void cleanup();