Forward+ Additional Lights in a CustomLighting Function // And a crash.

So the usual way of looping over lightsources now longer works with Forward+.

In this file

int GetAdditionalLightsCount()
{
#if USE_FORWARD_PLUS
    // Counting the number of lights in clustered requires traversing the bit list, and is not needed up front.
    return 0;
#else
    // TODO: we need to expose in SRP api an ability for the pipeline cap the amount of lights
    // in the culling. This way we could do the loop branch with an uniform
    // This would be helpful to support baking exceeding lights in SH as well
    return int(min(_AdditionalLightsCount.x, unity_LightData.y));
#endif
}

The functions for getting additional lights are changed to get light-structs from the structured buffer, so in my example, i can use GetAdditionalLight(0, d.positionWorldSpace) and it returns a lightstruct.

How do i know how many there are?
Or: How to i traverse the list in a better way?

Here in line 27+

there a cluster iterator loop defined. That works, with a little “but”.

    Light mainLight = GetMainLight(d.shadowCoord, d.positionWorldSpace, 1);
    float3 color = CustomLightHandling(d, mainLight);

    uint lightsCount = GetAdditionalLightsCount();
    LIGHT_LOOP_BEGIN(lightsCount)
        Light light = GetAdditionalLight(lightIndex, d.positionWorldSpace);
        float lightColor = light.color * light.distanceAttenuation;   
        color += light.color; // this line crashes the editor
    LIGHT_LOOP_END

This crashes unity with a D3D11 error saying GPU timeout.

Sources:

Functions and definitions:

URP shaders do use that way of looping additional lights:

1 Like

I’m still trying. I think i will create a ready to try code example of this and upload it later.

Update:
Cyanilux has a working implementation of additional lights in URP foeward plus.

One thing i missed back in '22 was the _FORWARD_PLUS keyword.

2 Likes

Thank u man! I missed the _FORWARD_PLUS too… and i crashed a lot…now i know how to fix this
All is the forwardPlus problem~~

Update for those searching and finding:
https://github.com/rsofia/CustomLightingForwardPlus

3 Likes