discard
Discards the current fragment, preventing it from being written to the framebuffer.
Test discard in a live shader
Open the playground, start from a visual preset, and wire discard into the fragment stage to see how it behaves with real values.
Open PlaygroundLive Demo
The discard intrinsic terminates processing of the current fragment and prevents any output to the framebuffer. This is commonly used for alpha testing, transparency cutouts, and conditional fragment rejection.
Signature
discard :: () {...}
Parameters
This function takes no parameters.
Return Value
This function does not return a value. Execution of the fragment shader terminates immediately.
Example
pipeline AlphaTestShader {
fragment {
float4 texColor = sample(albedoTexture, input.uv);
// Discard fragments with low alpha (alpha testing)
if (texColor.a < 0.5) {
discard();
}
output.color = texColor;
}
}Common Use Cases
Alpha Testing / Cutouts
// Simple alpha cutout for vegetation, fences, etc.
float4 diffuse = sample(diffuseMap, input.uv);
if (diffuse.a < alphaThreshold) {
discard();
}Stencil-Like Effects
// Discard fragments outside a circular region
float2 centered = input.uv - float2(0.5, 0.5);
if (dot(centered, centered) > 0.25) {
discard();
}Dithered Transparency
// Screen-door transparency effect
float threshold = fract(sin(dot(input.screenPos.xy, float2(12.9898, 78.233))) * 43758.5453);
if (alpha < threshold) {
discard();
}Conditional Clipping
// Clip plane implementation
if (dot(input.worldPos, clipPlane.xyz) + clipPlane.w < 0.0) {
discard();
}Distance-Based Culling
// Fade out and discard distant particles
float dist = length(input.worldPos - cameraPos);
float fade = 1.0 - saturate((dist - fadeStart) / (fadeEnd - fadeStart));
if (fade <= 0.0) {
discard();
}
output.color = float4(particleColor.rgb, particleColor.a * fade);Performance Considerations
Using discard can disable early depth testing optimizations on some GPUs. When possible, consider using alpha blending or pre-Z passes for better performance with transparent geometry.
Fragment Shader Only
The discard intrinsic is only available in fragment shaders. Using it in vertex or compute shaders will result in a compilation error.
Depth Buffer Behavior
When a fragment is discarded, it does not write to the depth buffer either. This is useful for creating holes in geometry that other objects can render through.
Compiled Output
When compiled to GLSL:
discard;
When compiled to HLSL:
discard;
// Or: clip(-1);
When compiled to SPIR-V:
Uses OpKill instruction.