Global Variables

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

Time

TypeNameDescription
floatg_flTimeCurrent time

Projections

These projection matrices are used for conversions between various coordinate spaces.

TypeNameDescriptionHelper Functions
float4x4g_matWorldToProjectionConverts 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 manipulationsPosition4WsToPs, Position3WsToPs
float4x4g_matProjectionToWorldConverts projection-space coordinates to world-space. Used in Depth::GetWorldPosition to reconstruct world-space position of ray from depth buffer...
float4x4g_matWorldToViewConverts world-space coordinates to view-space. This coordinate system is built relative to the camera, with it as the originPosition4WsToVs, Position3WsToVs
float4x4g_matViewToProjectionConverts view-space coordinates to projection-spacePosition4VsToPs, Position3VsToPs
float4x4g_matProjectionToViewConverts projection-space coordinates to view space...
float4x4g_matCurrFrameViewToPrevFrameProjConverts view-space coordinates of current frame to projection-space coordinates of previous frameReprojectFromLastFrameSs
float4g_vInvProjRow3Row 3 of the inverse projection matrix...

Camera

TypeNameDescription
float3g_vCameraPositionWsCamera position in world space
float3g_vCameraDirWsCamera direction in world space
float3g_vCameraUpDirWsCamera up direction in world space
float4g_vCameraAnglesCamera raw pitch/yaw/roll in radians, unused
floatg_flCameraFOVCamera FOV in radians
floatg_flNearPlaneCamera's near plane
floatg_flFarPlaneCamera's far plane

Viewport

TypeNameDescription
floatg_flViewportMinZDepth buffer min range for current viewport
floatg_flViewportMaxZDepth buffer max range for current viewport
float2g_vViewportSizeViewport width (X) and height (Y)
float2g_vViewportOffsetViewport top left XY coords for offset if it's on a shared render target
float2g_vRenderTargetSizeRender target width (X) and height (Y)
float2g_vInvViewportSizeReciprocal of original viewport size (1.0 / g_vViewportSize)
float4g_vFrameBufferCopyInvSizeAndUvScaleReciprocal 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

Other

TypeNameDescription
float4g_vRandomFloatsRandom floats in each component, values change every frame

Referenced API

Created at:
Updated at:

On this page