gbuffer.glsl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #ifndef _GBUFFER_GLSL_
  2. #define _GBUFFER_GLSL_
  3. vec2 octahedronWrap(const vec2 v) {
  4. return (1.0 - abs(v.yx)) * (vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0));
  5. }
  6. vec3 getNor(const vec2 enc) {
  7. vec3 n;
  8. n.z = 1.0 - abs(enc.x) - abs(enc.y);
  9. n.xy = n.z >= 0.0 ? enc.xy : octahedronWrap(enc.xy);
  10. n = normalize(n);
  11. return n;
  12. }
  13. vec3 getPosView(const vec3 viewRay, const float depth, const vec2 cameraProj) {
  14. float linearDepth = cameraProj.y / (cameraProj.x - depth);
  15. // float linearDepth = cameraProj.y / ((depth * 0.5 + 0.5) - cameraProj.x);
  16. return viewRay * linearDepth;
  17. }
  18. vec3 getPos(const vec3 eye, const vec3 eyeLook, const vec3 viewRay, const float depth, const vec2 cameraProj) {
  19. // eyeLook, viewRay should be normalized
  20. float linearDepth = cameraProj.y / ((depth * 0.5 + 0.5) - cameraProj.x);
  21. float viewZDist = dot(eyeLook, viewRay);
  22. vec3 wposition = eye + viewRay * (linearDepth / viewZDist);
  23. return wposition;
  24. }
  25. vec3 getPosNoEye(const vec3 eyeLook, const vec3 viewRay, const float depth, const vec2 cameraProj) {
  26. // eyeLook, viewRay should be normalized
  27. float linearDepth = cameraProj.y / ((depth * 0.5 + 0.5) - cameraProj.x);
  28. float viewZDist = dot(eyeLook, viewRay);
  29. vec3 wposition = viewRay * (linearDepth / viewZDist);
  30. return wposition;
  31. }
  32. #ifdef HLSL
  33. vec3 getPos2(const mat4 invVP, const float depth, vec2 coord) {
  34. coord.y = 1.0 - coord.y;
  35. #else
  36. vec3 getPos2(const mat4 invVP, const float depth, const vec2 coord) {
  37. #endif
  38. vec4 pos = vec4(coord * 2.0 - 1.0, depth, 1.0);
  39. pos = invVP * pos;
  40. pos.xyz /= pos.w;
  41. return pos.xyz;
  42. }
  43. #ifdef HLSL
  44. vec3 getPosView2(const mat4 invP, const float depth, vec2 coord) {
  45. coord.y = 1.0 - coord.y;
  46. #else
  47. vec3 getPosView2(const mat4 invP, const float depth, const vec2 coord) {
  48. #endif
  49. vec4 pos = vec4(coord * 2.0 - 1.0, depth, 1.0);
  50. pos = invP * pos;
  51. pos.xyz /= pos.w;
  52. return pos.xyz;
  53. }
  54. #ifdef HLSL
  55. vec3 getPos2NoEye(const vec3 eye, const mat4 invVP, const float depth, vec2 coord) {
  56. coord.y = 1.0 - coord.y;
  57. #else
  58. vec3 getPos2NoEye(const vec3 eye, const mat4 invVP, const float depth, const vec2 coord) {
  59. #endif
  60. vec4 pos = vec4(coord * 2.0 - 1.0, depth, 1.0);
  61. pos = invVP * pos;
  62. pos.xyz /= pos.w;
  63. return pos.xyz - eye;
  64. }
  65. float packFloat2(const float f1, const float f2) {
  66. // Higher f1 = less precise f2
  67. return floor(f1 * 255.0) + min(f2, 1.0 - 1.0 / 100.0);
  68. }
  69. vec2 unpackFloat2(const float f) {
  70. return vec2(floor(f) / 255.0, fract(f));
  71. }
  72. vec4 encodeRGBM(const vec3 rgb) {
  73. const float maxRange = 6.0;
  74. float maxRGB = max(rgb.x, max(rgb.g, rgb.b));
  75. float m = maxRGB / maxRange;
  76. m = ceil(m * 255.0) / 255.0;
  77. return vec4(rgb / (m * maxRange), m);
  78. }
  79. vec3 decodeRGBM(const vec4 rgbm) {
  80. const float maxRange = 6.0;
  81. return rgbm.rgb * rgbm.a * maxRange;
  82. }
  83. uint encNor(vec3 n) {
  84. ivec3 nor = ivec3(n * 255.0f);
  85. uvec3 norSigns;
  86. norSigns.x = (nor.x >> 5) & 0x04000000;
  87. norSigns.y = (nor.y >> 14) & 0x00020000;
  88. norSigns.z = (nor.z >> 23) & 0x00000100;
  89. nor = abs(nor);
  90. uint val = norSigns.x | (nor.x << 18) | norSigns.y | (nor.y << 9) | norSigns.z | nor.z;
  91. return val;
  92. }
  93. vec3 decNor(uint val) {
  94. uvec3 nor;
  95. nor.x = (val >> 18) & 0x000000ff;
  96. nor.y = (val >> 9) & 0x000000ff;
  97. nor.z = val & 0x000000ff;
  98. uvec3 norSigns;
  99. norSigns.x = (val >> 25) & 0x00000002;
  100. norSigns.y = (val >> 16) & 0x00000002;
  101. norSigns.z = (val >> 7) & 0x00000002;
  102. norSigns = 1 - norSigns;
  103. vec3 normal = vec3(nor) / 255.0f;
  104. normal *= norSigns;
  105. return normal;
  106. }
  107. // GBuffer helper - Sebastien Lagarde
  108. // https://seblagarde.wordpress.com/2018/09/02/gbuffer-helper-packing-integer-and-float-together/
  109. float packFloatInt8(const float f, const uint i) {
  110. // Constant optimize by compiler
  111. const int numBitTarget = 8;
  112. const int numBitI = 4;
  113. const float prec = float(1 << numBitTarget);
  114. const float maxi = float(1 << numBitI);
  115. const float precMinusOne = prec - 1.0;
  116. const float t1 = ((prec / maxi) - 1.0) / precMinusOne;
  117. const float t2 = (prec / maxi) / precMinusOne;
  118. // Code
  119. return t1 * f + t2 * float(i);
  120. }
  121. float packFloatInt16(const float f, const uint i) {
  122. // Constant optimize by compiler
  123. const int numBitTarget = 16;
  124. const int numBitI = 4;
  125. const float prec = float(1 << numBitTarget);
  126. const float maxi = float(1 << numBitI);
  127. const float precMinusOne = prec - 1.0;
  128. const float t1 = ((prec / maxi) - 1.0) / precMinusOne;
  129. const float t2 = (prec / maxi) / precMinusOne;
  130. // Code
  131. return t1 * f + t2 * float(i);
  132. }
  133. void unpackFloatInt8(const float val, out float f, out uint i) {
  134. // Constant optimize by compiler
  135. const int numBitTarget = 8;
  136. const int numBitI = 4;
  137. const float prec = float(1 << numBitTarget);
  138. const float maxi = float(1 << numBitI);
  139. const float precMinusOne = prec - 1.0;
  140. const float t1 = ((prec / maxi) - 1.0) / precMinusOne;
  141. const float t2 = (prec / maxi) / precMinusOne;
  142. // Code
  143. // extract integer part
  144. // + rcp(precMinusOne) to deal with precision issue
  145. i = int((val / t2) + (1.0 / precMinusOne));
  146. // Now that we have i, solve formula in packFloatInt for f
  147. //f = (val - t2 * float(i)) / t1 => convert in mads form
  148. f = clamp((-t2 * float(i) + val) / t1, 0.0, 1.0); // Saturate in case of precision issue
  149. }
  150. void unpackFloatInt16(const float val, out float f, out uint i) {
  151. // Constant optimize by compiler
  152. const int numBitTarget = 16;
  153. const int numBitI = 4;
  154. const float prec = float(1 << numBitTarget);
  155. const float maxi = float(1 << numBitI);
  156. const float precMinusOne = prec - 1.0;
  157. const float t1 = ((prec / maxi) - 1.0) / precMinusOne;
  158. const float t2 = (prec / maxi) / precMinusOne;
  159. // Code
  160. // extract integer part
  161. // + rcp(precMinusOne) to deal with precision issue
  162. i = int((val / t2) + (1.0 / precMinusOne));
  163. // Now that we have i, solve formula in packFloatInt for f
  164. //f = (val - t2 * float(i)) / t1 => convert in mads form
  165. f = clamp((-t2 * float(i) + val) / t1, 0.0, 1.0); // Saturate in case of precision issue
  166. }
  167. #endif