2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKShaderModule.h>
|
|
|
|
|
#include<hgl/graph/VKVertexAttributeBinding.h>
|
|
|
|
|
#include<hgl/graph/VKDevice.h>
|
2019-04-27 21:23:57 +08:00
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2020-10-21 12:39:22 +08:00
|
|
|
|
ShaderModule *GPUDevice::CreateShaderModule(ShaderResource *sr)
|
2020-09-19 23:49:32 +08:00
|
|
|
|
{
|
|
|
|
|
if(!sr)return(nullptr);
|
|
|
|
|
|
2020-10-19 22:24:25 +08:00
|
|
|
|
PipelineShaderStageCreateInfo *shader_stage=new PipelineShaderStageCreateInfo(sr->GetStage());
|
|
|
|
|
|
|
|
|
|
ShaderModuleCreateInfo moduleCreateInfo(sr);
|
2020-09-19 23:49:32 +08:00
|
|
|
|
|
|
|
|
|
if(vkCreateShaderModule(attr->device,&moduleCreateInfo,nullptr,&(shader_stage->module))!=VK_SUCCESS)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
ShaderModule *sm;
|
|
|
|
|
|
|
|
|
|
if(sr->GetStage()==VK_SHADER_STAGE_VERTEX_BIT)
|
|
|
|
|
sm=new VertexShaderModule(attr->device,shader_stage,sr);
|
|
|
|
|
else
|
|
|
|
|
sm=new ShaderModule(attr->device,shader_stage,sr);
|
|
|
|
|
|
|
|
|
|
return sm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ShaderModule::ShaderModule(VkDevice dev,VkPipelineShaderStageCreateInfo *sci,ShaderResource *sr)
|
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
|
|
|
|
ref_count=0;
|
|
|
|
|
|
|
|
|
|
stage_create_info=sci;
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
2020-06-09 19:40:08 +08:00
|
|
|
|
shader_resource=sr;
|
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);
|
2020-10-19 22:24:25 +08:00
|
|
|
|
//这里不用删除stage_create_info,材质中会删除的
|
2019-04-27 21:23:57 +08:00
|
|
|
|
}
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
2020-09-19 23:49:32 +08:00
|
|
|
|
VertexShaderModule::VertexShaderModule(VkDevice dev,VkPipelineShaderStageCreateInfo *pssci,ShaderResource *sr):ShaderModule(dev,pssci,sr)
|
2019-04-28 16:06:53 +08:00
|
|
|
|
{
|
2020-06-09 19:40:08 +08:00
|
|
|
|
const ShaderStageList &stage_inputs=sr->GetStageInputs();
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
2020-06-09 19:40:08 +08:00
|
|
|
|
attr_count=stage_inputs.GetCount();
|
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;
|
|
|
|
|
|
2020-06-09 19:40:08 +08:00
|
|
|
|
ShaderStage **si=stage_inputs.GetData();
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
2020-06-09 19:40:08 +08:00
|
|
|
|
for(uint i=0;i<attr_count;i++)
|
2019-04-28 16:06:53 +08:00
|
|
|
|
{
|
2020-06-10 17:11:24 +08:00
|
|
|
|
bind->binding =i; //binding对应在vkCmdBindVertexBuffer中设置的缓冲区的序列号,所以这个数字必须从0开始,而且紧密排列。
|
2019-06-25 22:26:09 +08:00
|
|
|
|
//在VertexInput类中,buf_list需要严格按照本此binding为序列号排列
|
2020-06-09 19:40:08 +08:00
|
|
|
|
bind->stride =GetStrideByFormat((*si)->format);
|
2019-04-28 16:06:53 +08:00
|
|
|
|
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
|
|
|
|
|
2020-06-10 17:11:24 +08:00
|
|
|
|
attr->binding =i;
|
2020-06-09 19:40:08 +08:00
|
|
|
|
attr->location =(*si)->location; //此值对应shader中的layout(location=
|
|
|
|
|
attr->format =(*si)->format;
|
2019-04-28 16:06:53 +08:00
|
|
|
|
attr->offset =0;
|
|
|
|
|
|
|
|
|
|
++attr;
|
|
|
|
|
++bind;
|
2020-06-09 19:40:08 +08:00
|
|
|
|
|
|
|
|
|
++si;
|
2019-04-28 16:06:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VertexAttributeBinding *VertexShaderModule::CreateVertexAttributeBinding()
|
|
|
|
|
{
|
2020-09-05 17:53:48 +08:00
|
|
|
|
VertexAttributeBinding *vab=new VertexAttributeBinding(attr_count,binding_list,attribute_list);
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
|
|
|
|
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
|