PDA

View Full Version : surface splatting using MRTs



gmak26
02-07-2011, 05:36 AM
Hello,

I would like to implement a surface splatting using MRTs. The point is, I have thousands of splats and some of them are affecting the same fragment position. So, I would like to blend their color contributions, but I could not.

I am using 2 render targets. In the 1st pass, I assign 0 to all targets.
gl_FragData[0] = vec4(0.0);
gl_FragData[1] = vec4(0.0);

In the 2nd pass, I send all splats to fragment shader and compute their contributions. Now, I want to add this value to the value which was already written in the target as:

vec4 computedValue; //compute it as you like for each splat
vec4 newValue = texture2D(texture0, vec2(xpos,ypos)) + computedValue;
gl_FragData[0] = newValue;

vec4 computedValue2; //compute it as you like for each splat
vec4 newValue2 = texture2D(texture1, vec2(xpos,ypos)) + computedValue2;
gl_FragData[1] = newValue2;

So, for each new splat, I assume that it is adding up. I have to do this blending at each single frame, in 1 pass for all affecting splats. So, ping-pong does not work for me in which the neccessary data is read in the other pass.

But the result does not seem blended. I am reading from/writing to the same texture. Can it be a problem? The reference papers say that they are using MRTs with additive alpha blending. How can I implement it?

Thanks in advance..

kRogue
02-07-2011, 01:29 PM
Additive blending, means use GL blending, i.e.:



glEnable(GL_BLEND);
glBlendEquation(GL_ADD);
glBlendFunc(GL_ONE, GL_ONE);

will make it so the whatever you write in the fragment shader is added to the current contents. The above calls affect all buffer targets, to have fine control over what happens to each buffer target: glBlendEquationi, glBlendFunci. Lastly to have alpha channel treated differently than rgb channels treated: glBlendFuncSeparatei, glBlendFuncSeparate, glBlendFuncSeparatei and glBlendFuncSeparate.

Lastly, reading from a texture that you are writing to via an FBO in unextended GL is a major no-no. There are some extensions that let you do this in a limited fashion: GL_NV_texture_barrier (http://www.opengl.org/registry/specs/NV/texture_barrier.txt) (NVIDIA GL3 hardware) and GL_EXT_shader_image_load_store (http://www.opengl.org/registry/specs/EXT/shader_image_load_store.txt) (NVIDIA and ATI GL4 hardware only).

kRogue
02-07-2011, 11:43 PM
Could not edit, but I should clarify: GL_EXT_shader_image_load_store allows one to read and write texture data with random access from a shader, it does not allow one to bind a texture as a sampler and to use it for a render target.