new descriptor layout 2

This commit is contained in:
Jorijn van der Graaf 2026-02-03 21:03:11 +01:00
commit d1b8e45fb6
13 changed files with 412 additions and 128 deletions

View file

@ -6,9 +6,9 @@ import std;
import Crafter.Event;
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 VulkanShader<"raygen.spv", "main", VK_SHADER_STAGE_RAYGEN_BIT_KHR> Raygenspv;
typedef VulkanShader<"closesthit.spv", "main", VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR> Closesthitspv;
typedef VulkanShader<"miss.spv", "main", VK_SHADER_STAGE_MISS_BIT_KHR> 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>,
@ -16,6 +16,22 @@ typedef std::tuple<
ShaderGroup<VK_SHADER_UNUSED_KHR, 2, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR>
> ShaderGroups;
typedef PipelineRTVulkan<AllShaders, ShaderGroups> Pipeline;
typedef DescriptorSetLayoutVulkan<1, {{
{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
},
}}> descriptorSetLayoutTlas;
typedef DescriptorSetLayoutVulkan<1, {{
{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
}
}}> descriptorSetLayoutImage;
int main() {
VulkanDevice::CreateDevice();
@ -27,13 +43,17 @@ int main() {
Misspv::CreateShader();
ShaderBindingTableVulkan<AllShaders>::Init();
std::array<VkDescriptorSetLayout, 3> layouts {{Raygenspv::layout, Raygenspv::layout, Raygenspv::layout}};
Pipeline::Init(cmd, layouts);
window.SetPipelineRT<Pipeline>();
descriptorSetLayoutTlas::Init();
descriptorSetLayoutImage::Init();
std::array<VkDescriptorSetLayout, 4> layouts {{descriptorSetLayoutTlas::layout, descriptorSetLayoutImage::layout, descriptorSetLayoutImage::layout, descriptorSetLayoutImage::layout}};
DescriptorPool pool;
pool.sets.resize(3);
pool.BuildPool(DescriptorPool::GetPoolSizes<Raygenspv, Raygenspv, Raygenspv>(), layouts);
pool.sets.resize(4);
pool.BuildPool(DescriptorPool::GetPoolSizes<descriptorSetLayoutTlas, descriptorSetLayoutImage, descriptorSetLayoutImage, descriptorSetLayoutImage>(), layouts);
Pipeline::Init(cmd, layouts);
window.SetPipelineRT<Pipeline>();
Mesh triangleMesh;
std::array<Vertex, 3> verts {{{-150, -150, 100}, {0, 150, 100}, {150, -150, 100}}};
@ -43,14 +63,24 @@ int main() {
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);
RenderingElement3DVulkan::tlases.resize(WindowVulkan::numFrames);
RenderingElement3DVulkan::BuildTLAS(cmd, 0);
RenderingElement3DVulkan::BuildTLAS(cmd, 1);
RenderingElement3DVulkan::BuildTLAS(cmd, 2);
VkDescriptorImageInfo imageInfo = {
.imageView = window.imageViews[0],
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
VkDescriptorImageInfo imageInfo[WindowVulkan::numFrames] = {
{
.imageView = window.imageViews[0],
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
},
{
.imageView = window.imageViews[1],
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
},
{
.imageView = window.imageViews[2],
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
},
};
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
@ -59,28 +89,31 @@ int main() {
.pAccelerationStructures = &RenderingElement3DVulkan::tlases[0].accelerationStructure
};
VkWriteDescriptorSet write[2] = {
{
VkWriteDescriptorSet write[4];
write[0] = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = &writeDescriptorSetAccelerationStructure,
.dstSet = pool.sets[0],
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
};
for(std::uint32_t i = 0; i < WindowVulkan::numFrames; i++) {
write[i+1] = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = &writeDescriptorSetAccelerationStructure,
.dstSet = pool.sets[0],
.dstSet = pool.sets[i+1],
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
},
{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = pool.sets[0],
.dstBinding = 1,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.pImageInfo = &imageInfo
}
};
vkUpdateDescriptorSets(VulkanDevice::device, 2, write, 0, nullptr);
window.descriptorsRt = {pool.sets[0]};
.pImageInfo = &imageInfo[i]
};
}
vkUpdateDescriptorSets(VulkanDevice::device, 4, write, 0, nullptr);
window.descriptorsRt = {pool.sets[0], pool.sets[1]};
/*
FinishInit executes all commands recorded to StartInit.
@ -104,10 +137,6 @@ int main() {
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Translation(value, 0, 0);
std::memcpy(el.instance.transform.matrix, transform.m, sizeof(transform.m));
RenderingElement3DVulkan::BuildTLAS(window.drawCmdBuffers[window.currentBuffer], window.currentBuffer);
VkDescriptorImageInfo imageInfo = {
.imageView = window.imageViews[window.currentBuffer],
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
};
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
@ -115,28 +144,18 @@ int main() {
.pAccelerationStructures = &RenderingElement3DVulkan::tlases[window.currentBuffer].accelerationStructure
};
VkWriteDescriptorSet write[2] = {
{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = &writeDescriptorSetAccelerationStructure,
.dstSet = pool.sets[window.currentBuffer],
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
},
{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = pool.sets[window.currentBuffer],
.dstBinding = 1,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.pImageInfo = &imageInfo
}
VkWriteDescriptorSet write = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = &writeDescriptorSetAccelerationStructure,
.dstSet = pool.sets[0],
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
};
vkUpdateDescriptorSets(VulkanDevice::device, 2, write, 0, nullptr);
window.descriptorsRt[0] = pool.sets[window.currentBuffer];
vkUpdateDescriptorSets(VulkanDevice::device, 1, &write, 0, nullptr);
window.descriptorsRt[1] = pool.sets[window.currentBuffer+1];
});
window.Render();