Rt works now, no idea why

This commit is contained in:
Dynamitos
2024-07-15 09:18:59 +02:00
parent 42d98d7233
commit 38986f4bfc
21 changed files with 251 additions and 206 deletions
+153 -118
View File
@@ -477,7 +477,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.flags = 0,
.stage = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
.module = rayGen->getModuleHandle(),
.pName = "main",
.pName = rayGen->getEntryPointName(),
});
shaderGroups.add(VkRayTracingShaderGroupCreateInfoKHR{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
@@ -498,7 +498,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.flags = 0,
.stage = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR,
.module = hit->getModuleHandle(),
.pName = "main",
.pName = hit->getEntryPointName(),
});
uint32 hitIndex = static_cast<uint32>(shaderStages.size() - 1);
uint32 anyHitIndex = VK_SHADER_UNUSED_KHR;
@@ -511,7 +511,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.flags = 0,
.stage = VK_SHADER_STAGE_ANY_HIT_BIT_KHR,
.module = anyHit->getModuleHandle(),
.pName = "main",
.pName = anyHit->getEntryPointName(),
});
}
if (hitgroup.intersectionShader != nullptr) {
@@ -589,144 +589,179 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
.pStages = shaderStages.data(),
.groupCount = static_cast<uint32>(shaderGroups.size()),
.pGroups = shaderGroups.data(),
.maxPipelineRayRecursionDepth = 24,
.maxPipelineRayRecursionDepth = 1,
.layout = createInfo.pipelineLayout.cast<PipelineLayout>()->getHandle(),
};
VkPipeline pipelineHandle;
VK_CHECK(vkCreateRayTracingPipelinesKHR(graphics->getDevice(), VK_NULL_HANDLE, cache, 1, &pipelineInfo, nullptr, &pipelineHandle));
/*
const uint32_t handle_size = graphics->getRayTracingProperties().shaderGroupHandleSize;
const uint32_t handle_size_aligned =
align(graphics->getRayTracingProperties().shaderGroupHandleSize, graphics->getRayTracingProperties().shaderGroupHandleAlignment);
const uint32_t handle_alignment = graphics->getRayTracingProperties().shaderGroupHandleAlignment;
const uint32_t group_count = static_cast<uint32_t>(shaderGroups.size());
const uint32_t sbt_size = group_count * handle_size_aligned;
const VkBufferUsageFlags sbt_buffer_usage_flags =
VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
const VmaMemoryUsage sbt_memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
// Raygen
// Create binding table buffers for each shader type
OBufferAllocation raygen_shader_binding_table = new BufferAllocation(graphics, "RayGen",
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.size = handle_size,
.usage = sbt_buffer_usage_flags,
},
VmaAllocationCreateInfo{
.usage = sbt_memory_usage,
},
Gfx::QueueType::GRAPHICS, 0);
OBufferAllocation miss_shader_binding_table = new BufferAllocation(graphics, "Miss",
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.size = handle_size,
.usage = sbt_buffer_usage_flags,
},
VmaAllocationCreateInfo{
.usage = sbt_memory_usage,
},
Gfx::QueueType::GRAPHICS, 0);
OBufferAllocation hit_shader_binding_table = new BufferAllocation(graphics, "Hit",
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.size = handle_size,
.usage = sbt_buffer_usage_flags,
},
VmaAllocationCreateInfo{
.usage = sbt_memory_usage,
},
Gfx::QueueType::GRAPHICS, 0);
// Copy the pipeline's shader handles into a host buffer
std::vector<uint8_t> shader_handle_storage(sbt_size);
VK_CHECK(vkGetRayTracingShaderGroupHandlesKHR(graphics->getDevice(), pipelineHandle, 0, group_count, sbt_size,
shader_handle_storage.data()));
// Copy the shader handles from the host buffer to the binding tables
uint8_t* data = static_cast<uint8_t*>(raygen_shader_binding_table->map());
memcpy(data, shader_handle_storage.data(), handle_size);
data = static_cast<uint8_t*>(miss_shader_binding_table->map());
memcpy(data, shader_handle_storage.data() + handle_size_aligned, handle_size);
data = static_cast<uint8_t*>(hit_shader_binding_table->map());
memcpy(data, shader_handle_storage.data() + handle_size_aligned * 2, handle_size);
raygen_shader_binding_table->unmap();
miss_shader_binding_table->unmap();
hit_shader_binding_table->unmap();*/
const uint32_t handleSize = graphics->getRayTracingProperties().shaderGroupHandleSize;
const uint32_t handleAlignment = graphics->getRayTracingProperties().shaderGroupBaseAlignment;
const uint32_t handleSizeAligned = align(handleSize, handleAlignment);
const uint32_t handleSizeAligned =
align(graphics->getRayTracingProperties().shaderGroupHandleSize, graphics->getRayTracingProperties().shaderGroupHandleAlignment);
const uint32_t handleAlignment = graphics->getRayTracingProperties().shaderGroupHandleAlignment;
const uint32_t groupCount = static_cast<uint32_t>(shaderGroups.size());
const uint32_t sbtSize = handleSizeAligned * groupCount;
const uint32_t sbtSize = groupCount * handleSizeAligned;
const VkBufferUsageFlags sbtBufferUsage =
VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
const VmaMemoryUsage sbtMemoryUsage = VMA_MEMORY_USAGE_CPU_TO_GPU;
OBufferAllocation rayGenBuffer = new BufferAllocation(graphics, "RayGenSBT",
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = handleSize,
.usage = sbtBufferUsage,
},
VmaAllocationCreateInfo{
.usage = sbtMemoryUsage,
},
Gfx::QueueType::GRAPHICS);
OBufferAllocation hitBuffer = new BufferAllocation(graphics, "HitSBT",
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = handleSize * createInfo.hitGroups.size(),
.usage = sbtBufferUsage,
},
VmaAllocationCreateInfo{
.usage = sbtMemoryUsage,
},
Gfx::QueueType::GRAPHICS);
OBufferAllocation missBuffer = new BufferAllocation(graphics, "MissSBT",
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = handleSize * createInfo.missGroups.size(),
.usage = sbtBufferUsage,
},
VmaAllocationCreateInfo{
.usage = sbtMemoryUsage,
},
Gfx::QueueType::GRAPHICS);
Array<uint8> sbt(sbtSize);
vkGetRayTracingShaderGroupHandlesKHR(graphics->getDevice(), pipelineHandle, 0, shaderGroups.size(), sbtSize, sbt.data());
uint32 rayGenSize = align<uint32>(handleSize + createInfo.rayGenGroup.parameters.size(), handleAlignment);
Array<uint8> rayGenSbt(handleSizeAligned);
std::memcpy(rayGenSbt.data(), sbt.data(), handleSize);
std::memcpy(rayGenSbt.data() + handleSize, createInfo.rayGenGroup.parameters.data(), createInfo.rayGenGroup.parameters.size());
OBufferAllocation rayGenBuffer =
new BufferAllocation(graphics, "RayGenSBT",
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = rayGenSbt.size(),
.usage = VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
},
VmaAllocationCreateInfo{
.usage = VMA_MEMORY_USAGE_AUTO,
},
Gfx::QueueType::GRAPHICS);
rayGenBuffer->updateContents(0, rayGenSbt.size(), rayGenSbt.data());
rayGenBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
uint64 sbtOffset = handleSizeAligned;
uint32 maxParamSize = 0;
for (auto& hitgroup : createInfo.hitGroups) {
maxParamSize = std::max<uint32>(maxParamSize, hitgroup.parameters.size());
}
uint64 hitStride = align(handleSize + maxParamSize, handleAlignment);
Array<uint8> hitSbt(hitStride * createInfo.hitGroups.size());
for (uint64 i = 0; i < createInfo.hitGroups.size(); ++i) {
std::memcpy(hitSbt.data() + i * hitStride, sbt.data() + sbtOffset, handleSize);
std::memcpy(hitSbt.data() + i * hitStride + handleSize, createInfo.hitGroups[i].parameters.data(),
createInfo.hitGroups[i].parameters.size());
sbtOffset += handleSizeAligned;
}
OBufferAllocation hitBuffer =
new BufferAllocation(graphics, "HitSBT",
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = hitSbt.size(),
.usage = VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
},
VmaAllocationCreateInfo{
.usage = VMA_MEMORY_USAGE_AUTO,
},
Gfx::QueueType::GRAPHICS);
hitBuffer->updateContents(0, hitSbt.size(), hitSbt.data());
hitBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
maxParamSize = 0;
for (auto& missGroup : createInfo.missGroups) {
maxParamSize = std::max<uint32>(maxParamSize, missGroup.parameters.size());
}
uint64 missStride = align(handleSize + maxParamSize, handleAlignment);
Array<uint8> missSbt(missStride * createInfo.missGroups.size());
for (uint64 i = 0; i < createInfo.missGroups.size(); ++i) {
std::memcpy(missSbt.data() + i * missStride, sbt.data() + sbtOffset, handleSize);
std::memcpy(missSbt.data() + i * missStride + handleSize, createInfo.missGroups[i].parameters.data(),
createInfo.missGroups[i].parameters.size());
sbtOffset += handleSizeAligned;
}
OBufferAllocation missBuffer =
new BufferAllocation(graphics, "MissSBT",
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = missSbt.size(),
.usage = VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
},
VmaAllocationCreateInfo{
.usage = VMA_MEMORY_USAGE_AUTO,
},
Gfx::QueueType::GRAPHICS);
missBuffer->updateContents(0, missSbt.size(), missSbt.data());
missBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
maxParamSize = 0;
/* maxParamSize = 0;
for (auto& callableGroup : createInfo.callableGroups) {
maxParamSize = std::max<uint32>(maxParamSize, callableGroup.parameters.size());
}
uint64 callableStride = align(handleSize + maxParamSize, handleAlignment);
Array<uint8> callableSbt(callableStride * createInfo.callableGroups.size());
OBufferAllocation callableBuffer = new BufferAllocation(graphics, "CallableSBT",
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = callableStride * createInfo.callableGroups.size(),
.usage = sbtBufferUsage,
},
VmaAllocationCreateInfo{
.usage = sbtMemoryUsage,
},
Gfx::QueueType::GRAPHICS);
uint8* callableData = static_cast<uint8*>(callableBuffer->map());
for (uint64 i = 0; i < createInfo.callableGroups.size(); ++i) {
std::memcpy(callableSbt.data() + i * callableStride, sbt.data() + sbtOffset, handleSize);
std::memcpy(callableSbt.data() + i * callableStride + handleSize, createInfo.callableGroups[i].parameters.data(),
std::memcpy(callableData, sbt.data() + sbtOffset, handleSize);
std::memcpy(callableData + handleSize, createInfo.callableGroups[i].parameters.data(),
createInfo.callableGroups[i].parameters.size());
sbtOffset += handleSizeAligned;
callableData += callableStride;
}
callableBuffer->unmap();
*/
uint64 sbtOffset = 0;
uint8* rayGenData = static_cast<uint8*>(rayGenBuffer->map());
std::memcpy(rayGenData, sbt.data() + sbtOffset, handleSize);
rayGenBuffer->unmap();
sbtOffset += handleSizeAligned;
OBufferAllocation callableBuffer =
new BufferAllocation(graphics, "CallableSBT",
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = callableSbt.size(),
.usage = VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
},
VmaAllocationCreateInfo{
.usage = VMA_MEMORY_USAGE_AUTO,
},
Gfx::QueueType::GRAPHICS);
callableBuffer->updateContents(0, callableSbt.size(), callableSbt.data());
callableBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
uint8* hitData = static_cast<uint8*>(hitBuffer->map());
for (uint64 i = 0; i < createInfo.hitGroups.size(); ++i) {
std::memcpy(hitData, sbt.data() + sbtOffset, handleSize);
sbtOffset += handleSizeAligned;
hitData += handleSizeAligned;
}
hitBuffer->unmap();
uint8* missData = static_cast<uint8*>(missBuffer->map());
for (uint64 i = 0; i < createInfo.missGroups.size(); ++i) {
std::memcpy(missData, sbt.data() + sbtOffset, handleSize);
sbtOffset += handleSizeAligned;
missData += handleSizeAligned;
}
missBuffer->unmap();
ORayTracingPipeline pipeline =
new RayTracingPipeline(graphics, pipelineHandle, std::move(rayGenBuffer), rayGenSize, std::move(hitBuffer), hitStride,
std::move(missBuffer), missStride, std::move(callableBuffer), callableStride, createInfo.pipelineLayout);
new RayTracingPipeline(graphics, pipelineHandle, std::move(rayGenBuffer), handleSizeAligned, std::move(hitBuffer),
handleSizeAligned, std::move(missBuffer), handleSizeAligned, nullptr, 0, createInfo.pipelineLayout);
PRayTracingPipeline handle = pipeline;
rayTracingPipelines[hash] = std::move(pipeline);
return handle;