diff --git a/include/Nazara/Utility/Algorithm.inl b/include/Nazara/Utility/Algorithm.inl index 829276bfb..e3f1e8be3 100644 --- a/include/Nazara/Utility/Algorithm.inl +++ b/include/Nazara/Utility/Algorithm.inl @@ -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" // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include namespace Nz { - namespace Detail - { - template - struct IsSuitableForComponent - { - constexpr static bool value = false; - }; - } - template constexpr ComponentType ComponentTypeId() { - static_assert(Detail::IsSuitableForComponent::value, "This type cannot be used as a component."); + static_assert(AlwaysFalse::value, "This type cannot be used as a component."); return ComponentType{}; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Color; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double1; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double2; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double3; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double4; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float1; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float2; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float3; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float4; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int1; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int2; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int3; } - template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int4; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Color; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double1; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double2; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double3; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Double4; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float1; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float2; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float3; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Float4; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int1; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int2; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int3; } + template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Int4; } template<> constexpr ComponentType ComponentTypeId() { return ComponentType_Quaternion; } template diff --git a/include/Nazara/Utility/Enums.hpp b/include/Nazara/Utility/Enums.hpp index cc7413844..d9891b026 100644 --- a/include/Nazara/Utility/Enums.hpp +++ b/include/Nazara/Utility/Enums.hpp @@ -356,35 +356,25 @@ namespace Nz { 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_Normal, VertexComponent_Position, VertexComponent_Tangent, VertexComponent_TexCoord, - VertexComponent_Userdata0, - VertexComponent_Userdata1, - VertexComponent_Userdata2, - VertexComponent_Userdata3, - VertexComponent_Userdata4, + VertexComponent_Userdata, - VertexComponent_FirstInstanceData = VertexComponent_InstanceData0, - VertexComponent_FirstVertexData = VertexComponent_Color, - VertexComponent_LastInstanceData = VertexComponent_InstanceData5, - VertexComponent_LastVertexData = VertexComponent_Userdata4, + VertexComponent_Max = VertexComponent_Userdata + }; - VertexComponent_Max = VertexComponent_Userdata4 + enum class VertexInputRate + { + Instance, + Vertex }; enum VertexLayout { - // Declarations meant for the rendering + // Predefined declarations for rendering VertexLayout_XY, VertexLayout_XY_Color, VertexLayout_XY_UV, @@ -397,7 +387,7 @@ namespace Nz VertexLayout_XYZ_Normal_UV_Tangent_Skinning, VertexLayout_XYZ_UV, - // Declarations meant for the instancing + // Predefined declarations for instancing VertexLayout_Matrix4, VertexLayout_Max = VertexLayout_Matrix4 diff --git a/include/Nazara/Utility/VertexDeclaration.hpp b/include/Nazara/Utility/VertexDeclaration.hpp index 536707e17..649482e6b 100644 --- a/include/Nazara/Utility/VertexDeclaration.hpp +++ b/include/Nazara/Utility/VertexDeclaration.hpp @@ -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 @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include @@ -30,52 +30,57 @@ namespace Nz friend class Utility; public: - VertexDeclaration(); - VertexDeclaration(const VertexDeclaration& declaration); - ~VertexDeclaration(); + struct Component; + struct ComponentEntry; - void DisableComponent(VertexComponent component); - void EnableComponent(VertexComponent component, ComponentType type, std::size_t offset); + VertexDeclaration(VertexInputRate inputRate, std::initializer_list components); + VertexDeclaration(const VertexDeclaration&) = delete; + VertexDeclaration(VertexDeclaration&&) noexcept = default; + ~VertexDeclaration() = default; - void GetComponent(VertexComponent component, bool* enabled, ComponentType* type, std::size_t* offset) const; - bool HasComponent(VertexComponent component) const; - template bool HasComponentOfType(VertexComponent component) const; - std::size_t GetStride() const; + inline const Component* FindComponent(VertexComponent vertexComponent, std::size_t componentIndex) const; - void SetStride(std::size_t stride); + template 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 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); template static VertexDeclarationRef New(Args&&... args); - // Signals: - NazaraSignal(OnVertexDeclarationRelease, const VertexDeclaration* /*vertexDeclaration*/); + struct Component + { + ComponentType type; + VertexComponent component; + std::size_t componentIndex; + std::size_t offset; + }; + + struct ComponentEntry + { + VertexComponent component; + ComponentType type; + std::size_t componentIndex; + }; private: static bool Initialize(); static void Uninitialize(); - struct Component - { - 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 m_components; + std::vector m_components; std::size_t m_stride; + VertexInputRate m_inputRate; - static std::array s_declarations; + static std::array s_declarations; static VertexDeclarationLibrary::LibraryMap s_library; }; } diff --git a/include/Nazara/Utility/VertexDeclaration.inl b/include/Nazara/Utility/VertexDeclaration.inl index 8f8e74179..f1ee4c290 100644 --- a/include/Nazara/Utility/VertexDeclaration.inl +++ b/include/Nazara/Utility/VertexDeclaration.inl @@ -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" // For conditions of distribution and use, see copyright notice in Config.hpp +#include +#include +#include #include #include -#include 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 + 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() == component->type) + return component; + } + + return nullptr; + } + + template + bool VertexDeclaration::HasComponentOfType(VertexComponent vertexComponent, std::size_t componentIndex) const + { + return GetComponentByType(vertexComponent, componentIndex) != nullptr; + } + + const VertexDeclarationRef& VertexDeclaration::Get(VertexLayout layout) + { + NazaraAssert(layout <= VertexLayout_Max, "Vertex layout out of enum"); + + return s_declarations[layout]; + } + template VertexDeclarationRef VertexDeclaration::New(Args&&... args) { - std::unique_ptr object(new VertexDeclaration(std::forward(args)...)); + std::unique_ptr object = std::make_unique(std::forward(args)...); object->SetPersistent(false); return object.release(); } - - template - bool VertexDeclaration::HasComponentOfType(VertexComponent component) const - { - bool enabled; - Nz::ComponentType type; - - GetComponent(component, &enabled, &type, nullptr); - - return enabled && GetComponentTypeOf() == type; - } } #include diff --git a/include/Nazara/VulkanRenderer/Utils.hpp b/include/Nazara/VulkanRenderer/Utils.hpp index b80a3c3fd..40e5009a9 100644 --- a/include/Nazara/VulkanRenderer/Utils.hpp +++ b/include/Nazara/VulkanRenderer/Utils.hpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -18,6 +19,7 @@ namespace Nz inline VkPolygonMode ToVulkan(FaceFilling faceFilling); inline VkPrimitiveTopology ToVulkan(PrimitiveMode primitiveMode); inline VkCompareOp ToVulkan(RendererComparison comparison); + inline VkShaderStageFlagBits ToVulkan(ShaderStageType stageType); inline VkStencilOp ToVulkan(StencilOperation stencilOp); NAZARA_VULKANRENDERER_API String TranslateVulkanError(VkResult code); } diff --git a/include/Nazara/VulkanRenderer/Utils.inl b/include/Nazara/VulkanRenderer/Utils.inl index 4abbe1d0c..1f806bc43 100644 --- a/include/Nazara/VulkanRenderer/Utils.inl +++ b/include/Nazara/VulkanRenderer/Utils.inl @@ -70,6 +70,18 @@ namespace Nz 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(stageType), 16)); + return {}; + } + VkStencilOp ToVulkan(StencilOperation stencilOp) { switch (stencilOp) diff --git a/include/Nazara/VulkanRenderer/VulkanRenderPipeline.hpp b/include/Nazara/VulkanRenderer/VulkanRenderPipeline.hpp index aed7b6769..76f73d224 100644 --- a/include/Nazara/VulkanRenderer/VulkanRenderPipeline.hpp +++ b/include/Nazara/VulkanRenderer/VulkanRenderPipeline.hpp @@ -23,19 +23,37 @@ namespace Nz VulkanRenderPipeline(Vk::DeviceHandle device, RenderPipelineInfo pipelineInfo); ~VulkanRenderPipeline() = default; - static std::vector BuildColorBlendAttachmentState(const RenderPipelineInfo& pipelineInfo); + static std::vector BuildColorBlendAttachmentStateList(const RenderPipelineInfo& pipelineInfo); + static VkPipelineColorBlendStateCreateInfo BuildColorBlendInfo(const RenderPipelineInfo& pipelineInfo, const std::vector& attachmentState); static VkPipelineDepthStencilStateCreateInfo BuildDepthStencilInfo(const RenderPipelineInfo& pipelineInfo); + static VkPipelineDynamicStateCreateInfo BuildDynamicStateInfo(const RenderPipelineInfo& pipelineInfo, const std::vector& dynamicStates); + static std::vector BuildDynamicStateList(const RenderPipelineInfo& pipelineInfo); static VkPipelineInputAssemblyStateCreateInfo BuildInputAssemblyInfo(const RenderPipelineInfo& pipelineInfo); static VkPipelineRasterizationStateCreateInfo BuildRasterizationInfo(const RenderPipelineInfo& pipelineInfo); + static VkPipelineViewportStateCreateInfo BuildViewportInfo(const RenderPipelineInfo& pipelineInfo); static VkStencilOpState BuildStencilOp(const RenderPipelineInfo& pipelineInfo, bool front); static std::vector BuildShaderStageInfo(const RenderPipelineInfo& pipelineInfo); + static CreateInfo BuildCreateInfo(const RenderPipelineInfo& pipelineInfo); struct CreateInfo { + struct StateData + { + VkPipelineVertexInputStateCreateInfo vertexInputState; + VkPipelineInputAssemblyStateCreateInfo inputAssemblyState; + VkPipelineViewportStateCreateInfo viewportState; + VkPipelineRasterizationStateCreateInfo rasterizationState; + VkPipelineDepthStencilStateCreateInfo depthStencilState; + VkPipelineColorBlendStateCreateInfo colorBlendState; + VkPipelineDynamicStateCreateInfo dynamicState; + }; + std::vector colorBlendAttachmentState; + std::vector dynamicStates; std::vector shaderStages; - VkGraphicsPipelineCreateInfo createInfo; + std::unique_ptr stateData; + VkGraphicsPipelineCreateInfo pipelineInfo; }; private: diff --git a/include/Nazara/VulkanRenderer/VulkanShaderStage.hpp b/include/Nazara/VulkanRenderer/VulkanShaderStage.hpp index c7c4dcf92..533592a45 100644 --- a/include/Nazara/VulkanRenderer/VulkanShaderStage.hpp +++ b/include/Nazara/VulkanRenderer/VulkanShaderStage.hpp @@ -26,6 +26,7 @@ namespace Nz bool Create(const Vk::DeviceHandle& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize); inline const Vk::ShaderModule& GetHandle() const; + inline ShaderStageType GetStageType() const; VulkanShaderStage& operator=(const VulkanShaderStage&) = delete; VulkanShaderStage& operator=(VulkanShaderStage&&) noexcept = default; diff --git a/include/Nazara/VulkanRenderer/VulkanShaderStage.inl b/include/Nazara/VulkanRenderer/VulkanShaderStage.inl index 5796071bb..e3a634c83 100644 --- a/include/Nazara/VulkanRenderer/VulkanShaderStage.inl +++ b/include/Nazara/VulkanRenderer/VulkanShaderStage.inl @@ -11,6 +11,11 @@ namespace Nz { return m_shaderModule; } + + inline ShaderStageType VulkanShaderStage::GetStageType() const + { + return m_stage; + } } #include diff --git a/src/Nazara/Utility/Utility.cpp b/src/Nazara/Utility/Utility.cpp index 4c8f1809e..1efd1c861 100644 --- a/src/Nazara/Utility/Utility.cpp +++ b/src/Nazara/Utility/Utility.cpp @@ -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 }; diff --git a/src/Nazara/Utility/VertexDeclaration.cpp b/src/Nazara/Utility/VertexDeclaration.cpp index 7edbd3124..d44692e31 100644 --- a/src/Nazara/Utility/VertexDeclaration.cpp +++ b/src/Nazara/Utility/VertexDeclaration.cpp @@ -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 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 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::s_declarations; + std::array VertexDeclaration::s_declarations; VertexDeclarationLibrary::LibraryMap VertexDeclaration::s_library; } diff --git a/src/Nazara/VulkanRenderer/VulkanRenderPipeline.cpp b/src/Nazara/VulkanRenderer/VulkanRenderPipeline.cpp index 1071f7573..99a185107 100644 --- a/src/Nazara/VulkanRenderer/VulkanRenderPipeline.cpp +++ b/src/Nazara/VulkanRenderer/VulkanRenderPipeline.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -16,11 +17,13 @@ namespace Nz { } - /*VkPipelineColorBlendAttachmentState VulkanRenderPipeline::BuildColorBlendAttachmentState(const RenderPipelineInfo& pipelineInfo) + std::vector 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 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& 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& 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 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(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.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; + } }