2020-07-29 01:00:25 +08:00
|
|
|
|
#ifndef HGL_GRAPH_TEXTURE_LOADER_INCLUDE
|
2020-07-28 16:37:01 +08:00
|
|
|
|
#define HGL_GRAPH_TEXTURE_LOADER_INCLUDE
|
|
|
|
|
|
|
|
|
|
#include<hgl/io/InputStream.h>
|
2020-09-05 17:54:21 +08:00
|
|
|
|
#include<hgl/type/String.h>
|
2020-07-28 18:47:22 +08:00
|
|
|
|
#include<hgl/graph/Bitmap.h>
|
2020-10-25 21:29:18 +08:00
|
|
|
|
#include<hgl/graph/VKFormat.h>
|
2020-07-28 16:37:01 +08:00
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace graph
|
|
|
|
|
{
|
|
|
|
|
#pragma pack(push,1)
|
2021-12-13 21:03:41 +08:00
|
|
|
|
struct TexPixelFormat
|
2020-07-28 16:37:01 +08:00
|
|
|
|
{
|
2021-12-13 21:03:41 +08:00
|
|
|
|
uint8 channels; //0: compress 1/2/3/4:normal
|
|
|
|
|
|
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
char colors[4];
|
|
|
|
|
uint8 bits[4];
|
|
|
|
|
uint8 datatype;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
uint16 compress_format;
|
|
|
|
|
};
|
|
|
|
|
};
|
2020-08-06 21:27:58 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2021-12-13 21:13:14 +08:00
|
|
|
|
const uint pixel_bits()const;
|
2021-12-13 21:03:41 +08:00
|
|
|
|
};//struct TexPixelFormat
|
2020-08-06 21:27:58 +08:00
|
|
|
|
|
2021-12-13 21:03:41 +08:00
|
|
|
|
constexpr uint TexPixelFormatLength=sizeof(TexPixelFormat);
|
|
|
|
|
|
|
|
|
|
struct alignas(8) TextureFileHeader
|
2020-08-06 21:27:58 +08:00
|
|
|
|
{
|
2021-12-14 10:34:44 +08:00
|
|
|
|
uint8 id_str[7]; ///<Texture\x1A
|
2021-12-13 21:03:41 +08:00
|
|
|
|
uint8 version; ///<必须为0
|
|
|
|
|
uint8 type; ///<贴图类型,等同于VkImageViewType
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
2021-12-13 21:03:41 +08:00
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
uint32 length; ///<长(1D纹理用)
|
2021-12-14 11:08:33 +08:00
|
|
|
|
uint32 width; ///<宽(2D/3D/Cube纹理用)
|
2021-12-13 21:03:41 +08:00
|
|
|
|
};
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
2021-12-13 21:03:41 +08:00
|
|
|
|
uint32 height; ///<高(2D/3D/Cube纹理用)
|
|
|
|
|
|
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
uint32 depth; ///<深度(3D纹理用)
|
|
|
|
|
uint32 layers; ///<层数(Arrays纹理用)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TexPixelFormat pixel_format; ///<象素格式
|
|
|
|
|
uint8 mipmaps; ///<mipmaps
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr uint TextureFileHeaderLength=sizeof(TextureFileHeader); //GPUBuffer内需要64位8字节对齐,如果此值不对齐,请想办法填0
|
2020-07-28 16:37:01 +08:00
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
2021-12-13 21:03:41 +08:00
|
|
|
|
const uint32 ComputeMipmapBytes(uint32 length,uint32 bytes);
|
|
|
|
|
const uint32 ComputeMipmapBytes(uint32 width,uint32 height,uint32 bytes);
|
|
|
|
|
const uint32 ComputeMipmapBytes(uint32 width,uint32 height,uint32 depth,uint32 bytes);
|
|
|
|
|
|
|
|
|
|
class TextureLoader
|
2020-07-28 16:37:01 +08:00
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
|
2021-12-13 21:03:41 +08:00
|
|
|
|
TextureFileHeader file_header;
|
|
|
|
|
|
|
|
|
|
VkFormat format;
|
|
|
|
|
|
|
|
|
|
uint32 mipmap_zero_total_bytes; ///< 0 级mipmaps单个图象的总字节数
|
|
|
|
|
uint32 total_bytes; ///<总字节数
|
2020-08-06 21:27:58 +08:00
|
|
|
|
|
2020-10-25 21:29:18 +08:00
|
|
|
|
protected:
|
2020-08-06 21:27:58 +08:00
|
|
|
|
|
2021-12-13 21:03:41 +08:00
|
|
|
|
virtual uint32 GetPixelsCount()const=0;
|
|
|
|
|
virtual uint32 GetTotalBytes()const=0; ///<计算总字节数
|
|
|
|
|
|
|
|
|
|
protected:
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
|
|
|
|
virtual void *OnBegin(uint32)=0;
|
|
|
|
|
virtual void OnEnd()=0;
|
|
|
|
|
virtual void OnError(){}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2022-01-05 11:26:24 +08:00
|
|
|
|
TextureLoader()
|
2021-12-14 16:32:03 +08:00
|
|
|
|
{
|
|
|
|
|
format=VK_FORMAT_UNDEFINED;
|
|
|
|
|
mipmap_zero_total_bytes=0;
|
|
|
|
|
total_bytes=0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~TextureLoader()=default;
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
2021-12-13 21:03:41 +08:00
|
|
|
|
virtual bool Load(io::InputStream *);
|
|
|
|
|
bool Load(const OSString &filename);
|
|
|
|
|
};//class TextureLoader
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
2021-12-13 21:13:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 1D纹理加载器
|
|
|
|
|
*/
|
|
|
|
|
class Texture1DLoader:public TextureLoader
|
|
|
|
|
{
|
|
|
|
|
protected: // override functions
|
|
|
|
|
|
|
|
|
|
uint32 GetPixelsCount()const override{return file_header.length;}
|
|
|
|
|
uint32 GetTotalBytes()const override
|
|
|
|
|
{
|
|
|
|
|
if(file_header.mipmaps<=1)
|
|
|
|
|
return mipmap_zero_total_bytes;
|
|
|
|
|
else
|
|
|
|
|
return ComputeMipmapBytes( file_header.length,
|
2022-01-05 11:26:24 +08:00
|
|
|
|
mipmap_zero_total_bytes);
|
2021-12-13 21:13:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2022-01-05 11:26:24 +08:00
|
|
|
|
using TextureLoader::TextureLoader;
|
2021-12-13 21:13:14 +08:00
|
|
|
|
virtual ~Texture1DLoader()=default;
|
|
|
|
|
};//class Texture1DLoader
|
|
|
|
|
|
2020-07-28 18:47:22 +08:00
|
|
|
|
/**
|
2021-12-13 21:03:41 +08:00
|
|
|
|
* 2D纹理加载器
|
2020-07-28 18:47:22 +08:00
|
|
|
|
*/
|
2021-12-13 21:03:41 +08:00
|
|
|
|
class Texture2DLoader:public TextureLoader
|
2020-07-28 18:47:22 +08:00
|
|
|
|
{
|
2021-12-13 21:03:41 +08:00
|
|
|
|
protected: // override functions
|
2020-07-28 18:47:22 +08:00
|
|
|
|
|
2021-12-13 21:03:41 +08:00
|
|
|
|
uint32 GetPixelsCount()const override{return file_header.width*file_header.height;}
|
|
|
|
|
uint32 GetTotalBytes()const override
|
|
|
|
|
{
|
|
|
|
|
if(file_header.mipmaps<=1)
|
|
|
|
|
return mipmap_zero_total_bytes;
|
|
|
|
|
else
|
|
|
|
|
return ComputeMipmapBytes( file_header.width,
|
2022-01-05 11:26:24 +08:00
|
|
|
|
file_header.height,
|
|
|
|
|
mipmap_zero_total_bytes);
|
2021-12-13 21:03:41 +08:00
|
|
|
|
}
|
2020-07-28 18:47:22 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2022-01-05 11:26:24 +08:00
|
|
|
|
using TextureLoader::TextureLoader;
|
2021-12-13 21:03:41 +08:00
|
|
|
|
virtual ~Texture2DLoader()=default;
|
|
|
|
|
};//class Texture2DLoader
|
2021-12-13 21:13:14 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 3D纹理加载器
|
|
|
|
|
*/
|
|
|
|
|
class Texture3DLoader:public TextureLoader
|
|
|
|
|
{
|
|
|
|
|
protected: // override functions
|
|
|
|
|
|
|
|
|
|
uint32 GetPixelsCount()const override{return file_header.width*file_header.height*file_header.depth;}
|
|
|
|
|
uint32 GetTotalBytes()const override
|
|
|
|
|
{
|
|
|
|
|
if(file_header.mipmaps<=1)
|
|
|
|
|
return mipmap_zero_total_bytes;
|
|
|
|
|
else
|
|
|
|
|
return ComputeMipmapBytes( file_header.width,
|
2022-01-05 11:26:24 +08:00
|
|
|
|
file_header.height,
|
|
|
|
|
file_header.depth,
|
|
|
|
|
mipmap_zero_total_bytes);
|
2021-12-13 21:13:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2022-01-05 11:26:24 +08:00
|
|
|
|
using TextureLoader::TextureLoader;
|
2021-12-13 21:13:14 +08:00
|
|
|
|
virtual ~Texture3DLoader()=default;
|
|
|
|
|
};//class Texture3DLoader
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cube纹理加载器
|
|
|
|
|
*/
|
|
|
|
|
class TextureCubeLoader:public TextureLoader
|
|
|
|
|
{
|
|
|
|
|
protected: // override functions
|
|
|
|
|
|
|
|
|
|
uint32 GetPixelsCount()const override{return file_header.width*file_header.height;}
|
|
|
|
|
uint32 GetTotalBytes()const override
|
|
|
|
|
{
|
|
|
|
|
if(file_header.mipmaps<=1)
|
|
|
|
|
return mipmap_zero_total_bytes*6;
|
|
|
|
|
else
|
|
|
|
|
return ComputeMipmapBytes( file_header.width,
|
2022-01-05 11:26:24 +08:00
|
|
|
|
file_header.height,
|
|
|
|
|
mipmap_zero_total_bytes)*6;
|
2021-12-13 21:13:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2022-01-05 11:26:24 +08:00
|
|
|
|
using TextureLoader::TextureLoader;
|
2021-12-13 21:13:14 +08:00
|
|
|
|
virtual ~TextureCubeLoader()=default;
|
|
|
|
|
};//class TextureCubeLoader
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 1D纹理阵列加载器
|
|
|
|
|
*/
|
|
|
|
|
class Texture1DArrayLoader:public TextureLoader
|
|
|
|
|
{
|
|
|
|
|
protected: // override functions
|
|
|
|
|
|
|
|
|
|
uint32 GetPixelsCount()const override{return file_header.length;}
|
|
|
|
|
uint32 GetTotalBytes()const override
|
|
|
|
|
{
|
|
|
|
|
if(file_header.mipmaps<=1)
|
|
|
|
|
return mipmap_zero_total_bytes*file_header.layers;
|
|
|
|
|
else
|
|
|
|
|
return ComputeMipmapBytes( file_header.length,
|
2022-01-05 11:26:24 +08:00
|
|
|
|
mipmap_zero_total_bytes)*file_header.layers;
|
2021-12-13 21:13:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2022-01-05 11:26:24 +08:00
|
|
|
|
using TextureLoader::TextureLoader;
|
2021-12-13 21:13:14 +08:00
|
|
|
|
virtual ~Texture1DArrayLoader()=default;
|
|
|
|
|
};//class Texture1DArrayLoader
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 2D纹理阵列加载器
|
|
|
|
|
*/
|
|
|
|
|
class Texture2DArrayLoader:public TextureLoader
|
|
|
|
|
{
|
|
|
|
|
protected: // override functions
|
|
|
|
|
|
|
|
|
|
uint32 GetPixelsCount()const override{return file_header.width*file_header.height;}
|
|
|
|
|
uint32 GetTotalBytes()const override
|
|
|
|
|
{
|
|
|
|
|
if(file_header.mipmaps<=1)
|
|
|
|
|
return mipmap_zero_total_bytes*file_header.layers;
|
|
|
|
|
else
|
|
|
|
|
return ComputeMipmapBytes( file_header.width,
|
2022-01-05 11:26:24 +08:00
|
|
|
|
file_header.height,
|
|
|
|
|
mipmap_zero_total_bytes)*file_header.layers;
|
2021-12-13 21:13:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2022-01-05 11:26:24 +08:00
|
|
|
|
using TextureLoader::TextureLoader;
|
2021-12-13 21:13:14 +08:00
|
|
|
|
virtual ~Texture2DArrayLoader()=default;
|
|
|
|
|
};//class Texture2DArrayLoader
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cube纹理阵列加载器
|
|
|
|
|
*/
|
|
|
|
|
class TextureCubeArrayLoader:public TextureLoader
|
|
|
|
|
{
|
|
|
|
|
protected: // override functions
|
|
|
|
|
|
|
|
|
|
uint32 GetPixelsCount()const override{return file_header.width*file_header.height;}
|
|
|
|
|
uint32 GetTotalBytes()const override
|
|
|
|
|
{
|
|
|
|
|
if(file_header.mipmaps<=1)
|
|
|
|
|
return mipmap_zero_total_bytes*6*file_header.layers;
|
|
|
|
|
else
|
|
|
|
|
return ComputeMipmapBytes( file_header.width,
|
2022-01-05 11:26:24 +08:00
|
|
|
|
file_header.height,
|
|
|
|
|
mipmap_zero_total_bytes)*6*file_header.layers;
|
2021-12-13 21:13:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2022-01-05 11:26:24 +08:00
|
|
|
|
using TextureLoader::TextureLoader;
|
2021-12-13 21:13:14 +08:00
|
|
|
|
virtual ~TextureCubeArrayLoader()=default;
|
|
|
|
|
};//class TextureCubeArrayLoader
|
2020-07-28 16:37:01 +08:00
|
|
|
|
}//namespace graph
|
|
|
|
|
}//namespace hgl
|
|
|
|
|
#endif//HGL_GRAPH_TEXTURE_LOADER_INCLUDE
|