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

@ -1,40 +1,32 @@
// Copyright (C) 2017 Jérôme Leclercq // Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module" // This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Algorithm.hpp> #include <Nazara/Utility/Algorithm.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Utility/Debug.hpp> #include <Nazara/Utility/Debug.hpp>
namespace Nz namespace Nz
{ {
namespace Detail
{
template<typename T>
struct IsSuitableForComponent
{
constexpr static bool value = false;
};
}
template<typename T> constexpr ComponentType ComponentTypeId() template<typename T> constexpr ComponentType ComponentTypeId()
{ {
static_assert(Detail::IsSuitableForComponent<T>::value, "This type cannot be used as a component."); static_assert(AlwaysFalse<T>::value, "This type cannot be used as a component.");
return ComponentType{}; return ComponentType{};
} }
template<> constexpr ComponentType ComponentTypeId<Color>() { return ComponentType_Color; } template<> constexpr ComponentType ComponentTypeId<Color>() { return ComponentType_Color; }
template<> constexpr ComponentType ComponentTypeId<double>() { return ComponentType_Double1; } template<> constexpr ComponentType ComponentTypeId<double>() { return ComponentType_Double1; }
template<> constexpr ComponentType ComponentTypeId<Vector2d>() { return ComponentType_Double2; } template<> constexpr ComponentType ComponentTypeId<Vector2d>() { return ComponentType_Double2; }
template<> constexpr ComponentType ComponentTypeId<Vector3d>() { return ComponentType_Double3; } template<> constexpr ComponentType ComponentTypeId<Vector3d>() { return ComponentType_Double3; }
template<> constexpr ComponentType ComponentTypeId<Vector4d>() { return ComponentType_Double4; } template<> constexpr ComponentType ComponentTypeId<Vector4d>() { return ComponentType_Double4; }
template<> constexpr ComponentType ComponentTypeId<float>() { return ComponentType_Float1; } template<> constexpr ComponentType ComponentTypeId<float>() { return ComponentType_Float1; }
template<> constexpr ComponentType ComponentTypeId<Vector2f>() { return ComponentType_Float2; } template<> constexpr ComponentType ComponentTypeId<Vector2f>() { return ComponentType_Float2; }
template<> constexpr ComponentType ComponentTypeId<Vector3f>() { return ComponentType_Float3; } template<> constexpr ComponentType ComponentTypeId<Vector3f>() { return ComponentType_Float3; }
template<> constexpr ComponentType ComponentTypeId<Vector4f>() { return ComponentType_Float4; } template<> constexpr ComponentType ComponentTypeId<Vector4f>() { return ComponentType_Float4; }
template<> constexpr ComponentType ComponentTypeId<int>() { return ComponentType_Int1; } template<> constexpr ComponentType ComponentTypeId<int>() { return ComponentType_Int1; }
template<> constexpr ComponentType ComponentTypeId<Vector2i>() { return ComponentType_Int2; } template<> constexpr ComponentType ComponentTypeId<Vector2i>() { return ComponentType_Int2; }
template<> constexpr ComponentType ComponentTypeId<Vector3i>() { return ComponentType_Int3; } template<> constexpr ComponentType ComponentTypeId<Vector3i>() { return ComponentType_Int3; }
template<> constexpr ComponentType ComponentTypeId<Vector4i>() { return ComponentType_Int4; } template<> constexpr ComponentType ComponentTypeId<Vector4i>() { return ComponentType_Int4; }
template<> constexpr ComponentType ComponentTypeId<Quaternionf>() { return ComponentType_Quaternion; } template<> constexpr ComponentType ComponentTypeId<Quaternionf>() { return ComponentType_Quaternion; }
template<typename T> template<typename T>

View File

@ -356,35 +356,25 @@ namespace Nz
{ {
VertexComponent_Unused = -1, VertexComponent_Unused = -1,
// We limit to 16 components by vertex since it's the minimal number supported by the GPU
VertexComponent_InstanceData0,
VertexComponent_InstanceData1,
VertexComponent_InstanceData2,
VertexComponent_InstanceData3,
VertexComponent_InstanceData4,
VertexComponent_InstanceData5,
VertexComponent_Color, VertexComponent_Color,
VertexComponent_Normal, VertexComponent_Normal,
VertexComponent_Position, VertexComponent_Position,
VertexComponent_Tangent, VertexComponent_Tangent,
VertexComponent_TexCoord, VertexComponent_TexCoord,
VertexComponent_Userdata0, VertexComponent_Userdata,
VertexComponent_Userdata1,
VertexComponent_Userdata2,
VertexComponent_Userdata3,
VertexComponent_Userdata4,
VertexComponent_FirstInstanceData = VertexComponent_InstanceData0, VertexComponent_Max = VertexComponent_Userdata
VertexComponent_FirstVertexData = VertexComponent_Color, };
VertexComponent_LastInstanceData = VertexComponent_InstanceData5,
VertexComponent_LastVertexData = VertexComponent_Userdata4,
VertexComponent_Max = VertexComponent_Userdata4 enum class VertexInputRate
{
Instance,
Vertex
}; };
enum VertexLayout enum VertexLayout
{ {
// Declarations meant for the rendering // Predefined declarations for rendering
VertexLayout_XY, VertexLayout_XY,
VertexLayout_XY_Color, VertexLayout_XY_Color,
VertexLayout_XY_UV, VertexLayout_XY_UV,
@ -397,7 +387,7 @@ namespace Nz
VertexLayout_XYZ_Normal_UV_Tangent_Skinning, VertexLayout_XYZ_Normal_UV_Tangent_Skinning,
VertexLayout_XYZ_UV, VertexLayout_XYZ_UV,
// Declarations meant for the instancing // Predefined declarations for instancing
VertexLayout_Matrix4, VertexLayout_Matrix4,
VertexLayout_Max = VertexLayout_Matrix4 VertexLayout_Max = VertexLayout_Matrix4

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" // This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
@ -11,7 +11,7 @@
#include <Nazara/Core/ObjectLibrary.hpp> #include <Nazara/Core/ObjectLibrary.hpp>
#include <Nazara/Core/ObjectRef.hpp> #include <Nazara/Core/ObjectRef.hpp>
#include <Nazara/Core/RefCounted.hpp> #include <Nazara/Core/RefCounted.hpp>
#include <Nazara/Core/Signal.hpp> #include <Nazara/Core/SparsePtr.hpp>
#include <Nazara/Utility/Config.hpp> #include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Enums.hpp> #include <Nazara/Utility/Enums.hpp>
#include <array> #include <array>
@ -30,52 +30,57 @@ namespace Nz
friend class Utility; friend class Utility;
public: public:
VertexDeclaration(); struct Component;
VertexDeclaration(const VertexDeclaration& declaration); struct ComponentEntry;
~VertexDeclaration();
void DisableComponent(VertexComponent component); VertexDeclaration(VertexInputRate inputRate, std::initializer_list<ComponentEntry> components);
void EnableComponent(VertexComponent component, ComponentType type, std::size_t offset); VertexDeclaration(const VertexDeclaration&) = delete;
VertexDeclaration(VertexDeclaration&&) noexcept = default;
~VertexDeclaration() = default;
void GetComponent(VertexComponent component, bool* enabled, ComponentType* type, std::size_t* offset) const; inline const Component* FindComponent(VertexComponent vertexComponent, std::size_t componentIndex) const;
bool HasComponent(VertexComponent component) const;
template<typename T> bool HasComponentOfType(VertexComponent component) const;
std::size_t GetStride() const;
void SetStride(std::size_t stride); template<typename T> const Component* GetComponentByType(VertexComponent vertexComponent, std::size_t componentIndex = 0) const;
VertexDeclaration& operator=(const VertexDeclaration& declaration); inline const Component& GetComponent(std::size_t componentIndex) const;
inline std::size_t GetComponentCount() const;
inline VertexInputRate GetInputRate() const;
inline std::size_t GetStride() const;
static VertexDeclaration* Get(VertexLayout layout); inline bool HasComponent(VertexComponent component, std::size_t componentIndex = 0) const;
template<typename T> bool HasComponentOfType(VertexComponent vertexComponent, std::size_t componentIndex = 0) const;
VertexDeclaration& operator=(const VertexDeclaration&) = delete;
VertexDeclaration& operator=(VertexDeclaration&&) noexcept = default;
static const VertexDeclarationRef& Get(VertexLayout layout);
static bool IsTypeSupported(ComponentType type); static bool IsTypeSupported(ComponentType type);
template<typename... Args> static VertexDeclarationRef New(Args&&... args); template<typename... Args> static VertexDeclarationRef New(Args&&... args);
// Signals: struct Component
NazaraSignal(OnVertexDeclarationRelease, const VertexDeclaration* /*vertexDeclaration*/); {
ComponentType type;
VertexComponent component;
std::size_t componentIndex;
std::size_t offset;
};
struct ComponentEntry
{
VertexComponent component;
ComponentType type;
std::size_t componentIndex;
};
private: private:
static bool Initialize(); static bool Initialize();
static void Uninitialize(); static void Uninitialize();
struct Component std::vector<Component> m_components;
{
ComponentType type; // Le type de donnée
bool enabled = false; // Ce composant est-il activé ?/
std::size_t offset; // La position, en octets, de la première donnée
/*
** -Lynix:
** Il serait aussi possible de préciser le stride de façon indépendante, ce que je ne permets pas
** pour décomplexifier l'interface en enlevant quelque chose que je juge inutile.
** Si vous pensez que ça peut être utile, n'hésitez pas à me le faire savoir !
** PS: Même cas pour le diviseur (instancing)
*/
};
std::array<Component, VertexComponent_Max + 1> m_components;
std::size_t m_stride; std::size_t m_stride;
VertexInputRate m_inputRate;
static std::array<VertexDeclaration, VertexLayout_Max + 1> s_declarations; static std::array<VertexDeclarationRef, VertexLayout_Max + 1> s_declarations;
static VertexDeclarationLibrary::LibraryMap s_library; static VertexDeclarationLibrary::LibraryMap s_library;
}; };
} }

View File

@ -1,32 +1,87 @@
// Copyright (C) 2017 Jérôme Leclercq // Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module" // This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/VertexDeclaration.hpp>
#include <Nazara/Utility/Algorithm.hpp>
#include <cassert>
#include <memory> #include <memory>
#include <Nazara/Utility/Debug.hpp> #include <Nazara/Utility/Debug.hpp>
#include <Nazara/Utility/Algorithm.hpp>
namespace Nz namespace Nz
{ {
inline auto Nz::VertexDeclaration::FindComponent(VertexComponent vertexComponent, std::size_t componentIndex) const -> const Component*
{
assert(componentIndex == 0 || vertexComponent == VertexComponent_Userdata);
for (const Component& component : m_components)
{
if (component.component == vertexComponent && component.componentIndex == componentIndex)
return &component;
}
return nullptr;
}
inline auto VertexDeclaration::GetComponent(std::size_t componentIndex) const -> const Component&
{
return m_components[componentIndex];
}
inline std::size_t VertexDeclaration::GetComponentCount() const
{
return m_components.size();
}
inline VertexInputRate VertexDeclaration::GetInputRate() const
{
return m_inputRate;
}
inline std::size_t VertexDeclaration::GetStride() const
{
return m_stride;
}
inline bool VertexDeclaration::HasComponent(VertexComponent component, std::size_t componentIndex) const
{
return FindComponent(component, componentIndex) != nullptr;
}
template<typename T>
auto VertexDeclaration::GetComponentByType(VertexComponent vertexComponent, std::size_t componentIndex) const -> const Component*
{
NazaraAssert(componentIndex == 0 || vertexComponent == VertexComponent_Userdata, "Only userdata vertex component can have component indexes");
if (const Component* component = FindComponent(vertexComponent, componentIndex))
{
if (GetComponentTypeOf<T>() == component->type)
return component;
}
return nullptr;
}
template<typename T>
bool VertexDeclaration::HasComponentOfType(VertexComponent vertexComponent, std::size_t componentIndex) const
{
return GetComponentByType<T>(vertexComponent, componentIndex) != nullptr;
}
const VertexDeclarationRef& VertexDeclaration::Get(VertexLayout layout)
{
NazaraAssert(layout <= VertexLayout_Max, "Vertex layout out of enum");
return s_declarations[layout];
}
template<typename... Args> template<typename... Args>
VertexDeclarationRef VertexDeclaration::New(Args&&... args) VertexDeclarationRef VertexDeclaration::New(Args&&... args)
{ {
std::unique_ptr<VertexDeclaration> object(new VertexDeclaration(std::forward<Args>(args)...)); std::unique_ptr<VertexDeclaration> object = std::make_unique<VertexDeclaration>(std::forward<Args>(args)...);
object->SetPersistent(false); object->SetPersistent(false);
return object.release(); return object.release();
} }
template<typename T>
bool VertexDeclaration::HasComponentOfType(VertexComponent component) const
{
bool enabled;
Nz::ComponentType type;
GetComponent(component, &enabled, &type, nullptr);
return enabled && GetComponentTypeOf<T>() == type;
}
} }
#include <Nazara/Utility/DebugOff.hpp> #include <Nazara/Utility/DebugOff.hpp>

View File

@ -9,6 +9,7 @@
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/String.hpp> #include <Nazara/Core/String.hpp>
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Utility/Enums.hpp> #include <Nazara/Utility/Enums.hpp>
#include <Nazara/VulkanRenderer/Wrapper/Loader.hpp> #include <Nazara/VulkanRenderer/Wrapper/Loader.hpp>
@ -18,6 +19,7 @@ namespace Nz
inline VkPolygonMode ToVulkan(FaceFilling faceFilling); inline VkPolygonMode ToVulkan(FaceFilling faceFilling);
inline VkPrimitiveTopology ToVulkan(PrimitiveMode primitiveMode); inline VkPrimitiveTopology ToVulkan(PrimitiveMode primitiveMode);
inline VkCompareOp ToVulkan(RendererComparison comparison); inline VkCompareOp ToVulkan(RendererComparison comparison);
inline VkShaderStageFlagBits ToVulkan(ShaderStageType stageType);
inline VkStencilOp ToVulkan(StencilOperation stencilOp); inline VkStencilOp ToVulkan(StencilOperation stencilOp);
NAZARA_VULKANRENDERER_API String TranslateVulkanError(VkResult code); NAZARA_VULKANRENDERER_API String TranslateVulkanError(VkResult code);
} }

View File

@ -70,6 +70,18 @@ namespace Nz
return VK_COMPARE_OP_NEVER; return VK_COMPARE_OP_NEVER;
} }
VkShaderStageFlagBits ToVulkan(ShaderStageType stageType)
{
switch (stageType)
{
case ShaderStageType::Fragment: return VK_SHADER_STAGE_FRAGMENT_BIT;
case ShaderStageType::Vertex: return VK_SHADER_STAGE_VERTEX_BIT;
}
NazaraError("Unhandled ShaderStageType 0x" + String::Number(static_cast<std::size_t>(stageType), 16));
return {};
}
VkStencilOp ToVulkan(StencilOperation stencilOp) VkStencilOp ToVulkan(StencilOperation stencilOp)
{ {
switch (stencilOp) switch (stencilOp)

View File

@ -23,19 +23,37 @@ namespace Nz
VulkanRenderPipeline(Vk::DeviceHandle device, RenderPipelineInfo pipelineInfo); VulkanRenderPipeline(Vk::DeviceHandle device, RenderPipelineInfo pipelineInfo);
~VulkanRenderPipeline() = default; ~VulkanRenderPipeline() = default;
static std::vector<VkPipelineColorBlendAttachmentState> BuildColorBlendAttachmentState(const RenderPipelineInfo& pipelineInfo); static std::vector<VkPipelineColorBlendAttachmentState> BuildColorBlendAttachmentStateList(const RenderPipelineInfo& pipelineInfo);
static VkPipelineColorBlendStateCreateInfo BuildColorBlendInfo(const RenderPipelineInfo& pipelineInfo, const std::vector<VkPipelineColorBlendAttachmentState>& attachmentState);
static VkPipelineDepthStencilStateCreateInfo BuildDepthStencilInfo(const RenderPipelineInfo& pipelineInfo); static VkPipelineDepthStencilStateCreateInfo BuildDepthStencilInfo(const RenderPipelineInfo& pipelineInfo);
static VkPipelineDynamicStateCreateInfo BuildDynamicStateInfo(const RenderPipelineInfo& pipelineInfo, const std::vector<VkDynamicState>& dynamicStates);
static std::vector<VkDynamicState> BuildDynamicStateList(const RenderPipelineInfo& pipelineInfo);
static VkPipelineInputAssemblyStateCreateInfo BuildInputAssemblyInfo(const RenderPipelineInfo& pipelineInfo); static VkPipelineInputAssemblyStateCreateInfo BuildInputAssemblyInfo(const RenderPipelineInfo& pipelineInfo);
static VkPipelineRasterizationStateCreateInfo BuildRasterizationInfo(const RenderPipelineInfo& pipelineInfo); static VkPipelineRasterizationStateCreateInfo BuildRasterizationInfo(const RenderPipelineInfo& pipelineInfo);
static VkPipelineViewportStateCreateInfo BuildViewportInfo(const RenderPipelineInfo& pipelineInfo);
static VkStencilOpState BuildStencilOp(const RenderPipelineInfo& pipelineInfo, bool front); static VkStencilOpState BuildStencilOp(const RenderPipelineInfo& pipelineInfo, bool front);
static std::vector<VkPipelineShaderStageCreateInfo> BuildShaderStageInfo(const RenderPipelineInfo& pipelineInfo); static std::vector<VkPipelineShaderStageCreateInfo> BuildShaderStageInfo(const RenderPipelineInfo& pipelineInfo);
static CreateInfo BuildCreateInfo(const RenderPipelineInfo& pipelineInfo); static CreateInfo BuildCreateInfo(const RenderPipelineInfo& pipelineInfo);
struct CreateInfo struct CreateInfo
{ {
struct StateData
{
VkPipelineVertexInputStateCreateInfo vertexInputState;
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState;
VkPipelineViewportStateCreateInfo viewportState;
VkPipelineRasterizationStateCreateInfo rasterizationState;
VkPipelineDepthStencilStateCreateInfo depthStencilState;
VkPipelineColorBlendStateCreateInfo colorBlendState;
VkPipelineDynamicStateCreateInfo dynamicState;
};
std::vector<VkPipelineColorBlendAttachmentState> colorBlendAttachmentState; std::vector<VkPipelineColorBlendAttachmentState> colorBlendAttachmentState;
std::vector<VkDynamicState> dynamicStates;
std::vector<VkPipelineShaderStageCreateInfo> shaderStages; std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
VkGraphicsPipelineCreateInfo createInfo; std::unique_ptr<StateData> stateData;
VkGraphicsPipelineCreateInfo pipelineInfo;
}; };
private: private:

View File

@ -26,6 +26,7 @@ namespace Nz
bool Create(const Vk::DeviceHandle& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize); bool Create(const Vk::DeviceHandle& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize);
inline const Vk::ShaderModule& GetHandle() const; inline const Vk::ShaderModule& GetHandle() const;
inline ShaderStageType GetStageType() const;
VulkanShaderStage& operator=(const VulkanShaderStage&) = delete; VulkanShaderStage& operator=(const VulkanShaderStage&) = delete;
VulkanShaderStage& operator=(VulkanShaderStage&&) noexcept = default; VulkanShaderStage& operator=(VulkanShaderStage&&) noexcept = default;

View File

@ -11,6 +11,11 @@ namespace Nz
{ {
return m_shaderModule; return m_shaderModule;
} }
inline ShaderStageType VulkanShaderStage::GetStageType() const
{
return m_stage;
}
} }
#include <Nazara/VulkanRenderer/DebugOff.hpp> #include <Nazara/VulkanRenderer/DebugOff.hpp>

View File

@ -211,7 +211,7 @@ namespace Nz
std::size_t Utility::ComponentStride[ComponentType_Max+1] = std::size_t Utility::ComponentStride[ComponentType_Max+1] =
{ {
4*sizeof(UInt8), // ComponentType_Color 4*sizeof(UInt8), // ComponentType_Color
1*sizeof(double), // ComponentType_Double1 1*sizeof(double), // ComponentType_Double1
2*sizeof(double), // ComponentType_Double2 2*sizeof(double), // ComponentType_Double2
3*sizeof(double), // ComponentType_Double3 3*sizeof(double), // ComponentType_Double3
@ -220,10 +220,10 @@ namespace Nz
2*sizeof(float), // ComponentType_Float2 2*sizeof(float), // ComponentType_Float2
3*sizeof(float), // ComponentType_Float3 3*sizeof(float), // ComponentType_Float3
4*sizeof(float), // ComponentType_Float4 4*sizeof(float), // ComponentType_Float4
1*sizeof(UInt32), // ComponentType_Int1 1*sizeof(UInt32), // ComponentType_Int1
2*sizeof(UInt32), // ComponentType_Int2 2*sizeof(UInt32), // ComponentType_Int2
3*sizeof(UInt32), // ComponentType_Int3 3*sizeof(UInt32), // ComponentType_Int3
4*sizeof(UInt32), // ComponentType_Int4 4*sizeof(UInt32), // ComponentType_Int4
4*sizeof(float) // ComponentType_Quaternion 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" // This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
@ -14,144 +14,27 @@
namespace Nz namespace Nz
{ {
VertexDeclaration::VertexDeclaration() : VertexDeclaration::VertexDeclaration(VertexInputRate inputRate, std::initializer_list<ComponentEntry> components) :
m_stride(0) m_inputRate(inputRate)
{ {
} std::size_t offset = 0;
VertexDeclaration::VertexDeclaration(const VertexDeclaration& declaration) : m_components.reserve(components.size());
RefCounted(), for (const ComponentEntry& entry : components)
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)
{ {
NazaraError("Vertex component out of enum"); auto& component = m_components.emplace_back();
return; component.component = entry.component;
} component.componentIndex = entry.componentIndex;
#endif component.offset = offset;
component.type = entry.type;
#if NAZARA_UTILITY_SAFE NazaraAssert(IsTypeSupported(component.type), "Component type 0x" + String::Number(component.type, 16) + " is not supported by vertex declarations");
if (component == VertexComponent_Unused) NazaraAssert(component.componentIndex == 0 || component.component == VertexComponent_Userdata, "Only userdata components can have non-zero component indexes");
{
NazaraError("Cannot disable \"unused\" component");
return;
}
#endif
Component& vertexComponent = m_components[component]; offset += Utility::ComponentStride[component.type];
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;
} }
m_stride += Utility::ComponentStride[type]; m_stride = offset;
}
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];
} }
bool VertexDeclaration::IsTypeSupported(ComponentType type) bool VertexDeclaration::IsTypeSupported(ComponentType type)
@ -193,22 +76,37 @@ namespace Nz
{ {
ErrorFlags flags(ErrorFlag_Silent | ErrorFlag_ThrowException); ErrorFlags flags(ErrorFlag_Silent | ErrorFlag_ThrowException);
// Layout : Type auto NewDeclaration = [](VertexInputRate inputRate, std::initializer_list<ComponentEntry> components)
VertexDeclaration* declaration; {
return New(inputRate, std::move(components));
};
// VertexLayout_XY : VertexStruct_XY // VertexLayout_XY : VertexStruct_XY
declaration = &s_declarations[VertexLayout_XY]; s_declarations[VertexLayout_XY] = NewDeclaration(VertexInputRate::Vertex, {
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XY, position)); {
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 s_declarations[VertexLayout_XY_Color] = NewDeclaration(VertexInputRate::Vertex, {
declaration = &s_declarations[VertexLayout_XY_Color]; {
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XY_Color, position)); VertexComponent_Position,
declaration->EnableComponent(VertexComponent_Color, ComponentType_Color, NazaraOffsetOf(VertexStruct_XY_Color, color)); ComponentType_Float2,
0
NazaraAssert(declaration->GetStride() == sizeof(VertexStruct_XY_Color), "Invalid stride for declaration VertexLayout_XY_Color"); },
{
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 // VertexLayout_XY_UV : VertexStruct_XY_UV
declaration = &s_declarations[VertexLayout_XY_UV]; declaration = &s_declarations[VertexLayout_XY_UV];
declaration->EnableComponent(VertexComponent_Position, ComponentType_Float2, NazaraOffsetOf(VertexStruct_XY_UV, position)); 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_InstanceData2, ComponentType_Float4, NazaraOffsetOf(Matrix4f, m31));
declaration->EnableComponent(VertexComponent_InstanceData3, ComponentType_Float4, NazaraOffsetOf(Matrix4f, m41)); 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) catch (const std::exception& e)
{ {
@ -301,8 +199,10 @@ namespace Nz
void VertexDeclaration::Uninitialize() void VertexDeclaration::Uninitialize()
{ {
VertexDeclarationLibrary::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; VertexDeclarationLibrary::LibraryMap VertexDeclaration::s_library;
} }

View File

@ -4,6 +4,7 @@
#include <Nazara/VulkanRenderer/VulkanRenderPipeline.hpp> #include <Nazara/VulkanRenderer/VulkanRenderPipeline.hpp>
#include <Nazara/Core/ErrorFlags.hpp> #include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/VulkanRenderer/VulkanShaderStage.hpp>
#include <Nazara/VulkanRenderer/Utils.hpp> #include <Nazara/VulkanRenderer/Utils.hpp>
#include <cassert> #include <cassert>
#include <Nazara/VulkanRenderer/Debug.hpp> #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; std::vector<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 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) if (pipelineInfo.blending)
{ {
@ -29,18 +32,29 @@ namespace Nz
{ {
blendState.dstAlphaBlendFactor blendState.dstAlphaBlendFactor
}*/ }*/
/*} }
else else
{ {
colorBlendStates.srcColorBlendFactor = VK_BLEND_FACTOR_ONE; colorBlendState.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendStates.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO; colorBlendState.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendStates.colorBlendOp = VK_BLEND_OP_ADD; colorBlendState.colorBlendOp = VK_BLEND_OP_ADD;
colorBlendStates.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; colorBlendState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendStates.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; colorBlendState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
colorBlendStates.alphaBlendOp = VK_BLEND_OP_ADD; colorBlendState.alphaBlendOp = VK_BLEND_OP_ADD;
} }
return colorBlendStates; 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) VkPipelineDepthStencilStateCreateInfo VulkanRenderPipeline::BuildDepthStencilInfo(const RenderPipelineInfo& pipelineInfo)
{ {
@ -56,6 +70,21 @@ namespace Nz
return createInfo; 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 VulkanRenderPipeline::BuildInputAssemblyInfo(const RenderPipelineInfo& pipelineInfo)
{ {
VkPipelineInputAssemblyStateCreateInfo createInfo = {}; VkPipelineInputAssemblyStateCreateInfo createInfo = {};
@ -77,6 +106,15 @@ namespace Nz
return createInfo; 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) VkStencilOpState VulkanRenderPipeline::BuildStencilOp(const RenderPipelineInfo& pipelineInfo, bool front)
{ {
const auto& pipelineStencil = (front) ? pipelineInfo.stencilFront : pipelineInfo.stencilBack; const auto& pipelineStencil = (front) ? pipelineInfo.stencilFront : pipelineInfo.stencilBack;
@ -99,10 +137,41 @@ namespace Nz
for (auto&& stagePtr : pipelineInfo.shaderStages) for (auto&& stagePtr : pipelineInfo.shaderStages)
{ {
Nz::VulkanShaderStage& vulkanStage = *static_cast<Nz::VulkanShaderStage*>(stagePtr.get());
VkPipelineShaderStageCreateInfo& createInfo = shaderStageCreateInfos.emplace_back(); 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; 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;
}
} }