Adding Raytracing shader stages

This commit is contained in:
Dynamitos
2024-06-14 22:12:26 +02:00
parent ac135eebd0
commit 86c179e2e7
6 changed files with 103 additions and 22 deletions
+6
View File
@@ -3,6 +3,12 @@
namespace Seele {
namespace Gfx {
class RayTracingPipeline
{
public:
private:
};
DEFINE_REF(RayTracingPipeline)
class BottomLevelAS {
public:
BottomLevelAS();
+36
View File
@@ -42,6 +42,42 @@ class ComputeShader {
};
DEFINE_REF(ComputeShader)
// Ray Tracing shaders
class RayGenShader {
public:
RayGenShader() {}
virtual ~RayGenShader() {}
};
DEFINE_REF(RayGenShader)
class AnyHitShader {
public:
AnyHitShader() {}
virtual ~AnyHitShader() {}
};
DEFINE_REF(AnyHitShader)
class ClosestHitShader {
public:
ClosestHitShader() {}
virtual ~ClosestHitShader() {}
};
DEFINE_REF(ClosestHitShader)
class MissShader {
public:
MissShader() {}
virtual ~MissShader() {}
};
DEFINE_REF(MissShader)
class CallableShader {
public:
CallableShader() {}
virtual ~CallableShader() {}
};
DEFINE_REF(CallableShader)
// Uniquely identifies a permutation of shaders
// using the type parameters used to generate it
struct ShaderPermutation {
+35 -6
View File
@@ -287,8 +287,16 @@ void Graphics::copyTexture(Gfx::PTexture source, Gfx::PTexture destination) {
},
.srcOffsets =
{
{0, 0, 0},
{(int32)src->getWidth(), (int32)src->getHeight(), (int32)src->getDepth()},
{
0,
0,
0,
},
{
(int32)src->getWidth(),
(int32)src->getHeight(),
(int32)src->getDepth(),
},
},
.dstSubresource =
{
@@ -299,8 +307,16 @@ void Graphics::copyTexture(Gfx::PTexture source, Gfx::PTexture destination) {
},
.dstOffsets =
{
{0, 0, 0},
{(int32)dst->getWidth(), (int32)dst->getHeight(), (int32)dst->getDepth()},
{
0,
0,
0,
},
{
(int32)dst->getWidth(),
(int32)dst->getHeight(),
(int32)dst->getDepth(),
},
},
};
PCommand command = getGraphicsCommands()->getCommands();
@@ -359,7 +375,7 @@ PCommandPool Graphics::getTransferCommands() {
return transferCommands;
}
VmaAllocator Graphics::getAllocator() { return allocator; }
VmaAllocator Graphics::getAllocator() const { return allocator; }
PDestructionManager Graphics::getDestructionManager() { return destructionManager; }
@@ -377,6 +393,7 @@ Array<const char*> Graphics::getRequiredExtensions() {
#endif // ENABLE_VALIDATION
return extensions;
}
void Graphics::initInstance(GraphicsInitializer initInfo) {
glfwInit();
assert(glfwVulkanSupported());
@@ -414,6 +431,7 @@ void Graphics::initInstance(GraphicsInitializer initInfo) {
};
VK_CHECK(vkCreateInstance(&info, nullptr, &instance));
}
void Graphics::setupDebugCallback() {
VkDebugUtilsMessengerCreateInfoEXT createInfo = {
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
@@ -441,6 +459,10 @@ void Graphics::pickPhysicalDevice() {
};
accelerationProperties = VkPhysicalDeviceAccelerationStructurePropertiesKHR{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_PROPERTIES_KHR,
.pNext = &rayTracingProperties,
};
rayTracingProperties = VkPhysicalDeviceRayTracingPipelinePropertiesKHR{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR,
.pNext = nullptr,
};
for (auto dev : physicalDevices) {
@@ -462,9 +484,15 @@ void Graphics::pickPhysicalDevice() {
physicalDevice = bestDevice;
vkGetPhysicalDeviceProperties2(physicalDevice, &props);
rayTracingFeatures = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR,
.pNext = nullptr,
.rayTracingPipeline = true,
.rayTraversalPrimitiveCulling = true,
};
accelerationFeatures = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR,
.pNext = nullptr,
.pNext = &rayTracingFeatures,
.accelerationStructure = true,
};
meshShaderFeatures = {
@@ -584,6 +612,7 @@ void Graphics::createDevice(GraphicsInitializer initializer) {
#endif
initializer.deviceExtensions.add(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME);
initializer.deviceExtensions.add(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
initializer.deviceExtensions.add(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME);
initializer.deviceExtensions.add(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME);
VkDeviceCreateInfo deviceInfo = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
+4 -1
View File
@@ -17,13 +17,14 @@ class Graphics : public Gfx::Graphics {
constexpr VkDevice getDevice() const { return handle; }
constexpr VkPhysicalDevice getPhysicalDevice() const { return physicalDevice; }
constexpr VkPhysicalDeviceAccelerationStructurePropertiesKHR getAccelerationProperties() const { return accelerationProperties; }
constexpr VkPhysicalDeviceRayTracingPipelinePropertiesKHR getRayTracingProperties() const { return rayTracingProperties; }
PCommandPool getQueueCommands(Gfx::QueueType queueType);
PCommandPool getGraphicsCommands();
PCommandPool getComputeCommands();
PCommandPool getTransferCommands();
VmaAllocator getAllocator();
VmaAllocator getAllocator() const;
PDestructionManager getDestructionManager();
// Inherited via Graphics
@@ -101,6 +102,8 @@ class Graphics : public Gfx::Graphics {
VkPhysicalDeviceMeshShaderFeaturesEXT meshShaderFeatures;
VkPhysicalDeviceAccelerationStructureFeaturesKHR accelerationFeatures;
VkPhysicalDeviceAccelerationStructurePropertiesKHR accelerationProperties;
VkPhysicalDeviceRayTracingPipelineFeaturesKHR rayTracingFeatures;
VkPhysicalDeviceRayTracingPipelinePropertiesKHR rayTracingProperties;
VkDebugUtilsMessengerEXT callback;
Map<uint32, OFramebuffer> allocatedFramebuffers;
VmaAllocator allocator;
+19 -13
View File
@@ -83,29 +83,29 @@ BottomLevelAS::BottomLevelAS(PGraphics graphics, const Gfx::BottomLevelASCreateI
vkGetAccelerationStructureBuildSizesKHR(graphics->getDevice(), VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR, &structureBuildGeometry,
&primitiveCount, &buildSizesInfo);
VkBufferCreateInfo tempBufferInfo = {
VkBufferCreateInfo bufferInfo = {
.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 tempBufferAllocInfo = {
VmaAllocationCreateInfo bufferAllocInfo = {
.usage = VMA_MEMORY_USAGE_AUTO,
};
OBufferAllocation tempAlloc = new BufferAllocation(graphics, "TempBuffer", tempBufferInfo, tempBufferAllocInfo, Gfx::QueueType::GRAPHICS);
buffer =
new BufferAllocation(graphics, "BLAS", bufferInfo, bufferAllocInfo, Gfx::QueueType::GRAPHICS);
VkAccelerationStructureCreateInfoKHR tempInfo = {
VkAccelerationStructureCreateInfoKHR blasInfo = {
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR,
.pNext = nullptr,
.createFlags = 0,
.buffer = tempAlloc->buffer,
.buffer = buffer->buffer,
.offset = 0,
.size = buildSizesInfo.accelerationStructureSize,
.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR,
};
VkAccelerationStructureKHR tempAS;
VK_CHECK(vkCreateAccelerationStructureKHR(graphics->getDevice(), &tempInfo, nullptr, &tempAS));
VK_CHECK(vkCreateAccelerationStructureKHR(graphics->getDevice(), &blasInfo, nullptr, &handle));
VkBufferCreateInfo scratchInfo = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
@@ -121,26 +121,32 @@ BottomLevelAS::BottomLevelAS(PGraphics graphics, const Gfx::BottomLevelASCreateI
new BufferAllocation(graphics, "ScratchBuffer", scratchInfo, scratchAllocInfo, Gfx::QueueType::GRAPHICS,
graphics->getAccelerationProperties().minAccelerationStructureScratchOffsetAlignment);
structureBuildGeometry.dstAccelerationStructure = tempAS;
structureBuildGeometry.dstAccelerationStructure = handle;
structureBuildGeometry.scratchData.deviceAddress = scratchAlloc->deviceAddress;
VkAccelerationStructureBuildRangeInfoKHR buildRangeInfo = {
.primitiveCount = primitiveCount, .primitiveOffset = 0, .firstVertex = 0, .transformOffset = 0};
.primitiveCount = primitiveCount,
.primitiveOffset = 0,
.firstVertex = 0,
.transformOffset = 0,
};
Array<VkAccelerationStructureBuildRangeInfoKHR*> ranges = {&buildRangeInfo};
PCommand cmd = graphics->getGraphicsCommands()->getCommands();
vkCmdBuildAccelerationStructuresKHR(cmd->getHandle(), 1, &structureBuildGeometry, ranges.data());
cmd->bindResource(PBufferAllocation(transformBuffer));
cmd->bindResource(PBufferAllocation(tempAlloc));
cmd->bindResource(PBufferAllocation(buffer));
cmd->bindResource(PBufferAllocation(scratchAlloc));
graphics->getDestructionManager()->queueResourceForDestruction(std::move(transformBuffer));
graphics->getDestructionManager()->queueResourceForDestruction(std::move(tempAlloc));
graphics->getDestructionManager()->queueResourceForDestruction(std::move(scratchAlloc));
//todo: compact
}
BottomLevelAS::~BottomLevelAS() {}
BottomLevelAS::~BottomLevelAS() { graphics->getDestructionManager()->queueResourceForDestruction(std::move(buffer)); }
TopLevelAS::TopLevelAS(PGraphics graphics, const Gfx::TopLevelASCreateInfo& createInfo) {}
TopLevelAS::TopLevelAS(PGraphics graphics, const Gfx::TopLevelASCreateInfo& createInfo) {
}
TopLevelAS::~TopLevelAS() {}
+3 -2
View File
@@ -3,7 +3,7 @@
#include "Graphics/Initializer.h"
#include "Graphics/RayTracing.h"
#include <vulkan/vulkan_core.h>
#include "Buffer.h"
namespace Seele {
namespace Vulkan {
@@ -15,7 +15,8 @@ class BottomLevelAS : public Gfx::BottomLevelAS {
private:
PGraphics graphics;
VkAccelerationStructureKHR handle;
Gfx::OShaderBuffer buffer;
OBufferAllocation buffer;
};
DEFINE_REF(BottomLevelAS)
class TopLevelAS : public Gfx::TopLevelAS {