Browse Source

update on adding android

It works for linux right now. Be careful about the order of including XCB related headers.
master
AliMehrabani 6 days ago
parent
commit
181af9c9ea
  1. 43
      VkTest.pro
  2. 8
      VkTest.pro.user
  3. 94
      VulkanTutorial1.0/ComputeAndGraphics.cpp
  4. 6
      VulkanTutorial1.0/ComputeAndGraphics.h
  5. 23
      VulkanTutorial1.0/Types.h
  6. 27
      VulkanTutorial1.0/VulkanWindow.cpp
  7. 14
      VulkanTutorial1.0/VulkanWindow.h

43
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 \
}

8
VkTest.pro.user

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.10.1, 2024-11-18T18:35:13. -->
<!-- Written by QtCreator 4.10.1, 2024-11-19T11:35:57. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@ -8,7 +8,7 @@
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
<value type="int">0</value>
<value type="int">1</value>
</data>
<data>
<variable>ProjectExplorer.Project.EditorSettings</variable>
@ -623,8 +623,8 @@
<valuelist type="QVariantList" key="Android.PreStartShellCmdListKey"/>
<value type="int" key="PE.EnvironmentAspect.Base">0</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">VkTest</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">VkTest</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="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>

94
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<const char*> ComputeAndGraphics::getRequiredExtensions()
std::vector<const char*> 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<VkDeviceQueueCreateInfo>& 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);

6
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<VkDeviceQueueCreateInfo>& 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;

23
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 <xcb/xcb.h>
#endif
#include <vulkan/vulkan.h>
#include <iostream>
#pragma once
//#define VK_USE_PLATFORM_
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
//#define GLFW_INCLUDE_VULKAN
//#include <GLFW/glfw3.h>
//#define GLFW_EXPOSE_NATIVE_
#include <GLFW/glfw3native.h>
//#include <GLFW/glfw3native.h>
#define GLM_FORCE_RADIANS
//#define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
@ -25,12 +32,6 @@
#include <QObject>
#ifdef Q_OS_ANDROID
const QString TARGET_PLATFORM = "Android";
#else
const QString TARGET_PLATFORM = "Linux";
#endif
struct SwapChainSupportDetails {
VkSurfaceCapabilitiesKHR capabilities;
std::vector<VkSurfaceFormatKHR> formats;

27
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<unsigned int>(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<ANativeWindow*>(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<unsigned int>(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);

14
VulkanTutorial1.0/VulkanWindow.h

@ -4,20 +4,19 @@
#include <QObject>
#include <QWindow>
#include <QDebug>
#include <QX11Info>
#include "Types.h"
#ifdef Q_OS_ANDROID
#if defined(Q_OS_ANDROID)
#define VK_USE_PLATFORM_ANDROID_KHR // Enable Vulkan support for Android
#include <vulkan_android.h>
#else
#elif defined(Q_OS_LINUX)
#include <xcb/xcb.h>
#define VK_USE_PLATFORM_XCB_KHR // Enable Vulkan support for Linux (XCB)
#include <vulkan_xcb.h>
#include <QX11Info>
#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();

Loading…
Cancel
Save