diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index 483b6905..3cd00c99 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -175,8 +175,8 @@ public: //Command Buffer 相关 const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); -// bool CreateColorAttachment( List &ref_list,List &desc_list,const List &color_format,const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); -// bool CreateDepthAttachment( List &ref_list,List &desc_list,const List &depth_format,const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + bool CreateColorAttachment( List &ref_list,List &desc_list,const List &color_format,const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + bool CreateDepthAttachment( List &ref_list,List &desc_list,const VkFormat &depth_format,const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); RenderPass * CreateRenderPass( const List &desc_list, const List &subpass, diff --git a/inc/hgl/graph/vulkan/VKPhysicalDevice.h b/inc/hgl/graph/vulkan/VKPhysicalDevice.h index 3b027409..f139c439 100644 --- a/inc/hgl/graph/vulkan/VKPhysicalDevice.h +++ b/inc/hgl/graph/vulkan/VKPhysicalDevice.h @@ -2,6 +2,7 @@ #include #include +#include VK_NAMESPACE_BEGIN class PhysicalDevice @@ -15,6 +16,15 @@ class PhysicalDevice List layer_properties; List extension_properties; + Set optimal_color_format; + Set optimal_depth_format; + Set linear_color_format; + Set linear_depth_format; + Set buffer_color_format; + Set buffer_depth_format; + + void InitFormatSupport(); + public: PhysicalDevice(VkInstance,VkPhysicalDevice); @@ -71,9 +81,14 @@ public: return fp; } + bool IsOptimalColorFormat(const VkFormat format)const{return optimal_color_format.IsMember(format);} + bool IsOptimalDepthFormat(const VkFormat format)const{return optimal_depth_format.IsMember(format);} + bool IsLinearColorFormat(const VkFormat format)const{return linear_color_format.IsMember(format);} + bool IsLinearDepthFormat(const VkFormat format)const{return linear_depth_format.IsMember(format);} + bool IsBufferColorFormat(const VkFormat format)const{return buffer_color_format.IsMember(format);} + bool IsBufferDepthFormat(const VkFormat format)const{return buffer_depth_format.IsMember(format);} + 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/VKDeviceRenderPass.cpp b/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp index 6c4c3f5f..2a66b78b 100644 --- a/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp @@ -64,9 +64,6 @@ void Device::CreateSubpassDependency(List &subpass_dependen bool Device::CreateAttachment(List &ref_list,List &desc_list,const List &color_format,const VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout) { - if(!attr->physical_device->CheckDepthFormat(depth_format)) - return(false); - uint atta_count=color_format.GetCount(); desc_list.SetCount(atta_count+1); @@ -106,6 +103,74 @@ bool Device::CreateAttachment(List &ref_list,List &ref_list,List &desc_list,const List &color_format,const VkImageLayout color_final_layout) +{ + const VkFormat *cf=color_format.GetData(); + + for(uint i=0;iphysical_device->IsOptimalColorFormat(*cf)) + return(false); + + ++cf; + } + + ref_list.SetCount(color_format.GetCount()); + VkAttachmentReference *ref=ref_list.GetData(); + + desc_list.SetCount(color_format.GetCount()); + VkAttachmentDescription *desc=desc_list.GetData(); + + for(uint i=0;iflags = 0; + desc->samples = VK_SAMPLE_COUNT_1_BIT; + desc->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; //LOAD_OP_CLEAR代表LOAD时清空内容 + desc->storeOp = VK_ATTACHMENT_STORE_OP_STORE; //STORE_OP_STROE代表SOTRE时储存内容 + desc->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; //DONT CARE表示不在意 + desc->stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + desc->initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; //代表不关心初始布局 + desc->finalLayout = color_final_layout; + ++desc; + + ref->attachment = i; + ref->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + ++ref; + } + + return(true); +} + +bool Device::CreateDepthAttachment( List &ref_list,List &desc_list,const VkFormat &depth_format,const VkImageLayout depth_final_layout) +{ + if(!attr->physical_device->IsOptimalDepthFormat(depth_format)) + return(false); + + { + ref_list.SetCount(1); + VkAttachmentReference *ref=ref_list.GetData(); + + ref->attachment=0; + ref->layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + } + + { + desc_list.SetCount(1); + VkAttachmentDescription *desc=desc_list.GetData(); + + desc->flags = 0; + desc->samples = VK_SAMPLE_COUNT_1_BIT; + desc->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; //LOAD_OP_CLEAR代表LOAD时清空内容 + desc->storeOp = VK_ATTACHMENT_STORE_OP_STORE; //STORE_OP_STROE代表SOTRE时储存内容 + desc->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; //DONT CARE表示不在意 + desc->stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + desc->initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; //代表不关心初始布局 + desc->finalLayout = depth_final_layout; + } + + return(true); +} + RenderPass *Device::CreateRenderPass( const List &desc_list, const List &subpass, const List &dependency, @@ -114,6 +179,20 @@ RenderPass *Device::CreateRenderPass( const List &des const VkImageLayout color_final_layout, const VkImageLayout depth_final_layout) { + { + const VkFormat *cf=color_format.GetData(); + + for(uint i=0;iphysical_device->IsOptimalColorFormat(*cf)) + return(false); + + ++cf; + } + } + + if(!attr->physical_device->IsOptimalDepthFormat(depth_format)) + return(false); VkRenderPassCreateInfo rp_info; rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; @@ -135,6 +214,12 @@ RenderPass *Device::CreateRenderPass( const List &des RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout) { + if(!attr->physical_device->IsOptimalColorFormat(color_format)) + return(false); + + if(!attr->physical_device->IsOptimalDepthFormat(depth_format)) + return(false); + List ref_list; List desc_list; diff --git a/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp b/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp index 07cb4fa9..0204c4e1 100644 --- a/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp +++ b/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp @@ -56,6 +56,8 @@ PhysicalDevice::PhysicalDevice(VkInstance inst,VkPhysicalDevice pd) hgl_zero(driver_properties); } + + InitFormatSupport(); } const uint32_t PhysicalDevice::GetExtensionSpecVersion(const UTF8String &name)const @@ -91,6 +93,27 @@ const bool PhysicalDevice::CheckMemoryType(uint32_t typeBits,VkMemoryPropertyFla // No memory types matched, return failure return false; } + +void PhysicalDevice::InitFormatSupport() +{ + VkFormatProperties formatProps; + + for(uint32 i=VK_FORMAT_BEGIN_RANGE;i<=VK_FORMAT_END_RANGE;i++) + { + hgl_zero(formatProps); + + vkGetPhysicalDeviceFormatProperties(physical_device,(VkFormat)i,&formatProps); + + if(formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT )optimal_color_format.Add((VkFormat)i); + if(formatProps.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT )linear_color_format.Add((VkFormat)i); + if(formatProps.bufferFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT )buffer_color_format.Add((VkFormat)i); + + if(formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)optimal_depth_format.Add((VkFormat)i); + if(formatProps.linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)linear_depth_format.Add((VkFormat)i); + if(formatProps.bufferFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)buffer_depth_format.Add((VkFormat)i); + } +} + VkFormat PhysicalDevice::GetDepthFormat(bool lower_to_high)const { constexpr VkFormat depthFormats[] = @@ -105,11 +128,7 @@ VkFormat PhysicalDevice::GetDepthFormat(bool lower_to_high)const for (auto& format : depthFormats) { - VkFormatProperties formatProps; - - vkGetPhysicalDeviceFormatProperties(physical_device, format, &formatProps); - - if (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) + if(IsOptimalDepthFormat(format)) { if(lower_to_high) return format; @@ -134,11 +153,7 @@ VkFormat PhysicalDevice::GetDepthStencilFormat(bool lower_to_high)const for (auto& format : depthStencilFormats) { - VkFormatProperties formatProps; - - vkGetPhysicalDeviceFormatProperties(physical_device, format, &formatProps); - - if (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) + if(IsOptimalDepthFormat(format)) { if(lower_to_high) return format; @@ -149,31 +164,4 @@ VkFormat PhysicalDevice::GetDepthStencilFormat(bool lower_to_high)const 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