[WIP] Shader handling rework

This commit is contained in:
2023-08-29 23:08:11 +02:00
parent 5735b93870
commit 5aec41ead4
13 changed files with 181 additions and 79 deletions

View File

@@ -6,7 +6,7 @@
#include "CubesExampleApp.hpp"
#include "Scene/Scene.hpp"
#include "Scene/Shader.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Scene/Geometry.hpp"
#include "Scene/Material.hpp"
#include "Scene/Vertex.hpp"
@@ -53,8 +53,8 @@ public:
cam.Init(70, 16, 9, 0.1f, 100);
scene.SetCamera(&cam);
cam.SetMatrix(Utils::translate(Matrix4f(1), Vector3f_SIMD(0,0,-10)));
shader.AddShaderProgram(ShaderProgramType::VERTEX, "Shader/basic");
shader.AddShaderProgram(ShaderProgramType::FRAGMENT, "Shader/basic");
shader.AddShaderProgram(openVulkanoCpp::ShaderProgramType::VERTEX, "Shader/basic");
shader.AddShaderProgram(openVulkanoCpp::ShaderProgramType::FRAGMENT, "Shader/basic");
shader.AddVertexInputDescription(openVulkanoCpp::Vertex::GetVertexInputDescription());
drawablesPool.resize(GEOS);
for(int i = 0; i < GEOS; i++)

View File

@@ -9,6 +9,7 @@
#include "Node.hpp"
#include "Math/Math.hpp"
#include "Math/Frustum.hpp"
#include <array>
namespace openVulkanoCpp::Scene
{

View File

@@ -6,7 +6,7 @@
#pragma once
#include "Shader.hpp"
#include "Shader/Shader.hpp"
#include "Texture.hpp"
namespace openVulkanoCpp::Scene

View File

@@ -7,7 +7,7 @@
#pragma once
#include "Scene/Drawable.hpp"
#include "Scene/Shader.hpp"
#include "Scene/Shader/Shader.hpp"
namespace openVulkanoCpp::Scene
{

View File

@@ -0,0 +1,57 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "ShaderProgramType.hpp"
namespace openVulkanoCpp
{
struct DescriptorSetLayoutBinding
{
enum Type : uint32_t {
TYPE_SAMPLER = 0,
TYPE_COMBINED_IMAGE_SAMPLER = 1,
TYPE_SAMPLED_IMAGE = 2,
TYPE_STORAGE_IMAGE = 3,
TYPE_UNIFORM_TEXEL_BUFFER = 4,
TYPE_STORAGE_TEXEL_BUFFER = 5,
TYPE_UNIFORM_BUFFER = 6,
TYPE_STORAGE_BUFFER = 7,
TYPE_UNIFORM_BUFFER_DYNAMIC = 8,
TYPE_STORAGE_BUFFER_DYNAMIC = 9,
TYPE_INPUT_ATTACHMENT = 10,
TYPE_INLINE_UNIFORM_BLOCK = 1000138000,
TYPE_ACCELERATION_STRUCTURE_KHR = 1000150000,
TYPE_ACCELERATION_STRUCTURE_NV = 1000165000,
TYPE_SAMPLE_WEIGHT_IMAGE_QCOM = 1000440000,
TYPE_BLOCK_MATCH_IMAGE_QCOM = 1000440001,
TYPE_MUTABLE_VALVE = 1000351000
};
uint32_t bindingId;
Type descriptorType;
uint32_t descriptorCount;
ShaderProgramType::Type stageFlags;
void* immutableSamplers;
DescriptorSetLayoutBinding()
: DescriptorSetLayoutBinding(Type::TYPE_UNIFORM_BUFFER_DYNAMIC, 1, ShaderProgramType::Type::VERTEX)
{}
DescriptorSetLayoutBinding(Type descriptorType, uint32_t descriptorCount,
ShaderProgramType::Type stageFlags, void* immutableSamplers = nullptr)
: bindingId(0), descriptorType(descriptorType)
, descriptorCount(descriptorCount), stageFlags(stageFlags)
, immutableSamplers(immutableSamplers)
{}
DescriptorSetLayoutBinding(uint32_t id, const DescriptorSetLayoutBinding& layout)
: bindingId(id), descriptorType(layout.descriptorType), descriptorCount(layout.descriptorCount)
, stageFlags(layout.stageFlags), immutableSamplers(layout.immutableSamplers)
{}
};
}

View File

@@ -9,6 +9,8 @@
#include "Base/ICloseable.hpp"
#include "Base/Utils.hpp"
#include "VertexInputDescription.hpp"
#include "ShaderProgramType.hpp"
#include "DescriptorInputDescription.hpp"
#include <string>
#include <stdexcept>
@@ -37,69 +39,6 @@ namespace openVulkanoCpp::Scene
PATCH_LIST
};
class ShaderProgramType
{
static constexpr std::string_view FILE_EXTENSIONS_OPENGL[] = {
".vert", ".tesc", ".tese", ".geom", ".frag", ".comp", ".mesh", ".task",
".rgen", ".rahit", ".rchit", ".rmiss", ".rint", ".rcall", ".glsl"
};
public:
enum Type : uint32_t {
VERTEX = 1,
TESSELLATION_CONTROL = 2,
TESSELLATION_EVALUATION = 4,
GEOMETRY = 8,
FRAGMENT = 16,
COMPUTE = 32,
// Mesh shader
MESH_TASK = 0x40,
MESH_MESH = 0x80,
// Raytracing
RAY_GEN = 0x100,
RAY_ANY_HIT = 0x200,
RAY_CLOSEST_HIT = 0x400,
RAY_MISS = 0x800,
RAY_INTERSECTION = 0x1000,
RAY_CALLABLE = 0x2000,
ALL_GRAPHICS = 0x0000001F
};
ShaderProgramType(Type type) : m_type(type) {}
[[nodiscard]] constexpr std::string_view GetExtensionOpenGL() const
{
return FILE_EXTENSIONS_OPENGL[GetId()];
}
[[nodiscard]] constexpr uint32_t GetId() const
{
if (m_type == ALL_GRAPHICS) return 14;
return Utils::Log2OfPow2(static_cast<uint32_t>(m_type));
}
[[nodiscard]] bool operator ==(Type rhs) const
{
return m_type == rhs;
}
[[nodiscard]] bool operator !=(Type rhs) const
{
return m_type != rhs;
}
[[nodiscard]] operator uint32_t() const
{
return m_type;
}
private:
Type m_type;
};
struct ShaderProgram
{
ShaderProgramType type;
@@ -125,11 +64,14 @@ namespace openVulkanoCpp::Scene
}
};
class Shader final : public ICloseable
{
public:
std::vector<ShaderProgram> shaderPrograms{};
std::vector<VertexInputDescription> vertexInputDescriptions{};
std::vector<DescriptorSetLayoutBinding> descriptorSetLayoutBindings{};
Topology topology = Topology::TRIANGLE_LIST;
CullMode cullMode = CullMode::BACK;
ICloseable* renderShader = nullptr;
@@ -140,14 +82,14 @@ namespace openVulkanoCpp::Scene
Shader& AddShaderProgram(const ShaderProgram& shaderProgram)
{
if (renderShader) throw std::runtime_error("Shader already initialized!");
CheckShaderInitState();
shaderPrograms.push_back(shaderProgram);
return *this;
}
Shader& AddShaderProgram(ShaderProgram&& shaderProgram)
{
if (renderShader) throw std::runtime_error("Shader already initialized!");
CheckShaderInitState();
shaderPrograms.push_back(std::move(shaderProgram));
return *this;
}
@@ -155,16 +97,16 @@ namespace openVulkanoCpp::Scene
template<typename ...ARGS>
Shader& AddShaderProgram(ARGS&&... args)
{
if (renderShader) throw std::runtime_error("Shader already initialized!");
CheckShaderInitState();
shaderPrograms.emplace_back(std::forward<ARGS>(args)...);
return *this;
}
Shader& AddVertexInputDescription(const VertexInputDescription& inputDescription, int bindingId = -1)
{
if (renderShader) throw std::runtime_error("Shader already initialized!");
CheckShaderInitState();
if (bindingId < 0) bindingId = static_cast<int>(vertexInputDescriptions.size());
if (bindingId > vertexInputDescriptions.size())
while (bindingId > vertexInputDescriptions.size())
{
vertexInputDescriptions.emplace_back(0, 0);
}
@@ -179,10 +121,36 @@ namespace openVulkanoCpp::Scene
return *this;
}
Shader& AddDescriptorSetLayoutBinding(const DescriptorSetLayoutBinding& binding, int bindingId = -1)
{
CheckShaderInitState();
if (bindingId < 0) bindingId = static_cast<int>(descriptorSetLayoutBindings.size());
while (bindingId > descriptorSetLayoutBindings.size())
{
descriptorSetLayoutBindings.emplace_back().bindingId = descriptorSetLayoutBindings.size() - 1;
}
if (bindingId == vertexInputDescriptions.size())
{
descriptorSetLayoutBindings.emplace_back(bindingId, binding);
}
else
{
descriptorSetLayoutBindings[bindingId] = binding;
descriptorSetLayoutBindings[bindingId].bindingId = bindingId;
}
return *this;
}
void Close() override
{
renderShader->Close();
renderShader = nullptr;
}
private:
void CheckShaderInitState() const
{
if (renderShader) throw std::runtime_error("Shader already initialized!");
}
};
}

View File

@@ -0,0 +1,76 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <string_view>
#include <cinttypes>
namespace openVulkanoCpp
{
class ShaderProgramType
{
static constexpr std::string_view FILE_EXTENSIONS_OPENGL[] = {
".vert", ".tesc", ".tese", ".geom", ".frag", ".comp", ".mesh", ".task",
".rgen", ".rahit", ".rchit", ".rmiss", ".rint", ".rcall", ".glsl"
};
public:
enum Type : uint32_t {
VERTEX = 1,
TESSELLATION_CONTROL = 2,
TESSELLATION_EVALUATION = 4,
GEOMETRY = 8,
FRAGMENT = 16,
COMPUTE = 32,
// Mesh shader
MESH_TASK = 0x40,
MESH_MESH = 0x80,
// Raytracing
RAY_GEN = 0x100,
RAY_ANY_HIT = 0x200,
RAY_CLOSEST_HIT = 0x400,
RAY_MISS = 0x800,
RAY_INTERSECTION = 0x1000,
RAY_CALLABLE = 0x2000,
ALL_GRAPHICS = 0x0000001F
};
ShaderProgramType(Type type) : m_type(type) {}
[[nodiscard]] constexpr std::string_view GetExtensionOpenGL() const
{
return FILE_EXTENSIONS_OPENGL[GetId()];
}
[[nodiscard]] constexpr uint32_t GetId() const
{
if (m_type == ALL_GRAPHICS) return 14;
return Utils::Log2OfPow2(static_cast<uint32_t>(m_type));
}
[[nodiscard]] bool operator ==(Type rhs) const
{
return m_type == rhs;
}
[[nodiscard]] bool operator !=(Type rhs) const
{
return m_type != rhs;
}
[[nodiscard]] operator uint32_t() const
{
return m_type;
}
private:
Type m_type;
};
}

View File

@@ -6,7 +6,7 @@
#pragma once
#include "DataFormat.hpp"
#include "../DataFormat.hpp"
#include <stdexcept>
#include <vector>

View File

@@ -7,7 +7,7 @@
#pragma once
#include "Math/Math.hpp"
#include "VertexInputDescription.hpp"
#include "Shader/VertexInputDescription.hpp"
#if __has_include("assimp/vector2.h")
#include <assimp/vector2.h>
#include <assimp/vector3.h>

View File

@@ -34,7 +34,7 @@ namespace openVulkanoCpp::Vulkan
private:
void CreatePipelineLayout()
{
vk::PushConstantRange camPushConstantDesc = { vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, 0, 3 * sizeof(Math::Matrix4f) + sizeof(Math::Vector4f) + 4 * sizeof(float) };
vk::PushConstantRange camPushConstantDesc = { vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, 0, 3 * sizeof(Math::Matrix4f) + sizeof(Math::Vector4f) + 8 * sizeof(float) };
std::array<vk::PushConstantRange, 1> camPushConstantDescs = { camPushConstantDesc };
vk::DescriptorSetLayoutBinding nodeLayoutBinding = { 0, vk::DescriptorType::eUniformBufferDynamic, 1, vk::ShaderStageFlagBits::eVertex };
//vk::DescriptorSetLayoutBinding textureLayoutBinding = { 0, vk::DescriptorType::image };

View File

@@ -6,7 +6,7 @@
#include "Renderer.hpp"
#include "VulkanDrawContext.hpp"
#include "Scene/Shader.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Scene/Geometry.hpp"
#include "Scene/VulkanGeometry.hpp"
#include "Scene/VulkanNode.hpp"

View File

@@ -6,7 +6,7 @@
#include "VulkanShader.hpp"
#include "Vulkan/Context.hpp"
#include "Scene/Shader.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Vulkan/Resources/IShaderOwner.hpp"
namespace openVulkanoCpp::Vulkan

View File

@@ -5,7 +5,7 @@
*/
#include "VulkanDrawContext.hpp"
#include "Scene/Shader.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Scene/VulkanShader.hpp"
namespace openVulkanoCpp::Vulkan