EntityLighting.hlsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifndef UNITY_ENTITY_LIGHTING_INCLUDED
  2. #define UNITY_ENTITY_LIGHTING_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  4. #define LIGHTMAP_RGBM_MAX_GAMMA real(5.0) // NB: Must match value in RGBMRanges.h
  5. #define LIGHTMAP_RGBM_MAX_LINEAR real(34.493242) // LIGHTMAP_RGBM_MAX_GAMMA ^ 2.2
  6. #ifdef UNITY_LIGHTMAP_RGBM_ENCODING
  7. #ifdef UNITY_COLORSPACE_GAMMA
  8. #define LIGHTMAP_HDR_MULTIPLIER LIGHTMAP_RGBM_MAX_GAMMA
  9. #define LIGHTMAP_HDR_EXPONENT real(1.0) // Not used in gamma color space
  10. #else
  11. #define LIGHTMAP_HDR_MULTIPLIER LIGHTMAP_RGBM_MAX_LINEAR
  12. #define LIGHTMAP_HDR_EXPONENT real(2.2)
  13. #endif
  14. #elif defined(UNITY_LIGHTMAP_DLDR_ENCODING)
  15. #ifdef UNITY_COLORSPACE_GAMMA
  16. #define LIGHTMAP_HDR_MULTIPLIER real(2.0)
  17. #else
  18. #define LIGHTMAP_HDR_MULTIPLIER real(4.59) // 2.0 ^ 2.2
  19. #endif
  20. #define LIGHTMAP_HDR_EXPONENT real(0.0)
  21. #else // (UNITY_LIGHTMAP_FULL_HDR)
  22. #define LIGHTMAP_HDR_MULTIPLIER real(1.0)
  23. #define LIGHTMAP_HDR_EXPONENT real(1.0)
  24. #endif
  25. // TODO: Check if PI is correctly handled!
  26. // Ref: "Efficient Evaluation of Irradiance Environment Maps" from ShaderX 2
  27. real3 SHEvalLinearL0L1(real3 N, real4 shAr, real4 shAg, real4 shAb)
  28. {
  29. real4 vA = real4(N, 1.0);
  30. real3 x1;
  31. // Linear (L1) + constant (L0) polynomial terms
  32. x1.r = dot(shAr, vA);
  33. x1.g = dot(shAg, vA);
  34. x1.b = dot(shAb, vA);
  35. return x1;
  36. }
  37. real3 SHEvalLinearL2(real3 N, real4 shBr, real4 shBg, real4 shBb, real4 shC)
  38. {
  39. real3 x2;
  40. // 4 of the quadratic (L2) polynomials
  41. real4 vB = N.xyzz * N.yzzx;
  42. x2.r = dot(shBr, vB);
  43. x2.g = dot(shBg, vB);
  44. x2.b = dot(shBb, vB);
  45. // Final (5th) quadratic (L2) polynomial
  46. real vC = N.x * N.x - N.y * N.y;
  47. real3 x3 = shC.rgb * vC;
  48. return x2 + x3;
  49. }
  50. #if HAS_HALF
  51. half3 SampleSH9(half4 SHCoefficients[7], half3 N)
  52. {
  53. half4 shAr = SHCoefficients[0];
  54. half4 shAg = SHCoefficients[1];
  55. half4 shAb = SHCoefficients[2];
  56. half4 shBr = SHCoefficients[3];
  57. half4 shBg = SHCoefficients[4];
  58. half4 shBb = SHCoefficients[5];
  59. half4 shCr = SHCoefficients[6];
  60. // Linear + constant polynomial terms
  61. half3 res = SHEvalLinearL0L1(N, shAr, shAg, shAb);
  62. // Quadratic polynomials
  63. res += SHEvalLinearL2(N, shBr, shBg, shBb, shCr);
  64. return res;
  65. }
  66. #endif
  67. float3 SampleSH9(float4 SHCoefficients[7], float3 N)
  68. {
  69. float4 shAr = SHCoefficients[0];
  70. float4 shAg = SHCoefficients[1];
  71. float4 shAb = SHCoefficients[2];
  72. float4 shBr = SHCoefficients[3];
  73. float4 shBg = SHCoefficients[4];
  74. float4 shBb = SHCoefficients[5];
  75. float4 shCr = SHCoefficients[6];
  76. // Linear + constant polynomial terms
  77. float3 res = SHEvalLinearL0L1(N, shAr, shAg, shAb);
  78. // Quadratic polynomials
  79. res += SHEvalLinearL2(N, shBr, shBg, shBb, shCr);
  80. return res;
  81. }
  82. // texture3dLod is not supported on gles2.
  83. #if !defined(SHADER_API_GLES)
  84. // This sample a 3D volume storing SH
  85. // Volume is store as 3D texture with 4 R, G, B, Occ set of 4 coefficient store atlas in same 3D texture. Occ is use for occlusion.
  86. // TODO: the packing here is inefficient as we will fetch values far away from each other and they may not fit into the cache - Suggest we pack RGB continuously
  87. // TODO: The calcul of texcoord could be perform with a single matrix multicplication calcualted on C++ side that will fold probeVolumeMin and probeVolumeSizeInv into it and handle the identity case, no reasons to do it in C++ (ask Ionut about it)
  88. // It should also handle the camera relative path (if the render pipeline use it)
  89. float3 SampleProbeVolumeSH4(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), float3 positionWS, float3 normalWS, float4x4 WorldToTexture,
  90. float transformToLocal, float texelSizeX, float3 probeVolumeMin, float3 probeVolumeSizeInv)
  91. {
  92. float3 position = (transformToLocal == 1.0) ? mul(WorldToTexture, float4(positionWS, 1.0)).xyz : positionWS;
  93. float3 texCoord = (position - probeVolumeMin) * probeVolumeSizeInv.xyz;
  94. // Each component is store in the same texture 3D. Each use one quater on the x axis
  95. // Here we get R component then increase by step size (0.25) to get other component. This assume 4 component
  96. // but last one is not used.
  97. // Clamp to edge of the "internal" texture, as R is from half texel to size of R texture minus half texel.
  98. // This avoid leaking
  99. texCoord.x = clamp(texCoord.x * 0.25, 0.5 * texelSizeX, 0.25 - 0.5 * texelSizeX);
  100. float4 shAr = SAMPLE_TEXTURE3D_LOD(SHVolumeTexture, SHVolumeSampler, texCoord, 0);
  101. texCoord.x += 0.25;
  102. float4 shAg = SAMPLE_TEXTURE3D_LOD(SHVolumeTexture, SHVolumeSampler, texCoord, 0);
  103. texCoord.x += 0.25;
  104. float4 shAb = SAMPLE_TEXTURE3D_LOD(SHVolumeTexture, SHVolumeSampler, texCoord, 0);
  105. return SHEvalLinearL0L1(normalWS, shAr, shAg, shAb);
  106. }
  107. // The SphericalHarmonicsL2 coefficients are packed into 7 coefficients per color channel instead of 9.
  108. // The packing from 9 to 7 is done from engine code and will use the alpha component of the pixel to store an additional SH coefficient.
  109. // The 3D atlas texture will contain 7 SH coefficient parts.
  110. float3 SampleProbeVolumeSH9(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), float3 positionWS, float3 normalWS, float4x4 WorldToTexture,
  111. float transformToLocal, float texelSizeX, float3 probeVolumeMin, float3 probeVolumeSizeInv)
  112. {
  113. float3 position = (transformToLocal == 1.0f) ? mul(WorldToTexture, float4(positionWS, 1.0)).xyz : positionWS;
  114. float3 texCoord = (position - probeVolumeMin) * probeVolumeSizeInv;
  115. const uint shCoeffCount = 7;
  116. const float invShCoeffCount = 1.0f / float(shCoeffCount);
  117. // We need to compute proper X coordinate to sample into the atlas.
  118. texCoord.x = texCoord.x / shCoeffCount;
  119. // Clamp the x coordinate otherwise we'll have leaking between RGB coefficients.
  120. float texCoordX = clamp(texCoord.x, 0.5f * texelSizeX, invShCoeffCount - 0.5f * texelSizeX);
  121. float4 SHCoefficients[7];
  122. for (uint i = 0; i < shCoeffCount; i++)
  123. {
  124. texCoord.x = texCoordX + i * invShCoeffCount;
  125. SHCoefficients[i] = SAMPLE_TEXTURE3D_LOD(SHVolumeTexture, SHVolumeSampler, texCoord, 0);
  126. }
  127. return SampleSH9(SHCoefficients, normalize(normalWS));
  128. }
  129. #endif
  130. float4 SampleProbeOcclusion(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), float3 positionWS, float4x4 WorldToTexture,
  131. float transformToLocal, float texelSizeX, float3 probeVolumeMin, float3 probeVolumeSizeInv)
  132. {
  133. float3 position = (transformToLocal == 1.0) ? mul(WorldToTexture, float4(positionWS, 1.0)).xyz : positionWS;
  134. float3 texCoord = (position - probeVolumeMin) * probeVolumeSizeInv.xyz;
  135. // Sample fourth texture in the atlas
  136. // We need to compute proper U coordinate to sample.
  137. // Clamp the coordinate otherwize we'll have leaking between ShB coefficients and Probe Occlusion(Occ) info
  138. texCoord.x = max(texCoord.x * 0.25 + 0.75, 0.75 + 0.5 * texelSizeX);
  139. return SAMPLE_TEXTURE3D(SHVolumeTexture, SHVolumeSampler, texCoord);
  140. }
  141. // Following functions are to sample enlighten lightmaps (or lightmaps encoded the same way as our
  142. // enlighten implementation). They assume use of RGB9E5 for dynamic illuminance map and RGBM for baked ones.
  143. // It is required for other platform that aren't supporting this format to implement variant of these functions
  144. // (But these kind of platform should use regular render loop and not news shaders).
  145. // TODO: This is the max value allowed for emissive (bad name - but keep for now to retrieve it) (It is 8^2.2 (gamma) and 8 is the limit of punctual light slider...), comme from UnityCg.cginc. Fix it!
  146. // Ask Jesper if this can be change for HDRenderPipeline
  147. #define EMISSIVE_RGBM_SCALE 97.0
  148. // RGBM stuff is temporary. For now baked lightmap are in RGBM and the RGBM range for lightmaps is specific so we can't use the generic method.
  149. // In the end baked lightmaps are going to be BC6H so the code will be the same as dynamic lightmaps.
  150. // Same goes for emissive packed as an input for Enlighten with another hard coded multiplier.
  151. // TODO: This function is used with the LightTransport pass to encode lightmap or emissive
  152. real4 PackEmissiveRGBM(real3 rgb)
  153. {
  154. real kOneOverRGBMMaxRange = 1.0 / EMISSIVE_RGBM_SCALE;
  155. const real kMinMultiplier = 2.0 * 1e-2;
  156. real4 rgbm = real4(rgb * kOneOverRGBMMaxRange, 1.0);
  157. rgbm.a = max(max(rgbm.r, rgbm.g), max(rgbm.b, kMinMultiplier));
  158. rgbm.a = ceil(rgbm.a * 255.0) / 255.0;
  159. // Division-by-zero warning from d3d9, so make compiler happy.
  160. rgbm.a = max(rgbm.a, kMinMultiplier);
  161. rgbm.rgb /= rgbm.a;
  162. return rgbm;
  163. }
  164. real3 UnpackLightmapRGBM(real4 rgbmInput, real4 decodeInstructions)
  165. {
  166. #ifdef UNITY_COLORSPACE_GAMMA
  167. return rgbmInput.rgb * (rgbmInput.a * decodeInstructions.x);
  168. #else
  169. return rgbmInput.rgb * (PositivePow(rgbmInput.a, decodeInstructions.y) * decodeInstructions.x);
  170. #endif
  171. }
  172. real3 UnpackLightmapDoubleLDR(real4 encodedColor, real4 decodeInstructions)
  173. {
  174. return encodedColor.rgb * decodeInstructions.x;
  175. }
  176. real3 DecodeLightmap(real4 encodedIlluminance, real4 decodeInstructions)
  177. {
  178. #if defined(UNITY_LIGHTMAP_RGBM_ENCODING)
  179. return UnpackLightmapRGBM(encodedIlluminance, decodeInstructions);
  180. #elif defined(UNITY_LIGHTMAP_DLDR_ENCODING)
  181. return UnpackLightmapDoubleLDR(encodedIlluminance, decodeInstructions);
  182. #else // (UNITY_LIGHTMAP_FULL_HDR)
  183. return encodedIlluminance.rgb;
  184. #endif
  185. }
  186. real3 DecodeHDREnvironment(real4 encodedIrradiance, real4 decodeInstructions)
  187. {
  188. // Take into account texture alpha if decodeInstructions.w is true(the alpha value affects the RGB channels)
  189. real alpha = max(decodeInstructions.w * (encodedIrradiance.a - 1.0) + 1.0, 0.0);
  190. // If Linear mode is not supported we can skip exponent part
  191. return (decodeInstructions.x * PositivePow(alpha, decodeInstructions.y)) * encodedIrradiance.rgb;
  192. }
  193. real3 SampleSingleLightmap(TEXTURE2D_PARAM(lightmapTex, lightmapSampler), float2 uv, float4 transform, bool encodedLightmap, real4 decodeInstructions)
  194. {
  195. // transform is scale and bias
  196. uv = uv * transform.xy + transform.zw;
  197. real3 illuminance = real3(0.0, 0.0, 0.0);
  198. // Remark: baked lightmap is RGBM for now, dynamic lightmap is RGB9E5
  199. if (encodedLightmap)
  200. {
  201. real4 encodedIlluminance = SAMPLE_TEXTURE2D(lightmapTex, lightmapSampler, uv).rgba;
  202. illuminance = DecodeLightmap(encodedIlluminance, decodeInstructions);
  203. }
  204. else
  205. {
  206. illuminance = SAMPLE_TEXTURE2D(lightmapTex, lightmapSampler, uv).rgb;
  207. }
  208. return illuminance;
  209. }
  210. real3 SampleDirectionalLightmap(TEXTURE2D_PARAM(lightmapTex, lightmapSampler), TEXTURE2D_PARAM(lightmapDirTex, lightmapDirSampler), float2 uv, float4 transform, float3 normalWS, bool encodedLightmap, real4 decodeInstructions)
  211. {
  212. // In directional mode Enlighten bakes dominant light direction
  213. // in a way, that using it for half Lambert and then dividing by a "rebalancing coefficient"
  214. // gives a result close to plain diffuse response lightmaps, but normalmapped.
  215. // Note that dir is not unit length on purpose. Its length is "directionality", like
  216. // for the directional specular lightmaps.
  217. // transform is scale and bias
  218. uv = uv * transform.xy + transform.zw;
  219. real4 direction = SAMPLE_TEXTURE2D(lightmapDirTex, lightmapDirSampler, uv);
  220. // Remark: baked lightmap is RGBM for now, dynamic lightmap is RGB9E5
  221. real3 illuminance = real3(0.0, 0.0, 0.0);
  222. if (encodedLightmap)
  223. {
  224. real4 encodedIlluminance = SAMPLE_TEXTURE2D(lightmapTex, lightmapSampler, uv).rgba;
  225. illuminance = DecodeLightmap(encodedIlluminance, decodeInstructions);
  226. }
  227. else
  228. {
  229. illuminance = SAMPLE_TEXTURE2D(lightmapTex, lightmapSampler, uv).rgb;
  230. }
  231. real halfLambert = dot(normalWS, direction.xyz - 0.5) + 0.5;
  232. return illuminance * halfLambert / max(1e-4, direction.w);
  233. }
  234. #endif // UNITY_ENTITY_LIGHTING_INCLUDED