Any advice?
I am trying to write a compute shader that modifies a texture that is later used as a ‘conventional’ texture/sampler2D in the rendering shader. The render shader applies the textures to a vbo mesh in the usual vert/frag way:
ofApp::setup(){
ofDisableArbTex();
...
ofFboTex->getTexture().loadData(simworld_colors.data(), worldSize.x, worldSize.y, GL_RGB);
}
ofApp::draw(){
...
shaderRender.begin();
shaderRender.setUniformTexture("tex0", ofFboTex->getTexture(), 0);
vboTerrain.draw();
}
renderShader.frag.glsl:
uniform sampler2D tex0;
main(){
out_fragColor = texture(tex0, vary_texCoord);
}
This part is working.
Before the draw/render I want my compute shader to modify the texture.
It needs to do something like… what?
ofApp::update(){
...
shaderCompute.begin();
// This seems wrong...
shaderCompute.setUniformTexture("tex0", ofFboTex->getTexture(), 0);
// or this?
ofFboTex->getTexture().bindAsImage(0, GL_WRITE_ONLY);
shader.dispatchCompute(worldSize.x, worldSize.y);
}
computeShader.compute.glsl:
layout(local_size_x = 1, local_size_y = 1) in;
layout(rgba32f, binding=0) uniform writeonly image2D tex0;
main(){
texPos = ivec2(2, 2); //calcTexCoord(gl_GlobalInvocationID, ...);
newColor = vec4(1.0, 1.0, 0.0, 1.0);; //computeColor(...);
imageStore(tex0, texPos, newColor);
}
But this does not seem to affect the texture buffer.
I have looked at a few examples:
How vert/frag shader can behave like compute unit by doing ping-pong renders to a texture as in
examples/gl/gpuParticleSystemExample
This is how my code currently works.
How a compute shader can do ping-pong writes to a image2D as in
examples/gl/computeShaderTextureExample
This is very close to how I want my code to work, I just want to write to an ‘actual texture’.
How a compute shader can write to ofVbo mesh color attributes as in
examples/gl/computeShaderParticlesExample
But I don’t want to write vertex color attributes.