ImageBasedLighting.bslinc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "$ENGINE$\ReflectionCubemapCommon.bslinc"
  2. mixin ImageBasedLighting
  3. {
  4. mixin ReflectionCubemapCommon;
  5. code
  6. {
  7. // Note: Size must be multiple of largest element, because of std430 rules
  8. struct ReflProbeData
  9. {
  10. float3 position;
  11. float radius;
  12. float3 boxExtents;
  13. float transitionDistance;
  14. float4x4 invBoxTransform;
  15. uint cubemapIdx;
  16. uint type; // 0 - Sphere, 1 - Box
  17. float2 padding;
  18. };
  19. [internal]
  20. TextureCube gSkyReflectionTex;
  21. SamplerState gSkyReflectionSamp;
  22. [internal]
  23. TextureCubeArray gReflProbeCubemaps;
  24. SamplerState gReflProbeSamp;
  25. [internal]
  26. Texture2D gAmbientOcclusionTex;
  27. [internal] [alias(gAmbientOcclusionTex)]
  28. SamplerState gAmbientOcclusionSamp
  29. {
  30. Filter = MIN_MAG_MIP_POINT;
  31. AddressU = CLAMP;
  32. AddressV = CLAMP;
  33. };
  34. [internal]
  35. Texture2D gSSRTex;
  36. [internal] [alias(gSSRTex)]
  37. SamplerState gSSRSamp
  38. {
  39. Filter = MIN_MAG_MIP_POINT;
  40. AddressU = CLAMP;
  41. AddressV = CLAMP;
  42. };
  43. [internal]
  44. Texture2D gPreintegratedEnvBRDF;
  45. [internal] [alias(gPreintegratedEnvBRDF)]
  46. SamplerState gPreintegratedEnvBRDFSamp
  47. {
  48. Filter = MIN_MAG_MIP_LINEAR;
  49. AddressU = CLAMP;
  50. AddressV = CLAMP;
  51. };
  52. [internal]
  53. cbuffer ReflProbeParams
  54. {
  55. uint gReflCubemapNumMips;
  56. uint gNumProbes;
  57. uint gSkyCubemapAvailable;
  58. uint gUseReflectionMaps;
  59. uint gSkyCubemapNumMips;
  60. float gSkyBrightness;
  61. }
  62. float getSphereReflectionContribution(float normalizedDistance)
  63. {
  64. // If closer than 60% to the probe radius, then full contribution is used.
  65. // For the other 40% we smoothstep and return contribution lower than 1 so other
  66. // reflection probes can be blended.
  67. // smoothstep from 1 to 0.6:
  68. // float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  69. // return t * t * (3.0 - 2.0 * t);
  70. float t = saturate(2.5 - 2.5 * normalizedDistance);
  71. return t * t * (3.0 - 2.0 * t);
  72. }
  73. float3 getLookupForSphereProxy(float3 originWS, float3 dirWS, float3 centerWS, float radius)
  74. {
  75. float radius2 = radius * radius;
  76. float3 originLS = originWS - centerWS;
  77. float a = dot(originLS, dirWS);
  78. float dist2 = a * a - dot(originLS, originLS) + radius2;
  79. float3 lookupDir = dirWS;
  80. [flatten]
  81. if(dist2 >= 0)
  82. {
  83. float farDist = sqrt(dist2) - a;
  84. lookupDir = originLS + farDist * dirWS;
  85. }
  86. return lookupDir;
  87. }
  88. float getDistBoxToPoint(float3 pt, float3 extents)
  89. {
  90. float3 d = max(max(-extents - pt, 0), pt - extents);
  91. return length(d);
  92. }
  93. float3 getLookupForBoxProxy(float3 originWS, float3 dirWS, float3 centerWS, float3 extents, float4x4 invBoxTransform, float transitionDistance, out float contribution)
  94. {
  95. // Transform origin and direction into box local space, where it is unit sized and axis aligned
  96. float3 originLS = mul(invBoxTransform, float4(originWS, 1)).xyz;
  97. float3 dirLS = mul(invBoxTransform, float4(dirWS, 0)).xyz;
  98. // Get distance from 3 min planes and 3 max planes of the unit AABB
  99. // float3 unitVec = float3(1.0f, 1.0f, 1.0f);
  100. // float3 intersectsMax = (unitVec - originLS) / dirLS;
  101. // float3 intersectsMin = (-unitVec - originLS) / dirLS;
  102. float3 invDirLS = rcp(dirLS);
  103. float3 intersectsMax = invDirLS - originLS * invDirLS;
  104. float3 intersectsMin = -invDirLS - originLS * invDirLS;
  105. // Find nearest positive (along ray direction) intersection
  106. float3 positiveIntersections = max(intersectsMax, intersectsMin);
  107. float intersectDist = min(positiveIntersections.x, min(positiveIntersections.y, positiveIntersections.z));
  108. float3 intersectPositionWS = originWS + intersectDist * dirWS;
  109. float3 lookupDir = intersectPositionWS - centerWS;
  110. // Calculate contribution
  111. //// Shrink the box so fade out happens within box extents
  112. float3 reducedExtents = extents - float3(transitionDistance, transitionDistance, transitionDistance);
  113. float distToBox = getDistBoxToPoint(originLS * extents, reducedExtents);
  114. float normalizedDistance = distToBox / transitionDistance;
  115. // If closer than 70% to the probe radius, then full contribution is used.
  116. // For the other 30% we smoothstep and return contribution lower than 1 so other
  117. // reflection probes can be blended.
  118. // smoothstep from 1 to 0.7:
  119. // float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  120. // return t * t * (3.0 - 2.0 * t);
  121. float t = saturate(3.3333 - 3.3333 * normalizedDistance);
  122. contribution = t * t * (3.0 - 2.0 * t);
  123. return lookupDir;
  124. }
  125. // rgb - probe color, a - probe contribution
  126. float4 evaluateProbe(float3 worldPos, float3 dir, float mipLevel, ReflProbeData probeData)
  127. {
  128. float3 probeToPos = worldPos - probeData.position;
  129. float distToProbe = length(probeToPos);
  130. float normalizedDist = saturate(distToProbe / probeData.radius);
  131. if(distToProbe <= probeData.radius)
  132. {
  133. float3 correctedDir;
  134. float contribution = 0;
  135. if(probeData.type == 0) // Sphere
  136. {
  137. correctedDir = getLookupForSphereProxy(worldPos, dir, probeData.position, probeData.radius);
  138. contribution = getSphereReflectionContribution(normalizedDist);
  139. }
  140. else if(probeData.type == 1) // Box
  141. {
  142. correctedDir = getLookupForBoxProxy(worldPos, dir, probeData.position, probeData.boxExtents, probeData.invBoxTransform, probeData.transitionDistance, contribution);
  143. }
  144. float4 probeSample = gReflProbeCubemaps.SampleLevel(gReflProbeSamp, float4(correctedDir, probeData.cubemapIdx), mipLevel);
  145. probeSample *= contribution;
  146. return float4(probeSample.rgb, (1.0f - contribution));
  147. }
  148. return float4(0, 0, 0, 1.0f);
  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. };
  156. };
  157. // Hackish way of "instantiating" two versions of a mixin (to be removed when template/specialization support is added)
  158. #include "$ENGINE$\ReflProbeAccumulator.bslinc"
  159. #define USE_UNIFORM_BUFFER
  160. #include "$ENGINE$\ReflProbeAccumulator.bslinc"
  161. #undef USE_UNIFORM_BUFFER