Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 440
- #extension GL_NV_shader_thread_group : require
- //TODO: coherent for these instead?
- layout(r32ui) volatile uniform uimage2D img2D_0;
- layout(rgba32f) volatile uniform image2D img2D_1;
- layout(rgba32f) volatile uniform image2D img2D_2;
- layout(rgba32f) volatile uniform image2D img2D_3;
- layout(rgba32f) volatile uniform image2D img2D_4;
- layout(early_fragment_tests) in;
- in vec4 _vec_vert_eye_vf;
- void main(void) {
- ivec2 coord = ivec2(gl_FragCoord.xy);
- vec4 vec_vert_eye = _vec_vert_eye_vf;
- float depth_added = (abs(vec_vert_eye.z)-0.1)/(5.0-0.1); //the new fragment's depth: linear in range [0.1,5.0]
- bool needs_write = !gl_HelperThreadNV; //Only real fragments need to write; don't stall the thread warp waiting
- //for threads in helper pixels to insert their depths (often they can't
- //anyway, because their writes, including to mutexes, are undefined).
- while (needs_write==true) {
- bool can_write = imageAtomicExchange(img2D_0,coord,1u)==2u; //locked is 1, unlocked is 2
- memoryBarrier(); //TODO: probably not necessary?
- //Loading data from data textures can happen whether or not we can write.
- float depth0 = imageLoad(img2D_1,coord).b;
- float depth1 = imageLoad(img2D_2,coord).b;
- float depth2 = imageLoad(img2D_3,coord).b;
- float depth3 = imageLoad(img2D_4,coord).b;
- //Add the new fragment's depth ("depth_added") into the sorted list of depths
- if (depth_added<depth0) {
- depth3 = depth2;
- depth2 = depth1;
- depth1 = depth0;
- depth0 = depth_added;
- } else if (depth_added<depth1) {
- depth3 = depth2;
- depth2 = depth1;
- depth1 = depth_added;
- } else if (depth_added<depth2) {
- depth3 = depth2;
- depth2 = depth_added;
- } else if (depth_added<depth3) {
- depth3 = depth_added;
- }
- //Write data textures, iff we are the thread that has the right to change them.
- if (can_write) {
- imageStore(img2D_1,coord,vec4(0.0,0.0,depth0,1.0));
- imageStore(img2D_2,coord,vec4(0.0,0.0,depth1,1.0));
- imageStore(img2D_3,coord,vec4(0.0,0.0,depth2,1.0));
- imageStore(img2D_4,coord,vec4(0.0,0.0,depth3,1.0));
- }
- //Ensure the data textures will be visible to other threads both inside and outside
- // this warp once the mutex is unlocked below (no guarantees on ordering otherwise).
- memoryBarrier();
- //Unlock the mutex.
- if (can_write) {
- imageAtomicExchange(img2D_0,coord,2u); //TODO: this can be imageStore, theoretically?
- needs_write = false;
- }
- memoryBarrier(); //TODO: maybe not necessary?
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement