From 7c575e6e37518016687eed278f44463bbaf4da07 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 5 Jan 2022 11:26:24 +0800 Subject: [PATCH] optimized codes of Texture Loader --- CMPlatform | 2 +- example/Vulkan/texture_rect.cpp | 2 +- inc/hgl/graph/Bitmap2DLoader.h | 2 +- inc/hgl/graph/TextureLoader.h | 45 ++++----- inc/hgl/graph/VKMaterialInstance.h | 1 + inc/hgl/graph/VKMaterialParameters.h | 2 +- inc/hgl/graph/VKTexture.h | 2 + src/SceneGraph/TextureLoader.cpp | 4 +- src/SceneGraph/Vulkan/VKTexture2DLoader.cpp | 106 ++------------------ src/SceneGraph/Vulkan/VKTextureLoader.h | 95 ++++++++++++++++++ 10 files changed, 136 insertions(+), 125 deletions(-) create mode 100644 src/SceneGraph/Vulkan/VKTextureLoader.h diff --git a/CMPlatform b/CMPlatform index c1a42765..bb671a96 160000 --- a/CMPlatform +++ b/CMPlatform @@ -1 +1 @@ -Subproject commit c1a42765331c8c6161e12805f19877e53003cb07 +Subproject commit bb671a9674155a184671caa047844dd9e82f2ad5 diff --git a/example/Vulkan/texture_rect.cpp b/example/Vulkan/texture_rect.cpp index 26dde38e..bc0067de 100644 --- a/example/Vulkan/texture_rect.cpp +++ b/example/Vulkan/texture_rect.cpp @@ -92,7 +92,7 @@ private: { const VkExtent2D extent=sc_render_target->GetExtent(); - cam.vp_width=cam.width=extent.width; + cam.vp_width =cam.width =extent.width; cam.vp_height=cam.height=extent.height; cam.Refresh(); diff --git a/inc/hgl/graph/Bitmap2DLoader.h b/inc/hgl/graph/Bitmap2DLoader.h index 677bc498..9b9bc61c 100644 --- a/inc/hgl/graph/Bitmap2DLoader.h +++ b/inc/hgl/graph/Bitmap2DLoader.h @@ -17,7 +17,7 @@ namespace hgl public: - using Texture2DLoader::Texture2DLoader; + Bitmap2DLoader():Texture2DLoader(){} ~Bitmap2DLoader(); void *OnBegin(uint32 total_bytes) override; diff --git a/inc/hgl/graph/TextureLoader.h b/inc/hgl/graph/TextureLoader.h index a35dd146..db6681f7 100644 --- a/inc/hgl/graph/TextureLoader.h +++ b/inc/hgl/graph/TextureLoader.h @@ -71,8 +71,6 @@ namespace hgl { protected: - VkImageViewType type; - TextureFileHeader file_header; VkFormat format; @@ -93,9 +91,8 @@ namespace hgl public: - TextureLoader(const VkImageViewType &ivt) + TextureLoader() { - type=ivt; format=VK_FORMAT_UNDEFINED; mipmap_zero_total_bytes=0; total_bytes=0; @@ -121,12 +118,12 @@ namespace hgl return mipmap_zero_total_bytes; else return ComputeMipmapBytes( file_header.length, - mipmap_zero_total_bytes); + mipmap_zero_total_bytes); } public: - Texture1DLoader():TextureLoader(VK_IMAGE_VIEW_TYPE_1D){} + using TextureLoader::TextureLoader; virtual ~Texture1DLoader()=default; };//class Texture1DLoader @@ -144,13 +141,13 @@ namespace hgl return mipmap_zero_total_bytes; else return ComputeMipmapBytes( file_header.width, - file_header.height, - mipmap_zero_total_bytes); + file_header.height, + mipmap_zero_total_bytes); } public: - Texture2DLoader():TextureLoader(VK_IMAGE_VIEW_TYPE_2D){} + using TextureLoader::TextureLoader; virtual ~Texture2DLoader()=default; };//class Texture2DLoader @@ -168,14 +165,14 @@ namespace hgl return mipmap_zero_total_bytes; else return ComputeMipmapBytes( file_header.width, - file_header.height, - file_header.depth, - mipmap_zero_total_bytes); + file_header.height, + file_header.depth, + mipmap_zero_total_bytes); } public: - Texture3DLoader():TextureLoader(VK_IMAGE_VIEW_TYPE_3D){} + using TextureLoader::TextureLoader; virtual ~Texture3DLoader()=default; };//class Texture3DLoader @@ -193,13 +190,13 @@ namespace hgl return mipmap_zero_total_bytes*6; else return ComputeMipmapBytes( file_header.width, - file_header.height, - mipmap_zero_total_bytes)*6; + file_header.height, + mipmap_zero_total_bytes)*6; } public: - TextureCubeLoader():TextureLoader(VK_IMAGE_VIEW_TYPE_CUBE){} + using TextureLoader::TextureLoader; virtual ~TextureCubeLoader()=default; };//class TextureCubeLoader @@ -217,12 +214,12 @@ namespace hgl return mipmap_zero_total_bytes*file_header.layers; else return ComputeMipmapBytes( file_header.length, - mipmap_zero_total_bytes)*file_header.layers;; + mipmap_zero_total_bytes)*file_header.layers; } public: - Texture1DArrayLoader():TextureLoader(VK_IMAGE_VIEW_TYPE_1D_ARRAY){} + using TextureLoader::TextureLoader; virtual ~Texture1DArrayLoader()=default; };//class Texture1DArrayLoader @@ -240,13 +237,13 @@ namespace hgl return mipmap_zero_total_bytes*file_header.layers; else return ComputeMipmapBytes( file_header.width, - file_header.height, - mipmap_zero_total_bytes)*file_header.layers; + file_header.height, + mipmap_zero_total_bytes)*file_header.layers; } public: - Texture2DArrayLoader():TextureLoader(VK_IMAGE_VIEW_TYPE_2D_ARRAY){} + using TextureLoader::TextureLoader; virtual ~Texture2DArrayLoader()=default; };//class Texture2DArrayLoader @@ -264,13 +261,13 @@ namespace hgl return mipmap_zero_total_bytes*6*file_header.layers; else return ComputeMipmapBytes( file_header.width, - file_header.height, - mipmap_zero_total_bytes)*6*file_header.layers; + file_header.height, + mipmap_zero_total_bytes)*6*file_header.layers; } public: - TextureCubeArrayLoader():TextureLoader(VK_IMAGE_VIEW_TYPE_CUBE_ARRAY){} + using TextureLoader::TextureLoader; virtual ~TextureCubeArrayLoader()=default; };//class TextureCubeArrayLoader }//namespace graph diff --git a/inc/hgl/graph/VKMaterialInstance.h b/inc/hgl/graph/VKMaterialInstance.h index e5a9b115..b9483658 100644 --- a/inc/hgl/graph/VKMaterialInstance.h +++ b/inc/hgl/graph/VKMaterialInstance.h @@ -30,3 +30,4 @@ public: };//class MaterialInstance VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_MATERIAL_INSTANCE_INCLUDE + \ No newline at end of file diff --git a/inc/hgl/graph/VKMaterialParameters.h b/inc/hgl/graph/VKMaterialParameters.h index 7b699df4..4893e5e2 100644 --- a/inc/hgl/graph/VKMaterialParameters.h +++ b/inc/hgl/graph/VKMaterialParameters.h @@ -21,7 +21,7 @@ private: public: - const DescriptorSetsType GetType (){return ds_type;} + const DescriptorSetsType GetType (){return ds_type;} DescriptorSets * GetDescriptorSet (){return descriptor_sets;} const VkDescriptorSet GetVkDescriptorSet ()const{return descriptor_sets->GetDescriptorSet();} diff --git a/inc/hgl/graph/VKTexture.h b/inc/hgl/graph/VKTexture.h index d828a7fb..e8f3c4de 100644 --- a/inc/hgl/graph/VKTexture.h +++ b/inc/hgl/graph/VKTexture.h @@ -66,6 +66,8 @@ public: Texture2D(VkDevice dev,TextureData *td):Texture(dev,td){} ~Texture2D()=default; + static VkImageViewType GetImageViewType(){return VK_IMAGE_VIEW_TYPE_2D;} + const uint32_t GetWidth ()const{return data?data->image_view->GetExtent().width:0;} const uint32_t GetHeight()const{return data?data->image_view->GetExtent().height:0;} };//class Texture2D:public Texture diff --git a/src/SceneGraph/TextureLoader.cpp b/src/SceneGraph/TextureLoader.cpp index 10130d48..174c724b 100644 --- a/src/SceneGraph/TextureLoader.cpp +++ b/src/SceneGraph/TextureLoader.cpp @@ -187,8 +187,8 @@ namespace hgl if(file_header.version!=0) return(false); - if(file_header.type!=type) - return(false); +// if(file_header.type!=type) +// return(false); if(file_header.pixel_format.channels==0) { diff --git a/src/SceneGraph/Vulkan/VKTexture2DLoader.cpp b/src/SceneGraph/Vulkan/VKTexture2DLoader.cpp index 3d36f597..e8e9ece2 100644 --- a/src/SceneGraph/Vulkan/VKTexture2DLoader.cpp +++ b/src/SceneGraph/Vulkan/VKTexture2DLoader.cpp @@ -1,107 +1,23 @@ -#include -#include -#include -#include +#include"VKTextureLoader.h" #include #include VK_NAMESPACE_BEGIN -namespace +template<> void VkTextureLoader::OnExtent(VkExtent3D &extent) { - class VkTextureLoader - { - }; + extent.width =file_header.width; + extent.height =file_header.height; + extent.depth =1; +} - class VkTexture2DLoader:public Texture2DLoader - { - protected: - - GPUDevice *device; - GPUBuffer *buf; - - bool auto_mipmaps; - - Texture2D *tex; - - public: - - VkTexture2DLoader(GPUDevice *dev,const bool am):device(dev) - { - buf=nullptr; - tex=nullptr; - auto_mipmaps=am; - } - - virtual ~VkTexture2DLoader() - { - SAFE_CLEAR(buf); - SAFE_CLEAR(tex); - } - - void *OnBegin(uint32 total_bytes) override - { - SAFE_CLEAR(buf); - SAFE_CLEAR(tex); - - if(!CheckVulkanFormat(format)) - return(nullptr); - - buf=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,total_bytes); - - if(!buf) - return(nullptr); - - return buf->Map(); - } - - void OnEnd() override - { - buf->Unmap(); - - TextureCreateInfo *tci=new TextureCreateInfo(format); - - VkExtent3D extent; - - extent.width =file_header.width; - extent.height =file_header.height; - extent.depth =1; - - tci->SetData(buf,extent); - - if(auto_mipmaps&&file_header.mipmaps<=1) - { - if(device->CheckFormatSupport(format,VK_FORMAT_FEATURE_BLIT_DST_BIT)) - { - tci->usage|=VK_IMAGE_USAGE_TRANSFER_SRC_BIT; - tci->SetAutoMipmaps(); - } - } - else - { - tci->origin_mipmaps= - tci->target_mipmaps=file_header.mipmaps; - } - - tci->mipmap_zero_total_bytes=mipmap_zero_total_bytes; - - tex=device->CreateTexture2D(tci); - - if(tex) - buf=nullptr; - } - - Texture2D *GetTexture() - { - Texture2D *result=tex; - tex=nullptr; - return result; - } - };//class VkTexture2DLoader -}//namespace +template<> Texture2D *VkTextureLoader::OnCreateTexture(TextureCreateInfo *tci) +{ + return device->CreateTexture2D(tci); +} Texture2D *CreateTexture2DFromFile(GPUDevice *device,const OSString &filename,bool auto_mipmaps) { - VkTexture2DLoader loader(device,auto_mipmaps); + VkTextureLoader loader(device,auto_mipmaps); if(!loader.Load(filename)) return(nullptr); diff --git a/src/SceneGraph/Vulkan/VKTextureLoader.h b/src/SceneGraph/Vulkan/VKTextureLoader.h new file mode 100644 index 00000000..f00b3c00 --- /dev/null +++ b/src/SceneGraph/Vulkan/VKTextureLoader.h @@ -0,0 +1,95 @@ +#pragma once +#include +#include +#include +#include +#include + +VK_NAMESPACE_BEGIN +template class VkTextureLoader:public TL +{ +protected: + + GPUDevice *device; + GPUBuffer *buf; + T *tex; + + bool auto_mipmaps; + +public: + + VkTextureLoader(GPUDevice *dev,const bool am) + { + device=dev; + buf=nullptr; + tex=nullptr; + auto_mipmaps=am; + } + + virtual ~VkTextureLoader() + { + SAFE_CLEAR(tex); + SAFE_CLEAR(buf); + } + + void *OnBegin(uint32 total_bytes) override + { + SAFE_CLEAR(buf); + + if(!CheckVulkanFormat(format)) + return(nullptr); + + buf=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,total_bytes); + + if(!buf) + return(nullptr); + + return buf->Map(); + } + + void OnExtent(VkExtent3D &extent); + T *OnCreateTexture(TextureCreateInfo *); + + void OnEnd() override + { + buf->Unmap(); + + TextureCreateInfo *tci=new TextureCreateInfo(format); + + VkExtent3D extent; + + OnExtent(extent); + + tci->SetData(buf,extent); + + if(auto_mipmaps&&file_header.mipmaps<=1) + { + if(device->CheckFormatSupport(format,VK_FORMAT_FEATURE_BLIT_DST_BIT)) + { + tci->usage|=VK_IMAGE_USAGE_TRANSFER_SRC_BIT; + tci->SetAutoMipmaps(); + } + } + else + { + tci->origin_mipmaps= + tci->target_mipmaps=file_header.mipmaps; + } + + tci->mipmap_zero_total_bytes=mipmap_zero_total_bytes; + + SAFE_CLEAR(tex); + tex=OnCreateTexture(tci); + + if(tex) + buf=nullptr; + } + + T *GetTexture() + { + T *result=tex; + tex=nullptr; + return result; + } +};//class VkTextureLoader +VK_NAMESPACE_END \ No newline at end of file