增加TGATexture.cpp/texture_rect,完成纹理提交代码,但似乎有报错,待修正

This commit is contained in:
2019-05-19 13:02:35 +08:00
parent ffc23403aa
commit 476a60b99e
9 changed files with 379 additions and 13 deletions

View File

@@ -3,5 +3,10 @@
target_link_libraries(${name} ${ULRE})
endmacro()
add_library(TGATexture STATIC TGATexture.cpp)
CreateProject(0.triangle main)
CreateProject(1.indices_rect indices_rect)
CreateProject(2.texture_rect texture_rect)
target_link_libraries(2.texture_rect TGATexture)

View File

@@ -0,0 +1,132 @@
#include<hgl/graph/vulkan/VK.h>
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/filesystem/FileSystem.h>
VK_NAMESPACE_BEGIN
namespace
{
#pragma pack(push,1)
struct TGAHeader
{
uint8 id;
uint8 color_map_type;
uint8 image_type; // 1 colormap image ,2 true-color,3 grayscale
uint16 color_map_first;
uint16 color_map_length;
uint8 color_map_size;
uint16 x_origin;
uint16 y_origin;
uint16 width;
uint16 height;
uint8 bit;
uint8 image_desc;
};
union TGAImageDesc
{
//不要把此union放到上面的struct中否则Visual C++会将此union编译成4字节。GCC无此问题
uint8 image_desc;
struct
{
uint alpha_depth:4;
uint reserved:1;
uint direction:1; //0 lower-left,1 upper left
};
};
#pragma pack(pop)
void RGB8to565(uint8 *data,uint size)
{
uint8 *src=data;
uint16 *target=(uint16 *)data;
for(uint i=0;i<size;i++)
{
*target=(((src[0])<<8)&0xF800)
|(((src[1])<<3)&0x7E0)
| ((src[2])>>3);
++target;
src+=3;
}
}
}//namespace
Texture2D *LoadTGATexture(const OSString &filename,Device *device)
{
uint8 *data;
const int64 file_length=filesystem::LoadFileToMemory(filename,(void **)&data);
if(file_length<=0)
{
std::cerr<<"[ERROR] open file<"<<filename.c_str()<<"> failed."<<std::endl;
return(nullptr);
}
TGAHeader *header=(TGAHeader *)data;
VkFormat format;
uint pixels_size=0;
if(header->image_type==2)
{
if(header->bit==24)
{
RGB8to565(data+sizeof(TGAHeader),header->width*header->height);
format=FMT_RGB565;
pixels_size=header->width*header->height*2;
}
else if(header->bit==32)
{
format=FMT_RGBA8UN;
pixels_size=header->width*header->height*4;
}
}
else if(header->image_type==3&&header->bit==8)
{
format=FMT_R8UN;
pixels_size=header->width*header->height;
}
else
{
std::cerr<<"[ERROR] Image format error,filename: "<<filename.c_str()<<std::endl;
delete[] data;
return(nullptr);
}
Texture2D *tex=device->CreateTexture2D(format,data+sizeof(TGAHeader),header->width,header->height,pixels_size);
if(tex)
{
std::cout<<"load image file<"<<filename.c_str()<<">:<"<<header->width<<"x"<<header->height<<"> to texture ok"<<std::endl;
}
else
{
std::cout<<"load image file<"<<filename.c_str()<<">:<"<<header->width<<"x"<<header->height<<"> to texture failed."<<std::endl;
}
delete[] data;
return(tex);
}
//GLuint CreateSamplerObject(GLint min_filter,GLint mag_filter,GLint clamp,const GLfloat *border_color)
//{
// GLuint sampler_object;
// glGenSamplers(1,&sampler_object);
// glSamplerParameteri(sampler_object,GL_TEXTURE_MIN_FILTER,min_filter);
// glSamplerParameteri(sampler_object,GL_TEXTURE_MAG_FILTER,mag_filter);
// glSamplerParameteri(sampler_object,GL_TEXTURE_WRAP_S,clamp);
// glSamplerParameteri(sampler_object,GL_TEXTURE_WRAP_T,clamp);
// glSamplerParameterfv(sampler_object,GL_TEXTURE_BORDER_COLOR,border_color);
// return(sampler_object);
//}
VK_NAMESPACE_END

View File

@@ -104,7 +104,7 @@ private:
bool InitPipeline()
{
vulkan::PipelineCreater *
pipeline_creater=new vulkan::PipelineCreater(device,material,device->GetRenderPass());
pipeline_creater=new vulkan::PipelineCreater(device,material,device->GetRenderPass(),device->GetExtent());
pipeline_creater->SetDepthTest(false);
pipeline_creater->SetDepthWrite(false);
pipeline_creater->CloseCullFace();

View File

@@ -0,0 +1,205 @@
// 1.indices_rect
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>0.triangle<6C>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾʹ<CABE><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#include"VulkanAppFramework.h"
#include<hgl/graph/vulkan/VKTexture.h>
#include<hgl/math/Math.h>
using namespace hgl;
using namespace hgl::graph;
VK_NAMESPACE_BEGIN
Texture2D *LoadTGATexture(const OSString &filename,Device *device);
VK_NAMESPACE_END
constexpr uint32_t SCREEN_WIDTH=128;
constexpr uint32_t SCREEN_HEIGHT=128;
struct WorldConfig
{
Matrix4f mvp;
}world;
constexpr uint32_t VERTEX_COUNT=4;
constexpr float vertex_data[VERTEX_COUNT][2]=
{
{SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.25},
{SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.25},
{SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.75},
{SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.75}
};
constexpr uint32_t INDEX_COUNT=6;
constexpr uint16 index_data[INDEX_COUNT]=
{
0,1,3,
0,3,2
};
class TestApp:public VulkanApplicationFramework
{
private:
uint swap_chain_count=0;
vulkan::Material * material =nullptr;
vulkan::Texture2D * texture =nullptr;
vulkan::DescriptorSets * desciptor_sets =nullptr;
vulkan::Renderable * render_obj =nullptr;
vulkan::Buffer * ubo_mvp =nullptr;
vulkan::Pipeline * pipeline =nullptr;
vulkan::CommandBuffer ** cmd_buf =nullptr;
vulkan::VertexBuffer * vertex_buffer =nullptr;
vulkan::IndexBuffer * index_buffer =nullptr;
public:
~TestApp()
{
SAFE_CLEAR(index_buffer);
SAFE_CLEAR(vertex_buffer);
SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count);
SAFE_CLEAR(pipeline);
SAFE_CLEAR(ubo_mvp);
SAFE_CLEAR(render_obj);
SAFE_CLEAR(desciptor_sets);
SAFE_CLEAR(texture);
SAFE_CLEAR(material);
}
private:
bool InitMaterial()
{
material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition.vert.spv"),
OS_TEXT("FlatColor.frag.spv"));
if(!material)
return(false);
render_obj=material->CreateRenderable();
desciptor_sets=material->CreateDescriptorSets();
texture=vulkan::LoadTGATexture(OS_TEXT("lena.tga"),device);
return(true);
}
bool InitUBO()
{
const VkExtent2D extent=device->GetExtent();
world.mvp=ortho(extent.width,extent.height);
ubo_mvp=device->CreateUBO(sizeof(WorldConfig),&world);
if(!ubo_mvp)
return(false);
return desciptor_sets->UpdateUBO(material->GetUBO("world"),*ubo_mvp);
}
void InitVBO()
{
vertex_buffer =device->CreateVBO(FMT_RG32F,VERTEX_COUNT,vertex_data);
index_buffer =device->CreateIBO16(INDEX_COUNT,index_data);
render_obj->Set("Vertex",vertex_buffer);
render_obj->Set(index_buffer);
}
bool InitPipeline()
{
vulkan::PipelineCreater *
pipeline_creater=new vulkan::PipelineCreater(device,material,device->GetRenderPass(),device->GetExtent());
pipeline_creater->SetDepthTest(false);
pipeline_creater->SetDepthWrite(false);
pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_TRIANGLES);
pipeline=pipeline_creater->Create();
delete pipeline_creater;
pipeline_creater=nullptr;
return pipeline;
}
bool InitCommandBuffer()
{
cmd_buf=hgl_zero_new<vulkan::CommandBuffer *>(swap_chain_count);
for(uint i=0;i<swap_chain_count;i++)
{
cmd_buf[i]=device->CreateCommandBuffer();
if(!cmd_buf[i])
return(false);
cmd_buf[i]->Begin();
cmd_buf[i]->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(i));
cmd_buf[i]->Bind(pipeline);
cmd_buf[i]->Bind(desciptor_sets);
cmd_buf[i]->Bind(render_obj);
cmd_buf[i]->DrawIndexed(INDEX_COUNT);
cmd_buf[i]->EndRenderPass();
cmd_buf[i]->End();
}
return(true);
}
public:
bool Init()
{
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
swap_chain_count=device->GetSwapChainImageCount();
if(!InitMaterial())
return(false);
if(!InitUBO())
return(false);
InitVBO();
if(!InitPipeline())
return(false);
if(!InitCommandBuffer())
return(false);
return(true);
}
void Draw() override
{
const uint32_t frame_index=device->GetCurrentFrameIndices();
const vulkan::CommandBuffer *cb=cmd_buf[frame_index];
Submit(*cb);
}
};//class TestApp:public VulkanApplicationFramework
int main(int,char **)
{
#ifdef _DEBUG
if(!vulkan::CheckStrideBytesByFormat())
return 0xff;
#endif//
TestApp app;
if(!app.Init())
return(-1);
while(app.Run());
return 0;
}