few codes about TextureManager

This commit is contained in:
hyzboy 2024-11-05 23:52:53 +08:00
parent 95e9fe79a9
commit acdbdcd8b5
10 changed files with 92 additions and 44 deletions

View File

@ -36,6 +36,7 @@ private:
}
public:
~DebugUtils()=default;
void SetName(VkObjectType,uint64_t,const char *);

View File

@ -102,7 +102,6 @@ public: //添加数据到管理器如果指针为nullptr会返回-1
PrimitiveID Add(Primitive * p ){return rm_primitives.Add(p);}
BufferID Add(DeviceBuffer * buf ){return rm_buffers.Add(buf);}
SamplerID Add(Sampler * s ){return rm_samplers.Add(s);}
// TextureID Add(Texture * t ){return rm_textures.Add(t);}
RenderableID Add(Renderable * r ){return rm_renderables.Add(r);}
StaticMeshID Add(StaticMesh * sm ){return rm_static_mesh.Add(sm);}
@ -164,7 +163,6 @@ public: //Get
Primitive * GetPrimitive (const PrimitiveID &id){return rm_primitives.Get(id);}
DeviceBuffer * GetBuffer (const BufferID &id){return rm_buffers.Get(id);}
Sampler * GetSampler (const SamplerID &id){return rm_samplers.Get(id);}
// Texture * GetTexture (const TextureID &id){return rm_textures.Get(id);}
Renderable * GetRenderable (const RenderableID &id){return rm_renderables.Get(id);}
StaticMesh * GetStaticMesh (const StaticMeshID &id){return rm_static_mesh.Get(id);}
@ -177,7 +175,6 @@ public: //Release
void Release(Primitive * p ){rm_primitives.Release(p);}
void Release(DeviceBuffer * buf ){rm_buffers.Release(buf);}
void Release(Sampler * s ){rm_samplers.Release(s);}
// void Release(Texture * t ){rm_textures.Release(t);}
void Release(Renderable * r ){rm_renderables.Release(r);}
void Release(StaticMesh * sm ){rm_static_mesh.Release(sm);}

View File

@ -11,11 +11,14 @@ VK_NAMESPACE_BEGIN
BitmapData *LoadBitmapFromFile(const OSString &filename);
using TextureID=int;
class Texture
{
protected:
TextureManager *manager;
TextureID texture_id;
VkDevice device;
TextureData *data;
@ -23,6 +26,7 @@ protected:
public:
TextureManager * GetManager () {return manager;}
const TextureID GetID ()const noexcept {return texture_id;}
TextureData * GetData () {return data;}
@ -44,9 +48,10 @@ public:
public:
Texture(TextureManager *tm,TextureData *td)
Texture(TextureManager *tm,const TextureID &id,TextureData *td)
{
manager=tm;
texture_id=id;
data=td;
}

View File

@ -17,10 +17,8 @@ public:
public:
GraphManager(GPUDevice *dev)
{
device=dev;
}
GraphManager(GPUDevice *dev){device=dev;}
virtual ~GraphManager()=default;
};//class GraphManager
VK_NAMESPACE_END

View File

@ -6,26 +6,38 @@
#include<hgl/type/RectScope.h>
#include<hgl/type/object/ObjectBaseInfo.h>
#include<hgl/graph/ImageRegion.h>
#include<hgl/graph/VKTexture.h>
VK_NAMESPACE_BEGIN
using TextureID =int;
class TextureManager:public GraphManager
{
DeviceQueue *texture_queue;
TextureCmdBuffer *texture_cmd_buf;
private:
TextureID texture_serial;
const TextureID AcquireID(){return texture_serial++;} ///<取得一个新的纹理ID
private:
SortedSet<VkImage> image_set;
SortedSet<Texture *> texture_set; ///<纹理合集
Map<TextureID,Texture *> texture_by_id;
Map<OSString,Texture *> texture_by_name;
private:
DeviceQueue *texture_queue;
TextureCmdBuffer *texture_cmd_buf;
const TextureID Add(Texture *);
const TextureID Add(Texture *,const OSString &);
public:
TextureManager();
TextureManager(GPUDevice *);
virtual ~TextureManager();
public: //Buffer

View File

@ -13,7 +13,7 @@ Texture2D *TextureManager::CreateTexture2D(TextureData *tex_data)
if(!tex_data)
return(nullptr);
Texture2D *tex=new Texture2D(this,tex_data);
Texture2D *tex=new Texture2D(this,AcquireID(),tex_data);
texture_set.Add(tex);

View File

@ -13,7 +13,7 @@ Texture2DArray *TextureManager::CreateTexture2DArray(TextureData *tex_data)
if(!tex_data)
return(nullptr);
return(new Texture2DArray(this,tex_data));
return(new Texture2DArray(this,AcquireID(),tex_data));
}
Texture2DArray *TextureManager::CreateTexture2DArray(TextureCreateInfo *tci)

View File

@ -12,7 +12,7 @@ TextureCube *TextureManager::CreateTextureCube(TextureData *tex_data)
if(!tex_data)
return(nullptr);
return(new TextureCube(this,tex_data));
return(new TextureCube(this,AcquireID(),tex_data));
}
TextureCube *TextureManager::CreateTextureCube(TextureCreateInfo *tci)

View File

@ -40,8 +40,6 @@ GPUDevice::~GPUDevice()
bool GPUDevice::Resize(const VkExtent2D &extent)
{
graph_module_manager->OnResize(extent);
SAFE_CLEAR(sc_rt);
attr->RefreshSurfaceCaps();

View File

@ -1,11 +1,15 @@
#include<hgl/graph/manager/TextureManager.h>
#include<hgl/graph/VKDevice.h>
#include<hgl/graph/VKCommandBuffer.h>
VK_NAMESPACE_BEGIN
TextureManager::TextureManager()
TextureManager::TextureManager(GPUDevice *dev):GraphManager(dev)
{
texture_cmd_buf=CreateTextureCommandBuffer(attr->physical_device->GetDeviceName()+AnsiString(":TexCmdBuffer"));
texture_queue=CreateQueue();
texture_cmd_buf=dev->CreateTextureCommandBuffer(GetPhysicalDevice()->GetDeviceName()+AnsiString(":TexCmdBuffer"));
texture_queue=dev->CreateQueue();
texture_serial=0;
}
TextureManager::~TextureManager()
@ -14,12 +18,47 @@ TextureManager::~TextureManager()
SAFE_CLEAR(texture_cmd_buf);
}
const TextureID TextureManager::Add(Texture *tex)
{
if(!tex)
return(-1);
if(tex->GetManager()!=this)
return(-2);
if(texture_set.Contains(tex))
return tex->GetID();
texture_set.Add(tex);
texture_by_id.Add(tex->GetID(),tex);
return tex->GetID();
}
const TextureID TextureManager::Add(Texture *tex,const OSString &tn)
{
TextureID id=Add(tex);
if(id<0)
return id;
if(!tn.IsEmpty())
texture_by_name.Add(tn,tex);
return id;
}
void TextureManager::Release(Texture *tex)
{
if(!tex)
return;
if(!texture_set.Contains(tex))
return;
texture_set.Delete(tex);
texture_by_id.DeleteByKey(tex->GetID());
texture_by_name.DeleteByValue(tex);
}
Texture2D *CreateTexture2DFromFile(TextureManager *tm,const OSString &filename,bool auto_mipmaps);
@ -35,19 +74,18 @@ Texture2D *TextureManager::LoadTexture2D(const OSString &filename,bool auto_mipm
if(tex)
{
texture_by_name.Add(filename,tex);
Add(tex);
Add(tex,filename);
#ifdef _DEBUG
DebugUtils *du=device->GetDebugUtils();
//#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
// if(du)
// {
// const UTF8String name=U8_TEXT("Tex2D:")+ToUTF8String(filename);
//
// du->SetImage(tex->GetImage(),(char *)(name.c_str()));
// }
//#endif//_DEBUG
}
return tex;
@ -62,16 +100,16 @@ Texture2DArray *TextureManager::CreateTexture2DArray(const AnsiString &name,cons
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
//#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;
}
@ -101,8 +139,7 @@ TextureCube *TextureManager::LoadTextureCube(const OSString &filename,bool auto_
if(tex)
{
texture_by_name.Add(filename,tex);
Add(tex);
Add(tex,filename);
}
return tex;