2019-05-05 21:30:55 +08:00
|
|
|
|
#include"VKDescriptorSetLayoutCreater.h"
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKDescriptorSets.h>
|
|
|
|
|
#include<hgl/graph/VKDevice.h>
|
2019-05-05 21:30:55 +08:00
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2020-10-21 12:09:15 +08:00
|
|
|
|
DescriptorSetLayoutCreater *RenderDevice::CreateDescriptorSetLayoutCreater()
|
2020-09-19 23:49:32 +08:00
|
|
|
|
{
|
|
|
|
|
return(new DescriptorSetLayoutCreater(attr->device,attr->desc_pool));
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 21:30:55 +08:00
|
|
|
|
DescriptorSetLayoutCreater::~DescriptorSetLayoutCreater()
|
|
|
|
|
{
|
|
|
|
|
if(pipeline_layout)
|
2020-09-19 23:49:32 +08:00
|
|
|
|
vkDestroyPipelineLayout(device,pipeline_layout,nullptr);
|
2019-05-05 21:30:55 +08:00
|
|
|
|
|
2019-05-20 17:52:23 +08:00
|
|
|
|
if(dsl)
|
2020-09-19 23:49:32 +08:00
|
|
|
|
vkDestroyDescriptorSetLayout(device,dsl,nullptr);
|
2019-05-05 21:30:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DescriptorSetLayoutCreater::Bind(const uint32_t binding,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags)
|
|
|
|
|
{
|
2019-07-17 12:00:43 +08:00
|
|
|
|
if(index_by_binding.KeyExist(binding))
|
|
|
|
|
{
|
|
|
|
|
//重复的绑定点,有可能存在的,比如WorldMatrix在vs/fs中同时存在
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-01 19:25:07 +08:00
|
|
|
|
VkDescriptorSetLayoutBinding layout_binding;
|
|
|
|
|
|
|
|
|
|
layout_binding.binding = binding;
|
|
|
|
|
layout_binding.descriptorType = desc_type;
|
|
|
|
|
layout_binding.descriptorCount = 1;
|
|
|
|
|
layout_binding.stageFlags = stageFlags;
|
|
|
|
|
layout_binding.pImmutableSamplers = nullptr;
|
2019-05-05 21:30:55 +08:00
|
|
|
|
|
|
|
|
|
const int index=layout_binding_list.Add(layout_binding);
|
|
|
|
|
|
|
|
|
|
index_by_binding.Add(binding,index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DescriptorSetLayoutCreater::Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags)
|
|
|
|
|
{
|
2019-05-20 17:52:23 +08:00
|
|
|
|
if(!binding||count<=0)return;
|
|
|
|
|
|
2019-05-05 21:30:55 +08:00
|
|
|
|
const uint old_count=layout_binding_list.GetCount();
|
|
|
|
|
|
2019-07-17 12:00:43 +08:00
|
|
|
|
layout_binding_list.PreMalloc(old_count+count);
|
2019-05-05 21:30:55 +08:00
|
|
|
|
|
|
|
|
|
VkDescriptorSetLayoutBinding *p=layout_binding_list.GetData()+old_count;
|
|
|
|
|
|
2019-07-17 12:00:43 +08:00
|
|
|
|
uint fin_count=0;
|
|
|
|
|
|
2019-05-05 21:30:55 +08:00
|
|
|
|
for(uint i=old_count;i<old_count+count;i++)
|
|
|
|
|
{
|
2019-07-17 12:00:43 +08:00
|
|
|
|
if(!index_by_binding.KeyExist(*binding))
|
|
|
|
|
{
|
|
|
|
|
p->binding = *binding;
|
|
|
|
|
p->descriptorType = desc_type;
|
|
|
|
|
p->descriptorCount = 1;
|
|
|
|
|
p->stageFlags = stageFlags;
|
|
|
|
|
p->pImmutableSamplers = nullptr;
|
|
|
|
|
|
|
|
|
|
index_by_binding.Add(*binding,i);
|
2019-05-05 21:30:55 +08:00
|
|
|
|
|
2019-07-17 12:00:43 +08:00
|
|
|
|
++p;
|
|
|
|
|
++fin_count;
|
|
|
|
|
}
|
2019-05-05 21:30:55 +08:00
|
|
|
|
|
|
|
|
|
++binding;
|
|
|
|
|
}
|
2019-07-17 12:00:43 +08:00
|
|
|
|
|
|
|
|
|
layout_binding_list.SetCount(old_count+fin_count);
|
2019-05-05 21:30:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DescriptorSetLayoutCreater::CreatePipelineLayout()
|
|
|
|
|
{
|
|
|
|
|
const int count=layout_binding_list.GetCount();
|
|
|
|
|
|
|
|
|
|
if(count<=0)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-09-27 20:58:25 +08:00
|
|
|
|
DescriptorSetLayoutCreateInfo descriptor_layout;
|
2019-07-01 19:25:07 +08:00
|
|
|
|
|
|
|
|
|
descriptor_layout.bindingCount = count;
|
|
|
|
|
descriptor_layout.pBindings = layout_binding_list.GetData();
|
2019-05-05 21:30:55 +08:00
|
|
|
|
|
2019-05-20 17:52:23 +08:00
|
|
|
|
if(dsl)
|
2020-09-19 23:49:32 +08:00
|
|
|
|
vkDestroyDescriptorSetLayout(device,dsl,nullptr);
|
2019-05-05 21:30:55 +08:00
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
if(vkCreateDescriptorSetLayout(device,&descriptor_layout,nullptr,&dsl)!=VK_SUCCESS)
|
2019-05-20 17:52:23 +08:00
|
|
|
|
return(false);
|
2019-05-29 21:48:56 +08:00
|
|
|
|
|
2020-06-09 19:40:08 +08:00
|
|
|
|
VkPushConstantRange push_constant_range;
|
2019-05-29 21:48:56 +08:00
|
|
|
|
|
2020-06-09 19:40:08 +08:00
|
|
|
|
push_constant_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
|
|
|
|
push_constant_range.size = MAX_PUSH_CONSTANT_BYTES;
|
|
|
|
|
push_constant_range.offset = 0;
|
2019-05-20 17:52:23 +08:00
|
|
|
|
|
2020-09-27 20:58:25 +08:00
|
|
|
|
PipelineLayoutCreateInfo pPipelineLayoutCreateInfo;
|
|
|
|
|
|
2019-07-01 19:25:07 +08:00
|
|
|
|
pPipelineLayoutCreateInfo.setLayoutCount = 1;
|
|
|
|
|
pPipelineLayoutCreateInfo.pSetLayouts = &dsl;
|
|
|
|
|
pPipelineLayoutCreateInfo.pushConstantRangeCount = 1;
|
2020-06-09 19:40:08 +08:00
|
|
|
|
pPipelineLayoutCreateInfo.pPushConstantRanges = &push_constant_range;
|
2019-05-20 17:52:23 +08:00
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
if(vkCreatePipelineLayout(device,&pPipelineLayoutCreateInfo,nullptr,&pipeline_layout)!=VK_SUCCESS)
|
2019-05-20 17:52:23 +08:00
|
|
|
|
return(false);
|
2019-05-05 21:30:55 +08:00
|
|
|
|
|
2019-05-20 17:52:23 +08:00
|
|
|
|
return(true);
|
2019-05-05 21:30:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DescriptorSets *DescriptorSetLayoutCreater::Create()
|
|
|
|
|
{
|
2019-05-20 17:52:23 +08:00
|
|
|
|
if(!pipeline_layout||!dsl)
|
2019-05-05 21:30:55 +08:00
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
const int count=layout_binding_list.GetCount();
|
|
|
|
|
|
|
|
|
|
if(count<=0)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
2020-09-27 20:58:25 +08:00
|
|
|
|
DescriptorSetAllocateInfo alloc_info;
|
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
alloc_info.descriptorPool = pool;
|
2019-07-01 19:25:07 +08:00
|
|
|
|
alloc_info.descriptorSetCount = 1;
|
|
|
|
|
alloc_info.pSetLayouts = &dsl;
|
2019-05-05 21:30:55 +08:00
|
|
|
|
|
2019-05-20 17:52:23 +08:00
|
|
|
|
VkDescriptorSet desc_set;
|
2019-05-05 21:30:55 +08:00
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
if(vkAllocateDescriptorSets(device,&alloc_info,&desc_set)!=VK_SUCCESS)
|
2019-05-05 21:30:55 +08:00
|
|
|
|
return(nullptr);
|
|
|
|
|
|
2019-05-06 21:11:10 +08:00
|
|
|
|
return(new DescriptorSets(device,count,pipeline_layout,desc_set,&index_by_binding));
|
2019-05-05 21:30:55 +08:00
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|