diff --git a/engine/platform/Vulkan/VKdescriptor.h b/engine/platform/Vulkan/VKdescriptor.h index b1ee2301..914ec9c9 100644 --- a/engine/platform/Vulkan/VKdescriptor.h +++ b/engine/platform/Vulkan/VKdescriptor.h @@ -46,6 +46,7 @@ namespace GfxRenderEngine Builder& AddBinding(uint binding, VkDescriptorType descriptorType, VkShaderStageFlags stageFlags, uint count = 1); + size_t Size() const { return m_Bindings.size(); } std::unique_ptr Build() const; private: diff --git a/engine/platform/Vulkan/VKmaterialDescriptor.cpp b/engine/platform/Vulkan/VKmaterialDescriptor.cpp index 7a37fe27..4ec9404d 100644 --- a/engine/platform/Vulkan/VKmaterialDescriptor.cpp +++ b/engine/platform/Vulkan/VKmaterialDescriptor.cpp @@ -25,19 +25,18 @@ #include "VKrenderer.h" #include "VKtexture.h" #include "VKcubemap.h" -#include "VKbuffer.h" namespace GfxRenderEngine { extern std::shared_ptr gTextureSpritesheet; - VK_MaterialDescriptor::VK_MaterialDescriptor(MaterialDescriptor::MaterialTypes materialType, - Material::MaterialTextures& textures, Material::MaterialBuffers& buffers) + VK_MaterialDescriptor::VK_MaterialDescriptor(MaterialDescriptor::MaterialType materialType, + Material::MaterialTextures& textures) : m_MaterialType{materialType} { switch (materialType) { - case MaterialDescriptor::MaterialTypes::MtPbr: + case MaterialDescriptor::MaterialType::MtPbr: { // textures std::shared_ptr diffuseMap; @@ -57,20 +56,6 @@ namespace GfxRenderEngine roughnessMap = textures[Material::ROUGHNESS_MAP_INDEX] ? textures[Material::ROUGHNESS_MAP_INDEX] : dummy; metallicMap = textures[Material::METALLIC_MAP_INDEX] ? textures[Material::METALLIC_MAP_INDEX] : dummy; - // buffers - std::shared_ptr& instanceUbo = buffers[Material::INSTANCE_BUFFER_INDEX]; - auto instanceBuffer = static_cast(instanceUbo.get()); - VkDescriptorBufferInfo instanceBufferInfo = instanceBuffer->DescriptorInfo(); - - std::shared_ptr& skeletalAnimationUbo = buffers[Material::SKELETAL_ANIMATION_BUFFER_INDEX]; - VK_Buffer* skeletalAnimationBuffer = nullptr; - VkDescriptorBufferInfo skeletalAnimationBufferInfo; - if (skeletalAnimationUbo) - { - skeletalAnimationBuffer = static_cast(skeletalAnimationUbo.get()); - skeletalAnimationBufferInfo = skeletalAnimationBuffer->DescriptorInfo(); - } - { VK_DescriptorSetLayout::Builder builder{}; builder.AddBinding(0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT) @@ -78,12 +63,7 @@ namespace GfxRenderEngine .AddBinding(2, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT) .AddBinding(3, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT) .AddBinding(4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT) - .AddBinding(5, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT) - .AddBinding(6, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT); - if (skeletalAnimationUbo) - { - builder.AddBinding(7, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT); - } + .AddBinding(5, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT); std::unique_ptr localDescriptorSetLayout = builder.Build(); auto& imageInfo0 = static_cast(diffuseMap.get())->GetDescriptorImageInfo(); @@ -99,32 +79,9 @@ namespace GfxRenderEngine .WriteImage(2, imageInfo2) .WriteImage(3, imageInfo3) .WriteImage(4, imageInfo4) - .WriteImage(5, imageInfo5) - .WriteBuffer(6, instanceBufferInfo); - if (skeletalAnimationBuffer) - { - descriptorWriter.WriteBuffer(7, skeletalAnimationBufferInfo); - } + .WriteImage(5, imageInfo5); descriptorWriter.Build(m_DescriptorSet); } - - { - VK_DescriptorSetLayout::Builder builder{}; - builder.AddBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT); - if (skeletalAnimationUbo) - { - builder.AddBinding(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT); - } - std::unique_ptr localDescriptorSetLayout = builder.Build(); - - VK_DescriptorWriter descriptorWriter(*localDescriptorSetLayout, *VK_Renderer::m_DescriptorPool); - descriptorWriter.WriteBuffer(0, instanceBufferInfo); - if (skeletalAnimationBuffer) - { - descriptorWriter.WriteBuffer(1, skeletalAnimationBufferInfo); - } - descriptorWriter.Build(m_ShadowDescriptorSet); - } break; } default: @@ -135,13 +92,13 @@ namespace GfxRenderEngine } } - VK_MaterialDescriptor::VK_MaterialDescriptor(MaterialDescriptor::MaterialTypes materialType, + VK_MaterialDescriptor::VK_MaterialDescriptor(MaterialDescriptor::MaterialType materialType, std::shared_ptr const& cubemap) : m_MaterialType{materialType} { switch (materialType) { - case MaterialDescriptor::MaterialTypes::MtCubemap: + case MaterialDescriptor::MaterialType::MtCubemap: { std::unique_ptr localDescriptorSetLayout = VK_DescriptorSetLayout::Builder() @@ -170,11 +127,21 @@ namespace GfxRenderEngine m_ShadowDescriptorSet = other.m_ShadowDescriptorSet; } + VK_MaterialDescriptor::VK_MaterialDescriptor(std::shared_ptr const& materialDescriptor) + { + if (materialDescriptor) + { + VK_MaterialDescriptor* other = static_cast(materialDescriptor.get()); + m_MaterialType = other->m_MaterialType; + m_DescriptorSet = other->m_DescriptorSet; + m_ShadowDescriptorSet = other->m_ShadowDescriptorSet; + } + } + VK_MaterialDescriptor::~VK_MaterialDescriptor() {} - MaterialDescriptor::MaterialTypes VK_MaterialDescriptor::GetMaterialType() const { return m_MaterialType; } + MaterialDescriptor::MaterialType VK_MaterialDescriptor::GetMaterialType() const { return m_MaterialType; } const VkDescriptorSet& VK_MaterialDescriptor::GetDescriptorSet() const { return m_DescriptorSet; } - const VkDescriptorSet& VK_MaterialDescriptor::GetShadowDescriptorSet() const { return m_ShadowDescriptorSet; } } // namespace GfxRenderEngine diff --git a/engine/platform/Vulkan/VKmaterialDescriptor.h b/engine/platform/Vulkan/VKmaterialDescriptor.h index 32e83788..cbd41c46 100644 --- a/engine/platform/Vulkan/VKmaterialDescriptor.h +++ b/engine/platform/Vulkan/VKmaterialDescriptor.h @@ -32,21 +32,20 @@ namespace GfxRenderEngine { public: - VK_MaterialDescriptor(MaterialDescriptor::MaterialTypes materialType, Material::MaterialTextures& textures, - Material::MaterialBuffers& buffers); - VK_MaterialDescriptor(MaterialDescriptor::MaterialTypes materialType, std::shared_ptr const& cubemap); + VK_MaterialDescriptor(MaterialDescriptor::MaterialType materialType, Material::MaterialTextures& textures); + VK_MaterialDescriptor(MaterialDescriptor::MaterialType materialType, std::shared_ptr const& cubemap); VK_MaterialDescriptor(VK_MaterialDescriptor const& other); + VK_MaterialDescriptor(std::shared_ptr const& materialDescriptor); virtual ~VK_MaterialDescriptor(); public: - virtual MaterialDescriptor::MaterialTypes GetMaterialType() const override; + virtual MaterialDescriptor::MaterialType GetMaterialType() const override; const VkDescriptorSet& GetDescriptorSet() const; - const VkDescriptorSet& GetShadowDescriptorSet() const; private: - MaterialDescriptor::MaterialTypes m_MaterialType; + MaterialDescriptor::MaterialType m_MaterialType; VkDescriptorSet m_DescriptorSet; VkDescriptorSet m_ShadowDescriptorSet; }; diff --git a/engine/platform/Vulkan/VKmodel.cpp b/engine/platform/Vulkan/VKmodel.cpp index ca81c937..11b1b8e5 100644 --- a/engine/platform/Vulkan/VKmodel.cpp +++ b/engine/platform/Vulkan/VKmodel.cpp @@ -103,9 +103,10 @@ namespace GfxRenderEngine VK_Model::~VK_Model() {} VK_Submesh::VK_Submesh(Submesh const& submesh) - : Submesh{submesh.m_FirstIndex, submesh.m_FirstVertex, submesh.m_IndexCount, - submesh.m_VertexCount, submesh.m_InstanceCount, submesh.m_Material}, - m_MaterialDescriptor(*(static_cast(submesh.m_Material.m_MaterialDescriptor.get()))) + : Submesh{submesh.m_FirstIndex, submesh.m_FirstVertex, submesh.m_IndexCount, submesh.m_VertexCount, + submesh.m_InstanceCount, submesh.m_Material, submesh.m_Resources}, + m_MaterialDescriptor(submesh.m_Material.m_MaterialDescriptor), + m_ResourceDescriptor(submesh.m_Resources.m_ResourceDescriptor) { } @@ -115,16 +116,16 @@ namespace GfxRenderEngine { VK_Submesh vkSubmesh(submesh); - MaterialDescriptor::MaterialTypes materialType = vkSubmesh.m_MaterialDescriptor.GetMaterialType(); + MaterialDescriptor::MaterialType materialType = vkSubmesh.m_MaterialDescriptor.GetMaterialType(); switch (materialType) { - case MaterialDescriptor::MaterialTypes::MtPbr: + case MaterialDescriptor::MaterialType::MtPbr: { m_SubmeshesPbrMap.push_back(vkSubmesh); break; } - case MaterialDescriptor::MaterialTypes::MtCubemap: + case MaterialDescriptor::MaterialType::MtCubemap: { m_SubmeshesCubemap.push_back(vkSubmesh); break; @@ -189,13 +190,31 @@ namespace GfxRenderEngine void VK_Model::BindDescriptors(const VK_FrameInfo& frameInfo, const VkPipelineLayout& pipelineLayout, VK_Submesh const& submesh) { - auto& localDescriptorSet = submesh.m_MaterialDescriptor.GetDescriptorSet(); - std::vector descriptorSets = {frameInfo.m_GlobalDescriptorSet, localDescriptorSet}; + const VkDescriptorSet& materialDescriptorSet = submesh.m_MaterialDescriptor.GetDescriptorSet(); + std::vector descriptorSets = {frameInfo.m_GlobalDescriptorSet, materialDescriptorSet}; vkCmdBindDescriptorSets(frameInfo.m_CommandBuffer, // VkCommandBuffer commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, // VkPipelineBindPoint pipelineBindPoint, pipelineLayout, // VkPipelineLayout layout, 0, // uint32_t firstSet, - 2, // uint32_t descriptorSetCount, + descriptorSets.size(), // uint32_t descriptorSetCount, + descriptorSets.data(), // const VkDescriptorSet* pDescriptorSets, + 0, // uint32_t dynamicOffsetCount, + nullptr // const uint32_t* pDynamicOffsets); + ); + } + + void VK_Model::BindDescriptors(const VK_FrameInfo& frameInfo, const VkPipelineLayout& pipelineLayout, + VK_Submesh const& submesh, bool bindResources) + { + const VkDescriptorSet& materialDescriptorSet = submesh.m_MaterialDescriptor.GetDescriptorSet(); + const VkDescriptorSet& resourceDescriptorSet = submesh.m_ResourceDescriptor.GetDescriptorSet(); + std::vector descriptorSets = {frameInfo.m_GlobalDescriptorSet, materialDescriptorSet, + resourceDescriptorSet}; + vkCmdBindDescriptorSets(frameInfo.m_CommandBuffer, // VkCommandBuffer commandBuffer, + VK_PIPELINE_BIND_POINT_GRAPHICS, // VkPipelineBindPoint pipelineBindPoint, + pipelineLayout, // VkPipelineLayout layout, + 0, // uint32_t firstSet, + descriptorSets.size(), // uint32_t descriptorSetCount, descriptorSets.data(), // const VkDescriptorSet* pDescriptorSets, 0, // uint32_t dynamicOffsetCount, nullptr // const uint32_t* pDynamicOffsets); @@ -260,7 +279,7 @@ namespace GfxRenderEngine { for (auto& submesh : m_SubmeshesPbrMap) { - BindDescriptors(frameInfo, pipelineLayout, submesh); + BindDescriptors(frameInfo, pipelineLayout, submesh, true /*bind resources*/); PushConstantsPbr(frameInfo, pipelineLayout, submesh); DrawSubmesh(frameInfo.m_CommandBuffer, submesh); } @@ -278,7 +297,7 @@ namespace GfxRenderEngine void VK_Model::DrawShadowInstancedInternal(VK_FrameInfo const& frameInfo, VkPipelineLayout const& pipelineLayout, VK_Submesh const& submesh, VkDescriptorSet const& shadowDescriptorSet) { - VkDescriptorSet localDescriptorSet = submesh.m_MaterialDescriptor.GetShadowDescriptorSet(); + VkDescriptorSet localDescriptorSet = submesh.m_ResourceDescriptor.GetDescriptorSet(); std::vector descriptorSets = {shadowDescriptorSet, localDescriptorSet}; vkCmdBindDescriptorSets(frameInfo.m_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 2, descriptorSets.data(), 0, nullptr); diff --git a/engine/platform/Vulkan/VKmodel.h b/engine/platform/Vulkan/VKmodel.h index 3dc226d4..42e55e1f 100644 --- a/engine/platform/Vulkan/VKmodel.h +++ b/engine/platform/Vulkan/VKmodel.h @@ -43,6 +43,7 @@ #include "VKtexture.h" #include "VKcubemap.h" #include "VKmaterialDescriptor.h" +#include "VKresourceDescriptor.h" namespace GfxRenderEngine { @@ -51,6 +52,7 @@ namespace GfxRenderEngine { VK_Submesh(Submesh const& submesh); VK_MaterialDescriptor m_MaterialDescriptor; + VK_ResourceDescriptor m_ResourceDescriptor; }; class VK_Model : public Model @@ -103,6 +105,8 @@ namespace GfxRenderEngine VK_Submesh const& submesh); void BindDescriptors(const VK_FrameInfo& frameInfo, const VkPipelineLayout& pipelineLayout, VK_Submesh const& submesh); + void BindDescriptors(const VK_FrameInfo& frameInfo, const VkPipelineLayout& pipelineLayout, + VK_Submesh const& submesh, bool bindResources); void Draw(VkCommandBuffer commandBuffer); void DrawSubmesh(VkCommandBuffer commandBuffer, Submesh const& submesh); diff --git a/engine/platform/Vulkan/VKrenderer.cpp b/engine/platform/Vulkan/VKrenderer.cpp index cd563844..1df08d79 100644 --- a/engine/platform/Vulkan/VKrenderer.cpp +++ b/engine/platform/Vulkan/VKrenderer.cpp @@ -121,7 +121,7 @@ namespace GfxRenderEngine .AddBinding(0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT) // color map .Build(); - std::unique_ptr pbrDescriptorSetLayout = + std::unique_ptr pbrMaterialDescriptorSetLayout = VK_DescriptorSetLayout::Builder() .AddBinding(0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT) // diffuse color map @@ -134,26 +134,18 @@ namespace GfxRenderEngine .AddBinding(4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT) // roughness map .AddBinding(5, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - VK_SHADER_STAGE_FRAGMENT_BIT) // metallic map - .AddBinding(6, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) // shader data for instances + VK_SHADER_STAGE_FRAGMENT_BIT) // metallic map .Build(); - std::unique_ptr pbrSADescriptorSetLayout = + std::unique_ptr pbrResourceDescriptorSetLayout = VK_DescriptorSetLayout::Builder() - .AddBinding(0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - VK_SHADER_STAGE_FRAGMENT_BIT) // diffuse color map - .AddBinding(1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - VK_SHADER_STAGE_FRAGMENT_BIT) // normal map - .AddBinding(2, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - VK_SHADER_STAGE_FRAGMENT_BIT) // roughness metallic map - .AddBinding(3, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - VK_SHADER_STAGE_FRAGMENT_BIT) // emissive map - .AddBinding(4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - VK_SHADER_STAGE_FRAGMENT_BIT) // roughness map - .AddBinding(5, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - VK_SHADER_STAGE_FRAGMENT_BIT) // metallic map - .AddBinding(6, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) // shader data for instances - .AddBinding(7, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) // shader data for animation + .AddBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) // shader data for instances + .Build(); + + std::unique_ptr pbrSAResourceDescriptorSetLayout = + VK_DescriptorSetLayout::Builder() + .AddBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) // shader data for instances + .AddBinding(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) // shader data for animation .Build(); std::unique_ptr instanceDescriptorSetLayout = @@ -197,10 +189,13 @@ namespace GfxRenderEngine std::vector descriptorSetLayoutsDiffuse = { m_GlobalDescriptorSetLayout, diffuseDescriptorSetLayout->GetDescriptorSetLayout()}; - std::vector descriptorSetLayoutsPbr = {m_GlobalDescriptorSetLayout, - pbrDescriptorSetLayout->GetDescriptorSetLayout()}; - std::vector descriptorSetLayoutsPbrSA = {m_GlobalDescriptorSetLayout, - pbrSADescriptorSetLayout->GetDescriptorSetLayout()}; + std::vector descriptorSetLayoutsPbr = { + m_GlobalDescriptorSetLayout, pbrMaterialDescriptorSetLayout->GetDescriptorSetLayout(), + pbrResourceDescriptorSetLayout->GetDescriptorSetLayout()}; + + std::vector descriptorSetLayoutsPbrSA = { + m_GlobalDescriptorSetLayout, pbrMaterialDescriptorSetLayout->GetDescriptorSetLayout(), + pbrSAResourceDescriptorSetLayout->GetDescriptorSetLayout()}; std::vector descriptorSetLayoutsLighting = { m_GlobalDescriptorSetLayout, m_LightingDescriptorSetLayout->GetDescriptorSetLayout(), diff --git a/engine/platform/Vulkan/VKresourceDescriptor.cpp b/engine/platform/Vulkan/VKresourceDescriptor.cpp new file mode 100644 index 00000000..29d7ff2d --- /dev/null +++ b/engine/platform/Vulkan/VKresourceDescriptor.cpp @@ -0,0 +1,127 @@ +/* Engine Copyright (c) 2024 Engine Development Team + https://github.com/beaumanvienna/vulkan + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +#include "VKresourceDescriptor.h" +#include "VKdescriptor.h" +#include "VKrenderer.h" +#include "VKcubemap.h" +#include "VKbuffer.h" + +namespace GfxRenderEngine +{ + VK_ResourceDescriptor::VK_ResourceDescriptor(Resources::ResourceBuffers& buffers) + { + + // instance buffer + std::shared_ptr& instanceUbo = buffers[Resources::INSTANCE_BUFFER_INDEX]; + VK_Buffer* instanceBuffer = nullptr; + VkDescriptorBufferInfo instanceBufferInfo; + if (instanceUbo) + { + instanceBuffer = static_cast(instanceUbo.get()); + instanceBufferInfo = instanceBuffer->DescriptorInfo(); + } + + // joint/bone matrices + std::shared_ptr& skeletalAnimationUbo = buffers[Resources::SKELETAL_ANIMATION_BUFFER_INDEX]; + VK_Buffer* skeletalAnimationBuffer = nullptr; + VkDescriptorBufferInfo skeletalAnimationBufferInfo; + if (skeletalAnimationUbo) + { + skeletalAnimationBuffer = static_cast(skeletalAnimationUbo.get()); + skeletalAnimationBufferInfo = skeletalAnimationBuffer->DescriptorInfo(); + } + + { + VK_DescriptorSetLayout::Builder builder{}; + if (instanceUbo) + { + builder.AddBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT); + } + if (skeletalAnimationUbo) + { + builder.AddBinding(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT); + } + + if (builder.Size()) + { + std::unique_ptr localDescriptorSetLayout = builder.Build(); + + VK_DescriptorWriter descriptorWriter(*localDescriptorSetLayout, *VK_Renderer::m_DescriptorPool); + descriptorWriter.WriteBuffer(0, instanceBufferInfo); + if (skeletalAnimationBuffer) + { + descriptorWriter.WriteBuffer(1, skeletalAnimationBufferInfo); + } + descriptorWriter.Build(m_DescriptorSet); + } + } + + // shadows + { + VK_DescriptorSetLayout::Builder builder{}; + if (instanceUbo) + { + builder.AddBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT); + } + if (skeletalAnimationUbo) + { + builder.AddBinding(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT); + } + if(builder.Size()) + { + std::unique_ptr localDescriptorSetLayout = builder.Build(); + + VK_DescriptorWriter descriptorWriter(*localDescriptorSetLayout, *VK_Renderer::m_DescriptorPool); + descriptorWriter.WriteBuffer(0, instanceBufferInfo); + if (skeletalAnimationBuffer) + { + descriptorWriter.WriteBuffer(1, skeletalAnimationBufferInfo); + } + descriptorWriter.Build(m_ShadowDescriptorSet); + } + } + } + + VK_ResourceDescriptor::VK_ResourceDescriptor(VK_ResourceDescriptor const& other) + { + m_DescriptorSet = other.m_DescriptorSet; + m_ShadowDescriptorSet = other.m_ShadowDescriptorSet; + } + + VK_ResourceDescriptor::VK_ResourceDescriptor(std::shared_ptr const& resourceDescriptor) + { + if (resourceDescriptor) + { + VK_ResourceDescriptor* other = static_cast(resourceDescriptor.get()); + + m_DescriptorSet = other->m_DescriptorSet; + m_ShadowDescriptorSet = other->m_ShadowDescriptorSet; + } + } + + VK_ResourceDescriptor::~VK_ResourceDescriptor() {} + + const VkDescriptorSet& VK_ResourceDescriptor::GetDescriptorSet() const { return m_DescriptorSet; } + + const VkDescriptorSet& VK_ResourceDescriptor::GetShadowDescriptorSet() const { return m_ShadowDescriptorSet; } +} // namespace GfxRenderEngine diff --git a/engine/platform/Vulkan/VKresourceDescriptor.h b/engine/platform/Vulkan/VKresourceDescriptor.h new file mode 100644 index 00000000..d6db4711 --- /dev/null +++ b/engine/platform/Vulkan/VKresourceDescriptor.h @@ -0,0 +1,49 @@ +/* Engine Copyright (c) 2024 Engine Development Team + https://github.com/beaumanvienna/vulkan + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +#pragma once + +#include + +#include "renderer/resourceDescriptor.h" + +namespace GfxRenderEngine +{ + class VK_ResourceDescriptor : public ResourceDescriptor + { + + public: + VK_ResourceDescriptor(Resources::ResourceBuffers& buffers); + VK_ResourceDescriptor(VK_ResourceDescriptor const& other); + VK_ResourceDescriptor(std::shared_ptr const& resourceDescriptor); + + virtual ~VK_ResourceDescriptor(); + + public: + const VkDescriptorSet& GetDescriptorSet() const; + const VkDescriptorSet& GetShadowDescriptorSet() const; + + private: + VkDescriptorSet m_DescriptorSet; + VkDescriptorSet m_ShadowDescriptorSet; + }; +} // namespace GfxRenderEngine diff --git a/engine/platform/Vulkan/material.h b/engine/platform/Vulkan/material.h index 12c99f12..66662800 100644 --- a/engine/platform/Vulkan/material.h +++ b/engine/platform/Vulkan/material.h @@ -28,8 +28,3 @@ #define GLSL_HAS_ROUGHNESS_METALLIC_MAP (0x1 << 0x4) #define GLSL_HAS_EMISSIVE_COLOR (0x1 << 0x5) #define GLSL_HAS_EMISSIVE_MAP (0x1 << 0x6) - -// shader properties -#define GLSL_HAS_SKELETAL_ANIMATION (0x1 << 0x0) - -#define MAX_INSTANCE 64 \ No newline at end of file diff --git a/engine/platform/Vulkan/resource.h b/engine/platform/Vulkan/resource.h new file mode 100644 index 00000000..f56b196f --- /dev/null +++ b/engine/platform/Vulkan/resource.h @@ -0,0 +1,28 @@ +/* Engine Copyright (c) 2024 Engine Development Team + https://github.com/beaumanvienna/vulkan + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ + +// shader properties + +#define GLSL_HAS_INSTANCING (0x1 << 0x0) +#define GLSL_HAS_SKELETAL_ANIMATION (0x1 << 0x1) + +#define MAX_INSTANCE 64 \ No newline at end of file diff --git a/engine/platform/Vulkan/shaders/pbr.vert b/engine/platform/Vulkan/shaders/pbr.vert index f6462acc..768ad964 100644 --- a/engine/platform/Vulkan/shaders/pbr.vert +++ b/engine/platform/Vulkan/shaders/pbr.vert @@ -25,7 +25,7 @@ #version 450 #include "engine/platform/Vulkan/pointlights.h" -#include "engine/platform/Vulkan/material.h" +#include "engine/platform/Vulkan/resource.h" layout(location = 0) in vec3 position; layout(location = 1) in vec3 color; @@ -64,7 +64,7 @@ layout(set = 0, binding = 0) uniform GlobalUniformBuffer int m_NumberOfActiveDirectionalLights; } ubo; -layout(set = 1, binding = 6) uniform InstanceUniformBuffer +layout(set = 2, binding = 0) uniform InstanceUniformBuffer { InstanceData m_InstanceData[MAX_INSTANCE]; } uboInstanced; diff --git a/engine/platform/Vulkan/shaders/pbrSA.vert b/engine/platform/Vulkan/shaders/pbrSA.vert index 70b2c0ae..4fb4f383 100644 --- a/engine/platform/Vulkan/shaders/pbrSA.vert +++ b/engine/platform/Vulkan/shaders/pbrSA.vert @@ -27,7 +27,7 @@ #include "engine/renderer/skeletalAnimation/joints.h" #include "engine/platform/Vulkan/pointlights.h" -#include "engine/platform/Vulkan/material.h" +#include "engine/platform/Vulkan/resource.h" layout(location = 0) in vec3 position; layout(location = 1) in vec3 color; @@ -68,12 +68,12 @@ layout(set = 0, binding = 0) uniform GlobalUniformBuffer int m_NumberOfActiveDirectionalLights; } ubo; -layout(set = 1, binding = 6) uniform InstanceUniformBuffer +layout(set = 2, binding = 0) uniform InstanceUniformBuffer { InstanceData m_InstanceData[MAX_INSTANCE]; } uboInstanced; -layout(set = 1, binding = 7) uniform SkeletalAnimationShaderData +layout(set = 2, binding = 1) uniform SkeletalAnimationShaderData { mat4 m_FinalJointsMatrices[MAX_JOINTS]; } skeletalAnimation; diff --git a/engine/platform/Vulkan/shaders/shadowShaderAnimatedInstanced.vert b/engine/platform/Vulkan/shaders/shadowShaderAnimatedInstanced.vert index ae77b5ff..df42d049 100644 --- a/engine/platform/Vulkan/shaders/shadowShaderAnimatedInstanced.vert +++ b/engine/platform/Vulkan/shaders/shadowShaderAnimatedInstanced.vert @@ -22,7 +22,7 @@ #version 450 #include "engine/renderer/skeletalAnimation/joints.h" -#include "engine/platform/Vulkan/material.h" +#include "engine/platform/Vulkan/resource.h" layout(location = 0) in vec3 position; layout(location = 5) in ivec4 jointIds; diff --git a/engine/platform/Vulkan/shaders/shadowShaderInstanced.vert b/engine/platform/Vulkan/shaders/shadowShaderInstanced.vert index faa57a05..69b76015 100644 --- a/engine/platform/Vulkan/shaders/shadowShaderInstanced.vert +++ b/engine/platform/Vulkan/shaders/shadowShaderInstanced.vert @@ -20,7 +20,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ #version 450 -#include "engine/platform/Vulkan/material.h" +#include "engine/platform/Vulkan/resource.h" layout(location = 0) in vec3 position; diff --git a/engine/renderer/builder/builder.cpp b/engine/renderer/builder/builder.cpp index 8abdfe39..ffd8f241 100644 --- a/engine/renderer/builder/builder.cpp +++ b/engine/renderer/builder/builder.cpp @@ -1,4 +1,5 @@ -/* Engine Copyright (c) 2023 Engine Development Team + +/* Engine Copyright (c) 2024 Engine Development Team https://github.com/beaumanvienna/vulkan Permission is hereby granted, free of charge, to any person @@ -20,9 +21,6 @@ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#define TINYOBJLOADER_IMPLEMENTATION -#include "tiny_obj_loader.h" - #include "auxiliary/hash.h" #include "core.h" #include "renderer/builder/builder.h" @@ -201,13 +199,17 @@ namespace GfxRenderEngine { // create material descriptor Material::MaterialTextures materialTextures; - Material::MaterialBuffers materialBuffers; - materialBuffers[Material::INSTANCE_BUFFER_INDEX] = instanceBuffer->GetBuffer(); - auto materialDescriptor = - MaterialDescriptor::Create(MaterialDescriptor::MtPbr, materialTextures, materialBuffers); + auto materialDescriptor = MaterialDescriptor::Create(MaterialDescriptor::MtPbr, materialTextures); submesh.m_Material.m_MaterialDescriptor = materialDescriptor; } + { // create resource descriptor + Resources::ResourceBuffers resourceBuffers; + + resourceBuffers[Resources::INSTANCE_BUFFER_INDEX] = instanceBuffer->GetBuffer(); + auto resourceDescriptor = ResourceDescriptor::Create(resourceBuffers); + submesh.m_Resources.m_ResourceDescriptor = resourceDescriptor; + } m_Submeshes.push_back(submesh); auto model = Engine::m_Engine->LoadModel(*this); MeshComponent mesh{"terrain", model}; @@ -403,6 +405,7 @@ namespace GfxRenderEngine auto materialDescriptor = MaterialDescriptor::Create(MaterialDescriptor::MtCubemap, m_Cubemaps[0]); submesh.m_Material.m_MaterialDescriptor = materialDescriptor; } + m_Submeshes.push_back(submesh); } diff --git a/engine/renderer/builder/builder.h b/engine/renderer/builder/builder.h index 8a824181..4fd2300f 100644 --- a/engine/renderer/builder/builder.h +++ b/engine/renderer/builder/builder.h @@ -33,7 +33,6 @@ namespace GfxRenderEngine public: Builder() = default; - void LoadModelObjWavefront(const std::string& filepath, int fragAmplification = 1.0); entt::entity LoadTerrainHeightMapPNG(const std::string& filepath, Scene& scene); void LoadParticle(glm::vec4 const& color); void LoadSprite(Sprite const& sprite, float const amplification = 0.0f, int const unlit = 0, diff --git a/engine/renderer/builder/fastgltfBuilder.cpp b/engine/renderer/builder/fastgltfBuilder.cpp index b735599c..a0d5f742 100644 --- a/engine/renderer/builder/fastgltfBuilder.cpp +++ b/engine/renderer/builder/fastgltfBuilder.cpp @@ -26,6 +26,7 @@ #include "core.h" #include "renderer/builder/fastgltfBuilder.h" #include "renderer/materialDescriptor.h" +#include "renderer/resourceDescriptor.h" #include "auxiliary/instrumentation.h" #include "auxiliary/file.h" @@ -803,40 +804,42 @@ namespace GfxRenderEngine void FastgltfBuilder::AssignMaterial(Submesh& submesh, int const materialIndex) { - if (!(static_cast(materialIndex) < m_Materials.size())) - { - LOG_CORE_CRITICAL("AssignMaterial: materialIndex must be less than m_Materials.size()"); - } + { // material + if (!(static_cast(materialIndex) < m_Materials.size())) + { + LOG_CORE_CRITICAL("AssignMaterial: materialIndex must be less than m_Materials.size()"); + } - Material material{}; // create from defaults - Material::MaterialTextures materialTextures; + Material material{}; // create from defaults + Material::MaterialTextures materialTextures; - // material - if (materialIndex != Gltf::GLTF_NOT_USED) - { - material = m_Materials[materialIndex]; - materialTextures = m_MaterialTextures[materialIndex]; + // material + if (materialIndex != Gltf::GLTF_NOT_USED) + { + material = m_Materials[materialIndex]; + materialTextures = m_MaterialTextures[materialIndex]; + } + + // create material descriptor + + material.m_MaterialDescriptor = + MaterialDescriptor::Create(MaterialDescriptor::MaterialType::MtPbr, materialTextures); + + // assign + submesh.m_Material = material; } - // buffers - Material::MaterialBuffers materialBuffers; - { + { // resources + Resources::ResourceBuffers resourceBuffers; std::shared_ptr instanceUbo{m_InstanceBuffer->GetBuffer()}; - materialBuffers[Material::INSTANCE_BUFFER_INDEX] = instanceUbo; + resourceBuffers[Resources::INSTANCE_BUFFER_INDEX] = instanceUbo; if (m_SkeletalAnimation) { - materialBuffers[Material::SKELETAL_ANIMATION_BUFFER_INDEX] = m_ShaderData; + resourceBuffers[Resources::SKELETAL_ANIMATION_BUFFER_INDEX] = m_ShaderData; } + submesh.m_Resources.m_ResourceDescriptor = ResourceDescriptor::Create(resourceBuffers); } - // create material descriptor - - material.m_MaterialDescriptor = - MaterialDescriptor::Create(MaterialDescriptor::MaterialTypes::MtPbr, materialTextures, materialBuffers); - - // assign - submesh.m_Material = material; - LOG_CORE_INFO("material assigned (fastgltf): material index {0}", materialIndex); } diff --git a/engine/renderer/builder/fbxBuilder.cpp b/engine/renderer/builder/fbxBuilder.cpp index 9d3d572c..8a2c6153 100644 --- a/engine/renderer/builder/fbxBuilder.cpp +++ b/engine/renderer/builder/fbxBuilder.cpp @@ -661,40 +661,42 @@ namespace GfxRenderEngine void FbxBuilder::AssignMaterial(Submesh& submesh, int const materialIndex) { - if (!(static_cast(materialIndex) < m_Materials.size())) - { - LOG_CORE_CRITICAL("AssignMaterial: materialIndex must be less than m_Materials.size()"); - } + { // material + if (!(static_cast(materialIndex) < m_Materials.size())) + { + LOG_CORE_CRITICAL("AssignMaterial: materialIndex must be less than m_Materials.size()"); + } - Material material{}; // create from defaults - Material::MaterialTextures materialTextures; + Material material{}; // create from defaults + Material::MaterialTextures materialTextures; - // material - if (materialIndex != Gltf::GLTF_NOT_USED) - { - material = m_Materials[materialIndex]; - materialTextures = m_MaterialTextures[materialIndex]; + // material + if (materialIndex != Fbx::FBX_NOT_USED) + { + material = m_Materials[materialIndex]; + materialTextures = m_MaterialTextures[materialIndex]; + } + + // create material descriptor + + material.m_MaterialDescriptor = + MaterialDescriptor::Create(MaterialDescriptor::MaterialType::MtPbr, materialTextures); + + // assign + submesh.m_Material = material; } - // buffers - Material::MaterialBuffers materialBuffers; - { + { // resources + Resources::ResourceBuffers resourceBuffers; std::shared_ptr instanceUbo{m_InstanceBuffer->GetBuffer()}; - materialBuffers[Material::INSTANCE_BUFFER_INDEX] = instanceUbo; + resourceBuffers[Resources::INSTANCE_BUFFER_INDEX] = instanceUbo; if (m_SkeletalAnimation) { - materialBuffers[Material::SKELETAL_ANIMATION_BUFFER_INDEX] = m_ShaderData; + resourceBuffers[Resources::SKELETAL_ANIMATION_BUFFER_INDEX] = m_ShaderData; } + submesh.m_Resources.m_ResourceDescriptor = ResourceDescriptor::Create(resourceBuffers); } - // create material descriptor - - material.m_MaterialDescriptor = - MaterialDescriptor::Create(MaterialDescriptor::MaterialTypes::MtPbr, materialTextures, materialBuffers); - - // assign - submesh.m_Material = material; - LOG_CORE_INFO("material assigned (fastgltf): material index {0}", materialIndex); } diff --git a/engine/renderer/builder/gltfBuilder.cpp b/engine/renderer/builder/gltfBuilder.cpp index 41b2ef32..febebd97 100644 --- a/engine/renderer/builder/gltfBuilder.cpp +++ b/engine/renderer/builder/gltfBuilder.cpp @@ -822,40 +822,42 @@ namespace GfxRenderEngine void GltfBuilder::AssignMaterial(Submesh& submesh, int const materialIndex) { - if (!(static_cast(materialIndex) < m_Materials.size())) - { - LOG_CORE_CRITICAL("AssignMaterial: materialIndex must be less than m_Materials.size()"); - } + { // material + if (!(static_cast(materialIndex) < m_Materials.size())) + { + LOG_CORE_CRITICAL("AssignMaterial: materialIndex must be less than m_Materials.size()"); + } - Material material{}; // create from defaults - Material::MaterialTextures materialTextures; + Material material{}; // create from defaults + Material::MaterialTextures materialTextures; - // material - if (materialIndex != Gltf::GLTF_NOT_USED) - { - material = m_Materials[materialIndex]; - materialTextures = m_MaterialTextures[materialIndex]; + // material + if (materialIndex != Gltf::GLTF_NOT_USED) + { + material = m_Materials[materialIndex]; + materialTextures = m_MaterialTextures[materialIndex]; + } + + // create material descriptor + + material.m_MaterialDescriptor = + MaterialDescriptor::Create(MaterialDescriptor::MaterialType::MtPbr, materialTextures); + + // assign + submesh.m_Material = material; } - // buffers - Material::MaterialBuffers materialBuffers; - { + { // resources + Resources::ResourceBuffers resourceBuffers; std::shared_ptr instanceUbo{m_InstanceBuffer->GetBuffer()}; - materialBuffers[Material::INSTANCE_BUFFER_INDEX] = instanceUbo; + resourceBuffers[Resources::INSTANCE_BUFFER_INDEX] = instanceUbo; if (m_SkeletalAnimation) { - materialBuffers[Material::SKELETAL_ANIMATION_BUFFER_INDEX] = m_ShaderData; + resourceBuffers[Resources::SKELETAL_ANIMATION_BUFFER_INDEX] = m_ShaderData; } + submesh.m_Resources.m_ResourceDescriptor = ResourceDescriptor::Create(resourceBuffers); } - // create material descriptor - - material.m_MaterialDescriptor = - MaterialDescriptor::Create(MaterialDescriptor::MaterialTypes::MtPbr, materialTextures, materialBuffers); - - // assign - submesh.m_Material = material; - LOG_CORE_INFO("material assigned (tinygltf): material index {0}", materialIndex); } diff --git a/engine/renderer/builder/ufbxBuilder.cpp b/engine/renderer/builder/ufbxBuilder.cpp index 922d6043..3d1f7e7d 100644 --- a/engine/renderer/builder/ufbxBuilder.cpp +++ b/engine/renderer/builder/ufbxBuilder.cpp @@ -693,40 +693,42 @@ namespace GfxRenderEngine void UFbxBuilder::AssignMaterial(Submesh& submesh, int const materialIndex) { - if (!(static_cast(materialIndex) < m_Materials.size())) - { - LOG_CORE_CRITICAL("AssignMaterial: materialIndex must be less than m_Materials.size()"); - } + { // material + if (!(static_cast(materialIndex) < m_Materials.size())) + { + LOG_CORE_CRITICAL("AssignMaterial: materialIndex must be less than m_Materials.size()"); + } - Material material{}; // create from defaults - Material::MaterialTextures materialTextures; + Material material{}; // create from defaults + Material::MaterialTextures materialTextures; - // material - if (materialIndex != Gltf::GLTF_NOT_USED) - { - material = m_Materials[materialIndex]; - materialTextures = m_MaterialTextures[materialIndex]; + // material + if (materialIndex != Fbx::FBX_NOT_USED) + { + material = m_Materials[materialIndex]; + materialTextures = m_MaterialTextures[materialIndex]; + } + + // create material descriptor + + material.m_MaterialDescriptor = + MaterialDescriptor::Create(MaterialDescriptor::MaterialType::MtPbr, materialTextures); + + // assign + submesh.m_Material = material; } - // buffers - Material::MaterialBuffers materialBuffers; - { + { // resources + Resources::ResourceBuffers resourceBuffers; std::shared_ptr instanceUbo{m_InstanceBuffer->GetBuffer()}; - materialBuffers[Material::INSTANCE_BUFFER_INDEX] = instanceUbo; + resourceBuffers[Resources::INSTANCE_BUFFER_INDEX] = instanceUbo; if (m_SkeletalAnimation) { - materialBuffers[Material::SKELETAL_ANIMATION_BUFFER_INDEX] = m_ShaderData; + resourceBuffers[Resources::SKELETAL_ANIMATION_BUFFER_INDEX] = m_ShaderData; } + submesh.m_Resources.m_ResourceDescriptor = ResourceDescriptor::Create(resourceBuffers); } - // create material descriptor - - material.m_MaterialDescriptor = - MaterialDescriptor::Create(MaterialDescriptor::MaterialTypes::MtPbr, materialTextures, materialBuffers); - - // assign - submesh.m_Material = material; - LOG_CORE_INFO("material assigned (ufbx): material index {0}", materialIndex); } diff --git a/engine/renderer/materialDescriptor.cpp b/engine/renderer/materialDescriptor.cpp index 57a63832..7d8b1ce5 100644 --- a/engine/renderer/materialDescriptor.cpp +++ b/engine/renderer/materialDescriptor.cpp @@ -1,4 +1,4 @@ -/* Engine Copyright (c) 2023 Engine Development Team +/* Engine Copyright (c) 2024 Engine Development Team https://github.com/beaumanvienna/vulkan Permission is hereby granted, free of charge, to any person @@ -27,16 +27,15 @@ namespace GfxRenderEngine { - std::shared_ptr MaterialDescriptor::Create(MaterialTypes materialType, - Material::MaterialTextures& textures, - Material::MaterialBuffers& buffers) + std::shared_ptr MaterialDescriptor::Create(MaterialType materialTypes, + Material::MaterialTextures& textures) { std::shared_ptr materialDescriptor; switch (RendererAPI::GetAPI()) { case RendererAPI::VULKAN: - materialDescriptor = std::make_shared(materialType, textures, buffers); + materialDescriptor = std::make_shared(materialTypes, textures); break; default: materialDescriptor = nullptr; @@ -46,7 +45,7 @@ namespace GfxRenderEngine return materialDescriptor; } - std::shared_ptr MaterialDescriptor::Create(MaterialTypes materialType, + std::shared_ptr MaterialDescriptor::Create(MaterialType materialTypes, std::shared_ptr const& cubemap) { std::shared_ptr materialDescriptor; @@ -54,7 +53,7 @@ namespace GfxRenderEngine switch (RendererAPI::GetAPI()) { case RendererAPI::VULKAN: - materialDescriptor = std::make_shared(materialType, cubemap); + materialDescriptor = std::make_shared(materialTypes, cubemap); break; default: materialDescriptor = nullptr; diff --git a/engine/renderer/materialDescriptor.h b/engine/renderer/materialDescriptor.h index d9410c9a..56c68977 100644 --- a/engine/renderer/materialDescriptor.h +++ b/engine/renderer/materialDescriptor.h @@ -25,7 +25,6 @@ #include #include "engine.h" -#include "renderer/buffer.h" #include "renderer/texture.h" #include "renderer/cubemap.h" #include "scene/material.h" @@ -36,23 +35,22 @@ namespace GfxRenderEngine class MaterialDescriptor { public: - enum MaterialTypes + enum MaterialType { - MtPbr = 0x1 << 0x00, // 1 - MtCubemap = 0x1 << 0x1 // 2 + MtPbr = 0x1 << 0x00, // 1 + MtCubemap = 0x1 << 0x01 // 2 }; - static constexpr uint ALL_PBR_MATERIALS = MaterialTypes::MtPbr; + static constexpr uint ALL_PBR_MATERIALS = MaterialType::MtPbr; public: virtual ~MaterialDescriptor() = default; - static std::shared_ptr Create(MaterialDescriptor::MaterialTypes materialType, - Material::MaterialTextures& textures, - Material::MaterialBuffers& buffers); - static std::shared_ptr Create(MaterialDescriptor::MaterialTypes materialType, + static std::shared_ptr Create(MaterialDescriptor::MaterialType materialTypes, + Material::MaterialTextures& textures); + static std::shared_ptr Create(MaterialDescriptor::MaterialType materialTypes, std::shared_ptr const& cubemap); public: - virtual MaterialDescriptor::MaterialTypes GetMaterialType() const = 0; + virtual MaterialDescriptor::MaterialType GetMaterialType() const = 0; }; } // namespace GfxRenderEngine diff --git a/engine/renderer/model.h b/engine/renderer/model.h index 568ad43a..4edf85df 100644 --- a/engine/renderer/model.h +++ b/engine/renderer/model.h @@ -46,6 +46,7 @@ #include "renderer/skeletalAnimation/skeleton.h" #include "renderer/skeletalAnimation/skeletalAnimations.h" #include "renderer/materialDescriptor.h" +#include "renderer/resourceDescriptor.h" #include "renderer/texture.h" #include "renderer/cubemap.h" #include "sprite/sprite.h" @@ -55,13 +56,13 @@ namespace GfxRenderEngine { struct Vertex // 3D, with animation { - glm::vec3 m_Position; // layout(location = 0) - glm::vec4 m_Color; // layout(location = 1) - glm::vec3 m_Normal; // layout(location = 2) - glm::vec2 m_UV; // layout(location = 3) - glm::vec3 m_Tangent; // layout(location = 4) - glm::ivec4 m_JointIds; // layout(location = 5) - glm::vec4 m_Weights; // layout(location = 6) + glm::vec3 m_Position; // layout(location = 0) + glm::vec4 m_Color; // layout(location = 1) + glm::vec3 m_Normal; // layout(location = 2) + glm::vec2 m_UV; // layout(location = 3) + glm::vec3 m_Tangent; // layout(location = 4) + glm::ivec4 m_JointIds; // layout(location = 5) + glm::vec4 m_Weights; // layout(location = 6) }; struct Submesh @@ -72,6 +73,7 @@ namespace GfxRenderEngine uint m_VertexCount; uint m_InstanceCount; Material m_Material; + Resources m_Resources; }; class Model diff --git a/engine/renderer/resourceDescriptor.cpp b/engine/renderer/resourceDescriptor.cpp new file mode 100644 index 00000000..dc95aa83 --- /dev/null +++ b/engine/renderer/resourceDescriptor.cpp @@ -0,0 +1,46 @@ +/* Engine Copyright (c) 2024 Engine Development Team + https://github.com/beaumanvienna/vulkan + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +#include "renderer/rendererAPI.h" +#include "renderer/resourceDescriptor.h" + +#include "VKresourceDescriptor.h" + +namespace GfxRenderEngine +{ + std::shared_ptr ResourceDescriptor::Create(Resources::ResourceBuffers& buffers) + { + std::shared_ptr resourceDescriptor; + + switch (RendererAPI::GetAPI()) + { + case RendererAPI::VULKAN: + resourceDescriptor = std::make_shared(buffers); + break; + default: + resourceDescriptor = nullptr; + break; + } + + return resourceDescriptor; + } +} // namespace GfxRenderEngine diff --git a/engine/renderer/resourceDescriptor.h b/engine/renderer/resourceDescriptor.h new file mode 100644 index 00000000..fa2b1b83 --- /dev/null +++ b/engine/renderer/resourceDescriptor.h @@ -0,0 +1,42 @@ +/* Engine Copyright (c) 2024 Engine Development Team + https://github.com/beaumanvienna/vulkan + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +#pragma once + +#include + +#include "engine.h" +#include "renderer/buffer.h" +#include "renderer/cubemap.h" +#include "scene/resource.h" + +namespace GfxRenderEngine +{ + + class ResourceDescriptor + { + public: + virtual ~ResourceDescriptor() = default; + + static std::shared_ptr Create(Resources::ResourceBuffers& buffers); + }; +} // namespace GfxRenderEngine diff --git a/engine/scene/material.h b/engine/scene/material.h index fd066731..031974c2 100644 --- a/engine/scene/material.h +++ b/engine/scene/material.h @@ -45,16 +45,8 @@ namespace GfxRenderEngine NUM_TEXTURES }; - enum BufferIndices - { - INSTANCE_BUFFER_INDEX = 0, - SKELETAL_ANIMATION_BUFFER_INDEX, - NUM_BUFFERS - }; - // fixed-size array for material textures typedef std::array, Material::NUM_TEXTURES> MaterialTextures; - typedef std::array, Material::NUM_BUFFERS> MaterialBuffers; enum MaterialFeatures // bitset { diff --git a/engine/scene/resource.h b/engine/scene/resource.h new file mode 100644 index 00000000..a704adfe --- /dev/null +++ b/engine/scene/resource.h @@ -0,0 +1,57 @@ +/* Engine Copyright (c) 2022 Engine Development Team + https://github.com/beaumanvienna/vulkan + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +#pragma once + +#include +#include "renderer/texture.h" +#include "renderer/buffer.h" +#include "engine/platform/Vulkan/resource.h" +#include "engine.h" + +namespace GfxRenderEngine +{ + class ResourceDescriptor; + class Resources + { + public: + enum BufferIndices + { + INSTANCE_BUFFER_INDEX = 0, + SKELETAL_ANIMATION_BUFFER_INDEX, + NUM_BUFFERS + }; + + // fixed-size array for resource buffers + typedef std::array, Resources::NUM_BUFFERS> ResourceBuffers; + + enum ResourceFeatures // bitset + { + HAS_INSTANCING = GLSL_HAS_INSTANCING, + HAS_SKELETAL_ANIMATION = GLSL_HAS_SKELETAL_ANIMATION + }; + + public: + std::shared_ptr m_ResourceDescriptor; + }; + +} // namespace GfxRenderEngine