OpenGL 读写相同的纹理

2021-12-19 00:00:00 opengl c++

我有一个 RGBA16F 纹理,上面有深度、normal.x、normal.y.我想读取 r、g、b 并写入纹理上的 a.我会准确地击中每个像素一次.

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.

您无法从您通过 FBO 写入的图像中读取并期望获得合理的结果.这会产生未定义的行为.

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

如果您使用 shader_image_load_store 进行读/写,您可能可以逃脱.但即便如此,它也是一个读/修改/写操作;你必须写回你读到的 alpha.

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.

话虽如此,如果您确定每个像素恰好命中一次"(强调),那么您确实有追索权.即,NV_texture_barrier.不要让这个扩展上的NV"欺骗你;它也广泛应用于 AMD 硬件(所有 HD 系列卡).这个扩展允许你使用一个屏障"函数(实际上是一个告诉 GPU 清除帧缓冲区和纹理缓存的函数),在调用后,它将允许你完全 one 读取/在片段着色器中修改/写入.在第一次通过之后,您需要在第二次通过之间建立另一个障碍.

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.

相关文章