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

@@ -30,6 +30,7 @@ namespace Nz
template<typename T> std::size_t CountOf(const T& c);
template<typename T> void HashCombine(std::size_t& seed, const T& v);
template<typename T> T ReverseBits(T integer);
template<typename T> auto UnderlyingCast(T value) -> std::underlying_type_t<T>;
template<typename T>
struct AlwaysFalse : std::false_type {};

View File

@@ -195,6 +195,12 @@ namespace Nz
return reversed;
}
template<typename T>
auto UnderlyingCast(T value) -> std::underlying_type_t<T>
{
return static_cast<std::underlying_type_t<T>>(value);
}
template<typename T> struct PointedType<T*> { using type = T; };
template<typename T> struct PointedType<T* const> { using type = T; };
template<typename T> struct PointedType<T* volatile> { using type = T; };

View File

@@ -9,6 +9,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Utility/VertexDeclaration.hpp>
#include <memory>
#include <vector>
@@ -36,7 +37,14 @@ namespace Nz
UInt32 writeMask = 0xFFFFFFFF;
} stencilBack, stencilFront;
struct VertexBufferData
{
std::size_t binding;
VertexDeclarationConstRef declaration;
};
std::vector<std::shared_ptr<ShaderStageImpl>> shaderStages;
std::vector<VertexBufferData> vertexBuffers;
bool blending = false;
bool colorWrite = true;

View File

@@ -53,7 +53,7 @@ namespace Nz
VertexDeclaration& operator=(const VertexDeclaration&) = delete;
VertexDeclaration& operator=(VertexDeclaration&&) noexcept = default;
static const VertexDeclarationRef& Get(VertexLayout layout);
static inline const VertexDeclarationRef& Get(VertexLayout layout);
static bool IsTypeSupported(ComponentType type);
template<typename... Args> static VertexDeclarationRef New(Args&&... args);

View File

@@ -67,7 +67,7 @@ namespace Nz
return GetComponentByType<T>(vertexComponent, componentIndex) != nullptr;
}
const VertexDeclarationRef& VertexDeclaration::Get(VertexLayout layout)
inline const VertexDeclarationRef& VertexDeclaration::Get(VertexLayout layout)
{
NazaraAssert(layout <= VertexLayout_Max, "Vertex layout out of enum");
@@ -84,4 +84,19 @@ namespace Nz
}
}
namespace std
{
inline const Nz::VertexDeclaration::Component* begin(const Nz::VertexDeclaration& declaration)
{
assert(declaration.GetComponentCount() != 0);
return &declaration.GetComponent(0);
}
inline const Nz::VertexDeclaration::Component* end(const Nz::VertexDeclaration& declaration)
{
assert(declaration.GetComponentCount() != 0);
return (&declaration.GetComponent(declaration.GetComponentCount() - 1) + 1);
}
}
#include <Nazara/Utility/DebugOff.hpp>

View File

@@ -26,7 +26,7 @@ namespace Nz
VertexMapper(const VertexBuffer* vertexBuffer, BufferAccess access = BufferAccess_ReadOnly);
~VertexMapper();
template<typename T> SparsePtr<T> GetComponentPtr(VertexComponent component);
template<typename T> SparsePtr<T> GetComponentPtr(VertexComponent component, std::size_t componentIndex = 0);
inline const VertexBuffer* GetVertexBuffer() const;
inline std::size_t GetVertexCount() const;

View File

@@ -10,19 +10,13 @@
namespace Nz
{
template <typename T>
SparsePtr<T> VertexMapper::GetComponentPtr(VertexComponent component)
SparsePtr<T> VertexMapper::GetComponentPtr(VertexComponent component, std::size_t componentIndex)
{
// On récupère la déclaration depuis le buffer
const VertexDeclaration* declaration = m_mapper.GetBuffer()->GetVertexDeclaration();
// Ensuite le composant qui nous intéresse
bool enabled;
ComponentType type;
std::size_t offset;
declaration->GetComponent(component, &enabled, &type, &offset);
if (enabled && GetComponentTypeOf<T>() == type)
return SparsePtr<T>(static_cast<UInt8*>(m_mapper.GetPointer()) + offset, declaration->GetStride());
if (const auto* componentData = declaration->GetComponentByType<T>(component, componentIndex))
return SparsePtr<T>(static_cast<UInt8*>(m_mapper.GetPointer()) + componentData->offset, declaration->GetStride());
else
return SparsePtr<T>();
}

View File

@@ -15,12 +15,15 @@
namespace Nz
{
inline VkFormat ToVulkan(ComponentType componentType);
inline VkCullModeFlagBits ToVulkan(FaceSide faceSide);
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);
inline VkVertexInputRate ToVulkan(VertexInputRate inputRate);
NAZARA_VULKANRENDERER_API String TranslateVulkanError(VkResult code);
}

View File

@@ -3,12 +3,37 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/VulkanRenderer/Utils.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
{
VkFormat ToVulkan(ComponentType componentType)
{
switch (componentType)
{
case ComponentType_Color: return VK_FORMAT_R8G8B8A8_UINT;
case ComponentType_Double1: return VK_FORMAT_R64_SFLOAT;
case ComponentType_Double2: return VK_FORMAT_R64G64_SFLOAT;
case ComponentType_Double3: return VK_FORMAT_R64G64B64_SFLOAT;
case ComponentType_Double4: return VK_FORMAT_R64G64B64A64_SFLOAT;
case ComponentType_Float1: return VK_FORMAT_R32_SFLOAT;
case ComponentType_Float2: return VK_FORMAT_R32G32_SFLOAT;
case ComponentType_Float3: return VK_FORMAT_R32G32B32_SFLOAT;
case ComponentType_Float4: return VK_FORMAT_R32G32B32A32_SFLOAT;
case ComponentType_Int1: return VK_FORMAT_R32_SINT;
case ComponentType_Int2: return VK_FORMAT_R32G32_SINT;
case ComponentType_Int3: return VK_FORMAT_R32G32B32_SINT;
case ComponentType_Int4: return VK_FORMAT_R32G32B32A32_SINT;
case ComponentType_Quaternion: return VK_FORMAT_R32G32B32A32_SFLOAT;
}
NazaraError("Unhandled ComponentType 0x" + String::Number(componentType, 16));
return VK_FORMAT_UNDEFINED;
}
VkCullModeFlagBits ToVulkan(FaceSide faceSide)
{
switch (faceSide)
@@ -99,6 +124,18 @@ namespace Nz
NazaraError("Unhandled RendererComparison 0x" + String::Number(stencilOp, 16));
return VK_STENCIL_OP_KEEP;
}
VkVertexInputRate ToVulkan(VertexInputRate inputRate)
{
switch (inputRate)
{
case VertexInputRate::Instance: return VK_VERTEX_INPUT_RATE_INSTANCE;
case VertexInputRate::Vertex: return VK_VERTEX_INPUT_RATE_VERTEX;
}
NazaraError("Unhandled VertexInputRate 0x" + String::Number(UnderlyingCast(inputRate), 16));
return VK_VERTEX_INPUT_RATE_VERTEX;
}
}
#include <Nazara/VulkanRenderer/DebugOff.hpp>

View File

@@ -29,10 +29,14 @@ namespace Nz
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 VkPipelineMultisampleStateCreateInfo BuildMultisampleInfo(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<VkPipelineShaderStageCreateInfo> BuildShaderStageInfo(const RenderPipelineInfo& pipelineInfo);
static std::vector<VkVertexInputAttributeDescription> BuildVertexAttributeDescription(const RenderPipelineInfo& pipelineInfo);
static std::vector<VkVertexInputBindingDescription> BuildVertexBindingDescription(const RenderPipelineInfo& pipelineInfo);
static VkPipelineVertexInputStateCreateInfo BuildVertexInputInfo(const RenderPipelineInfo& pipelineInfo, const std::vector<VkVertexInputAttributeDescription>& vertexAttributes, const std::vector<VkVertexInputBindingDescription>& bindingDescription);
static CreateInfo BuildCreateInfo(const RenderPipelineInfo& pipelineInfo);
@@ -40,18 +44,21 @@ namespace Nz
{
struct StateData
{
VkPipelineVertexInputStateCreateInfo vertexInputState;
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState;
VkPipelineViewportStateCreateInfo viewportState;
VkPipelineRasterizationStateCreateInfo rasterizationState;
VkPipelineDepthStencilStateCreateInfo depthStencilState;
VkPipelineColorBlendStateCreateInfo colorBlendState;
VkPipelineDepthStencilStateCreateInfo depthStencilState;
VkPipelineDynamicStateCreateInfo dynamicState;
VkPipelineMultisampleStateCreateInfo multiSampleState;
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState;
VkPipelineRasterizationStateCreateInfo rasterizationState;
VkPipelineVertexInputStateCreateInfo vertexInputState;
VkPipelineViewportStateCreateInfo viewportState;
};
std::vector<VkPipelineColorBlendAttachmentState> colorBlendAttachmentState;
std::vector<VkDynamicState> dynamicStates;
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
std::vector<VkVertexInputAttributeDescription> vertexAttributesDescription;
std::vector<VkVertexInputBindingDescription> vertexBindingDescription;
std::unique_ptr<StateData> stateData;
VkGraphicsPipelineCreateInfo pipelineInfo;
};