exchange LoadTGATexture to CreateTextureFromFile for texture_rect sample
This commit is contained in:
parent
0d353d47cb
commit
8515f41718
@ -11,7 +11,7 @@ endmacro()
|
|||||||
|
|
||||||
CreateProject(00.triangle first_triangle.cpp)
|
CreateProject(00.triangle first_triangle.cpp)
|
||||||
CreateProject(01.indices_rect indices_rect.cpp)
|
CreateProject(01.indices_rect indices_rect.cpp)
|
||||||
CreateProject(02.1.texture_rect texture_rect.cpp TGATexture.cpp)
|
CreateProject(02.1.texture_rect texture_rect.cpp)
|
||||||
#CreateProject(02.2.texture_linear texture_linear.cpp TGATexture.cpp)
|
#CreateProject(02.2.texture_linear texture_linear.cpp TGATexture.cpp)
|
||||||
CreateProject(03.HQFilterTexture HQFilterTexture.cpp TGATexture.cpp)
|
CreateProject(03.HQFilterTexture HQFilterTexture.cpp TGATexture.cpp)
|
||||||
CreateProject(04.Geometry2D Geometry2D.cpp)
|
CreateProject(04.Geometry2D Geometry2D.cpp)
|
||||||
|
@ -10,7 +10,7 @@ using namespace hgl;
|
|||||||
using namespace hgl::graph;
|
using namespace hgl::graph;
|
||||||
|
|
||||||
VK_NAMESPACE_BEGIN
|
VK_NAMESPACE_BEGIN
|
||||||
Texture2D *LoadTGATexture(const OSString &filename,Device *device,bool use_optimal=true);
|
Texture2D *CreateTextureFromFile(Device *device,const OSString &filename,bool use_optimar=true);
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
|
|
||||||
constexpr uint32_t SCREEN_WIDTH=128;
|
constexpr uint32_t SCREEN_WIDTH=128;
|
||||||
@ -92,7 +92,7 @@ private:
|
|||||||
render_obj=material->CreateRenderable(VERTEX_COUNT);
|
render_obj=material->CreateRenderable(VERTEX_COUNT);
|
||||||
descriptor_sets=material->CreateDescriptorSets();
|
descriptor_sets=material->CreateDescriptorSets();
|
||||||
|
|
||||||
texture=vulkan::LoadTGATexture(OS_TEXT("res/image/lena.tga"),device);
|
texture=vulkan::CreateTextureFromFile(device,OS_TEXT("res/image/lena.Tex2D"));
|
||||||
|
|
||||||
VkSamplerCreateInfo sampler_create_info;
|
VkSamplerCreateInfo sampler_create_info;
|
||||||
|
|
||||||
|
@ -288,7 +288,7 @@ constexpr size_t FMT_PVRTC_RANGE_SIZE=FMT_PVRTC_END_RANGE-FMT_PVRTC_BEGIN_RANGE+
|
|||||||
//以下为AMD/NVIDIA/INTEL/QUALCOMM/ARM/POWERVR全部可用的通用格式
|
//以下为AMD/NVIDIA/INTEL/QUALCOMM/ARM/POWERVR全部可用的通用格式
|
||||||
#define UFMT_BGRA4 FMT_BGRA4
|
#define UFMT_BGRA4 FMT_BGRA4
|
||||||
#define UFMT_RGB565 FMT_RGB565
|
#define UFMT_RGB565 FMT_RGB565
|
||||||
#define UFMT_A4RGB5 FMT_A1RGB5
|
#define UFMT_A1RGB5 FMT_A1RGB5
|
||||||
#define UFMT_R8 FMT_R8UN
|
#define UFMT_R8 FMT_R8UN
|
||||||
#define UFMT_RG8 FMT_RG8UN
|
#define UFMT_RG8 FMT_RG8UN
|
||||||
#define UFMT_RGBA8 FMT_RGBA8UN
|
#define UFMT_RGBA8 FMT_RGBA8UN
|
||||||
@ -340,6 +340,8 @@ enum class VulkanDataType
|
|||||||
UFLOAT,
|
UFLOAT,
|
||||||
SFLOAT,
|
SFLOAT,
|
||||||
SRGB,
|
SRGB,
|
||||||
|
|
||||||
|
END
|
||||||
};//
|
};//
|
||||||
|
|
||||||
struct VulkanFormat
|
struct VulkanFormat
|
||||||
|
@ -1,43 +1,57 @@
|
|||||||
file(GLOB RENDER_DEVICE_VULKAN_HEADER ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/*.h)
|
file(GLOB RENDER_DEVICE_VULKAN_HEADER ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/*.h)
|
||||||
|
|
||||||
|
SET(VK_DEVICE_SOURCE VKDeviceAttribute.cpp
|
||||||
|
VKDeviceCreater.cpp
|
||||||
|
VKDevice.cpp
|
||||||
|
VKDeviceBuffer.cpp
|
||||||
|
VKDeviceImage.cpp
|
||||||
|
VKDeviceTexture.cpp
|
||||||
|
VKDeviceSwapchain.cpp
|
||||||
|
VKDeviceRenderPass.cpp)
|
||||||
|
|
||||||
|
SET(VK_DESCRIPTOR_SETS_SOURCE VKDescriptorSets.cpp
|
||||||
|
VKDescriptorSetLayoutCreater.cpp
|
||||||
|
VKDescriptorSetLayoutCreater.h)
|
||||||
|
|
||||||
|
SET(VK_SHADER_SOURCE VKShaderParse.h
|
||||||
|
VKShaderModule.cpp
|
||||||
|
VKShaderModuleManage.cpp)
|
||||||
|
|
||||||
|
SET(VK_MATERIAL_SOURCE VKImageView.cpp
|
||||||
|
VKTexture.cpp
|
||||||
|
VKMaterial.cpp)
|
||||||
|
|
||||||
|
SET(VK_RENDER_PASS_SOURCE VKFramebuffer.cpp
|
||||||
|
VKPipeline.cpp
|
||||||
|
VKRenderPass.cpp
|
||||||
|
VKRenderTarget.cpp
|
||||||
|
VKSwapchain.cpp
|
||||||
|
VKCommandBuffer.cpp)
|
||||||
|
|
||||||
|
|
||||||
SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
|
SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
|
||||||
VKMemory.cpp
|
VKMemory.cpp
|
||||||
VKProperties.cpp
|
VKProperties.cpp
|
||||||
VKDebugOut.cpp
|
VKDebugOut.cpp
|
||||||
VKInstance.cpp
|
VKInstance.cpp
|
||||||
VKPhysicalDevice.cpp
|
VKPhysicalDevice.cpp
|
||||||
VKImageView.cpp
|
|
||||||
VKCommandBuffer.cpp
|
|
||||||
VKDeviceAttribute.cpp
|
|
||||||
VKDeviceCreater.cpp
|
|
||||||
VKDevice.cpp
|
|
||||||
VKDeviceBuffer.cpp
|
|
||||||
VKDeviceImage.cpp
|
|
||||||
VKDeviceTexture.cpp
|
|
||||||
VKDeviceSwapchain.cpp
|
|
||||||
VKDeviceRenderPass.cpp
|
|
||||||
VKBuffer.cpp
|
VKBuffer.cpp
|
||||||
VKDescriptorSets.cpp
|
|
||||||
VKDescriptorSetLayoutCreater.cpp
|
|
||||||
VKDescriptorSetLayoutCreater.h
|
|
||||||
VKRenderPass.cpp
|
|
||||||
VKRenderTarget.cpp
|
|
||||||
VKShaderParse.h
|
|
||||||
VKShaderModule.cpp
|
|
||||||
VKShaderModuleManage.cpp
|
|
||||||
VKVertexAttributeBinding.cpp
|
VKVertexAttributeBinding.cpp
|
||||||
VKPipeline.cpp
|
|
||||||
VKSemaphore.cpp
|
VKSemaphore.cpp
|
||||||
VKFramebuffer.cpp
|
|
||||||
VKFence.cpp
|
VKFence.cpp
|
||||||
VKMaterial.cpp
|
|
||||||
VKRenderable.cpp
|
VKRenderable.cpp
|
||||||
VKTexture.cpp
|
VKSampler.cpp)
|
||||||
VKSampler.cpp
|
|
||||||
VKSwapchain.cpp
|
|
||||||
)
|
|
||||||
|
|
||||||
SET(RENDER_DEVICE_VULKAN_POD_SOURCE pod/VKPipelineCreateInfo.POD.cpp)
|
SET(RENDER_DEVICE_VULKAN_POD_SOURCE pod/VKPipelineCreateInfo.POD.cpp
|
||||||
|
pod/VKTextureLoader.cpp)
|
||||||
|
|
||||||
|
SOURCE_GROUP("Device" FILES ${VK_DEVICE_SOURCE})
|
||||||
|
SOURCE_GROUP("Descriptor Sets" FILES ${VK_DESCRIPTOR_SETS_SOURCE})
|
||||||
|
|
||||||
|
SOURCE_GROUP("Material\\Shader" FILES ${VK_SHADER_SOURCE})
|
||||||
|
SOURCE_GROUP("Material" FILES ${VK_MATERIAL_SOURCE})
|
||||||
|
|
||||||
|
SOURCE_GROUP("Render Pass" FILES ${VK_RENDER_PASS_SOURCE})
|
||||||
|
|
||||||
SOURCE_GROUP("Header Files" FILES ${RENDER_DEVICE_VULKAN_HEADER})
|
SOURCE_GROUP("Header Files" FILES ${RENDER_DEVICE_VULKAN_HEADER})
|
||||||
SOURCE_GROUP("Source Files" FILES ${RENDER_DEVICE_VULKAN_SOURCE})
|
SOURCE_GROUP("Source Files" FILES ${RENDER_DEVICE_VULKAN_SOURCE})
|
||||||
@ -45,6 +59,13 @@ SOURCE_GROUP("POD Source Files" FILES ${RENDER_DEVICE_VULKAN_POD_SOURCE})
|
|||||||
|
|
||||||
add_library(ULRE.RenderDevice.Vulkan STATIC ${RENDER_DEVICE_VULKAN_HEADER}
|
add_library(ULRE.RenderDevice.Vulkan STATIC ${RENDER_DEVICE_VULKAN_HEADER}
|
||||||
${RENDER_DEVICE_VULKAN_SOURCE}
|
${RENDER_DEVICE_VULKAN_SOURCE}
|
||||||
${RENDER_DEVICE_VULKAN_POD_SOURCE})
|
${RENDER_DEVICE_VULKAN_POD_SOURCE}
|
||||||
|
|
||||||
|
${VK_DEVICE_SOURCE}
|
||||||
|
${VK_DESCRIPTOR_SETS_SOURCE}
|
||||||
|
${VK_SHADER_SOURCE}
|
||||||
|
${VK_MATERIAL_SOURCE}
|
||||||
|
${VK_RENDER_PASS_SOURCE}
|
||||||
|
${RENDER_DEVICE_VULKAN_SOURCE})
|
||||||
|
|
||||||
set_property(TARGET ULRE.RenderDevice.Vulkan PROPERTY FOLDER "ULRE")
|
set_property(TARGET ULRE.RenderDevice.Vulkan PROPERTY FOLDER "ULRE")
|
||||||
|
164
src/RenderDevice/Vulkan/POD/VKTextureLoader.cpp
Normal file
164
src/RenderDevice/Vulkan/POD/VKTextureLoader.cpp
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
#include<hgl/graph/vulkan/VK.h>
|
||||||
|
#include<hgl/graph/vulkan/VKDevice.h>
|
||||||
|
#include<hgl/graph/vulkan/VKBuffer.h>
|
||||||
|
#include<hgl/io/FileInputStream.h>
|
||||||
|
#include<hgl/log/LogInfo.h>
|
||||||
|
|
||||||
|
VK_NAMESPACE_BEGIN
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct PixelFormat
|
||||||
|
{
|
||||||
|
VkFormat format;
|
||||||
|
|
||||||
|
uint8 channels; //颜色通道数
|
||||||
|
char colors[4];
|
||||||
|
uint8 bits[4];
|
||||||
|
VulkanDataType type;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
const uint GetPixelBytes()const{return (bits[0]+bits[1]+bits[2]+bits[3])>>3;} ///<获取单个象素所需字节数
|
||||||
|
};//
|
||||||
|
|
||||||
|
constexpr PixelFormat pf_list[]=
|
||||||
|
{
|
||||||
|
{UFMT_BGRA4, 4,{'B','G','R','A'},{ 4, 4, 4, 4},VulkanDataType::UNORM},
|
||||||
|
{UFMT_RGB565, 3,{'R','G','B', 0 },{ 5, 6, 5, 0},VulkanDataType::UNORM},
|
||||||
|
{UFMT_A1RGB5, 4,{'A','R','G','B'},{ 1, 5, 5, 5},VulkanDataType::UNORM},
|
||||||
|
{UFMT_R8, 1,{'R', 0 , 0 , 0 },{ 8, 0, 0, 0},VulkanDataType::UNORM},
|
||||||
|
{UFMT_RG8, 2,{'R','G', 0 , 0 },{ 8, 8, 0, 0},VulkanDataType::UNORM},
|
||||||
|
{UFMT_RGBA8, 4,{'R','G','B','A'},{ 8, 8, 8, 8},VulkanDataType::UNORM},
|
||||||
|
{UFMT_A2BGR10, 4,{'A','B','G','R'},{ 2,10,10,10},VulkanDataType::UNORM},
|
||||||
|
{UFMT_R16, 1,{'R', 0 , 0 , 0 },{16, 0, 0, 0},VulkanDataType::UNORM},
|
||||||
|
{UFMT_R16F, 1,{'R', 0 , 0 , 0 },{16, 0, 0, 0},VulkanDataType::SFLOAT},
|
||||||
|
{UFMT_RG16, 2,{'R','G', 0 , 0 },{16,16, 0, 0},VulkanDataType::UNORM},
|
||||||
|
{UFMT_RG16F, 2,{'R','G', 0 , 0 },{16,16, 0, 0},VulkanDataType::SFLOAT},
|
||||||
|
{UFMT_RGBA16, 4,{'R','G','B','A'},{16,16,16,16},VulkanDataType::UNORM},
|
||||||
|
{UFMT_RGBA16F, 4,{'R','G','B','A'},{16,16,16,16},VulkanDataType::SFLOAT},
|
||||||
|
{UFMT_R32U, 1,{'R', 0 , 0 , 0 },{32, 0, 0, 0},VulkanDataType::UINT},
|
||||||
|
{UFMT_R32I, 1,{'R', 0 , 0 , 0 },{32, 0, 0, 0},VulkanDataType::SINT},
|
||||||
|
{UFMT_R32F, 1,{'R', 0 , 0 , 0 },{32, 0, 0, 0},VulkanDataType::SFLOAT},
|
||||||
|
{UFMT_RG32U, 2,{'R','G', 0 , 0 },{32,32, 0, 0},VulkanDataType::UINT},
|
||||||
|
{UFMT_RG32I, 2,{'R','G', 0 , 0 },{32,32, 0, 0},VulkanDataType::SINT},
|
||||||
|
{UFMT_RG32F, 2,{'R','G', 0 , 0 },{32,32, 0, 0},VulkanDataType::SFLOAT},
|
||||||
|
{UFMT_RGB32U, 3,{'R','G','B', 0 },{32,32,32, 0},VulkanDataType::UINT},
|
||||||
|
{UFMT_RGB32I, 3,{'R','G','B', 0 },{32,32,32, 0},VulkanDataType::SINT},
|
||||||
|
{UFMT_RGB32F, 3,{'R','G','B', 0 },{32,32,32, 0},VulkanDataType::SFLOAT},
|
||||||
|
{UFMT_RGBA32U, 4,{'R','G','B','A'},{32,32,32,32},VulkanDataType::UINT},
|
||||||
|
{UFMT_RGBA32I, 4,{'R','G','B','A'},{32,32,32,32},VulkanDataType::SINT},
|
||||||
|
{UFMT_RGBA32F, 4,{'R','G','B','A'},{32,32,32,32},VulkanDataType::SFLOAT}
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr uint PixelFormatCount=sizeof(pf_list)/sizeof(PixelFormat);
|
||||||
|
|
||||||
|
#pragma pack(push,1)
|
||||||
|
struct Tex2DFileHeader
|
||||||
|
{
|
||||||
|
uint8 id[6]; ///<Tex2D\x1A
|
||||||
|
uint8 version; ///<必须为2
|
||||||
|
bool mipmaps;
|
||||||
|
uint32 width;
|
||||||
|
uint32 height;
|
||||||
|
uint8 channels;
|
||||||
|
char colors[4];
|
||||||
|
uint8 bits[4];
|
||||||
|
uint8 datatype;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
const uint pixel_count()const{return width*height;}
|
||||||
|
const uint pixel_bytes()const{return (bits[0]+bits[1]+bits[2]+bits[3])>>3;}
|
||||||
|
const uint total_bytes()const{return pixel_count()*pixel_bytes();}
|
||||||
|
|
||||||
|
const VkFormat vk_format()const
|
||||||
|
{
|
||||||
|
const PixelFormat *pf=pf_list;
|
||||||
|
|
||||||
|
for(uint i=0;i<PixelFormatCount;i++,++pf)
|
||||||
|
{
|
||||||
|
if(channels!=pf->channels)continue;
|
||||||
|
if(datatype!=(uint8)pf->type)continue;
|
||||||
|
|
||||||
|
if(colors[0]!=pf->colors[0])continue;
|
||||||
|
if(colors[1]!=pf->colors[1])continue;
|
||||||
|
if(colors[2]!=pf->colors[2])continue;
|
||||||
|
if(colors[3]!=pf->colors[3])continue;
|
||||||
|
|
||||||
|
if(bits[0]!=pf->bits[0])continue;
|
||||||
|
if(bits[1]!=pf->bits[1])continue;
|
||||||
|
if(bits[2]!=pf->bits[2])continue;
|
||||||
|
if(bits[3]!=pf->bits[3])continue;
|
||||||
|
|
||||||
|
return pf->format;
|
||||||
|
}
|
||||||
|
|
||||||
|
return VK_FORMAT_UNDEFINED;
|
||||||
|
}
|
||||||
|
};//
|
||||||
|
#pragma pack(pop)
|
||||||
|
}//namespace
|
||||||
|
|
||||||
|
Texture2D *CreateTextureFromFile(Device *device,const OSString &filename,bool use_optimar)
|
||||||
|
{
|
||||||
|
if(!device)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
io::OpenFileInputStream fis(filename);
|
||||||
|
|
||||||
|
if(!fis)
|
||||||
|
{
|
||||||
|
LOG_ERROR(OS_TEXT("[ERROR] open texture file<")+filename+OS_TEXT("> failed."));
|
||||||
|
return(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
const int64 file_length=fis->GetSize();
|
||||||
|
|
||||||
|
if(file_length<sizeof(Tex2DFileHeader))
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
Tex2DFileHeader file_header;
|
||||||
|
|
||||||
|
if(fis->Read(&file_header,sizeof(Tex2DFileHeader))!=sizeof(Tex2DFileHeader))
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
if(file_header.version!=2)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
if(file_header.total_bytes()==0)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
const VkFormat format=file_header.vk_format();
|
||||||
|
|
||||||
|
if(format<=VK_FORMAT_BEGIN_RANGE
|
||||||
|
||format>=VK_FORMAT_END_RANGE)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
const uint total_bytes=file_header.total_bytes();
|
||||||
|
|
||||||
|
if(file_length<sizeof(Tex2DFileHeader)+total_bytes)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
if(use_optimar)
|
||||||
|
{
|
||||||
|
vulkan::Buffer *buf=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,total_bytes);
|
||||||
|
|
||||||
|
if(!buf)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
void *pixel_data=buf->Map();
|
||||||
|
|
||||||
|
fis->Read(pixel_data,total_bytes);
|
||||||
|
|
||||||
|
buf->Unmap();
|
||||||
|
|
||||||
|
Texture2D *tex=device->CreateTexture2D(format,buf,file_header.width,file_header.height);
|
||||||
|
|
||||||
|
return tex;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VK_NAMESPACE_END
|
Loading…
x
Reference in New Issue
Block a user