From a9e4ea026ff9a3eec8f126ef436853e235dc47e4 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 26 Jun 2019 19:05:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BTGATexture=EF=BC=8C=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=8A=A0=E8=BD=BD=E7=9B=B4=E6=8E=A5=E8=BF=9B=E6=98=BE?= =?UTF-8?q?=E5=AD=98=EF=BC=8C=E5=87=8F=E5=B0=91=E4=B8=80=E6=AD=A5=E5=86=85?= =?UTF-8?q?=E5=AD=98=E5=88=86=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/TGATexture.cpp | 120 ++++++++++++++++++++------------ inc/hgl/graph/vulkan/VKFormat.h | 2 + inc/hgl/io/FileInputStream.h | 2 +- 3 files changed, 78 insertions(+), 46 deletions(-) diff --git a/example/Vulkan/TGATexture.cpp b/example/Vulkan/TGATexture.cpp index 0812a60b..d47216da 100644 --- a/example/Vulkan/TGATexture.cpp +++ b/example/Vulkan/TGATexture.cpp @@ -1,6 +1,7 @@ #include #include -#include +#include +#include #include VK_NAMESPACE_BEGIN @@ -40,11 +41,8 @@ namespace }; #pragma pack(pop) - void RGB8to565(uint8 *data,uint size) + void RGB8to565(uint16 *target,uint8 *src,uint size) { - uint8 *src=data; - uint16 *target=(uint16 *)data; - for(uint i=0;i failed.")); return(nullptr); } - TGAHeader *header=(TGAHeader *)data; - TGAImageDesc image_desc; - uint8 *pixel_data=data+sizeof(TGAHeader); + const int64 file_length=fis->GetSize(); - image_desc.image_desc=header->image_desc; - - VkFormat format; - uint line_size; - - if(header->image_type==2) + if(file_length<=sizeof(TGAHeader)) { - if(header->bit==24) - { - RGB8to565(pixel_data,header->width*header->height); - - format=FMT_RGB565; - line_size=header->width*2; - } - else if(header->bit==32) - { - format=FMT_RGBA8UN; - line_size=header->width*4; - } - } - else if(header->image_type==3&&header->bit==8) - { - format=FMT_R8UN; - line_size=header->width; - } - else - { - LOG_ERROR(OS_TEXT("[ERROR] Image format error,filename: ")+filename); - delete[] data; + LOG_ERROR(OS_TEXT("[ERROR] file<")+filename+OS_TEXT("> length < sizeof(TGAHeader).")); return(nullptr); } - if(image_desc.direction==0) - SwapRow(pixel_data,line_size,header->height); + TGAHeader header; + TGAImageDesc image_desc; - Texture2D *tex=device->CreateTexture2D(format,pixel_data,header->width,header->height,line_size*header->height); + if(fis->Read(&header,sizeof(TGAHeader))!=sizeof(TGAHeader)) + return(false); + + const uint total_bytes=header.width*header.height*header.bit>>3; + + if(file_length length error.")); + return(nullptr); + } + + image_desc.image_desc=header.image_desc; + + VkFormat format=FMT_UNDEFINED; + + if(header.image_type==2) + { + if(header.bit==24)format=FMT_RGB8UN;else + if(header.bit==32)format=FMT_RGBA8UN; + } + else if(header.image_type==3&&header.bit==8) + format=FMT_R8UN; + + if(format==FMT_UNDEFINED) + { + LOG_ERROR(OS_TEXT("[ERROR] Image format error,filename: ")+filename); + return(nullptr); + } + + vulkan::Buffer *buf; + + if(header.bit==24) + { + uint8 *pixel_data=new uint8[total_bytes]; + + fis->Read(pixel_data,total_bytes); + + if(image_desc.direction==0) + SwapRow((uint8 *)pixel_data,header.width*header.bit/8,header.height); + + buf=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,header.width*header.height*2); + RGB8to565((uint16 *)buf->Map(),pixel_data,header.width*header.height); + buf->Unmap(); + + format=FMT_RGB565; + } + else + { + vulkan::Buffer *buf=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,total_bytes); + + uint8 *pixel_data=(uint8 *)buf->Map(); + + fis->Read(pixel_data,total_bytes); + + if(image_desc.direction==0) + SwapRow((uint8 *)pixel_data,header.width*header.bit/8,header.height); + + buf->Unmap(); + } + + Texture2D *tex=device->CreateTexture2D(format,buf,header.width,header.height); + + delete buf; if(tex) { - LOG_INFO(OS_TEXT("load image file<")+filename+OS_TEXT(">:<")+OSString(header->width)+OS_TEXT("x")+OSString(header->height)+OS_TEXT("> to texture ok")); + LOG_INFO(OS_TEXT("load image file<")+filename+OS_TEXT(">:<")+OSString(header.width)+OS_TEXT("x")+OSString(header.height)+OS_TEXT("> to texture ok")); //下面代码用于测试修改纹理 //device->ChangeTexture2D(tex,pixel_data,header->width/4,header->height/4,header->width/2,header->height/2,line_size*header->height/4); } else { - LOG_ERROR(OS_TEXT("load image file<")+filename+OS_TEXT(">:<")+OSString(header->width)+OS_TEXT("x")+OSString(header->height)+OS_TEXT("> to texture failed.")); + LOG_ERROR(OS_TEXT("load image file<")+filename+OS_TEXT(">:<")+OSString(header.width)+OS_TEXT("x")+OSString(header.height)+OS_TEXT("> to texture failed.")); } - delete[] data; return(tex); } VK_NAMESPACE_END \ No newline at end of file diff --git a/inc/hgl/graph/vulkan/VKFormat.h b/inc/hgl/graph/vulkan/VKFormat.h index 389c2439..d37c37ee 100644 --- a/inc/hgl/graph/vulkan/VKFormat.h +++ b/inc/hgl/graph/vulkan/VKFormat.h @@ -3,6 +3,8 @@ #include +#define FMT_UNDEFINED VK_FORMAT_UNDEFINED + #define FMT_RG4UN VK_FORMAT_R4G4_UNORM_PACK8 #define FMT_RGBA4 VK_FORMAT_R4G4B4A4_UNORM_PACK16 #define FMT_BGRA4 VK_FORMAT_B4G4R4A4_UNORM_PACK16 diff --git a/inc/hgl/io/FileInputStream.h b/inc/hgl/io/FileInputStream.h index 83678128..c3641876 100644 --- a/inc/hgl/io/FileInputStream.h +++ b/inc/hgl/io/FileInputStream.h @@ -39,7 +39,7 @@ namespace hgl virtual int64 Tell()const; ///<取当前位置 virtual int64 GetSize()const; ///<取得文件长度 virtual bool Restart(); ///<复位访问指针 - virtual int64 Seek(int64,SeekOrigin=SeekOrigin::Begin); ///<移动访问指针 + virtual int64 Seek(int64,SeekOrigin=SeekOrigin::Begin); ///<移动访问指针 virtual int64 Available()const; ///<剩下的可以不受阻塞访问的字节数