TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/facepunch/sbox-public/llms.txt
Use this file to discover all available pages before exploring further.
Graphics static class is the primary interface for issuing draw calls and managing GPU resources in s&box. It is only valid inside a rendering block (e.g. a SceneCustomObject.OnRender callback). Supporting types — RenderAttributes, RenderTarget, ComputeShader, and GpuBuffer — let you pass data to shaders, render off-screen, and run compute workloads.
Graphics static class
State
true when executing inside a render block. Use this guard before issuing draw calls from code that may run both inside and outside rendering.The current render layer — opaque, transparent, shadow, or other. Use this to conditionally emit draw calls only for the appropriate pass.
Gets or sets the current pixel viewport rectangle. Setting this changes the scissor region for subsequent draw calls.
The
RenderAttributes bag for the current render context. Set material parameters here before calling Draw or Blit. Cleared at the end of the render block.Gets or sets the active render target. Setting a non-null value binds that target and resizes the viewport to match. Set back to
null to restore the default framebuffer.Camera
The world-space transform of the camera for the current view.
The world-space position of the active camera.
The world-space rotation of the active camera.
The field of view of the active camera in degrees.
The view frustum of the currently rendering camera. Use this for custom visibility checks.
Draw calls
Graphics.Draw(Span<Vertex> vertices, int vertCount, Material material, RenderAttributes attributes, PrimitiveType primitiveType)
void
Draws a span of
Vertex values with the given material.List<Vertex> instead of a Span<Vertex>. An additional overload accepts Span<ushort> indices and int indexCount for indexed drawing.
Draws a full-screen quad in screen space using the provided material. Your material must use a screen-space shader or it will render at the world origin.
Renders a
SceneObject immediately into the current render context. Useful for rendering a specific object with a custom transform or material override.Draws a 2D quad at the given screen-space rectangle.
Graphics.DrawText(Rect position, string text, Color color, string fontFamily, float fontSize, float fontWeight, TextFlag flags)
Rect
Renders text into a screen-space rectangle using
Material.UI.Text. Returns the actual bounds of the rendered text.Renders a Material Icons glyph.
iconName is the lowercase icon identifier from the Material Icons library.Graphics.DrawRoundedRectangle(Rect rect, Color color, Vector4 cornerRadius, Vector4 borderWidth, Color borderColor)
void
Draws a rounded rectangle with optional border using
Material.UI.Box.Graphics.MeasureText(Rect position, string text, string fontFamily, float fontSize, float fontWeight, TextFlag flags)
Rect
Measures the bounds of text without rendering it. Use this to compute layouts before drawing.
Texture utilities
Graphics.GrabFrameTexture(string targetName, RenderAttributes renderAttributes, DownsampleMethod downsampleMethod, int maxMips)
RenderTarget
Copies the current color framebuffer into a temporary
RenderTarget and registers it on renderAttributes under targetName. The texture is automatically returned to the pool at the end of the render block.Copies the current depth buffer into a temporary
RenderTarget and registers it on renderAttributes under targetName.Copies pixel data between two GPU textures. Format and size must match exactly.
Graphics.CopyTexture(Texture srcTexture, Texture dstTexture, int srcMipSlice, int srcArraySlice, int dstMipSlice, int dstArraySlice)
void
Copies a specific mip level and array slice between textures.
Populates
targetAttributes with per-object lighting data for the given SceneObject. Call this before drawing an object that needs dynamic lighting.Clears the active render target. All parameters default to clearing everything to the given color.
Forces the GPU to complete all pending commands synchronously. Can be called outside a render block. Use sparingly as it stalls the CPU.
Primitive types
Graphics.PrimitiveType controls the interpretation of vertex data:
| Value | Description |
|---|---|
Points | Each vertex is an independent point. |
Lines | Every two vertices form a line segment. |
LineStrip | Vertices form a connected line strip. |
Triangles | Every three vertices form a triangle. |
TriangleStrip | Vertices form a strip of connected triangles. |
*WithAdjacency | Variants include adjacency data for geometry shaders. |
RenderAttributes
RenderAttributes is a key/value bag that passes typed data to materials and shaders. The shader side accesses attributes with the Attribute( "Name" ) annotation.
Setting values
RenderAttributes has Set overloads for every supported type:
| C# type | Method |
|---|---|
int | Set( StringToken k, int value ) |
float / double | Set( StringToken k, float value ) |
bool | Set( StringToken k, bool value ) |
string | Set( StringToken k, string value ) |
Vector2 | Set( StringToken k, Vector2 value ) |
Vector3 | Set( StringToken k, Vector3 value ) |
Vector4 | Set( StringToken k, Vector4 value ) |
Angles | Set( StringToken k, Angles value ) |
Matrix | Set( StringToken k, Matrix value ) |
Texture | Set( StringToken k, Texture value, int mip ) |
SamplerState | Set( StringToken k, SamplerState value ) |
GpuBuffer | Set( StringToken k, GpuBuffer value ) |
SetData<T>( StringToken k, Span<T> value ) uploads raw struct data into a constant buffer. T must be a POD (blittable) struct.
SetCombo( StringToken k, int value ) sets a shader static combo by name.
Getting values
MatchingGet* methods retrieve values back from the bag: GetBool, GetFloat, GetInt, GetVector, GetVector4, GetAngles, GetMatrix, GetTexture.
Lifecycle
Graphics.Attributes is the per-render-context bag — it is cleared automatically at the end of each render block. To pass attributes that live longer, create your own RenderAttributes instance:
RenderAttributes objects clean up their native memory asynchronously on the main thread.
RenderTarget
RenderTarget wraps a color texture and an optional depth texture for off-screen rendering.
Width of the render target in pixels.
Height of the render target in pixels.
The color texture attached to this render target.
The depth texture attached to this render target. May be
null if no depth is needed.Creates a
RenderTarget from existing render-target-compatible textures. Both textures must have been created with Texture.CreateRenderTarget.Returns a temporary
RenderTarget to the pool. Call this when done with a target obtained from Graphics.GrabFrameTexture or Graphics.GrabDepthTexture if not relying on automatic pool return.ComputeShader
ComputeShader runs a shader program on the GPU outside the standard rendering pipeline. Pair it with GpuBuffer to exchange data between the CPU and GPU.
Loads a compute shader from the given asset path.
The
RenderAttributes bag for this shader. Set input and output buffers here before calling Dispatch.Dispatches the compute shader. Thread counts are automatically divided by the thread group size declared in the shader.When called inside a graphics context the dispatch runs asynchronously. When called outside a graphics context it runs immediately.
ComputeShader.DispatchWithAttributes(RenderAttributes attributes, int threadsX, int threadsY, int threadsZ)
void
Dispatches with an explicit
RenderAttributes bag instead of the shader’s built-in Attributes property.Reads dispatch thread group counts from a GPU buffer of type
GpuBuffer.UsageFlags.IndirectDrawArguments. The buffer’s element size must be 12 bytes.GpuBuffer
GpuBuffer and the generic GpuBuffer<T> wrap a GPU-side memory buffer for use with compute shaders and custom rendering. Use SetData to upload from the CPU and GetData to read back.
Construction
Creates an untyped buffer.
Creates a typed buffer.
T must be a blittable struct. Element size is inferred from sizeof(T).Properties
Number of elements allocated in the buffer.
Size in bytes of a single element.
The usage flags the buffer was created with.
true when the native GPU handle is valid and the buffer has not been disposed.Data transfer
Synchronously uploads data from a
Span<T> to the GPU. Blocks until the upload completes.Same as the
Span overload but accepts a List<T>.Synchronously downloads all buffer data from the GPU. Blocks the CPU until the read is complete.
Downloads a range of elements from the GPU.
Asynchronously reads the full buffer and invokes
callback with the data. The ReadOnlySpan<T> is only valid inside the callback.Asynchronously reads a range of the buffer.
Utilities
Fills the entire buffer with a repeated
uint32 value on the GPU. Defaults to 0.Sets the hidden atomic counter for
Append or Counter structured buffers.Copies the hidden structure count from an
Append or Structured buffer into destBuffer.Destroys the GPU buffer. Must be called on the main thread. After disposal
IsValid returns false.UsageFlags
| Flag | HLSL mapping | Notes |
|---|---|---|
Structured | StructuredBuffer<T> / RWStructuredBuffer<T> | Default. Element size must be 4-byte aligned. |
Index | Index buffer | Element size must be 2 or 4 bytes. |
Append | AppendStructuredBuffer<T> | Supports atomic append and hidden counter. |
Counter | RWStructuredBuffer<T> with counter | Supports SetCounterValue. |
ByteAddress | ByteAddressBuffer | Raw byte-addressable reads. |
IndirectDrawArguments | Indirect dispatch/draw args | Required for DispatchIndirect. Element size must be 12 bytes. |
Usage examples
Related pages
Rendering guide
How to write custom render passes and post-processing effects in s&box.
Shaders guide
Writing and compiling HLSL shaders with the s&box shader system.
Camera component
The
CameraComponent API for setting up scene views.Game class
Global game state and active scene access.