Shaders have access to a number of variables that are available from everywhere. They are defined per-view, meaning that some data will vary depending on current viewport. Compute shader can access this data as well, as long as they are being dispatched from render context.
Shaders have access to a number of variables that are available from everywhere. They are defined per-view, meaning that some data will vary depending on current viewport. Compute shader can access this data as well, as long as they are being dispatched from render context.
I don't love these names but they're not going to go anywhere, so you're free to use them. In all likelihood in the future we'll get together and create a bunch of nicer #define's for them, so g_flTime will be accessible viaTime.Now, or just time etc.
This page only includes variables that are actually used by the engine and have useful purpose for shaders. You can look up a full list of all global per-view variables, including unused ones, here: game/core/shaders/common.fxc
These projection matrices are used for conversions between various coordinate spaces.
Type
Name
Description
Helper Functions
float4x4
g_matWorldToProjection
Converts world-space coordinates to projection-space. In vertex shaders, it is common to convert world-space vertex position back to projection space before finalizing vertex manipulations
Position4WsToPs, Position3WsToPs
float4x4
g_matProjectionToWorld
Converts projection-space coordinates to world-space. Used in Depth::GetWorldPosition to reconstruct world-space position of ray from depth buffer
...
float4x4
g_matWorldToView
Converts world-space coordinates to view-space. This coordinate system is built relative to the camera, with it as the origin
Position4WsToVs, Position3WsToVs
float4x4
g_matViewToProjection
Converts view-space coordinates to projection-space
Position4VsToPs, Position3VsToPs
float4x4
g_matProjectionToView
Converts projection-space coordinates to view space
...
float4x4
g_matCurrFrameViewToPrevFrameProj
Converts view-space coordinates of current frame to projection-space coordinates of previous frame
Viewport top left XY coords for offset if it's on a shared render target
float2
g_vRenderTargetSize
Render target width (X) and height (Y)
float2
g_vInvViewportSize
Reciprocal of original viewport size (1.0 / g_vViewportSize)
float4
g_vFrameBufferCopyInvSizeAndUvScale
Reciprocal of frame buffer size. X and Y are 1.0f / viewport size, Z and W are always 1.0. This property is considered unused, but you still may see it in some older shaders