ReflectionCubeImportanceSample.bsl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "$ENGINE$\PPBase.bslinc"
  2. #include "$ENGINE$\ReflectionCubemapCommon.bslinc"
  3. technique ReflectionCubeImportanceSample
  4. {
  5. mixin PPBase;
  6. mixin ReflectionCubemapCommon;
  7. code
  8. {
  9. #define PI 3.1415926
  10. float radicalInverse(uint bits)
  11. {
  12. // Reverse bits. Algorithm from Hacker's Delight.
  13. bits = (bits << 16u) | (bits >> 16u);
  14. bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
  15. bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
  16. bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
  17. bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
  18. // Normalizes unsigned int in range [0, 4294967295] to [0, 1]
  19. return float(bits) * 2.3283064365386963e-10;
  20. }
  21. float2 hammersleySequence(uint i, uint count)
  22. {
  23. float2 output;
  24. output.x = i / (float)count;
  25. output.y = radicalInverse(i);
  26. return output;
  27. }
  28. // Returns cos(theta) in x and phi in y
  29. float2 importanceSampleGGX(float2 e, float roughness4)
  30. {
  31. // See GGXImportanceSample.nb for derivation (essentially, take base GGX, normalize it,
  32. // generate PDF, split PDF into marginal probability for theta and conditional probability
  33. // for phi. Plug those into the CDF, invert it.)
  34. float cosTheta = sqrt((1.0f - e.x) / (1.0f + (roughness4 - 1.0f) * e.x));
  35. float phi = 2.0f * PI * e.y;
  36. return float2(cosTheta, phi);
  37. }
  38. float3 sphericalToCartesian(float cosTheta, float sinTheta, float phi)
  39. {
  40. float3 output;
  41. output.x = sinTheta * cos(phi);
  42. output.y = sinTheta * sin(phi);
  43. output.z = cosTheta;
  44. return output;
  45. }
  46. float pdfGGX(float cosTheta, float sinTheta, float roughness4)
  47. {
  48. float d = (cosTheta*roughness4 - cosTheta) * cosTheta + 1;
  49. return roughness4 * cosTheta * sinTheta / (d*d*PI);
  50. }
  51. cbuffer Input
  52. {
  53. int gCubeFace;
  54. int gMipLevel;
  55. int gNumMips;
  56. float gPrecomputedMipFactor;
  57. }
  58. SamplerState gInputSamp;
  59. TextureCube gInputTex;
  60. float4 fsmain(VStoFS input) : SV_Target0
  61. {
  62. float2 scaledUV = input.uv0 * 2.0f - 1.0f;
  63. float3 N = getDirFromCubeFace(gCubeFace, scaledUV);
  64. N = normalize(N);
  65. // Determine which mip level to sample from depending on PDF and cube -> sphere mapping distortion
  66. float distortion = rcp(pow(N.x * N.x + N.y * N.y + N.z * N.z, 3.0f/2.0f));
  67. float roughness = mapMipLevelToRoughness(gMipLevel, gNumMips);
  68. float roughness2 = roughness * roughness;
  69. float roughness4 = roughness2 * roughness2;
  70. float4 sum = 0;
  71. for(uint i = 0; i < NUM_SAMPLES; i++)
  72. {
  73. float2 random = hammersleySequence(i, NUM_SAMPLES);
  74. float2 sphericalH = importanceSampleGGX(random, roughness4);
  75. float cosTheta = sphericalH.x;
  76. float phi = sphericalH.y;
  77. float sinTheta = sqrt(1.0f - cosTheta * cosTheta);
  78. float3 H = sphericalToCartesian(cosTheta, sinTheta, phi);
  79. float PDF = pdfGGX(cosTheta, sinTheta, roughness4);
  80. // Transform H to world space
  81. float3 up = abs(H.z) < 0.999 ? float3(0, 0, 1) : float3(1, 0, 0);
  82. float3 tangentX = normalize(cross(up, N));
  83. float3 tangentY = cross(N, tangentX);
  84. H = tangentX * H.x + tangentY * H.y + N * H.z;
  85. // Calculating mip level from distortion and pdf and described by http://http.developer.nvidia.com/GPUGems3/gpugems3_ch20.html
  86. int mipLevel = max(gPrecomputedMipFactor - 0.5f * log2(PDF * distortion), 0);
  87. // Note: Adding +1 bias as it looks better
  88. mipLevel++;
  89. // We need a light direction to properly evaluate the NoL term of the evaluation integral
  90. // Li(u) * brdf(u, v) * (u.n) / pdf(u, v)
  91. // which we don't have, so we assume a viewing direction is equal to normal and calculate lighting dir from it and half-vector
  92. float3 L = 2 * dot(N, H) * H - N;
  93. float NoL = saturate(dot(N, L));
  94. // sum += radiance * GGX(h, roughness) * NoL / PDF. In GGX/PDF most factors cancel out and we're left with 1/cos (sine factor of the PDF only needed for the integral (I think), so we don't include it)
  95. if(NoL > 0)
  96. sum += gInputTex.SampleLevel(gInputSamp, H, mipLevel) * NoL / cosTheta;
  97. }
  98. return sum / NUM_SAMPLES;
  99. }
  100. };
  101. };