Advertisement
Geometrian

Untitled

Aug 23rd, 2014
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #version 440
  2. #extension GL_NV_shader_thread_group : require
  3.  
  4. //TODO: coherent for these instead?
  5. layout(r32ui) volatile uniform uimage2D img2D_0;
  6.  
  7. layout(rgba32f) volatile uniform image2D img2D_1;
  8. layout(rgba32f) volatile uniform image2D img2D_2;
  9. layout(rgba32f) volatile uniform image2D img2D_3;
  10. layout(rgba32f) volatile uniform image2D img2D_4;
  11.  
  12. layout(early_fragment_tests) in;
  13.  
  14. in vec4 _vec_vert_eye_vf;
  15. void main(void) {
  16.     ivec2 coord = ivec2(gl_FragCoord.xy);
  17.  
  18.     vec4 vec_vert_eye = _vec_vert_eye_vf;
  19.     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]
  20.  
  21.     bool needs_write = !gl_HelperThreadNV; //Only real fragments need to write; don't stall the thread warp waiting
  22.                                            //for threads in helper pixels to insert their depths (often they can't
  23.                                            //anyway, because their writes, including to mutexes, are undefined).
  24.     while (needs_write==true) {
  25.         bool can_write = imageAtomicExchange(img2D_0,coord,1u)==2u; //locked is 1, unlocked is 2
  26.         memoryBarrier(); //TODO: probably not necessary?
  27.  
  28.         //Loading data from data textures can happen whether or not we can write.
  29.         float depth0 = imageLoad(img2D_1,coord).b;
  30.         float depth1 = imageLoad(img2D_2,coord).b;
  31.         float depth2 = imageLoad(img2D_3,coord).b;
  32.         float depth3 = imageLoad(img2D_4,coord).b;
  33.         //Add the new fragment's depth ("depth_added") into the sorted list of depths
  34.         if (depth_added<depth0) {
  35.             depth3 = depth2;
  36.             depth2 = depth1;
  37.             depth1 = depth0;
  38.             depth0 = depth_added;
  39.         } else if (depth_added<depth1) {
  40.             depth3 = depth2;
  41.             depth2 = depth1;
  42.             depth1 = depth_added;
  43.         } else if (depth_added<depth2) {
  44.             depth3 = depth2;
  45.             depth2 = depth_added;
  46.         } else if (depth_added<depth3) {
  47.             depth3 = depth_added;
  48.         }
  49.        
  50.         //Write data textures, iff we are the thread that has the right to change them.
  51.         if (can_write) {
  52.             imageStore(img2D_1,coord,vec4(0.0,0.0,depth0,1.0));
  53.             imageStore(img2D_2,coord,vec4(0.0,0.0,depth1,1.0));
  54.             imageStore(img2D_3,coord,vec4(0.0,0.0,depth2,1.0));
  55.             imageStore(img2D_4,coord,vec4(0.0,0.0,depth3,1.0));
  56.         }
  57.         //Ensure the data textures will be visible to other threads both inside and outside
  58.         //  this warp once the mutex is unlocked below (no guarantees on ordering otherwise).
  59.         memoryBarrier();
  60.        
  61.         //Unlock the mutex.
  62.         if (can_write) {
  63.             imageAtomicExchange(img2D_0,coord,2u); //TODO: this can be imageStore, theoretically?
  64.             needs_write = false;
  65.         }
  66.         memoryBarrier(); //TODO: maybe not necessary?
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement