mirror of
https://github.com/ZDoom/ZDRay.git
synced 2024-11-10 14:51:40 +00:00
Build the bottom level acceleration structure using the KHR raytracing extension
This commit is contained in:
parent
a294ca216a
commit
0065d91f44
5 changed files with 158 additions and 77 deletions
|
@ -31,26 +31,16 @@ void GPURaytracer::Raytrace(LevelMesh* level)
|
|||
{
|
||||
mesh = level;
|
||||
|
||||
std::unique_ptr<VulkanCommandPool> cmdpool = std::make_unique<VulkanCommandPool>(device.get(), device->graphicsFamily);
|
||||
std::unique_ptr<VulkanCommandBuffer> cmdbuffer = cmdpool->createBuffer();
|
||||
|
||||
printf("Creating vertex and index buffers\n");
|
||||
|
||||
size_t vertexbuffersize = (size_t)mesh->MeshVertices.Size() * sizeof(Vec3);
|
||||
size_t indexbuffersize = (size_t)mesh->MeshElements.Size() * sizeof(uint32_t);
|
||||
size_t aabbbuffersize = sizeof(Vec3) * 2;
|
||||
size_t transferbuffersize = vertexbuffersize + indexbuffersize + aabbbuffersize;
|
||||
size_t transferbuffersize = vertexbuffersize + indexbuffersize;
|
||||
size_t vertexoffset = 0;
|
||||
size_t indexoffset = vertexoffset + vertexbuffersize;
|
||||
size_t aabboffset = indexoffset + indexbuffersize;
|
||||
|
||||
Vec3 aabbs[2] = { mesh->MeshVertices[0], mesh->MeshVertices[1] };
|
||||
for (Vec3& v : mesh->MeshVertices)
|
||||
{
|
||||
aabbs[0].x = std::min(aabbs[0].x, v.x);
|
||||
aabbs[0].y = std::min(aabbs[0].y, v.y);
|
||||
aabbs[0].z = std::min(aabbs[0].z, v.z);
|
||||
aabbs[1].x = std::max(aabbs[1].x, v.x);
|
||||
aabbs[1].y = std::max(aabbs[1].y, v.y);
|
||||
aabbs[1].z = std::max(aabbs[1].z, v.z);
|
||||
}
|
||||
|
||||
BufferBuilder vbuilder;
|
||||
vbuilder.setUsage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT);
|
||||
|
@ -62,11 +52,6 @@ void GPURaytracer::Raytrace(LevelMesh* level)
|
|||
ibuilder.setSize(indexbuffersize);
|
||||
auto indexBuffer = ibuilder.create(device.get());
|
||||
|
||||
BufferBuilder abuilder;
|
||||
abuilder.setUsage(/*VK_BUFFER_USAGE_AABB_BUFFER_BIT |*/ VK_IMAGE_USAGE_TRANSFER_DST_BIT);
|
||||
abuilder.setSize(aabbbuffersize);
|
||||
auto aabbBuffer = abuilder.create(device.get());
|
||||
|
||||
BufferBuilder tbuilder;
|
||||
tbuilder.setUsage(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY);
|
||||
tbuilder.setSize(transferbuffersize);
|
||||
|
@ -74,32 +59,95 @@ void GPURaytracer::Raytrace(LevelMesh* level)
|
|||
auto data = (uint8_t*)transferBuffer->Map(0, transferbuffersize);
|
||||
memcpy(data + vertexoffset, mesh->MeshVertices.Data(), vertexbuffersize);
|
||||
memcpy(data + indexoffset, mesh->MeshElements.Data(), indexbuffersize);
|
||||
memcpy(data + aabboffset, aabbs, aabbbuffersize);
|
||||
transferBuffer->Unmap();
|
||||
|
||||
cmdbuffer->copyBuffer(transferBuffer.get(), vertexBuffer.get(), vertexoffset);
|
||||
cmdbuffer->copyBuffer(transferBuffer.get(), indexBuffer.get(), indexoffset);
|
||||
|
||||
printf("Creating bottom level acceleration structure\n");
|
||||
|
||||
AccelerationStructureBuilder blbuilder;
|
||||
blbuilder.setUsage(VMA_MEMORY_USAGE_GPU_ONLY);
|
||||
blbuilder.setType(VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_NV, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_NV);
|
||||
blbuilder.setInstanceCount(1);
|
||||
blbuilder.addTriangles();
|
||||
blbuilder.setVertices(vertexBuffer.get(), 0, mesh->MeshVertices.Size(), sizeof(Vec3), VK_FORMAT_R32G32B32_SFLOAT);
|
||||
blbuilder.setIndices(indexBuffer.get(), 0, mesh->MeshElements.Size(), VK_INDEX_TYPE_UINT32);
|
||||
auto blAccelStruct = blbuilder.create(device.get());
|
||||
VkBufferDeviceAddressInfo info = { VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO };
|
||||
info.buffer = vertexBuffer->buffer;
|
||||
VkDeviceAddress vertexAddress = vkGetBufferDeviceAddress(device->device, &info);
|
||||
|
||||
ScratchBufferBuilder blsbuilder;
|
||||
blsbuilder.setAccelerationStruct(blAccelStruct.get());
|
||||
auto blScratchBuffer = blsbuilder.create(device.get());
|
||||
info = { VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO };
|
||||
info.buffer = indexBuffer->buffer;
|
||||
VkDeviceAddress indexAddress = vkGetBufferDeviceAddress(device->device, &info);
|
||||
|
||||
VkAccelerationStructureGeometryTrianglesDataKHR triangles = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR };
|
||||
triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;
|
||||
triangles.vertexData.deviceAddress = vertexAddress;
|
||||
triangles.vertexStride = sizeof(Vec3);
|
||||
triangles.indexType = VK_INDEX_TYPE_UINT32;
|
||||
triangles.indexData.deviceAddress = indexAddress;
|
||||
triangles.maxVertex = mesh->MeshVertices.Size();
|
||||
|
||||
VkAccelerationStructureGeometryKHR accelStructBLDesc = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR };
|
||||
accelStructBLDesc.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
|
||||
accelStructBLDesc.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
|
||||
accelStructBLDesc.geometry.triangles = triangles;
|
||||
|
||||
VkAccelerationStructureBuildRangeInfoKHR rangeInfo = {};
|
||||
rangeInfo.firstVertex = 0;
|
||||
rangeInfo.primitiveCount = mesh->MeshElements.Size() / 3;
|
||||
rangeInfo.primitiveOffset = 0;
|
||||
rangeInfo.transformOffset = 0;
|
||||
|
||||
VkAccelerationStructureBuildGeometryInfoKHR buildInfo = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
|
||||
buildInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
|
||||
buildInfo.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
|
||||
buildInfo.flags = accelStructBLDesc.flags | VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
|
||||
buildInfo.geometryCount = 1;
|
||||
buildInfo.pGeometries = &accelStructBLDesc;
|
||||
|
||||
uint32_t maxPrimitiveCount = rangeInfo.primitiveCount;
|
||||
|
||||
VkAccelerationStructureBuildSizesInfoKHR sizeInfo = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR };
|
||||
vkGetAccelerationStructureBuildSizesKHR(device->device, VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR, &buildInfo, &maxPrimitiveCount, &sizeInfo);
|
||||
|
||||
BufferBuilder sbuilder;
|
||||
sbuilder.setUsage(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
|
||||
sbuilder.setSize(sizeInfo.buildScratchSize);
|
||||
auto scratchBuffer = sbuilder.create(device.get());
|
||||
|
||||
info = { VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO };
|
||||
info.buffer = scratchBuffer->buffer;
|
||||
VkDeviceAddress scratchAddress = vkGetBufferDeviceAddress(device->device, &info);
|
||||
|
||||
BufferBuilder blbufbuilder;
|
||||
blbufbuilder.setUsage(VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
|
||||
blbufbuilder.setSize(sizeInfo.accelerationStructureSize);
|
||||
auto blAccelStructBuffer = blbufbuilder.create(device.get());
|
||||
|
||||
VkAccelerationStructureKHR blAccelStructHandle = {};
|
||||
VkAccelerationStructureCreateInfoKHR createInfo = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR };
|
||||
createInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
|
||||
createInfo.buffer = blAccelStructBuffer->buffer;
|
||||
createInfo.size = sizeInfo.accelerationStructureSize;
|
||||
VkResult result = vkCreateAccelerationStructureKHR(device->device, &createInfo, nullptr, &blAccelStructHandle);
|
||||
if (result != VK_SUCCESS)
|
||||
throw std::runtime_error("vkCreateAccelerationStructureKHR failed");
|
||||
auto blAccelStruct = std::make_unique<VulkanAccelerationStructure>(device.get(), blAccelStructHandle);
|
||||
|
||||
buildInfo.dstAccelerationStructure = blAccelStruct->accelstruct;
|
||||
buildInfo.scratchData.deviceAddress = scratchAddress;
|
||||
VkAccelerationStructureBuildRangeInfoKHR* rangeInfos[] = { &rangeInfo };
|
||||
vkCmdBuildAccelerationStructuresKHR(cmdbuffer->buffer, 1, &buildInfo, rangeInfos);
|
||||
|
||||
printf("Creating top level acceleration structure\n");
|
||||
|
||||
AccelerationStructureBuilder tlbuilder;
|
||||
tlbuilder.setUsage(VMA_MEMORY_USAGE_GPU_ONLY);
|
||||
tlbuilder.setType(VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_NV, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_NV);
|
||||
tlbuilder.setInstanceCount(1);
|
||||
//tlbuilder.addAABBs(aabbBuffer.get(), 1, sizeof(Vec3) * 2, 0);
|
||||
auto topLevel = tlbuilder.create(device.get());
|
||||
/*
|
||||
Vec3 aabbs[2] = { mesh->MeshVertices[0], mesh->MeshVertices[1] };
|
||||
for (Vec3& v : mesh->MeshVertices)
|
||||
{
|
||||
aabbs[0].x = std::min(aabbs[0].x, v.x);
|
||||
aabbs[0].y = std::min(aabbs[0].y, v.y);
|
||||
aabbs[0].z = std::min(aabbs[0].z, v.z);
|
||||
aabbs[1].x = std::max(aabbs[1].x, v.x);
|
||||
aabbs[1].y = std::max(aabbs[1].y, v.y);
|
||||
aabbs[1].z = std::max(aabbs[1].z, v.z);
|
||||
}
|
||||
*/
|
||||
|
||||
printf("Tracing light probes\n");
|
||||
|
||||
|
|
|
@ -131,10 +131,10 @@ private:
|
|||
int stage;
|
||||
};
|
||||
|
||||
class AccelerationStructureBuilder
|
||||
class AccelerationStructureBuilderNV
|
||||
{
|
||||
public:
|
||||
AccelerationStructureBuilder();
|
||||
AccelerationStructureBuilderNV();
|
||||
|
||||
void setUsage(VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags allocFlags = 0);
|
||||
|
||||
|
@ -148,7 +148,7 @@ public:
|
|||
|
||||
void addAABBs(VulkanBuffer *aabbData, size_t numAABBs, size_t stride, size_t offset, VkBuildAccelerationStructureFlagsNV flags = 0);
|
||||
|
||||
std::unique_ptr<VulkanAccelerationStructure> create(VulkanDevice *device);
|
||||
std::unique_ptr<VulkanAccelerationStructureNV> create(VulkanDevice *device);
|
||||
|
||||
private:
|
||||
VkAccelerationStructureCreateInfoNV createInfo = {};
|
||||
|
@ -156,12 +156,12 @@ private:
|
|||
std::vector<VkGeometryNV> geometries;
|
||||
};
|
||||
|
||||
class ScratchBufferBuilder
|
||||
class ScratchBufferBuilderNV
|
||||
{
|
||||
public:
|
||||
ScratchBufferBuilder();
|
||||
ScratchBufferBuilderNV();
|
||||
|
||||
void setAccelerationStruct(VulkanAccelerationStructure *accelstruct);
|
||||
void setAccelerationStruct(VulkanAccelerationStructureNV *accelstruct);
|
||||
void setUpdateType();
|
||||
|
||||
std::unique_ptr<VulkanBuffer> create(VulkanDevice *device);
|
||||
|
@ -417,7 +417,7 @@ public:
|
|||
void addBuffer(VulkanDescriptorSet *descriptorSet, int binding, VkDescriptorType type, VulkanBuffer *buffer, size_t offset, size_t range);
|
||||
void addStorageImage(VulkanDescriptorSet *descriptorSet, int binding, VulkanImageView *view, VkImageLayout imageLayout);
|
||||
void addCombinedImageSampler(VulkanDescriptorSet *descriptorSet, int binding, VulkanImageView *view, VulkanSampler *sampler, VkImageLayout imageLayout);
|
||||
void addAccelerationStructure(VulkanDescriptorSet *descriptorSet, int binding, VulkanAccelerationStructure *accelStruct);
|
||||
void addAccelerationStructure(VulkanDescriptorSet *descriptorSet, int binding, VulkanAccelerationStructureNV *accelStruct);
|
||||
|
||||
void updateSets(VulkanDevice *device);
|
||||
|
||||
|
@ -653,30 +653,30 @@ inline std::unique_ptr<VulkanBuffer> BufferBuilder::create(VulkanDevice *device)
|
|||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline AccelerationStructureBuilder::AccelerationStructureBuilder()
|
||||
inline AccelerationStructureBuilderNV::AccelerationStructureBuilderNV()
|
||||
{
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_NV;
|
||||
createInfo.info.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_INFO_NV;
|
||||
}
|
||||
|
||||
inline void AccelerationStructureBuilder::setUsage(VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags allocFlags)
|
||||
inline void AccelerationStructureBuilderNV::setUsage(VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags allocFlags)
|
||||
{
|
||||
allocInfo.usage = memoryUsage;
|
||||
allocInfo.flags = allocFlags;
|
||||
}
|
||||
|
||||
inline void AccelerationStructureBuilder::setType(VkAccelerationStructureTypeNV type, VkBuildAccelerationStructureFlagsNV flags)
|
||||
inline void AccelerationStructureBuilderNV::setType(VkAccelerationStructureTypeNV type, VkBuildAccelerationStructureFlagsNV flags)
|
||||
{
|
||||
createInfo.info.type = type;
|
||||
createInfo.info.flags = flags;
|
||||
}
|
||||
|
||||
inline void AccelerationStructureBuilder::setInstanceCount(int instanceCount)
|
||||
inline void AccelerationStructureBuilderNV::setInstanceCount(int instanceCount)
|
||||
{
|
||||
createInfo.info.instanceCount = instanceCount;
|
||||
}
|
||||
|
||||
inline void AccelerationStructureBuilder::addTriangles(VkGeometryFlagsNV flags)
|
||||
inline void AccelerationStructureBuilderNV::addTriangles(VkGeometryFlagsNV flags)
|
||||
{
|
||||
VkGeometryNV g = {};
|
||||
g.sType = VK_STRUCTURE_TYPE_GEOMETRY_NV;
|
||||
|
@ -689,7 +689,7 @@ inline void AccelerationStructureBuilder::addTriangles(VkGeometryFlagsNV flags)
|
|||
createInfo.info.geometryCount = (uint32_t)geometries.size();
|
||||
}
|
||||
|
||||
inline void AccelerationStructureBuilder::setVertices(VulkanBuffer *vertexData, size_t vertexOffset, size_t vertexCount, size_t vertexStride, VkFormat vertexFormat)
|
||||
inline void AccelerationStructureBuilderNV::setVertices(VulkanBuffer *vertexData, size_t vertexOffset, size_t vertexCount, size_t vertexStride, VkFormat vertexFormat)
|
||||
{
|
||||
auto &g = geometries.back();
|
||||
g.geometry.triangles.vertexData = vertexData->buffer;
|
||||
|
@ -699,7 +699,7 @@ inline void AccelerationStructureBuilder::setVertices(VulkanBuffer *vertexData,
|
|||
g.geometry.triangles.vertexFormat = vertexFormat;
|
||||
}
|
||||
|
||||
inline void AccelerationStructureBuilder::setIndices(VulkanBuffer *indexData, size_t indexOffset, size_t indexCount, VkIndexType indexType)
|
||||
inline void AccelerationStructureBuilderNV::setIndices(VulkanBuffer *indexData, size_t indexOffset, size_t indexCount, VkIndexType indexType)
|
||||
{
|
||||
auto &g = geometries.back();
|
||||
g.geometry.triangles.indexData = indexData->buffer;
|
||||
|
@ -708,14 +708,14 @@ inline void AccelerationStructureBuilder::setIndices(VulkanBuffer *indexData, si
|
|||
g.geometry.triangles.indexType = indexType;
|
||||
}
|
||||
|
||||
inline void AccelerationStructureBuilder::setTransforms(VulkanBuffer *transformData, size_t transformOffset)
|
||||
inline void AccelerationStructureBuilderNV::setTransforms(VulkanBuffer *transformData, size_t transformOffset)
|
||||
{
|
||||
auto &g = geometries.back();
|
||||
g.geometry.triangles.transformData = transformData->buffer;
|
||||
g.geometry.triangles.transformOffset = transformOffset;
|
||||
}
|
||||
|
||||
inline void AccelerationStructureBuilder::addAABBs(VulkanBuffer *aabbData, size_t numAABBs, size_t stride, size_t offset, VkGeometryFlagsNV flags)
|
||||
inline void AccelerationStructureBuilderNV::addAABBs(VulkanBuffer *aabbData, size_t numAABBs, size_t stride, size_t offset, VkGeometryFlagsNV flags)
|
||||
{
|
||||
VkGeometryNV g = {};
|
||||
g.sType = VK_STRUCTURE_TYPE_GEOMETRY_NV;
|
||||
|
@ -731,7 +731,7 @@ inline void AccelerationStructureBuilder::addAABBs(VulkanBuffer *aabbData, size_
|
|||
createInfo.info.geometryCount = (uint32_t)geometries.size();
|
||||
}
|
||||
|
||||
inline std::unique_ptr<VulkanAccelerationStructure> AccelerationStructureBuilder::create(VulkanDevice *device)
|
||||
inline std::unique_ptr<VulkanAccelerationStructureNV> AccelerationStructureBuilderNV::create(VulkanDevice *device)
|
||||
{
|
||||
VkAccelerationStructureNV accelstruct;
|
||||
VkResult result = vkCreateAccelerationStructureNV(device->device, &createInfo, nullptr, &accelstruct);
|
||||
|
@ -769,12 +769,12 @@ inline std::unique_ptr<VulkanAccelerationStructure> AccelerationStructureBuilder
|
|||
throw std::runtime_error("could not bind memory to vulkan acceleration structure");
|
||||
}
|
||||
|
||||
return std::make_unique<VulkanAccelerationStructure>(device, accelstruct, allocation, std::move(createInfo.info), std::move(geometries));
|
||||
return std::make_unique<VulkanAccelerationStructureNV>(device, accelstruct, allocation, std::move(createInfo.info), std::move(geometries));
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline ScratchBufferBuilder::ScratchBufferBuilder()
|
||||
inline ScratchBufferBuilderNV::ScratchBufferBuilderNV()
|
||||
{
|
||||
reqInfo.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_INFO_NV;
|
||||
reqInfo.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_BUILD_SCRATCH_NV;
|
||||
|
@ -782,17 +782,17 @@ inline ScratchBufferBuilder::ScratchBufferBuilder()
|
|||
allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
||||
}
|
||||
|
||||
inline void ScratchBufferBuilder::setAccelerationStruct(VulkanAccelerationStructure *accelstruct)
|
||||
inline void ScratchBufferBuilderNV::setAccelerationStruct(VulkanAccelerationStructureNV *accelstruct)
|
||||
{
|
||||
reqInfo.accelerationStructure = accelstruct->accelstruct;
|
||||
}
|
||||
|
||||
inline void ScratchBufferBuilder::setUpdateType()
|
||||
inline void ScratchBufferBuilderNV::setUpdateType()
|
||||
{
|
||||
reqInfo.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_UPDATE_SCRATCH_NV;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<VulkanBuffer> ScratchBufferBuilder::create(VulkanDevice *device)
|
||||
inline std::unique_ptr<VulkanBuffer> ScratchBufferBuilderNV::create(VulkanDevice *device)
|
||||
{
|
||||
VkMemoryRequirements2KHR memoryRequirements2 = {};
|
||||
memoryRequirements2.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2_KHR;
|
||||
|
@ -1721,7 +1721,7 @@ inline void WriteDescriptors::addCombinedImageSampler(VulkanDescriptorSet *descr
|
|||
writeExtras.push_back(std::move(extra));
|
||||
}
|
||||
|
||||
inline void WriteDescriptors::addAccelerationStructure(VulkanDescriptorSet *descriptorSet, int binding, VulkanAccelerationStructure *accelStruct)
|
||||
inline void WriteDescriptors::addAccelerationStructure(VulkanDescriptorSet *descriptorSet, int binding, VulkanAccelerationStructureNV *accelStruct)
|
||||
{
|
||||
auto extra = std::make_unique<WriteExtra>();
|
||||
extra->accelStruct = {};
|
||||
|
|
|
@ -282,11 +282,11 @@ void VulkanDevice::createInstance()
|
|||
|
||||
VkApplicationInfo appInfo = {};
|
||||
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
||||
appInfo.pApplicationName = "UT99";
|
||||
appInfo.pApplicationName = "ZDRay";
|
||||
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
||||
appInfo.pEngineName = "UT99";
|
||||
appInfo.pEngineName = "ZDRay";
|
||||
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
||||
appInfo.apiVersion = VK_API_VERSION_1_1;
|
||||
appInfo.apiVersion = VK_API_VERSION_1_2;
|
||||
|
||||
VkInstanceCreateInfo createInfo = {};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
||||
|
|
|
@ -66,17 +66,22 @@ public:
|
|||
std::vector<VkLayerProperties> availableLayers;
|
||||
std::vector<VkExtensionProperties> extensions;
|
||||
std::vector<const char *> enabledExtensions;
|
||||
std::vector<const char *> optionalExtensions = { VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME };
|
||||
std::vector<const char *> optionalExtensions = { };
|
||||
std::vector<const char*> enabledValidationLayers;
|
||||
|
||||
// Device setup
|
||||
VkPhysicalDeviceFeatures enabledDeviceFeatures = {};
|
||||
std::vector<const char *> enabledDeviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
|
||||
std::vector<const char *> enabledDeviceExtensions = {
|
||||
//VK_KHR_SWAPCHAIN_EXTENSION_NAME,
|
||||
VK_KHR_RAY_QUERY_EXTENSION_NAME,
|
||||
VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME,
|
||||
VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME
|
||||
};
|
||||
std::vector<const char *> optionalDeviceExtensions = {
|
||||
VK_EXT_HDR_METADATA_EXTENSION_NAME,
|
||||
//VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
|
||||
//VK_EXT_HDR_METADATA_EXTENSION_NAME,
|
||||
VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
|
||||
VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
|
||||
VK_NV_RAY_TRACING_EXTENSION_NAME
|
||||
VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME
|
||||
};
|
||||
VulkanPhysicalDevice physicalDevice;
|
||||
bool debugLayerActive = false;
|
||||
|
|
|
@ -222,9 +222,25 @@ private:
|
|||
class VulkanAccelerationStructure
|
||||
{
|
||||
public:
|
||||
VulkanAccelerationStructure(VulkanDevice *device, VkAccelerationStructureNV accelstruct, VmaAllocation allocation, VkAccelerationStructureInfoNV &&info, std::vector<VkGeometryNV> &&geometries);
|
||||
VulkanAccelerationStructure(VulkanDevice* device, VkAccelerationStructureKHR accelstruct);
|
||||
~VulkanAccelerationStructure();
|
||||
|
||||
void SetDebugName(const char* name) { device->setDebugObjectName(name, (uint64_t)accelstruct, VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR); }
|
||||
|
||||
VulkanDevice* device;
|
||||
VkAccelerationStructureKHR accelstruct;
|
||||
|
||||
private:
|
||||
VulkanAccelerationStructure(const VulkanAccelerationStructure&) = delete;
|
||||
VulkanAccelerationStructure& operator=(const VulkanAccelerationStructure&) = delete;
|
||||
};
|
||||
|
||||
class VulkanAccelerationStructureNV
|
||||
{
|
||||
public:
|
||||
VulkanAccelerationStructureNV(VulkanDevice *device, VkAccelerationStructureNV accelstruct, VmaAllocation allocation, VkAccelerationStructureInfoNV &&info, std::vector<VkGeometryNV> &&geometries);
|
||||
~VulkanAccelerationStructureNV();
|
||||
|
||||
void SetDebugName(const char *name) { device->setDebugObjectName(name, (uint64_t)accelstruct, VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_NV); }
|
||||
|
||||
VulkanDevice *device;
|
||||
|
@ -235,8 +251,8 @@ public:
|
|||
std::vector<VkGeometryNV> geometries;
|
||||
|
||||
private:
|
||||
VulkanAccelerationStructure(const VulkanAccelerationStructure &) = delete;
|
||||
VulkanAccelerationStructure &operator=(const VulkanAccelerationStructure &) = delete;
|
||||
VulkanAccelerationStructureNV(const VulkanAccelerationStructureNV &) = delete;
|
||||
VulkanAccelerationStructureNV &operator=(const VulkanAccelerationStructureNV &) = delete;
|
||||
};
|
||||
|
||||
class VulkanPipeline
|
||||
|
@ -373,9 +389,9 @@ public:
|
|||
void endRenderPass();
|
||||
void executeCommands(uint32_t commandBufferCount, const VkCommandBuffer* pCommandBuffers);
|
||||
|
||||
void buildAccelerationStructure(const VkAccelerationStructureInfoNV* pInfo, VulkanBuffer *instanceData, VkDeviceSize instanceOffset, VkBool32 update, VulkanAccelerationStructure *dst, VulkanAccelerationStructure *src, VulkanBuffer *scratch, VkDeviceSize scratchOffset);
|
||||
void buildAccelerationStructure(const VkAccelerationStructureInfoNV* pInfo, VulkanBuffer *instanceData, VkDeviceSize instanceOffset, VkBool32 update, VulkanAccelerationStructureNV *dst, VulkanAccelerationStructureNV *src, VulkanBuffer *scratch, VkDeviceSize scratchOffset);
|
||||
void buildAccelerationStructure(const VkAccelerationStructureInfoNV* pInfo, VkBuffer instanceData, VkDeviceSize instanceOffset, VkBool32 update, VkAccelerationStructureNV dst, VkAccelerationStructureNV src, VkBuffer scratch, VkDeviceSize scratchOffset);
|
||||
void copyAccelerationStructure(VulkanAccelerationStructure *dst, VulkanAccelerationStructure *src, VkCopyAccelerationStructureModeNV mode);
|
||||
void copyAccelerationStructure(VulkanAccelerationStructureNV *dst, VulkanAccelerationStructureNV *src, VkCopyAccelerationStructureModeNV mode);
|
||||
void copyAccelerationStructure(VkAccelerationStructureNV dst, VkAccelerationStructureNV src, VkCopyAccelerationStructureModeNV mode);
|
||||
void traceRays(VulkanBuffer *raygenShaderBindingTableBuffer, VkDeviceSize raygenShaderBindingOffset, VulkanBuffer *missShaderBindingTableBuffer, VkDeviceSize missShaderBindingOffset, VkDeviceSize missShaderBindingStride, VulkanBuffer *hitShaderBindingTableBuffer, VkDeviceSize hitShaderBindingOffset, VkDeviceSize hitShaderBindingStride, VulkanBuffer *callableShaderBindingTableBuffer, VkDeviceSize callableShaderBindingOffset, VkDeviceSize callableShaderBindingStride, uint32_t width, uint32_t height, uint32_t depth);
|
||||
void traceRays(VkBuffer raygenShaderBindingTableBuffer, VkDeviceSize raygenShaderBindingOffset, VkBuffer missShaderBindingTableBuffer, VkDeviceSize missShaderBindingOffset, VkDeviceSize missShaderBindingStride, VkBuffer hitShaderBindingTableBuffer, VkDeviceSize hitShaderBindingOffset, VkDeviceSize hitShaderBindingStride, VkBuffer callableShaderBindingTableBuffer, VkDeviceSize callableShaderBindingOffset, VkDeviceSize callableShaderBindingStride, uint32_t width, uint32_t height, uint32_t depth);
|
||||
|
@ -911,7 +927,7 @@ inline void VulkanCommandBuffer::executeCommands(uint32_t commandBufferCount, co
|
|||
vkCmdExecuteCommands(buffer, commandBufferCount, pCommandBuffers);
|
||||
}
|
||||
|
||||
inline void VulkanCommandBuffer::buildAccelerationStructure(const VkAccelerationStructureInfoNV* pInfo, VulkanBuffer *instanceData, VkDeviceSize instanceOffset, VkBool32 update, VulkanAccelerationStructure *dst, VulkanAccelerationStructure *src, VulkanBuffer *scratch, VkDeviceSize scratchOffset)
|
||||
inline void VulkanCommandBuffer::buildAccelerationStructure(const VkAccelerationStructureInfoNV* pInfo, VulkanBuffer *instanceData, VkDeviceSize instanceOffset, VkBool32 update, VulkanAccelerationStructureNV *dst, VulkanAccelerationStructureNV *src, VulkanBuffer *scratch, VkDeviceSize scratchOffset)
|
||||
{
|
||||
buildAccelerationStructure(pInfo, instanceData->buffer, instanceOffset, update, dst->accelstruct, src->accelstruct, scratch->buffer, scratchOffset);
|
||||
}
|
||||
|
@ -921,7 +937,7 @@ inline void VulkanCommandBuffer::buildAccelerationStructure(const VkAcceleration
|
|||
vkCmdBuildAccelerationStructureNV(buffer, pInfo, instanceData, instanceOffset, update, dst, src, scratch, scratchOffset);
|
||||
}
|
||||
|
||||
inline void VulkanCommandBuffer::copyAccelerationStructure(VulkanAccelerationStructure *dst, VulkanAccelerationStructure *src, VkCopyAccelerationStructureModeNV mode)
|
||||
inline void VulkanCommandBuffer::copyAccelerationStructure(VulkanAccelerationStructureNV *dst, VulkanAccelerationStructureNV *src, VkCopyAccelerationStructureModeNV mode)
|
||||
{
|
||||
copyAccelerationStructure(dst->accelstruct, src->accelstruct, mode);
|
||||
}
|
||||
|
@ -1093,12 +1109,24 @@ inline VulkanSampler::~VulkanSampler()
|
|||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline VulkanAccelerationStructure::VulkanAccelerationStructure(VulkanDevice *device, VkAccelerationStructureNV accelstruct, VmaAllocation allocation, VkAccelerationStructureInfoNV &&info, std::vector<VkGeometryNV> &&geometries)
|
||||
: device(device), accelstruct(accelstruct), allocation(allocation), info(std::move(info)), geometries(std::move(geometries))
|
||||
inline VulkanAccelerationStructure::VulkanAccelerationStructure(VulkanDevice* device, VkAccelerationStructureKHR accelstruct)
|
||||
: device(device), accelstruct(accelstruct)
|
||||
{
|
||||
}
|
||||
|
||||
inline VulkanAccelerationStructure::~VulkanAccelerationStructure()
|
||||
{
|
||||
vkDestroyAccelerationStructureKHR(device->device, accelstruct, nullptr);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline VulkanAccelerationStructureNV::VulkanAccelerationStructureNV(VulkanDevice *device, VkAccelerationStructureNV accelstruct, VmaAllocation allocation, VkAccelerationStructureInfoNV &&info, std::vector<VkGeometryNV> &&geometries)
|
||||
: device(device), accelstruct(accelstruct), allocation(allocation), info(std::move(info)), geometries(std::move(geometries))
|
||||
{
|
||||
}
|
||||
|
||||
inline VulkanAccelerationStructureNV::~VulkanAccelerationStructureNV()
|
||||
{
|
||||
vkDestroyAccelerationStructureNV(device->device, accelstruct, nullptr);
|
||||
vmaFreeMemory(device->allocator, allocation);
|
||||
|
|
Loading…
Reference in a new issue