Revision Difference
Constant_Buffers#545915
<cat>Code.Shader</cat>
<title>Constant Buffers</title>
# 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:
```
VS
{
#include "common/vertex.hlsl"
#include "common.fxc" // Contains PerViewConstantBuffer_t
//
// Main
//
PixelInput MainVs( INSTANCED_SHADER_PARAMS( VS_INPUT i ) )
{
PixelInput o = ProcessVertex( i );
o.vPositionWs.z += sin(g_flTime) * 5.0f; // wiggle
return FinalizeVertex( o );
}
}
```
⤶
## BakedLightingConstantBuffer_t⤶
⤶
Usage: `#include "baked_lighting_constants.fxc"`⤶
⤶
| Type | Field | Note |⤶
|------|-------|------|⤶
| float4 | g_vLightmapUvScale | |⤶
## PerViewConstantBuffer_t
Usage: `#include "common.fxc"`
| Type | Field | Note |
|------|-------|------|
| float4x4 | g_matWorldToProjection | |
| float4x4 | g_matProjectionToWorld | |
| float4x4 | g_matWorldToView | |
| float4x4 | g_matViewToProjection | |
| float4 | g_vInvProjRow3 | |
| float4 | g_vClipPlane0 | |
| float | g_flToneMapScalarLinear | |
| float | g_flLightMapScalar | |
| float | g_flEnvMapScalar | |
| float | g_flToneMapScalarGamma | |
| float3 | g_vCameraPositionWs | The world position of the camera |
| float | g_flViewportMinZ | |
| float3 | g_vCameraDirWs | The current camera direction |
| float | g_flViewportMaxZ | |
| float3 | g_vCameraUpDirWs | |
| 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_vInvViewportSize | |
| float2 | g_vViewportToGBufferRatio | |
| float2 | g_vMorphTextureAtlasSize | |
| float4 | g_vInvGBufferSize | |
| float | g_flOOTransformTextureWidth | |
| float | g_flOOTransformTextureHeight | |
| float2 | g_vViewportOffset | |
| float2 | g_vViewportSize | |
| 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 ] | |
| float4 | g_vCameraPositionWsMultiview[ 2 ] | |
| float4 | g_vFrameBufferCopyInvSizeAndUvScale | |
| float4 | g_vCameraAngles | |
| float4 | g_vWorldToCameraOffset | |
| float4 | g_vWorldToCameraOffsetMultiview[ 2 ] | |
| float4 | g_vPerViewConstantExtraData0 | |
| float4 | g_vPerViewConstantExtraData1 | |
| float4 | g_vPerViewConstantExtraData2 | |
| float4 | g_vPerViewConstantExtraData3 | |
| float4x4 | m_matPrevProjectionToWorld | |
## PerViewLightingConstantBuffer_t
Usage: `#include "common.fxc"`
| Type | Field | Note |
|------|-------|------|
| float | g_flInvPointLightTextureWidth | |
| float | g_flInvSpotLightTextureWidth | |
| float | g_flInvLightTransformTextureWidth | |
| float | g_flInvLightTextureHeight | |
| float4 | g_vLightBinCounts | |
| float2 | g_vLowResDeferredLightingTextureDim | |
| float2 | g_vLowResDeferredLightingTextureInvDim | |
| float3 | g_vToolsAmbientLighting | |
| float | g_flSSAOEnabled | |
| float3 | g_vIrradAtlasInvSize | |
| float | g_flIrradGlobalLightingFactor | |
| float4 | g_vGlobalLightingSHRed | |
| float4 | g_vGlobalLightingSHGreen | |
| float4 | g_vGlobalLightingSHBlue | |
| float3 | g_vRayleighScatteringCoeff | |
| float | g_flMieScatteringEccentricity | |
| float3 | g_vMieScatteringCoeff | |
| float | g_flMieForwardScatteringRatio | |
| float3 | g_vSpecularCubeMapScale | |
| float | g_flDefaultEnvMapIndex | |
| float4 | g_vLightCookieSheetSequenceAndSizes | |
| float3 | g_vLpvBoxMinCascade0 | |
| float | g_flLpvBrightnessScale | |
| float3 | g_vLpvInvBoxDimCascade0 | |
| float2 | g_vLpvInvCoeffTextureDim | |
## PerLayerConstantBuffer_t
Usage: `#include "common.fxc"`
| Type | Field | Note |
|------|-------|------|
| float4 | g_vWireframeColor | The wireframe color for the current rendering layer(mat_wireframe N) |
⤶
## PerBatchLightProbeVolumeConstantBuffer_t⤶
⤶
Usage: `#include "light_probe_volume.fxc"`⤶
⤶
| Type | Field | Note |⤶
|------|-------|------|⤶
| float4x3 | g_matLightProbeVolumeWorldToLocal | |⤶
| float4 | g_vLightProbeVolumeLayer0TextureMin | |⤶
| float4 | g_vLightProbeVolumeLayer0TextureMax | |⤶
| float4 | g_vLightProbeVolumeColor | |⤶
⤶
## MSAASampleOffsetConstantBuffer_t⤶
⤶
Usage: `#include "msaa_offsets.fxc"`⤶
⤶
| Type | Field | Note |⤶
|------|-------|------|⤶
| float4 | g_vMSAASampleOffsets[16] | |⤶
| uint | g_nMSAASampleCount | |⤶
⤶
## ReflectionMapDescriptor_t⤶
⤶
Usage: `#include "reflection.fxc"`⤶
⤶
| Type | Field | Note |⤶
|------|-------|------|⤶
| float4x4 | g_matReflectionWorldToProjection | |⤶
| float4 | g_vReflectionPlane | |⤶
| float4 | g_vReflectionRenderTargetRectangle | |⤶
⤶
## SFMLightsConstantBuffer_t⤶
⤶
Usage: `#include "sfm_lighting_nocombos.fxc"`⤶
⤶
| Type | Field | Note |⤶
|------|-------|------|⤶
| float | g_SFMLight_Count | |⤶
| float | g_SFMLight_VolumetricOffset | |⤶
| float | g_SFMLight_PadZ | |⤶
| float | g_SFMLight_PadW | |⤶
| float2 | g_SFMLight_ShadowSampleDelta0 | |⤶
| float2 | g_SFMLight_ShadowSampleDelta1 | |⤶
| float2 | g_SFMLight_ShadowSampleDelta2 | |⤶
| float2 | g_SFMLight_ShadowSampleDelta3 | |⤶
| float4 | g_SFMLight_Position_Roundness[ 32 ] | |⤶
| float4 | g_SFMLight_Direction_ShadowFilterSize[ 32 ] | |⤶
| float4 | g_SFMLight_Color_ShadowTexIndex[ 32 ] | |⤶
| float4 | g_SFMLight_Attenuation_CookieTexIndex[ 32 ] | |⤶
| float4 | g_SFMLight_DepthParams_SpotFalloff[ 32 ] | |⤶
| float4 | g_SFMLight_GoboTexCoords[ 32 ] | |⤶
| float4 | g_SFMLight_PadXYZ_AmbientIntensity[ 32 ] | |⤶
| float4x4 | g_SFMLight_ViewProj[ 32 ] | |⤶
⤶
⤶
## SkyConstantBuffer_t⤶
⤶
Usage: `#include "sky.fxc"`⤶
⤶
| Type | Field | Note |⤶
|------|-------|------|⤶
| float4x3 | g_matSkyOsToWs | Convert from Sky Object Space to Sky World Space |⤶
| float4x3 | g_matSkyWsToOs | Convert from Sky World Space to Sky Object Space |⤶
⤶
## SunLightShaderParamsCB⤶
⤶
Usage: `#include "sun_shadowing.fxc"`⤶
⤶
| Type | Field | Note |⤶
|------|-------|------|⤶
| float4 | g_vSunLightColor | Current sun color |⤶
| float3 | g_vSunLightDir | Current sun direction |⤶
| float | g_flSunNormalizedRadius | |⤶
| float | g_flSunShadowingInvShadowTextureWidth | |⤶
| float | g_flSunShadowingInvShadowTextureHeight | |⤶
| float | g_flSunShadowingHalfInvShadowTextureWidth | |⤶
| float | g_flSunShadowingHalfInvShadowTextureHeight | |⤶
| float | g_flSunShadowingShadowTextureWidth | |⤶
| float | g_flSunShadowingShadowTextureHeight | |⤶
| float | g_flSunShadowingSplitLerpFactorBase | |⤶
| float | g_flSunShadowingSplitLerpFactorInvRange | |⤶
| float | g_flSunShadowingZLerpFactorBase | |⤶
| float | g_flSunShadowingZLerpFactorRange | |⤶
| float4x4 | g_matWorldToShadowTexMatrices[4] | |⤶
| float4 | g_vCascadeAtlasUVOffsets[4] | |⤶
| float4 | g_flCascadeViewShadowBiasScales[4] | |⤶
| int | g_nSunShadowCascadeCount | |⤶
## PerViewConstantBufferVR_t
Usage: `#include "vr_common.fxc"`
| Type | Field | Note |
|------|-------|------|
| bool | g_bVolumetricFogEnabled | |
| bool | g_bGradientFogEnabled | |
| bool | g_bCubemapFogEnabled | |
| bool | g_bSphericalVignetteEnabled | |
| bool | g_bAmbientOcclusionProxiesEnabled | |
| float4 | g_vAoProxyDownres | |
| float | g_flExcitationAmount | Xen animations |
| float | g_flDepressionAmount | Xen animations |
| 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 | |
## SkyConstantBuffer_t⤶
Usage: `#include "sky.fxc"`
## HighQualityShadowsConstantBuffer_t⤶
Usage: `#include "vr_lighting.fxc"`
| Type | Field | Note |
|------|-------|------|
| float4x3 | g_matSkyOsToWs | Convert from Sky Object Space to Sky World Space |
| float4x3 | g_matSkyWsToOs | Convert from Sky World Space to Sky Object Space |
⤶
## SunLightShaderParamsCB⤶
⤶
Usage: `#include "sun_shadowing.fxc"`⤶
| int4 | g_nHighQualityShadowsCount | |
| int4 | g_vHighQualityShadowsIndices[4] | |
| float4 | g_vHighQualityLightSize[4] | |⤶
| float4x4 | g_matHighQualityWorldToShadow[4] | |⤶
| float4 | g_vHighQualityLightPosition[4] | |⤶
| float4 | g_vHighQualityShadowRegion | |⤶
⤶
## PerViewLightingConstantBufferVr_t⤶
⤶
Usage: `#include "vr_per_view_lighting_constants.fxc"`⤶
| Type | Field | Note |
|------|-------|------|
| float4 | g_vSunLightColor | Current sun color |
| float3 | g_vSunLightDir | Current sun direction |
| float | g_flSunNormalizedRadius | |
| float | g_flSunShadowingInvShadowTextureWidth | |
| float | g_flSunShadowingInvShadowTextureHeight | |
| float | g_flSunShadowingHalfInvShadowTextureWidth | |
| float | g_flSunShadowingHalfInvShadowTextureHeight | |
| float | g_flSunShadowingShadowTextureWidth | |
| float | g_flSunShadowingShadowTextureHeight | |
| float | g_flSunShadowingSplitLerpFactorBase | |
| float | g_flSunShadowingSplitLerpFactorInvRange | |
| float | g_flSunShadowingZLerpFactorBase | |
| float | g_flSunShadowingZLerpFactorRange | |
| float4x4 | g_matWorldToShadowTexMatrices[4] | |
| float4 | g_vCascadeAtlasUVOffsets[4] | |
| float4 | g_flCascadeViewShadowBiasScales[4] | |
| int | g_nSunShadowCascadeCount | |
| int | g_nNumLights | Amount of lights to iterate within the other fields, (g_vLightParams, g_vLightPosition_flInvRadius, etc) |
| int4 | g_vMultiviewData | |
| int | g_nNumViews | |
| int | g_nNumSubiews | |
| int | g_nNumTotalMultiviews | |
| int4 | g_vMultiviewViewAndSubviewIndexArray[4] | |
| int4 | g_vLightParams[128] | |
| float4 | g_vLightPosition_flInvRadius[128] | |
| float4 | g_vLightColor[128] | |
| float4 | g_vLightFalloffParams[128] | |
| float4 | g_vLightDirection[128] | |
| float4 | g_vSpotLightInnerOuterConeCosines[128] | |
| float4x4 | g_matWorldToShadow[128] | |
| float4 | g_vShadow3x3PCFTermC0 | |
| float4 | g_vShadow3x3PCFTermC1 | |
| float4 | g_vShadow3x3PCFTermC2 | |
| float4 | g_vShadow3x3PCFTermC3 | |
| float4 | g_vToolsAmbientLightingVr | |⤶
| float4 | g_vLightBinCameraPositionWs | |⤶
| float4 | g_vLightBinCameraDirWs | |⤶
| float | g_flLightBinCameraNearZ | |⤶
| float | g_flLightBinCameraFarZ | |⤶
| float4x4 | g_matLightBinViewProj | |⤶
| float4 | g_vMultiviewScaleBias[4] | |⤶
| float4 | g_vMultiviewClipEdge0[4] | |⤶
| float4 | g_vMultiviewClipEdge1[4] | |⤶
| float | g_flNumViews | |⤶
| float | g_flOoNumViews | |⤶
| float | g_flNumSubiews | |⤶
| float | g_flOoNumSubviews | |⤶
| float | g_flNumTotalMultiviews | |⤶
| float | g_flOoNumTotalMultiviews | |⤶
| float | g_flVrLuminanceOffset | |⤶
| float | g_flVrLuminanceScale | |⤶
| float | g_fDisableDirectionalLightmaps | |⤶
| float4 | g_vScreenSpaceDitherParams | |⤶
| float4x4 | g_matWorldToLightCookie[128] | |⤶
| float4 | g_vSpotLightShadowOffsetScale[128] | |⤶
| float4 | g_vSpotLightShadowBounds[128] | |⤶
| float4 | g_vLightProjectiveDepthToLinearDepth[128] | |⤶
| float4 | g_vUnlitShadowColor | |⤶
| float4 | g_vAmbientLightingSH[3] | |⤶
| float4 | g_vLightIndexData[128] | |⤶
| float4 | g_vSunLightIndexData | |⤶
| float4 | g_vAmbientOcclusionProxyLightPositions[4] | |⤶
| float4 | g_vAmbientOcclusionProxyLightConeAngles | |⤶
| float4 | g_vAmbientOcclusionProxyLightStrengths | |⤶
| float4 | g_vAmbientOcclusionProxyAmbientStrength | |⤶
| float4 | g_vCubemapNormalizationParams | |