From 88f5948a2e4cc5a5a35b2c67315bfcb512db9e40 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 5 Nov 2024 23:02:38 +0800 Subject: [PATCH] moved LoadTexture2D/CreateTexture2DArray/LoadTextureCube to TextureManager from VKRenderResource --- inc/hgl/graph/VKRenderResource.h | 10 +-- inc/hgl/graph/manager/TextureManager.h | 13 +++- src/SceneGraph/Vulkan/VKRenderResource.cpp | 86 ---------------------- src/SceneGraph/manager/TextureManager.cpp | 86 ++++++++++++++++++++++ 4 files changed, 97 insertions(+), 98 deletions(-) diff --git a/inc/hgl/graph/VKRenderResource.h b/inc/hgl/graph/VKRenderResource.h index fc95cc15..752e2a04 100644 --- a/inc/hgl/graph/VKRenderResource.h +++ b/inc/hgl/graph/VKRenderResource.h @@ -50,7 +50,7 @@ class RenderResource ShaderModuleMapByName shader_module_by_name[VK_SHADER_STAGE_TYPE_COUNT]; Map material_by_name; - Map texture_by_name; + //Map texture_by_name; IDObjectManage rm_material; ///<材质合集 IDObjectManage rm_material_instance; ///<材质实例合集 @@ -157,14 +157,6 @@ public: //Material Sampler * CreateSampler(VkSamplerCreateInfo *sci=nullptr); Sampler * CreateSampler(Texture *); -public: //texture - - Texture2D * LoadTexture2D(const OSString &,bool auto_mipmaps=false); - TextureCube * LoadTextureCube(const OSString &,bool auto_mipmaps=false); - - Texture2DArray * CreateTexture2DArray(const AnsiString &name,const uint32_t width,const uint32_t height,const uint32_t layer,const VkFormat &fmt,bool auto_mipmaps=false); - bool LoadTexture2DToArray(Texture2DArray *,const uint32_t layer,const OSString &); - public: //Get Material * GetMaterial (const MaterialID &id){return rm_material.Get(id);} diff --git a/inc/hgl/graph/manager/TextureManager.h b/inc/hgl/graph/manager/TextureManager.h index 7bd8381c..fcfcc762 100644 --- a/inc/hgl/graph/manager/TextureManager.h +++ b/inc/hgl/graph/manager/TextureManager.h @@ -26,12 +26,10 @@ public: TextureManager(); virtual ~TextureManager(); -private: //Buffer +public: //Buffer DeviceBuffer *CreateTransferSourceBuffer(const VkDeviceSize,const void *data_ptr=nullptr); - friend class TileData; - private: //Image VkImage CreateImage (VkImageCreateInfo *); @@ -91,6 +89,15 @@ public: //Create/Chagne public: void Release(Texture *); + +public: // Load + + Texture2D * LoadTexture2D(const OSString &,bool auto_mipmaps=false); + TextureCube * LoadTextureCube(const OSString &,bool auto_mipmaps=false); + + Texture2DArray * CreateTexture2DArray(const AnsiString &name,const uint32_t width,const uint32_t height,const uint32_t layer,const VkFormat &fmt,bool auto_mipmaps=false); + bool LoadTexture2DToArray(Texture2DArray *,const uint32_t layer,const OSString &); + };//class TextureManager VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKRenderResource.cpp b/src/SceneGraph/Vulkan/VKRenderResource.cpp index b9b3a460..7edcc238 100644 --- a/src/SceneGraph/Vulkan/VKRenderResource.cpp +++ b/src/SceneGraph/Vulkan/VKRenderResource.cpp @@ -136,90 +136,4 @@ Sampler *RenderResource::CreateSampler(Texture *tex) return s; } - -Texture2D *CreateTexture2DFromFile(GPUDevice *device,const OSString &filename,bool auto_mipmaps); - -Texture2D *RenderResource::LoadTexture2D(const OSString &filename,bool auto_mipmaps) -{ - Texture2D *tex; - - if(texture_by_name.Get(filename,(Texture *&)tex)) - return tex; - - tex=CreateTexture2DFromFile(device,filename,auto_mipmaps); - - if(tex) - { - texture_by_name.Add(filename,tex); - Add(tex); - - #ifdef _DEBUG - DebugUtils *du=device->GetDebugUtils(); - - if(du) - { - const UTF8String name=U8_TEXT("Tex2D:")+ToUTF8String(filename); - - du->SetImage(tex->GetImage(),(char *)(name.c_str())); - } - #endif//_DEBUG - } - - return tex; -} - -Texture2DArray *RenderResource::CreateTexture2DArray(const AnsiString &name,const uint32_t width,const uint32_t height,const uint32_t layer,const VkFormat &fmt,bool auto_mipmaps) -{ - Texture2DArray *ta=device->CreateTexture2DArray(width,height,layer,fmt,auto_mipmaps); - - if(ta) - Add(ta); - else - return nullptr; - - #ifdef _DEBUG - DebugUtils *du=device->GetDebugUtils(); - - if(du) - { - du->SetImage(ta->GetImage(),"Tex2DArrayImage:"+name); - du->SetImageView(ta->GetVulkanImageView(),"Tex2DArrayImageView:"+name); - du->SetDeviceMemory(ta->GetDeviceMemory(),"Tex2DArrayMemory:"+name); - } - #endif//_DEBUG - - return ta; -} - -bool LoadTexture2DLayerFromFile(TextureManager *tm,Texture2DArray *t2d,const uint32_t layer,const OSString &filename,bool auto_mipmaps); - -bool RenderResource::LoadTexture2DToArray(Texture2DArray *ta,const uint32_t layer,const OSString &filename) -{ - if(!ta)return(false); - - if(!LoadTexture2DLayerFromFile(device,ta,layer,filename,false)) - return(false); - - return(true); -} - -TextureCube *CreateTextureCubeFromFile(GPUDevice *device,const OSString &filename,bool auto_mipmaps); - -TextureCube *RenderResource::LoadTextureCube(const OSString &filename,bool auto_mipmaps) -{ - TextureCube *tex; - - if(texture_by_name.Get(filename,(Texture *&)tex)) - return tex; - - tex=CreateTextureCubeFromFile(device,filename,auto_mipmaps); - - if(tex) - { - texture_by_name.Add(filename,tex); - Add(tex); - } - - return tex; -} VK_NAMESPACE_END diff --git a/src/SceneGraph/manager/TextureManager.cpp b/src/SceneGraph/manager/TextureManager.cpp index 917c71a7..646ff515 100644 --- a/src/SceneGraph/manager/TextureManager.cpp +++ b/src/SceneGraph/manager/TextureManager.cpp @@ -10,4 +10,90 @@ void TextureManager::Release(Texture *tex) texture_set.Delete(tex); } +Texture2D *CreateTexture2DFromFile(GPUDevice *device,const OSString &filename,bool auto_mipmaps); + +Texture2D *TextureManager::LoadTexture2D(const OSString &filename,bool auto_mipmaps) +{ + Texture2D *tex; + + if(texture_by_name.Get(filename,(Texture *&)tex)) + return tex; + + tex=CreateTexture2DFromFile(device,filename,auto_mipmaps); + + if(tex) + { + texture_by_name.Add(filename,tex); + Add(tex); + + #ifdef _DEBUG + DebugUtils *du=device->GetDebugUtils(); + + if(du) + { + const UTF8String name=U8_TEXT("Tex2D:")+ToUTF8String(filename); + + du->SetImage(tex->GetImage(),(char *)(name.c_str())); + } + #endif//_DEBUG + } + + return tex; +} + +Texture2DArray *TextureManager::CreateTexture2DArray(const AnsiString &name,const uint32_t width,const uint32_t height,const uint32_t layer,const VkFormat &fmt,bool auto_mipmaps) +{ + Texture2DArray *ta=device->CreateTexture2DArray(width,height,layer,fmt,auto_mipmaps); + + if(ta) + Add(ta); + else + return nullptr; + + #ifdef _DEBUG + DebugUtils *du=device->GetDebugUtils(); + + if(du) + { + du->SetImage(ta->GetImage(),"Tex2DArrayImage:"+name); + du->SetImageView(ta->GetVulkanImageView(),"Tex2DArrayImageView:"+name); + du->SetDeviceMemory(ta->GetDeviceMemory(),"Tex2DArrayMemory:"+name); + } + #endif//_DEBUG + + return ta; +} + +bool LoadTexture2DLayerFromFile(TextureManager *tm,Texture2DArray *t2d,const uint32_t layer,const OSString &filename,bool auto_mipmaps); + +bool TextureManager::LoadTexture2DToArray(Texture2DArray *ta,const uint32_t layer,const OSString &filename) +{ + if(!ta)return(false); + + if(!LoadTexture2DLayerFromFile(device,ta,layer,filename,false)) + return(false); + + return(true); +} + +TextureCube *CreateTextureCubeFromFile(GPUDevice *device,const OSString &filename,bool auto_mipmaps); + +TextureCube *TextureManager::LoadTextureCube(const OSString &filename,bool auto_mipmaps) +{ + TextureCube *tex; + + if(texture_by_name.Get(filename,(Texture *&)tex)) + return tex; + + tex=CreateTextureCubeFromFile(device,filename,auto_mipmaps); + + if(tex) + { + texture_by_name.Add(filename,tex); + Add(tex); + } + + return tex; +} + VK_NAMESPACE_END