Surface Cache Global Illumination (GI) delivers indirect lighting through a full-screen buffer that shaders sample at their screen position, rather than through lightmap coordinates or probes. A code shader receives that lighting only if it declares the right shader keyword and passes its screen position to the lighting library.
Shaders you create in Shader Graph with a Lit target already do both, so you don’t need to change them.
This page covers code shaders that use the URP shader library. Shaders that implement lighting entirely themselves aren’t covered, because they don’t go through the library function described here.
Before you begin, set up Surface Cache GI in your project. Refer to Set up Surface Cache Global Illumination.
Unity compiles the Surface Cache code path into your shader only when the _SCREEN_SPACE_IRRADIANCE keyword is available.
Add the following line to each pass that calls InitializeBakedGI:
#pragma multi_compile_fragment _ _SCREEN_SPACE_IRRADIANCE
This includes the pass with the UniversalForward light mode, and, if your shader also supports the Deferred rendering paths, the pass with the UniversalGBuffer light mode. Surface Cache GI works in all four rendering paths, so a shader that supports both passes needs the keyword in both.
Note: Don’t add the keyword to passes that don’t calculate lighting, such as the ShadowCaster pass.
URP gathers the inputs for indirect lighting in a GIParams struct, then fills InputData through InitializeBakedGI. Surface Cache GI reads the lighting at a screen position, so the positionSS field must contain the fragment’s screen position in pixels.
In your fragment function, build the struct and call InitializeBakedGI:
GIParams giParams = (GIParams)0;
giParams.positionWS = inputData.positionWS;
giParams.normalWS = inputData.normalWS;
giParams.viewDirWS = inputData.viewDirectionWS;
giParams.positionSS = input.positionCS.xy;
giParams.isSurfaceTypeTransparent = IsSurfaceTypeTransparent();
InitializeBakedGI(giParams, inputData.bakedGI, inputData.shadowMask);
Despite its name, input.positionCS already holds screen-space pixel coordinates at the fragment stage, through the SV_POSITION semantic. You don’t need to divide this value by _ScaledScreenParams, as you would when you reconstruct a world position from depth.
Zero-initialize the struct with (GIParams)0. Fields you leave at zero select neutral behavior, so you only need to fill the fields your pass has data for. If your shader also supports lightmaps or Adaptive Probe Volumes, then you must also fill the fields those paths use, such as staticLightmapUV and vertexSH. Unity selects the right source at compile time based on the keywords that are set.
The field that matters for Surface Cache GI is positionSS. A shader written before Surface Cache GI existed usually fills only the lightmap and probe fields, so it compiles and runs but receives no indirect lighting.
Surfaces drawn with your shader now receive the same indirect diffuse lighting as surfaces drawn with URP’s Lit shader.
Transparent surfaces are a deliberate exception. Surface Cache GI doesn’t light them, so when isSurfaceTypeTransparent is true, Unity falls back to the ambient probe. This happens automatically and needs no extra code.