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/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);
|
||||
}
|
||||
|
||||
|
@ -67,7 +67,7 @@ public:
|
||||
|
||||
bool BeginRenderPass(RenderPass *rp,Framebuffer *fb);
|
||||
bool Bind(Pipeline *p);
|
||||
bool Bind(DescriptorSets *,int first=0,int count=0);
|
||||
bool Bind(DescriptorSets *);
|
||||
bool Bind(Renderable *);
|
||||
void EndRenderPass();
|
||||
bool End();
|
||||
|
@ -10,7 +10,7 @@ class DescriptorSets
|
||||
{
|
||||
Device *device;
|
||||
int count;
|
||||
VkDescriptorSet *desc_set_list;
|
||||
VkDescriptorSet desc_set;
|
||||
const Map<uint32_t,int> *index_by_binding;
|
||||
|
||||
VkPipelineLayout pipeline_layout;
|
||||
@ -19,31 +19,27 @@ private:
|
||||
|
||||
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;
|
||||
count=c;
|
||||
desc_set_list=desc_set;
|
||||
desc_set=ds;
|
||||
pipeline_layout=pl;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
~DescriptorSets();
|
||||
~DescriptorSets()=default;
|
||||
|
||||
const uint32_t GetCount ()const{return count;}
|
||||
const VkDescriptorSet * GetDescriptorSets ()const{return desc_set_list;}
|
||||
VkDescriptorSet GetDescriptorSet (const uint32_t binding)const;
|
||||
const VkDescriptorSet * GetDescriptorSets ()const{return &desc_set;}
|
||||
const VkPipelineLayout GetPipelineLayout ()const{return pipeline_layout;}
|
||||
|
||||
bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info);
|
||||
//bool UpdateUBO(const UTF8String &name,const VkDescriptorBufferInfo *buf_info)
|
||||
//{
|
||||
// if(name.IsEmpty()||!buf_info)
|
||||
// return(false);
|
||||
//未来统合所有的write descriptor sets,这里的update改为只是添加记录
|
||||
//最终bind到cmd时一次性写入。
|
||||
|
||||
// return UpdateUBO(GetUBOBinding(name),buf_info);
|
||||
//}
|
||||
bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *);
|
||||
bool UpdateSampler(const uint32_t binding,const VkDescriptorImageInfo *);
|
||||
};//class DescriptorSets
|
||||
VK_NAMESPACE_END
|
||||
#endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE
|
||||
|
@ -2,9 +2,11 @@
|
||||
#define HGL_GRAPH_VULKAN_TEXTURE_INCLUDE
|
||||
|
||||
#include<hgl/graph/vulkan/VK.h>
|
||||
#include<hgl/graph/vulkan/VKImageView.h>
|
||||
VK_NAMESPACE_BEGIN
|
||||
struct TextureData
|
||||
{
|
||||
VkDeviceMemory memory;
|
||||
VkImage image;
|
||||
VkImageLayout image_layout;
|
||||
ImageView *image_view;
|
||||
@ -23,7 +25,7 @@ public:
|
||||
|
||||
operator VkImage (){return data?data->image:nullptr;}
|
||||
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 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 OnlyPosition.vert.spv OnlyPosition.vert
|
||||
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);
|
||||
}
|
||||
|
||||
bool CommandBuffer::Bind(DescriptorSets *dsl,int first,int count)
|
||||
bool CommandBuffer::Bind(DescriptorSets *dsl)
|
||||
{
|
||||
if(!dsl)
|
||||
return(false);
|
||||
|
||||
if(first<0)
|
||||
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);
|
||||
vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,dsl->GetPipelineLayout(),0,1,dsl->GetDescriptorSets(),0,nullptr);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
@ -3,31 +3,13 @@
|
||||
#include<hgl/graph/vulkan/VKDevice.h>
|
||||
|
||||
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()
|
||||
{
|
||||
if(pipeline_layout)
|
||||
vkDestroyPipelineLayout(*device,pipeline_layout,nullptr);
|
||||
|
||||
if(dsl_list)
|
||||
{
|
||||
DestroyDescriptorSetLayout(*device,layout_binding_list.GetCount(),dsl_list);
|
||||
delete[] dsl_list;
|
||||
}
|
||||
if(dsl)
|
||||
vkDestroyDescriptorSetLayout(*device,dsl,nullptr);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if(!binding||count<=0)return;
|
||||
|
||||
const uint old_count=layout_binding_list.GetCount();
|
||||
|
||||
layout_binding_list.SetCount(old_count+count);
|
||||
@ -80,30 +64,29 @@ bool DescriptorSetLayoutCreater::CreatePipelineLayout()
|
||||
descriptor_layout.bindingCount = count;
|
||||
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)
|
||||
{
|
||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
|
||||
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||
pPipelineLayoutCreateInfo.pNext = nullptr;
|
||||
pPipelineLayoutCreateInfo.pushConstantRangeCount = 0;
|
||||
pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
|
||||
pPipelineLayoutCreateInfo.setLayoutCount = count;
|
||||
pPipelineLayoutCreateInfo.pSetLayouts = dsl_list;
|
||||
if(vkCreateDescriptorSetLayout(*device,&descriptor_layout,nullptr,&dsl)!=VK_SUCCESS)
|
||||
return(false);
|
||||
|
||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
|
||||
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||
pPipelineLayoutCreateInfo.pNext = nullptr;
|
||||
pPipelineLayoutCreateInfo.pushConstantRangeCount = 0;
|
||||
pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
|
||||
pPipelineLayoutCreateInfo.setLayoutCount = 1;
|
||||
pPipelineLayoutCreateInfo.pSetLayouts = &dsl;
|
||||
|
||||
if(vkCreatePipelineLayout(*device,&pPipelineLayoutCreateInfo,nullptr,&pipeline_layout)==VK_SUCCESS)
|
||||
return(true);
|
||||
}
|
||||
if(vkCreatePipelineLayout(*device,&pPipelineLayoutCreateInfo,nullptr,&pipeline_layout)!=VK_SUCCESS)
|
||||
return(false);
|
||||
|
||||
delete[] dsl_list;
|
||||
dsl_list=nullptr;
|
||||
return(false);
|
||||
return(true);
|
||||
}
|
||||
|
||||
DescriptorSets *DescriptorSetLayoutCreater::Create()
|
||||
{
|
||||
if(!pipeline_layout||!dsl_list)
|
||||
if(!pipeline_layout||!dsl)
|
||||
return(nullptr);
|
||||
|
||||
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.pNext = nullptr;
|
||||
alloc_info.descriptorPool = device->GetDescriptorPool();
|
||||
alloc_info.descriptorSetCount = count;
|
||||
alloc_info.pSetLayouts = dsl_list;
|
||||
alloc_info.descriptorSetCount = 1;
|
||||
alloc_info.pSetLayouts = &dsl;
|
||||
|
||||
VkDescriptorSet *desc_set=new VkDescriptorSet[count];
|
||||
VkDescriptorSet desc_set;
|
||||
|
||||
if(vkAllocateDescriptorSets(*device,&alloc_info,desc_set)!=VK_SUCCESS)
|
||||
{
|
||||
delete[] desc_set;
|
||||
if(vkAllocateDescriptorSets(*device,&alloc_info,&desc_set)!=VK_SUCCESS)
|
||||
return(nullptr);
|
||||
}
|
||||
|
||||
return(new DescriptorSets(device,count,pipeline_layout,desc_set,&index_by_binding));
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ class DescriptorSetLayoutCreater
|
||||
Device *device;
|
||||
|
||||
List<VkDescriptorSetLayoutBinding> layout_binding_list;
|
||||
VkDescriptorSetLayout *dsl_list=nullptr;
|
||||
VkDescriptorSetLayout dsl=nullptr;
|
||||
Map<uint32_t,int> index_by_binding;
|
||||
|
||||
VkPipelineLayout pipeline_layout=nullptr;
|
||||
@ -25,11 +25,14 @@ public:
|
||||
|
||||
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 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)
|
||||
{
|
||||
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)即可全部替代,而且更方便,但以此为提示
|
||||
|
@ -2,32 +2,12 @@
|
||||
#include<hgl/graph/vulkan/VKDevice.h>
|
||||
|
||||
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)
|
||||
{
|
||||
VkDescriptorSet set=GetDescriptorSet(binding);
|
||||
|
||||
VkWriteDescriptorSet writeDescriptorSet = {};
|
||||
|
||||
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
writeDescriptorSet.dstSet = set;
|
||||
writeDescriptorSet.dstSet = desc_set;
|
||||
writeDescriptorSet.descriptorCount = 1;
|
||||
writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
writeDescriptorSet.pBufferInfo = buf_info;
|
||||
@ -36,4 +16,19 @@ bool DescriptorSets::UpdateUBO(const uint32_t binding,const VkDescriptorBufferIn
|
||||
vkUpdateDescriptorSets(*device,1,&writeDescriptorSet,0,nullptr);
|
||||
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
|
||||
|
@ -242,21 +242,18 @@ namespace
|
||||
|
||||
VkDescriptorPool CreateDescriptorPool(VkDevice device,int sets_count)
|
||||
{
|
||||
constexpr size_t DESC_POOL_COUNT=1;
|
||||
VkDescriptorPoolSize pool_size[2];
|
||||
|
||||
VkDescriptorPoolSize pool_size[DESC_POOL_COUNT];
|
||||
|
||||
for(size_t i=0;i<DESC_POOL_COUNT;i++)
|
||||
{
|
||||
pool_size[i].type=VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
pool_size[i].descriptorCount=1;
|
||||
}
|
||||
pool_size[0].type=VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
pool_size[0].descriptorCount=1024;
|
||||
pool_size[1].type=VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
pool_size[1].descriptorCount=1024;
|
||||
|
||||
VkDescriptorPoolCreateInfo dp_create_info={};
|
||||
dp_create_info.sType=VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||
dp_create_info.pNext=nullptr;
|
||||
dp_create_info.maxSets=sets_count;
|
||||
dp_create_info.poolSizeCount=DESC_POOL_COUNT;
|
||||
dp_create_info.poolSizeCount=2;
|
||||
dp_create_info.pPoolSizes=pool_size;
|
||||
|
||||
VkDescriptorPool desc_pool;
|
||||
@ -327,7 +324,7 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device
|
||||
if(!CreateSwapchinAndImageView(attr))
|
||||
return(nullptr);
|
||||
|
||||
attr->desc_pool=CreateDescriptorPool(attr->device,1);
|
||||
attr->desc_pool=CreateDescriptorPool(attr->device,1024);
|
||||
|
||||
if(!attr->desc_pool)
|
||||
return(nullptr);
|
||||
|
@ -47,6 +47,7 @@ Texture2D *Device::CreateTexture2D(const VkFormat video_format,void *data,uint32
|
||||
|
||||
TextureData *tex_data=new TextureData();
|
||||
|
||||
tex_data->memory=nullptr;
|
||||
tex_data->image=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;
|
||||
|
||||
VkDeviceMemory device_memory;
|
||||
|
||||
VkImageCreateInfo imageCreateInfo{};
|
||||
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
||||
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);
|
||||
memAllocInfo.allocationSize = memReqs.size;
|
||||
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(vkBindImageMemory(attr->device, tex_data->image, device_memory, 0))
|
||||
VK_CHECK_RESULT(vkAllocateMemory(attr->device, &memAllocInfo, nullptr, &tex_data->memory))
|
||||
VK_CHECK_RESULT(vkBindImageMemory(attr->device, tex_data->image, tex_data->memory, 0))
|
||||
|
||||
CommandBuffer *cmd_buf=CreateCommandBuffer();
|
||||
|
||||
@ -188,12 +187,14 @@ Sampler *Device::CreateSampler(VkSamplerCreateInfo *sci)
|
||||
|
||||
VkSampler sampler;
|
||||
|
||||
if(attr->physical_device->features.samplerAnisotropy)
|
||||
{
|
||||
sci->maxAnisotropy = attr->physical_device->properties.limits.maxSamplerAnisotropy;
|
||||
sci->anisotropyEnable = VK_TRUE;
|
||||
}
|
||||
else
|
||||
sci->sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
||||
|
||||
//if(attr->physical_device->features.samplerAnisotropy) //不知道为什么不准,先全部禁用吧
|
||||
//{
|
||||
// sci->maxAnisotropy = attr->physical_device->properties.limits.maxSamplerAnisotropy;
|
||||
// sci->anisotropyEnable = VK_TRUE;
|
||||
//}
|
||||
//else
|
||||
{
|
||||
sci->maxAnisotropy = 1.0;
|
||||
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_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()
|
||||
|
@ -10,5 +10,8 @@ Texture::~Texture()
|
||||
|
||||
if(data->image)
|
||||
vkDestroyImage(device,data->image,nullptr);
|
||||
|
||||
if(data->memory)
|
||||
vkFreeMemory(device,data->memory,nullptr);
|
||||
}
|
||||
VK_NAMESPACE_END
|
||||
|
Loading…
x
Reference in New Issue
Block a user