Shader types
| Type | Description |
|---|---|
| Material Shaders | Render objects in world space |
| Post Processing Shaders | Full-screen effects |
| Compute Shaders | GPU compute workloads |
Recommended editor setup
Use VSCode with the Slang Extension for a full IDE experience including IntelliSense. Opening your project folder in VSCode prompts you to install the extension and automatically sets up file extension associations, workspace flavor, and search paths.Shader modes
Modes control which render passes your shader participates in. Declare them in aMODES block:
| Mode | Purpose |
|---|---|
Default | Compute shaders |
Forward | Standard rendering |
Depth | Depth prepass and shadows |
ToolsShadingComplexity | Quad overdraw visualisation |
The Material API
Material is the contract between your shader and the lighting system. Fill its fields to describe the surface, then pass it to a shading model.
Material::From( PixelInput i ) to process the standard pixel input and expose textures to the Material Editor. Use Material::Init() to start from an empty material and fill only the fields you need.
Shading models
A shading model consumes aMaterial and outputs the final lit colour.
ShadingModelStandard
The default Source 2 lighting model:ShadingModelStandard::Shade( Material m ) returns a float4 with the fully-lit pixel colour.
Custom shading model
Implement your own model by separating direct and indirect lighting into specular and diffuse components. The example below shows a minimal toon shader skeleton:Attributes and variables
Attributes pass data from C# to the GPU. Declare them by appending< Attribute("Name"); > to a variable:
Material Editor integration
Adding aDefault value makes the attribute appear in the Material Editor UI:
Default value annotations
Default value annotations
| Annotation | Description |
|---|---|
Default(arg) / Default1(arg) | Sets a scalar default |
Default2(a, b) | Sets a float2 / int2 default |
Default3(a, b, c) | Sets a float3 / int3 default |
Default4(a, b, c, d) | Sets a float4 default |
Range(arg) | Clamps the UI slider |
Range2(a, b) | Clamps a float2 / int2 slider |
Range3(a, b, c) | Clamps a float3 / int3 slider |
Range4(a, b, c, d) | Clamps a float4 slider |
UIType(type) | Explicit UI widget: Slider, Color, CheckBox |
DefaultFile(path) | Default texture file if none is specified |
Texture inputs
Texture inputs
Declare a texture input for the Material Editor with Expose the compiled texture as a Pack multiple images into a single texture by specifying channels:
CreateInputTexture2D:Texture2D:Attributes on SceneObjects
Set attributes on a specificSceneObject to affect only that object’s materials at render time:
GPU buffers
Send large blocks of data at once using constant buffers or structured buffers.Command lists
Command lists replace the oldRenderHook system. They let you schedule deferred GPU commands at a specific render stage.
Camera component:
Render states
Control render state anywhere outside a function block:Rasteriser states
Rasteriser states
| Name | Values |
|---|---|
FillMode | WIREFRAME, SOLID |
CullMode | NONE, BACK, FRONT |
DepthBias | true, false |
DepthClipEnable | true, false |
MultisampleEnable | true, false |
Depth and stencil states
Depth and stencil states
| Name | Values |
|---|---|
DepthEnable | true, false |
DepthWriteEnable | true, false |
DepthFunc | NEVER, LESS, EQUAL, LESS_EQUAL, GREATER, NOT_EQUAL, GREATER_EQUAL, ALWAYS |
StencilEnable | true, false |
StencilReadMask | 0–255 |
StencilWriteMask | 0–255 |
StencilFailOp | KEEP, ZERO, REPLACE, INCR_SAT, DECR_SAT, INVERT, INCR, DECR |
StencilPassOp | KEEP, ZERO, REPLACE, INCR_SAT, DECR_SAT, INVERT, INCR, DECR |
StencilFunc | NEVER, LESS, EQUAL, LESS_EQUAL, GREATER, NOT_EQUAL, GREATER_EQUAL, ALWAYS |
StencilRef | 0–255 |
Blend states
Blend states
| Name | Values |
|---|---|
BlendEnable | true, false |
SrcBlend | ZERO, ONE, SRC_COLOR, INV_SRC_COLOR, SRC_ALPHA, INV_SRC_ALPHA, DEST_ALPHA, INV_DEST_ALPHA, DEST_COLOR, INV_DEST_COLOR, SRC_ALPHA_SAT, BLEND_FACTOR, SRC1_COLOR, INV_SRC1_COLOR, SRC1_ALPHA, INV_SRC1_ALPHA |
DstBlend | (same values as SrcBlend) |
BlendOp | ADD, SUBTRACT, REV_SUBTRACT, MIN, MAX |
SrcBlendAlpha | (same values as SrcBlend) |
DstBlendAlpha | (same values as SrcBlend) |
BlendOpAlpha | ADD, SUBTRACT, REV_SUBTRACT, MIN, MAX |
ColorWriteEnable0–3 | false, R, G, B, A, RGB, RGBA, … |
AlphaToCoverageEnable | true, false |
AlphaTestEnable | true, false |
AlphaTestFunc | NEVER, LESS, EQUAL, LESS_EQUAL, GREATER, NOT_EQUAL, GREATER_EQUAL, ALWAYS |
Sampler states
Sampler states control how textures are filtered and addressed. You can bind up to 128 textures but only 16 samplers per shader, so share samplers across textures.Sampler annotations
| Annotation | Description | Values |
|---|---|---|
Filter | Filtering mode | Point, Linear, Anisotropic |
AddressU | U addressing | Wrap, Mirror, Clamp, Border, Mirror_Once |
AddressV | V addressing | Wrap, Mirror, Clamp, Border, Mirror_Once |
AddressW | W addressing | Wrap, Mirror, Clamp, Border, Mirror_Once |
MaxAniso | Maximum anisotropy level | Integer (e.g. 8) |
Predefined samplers
| Sampler | Description |
|---|---|
g_sAniso | Anisotropic, max anisotropy 8 |
g_sBilinearClamp | Bilinear, clamp UVW |
g_sTrilinearWrap | Trilinear, wrap UVW |
g_sTrilinearClamp | Trilinear, clamp UVW |
g_sPointClamp | Point, clamp UVW |
g_sPointWrap | Point, wrap UV, clamp W |
GPU instancing
s&box instances models automatically when the renderer can batch them. You do not need to do anything for standard instancing. To access the object-to-world matrix in a vertex shader:Procedural instancing
Use procedural instancing when you want to derive transforms inside the shader itself. The instance ID is available viaSV_InstanceID:
Shader reference
Default vertex input
Default pixel input
Global variables
| Type | Name | Description |
|---|---|---|
float | g_flTime | Current time in seconds |
float4x4 | g_matWorldToProjection | World-to-projection matrix |
float4x4 | g_matProjectionToWorld | Projection-to-world matrix |
float4x4 | g_matWorldToView | World-to-view matrix |
float4x4 | g_matViewToProjection | View-to-projection matrix |
float3 | g_vCameraPositionWs | Camera position in world space |
float3 | g_vCameraDirWs | Camera direction in world space |
float2 | g_vViewportSize | Viewport dimensions |
float2 | g_vRenderTargetSize | Render target dimensions |
These names follow the older Source 2 convention. A future update may add friendlier aliases such as
Time.Now or time.