BRDF.hlsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #ifndef UNIVERSAL_BRDF_INCLUDED
  2. #define UNIVERSAL_BRDF_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/BSDF.hlsl"
  4. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Deprecated.hlsl"
  6. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceData.hlsl"
  7. #define kDielectricSpec half4(0.04, 0.04, 0.04, 1.0 - 0.04) // standard dielectric reflectivity coef at incident angle (= 4%)
  8. struct BRDFData
  9. {
  10. half3 albedo;
  11. half3 diffuse;
  12. half3 specular;
  13. half reflectivity;
  14. half perceptualRoughness;
  15. half roughness;
  16. half roughness2;
  17. half grazingTerm;
  18. // We save some light invariant BRDF terms so we don't have to recompute
  19. // them in the light loop. Take a look at DirectBRDF function for detailed explaination.
  20. half normalizationTerm; // roughness * 4.0 + 2.0
  21. half roughness2MinusOne; // roughness^2 - 1.0
  22. };
  23. half ReflectivitySpecular(half3 specular)
  24. {
  25. #if defined(SHADER_API_GLES)
  26. return specular.r; // Red channel - because most metals are either monocrhome or with redish/yellowish tint
  27. #else
  28. return Max3(specular.r, specular.g, specular.b);
  29. #endif
  30. }
  31. half OneMinusReflectivityMetallic(half metallic)
  32. {
  33. // We'll need oneMinusReflectivity, so
  34. // 1-reflectivity = 1-lerp(dielectricSpec, 1, metallic) = lerp(1-dielectricSpec, 0, metallic)
  35. // store (1-dielectricSpec) in kDielectricSpec.a, then
  36. // 1-reflectivity = lerp(alpha, 0, metallic) = alpha + metallic*(0 - alpha) =
  37. // = alpha - metallic * alpha
  38. half oneMinusDielectricSpec = kDielectricSpec.a;
  39. return oneMinusDielectricSpec - metallic * oneMinusDielectricSpec;
  40. }
  41. half MetallicFromReflectivity(half reflectivity)
  42. {
  43. half oneMinusDielectricSpec = kDielectricSpec.a;
  44. return (reflectivity - kDielectricSpec.r) / oneMinusDielectricSpec;
  45. }
  46. inline void InitializeBRDFDataDirect(half3 albedo, half3 diffuse, half3 specular, half reflectivity, half oneMinusReflectivity, half smoothness, inout half alpha, out BRDFData outBRDFData)
  47. {
  48. outBRDFData = (BRDFData)0;
  49. outBRDFData.albedo = albedo;
  50. outBRDFData.diffuse = diffuse;
  51. outBRDFData.specular = specular;
  52. outBRDFData.reflectivity = reflectivity;
  53. outBRDFData.perceptualRoughness = PerceptualSmoothnessToPerceptualRoughness(smoothness);
  54. outBRDFData.roughness = max(PerceptualRoughnessToRoughness(outBRDFData.perceptualRoughness), HALF_MIN_SQRT);
  55. outBRDFData.roughness2 = max(outBRDFData.roughness * outBRDFData.roughness, HALF_MIN);
  56. outBRDFData.grazingTerm = saturate(smoothness + reflectivity);
  57. outBRDFData.normalizationTerm = outBRDFData.roughness * half(4.0) + half(2.0);
  58. outBRDFData.roughness2MinusOne = outBRDFData.roughness2 - half(1.0);
  59. #ifdef _ALPHAPREMULTIPLY_ON
  60. outBRDFData.diffuse *= alpha;
  61. alpha = alpha * oneMinusReflectivity + reflectivity; // NOTE: alpha modified and propagated up.
  62. #endif
  63. }
  64. // Legacy: do not call, will not correctly initialize albedo property.
  65. inline void InitializeBRDFDataDirect(half3 diffuse, half3 specular, half reflectivity, half oneMinusReflectivity, half smoothness, inout half alpha, out BRDFData outBRDFData)
  66. {
  67. InitializeBRDFDataDirect(half3(0.0, 0.0, 0.0), diffuse, specular, reflectivity, oneMinusReflectivity, smoothness, alpha, outBRDFData);
  68. }
  69. // Initialize BRDFData for material, managing both specular and metallic setup using shader keyword _SPECULAR_SETUP.
  70. inline void InitializeBRDFData(half3 albedo, half metallic, half3 specular, half smoothness, inout half alpha, out BRDFData outBRDFData)
  71. {
  72. #ifdef _SPECULAR_SETUP
  73. half reflectivity = ReflectivitySpecular(specular);
  74. half oneMinusReflectivity = half(1.0) - reflectivity;
  75. half3 brdfDiffuse = albedo * (half3(1.0, 1.0, 1.0) - specular);
  76. half3 brdfSpecular = specular;
  77. #else
  78. half oneMinusReflectivity = OneMinusReflectivityMetallic(metallic);
  79. half reflectivity = half(1.0) - oneMinusReflectivity;
  80. half3 brdfDiffuse = albedo * oneMinusReflectivity;
  81. half3 brdfSpecular = lerp(kDieletricSpec.rgb, albedo, metallic);
  82. #endif
  83. InitializeBRDFDataDirect(albedo, brdfDiffuse, brdfSpecular, reflectivity, oneMinusReflectivity, smoothness, alpha, outBRDFData);
  84. }
  85. inline void InitializeBRDFData(inout SurfaceData surfaceData, out BRDFData brdfData)
  86. {
  87. InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
  88. }
  89. half3 ConvertF0ForClearCoat15(half3 f0)
  90. {
  91. #if defined(SHADER_API_MOBILE)
  92. return ConvertF0ForAirInterfaceToF0ForClearCoat15Fast(f0);
  93. #else
  94. return ConvertF0ForAirInterfaceToF0ForClearCoat15(f0);
  95. #endif
  96. }
  97. inline void InitializeBRDFDataClearCoat(half clearCoatMask, half clearCoatSmoothness, inout BRDFData baseBRDFData, out BRDFData outBRDFData)
  98. {
  99. outBRDFData = (BRDFData)0;
  100. outBRDFData.albedo = half(1.0);
  101. // Calculate Roughness of Clear Coat layer
  102. outBRDFData.diffuse = kDielectricSpec.aaa; // 1 - kDielectricSpec
  103. outBRDFData.specular = kDielectricSpec.rgb;
  104. outBRDFData.reflectivity = kDielectricSpec.r;
  105. outBRDFData.perceptualRoughness = PerceptualSmoothnessToPerceptualRoughness(clearCoatSmoothness);
  106. outBRDFData.roughness = max(PerceptualRoughnessToRoughness(outBRDFData.perceptualRoughness), HALF_MIN_SQRT);
  107. outBRDFData.roughness2 = max(outBRDFData.roughness * outBRDFData.roughness, HALF_MIN);
  108. outBRDFData.normalizationTerm = outBRDFData.roughness * half(4.0) + half(2.0);
  109. outBRDFData.roughness2MinusOne = outBRDFData.roughness2 - half(1.0);
  110. outBRDFData.grazingTerm = saturate(clearCoatSmoothness + kDielectricSpec.x);
  111. // Relatively small effect, cut it for lower quality
  112. #if !defined(SHADER_API_MOBILE)
  113. // Modify Roughness of base layer using coat IOR
  114. half ieta = lerp(1.0h, CLEAR_COAT_IETA, clearCoatMask);
  115. half coatRoughnessScale = Sq(ieta);
  116. half sigma = RoughnessToVariance(PerceptualRoughnessToRoughness(baseBRDFData.perceptualRoughness));
  117. baseBRDFData.perceptualRoughness = RoughnessToPerceptualRoughness(VarianceToRoughness(sigma * coatRoughnessScale));
  118. // Recompute base material for new roughness, previous computation should be eliminated by the compiler (as it's unused)
  119. baseBRDFData.roughness = max(PerceptualRoughnessToRoughness(baseBRDFData.perceptualRoughness), HALF_MIN_SQRT);
  120. baseBRDFData.roughness2 = max(baseBRDFData.roughness * baseBRDFData.roughness, HALF_MIN);
  121. baseBRDFData.normalizationTerm = baseBRDFData.roughness * 4.0h + 2.0h;
  122. baseBRDFData.roughness2MinusOne = baseBRDFData.roughness2 - 1.0h;
  123. #endif
  124. // Darken/saturate base layer using coat to surface reflectance (vs. air to surface)
  125. baseBRDFData.specular = lerp(baseBRDFData.specular, ConvertF0ForClearCoat15(baseBRDFData.specular), clearCoatMask);
  126. // TODO: what about diffuse? at least in specular workflow diffuse should be recalculated as it directly depends on it.
  127. }
  128. BRDFData CreateClearCoatBRDFData(SurfaceData surfaceData, inout BRDFData brdfData)
  129. {
  130. BRDFData brdfDataClearCoat = (BRDFData)0;
  131. #if defined(_CLEARCOAT) || defined(_CLEARCOATMAP)
  132. // base brdfData is modified here, rely on the compiler to eliminate dead computation by InitializeBRDFData()
  133. InitializeBRDFDataClearCoat(surfaceData.clearCoatMask, surfaceData.clearCoatSmoothness, brdfData, brdfDataClearCoat);
  134. #endif
  135. return brdfDataClearCoat;
  136. }
  137. // Computes the specular term for EnvironmentBRDF
  138. half3 EnvironmentBRDFSpecular(BRDFData brdfData, half fresnelTerm)
  139. {
  140. float surfaceReduction = 1.0 / (brdfData.roughness2 + 1.0);
  141. return half3(surfaceReduction * lerp(brdfData.specular, brdfData.grazingTerm, fresnelTerm));
  142. }
  143. half3 EnvironmentBRDF(BRDFData brdfData, half3 indirectDiffuse, half3 indirectSpecular, half fresnelTerm)
  144. {
  145. half3 c = indirectDiffuse * brdfData.diffuse;
  146. c += indirectSpecular * EnvironmentBRDFSpecular(brdfData, fresnelTerm);
  147. return c;
  148. }
  149. // Environment BRDF without diffuse for clear coat
  150. half3 EnvironmentBRDFClearCoat(BRDFData brdfData, half clearCoatMask, half3 indirectSpecular, half fresnelTerm)
  151. {
  152. float surfaceReduction = 1.0 / (brdfData.roughness2 + 1.0);
  153. return indirectSpecular * EnvironmentBRDFSpecular(brdfData, fresnelTerm) * clearCoatMask;
  154. }
  155. // Computes the scalar specular term for Minimalist CookTorrance BRDF
  156. // NOTE: needs to be multiplied with reflectance f0, i.e. specular color to complete
  157. half DirectBRDFSpecular(BRDFData brdfData, half3 normalWS, half3 lightDirectionWS, half3 viewDirectionWS)
  158. {
  159. float3 lightDirectionWSFloat3 = float3(lightDirectionWS);
  160. float3 halfDir = SafeNormalize(lightDirectionWSFloat3 + float3(viewDirectionWS));
  161. float NoH = saturate(dot(float3(normalWS), halfDir));
  162. half LoH = half(saturate(dot(lightDirectionWSFloat3, halfDir)));
  163. // GGX Distribution multiplied by combined approximation of Visibility and Fresnel
  164. // BRDFspec = (D * V * F) / 4.0
  165. // D = roughness^2 / ( NoH^2 * (roughness^2 - 1) + 1 )^2
  166. // V * F = 1.0 / ( LoH^2 * (roughness + 0.5) )
  167. // See "Optimizing PBR for Mobile" from Siggraph 2015 moving mobile graphics course
  168. // https://community.arm.com/events/1155
  169. // Final BRDFspec = roughness^2 / ( NoH^2 * (roughness^2 - 1) + 1 )^2 * (LoH^2 * (roughness + 0.5) * 4.0)
  170. // We further optimize a few light invariant terms
  171. // brdfData.normalizationTerm = (roughness + 0.5) * 4.0 rewritten as roughness * 4.0 + 2.0 to a fit a MAD.
  172. float d = NoH * NoH * brdfData.roughness2MinusOne + 1.00001f;
  173. half d2 = half(d * d);
  174. half LoH2 = LoH * LoH;
  175. half specularTerm = brdfData.roughness2 / (d2 * max(half(0.1), LoH2) * brdfData.normalizationTerm);
  176. // On platforms where half actually means something, the denominator has a risk of overflow
  177. // clamp below was added specifically to "fix" that, but dx compiler (we convert bytecode to metal/gles)
  178. // sees that specularTerm have only non-negative terms, so it skips max(0,..) in clamp (leaving only min(100,...))
  179. #if defined (SHADER_API_MOBILE) || defined (SHADER_API_SWITCH)
  180. specularTerm = specularTerm - HALF_MIN;
  181. specularTerm = clamp(specularTerm, 0.0, 100.0); // Prevent FP16 overflow on mobiles
  182. #endif
  183. return specularTerm;
  184. }
  185. // Based on Minimalist CookTorrance BRDF
  186. // Implementation is slightly different from original derivation: http://www.thetenthplanet.de/archives/255
  187. //
  188. // * NDF [Modified] GGX
  189. // * Modified Kelemen and Szirmay-Kalos for Visibility term
  190. // * Fresnel approximated with 1/LdotH
  191. half3 DirectBDRF(BRDFData brdfData, half3 normalWS, half3 lightDirectionWS, half3 viewDirectionWS, bool specularHighlightsOff)
  192. {
  193. // Can still do compile-time optimisation.
  194. // If no compile-time optimized, extra overhead if branch taken is around +2.5% on some untethered platforms, -10% if not taken.
  195. [branch] if (!specularHighlightsOff)
  196. {
  197. half specularTerm = DirectBRDFSpecular(brdfData, normalWS, lightDirectionWS, viewDirectionWS);
  198. half3 color = brdfData.diffuse + specularTerm * brdfData.specular;
  199. return color;
  200. }
  201. else
  202. return brdfData.diffuse;
  203. }
  204. // Based on Minimalist CookTorrance BRDF
  205. // Implementation is slightly different from original derivation: http://www.thetenthplanet.de/archives/255
  206. //
  207. // * NDF [Modified] GGX
  208. // * Modified Kelemen and Szirmay-Kalos for Visibility term
  209. // * Fresnel approximated with 1/LdotH
  210. half3 DirectBRDF(BRDFData brdfData, half3 normalWS, half3 lightDirectionWS, half3 viewDirectionWS)
  211. {
  212. #ifndef _SPECULARHIGHLIGHTS_OFF
  213. return brdfData.diffuse + DirectBRDFSpecular(brdfData, normalWS, lightDirectionWS, viewDirectionWS) * brdfData.specular;
  214. #else
  215. return brdfData.diffuse;
  216. #endif
  217. }
  218. #endif