ImageBasedLighting.bslinc 7.3 KB

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