Posts

Showing posts from June 28, 2019

Using compute shaders for reading/writing to textures and FBO

To see how to use compute shaders for pixelwise working with images, see openFrameworks example examples/gl/computeShaderTextureExample . Also, I modified this example on using FBO (ofFbo) instead textures (ofTexture). As I checked, that FBO's textures (fbo.getTextureReference) also works with shader for reading and writing. So, FBOs can be used in compute shaders as an universal "drawable" textures: it's possible to read and write to them pixelwise, and it's possible to draw in them using OpenGL rendering (fbo.begin()...fbo.end()) Additional links: https://www.khronos.org/opengl/wiki/Image_Load_Store https://www.khronos.org/opengl/wiki/Buffer_Object#Buffer_Object_Usage

Creating particles with compute shaders in openFrameworks

In openFrameworks it's easy to create particles system with compute shaders, that is, particles physics is updates using GPU. So, you may manipulate millions particles realtime, compared with hundred thousand using CPU. The example to start with is examples/gl/computeShaderParticlesExample . It's based on declaring Particle structure - just like normal CPU particle definition with position, speed and color, and defining array vector<Particle> particles; which holds all particles. But, the next big step is using ofBufferObject which maps this vector to GPU memory. particlesBuffer.allocate(particles,GL_DYNAMIC_DRAW); ( About GL_DYNAMIC_DRAW - see details here ) particlesBuffer2 is similar and is used as previous particles state during computations, and is filled with command particlesBuffer.copyTo(particlesBuffer2); The position and color is used in ofVbo vbo object vbo.setVertexBuffer(particlesBuffer,4,sizeof(Particle)); vbo.setColorBuffer(particl