Skip to content

Commit

Permalink
raytracing: Initial Vulkan support
Browse files Browse the repository at this point in the history
- Vulkan implementations in `RenderingDeviceDriverVulkan`
- Raytracing instruction list in `RenderingDeviceGraph`
- Functions to create acceleration structures and raytracing pipelines
  in `RenderingDevice`
  • Loading branch information
Fahien committed Nov 12, 2024
1 parent ec6a1c0 commit 4ff001d
Show file tree
Hide file tree
Showing 9 changed files with 1,563 additions and 17 deletions.
547 changes: 545 additions & 2 deletions drivers/vulkan/rendering_device_driver_vulkan.cpp

Large diffs are not rendered by default.

83 changes: 82 additions & 1 deletion drivers/vulkan/rendering_device_driver_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {
bool storage_input_output_16 = false;
};

struct RaytracingCapabilities {
bool buffer_device_address_support = false;
bool acceleration_structure_support = false;
bool raytracing_pipeline_support = false;
uint32_t shader_group_handle_size = 0;
uint32_t shader_group_handle_alignment = 0;
uint32_t shader_group_handle_size_aligned = 0;
uint32_t shader_group_base_alignment = 0;
bool validation = false;
};

struct DeviceFunctions {
PFN_vkCreateSwapchainKHR CreateSwapchainKHR = nullptr;
PFN_vkDestroySwapchainKHR DestroySwapchainKHR = nullptr;
Expand All @@ -116,6 +127,10 @@ class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {

// Debug device fault.
PFN_vkGetDeviceFaultInfoEXT GetDeviceFaultInfoEXT = nullptr;

// Raytracing extensions.
PFN_vkCreateAccelerationStructureKHR CreateAccelerationStructureKHR = nullptr;
PFN_vkCreateRayTracingPipelinesKHR CreateRaytracingPipelinesKHR = nullptr;
};
// Debug marker extensions.
VkDebugReportObjectTypeEXT _convert_to_debug_report_objectType(VkObjectType p_object_type);
Expand All @@ -138,6 +153,7 @@ class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {
VRSCapabilities vrs_capabilities;
ShaderCapabilities shader_capabilities;
StorageBufferCapabilities storage_buffer_capabilities;
RaytracingCapabilities raytracing_capabilities;
bool pipeline_cache_control_support = false;
bool device_fault_support = false;
#if defined(VK_TRACK_DEVICE_MEMORY)
Expand Down Expand Up @@ -198,6 +214,10 @@ class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {
VkBufferView vk_view = VK_NULL_HANDLE; // For texel buffers.
};

private:
VkDeviceAddress _buffer_get_device_address(BufferID p_buffer);

public:
virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type) override final;
virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) override final;
virtual void buffer_free(BufferID p_buffer) override final;
Expand Down Expand Up @@ -424,7 +444,7 @@ class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {
uint64_t vertex_input_mask = 0;
uint32_t fragment_output_mask = 0;
uint32_t specialization_constants_count = 0;
uint32_t is_compute = 0;
PipelineType pipeline_type = PipelineType::RASTERIZATION;
uint32_t compute_local_size[3] = {};
uint32_t set_count = 0;
uint32_t push_constant_size = 0;
Expand All @@ -434,11 +454,28 @@ class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {
};
};

struct RaytracingShaderRegions {
VkStridedDeviceAddressRegionKHR raygen;
uint32_t raygen_count = 0;
VkStridedDeviceAddressRegionKHR miss;
uint32_t miss_count = 0;
VkStridedDeviceAddressRegionKHR closest_hit;
uint32_t closest_hit_count = 0;
VkStridedDeviceAddressRegionKHR call;
uint32_t group_count = 0;

// Size of one shader group handle
LocalVector<uint8_t> handles_data;
};

struct ShaderInfo {
VkShaderStageFlags vk_push_constant_stages = 0;
TightLocalVector<VkPipelineShaderStageCreateInfo> vk_stages_create_info;
TightLocalVector<VkRayTracingShaderGroupCreateInfoKHR> vk_groups_create_info;
TightLocalVector<VkDescriptorSetLayout> vk_descriptor_set_layouts;
VkPipelineLayout vk_pipeline_layout = VK_NULL_HANDLE;
RaytracingShaderRegions regions;
BufferID sbt_buffer;
};

public:
Expand Down Expand Up @@ -626,6 +663,50 @@ class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {

virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;

/********************/
/**** RAYTRACING ****/
/********************/
struct AccelerationStructureInfo {
VkAccelerationStructureKHR vk_acceleration_structure = VK_NULL_HANDLE;
// Buffer used for the structure
RDD::BufferID buffer;
// Buffer used for building the structure
RDD::BufferID scratch_buffer;
// Buffer used for instances in a TLAS
RDD::BufferID instances_buffer;

// Required for building
VkAccelerationStructureGeometryKHR geometry;
LocalVector<VkAccelerationStructureInstanceKHR> instances;
VkAccelerationStructureBuildGeometryInfoKHR build_info;
VkAccelerationStructureBuildRangeInfoKHR range_info;
};

virtual AccelerationStructureID blas_create(BufferID p_vertex_buffer, uint64_t p_vertex_offset, VertexFormatID p_vertex_format, uint32_t p_vertex_count, BufferID p_index_buffer, IndexBufferFormat p_index_format, uint64_t p_index_offset, uint32_t p_index_count, BufferID p_transform_buffer, uint64_t p_transform_offset) override final;
virtual AccelerationStructureID tlas_create(const LocalVector<AccelerationStructureID> &p_blases) override final;
virtual void acceleration_structure_free(AccelerationStructureID p_acceleration_structure) override final;

private:
void _acceleration_structure_create(VkAccelerationStructureTypeKHR p_type, VkAccelerationStructureBuildSizesInfoKHR p_size_info, AccelerationStructureInfo *r_accel_info);

public:
// ----- PIPELINE -----

struct RaytracingPipelineInfo {
VkPipeline vk_pipeline;
};

virtual RaytracingPipelineID raytracing_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
virtual void raytracing_pipeline_free(RaytracingPipelineID p_pipeline) override final;

// ----- COMMANDS -----

virtual void command_build_acceleration_structure(CommandBufferID p_cmd_buffer, AccelerationStructureID p_acceleration_structure) override final;
virtual void command_bind_raytracing_pipeline(CommandBufferID p_cmd_buffer, RaytracingPipelineID p_pipeline) override final;
virtual void command_bind_raytracing_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
virtual void command_raytracing_trace_rays(CommandBufferID p_cmd_buffer, RaytracingPipelineID p_pipeline, ShaderID p_shader, uint32_t p_width, uint32_t p_height) override final;

public:
/*****************/
/**** QUERIES ****/
/*****************/
Expand Down
Loading

0 comments on commit 4ff001d

Please sign in to comment.