From 56bc24aa9b5e5c19af8cc6c3df687560909356e5 Mon Sep 17 00:00:00 2001 From: Lukas Stockner Date: Sat, 22 Jul 2023 05:07:11 +0200 Subject: [PATCH] Cycles: Merge OSL Clearcoat closure into microfacet() There's no reason why this would need to be its own closure, it was just a slightly different microfacet distribution with a hardcoded IOR and intensity multiplier internally. No functional change, just cleaning up the mess of custom OSL closures. Pull Request: https://projects.blender.org/blender/blender/pulls/109951 --- intern/cycles/kernel/osl/closures_setup.h | 28 +++---------------- intern/cycles/kernel/osl/closures_template.h | 6 ---- .../osl/shaders/node_principled_bsdf.osl | 4 +-- intern/cycles/kernel/osl/shaders/stdcycles.h | 1 - 4 files changed, 6 insertions(+), 33 deletions(-) diff --git a/intern/cycles/kernel/osl/closures_setup.h b/intern/cycles/kernel/osl/closures_setup.h index ee60e8e94b8..f6359f6823a 100644 --- a/intern/cycles/kernel/osl/closures_setup.h +++ b/intern/cycles/kernel/osl/closures_setup.h @@ -403,6 +403,10 @@ ccl_device void osl_closure_microfacet_setup(KernelGlobals kg, else if (closure->distribution == make_string("ashikhmin_shirley", 11318482998918370922ull)) { sd->flag |= bsdf_ashikhmin_shirley_setup(bsdf); } + /* Clearcoat */ + else if (closure->distribution == make_string("clearcoat", 3490136178980547276ull)) { + sd->flag |= bsdf_microfacet_ggx_clearcoat_setup(bsdf, sd); + } /* GGX (either single- or multi-scattering) */ else { if (closure->refract == 1) { @@ -645,30 +649,6 @@ ccl_device void osl_closure_principled_sheen_setup( sd->flag |= bsdf_principled_sheen_setup(sd, bsdf); } -ccl_device void osl_closure_principled_clearcoat_setup( - KernelGlobals kg, - ccl_private ShaderData *sd, - uint32_t path_flag, - float3 weight, - ccl_private const PrincipledClearcoatClosure *closure) -{ - weight *= 0.25f * closure->clearcoat; - ccl_private MicrofacetBsdf *bsdf = (ccl_private MicrofacetBsdf *)bsdf_alloc( - sd, sizeof(MicrofacetBsdf), rgb_to_spectrum(weight)); - if (!bsdf) { - return; - } - - bsdf->N = ensure_valid_specular_reflection(sd->Ng, sd->wi, closure->N); - bsdf->alpha_x = closure->clearcoat_roughness; - bsdf->alpha_y = closure->clearcoat_roughness; - bsdf->ior = 1.5f; - - bsdf->T = zero_float3(); - - sd->flag |= bsdf_microfacet_ggx_clearcoat_setup(bsdf, sd); -} - /* Variable cone emissive closure * * This primitive emits in a cone having a configurable penumbra area where the light decays to 0 diff --git a/intern/cycles/kernel/osl/closures_template.h b/intern/cycles/kernel/osl/closures_template.h index 16371993048..b20a13e1c66 100644 --- a/intern/cycles/kernel/osl/closures_template.h +++ b/intern/cycles/kernel/osl/closures_template.h @@ -136,12 +136,6 @@ OSL_CLOSURE_STRUCT_BEGIN(PrincipledSheen, principled_sheen) OSL_CLOSURE_STRUCT_MEMBER(PrincipledSheen, VECTOR, packed_float3, N, NULL) OSL_CLOSURE_STRUCT_END(PrincipledSheen, principled_sheen) -OSL_CLOSURE_STRUCT_BEGIN(PrincipledClearcoat, principled_clearcoat) - OSL_CLOSURE_STRUCT_MEMBER(PrincipledClearcoat, VECTOR, packed_float3, N, NULL) - OSL_CLOSURE_STRUCT_MEMBER(PrincipledClearcoat, FLOAT, float, clearcoat, NULL) - OSL_CLOSURE_STRUCT_MEMBER(PrincipledClearcoat, FLOAT, float, clearcoat_roughness, NULL) -OSL_CLOSURE_STRUCT_END(PrincipledClearcoat, principled_clearcoat) - OSL_CLOSURE_STRUCT_BEGIN(GenericEmissive, emission) OSL_CLOSURE_STRUCT_END(GenericEmissive, emission) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index c392b497686..57e59d32ffe 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -98,7 +98,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", } if (Clearcoat > 1e-5) { - BSDF += principled_clearcoat( - ClearcoatNormal, Clearcoat, ClearcoatRoughness * ClearcoatRoughness); + float clearcoat_r2 = ClearcoatRoughness * ClearcoatRoughness; + BSDF += 0.25 * Clearcoat * microfacet("clearcoat", ClearcoatNormal, clearcoat_r2, 1.5, 0); } } diff --git a/intern/cycles/kernel/osl/shaders/stdcycles.h b/intern/cycles/kernel/osl/shaders/stdcycles.h index 430b22f6c1a..c72af45bd4a 100644 --- a/intern/cycles/kernel/osl/shaders/stdcycles.h +++ b/intern/cycles/kernel/osl/shaders/stdcycles.h @@ -26,7 +26,6 @@ closure color ashikhmin_velvet(normal N, float sigma) BUILTIN; closure color ambient_occlusion() BUILTIN; closure color principled_diffuse(normal N, float roughness) BUILTIN; closure color principled_sheen(normal N) BUILTIN; -closure color principled_clearcoat(normal N, float clearcoat, float clearcoat_roughness) BUILTIN; /* Needed to pass along the color for multi-scattering saturation adjustment, * otherwise could be replaced by microfacet() */