LightingCommon.bslinc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #include "$ENGINE$\SurfaceData.bslinc"
  2. mixin LightingCommon
  3. {
  4. mixin SurfaceData;
  5. code
  6. {
  7. #define PI 3.1415926
  8. #define HALF_PI 1.5707963
  9. // Note: Size must be multiple of largest element, because of std430 rules
  10. struct LightData
  11. {
  12. float3 position;
  13. float attRadius;
  14. float3 direction;
  15. float luminance;
  16. float3 spotAngles;
  17. float attRadiusSqrdInv;
  18. float3 color;
  19. float srcRadius;
  20. float3 shiftedLightPosition;
  21. float padding;
  22. };
  23. float3 calcMicrofacetFresnelShlick(float3 F0, float LoH)
  24. {
  25. return F0 + (1.0f - F0) * pow(1.0f - LoH, 5.0f);
  26. }
  27. float calcMicrofacetShadowingSmithGGX(float roughness4, float NoV, float NoL)
  28. {
  29. // Note: It's probably better to use the joint shadowing + masking version of this function
  30. // Note: Original GGX G1 multiplied by NoV & NoL (respectively), so that the microfacet function divisor gets canceled out
  31. // Original formula being (ignoring the factor for masking negative directions):
  32. // G1(v) = 2 / (1 + sqrt(1 + roughness^4 * tan^2(v)))
  33. //
  34. // Using trig identities: tan = sin/cos & sin^2 + cos^2 = 1
  35. // G1(v) = 2 / (1 + sqrt(1 + roughness^4 * (1 - cos^2(v))/cos^2(v)))
  36. //
  37. // Multiply by cos(v) so that we cancel out the (NoL * NoV) factor in the microfacet formula divisor
  38. // G1(v) = 2 * cos(v) / (cos^2(v) + sqrt(cos^2 + roughness^4 - roughness^4 * cos^2(v)))
  39. //
  40. // Actually do the cancellation:
  41. // G1(v) = 2 / (cos^2(v) + sqrt(cos^2 + roughness^4 - roughness^4 * cos^2(v)))
  42. //
  43. // Also cancel out the 2 and the 4:
  44. // G1(v) = 1 / (cos^2(v) + sqrt(cos^2 + roughness^4 - roughness^4 * cos^2(v)))
  45. //
  46. // Final equation being:
  47. // G(v, l) = G1(v) * G1(l)
  48. //
  49. // Where cos(v) is NoV or NoL
  50. float g1V = NoV + sqrt(NoV * (NoV - NoV * roughness4) + roughness4);
  51. float g1L = NoL + sqrt(NoL * (NoL - NoL * roughness4) + roughness4);
  52. return rcp(g1V * g1L);
  53. }
  54. float calcMicrofacetDistGGX(float roughness4, float NoH)
  55. {
  56. float d = (NoH * roughness4 - NoH) * NoH + 1.0f;
  57. return roughness4 / (PI * d * d);
  58. }
  59. float3 calcDiffuseLambert(float3 color)
  60. {
  61. return color * (1.0f / PI);
  62. }
  63. float getSpotAttenuation(float3 toLight, LightData lightData)
  64. {
  65. float output = saturate((dot(toLight, -lightData.direction) - lightData.spotAngles.y) * lightData.spotAngles.z);
  66. return output * output;
  67. }
  68. // Window function to ensure the light contribution fades out to 0 at attenuation radius
  69. float getRadialAttenuation(float distance2, LightData lightData)
  70. {
  71. float radialAttenuation = distance2 * lightData.attRadiusSqrdInv;
  72. radialAttenuation *= radialAttenuation;
  73. radialAttenuation = saturate(1.0f - radialAttenuation);
  74. radialAttenuation *= radialAttenuation;
  75. return radialAttenuation;
  76. }
  77. // Calculates illuminance from a non-area point light
  78. float illuminancePointLight(float distance2, float NoL, LightData lightData)
  79. {
  80. return (lightData.luminance * NoL) / max(distance2, 0.01f*0.01f);
  81. }
  82. // Calculates illuminance scale for a sphere or a disc area light, while also handling the case when
  83. // parts of the area light are below the horizon.
  84. // Input NoL must be unclamped.
  85. // Sphere solid angle = arcsin(r / d)
  86. // Right disc solid angle = atan(r / d)
  87. // - To compensate for oriented discs, multiply by dot(diskNormal, -L)
  88. float illuminanceScaleSphereDiskAreaLight(float unclampedNoL, float sinSolidAngleSqrd)
  89. {
  90. // Handles parts of the area light below the surface horizon
  91. // See https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf for reference
  92. float sinSolidAngle = sqrt(sinSolidAngleSqrd);
  93. // TODO - Below horizon handling disabled as it currently outputs incorrect values, need to find a better approximation or just use the reference implementation
  94. //if(unclampedNoL < sinSolidAngle)
  95. //{
  96. // // Hermite spline approximation (see reference for exact formula)
  97. // unclampedNoL = max(unclampedNoL, -sinSolidAngle);
  98. // return ((sinSolidAngle + unclampedNoL) * (sinSolidAngle + unclampedNoL)) / (4 * sinSolidAngle);
  99. //}
  100. //else
  101. return PI * sinSolidAngleSqrd * saturate(unclampedNoL);
  102. }
  103. // Calculates illuminance from a sphere area light.
  104. float illuminanceSphereAreaLight(float unclampedNoL, float distToLight2, LightData lightData)
  105. {
  106. float radius2 = lightData.srcRadius * lightData.srcRadius;
  107. // Squared sine of the sphere solid angle
  108. float sinSolidAngle2 = radius2 / distToLight2;
  109. // Prevent divide by zero
  110. sinSolidAngle2 = min(sinSolidAngle2, 0.9999f);
  111. return lightData.luminance * illuminanceScaleSphereDiskAreaLight(unclampedNoL, sinSolidAngle2);
  112. }
  113. // Calculates illuminance from a disc area light.
  114. float illuminanceDiscAreaLight(float unclampedNoL, float distToLight2, float3 L, LightData lightData)
  115. {
  116. // Solid angle for right disk = atan (r / d)
  117. // atan (r / d) = asin((r / d)/sqrt((r / d)^2+1))
  118. // sinAngle = (r / d)/sqrt((r / d)^2 + 1)
  119. // sinAngle^2 = (r / d)^2 / (r / d)^2 + 1
  120. // = r^2 / (d^2 + r^2)
  121. float radius2 = lightData.srcRadius * lightData.srcRadius;
  122. // max() to prevent light penetrating object
  123. float sinSolidAngle2 = saturate(radius2 / (radius2 + max(radius2, distToLight2)));
  124. // Multiply by extra term to somewhat handle the case of the oriented disc (formula above only works
  125. // for right discs).
  126. return lightData.luminance * illuminanceScaleSphereDiskAreaLight(unclampedNoL, sinSolidAngle2 * saturate(dot(lightData.direction, -L)));
  127. }
  128. // With microfacet BRDF the BRDF lobe is not centered around the reflected (mirror) direction.
  129. // Because of NoL and shadow-masking terms the lobe gets shifted toward the normal as roughness
  130. // increases. This is called the "off-specular peak". We approximate it using this function.
  131. float3 getSpecularDominantDir(float3 N, float3 R, float roughness)
  132. {
  133. // Note: Try this formula as well:
  134. // float smoothness = 1 - roughness;
  135. // return lerp(N, R, smoothness * (sqrt(smoothness) + roughness));
  136. float r2 = roughness * roughness;
  137. return normalize(lerp(N, R, (1 - r2) * (sqrt(1 - r2) + r2)));
  138. }
  139. float3 getSurfaceShading(float3 V, float3 L, float specLobeEnergy, SurfaceData surfaceData)
  140. {
  141. float3 N = surfaceData.worldNormal.xyz;
  142. float3 H = normalize(V + L);
  143. float LoH = saturate(dot(L, H));
  144. float NoH = saturate(dot(N, H));
  145. float NoV = saturate(dot(N, V));
  146. float NoL = saturate(dot(N, L));
  147. float3 diffuseColor = lerp(surfaceData.albedo.rgb, float3(0.0f, 0.0f, 0.0f), surfaceData.metalness);
  148. // Note: Using a fixed F0 value of 0.04 (plastic) for dielectrics, and using albedo as specular for conductors.
  149. // For more customizability allow the user to provide separate albedo/specular colors for both types.
  150. float3 specularColor = lerp(float3(0.04f, 0.04f, 0.04f), surfaceData.albedo.rgb, surfaceData.metalness);
  151. float3 diffuse = calcDiffuseLambert(diffuseColor);
  152. float roughness = max(surfaceData.roughness, 0.04f); // Prevent NaNs
  153. float roughness2 = roughness * roughness;
  154. float roughness4 = roughness2 * roughness2;
  155. float3 specular = calcMicrofacetFresnelShlick(specularColor, LoH) *
  156. calcMicrofacetDistGGX(roughness4, NoH) *
  157. calcMicrofacetShadowingSmithGGX(roughness4, NoV, NoL);
  158. // Note: Need to add energy conservation between diffuse and specular terms?
  159. return diffuse + specular * specLobeEnergy;
  160. }
  161. float3 getLuminanceDirectional(LightData lightData, float3 worldPos, float3 V, float3 R, SurfaceData surfaceData)
  162. {
  163. float3 N = surfaceData.worldNormal.xyz;
  164. float3 L = -lightData.direction;
  165. float NoL = saturate(dot(N, L));
  166. float specEnergy = 1.0f;
  167. // Distant disk area light. Calculate its contribution analytically by
  168. // finding the most important (least error) point on the area light and
  169. // use it as a form of importance sampling.
  170. if(lightData.srcRadius > 0)
  171. {
  172. float diskRadius = sin(lightData.srcRadius);
  173. float distanceToDisk = cos(lightData.srcRadius);
  174. // Closest point to disk (approximation for distant disks)
  175. float DoR = dot(L, R);
  176. float3 S = normalize(R - DoR * L);
  177. L = DoR < distanceToDisk ? normalize(distanceToDisk * L + S * diskRadius) : R;
  178. }
  179. float3 surfaceShading = getSurfaceShading(V, L, specEnergy, surfaceData);
  180. float illuminance = lightData.luminance * NoL;
  181. return lightData.color * illuminance * surfaceShading;
  182. }
  183. float3 getLuminanceRadial(LightData lightData, float3 worldPos, float3 V, float3 R, float roughness2, SurfaceData surfaceData)
  184. {
  185. float3 N = surfaceData.worldNormal.xyz;
  186. float3 toLight = lightData.position - worldPos;
  187. float distToLightSqrd = dot(toLight, toLight);
  188. float invDistToLight = rsqrt(distToLightSqrd);
  189. float3 L = toLight * invDistToLight;
  190. float NoL = dot(N, L);
  191. float specEnergy = 1.0f;
  192. float illuminance = 0.0f;
  193. // Sphere area light. Calculate its contribution analytically by
  194. // finding the most important (least error) point on the area light and
  195. // use it as a form of importance sampling.
  196. if(lightData.srcRadius > 0)
  197. {
  198. // Calculate illuminance depending on source size, distance and angle
  199. illuminance = illuminanceSphereAreaLight(NoL, distToLightSqrd, lightData);
  200. // Energy conservation:
  201. // We are widening the specular distribution by the sphere's subtended angle,
  202. // so we need to handle the increase in energy. It is not enough just to account
  203. // for the sphere solid angle, since the energy difference is highly dependent on
  204. // specular distribution. By accounting for this energy difference we ensure glossy
  205. // reflections have sharp edges, instead of being too blurry.
  206. // See http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf for reference
  207. float sphereAngle = saturate(lightData.srcRadius * invDistToLight);
  208. specEnergy = roughness2 / saturate(roughness2 + 0.5f * sphereAngle);
  209. specEnergy *= specEnergy;
  210. // Find closest point on sphere to ray
  211. float3 closestPointOnRay = dot(toLight, R) * R;
  212. float3 centerToRay = closestPointOnRay - toLight;
  213. float invDistToRay = rsqrt(dot(centerToRay, centerToRay));
  214. float3 closestPointOnSphere = toLight + centerToRay * saturate(lightData.srcRadius * invDistToRay);
  215. toLight = closestPointOnSphere;
  216. L = normalize(toLight);
  217. }
  218. else
  219. {
  220. NoL = saturate(NoL);
  221. illuminance = illuminancePointLight(distToLightSqrd, NoL, lightData);
  222. }
  223. float attenuation = getRadialAttenuation(distToLightSqrd, lightData);
  224. float3 surfaceShading = getSurfaceShading(V, L, specEnergy, surfaceData);
  225. return lightData.color * illuminance * attenuation * surfaceShading;
  226. }
  227. float3 getLuminanceSpot(LightData lightData, float3 worldPos, float3 V, float3 R, float roughness2, SurfaceData surfaceData)
  228. {
  229. float3 N = surfaceData.worldNormal.xyz;
  230. float3 toLight = lightData.position - worldPos;
  231. float distToLightSqrd = dot(toLight, toLight);
  232. float invDistToLight = rsqrt(distToLightSqrd);
  233. float3 L = toLight * invDistToLight;
  234. float NoL = dot(N, L);
  235. float specEnergy = 1.0f;
  236. float illuminance = 0.0f;
  237. float spotAttenuation = 1.0f;
  238. // Disc area light. Calculate its contribution analytically by
  239. // finding the most important (least error) point on the area light and
  240. // use it as a form of importance sampling.
  241. if(lightData.srcRadius > 0)
  242. {
  243. // Calculate illuminance depending on source size, distance and angle
  244. illuminance = illuminanceDiscAreaLight(NoL, distToLightSqrd, L, lightData);
  245. // Energy conservation: Similar case as with radial lights
  246. float rightDiscAngle = saturate(lightData.srcRadius * invDistToLight);
  247. // Account for disc orientation somewhat
  248. float discAngle = rightDiscAngle * saturate(dot(lightData.direction, -L));
  249. specEnergy = roughness2 / saturate(roughness2 + 0.5f * discAngle);
  250. specEnergy *= specEnergy;
  251. // Find closest point on disc to ray
  252. float3 discNormal = -lightData.direction;
  253. float distAlongLightDir = max(dot(R, discNormal), 1e-6f);
  254. float t = dot(toLight, discNormal) / distAlongLightDir;
  255. float3 closestPointOnPlane = R * t; // Relative to shaded world point
  256. float3 centerToRay = closestPointOnPlane - toLight;
  257. float invDistToRay = rsqrt(dot(centerToRay, centerToRay));
  258. float3 closestPointOnDisc = toLight + centerToRay * saturate(lightData.srcRadius * invDistToRay);
  259. toLight = closestPointOnDisc;
  260. L = normalize(toLight);
  261. // Expand spot attenuation by disc radius (not physically based)
  262. float3 toSpotEdge = normalize(lightData.shiftedLightPosition - worldPos);
  263. spotAttenuation = getSpotAttenuation(toSpotEdge, lightData);
  264. // TODO - Spot attenuation fades out the specular highlight in a noticeable way
  265. }
  266. else
  267. {
  268. NoL = saturate(NoL);
  269. illuminance = illuminancePointLight(distToLightSqrd, NoL, lightData);
  270. spotAttenuation = getSpotAttenuation(L, lightData);
  271. }
  272. float radialAttenuation = getRadialAttenuation(distToLightSqrd, lightData);
  273. float attenuation = spotAttenuation * radialAttenuation;
  274. float3 surfaceShading = getSurfaceShading(V, L, specEnergy, surfaceData);
  275. return lightData.color * illuminance * attenuation * surfaceShading;
  276. }
  277. };
  278. };
  279. // Hackish way of "instantiating" two versions of a mixin (to be removed when template/specialization support is added)
  280. #include "$ENGINE$\DirectLightAccumulator.bslinc"
  281. #define USE_UNIFORM_BUFFER
  282. #include "$ENGINE$\DirectLightAccumulator.bslinc"
  283. #undef USE_UNIFORM_BUFFER