Dominick
Dominick

Reputation: 321

Read and Write in one Texture (OpenGL)

I want to store and update informations in a texture. So the idea is, that I create a new texture with current informations. While storing it in the render process I actually want to read the informations out of the same pixel and store a weighted average of both values. So the value that was rendered to that pixel and the value that was already on that pixel.

Now I read very often that I can not read and write on the same texture. Now my questions is, may it maybe be possible? and if not should I copy the texture information, before the rendering step and pass the copy to the shader? If so, how can I copy the texture? or should I do a extra rendering step for copying?

Upvotes: 0

Views: 322

Answers (2)

BDL
BDL

Reputation: 22157

I see two possible options here, depending on the mix equation

  • Alpha Blending: If the equation used can be mapped to one of the glBlendFunc functions, then this is the way to go. If you want to use linear factors for the stored and the new value this should be possible. This is also the option where I would expect the best performance.

  • Image Load Store: With this method one can read and write to the same texture at the same time (see here). The performance will usually be very bad here and you will have to use the image atomic operations to ensure that multiple fragments at the same location always read the correct value.

Copying the texture would, in my opinion, only work if you render an image and then perform one weighted average computation on it afterwards (otherwise you would have to copy the texture after each store operation). But if this is the case, one could simple render the result of the average computation to a different texture and completely avoid all the trouble of copying the input data.

Upvotes: 2

Aiman Al-Eryani
Aiman Al-Eryani

Reputation: 709

If resorting to an extension is an option, you can use NV_texture_barrier which allows writing and reading from the same texture.

Upvotes: 0

Related Questions