class Compute
Compute shaders allow you to run code on the GPU in a massively parallel way! This is great for accelerating complex work, or simply for working inline with the graphics pipeline with easy access to GPU memory.
This behaves very much like Materials do! You can set parameters, and attach buffers or textures! Unlike Materials, you need to dispatch the compute shader to make it run. You may need to be a bit cautious about compute data, since the GPU can be picky about what and when it reads and writes to!
Instance Fields and Properties
| string Id | Gets or sets the unique identifier of this asset resource! This can be helpful for debugging, managing your assets, or finding them later on! |
| int ParamCount | The number of shader parameters available on this Compute! This includes both variables and textures/buffers, great for building a GUI that can inspect any shader. |
| Shader Shader | The shader associated with this compute object. Each access here creates a new reference! |
Instance Methods
| Compute | Create a Compute dispatch from a shader that has a compute stage! If the shader doesn’t have a compute stage, this will fail. |
| Dispatch | Queue this compute dispatch into the render pipeline. It will run during the next frame’s render setup phase, in source order with other queued render actions (Renderer.RenderTo, Renderer.SetGlobal). This is the recommended path for compute work that participates in the frame’s rendering pipeline (e.g. populating a texture that a later RenderTo or the main pass will sample), since sk_renderer can manage the necessary GPU barriers between queued items. IMPORTANT: bindings (textures, buffers, constants, scalar parameters) are NOT snapshotted when Dispatch is called — they are read at execute time, which happens later in the frame. If you change a binding between two queued Dispatch calls on the same Compute, both dispatches will see the final binding state, not the state at their respective Dispatch times. To dispatch the same Compute with different bindings, either issue each Dispatch with DispatchNow, or use a separate Compute instance per binding set. The parameters are the number of thread _groups_, not individual threads. Total thread count = groupCount * numthreads (as defined in your HLSL). So if your shader says [numthreads(8,8,1)] and you dispatch (64,64,1), you’ll get 512512 threads. |
| DispatchNow | Run this compute dispatch synchronously, right now, on the calling thread. Use this for ad-hoc work that doesn’t belong in the per-frame render pipeline (debugging, one-shot tasks, immediate readbacks). For compute work that feeds into later render passes within the same frame, prefer Dispatch, which queues into the pipeline and lets sk_renderer handle ordering and barriers automatically. |
| GetAllParamInfo | Gets an enumerable list of all parameter info on this Compute! Handy for building auto-generated shader GUIs or inspectors. |
| GetBool | Gets the value of a shader parameter by name! If the name isn’t found, you’ll get a default value back. |
| GetColor | Gets a color parameter by name! Note that SetColor converts gamma to linear, so this returns the linear value the shader is actually using. |
| GetFloat | Gets the value of a shader parameter by name! If the name isn’t found, you’ll get a default value back. |
| GetInt | Gets the value of a shader parameter by name! If the name isn’t found, you’ll get a default value back. |
| GetMatrix | Gets the value of a shader parameter by name! If the name isn’t found, you’ll get a default value back. |
| GetParamInfo | Gets parameter info at a specific index! Parameters are listed as variables first, then textures and buffers. |
| GetUInt | Gets the value of a shader parameter by name! If the name isn’t found, you’ll get a default value back. |
| GetVector2 | Gets the value of a shader parameter by name! If the name isn’t found, you’ll get a default value back. |
| GetVector3 | Gets the value of a shader parameter by name! If the name isn’t found, you’ll get a default value back. |
| GetVector4 | Gets the value of a shader parameter by name! If the name isn’t found, you’ll get a default value back. |
| SetBool | Set a shader parameter by name! The name must match a variable in the HLSL compute shader exactly, and if no match is found, nothing happens. Same as Material! |
| SetColor | Set a color parameter by name! Color is converted from gamma to linear space, so what the shader receives will be in linear. If you’re working in linear already, use SetVector with a Vec4 instead! |
| SetConstant | Sets a constant/uniform buffer (cbuffer) on the shader. This is for smaller chunks of data (16kb max) that can be read from faster than textures or StructuredBuffers. |
| SetFloat | Set a shader parameter by name! The name must match a variable in the HLSL compute shader exactly, and if no match is found, nothing happens. Same as Material! |
| SetInt | Set a shader parameter by name! The name must match a variable in the HLSL compute shader exactly, and if no match is found, nothing happens. Same as Material! |
| SetMatrix | Set a shader parameter by name! The name must match a variable in the HLSL compute shader exactly, and if no match is found, nothing happens. Same as Material! |
| SetStorage | Sets a RW/StructuredBuffer or ByteAddressBuffer on the shader. This is used to provide BIG arrays of data to the GPU, for both reading and writing! These perform very similarly to textures, and can be thought of as big textures of just data! |
| SetTexture | Bind a texture to a named resource in the shader! If you’re writing to it (RWTexture2D), the texture must have TexType.Compute set, and use a format like TexFormat.Rgba128. Read-only Texture2D bindings work with any texture. Fallbacks are resolved at Dispatch time, so textures that are still loading will Just Work. |
| SetUInt | Set a shader parameter by name! The name must match a variable in the HLSL compute shader exactly, and if no match is found, nothing happens. Same as Material! |
| SetVector | Set a shader parameter by name! The name must match a variable in the HLSL compute shader exactly, and if no match is found, nothing happens. Same as Material! |
Static Methods
| Find | Looks for a Compute object that has already been created with a matching id! |
Found an issue with these docs, or have some additional questions? Create an Issue on Github!