Optimizing Per-Pixel Point Lights

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
cyberjunk
Halfling
Posts: 51
Joined: Tue Jun 05, 2012 4:33 pm
x 1

Optimizing Per-Pixel Point Lights

Post 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.
hyyou
Gremlin
Posts: 173
Joined: Wed Feb 03, 2016 2:24 am
x 17
Contact:

Re: Optimizing Per-Pixel Point Lights

Post 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.
Last edited by hyyou on Mon Apr 18, 2016 5:29 am, edited 1 time in total.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Optimizing Per-Pixel Point Lights

Post 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.
cyberjunk
Halfling
Posts: 51
Joined: Tue Jun 05, 2012 4:33 pm
x 1

Re: Optimizing Per-Pixel Point Lights

Post by cyberjunk »

Thank you very much for your responses.
I'll try it out and let you know :)
cyberjunk
Halfling
Posts: 51
Joined: Tue Jun 05, 2012 4:33 pm
x 1

Re: Optimizing Per-Pixel Point Lights

Post 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.
scrawl
OGRE Expert User
OGRE Expert User
Posts: 1119
Joined: Sat Jan 01, 2011 7:57 pm
x 216

Re: Optimizing Per-Pixel Point Lights

Post 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.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 114

Re: Optimizing Per-Pixel Point Lights

Post 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.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
Post Reply