newly Loader code of Texture
This commit is contained in:
32
inc/hgl/graph/Bitmap2DLoader.h
Normal file
32
inc/hgl/graph/Bitmap2DLoader.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef HGL_GRAPH_BITMAP2D_LOADER_INCLUDE
|
||||
#define HGL_GRAPH_BITMAP2D_LOADER_INCLUDE
|
||||
|
||||
#include<hgl/graph/TextureLoader.h>
|
||||
namespace hgl
|
||||
{
|
||||
namespace graph
|
||||
{
|
||||
/**
|
||||
* 2Dλͼ<CEBB><CDBC><EFBFBD><EFBFBD>
|
||||
*/
|
||||
class Bitmap2DLoader:public Texture2DLoader
|
||||
{
|
||||
protected:
|
||||
|
||||
BitmapData *bmp=nullptr;
|
||||
|
||||
public:
|
||||
|
||||
using Texture2DLoader::Texture2DLoader;
|
||||
~Bitmap2DLoader();
|
||||
|
||||
void *OnBegin(uint32 total_bytes) override;
|
||||
void OnEnd() override {}
|
||||
|
||||
BitmapData *GetBitmap();
|
||||
};//class Bitmap2DLoader
|
||||
|
||||
BitmapData *LoadBitmapFromFile(const OSString &filename);
|
||||
}//namespace graph
|
||||
}//namespace hgl
|
||||
#endif//HGL_GRAPH_BITMAP2D_LOADER_INCLUDE
|
@@ -27,90 +27,124 @@ namespace hgl
|
||||
constexpr uint32 CompressFormatCount=sizeof(CompressFormatList)/sizeof(VkFormat);
|
||||
|
||||
#pragma pack(push,1)
|
||||
struct Tex2DFileHeader
|
||||
{
|
||||
uint8 id[6]; ///<Tex2D\x1A
|
||||
uint8 version; ///<必须为3
|
||||
uint8 mipmaps;
|
||||
uint32 width;
|
||||
uint32 height;
|
||||
uint8 channels;
|
||||
|
||||
public:
|
||||
|
||||
const uint pixel_count()const{return width*height;}
|
||||
};//struct Tex2DFileHeader
|
||||
|
||||
struct TexPixelFormat
|
||||
{
|
||||
char colors[4];
|
||||
uint8 bits[4];
|
||||
uint8 datatype;
|
||||
|
||||
public:
|
||||
|
||||
const uint pixel_bits()const{return bits[0]+bits[1]+bits[2]+bits[3];}
|
||||
const uint pixel_bytes()const{return pixel_bits()>>3;}
|
||||
};//struct TexPixelFormat
|
||||
#pragma pack(pop)
|
||||
|
||||
/**
|
||||
* 2D纹理加载器
|
||||
*/
|
||||
class Texture2DLoader
|
||||
{
|
||||
protected:
|
||||
|
||||
Tex2DFileHeader file_header;
|
||||
uint8 channels; //0: compress 1/2/3/4:normal
|
||||
|
||||
union
|
||||
{
|
||||
TexPixelFormat pixel_format;
|
||||
uint16 compress_format;
|
||||
struct
|
||||
{
|
||||
char colors[4];
|
||||
uint8 bits[4];
|
||||
uint8 datatype;
|
||||
};
|
||||
|
||||
struct
|
||||
{
|
||||
uint16 compress_format;
|
||||
};
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
const uint pixel_bits()const
|
||||
{
|
||||
return channels ?bits[0]+bits[1]+bits[2]+bits[3]
|
||||
:CompressFormatBits[compress_format];
|
||||
}
|
||||
};//struct TexPixelFormat
|
||||
|
||||
constexpr uint TexPixelFormatLength=sizeof(TexPixelFormat);
|
||||
|
||||
struct alignas(8) TextureFileHeader
|
||||
{
|
||||
uint8 id_str[8]; ///<Texture\x1A
|
||||
uint8 version; ///<必须为0
|
||||
uint8 type; ///<贴图类型,等同于VkImageViewType
|
||||
|
||||
union
|
||||
{
|
||||
uint32 length; ///<长(1D纹理用)
|
||||
uint32 width; ///<宽(2D/Cube纹理用)
|
||||
};
|
||||
|
||||
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
|
||||
#pragma pack(pop)
|
||||
|
||||
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
|
||||
{
|
||||
protected:
|
||||
|
||||
VkImageViewType type;
|
||||
|
||||
TextureFileHeader file_header;
|
||||
|
||||
VkFormat format;
|
||||
|
||||
uint32 mipmap_zero_total_bytes; ///< 0 级mipmaps单个图象的总字节数
|
||||
uint32 total_bytes; ///<总字节数
|
||||
|
||||
protected:
|
||||
|
||||
uint32 mipmap_zero_total_bytes;
|
||||
virtual uint32 GetPixelsCount()const=0;
|
||||
virtual uint32 GetImageCount()const=0; ///<每个级别的图象数量
|
||||
virtual uint32 GetTotalBytes()const=0; ///<计算总字节数
|
||||
|
||||
protected:
|
||||
|
||||
uint32 ComputeTotalBytes();
|
||||
|
||||
virtual void *OnBegin(uint32)=0;
|
||||
virtual void OnEnd()=0;
|
||||
virtual void OnError(){}
|
||||
|
||||
public:
|
||||
|
||||
const Tex2DFileHeader *GetFileHeader()const{return &file_header;}
|
||||
TextureLoader(const VkImageViewType &ivt){type=ivt;}
|
||||
|
||||
public:
|
||||
virtual bool Load(io::InputStream *);
|
||||
bool Load(const OSString &filename);
|
||||
};//class TextureLoader
|
||||
|
||||
virtual ~Texture2DLoader()=default;
|
||||
|
||||
bool Load(io::InputStream *is);
|
||||
bool Load(const OSString &filename);
|
||||
};//class Texture2DLoader
|
||||
|
||||
/**
|
||||
* 2D位图加载
|
||||
* 2D纹理加载器
|
||||
*/
|
||||
class Bitmap2DLoader:public Texture2DLoader
|
||||
class Texture2DLoader:public TextureLoader
|
||||
{
|
||||
protected:
|
||||
protected: // override functions
|
||||
|
||||
BitmapData *bmp=nullptr;
|
||||
uint32 GetPixelsCount()const override{return file_header.width*file_header.height;}
|
||||
uint32 GetImageCount()const override{return 1;}
|
||||
uint32 GetTotalBytes()const override
|
||||
{
|
||||
if(file_header.mipmaps<=1)
|
||||
return mipmap_zero_total_bytes;
|
||||
else
|
||||
return ComputeMipmapBytes( file_header.width,
|
||||
file_header.height,
|
||||
mipmap_zero_total_bytes);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
~Bitmap2DLoader();
|
||||
|
||||
void *OnBegin(uint32 total_bytes) override;
|
||||
void OnEnd() override {}
|
||||
|
||||
BitmapData *GetBitmap();
|
||||
};//class Bitmap2DLoader
|
||||
|
||||
BitmapData *LoadBitmapFromFile(const OSString &filename);
|
||||
Texture2DLoader():TextureLoader(VK_IMAGE_VIEW_TYPE_2D){}
|
||||
virtual ~Texture2DLoader()=default;
|
||||
};//class Texture2DLoader
|
||||
}//namespace graph
|
||||
}//namespace hgl
|
||||
#endif//HGL_GRAPH_TEXTURE_LOADER_INCLUDE
|
||||
|
Reference in New Issue
Block a user