I’m playing around with compute shaders for the first time. basically, i need to calculate a texture inside a compute shader via imageStore and then display it - which sounds simple enough, yet my program crashes and I can’t seem to figure out the reason for it.
the rendering code:
// compute shader pass - fills the texture
// work groups: 32 x 32, work group size: 16 x 16 -> 512 x 512 texture
glBindImageTexture( 0, tex, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F );
glUseProgram( storeProg );
glUniform1f( glGetUniformLocation( storeProg, “val” ), 0.5f );
glDispatchCompute( 32, 32, 1 );
glUseProgram( 0 );
glBindImageTexture( 0, 0, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F );
glMemoryBarrier( GL_TEXTURE_FETCH_BARRIER_BIT );
// render pass - draws fullscreen quad
glBindTexture( GL_TEXTURE_2D, tex );
glUseProgram( renderProg );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 2, GL_FLOAT, false, 2*sizeof( float ), nullptr );
glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glDisableVertexAttribArray( 0 );
glUseProgram( 0 );
glBindTexture( GL_TEXTURE_2D, 0 );
the involved shaders are straight-forward as well:
compute shader:
#version 420 core
#extension GL_ARB_compute_shader : require
layout ( binding = 0, rgba32f ) writeonly image2D img;
uniform float val = 0.0;
layout ( local_size_x = 16, local_size_y = 16 ) in;
void main()
{
[INDENT] imageStore( img, ivec2( gl_GlobalInvocationID.xy, vec4( val ) );
}[/INDENT]
render pass vertex & fragment shader:
#version 330 core
layout ( location = 0 ) in vec2 position;
out vec2 uv;
void main()
{
[INDENT] uv = 0.5 * position.xy + vec2( 0.5 );
gl_Position = vec4( position, 0.0, 1.0 );
}
#version 330 core
in vec2 uv;
out vec4 fragColor;
layout ( binding = 0 ) uniform sampler2D tex;
void main()
{
fragColor = texture( tex, uv );
}[/INDENT]
what’s puzzling me the most is that swapping the ordering of rendering pass & compute shader pass removes the crash - so it probably has something to do with synchronization that I’m unaware of; yet changing the memory barrier to GL_ALL_BARRIER_BITS does not help me at all.
the whole program code can also be seen here: Dropbox - Error