From 8633a18e0140dea613e5c53aac003bbd9f506a44 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 19 Apr 2024 00:40:51 +0800 Subject: [PATCH] supported uint8_index_type --- inc/hgl/graph/VK.h | 8 +++++++- inc/hgl/graph/VKDevice.h | 3 +++ inc/hgl/graph/VKDeviceAttribute.h | 2 ++ src/SceneGraph/Vulkan/VKDeviceBuffer.cpp | 24 +++++++++++++++++++++++ src/SceneGraph/Vulkan/VKDeviceCreater.cpp | 18 +++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/inc/hgl/graph/VK.h b/inc/hgl/graph/VK.h index f33b4402..b6e5d7b6 100644 --- a/inc/hgl/graph/VK.h +++ b/inc/hgl/graph/VK.h @@ -138,8 +138,9 @@ inline const uint32_t GetMipLevel(const VkExtent3D &ext) /** * 索引类型,等同于VkIndexType */ -enum IndexType:uint +enum IndexType:int { + AUTO=-1, U16=0, U32, U8=VK_INDEX_TYPE_UINT8_EXT, @@ -147,6 +148,11 @@ enum IndexType:uint ERR=VK_INDEX_TYPE_MAX_ENUM }; +inline const bool IsIndexType(const IndexType it) +{ + return it>=U16&&it<=U8; +} + //Push Constant max-lengths: // // 256 bytes: nvidia,arm diff --git a/inc/hgl/graph/VKDevice.h b/inc/hgl/graph/VKDevice.h index dcb9c297..802437e4 100644 --- a/inc/hgl/graph/VKDevice.h +++ b/inc/hgl/graph/VKDevice.h @@ -117,6 +117,9 @@ public: //Buffer相关 VBO * CreateVBO (VkFormat format, uint32_t count,const void *data, SharingMode sm=SharingMode::Exclusive); VBO * CreateVBO (VkFormat format, uint32_t count, SharingMode sm=SharingMode::Exclusive){return CreateVBO(format,count,nullptr,sm);} + const IndexType GetIndexType(const VkDeviceSize &vertex_count)const; ///<求一个合适的索引类型 + const bool CheckIndexType(const IndexType,const VkDeviceSize &vertex_count)const; ///<检测一个索引类型是否合适 + IndexBuffer * CreateIBO (IndexType type, uint32_t count,const void * data, SharingMode sm=SharingMode::Exclusive); IndexBuffer * CreateIBO8 ( uint32_t count,const void * data, SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U8, count,(void *)data,sm);} IndexBuffer * CreateIBO16 ( uint32_t count,const uint16 *data, SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U16, count,(void *)data,sm);} diff --git a/inc/hgl/graph/VKDeviceAttribute.h b/inc/hgl/graph/VKDeviceAttribute.h index f57e8502..5c7d4b5e 100644 --- a/inc/hgl/graph/VKDeviceAttribute.h +++ b/inc/hgl/graph/VKDeviceAttribute.h @@ -39,6 +39,8 @@ struct GPUDeviceAttribute VkSurfaceTransformFlagBitsKHR preTransform; VkCompositeAlphaFlagBitsKHR compositeAlpha =VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; + bool uint8_index_type=false; + VkDevice device =VK_NULL_HANDLE; VkCommandPool cmd_pool =VK_NULL_HANDLE; diff --git a/src/SceneGraph/Vulkan/VKDeviceBuffer.cpp b/src/SceneGraph/Vulkan/VKDeviceBuffer.cpp index c6d4c585..d41ff1b3 100644 --- a/src/SceneGraph/Vulkan/VKDeviceBuffer.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceBuffer.cpp @@ -77,6 +77,30 @@ VBO *GPUDevice::CreateVBO(VkFormat format,uint32_t count,const void *data,Sharin return(new VertexAttribBuffer(attr->device,buf,format,stride,count)); } +const IndexType GPUDevice::GetIndexType(const VkDeviceSize &vertex_count)const +{ + if(vertex_count<=0)return(IndexType::ERR); + + if(attr->uint8_index_type&& vertex_count<=0xFF )return IndexType::U8; else + if( vertex_count<=0xFFFF)return IndexType::U16; else + if(attr->physical_device->SupportU32Index() )return IndexType::U32; else + + return IndexType::ERR; +} + +const bool GPUDevice::CheckIndexType(const IndexType it,const VkDeviceSize &vertex_count)const +{ + if(vertex_count<=0)return(false); + + if(it==IndexType::U16&&vertex_count<=0xFFFF)return(true); + + if(it==IndexType::U32&&attr->physical_device->SupportU32Index())return(true); + + if(it==IndexType::U8 &&vertex_count<=0xFF&&attr->uint8_index_type)return(true); + + return(false); +} + IndexBuffer *GPUDevice::CreateIBO(IndexType index_type,uint32_t count,const void *data,SharingMode sharing_mode) { if(count==0)return(nullptr); diff --git a/src/SceneGraph/Vulkan/VKDeviceCreater.cpp b/src/SceneGraph/Vulkan/VKDeviceCreater.cpp index 5a960e10..2299e694 100644 --- a/src/SceneGraph/Vulkan/VKDeviceCreater.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceCreater.cpp @@ -229,6 +229,18 @@ VkDevice VulkanDeviceCreater::CreateDevice(const uint32_t graphics_family) create_info.ppEnabledLayerNames =nullptr; create_info.pEnabledFeatures =&features; + VkPhysicalDeviceIndexTypeUint8FeaturesEXT index_type_uint8_features; + + if(physical_device->SupportU8Index() + &&require.fullDrawIndexUint8>=VulkanHardwareRequirement::SupportLevel::Want) + { + create_info.pNext=&index_type_uint8_features; + + index_type_uint8_features.sType =VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INDEX_TYPE_UINT8_FEATURES_EXT; + index_type_uint8_features.pNext =nullptr; + index_type_uint8_features.indexTypeUint8=VK_TRUE; + } + VkDevice device; if(vkCreateDevice(*physical_device,&create_info,nullptr,&device)==VK_SUCCESS) @@ -300,6 +312,12 @@ GPUDevice *VulkanDeviceCreater::CreateRenderDevice() ChooseSurfaceFormat(); + if(physical_device->SupportU8Index() + &&require.fullDrawIndexUint8>=VulkanHardwareRequirement::SupportLevel::Want) + { + device_attr->uint8_index_type=true; + } + device_attr->surface_format=surface_format; GetDeviceQueue(device_attr);