ReflectionCubemapSampling.bslinc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. Technique : base("ReflectionCubemapSampling") =
  2. {
  3. Language = "HLSL11";
  4. Pass =
  5. {
  6. Common =
  7. {
  8. // Arbitrary limit, increase if needed
  9. #define MAX_PROBES 512
  10. struct ReflProbeData
  11. {
  12. float3 position;
  13. float radius;
  14. float3 boxExtents;
  15. float4x4 invBoxTransform;
  16. float transitionDistance;
  17. uint cubemapIdx;
  18. uint type; // 0 - Sphere, 1 - Box
  19. };
  20. TextureCube gSkyReflectionTex;
  21. SamplerState gSkyReflectionSamp;
  22. TextureCube gSkyIrradianceTex;
  23. SamplerState gSkyIrradianceSamp;
  24. TextureCubeArray gReflProbeCubemaps;
  25. SamplerState gReflProbeSamp;
  26. Texture2D gPreintegratedEnvBRDF;
  27. SamplerState gPreintegratedEnvBRDFSamp;
  28. StructuredBuffer<ReflProbeData> gReflectionProbes;
  29. #ifdef USE_COMPUTE_INDICES
  30. groupshared uint gReflectionProbeIndices[MAX_PROBES];
  31. #else
  32. Buffer<uint> gReflectionProbeIndices;
  33. #endif
  34. cbuffer ReflProbeParams
  35. {
  36. uint gReflCubemapNumMips;
  37. uint gNumProbes;
  38. uint gSkyCubemapAvailable;
  39. uint gSkyCubemapNumMips;
  40. float gSkyBrightness;
  41. }
  42. float3 getSkyIndirectDiffuse(float3 dir)
  43. {
  44. return gSkyIrradianceTex.SampleLevel(gSkyIrradianceSamp, dir, 0).rgb * gSkyBrightness;
  45. }
  46. float3 getSphereReflectionContribution(float normalizedDistance)
  47. {
  48. // If closer than 60% to the probe radius, then full contribution is used.
  49. // For the other 40% we smoothstep and return contribution lower than 1 so other
  50. // reflection probes can be blended.
  51. // smoothstep from 1 to 0.6:
  52. // float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  53. // return t * t * (3.0 - 2.0 * t);
  54. float t = saturate(2.5 - 2.5 * normalizedDistance);
  55. return t * t * (3.0 - 2.0 * t);
  56. }
  57. float3 getLookupForSphereProxy(float3 originWS, float3 dirWS, float3 centerWS, float radius)
  58. {
  59. float radius2 = radius * radius;
  60. float3 originLS = originWS - centerWS;
  61. float a = dot(originLS, dirWS);
  62. float dist2 = a * a - dot(originLS, originLS) + radius2;
  63. float3 lookupDir = dirWS;
  64. [flatten]
  65. if(dist2 >= 0)
  66. {
  67. float farDist = sqrt(dist2) - a;
  68. lookupDir = originLS + farDist * dirWS;
  69. }
  70. return lookupDir;
  71. }
  72. float3 getDistBoxToPoint(float3 pt, float3 extents)
  73. {
  74. float3 d = max(max(-extents - pt, 0), pt - extents);
  75. return length(d);
  76. }
  77. float3 getLookupForBoxProxy(float3 originWS, float3 dirWS, float3 centerWS, float3 extents, float4x4 invBoxTransform, float transitionDistance, out float contribution)
  78. {
  79. // Transform origin and direction into box local space, where it is united sized and axis aligned
  80. float3 originLS = mul(invBoxTransform, float4(originWS, 1)).xyz;
  81. float3 dirLS = mul(invBoxTransform, float4(dirWS, 0)).xyz;
  82. // Get distance from 3 min planes and 3 max planes of the unit AABB
  83. // float3 unitVec = float3(1.0f, 1.0f, 1.0f);
  84. // float3 intersectsMax = (unitVec - originLS) / dirLS;
  85. // float3 intersectsMin = (-unitVec - originLS) / dirLS;
  86. float3 invDirLS = rcp(dirLS);
  87. float3 intersectsMax = invDirLS - originLS * invDirLS;
  88. float3 intersectsMin = -invDirLS - originLS * invDirLS;
  89. // Find nearest positive (along ray direction) intersection
  90. float3 positiveIntersections = max(intersectsMax, intersectsMin);
  91. float intersectDist = min(positiveIntersections.x, min(positiveIntersections.y, positiveIntersections.z));
  92. float3 intersectPositionWS = originWS + intersectDist * dirWS;
  93. float3 lookupDir = intersectPositionWS - centerWS;
  94. // Calculate contribution
  95. //// Shrink the box so fade out happens within box extents
  96. float3 reducedExtents = extents - float3(transitionDistance, transitionDistance, transitionDistance);
  97. float distToBox = getDistBoxToPoint(originLS * reducedExtents, reducedExtents);
  98. float normalizedDistance = distToBox / transitionDistance;
  99. // If closer than 70% to the probe radius, then full contribution is used.
  100. // For the other 30% we smoothstep and return contribution lower than 1 so other
  101. // reflection probes can be blended.
  102. // smoothstep from 1 to 0.7:
  103. // float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  104. // return t * t * (3.0 - 2.0 * t);
  105. float t = saturate(3.3333 - 3.3333 * normalizedDistance);
  106. return t * t * (3.0 - 2.0 * t);
  107. return lookupDir;
  108. }
  109. float3 gatherReflectionRadiance(float3 worldPos, float3 dir, float roughness, float3 specularColor, uint probeOffset, uint numProbes)
  110. {
  111. #if CAPTURING_REFLECTIONS
  112. return specularColor;
  113. #else
  114. float mipLevel = mapRoughnessToMipLevel(roughness, gReflCubemapNumMips);
  115. float3 output = 0;
  116. float leftoverContribution = 1.0f;
  117. for(uint i = 0; i < numProbes; i++)
  118. {
  119. if(leftoverContribution < 0.001f)
  120. break;
  121. uint probeIdx = gReflectionProbeIndices[probeOffset + i];
  122. ReflProbeData probeData = gReflectionProbes[probeIdx];
  123. float3 probeToPos = worldPos - probeData.position;
  124. float distToProbe = length(probeToPos);
  125. float normalizedDist = saturate(distToProbe / probeData.radius);
  126. if(distToProbe <= probeData.radius)
  127. {
  128. float3 correctedDir;
  129. float contribution = 0;
  130. if(probeData.type == 0) // Sphere
  131. {
  132. correctedDir = getLookupForSphereProxy(worldPos, dir, probeData.position, probeData.radius);
  133. contribution = getSphereReflectionContribution(normalizedDist);
  134. }
  135. else if(probeData.type == 1) // Box
  136. {
  137. correctedDir = getLookupForBoxProxy(worldPos, dir, probeData.position, probeData.boxExtents, probeData.invBoxTransform, probeData.transitionDistance, contribution);
  138. }
  139. float4 sample = gReflProbeCubemaps.SampleLevel(gReflProbeSamp, float4(correctedDir, probeData.cubemapIdx), mipLevel);
  140. sample *= contribution;
  141. output += sample.rgb * leftoverContribution;
  142. leftoverContribution *= (1.0f - contribution);
  143. }
  144. }
  145. if(gSkyCubemapAvailable > 0)
  146. {
  147. float skyMipLevel = mapRoughnessToMipLevel(roughness, gSkyCubemapNumMips);
  148. float4 sample = gSkyReflectionTex.SampleLevel(gSkyReflectionSamp, dir, skyMipLevel) * gSkyBrightness;
  149. output += sample.rgb * leftoverContribution;
  150. }
  151. return output;
  152. #endif
  153. }
  154. float3 getImageBasedSpecular(float3 worldPos, float3 V, float3 R, SurfaceData surfaceData)
  155. {
  156. // See C++ code for generation of gPreintegratedEnvBRDF to see why this code works as is
  157. float3 N = surfaceData.worldNormal.xyz;
  158. float NoV = saturate(dot(N, V));
  159. // Note: Using a fixed F0 value of 0.04 (plastic) for dielectrics, and using albedo as specular for conductors.
  160. // For more customizability allow the user to provide separate albedo/specular colors for both types.
  161. float3 specularColor = lerp(float3(0.04f, 0.04f, 0.04f), surfaceData.albedo.rgb, surfaceData.metalness);
  162. float3 radiance = gatherReflectionRadiance(worldPos, R, surfaceData.roughness, specularColor, 0, 0);
  163. float2 envBRDF = gPreintegratedEnvBRDF.SampleLevel(gPreintegratedEnvBRDFSamp, float2(NoV, surfaceData.roughness), 0).rg;
  164. return radiance * (specularColor * envBRDF.x + envBRDF.y);
  165. }
  166. };
  167. };
  168. };
  169. Technique : base("ReflectionCubemapSampling") =
  170. {
  171. Language = "GLSL";
  172. Pass =
  173. {
  174. Common =
  175. {
  176. };
  177. };
  178. };