improved vulkanbuffer
This commit is contained in:
parent
819517d150
commit
2e11ac6484
11 changed files with 396 additions and 762 deletions
|
|
@ -33,85 +33,14 @@ constexpr std::size_t alignUp(std::size_t value, std::size_t alignment) {
|
|||
}
|
||||
|
||||
void Mesh::Build(std::span<Vertex> verticies, std::span<std::uint32_t> indicies, VkCommandBuffer cmd) {
|
||||
new (&vertexStaging) VulkanBuffer<Vertex>(VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, verticies.size());
|
||||
new (&indexStaging) VulkanBuffer<std::uint32_t>(VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, indicies.size());
|
||||
vertexBuffer.Resize(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_2_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, verticies.size());
|
||||
indexBuffer.Resize(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_2_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, indicies.size());
|
||||
|
||||
std::memcpy(vertexStaging.value, verticies.data(), verticies.size() * sizeof(Vertex));
|
||||
std::memcpy(indexStaging.value, indicies.data(), indicies.size() * sizeof(std::uint32_t));
|
||||
|
||||
new (&vertexBuffer) VulkanBuffer<Vertex>(VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_2_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, verticies.size());
|
||||
new (&indexBuffer) VulkanBuffer<std::uint32_t>(VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_2_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indicies.size());
|
||||
|
||||
VkBufferCopy copyRegion = {
|
||||
.srcOffset = 0,
|
||||
.dstOffset = 0,
|
||||
.size = verticies.size() * sizeof(Vertex)
|
||||
};
|
||||
|
||||
vkCmdCopyBuffer(
|
||||
cmd,
|
||||
vertexStaging.buffer,
|
||||
vertexBuffer.buffer,
|
||||
1,
|
||||
©Region
|
||||
);
|
||||
|
||||
VkBufferCopy copyRegion2 = {
|
||||
.srcOffset = 0,
|
||||
.dstOffset = 0,
|
||||
.size = indicies.size() * sizeof(std::uint32_t)
|
||||
};
|
||||
|
||||
vkCmdCopyBuffer(
|
||||
cmd,
|
||||
indexStaging.buffer,
|
||||
indexBuffer.buffer,
|
||||
1,
|
||||
©Region2
|
||||
);
|
||||
|
||||
VkBufferMemoryBarrier barrier = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
|
||||
.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.buffer = vertexBuffer.buffer,
|
||||
.offset = 0,
|
||||
.size = VK_WHOLE_SIZE
|
||||
};
|
||||
|
||||
vkCmdPipelineBarrier(
|
||||
cmd,
|
||||
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
|
||||
0,
|
||||
0, NULL,
|
||||
1, &barrier,
|
||||
0, NULL
|
||||
);
|
||||
|
||||
VkBufferMemoryBarrier barrier2 = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
|
||||
.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.buffer = indexStaging.buffer,
|
||||
.offset = 0,
|
||||
.size = VK_WHOLE_SIZE
|
||||
};
|
||||
|
||||
vkCmdPipelineBarrier(
|
||||
cmd,
|
||||
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR,
|
||||
0,
|
||||
0, NULL,
|
||||
1, &barrier2,
|
||||
0, NULL
|
||||
);
|
||||
std::memcpy(vertexBuffer.value, verticies.data(), verticies.size() * sizeof(Vertex));
|
||||
std::memcpy(indexBuffer.value, indicies.data(), indicies.size() * sizeof(std::uint32_t));
|
||||
|
||||
vertexBuffer.FlushDevice(cmd, VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR);
|
||||
vertexBuffer.FlushDevice(cmd, VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR);
|
||||
|
||||
VkDeviceOrHostAddressConstKHR vertexAddr;
|
||||
vertexAddr.deviceAddress = vertexBuffer.address;
|
||||
|
|
@ -160,7 +89,7 @@ void Mesh::Build(std::span<Vertex> verticies, std::span<std::uint32_t> indicies,
|
|||
&blasBuildSizes
|
||||
);
|
||||
|
||||
new (&scratchBuffer) VulkanBuffer<char>(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, blasBuildSizes.buildScratchSize);
|
||||
scratchBuffer.Resize(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, blasBuildSizes.buildScratchSize);
|
||||
|
||||
VkPhysicalDeviceAccelerationStructurePropertiesKHR asProps{
|
||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_PROPERTIES_KHR
|
||||
|
|
@ -169,7 +98,7 @@ void Mesh::Build(std::span<Vertex> verticies, std::span<std::uint32_t> indicies,
|
|||
VkDeviceAddress scratchAddr = scratchBuffer.address;
|
||||
blasBuildGeometryInfo.scratchData.deviceAddress = scratchAddr;
|
||||
|
||||
new (&blasBuffer) VulkanBuffer<char>(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, blasBuildSizes.accelerationStructureSize);
|
||||
blasBuffer.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, blasBuildSizes.accelerationStructureSize);
|
||||
|
||||
// Create and store the BLAS handle
|
||||
VkAccelerationStructureCreateInfoKHR blasCreateInfo{
|
||||
|
|
@ -180,10 +109,9 @@ void Mesh::Build(std::span<Vertex> verticies, std::span<std::uint32_t> indicies,
|
|||
.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR,
|
||||
};
|
||||
|
||||
VkAccelerationStructureKHR aclereationStructure {};
|
||||
|
||||
VulkanDevice::vkCreateAccelerationStructureKHR(VulkanDevice::device, &blasCreateInfo, nullptr, &aclereationStructure);
|
||||
blasBuildGeometryInfo.dstAccelerationStructure = aclereationStructure;
|
||||
VulkanDevice::CheckVkResult(VulkanDevice::vkCreateAccelerationStructureKHR(VulkanDevice::device, &blasCreateInfo, nullptr, &accelerationStructure));
|
||||
blasBuildGeometryInfo.dstAccelerationStructure = accelerationStructure;
|
||||
|
||||
// Prepare the build range for the BLAS
|
||||
VkAccelerationStructureBuildRangeInfoKHR blasRangeInfo {
|
||||
|
|
|
|||
130
implementations/Crafter.Graphics-RenderingElement3DVulkan.cpp
Normal file
130
implementations/Crafter.Graphics-RenderingElement3DVulkan.cpp
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
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 <vulkan/vulkan_core.h>
|
||||
module Crafter.Graphics:RenderingElement3DVulkan_impl;
|
||||
import :RenderingElement3DVulkan;
|
||||
import std;
|
||||
|
||||
using namespace Crafter;
|
||||
|
||||
//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));
|
||||
|
||||
// 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<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());
|
||||
|
||||
// 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<uint32_t>(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});
|
||||
//}
|
||||
|
|
@ -299,6 +299,7 @@ void VulkanDevice::CreateDevice() {
|
|||
vkGetAccelerationStructureBuildSizesKHR = reinterpret_cast<PFN_vkGetAccelerationStructureBuildSizesKHR>(vkGetInstanceProcAddr(instance, "vkGetAccelerationStructureBuildSizesKHR"));
|
||||
vkCreateAccelerationStructureKHR = reinterpret_cast<PFN_vkCreateAccelerationStructureKHR>(vkGetInstanceProcAddr(instance, "vkCreateAccelerationStructureKHR"));
|
||||
vkCmdBuildAccelerationStructuresKHR = reinterpret_cast<PFN_vkCmdBuildAccelerationStructuresKHR>(vkGetInstanceProcAddr(instance, "vkCmdBuildAccelerationStructuresKHR"));
|
||||
vkGetAccelerationStructureDeviceAddressKHR = reinterpret_cast<PFN_vkGetAccelerationStructureDeviceAddressKHR>(vkGetInstanceProcAddr(instance, "vkGetAccelerationStructureDeviceAddressKHR"));
|
||||
}
|
||||
|
||||
std::uint32_t VulkanDevice::GetMemoryType(uint32_t typeBits, VkMemoryPropertyFlags properties) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue