Skip to content

Commit d5a1ee4

Browse files
committed
Documentation for shader built-in include files (godotengine/godot#94193)
Documents merged feature godotengine/godot#94193 to the end of the compute shader tutorial.
1 parent da75dd1 commit d5a1ee4

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

tutorials/shaders/compute_shaders.rst

+78
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,81 @@ shaders.
377377
GPU separately, which lets you compare how a similar algorithm can be
378378
implemented in two different ways (with the GPU implementation being faster
379379
in most cases).
380+
381+
Includes Database
382+
-----------------
383+
.. warning::
384+
385+
The feature is experimental.
386+
387+
Godot provides some shader includes through ``ShaderIncludeDB`` class. Include
388+
operation is done automatically by Godot. Shader includes are **not** files in
389+
our system.
390+
391+
To see what headers are available, use
392+
``ShaderIncludeDB.list_built_in_include_files``. The contents of these files
393+
can be found in Godot's Git repository or by calling
394+
``ShaderIncludeDB.get_built_in_include_file``.
395+
396+
The example below renders a transparent pulsating circle at the centre of the
397+
screen. The corners of the screen are coloured fully white. The edge of the
398+
circle is smoothed.
399+
400+
.. code-block:: glsl
401+
402+
#[compute]
403+
#version 450
404+
405+
#define MAX_VIEWS 2
406+
#include "godot/scene_data_inc.glsl"
407+
408+
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
409+
410+
layout(set = 0, binding = 0, std140) uniform SceneDataBlock {
411+
SceneData data;
412+
SceneData prev_data;
413+
} scene_data_block;
414+
415+
layout(rgba16f, set = 0, binding = 1) uniform image2D screen;
416+
417+
void main(){
418+
ivec2 uv = ivec2(gl_GlobalInvocationID.xy);
419+
420+
vec4 color = imageLoad(screen, uv);
421+
422+
// Check UV is in the view.
423+
if (uv.x >= scene_data_block.data.viewport_size.x || uv.y >= scene_data_block.data.viewport_size.y) {
424+
return;
425+
}
426+
427+
float maxlen = max(scene_data_block.data.viewport_size.x, scene_data_block.data.viewport_size.y);
428+
429+
vec2 center = vec2(scene_data_block.data.viewport_size.x / 2, scene_data_block.data.viewport_size.y / 2);
430+
float r = length(abs(center - uv));
431+
float alpha = smoothstep(maxlen * (0.55 + 0.3 * sin(scene_data_block.data.time)), maxlen * (0.2 + 0.2 * sin(scene_data_block.data.time)), r);
432+
433+
// Calculate blend between full white and image's current color.
434+
color = (1 - alpha) * vec4(1, 1, 1, 1) + alpha * color;
435+
imageStore(screen, uv, color);
436+
}
437+
438+
The example includes ``godot/scene_data_inc.glsl``, which defines ``SceneData``
439+
data structure. ``SceneData`` needs ``MAX_VIEWS`` for some of its' members.
440+
Uniform ``SceneDataBlock`` correspondence to buffer got from
441+
``p_render_data.get_render_scene_data()``. In ``_render_callback``, you can
442+
bind scene data to a uniform.
443+
444+
.. tabs::
445+
.. code-tab:: gdscript GDScript
446+
447+
var scene_data_buffers = p_render_data.get_render_scene_data().get_uniform_buffer();
448+
var uniform = RDUniform.new()
449+
uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_UNIFORM_BUFFER;
450+
uniform.binding = 0;
451+
uniform.add_id(scene_data_buffers);
452+
453+
Shader uses ``SceneDataBlock.data`` members ``viewport_size`` and ``time``
454+
counter. Member ``viewport`` is currently the viewport's size. It is used to
455+
calculate the centre of the screen. Counter ``time`` is an analogue of time
456+
since the start of the game. It is used to make pulsating animation.
457+

0 commit comments

Comments
 (0)