ImageBasedLighting.bslinc 8.1 KB

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