3

Is there a way to read fragment from the framebuffer currently rendered?

So, I'm looking for a way to read color information from the fragment that's on the place that current fragment will probably overwrite. So, exact position of the fragment that previously rendered.

I found gl_FragData and gl_LastFragData to be added with certain EXT_ extensions to shaders, but if they are what I need, could somebody explain how to use those?

I am looking either for a OpenGL or OpenGL ES 2.0 solution.

EDIT:

All the time I was searching for the solution that would allow me to have some kind of read&write "uniform" accessible from shaders. For anyone out there searching for similar thing, OpenGL version 4.3+ support image and buffer storage types. They do allow both reading and writing to them simultaneously, and in combination with compute shaders they proved to be very powerful tool.

2
  • 2
    Do you mean desktop OpenGL or OpenGL ES? Commented May 13, 2013 at 21:16
  • It doesn't mater. Any will do. Commented May 13, 2013 at 22:43

2 Answers 2

6

Your question seems rather confused.

Part of your question (the first sentence) asks if you can read from the framebuffer in the fragment shader. The answer is, generally no. There is an OpenGL ES 2.0 extension that lets you do so, but it's only supported on some hardware. In desktop GL 4.2+, you can use arbitrary image load/store to get the same effect. But you can't render to that image anymore; you have to write your data using image storing functions.

gl_LastFragData is pretty simple: it's the color from the sample in the framebuffer that will be overwritten by this fragment shader. You can do with it what you wish, if it is available.

The second part of your question (the second paragraph) is a completely different question. There, you're asking about fragments that were potentially never written to the framebuffer. You can't read from a fragment shader; you can only read images. And if a fragment fails the depth test, then it's data was never rendered to an image. So you can't read it.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you have link with further explanation of gl_LastFragData and maybe its usage? What are dependencies? Is it supported by webGL? It seems like that's what I need, but I can't google it. :/
@AbstractAlgorithm: It's from the extension I linked to. The dependency is that... it's an extension. If the extension isn't supported, you can't use it. It's usage is... if you want to know the color of the sample being written to, you read it. You know, like any other variable. It's just a variable; it's not magic. It's not something that has "usage" outside of the way that any other variable has "usage". I'm not sure what you're looking for here.
1

With most nVidia hardware you can use the GL_NV_texture_barrier extension to read from a texture that's currently bound to a framebuffer. But bear in mind that you won't be able to read data any more recent than produced in the previous draw call

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.