2019-06-25 22:26:09 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKShaderModule.h>
|
2019-04-30 16:42:59 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKVertexAttributeBinding.h>
|
2019-05-05 21:42:15 +08:00
|
|
|
|
#include"VKShaderParse.h"
|
2019-04-27 21:23:57 +08:00
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2019-05-06 22:33:21 +08:00
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
void EnumShaderResource(const ShaderParse *parse,ShaderResourceList &sr,const spirv_cross::SmallVector<spirv_cross::Resource> &res)
|
|
|
|
|
{
|
|
|
|
|
for(const auto &obj:res)
|
|
|
|
|
{
|
|
|
|
|
const UTF8String & name =parse->GetName(obj);
|
|
|
|
|
const uint binding =parse->GetBinding(obj);
|
|
|
|
|
|
|
|
|
|
sr.binding_by_name.Add(name,binding);
|
|
|
|
|
sr.binding_list.Add(binding);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}//namespace
|
|
|
|
|
|
2019-05-05 21:30:55 +08:00
|
|
|
|
ShaderModule::ShaderModule(VkDevice dev,int id,VkPipelineShaderStageCreateInfo *sci,const ShaderParse *sp)
|
2019-04-27 21:23:57 +08:00
|
|
|
|
{
|
2019-05-05 21:30:55 +08:00
|
|
|
|
device=dev;
|
2019-04-27 21:23:57 +08:00
|
|
|
|
shader_id=id;
|
|
|
|
|
ref_count=0;
|
|
|
|
|
|
|
|
|
|
stage_create_info=sci;
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
2019-05-06 22:33:21 +08:00
|
|
|
|
EnumShaderResource(sp,resource[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER],sp->GetUBO());
|
|
|
|
|
EnumShaderResource(sp,resource[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER],sp->GetSSBO());
|
2019-05-20 17:52:23 +08:00
|
|
|
|
EnumShaderResource(sp,resource[VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER],sp->GetSampler());
|
2019-04-27 21:23:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ShaderModule::~ShaderModule()
|
|
|
|
|
{
|
2019-05-05 21:30:55 +08:00
|
|
|
|
vkDestroyShaderModule(device,stage_create_info->module,nullptr);
|
2019-04-27 21:23:57 +08:00
|
|
|
|
delete stage_create_info;
|
|
|
|
|
}
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
2019-05-05 21:30:55 +08:00
|
|
|
|
VertexShaderModule::VertexShaderModule(VkDevice dev,int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *parse):ShaderModule(dev,id,pssci,parse)
|
2019-04-28 16:06:53 +08:00
|
|
|
|
{
|
2019-05-06 22:33:21 +08:00
|
|
|
|
const auto &stage_inputs=parse->GetStageInputs();
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
2019-05-05 21:30:55 +08:00
|
|
|
|
attr_count=(uint32_t)stage_inputs.size();
|
2019-04-28 16:06:53 +08:00
|
|
|
|
binding_list=new VkVertexInputBindingDescription[attr_count];
|
|
|
|
|
attribute_list=new VkVertexInputAttributeDescription[attr_count];
|
|
|
|
|
|
|
|
|
|
VkVertexInputBindingDescription *bind=binding_list;
|
|
|
|
|
VkVertexInputAttributeDescription *attr=attribute_list;
|
|
|
|
|
|
|
|
|
|
uint32_t binding_index=0;
|
|
|
|
|
|
|
|
|
|
for(const auto &si:stage_inputs)
|
|
|
|
|
{
|
2019-06-25 22:26:09 +08:00
|
|
|
|
const VkFormat format =parse->GetFormat(si); //注意这个格式有可能会解析不出来(比如各种压缩格式)
|
2019-04-28 16:06:53 +08:00
|
|
|
|
const UTF8String & name =parse->GetName(si);
|
|
|
|
|
|
2019-06-25 22:26:09 +08:00
|
|
|
|
bind->binding =binding_index; //binding对应在vkCmdBindVertexBuffer中设置的缓冲区的序列号,所以这个数字必须从0开始,而且紧密排列。
|
|
|
|
|
//在VertexInput类中,buf_list需要严格按照本此binding为序列号排列
|
2019-04-28 16:06:53 +08:00
|
|
|
|
bind->stride =GetStrideByFormat(format);
|
|
|
|
|
bind->inputRate =VK_VERTEX_INPUT_RATE_VERTEX;
|
|
|
|
|
|
2019-06-25 22:26:09 +08:00
|
|
|
|
//binding对应的是第几个数据输入流
|
|
|
|
|
//实际使用一个binding可以绑定多个attrib
|
|
|
|
|
//比如在一个流中传递{pos,color}这样两个数据,就需要两个attrib
|
|
|
|
|
//但在我们的设计中,仅支持一个流传递一个attrib
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
|
|
|
|
attr->binding =binding_index;
|
2019-06-25 22:26:09 +08:00
|
|
|
|
attr->location =parse->GetLocation(si); //此值对应shader中的layout(location=
|
2019-04-28 16:06:53 +08:00
|
|
|
|
attr->format =format;
|
|
|
|
|
attr->offset =0;
|
|
|
|
|
|
|
|
|
|
stage_input_locations.Add(name,attr);
|
|
|
|
|
|
|
|
|
|
++attr;
|
|
|
|
|
++bind;
|
|
|
|
|
++binding_index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VertexShaderModule::~VertexShaderModule()
|
|
|
|
|
{
|
|
|
|
|
if(vab_sets.GetCount()>0)
|
|
|
|
|
{
|
2019-06-25 22:26:09 +08:00
|
|
|
|
//还有在用的,这是个错误
|
2019-04-28 16:06:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SAFE_CLEAR_ARRAY(binding_list);
|
|
|
|
|
SAFE_CLEAR_ARRAY(attribute_list);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 21:01:28 +08:00
|
|
|
|
const int VertexShaderModule::GetStageInputBinding(const UTF8String &name)const
|
2019-04-28 16:06:53 +08:00
|
|
|
|
{
|
|
|
|
|
if(name.IsEmpty())return -1;
|
|
|
|
|
|
|
|
|
|
VkVertexInputAttributeDescription *attr;
|
|
|
|
|
|
|
|
|
|
if(!stage_input_locations.Get(name,attr))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return attr->binding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VertexAttributeBinding *VertexShaderModule::CreateVertexAttributeBinding()
|
|
|
|
|
{
|
|
|
|
|
VertexAttributeBinding *vab=new VertexAttributeBinding(this);
|
|
|
|
|
|
|
|
|
|
vab_sets.Add(vab);
|
|
|
|
|
|
|
|
|
|
return(vab);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VertexShaderModule::Release(VertexAttributeBinding *vab)
|
|
|
|
|
{
|
|
|
|
|
return vab_sets.Delete(vab);
|
|
|
|
|
}
|
2019-04-27 21:23:57 +08:00
|
|
|
|
VK_NAMESPACE_END
|