简体   繁体   中英

OpenGl read and write to the same texture

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?

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.

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

A texture can be either data source or target, mutually exclusive. You can't use a texture for both at the same time.

Also texture writes (well, image writes) have been introduced only with OpenGL-4.2, and they're not very performant, because GPUs are optimized for gather reads, not scatter writes.

You cant read and write from a single texture at the same time. To get round this you usually create another texture so that you read from one texture and write into another. These two textures can then be read back together or individually in your shader.

This can be done if you can write first and then copy the result to a texture. In this case, you render with your shader first and then after it all gets rendered, you would copy the result to a texture with

glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, windowWidth, windowHeight, 0,GL_RGB, GL_UNSIGNED_BYTE, 0);

And also use this texture as the input of your shader.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM