TLAS
This commit is contained in:
parent
2e11ac6484
commit
e08d5f104a
3 changed files with 108 additions and 112 deletions
|
|
@ -23,8 +23,8 @@ int main() {
|
|||
std::array<Vertex, 3> verts {{{-0.1, 0, 0}, {0, 0.1, 0}, {0.1, 0, 0}}};
|
||||
std::array<std::uint32_t, 3> index {{0,1,2}};
|
||||
triangleMesh.Build(verts, index, cmd);
|
||||
// RenderingElement3DVulkan::elements.emplace_back(triangleMesh);
|
||||
// RenderingElement3DVulkan::BuildTLAS(cmd);
|
||||
RenderingElement3DVulkan::elements.emplace_back(triangleMesh);
|
||||
RenderingElement3DVulkan::BuildTLAS(cmd);
|
||||
|
||||
/*
|
||||
FinishInit executes all commands recorded to StartInit.
|
||||
|
|
|
|||
|
|
@ -25,106 +25,99 @@ import std;
|
|||
|
||||
using namespace Crafter;
|
||||
|
||||
//std::vector<RenderingElement3DVulkan> RenderingElement3DVulkan::elements;
|
||||
std::vector<RenderingElement3DVulkan> RenderingElement3DVulkan::elements;
|
||||
|
||||
//RenderingElement3DVulkan::RenderingElement3DVulkan(Mesh& mesh) {
|
||||
// VkTransformMatrixKHR identity{};
|
||||
// float tmp[3][4] = {
|
||||
// {1.f, 0.f, 0.f, 0.f},
|
||||
// {0.f, 1.f, 0.f, 0.f},
|
||||
// {0.f, 0.f, 1.f, 0.f}
|
||||
// };
|
||||
// std::memcpy(identity.matrix, tmp, sizeof(tmp));
|
||||
RenderingElement3DVulkan::RenderingElement3DVulkan(Mesh& mesh) {
|
||||
VkTransformMatrixKHR identity{};
|
||||
float tmp[3][4] = {
|
||||
{1.f, 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f, 0.f, 0.f},
|
||||
{0.f, 0.f, 1.f, 0.f}
|
||||
};
|
||||
std::memcpy(identity.matrix, tmp, sizeof(tmp));
|
||||
|
||||
// VkAccelerationStructureDeviceAddressInfoKHR addrInfo {
|
||||
// .sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR,
|
||||
// .accelerationStructure = mesh.accelerationStructure
|
||||
// };
|
||||
// VkDeviceAddress blasDeviceAddr = VulkanDevice::vkGetAccelerationStructureDeviceAddressKHR(VulkanDevice::device, &addrInfo);
|
||||
VkAccelerationStructureDeviceAddressInfoKHR addrInfo {
|
||||
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR,
|
||||
.accelerationStructure = mesh.accelerationStructure
|
||||
};
|
||||
VkDeviceAddress blasDeviceAddr = VulkanDevice::vkGetAccelerationStructureDeviceAddressKHR(VulkanDevice::device, &addrInfo);
|
||||
|
||||
// instance = {
|
||||
// .transform = identity,
|
||||
// .mask = 0xFF,
|
||||
// .accelerationStructureReference = blasDeviceAddr
|
||||
// };
|
||||
// }
|
||||
instance = {
|
||||
.transform = identity,
|
||||
.mask = 0xFF,
|
||||
.accelerationStructureReference = blasDeviceAddr
|
||||
};
|
||||
}
|
||||
|
||||
// void RenderingElement3DVulkan::BuildTLAS(VkCommandBuffer cmd) {
|
||||
// VulkanBuffer<VkAccelerationStructureInstanceKHR> newBuffer(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_2_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, elements.size());
|
||||
void RenderingElement3DVulkan::BuildTLAS(VkCommandBuffer cmd) {
|
||||
instanceBuffer.Resize(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, elements.size());
|
||||
|
||||
// for(std::uint32_t i = 0; i < elements.size(); i++) {
|
||||
// newBuffer.value[i] = elements[i].instance;
|
||||
// }
|
||||
for(std::uint32_t i = 0; i < elements.size(); i++) {
|
||||
instanceBuffer.value[i] = elements[i].instance;
|
||||
}
|
||||
|
||||
// VkAccelerationStructureGeometryInstancesDataKHR instancesData = VkAccelerationStructureGeometryInstancesDataKHR {
|
||||
// .arrayOfPointers = VK_FALSE,
|
||||
// .data = {newBuffer.address}
|
||||
// };
|
||||
VkAccelerationStructureGeometryInstancesDataKHR instancesData = VkAccelerationStructureGeometryInstancesDataKHR {
|
||||
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_INSTANCES_DATA_KHR,
|
||||
.arrayOfPointers = VK_FALSE,
|
||||
.data = {instanceBuffer.address}
|
||||
};
|
||||
|
||||
// vk::AccelerationStructureGeometryDataKHR geometryData(instancesData);
|
||||
VkAccelerationStructureGeometryDataKHR geometryData;
|
||||
geometryData.instances = instancesData;
|
||||
|
||||
// vk::AccelerationStructureGeometryKHR tlasGeometry{
|
||||
// .geometryType = vk::GeometryTypeKHR::eInstances,
|
||||
// .geometry = geometryData};
|
||||
VkAccelerationStructureGeometryKHR tlasGeometry {
|
||||
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR,
|
||||
.geometryType = VK_GEOMETRY_TYPE_INSTANCES_KHR,
|
||||
.geometry = geometryData
|
||||
};
|
||||
|
||||
// vk::AccelerationStructureBuildGeometryInfoKHR tlasBuildGeometryInfo{
|
||||
// .type = vk::AccelerationStructureTypeKHR::eTopLevel,
|
||||
// .mode = vk::BuildAccelerationStructureModeKHR::eBuild,
|
||||
// .geometryCount = 1,
|
||||
// .pGeometries = &tlasGeometry};
|
||||
VkAccelerationStructureBuildGeometryInfoKHR tlasBuildGeometryInfo{
|
||||
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR,
|
||||
.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR,
|
||||
.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR,
|
||||
.geometryCount = 1,
|
||||
.pGeometries = &tlasGeometry
|
||||
};
|
||||
|
||||
// // Query the memory sizes that will be needed for this TLAS
|
||||
// auto primitiveCount = static_cast<uint32_t>(instances.size());
|
||||
// Query the memory sizes that will be needed for this TLAS
|
||||
auto primitiveCount = static_cast<uint32_t>(elements.size());
|
||||
|
||||
// vk::AccelerationStructureBuildSizesInfoKHR tlasBuildSizes =
|
||||
// device.getAccelerationStructureBuildSizesKHR(
|
||||
// vk::AccelerationStructureBuildTypeKHR::eDevice,
|
||||
// tlasBuildGeometryInfo,
|
||||
// {primitiveCount});
|
||||
VkAccelerationStructureBuildSizesInfoKHR tlasBuildSizes {
|
||||
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR
|
||||
};
|
||||
VulkanDevice::vkGetAccelerationStructureBuildSizesKHR(
|
||||
VulkanDevice::device,
|
||||
VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
|
||||
&tlasBuildGeometryInfo,
|
||||
&primitiveCount,
|
||||
&tlasBuildSizes
|
||||
);
|
||||
|
||||
// // Create a scratch buffer for the TLAS, this will hold temporary data
|
||||
// // during the build process
|
||||
// createBuffer(
|
||||
// tlasBuildSizes.buildScratchSize,
|
||||
// vk::BufferUsageFlagBits::eStorageBuffer |
|
||||
// vk::BufferUsageFlagBits::eShaderDeviceAddress,
|
||||
// vk::MemoryPropertyFlagBits::eDeviceLocal,
|
||||
// tlasScratchBuffer, tlasScratchMemory);
|
||||
scratchBuffer.Resize(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, tlasBuildSizes.buildScratchSize);
|
||||
tlasBuildGeometryInfo.scratchData.deviceAddress = scratchBuffer.address;
|
||||
|
||||
// // Save the scratch buffer address in the build info structure
|
||||
// vk::BufferDeviceAddressInfo scratchAddressInfo{.buffer = *tlasScratchBuffer};
|
||||
// vk::DeviceAddress scratchAddr = device.getBufferAddressKHR(scratchAddressInfo);
|
||||
// tlasBuildGeometryInfo.scratchData.deviceAddress = scratchAddr;
|
||||
tlasBuffer.Resize(VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, tlasBuildSizes.accelerationStructureSize);
|
||||
|
||||
// // Create a buffer for the TLAS itself now that we now the required size
|
||||
// createBuffer(
|
||||
// tlasBuildSizes.accelerationStructureSize,
|
||||
// vk::BufferUsageFlagBits::eAccelerationStructureStorageKHR |
|
||||
// vk::BufferUsageFlagBits::eShaderDeviceAddress |
|
||||
// vk::BufferUsageFlagBits::eAccelerationStructureBuildInputReadOnlyKHR,
|
||||
// vk::MemoryPropertyFlagBits::eDeviceLocal,
|
||||
// tlasBuffer, tlasMemory);
|
||||
// Create and store the TLAS handle
|
||||
VkAccelerationStructureCreateInfoKHR tlasCreateInfo {
|
||||
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR,
|
||||
.buffer = tlasBuffer.buffer,
|
||||
.offset = 0,
|
||||
.size = tlasBuildSizes.accelerationStructureSize,
|
||||
.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR,
|
||||
};
|
||||
|
||||
// // Create and store the TLAS handle
|
||||
// vk::AccelerationStructureCreateInfoKHR tlasCreateInfo{
|
||||
// .buffer = tlasBuffer,
|
||||
// .offset = 0,
|
||||
// .size = tlasBuildSizes.accelerationStructureSize,
|
||||
// .type = vk::AccelerationStructureTypeKHR::eTopLevel,
|
||||
// };
|
||||
VulkanDevice::CheckVkResult(VulkanDevice::vkCreateAccelerationStructureKHR(VulkanDevice::device, &tlasCreateInfo, nullptr, &accelerationStructure));
|
||||
tlasBuildGeometryInfo.dstAccelerationStructure = accelerationStructure;
|
||||
|
||||
// tlas = device.createAccelerationStructureKHR(tlasCreateInfo);
|
||||
// Prepare the build range for the TLAS
|
||||
VkAccelerationStructureBuildRangeInfoKHR tlasRangeInfo {
|
||||
.primitiveCount = primitiveCount,
|
||||
.primitiveOffset = 0,
|
||||
.firstVertex = 0,
|
||||
.transformOffset = 0
|
||||
};
|
||||
|
||||
// // Save the TLAS handle in the build info structure
|
||||
// tlasBuildGeometryInfo.dstAccelerationStructure = tlas;
|
||||
|
||||
// // Prepare the build range for the TLAS
|
||||
// vk::AccelerationStructureBuildRangeInfoKHR tlasRangeInfo{
|
||||
// .primitiveCount = primitiveCount,
|
||||
// .primitiveOffset = 0,
|
||||
// .firstVertex = 0,
|
||||
// .transformOffset = 0};
|
||||
|
||||
|
||||
// cmd->buildAccelerationStructuresKHR({tlasBuildGeometryInfo}, {&tlasRangeInfo});
|
||||
//}
|
||||
VkAccelerationStructureBuildRangeInfoKHR* tlasRangeInfoPP = &tlasRangeInfo;
|
||||
VulkanDevice::vkCmdBuildAccelerationStructuresKHR(cmd, 1, &tlasBuildGeometryInfo, &tlasRangeInfoPP);
|
||||
}
|
||||
|
|
@ -29,13 +29,16 @@ import :VulkanBuffer;
|
|||
|
||||
export namespace Crafter {
|
||||
class RenderingElement3DVulkan {
|
||||
// public:
|
||||
// Mesh* mesh;
|
||||
// VkAccelerationStructureInstanceKHR instance;
|
||||
// static std::vector<RenderingElement3DVulkan> elements;
|
||||
// inline static VulkanBuffer<VkAccelerationStructureInstanceKHR> instanceBuffer;
|
||||
// RenderingElement3DVulkan(Mesh& mesh);
|
||||
// static void BuildTLAS(VkCommandBuffer cmd);
|
||||
public:
|
||||
Mesh* mesh;
|
||||
VkAccelerationStructureInstanceKHR instance;
|
||||
static std::vector<RenderingElement3DVulkan> elements;
|
||||
inline static VulkanBuffer<VkAccelerationStructureInstanceKHR, true, true, false> instanceBuffer;
|
||||
inline static VulkanBuffer<char, false, true, false> scratchBuffer;
|
||||
inline static VulkanBuffer<char, false, true, false> tlasBuffer;
|
||||
inline static VkAccelerationStructureKHR accelerationStructure;
|
||||
RenderingElement3DVulkan(Mesh& mesh);
|
||||
static void BuildTLAS(VkCommandBuffer cmd);
|
||||
};
|
||||
}
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue