Improve pipeline building

This commit is contained in:
Lynix
2020-03-03 22:26:57 +01:00
parent d5c75926c6
commit 7bf734cdd4
16 changed files with 360 additions and 217 deletions

View File

@@ -106,86 +106,216 @@ namespace Nz
});
NazaraAssert(s_declarations[VertexLayout_XY_Color]->GetStride() == sizeof(VertexStruct_XY_Color), "Invalid stride for declaration VertexLayout_XY_Color");
/*
// VertexLayout_XY_UV : VertexStruct_XY_UV
declaration = &s_declarations[VertexLayout_XY_UV];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XY_UV, position));
declaration->EnableComponent(VertexComponent_TexCoord, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XY_UV, uv));
s_declarations[VertexLayout_XY_UV] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Position,
ComponentType_Float2,
0
},
{
VertexComponent_TexCoord,
ComponentType_Float2,
0
},
});
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XY_UV), "Invalid stride for declaration VertexLayout_XY_UV");
NazaraAssert(s_declarations[VertexLayout_XY_UV]->GetStride() == sizeof(VertexStruct_XY_UV), "Invalid stride for declaration VertexLayout_XY_UV");
// VertexLayout_XYZ : VertexStruct_XYZ
declaration = &s_declarations[VertexLayout_XYZ];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ, position));
s_declarations[VertexLayout_XYZ] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Position,
ComponentType_Float3,
0
},
});
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XYZ), "Invalid stride for declaration VertexLayout_XYZ");
NazaraAssert(s_declarations[VertexLayout_XYZ]->GetStride() == sizeof(VertexStruct_XYZ), "Invalid stride for declaration VertexLayout_XYZ");
// VertexLayout_XYZ_Color : VertexStruct_XYZ_Color
declaration = &s_declarations[VertexLayout_XYZ_Color];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Color, position));
declaration->EnableComponent(VertexComponent_Color, ComponentType_Color, NazaraOffsetOf(VertexStruct_XYZ_Color, color));
s_declarations[VertexLayout_XYZ_Color] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Position,
ComponentType_Float3,
0
},
{
VertexComponent_Color,
ComponentType_Color,
0
}
});
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XYZ_Color), "Invalid stride for declaration VertexLayout_XYZ_Color");
NazaraAssert(s_declarations[VertexLayout_XYZ_Color]->GetStride() == sizeof(VertexStruct_XYZ_Color), "Invalid stride for declaration VertexLayout_XYZ_Color");
// VertexLayout_XYZ_Color_UV : VertexStruct_XYZ_Color_UV
declaration = &s_declarations[VertexLayout_XYZ_Color_UV];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Color_UV, position));
declaration->EnableComponent(VertexComponent_Color, ComponentType_Color, NazaraOffsetOf(VertexStruct_XYZ_Color_UV, color));
declaration->EnableComponent(VertexComponent_TexCoord, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XYZ_Color_UV, uv));
s_declarations[VertexLayout_XYZ_Color_UV] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Position,
ComponentType_Float3,
0
},
{
VertexComponent_Color,
ComponentType_Color,
0
},
{
VertexComponent_TexCoord,
ComponentType_Float2,
0
},
});
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XYZ_Color_UV), "Invalid stride for declaration VertexLayout_XYZ_Color_UV");
NazaraAssert(s_declarations[VertexLayout_XYZ_Color_UV]->GetStride() == sizeof(VertexStruct_XYZ_Color_UV), "Invalid stride for declaration VertexLayout_XYZ_Color_UV");
// VertexLayout_XYZ_Normal : VertexStruct_XYZ_Normal
declaration = &s_declarations[VertexLayout_XYZ_Normal];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Normal, position));
declaration->EnableComponent(VertexComponent_Normal, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Normal, normal));
s_declarations[VertexLayout_XYZ_Normal] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Position,
ComponentType_Float3,
0
},
{
VertexComponent_Normal,
ComponentType_Float3,
0
}
});
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XYZ_Normal), "Invalid stride for declaration VertexLayout_XYZ_Normal");
NazaraAssert(s_declarations[VertexLayout_XYZ_Normal]->GetStride() == sizeof(VertexStruct_XYZ_Normal), "Invalid stride for declaration VertexLayout_XYZ_Normal");
// VertexLayout_XYZ_Normal_UV : VertexStruct_XYZ_Normal_UV
declaration = &s_declarations[VertexLayout_XYZ_Normal_UV];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV, position));
declaration->EnableComponent(VertexComponent_Normal, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV, normal));
declaration->EnableComponent(VertexComponent_TexCoord, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV, uv));
s_declarations[VertexLayout_XYZ_Normal_UV] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Position,
ComponentType_Float3,
0
},
{
VertexComponent_Normal,
ComponentType_Float3,
0
},
{
VertexComponent_TexCoord,
ComponentType_Float2,
0
}
});
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XYZ_Normal_UV), "Invalid stride for declaration VertexLayout_XYZ_Normal_UV");
NazaraAssert(s_declarations[VertexLayout_XYZ_Normal_UV]->GetStride() == sizeof(VertexStruct_XYZ_Normal_UV), "Invalid stride for declaration VertexLayout_XYZ_Normal_UV");
// VertexLayout_XYZ_Normal_UV_Tangent : VertexStruct_XYZ_Normal_UV_Tangent
declaration = &s_declarations[VertexLayout_XYZ_Normal_UV_Tangent];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV_Tangent, position));
declaration->EnableComponent(VertexComponent_Normal, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV_Tangent, normal));
declaration->EnableComponent(VertexComponent_TexCoord, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV_Tangent, uv));
declaration->EnableComponent(VertexComponent_Tangent, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV_Tangent, tangent));
s_declarations[VertexLayout_XYZ_Normal_UV_Tangent] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Position,
ComponentType_Float3,
0
},
{
VertexComponent_Normal,
ComponentType_Float3,
0
},
{
VertexComponent_TexCoord,
ComponentType_Float2,
0
},
{
VertexComponent_Tangent,
ComponentType_Float3,
0
}
});
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XYZ_Normal_UV_Tangent), "Invalid stride for declaration VertexLayout_XYZ_Normal_UV_Tangent");
NazaraAssert(s_declarations[VertexLayout_XYZ_Normal_UV_Tangent]->GetStride() == sizeof(VertexStruct_XYZ_Normal_UV_Tangent), "Invalid stride for declaration VertexLayout_XYZ_Normal_UV_Tangent");
// VertexLayout_XYZ_Normal_UV_Tangent_Skinning : VertexStruct_XYZ_Normal_UV_Tangent_Skinning
declaration = &s_declarations[VertexLayout_XYZ_Normal_UV_Tangent_Skinning];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV_Tangent_Skinning, position));
declaration->EnableComponent(VertexComponent_Normal, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV_Tangent_Skinning, normal));
declaration->EnableComponent(VertexComponent_TexCoord, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV_Tangent_Skinning, uv));
declaration->EnableComponent(VertexComponent_Tangent, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV_Tangent_Skinning, tangent));
declaration->EnableComponent(VertexComponent_Unused, ComponentType_Int1, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV_Tangent_Skinning, weightCount));
declaration->EnableComponent(VertexComponent_Userdata0, ComponentType_Float4, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV_Tangent_Skinning, weights));
declaration->EnableComponent(VertexComponent_Userdata1, ComponentType_Int4, NazaraOffsetOf(VertexStruct_XYZ_Normal_UV_Tangent_Skinning, jointIndexes));
s_declarations[VertexLayout_XYZ_Normal_UV_Tangent_Skinning] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Position,
ComponentType_Float3,
0
},
{
VertexComponent_Normal,
ComponentType_Float3,
0
},
{
VertexComponent_TexCoord,
ComponentType_Float2,
0
},
{
VertexComponent_Tangent,
ComponentType_Float3,
0
},
{
VertexComponent_Userdata,
ComponentType_Int1,
0 // Weight count
},
{
VertexComponent_Userdata,
ComponentType_Float4,
1 // Weights
},
{
VertexComponent_Userdata,
ComponentType_Int4,
2 // Joint indexes
},
});
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XYZ_Normal_UV_Tangent_Skinning), "Invalid stride for declaration VertexLayout_XYZ_Normal_UV_Tangent_Skinning");
NazaraAssert(s_declarations[VertexLayout_XYZ_Normal_UV_Tangent_Skinning]->GetStride() == sizeof(VertexStruct_XYZ_Normal_UV_Tangent_Skinning), "Invalid stride for declaration VertexLayout_XYZ_Normal_UV_Tangent_Skinning");
// VertexLayout_XYZ_UV : VertexStruct_XYZ_UV
declaration = &s_declarations[VertexLayout_XYZ_UV];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float3, NazaraOffsetOf(VertexStruct_XYZ_UV, position));
declaration->EnableComponent(VertexComponent_TexCoord, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XYZ_UV, uv));
s_declarations[VertexLayout_XYZ_UV] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Position,
ComponentType_Float3,
0
},
{
VertexComponent_TexCoord,
ComponentType_Float2,
0
}
});
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XYZ_UV), "Invalid stride for declaration VertexLayout_XYZ_UV");
NazaraAssert(s_declarations[VertexLayout_XYZ_UV]->GetStride() == sizeof(VertexStruct_XYZ_UV), "Invalid stride for declaration VertexLayout_XYZ_UV");
// VertexLayout_Matrix4 : Matrix4f
declaration = &s_declarations[VertexLayout_Matrix4];
declaration->EnableComponent(VertexComponent_InstanceData0, ComponentType_Float4, NazaraOffsetOf(Matrix4f, m11));
declaration->EnableComponent(VertexComponent_InstanceData1, ComponentType_Float4, NazaraOffsetOf(Matrix4f, m21));
declaration->EnableComponent(VertexComponent_InstanceData2, ComponentType_Float4, NazaraOffsetOf(Matrix4f, m31));
declaration->EnableComponent(VertexComponent_InstanceData3, ComponentType_Float4, NazaraOffsetOf(Matrix4f, m41));
s_declarations[VertexLayout_Matrix4] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Userdata,
ComponentType_Float4,
0
},
{
VertexComponent_Userdata,
ComponentType_Float4,
1
},
{
VertexComponent_Userdata,
ComponentType_Float4,
2
},
{
VertexComponent_Userdata,
ComponentType_Float4,
3
}
});
NazaraAssert(declaration->GetStride() == sizeof(Matrix4f), "Invalid stride for declaration VertexLayout_Matrix4");*/
NazaraAssert(s_declarations[VertexLayout_Matrix4]->GetStride() == sizeof(Matrix4f), "Invalid stride for declaration VertexLayout_Matrix4");
}
catch (const std::exception& e)
{

View File

@@ -7,6 +7,7 @@
#include <Nazara/VulkanRenderer/VulkanShaderStage.hpp>
#include <Nazara/VulkanRenderer/Utils.hpp>
#include <cassert>
#include <iostream>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
@@ -21,7 +22,7 @@ namespace Nz
{
std::vector<VkPipelineColorBlendAttachmentState> colorBlendStates;
VkPipelineColorBlendAttachmentState colorBlendState = colorBlendStates.emplace_back();
VkPipelineColorBlendAttachmentState& colorBlendState = colorBlendStates.emplace_back();
colorBlendState.blendEnable = pipelineInfo.blending;
colorBlendState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; //< TODO
@@ -94,6 +95,16 @@ namespace Nz
return createInfo;
}
VkPipelineMultisampleStateCreateInfo VulkanRenderPipeline::BuildMultisampleInfo(const RenderPipelineInfo& pipelineInfo)
{
VkPipelineMultisampleStateCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
createInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
createInfo.minSampleShading = 1.0f; //< TODO: Remove
return createInfo;
}
VkPipelineRasterizationStateCreateInfo VulkanRenderPipeline::BuildRasterizationInfo(const RenderPipelineInfo& pipelineInfo)
{
VkPipelineRasterizationStateCreateInfo createInfo = {};
@@ -149,6 +160,58 @@ namespace Nz
return shaderStageCreateInfos;
}
std::vector<VkVertexInputAttributeDescription> VulkanRenderPipeline::BuildVertexAttributeDescription(const RenderPipelineInfo& pipelineInfo)
{
std::vector<VkVertexInputAttributeDescription> vertexAttributes;
std::uint32_t locationIndex = 0;
for (const auto& bufferData : pipelineInfo.vertexBuffers)
{
std::uint32_t binding = std::uint32_t(bufferData.binding);
for (const auto& componentInfo : *bufferData.declaration)
{
auto& bufferAttribute = vertexAttributes.emplace_back();
bufferAttribute.binding = binding;
bufferAttribute.location = locationIndex++;
bufferAttribute.offset = std::uint32_t(componentInfo.offset);
bufferAttribute.format = ToVulkan(componentInfo.type);
}
}
return vertexAttributes;
}
std::vector<VkVertexInputBindingDescription> VulkanRenderPipeline::BuildVertexBindingDescription(const RenderPipelineInfo& pipelineInfo)
{
std::vector<VkVertexInputBindingDescription> vertexBindings;
for (const auto& bufferData : pipelineInfo.vertexBuffers)
{
auto& bufferBinding = vertexBindings.emplace_back();
bufferBinding.binding = std::uint32_t(bufferData.binding);
bufferBinding.stride = std::uint32_t(bufferData.declaration->GetStride());
bufferBinding.inputRate = ToVulkan(bufferData.declaration->GetInputRate());
}
return vertexBindings;
}
VkPipelineVertexInputStateCreateInfo VulkanRenderPipeline::BuildVertexInputInfo(const RenderPipelineInfo& pipelineInfo, const std::vector<VkVertexInputAttributeDescription>& vertexAttributes, const std::vector<VkVertexInputBindingDescription>& bindingDescriptions)
{
VkPipelineVertexInputStateCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
createInfo.vertexAttributeDescriptionCount = std::uint32_t(vertexAttributes.size());
createInfo.pVertexAttributeDescriptions = vertexAttributes.data();
createInfo.vertexBindingDescriptionCount = std::uint32_t(bindingDescriptions.size());
createInfo.pVertexBindingDescriptions = bindingDescriptions.data();
return createInfo;
}
auto VulkanRenderPipeline::BuildCreateInfo(const RenderPipelineInfo& pipelineInfo) -> CreateInfo
{
CreateInfo createInfo = {};
@@ -157,20 +220,30 @@ namespace Nz
createInfo.colorBlendAttachmentState = BuildColorBlendAttachmentStateList(pipelineInfo);
createInfo.dynamicStates = BuildDynamicStateList(pipelineInfo);
createInfo.shaderStages = BuildShaderStageInfo(pipelineInfo);
createInfo.vertexAttributesDescription = BuildVertexAttributeDescription(pipelineInfo);
createInfo.vertexBindingDescription = BuildVertexBindingDescription(pipelineInfo);
createInfo.stateData->colorBlendState = BuildColorBlendInfo(pipelineInfo, createInfo.colorBlendAttachmentState);
createInfo.stateData->depthStencilState = BuildDepthStencilInfo(pipelineInfo);
createInfo.stateData->dynamicState = BuildDynamicStateInfo(pipelineInfo, createInfo.dynamicStates);
createInfo.stateData->inputAssemblyState = BuildInputAssemblyInfo(pipelineInfo);
createInfo.stateData->multiSampleState = BuildMultisampleInfo(pipelineInfo);
createInfo.stateData->rasterizationState = BuildRasterizationInfo(pipelineInfo);
createInfo.stateData->viewportState = BuildViewportInfo(pipelineInfo);
createInfo.stateData->vertexInputState = BuildVertexInputInfo(pipelineInfo, createInfo.vertexAttributesDescription, createInfo.vertexBindingDescription);
createInfo.pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
createInfo.pipelineInfo.stageCount = std::uint32_t(createInfo.shaderStages.size());
createInfo.pipelineInfo.pStages = createInfo.shaderStages.data();
createInfo.pipelineInfo.pColorBlendState = createInfo.colorBlendAttachmentState.data();
createInfo.pipelineInfo.
createInfo.pipelineInfo.pColorBlendState = &createInfo.stateData->colorBlendState;
createInfo.pipelineInfo.pDepthStencilState = &createInfo.stateData->depthStencilState;
createInfo.pipelineInfo.pDynamicState = &createInfo.stateData->dynamicState;
createInfo.pipelineInfo.pInputAssemblyState = &createInfo.stateData->inputAssemblyState;
createInfo.pipelineInfo.pMultisampleState = &createInfo.stateData->multiSampleState;
createInfo.pipelineInfo.pRasterizationState = &createInfo.stateData->rasterizationState;
createInfo.pipelineInfo.pVertexInputState = &createInfo.stateData->vertexInputState;
createInfo.pipelineInfo.pViewportState = &createInfo.stateData->viewportState;
return createInfo;
}