hello, I want to support up to 32 vertex point lights in a uber shader for a mobile game(support OpenGLES2).
I would like to hear some suggestions about the best way to do it before commit my current code.
Why not just use what Unity provided?
Unity’s default point light is calculated per pixel & it is additive blending by extra drawcalls, performance seems not good for mobile I guess (+drawcalls & heavy fragment shader & many overdraw, I don’t want them).
I believe unity have some vertex lit LightMode, but I am not sure how they work & how to add them to my uber shader.
What I have done currently
So I implement vertex point light in a naive way first (not rely on any Unity’s lighting system, just mono behavior & some GlobalVectorArrays to let shader know what lights to render). My uber shader will ALWAYS
1.compute 32 separate vertex point light’s additive colors in vertex shader
2.add them up in vertex shader
3.pass this single result additive color(step 2) to fragment shader by fixed4 interpolater
4.just add that result to final fragment shader color(so per pixel calculation is just an ADD).
I tested this solution on some mobiles devices:
-some of them did not affect fps at all(currently fragment bound by image effects), which is good!
-some of them did cost 2~3ms(maybe 32 point lights in vertex shader are just too heavy)
which seems to be a not perfect but at least practical solution.
What is the problem?
when no point light is active & visible, I just turn off the whole point light part by Shader Keyword,
which is perfect(0 performance cost).
but sometime I just need a few point light(1~3), because my solution can only support 0 or 32 vertex point light(either off or on), setting the rest unused point light(20+) to black seems like a big waste.The cost of having 1 or 32 point light is the same.
I want to skip every unnecessary calculation for performance reason.
How other developer handle “single pass multi light”?
To improve the performance of my solution, I downloaded The Lab Renderer by Valve and study how they handle dynamic point lights in VR platform.
I believe I found the most important part by reading their vr_lighting.cginc
//-------------------------------------//
// Point, spot, and directional lights //
//-------------------------------------//
int nNumLightsUsed = 0;
[ loop ] for ( int i = 0; i < g_nNumLights; i++ )
{
float3 vPositionToLightRayWs = g_vLightPosition_flInvRadius[ i ].xyz - vPositionWs.xyz;
float flDistToLightSq = dot( vPositionToLightRayWs.xyz, vPositionToLightRayWs.xyz );
if ( flDistToLightSq > g_vLightFalloffParams[ i ].z ) // .z stores radius squared of light
{
// Outside light range
continue;
}
if ( dot( vNormalWs.xyz, vPositionToLightRayWs.xyz ) <= 0.0 )
{
// Backface cull pixel to this light
continue;
}
//.......
seems The Lab Renderer use a dynamic forloop(loop iteration is controlled by Shader.SetGlobalInt()) to skip unwanted lighting calculation, and inside forloop they use “if(condition) continue;” to further cull lights that will have black result(not affecting rendering)
so the first question:
is writing forloop in mobile vertex shader a good idea?(In my situation, about 50k vertices will run vertex point light calculation per frame)
I know writing forloop with fixed iteration count is safe(hardcode iteration count in shader), and on the other hand, I know there will be dynamic branching cost if the iteration count is calculated in shader.
But how about setting the iteration count by uniform?(set iteration count by Shader.SetGlobalInt() just like what The Lab Renderer did)?
the second question:
if “forloop & set iteration by uniform(Shader.SetGlobalInt())” is safe to use,
is writing “if(condition) continue;” to cull light a good idea?(just like what The Lab Renderer did)?
All I want to do is to add many vertex point light for mobile platform without costing too much performance.
if there is a totally different but better solution, I would like to hear it also.
thank you!