diff --git a/example/Vulkan/TGATexture.cpp b/example/Vulkan/TGATexture.cpp index 57204751..c463e6a1 100644 --- a/example/Vulkan/TGATexture.cpp +++ b/example/Vulkan/TGATexture.cpp @@ -70,17 +70,51 @@ namespace } } - void RGBtoRGBA(uint8 *tar,uint8 *src,uint size) + template + void RGB2RGBA(T *tar,T *src,uint size,const T alpha) { for(uint i=0;i + void BGR2RGBASwap(T *tar,T *src,uint size,const T alpha) + { + for(uint i=0;i + void BGR2FloatRGBASwap(T *tar,S *src,uint size,const T max_value,const T alpha) + { + for(uint i=0;i + void UInteger2Float(T *tar,S *src,uint size,const T max_value) + { + for(uint i=0;iRead(&header,sizeof(tga::Header))!=sizeof(tga::Header)) return(false); - const uint total_bytes=header.width*header.height*header.bit>>3; + const uint pixel_count =header.width*header.height; //象素数量 + const uint pixel_byte =header.bit>>3; //单个象素字节数 + const uint line_bytes =header.width*pixel_byte; //每行字节数 + const uint total_bytes =header.height*line_bytes; //总字节数 if(file_lengthRead(pixel_data,total_bytes); if(image_desc.vertical_direction==tga::VerticalDirection::BottomToTop) - SwapRow((uint8 *)pixel_data,header.width*header.bit/8,header.height); + SwapRow((uint8 *)pixel_data,line_bytes,header.height); + + const uint new_pixel_byte=((header.bit>>3)/3)*4; + + buf=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,pixel_count*new_pixel_byte); + + if(new_pixel_byte==4) + { + RGB2RGBA((uint8 *)buf->Map(),pixel_data,pixel_count,HGL_U8_MAX); + format=FMT_BGRA8UN; + } + else + if(new_pixel_byte==8) + { + BGR2RGBASwap((uint16 *)buf->Map(),(uint16 *)pixel_data,pixel_count,HGL_U16_MAX); + format=FMT_RGBA16UN; + } + else + if(new_pixel_byte==16) + { + BGR2FloatRGBASwap((float *)buf->Map(),(uint32 *)pixel_data,pixel_count,(float)HGL_U32_MAX,1.0f); + format=FMT_RGBA32F; + } - buf=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,header.width*header.height*4); - RGBtoRGBA((uint8 *)buf->Map(),pixel_data,header.width*header.height); buf->Unmap(); - - format=FMT_BGRA8UN; } else { @@ -180,8 +239,11 @@ Texture2D *LoadTGATexture(const OSString &filename,Device *device) fis->Read(pixel_data,total_bytes); + if(header.image_type==tga::ImageType::Grayscale&&header.bit==32) + UInteger2Float((float *)pixel_data,(uint32 *)pixel_data,pixel_count,(float)HGL_U32_MAX); + if(image_desc.vertical_direction==tga::VerticalDirection::BottomToTop) - SwapRow((uint8 *)pixel_data,header.width*header.bit/8,header.height); + SwapRow((uint8 *)pixel_data,line_bytes,header.height); buf->Unmap(); }