2019-04-16 02:23:03 +08:00
|
|
|
|
#include"VKVertexInput.h"
|
2019-04-18 22:10:24 +08:00
|
|
|
|
#include"VKBuffer.h"
|
2019-04-16 02:23:03 +08:00
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2019-04-25 16:02:13 +08:00
|
|
|
|
|
|
|
|
|
void VertexInputState::Add(const uint32_t shader_location,const VkFormat format,uint32_t offset,bool instance)
|
2019-04-16 02:23:03 +08:00
|
|
|
|
{
|
2019-04-25 16:02:13 +08:00
|
|
|
|
const int binding_index=binding_list.GetCount(); //参考opengl vab,binding_index必须从0开始,紧密排列。对应在vkCmdBindVertexBuffer中的缓冲区索引
|
2019-04-16 02:23:03 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
binding_list.SetCount(binding_index+1);
|
|
|
|
|
attribute_list.SetCount(binding_index+1);
|
2019-04-16 02:23:03 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
VkVertexInputBindingDescription *binding=binding_list.GetData()+binding_index;
|
|
|
|
|
VkVertexInputAttributeDescription *attrib=attribute_list.GetData()+binding_index;
|
2019-04-16 02:23:03 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
binding->binding=binding_index;
|
|
|
|
|
binding->stride=GetStrideByFormat(format);
|
|
|
|
|
binding->inputRate=instance?VK_VERTEX_INPUT_RATE_INSTANCE:VK_VERTEX_INPUT_RATE_VERTEX;
|
|
|
|
|
|
|
|
|
|
//实际使用可以一个binding绑多个attrib,但我们仅支持1v1。
|
|
|
|
|
//一个binding是指在vertex shader中,由一个vertex输入流输入数据,attrib指其中的数据成分
|
|
|
|
|
//比如在一个流中传递{pos,color}这样两个数据,就需要两个attrib
|
|
|
|
|
//但我们在一个流中,仅支持一个attrib传递
|
2019-04-16 02:23:03 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
attrib->binding=binding_index;
|
|
|
|
|
attrib->location=shader_location;
|
|
|
|
|
attrib->format=format;
|
|
|
|
|
attrib->offset=offset;
|
|
|
|
|
}
|
2019-04-16 02:23:03 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
void VertexInputState::Write(VkPipelineVertexInputStateCreateInfo &vis) const
|
|
|
|
|
{
|
|
|
|
|
vis.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
2019-04-16 02:23:03 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
vis.vertexBindingDescriptionCount = binding_list.GetCount();
|
|
|
|
|
vis.pVertexBindingDescriptions = binding_list.GetData();
|
2019-04-18 16:37:59 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
vis.vertexAttributeDescriptionCount = attribute_list.GetCount();
|
|
|
|
|
vis.pVertexAttributeDescriptions = attribute_list.GetData();
|
2019-04-16 02:23:03 +08:00
|
|
|
|
}
|
2019-04-18 16:37:59 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
VertexInput::VertexInput(VertexInputState *state)
|
2019-04-18 16:37:59 +08:00
|
|
|
|
{
|
2019-04-25 16:02:13 +08:00
|
|
|
|
vis=state;
|
2019-04-18 16:37:59 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
if(!vis)
|
|
|
|
|
return;
|
2019-04-18 16:37:59 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
buf_count=vis->GetCount();
|
2019-04-18 16:37:59 +08:00
|
|
|
|
|
2019-04-25 16:02:13 +08:00
|
|
|
|
buf_list=hgl_zero_new<VkBuffer>(buf_count);
|
|
|
|
|
buf_offset=hgl_zero_new<VkDeviceSize>(buf_count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VertexInput::~VertexInput()
|
|
|
|
|
{
|
|
|
|
|
delete[] buf_offset;
|
|
|
|
|
delete[] buf_list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VertexInput::Set(uint32_t index,VertexBuffer *buf,VkDeviceSize offset)
|
|
|
|
|
{
|
|
|
|
|
if(index<0||index>=buf_count)return(false);
|
|
|
|
|
|
|
|
|
|
VkVertexInputBindingDescription *desc=vis->GetDesc(index);
|
|
|
|
|
VkVertexInputAttributeDescription *attr=vis->GetAttr(index);
|
|
|
|
|
|
|
|
|
|
if(buf->GetFormat()!=attr->format)return(false);
|
|
|
|
|
if(buf->GetStride()!=desc->stride)return(false);
|
|
|
|
|
|
|
|
|
|
buf_list[index]=*buf;
|
|
|
|
|
buf_offset[index]=offset;
|
|
|
|
|
|
|
|
|
|
return(true);
|
2019-04-18 16:37:59 +08:00
|
|
|
|
}
|
2019-04-16 02:23:03 +08:00
|
|
|
|
VK_NAMESPACE_END
|