templated pipeline
This commit is contained in:
parent
8275e01b6c
commit
74832c6824
8 changed files with 229 additions and 170 deletions
|
|
@ -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<"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<"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 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() {
|
int main() {
|
||||||
/*
|
VulkanDevice::CreateDevice();
|
||||||
This sets up all necessary things and creates the vulkan device.
|
WindowVulkan window(1280, 720, "HelloVulkan");
|
||||||
This must be called before any vulkan related things.
|
VkCommandBuffer cmd = window.StartInit();
|
||||||
Things like VkDevice are static members of the VulkanDevice class.
|
|
||||||
*/
|
|
||||||
VulkanDevice::CreateDevice();
|
|
||||||
Raygenspv::CreateShader();
|
Raygenspv::CreateShader();
|
||||||
Closesthitspv::CreateShader();
|
Closesthitspv::CreateShader();
|
||||||
Misspv::CreateShader();
|
Misspv::CreateShader();
|
||||||
|
|
||||||
|
ShaderBindingTableVulkan<AllShaders>::Init();
|
||||||
std::array<VkDescriptorSetLayout, 3> layouts {{Raygenspv::layout, Raygenspv::layout, Raygenspv::layout}};
|
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;
|
DescriptorPool pool;
|
||||||
pool.sets.resize(3);
|
pool.sets.resize(3);
|
||||||
pool.BuildPool(DescriptorPool::GetPoolSizes<Raygenspv, Raygenspv, Raygenspv>(), layouts);
|
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;
|
Mesh triangleMesh;
|
||||||
std::array<Vertex, 3> verts {{{-150, -150, 100}, {0, 150, 100}, {150, -150, 100}}};
|
std::array<Vertex, 3> verts {{{-150, -150, 100}, {0, 150, 100}, {150, -150, 100}}};
|
||||||
std::array<std::uint32_t, 3> index {{2,1,0}};
|
std::array<std::uint32_t, 3> index {{2,1,0}};
|
||||||
|
|
@ -41,13 +42,12 @@ int main() {
|
||||||
RenderingElement3DVulkan& el = RenderingElement3DVulkan::elements.emplace_back(triangleMesh);
|
RenderingElement3DVulkan& el = RenderingElement3DVulkan::elements.emplace_back(triangleMesh);
|
||||||
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Identity();
|
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Identity();
|
||||||
std::memcpy(el.instance.transform.matrix, transform.m, sizeof(transform.m));
|
std::memcpy(el.instance.transform.matrix, transform.m, sizeof(transform.m));
|
||||||
|
|
||||||
RenderingElement3DVulkan::tlases.resize(3);
|
RenderingElement3DVulkan::tlases.resize(3);
|
||||||
|
|
||||||
window.SetPipelineRT<Raygenspv, Closesthitspv, Misspv, Raygenspv, Closesthitspv, Misspv>();
|
|
||||||
|
|
||||||
RenderingElement3DVulkan::BuildTLAS(cmd, 0);
|
RenderingElement3DVulkan::BuildTLAS(cmd, 0);
|
||||||
RenderingElement3DVulkan::BuildTLAS(cmd, 1);
|
RenderingElement3DVulkan::BuildTLAS(cmd, 1);
|
||||||
RenderingElement3DVulkan::BuildTLAS(cmd, 2);
|
RenderingElement3DVulkan::BuildTLAS(cmd, 2);
|
||||||
|
|
||||||
VkDescriptorImageInfo imageInfo = {
|
VkDescriptorImageInfo imageInfo = {
|
||||||
.imageView = window.imageViews[0],
|
.imageView = window.imageViews[0],
|
||||||
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
|
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
|
||||||
|
|
|
||||||
|
|
@ -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<"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<"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 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() {
|
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();
|
VulkanDevice::CreateDevice();
|
||||||
|
WindowVulkan window(1280, 720, "HelloVulkan");
|
||||||
|
VkCommandBuffer cmd = window.StartInit();
|
||||||
|
|
||||||
Raygenspv::CreateShader();
|
Raygenspv::CreateShader();
|
||||||
Closesthitspv::CreateShader();
|
Closesthitspv::CreateShader();
|
||||||
Misspv::CreateShader();
|
Misspv::CreateShader();
|
||||||
|
ShaderBindingTableVulkan<AllShaders>::Init();
|
||||||
std::array<VkDescriptorSetLayout, 1> layouts {{Raygenspv::layout}};
|
std::array<VkDescriptorSetLayout, 1> layouts {{Raygenspv::layout}};
|
||||||
PipelineRTVulkan<Raygenspv, Closesthitspv, Misspv, Raygenspv, Closesthitspv, Misspv>::Init(layouts);
|
Pipeline::Init(cmd, layouts);
|
||||||
DescriptorPool pool;
|
DescriptorPool pool;
|
||||||
pool.sets.resize(1);
|
pool.sets.resize(1);
|
||||||
pool.BuildPool(DescriptorPool::GetPoolSizes<Raygenspv, Closesthitspv, Misspv>(), layouts);
|
pool.BuildPool(DescriptorPool::GetPoolSizes<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;
|
Mesh triangleMesh;
|
||||||
std::array<Vertex, 3> verts {{{-150, -150, 100}, {0, 150, 100}, {150, -150, 100}}};
|
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);
|
vkUpdateDescriptorSets(VulkanDevice::device, 2, write, 0, nullptr);
|
||||||
|
|
||||||
|
window.SetPipelineRT<Pipeline>();
|
||||||
/*
|
|
||||||
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.descriptorsRt = pool.sets;
|
window.descriptorsRt = pool.sets;
|
||||||
|
|
||||||
|
window.FinishInit();
|
||||||
window.Render();
|
window.Render();
|
||||||
window.StartSync();
|
window.StartSync();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
{
|
{
|
||||||
"path":"../../project.json",
|
"path":"../../project.json",
|
||||||
"configuration":"lib-vulkan"
|
"configuration":"lib-vulkan-debug"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"shaders": [
|
"shaders": [
|
||||||
|
|
|
||||||
|
|
@ -26,10 +26,19 @@ export module Crafter.Graphics:PipelineRTVulkan;
|
||||||
import std;
|
import std;
|
||||||
import :VulkanDevice;
|
import :VulkanDevice;
|
||||||
import :VulkanBuffer;
|
import :VulkanBuffer;
|
||||||
|
import :ShaderBindingTableVulkan;
|
||||||
import :Types;
|
import :Types;
|
||||||
|
|
||||||
export namespace Crafter {
|
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 {
|
class PipelineRTVulkan {
|
||||||
public:
|
public:
|
||||||
inline static VkPipeline pipeline;
|
inline static VkPipeline pipeline;
|
||||||
|
|
@ -41,7 +50,7 @@ export namespace Crafter {
|
||||||
inline static VkStridedDeviceAddressRegionKHR hitRegion;
|
inline static VkStridedDeviceAddressRegionKHR hitRegion;
|
||||||
inline static VkStridedDeviceAddressRegionKHR callableRegion;
|
inline static VkStridedDeviceAddressRegionKHR callableRegion;
|
||||||
|
|
||||||
static void Init(std::span<VkDescriptorSetLayout> setLayouts) {
|
static void Init(VkCommandBuffer cmd, std::span<VkDescriptorSetLayout> setLayouts) {
|
||||||
VkPipelineLayoutCreateInfo pipelineLayoutInfo {
|
VkPipelineLayoutCreateInfo pipelineLayoutInfo {
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
|
||||||
.setLayoutCount = static_cast<std::uint32_t>(setLayouts.size()),
|
.setLayoutCount = static_cast<std::uint32_t>(setLayouts.size()),
|
||||||
|
|
@ -50,144 +59,146 @@ export namespace Crafter {
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkCreatePipelineLayout(VulkanDevice::device, &pipelineLayoutInfo, nullptr, &pipelineLayout));
|
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;
|
constexpr std::array<VkRayTracingShaderGroupCreateInfoKHR, std::tuple_size_v<ShaderGroups>> groups = GetShaderGroups(groupIndexSeq);
|
||||||
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
|
|
||||||
}
|
|
||||||
}};
|
|
||||||
|
|
||||||
VkRayTracingPipelineCreateInfoKHR rtPipelineInfo{
|
VkRayTracingPipelineCreateInfoKHR rtPipelineInfo{
|
||||||
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR,
|
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR,
|
||||||
.stageCount = static_cast<std::uint32_t>(shaderStages.size()),
|
.stageCount = static_cast<std::uint32_t>(ShaderBindingTableVulkan<Shaders>::shaderStages.size()),
|
||||||
.pStages = shaderStages.data(),
|
.pStages = ShaderBindingTableVulkan<Shaders>::shaderStages.data(),
|
||||||
.groupCount = static_cast<std::uint32_t>(groups.size()),
|
.groupCount = static_cast<std::uint32_t>(groups.size()),
|
||||||
.pGroups = groups.data(),
|
.pGroups = groups.data(),
|
||||||
.maxPipelineRayRecursionDepth = 2,
|
.maxPipelineRayRecursionDepth = 1,
|
||||||
.layout = pipelineLayout
|
.layout = pipelineLayout
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(VulkanDevice::vkCreateRayTracingPipelinesKHR(VulkanDevice::device, {}, {}, 1, &rtPipelineInfo, nullptr, &pipeline));
|
VulkanDevice::CheckVkResult(VulkanDevice::vkCreateRayTracingPipelinesKHR(VulkanDevice::device, {}, {}, 1, &rtPipelineInfo, nullptr, &pipeline));
|
||||||
|
|
||||||
std::uint32_t handleSize = VulkanDevice::rayTracingProperties.shaderGroupHandleSize;
|
std::size_t dataSize = VulkanDevice::rayTracingProperties.shaderGroupHandleSize * rtPipelineInfo.groupCount;
|
||||||
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;
|
|
||||||
shaderHandles.resize(dataSize);
|
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 sbtStride = AlignUp(VulkanDevice::rayTracingProperties.shaderGroupHandleSize, VulkanDevice::rayTracingProperties.shaderGroupHandleAlignment);
|
||||||
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 hitGroupCount = 2;
|
raygenRegion.stride = sbtStride;
|
||||||
std::size_t bufferSize = hitOffset + sbtStride * hitGroupCount;
|
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);
|
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)
|
AddShaderGroupsToBuffer(sbtStride, groupIndexSeq);
|
||||||
std::memcpy(sbtBuffer.value + raygenOffset, shaderHandles.data() + 0 * handleSize, handleSize);
|
sbtBuffer.FlushDevice(cmd, VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
|
||||||
raygenRegion.deviceAddress = sbtBuffer.address + raygenOffset;
|
|
||||||
raygenRegion.stride = sbtStride;
|
|
||||||
raygenRegion.size = sbtStride;
|
|
||||||
|
|
||||||
std::memcpy(sbtBuffer.value + missOffset, shaderHandles.data() + 1 * handleSize, handleSize);
|
raygenRegion.deviceAddress += sbtBuffer.address;
|
||||||
std::memcpy(sbtBuffer.value + missOffset + 1 * sbtStride, shaderHandles.data() + 4 * handleSize, handleSize);
|
missRegion.deviceAddress += sbtBuffer.address;
|
||||||
missRegion.deviceAddress = sbtBuffer.address + missOffset;
|
hitRegion.deviceAddress += sbtBuffer.address;
|
||||||
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;
|
|
||||||
|
|
||||||
callableRegion.deviceAddress = 0;
|
callableRegion.deviceAddress = 0;
|
||||||
callableRegion.stride = 0;
|
callableRegion.stride = 0;
|
||||||
callableRegion.size = 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), ...);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
56
interfaces/Crafter.Graphics-ShaderBindingTableVulkan.cppm
Normal file
56
interfaces/Crafter.Graphics-ShaderBindingTableVulkan.cppm
Normal 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
|
||||||
|
|
@ -313,14 +313,14 @@ export namespace Crafter {
|
||||||
VkCommandBuffer StartInit();
|
VkCommandBuffer StartInit();
|
||||||
void FinishInit();
|
void FinishInit();
|
||||||
|
|
||||||
template <typename Raygen, typename Closesthit, typename Miss, typename... Shaders>
|
template <typename Pipeline>
|
||||||
void SetPipelineRT() {
|
void SetPipelineRT() {
|
||||||
rtPipeline = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::pipeline;
|
rtPipeline = Pipeline::pipeline;
|
||||||
rtPipelineLayout = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::pipelineLayout;
|
rtPipelineLayout = Pipeline::pipelineLayout;
|
||||||
raygenRegion = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::raygenRegion;
|
raygenRegion = Pipeline::raygenRegion;
|
||||||
missRegion = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::missRegion;
|
missRegion = Pipeline::missRegion;
|
||||||
hitRegion = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::hitRegion;
|
hitRegion = Pipeline::hitRegion;
|
||||||
callableRegion = PipelineRTVulkan<Raygen, Closesthit, Miss, Shaders...>::callableRegion;
|
callableRegion = Pipeline::callableRegion;
|
||||||
}
|
}
|
||||||
inline static wl_compositor* compositor = nullptr;
|
inline static wl_compositor* compositor = nullptr;
|
||||||
static void wl_surface_frame_done(void *data, wl_callback *cb, uint32_t time);
|
static void wl_surface_frame_done(void *data, wl_callback *cb, uint32_t time);
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ export import :VulkanTransition;
|
||||||
export import :VulkanBuffer;
|
export import :VulkanBuffer;
|
||||||
export import :DescriptorPoolVulkan;
|
export import :DescriptorPoolVulkan;
|
||||||
export import :ShaderVulkan;
|
export import :ShaderVulkan;
|
||||||
|
export import :ShaderBindingTableVulkan;
|
||||||
export import :PipelineRTVulkan;
|
export import :PipelineRTVulkan;
|
||||||
export import :RenderingElement3DVulkan;
|
export import :RenderingElement3DVulkan;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
{
|
{
|
||||||
"name": "base",
|
"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"],
|
"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"
|
"type": "library"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue