Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGl read and write to the same texture

Tags:

c++

opengl

I have a RGBA16F texture with depth, normal.x, normal.y on it. I want to read r, g, b and write to a on the texture. I will hit every pixel exactly once.

Would there be a performance problem if I read and write to the same texture in this case?

like image 413
aCuria Avatar asked Jul 10 '12 09:07

aCuria


1 Answers

There won't be a performance problem. There will be a functionality problem. As in, it won't work.

You cannot read from an image that you are writing to via FBO and expect to get reasonable results. That's yields undefined behavior.

If you were using shader_image_load_store to do your reading/writing, you might be able to get away with it. But even then, it's a read/modify/write operation; you have to write back the alpha that you read.

That being said, if you are certain that you will "hit every pixel exactly once" (emphasis added), you do have a recourse. Namely, NV_texture_barrier. Don't let the "NV" on this extension fool you; it's widely implemented on AMD hardware as well (all HD-series cards). This extension allows you to use a "barrier" function (effectively a function that tells the GPU to clear the framebuffer and texture caches) which, after calling, will allow you to do exactly one pass of read/modify/write in your fragment shader. After that one pass, you need another barrier between that and your second pass.

like image 139
Nicol Bolas Avatar answered Sep 21 '22 01:09

Nicol Bolas