I’m trying to implement forward shading using additional texture data with positions and colors of light sources. That’s a 32x32 texture that I update every frame because of lights movement. It takes time to extract values from the texture data in the fragment shader program. For example, 20 light sources require to find and read 100 pixels of data per a fragment.
I know, forward shading is not the best way of rendering lighting. But maybe performance could be better if iterating over uniform arrays instead of reading 5 pixels per light source and extracting all the parameters from them?
For example, look at this method of extracting the light source position from 3 pixels that I use now:
vec3 get_data(int index) {
float x = float(index % texture_size) / float(texture_size);
float y = float(index / texture_size) / float(texture_size);
vec2 coord = vec2(x, y);
return texture2D(illumination, coord).rgb;
}
float data_to_axis(vec3 data) {
float r = 255.0 * data.r * 256.0 * 256.0;
float g = 255.0 * data.g * 256.0;
float b = 255.0 * data.b;
float value = r + g + b;
value = value - (max_uint24 * 0.5);
value = value * (axis_capacity / max_uint24);
return value;
}
...
vec3 light_position = vec3(
data_to_axis(get_data(pixel + 1)),
data_to_axis(get_data(pixel + 2)),
data_to_axis(get_data(pixel + 3))
);
...
Voting
If you’d like to have forward shading like that more optimized, please upvote for #6529
Hi, I’ve started working on this in an experimental fashion already, it just needs a bit of design and cleaning up probably done in a week or two as I’m currently doing it on the side for fun
Apologies for reviving an old thread, but now that this has been added, how would I access the values in a constant buffer in the fragment program?
For example, if I have constant_buffer.lights[3] = vec4(1.0, 0.5, 0.2, 0.0), would the light array show up as uniform mediump vec4 light3 or maybe something else?
Do you need to configure their data from the editor or just specify that there is a constant? If you don’t need to modify the data you just add a constant with the base name of the array, so if you have an array specified in the shader like so: uniform vec4 my_array[16] you just add a single constant in the list with the name “my_array”.
For editing in the material view there is an unfortunate side-effect that the UI doesn’t support vectors. We have planned an redesign of that view to fix this (and other UX issues) at some point, but if you need to change the “value” part of an array you need to open the material in a text file and do the modifications there. For now it’s probably easiest to just set the values from code (what I would do).