How to clear a texture generated on the GPU?

In my shaders I want to store extra information in a texture that will be retrieved later by the CPU, to do so I’m using imageStore() to write the information in a bound texture :

        self.data_texture = Texture()
        self.data_texture.setup_2d_texture(255,255,Texture.T_unsigned_byte, Texture.F_rgba8)
        self.data_texture.set_clear_color(LColor(0, 0, 0, 0))
        self.render.set_shader_input("data_store", self.data_texture)

Once the frame has been rendered, I retrieve the texture data on the CPU :


So far so good, however not all pixel of the data texture are written (most of the time because there are no object on that part of the screen/texture), so I need to clear the texture before each frames, but simply calling data_texture.clear_image() does not seem to work, parts of the texture that should be cleared still contains data from previous frames.

So, is there a way to clear the texture on the GPU or should I replace the texture each frame ?

clear_image() should work. It does not clear the image immediately, but removes the RAM image and marks it as having been modified, which has the effect of clearing it the next time it is being used for anything on the renderer side. How are you determining that it has not yet been cleared? Could you put together a simple sample?

Also, does make_ram_image() after clear_image() work?

make_ram_image() does not help either.

In the texture there should be data only where there is an object present, if there is no longer an object over a given texel, it should contains the clear color and no longer the data associated with that object.

I will make a simple demo to show the problem.

Merf, I feel stupid… With the demo code, everything works as expected!

Actually in my own app I put clear_image() in the wrong task, and so the texture was never cleared :frowning: Next time I will write the demo code first, then ask for help :slight_smile:

No worries, glad you figured it out!