1.修正DescriptorSetLayout的不正确数量定义
2.完成贴图测试
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include<hgl/graph/vulkan/VK.h>
|
||||
#include<hgl/graph/vulkan/VKDevice.h>
|
||||
#include<hgl/filesystem/FileSystem.h>
|
||||
#include<hgl/LogInfo.h>
|
||||
|
||||
VK_NAMESPACE_BEGIN
|
||||
namespace
|
||||
@@ -54,6 +55,25 @@ namespace
|
||||
src+=3;
|
||||
}
|
||||
}
|
||||
|
||||
void SwapRow(uint8 *data,uint line_size,uint height)
|
||||
{
|
||||
uint8 *top=data;
|
||||
uint8 *bottom=data+(height-1)*line_size;
|
||||
uint8 *tmp=new uint8[line_size];
|
||||
|
||||
while(top<bottom)
|
||||
{
|
||||
memcpy(tmp,bottom,line_size);
|
||||
memcpy(bottom,top,line_size);
|
||||
memcpy(top,tmp,line_size);
|
||||
|
||||
top+=line_size;
|
||||
bottom-=line_size;
|
||||
}
|
||||
|
||||
delete[] tmp;
|
||||
}
|
||||
}//namespace
|
||||
|
||||
Texture2D *LoadTGATexture(const OSString &filename,Device *device)
|
||||
@@ -63,70 +83,61 @@ Texture2D *LoadTGATexture(const OSString &filename,Device *device)
|
||||
|
||||
if(file_length<=0)
|
||||
{
|
||||
std::cerr<<"[ERROR] open file<"<<filename.c_str()<<"> failed."<<std::endl;
|
||||
LOG_ERROR(OS_TEXT("[ERROR] open file<")+filename+OS_TEXT("> failed."));
|
||||
return(nullptr);
|
||||
}
|
||||
|
||||
TGAHeader *header=(TGAHeader *)data;
|
||||
TGAImageDesc image_desc;
|
||||
uint8 *pixel_data=data+sizeof(TGAHeader);
|
||||
|
||||
image_desc.image_desc=header->image_desc;
|
||||
|
||||
VkFormat format;
|
||||
uint pixels_size=0;
|
||||
uint line_size;
|
||||
|
||||
if(header->image_type==2)
|
||||
{
|
||||
if(header->bit==24)
|
||||
{
|
||||
RGB8to565(data+sizeof(TGAHeader),header->width*header->height);
|
||||
RGB8to565(pixel_data,header->width*header->height);
|
||||
|
||||
format=FMT_RGB565;
|
||||
pixels_size=header->width*header->height*2;
|
||||
format=FMT_BGR565;
|
||||
line_size=header->width*2;
|
||||
}
|
||||
else if(header->bit==32)
|
||||
{
|
||||
format=FMT_RGBA8UN;
|
||||
pixels_size=header->width*header->height*4;
|
||||
line_size=header->width*4;
|
||||
}
|
||||
}
|
||||
else if(header->image_type==3&&header->bit==8)
|
||||
{
|
||||
format=FMT_R8UN;
|
||||
pixels_size=header->width*header->height;
|
||||
line_size=header->width;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr<<"[ERROR] Image format error,filename: "<<filename.c_str()<<std::endl;
|
||||
LOG_ERROR(OS_TEXT("[ERROR] Image format error,filename: ")+filename);
|
||||
delete[] data;
|
||||
return(nullptr);
|
||||
}
|
||||
|
||||
Texture2D *tex=device->CreateTexture2D(format,data+sizeof(TGAHeader),header->width,header->height,pixels_size);
|
||||
if(image_desc.direction==0)
|
||||
SwapRow(pixel_data,line_size,header->height);
|
||||
|
||||
Texture2D *tex=device->CreateTexture2D(format,pixel_data,header->width,header->height,line_size*header->height);
|
||||
|
||||
if(tex)
|
||||
{
|
||||
std::cout<<"load image file<"<<filename.c_str()<<">:<"<<header->width<<"x"<<header->height<<"> to texture ok"<<std::endl;
|
||||
LOG_INFO(OS_TEXT("load image file<")+filename+OS_TEXT(">:<")+OSString(header->width)+OS_TEXT("x")+OSString(header->height)+OS_TEXT("> to texture ok"));
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout<<"load image file<"<<filename.c_str()<<">:<"<<header->width<<"x"<<header->height<<"> to texture failed."<<std::endl;
|
||||
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);
|
||||
}
|
||||
|
||||
//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
|
@@ -3,6 +3,7 @@
|
||||
|
||||
#include"VulkanAppFramework.h"
|
||||
#include<hgl/graph/vulkan/VKTexture.h>
|
||||
#include<hgl/graph/vulkan/VKSampler.h>
|
||||
#include<hgl/math/Math.h>
|
||||
|
||||
using namespace hgl;
|
||||
@@ -24,10 +25,18 @@ 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}
|
||||
{0, 0},
|
||||
{SCREEN_WIDTH, 0},
|
||||
{0, SCREEN_HEIGHT},
|
||||
{SCREEN_WIDTH, SCREEN_HEIGHT}
|
||||
};
|
||||
|
||||
constexpr float tex_coord_data[VERTEX_COUNT][2]=
|
||||
{
|
||||
{0,0},
|
||||
{1,0},
|
||||
{0,1},
|
||||
{1,1}
|
||||
};
|
||||
|
||||
constexpr uint32_t INDEX_COUNT=6;
|
||||
@@ -46,6 +55,7 @@ private:
|
||||
|
||||
vulkan::Material * material =nullptr;
|
||||
vulkan::Texture2D * texture =nullptr;
|
||||
vulkan::Sampler * sampler =nullptr;
|
||||
vulkan::DescriptorSets * desciptor_sets =nullptr;
|
||||
vulkan::Renderable * render_obj =nullptr;
|
||||
vulkan::Buffer * ubo_mvp =nullptr;
|
||||
@@ -54,6 +64,7 @@ private:
|
||||
vulkan::CommandBuffer ** cmd_buf =nullptr;
|
||||
|
||||
vulkan::VertexBuffer * vertex_buffer =nullptr;
|
||||
vulkan::VertexBuffer * tex_coord_buffer =nullptr;
|
||||
vulkan::IndexBuffer * index_buffer =nullptr;
|
||||
|
||||
public:
|
||||
@@ -61,12 +72,14 @@ public:
|
||||
~TestApp()
|
||||
{
|
||||
SAFE_CLEAR(index_buffer);
|
||||
SAFE_CLEAR(tex_coord_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(sampler);
|
||||
SAFE_CLEAR(texture);
|
||||
SAFE_CLEAR(material);
|
||||
}
|
||||
@@ -75,8 +88,8 @@ private:
|
||||
|
||||
bool InitMaterial()
|
||||
{
|
||||
material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition.vert.spv"),
|
||||
OS_TEXT("FlatColor.frag.spv"));
|
||||
material=shader_manage->CreateMaterial(OS_TEXT("FlatTexture.vert.spv"),
|
||||
OS_TEXT("FlatTexture.frag.spv"));
|
||||
if(!material)
|
||||
return(false);
|
||||
|
||||
@@ -84,6 +97,28 @@ private:
|
||||
desciptor_sets=material->CreateDescriptorSets();
|
||||
|
||||
texture=vulkan::LoadTGATexture(OS_TEXT("lena.tga"),device);
|
||||
|
||||
VkSamplerCreateInfo sampler_create_info{};
|
||||
|
||||
sampler_create_info.magFilter = VK_FILTER_LINEAR;
|
||||
sampler_create_info.minFilter = VK_FILTER_LINEAR;
|
||||
sampler_create_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
||||
sampler_create_info.addressModeU= VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||
sampler_create_info.addressModeV= VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||
sampler_create_info.addressModeW= VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||
sampler_create_info.mipLodBias = 0.0f;
|
||||
sampler_create_info.compareOp = VK_COMPARE_OP_NEVER;
|
||||
sampler_create_info.minLod = 0.0f;
|
||||
sampler_create_info.maxLod = 1.0f;
|
||||
|
||||
sampler=device->CreateSampler(&sampler_create_info);
|
||||
|
||||
VkDescriptorImageInfo image_info;
|
||||
image_info.imageView =*texture;
|
||||
image_info.imageLayout =*texture;
|
||||
image_info.sampler =*sampler;
|
||||
|
||||
desciptor_sets->UpdateSampler(material->GetCombindImageSampler("texture_lena"),&image_info);
|
||||
return(true);
|
||||
}
|
||||
|
||||
@@ -104,9 +139,11 @@ private:
|
||||
void InitVBO()
|
||||
{
|
||||
vertex_buffer =device->CreateVBO(FMT_RG32F,VERTEX_COUNT,vertex_data);
|
||||
tex_coord_buffer=device->CreateVBO(FMT_RG32F,VERTEX_COUNT,tex_coord_data);
|
||||
index_buffer =device->CreateIBO16(INDEX_COUNT,index_data);
|
||||
|
||||
render_obj->Set("Vertex",vertex_buffer);
|
||||
render_obj->Set("TexCoord",tex_coord_buffer);
|
||||
render_obj->Set(index_buffer);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user