Attributes
The included godot Slang module exposes many useful attributes that effect the behavior of shaders within Godot.
gd::Class
Indicates that a struct is represented by a global Godot class. This will, for example, expose fields of the type as the corresponding Godot class in the property inspector.
Target: Var
Fields:
Name |
Type |
Description |
|---|---|---|
|
|
The Godot class name to associate with this struct. Must be the name of a named class |
Example:
[gd::Class("MyGDScriptClass")]
struct MyStruct {
float some_value;
};
// will appear as an instance of MyGDScriptClass in the inspector
uniform MyStruct my_struct;
gd::Color
Indicates that a variable should be treated as sRGB color, rather than linear color.
When applied to a float3 or float4, the value will be converted from sRGB to linear color when binding to the shader.
When applied to a texture, a value passed as a Texture2D will be read as sRGB.
Target: Var
Example:
[gd::Color]
uniform float3 my_color;
[gd::Color]
uniform float4 my_color_with_alpha;
[gd::Color]
uniform Texture<float4> albedo;
gd::DefaultBlack
When used within a ComputeShaderTask, will bind a 4x4 black texture if no texture is provided.
Target: Var
Example:
[gd::DefaultBlack]
uniform Texture2D<float4> texture_param;
gd::DefaultWhite
When used within a ComputeShaderTask, will bind a 4x4 white texture if no texture is provided.
Target: Var
Example:
[gd::DefaultWhite]
uniform Texture2D<float4> texture_param;
gd::Export
Indicates that a variable should be exported within Godot. This will expose it within a ComputeShaderTask's property inspector and allow its value to be serialized with that task.
Target: Var
Example:
[gd::Export]
uniform float my_parameter;
gd::ExportParam
Indicates that a variable should be exported within Godot. This is the same as gd::Export except it may be applied to function parameters. Is only valid on parameters of an entry-point function parameter.
Target: Param
Example:
[shader("compute")]
[numthreads(8, 8, 1)]
void compute_main(uint3 threadId: SV_DispatchThreadID, [gd::ExportParam] float my_parameter) {
// ...
}
gd::FrameID
When used within a ComputeShaderTask, will automatically bind the current frame (fetched via get_frames_drawn).
Target: Var
Example:
[gd::FrameID]
uniform int frame_id;
gd::GlobalParam
When used within a ComputeShaderTask, will bind the value of the corresponding global shader parameter if no value is otherwise provided.
Target: Var
Fields:
Name |
Type |
Description |
|---|---|---|
|
|
The name of the global shader parameter to bind. |
Example:
[gd::GlobalParam("my_noise_texture")]
uniform Texture2D<float4> noise;
gd::KernelGroup
Associates an entry-point with the specified group name. Allows a group of entry-points to be conveniently dispatched together. Currently, an entry-point may be associated with at most one kernel group.
Target: Function
Fields:
Name |
Type |
Description |
|---|---|---|
|
|
The name of the kernel group. |
Example:
[shader("compute")]
[numthreads(8, 8, 1)]
[gd::KernelGroup("my_group")]
void first(uint3 threadId: SV_DispatchThreadID) {
// ...
}
[shader("compute")]
[numthreads(8, 8, 1)]
[gd::KernelGroup("my_group")]
void second(uint3 threadId: SV_DispatchThreadID) {
// ...
}
var task: ComputeShaderTask = get_task()
task.dispatch_group("my_group", thread_groups)
gd::MousePosition
When used within a ComputeShaderTask, will automatically bind the mouse position within the root window.
Target: Var
Example:
[gd::MousePosition]
uniform int2 mouse_position;
gd::Name
Indicates that a variable should use the specified name within Godot. The specified name will be emitted in the reflection metadata instead of the name declared in the shader code.
Target: Var
Fields:
Name |
Type |
Description |
|---|---|---|
|
|
The identifier/name to use for this variable within Godot. |
Example:
struct MyStruct {
[gd::Name("exposed_name")]
float internal_name;
};
[gd::Name("exposed_parameter")]
uniform MyStruct my_parameter;
var task: ComputeShaderTask = get_task()
task.set_shader_parameter("exposed_parameter/exposed_name", 1234.0)
gd::PropertyHint
Sets the property hint and hint string for an exported parameter, controlling how Godot displays it in the property inspector.
See the @export_custom documentation for more information.
Target: Var
Fields:
Name |
Type |
Description |
|---|---|---|
|
|
The hint enum value (e.g. |
|
|
The hint string passed to Godot (e.g. |
Example:
[gd::PropertyHint(PropertyHint.Range, "0,1,0.01")]
uniform float my_param;
gd::Sampler
When used within a ComputeShaderTask, will bind a cached sampler with the specified filter and repeat mode if no sampler is provided.
Target: Var
Fields:
Name |
Type |
Description |
|---|---|---|
|
|
The filter to use for this sampler (linear or nearest). |
|
|
The repeat mode to use for this sampler. |
Example:
[gd::Sampler(gd::SamplerFilter.LINEAR, gd::SamplerRepeatMode.REPEAT)]
uniform SamplerState sampler_state;
gd::Sync
Controls whether a parameter is written before every dispatch, or only when it is assigned.
Synced parameters are written on every dispatch, so the value held by the ComputeShaderTask always wins.
Unsynced parameters are written only when they are assigned, leaving whatever the shader wrote into them intact between dispatches.
Parameters that the shader can write to, such as RWStructuredBuffer or RWTexture2D, are unsynced by default, and every other parameter is synced by default.
Applying this attribute overrides that default. Applying it to a struct or ParameterBlock also covers the members that don't declare their own.
An unsynced parameter is written when it is assigned through set_shader_parameter, when one of its members is assigned by name, and on the first dispatch after the shader is loaded, since there is no GPU state to preserve yet.
Assigning a parameter writes every member the assigned value supplies, so leaving a member out is how you preserve the shader's output for it.
Parameters bound automatically by an attribute whose value changes between dispatches, such as gd::Time or the compositor textures, are always written regardless of this attribute.
Push constants are re-sent on every dispatch by construction, so marking a push constant field unsynced has no effect.
Target: Var
Fields:
Name |
Type |
Description |
|---|---|---|
|
|
Whether the parameter is written before every dispatch. Defaults to |
Example:
// written by the shader, so unsynced by default
uniform RWStructuredBuffer<float> accumulator;
// opt a shader-writable parameter back into being written every dispatch
[gd::Sync(true)]
uniform RWStructuredBuffer<float> scratch;
var task: ComputeShaderTask = get_task()
# seeds the accumulator
task.set_shader_parameter("accumulator", [0.0, 0.0, 0.0])
# the values the shader accumulates survive each dispatch
task.dispatch_all(thread_groups)
task.dispatch_all(thread_groups)
# assign it again to reseed it
task.set_shader_parameter("accumulator", [0.0, 0.0, 0.0])
gd::Time
When used within a ComputeShaderTask, will automatically bind the current time in seconds (fetched via get_ticks_msec).
Target: Var
Example:
[gd::Time]
uniform float time;