imageAtomicMin/Max not working?

Hi,
I am using the fragment shader below to do a custom depth test using imageAtomicMax() on a uint32 image. Only if this test passes I am writing a color to another image (RGBA8). I am still getting flickering fragments. I also tried this without the custom depth test using the layout(early_fragment_tests) specifier to force early-z testing, which gives me exactly the same results.

I am very new to the image load/store functionality, so i may be missing something here. The texture images bound to the uniform images show expected values so the binding and clearing works.

Has anyone played around with the atomic functions and maybe can help or give me some hints on this?


#version 410 core

#extension GL_EXT_shader_image_load_store : require
#extension GL_ARB_gpu_shader5 : require // weird, need to include this for coherent keyword...

// input layout definitions 
//layout(early_fragment_tests) in;

// input/output definitions 
in per_vertex {
    vec3 normal;
    vec2 texcoord;
} v_in;

// attribute layout definitions 
layout(location = 0, index = 0) out vec4 out_color;

// uniform input definitions 
layout(size1x32) coherent uniform image2D  output_image;
layout(size1x32) coherent uniform uimage2D depth_image;

// implementation 
void main() 
{
    vec4 c = vec4(v_in.normal, 1.0);

    ivec2 frag_coord = ivec2(gl_FragCoord.xy);
    ivec2 out_coord = frag_coord; // later used for under sampling
    uint z          = uint((1.0 - gl_FragCoord.z) * float(0xFFFFFFFFu));
    if (z > imageAtomicMax(depth_image, out_coord, z)) {
        imageStore(output_image, out_coord, c);
        //memoryBarrier();
    }
    discard;

    out_color = c;
}

Regards
-chris