/* 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 as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version. 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; #include module Crafter.Graphics:RenderingElement3DVulkan_impl; import :RenderingElement3DVulkan; import std; using namespace Crafter; //std::vector 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)); // 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 // }; // } // void RenderingElement3DVulkan::BuildTLAS(VkCommandBuffer cmd) { // VulkanBuffer 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()); // for(std::uint32_t i = 0; i < elements.size(); i++) { // newBuffer.value[i] = elements[i].instance; // } // VkAccelerationStructureGeometryInstancesDataKHR instancesData = VkAccelerationStructureGeometryInstancesDataKHR { // .arrayOfPointers = VK_FALSE, // .data = {newBuffer.address} // }; // vk::AccelerationStructureGeometryDataKHR geometryData(instancesData); // vk::AccelerationStructureGeometryKHR tlasGeometry{ // .geometryType = vk::GeometryTypeKHR::eInstances, // .geometry = geometryData}; // vk::AccelerationStructureBuildGeometryInfoKHR tlasBuildGeometryInfo{ // .type = vk::AccelerationStructureTypeKHR::eTopLevel, // .mode = vk::BuildAccelerationStructureModeKHR::eBuild, // .geometryCount = 1, // .pGeometries = &tlasGeometry}; // // Query the memory sizes that will be needed for this TLAS // auto primitiveCount = static_cast(instances.size()); // vk::AccelerationStructureBuildSizesInfoKHR tlasBuildSizes = // device.getAccelerationStructureBuildSizesKHR( // vk::AccelerationStructureBuildTypeKHR::eDevice, // tlasBuildGeometryInfo, // {primitiveCount}); // // 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); // // 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; // // 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 // vk::AccelerationStructureCreateInfoKHR tlasCreateInfo{ // .buffer = tlasBuffer, // .offset = 0, // .size = tlasBuildSizes.accelerationStructureSize, // .type = vk::AccelerationStructureTypeKHR::eTopLevel, // }; // tlas = device.createAccelerationStructureKHR(tlasCreateInfo); // // 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}); //}