Skip to content

Commit da81105

Browse files
committed
raytracing: Initial Vulkan support
- Vulkan implementations in `RenderingDeviceDriverVulkan`. - Raytracing instruction list in `RenderingDeviceGraph`. - Functions to create acceleration structures and raytracing pipelines in `RenderingDevice`. - Raygen, Miss, and ClosestHit shader stages support. - GDScript bindings. - Update classes documentation. - Include a-johnston RenderingDeviceDriverMetal changes. - Unimplemented placeholders for Metal and D3D12. - Apply Mickeon docs suggestions. - Build acceleration structure command. - Expose a shader preprocessor define. - Align build scratch address. - Create STB after creating the pipeline. - Separate acceleration structure barriers. - Use transforms in TLAS instances. - Other modifications suggested by DarioSamo. - AnyHit and Intersection stages. - Optionally set acceleration structure build input buffer usage. - Introduce instances buffer. - Move scratch buffers to RenderingDevice. - Rename AccelerationStructureGeometryBits. - Use VectorView. - Use regular buffer creation and free routines for scratch buffer. - Store trackers in acceleration structures. - Bump Shader SPIR-V version to 1.4 - Add Position attribute location in blas_create. - Encapsulate MoltenVK check in preprocessor macro. - Split SUPPORTS_RAYTRACING for pipeline and query.
1 parent 79603b2 commit da81105

28 files changed

+2994
-114
lines changed

doc/classes/RDShaderSPIRV.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,27 @@
4242
</method>
4343
</methods>
4444
<members>
45+
<member name="bytecode_any_hit" type="PackedByteArray" setter="set_stage_bytecode" getter="get_stage_bytecode" default="PackedByteArray()">
46+
The SPIR-V bytecode for the any hit shader stage.
47+
</member>
48+
<member name="bytecode_closest_hit" type="PackedByteArray" setter="set_stage_bytecode" getter="get_stage_bytecode" default="PackedByteArray()">
49+
The SPIR-V bytecode for the closest hit shader stage.
50+
</member>
4551
<member name="bytecode_compute" type="PackedByteArray" setter="set_stage_bytecode" getter="get_stage_bytecode" default="PackedByteArray()">
4652
The SPIR-V bytecode for the compute shader stage.
4753
</member>
4854
<member name="bytecode_fragment" type="PackedByteArray" setter="set_stage_bytecode" getter="get_stage_bytecode" default="PackedByteArray()">
4955
The SPIR-V bytecode for the fragment shader stage.
5056
</member>
57+
<member name="bytecode_intersection" type="PackedByteArray" setter="set_stage_bytecode" getter="get_stage_bytecode" default="PackedByteArray()">
58+
The SPIR-V bytecode for the intersection shader stage.
59+
</member>
60+
<member name="bytecode_miss" type="PackedByteArray" setter="set_stage_bytecode" getter="get_stage_bytecode" default="PackedByteArray()">
61+
The SPIR-V bytecode for the miss shader stage.
62+
</member>
63+
<member name="bytecode_raygen" type="PackedByteArray" setter="set_stage_bytecode" getter="get_stage_bytecode" default="PackedByteArray()">
64+
The SPIR-V bytecode for the ray generation shader stage.
65+
</member>
5166
<member name="bytecode_tesselation_control" type="PackedByteArray" setter="set_stage_bytecode" getter="get_stage_bytecode" default="PackedByteArray()">
5267
The SPIR-V bytecode for the tessellation control shader stage.
5368
</member>
@@ -57,12 +72,27 @@
5772
<member name="bytecode_vertex" type="PackedByteArray" setter="set_stage_bytecode" getter="get_stage_bytecode" default="PackedByteArray()">
5873
The SPIR-V bytecode for the vertex shader stage.
5974
</member>
75+
<member name="compile_error_any_hit" type="String" setter="set_stage_compile_error" getter="get_stage_compile_error" default="&quot;&quot;">
76+
The compilation error message for the any hit shader stage (set by the SPIR-V compiler and Godot). If empty, shader compilation was successful.
77+
</member>
78+
<member name="compile_error_closest_hit" type="String" setter="set_stage_compile_error" getter="get_stage_compile_error" default="&quot;&quot;">
79+
The compilation error message for the closest hit shader stage (set by the SPIR-V compiler and Godot). If empty, shader compilation was successful.
80+
</member>
6081
<member name="compile_error_compute" type="String" setter="set_stage_compile_error" getter="get_stage_compile_error" default="&quot;&quot;">
6182
The compilation error message for the compute shader stage (set by the SPIR-V compiler and Godot). If empty, shader compilation was successful.
6283
</member>
6384
<member name="compile_error_fragment" type="String" setter="set_stage_compile_error" getter="get_stage_compile_error" default="&quot;&quot;">
6485
The compilation error message for the fragment shader stage (set by the SPIR-V compiler and Godot). If empty, shader compilation was successful.
6586
</member>
87+
<member name="compile_error_intersection" type="String" setter="set_stage_compile_error" getter="get_stage_compile_error" default="&quot;&quot;">
88+
The compilation error message for the intersection shader stage (set by the SPIR-V compiler and Godot). If empty, shader compilation was successful.
89+
</member>
90+
<member name="compile_error_miss" type="String" setter="set_stage_compile_error" getter="get_stage_compile_error" default="&quot;&quot;">
91+
The compilation error message for the miss shader stage (set by the SPIR-V compiler and Godot). If empty, shader compilation was successful.
92+
</member>
93+
<member name="compile_error_raygen" type="String" setter="set_stage_compile_error" getter="get_stage_compile_error" default="&quot;&quot;">
94+
The compilation error message for the ray generation shader stage (set by the SPIR-V compiler and Godot). If empty, shader compilation was successful.
95+
</member>
6696
<member name="compile_error_tesselation_control" type="String" setter="set_stage_compile_error" getter="get_stage_compile_error" default="&quot;&quot;">
6797
The compilation error message for the tessellation control shader stage (set by the SPIR-V compiler and Godot). If empty, shader compilation was successful.
6898
</member>

doc/classes/RDShaderSource.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,27 @@
3131
<member name="language" type="int" setter="set_language" getter="get_language" enum="RenderingDevice.ShaderLanguage" default="0">
3232
The language the shader is written in.
3333
</member>
34+
<member name="source_any_hit" type="String" setter="set_stage_source" getter="get_stage_source" default="&quot;&quot;">
35+
Source code for the shader's any hit stage.
36+
</member>
37+
<member name="source_closest_hit" type="String" setter="set_stage_source" getter="get_stage_source" default="&quot;&quot;">
38+
Source code for the shader's closest hit stage.
39+
</member>
3440
<member name="source_compute" type="String" setter="set_stage_source" getter="get_stage_source" default="&quot;&quot;">
3541
Source code for the shader's compute stage.
3642
</member>
3743
<member name="source_fragment" type="String" setter="set_stage_source" getter="get_stage_source" default="&quot;&quot;">
3844
Source code for the shader's fragment stage.
3945
</member>
46+
<member name="source_intersection" type="String" setter="set_stage_source" getter="get_stage_source" default="&quot;&quot;">
47+
Source code for the shader's intersection stage.
48+
</member>
49+
<member name="source_miss" type="String" setter="set_stage_source" getter="get_stage_source" default="&quot;&quot;">
50+
Source code for the shader's miss stage.
51+
</member>
52+
<member name="source_raygen" type="String" setter="set_stage_source" getter="get_stage_source" default="&quot;&quot;">
53+
Source code for the shader's ray generation stage.
54+
</member>
4055
<member name="source_tesselation_control" type="String" setter="set_stage_source" getter="get_stage_source" default="&quot;&quot;">
4156
Source code for the shader's tessellation control stage.
4257
</member>

doc/classes/RenderingDevice.xml

Lines changed: 200 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
<link title="Using compute shaders">$DOCS_URL/tutorials/shaders/compute_shaders.html</link>
1515
</tutorials>
1616
<methods>
17+
<method name="acceleration_structure_build">
18+
<return type="int" enum="Error" />
19+
<param index="0" name="acceleration_structure" type="RID" />
20+
<description>
21+
Builds the [param acceleration_structure].
22+
</description>
23+
</method>
1724
<method name="barrier" deprecated="Barriers are automatically inserted by RenderingDevice.">
1825
<return type="void" />
1926
<param index="0" name="from" type="int" enum="RenderingDevice.BarrierMask" is_bitfield="true" default="32767" />
@@ -22,6 +29,18 @@
2229
This method does nothing.
2330
</description>
2431
</method>
32+
<method name="blas_create">
33+
<return type="RID" />
34+
<param index="0" name="vertex_array" type="RID" />
35+
<param index="1" name="index_array" type="RID" />
36+
<param index="2" name="geometry_bits" type="int" enum="RenderingDevice.AccelerationStructureGeometryBits" is_bitfield="true" default="0" />
37+
<param index="3" name="position_attribute_location" type="int" default="0" />
38+
<description>
39+
Creates a new Bottom Level Acceleration Structure. It can be accessed with the RID that is returned.
40+
Once finished with your RID, you will want to free the RID using the RenderingDevice's [method free_rid] method.
41+
[param position_attribute_location] selects which vertex attribute location supplies the position data (default is 0).
42+
</description>
43+
</method>
2544
<method name="buffer_clear">
2645
<return type="int" enum="Error" />
2746
<param index="0" name="buffer" type="RID" />
@@ -732,6 +751,102 @@
732751
Limits for various graphics hardware can be found in the [url=https://vulkan.gpuinfo.org/]Vulkan Hardware Database[/url].
733752
</description>
734753
</method>
754+
<method name="raytracing_list_begin">
755+
<return type="int" />
756+
<description>
757+
Starts a list of raytracing commands. The returned value should be passed to other [code]raytracing_list_*[/code] functions.
758+
Multiple raytracing lists cannot be created at the same time; you must finish the previous raytracing list first using [method raytracing_list_end].
759+
A simple raytracing operation might look like this (code is not a complete example):
760+
[codeblocks]
761+
[gdscript]
762+
var rd = RenderingDevice.new()
763+
assert(rd.has_feature(RenderingDevice.SUPPORTS_RAYTRACING_PIPELINE))
764+
765+
# Create a BLAS for a mesh.
766+
blas = rd.blas_create(vertex_array, index_array, RenderingDevice.ACCELERATION_STRUCTURE_GEOMETRY_OPAQUE)
767+
# Create TLAS with BLASs.
768+
instances_buffer = rd.tlas_instances_buffer_create(1)
769+
rd.tlas_instances_buffer_fill(instances_buffer, [blas], [Transform3D()])
770+
tlas = rd.tlas_create(instances_buffer)
771+
772+
# Build acceleration structures.
773+
rd.acceleration_structure_build(blas)
774+
rd.acceleration_structure_build(tlas)
775+
776+
var raylist = rd.raytracing_list_begin()
777+
778+
# Bind pipeline and uniforms.
779+
rd.raytracing_list_bind_raytracing_pipeline(raylist, raytracing_pipeline)
780+
rd.raytracing_list_bind_uniform_set(raylist, uniform_set, 0)
781+
782+
# Trace rays.
783+
var width = get_viewport().size.x
784+
var height = get_viewport().size.y
785+
rd.raytracing_list_trace_rays(raylist, width, height)
786+
787+
rd.raytracing_list_end()
788+
[/gdscript]
789+
[/codeblocks]
790+
</description>
791+
</method>
792+
<method name="raytracing_list_bind_raytracing_pipeline">
793+
<return type="void" />
794+
<param index="0" name="raytracing_list" type="int" />
795+
<param index="1" name="raytracing_pipeline" type="RID" />
796+
<description>
797+
Binds [param raytracing_pipeline] to the specified [param raytracing_list].
798+
</description>
799+
</method>
800+
<method name="raytracing_list_bind_uniform_set">
801+
<return type="void" />
802+
<param index="0" name="raytracing_list" type="int" />
803+
<param index="1" name="uniform_set" type="RID" />
804+
<param index="2" name="set_index" type="int" />
805+
<description>
806+
Binds the [param uniform_set] to this [param raytracing_list].
807+
</description>
808+
</method>
809+
<method name="raytracing_list_end">
810+
<return type="void" />
811+
<description>
812+
Finishes a list of raytracing commands created with the [code]raytracing_*[/code] methods.
813+
</description>
814+
</method>
815+
<method name="raytracing_list_set_push_constant">
816+
<return type="void" />
817+
<param index="0" name="raytracing_list" type="int" />
818+
<param index="1" name="buffer" type="PackedByteArray" />
819+
<param index="2" name="size_bytes" type="int" />
820+
<description>
821+
Sets the push constant data to [param buffer] for the specified [param raytracing_list]. The shader determines how this binary data is used. The buffer's size in bytes must also be specified in [param size_bytes] (this can be obtained by calling the [method PackedByteArray.size] method on the passed [param buffer]).
822+
</description>
823+
</method>
824+
<method name="raytracing_list_trace_rays">
825+
<return type="void" />
826+
<param index="0" name="raytracing_list" type="int" />
827+
<param index="1" name="width" type="int" />
828+
<param index="2" name="height" type="int" />
829+
<description>
830+
Initializes a ray tracing dispatch for the specified [param raytracing_list] assembling a group of [param width] x [param height] rays.
831+
</description>
832+
</method>
833+
<method name="raytracing_pipeline_create">
834+
<return type="RID" />
835+
<param index="0" name="shader" type="RID" />
836+
<param index="1" name="specialization_constants" type="RDPipelineSpecializationConstant[]" default="[]" />
837+
<description>
838+
Creates a new raytracing pipeline. It can be accessed with the RID that is returned.
839+
Once finished with your RID, you will want to free the RID using the RenderingDevice's [method free_rid] method.
840+
[b]Note:[/b]: Recursive raytracing is not permitted.
841+
</description>
842+
</method>
843+
<method name="raytracing_pipeline_is_valid">
844+
<return type="bool" />
845+
<param index="0" name="raytracing_pipeline" type="RID" />
846+
<description>
847+
Returns [code]true[/code] if the raytracing pipeline specified by the [param raytracing_pipeline] RID is valid, [code]false[/code] otherwise.
848+
</description>
849+
</method>
735850
<method name="render_pipeline_create">
736851
<return type="RID" />
737852
<param index="0" name="shader" type="RID" />
@@ -1089,6 +1204,33 @@
10891204
[b]Note:[/b] The existing [param texture] requires the [constant TEXTURE_USAGE_CAN_UPDATE_BIT] to be updatable.
10901205
</description>
10911206
</method>
1207+
<method name="tlas_create">
1208+
<return type="RID" />
1209+
<param index="0" name="instances_buffer" type="RID" />
1210+
<description>
1211+
Creates a new Top Level Acceleration Structure. It can be accessed with the RID that is returned.
1212+
The instances buffer passed as input is expected to be filled before building the TLAS.
1213+
Once finished with your RID, you will want to free the RID using the RenderingDevice's [method free_rid] method.
1214+
</description>
1215+
</method>
1216+
<method name="tlas_instances_buffer_create">
1217+
<return type="RID" />
1218+
<param index="0" name="instance_count" type="int" />
1219+
<param index="1" name="creation_bits" type="int" enum="RenderingDevice.BufferCreationBits" is_bitfield="true" default="0" />
1220+
<description>
1221+
Creates a new instances buffer which can be used to create a TLAS. It can be accessed with the RID that is returned.
1222+
Once finished with your RID, you will want to free the RID using the RenderingDevice's [method free_rid] method.
1223+
</description>
1224+
</method>
1225+
<method name="tlas_instances_buffer_fill">
1226+
<return type="void" />
1227+
<param index="0" name="instances_buffer" type="RID" />
1228+
<param index="1" name="blases" type="RID[]" />
1229+
<param index="2" name="transforms" type="Transform3D[]" />
1230+
<description>
1231+
Fills the content of an instances buffer. The number of BLASes and transforms passed as input should be the same and should equal the instance count used at instance buffer creation time.
1232+
</description>
1233+
</method>
10921234
<method name="uniform_buffer_create">
10931235
<return type="RID" />
10941236
<param index="0" name="size_bytes" type="int" />
@@ -2145,6 +2287,23 @@
21452287
<constant name="BUFFER_CREATION_AS_STORAGE_BIT" value="2" enum="BufferCreationBits" is_bitfield="true">
21462288
Set this flag so that it is created as storage. This is useful if Compute Shaders need access (for reading or writing) to the buffer, e.g. skeletal animations are processed in Compute Shaders which need access to vertex buffers, to be later consumed by vertex shaders as part of the regular rasterization pipeline.
21472289
</constant>
2290+
<constant name="BUFFER_CREATION_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT" value="8" enum="BufferCreationBits" is_bitfield="true">
2291+
Allows usage of this buffer as input data for an acceleration structure build operation. You must first check the GPU supports it:
2292+
[codeblocks]
2293+
[gdscript]
2294+
rd = RenderingServer.get_rendering_device()
2295+
2296+
if rd.has_feature(RenderingDevice.SUPPORTS_RAYTRACING_PIPELINE):
2297+
storage_buffer = rd.storage_buffer_create(bytes.size(), bytes, RenderingDevice.BUFFER_CREATION_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT)
2298+
[/gdscript]
2299+
[/codeblocks]
2300+
</constant>
2301+
<constant name="ACCELERATION_STRUCTURE_GEOMETRY_OPAQUE" value="1" enum="AccelerationStructureGeometryBits" is_bitfield="true">
2302+
An opaque geometry does not invoke the any-hit shaders.
2303+
</constant>
2304+
<constant name="ACCELERATION_STRUCTURE_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION" value="2" enum="AccelerationStructureGeometryBits" is_bitfield="true">
2305+
This geometry only calls the any-hit shader a single time for each primitive.
2306+
</constant>
21482307
<constant name="UNIFORM_TYPE_SAMPLER" value="0" enum="UniformType">
21492308
Sampler uniform.
21502309
</constant>
@@ -2185,7 +2344,10 @@
21852344
[b]Note:[/b] This flag is not available to GD users due to being too dangerous (i.e. wrong usage can result in visual glitches).
21862345
It's exposed in case GD users receive a buffer created with such flag from Godot.
21872346
</constant>
2188-
<constant name="UNIFORM_TYPE_MAX" value="12" enum="UniformType">
2347+
<constant name="UNIFORM_TYPE_ACCELERATION_STRUCTURE" value="12" enum="UniformType">
2348+
Acceleration structure uniform.
2349+
</constant>
2350+
<constant name="UNIFORM_TYPE_MAX" value="13" enum="UniformType">
21892351
Represents the size of the [enum UniformType] enum.
21902352
</constant>
21912353
<constant name="RENDER_PRIMITIVE_POINTS" value="0" enum="RenderPrimitive">
@@ -2493,7 +2655,22 @@
24932655
<constant name="SHADER_STAGE_COMPUTE" value="4" enum="ShaderStage">
24942656
Compute shader stage. This can be used to run arbitrary computing tasks in a shader, performing them on the GPU instead of the CPU.
24952657
</constant>
2496-
<constant name="SHADER_STAGE_MAX" value="5" enum="ShaderStage">
2658+
<constant name="SHADER_STAGE_RAYGEN" value="5" enum="ShaderStage">
2659+
Ray generation shader stage. This can be used to generate primary rays.
2660+
</constant>
2661+
<constant name="SHADER_STAGE_ANY_HIT" value="6" enum="ShaderStage">
2662+
Any hit shader stage. Invoked when ray intersections are not opaque. This can be used when to specify what happens when a ray hits any of the geometry in the scene.
2663+
</constant>
2664+
<constant name="SHADER_STAGE_CLOSEST_HIT" value="7" enum="ShaderStage">
2665+
Closest hit shader stage. This can be used to specify what happens when a ray hits the closest geometry in the scene.
2666+
</constant>
2667+
<constant name="SHADER_STAGE_MISS" value="8" enum="ShaderStage">
2668+
Miss shader stage. This can be used to specify what happens if a ray does not hit anything in the scene.
2669+
</constant>
2670+
<constant name="SHADER_STAGE_INTERSECTION" value="9" enum="ShaderStage">
2671+
Intersection shader stage. The intersection shader for triangles is built-in. This can be used to compute ray intersections with primitives that are not triangles.
2672+
</constant>
2673+
<constant name="SHADER_STAGE_MAX" value="10" enum="ShaderStage">
24972674
Represents the size of the [enum ShaderStage] enum.
24982675
</constant>
24992676
<constant name="SHADER_STAGE_VERTEX_BIT" value="1" enum="ShaderStage">
@@ -2511,6 +2688,21 @@
25112688
<constant name="SHADER_STAGE_COMPUTE_BIT" value="16" enum="ShaderStage">
25122689
Compute shader stage bit (see also [constant SHADER_STAGE_COMPUTE]).
25132690
</constant>
2691+
<constant name="SHADER_STAGE_RAYGEN_BIT" value="32" enum="ShaderStage">
2692+
Ray generation shader stage bit (see also [constant SHADER_STAGE_RAYGEN]).
2693+
</constant>
2694+
<constant name="SHADER_STAGE_ANY_HIT_BIT" value="64" enum="ShaderStage">
2695+
Any hit shader stage bit (see also [constant SHADER_STAGE_ANY_HIT]).
2696+
</constant>
2697+
<constant name="SHADER_STAGE_CLOSEST_HIT_BIT" value="128" enum="ShaderStage">
2698+
Closest hit shader stage bit (see also [constant SHADER_STAGE_CLOSEST_HIT]).
2699+
</constant>
2700+
<constant name="SHADER_STAGE_MISS_BIT" value="256" enum="ShaderStage">
2701+
Miss shader stage bit (see also [constant SHADER_STAGE_MISS]).
2702+
</constant>
2703+
<constant name="SHADER_STAGE_INTERSECTION_BIT" value="512" enum="ShaderStage">
2704+
Intersection shader stage bit (see also [constant SHADER_STAGE_INTERSECTION]).
2705+
</constant>
25142706
<constant name="SHADER_LANGUAGE_GLSL" value="0" enum="ShaderLanguage">
25152707
Khronos' GLSL shading language (used natively by OpenGL and Vulkan). This is the language used for core Godot shaders.
25162708
</constant>
@@ -2538,6 +2730,12 @@
25382730
<constant name="SUPPORTS_IMAGE_ATOMIC_32_BIT" value="7" enum="Features">
25392731
Support for 32-bit image atomic operations.
25402732
</constant>
2733+
<constant name="SUPPORTS_RAY_QUERY" value="11" enum="Features">
2734+
Support for ray query extension.
2735+
</constant>
2736+
<constant name="SUPPORTS_RAYTRACING_PIPELINE" value="12" enum="Features">
2737+
Support for raytracing pipeline extension.
2738+
</constant>
25412739
<constant name="LIMIT_MAX_BOUND_UNIFORM_SETS" value="0" enum="Limit">
25422740
Maximum number of uniform sets that can be bound at a given time.
25432741
</constant>

0 commit comments

Comments
 (0)