user2292539
user2292539

Reputation: 245

Getting the color of the back buffer in GLSL

I am trying to extract the color behind my shader fragment. I have searched around and found various examples of people doing this as such:

vec2 position = ( gl_FragCoord.xy / u_resolution.xy );
vec4 color = texture2D(u_backbuffer, v_texCoord);

This makes sense. However nobody has shown an example where you pass in the back buffer uniform.

I tried to do it like this:

int backbuffer = glGetUniformLocation(self.shaderProgram->program_, "u_backbuffer");
GLint textureID;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &textureID);//tried both of these one at a time
glGetIntegerv(GL_RENDERBUFFER_BINDING, &textureID);//tried both of these one at a time
glUniform1i(backbuffer, textureID);

But i just get black. This is in cocos2d iOS FYI

Any suggestions?

Upvotes: 2

Views: 1831

Answers (2)

Brad Larson
Brad Larson

Reputation: 170317

You can do this, but only on iOS 6.0. Apple added an extension called GL_EXT_shader_framebuffer_fetch which lets you read the contents of the current framebuffer at the fragment you're rendering. This extension introduces a new variable, called gl_lastFragData, which you can read in your fragment shader.

This question by RayDeeA shows en example of this in action, although you'll need to change the name of the extension as combinatorial points out in their answer.

This should be supported on all devices running iOS 6.0 and is a great way to implement custom blend modes. I've heard that it's a very low cost operation, but haven't done much profiling myself yet.

Upvotes: 2

Nicol Bolas
Nicol Bolas

Reputation: 474016

That is not allowed. You cannot simultaneously sample from an image that you're currently writing to as part of an FBO.

Upvotes: 0

Related Questions