S&box Wiki

Constant Buffers

What are constant buffers

Constant buffer or "cbuffers" as known by HLSL is a buffer/struct which is stored in GPU memory and can be accessed within your shader. Once the file is included you should be able to access the cbuffer like a regular variable within your shader.

An example of its usage can be seen below:

COMMON { #include "common/shared.hlsl" // Contains PerViewConstantBuffer_t } VS { #include "common/vertex.hlsl" // // Main // PixelInput MainVs( VS_INPUT i ) { PixelInput o = ProcessVertex( i ); o.vPositionWs.z += sin(g_flTime) * 5.0f; // wiggle return FinalizeVertex( o ); } }

Settings constant buffers in code

Constant buffers are set using the RenderAttributes.SetData function. Much like the other attributes you only need to pass the data itself to the attribute.

struct MyData { public float SomeFloat; public Vector3 SomeVector; } // ... { MyData myStuff = new(); myStuff.SomeFloat = 10.0f; myStuff.SomeVector = Vector3.One; Attributes.SetData( "MyConstBuffer_t", myStuff ); }

And accessing it within shaders is as simple as:

VS { #include "common/vertex.hlsl" cbuffer MyConstBuffer_t { float SomeFloat; float3 SomeVector; }; // // Main // PixelInput MainVs( VS_INPUT i ) { PixelInput o = ProcessVertex( i ); o.vPositionWs += SomeVector * SomeFloat; // Use our constbuffer values! return FinalizeVertex( o ); } }

Built-in constant buffer

PerViewConstantBuffer_t

Usage: #include "common/shared.hlsl"

Type Field Note
float4x4 g_matWorldToProjection
float4x4 g_matProjectionToWorld
float4x4 g_matWorldToView
float4x4 g_matViewToProjection
float4x4 g_matPrevProjectionToWorld
float4x4 g_matViewToScreen
float4x4 g_matProjectionToView
float4x4 g_matCurrFrameViewToPrevFrameProj
float4 g_vRandomFloats
float4 g_vInvProjRow3
float4 g_vClipPlane0
float g_flToneMapScalarLinear
float g_flLightMapScalar
float g_flEnvMapScalar
float g_flToneMapScalarGamma
float4 g_vCameraAngles
float3 g_vCameraPositionWs The world position of the camera
float3 g_vCameraDirWs The current camera direction (forward)
float3 g_vCameraUpDirWs The up direction of the camera
float3 g_vSunLightDir The direction of the sun in world space
float g_flViewportMinZ
float g_flViewportMaxZ
float g_flTime The time since the client has connected
float3 g_vDepthPsToVsConversion
float g_flNearPlane NearZ
float g_flFarPlane FarZ
float g_flLightBinnerFarPlane
float2 g_vViewportToGBufferRatio
float2 g_vMorphTextureAtlasSize
float4 g_vInvGBufferSize
float g_flOOTransformTextureWidth
float g_flOOTransformTextureHeight
float2 g_vViewportOffset
float2 g_vViewportSize
float2 g_vInvViewportSize
float2 g_vRenderTargetSize
float g_flFogBlendToBackground
float g_flHenyeyGreensteinCoeff
float3 g_vFogColor
float g_flNegFogStartOverFogRange
float g_flInvFogRange
float g_flFogMaxDensity
float g_flFogExponent
float g_flMod2xIdentity
float2 g_bRoughnessParams
bool g_bUseRoughnessCone
bool g_bUseRoughnessEllipse
float g_bStereoEnabled
float g_flStereoCameraIndex
float3 g_vMiddleEyePositionWs
float g_flPad2 Padding for alignment, unused
float4x4 g_matWorldToProjectionMultiview[ 2 ] (?) Might not work - multiview instancing is removed
float4 g_vCameraPositionWsMultiview[ 2 ] (?) Might not work - multiview instancing is removed
float4 g_vFrameBufferCopyInvSizeAndUvScale
float4 g_vWorldToCameraOffset
float4 g_vWorldToCameraOffsetMultiview[ 2 ] (?) Might not work - multiview instancing is removed
float4 g_vPerViewConstantExtraData0
float4 g_vPerViewConstantExtraData1
float4 g_vPerViewConstantExtraData2
float4 g_vPerViewConstantExtraData3

PerViewConstantBufferVR_t

Usage: #include "common/shared.hlsl"

Type Field Note
bool g_bVolumetricFogEnabled
bool g_bGradientFogEnabled
bool g_bCubemapFogEnabled
bool g_bSphericalVignetteEnabled
bool g_bAmbientOcclusionProxiesEnabled
float4 g_vAoProxyDownres
float4 g_vWindDirection Current wind direction
float g_flWindSpeed Current wind speed
float4 g_vInteractionProjectionOrigin
float4 g_vInteractionVolumeInvExtents
float4 g_vInteractionTriggerVolumeInvMins
float4 g_vInteractionTriggerVolumeWorldToVolumeScale
float4 g_vGradientFogBiasAndScale
float4 m_vGradientFogExponents
float4 g_vGradientFogColor_Opacity
float4 g_vGradientFogCullingParams
float g_flCubeFogOffset
float g_flCubeFogScale
float g_flCubeFogBias
float g_flCubeFogExponent
float g_flCubeFogHeightOffset
float g_flCubeFogHeightScale
float g_flCubeFogHeightExponent
float g_flCubeMapSizeLog2
float4x4 g_matvCubeFogSkyWsToOs
float4 g_vCubeFogCullingParams
float4 g_vSphericalVignetteBiasAndScale
float4 g_vSphericalVignetteOrigin_Exponent
float4 g_vSphericalVignetteColor_Opacity
float g_flVolFogNearClipPlane
float g_flVolFogClipPlaneRange
float4 g_vVolFogDitherScaleBias
float4 g_vVolFogPostWorldToFrustumScale
float4 g_vVolFogPostWorldToFrustumBias
float4x4 g_mVolFogFromWorld[ 2 ]
float4 g_vHighPrecisionLightingOffsetWs Returns same value as g_vCameraPositionWs, but will return 0 if r_high_precision_lighting convar is set to false.