我目前正在 OpenGL 3D 引擎中实现深度剥离。我想将值存储在深度 2D 纹理数组中。该算法在第 n 次执行时需要读取第 n-1 层,如果当前值较大(对象较远),则将当前值插入第 n 层。但是,我们不应该能够在相同的纹理中进行读写。
例如,是否可以从中读取(仅第 n-1 层)并将第 n 层附加为当前 FBO 的深度附件?
However, we are not supposed to be able to read and write in the same texture.
Says who?
Textures store images. Note the plural. There is no prohibition against reading from and writing to the same texture. The prohibition is against reading from and writing to the same image.
Array textures contain multiple images. Each array layer is its own 2D image (or set of 2D mipmap images). Therefore, it is perfectly legal to read from one array layer and write to another. It's perfectly legal to read from one mipmap in an array layer and write to another mipmap in the same array layer.
What is not legal is reading/writing on the same mipmap of the same array layer.
This is why OpenGL doesn't give an error if the same texture is attached to the FBO at the same time it is bound to the rendering context for reading. This is legal as long as you ensure that you're not reading from/writing to the same image.