Adding Raytracing shader stages
This commit is contained in:
@@ -3,6 +3,12 @@
|
|||||||
|
|
||||||
namespace Seele {
|
namespace Seele {
|
||||||
namespace Gfx {
|
namespace Gfx {
|
||||||
|
class RayTracingPipeline
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
DEFINE_REF(RayTracingPipeline)
|
||||||
class BottomLevelAS {
|
class BottomLevelAS {
|
||||||
public:
|
public:
|
||||||
BottomLevelAS();
|
BottomLevelAS();
|
||||||
|
|||||||
@@ -42,6 +42,42 @@ class ComputeShader {
|
|||||||
};
|
};
|
||||||
DEFINE_REF(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
|
// Uniquely identifies a permutation of shaders
|
||||||
// using the type parameters used to generate it
|
// using the type parameters used to generate it
|
||||||
struct ShaderPermutation {
|
struct ShaderPermutation {
|
||||||
|
|||||||
@@ -287,8 +287,16 @@ void Graphics::copyTexture(Gfx::PTexture source, Gfx::PTexture destination) {
|
|||||||
},
|
},
|
||||||
.srcOffsets =
|
.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 =
|
.dstSubresource =
|
||||||
{
|
{
|
||||||
@@ -299,8 +307,16 @@ void Graphics::copyTexture(Gfx::PTexture source, Gfx::PTexture destination) {
|
|||||||
},
|
},
|
||||||
.dstOffsets =
|
.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();
|
PCommand command = getGraphicsCommands()->getCommands();
|
||||||
@@ -359,7 +375,7 @@ PCommandPool Graphics::getTransferCommands() {
|
|||||||
return transferCommands;
|
return transferCommands;
|
||||||
}
|
}
|
||||||
|
|
||||||
VmaAllocator Graphics::getAllocator() { return allocator; }
|
VmaAllocator Graphics::getAllocator() const { return allocator; }
|
||||||
|
|
||||||
PDestructionManager Graphics::getDestructionManager() { return destructionManager; }
|
PDestructionManager Graphics::getDestructionManager() { return destructionManager; }
|
||||||
|
|
||||||
@@ -377,6 +393,7 @@ Array<const char*> Graphics::getRequiredExtensions() {
|
|||||||
#endif // ENABLE_VALIDATION
|
#endif // ENABLE_VALIDATION
|
||||||
return extensions;
|
return extensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Graphics::initInstance(GraphicsInitializer initInfo) {
|
void Graphics::initInstance(GraphicsInitializer initInfo) {
|
||||||
glfwInit();
|
glfwInit();
|
||||||
assert(glfwVulkanSupported());
|
assert(glfwVulkanSupported());
|
||||||
@@ -414,6 +431,7 @@ void Graphics::initInstance(GraphicsInitializer initInfo) {
|
|||||||
};
|
};
|
||||||
VK_CHECK(vkCreateInstance(&info, nullptr, &instance));
|
VK_CHECK(vkCreateInstance(&info, nullptr, &instance));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Graphics::setupDebugCallback() {
|
void Graphics::setupDebugCallback() {
|
||||||
VkDebugUtilsMessengerCreateInfoEXT createInfo = {
|
VkDebugUtilsMessengerCreateInfoEXT createInfo = {
|
||||||
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
|
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
|
||||||
@@ -441,6 +459,10 @@ void Graphics::pickPhysicalDevice() {
|
|||||||
};
|
};
|
||||||
accelerationProperties = VkPhysicalDeviceAccelerationStructurePropertiesKHR{
|
accelerationProperties = VkPhysicalDeviceAccelerationStructurePropertiesKHR{
|
||||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_PROPERTIES_KHR,
|
.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,
|
.pNext = nullptr,
|
||||||
};
|
};
|
||||||
for (auto dev : physicalDevices) {
|
for (auto dev : physicalDevices) {
|
||||||
@@ -462,9 +484,15 @@ void Graphics::pickPhysicalDevice() {
|
|||||||
physicalDevice = bestDevice;
|
physicalDevice = bestDevice;
|
||||||
|
|
||||||
vkGetPhysicalDeviceProperties2(physicalDevice, &props);
|
vkGetPhysicalDeviceProperties2(physicalDevice, &props);
|
||||||
|
rayTracingFeatures = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.rayTracingPipeline = true,
|
||||||
|
.rayTraversalPrimitiveCulling = true,
|
||||||
|
};
|
||||||
accelerationFeatures = {
|
accelerationFeatures = {
|
||||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR,
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR,
|
||||||
.pNext = nullptr,
|
.pNext = &rayTracingFeatures,
|
||||||
.accelerationStructure = true,
|
.accelerationStructure = true,
|
||||||
};
|
};
|
||||||
meshShaderFeatures = {
|
meshShaderFeatures = {
|
||||||
@@ -584,6 +612,7 @@ void Graphics::createDevice(GraphicsInitializer initializer) {
|
|||||||
#endif
|
#endif
|
||||||
initializer.deviceExtensions.add(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME);
|
initializer.deviceExtensions.add(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME);
|
||||||
initializer.deviceExtensions.add(VK_KHR_ACCELERATION_STRUCTURE_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);
|
initializer.deviceExtensions.add(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME);
|
||||||
VkDeviceCreateInfo deviceInfo = {
|
VkDeviceCreateInfo deviceInfo = {
|
||||||
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
||||||
|
|||||||
@@ -17,13 +17,14 @@ class Graphics : public Gfx::Graphics {
|
|||||||
constexpr VkDevice getDevice() const { return handle; }
|
constexpr VkDevice getDevice() const { return handle; }
|
||||||
constexpr VkPhysicalDevice getPhysicalDevice() const { return physicalDevice; }
|
constexpr VkPhysicalDevice getPhysicalDevice() const { return physicalDevice; }
|
||||||
constexpr VkPhysicalDeviceAccelerationStructurePropertiesKHR getAccelerationProperties() const { return accelerationProperties; }
|
constexpr VkPhysicalDeviceAccelerationStructurePropertiesKHR getAccelerationProperties() const { return accelerationProperties; }
|
||||||
|
constexpr VkPhysicalDeviceRayTracingPipelinePropertiesKHR getRayTracingProperties() const { return rayTracingProperties; }
|
||||||
|
|
||||||
PCommandPool getQueueCommands(Gfx::QueueType queueType);
|
PCommandPool getQueueCommands(Gfx::QueueType queueType);
|
||||||
PCommandPool getGraphicsCommands();
|
PCommandPool getGraphicsCommands();
|
||||||
PCommandPool getComputeCommands();
|
PCommandPool getComputeCommands();
|
||||||
PCommandPool getTransferCommands();
|
PCommandPool getTransferCommands();
|
||||||
|
|
||||||
VmaAllocator getAllocator();
|
VmaAllocator getAllocator() const;
|
||||||
PDestructionManager getDestructionManager();
|
PDestructionManager getDestructionManager();
|
||||||
|
|
||||||
// Inherited via Graphics
|
// Inherited via Graphics
|
||||||
@@ -101,6 +102,8 @@ class Graphics : public Gfx::Graphics {
|
|||||||
VkPhysicalDeviceMeshShaderFeaturesEXT meshShaderFeatures;
|
VkPhysicalDeviceMeshShaderFeaturesEXT meshShaderFeatures;
|
||||||
VkPhysicalDeviceAccelerationStructureFeaturesKHR accelerationFeatures;
|
VkPhysicalDeviceAccelerationStructureFeaturesKHR accelerationFeatures;
|
||||||
VkPhysicalDeviceAccelerationStructurePropertiesKHR accelerationProperties;
|
VkPhysicalDeviceAccelerationStructurePropertiesKHR accelerationProperties;
|
||||||
|
VkPhysicalDeviceRayTracingPipelineFeaturesKHR rayTracingFeatures;
|
||||||
|
VkPhysicalDeviceRayTracingPipelinePropertiesKHR rayTracingProperties;
|
||||||
VkDebugUtilsMessengerEXT callback;
|
VkDebugUtilsMessengerEXT callback;
|
||||||
Map<uint32, OFramebuffer> allocatedFramebuffers;
|
Map<uint32, OFramebuffer> allocatedFramebuffers;
|
||||||
VmaAllocator allocator;
|
VmaAllocator allocator;
|
||||||
|
|||||||
@@ -83,29 +83,29 @@ BottomLevelAS::BottomLevelAS(PGraphics graphics, const Gfx::BottomLevelASCreateI
|
|||||||
vkGetAccelerationStructureBuildSizesKHR(graphics->getDevice(), VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR, &structureBuildGeometry,
|
vkGetAccelerationStructureBuildSizesKHR(graphics->getDevice(), VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR, &structureBuildGeometry,
|
||||||
&primitiveCount, &buildSizesInfo);
|
&primitiveCount, &buildSizesInfo);
|
||||||
|
|
||||||
VkBufferCreateInfo tempBufferInfo = {
|
VkBufferCreateInfo bufferInfo = {
|
||||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||||
.pNext = nullptr,
|
.pNext = nullptr,
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
.size = buildSizesInfo.accelerationStructureSize,
|
.size = buildSizesInfo.accelerationStructureSize,
|
||||||
.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
|
.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,
|
.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,
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR,
|
||||||
.pNext = nullptr,
|
.pNext = nullptr,
|
||||||
.createFlags = 0,
|
.createFlags = 0,
|
||||||
.buffer = tempAlloc->buffer,
|
.buffer = buffer->buffer,
|
||||||
.offset = 0,
|
.offset = 0,
|
||||||
.size = buildSizesInfo.accelerationStructureSize,
|
.size = buildSizesInfo.accelerationStructureSize,
|
||||||
.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR,
|
.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR,
|
||||||
};
|
};
|
||||||
VkAccelerationStructureKHR tempAS;
|
VK_CHECK(vkCreateAccelerationStructureKHR(graphics->getDevice(), &blasInfo, nullptr, &handle));
|
||||||
VK_CHECK(vkCreateAccelerationStructureKHR(graphics->getDevice(), &tempInfo, nullptr, &tempAS));
|
|
||||||
|
|
||||||
VkBufferCreateInfo scratchInfo = {
|
VkBufferCreateInfo scratchInfo = {
|
||||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
.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,
|
new BufferAllocation(graphics, "ScratchBuffer", scratchInfo, scratchAllocInfo, Gfx::QueueType::GRAPHICS,
|
||||||
graphics->getAccelerationProperties().minAccelerationStructureScratchOffsetAlignment);
|
graphics->getAccelerationProperties().minAccelerationStructureScratchOffsetAlignment);
|
||||||
|
|
||||||
structureBuildGeometry.dstAccelerationStructure = tempAS;
|
structureBuildGeometry.dstAccelerationStructure = handle;
|
||||||
structureBuildGeometry.scratchData.deviceAddress = scratchAlloc->deviceAddress;
|
structureBuildGeometry.scratchData.deviceAddress = scratchAlloc->deviceAddress;
|
||||||
|
|
||||||
VkAccelerationStructureBuildRangeInfoKHR buildRangeInfo = {
|
VkAccelerationStructureBuildRangeInfoKHR buildRangeInfo = {
|
||||||
.primitiveCount = primitiveCount, .primitiveOffset = 0, .firstVertex = 0, .transformOffset = 0};
|
.primitiveCount = primitiveCount,
|
||||||
|
.primitiveOffset = 0,
|
||||||
|
.firstVertex = 0,
|
||||||
|
.transformOffset = 0,
|
||||||
|
};
|
||||||
Array<VkAccelerationStructureBuildRangeInfoKHR*> ranges = {&buildRangeInfo};
|
Array<VkAccelerationStructureBuildRangeInfoKHR*> ranges = {&buildRangeInfo};
|
||||||
|
|
||||||
PCommand cmd = graphics->getGraphicsCommands()->getCommands();
|
PCommand cmd = graphics->getGraphicsCommands()->getCommands();
|
||||||
vkCmdBuildAccelerationStructuresKHR(cmd->getHandle(), 1, &structureBuildGeometry, ranges.data());
|
vkCmdBuildAccelerationStructuresKHR(cmd->getHandle(), 1, &structureBuildGeometry, ranges.data());
|
||||||
cmd->bindResource(PBufferAllocation(transformBuffer));
|
cmd->bindResource(PBufferAllocation(transformBuffer));
|
||||||
cmd->bindResource(PBufferAllocation(tempAlloc));
|
cmd->bindResource(PBufferAllocation(buffer));
|
||||||
cmd->bindResource(PBufferAllocation(scratchAlloc));
|
cmd->bindResource(PBufferAllocation(scratchAlloc));
|
||||||
|
|
||||||
graphics->getDestructionManager()->queueResourceForDestruction(std::move(transformBuffer));
|
graphics->getDestructionManager()->queueResourceForDestruction(std::move(transformBuffer));
|
||||||
graphics->getDestructionManager()->queueResourceForDestruction(std::move(tempAlloc));
|
|
||||||
graphics->getDestructionManager()->queueResourceForDestruction(std::move(scratchAlloc));
|
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() {}
|
TopLevelAS::~TopLevelAS() {}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
#include "Graphics/Initializer.h"
|
#include "Graphics/Initializer.h"
|
||||||
#include "Graphics/RayTracing.h"
|
#include "Graphics/RayTracing.h"
|
||||||
#include <vulkan/vulkan_core.h>
|
#include <vulkan/vulkan_core.h>
|
||||||
|
#include "Buffer.h"
|
||||||
|
|
||||||
namespace Seele {
|
namespace Seele {
|
||||||
namespace Vulkan {
|
namespace Vulkan {
|
||||||
@@ -15,7 +15,8 @@ class BottomLevelAS : public Gfx::BottomLevelAS {
|
|||||||
private:
|
private:
|
||||||
PGraphics graphics;
|
PGraphics graphics;
|
||||||
VkAccelerationStructureKHR handle;
|
VkAccelerationStructureKHR handle;
|
||||||
Gfx::OShaderBuffer buffer;
|
OBufferAllocation buffer;
|
||||||
|
|
||||||
};
|
};
|
||||||
DEFINE_REF(BottomLevelAS)
|
DEFINE_REF(BottomLevelAS)
|
||||||
class TopLevelAS : public Gfx::TopLevelAS {
|
class TopLevelAS : public Gfx::TopLevelAS {
|
||||||
|
|||||||
Reference in New Issue
Block a user