From 223f767b0153e70cce12eb58561ea79e1189e16d Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 19 Jun 2019 16:54:41 +0800 Subject: [PATCH] =?UTF-8?q?PhysicalDevice=E5=A2=9E=E5=8A=A0GetDepthFormat/?= =?UTF-8?q?GetDepthStencilFormat/CheckDepthFormat=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=A3=80=E6=B5=8B=E6=A0=BC=E5=BC=8F=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=8F=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/graph/vulkan/VKPhysicalDevice.h | 5 ++ src/RenderDevice/Vulkan/VKPhysicalDevice.cpp | 87 +++++++++++++++++++- 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/inc/hgl/graph/vulkan/VKPhysicalDevice.h b/inc/hgl/graph/vulkan/VKPhysicalDevice.h index 35395ac0..b475bd4d 100644 --- a/inc/hgl/graph/vulkan/VKPhysicalDevice.h +++ b/inc/hgl/graph/vulkan/VKPhysicalDevice.h @@ -70,5 +70,10 @@ public: return fp; } + + VkFormat GetDepthFormat(bool lower_to_high=true)const; + VkFormat GetDepthStencilFormat(bool lower_to_high=true)const; + + bool CheckDepthFormat(const VkFormat)const; };//class PhysicalDevice VK_NAMESPACE_END \ No newline at end of file diff --git a/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp b/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp index f618dbc2..621900d7 100644 --- a/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp +++ b/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp @@ -35,7 +35,7 @@ PhysicalDevice::PhysicalDevice(VkInstance inst,VkPhysicalDevice pd) if(GetExtensionSpecVersion(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME)) GetPhysicalDeviceProperties2=(PFN_vkGetPhysicalDeviceProperties2KHR)vkGetInstanceProcAddr(instance,"vkGetPhysicalDeviceProperties2KHR"); - + if(!GetPhysicalDeviceProperties2) if(GetExtensionSpecVersion(VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME)) GetPhysicalDeviceProperties2=(PFN_vkGetPhysicalDeviceProperties2)vkGetInstanceProcAddr(instance,"vkGetPhysicalDeviceProperties2"); @@ -91,4 +91,89 @@ const bool PhysicalDevice::CheckMemoryType(uint32_t typeBits,VkFlags requirement // No memory types matched, return failure return false; } +VkFormat PhysicalDevice::GetDepthFormat(bool lower_to_high)const +{ + constexpr VkFormat depthFormats[] = + { + VK_FORMAT_D16_UNORM, + VK_FORMAT_D24_UNORM_S8_UINT, + VK_FORMAT_D32_SFLOAT, + VK_FORMAT_D32_SFLOAT_S8_UINT + }; + + VkFormat result=VK_FORMAT_UNDEFINED; + + for (auto& format : depthFormats) + { + VkFormatProperties formatProps; + + vkGetPhysicalDeviceFormatProperties(physical_device, format, &formatProps); + + if (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) + { + if(lower_to_high) + return format; + else + result=format; + } + } + + return result; +} + +VkFormat PhysicalDevice::GetDepthStencilFormat(bool lower_to_high)const +{ + constexpr VkFormat depthStencilFormats[] = + { + VK_FORMAT_D16_UNORM_S8_UINT, + VK_FORMAT_D24_UNORM_S8_UINT, + VK_FORMAT_D32_SFLOAT_S8_UINT + }; + + VkFormat result=VK_FORMAT_UNDEFINED; + + for (auto& format : depthStencilFormats) + { + VkFormatProperties formatProps; + + vkGetPhysicalDeviceFormatProperties(physical_device, format, &formatProps); + + if (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) + { + if(lower_to_high) + return format; + else + result=format; + } + } + + return result; +} + +bool PhysicalDevice::CheckDepthFormat(const VkFormat)const +{ + constexpr VkFormat depthFormats[] = + { + VK_FORMAT_D16_UNORM, + VK_FORMAT_X8_D24_UNORM_PACK32, + VK_FORMAT_D32_SFLOAT, + VK_FORMAT_D16_UNORM_S8_UINT, + VK_FORMAT_D24_UNORM_S8_UINT, + VK_FORMAT_D32_SFLOAT_S8_UINT, + }; + + VkFormat result=VK_FORMAT_UNDEFINED; + + for (auto& format : depthFormats) + { + VkFormatProperties formatProps; + + vkGetPhysicalDeviceFormatProperties(physical_device, format, &formatProps); + + if (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) + return(true); + } + + return false; +} VK_NAMESPACE_END \ No newline at end of file