WIP (VertexDeclaration)

This commit is contained in:
Lynix
2020-03-03 01:04:24 +01:00
parent 287be5d9b6
commit d5c75926c6
12 changed files with 304 additions and 255 deletions

View File

@@ -211,7 +211,7 @@ namespace Nz
std::size_t Utility::ComponentStride[ComponentType_Max+1] =
{
4*sizeof(UInt8), // ComponentType_Color
4*sizeof(UInt8), // ComponentType_Color
1*sizeof(double), // ComponentType_Double1
2*sizeof(double), // ComponentType_Double2
3*sizeof(double), // ComponentType_Double3
@@ -220,10 +220,10 @@ namespace Nz
2*sizeof(float), // ComponentType_Float2
3*sizeof(float), // ComponentType_Float3
4*sizeof(float), // ComponentType_Float4
1*sizeof(UInt32), // ComponentType_Int1
2*sizeof(UInt32), // ComponentType_Int2
3*sizeof(UInt32), // ComponentType_Int3
4*sizeof(UInt32), // ComponentType_Int4
1*sizeof(UInt32), // ComponentType_Int1
2*sizeof(UInt32), // ComponentType_Int2
3*sizeof(UInt32), // ComponentType_Int3
4*sizeof(UInt32), // ComponentType_Int4
4*sizeof(float) // ComponentType_Quaternion
};

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2017 Jérôme Leclercq
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
@@ -14,144 +14,27 @@
namespace Nz
{
VertexDeclaration::VertexDeclaration() :
m_stride(0)
VertexDeclaration::VertexDeclaration(VertexInputRate inputRate, std::initializer_list<ComponentEntry> components) :
m_inputRate(inputRate)
{
}
std::size_t offset = 0;
VertexDeclaration::VertexDeclaration(const VertexDeclaration& declaration) :
RefCounted(),
m_components(declaration.m_components),
m_stride(declaration.m_stride)
{
}
VertexDeclaration::~VertexDeclaration()
{
OnVertexDeclarationRelease(this);
}
void VertexDeclaration::DisableComponent(VertexComponent component)
{
#ifdef NAZARA_DEBUG
if (component > VertexComponent_Max)
m_components.reserve(components.size());
for (const ComponentEntry& entry : components)
{
NazaraError("Vertex component out of enum");
return;
}
#endif
auto& component = m_components.emplace_back();
component.component = entry.component;
component.componentIndex = entry.componentIndex;
component.offset = offset;
component.type = entry.type;
#if NAZARA_UTILITY_SAFE
if (component == VertexComponent_Unused)
{
NazaraError("Cannot disable \"unused\" component");
return;
}
#endif
NazaraAssert(IsTypeSupported(component.type), "Component type 0x" + String::Number(component.type, 16) + " is not supported by vertex declarations");
NazaraAssert(component.componentIndex == 0 || component.component == VertexComponent_Userdata, "Only userdata components can have non-zero component indexes");
Component& vertexComponent = m_components[component];
if (vertexComponent.enabled)
{
vertexComponent.enabled = false;
m_stride -= Utility::ComponentStride[vertexComponent.type];
}
}
void VertexDeclaration::EnableComponent(VertexComponent component, ComponentType type, std::size_t offset)
{
#ifdef NAZARA_DEBUG
if (component > VertexComponent_Max)
{
NazaraError("Vertex component out of enum");
return;
}
#endif
#if NAZARA_UTILITY_SAFE
if (!IsTypeSupported(type))
{
NazaraError("Component type 0x" + String::Number(type, 16) + " is not supported by vertex declarations");
return;
}
#endif
if (component != VertexComponent_Unused)
{
Component& vertexComponent = m_components[component];
if (vertexComponent.enabled)
m_stride -= Utility::ComponentStride[vertexComponent.type];
else
vertexComponent.enabled = true;
vertexComponent.offset = offset;
vertexComponent.type = type;
offset += Utility::ComponentStride[component.type];
}
m_stride += Utility::ComponentStride[type];
}
void VertexDeclaration::GetComponent(VertexComponent component, bool* enabled, ComponentType* type, std::size_t* offset) const
{
#ifdef NAZARA_DEBUG
if (component > VertexComponent_Max)
{
NazaraError("Vertex component out of enum");
return;
}
#endif
#if NAZARA_UTILITY_SAFE
if (component == VertexComponent_Unused)
{
NazaraError("Cannot get \"unused\" component");
return;
}
#endif
const Component& vertexComponent = m_components[component];
if (enabled)
*enabled = vertexComponent.enabled;
if (type)
*type = vertexComponent.type;
if (offset)
*offset = vertexComponent.offset;
}
bool VertexDeclaration::HasComponent(VertexComponent component) const
{
bool enabled;
GetComponent(component, &enabled, nullptr, nullptr);
return enabled;
}
std::size_t VertexDeclaration::GetStride() const
{
return m_stride;
}
void VertexDeclaration::SetStride(std::size_t stride)
{
m_stride = stride;
}
VertexDeclaration& VertexDeclaration::operator=(const VertexDeclaration& declaration)
{
m_components = declaration.m_components;
m_stride = declaration.m_stride;
return *this;
}
VertexDeclaration* VertexDeclaration::Get(VertexLayout layout)
{
NazaraAssert(layout <= VertexLayout_Max, "Vertex layout out of enum");
return &s_declarations[layout];
m_stride = offset;
}
bool VertexDeclaration::IsTypeSupported(ComponentType type)
@@ -193,22 +76,37 @@ namespace Nz
{
ErrorFlags flags(ErrorFlag_Silent | ErrorFlag_ThrowException);
// Layout : Type
VertexDeclaration* declaration;
auto NewDeclaration = [](VertexInputRate inputRate, std::initializer_list<ComponentEntry> components)
{
return New(inputRate, std::move(components));
};
// VertexLayout_XY : VertexStruct_XY
declaration = &s_declarations[VertexLayout_XY];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XY, position));
s_declarations[VertexLayout_XY] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Position,
ComponentType_Float2,
0
}
});
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XY), "Invalid stride for declaration VertexLayout_XY");
NazaraAssert(s_declarations[VertexLayout_XY]->GetStride() == sizeof(VertexStruct_XY), "Invalid stride for declaration VertexLayout_XY");
// VertexLayout_XY_Color : VertexStruct_XY_Color
declaration = &s_declarations[VertexLayout_XY_Color];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XY_Color, position));
declaration->EnableComponent(VertexComponent_Color, ComponentType_Color, NazaraOffsetOf(VertexStruct_XY_Color, color));
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XY_Color), "Invalid stride for declaration VertexLayout_XY_Color");
s_declarations[VertexLayout_XY_Color] = NewDeclaration(VertexInputRate::Vertex, {
{
VertexComponent_Position,
ComponentType_Float2,
0
},
{
VertexComponent_Color,
ComponentType_Color,
0
},
});
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));
@@ -287,7 +185,7 @@ namespace Nz
declaration->EnableComponent(VertexComponent_InstanceData2, ComponentType_Float4, NazaraOffsetOf(Matrix4f, m31));
declaration->EnableComponent(VertexComponent_InstanceData3, ComponentType_Float4, NazaraOffsetOf(Matrix4f, m41));
NazaraAssert(declaration->GetStride() == sizeof(Matrix4f), "Invalid stride for declaration VertexLayout_Matrix4");
NazaraAssert(declaration->GetStride() == sizeof(Matrix4f), "Invalid stride for declaration VertexLayout_Matrix4");*/
}
catch (const std::exception& e)
{
@@ -301,8 +199,10 @@ namespace Nz
void VertexDeclaration::Uninitialize()
{
VertexDeclarationLibrary::Uninitialize();
s_declarations.fill(nullptr);
}
std::array<VertexDeclaration, VertexLayout_Max + 1> VertexDeclaration::s_declarations;
std::array<VertexDeclarationRef, VertexLayout_Max + 1> VertexDeclaration::s_declarations;
VertexDeclarationLibrary::LibraryMap VertexDeclaration::s_library;
}

View File

@@ -4,6 +4,7 @@
#include <Nazara/VulkanRenderer/VulkanRenderPipeline.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/VulkanRenderer/VulkanShaderStage.hpp>
#include <Nazara/VulkanRenderer/Utils.hpp>
#include <cassert>
#include <Nazara/VulkanRenderer/Debug.hpp>
@@ -16,11 +17,13 @@ namespace Nz
{
}
/*VkPipelineColorBlendAttachmentState VulkanRenderPipeline::BuildColorBlendAttachmentState(const RenderPipelineInfo& pipelineInfo)
std::vector<VkPipelineColorBlendAttachmentState> VulkanRenderPipeline::BuildColorBlendAttachmentStateList(const RenderPipelineInfo& pipelineInfo)
{
VkPipelineColorBlendAttachmentState colorBlendStates;
colorBlendStates.blendEnable = pipelineInfo.blending;
colorBlendStates.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; //< TODO
std::vector<VkPipelineColorBlendAttachmentState> colorBlendStates;
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
if (pipelineInfo.blending)
{
@@ -29,18 +32,29 @@ namespace Nz
{
blendState.dstAlphaBlendFactor
}*/
/*}
}
else
{
colorBlendStates.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendStates.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendStates.colorBlendOp = VK_BLEND_OP_ADD;
colorBlendStates.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendStates.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendStates.alphaBlendOp = VK_BLEND_OP_ADD;
colorBlendState.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendState.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendState.colorBlendOp = VK_BLEND_OP_ADD;
colorBlendState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendState.alphaBlendOp = VK_BLEND_OP_ADD;
}
return colorBlendStates;
}*/
}
VkPipelineColorBlendStateCreateInfo VulkanRenderPipeline::BuildColorBlendInfo(const RenderPipelineInfo& pipelineInfo, const std::vector<VkPipelineColorBlendAttachmentState>& attachmentState)
{
VkPipelineColorBlendStateCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
createInfo.attachmentCount = std::uint32_t(attachmentState.size());
createInfo.pAttachments = attachmentState.data();
return createInfo;
}
VkPipelineDepthStencilStateCreateInfo VulkanRenderPipeline::BuildDepthStencilInfo(const RenderPipelineInfo& pipelineInfo)
{
@@ -56,6 +70,21 @@ namespace Nz
return createInfo;
}
VkPipelineDynamicStateCreateInfo VulkanRenderPipeline::BuildDynamicStateInfo(const RenderPipelineInfo& pipelineInfo, const std::vector<VkDynamicState>& dynamicStates)
{
VkPipelineDynamicStateCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
createInfo.dynamicStateCount = std::uint32_t(dynamicStates.size());
createInfo.pDynamicStates = dynamicStates.data();
return createInfo;
}
std::vector<VkDynamicState> VulkanRenderPipeline::BuildDynamicStateList(const RenderPipelineInfo& pipelineInfo)
{
return { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
}
VkPipelineInputAssemblyStateCreateInfo VulkanRenderPipeline::BuildInputAssemblyInfo(const RenderPipelineInfo& pipelineInfo)
{
VkPipelineInputAssemblyStateCreateInfo createInfo = {};
@@ -77,6 +106,15 @@ namespace Nz
return createInfo;
}
VkPipelineViewportStateCreateInfo VulkanRenderPipeline::BuildViewportInfo(const RenderPipelineInfo& pipelineInfo)
{
VkPipelineViewportStateCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
createInfo.scissorCount = createInfo.viewportCount = 1; //< TODO
return createInfo;
}
VkStencilOpState VulkanRenderPipeline::BuildStencilOp(const RenderPipelineInfo& pipelineInfo, bool front)
{
const auto& pipelineStencil = (front) ? pipelineInfo.stencilFront : pipelineInfo.stencilBack;
@@ -99,10 +137,41 @@ namespace Nz
for (auto&& stagePtr : pipelineInfo.shaderStages)
{
Nz::VulkanShaderStage& vulkanStage = *static_cast<Nz::VulkanShaderStage*>(stagePtr.get());
VkPipelineShaderStageCreateInfo& createInfo = shaderStageCreateInfos.emplace_back();
//createInfo.
createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
createInfo.module = vulkanStage.GetHandle();
createInfo.pName = "main";
createInfo.stage = ToVulkan(vulkanStage.GetStageType());
}
return shaderStageCreateInfos;
}
auto VulkanRenderPipeline::BuildCreateInfo(const RenderPipelineInfo& pipelineInfo) -> CreateInfo
{
CreateInfo createInfo = {};
createInfo.stateData = std::make_unique<CreateInfo::StateData>();
createInfo.colorBlendAttachmentState = BuildColorBlendAttachmentStateList(pipelineInfo);
createInfo.dynamicStates = BuildDynamicStateList(pipelineInfo);
createInfo.shaderStages = BuildShaderStageInfo(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->rasterizationState = BuildRasterizationInfo(pipelineInfo);
createInfo.stateData->viewportState = BuildViewportInfo(pipelineInfo);
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.
return createInfo;
}
}