ReflectionCubeImportanceSample.bsl 4.3 KB

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