Page 1 of 1

Optimizing Per-Pixel Point Lights

Posted: Sat Apr 09, 2016 7:33 pm
by cyberjunk
Hey there,

I'm using Ogre 1.10 and sometimes I gotta deal with quite a lot of point lights in my scene (max 48).
I'm doing it per-pixel, so currently I got a first pass on my materials to do ambient + directional light and then I got a second pass for each point-light like this:
pass
{
illumination_stage per_light
iteration once_per_light point
max_lights 48
scene_blend add
..
}
The pixel-shader which belongs to this second pass and adds light from the different point lights isn't very expansive, but running it so many times can be...
I'm looking for ways to improve this, so:

1) Does Ogre actually sort out point-lights which are too far away from the mesh with the mentioned material set?
Or will the second pass be run with any point light in the scene? My scene can be rather large, and the point-light affected areas are rather small.
So for many meshes/entities in the scene a lot of point lights could be skipped (based on distance from pointlight<->mesh_bbox_center)...
However setting the MaximumRenderingDistnace on the Light itself isn't what I wanted (will be based on camera<->light distance)

2) Compute multiple point-lights in one pass
I could technically pass the parameters for at least 2 or more lights to the pixelshader at once and combine the computations in there, creating the same result with less executions of the pixelshader.
Is this possible somehow? Like adjust "iteration once_per_light point" so it's something like "iteration once_per_two_light point"

Any suggestions welcome, thanks.

Re: Optimizing Per-Pixel Point Lights

Posted: Wed Apr 13, 2016 3:11 am
by hyyou
1. I tried glsl with 1 very-low-range-but-very-bright point light in a scene (gl_FragColor=1,1,1,1) & max-light=100.
The very-far-away mesh is not lit. (except shadow enabled)
So I assume that they it not give pass. (Thus save some GPU power.)
Edit: A part of this comment has been deleted because it is overshadowed by Kojack's answer.

Re: Optimizing Per-Pixel Point Lights

Posted: Wed Apr 13, 2016 6:49 am
by Kojack
Lights should be culled based on distance (the range setting of the light's attenuation factors). It is based on the origin of the object though, not the actual geometry (iirc).

I've never actually tried it, but in the material script manual it has:

Code: Select all

iteration 1 per_n_lights 2 point
    The render state for the pass will be setup and the draw call executed once for every 2 lights. 
What you could try (might or might not work this way, as I said I've never tried) is set that iteration to the number of lights you want (the second number) then when defining the shader parameters, add the index number of a light on all the light parameters. For example:

Code: Select all

param_named_auto firstlight light_position 0
param_named_auto secondlight light_position 1
etc.
I think that might do it a batch at a time.

There are a couple of other things you can do for a lot of lights, depending on the scene and how the lighting needs to look.
You could use spherical harmonic irradiance (for example tom forsyth used it in a game to get 50 lights working on a single mesh on a gamecube, which can only do 8 hardware lights and no shaders). That will give soft blurred lights though.
You could also use a more specific method, like my street light method that handled thousands of lights with the performance hit of 9 lights. But they need to be spaced apart (only minimal overlap of light volumes was possible.

Re: Optimizing Per-Pixel Point Lights

Posted: Sun Apr 17, 2016 7:19 pm
by cyberjunk
Thank you very much for your responses.
I'll try it out and let you know :)

Re: Optimizing Per-Pixel Point Lights

Posted: Sun Apr 17, 2016 8:43 pm
by cyberjunk
Kojack, you're the man!!! :-)
I was able to batch-up two point lights with the changes you proposed - I didn't even need to touch the app-code... all done in material and hlsl.

This is the commit on my game:
https://github.com/cyberjunk/meridian59 ... 02493678e3

Kudos to you!
A first test showed a FPS increase from ~180 FPS to ~250 FPS in an heavy point light scene

PS: I increased it to batch up 4 point-lights and it's up to ~300 FPS now. Very impressive.

Re: Optimizing Per-Pixel Point Lights

Posted: Mon Apr 18, 2016 3:56 am
by scrawl
Looks good. Note you could save yourself the hassle of copy/pasting the lighting code for each light. Just use the _array variant of the lighting parameters and a for loop.

Re: Optimizing Per-Pixel Point Lights

Posted: Wed Jun 01, 2016 5:40 pm
by mkultra333
If you're doing lots of point lights, you should probably look into deferred shading.
Edit: Oops, just noticed the last post is from 6 weeks ago.