Blending using a shader?

New to shaders. Recently ordered the Orange Book.

Hoping someone can answer a question related to blending of images. For a variety of reasons I want to perform my own blending operations during certain stages of rendering. At one point in my drawing routine I need to not blend incoming textures against the frame buffer, only with each other. It’s hard to explain but I’ll try.

Here’s some pseudo code:


clear framebuffer buffer with black

for(i < numtextures)
  drawtexture(i) //do NOT blend against black background

The kicker is I only want to blend the textures with one another, not against the background. I’m fine with simply overwriting the framebuffer contents with the texture, but I need the textures to blend with one another.

I probably don’t even need to use a shader to do this, but I’m drawing a blank on how to do this procedurally.

You are new to shaders.

Shaders do not deal with the framebuffer. All they do is output a color that will go into the pixel blend stage to interact with the framebuffer.

Your fundamental error in thought comes from the concept of “stages”. You don’t have any.

You are now running a program on the GPU. Your inputs (for the fragment shader, in this case) are the per-vertex varying parameters and any per-object uniform variables you may have set. Your outputs are a single color (perhaps many if you’re rendering to multiple buffers) and optionally a depth value, if you want to affect depth.

Textures do not get drawn; they are accessed. They return a color that you can do whatever you want with. If you want to just forget about it, don’t access the texture at all. Maybe it’s a normal map; dot the result with the

So, conceptually, a fragment shader looks like,

uniform color ambientColor;
fragment_shader(varying texCoord1, varying texCoord2, varying lightDirection)
  color TempColor;
  TempColor = AccessTexture(0, texCoord1);
  TempColor *= clamp(dot(AccessTexture(1, texCoord2), lightDirection), 0, 1.0);
  return TempColor + ambientColor;

Note that this is not glslang; it is only pseudo code to give you the idea of what’s going on.

The first line accesses texture one; presumable a diffuse color texture. The second line accesses texture 2, which is probably a normal map, and dot’s the result with the light direction to produce an intensity for the incoming light. The intensity is then clamped and multiplied into the color from texture 1. Lastly, the function returns the sum of TempColor and ambientColor.

Key concept: it’s a program, not a fixed set of stages. You don’t draw textures or blend them. You merely access them and do stuff with the results. And the data from a texture can be anything, not just a color. And none of this directly touches what pixel is in the framebuffer; that’s handled by something else.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.