ReflectionCubeImportanceSample.bsl 4.8 KB

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