ImageBasedLighting.bslinc 7.5 KB

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