0

If I have a Renderbuffer that uses a color format without alpha, for example GL_RG8, how can I tell the alpha blender to use the green channel for alpha? This can be done in textures using a swizzle mask, but as renderbuffers don't support those, what can I do?

My current blendFunc is GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Fuzzyzilla
  • 356
  • 4
  • 15
  • what about `glBlendFunc` using `GL_???_COLOR` but that would use RGB not just G. – Spektre Jul 16 '18 at 07:15
  • @Spektre unfortunately in my case the Red channel stores information that is unrelated to the alpha, so that wouldn't work :( – Fuzzyzilla Jul 16 '18 at 07:23
  • Are you trying to use the destination "alpha" or the alpha that the fragment shader's output provides (ie: the source color)? – Nicol Bolas Jul 16 '18 at 16:04
  • @Fuzzyzilla: "*This can be done in textures using a swizzle mask*" No, it can't. Swizzle masks *only apply* to reads from a shader. Blending operations do not apply the swizzle. – Nicol Bolas Jul 16 '18 at 16:07
  • @NicolBolas my current blendFunc is `GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA`. Also, thank you for the statement regarding swizzle masks, I had read somewhere that swizzle masks effect all read operations, even built-in ones (exception: blits), but maybe that is incorrect. – Fuzzyzilla Jul 16 '18 at 17:27

1 Answers1

3

Each user-defined output of the fragment shader contains 4 channels: RGBA. This is true regardless of the image format of the destination that the output will write to. These outputs are the source colors for the blend operation.

So just write to the alpha of the output as normal. It doesn't matter that the alpha won't be written to the framebuffer image. It's still a part of the source color, so it can still be used for blending purposes.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • "Each user-defined output of the fragment shader contains 4 channels: RGBA. This is true regardless of the image format of the destination that the output will write to." This was my problem. I had used `out vec2 gl_FragColor` - which works as you might expect it when GL_BLEND was disabled - but changing it out for a `vec4` and using duplicating the green across the G and A channels of the output made it work with alpha blending. Thank you for your help! – Fuzzyzilla Jul 16 '18 at 18:09