Guest User

Logarithmic depth buffer

a guest
Mar 28th, 2014
168
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #extension GL_ARB_conservative_depth : enable
  2.  
  3. float Fcoef = 2.0 / log2(4194304.0 + 1.0);
  4.  
  5. #if CRUS_VERTEX_SHADER
  6.  
  7. layout(location = nVERTEX) in vec3 inVertex;
  8.  
  9. layout(binding = nTRANSFORM, row_major) uniform TRSM {
  10.     mat4 ProjMatrix;
  11.     mat4 ViewMatrix;
  12.  
  13.     mat4 ViewProj;
  14.     mat4 ModelView;
  15. };
  16.  
  17. out float logz;
  18.  
  19. void main()
  20. {
  21.     gl_Position = ViewProj * vec4(inVertex, 1.0);
  22.  
  23.     logz = 1.0 + gl_Position.w;
  24.     gl_Position.z = log2(max(1e-6, logz)) * Fcoef - 1.0;
  25. }
  26.  
  27. #elif CRUS_FRAGMENT_SHADER
  28.  
  29. layout(binding = nMATERIAL, shared) uniform CMTS {
  30.     vec4 Clr0;
  31. };
  32.  
  33. layout(location = nFRAG_COLOR, index = 0) out vec4 FragColor;
  34. layout(depth_less) out float gl_FragDepth;
  35.  
  36. in float logz;
  37.  
  38. void main()
  39. {
  40.     FragColor = Clr0;
  41.  
  42.     gl_FragDepth = log2(logz) * 0.5 * Fcoef;
  43. }
  44.  
  45. #endif
RAW Paste Data