From 1d6521570e3eae74af3b891abd5783fe92bcac24 Mon Sep 17 00:00:00 2001 From: ink-soul Date: Mon, 1 Apr 2024 16:21:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90physicalDevice=20related=20?= =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/vulkanexamplebase.cpp | 1 - src/CMakeLists.txt | 2 +- src/render/renderFoundation.cpp | 6 -- src/render/renderFoundation.h | 32 ---------- src/render/vulkanFoundation.cpp | 104 ++++++++++++++++++++++++++++++++ src/render/vulkanFoundation.h | 68 +++++++++++++++++++++ 6 files changed, 173 insertions(+), 40 deletions(-) delete mode 100644 src/render/renderFoundation.cpp delete mode 100644 src/render/renderFoundation.h create mode 100644 src/render/vulkanFoundation.cpp create mode 100644 src/render/vulkanFoundation.h diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index ef2f868..1f138f8 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -2022,7 +2022,6 @@ void VulkanExampleBase::handleMouseMove(int32_t x, int32_t y) void VulkanExampleBase::initSwapchain() { #if defined(_WIN32) - swapChain.initSurface(windowInstance, window); #elif defined(VK_USE_PLATFORM_ANDROID_KHR) swapChain.initSurface(androidApp->window); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8f3e355..fad9224 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -34,7 +34,7 @@ function(buildHomework HOMEWORK_NAME) "render/glTFModel.h" "render/glTFModel.cpp" - "render/renderFoundation.h" "render/renderFoundation.cpp" "render/renderSetter.h" "render/renderSetter.cpp") + "render/vulkanFoundation.h" "render/vulkanFoundation.cpp" "render/renderSetter.h" "render/renderSetter.cpp") target_link_libraries(${HOMEWORK_NAME} base ${Vulkan_LIBRARY} ${WINLIBS}) else(WIN32) add_executable(${HOMEWORK_NAME} ${MAIN_CPP} ${SOURCE} ${MAIN_HEADER} ${SHADERS_GLSL} ${SHADERS_HLSL} ${README_FILES}) diff --git a/src/render/renderFoundation.cpp b/src/render/renderFoundation.cpp deleted file mode 100644 index b77ecd5..0000000 --- a/src/render/renderFoundation.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "renderFoundation.h" - -inline void VulkanBackend::VulkanFondation::pickPhysicalDevice() -{ - -} \ No newline at end of file diff --git a/src/render/renderFoundation.h b/src/render/renderFoundation.h deleted file mode 100644 index f84a852..0000000 --- a/src/render/renderFoundation.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include -#include "GLFW/glfw3.h" - -namespace VulkanBackend -{ - class VulkanFondation - { - public: - VulkanFondation(); - ~VulkanFondation(); - - - VkInstance instance; - - - private: - void createSurface(); - - void pickPhysicalDevice(); - }; - - VulkanFondation::VulkanFondation() - { - } - - VulkanFondation::~VulkanFondation() - { - } - -} \ No newline at end of file diff --git a/src/render/vulkanFoundation.cpp b/src/render/vulkanFoundation.cpp new file mode 100644 index 0000000..3eef5bb --- /dev/null +++ b/src/render/vulkanFoundation.cpp @@ -0,0 +1,104 @@ +#include "vulkanFoundation.h" + +void VulkanBackend::VulkanFondation::createSurface() +{ + +} + +inline void VulkanBackend::VulkanFondation::pickPhysicalDevice() +{ + + uint32_t deviceCount = 0; + vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr); + if (deviceCount == 0) + { + throw std::runtime_error("failed to find GPUs with Vulkan support"); + + } + std::vector devices(deviceCount); + vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data()); + if (setter.settings.selectedPhysicalDeviceIndex != NULL) + { + physicalDevice = devices[setter.settings.selectedPhysicalDeviceIndex]; + } + + for (const auto& device : devices) + { + if (isDeviceSuitable(device)) + { + physicalDevice = device; + break; + } + + } + if (physicalDevice == VK_NULL_HANDLE) + { + throw std::runtime_error("failed to find a suitable GPU"); + } +} + +bool VulkanBackend::VulkanFondation::isDeviceSuitable(VkPhysicalDevice device) +{ + if (setter.settings.headless) + { + bool extensionsSupported = checkDeviceExtensionSupport(device); + return extensionsSupported; + } + else + { + bool extensionsSupported = checkDeviceExtensionSupport(device); + bool swapChainAdequate = false; + QueueFamilyIndices indices = findQueueFamilies(device); + if (extensionsSupported) + { + SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device); + swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty(); + } + } +} + +bool VulkanBackend::VulkanFondation::checkDeviceExtensionSupport(VkPhysicalDevice device) +{ + return false; +} + +VulkanBackend::VulkanFondation::QueueFamilyIndices VulkanBackend::VulkanFondation::findQueueFamilies(VkPhysicalDevice device) +{ + QueueFamilyIndices indices; + + uint32_t queueFamilyCount = 0; + vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr); + + std::vector queueFamilies(queueFamilyCount); + vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data()); + + VkBool32 presentSupport = false; + + + int i = 0; + vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport); + for (const auto& queueFamily : queueFamilies) + { + if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) { + indices.graphicsFamily = i; + } + if (indices.isComplete()) + { + break; + } + if (presentSupport) + { + indices.presentFamily = i; + } + + i++; + + } + + return indices; +} + +VulkanBackend::VulkanFondation::SwapChainSupportDetails VulkanBackend::VulkanFondation::querySwapChainSupport(VkPhysicalDevice device) +{ + +} diff --git a/src/render/vulkanFoundation.h b/src/render/vulkanFoundation.h new file mode 100644 index 0000000..c710aa3 --- /dev/null +++ b/src/render/vulkanFoundation.h @@ -0,0 +1,68 @@ +#pragma once + +#include +#include "GLFW/glfw3.h" +#include +#include +#include + +#include "renderSetter.h" + +namespace VulkanBackend +{ + class VulkanFondation + { + public: + VulkanFondation(); + ~VulkanFondation(); + + VulkanBackend::Setter setter; + + VkInstance instance; + + + private: + + VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; + VkDevice device; + VkSurfaceKHR surface; + struct QueueFamilyIndices + { + std::optional graphicsFamily; + std::optional presentFamily; + + bool isComplete() { + return graphicsFamily.has_value() && presentFamily.has_value(); + } + }; + + struct SwapChainSupportDetails + { + VkSurfaceCapabilitiesKHR capabilities; + std::vector formats; + std::vector presentModes; + }; + + + + void pickPhysicalDevice(); + + + + void createSurface(); + + bool isDeviceSuitable(VkPhysicalDevice device); + bool checkDeviceExtensionSupport(VkPhysicalDevice device); + VulkanBackend::VulkanFondation::QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device); + VulkanBackend::VulkanFondation::SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device); + }; + + VulkanFondation::VulkanFondation() + { + } + + VulkanFondation::~VulkanFondation() + { + } + +} \ No newline at end of file