Skip to content

Commit

Permalink
metal wip
Browse files Browse the repository at this point in the history
  • Loading branch information
a-johnston committed Nov 19, 2024
1 parent 728af42 commit a220083
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 26 deletions.
18 changes: 18 additions & 0 deletions drivers/metal/rendering_device_driver_metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,24 @@ class API_AVAILABLE(macos(11.0), ios(14.0)) RenderingDeviceDriverMetal : public

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

#pragma mark - Raytracing

virtual RDD::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_bytes, uint32_t p_index_count, BufferID p_transform_buffer, uint64_t p_transform_offset) override final;
virtual RDD::AccelerationStructureID tlas_create(const LocalVector<AccelerationStructureID> &p_blases) override final;
virtual void acceleration_structure_free(AccelerationStructureID p_acceleration_structure) override final;

// ----- PIPELINE -----

virtual RaytracingPipelineID raytracing_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
virtual void raytracing_pipeline_free(RDD::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;

#pragma mark - Queries

// ----- TIMESTAMP -----
Expand Down
67 changes: 58 additions & 9 deletions drivers/metal/rendering_device_driver_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ struct API_AVAILABLE(macos(11.0), ios(14.0)) ShaderBinaryData {
uint32_t vertex_input_mask = UINT32_MAX;
uint32_t fragment_output_mask = UINT32_MAX;
uint32_t spirv_specialization_constants_ids_mask = UINT32_MAX;
uint32_t is_compute = UINT32_MAX;
uint32_t pipeline_type = (uint32_t)RenderingDevice::PipelineType::RASTERIZATION;
uint32_t needs_view_mask_buffer = UINT32_MAX;
ComputeSize compute_local_size;
PushConstantData push_constant;
Expand All @@ -1538,7 +1538,7 @@ size_t serialize_size() const {
size += sizeof(uint32_t); // vertex_input_mask
size += sizeof(uint32_t); // fragment_output_mask
size += sizeof(uint32_t); // spirv_specialization_constants_ids_mask
size += sizeof(uint32_t); // is_compute
size += sizeof(uint32_t); // pipeline_type
size += sizeof(uint32_t); // needs_view_mask_buffer
size += compute_local_size.serialize_size(); // compute_local_size
size += push_constant.serialize_size(); // push_constant
Expand All @@ -1563,7 +1563,7 @@ void serialize(BufWriter &p_writer) const {
p_writer.write(vertex_input_mask);
p_writer.write(fragment_output_mask);
p_writer.write(spirv_specialization_constants_ids_mask);
p_writer.write(is_compute);
p_writer.write(pipeline_type);
p_writer.write(needs_view_mask_buffer);
p_writer.write(compute_local_size);
p_writer.write(push_constant);
Expand All @@ -1578,7 +1578,7 @@ void deserialize(BufReader &p_reader) {
p_reader.read(vertex_input_mask);
p_reader.read(fragment_output_mask);
p_reader.read(spirv_specialization_constants_ids_mask);
p_reader.read(is_compute);
p_reader.read(pipeline_type);
p_reader.read(needs_view_mask_buffer);
p_reader.read(compute_local_size);
p_reader.read(push_constant);
Expand Down Expand Up @@ -1617,10 +1617,14 @@ void deserialize(BufReader &p_reader) {
ShaderStage stage_flag = (ShaderStage)(1 << p_spirv[i].shader_stage);

if (p_spirv[i].shader_stage == SHADER_STAGE_COMPUTE) {
r_reflection.is_compute = true;
r_reflection.pipeline_type = PipelineType::COMPUTE;
ERR_FAIL_COND_V_MSG(p_spirv.size() != 1, FAILED,
"Compute shaders can only receive one stage, dedicated to compute.");
}
if (p_spirv[i].shader_stage == SHADER_STAGE_RAYGEN || p_spirv[i].shader_stage == SHADER_STAGE_MISS || p_spirv[i].shader_stage == SHADER_STAGE_CLOSEST_HIT) {
r_reflection.pipeline_type = PipelineType::RAYTRACING;
}

ERR_FAIL_COND_V_MSG(r_reflection.stages.has_flag(stage_flag), FAILED,
"Stage " + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + " submitted more than once.");

Expand All @@ -1629,7 +1633,7 @@ void deserialize(BufReader &p_reader) {

Compiler compiler(std::move(pir));

if (r_reflection.is_compute) {
if (r_reflection.pipeline_type == PipelineType::COMPUTE) {
r_reflection.compute_local_size[0] = compiler.get_execution_mode_argument(spv::ExecutionModeLocalSize, 0);
r_reflection.compute_local_size[1] = compiler.get_execution_mode_argument(spv::ExecutionModeLocalSize, 1);
r_reflection.compute_local_size[2] = compiler.get_execution_mode_argument(spv::ExecutionModeLocalSize, 2);
Expand Down Expand Up @@ -1926,7 +1930,7 @@ void deserialize(BufReader &p_reader) {
.y = spirv_data.compute_local_size[1],
.z = spirv_data.compute_local_size[2],
};
bin_data.is_compute = spirv_data.is_compute;
bin_data.pipeline_type = (uint32_t)spirv_data.pipeline_type;
bin_data.push_constant.size = spirv_data.push_constant_size;
bin_data.push_constant.stages = (ShaderStageUsage)(uint8_t)spirv_data.push_constant_stages;
bin_data.needs_view_mask_buffer = shader_meta.has_multiview ? 1 : 0;
Expand Down Expand Up @@ -2479,7 +2483,7 @@ void deserialize(BufReader &p_reader) {
}

MDShader *shader = nullptr;
if (binary_data.is_compute) {
if (binary_data.pipeline_type == (uint32_t)PipelineType::COMPUTE) {
MDComputeShader *cs = new MDComputeShader(binary_data.shader_name, uniform_sets, libraries[ShaderStage::SHADER_STAGE_COMPUTE]);

uint32_t *binding = binary_data.push_constant.msl_binding.getptr(SHADER_STAGE_COMPUTE);
Expand Down Expand Up @@ -2521,7 +2525,7 @@ void deserialize(BufReader &p_reader) {

r_shader_desc.vertex_input_mask = binary_data.vertex_input_mask;
r_shader_desc.fragment_output_mask = binary_data.fragment_output_mask;
r_shader_desc.is_compute = binary_data.is_compute;
r_shader_desc.pipeline_type = (RenderingDevice::PipelineType)binary_data.pipeline_type;
r_shader_desc.compute_local_size[0] = binary_data.compute_local_size.x;
r_shader_desc.compute_local_size[1] = binary_data.compute_local_size.y;
r_shader_desc.compute_local_size[2] = binary_data.compute_local_size.z;
Expand Down Expand Up @@ -3624,6 +3628,51 @@ bool isArrayTexture(MTLTextureType p_type) {
return PipelineID(pipeline);
}

#pragma mark - Raytracing

RDD::AccelerationStructureID RenderingDeviceDriverMetal::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_bytes, uint32_t p_index_count, RDD::BufferID p_transform_buffer, uint64_t p_transform_offset) {
// TODO
return RDD::AccelerationStructureID();
}

RDD::AccelerationStructureID RenderingDeviceDriverMetal::tlas_create(const LocalVector<RDD::AccelerationStructureID> &p_blases) {
// TODO
return RDD::AccelerationStructureID();
}

void RenderingDeviceDriverMetal::acceleration_structure_free(RDD::AccelerationStructureID p_acceleration_structure) {
// TODO
}

// ----- PIPELINE -----

RDD::RaytracingPipelineID RenderingDeviceDriverMetal::raytracing_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) {
// TODO
return RaytracingPipelineID();
}

void RenderingDeviceDriverMetal::raytracing_pipeline_free(RDD::RaytracingPipelineID p_pipeline) {
// TODO
}

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

void RenderingDeviceDriverMetal::command_build_acceleration_structure(CommandBufferID p_cmd_buffer, AccelerationStructureID p_acceleration_structure) {
// TODO
}

void RenderingDeviceDriverMetal::command_bind_raytracing_pipeline(CommandBufferID p_cmd_buffer, RaytracingPipelineID p_pipeline) {
// TODO
}

void RenderingDeviceDriverMetal::command_bind_raytracing_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) {
// TODO
}

void RenderingDeviceDriverMetal::command_raytracing_trace_rays(CommandBufferID p_cmd_buffer, RaytracingPipelineID p_pipeline, ShaderID p_shader, uint32_t p_width, uint32_t p_height) {
// TODO
}

#pragma mark - Queries

// ----- TIMESTAMP -----
Expand Down
24 changes: 24 additions & 0 deletions drivers/vulkan/rendering_device_driver_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5265,7 +5265,12 @@ RDD::PipelineID RenderingDeviceDriverVulkan::render_pipeline_create(
/**** RAYTRACING ****/
/********************/

bool RenderingDeviceDriverVulkan::is_raytracing_supported() {
return raytracing_capabilities.buffer_device_address_support && raytracing_capabilities.acceleration_structure_support && raytracing_capabilities.raytracing_pipeline_support;
}

RDD::AccelerationStructureID RenderingDeviceDriverVulkan::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_bytes, uint32_t p_index_count, BufferID p_transform_buffer, uint64_t p_transform_offset) {
#if !(defined(MACOS_ENABLED) || defined(IOS_ENABLED))
// Vertex positions is first buffer
const VertexFormatInfo *vf_info = (const VertexFormatInfo *)p_vertex_format.id;
VkDeviceSize buffer_offset = vf_info->vk_attributes[0].offset;
Expand Down Expand Up @@ -5322,9 +5327,13 @@ RDD::AccelerationStructureID RenderingDeviceDriverVulkan::blas_create(BufferID p
_acceleration_structure_create(VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR, size_info, accel_info);

return AccelerationStructureID(accel_info);
#else
return AccelerationStructureID();
#endif
}

RDD::AccelerationStructureID RenderingDeviceDriverVulkan::tlas_create(const LocalVector<AccelerationStructureID> &p_blases) {
#if !(defined(MACOS_ENABLED) || defined(IOS_ENABLED))
AccelerationStructureInfo *accel_info = VersatileResource::allocate<AccelerationStructureInfo>(resources_allocator);

for (uint32_t i = 0; i < p_blases.size(); ++i) {
Expand Down Expand Up @@ -5380,9 +5389,13 @@ RDD::AccelerationStructureID RenderingDeviceDriverVulkan::tlas_create(const Loca

_acceleration_structure_create(VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR, size_info, accel_info);
return AccelerationStructureID(accel_info);
#else
return AccelerationStructureID();
#endif
}

void RenderingDeviceDriverVulkan::_acceleration_structure_create(VkAccelerationStructureTypeKHR p_type, VkAccelerationStructureBuildSizesInfoKHR p_size_info, AccelerationStructureInfo *r_accel_info) {
#if !(defined(MACOS_ENABLED) || defined(IOS_ENABLED))
RDD::BufferID buffer = buffer_create(p_size_info.accelerationStructureSize, RDD::BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT | RDD::BUFFER_USAGE_STORAGE_BIT | RDD::BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, RDD::MEMORY_ALLOCATION_TYPE_GPU);
r_accel_info->buffer = buffer;

Expand All @@ -5398,9 +5411,11 @@ void RenderingDeviceDriverVulkan::_acceleration_structure_create(VkAccelerationS
VkResult err = vkCreateAccelerationStructureKHR(vk_device, &blas_create_info, nullptr, &r_accel_info->vk_acceleration_structure);
ERR_FAIL_COND_MSG(err, "vkCreateAccelerationStructureKHR failed with error " + itos(err) + ".");
r_accel_info->build_info.dstAccelerationStructure = r_accel_info->vk_acceleration_structure;
#endif
}

void RenderingDeviceDriverVulkan::acceleration_structure_free(AccelerationStructureID p_acceleration_structure) {
#if !(defined(MACOS_ENABLED) || defined(IOS_ENABLED))
AccelerationStructureInfo *accel_info = (AccelerationStructureInfo *)p_acceleration_structure.id;
if (accel_info->instances_buffer) {
buffer_free(accel_info->instances_buffer);
Expand All @@ -5415,14 +5430,17 @@ void RenderingDeviceDriverVulkan::acceleration_structure_free(AccelerationStruct
vkDestroyAccelerationStructureKHR(vk_device, accel_info->vk_acceleration_structure, nullptr);
}
VersatileResource::free(resources_allocator, accel_info);
#endif
}

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

void RenderingDeviceDriverVulkan::command_build_acceleration_structure(CommandBufferID p_cmd_buffer, AccelerationStructureID p_acceleration_structure) {
#if !(defined(MACOS_ENABLED) || defined(IOS_ENABLED))
const AccelerationStructureInfo *accel_info = (const AccelerationStructureInfo *)p_acceleration_structure.id;
const VkAccelerationStructureBuildRangeInfoKHR *range_info_ptr = &accel_info->range_info;
vkCmdBuildAccelerationStructuresKHR((VkCommandBuffer)p_cmd_buffer.id, 1, &accel_info->build_info, &range_info_ptr);
#endif
}

void RenderingDeviceDriverVulkan::command_bind_raytracing_pipeline(CommandBufferID p_cmd_buffer, RaytracingPipelineID p_pipeline) {
Expand All @@ -5437,6 +5455,7 @@ void RenderingDeviceDriverVulkan::command_bind_raytracing_uniform_set(CommandBuf
}

void RenderingDeviceDriverVulkan::command_raytracing_trace_rays(CommandBufferID p_cmd_buffer, RaytracingPipelineID p_pipeline, ShaderID p_shader, uint32_t p_width, uint32_t p_height) {
#if !(defined(MACOS_ENABLED) || defined(IOS_ENABLED))
ShaderInfo *shader_info = (ShaderInfo *)p_shader.id;
const RaytracingPipelineInfo *pipeline_info = (const RaytracingPipelineInfo *)p_pipeline.id;

Expand Down Expand Up @@ -5471,6 +5490,7 @@ void RenderingDeviceDriverVulkan::command_raytracing_trace_rays(CommandBufferID
buffer_unmap(shader_info->sbt_buffer);

vkCmdTraceRaysKHR((VkCommandBuffer)p_cmd_buffer.id, &shader_info->regions.raygen, &shader_info->regions.miss, &shader_info->regions.closest_hit, &shader_info->regions.call, p_width, p_height, 1);
#endif
}

/*****************/
Expand Down Expand Up @@ -5535,6 +5555,7 @@ RDD::PipelineID RenderingDeviceDriverVulkan::compute_pipeline_create(ShaderID p_
}

RDD::RaytracingPipelineID RenderingDeviceDriverVulkan::raytracing_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) {
#if !(defined(MACOS_ENABLED) || defined(IOS_ENABLED))
const ShaderInfo *shader_info = (const ShaderInfo *)p_shader.id;

VkRayTracingPipelineCreateInfoKHR pipeline_create_info = {};
Expand Down Expand Up @@ -5590,6 +5611,9 @@ RDD::RaytracingPipelineID RenderingDeviceDriverVulkan::raytracing_pipeline_creat
pipeline_info->vk_pipeline = vk_pipeline;

return RaytracingPipelineID(pipeline_info);
#else
return RaytracingPipelineID();
#endif
}

void RenderingDeviceDriverVulkan::raytracing_pipeline_free(RaytracingPipelineID p_pipeline) {
Expand Down
1 change: 1 addition & 0 deletions drivers/vulkan/rendering_device_driver_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {
VkAccelerationStructureBuildRangeInfoKHR range_info;
};

virtual bool is_raytracing_supported() override final;
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_bytes, 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**************************************************************************/
/* render_raytracing.cpp */
/* render_raytracing.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
Expand Down Expand Up @@ -5065,4 +5065,3 @@ RenderRaytracing::~RenderRaytracing() {
sdfgi_framebuffer_size_cache.remove(sdfgi_framebuffer_size_cache.begin());
}
}

9 changes: 4 additions & 5 deletions servers/rendering/renderer_rd/raytracing/render_raytracing.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**************************************************************************/
/* render_raytracing.h */
/* render_raytracing.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
Expand Down Expand Up @@ -28,8 +28,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef RENDER_RAYTRACING_H
#define RENDER_RAYTRACING_H
#ifndef RENDER_RAYTRACING_RD_H
#define RENDER_RAYTRACING_RD_H

#include "core/templates/paged_allocator.h"
#include "servers/rendering/renderer_rd/cluster_builder_rd.h"
Expand Down Expand Up @@ -757,5 +757,4 @@ class RenderRaytracing : public RendererSceneRenderRD {
};
} // namespace RendererSceneRenderImplementation

#endif // RENDER_RAYTRACING_H

#endif // RENDER_RAYTRACING_RD_H
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**************************************************************************/
/* scene_shader_raytracing.cpp */
/* scene_shader_raytracing.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
Expand Down Expand Up @@ -930,4 +930,3 @@ uint32_t SceneShaderRaytracing::get_pipeline_compilations(RS::PipelineSource p_s
MutexLock lock(SceneShaderRaytracing::singleton_mutex);
return pipeline_compilations[p_source];
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**************************************************************************/
/* scene_shader_raytracing.h */
/* scene_shader_raytracing.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
Expand Down Expand Up @@ -28,8 +28,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef SCENE_SHADER_RAYTRACING_H
#define SCENE_SHADER_RAYTRACING_H
#ifndef SCENE_SHADER_RAYTRACING_RD_H
#define SCENE_SHADER_RAYTRACING_RD_H

#include "servers/rendering/renderer_rd/pipeline_hash_map_rd.h"
#include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
Expand Down Expand Up @@ -369,5 +369,4 @@ class SceneShaderRaytracing {

} // namespace RendererSceneRenderImplementation

#endif // SCENE_SHADER_RAYTRACING_H

#endif // SCENE_SHADER_RAYTRACING_RD_H
2 changes: 1 addition & 1 deletion servers/rendering/renderer_rd/renderer_compositor_rd.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
#include "servers/rendering/renderer_rd/environment/fog.h"
#include "servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.h"
#include "servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.h"
#include "servers/rendering/renderer_rd/raytracing/render_raytracing.h"
#include "servers/rendering/renderer_rd/framebuffer_cache_rd.h"
#include "servers/rendering/renderer_rd/raytracing/render_raytracing.h"
#include "servers/rendering/renderer_rd/renderer_canvas_render_rd.h"
#include "servers/rendering/renderer_rd/shaders/blit.glsl.gen.h"
#include "servers/rendering/renderer_rd/storage_rd/light_storage.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ layout(set = 0, binding = 1) uniform accelerationStructureEXT tlas;

layout(set = 0, binding = 2, std140) uniform SceneDataBlock {
SceneData data;
} scene_data_block;

}
scene_data_block;

void main() {
const vec2 pixel_center = vec2(gl_LaunchIDEXT.xy) + vec2(0.5);
Expand Down
Loading

0 comments on commit a220083

Please sign in to comment.