S&box Wiki

Revision Difference

Constant_Buffers#545914

<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 );⤶ }⤶ }⤶ ```⤶ ⤶ ## 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) |⤶ ⤶ ⤶ ## 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"`⤶ ⤶ | 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 | |⤶