optimized codes of Texture Loader

This commit is contained in:
hyzboy 2022-01-05 11:26:24 +08:00
parent 89d27962b2
commit 7c575e6e37
10 changed files with 136 additions and 125 deletions

@ -1 +1 @@
Subproject commit c1a42765331c8c6161e12805f19877e53003cb07
Subproject commit bb671a9674155a184671caa047844dd9e82f2ad5

View File

@ -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();

View File

@ -17,7 +17,7 @@ namespace hgl
public:
using Texture2DLoader::Texture2DLoader;
Bitmap2DLoader():Texture2DLoader(){}
~Bitmap2DLoader();
void *OnBegin(uint32 total_bytes) override;

View File

@ -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

View File

@ -30,3 +30,4 @@ public:
};//class MaterialInstance
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_MATERIAL_INSTANCE_INCLUDE

View File

@ -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();}

View File

@ -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

View File

@ -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)
{

View File

@ -1,107 +1,23 @@
#include<hgl/graph/VK.h>
#include<hgl/graph/VKDevice.h>
#include<hgl/graph/VKBuffer.h>
#include<hgl/graph/TextureLoader.h>
#include"VKTextureLoader.h"
#include<hgl/io/FileInputStream.h>
#include<hgl/log/LogInfo.h>
VK_NAMESPACE_BEGIN
namespace
template<> void VkTextureLoader<Texture2D,Texture2DLoader>::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<Texture2D,Texture2DLoader>::OnCreateTexture(TextureCreateInfo *tci)
{
return device->CreateTexture2D(tci);
}
Texture2D *CreateTexture2DFromFile(GPUDevice *device,const OSString &filename,bool auto_mipmaps)
{
VkTexture2DLoader loader(device,auto_mipmaps);
VkTextureLoader<Texture2D,Texture2DLoader> loader(device,auto_mipmaps);
if(!loader.Load(filename))
return(nullptr);

View File

@ -0,0 +1,95 @@
#pragma once
#include<hgl/graph/VK.h>
#include<hgl/graph/VKDevice.h>
#include<hgl/graph/VKBuffer.h>
#include<hgl/graph/TextureLoader.h>
#include<hgl/graph/VKTextureCreateInfo.h>
VK_NAMESPACE_BEGIN
template<typename T,typename TL> 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