Cycles: unify OSL BSSRDF closure into a single bssrdf() closure with method.
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Thu, 8 Feb 2018 15:14:04 +0000 (16:14 +0100)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Thu, 8 Feb 2018 15:56:11 +0000 (16:56 +0100)
This is similar to the upstream unified microfacet() closure, and makes it
easier to extend in the future.

intern/cycles/kernel/osl/osl_bssrdf.cpp
intern/cycles/kernel/osl/osl_closures.cpp
intern/cycles/kernel/osl/osl_closures.h
intern/cycles/kernel/shaders/node_principled_bsdf.osl
intern/cycles/kernel/shaders/node_subsurface_scattering.osl
intern/cycles/kernel/shaders/stdosl.h

index 3e7905f26dfd34e101eb253f66509b655c249239..db6426f60e5d114e2a3deb9857c1864c2c4c6f5f 100644 (file)
@@ -48,9 +48,38 @@ CCL_NAMESPACE_BEGIN
 
 using namespace OSL;
 
+static ustring u_cubic("cubic");
+static ustring u_gaussian("gaussian");
+static ustring u_burley("burley");
+static ustring u_principled("principled");
+
 class CBSSRDFClosure : public CClosurePrimitive {
 public:
        Bssrdf params;
+       ustring method;
+
+       CBSSRDFClosure()
+       {
+               params.texture_blur = 0.0f;
+               params.sharpness = 0.0f;
+               params.roughness = 0.0f;
+       }
+
+       void setup(ShaderData *sd, int path_flag, float3 weight)
+       {
+               if (method == u_cubic) {
+                       alloc(sd, path_flag, weight, CLOSURE_BSSRDF_CUBIC_ID);
+               }
+               else if (method == u_gaussian) {
+                       alloc(sd, path_flag, weight, CLOSURE_BSSRDF_GAUSSIAN_ID);
+               }
+               else if (method == u_burley) {
+                       alloc(sd, path_flag, weight, CLOSURE_BSSRDF_BURLEY_ID);
+               }
+               else if (method == u_principled) {
+                       alloc(sd, path_flag, weight, CLOSURE_BSSRDF_PRINCIPLED_ID);
+               }
+       }
 
        void alloc(ShaderData *sd, int path_flag, float3 weight, ClosureType type)
        {
@@ -76,105 +105,23 @@ public:
        }
 };
 
-/* Cubic */
-
-class CubicBSSRDFClosure : public CBSSRDFClosure {
-public:
-       void setup(ShaderData *sd, int path_flag, float3 weight)
-       {
-               alloc(sd, path_flag, weight, CLOSURE_BSSRDF_CUBIC_ID);
-       }
-};
-
-ClosureParam *closure_bssrdf_cubic_params()
-{
-       static ClosureParam params[] = {
-               CLOSURE_FLOAT3_PARAM(CubicBSSRDFClosure, params.N),
-               CLOSURE_FLOAT3_PARAM(CubicBSSRDFClosure, params.radius),
-               CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, params.texture_blur),
-               CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, params.sharpness),
-               CLOSURE_STRING_KEYPARAM(CubicBSSRDFClosure, label, "label"),
-               CLOSURE_FINISH_PARAM(CubicBSSRDFClosure)
-       };
-       return params;
-}
-
-CCLOSURE_PREPARE(closure_bssrdf_cubic_prepare, CubicBSSRDFClosure)
-
-/* Gaussian */
-
-class GaussianBSSRDFClosure : public CBSSRDFClosure {
-public:
-       void setup(ShaderData *sd, int path_flag, float3 weight)
-       {
-               alloc(sd, path_flag, weight, CLOSURE_BSSRDF_GAUSSIAN_ID);
-       }
-};
-
-ClosureParam *closure_bssrdf_gaussian_params()
-{
-       static ClosureParam params[] = {
-               CLOSURE_FLOAT3_PARAM(GaussianBSSRDFClosure, params.N),
-               CLOSURE_FLOAT3_PARAM(GaussianBSSRDFClosure, params.radius),
-               CLOSURE_FLOAT_PARAM(GaussianBSSRDFClosure, params.texture_blur),
-               CLOSURE_STRING_KEYPARAM(GaussianBSSRDFClosure, label, "label"),
-               CLOSURE_FINISH_PARAM(GaussianBSSRDFClosure)
-       };
-       return params;
-}
-
-CCLOSURE_PREPARE(closure_bssrdf_gaussian_prepare, GaussianBSSRDFClosure)
-
-/* Burley */
-
-class BurleyBSSRDFClosure : public CBSSRDFClosure {
-public:
-       void setup(ShaderData *sd, int path_flag, float3 weight)
-       {
-               alloc(sd, path_flag, weight, CLOSURE_BSSRDF_BURLEY_ID);
-       }
-};
-
-ClosureParam *closure_bssrdf_burley_params()
-{
-       static ClosureParam params[] = {
-               CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, params.N),
-               CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, params.radius),
-               CLOSURE_FLOAT_PARAM(BurleyBSSRDFClosure, params.texture_blur),
-               CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, params.albedo),
-               CLOSURE_STRING_KEYPARAM(BurleyBSSRDFClosure, label, "label"),
-               CLOSURE_FINISH_PARAM(BurleyBSSRDFClosure)
-       };
-       return params;
-}
-
-CCLOSURE_PREPARE(closure_bssrdf_burley_prepare, BurleyBSSRDFClosure)
-
-/* Disney principled */
-
-class PrincipledBSSRDFClosure : public CBSSRDFClosure {
-public:
-       void setup(ShaderData *sd, int path_flag, float3 weight)
-       {
-               alloc(sd, path_flag, weight, CLOSURE_BSSRDF_PRINCIPLED_ID);
-       }
-};
-
-ClosureParam *closure_bssrdf_principled_params()
+ClosureParam *closure_bssrdf_params()
 {
        static ClosureParam params[] = {
-               CLOSURE_FLOAT3_PARAM(PrincipledBSSRDFClosure, params.N),
-               CLOSURE_FLOAT3_PARAM(PrincipledBSSRDFClosure, params.radius),
-               CLOSURE_FLOAT_PARAM(PrincipledBSSRDFClosure, params.texture_blur),
-               CLOSURE_FLOAT3_PARAM(PrincipledBSSRDFClosure, params.albedo),
-               CLOSURE_FLOAT_PARAM(PrincipledBSSRDFClosure, params.roughness),
-               CLOSURE_STRING_KEYPARAM(PrincipledBSSRDFClosure, label, "label"),
-               CLOSURE_FINISH_PARAM(PrincipledBSSRDFClosure)
+               CLOSURE_STRING_PARAM(CBSSRDFClosure, method),
+               CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.N),
+               CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.radius),
+               CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.albedo),
+               CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.texture_blur, "texture_blur"),
+               CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.sharpness, "sharpness"),
+               CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.roughness, "roughness"),
+               CLOSURE_STRING_KEYPARAM(CBSSRDFClosure, label, "label"),
+               CLOSURE_FINISH_PARAM(CBSSRDFClosure)
        };
        return params;
 }
 
-CCLOSURE_PREPARE(closure_bssrdf_principled_prepare, PrincipledBSSRDFClosure)
+CCLOSURE_PREPARE(closure_bssrdf_prepare, CBSSRDFClosure)
 
 CCL_NAMESPACE_END
 
index 8acab1ab55845ec3c374ef11303b05655d5c2c09..d0c357580fd3f77a8acf4d015b137e60c9e4cee2 100644 (file)
@@ -316,14 +316,8 @@ void OSLShader::register_closures(OSLShadingSystem *ss_)
                closure_bsdf_diffuse_ramp_params(), closure_bsdf_diffuse_ramp_prepare);
        register_closure(ss, "phong_ramp", id++,
                closure_bsdf_phong_ramp_params(), closure_bsdf_phong_ramp_prepare);
-       register_closure(ss, "bssrdf_cubic", id++,
-               closure_bssrdf_cubic_params(), closure_bssrdf_cubic_prepare);
-       register_closure(ss, "bssrdf_gaussian", id++,
-               closure_bssrdf_gaussian_params(), closure_bssrdf_gaussian_prepare);
-       register_closure(ss, "bssrdf_burley", id++,
-               closure_bssrdf_burley_params(), closure_bssrdf_burley_prepare);
-       register_closure(ss, "bssrdf_principled", id++,
-               closure_bssrdf_principled_params(), closure_bssrdf_principled_prepare);
+       register_closure(ss, "bssrdf", id++,
+               closure_bssrdf_params(), closure_bssrdf_prepare);
 
        register_closure(ss, "hair_reflection", id++,
                bsdf_hair_reflection_params(), bsdf_hair_reflection_prepare);
index 38943b77656596f6afcca901815db89083ff96e5..dca7e74f154ffd4c10f788c9b4abb753894c1eca 100644 (file)
@@ -49,10 +49,7 @@ OSL::ClosureParam *closure_ambient_occlusion_params();
 OSL::ClosureParam *closure_bsdf_diffuse_ramp_params();
 OSL::ClosureParam *closure_bsdf_phong_ramp_params();
 OSL::ClosureParam *closure_bsdf_transparent_params();
-OSL::ClosureParam *closure_bssrdf_cubic_params();
-OSL::ClosureParam *closure_bssrdf_gaussian_params();
-OSL::ClosureParam *closure_bssrdf_burley_params();
-OSL::ClosureParam *closure_bssrdf_principled_params();
+OSL::ClosureParam *closure_bssrdf_params();
 OSL::ClosureParam *closure_absorption_params();
 OSL::ClosureParam *closure_henyey_greenstein_params();
 OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_params();
@@ -72,10 +69,7 @@ void closure_ambient_occlusion_prepare(OSL::RendererServices *, int id, void *da
 void closure_bsdf_diffuse_ramp_prepare(OSL::RendererServices *, int id, void *data);
 void closure_bsdf_phong_ramp_prepare(OSL::RendererServices *, int id, void *data);
 void closure_bsdf_transparent_prepare(OSL::RendererServices *, int id, void *data);
-void closure_bssrdf_cubic_prepare(OSL::RendererServices *, int id, void *data);
-void closure_bssrdf_gaussian_prepare(OSL::RendererServices *, int id, void *data);
-void closure_bssrdf_burley_prepare(OSL::RendererServices *, int id, void *data);
-void closure_bssrdf_principled_prepare(OSL::RendererServices *, int id, void *data);
+void closure_bssrdf_prepare(OSL::RendererServices *, int id, void *data);
 void closure_absorption_prepare(OSL::RendererServices *, int id, void *data);
 void closure_henyey_greenstein_prepare(OSL::RendererServices *, int id, void *data);
 void closure_bsdf_microfacet_multi_ggx_prepare(OSL::RendererServices *, int id, void *data);
index 6870d479af3a01335b962177edd7f291d793318e..0e31dcedee42361084d6d353144d4feb714c0666 100644 (file)
@@ -58,7 +58,7 @@ shader node_principled_bsdf(
        if (diffuse_weight > 1e-5) {
                if (Subsurface > 1e-5) {
                        color mixed_ss_base_color = SubsurfaceColor * Subsurface + BaseColor * (1.0 - Subsurface);
-                       BSDF = mixed_ss_base_color * bssrdf_principled(Normal, Subsurface * SubsurfaceRadius, 0.0, SubsurfaceColor, Roughness);
+                       BSDF = mixed_ss_base_color * bssrdf("principled", Normal, Subsurface * SubsurfaceRadius, SubsurfaceColor, "roughness", Roughness);
                } else {
                        BSDF = BaseColor * principled_diffuse(Normal, Roughness);
                }
index 5ba8f34021de350a9b09a14bb1f181d03357d47c..c9983fcd5ddfbe3b70133c4c2a5047008db24928 100644 (file)
@@ -27,10 +27,10 @@ shader node_subsurface_scattering(
        output closure color BSSRDF = 0)
 {
        if (falloff == "gaussian")
-               BSSRDF = Color * bssrdf_gaussian(Normal, Scale * Radius, TextureBlur);
+               BSSRDF = Color * bssrdf("gaussian", Normal, Scale * Radius, Color, "texture_blur", TextureBlur);
        else if (falloff == "cubic")
-               BSSRDF = Color * bssrdf_cubic(Normal, Scale * Radius, TextureBlur, Sharpness);
+               BSSRDF = Color * bssrdf("cubic", Normal, Scale * Radius, Color, "texture_blur", TextureBlur, "sharpness", Sharpness);
        else
-               BSSRDF = Color * bssrdf_burley(Normal, Scale * Radius, TextureBlur, Color);
+               BSSRDF = Color * bssrdf("burley", Normal, Scale * Radius, Color, "texture_blur", TextureBlur);
 }
 
index c91d29186879d20a5b67fee73b7a070ec7047e9b..091ade4a60d0d28b5a3a76971fb6facdbc3ffeb6 100644 (file)
@@ -549,10 +549,7 @@ closure color principled_sheen(normal N) BUILTIN;
 closure color principled_clearcoat(normal N, float clearcoat, float clearcoat_roughness) BUILTIN;
 
 // BSSRDF
-closure color bssrdf_cubic(normal N, vector radius, float texture_blur, float sharpness) BUILTIN;
-closure color bssrdf_gaussian(normal N, vector radius, float texture_blur) BUILTIN;
-closure color bssrdf_burley(normal N, vector radius, float texture_blur, color albedo) BUILTIN;
-closure color bssrdf_principled(normal N, vector radius, float texture_blur, color subsurface_color, float roughness) BUILTIN;
+closure color bssrdf(string method, normal N, vector radius, color albedo) BUILTIN;
 
 // Hair
 closure color hair_reflection(normal N, float roughnessu, float roughnessv, vector T, float offset) BUILTIN;