Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Common.hlsl 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef _SAMPLING_SAMPLING_COMMON_HLSL_
  2. #define _SAMPLING_SAMPLING_COMMON_HLSL_
  3. #ifndef PI
  4. #define PI 3.141592653589f
  5. #endif
  6. // Paper: Building an Orthonormal Basis, Revisited.
  7. // Tom Duff, James Burgess, Per Christensen, Christophe Hery, Andrew Kensler, Max Liani, and Ryusuke Villemin (Pixar).
  8. // https://graphics.pixar.com/library/OrthonormalB/paper.pdf
  9. void OrthoBasisFromVector(float3 n, out float3 b1, out float3 b2)
  10. {
  11. float sign = n.z >= 0.0f ? 1.0f : -1.0f;
  12. const float a = -1.0f / (sign + n.z);
  13. const float b = n.x * n.y * a;
  14. b1 = float3(1.0f + sign * n.x * n.x * a, sign * b, -sign * n.x);
  15. b2 = float3(b, sign + n.y * n.y * a, -n.y);
  16. }
  17. float3x3 OrthoBasisFromVector(float3 n)
  18. {
  19. float3 t, b;
  20. OrthoBasisFromVector(n, t, b);
  21. return transpose(float3x3(t, b, n));
  22. }
  23. void SampleDiffuseBrdf(float2 u, float3 shadingNormal, out float3 wi)
  24. {
  25. wi = float3(0, 0, 0);
  26. float a = sqrt(u.x);
  27. float b = 2.0 * PI * u.y;
  28. float3 localWi = float3(a * cos(b), a * sin(b), sqrt(1.0f - u.x));
  29. float3x3 TBN = OrthoBasisFromVector(shadingNormal);
  30. wi = mul(TBN, localWi);
  31. }
  32. bool SampleDiffuseBrdf(float2 u, float3 geometryNormal, float3 shadingNormal, float3 wo, out float3 wi, out float pdf)
  33. {
  34. pdf = 0.0f;
  35. wi = 0.0f;
  36. bool valid = true;
  37. if (dot(geometryNormal, wo) <= 0.0f)
  38. valid = false;
  39. else
  40. {
  41. float a = sqrt(1.0f - u.x);
  42. float b = 2.0 * PI * u.y;
  43. float3 localWi = float3(a * cos(b), a * sin(b), sqrt(u.x));
  44. float3x3 TBN = OrthoBasisFromVector(shadingNormal);
  45. wi = mul(TBN, localWi);
  46. pdf = localWi.z / PI;
  47. }
  48. return valid;
  49. }
  50. float3 MapSquareToSphere(float2 unitSquareCoords)
  51. {
  52. float theta = (2.0f * PI) * unitSquareCoords.x;
  53. float sinTheta, cosTheta;
  54. sincos(theta, sinTheta, cosTheta);
  55. float cosPhi = 1.0f - 2.0f * unitSquareCoords.y;
  56. float sinPhi = sqrt(max(0.0f, 1.0f - cosPhi * cosPhi));
  57. return float3(sinPhi * cosTheta, sinPhi * sinTheta, cosPhi);
  58. }
  59. // An optimized version of the area-preserving square-to-disk map presented in "A Low Distortion Map Between Disk and Square". Copied from from http://psgraphics.blogspot.com/2011/01/improved-code-for-concentric-map.html.
  60. float2 MapSquareToDisk(float2 rnd)
  61. {
  62. //Code flow makes sure that division by 0 and thus NaNs cannot happen.
  63. float phi;
  64. float r;
  65. float a = rnd.x * 2.0f - 1.0f;
  66. float b = rnd.y * 2.0f - 1.0f;
  67. if (a * a > b * b)
  68. {
  69. r = a;
  70. phi = (PI * 0.25f) * (b / a);
  71. }
  72. else
  73. {
  74. r = b;
  75. if (b == 0.0f)
  76. phi = PI * 0.5f;
  77. else
  78. phi = (PI * 0.5f) - (PI * 0.25f) * (a / b);
  79. }
  80. return float2(r * cos(phi), r * sin(phi));
  81. }
  82. float3 CosineSample(float2 u, float3 normal)
  83. {
  84. float a = sqrt(u.x);
  85. float b = 2.0f * PI * u.y;
  86. float3 localDir = float3(a * cos(b), a * sin(b), sqrt(1.0f - u.x));
  87. float3x3 basis = OrthoBasisFromVector(normal);
  88. return mul(basis, localDir);
  89. }
  90. float PowerHeuristic(float f, float b)
  91. {
  92. float q = (f * f) + (b * b);
  93. return q > 0 ? (f * f) / q : 0;
  94. }
  95. float UintToFloat01(uint x)
  96. {
  97. return x * 2.3283064365386963e-10; // (1.f / (1ULL << 32));
  98. }
  99. int Log2Int(uint v)
  100. {
  101. return firstbithigh(v);
  102. }
  103. int Log2IntUp(uint v)
  104. {
  105. int n = Log2Int(v);
  106. return v > (1u << n) ? n + 1 : n;
  107. }
  108. #endif