Skip to content

Commit 0df157f

Browse files
committed
Add reminder to free RID's in compute shader tutorial
1 parent da17e45 commit 0df157f

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

tutorials/shaders/compute_shaders.rst

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ which can be created using the :ref:`class_RenderingServer`:
163163
.. code-tab:: gdscript GDScript
164164

165165
# Create a local rendering device.
166-
var rd := RenderingServer.create_local_rendering_device()
166+
var rd = RenderingServer.create_local_rendering_device()
167167

168168
.. code-tab:: csharp
169169

@@ -177,9 +177,9 @@ and create a precompiled version of it using this:
177177
.. code-tab:: gdscript GDScript
178178

179179
# Load GLSL shader
180-
var shader_file := load("res://compute_example.glsl")
181-
var shader_spirv: RDShaderSPIRV = shader_file.get_spirv()
182-
var shader := rd.shader_create_from_spirv(shader_spirv)
180+
var shader_file = load("res://compute_example.glsl")
181+
var shader_spirv = shader_file.get_spirv()
182+
var shader = rd.shader_create_from_spirv(shader_spirv)
183183

184184
.. code-tab:: csharp
185185

@@ -210,12 +210,12 @@ So let's initialize an array of floats and create a storage buffer:
210210
.. code-tab:: gdscript GDScript
211211

212212
# Prepare our data. We use floats in the shader, so we need 32 bit.
213-
var input := PackedFloat32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
214-
var input_bytes := input.to_byte_array()
213+
var input = PackedFloat32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
214+
var input_bytes = input.to_byte_array()
215215

216216
# Create a storage buffer that can hold our float values.
217217
# Each float has 4 bytes (32 bit) so 10 x 4 = 40 bytes
218-
var buffer := rd.storage_buffer_create(input_bytes.size(), input_bytes)
218+
var buffer = rd.storage_buffer_create(input_bytes.size(), input_bytes)
219219

220220
.. code-tab:: csharp
221221

@@ -236,11 +236,11 @@ assign it to a uniform set which we can pass to our shader later.
236236
.. code-tab:: gdscript GDScript
237237

238238
# Create a uniform to assign the buffer to the rendering device
239-
var uniform := RDUniform.new()
239+
var uniform = RDUniform.new()
240240
uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_STORAGE_BUFFER
241241
uniform.binding = 0 # this needs to match the "binding" in our shader file
242242
uniform.add_id(buffer)
243-
var uniform_set := rd.uniform_set_create([uniform], shader, 0) # the last parameter (the 0) needs to match the "set" in our shader file
243+
var uniform_set = rd.uniform_set_create([uniform], shader, 0) # the last parameter (the 0) needs to match the "set" in our shader file
244244

245245
.. code-tab:: csharp
246246

@@ -273,8 +273,8 @@ The steps we need to do to compute our result are:
273273
.. code-tab:: gdscript GDScript
274274

275275
# Create a compute pipeline
276-
var pipeline := rd.compute_pipeline_create(shader)
277-
var compute_list := rd.compute_list_begin()
276+
var pipeline = rd.compute_pipeline_create(shader)
277+
var compute_list = rd.compute_list_begin()
278278
rd.compute_list_bind_compute_pipeline(compute_list, pipeline)
279279
rd.compute_list_bind_uniform_set(compute_list, uniform_set, 0)
280280
rd.compute_list_dispatch(compute_list, 5, 1, 1)
@@ -366,6 +366,11 @@ the data and print the results to our console.
366366
GD.Print("Input: ", string.Join(", ", input));
367367
GD.Print("Output: ", string.Join(", ", output));
368368

369+
Freeing memory
370+
------------------
371+
372+
The ``buffer``, ``pipeline``, and ``uniform_set`` variables we've been using are each an :ref:`class_RID`. Because RenderingDevice is meant to be a lower-level API, RID's aren't freed automatically. This means that once you're done using ``buffer`` or any other RID object, you are responsible for freeing its memory manually using the :ref:`class_RenderingDevice` ``free_rid`` method.
373+
369374
With that, you have everything you need to get started working with compute
370375
shaders.
371376

0 commit comments

Comments
 (0)