diff --git a/examples/VulkanTriangle/main.cpp b/examples/VulkanTriangle/main.cpp index 67a29d4..af4ad06 100644 --- a/examples/VulkanTriangle/main.cpp +++ b/examples/VulkanTriangle/main.cpp @@ -15,6 +15,8 @@ int main() { */ VulkanDevice::CreateDevice(); Raygenspv::CreateShader(); + DescriptorLayoutVulkan::Init(); + PipelineRTVulkan::Init(); DescriptorPool<1, Raygenspv> pool; pool.setsCount = 1; pool.BuildPool(0); diff --git a/implementations/Crafter.Graphics-VulkanDevice.cpp b/implementations/Crafter.Graphics-VulkanDevice.cpp index 7213643..0eb7649 100644 --- a/implementations/Crafter.Graphics-VulkanDevice.cpp +++ b/implementations/Crafter.Graphics-VulkanDevice.cpp @@ -293,6 +293,7 @@ void VulkanDevice::CreateDevice() { vkCreateAccelerationStructureKHR = reinterpret_cast(vkGetInstanceProcAddr(instance, "vkCreateAccelerationStructureKHR")); vkCmdBuildAccelerationStructuresKHR = reinterpret_cast(vkGetInstanceProcAddr(instance, "vkCmdBuildAccelerationStructuresKHR")); vkGetAccelerationStructureDeviceAddressKHR = reinterpret_cast(vkGetInstanceProcAddr(instance, "vkGetAccelerationStructureDeviceAddressKHR")); + vkCreateRayTracingPipelinesKHR = reinterpret_cast(vkGetInstanceProcAddr(instance, "vkCreateRayTracingPipelinesKHR")); } std::uint32_t VulkanDevice::GetMemoryType(uint32_t typeBits, VkMemoryPropertyFlags properties) { diff --git a/interfaces/Crafter.Graphics-DescriptorSetVulkan.cppm b/interfaces/Crafter.Graphics-DescriptorLayoutVulkan.cppm similarity index 72% rename from interfaces/Crafter.Graphics-DescriptorSetVulkan.cppm rename to interfaces/Crafter.Graphics-DescriptorLayoutVulkan.cppm index e976026..396a7fe 100644 --- a/interfaces/Crafter.Graphics-DescriptorSetVulkan.cppm +++ b/interfaces/Crafter.Graphics-DescriptorLayoutVulkan.cppm @@ -21,12 +21,11 @@ module; #ifdef CRAFTER_GRAPHICS_VULKAN #include #endif -export module Crafter.Graphics:DescriptorSetVulkan; +export module Crafter.Graphics:DescriptorLayoutVulkan; #ifdef CRAFTER_GRAPHICS_VULKAN import std; import :VulkanDevice; import :Types; -import Crafter.Event; export namespace Crafter { struct DescriptorEntry { @@ -34,17 +33,11 @@ export namespace Crafter { bool occured = true; }; - - template - class DescriptorPool { + template + class DescriptorLayoutVulkan { public: - inline static Event onDescriptorRefresh; - inline static std::uint32_t setIndex = 0; - inline static std::uint32_t setsCount = 0; - inline static std::vector sets; - inline static VkDescriptorPool descriptorPool[PoolCount] = { VK_NULL_HANDLE }; + inline static VkDescriptorSetLayout descriptorSetLayout[sizeof...(Shaders)]; - private: template consteval static void GetOccuringDescriptors(std::array& types) { for (const DescriptorBinding& binding : Shader::descriptors) { @@ -115,27 +108,7 @@ export namespace Crafter { } public: - static void BuildPool(std::uint32_t poolIndex) { - if(descriptorPool[poolIndex] != VK_NULL_HANDLE) { - vkDestroyDescriptorPool(VulkanDevice::device, descriptorPool[poolIndex], nullptr); - } - - std::array poolSizes = GetPoolSizes(); - for(VkDescriptorPoolSize& size : poolSizes) { - size.descriptorCount *= setsCount; - } - - VkDescriptorPoolCreateInfo descriptorPoolInfo { - .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, - .maxSets = static_cast(setsCount), - .poolSizeCount = uniqueDescriptorCount, - .pPoolSizes = poolSizes.data() - }; - - VulkanDevice::CheckVkResult(vkCreateDescriptorPool(VulkanDevice::device, &descriptorPoolInfo, nullptr, &descriptorPool[poolIndex])); - - VkDescriptorSetLayout descriptorSetLayout[sizeof...(Shaders)]; - + static void Init() { std::uint32_t shaderIndex = 0; ([&] { @@ -148,20 +121,6 @@ export namespace Crafter { VulkanDevice::CheckVkResult(vkCreateDescriptorSetLayout(VulkanDevice::device, &descriptorLayoutInfoMesh, nullptr, &descriptorSetLayout[shaderIndex++])); }(), ...); - - VkDescriptorSetAllocateInfo allocInfo { - .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, - .descriptorPool = descriptorPool[poolIndex], - .descriptorSetCount = setsCount, - .pSetLayouts = descriptorSetLayout, - }; - - sets.resize(setsCount); - VulkanDevice::CheckVkResult(vkAllocateDescriptorSets(VulkanDevice::device, &allocInfo, sets.data())); - - setIndex = 0; - - onDescriptorRefresh.Invoke(); } }; } diff --git a/interfaces/Crafter.Graphics-DescriptorPoolVulkan.cppm b/interfaces/Crafter.Graphics-DescriptorPoolVulkan.cppm new file mode 100644 index 0000000..4ec2a3c --- /dev/null +++ b/interfaces/Crafter.Graphics-DescriptorPoolVulkan.cppm @@ -0,0 +1,79 @@ +/* +Crafter®.Graphics +Copyright (C) 2026 Catcrafts® +catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License version 3.0 as published by the Free Software Foundation; + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +module; +#ifdef CRAFTER_GRAPHICS_VULKAN +#include +#endif +export module Crafter.Graphics:DescriptorPoolVulkan; +#ifdef CRAFTER_GRAPHICS_VULKAN +import std; +import :VulkanDevice; +import :Types; +import :DescriptorLayoutVulkan; +import Crafter.Event; + +export namespace Crafter { + template + class DescriptorPool { + public: + inline static Event onDescriptorRefresh; + inline static std::uint32_t setIndex = 0; + inline static std::uint32_t setsCount = 0; + inline static std::vector sets; + inline static VkDescriptorPool descriptorPool[PoolCount] = { VK_NULL_HANDLE }; + + public: + static void BuildPool(std::uint32_t poolIndex) { + if(descriptorPool[poolIndex] != VK_NULL_HANDLE) { + vkDestroyDescriptorPool(VulkanDevice::device, descriptorPool[poolIndex], nullptr); + } + + std::array::uniqueDescriptorCount> poolSizes = DescriptorLayoutVulkan::GetPoolSizes(); + for(VkDescriptorPoolSize& size : poolSizes) { + size.descriptorCount *= setsCount; + } + + VkDescriptorPoolCreateInfo descriptorPoolInfo { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, + .maxSets = static_cast(setsCount), + .poolSizeCount = DescriptorLayoutVulkan::uniqueDescriptorCount, + .pPoolSizes = poolSizes.data() + }; + + VulkanDevice::CheckVkResult(vkCreateDescriptorPool(VulkanDevice::device, &descriptorPoolInfo, nullptr, &descriptorPool[poolIndex])); + + VkDescriptorSetAllocateInfo allocInfo { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, + .descriptorPool = descriptorPool[poolIndex], + .descriptorSetCount = setsCount, + .pSetLayouts = DescriptorLayoutVulkan::descriptorSetLayout, + }; + + sets.resize(setsCount); + VulkanDevice::CheckVkResult(vkAllocateDescriptorSets(VulkanDevice::device, &allocInfo, sets.data())); + + setIndex = 0; + + onDescriptorRefresh.Invoke(); + } + }; +} + +#endif \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-PipelineRTVulkan.cppm b/interfaces/Crafter.Graphics-PipelineRTVulkan.cppm new file mode 100644 index 0000000..990563d --- /dev/null +++ b/interfaces/Crafter.Graphics-PipelineRTVulkan.cppm @@ -0,0 +1,79 @@ +/* +Crafter®.Graphics +Copyright (C) 2026 Catcrafts® +catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License version 3.0 as published by the Free Software Foundation; + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +module; +#ifdef CRAFTER_GRAPHICS_VULKAN +#include +#endif +export module Crafter.Graphics:PipelineRTVulkan; +#ifdef CRAFTER_GRAPHICS_VULKAN +import std; +import :VulkanDevice; +import :DescriptorLayoutVulkan; + +export namespace Crafter { + template + class PipelineRTVulkan { + public: + inline static VkPipeline pipeline; + inline static VkPipelineLayout pipelineLayout; + + static void Init() { + VkPipelineLayoutCreateInfo pipelineLayoutInfo { + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = sizeof...(Shaders), + .pSetLayouts = DescriptorLayoutVulkan::descriptorSetLayout + }; + + VulkanDevice::CheckVkResult(vkCreatePipelineLayout(VulkanDevice::device, &pipelineLayoutInfo, nullptr, &pipelineLayout)); + + std::array shaderStages; + + shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; + shaderStages[0].stage = Raygen::_stage; + shaderStages[0].module = Raygen::shader; + shaderStages[0].pName = Raygen::_entrypoint.value; + shaderStages[0].flags = 0; + shaderStages[0].pSpecializationInfo = nullptr; + shaderStages[0].pNext = nullptr; + + VkRayTracingShaderGroupCreateInfoKHR group { + .sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR, + .generalShader = 0, + .closestHitShader = VK_SHADER_UNUSED_KHR, + .anyHitShader = VK_SHADER_UNUSED_KHR, + .intersectionShader = VK_SHADER_UNUSED_KHR + }; + + VkRayTracingPipelineCreateInfoKHR rtPipelineInfo{ + .sType = VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR, + .stageCount = static_cast(shaderStages.size()), + .pStages = shaderStages.data(), + .groupCount = static_cast(1), + .pGroups = &group, + .maxPipelineRayRecursionDepth = 1, + .layout = pipelineLayout + }; + + VulkanDevice::vkCreateRayTracingPipelinesKHR(VulkanDevice::device, {}, {}, 1, &rtPipelineInfo, nullptr, &pipeline); + } + }; +} + +#endif \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-VulkanDevice.cppm b/interfaces/Crafter.Graphics-VulkanDevice.cppm index 1b4e937..76c555d 100644 --- a/interfaces/Crafter.Graphics-VulkanDevice.cppm +++ b/interfaces/Crafter.Graphics-VulkanDevice.cppm @@ -44,6 +44,7 @@ export namespace Crafter { inline static PFN_vkCreateAccelerationStructureKHR vkCreateAccelerationStructureKHR; inline static PFN_vkCmdBuildAccelerationStructuresKHR vkCmdBuildAccelerationStructuresKHR; inline static PFN_vkGetAccelerationStructureDeviceAddressKHR vkGetAccelerationStructureDeviceAddressKHR; + inline static PFN_vkCreateRayTracingPipelinesKHR vkCreateRayTracingPipelinesKHR; inline static VkPhysicalDeviceMemoryProperties memoryProperties; static void CreateDevice(); static void CheckVkResult(VkResult result); diff --git a/interfaces/Crafter.Graphics.cppm b/interfaces/Crafter.Graphics.cppm index d3ca9c3..242002e 100644 --- a/interfaces/Crafter.Graphics.cppm +++ b/interfaces/Crafter.Graphics.cppm @@ -36,8 +36,10 @@ export import :Mesh; export import :VulkanDevice; export import :VulkanTransition; export import :VulkanBuffer; -export import :DescriptorSetVulkan; +export import :DescriptorPoolVulkan; +export import :DescriptorLayoutVulkan; export import :ShaderVulkan; +export import :PipelineRTVulkan; export import :RenderingElement3DVulkan; #endif diff --git a/project.json b/project.json index 188c807..06dc5c8 100644 --- a/project.json +++ b/project.json @@ -4,7 +4,7 @@ { "name": "base", "implementations": ["implementations/Crafter.Graphics-Font", "implementations/Crafter.Graphics-Shm", "implementations/Crafter.Graphics-Window", "implementations/Crafter.Graphics-MouseElement", "implementations/Crafter.Graphics-Transform", "implementations/Crafter.Graphics-GridElement", "implementations/Crafter.Graphics-Image"], - "interfaces": ["interfaces/Crafter.Graphics-Window", "interfaces/Crafter.Graphics", "interfaces/Crafter.Graphics-Types", "interfaces/Crafter.Graphics-Font", "interfaces/Crafter.Graphics-Image", "interfaces/Crafter.Graphics-Shm", "interfaces/Crafter.Graphics-Animation", "interfaces/Crafter.Graphics-RenderingElement", "interfaces/Crafter.Graphics-MouseElement", "interfaces/Crafter.Graphics-Transform", "interfaces/Crafter.Graphics-GridElement", "interfaces/Crafter.Graphics-VulkanDevice", "interfaces/Crafter.Graphics-VulkanTransition", "interfaces/Crafter.Graphics-Mesh", "interfaces/Crafter.Graphics-VulkanBuffer", "interfaces/Crafter.Graphics-RenderingElement3DVulkan", "interfaces/Crafter.Graphics-DescriptorSetVulkan", "interfaces/Crafter.Graphics-ShaderVulkan"], + "interfaces": ["interfaces/Crafter.Graphics-Window", "interfaces/Crafter.Graphics", "interfaces/Crafter.Graphics-Types", "interfaces/Crafter.Graphics-Font", "interfaces/Crafter.Graphics-Image", "interfaces/Crafter.Graphics-Shm", "interfaces/Crafter.Graphics-Animation", "interfaces/Crafter.Graphics-RenderingElement", "interfaces/Crafter.Graphics-MouseElement", "interfaces/Crafter.Graphics-Transform", "interfaces/Crafter.Graphics-GridElement", "interfaces/Crafter.Graphics-VulkanDevice", "interfaces/Crafter.Graphics-VulkanTransition", "interfaces/Crafter.Graphics-Mesh", "interfaces/Crafter.Graphics-VulkanBuffer", "interfaces/Crafter.Graphics-RenderingElement3DVulkan", "interfaces/Crafter.Graphics-DescriptorPoolVulkan", "interfaces/Crafter.Graphics-ShaderVulkan", "interfaces/Crafter.Graphics-PipelineRTVulkan", "interfaces/Crafter.Graphics-DescriptorLayoutVulkan"], "type": "library" }, {