Unity uses internal screen space mask for pixel spot and point light?

Unity 5.5.3f1, Win10, DirectX11, GTX1060 Intel Core i-7, forward rendering

I’m testing the gpu cost of spot/point light, I thought in forward rendering, if the engine decides to apply a spot/point light on a single mesh, it applies the light on all vertex and pixels of that mesh. So I originally thought that if only a corner is shed by the light, all pixels of the whole mesh need to calculate that pass. But after I changed the standard pbs code to this:

half4 fragForwardAddInternal (VertexOutputForwardAdd i)
{
#ifdef POINT
    return fixed4(0.1, 0, 0, 1);
#elif SPOT
    return fixed4(0, 0.1 , 0, 1);
#endif
    FRAGMENT_SETUP_FWDADD(s)

    UnityLight light = AdditiveLight (IN_LIGHTDIR_FWDADD(i), LIGHT_ATTENUATION(i));
    UnityIndirect noIndirect = ZeroIndirect ();

    half4 c = UNITY_BRDF_PBS (s.diffColor, s.specColor, s.oneMinusReflectivity, s.smoothness, s.normalWorld, -s.eyeVec, light, noIndirect);
 
    UNITY_APPLY_FOG_COLOR(i.fogCoord, c.rgb, half4(0,0,0,0)); // fog towards black in additive pass
    return OutputForward (c, s.alpha);
}

it appears that spot/point light in forward rendering seems to have a pre-generated screen space mask to skip pixels not in the light area:


To be specific, the mask is always screen aligned rectangle, so I guess the light’s world space bound box is calculated to screen space uv bound rectangle, so then in pixel shading, pixels outside the rectangle is skipped. It seems work this way, but I can not find any relevant implementation in builtin shaders.

I’m planing the mesh combination and batching strategy to avoid too much GPU cost for lights, I can not find any documentation of this “mask”, anyone could share some insight about this? Thanks.

The actual implementation isn’t anywhere in any file we have access to, but Unity plays with the stencil values for draw calls in both forward and deferred. I believe setting stencil ops on a forward add pass disable’s Unity’s use of them for this optimization, but in deferred passes any defined stencil ops are completely ignored as Unity uses them for light layer masks, as well as to skip any area the skybox / forward rendered opaque objects render. Using the frame debugger in Unity the various stencil operations are hidden, but you can see them if you use RenderDoc to get a better sense of what it’s doing. No that you need to really as you’ve essentially stated the bulk of what any of us know in your post.

For optimization purposes it’s good to keep in mind that each additive light pass, while well optimized from a fill rate perspective, is still paying the full vertex shader cost for rendering the entire mesh. If your meshes are especially high poly this can be problematic for performance. Some game engines, like the one used in past versions of CoD, will cull individual triangles prior to being rendered to limit the vertex shader invocations, but Unity does not do anything like this. The other concern is if you batch large collections of objects that exist in disparate areas you are potentially paying more in the cost of rendering the entire batch multiple times vs. keeping batches localized and lower poly. It’s rare though, and obviously depends on how you handle lighting.

hi, bgolus, thanks for your reply.

The reason why I’m considering combining small meshes into one whole single mesh(no submesh), is that, unity will issue one batch(setpass call) for each of the small meshes(originally one batch), even if they’re lit by the same point/spot light, the performance impact of this fact is huge both on CPU and GPU. So to avoid this, I’m planning to combine every a few of them into one single mesh(change granularity), but this introduces the fillrate problem due to potential overdrawn and huge shading cost of light for even only a small portion of mesh lit. After some tests I found this “mask optimization” of unity, thinking that this might help relieve shading cost more or less if the camera is not in the bound box of the light.

Another thing to take into account is that, if the bound box of combined mesh is too big, the LODGroup may not be working as usual. But vert/tris count is currently the least performance intense factor on CPU to me, either setpass and batching, even submesh count of a combined mesh are consuming more CPU than vert/tris count alone. I don’t know what unity is internally doing with those submeshes when preparing batches, but it’s really slow.

Hi, I ran into the same issue today, I want to use the downside of drawing the same mesh each light in forward rendering to my advantage.
I’m intentionally displacing meshes each forwardadd drawcall, but I’m noticing that the meshes get cut off when they’re not in the bounding boxes of the lights.
@bgolus mentioned that stencil ops might disable this mask, but I couldn’t get that to work.
Is there any news on this phenomenon? or is there perhaps a way to increase the bounding box?

EDIT: Alright, I managed to fix the issue by using editing the Light.boundingSphereOverride