From a5ab2edf1274e4162890994fb1c14e18c8994fa2 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 6 Aug 2020 21:27:58 +0800 Subject: [PATCH] support compress texture file format. --- CMSceneGraph | 2 +- example/Vulkan/DrawTile.cpp | 2 +- inc/hgl/graph/TextureLoader.h | 22 +++++++-- inc/hgl/graph/TileData.h | 2 - res | 2 +- .../Vulkan/POD/VKTextureLoader.cpp | 47 ++++++++++++++----- src/SceneGraph/Texture2DLoader.cpp | 19 ++++++-- src/SceneGraph/TileData.cpp | 3 +- 8 files changed, 73 insertions(+), 26 deletions(-) diff --git a/CMSceneGraph b/CMSceneGraph index 780a97d2..81c18490 160000 --- a/CMSceneGraph +++ b/CMSceneGraph @@ -1 +1 @@ -Subproject commit 780a97d2435577b6fc6008bb656e623c5c058431 +Subproject commit 81c18490a375fbeaa1ea99340916e44833ef6a74 diff --git a/example/Vulkan/DrawTile.cpp b/example/Vulkan/DrawTile.cpp index d3362c82..b0a7093b 100644 --- a/example/Vulkan/DrawTile.cpp +++ b/example/Vulkan/DrawTile.cpp @@ -90,7 +90,7 @@ private: bool InitTileTexture() { - tile_data=device->CreateTileData( FMT_A1RGB5, //纹理格式,因VK不支持实时转换,所以提交的数据格式必须与此一致 + tile_data=device->CreateTileData( FMT_BC1_RGBAUN, //纹理格式,因VK不支持实时转换,所以提交的数据格式必须与此一致 512,512, //TILE大小 tile_list.GetCount()); //TILE需求数量 diff --git a/inc/hgl/graph/TextureLoader.h b/inc/hgl/graph/TextureLoader.h index 45e9690d..7382bb84 100644 --- a/inc/hgl/graph/TextureLoader.h +++ b/inc/hgl/graph/TextureLoader.h @@ -13,20 +13,26 @@ namespace hgl { uint8 id[6]; ///>3;} - const uint total_bytes()const{return pixel_count()*pixel_bytes();} - };// + };//struct TexPixelFormat #pragma pack(pop) /** @@ -38,6 +44,14 @@ namespace hgl Tex2DFileHeader file_header; + union + { + TexPixelFormat pixel_format; + uint16 compress_format; + }; + + uint32 total_bytes; + protected: virtual void *OnBegin(uint32)=0; diff --git a/inc/hgl/graph/TileData.h b/inc/hgl/graph/TileData.h index 498fb5af..f21a66e7 100644 --- a/inc/hgl/graph/TileData.h +++ b/inc/hgl/graph/TileData.h @@ -37,8 +37,6 @@ namespace hgl protected: - uint pixel_bytes; ///<单个象素字节数 - Texture2D *tile_texture; /// to_pool; ///channels)continue; - if(fh.datatype!=(uint8)pf->type)continue; + if(channels!=pf->channels)continue; + if(tpf.datatype!=(uint8)pf->type)continue; - if(fh.colors[0]!=pf->colors[0])continue; - if(fh.colors[1]!=pf->colors[1])continue; - if(fh.colors[2]!=pf->colors[2])continue; - if(fh.colors[3]!=pf->colors[3])continue; + if(tpf.colors[0]!=pf->colors[0])continue; + if(tpf.colors[1]!=pf->colors[1])continue; + if(tpf.colors[2]!=pf->colors[2])continue; + if(tpf.colors[3]!=pf->colors[3])continue; - if(fh.bits[0]!=pf->bits[0])continue; - if(fh.bits[1]!=pf->bits[1])continue; - if(fh.bits[2]!=pf->bits[2])continue; - if(fh.bits[3]!=pf->bits[3])continue; + if(tpf.bits[0]!=pf->bits[0])continue; + if(tpf.bits[1]!=pf->bits[1])continue; + if(tpf.bits[2]!=pf->bits[2])continue; + if(tpf.bits[3]!=pf->bits[3])continue; return pf->format; } @@ -107,10 +107,33 @@ namespace void *OnBegin(uint32 total_bytes) override { + constexpr VkFormat CompressFormatList[]= + { + FMT_BC1_RGBUN, + FMT_BC1_RGBAUN, + FMT_BC2UN, + FMT_BC3UN, + FMT_BC4UN, + FMT_BC5UN, + FMT_BC6UF, + FMT_BC6SF, + FMT_BC7UN + }; + + constexpr size_t CompressFormatCount=sizeof(CompressFormatList)/sizeof(VkFormat); + SAFE_CLEAR(buf); SAFE_CLEAR(tex); - format=GetVulkanFormat(file_header); + if(file_header.channels==0) + { + if(compress_format<0||compress_format>=CompressFormatCount) + return(nullptr); + + format=CompressFormatList[compress_format]; + } + else + format=GetVulkanFormat(file_header.channels,pixel_format); if(!CheckVulkanFormat(format)) return(nullptr); diff --git a/src/SceneGraph/Texture2DLoader.cpp b/src/SceneGraph/Texture2DLoader.cpp index 1ebcb171..76c9c411 100644 --- a/src/SceneGraph/Texture2DLoader.cpp +++ b/src/SceneGraph/Texture2DLoader.cpp @@ -17,10 +17,23 @@ namespace hgl if(file_header.version!=2) return(false); - if(file_header.total_bytes()==0) - return(false); + total_bytes=0; - const uint total_bytes=file_header.total_bytes(); + if(file_header.channels==0) //压缩格式 + { + if(is->Read(&compress_format,sizeof(uint16))!=sizeof(uint16)) + return(false); + + if(is->Read(&total_bytes,sizeof(uint32))!=sizeof(uint32)) + return(false); + } + else + { + if(is->Read(&pixel_format,sizeof(TexPixelFormat))!=sizeof(TexPixelFormat)) + return(false); + + total_bytes=file_header.pixel_count()*pixel_format.pixel_bytes(); + } if(is->Available()GetFormat()); - tile_bytes =tile_width*tile_height*pixel_bytes; + tile_bytes=GetImageBytes(tile_texture->GetFormat(),tile_width*tile_height); tile_buffer=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,tile_bytes*tile_max_count,nullptr);