2024-06-09 10:44:24 +02:00
|
|
|
#include "RayTracing.h"
|
|
|
|
|
#include "Buffer.h"
|
2024-06-13 15:43:03 +02:00
|
|
|
#include "Command.h"
|
|
|
|
|
#include "Enums.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "Graphics/Buffer.h"
|
2024-06-13 15:43:03 +02:00
|
|
|
#include "Graphics/Enums.h"
|
|
|
|
|
#include "Graphics/Graphics.h"
|
2024-06-09 10:44:24 +02:00
|
|
|
#include "Graphics/Initializer.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "Graphics/Mesh.h"
|
|
|
|
|
#include "Graphics/VertexData.h"
|
2024-06-09 10:44:24 +02:00
|
|
|
#include <vulkan/vulkan_core.h>
|
|
|
|
|
|
|
|
|
|
using namespace Seele::Vulkan;
|
|
|
|
|
|
2024-07-08 13:46:49 +02:00
|
|
|
BottomLevelAS::BottomLevelAS(PGraphics graphics, const Gfx::BottomLevelASCreateInfo& createInfo)
|
|
|
|
|
: graphics(graphics), material(createInfo.mesh->referencedMaterial->getHandle()) {
|
2024-07-12 13:33:52 +02:00
|
|
|
matrix = {
|
|
|
|
|
// createInfo.mesh->transform[0][0], createInfo.mesh->transform[0][1], createInfo.mesh->transform[0][2],
|
|
|
|
|
// createInfo.mesh->transform[0][3], createInfo.mesh->transform[1][0], createInfo.mesh->transform[1][1],
|
|
|
|
|
// createInfo.mesh->transform[1][2], createInfo.mesh->transform[1][3], createInfo.mesh->transform[2][0],
|
|
|
|
|
// createInfo.mesh->transform[2][1], createInfo.mesh->transform[2][2], createInfo.mesh->transform[2][3],
|
|
|
|
|
1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
|
};
|
2024-06-09 12:20:04 +02:00
|
|
|
VertexData* vertexData = createInfo.mesh->vertexData;
|
2024-06-13 15:43:03 +02:00
|
|
|
MeshData meshData = vertexData->getMeshData(createInfo.mesh->id);
|
2024-07-12 13:33:52 +02:00
|
|
|
vertexOffset = vertexData->getMeshOffset(createInfo.mesh->id) * sizeof(Vector4);
|
2024-07-15 08:32:50 +02:00
|
|
|
vertexCount = vertexData->getMeshVertexCount(createInfo.mesh->id);
|
2024-07-12 13:33:52 +02:00
|
|
|
indexOffset = meshData.firstIndex * sizeof(uint32);
|
|
|
|
|
primitiveCount = meshData.numIndices / 3;
|
2024-07-08 13:46:49 +02:00
|
|
|
|
2024-06-18 23:33:03 +02:00
|
|
|
// todo: compact
|
2024-06-09 10:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-14 22:12:26 +02:00
|
|
|
BottomLevelAS::~BottomLevelAS() { graphics->getDestructionManager()->queueResourceForDestruction(std::move(buffer)); }
|
2024-06-09 10:44:24 +02:00
|
|
|
|
2024-07-12 13:33:52 +02:00
|
|
|
TopLevelAS::TopLevelAS(PGraphics graphics, const Gfx::TopLevelASCreateInfo& createInfo) : graphics(graphics) {
|
2024-07-10 21:07:10 +02:00
|
|
|
Array<VkAccelerationStructureInstanceKHR> instances(createInfo.instances.size());
|
|
|
|
|
for (uint32 i = 0; i < instances.size(); ++i) {
|
|
|
|
|
auto blas = createInfo.bottomLevelStructures[i].cast<BottomLevelAS>();
|
|
|
|
|
|
|
|
|
|
instances[i] = VkAccelerationStructureInstanceKHR{
|
|
|
|
|
.transform =
|
|
|
|
|
VkTransformMatrixKHR{
|
|
|
|
|
createInfo.instances[i].transformMatrix[0][0],
|
|
|
|
|
createInfo.instances[i].transformMatrix[1][0],
|
|
|
|
|
createInfo.instances[i].transformMatrix[2][0],
|
|
|
|
|
createInfo.instances[i].transformMatrix[3][0],
|
|
|
|
|
createInfo.instances[i].transformMatrix[0][1],
|
|
|
|
|
createInfo.instances[i].transformMatrix[1][1],
|
|
|
|
|
createInfo.instances[i].transformMatrix[2][1],
|
|
|
|
|
createInfo.instances[i].transformMatrix[3][1],
|
|
|
|
|
createInfo.instances[i].transformMatrix[0][2],
|
|
|
|
|
createInfo.instances[i].transformMatrix[1][2],
|
|
|
|
|
createInfo.instances[i].transformMatrix[2][2],
|
|
|
|
|
createInfo.instances[i].transformMatrix[3][2],
|
|
|
|
|
},
|
2024-07-13 09:25:42 +02:00
|
|
|
.instanceCustomIndex = i,
|
2024-07-10 21:07:10 +02:00
|
|
|
.mask = 0xff,
|
2024-07-15 17:55:22 +02:00
|
|
|
.instanceShaderBindingTableRecordOffset = i,
|
2024-07-10 21:07:10 +02:00
|
|
|
.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR,
|
|
|
|
|
.accelerationStructureReference = blas->getDeviceAddress(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 13:33:52 +02:00
|
|
|
instanceAllocation = new BufferAllocation(graphics, "ASInstances",
|
|
|
|
|
VkBufferCreateInfo{
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.flags = 0,
|
|
|
|
|
.size = sizeof(VkAccelerationStructureInstanceKHR) * instances.size(),
|
|
|
|
|
.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR |
|
|
|
|
|
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
|
|
|
|
},
|
|
|
|
|
VmaAllocationCreateInfo{
|
|
|
|
|
.usage = VMA_MEMORY_USAGE_AUTO,
|
|
|
|
|
},
|
|
|
|
|
Gfx::QueueType::GRAPHICS);
|
|
|
|
|
instanceAllocation->updateContents(0, sizeof(VkAccelerationStructureInstanceKHR) * instances.size(), instances.data());
|
2024-07-10 21:07:10 +02:00
|
|
|
VkDeviceOrHostAddressConstKHR instanceDeviceAddress = {
|
|
|
|
|
.deviceAddress = instanceAllocation->deviceAddress,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
VkAccelerationStructureGeometryKHR geometry = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR,
|
|
|
|
|
.pNext = nullptr,
|
2024-07-12 13:33:52 +02:00
|
|
|
.geometryType = VK_GEOMETRY_TYPE_INSTANCES_KHR,
|
2024-07-12 22:05:52 +02:00
|
|
|
.geometry =
|
|
|
|
|
{
|
|
|
|
|
.instances =
|
|
|
|
|
{
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_INSTANCES_DATA_KHR,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.arrayOfPointers = VK_FALSE,
|
|
|
|
|
.data = instanceDeviceAddress,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-07-10 21:07:10 +02:00
|
|
|
.flags = VK_GEOMETRY_OPAQUE_BIT_KHR,
|
|
|
|
|
};
|
|
|
|
|
VkAccelerationStructureBuildGeometryInfoKHR structureBuildGeometry = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR,
|
|
|
|
|
.pNext = nullptr,
|
2024-07-12 13:33:52 +02:00
|
|
|
.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR,
|
2024-07-10 21:07:10 +02:00
|
|
|
.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR,
|
|
|
|
|
.geometryCount = 1,
|
|
|
|
|
.pGeometries = &geometry,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const uint32 primitiveCount = instances.size();
|
|
|
|
|
|
|
|
|
|
VkAccelerationStructureBuildSizesInfoKHR buildSizesInfo = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
};
|
|
|
|
|
vkGetAccelerationStructureBuildSizesKHR(graphics->getDevice(), VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR, &structureBuildGeometry,
|
|
|
|
|
&primitiveCount, &buildSizesInfo);
|
|
|
|
|
|
|
|
|
|
buffer = new BufferAllocation(
|
|
|
|
|
graphics, "TLAS",
|
|
|
|
|
VkBufferCreateInfo{
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.flags = 0,
|
|
|
|
|
.size = buildSizesInfo.accelerationStructureSize,
|
|
|
|
|
.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
VmaAllocationCreateInfo{
|
|
|
|
|
.usage = VMA_MEMORY_USAGE_AUTO,
|
|
|
|
|
},
|
|
|
|
|
Gfx::QueueType::GRAPHICS);
|
|
|
|
|
|
|
|
|
|
VkAccelerationStructureCreateInfoKHR accelerationInfo = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.buffer = buffer->buffer,
|
|
|
|
|
.size = buildSizesInfo.accelerationStructureSize,
|
|
|
|
|
.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR,
|
|
|
|
|
};
|
|
|
|
|
VK_CHECK(vkCreateAccelerationStructureKHR(graphics->getDevice(), &accelerationInfo, nullptr, &handle));
|
|
|
|
|
|
|
|
|
|
OBufferAllocation scratchBuffer =
|
|
|
|
|
new BufferAllocation(graphics, "ScratchBuffer",
|
|
|
|
|
VkBufferCreateInfo{
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.flags = 0,
|
|
|
|
|
.size = buildSizesInfo.buildScratchSize,
|
|
|
|
|
.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
VmaAllocationCreateInfo{
|
|
|
|
|
.usage = VMA_MEMORY_USAGE_AUTO,
|
|
|
|
|
},
|
|
|
|
|
Gfx::QueueType::GRAPHICS);
|
|
|
|
|
|
|
|
|
|
VkAccelerationStructureBuildGeometryInfoKHR buildGeometry = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR,
|
|
|
|
|
.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR,
|
|
|
|
|
.dstAccelerationStructure = handle,
|
|
|
|
|
.geometryCount = 1,
|
|
|
|
|
.pGeometries = &geometry,
|
|
|
|
|
.scratchData =
|
|
|
|
|
{
|
|
|
|
|
.deviceAddress = scratchBuffer->deviceAddress,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
VkAccelerationStructureBuildRangeInfoKHR buildRange = {
|
|
|
|
|
.primitiveCount = uint32(instances.size()),
|
|
|
|
|
.primitiveOffset = 0,
|
|
|
|
|
.firstVertex = 0,
|
|
|
|
|
.transformOffset = 0,
|
|
|
|
|
};
|
|
|
|
|
VkAccelerationStructureBuildRangeInfoKHR* buildRangeInfos[] = {&buildRange};
|
|
|
|
|
|
|
|
|
|
auto cmd = graphics->getGraphicsCommands()->getCommands();
|
2024-07-12 22:05:52 +02:00
|
|
|
|
2024-07-10 21:07:10 +02:00
|
|
|
vkCmdBuildAccelerationStructuresKHR(cmd->getHandle(), 1, &buildGeometry, buildRangeInfos);
|
2024-07-12 22:05:52 +02:00
|
|
|
VkBufferMemoryBarrier barrier = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR,
|
|
|
|
|
.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR,
|
|
|
|
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
|
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
|
.buffer = buffer->buffer,
|
|
|
|
|
.offset = 0,
|
|
|
|
|
.size = buffer->size,
|
|
|
|
|
};
|
|
|
|
|
vkCmdPipelineBarrier(cmd->getHandle(), VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR, 0, 0,
|
|
|
|
|
nullptr, 1, &barrier, 0, nullptr);
|
2024-07-10 21:07:10 +02:00
|
|
|
scratchBuffer->bind();
|
2024-07-12 13:33:52 +02:00
|
|
|
cmd->bindResource(PBufferAllocation(scratchBuffer));
|
2024-07-10 21:07:10 +02:00
|
|
|
graphics->getDestructionManager()->queueResourceForDestruction(std::move(scratchBuffer));
|
2024-07-12 13:33:52 +02:00
|
|
|
|
|
|
|
|
buffer->bind();
|
|
|
|
|
cmd->bindResource(PBufferAllocation(buffer));
|
|
|
|
|
|
|
|
|
|
instanceAllocation->bind();
|
|
|
|
|
cmd->bindResource(PBufferAllocation(instanceAllocation));
|
2024-07-10 21:07:10 +02:00
|
|
|
}
|
2024-06-09 10:44:24 +02:00
|
|
|
|
2024-07-12 13:33:52 +02:00
|
|
|
TopLevelAS::~TopLevelAS() {
|
|
|
|
|
graphics->getDestructionManager()->queueResourceForDestruction(std::move(buffer));
|
|
|
|
|
graphics->getDestructionManager()->queueResourceForDestruction(std::move(instanceAllocation));
|
|
|
|
|
}
|
2024-06-18 23:33:03 +02:00
|
|
|
|
2024-07-10 21:07:10 +02:00
|
|
|
RayTracingPipeline::RayTracingPipeline(PGraphics graphics, VkPipeline handle, OBufferAllocation rayGen, uint64 rayGenStride,
|
|
|
|
|
OBufferAllocation hit, uint64 hitStride, OBufferAllocation miss, uint64 missStride,
|
2024-07-12 13:33:52 +02:00
|
|
|
OBufferAllocation callable, uint64 callableStride, Gfx::PPipelineLayout layout)
|
2024-07-10 21:07:10 +02:00
|
|
|
: Gfx::RayTracingPipeline(layout), graphics(graphics), pipeline(handle), rayGen(std::move(rayGen)), rayGenStride(rayGenStride),
|
2024-07-12 13:33:52 +02:00
|
|
|
hit(std::move(hit)), hitStride(hitStride), miss(std::move(miss)), missStride(missStride), callable(std::move(callable)),
|
|
|
|
|
callableStride(callableStride) {}
|
2024-06-18 23:33:03 +02:00
|
|
|
|
2024-07-12 13:33:52 +02:00
|
|
|
RayTracingPipeline::~RayTracingPipeline() {
|
|
|
|
|
graphics->getDestructionManager()->queueResourceForDestruction(std::move(rayGen));
|
|
|
|
|
graphics->getDestructionManager()->queueResourceForDestruction(std::move(hit));
|
|
|
|
|
graphics->getDestructionManager()->queueResourceForDestruction(std::move(miss));
|
|
|
|
|
graphics->getDestructionManager()->queueResourceForDestruction(std::move(callable));
|
|
|
|
|
}
|
2024-07-10 21:07:10 +02:00
|
|
|
|
2024-07-16 15:32:51 +02:00
|
|
|
void RayTracingPipeline::bind(VkCommandBuffer handle) {
|
|
|
|
|
vkCmdBindPipeline(handle, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, pipeline);
|
|
|
|
|
rayGen->bind();
|
|
|
|
|
hit->bind();
|
|
|
|
|
miss->bind();
|
|
|
|
|
}
|