Reading texture inside compute shader

Hi! After many years I’m back to use OF and I’m very happy.
I’m trying compute shader to move some particles.
Is it possibile to use texture data (from an fbo) inside a compute shader?
I’m not sure if I have to use the “setUniformTexture” like this:

computeShader.setUniformTexture("field", field.noiseFbo.getTexture(), 0);

(but don’t know how to get the data inside the compute shader)
or if I need to create an ofBufferObject where I store my data.
The point is that I need to update the data of the texture inside my OF update function.
Any suggestions?

Hey @Mauro glad you’re back working with OF again! Have you looked at the textureBufferInstancedExample? While it doesn’t have a compute shader, I’m thinking a similar approach would work. Its uses an ofBufferObject and an ofTexture that has been allocated with .allocateAsBufferTexture().

Hi @Mauro . setUniformTexture should work just like you are calling it. In your compute shader, just add:

layout(binding = 0) uniform sampler2D field;

And then you should be able to access the data using

texture(field, yourUVCoords)


Also one more quick thought. An ofBufferObject and associated ofTexture will allow data to be computed (updated) by the cpu before it’s sent to the gpu, like in the example.

Multiple passes of shaders can also update data on the gpu. The gpuParticleSystemExample uses ofFbos for position and velocity data, and shaders calculate new values for those textures every frame. The GL_RGBA32F format for textures can be helpful when doing calculations with shaders.