Game Development Community

Accessing the lights in the scene from the GPU

by Lukas Joergensen · in Torque 3D Professional · 04/01/2014 (5:02 pm) · 11 replies

Hey guys, I'm hoping you can help me.
I'm trying to send all the lights in the scene to the GPU in some form.
I'd like to save it in memory on the GPU while I call a bunch of shaders, so I don't have to send the information every time I call one of the shaders.
(FYI I'm calling the shaders in one go, I just need to access the information across all the shaders so I was hoping for some optimization)

Can this be done using the GFX layer of T3D? And whats the cleanest way of doing it?

Thanks for your help in advance!

Edit:
Could you use a ConstHandle?
Somehow make this:
mShader.mModelViewProjSC = mShader->getShaderConstHandle( "$modelViewProj" );
Hold an array of e.g. float3's ?
Or would that still be sent to the GPU everytime a shader is called?

#1
04/02/2014 (6:21 am)
You already have the light information, this is the light buffer.
Just unpack and pre-pass the data.

Refer to this tutorial:
liman3d.com/tutorial_cm_shadows.html

If you need to sample additional properties, you can bake the info to a indexed RT.
#2
04/02/2014 (6:30 am)
Unfortunately I need the position, color and attenuation of each light. Basically I need to do another light pass for a special kind of objects that are not covered by the first lighting pass.
It's worth noting that what I'm saying might very well be nonsense, I don't claim to be an expert in the field :)

Edit: So doing a it in another RT, would it be possible to avoid sending the RT back and forth, and just keep it on the GPU?
#3
04/03/2014 (7:46 am)
U can see this tutorials where i use the lights info (for the tutorials olny one light):

T3D Custom Shader [Part 1] (Base)
T3D Custom Shader [Part 2] (Textures Diffuse/Normal/Specular)
T3D Custom Shader [Part 3] (Shadows)

With the light position data u can set the attenuation
#4
04/06/2014 (3:11 pm)
I've been trying for a while now to bake the information to a texture but with no luck..

Can someone show me an example of how to bake a list of positions to a texture and send it to the shader? That would really be appreciated!
#5
04/09/2014 (6:45 am)
Just use uniforms if your positions are not many.
#6
04/17/2014 (5:21 pm)
You need:

* LightPosition and color ( as on my exammple vertex shader)
OUT.LightPosition0 = float3(inLightPos[0].x, inLightPos[1].x, inLightPos[2].x);
OUT.LightPosition1 = float3(inLightPos[0].y, inLightPos[1].y, inLightPos[2].y);
OUT.LightPosition2 = float3(inLightPos[0].z, inLightPos[1].z, inLightPos[2].z);
OUT.LightPosition3 = float3(inLightPos[0].w, inLightPos[1].w, inLightPos[2].w);

OUT.LightColor0 = inLightColor[0].rgba; 
OUT.LightColor1 = inLightColor[1].rgba; 
OUT.LightColor2 = inLightColor[2].rgba; 
OUT.LightColor3 = inLightColor[3].rgba;

* The depth information of the scene (depthmap) to calculate the distance from each light position and the textel to be illuminate.

* And an attenuation value for each light

So u have all to can calculate and aply your lights:

LightColor0
LightPosition0
LightAttenuation0
DepthInfo
#7
04/18/2014 (3:58 am)
@Alfio that is fine if you want just 4 lights.
However I want to experiment with support for all the lights in the scene, our rendering model supports hundreds of lights in the scene and I want to do so as well, otherwise it'd would be a huge issue in itself just to pick out what lights the shader should take into account.

To do that I think the only way is using a custom sampler, and I've almost completed that I just need some help in this thread.
#8
04/18/2014 (5:33 am)
I seem to recall that one of the developers of T3D, he has told me that torque exports to the shaders only the 4 most important lights (i presume the 4 lights closest to the camera). So you will need to modify most of the GFX code.
#9
04/18/2014 (6:54 am)
Quote:only the 4 most important lights

I think you're right, when I was trying to get more understanding of the shaders I stumbled upon a function called calculate4lights. I think it was using some kind of an array with only 4 lights. Browsing the source for this might shine a little light on it (forgive me the pun).
#10
04/18/2014 (8:21 am)
There...are...four lights!
#11
04/22/2014 (2:32 am)
Hmm I'd find it weird if it sent those 4 lights to all shaders..
Anyways I think I'd be more comfortable with more than 4 lights.. I need it for lighting particles, and just the 4 lights closest to the camera wouldn't cut it..

The 4 lights with the most influence on each particle system would do, but thats a "big" calculation in itself.