ImageBasedLighting.bslinc 8.4 KB

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