ImageReflections.glsl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // Contains resources and functions for image reflections
  6. #ifndef ANKI_SHADERS_IMAGE_REFLECTIONS_GLSL
  7. #define ANKI_SHADERS_IMAGE_REFLECTIONS_GLSL
  8. #pragma anki include "shaders/Clusterer.glsl"
  9. #define ACCURATE_RAYS 0
  10. // Representation of a reflection probe
  11. struct ReflectionProbe
  12. {
  13. // Position of the prove in view space. Radius of probe squared
  14. vec4 positionRadiusSq;
  15. // Slice in u_reflectionsTex vector.
  16. vec4 cubemapIndexPad3;
  17. };
  18. layout(std140, row_major, SS_BINDING(IMAGE_REFLECTIONS_SET,
  19. IMAGE_REFLECTIONS_FIRST_SS_BINDING)) readonly buffer _irs1
  20. {
  21. mat3 u_invViewRotation;
  22. vec4 u_nearClusterMagicPad2;
  23. ReflectionProbe u_reflectionProbes[];
  24. };
  25. layout(std430, row_major, SS_BINDING(IMAGE_REFLECTIONS_SET,
  26. IMAGE_REFLECTIONS_FIRST_SS_BINDING + 1)) readonly buffer _irs2
  27. {
  28. uint u_reflectionProbeIndices[];
  29. };
  30. layout(std430, row_major, SS_BINDING(IMAGE_REFLECTIONS_SET,
  31. IMAGE_REFLECTIONS_FIRST_SS_BINDING + 2)) readonly buffer _irs3
  32. {
  33. uvec2 u_reflectionClusters[];
  34. };
  35. layout(TEX_BINDING(IMAGE_REFLECTIONS_SET, IMAGE_REFLECTIONS_TEX_BINDING))
  36. uniform samplerCubeArray u_reflectionsTex;
  37. //==============================================================================
  38. // Compute the cubemap texture lookup vector given the reflection vector (r)
  39. // the radius squared of the probe (R2) and the frag pos in sphere space (f)
  40. vec3 computeCubemapVec(in vec3 r, in float R2, in vec3 f)
  41. {
  42. #if ACCURATE_RAYS
  43. // Compute the collision of the r to the inner part of the sphere
  44. // From now on we work on the sphere's space
  45. // Project the center of the sphere (it's zero now since we are in sphere
  46. // space) in ray "f,r"
  47. vec3 p = f - r * dot(f, r);
  48. // The collision to the sphere is point x where x = p + T * r
  49. // Because of the pythagorean theorem: R^2 = dot(p, p) + dot(T * r, T * r)
  50. // solving for T, T = R / |p|
  51. // then x becomes x = sqrt(R^2 - dot(p, p)) * r + p;
  52. float pp = dot(p, p);
  53. pp = min(pp, R2);
  54. float sq = sqrt(R2 - pp);
  55. vec3 x = p + sq * r;
  56. // Rotate UV to move it to world space
  57. vec3 uv = u_invViewRotation * x;
  58. return uv;
  59. #else
  60. return u_invViewRotation * r;
  61. #endif
  62. }
  63. //==============================================================================
  64. vec3 readReflection(in uint clusterIndex, in vec3 posVSpace,
  65. in vec3 r, in float lod)
  66. {
  67. vec3 color = vec3(0.0);
  68. // Check proxy
  69. uvec2 cluster = u_reflectionClusters[clusterIndex];
  70. uint indexOffset = cluster[0];
  71. uint indexCount = cluster[1];
  72. for(uint i = 0; i < indexCount; ++i)
  73. {
  74. uint probeIndex = u_reflectionProbeIndices[indexOffset++];
  75. ReflectionProbe probe = u_reflectionProbes[probeIndex];
  76. float R2 = probe.positionRadiusSq.w;
  77. vec3 center = probe.positionRadiusSq.xyz;
  78. // Get distance from the center of the probe
  79. vec3 f = posVSpace - center;
  80. // Cubemap UV in view space
  81. vec3 uv = computeCubemapVec(r, R2, f);
  82. // Read!
  83. float cubemapIndex = probe.cubemapIndexPad3.x;
  84. vec3 c = textureLod(u_reflectionsTex, vec4(uv, cubemapIndex), lod).rgb;
  85. // Combine (lerp) with previous color
  86. float d = dot(f, f);
  87. float factor = d / R2;
  88. factor = min(factor, 1.0);
  89. color = mix(c, color, factor);
  90. //Equivelent: color = c * (1.0 - factor) + color * factor;
  91. }
  92. return color;
  93. }
  94. //==============================================================================
  95. vec3 doImageReflections(in vec3 posVSpace, in vec3 r, in float lod)
  96. {
  97. uint clusterIdx = computeClusterIndexUsingFragCoord(
  98. u_nearClusterMagicPad2.x,
  99. u_nearClusterMagicPad2.y,
  100. posVSpace.z,
  101. TILE_COUNT_X,
  102. TILE_COUNT_Y);
  103. return readReflection(clusterIdx, posVSpace, r, lod);
  104. }
  105. #endif