templated pipeline

This commit is contained in:
Jorijn van der Graaf 2026-01-31 21:08:42 +01:00
commit 74832c6824
8 changed files with 229 additions and 170 deletions

View file

@ -9,31 +9,32 @@ import Crafter.Math;
typedef VulkanShader<"raygen.spv", "main", VK_SHADER_STAGE_RAYGEN_BIT_KHR, 2, {{{VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1}}}> Raygenspv;
typedef VulkanShader<"closesthit.spv", "main", VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, 0, {{}}> Closesthitspv;
typedef VulkanShader<"miss.spv", "main", VK_SHADER_STAGE_MISS_BIT_KHR, 0, {{}}> Misspv;
typedef std::tuple<Raygenspv, Misspv, Closesthitspv> AllShaders;
typedef std::tuple<
ShaderGroup<0, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR>,
ShaderGroup<1, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR>,
ShaderGroup<VK_SHADER_UNUSED_KHR, 2, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR>
> ShaderGroups;
typedef PipelineRTVulkan<AllShaders, ShaderGroups> Pipeline;
int main() {
/*
This sets up all necessary things and creates the vulkan device.
This must be called before any vulkan related things.
Things like VkDevice are static members of the VulkanDevice class.
*/
VulkanDevice::CreateDevice();
WindowVulkan window(1280, 720, "HelloVulkan");
VkCommandBuffer cmd = window.StartInit();
Raygenspv::CreateShader();
Closesthitspv::CreateShader();
Misspv::CreateShader();
ShaderBindingTableVulkan<AllShaders>::Init();
std::array<VkDescriptorSetLayout, 3> layouts {{Raygenspv::layout, Raygenspv::layout, Raygenspv::layout}};
PipelineRTVulkan<Raygenspv, Closesthitspv, Misspv, Raygenspv, Closesthitspv, Misspv>::Init(layouts);
Pipeline::Init(cmd, layouts);
window.SetPipelineRT<Pipeline>();
DescriptorPool pool;
pool.sets.resize(3);
pool.BuildPool(DescriptorPool::GetPoolSizes<Raygenspv, Raygenspv, Raygenspv>(), layouts);
WindowVulkan window(1280, 720, "HelloVulkan");
/*
StartInit gives you a VkCommandBuffer to use before the event loop starts
Use this for inititializing things like textures.
*/
VkCommandBuffer cmd = window.StartInit();
Mesh triangleMesh;
std::array<Vertex, 3> verts {{{-150, -150, 100}, {0, 150, 100}, {150, -150, 100}}};
std::array<std::uint32_t, 3> index {{2,1,0}};
@ -41,13 +42,12 @@ int main() {
RenderingElement3DVulkan& el = RenderingElement3DVulkan::elements.emplace_back(triangleMesh);
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Identity();
std::memcpy(el.instance.transform.matrix, transform.m, sizeof(transform.m));
RenderingElement3DVulkan::tlases.resize(3);
window.SetPipelineRT<Raygenspv, Closesthitspv, Misspv, Raygenspv, Closesthitspv, Misspv>();
RenderingElement3DVulkan::BuildTLAS(cmd, 0);
RenderingElement3DVulkan::BuildTLAS(cmd, 1);
RenderingElement3DVulkan::BuildTLAS(cmd, 2);
VkDescriptorImageInfo imageInfo = {
.imageView = window.imageViews[0],
.imageLayout = VK_IMAGE_LAYOUT_GENERAL

View file

@ -9,30 +9,28 @@ import Crafter.Math;
typedef VulkanShader<"raygen.spv", "main", VK_SHADER_STAGE_RAYGEN_BIT_KHR, 2, {{{VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1}}}> Raygenspv;
typedef VulkanShader<"closesthit.spv", "main", VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, 0, {{}}> Closesthitspv;
typedef VulkanShader<"miss.spv", "main", VK_SHADER_STAGE_MISS_BIT_KHR, 0, {{}}> Misspv;
typedef std::tuple<Raygenspv, Misspv, Closesthitspv> AllShaders;
typedef std::tuple<
ShaderGroup<0, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR>,
ShaderGroup<1, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR>,
ShaderGroup<VK_SHADER_UNUSED_KHR, 2, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR>
> ShaderGroups;
typedef PipelineRTVulkan<AllShaders, ShaderGroups> Pipeline;
int main() {
/*
This sets up all necessary things and creates the vulkan device.
This must be called before any vulkan related things.
Things like VkDevice are static members of the VulkanDevice class.
*/
VulkanDevice::CreateDevice();
WindowVulkan window(1280, 720, "HelloVulkan");
VkCommandBuffer cmd = window.StartInit();
Raygenspv::CreateShader();
Closesthitspv::CreateShader();
Misspv::CreateShader();
ShaderBindingTableVulkan<AllShaders>::Init();
std::array<VkDescriptorSetLayout, 1> layouts {{Raygenspv::layout}};
PipelineRTVulkan<Raygenspv, Closesthitspv, Misspv, Raygenspv, Closesthitspv, Misspv>::Init(layouts);
Pipeline::Init(cmd, layouts);
DescriptorPool pool;
pool.sets.resize(1);
pool.BuildPool(DescriptorPool::GetPoolSizes<Raygenspv, Closesthitspv, Misspv>(), layouts);
WindowVulkan window(1280, 720, "HelloVulkan");
/*
StartInit gives you a VkCommandBuffer to use before the event loop starts
Use this for inititializing things like textures.
*/
VkCommandBuffer cmd = window.StartInit();
pool.BuildPool(DescriptorPool::GetPoolSizes<Raygenspv>(), layouts);
Mesh triangleMesh;
std::array<Vertex, 3> verts {{{-150, -150, 100}, {0, 150, 100}, {150, -150, 100}}};
@ -77,17 +75,10 @@ int main() {
};
vkUpdateDescriptorSets(VulkanDevice::device, 2, write, 0, nullptr);
/*
FinishInit executes all commands recorded to StartInit.
This must be called before the the event loops starts if you called StartInit before.
*/
window.FinishInit();
window.SetPipelineRT<Raygenspv, Closesthitspv, Misspv, Raygenspv, Closesthitspv, Misspv>();
window.SetPipelineRT<Pipeline>();
window.descriptorsRt = pool.sets;
window.FinishInit();
window.Render();
window.StartSync();
}

View file

@ -7,7 +7,7 @@
"dependencies": [
{
"path":"../../project.json",
"configuration":"lib-vulkan"
"configuration":"lib-vulkan-debug"
}
],
"shaders": [

View file

@ -26,10 +26,19 @@ export module Crafter.Graphics:PipelineRTVulkan;
import std;
import :VulkanDevice;
import :VulkanBuffer;
import :ShaderBindingTableVulkan;
import :Types;
export namespace Crafter {
template <typename Raygen, typename ClosestHit, typename Miss, typename ShadowClosestHit, typename ShadowMiss, typename... Shaders>
template <std::uint32_t GeneralShader, std::uint32_t ClosestHitShader, std::uint32_t AnyHitShader, std::uint32_t IntersectionShader>
struct ShaderGroup {
static constexpr std::uint32_t generalShader = GeneralShader;
static constexpr std::uint32_t closestHitShader = ClosestHitShader;
static constexpr std::uint32_t anyHitShader = AnyHitShader;
static constexpr std::uint32_t intersectionShader = IntersectionShader;
};
template <typename Shaders, typename ShaderGroups>
class PipelineRTVulkan {
public:
inline static VkPipeline pipeline;
@ -41,7 +50,7 @@ export namespace Crafter {
inline static VkStridedDeviceAddressRegionKHR hitRegion;
inline static VkStridedDeviceAddressRegionKHR callableRegion;
static void Init(std::span<VkDescriptorSetLayout> setLayouts) {
static void Init(VkCommandBuffer cmd, std::span<VkDescriptorSetLayout> setLayouts) {
VkPipelineLayoutCreateInfo pipelineLayoutInfo {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = static_cast<std::uint32_t>(setLayouts.size()),
@ -50,144 +59,146 @@ export namespace Crafter {
VulkanDevice::CheckVkResult(vkCreatePipelineLayout(VulkanDevice::device, &pipelineLayoutInfo, nullptr, &pipelineLayout));
std::array<VkPipelineShaderStageCreateInfo, 5> shaderStages;
constexpr auto groupIndexSeq = std::make_index_sequence<std::tuple_size_v<ShaderGroups>>{};
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;
shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStages[1].stage = Miss::_stage;
shaderStages[1].module = Miss::shader;
shaderStages[1].pName = Miss::_entrypoint.value;
shaderStages[1].flags = 0;
shaderStages[1].pSpecializationInfo = nullptr;
shaderStages[1].pNext = nullptr;
shaderStages[2].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStages[2].stage = ClosestHit::_stage;
shaderStages[2].module = ClosestHit::shader;
shaderStages[2].pName = ClosestHit::_entrypoint.value;
shaderStages[2].flags = 0;
shaderStages[2].pSpecializationInfo = nullptr;
shaderStages[2].pNext = nullptr;
shaderStages[3].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStages[3].stage = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR;
shaderStages[3].module = ShadowClosestHit::shader;
shaderStages[3].pName = ShadowClosestHit::_entrypoint.value;
shaderStages[3].flags = 0;
shaderStages[3].pSpecializationInfo = nullptr;
shaderStages[3].pNext = nullptr;
shaderStages[4].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStages[4].stage = ShadowMiss::_stage;
shaderStages[4].module = ShadowMiss::shader;
shaderStages[4].pName = ShadowMiss::_entrypoint.value;
shaderStages[4].flags = 0;
shaderStages[4].pSpecializationInfo = nullptr;
shaderStages[4].pNext = nullptr;
std::array<VkRayTracingShaderGroupCreateInfoKHR, 5> groups {{
{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR,
.generalShader = 0,
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR
},
{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR,
.generalShader = 1,
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR
},
{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR,
.generalShader = VK_SHADER_UNUSED_KHR,
.closestHitShader = 2,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR
},
{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR,
.generalShader = VK_SHADER_UNUSED_KHR,
.closestHitShader = 3,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR
},
{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR,
.generalShader = 4,
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR
}
}};
constexpr std::array<VkRayTracingShaderGroupCreateInfoKHR, std::tuple_size_v<ShaderGroups>> groups = GetShaderGroups(groupIndexSeq);
VkRayTracingPipelineCreateInfoKHR rtPipelineInfo{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR,
.stageCount = static_cast<std::uint32_t>(shaderStages.size()),
.pStages = shaderStages.data(),
.stageCount = static_cast<std::uint32_t>(ShaderBindingTableVulkan<Shaders>::shaderStages.size()),
.pStages = ShaderBindingTableVulkan<Shaders>::shaderStages.data(),
.groupCount = static_cast<std::uint32_t>(groups.size()),
.pGroups = groups.data(),
.maxPipelineRayRecursionDepth = 2,
.maxPipelineRayRecursionDepth = 1,
.layout = pipelineLayout
};
VulkanDevice::CheckVkResult(VulkanDevice::vkCreateRayTracingPipelinesKHR(VulkanDevice::device, {}, {}, 1, &rtPipelineInfo, nullptr, &pipeline));
std::uint32_t handleSize = VulkanDevice::rayTracingProperties.shaderGroupHandleSize;
std::uint32_t handleAlignment = VulkanDevice::rayTracingProperties.shaderGroupHandleAlignment;
std::uint32_t baseAlignment = VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment;
std::uint32_t groupCount = rtPipelineInfo.groupCount;
std::size_t dataSize = handleSize * groupCount;
std::size_t dataSize = VulkanDevice::rayTracingProperties.shaderGroupHandleSize * rtPipelineInfo.groupCount;
shaderHandles.resize(dataSize);
VulkanDevice::CheckVkResult(VulkanDevice::vkGetRayTracingShaderGroupHandlesKHR(VulkanDevice::device, pipeline, 0, groupCount, dataSize, shaderHandles.data()));
VulkanDevice::CheckVkResult(VulkanDevice::vkGetRayTracingShaderGroupHandlesKHR(VulkanDevice::device, pipeline, 0, rtPipelineInfo.groupCount, dataSize, shaderHandles.data()));
std::uint32_t sbtStride = AlignUp(handleSize, handleAlignment);
std::uint32_t raygenOffset = 0;
std::uint32_t missOffset = AlignUp(raygenOffset + sbtStride, baseAlignment);
std::uint32_t hitOffset = AlignUp(missOffset + sbtStride * 2, baseAlignment);
std::uint32_t sbtStride = AlignUp(VulkanDevice::rayTracingProperties.shaderGroupHandleSize, VulkanDevice::rayTracingProperties.shaderGroupHandleAlignment);
std::uint32_t hitGroupCount = 2;
std::size_t bufferSize = hitOffset + sbtStride * hitGroupCount;
raygenRegion.stride = sbtStride;
raygenRegion.deviceAddress = 0;
raygenRegion.size = GetGroupCount<VK_SHADER_STAGE_RAYGEN_BIT_KHR>(groupIndexSeq) * sbtStride;
missRegion.stride = sbtStride;
missRegion.deviceAddress = AlignUp(raygenRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
missRegion.size = GetGroupCount<VK_SHADER_STAGE_MISS_BIT_KHR>(groupIndexSeq) * sbtStride;
hitRegion.stride = sbtStride;
hitRegion.deviceAddress = AlignUp(missRegion.deviceAddress + missRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
hitRegion.size = GetGroupCount<VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR>(groupIndexSeq) * sbtStride;
std::size_t bufferSize = hitRegion.deviceAddress + hitRegion.size;
sbtBuffer.Create(VK_BUFFER_USAGE_2_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bufferSize);
// Ray generation shader (group 0)
std::memcpy(sbtBuffer.value + raygenOffset, shaderHandles.data() + 0 * handleSize, handleSize);
raygenRegion.deviceAddress = sbtBuffer.address + raygenOffset;
raygenRegion.stride = sbtStride;
raygenRegion.size = sbtStride;
AddShaderGroupsToBuffer(sbtStride, groupIndexSeq);
sbtBuffer.FlushDevice(cmd, VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
std::memcpy(sbtBuffer.value + missOffset, shaderHandles.data() + 1 * handleSize, handleSize);
std::memcpy(sbtBuffer.value + missOffset + 1 * sbtStride, shaderHandles.data() + 4 * handleSize, handleSize);
missRegion.deviceAddress = sbtBuffer.address + missOffset;
missRegion.stride = sbtStride;
missRegion.size = sbtStride * 2;
std::memcpy(sbtBuffer.value + hitOffset + 0 * sbtStride, shaderHandles.data() + 2 * handleSize, handleSize);
std::memcpy(sbtBuffer.value + hitOffset + 1 * sbtStride, shaderHandles.data() + 3 * handleSize, handleSize);
hitRegion.deviceAddress = sbtBuffer.address + hitOffset;
hitRegion.stride = sbtStride;
hitRegion.size = sbtStride * hitGroupCount;
raygenRegion.deviceAddress += sbtBuffer.address;
missRegion.deviceAddress += sbtBuffer.address;
hitRegion.deviceAddress += sbtBuffer.address;
callableRegion.deviceAddress = 0;
callableRegion.stride = 0;
callableRegion.size = 0;
}
private:
template<std::size_t index>
consteval static void AddShaderGroup(std::array<VkRayTracingShaderGroupCreateInfoKHR, std::tuple_size_v<ShaderGroups>>& groups) {
using groupTemplate = std::tuple_element_t<index, ShaderGroups>;
VkRayTracingShaderGroupTypeKHR groupType;
if constexpr(groupTemplate::generalShader != VK_SHADER_UNUSED_KHR) {
groupType = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR;
} else if constexpr(groupTemplate::closestHitShader != VK_SHADER_UNUSED_KHR) {
groupType = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR;
} else {
static_assert(
groupTemplate::generalShader != VK_SHADER_UNUSED_KHR ||
groupTemplate::closestHitShader != VK_SHADER_UNUSED_KHR,
"Shader group must define either a general or closest-hit shader"
);
}
groups[index] = {
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.type = groupType,
.generalShader = groupTemplate::generalShader,
.closestHitShader = groupTemplate::closestHitShader,
.anyHitShader = groupTemplate::anyHitShader,
.intersectionShader = groupTemplate::intersectionShader
};
}
template<std::size_t... Is>
consteval static std::array<VkRayTracingShaderGroupCreateInfoKHR, std::tuple_size_v<ShaderGroups>> GetShaderGroups(std::index_sequence<Is...>) {
std::array<VkRayTracingShaderGroupCreateInfoKHR, std::tuple_size_v<ShaderGroups>> groups{};
(AddShaderGroup<Is>(groups), ...);
return groups;
}
template<std::size_t index, VkShaderStageFlagBits stage>
consteval static void GetGroupCountImpl(std::uint32_t& count) {
using groupTemplate = std::tuple_element_t<index, ShaderGroups>;
if constexpr(groupTemplate::generalShader != VK_SHADER_UNUSED_KHR) {
using shaderTemplate = std::tuple_element_t<groupTemplate::generalShader, Shaders>;
if constexpr(shaderTemplate::_stage == stage) {
count++;
}
} else if constexpr(groupTemplate::closestHitShader != VK_SHADER_UNUSED_KHR) {
using shaderTemplate = std::tuple_element_t<groupTemplate::closestHitShader, Shaders>;
if constexpr(shaderTemplate::_stage == stage) {
count++;
}
} else {
static_assert(
groupTemplate::generalShader != VK_SHADER_UNUSED_KHR ||
groupTemplate::closestHitShader != VK_SHADER_UNUSED_KHR,
"Shader group must define either a general or closest-hit shader"
);
}
}
template<VkShaderStageFlagBits stage, std::size_t... Is>
consteval static std::uint32_t GetGroupCount(std::index_sequence<Is...>) {
std::uint32_t count = 0;
(GetGroupCountImpl<Is, stage>(count), ...);
return count;
}
template<std::size_t index, VkShaderStageFlagBits stage>
static void AddShaderGroupToBuffer(std::uint32_t sbtStride, std::uint32_t& offset) {
using groupTemplate = std::tuple_element_t<index, ShaderGroups>;
if constexpr(groupTemplate::generalShader != VK_SHADER_UNUSED_KHR) {
using shaderTemplate = std::tuple_element_t<groupTemplate::generalShader, Shaders>;
if constexpr(shaderTemplate::_stage == stage) {
std::memcpy(sbtBuffer.value + offset, shaderHandles.data() + index * VulkanDevice::rayTracingProperties.shaderGroupHandleSize, VulkanDevice::rayTracingProperties.shaderGroupHandleSize);
offset += sbtStride;
}
} else if constexpr(groupTemplate::closestHitShader != VK_SHADER_UNUSED_KHR) {
using shaderTemplate = std::tuple_element_t<groupTemplate::closestHitShader, Shaders>;
if constexpr(shaderTemplate::_stage == stage) {
std::memcpy(sbtBuffer.value + offset, shaderHandles.data() + index * VulkanDevice::rayTracingProperties.shaderGroupHandleSize, VulkanDevice::rayTracingProperties.shaderGroupHandleSize);
offset += sbtStride;
}
} else {
static_assert(
groupTemplate::generalShader != VK_SHADER_UNUSED_KHR ||
groupTemplate::closestHitShader != VK_SHADER_UNUSED_KHR,
"Shader group must define either a general or closest-hit shader"
);
}
}
template<std::size_t... Is>
static void AddShaderGroupsToBuffer(std::uint32_t sbtStride, std::index_sequence<Is...>) {
std::uint32_t offset = 0;
(AddShaderGroupToBuffer<Is, VK_SHADER_STAGE_RAYGEN_BIT_KHR>(sbtStride, offset), ...);
offset = AlignUp(offset, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
(AddShaderGroupToBuffer<Is, VK_SHADER_STAGE_MISS_BIT_KHR>(sbtStride, offset), ...);
offset = AlignUp(offset, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
(AddShaderGroupToBuffer<Is, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR>(sbtStride, offset), ...);
}
};
}

View file

@ -0,0 +1,56 @@
/*
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 <vulkan/vulkan.h>
#endif
export module Crafter.Graphics:ShaderBindingTableVulkan;
#ifdef CRAFTER_GRAPHICS_VULKAN
import std;
import :VulkanDevice;
import :VulkanBuffer;
import :Types;
export namespace Crafter {
template <typename Shaders>
class ShaderBindingTableVulkan {
public:
inline static std::array<VkPipelineShaderStageCreateInfo, std::tuple_size_v<Shaders>> shaderStages;
static void Init() {
AddAllToSBT(std::make_index_sequence<std::tuple_size_v<Shaders>>{});
}
private:
template<std::size_t index>
static void AddToSBT() {
shaderStages[index] = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = std::tuple_element_t<index, Shaders>::_stage,
.module = std::tuple_element_t<index, Shaders>::shader,
.pName = std::tuple_element_t<index, Shaders>::_entrypoint.value
};
}
template<std::size_t... Is>
static void AddAllToSBT(std::index_sequence<Is...>) {
(AddToSBT<Is>(), ...);
}
};
}
#endif

View file

@ -313,14 +313,14 @@ export namespace Crafter {
VkCommandBuffer StartInit();
void FinishInit();
template <typename Raygen, typename Closesthit, typename Miss, typename... Shaders>
template <typename Pipeline>
void SetPipelineRT() {
rtPipeline = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::pipeline;
rtPipelineLayout = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::pipelineLayout;
raygenRegion = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::raygenRegion;
missRegion = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::missRegion;
hitRegion = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::hitRegion;
callableRegion = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::callableRegion;
rtPipeline = Pipeline::pipeline;
rtPipelineLayout = Pipeline::pipelineLayout;
raygenRegion = Pipeline::raygenRegion;
missRegion = Pipeline::missRegion;
hitRegion = Pipeline::hitRegion;
callableRegion = Pipeline::callableRegion;
}
inline static wl_compositor* compositor = nullptr;
static void wl_surface_frame_done(void *data, wl_callback *cb, uint32_t time);

View file

@ -38,6 +38,7 @@ export import :VulkanTransition;
export import :VulkanBuffer;
export import :DescriptorPoolVulkan;
export import :ShaderVulkan;
export import :ShaderBindingTableVulkan;
export import :PipelineRTVulkan;
export import :RenderingElement3DVulkan;
#endif

View file

@ -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-DescriptorPoolVulkan", "interfaces/Crafter.Graphics-ShaderVulkan", "interfaces/Crafter.Graphics-PipelineRTVulkan"],
"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-ShaderBindingTableVulkan"],
"type": "library"
},
{