diff --git a/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp b/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp index 273cd1b..56e6efe 100644 --- a/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp +++ b/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp @@ -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++) diff --git a/openVulkanoCpp/Scene/Camera.hpp b/openVulkanoCpp/Scene/Camera.hpp index 406b27a..1150219 100644 --- a/openVulkanoCpp/Scene/Camera.hpp +++ b/openVulkanoCpp/Scene/Camera.hpp @@ -9,6 +9,7 @@ #include "Node.hpp" #include "Math/Math.hpp" #include "Math/Frustum.hpp" +#include namespace openVulkanoCpp::Scene { diff --git a/openVulkanoCpp/Scene/Material.hpp b/openVulkanoCpp/Scene/Material.hpp index f24cd9e..adc4824 100644 --- a/openVulkanoCpp/Scene/Material.hpp +++ b/openVulkanoCpp/Scene/Material.hpp @@ -6,7 +6,7 @@ #pragma once -#include "Shader.hpp" +#include "Shader/Shader.hpp" #include "Texture.hpp" namespace openVulkanoCpp::Scene diff --git a/openVulkanoCpp/Scene/Prefabs/GridDrawable.hpp b/openVulkanoCpp/Scene/Prefabs/GridDrawable.hpp index 615f83d..595db76 100644 --- a/openVulkanoCpp/Scene/Prefabs/GridDrawable.hpp +++ b/openVulkanoCpp/Scene/Prefabs/GridDrawable.hpp @@ -7,7 +7,7 @@ #pragma once #include "Scene/Drawable.hpp" -#include "Scene/Shader.hpp" +#include "Scene/Shader/Shader.hpp" namespace openVulkanoCpp::Scene { diff --git a/openVulkanoCpp/Scene/Shader/DescriptorInputDescription.hpp b/openVulkanoCpp/Scene/Shader/DescriptorInputDescription.hpp new file mode 100644 index 0000000..9564fdb --- /dev/null +++ b/openVulkanoCpp/Scene/Shader/DescriptorInputDescription.hpp @@ -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) + {} + }; +} \ No newline at end of file diff --git a/openVulkanoCpp/Scene/Shader.hpp b/openVulkanoCpp/Scene/Shader/Shader.hpp similarity index 66% rename from openVulkanoCpp/Scene/Shader.hpp rename to openVulkanoCpp/Scene/Shader/Shader.hpp index 10d4d9e..3583c74 100644 --- a/openVulkanoCpp/Scene/Shader.hpp +++ b/openVulkanoCpp/Scene/Shader/Shader.hpp @@ -9,6 +9,8 @@ #include "Base/ICloseable.hpp" #include "Base/Utils.hpp" #include "VertexInputDescription.hpp" +#include "ShaderProgramType.hpp" +#include "DescriptorInputDescription.hpp" #include #include @@ -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(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 shaderPrograms{}; std::vector vertexInputDescriptions{}; + std::vector 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 Shader& AddShaderProgram(ARGS&&... args) { - if (renderShader) throw std::runtime_error("Shader already initialized!"); + CheckShaderInitState(); shaderPrograms.emplace_back(std::forward(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(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(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!"); + } }; } diff --git a/openVulkanoCpp/Scene/Shader/ShaderProgramType.hpp b/openVulkanoCpp/Scene/Shader/ShaderProgramType.hpp new file mode 100644 index 0000000..eb1c461 --- /dev/null +++ b/openVulkanoCpp/Scene/Shader/ShaderProgramType.hpp @@ -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 +#include + +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(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; + }; +} \ No newline at end of file diff --git a/openVulkanoCpp/Scene/VertexInputDescription.hpp b/openVulkanoCpp/Scene/Shader/VertexInputDescription.hpp similarity index 98% rename from openVulkanoCpp/Scene/VertexInputDescription.hpp rename to openVulkanoCpp/Scene/Shader/VertexInputDescription.hpp index 74111c2..960b15b 100644 --- a/openVulkanoCpp/Scene/VertexInputDescription.hpp +++ b/openVulkanoCpp/Scene/Shader/VertexInputDescription.hpp @@ -6,7 +6,7 @@ #pragma once -#include "DataFormat.hpp" +#include "../DataFormat.hpp" #include #include diff --git a/openVulkanoCpp/Scene/Vertex.hpp b/openVulkanoCpp/Scene/Vertex.hpp index 9637a3a..e7ddd5d 100644 --- a/openVulkanoCpp/Scene/Vertex.hpp +++ b/openVulkanoCpp/Scene/Vertex.hpp @@ -7,7 +7,7 @@ #pragma once #include "Math/Math.hpp" -#include "VertexInputDescription.hpp" +#include "Shader/VertexInputDescription.hpp" #if __has_include("assimp/vector2.h") #include #include diff --git a/openVulkanoCpp/Vulkan/Pipeline.hpp b/openVulkanoCpp/Vulkan/Pipeline.hpp index 1df19da..77f3ea3 100644 --- a/openVulkanoCpp/Vulkan/Pipeline.hpp +++ b/openVulkanoCpp/Vulkan/Pipeline.hpp @@ -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 camPushConstantDescs = { camPushConstantDesc }; vk::DescriptorSetLayoutBinding nodeLayoutBinding = { 0, vk::DescriptorType::eUniformBufferDynamic, 1, vk::ShaderStageFlagBits::eVertex }; //vk::DescriptorSetLayoutBinding textureLayoutBinding = { 0, vk::DescriptorType::image }; diff --git a/openVulkanoCpp/Vulkan/Renderer.cpp b/openVulkanoCpp/Vulkan/Renderer.cpp index c7f3627..c705be2 100644 --- a/openVulkanoCpp/Vulkan/Renderer.cpp +++ b/openVulkanoCpp/Vulkan/Renderer.cpp @@ -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" diff --git a/openVulkanoCpp/Vulkan/Scene/VulkanShader.cpp b/openVulkanoCpp/Vulkan/Scene/VulkanShader.cpp index 306d3cc..012cf32 100644 --- a/openVulkanoCpp/Vulkan/Scene/VulkanShader.cpp +++ b/openVulkanoCpp/Vulkan/Scene/VulkanShader.cpp @@ -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 diff --git a/openVulkanoCpp/Vulkan/VulkanDrawContext.cpp b/openVulkanoCpp/Vulkan/VulkanDrawContext.cpp index a1e5fd4..56f4ce5 100644 --- a/openVulkanoCpp/Vulkan/VulkanDrawContext.cpp +++ b/openVulkanoCpp/Vulkan/VulkanDrawContext.cpp @@ -5,7 +5,7 @@ */ #include "VulkanDrawContext.hpp" -#include "Scene/Shader.hpp" +#include "Scene/Shader/Shader.hpp" #include "Scene/VulkanShader.hpp" namespace openVulkanoCpp::Vulkan