完成physicalDevice related 重构
parent
fba7cd0bae
commit
1d6521570e
|
@ -2022,7 +2022,6 @@ void VulkanExampleBase::handleMouseMove(int32_t x, int32_t y)
|
||||||
void VulkanExampleBase::initSwapchain()
|
void VulkanExampleBase::initSwapchain()
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
||||||
swapChain.initSurface(windowInstance, window);
|
swapChain.initSurface(windowInstance, window);
|
||||||
#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
|
#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
|
||||||
swapChain.initSurface(androidApp->window);
|
swapChain.initSurface(androidApp->window);
|
||||||
|
|
|
@ -34,7 +34,7 @@ function(buildHomework HOMEWORK_NAME)
|
||||||
"render/glTFModel.h"
|
"render/glTFModel.h"
|
||||||
"render/glTFModel.cpp"
|
"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})
|
target_link_libraries(${HOMEWORK_NAME} base ${Vulkan_LIBRARY} ${WINLIBS})
|
||||||
else(WIN32)
|
else(WIN32)
|
||||||
add_executable(${HOMEWORK_NAME} ${MAIN_CPP} ${SOURCE} ${MAIN_HEADER} ${SHADERS_GLSL} ${SHADERS_HLSL} ${README_FILES})
|
add_executable(${HOMEWORK_NAME} ${MAIN_CPP} ${SOURCE} ${MAIN_HEADER} ${SHADERS_GLSL} ${SHADERS_HLSL} ${README_FILES})
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
#include "renderFoundation.h"
|
|
||||||
|
|
||||||
inline void VulkanBackend::VulkanFondation::pickPhysicalDevice()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <vulkan/vulkan.h>
|
|
||||||
#include "GLFW/glfw3.h"
|
|
||||||
|
|
||||||
namespace VulkanBackend
|
|
||||||
{
|
|
||||||
class VulkanFondation
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
VulkanFondation();
|
|
||||||
~VulkanFondation();
|
|
||||||
|
|
||||||
|
|
||||||
VkInstance instance;
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
|
||||||
void createSurface();
|
|
||||||
|
|
||||||
void pickPhysicalDevice();
|
|
||||||
};
|
|
||||||
|
|
||||||
VulkanFondation::VulkanFondation()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
VulkanFondation::~VulkanFondation()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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<VkPhysicalDevice> 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<VkQueueFamilyProperties> 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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vulkan/vulkan.h>
|
||||||
|
#include "GLFW/glfw3.h"
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <vector>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
#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<uint32_t> graphicsFamily;
|
||||||
|
std::optional<uint32_t> presentFamily;
|
||||||
|
|
||||||
|
bool isComplete() {
|
||||||
|
return graphicsFamily.has_value() && presentFamily.has_value();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SwapChainSupportDetails
|
||||||
|
{
|
||||||
|
VkSurfaceCapabilitiesKHR capabilities;
|
||||||
|
std::vector<VkSurfaceFormatKHR> formats;
|
||||||
|
std::vector<VkPresentModeKHR> 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()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue