1.修正DescriptorSetLayout的不正确数量定义
2.完成贴图测试
This commit is contained in:
parent
fa4b77010e
commit
56c6bc8d6e
@ -1,6 +1,7 @@
|
|||||||
#include<hgl/graph/vulkan/VK.h>
|
#include<hgl/graph/vulkan/VK.h>
|
||||||
#include<hgl/graph/vulkan/VKDevice.h>
|
#include<hgl/graph/vulkan/VKDevice.h>
|
||||||
#include<hgl/filesystem/FileSystem.h>
|
#include<hgl/filesystem/FileSystem.h>
|
||||||
|
#include<hgl/LogInfo.h>
|
||||||
|
|
||||||
VK_NAMESPACE_BEGIN
|
VK_NAMESPACE_BEGIN
|
||||||
namespace
|
namespace
|
||||||
@ -54,6 +55,25 @@ namespace
|
|||||||
src+=3;
|
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
|
}//namespace
|
||||||
|
|
||||||
Texture2D *LoadTGATexture(const OSString &filename,Device *device)
|
Texture2D *LoadTGATexture(const OSString &filename,Device *device)
|
||||||
@ -63,70 +83,61 @@ Texture2D *LoadTGATexture(const OSString &filename,Device *device)
|
|||||||
|
|
||||||
if(file_length<=0)
|
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);
|
return(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
TGAHeader *header=(TGAHeader *)data;
|
TGAHeader *header=(TGAHeader *)data;
|
||||||
|
TGAImageDesc image_desc;
|
||||||
|
uint8 *pixel_data=data+sizeof(TGAHeader);
|
||||||
|
|
||||||
|
image_desc.image_desc=header->image_desc;
|
||||||
|
|
||||||
VkFormat format;
|
VkFormat format;
|
||||||
uint pixels_size=0;
|
uint line_size;
|
||||||
|
|
||||||
if(header->image_type==2)
|
if(header->image_type==2)
|
||||||
{
|
{
|
||||||
if(header->bit==24)
|
if(header->bit==24)
|
||||||
{
|
{
|
||||||
RGB8to565(data+sizeof(TGAHeader),header->width*header->height);
|
RGB8to565(pixel_data,header->width*header->height);
|
||||||
|
|
||||||
format=FMT_RGB565;
|
format=FMT_BGR565;
|
||||||
pixels_size=header->width*header->height*2;
|
line_size=header->width*2;
|
||||||
}
|
}
|
||||||
else if(header->bit==32)
|
else if(header->bit==32)
|
||||||
{
|
{
|
||||||
format=FMT_RGBA8UN;
|
format=FMT_RGBA8UN;
|
||||||
pixels_size=header->width*header->height*4;
|
line_size=header->width*4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(header->image_type==3&&header->bit==8)
|
else if(header->image_type==3&&header->bit==8)
|
||||||
{
|
{
|
||||||
format=FMT_R8UN;
|
format=FMT_R8UN;
|
||||||
pixels_size=header->width*header->height;
|
line_size=header->width;
|
||||||
}
|
}
|
||||||
else
|
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;
|
delete[] data;
|
||||||
return(nullptr);
|
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)
|
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
|
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;
|
delete[] data;
|
||||||
return(tex);
|
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
|
VK_NAMESPACE_END
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include"VulkanAppFramework.h"
|
#include"VulkanAppFramework.h"
|
||||||
#include<hgl/graph/vulkan/VKTexture.h>
|
#include<hgl/graph/vulkan/VKTexture.h>
|
||||||
|
#include<hgl/graph/vulkan/VKSampler.h>
|
||||||
#include<hgl/math/Math.h>
|
#include<hgl/math/Math.h>
|
||||||
|
|
||||||
using namespace hgl;
|
using namespace hgl;
|
||||||
@ -24,10 +25,18 @@ constexpr uint32_t VERTEX_COUNT=4;
|
|||||||
|
|
||||||
constexpr float vertex_data[VERTEX_COUNT][2]=
|
constexpr float vertex_data[VERTEX_COUNT][2]=
|
||||||
{
|
{
|
||||||
{SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.25},
|
{0, 0},
|
||||||
{SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.25},
|
{SCREEN_WIDTH, 0},
|
||||||
{SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.75},
|
{0, SCREEN_HEIGHT},
|
||||||
{SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.75}
|
{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;
|
constexpr uint32_t INDEX_COUNT=6;
|
||||||
@ -46,6 +55,7 @@ private:
|
|||||||
|
|
||||||
vulkan::Material * material =nullptr;
|
vulkan::Material * material =nullptr;
|
||||||
vulkan::Texture2D * texture =nullptr;
|
vulkan::Texture2D * texture =nullptr;
|
||||||
|
vulkan::Sampler * sampler =nullptr;
|
||||||
vulkan::DescriptorSets * desciptor_sets =nullptr;
|
vulkan::DescriptorSets * desciptor_sets =nullptr;
|
||||||
vulkan::Renderable * render_obj =nullptr;
|
vulkan::Renderable * render_obj =nullptr;
|
||||||
vulkan::Buffer * ubo_mvp =nullptr;
|
vulkan::Buffer * ubo_mvp =nullptr;
|
||||||
@ -54,6 +64,7 @@ private:
|
|||||||
vulkan::CommandBuffer ** cmd_buf =nullptr;
|
vulkan::CommandBuffer ** cmd_buf =nullptr;
|
||||||
|
|
||||||
vulkan::VertexBuffer * vertex_buffer =nullptr;
|
vulkan::VertexBuffer * vertex_buffer =nullptr;
|
||||||
|
vulkan::VertexBuffer * tex_coord_buffer =nullptr;
|
||||||
vulkan::IndexBuffer * index_buffer =nullptr;
|
vulkan::IndexBuffer * index_buffer =nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -61,12 +72,14 @@ public:
|
|||||||
~TestApp()
|
~TestApp()
|
||||||
{
|
{
|
||||||
SAFE_CLEAR(index_buffer);
|
SAFE_CLEAR(index_buffer);
|
||||||
|
SAFE_CLEAR(tex_coord_buffer);
|
||||||
SAFE_CLEAR(vertex_buffer);
|
SAFE_CLEAR(vertex_buffer);
|
||||||
SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count);
|
SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count);
|
||||||
SAFE_CLEAR(pipeline);
|
SAFE_CLEAR(pipeline);
|
||||||
SAFE_CLEAR(ubo_mvp);
|
SAFE_CLEAR(ubo_mvp);
|
||||||
SAFE_CLEAR(render_obj);
|
SAFE_CLEAR(render_obj);
|
||||||
SAFE_CLEAR(desciptor_sets);
|
SAFE_CLEAR(desciptor_sets);
|
||||||
|
SAFE_CLEAR(sampler);
|
||||||
SAFE_CLEAR(texture);
|
SAFE_CLEAR(texture);
|
||||||
SAFE_CLEAR(material);
|
SAFE_CLEAR(material);
|
||||||
}
|
}
|
||||||
@ -75,8 +88,8 @@ private:
|
|||||||
|
|
||||||
bool InitMaterial()
|
bool InitMaterial()
|
||||||
{
|
{
|
||||||
material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition.vert.spv"),
|
material=shader_manage->CreateMaterial(OS_TEXT("FlatTexture.vert.spv"),
|
||||||
OS_TEXT("FlatColor.frag.spv"));
|
OS_TEXT("FlatTexture.frag.spv"));
|
||||||
if(!material)
|
if(!material)
|
||||||
return(false);
|
return(false);
|
||||||
|
|
||||||
@ -84,6 +97,28 @@ private:
|
|||||||
desciptor_sets=material->CreateDescriptorSets();
|
desciptor_sets=material->CreateDescriptorSets();
|
||||||
|
|
||||||
texture=vulkan::LoadTGATexture(OS_TEXT("lena.tga"),device);
|
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);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,9 +139,11 @@ private:
|
|||||||
void InitVBO()
|
void InitVBO()
|
||||||
{
|
{
|
||||||
vertex_buffer =device->CreateVBO(FMT_RG32F,VERTEX_COUNT,vertex_data);
|
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);
|
index_buffer =device->CreateIBO16(INDEX_COUNT,index_data);
|
||||||
|
|
||||||
render_obj->Set("Vertex",vertex_buffer);
|
render_obj->Set("Vertex",vertex_buffer);
|
||||||
|
render_obj->Set("TexCoord",tex_coord_buffer);
|
||||||
render_obj->Set(index_buffer);
|
render_obj->Set(index_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ public:
|
|||||||
|
|
||||||
bool BeginRenderPass(RenderPass *rp,Framebuffer *fb);
|
bool BeginRenderPass(RenderPass *rp,Framebuffer *fb);
|
||||||
bool Bind(Pipeline *p);
|
bool Bind(Pipeline *p);
|
||||||
bool Bind(DescriptorSets *,int first=0,int count=0);
|
bool Bind(DescriptorSets *);
|
||||||
bool Bind(Renderable *);
|
bool Bind(Renderable *);
|
||||||
void EndRenderPass();
|
void EndRenderPass();
|
||||||
bool End();
|
bool End();
|
||||||
|
@ -10,7 +10,7 @@ class DescriptorSets
|
|||||||
{
|
{
|
||||||
Device *device;
|
Device *device;
|
||||||
int count;
|
int count;
|
||||||
VkDescriptorSet *desc_set_list;
|
VkDescriptorSet desc_set;
|
||||||
const Map<uint32_t,int> *index_by_binding;
|
const Map<uint32_t,int> *index_by_binding;
|
||||||
|
|
||||||
VkPipelineLayout pipeline_layout;
|
VkPipelineLayout pipeline_layout;
|
||||||
@ -19,31 +19,27 @@ private:
|
|||||||
|
|
||||||
friend class DescriptorSetLayoutCreater;
|
friend class DescriptorSetLayoutCreater;
|
||||||
|
|
||||||
DescriptorSets(Device *dev,const int c,VkPipelineLayout pl,VkDescriptorSet *desc_set,const Map<uint32_t,int> *bi):index_by_binding(bi)
|
DescriptorSets(Device *dev,const int c,VkPipelineLayout pl,VkDescriptorSet ds,const Map<uint32_t,int> *bi):index_by_binding(bi)
|
||||||
{
|
{
|
||||||
device=dev;
|
device=dev;
|
||||||
count=c;
|
count=c;
|
||||||
desc_set_list=desc_set;
|
desc_set=ds;
|
||||||
pipeline_layout=pl;
|
pipeline_layout=pl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
~DescriptorSets();
|
~DescriptorSets()=default;
|
||||||
|
|
||||||
const uint32_t GetCount ()const{return count;}
|
const uint32_t GetCount ()const{return count;}
|
||||||
const VkDescriptorSet * GetDescriptorSets ()const{return desc_set_list;}
|
const VkDescriptorSet * GetDescriptorSets ()const{return &desc_set;}
|
||||||
VkDescriptorSet GetDescriptorSet (const uint32_t binding)const;
|
|
||||||
const VkPipelineLayout GetPipelineLayout ()const{return pipeline_layout;}
|
const VkPipelineLayout GetPipelineLayout ()const{return pipeline_layout;}
|
||||||
|
|
||||||
bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info);
|
//未来统合所有的write descriptor sets,这里的update改为只是添加记录
|
||||||
//bool UpdateUBO(const UTF8String &name,const VkDescriptorBufferInfo *buf_info)
|
//最终bind到cmd时一次性写入。
|
||||||
//{
|
|
||||||
// if(name.IsEmpty()||!buf_info)
|
|
||||||
// return(false);
|
|
||||||
|
|
||||||
// return UpdateUBO(GetUBOBinding(name),buf_info);
|
bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *);
|
||||||
//}
|
bool UpdateSampler(const uint32_t binding,const VkDescriptorImageInfo *);
|
||||||
};//class DescriptorSets
|
};//class DescriptorSets
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
#endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE
|
#endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE
|
||||||
|
@ -2,9 +2,11 @@
|
|||||||
#define HGL_GRAPH_VULKAN_TEXTURE_INCLUDE
|
#define HGL_GRAPH_VULKAN_TEXTURE_INCLUDE
|
||||||
|
|
||||||
#include<hgl/graph/vulkan/VK.h>
|
#include<hgl/graph/vulkan/VK.h>
|
||||||
|
#include<hgl/graph/vulkan/VKImageView.h>
|
||||||
VK_NAMESPACE_BEGIN
|
VK_NAMESPACE_BEGIN
|
||||||
struct TextureData
|
struct TextureData
|
||||||
{
|
{
|
||||||
|
VkDeviceMemory memory;
|
||||||
VkImage image;
|
VkImage image;
|
||||||
VkImageLayout image_layout;
|
VkImageLayout image_layout;
|
||||||
ImageView *image_view;
|
ImageView *image_view;
|
||||||
@ -23,7 +25,7 @@ public:
|
|||||||
|
|
||||||
operator VkImage (){return data?data->image:nullptr;}
|
operator VkImage (){return data?data->image:nullptr;}
|
||||||
operator VkImageLayout (){return data?data->image_layout:VK_IMAGE_LAYOUT_UNDEFINED;}
|
operator VkImageLayout (){return data?data->image_layout:VK_IMAGE_LAYOUT_UNDEFINED;}
|
||||||
operator ImageView * (){return data?data->image_view:nullptr;}
|
operator VkImageView (){return data?data->image_view->operator VkImageView():nullptr;}
|
||||||
|
|
||||||
const uint32 GetMipLevels()const{return data?data->mip_levels:0;}
|
const uint32 GetMipLevels()const{return data?data->mip_levels:0;}
|
||||||
const bool IsLinear ()const{return data?data->linear:false;}
|
const bool IsLinear ()const{return data?data->linear:false;}
|
||||||
|
11
res/shader/FlatTexture.frag
Normal file
11
res/shader/FlatTexture.frag
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#version 450 core
|
||||||
|
|
||||||
|
layout(binding = 2) uniform sampler2D texture_lena;
|
||||||
|
|
||||||
|
layout(location = 0) in vec2 FragmentTexCoord;
|
||||||
|
layout(location = 0) out vec4 FragColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor=texture(texture_lena,FragmentTexCoord);
|
||||||
|
}
|
18
res/shader/FlatTexture.vert
Normal file
18
res/shader/FlatTexture.vert
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#version 450 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec2 Vertex;
|
||||||
|
layout(location = 1) in vec2 TexCoord;
|
||||||
|
|
||||||
|
layout(binding = 0) uniform WorldConfig
|
||||||
|
{
|
||||||
|
mat4 mvp;
|
||||||
|
} world;
|
||||||
|
|
||||||
|
layout(location = 0) out vec2 FragmentTexCoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragmentTexCoord=TexCoord;
|
||||||
|
|
||||||
|
gl_Position=vec4(Vertex,0.0,1.0)*world.mvp;
|
||||||
|
}
|
@ -1,3 +1,6 @@
|
|||||||
glslangValidator -V -o FlatColor.vert.spv FlatColor.vert
|
glslangValidator -V -o FlatColor.vert.spv FlatColor.vert
|
||||||
glslangValidator -V -o OnlyPosition.vert.spv OnlyPosition.vert
|
glslangValidator -V -o OnlyPosition.vert.spv OnlyPosition.vert
|
||||||
glslangValidator -V -o FlatColor.frag.spv FlatColor.frag
|
glslangValidator -V -o FlatColor.frag.spv FlatColor.frag
|
||||||
|
|
||||||
|
glslangValidator -V -o FlatTexture.vert.spv FlatTexture.vert
|
||||||
|
glslangValidator -V -o FlatTexture.frag.spv FlatTexture.frag
|
||||||
|
@ -80,19 +80,12 @@ bool CommandBuffer::Bind(Pipeline *p)
|
|||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CommandBuffer::Bind(DescriptorSets *dsl,int first,int count)
|
bool CommandBuffer::Bind(DescriptorSets *dsl)
|
||||||
{
|
{
|
||||||
if(!dsl)
|
if(!dsl)
|
||||||
return(false);
|
return(false);
|
||||||
|
|
||||||
if(first<0)
|
vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,dsl->GetPipelineLayout(),0,1,dsl->GetDescriptorSets(),0,nullptr);
|
||||||
first=0;
|
|
||||||
|
|
||||||
if(count==0||first+count>dsl->GetCount())
|
|
||||||
count=dsl->GetCount()-first;
|
|
||||||
|
|
||||||
if(count>0)
|
|
||||||
vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,dsl->GetPipelineLayout(),first,count,dsl->GetDescriptorSets(),0,nullptr);
|
|
||||||
|
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
@ -3,31 +3,13 @@
|
|||||||
#include<hgl/graph/vulkan/VKDevice.h>
|
#include<hgl/graph/vulkan/VKDevice.h>
|
||||||
|
|
||||||
VK_NAMESPACE_BEGIN
|
VK_NAMESPACE_BEGIN
|
||||||
namespace
|
|
||||||
{
|
|
||||||
void DestroyDescriptorSetLayout(VkDevice device,const int count,VkDescriptorSetLayout *dsl_list)
|
|
||||||
{
|
|
||||||
if(count<=0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
for(int i=0;i<count;i++)
|
|
||||||
{
|
|
||||||
vkDestroyDescriptorSetLayout(device,*dsl_list,nullptr);
|
|
||||||
++dsl_list;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}//namespace
|
|
||||||
|
|
||||||
DescriptorSetLayoutCreater::~DescriptorSetLayoutCreater()
|
DescriptorSetLayoutCreater::~DescriptorSetLayoutCreater()
|
||||||
{
|
{
|
||||||
if(pipeline_layout)
|
if(pipeline_layout)
|
||||||
vkDestroyPipelineLayout(*device,pipeline_layout,nullptr);
|
vkDestroyPipelineLayout(*device,pipeline_layout,nullptr);
|
||||||
|
|
||||||
if(dsl_list)
|
if(dsl)
|
||||||
{
|
vkDestroyDescriptorSetLayout(*device,dsl,nullptr);
|
||||||
DestroyDescriptorSetLayout(*device,layout_binding_list.GetCount(),dsl_list);
|
|
||||||
delete[] dsl_list;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DescriptorSetLayoutCreater::Bind(const uint32_t binding,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags)
|
void DescriptorSetLayoutCreater::Bind(const uint32_t binding,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags)
|
||||||
@ -46,6 +28,8 @@ void DescriptorSetLayoutCreater::Bind(const uint32_t binding,VkDescriptorType de
|
|||||||
|
|
||||||
void DescriptorSetLayoutCreater::Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags)
|
void DescriptorSetLayoutCreater::Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags)
|
||||||
{
|
{
|
||||||
|
if(!binding||count<=0)return;
|
||||||
|
|
||||||
const uint old_count=layout_binding_list.GetCount();
|
const uint old_count=layout_binding_list.GetCount();
|
||||||
|
|
||||||
layout_binding_list.SetCount(old_count+count);
|
layout_binding_list.SetCount(old_count+count);
|
||||||
@ -80,30 +64,29 @@ bool DescriptorSetLayoutCreater::CreatePipelineLayout()
|
|||||||
descriptor_layout.bindingCount = count;
|
descriptor_layout.bindingCount = count;
|
||||||
descriptor_layout.pBindings = layout_binding_list.GetData();
|
descriptor_layout.pBindings = layout_binding_list.GetData();
|
||||||
|
|
||||||
dsl_list=new VkDescriptorSetLayout[count];
|
if(dsl)
|
||||||
|
vkDestroyDescriptorSetLayout(*device,dsl,nullptr);
|
||||||
|
|
||||||
if(vkCreateDescriptorSetLayout(*device,&descriptor_layout,nullptr,dsl_list)==VK_SUCCESS)
|
if(vkCreateDescriptorSetLayout(*device,&descriptor_layout,nullptr,&dsl)!=VK_SUCCESS)
|
||||||
{
|
return(false);
|
||||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
|
|
||||||
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
|
||||||
pPipelineLayoutCreateInfo.pNext = nullptr;
|
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||||
pPipelineLayoutCreateInfo.pushConstantRangeCount = 0;
|
pPipelineLayoutCreateInfo.pNext = nullptr;
|
||||||
pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
|
pPipelineLayoutCreateInfo.pushConstantRangeCount = 0;
|
||||||
pPipelineLayoutCreateInfo.setLayoutCount = count;
|
pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
|
||||||
pPipelineLayoutCreateInfo.pSetLayouts = dsl_list;
|
pPipelineLayoutCreateInfo.setLayoutCount = 1;
|
||||||
|
pPipelineLayoutCreateInfo.pSetLayouts = &dsl;
|
||||||
|
|
||||||
if(vkCreatePipelineLayout(*device,&pPipelineLayoutCreateInfo,nullptr,&pipeline_layout)==VK_SUCCESS)
|
if(vkCreatePipelineLayout(*device,&pPipelineLayoutCreateInfo,nullptr,&pipeline_layout)!=VK_SUCCESS)
|
||||||
return(true);
|
return(false);
|
||||||
}
|
|
||||||
|
|
||||||
delete[] dsl_list;
|
return(true);
|
||||||
dsl_list=nullptr;
|
|
||||||
return(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DescriptorSets *DescriptorSetLayoutCreater::Create()
|
DescriptorSets *DescriptorSetLayoutCreater::Create()
|
||||||
{
|
{
|
||||||
if(!pipeline_layout||!dsl_list)
|
if(!pipeline_layout||!dsl)
|
||||||
return(nullptr);
|
return(nullptr);
|
||||||
|
|
||||||
const int count=layout_binding_list.GetCount();
|
const int count=layout_binding_list.GetCount();
|
||||||
@ -115,16 +98,13 @@ DescriptorSets *DescriptorSetLayoutCreater::Create()
|
|||||||
alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||||||
alloc_info.pNext = nullptr;
|
alloc_info.pNext = nullptr;
|
||||||
alloc_info.descriptorPool = device->GetDescriptorPool();
|
alloc_info.descriptorPool = device->GetDescriptorPool();
|
||||||
alloc_info.descriptorSetCount = count;
|
alloc_info.descriptorSetCount = 1;
|
||||||
alloc_info.pSetLayouts = dsl_list;
|
alloc_info.pSetLayouts = &dsl;
|
||||||
|
|
||||||
VkDescriptorSet *desc_set=new VkDescriptorSet[count];
|
VkDescriptorSet desc_set;
|
||||||
|
|
||||||
if(vkAllocateDescriptorSets(*device,&alloc_info,desc_set)!=VK_SUCCESS)
|
if(vkAllocateDescriptorSets(*device,&alloc_info,&desc_set)!=VK_SUCCESS)
|
||||||
{
|
|
||||||
delete[] desc_set;
|
|
||||||
return(nullptr);
|
return(nullptr);
|
||||||
}
|
|
||||||
|
|
||||||
return(new DescriptorSets(device,count,pipeline_layout,desc_set,&index_by_binding));
|
return(new DescriptorSets(device,count,pipeline_layout,desc_set,&index_by_binding));
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ class DescriptorSetLayoutCreater
|
|||||||
Device *device;
|
Device *device;
|
||||||
|
|
||||||
List<VkDescriptorSetLayoutBinding> layout_binding_list;
|
List<VkDescriptorSetLayoutBinding> layout_binding_list;
|
||||||
VkDescriptorSetLayout *dsl_list=nullptr;
|
VkDescriptorSetLayout dsl=nullptr;
|
||||||
Map<uint32_t,int> index_by_binding;
|
Map<uint32_t,int> index_by_binding;
|
||||||
|
|
||||||
VkPipelineLayout pipeline_layout=nullptr;
|
VkPipelineLayout pipeline_layout=nullptr;
|
||||||
@ -25,11 +25,14 @@ public:
|
|||||||
|
|
||||||
void Bind(const uint32_t binding,VkDescriptorType,VkShaderStageFlagBits);
|
void Bind(const uint32_t binding,VkDescriptorType,VkShaderStageFlagBits);
|
||||||
void Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType type,VkShaderStageFlagBits stage);
|
void Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType type,VkShaderStageFlagBits stage);
|
||||||
void Bind(const ShaderResourceList &srl,VkDescriptorType type,VkShaderStageFlagBits stage){Bind(srl.binding_list.GetData(),srl.binding_list.GetCount(),type,stage);}
|
void Bind(const ShaderResourceList &srl,VkDescriptorType type,VkShaderStageFlagBits stage){if(srl.binding_list.GetCount()>0)Bind(srl.binding_list.GetData(),srl.binding_list.GetCount(),type,stage);}
|
||||||
void Bind(const ShaderResource &sr,VkShaderStageFlagBits stage)
|
void Bind(const ShaderResource &sr,VkShaderStageFlagBits stage)
|
||||||
{
|
{
|
||||||
for(uint32_t i=VK_DESCRIPTOR_TYPE_BEGIN_RANGE;i<=VK_DESCRIPTOR_TYPE_END_RANGE;i++)
|
for(uint32_t i=VK_DESCRIPTOR_TYPE_BEGIN_RANGE;i<=VK_DESCRIPTOR_TYPE_END_RANGE;i++)
|
||||||
Bind(sr[i],(VkDescriptorType)i,stage);
|
{
|
||||||
|
if(sr[i].binding_list.GetCount()>0)
|
||||||
|
Bind(sr[i],(VkDescriptorType)i,stage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//以下代码不再需要,使用一个void Bind(const ShaderResource &sr,VkShaderStageFlagBits stage)即可全部替代,而且更方便,但以此为提示
|
//以下代码不再需要,使用一个void Bind(const ShaderResource &sr,VkShaderStageFlagBits stage)即可全部替代,而且更方便,但以此为提示
|
||||||
|
@ -2,32 +2,12 @@
|
|||||||
#include<hgl/graph/vulkan/VKDevice.h>
|
#include<hgl/graph/vulkan/VKDevice.h>
|
||||||
|
|
||||||
VK_NAMESPACE_BEGIN
|
VK_NAMESPACE_BEGIN
|
||||||
VkDescriptorSet DescriptorSets::GetDescriptorSet(const uint32_t binding)const
|
|
||||||
{
|
|
||||||
int index;
|
|
||||||
|
|
||||||
if(!index_by_binding->Get(binding,index))
|
|
||||||
return(nullptr);
|
|
||||||
|
|
||||||
return desc_set_list[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
DescriptorSets::~DescriptorSets()
|
|
||||||
{
|
|
||||||
//if(count>0)
|
|
||||||
// vkFreeDescriptorSets(device->GetDevice(),device->GetDescriptorPool(),count,desc_set_list);
|
|
||||||
|
|
||||||
delete[] desc_set_list;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool DescriptorSets::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info)
|
bool DescriptorSets::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info)
|
||||||
{
|
{
|
||||||
VkDescriptorSet set=GetDescriptorSet(binding);
|
|
||||||
|
|
||||||
VkWriteDescriptorSet writeDescriptorSet = {};
|
VkWriteDescriptorSet writeDescriptorSet = {};
|
||||||
|
|
||||||
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||||
writeDescriptorSet.dstSet = set;
|
writeDescriptorSet.dstSet = desc_set;
|
||||||
writeDescriptorSet.descriptorCount = 1;
|
writeDescriptorSet.descriptorCount = 1;
|
||||||
writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||||
writeDescriptorSet.pBufferInfo = buf_info;
|
writeDescriptorSet.pBufferInfo = buf_info;
|
||||||
@ -36,4 +16,19 @@ bool DescriptorSets::UpdateUBO(const uint32_t binding,const VkDescriptorBufferIn
|
|||||||
vkUpdateDescriptorSets(*device,1,&writeDescriptorSet,0,nullptr);
|
vkUpdateDescriptorSets(*device,1,&writeDescriptorSet,0,nullptr);
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool DescriptorSets::UpdateSampler(const uint32_t binding,const VkDescriptorImageInfo *image_info)
|
||||||
|
{
|
||||||
|
VkWriteDescriptorSet writeDescriptorSet = {};
|
||||||
|
|
||||||
|
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||||
|
writeDescriptorSet.dstSet = desc_set;
|
||||||
|
writeDescriptorSet.descriptorCount = 1;
|
||||||
|
writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||||
|
writeDescriptorSet.pImageInfo = image_info;
|
||||||
|
writeDescriptorSet.dstBinding = binding;
|
||||||
|
|
||||||
|
vkUpdateDescriptorSets(*device,1,&writeDescriptorSet,0,nullptr);
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
|
@ -242,21 +242,18 @@ namespace
|
|||||||
|
|
||||||
VkDescriptorPool CreateDescriptorPool(VkDevice device,int sets_count)
|
VkDescriptorPool CreateDescriptorPool(VkDevice device,int sets_count)
|
||||||
{
|
{
|
||||||
constexpr size_t DESC_POOL_COUNT=1;
|
VkDescriptorPoolSize pool_size[2];
|
||||||
|
|
||||||
VkDescriptorPoolSize pool_size[DESC_POOL_COUNT];
|
pool_size[0].type=VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||||
|
pool_size[0].descriptorCount=1024;
|
||||||
for(size_t i=0;i<DESC_POOL_COUNT;i++)
|
pool_size[1].type=VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||||
{
|
pool_size[1].descriptorCount=1024;
|
||||||
pool_size[i].type=VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
|
||||||
pool_size[i].descriptorCount=1;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkDescriptorPoolCreateInfo dp_create_info={};
|
VkDescriptorPoolCreateInfo dp_create_info={};
|
||||||
dp_create_info.sType=VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
dp_create_info.sType=VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||||
dp_create_info.pNext=nullptr;
|
dp_create_info.pNext=nullptr;
|
||||||
dp_create_info.maxSets=sets_count;
|
dp_create_info.maxSets=sets_count;
|
||||||
dp_create_info.poolSizeCount=DESC_POOL_COUNT;
|
dp_create_info.poolSizeCount=2;
|
||||||
dp_create_info.pPoolSizes=pool_size;
|
dp_create_info.pPoolSizes=pool_size;
|
||||||
|
|
||||||
VkDescriptorPool desc_pool;
|
VkDescriptorPool desc_pool;
|
||||||
@ -327,7 +324,7 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device
|
|||||||
if(!CreateSwapchinAndImageView(attr))
|
if(!CreateSwapchinAndImageView(attr))
|
||||||
return(nullptr);
|
return(nullptr);
|
||||||
|
|
||||||
attr->desc_pool=CreateDescriptorPool(attr->device,1);
|
attr->desc_pool=CreateDescriptorPool(attr->device,1024);
|
||||||
|
|
||||||
if(!attr->desc_pool)
|
if(!attr->desc_pool)
|
||||||
return(nullptr);
|
return(nullptr);
|
||||||
|
@ -47,6 +47,7 @@ Texture2D *Device::CreateTexture2D(const VkFormat video_format,void *data,uint32
|
|||||||
|
|
||||||
TextureData *tex_data=new TextureData();
|
TextureData *tex_data=new TextureData();
|
||||||
|
|
||||||
|
tex_data->memory=nullptr;
|
||||||
tex_data->image=nullptr;
|
tex_data->image=nullptr;
|
||||||
tex_data->image_view=nullptr;
|
tex_data->image_view=nullptr;
|
||||||
|
|
||||||
@ -76,8 +77,6 @@ Texture2D *Device::CreateTexture2D(const VkFormat video_format,void *data,uint32
|
|||||||
|
|
||||||
tex_data->image_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
tex_data->image_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||||
|
|
||||||
VkDeviceMemory device_memory;
|
|
||||||
|
|
||||||
VkImageCreateInfo imageCreateInfo{};
|
VkImageCreateInfo imageCreateInfo{};
|
||||||
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
||||||
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
||||||
@ -101,8 +100,8 @@ Texture2D *Device::CreateTexture2D(const VkFormat video_format,void *data,uint32
|
|||||||
vkGetImageMemoryRequirements(attr->device, tex_data->image, &memReqs);
|
vkGetImageMemoryRequirements(attr->device, tex_data->image, &memReqs);
|
||||||
memAllocInfo.allocationSize = memReqs.size;
|
memAllocInfo.allocationSize = memReqs.size;
|
||||||
attr->CheckMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,&memAllocInfo.memoryTypeIndex);
|
attr->CheckMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,&memAllocInfo.memoryTypeIndex);
|
||||||
VK_CHECK_RESULT(vkAllocateMemory(attr->device, &memAllocInfo, nullptr, &device_memory))
|
VK_CHECK_RESULT(vkAllocateMemory(attr->device, &memAllocInfo, nullptr, &tex_data->memory))
|
||||||
VK_CHECK_RESULT(vkBindImageMemory(attr->device, tex_data->image, device_memory, 0))
|
VK_CHECK_RESULT(vkBindImageMemory(attr->device, tex_data->image, tex_data->memory, 0))
|
||||||
|
|
||||||
CommandBuffer *cmd_buf=CreateCommandBuffer();
|
CommandBuffer *cmd_buf=CreateCommandBuffer();
|
||||||
|
|
||||||
@ -188,12 +187,14 @@ Sampler *Device::CreateSampler(VkSamplerCreateInfo *sci)
|
|||||||
|
|
||||||
VkSampler sampler;
|
VkSampler sampler;
|
||||||
|
|
||||||
if(attr->physical_device->features.samplerAnisotropy)
|
sci->sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
||||||
{
|
|
||||||
sci->maxAnisotropy = attr->physical_device->properties.limits.maxSamplerAnisotropy;
|
//if(attr->physical_device->features.samplerAnisotropy) //不知道为什么不准,先全部禁用吧
|
||||||
sci->anisotropyEnable = VK_TRUE;
|
//{
|
||||||
}
|
// sci->maxAnisotropy = attr->physical_device->properties.limits.maxSamplerAnisotropy;
|
||||||
else
|
// sci->anisotropyEnable = VK_TRUE;
|
||||||
|
//}
|
||||||
|
//else
|
||||||
{
|
{
|
||||||
sci->maxAnisotropy = 1.0;
|
sci->maxAnisotropy = 1.0;
|
||||||
sci->anisotropyEnable = VK_FALSE;
|
sci->anisotropyEnable = VK_FALSE;
|
||||||
|
@ -28,7 +28,7 @@ ShaderModule::ShaderModule(VkDevice dev,int id,VkPipelineShaderStageCreateInfo *
|
|||||||
|
|
||||||
EnumShaderResource(sp,resource[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER],sp->GetUBO());
|
EnumShaderResource(sp,resource[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER],sp->GetUBO());
|
||||||
EnumShaderResource(sp,resource[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER],sp->GetSSBO());
|
EnumShaderResource(sp,resource[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER],sp->GetSSBO());
|
||||||
EnumShaderResource(sp,resource[VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE],sp->GetSampler());
|
EnumShaderResource(sp,resource[VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER],sp->GetSampler());
|
||||||
}
|
}
|
||||||
|
|
||||||
ShaderModule::~ShaderModule()
|
ShaderModule::~ShaderModule()
|
||||||
|
@ -10,5 +10,8 @@ Texture::~Texture()
|
|||||||
|
|
||||||
if(data->image)
|
if(data->image)
|
||||||
vkDestroyImage(device,data->image,nullptr);
|
vkDestroyImage(device,data->image,nullptr);
|
||||||
|
|
||||||
|
if(data->memory)
|
||||||
|
vkFreeMemory(device,data->memory,nullptr);
|
||||||
}
|
}
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
|
Loading…
x
Reference in New Issue
Block a user