From 86c179e2e770476c7e9b4553b1218fe66ebb3d0a Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Fri, 14 Jun 2024 22:12:26 +0200 Subject: [PATCH] Adding Raytracing shader stages --- src/Engine/Graphics/RayTracing.h | 6 ++++ src/Engine/Graphics/Shader.h | 36 ++++++++++++++++++++ src/Engine/Graphics/Vulkan/Graphics.cpp | 41 +++++++++++++++++++---- src/Engine/Graphics/Vulkan/Graphics.h | 5 ++- src/Engine/Graphics/Vulkan/RayTracing.cpp | 32 +++++++++++------- src/Engine/Graphics/Vulkan/RayTracing.h | 5 +-- 6 files changed, 103 insertions(+), 22 deletions(-) diff --git a/src/Engine/Graphics/RayTracing.h b/src/Engine/Graphics/RayTracing.h index 682aca4..f5b7978 100644 --- a/src/Engine/Graphics/RayTracing.h +++ b/src/Engine/Graphics/RayTracing.h @@ -3,6 +3,12 @@ namespace Seele { namespace Gfx { +class RayTracingPipeline +{ + public: + private: +}; +DEFINE_REF(RayTracingPipeline) class BottomLevelAS { public: BottomLevelAS(); diff --git a/src/Engine/Graphics/Shader.h b/src/Engine/Graphics/Shader.h index 133491d..3385a0d 100644 --- a/src/Engine/Graphics/Shader.h +++ b/src/Engine/Graphics/Shader.h @@ -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 { diff --git a/src/Engine/Graphics/Vulkan/Graphics.cpp b/src/Engine/Graphics/Vulkan/Graphics.cpp index cb7f0c2..3f211b4 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.cpp +++ b/src/Engine/Graphics/Vulkan/Graphics.cpp @@ -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 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, diff --git a/src/Engine/Graphics/Vulkan/Graphics.h b/src/Engine/Graphics/Vulkan/Graphics.h index 0b9488e..b81a7ed 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.h +++ b/src/Engine/Graphics/Vulkan/Graphics.h @@ -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 allocatedFramebuffers; VmaAllocator allocator; diff --git a/src/Engine/Graphics/Vulkan/RayTracing.cpp b/src/Engine/Graphics/Vulkan/RayTracing.cpp index 949e160..8692b59 100644 --- a/src/Engine/Graphics/Vulkan/RayTracing.cpp +++ b/src/Engine/Graphics/Vulkan/RayTracing.cpp @@ -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 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() {} \ No newline at end of file diff --git a/src/Engine/Graphics/Vulkan/RayTracing.h b/src/Engine/Graphics/Vulkan/RayTracing.h index e8f82f2..9b5437b 100644 --- a/src/Engine/Graphics/Vulkan/RayTracing.h +++ b/src/Engine/Graphics/Vulkan/RayTracing.h @@ -3,7 +3,7 @@ #include "Graphics/Initializer.h" #include "Graphics/RayTracing.h" #include - +#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 {