Using image load and store, i would like to do the following in GLSL 4.2:
vec3 someColor = ...;
vec4 currentPixel = imageLoad(myImage, uv);
float a = currentPixel.a/(currentPixel.a+1.0f);
vec4 newPixel = vec4(currentPixel.rgb*a+someColor*(1.0f-a),currentPixel.a+1.0f);
imageStore(myImage, uv, newPixel);
the value for ‘uv’ can be the same for multiple rasterized pixels. In order to get the proper result, of course I want no other shaderexecution to write into my pixel inbetween the calls of imageLoad() and imageStore();
Is this possible to do somehow with memoryBarrier? if so, how does it have to be used in this code?
Then you can’t do it.
memoryBarrieris not a way to create an atomic operation. It only guarantees the ordering for a single shader’s operation. So if a particular shader invocation reads an image, writes it, and then reads it again, you need amemoryBarrierto ensure that what is read is what was written before. If some other shader invocation wrote to it, then you’re out of luck (unless it was a dependent invocation. The rules for this stuff are complex).If you’re trying to do programmatic blending, then you need to make certain that each fragment shader invocation reads/writes to a unique value. Otherwise, it’s not going to work.
You don’t say what it is you’re trying to actually achieve, so it’s not possible to provide a better way of getting what you want. All I can say is that this way is not going to work.