diff options
author | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2016-07-25 04:03:23 +0300 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2016-07-31 03:34:43 +0300 |
commit | 9b6ed3a42b9a0fea56808fd5ce0d18cb5231f47b (patch) | |
tree | ed34a31222ae6d9fbd315f722b0ce327a3d397a9 /intern/cycles/kernel/osl | |
parent | 1776f75c3b3621a28ed7af535192ce7f05faea8f (diff) |
Cycles: refactor kernel closure storage to use structs per closure type.
Reviewed By: dingto, sergey
Differential Revision: https://developer.blender.org/D2127
Diffstat (limited to 'intern/cycles/kernel/osl')
-rw-r--r-- | intern/cycles/kernel/osl/CMakeLists.txt | 1 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/background.cpp | 20 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp | 46 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/bsdf_phong_ramp.cpp | 48 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/emissive.cpp | 23 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_bssrdf.cpp | 102 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_bssrdf.h | 61 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_closures.cpp | 248 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_closures.h | 110 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_shader.cpp | 285 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_shader.h | 10 |
11 files changed, 284 insertions, 670 deletions
diff --git a/intern/cycles/kernel/osl/CMakeLists.txt b/intern/cycles/kernel/osl/CMakeLists.txt index 9cf4f2d759a..98de40e5a8a 100644 --- a/intern/cycles/kernel/osl/CMakeLists.txt +++ b/intern/cycles/kernel/osl/CMakeLists.txt @@ -25,7 +25,6 @@ set(SRC ) set(HEADER_SRC - osl_bssrdf.h osl_closures.h osl_globals.h osl_services.h diff --git a/intern/cycles/kernel/osl/background.cpp b/intern/cycles/kernel/osl/background.cpp index 85fa7b34bcc..d835f9be45c 100644 --- a/intern/cycles/kernel/osl/background.cpp +++ b/intern/cycles/kernel/osl/background.cpp @@ -36,6 +36,9 @@ #include "osl_closures.h" +#include "kernel_compat_cpu.h" +#include "closure/alloc.h" + CCL_NAMESPACE_BEGIN using namespace OSL; @@ -48,7 +51,10 @@ using namespace OSL; /// class GenericBackgroundClosure : public CClosurePrimitive { public: - GenericBackgroundClosure() : CClosurePrimitive(Background) {} + void setup(ShaderData *sd, int /* path_flag */, float3 weight) + { + closure_alloc(sd, sizeof(ShaderClosure), CLOSURE_BACKGROUND_ID, weight); + } }; /// Holdout closure @@ -60,7 +66,11 @@ public: /// class HoldoutClosure : CClosurePrimitive { public: - HoldoutClosure () : CClosurePrimitive(Holdout) {} + void setup(ShaderData *sd, int /* path_flag */, float3 weight) + { + closure_alloc(sd, sizeof(ShaderClosure), CLOSURE_HOLDOUT_ID, weight); + sd->flag |= SD_HOLDOUT; + } }; /// ambient occlusion closure @@ -71,7 +81,11 @@ public: /// class AmbientOcclusionClosure : public CClosurePrimitive { public: - AmbientOcclusionClosure () : CClosurePrimitive(AmbientOcclusion) {} + void setup(ShaderData *sd, int /* path_flag */, float3 weight) + { + closure_alloc(sd, sizeof(ShaderClosure), CLOSURE_AMBIENT_OCCLUSION_ID, weight); + sd->flag |= SD_AO; + } }; ClosureParam *closure_background_params() diff --git a/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp b/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp index b5c0d76cf37..bc26f42b559 100644 --- a/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp +++ b/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp @@ -39,6 +39,7 @@ #include "kernel_types.h" #include "kernel_montecarlo.h" +#include "closure/alloc.h" #include "closure/bsdf_diffuse_ramp.h" CCL_NAMESPACE_BEGIN @@ -47,51 +48,30 @@ using namespace OSL; class DiffuseRampClosure : public CBSDFClosure { public: + DiffuseRampBsdf params; Color3 colors[8]; - float3 fcolors[8]; - DiffuseRampClosure() : CBSDFClosure(LABEL_DIFFUSE) - {} - - void setup() + void setup(ShaderData *sd, int /* path_flag */, float3 weight) { - sc.prim = this; - m_shaderdata_flag = bsdf_diffuse_ramp_setup(&sc); + DiffuseRampBsdf *bsdf = (DiffuseRampBsdf*)bsdf_alloc_osl(sd, sizeof(DiffuseRampBsdf), weight, ¶ms); - for(int i = 0; i < 8; i++) - fcolors[i] = TO_FLOAT3(colors[i]); - } + if(bsdf) { + bsdf->colors = (float3*)closure_alloc_extra(sd, sizeof(float3)*8); - void blur(float roughness) - { - bsdf_diffuse_ramp_blur(&sc, roughness); - } + if(bsdf->colors) { + for(int i = 0; i < 8; i++) + bsdf->colors[i] = TO_FLOAT3(colors[i]); - float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float& pdf) const - { - return bsdf_diffuse_ramp_eval_reflect(&sc, fcolors, omega_out, omega_in, &pdf); - } - - float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float& pdf) const - { - return bsdf_diffuse_ramp_eval_transmit(&sc, fcolors, omega_out, omega_in, &pdf); - } - - int sample(const float3 &Ng, - const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy, - float randu, float randv, - float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy, - float &pdf, float3 &eval) const - { - return bsdf_diffuse_ramp_sample(&sc, fcolors, Ng, omega_out, domega_out_dx, domega_out_dy, - randu, randv, &eval, &omega_in, &domega_in_dx, &domega_in_dy, &pdf); + sd->flag |= bsdf_diffuse_ramp_setup(bsdf); + } + } } }; ClosureParam *closure_bsdf_diffuse_ramp_params() { static ClosureParam params[] = { - CLOSURE_FLOAT3_PARAM(DiffuseRampClosure, sc.N), + CLOSURE_FLOAT3_PARAM(DiffuseRampClosure, params.N), CLOSURE_COLOR_ARRAY_PARAM(DiffuseRampClosure, colors, 8), CLOSURE_STRING_KEYPARAM(DiffuseRampClosure, label, "label"), CLOSURE_FINISH_PARAM(DiffuseRampClosure) diff --git a/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp b/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp index bc73d80cd78..14c7644936e 100644 --- a/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp +++ b/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp @@ -38,6 +38,7 @@ #include "osl_closures.h" #include "kernel_types.h" +#include "closure/alloc.h" #include "closure/bsdf_phong_ramp.h" CCL_NAMESPACE_BEGIN @@ -46,52 +47,31 @@ using namespace OSL; class PhongRampClosure : public CBSDFClosure { public: + PhongRampBsdf params; Color3 colors[8]; - float3 fcolors[8]; - PhongRampClosure() : CBSDFClosure(LABEL_GLOSSY) - {} - - void setup() + void setup(ShaderData *sd, int /* path_flag */, float3 weight) { - sc.prim = this; - m_shaderdata_flag = bsdf_phong_ramp_setup(&sc); + PhongRampBsdf *bsdf = (PhongRampBsdf*)bsdf_alloc_osl(sd, sizeof(PhongRampBsdf), weight, ¶ms); - for(int i = 0; i < 8; i++) - fcolors[i] = TO_FLOAT3(colors[i]); - } + if(bsdf) { + bsdf->colors = (float3*)closure_alloc_extra(sd, sizeof(float3)*8); - void blur(float roughness) - { - bsdf_phong_ramp_blur(&sc, roughness); - } + if(bsdf->colors) { + for(int i = 0; i < 8; i++) + bsdf->colors[i] = TO_FLOAT3(colors[i]); - float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float& pdf) const - { - return bsdf_phong_ramp_eval_reflect(&sc, fcolors, omega_out, omega_in, &pdf); - } - - float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float& pdf) const - { - return bsdf_phong_ramp_eval_transmit(&sc, fcolors, omega_out, omega_in, &pdf); - } - - int sample(const float3 &Ng, - const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy, - float randu, float randv, - float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy, - float &pdf, float3 &eval) const - { - return bsdf_phong_ramp_sample(&sc, fcolors, Ng, omega_out, domega_out_dx, domega_out_dy, - randu, randv, &eval, &omega_in, &domega_in_dx, &domega_in_dy, &pdf); + sd->flag |= bsdf_phong_ramp_setup(bsdf); + } + } } }; ClosureParam *closure_bsdf_phong_ramp_params() { static ClosureParam params[] = { - CLOSURE_FLOAT3_PARAM(PhongRampClosure, sc.N), - CLOSURE_FLOAT_PARAM(PhongRampClosure, sc.data0), + CLOSURE_FLOAT3_PARAM(PhongRampClosure, params.N), + CLOSURE_FLOAT_PARAM(PhongRampClosure, params.exponent), CLOSURE_COLOR_ARRAY_PARAM(PhongRampClosure, colors, 8), CLOSURE_STRING_KEYPARAM(PhongRampClosure, label, "label"), CLOSURE_FINISH_PARAM(PhongRampClosure) diff --git a/intern/cycles/kernel/osl/emissive.cpp b/intern/cycles/kernel/osl/emissive.cpp index f91fd6e015c..3f13e08b302 100644 --- a/intern/cycles/kernel/osl/emissive.cpp +++ b/intern/cycles/kernel/osl/emissive.cpp @@ -36,7 +36,9 @@ #include "osl_closures.h" +#include "kernel_compat_cpu.h" #include "kernel_types.h" +#include "closure/alloc.h" #include "closure/emissive.h" CCL_NAMESPACE_BEGIN @@ -52,25 +54,10 @@ using namespace OSL; /// class GenericEmissiveClosure : public CClosurePrimitive { public: - GenericEmissiveClosure() : CClosurePrimitive(Emissive) { } - - Color3 eval(const Vec3 &Ng, const Vec3 &omega_out) const - { - float3 result = emissive_simple_eval(TO_FLOAT3(Ng), TO_FLOAT3(omega_out)); - return TO_COLOR3(result); - } - - void sample(const Vec3 &Ng, float randu, float randv, - Vec3 &omega_out, float &pdf) const - { - float3 omega_out_; - emissive_sample(TO_FLOAT3(Ng), randu, randv, &omega_out_, &pdf); - omega_out = TO_VEC3(omega_out_); - } - - float pdf(const Vec3 &Ng, const Vec3 &omega_out) const + void setup(ShaderData *sd, int /* path_flag */, float3 weight) { - return emissive_pdf(TO_FLOAT3(Ng), TO_FLOAT3(omega_out)); + closure_alloc(sd, sizeof(ShaderClosure), CLOSURE_EMISSION_ID, weight); + sd->flag |= SD_EMISSION; } }; diff --git a/intern/cycles/kernel/osl/osl_bssrdf.cpp b/intern/cycles/kernel/osl/osl_bssrdf.cpp index da4afb138f6..3614717e28c 100644 --- a/intern/cycles/kernel/osl/osl_bssrdf.cpp +++ b/intern/cycles/kernel/osl/osl_bssrdf.cpp @@ -30,17 +30,15 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <OpenImageIO/fmath.h> - #include <OSL/genclosure.h> #include "kernel_compat_cpu.h" -#include "osl_bssrdf.h" #include "osl_closures.h" #include "kernel_types.h" #include "kernel_montecarlo.h" +#include "closure/alloc.h" #include "closure/bsdf_diffuse.h" #include "closure/bssrdf.h" @@ -48,27 +46,83 @@ CCL_NAMESPACE_BEGIN using namespace OSL; +class CBSSRDFClosure : public CClosurePrimitive { +public: + Bssrdf params; + float3 radius; + float3 albedo; + + void alloc(ShaderData *sd, int path_flag, float3 weight, ClosureType type) + { + float sample_weight = fabsf(average(weight)); + + /* disable in case of diffuse ancestor, can't see it well then and + * adds considerably noise due to probabilities of continuing path + * getting lower and lower */ + if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR) { + radius = make_float3(0.0f, 0.0f, 0.0f); + } + + if(sample_weight > CLOSURE_WEIGHT_CUTOFF) { + /* sharpness */ + float sharpness = params.sharpness; + /* texture color blur */ + float texture_blur = params.texture_blur; + + /* create one closure per color channel */ + Bssrdf *bssrdf = bssrdf_alloc(sd, make_float3(weight.x, 0.0f, 0.0f)); + if(bssrdf) { + bssrdf->sample_weight = sample_weight; + bssrdf->radius = radius.x; + bssrdf->texture_blur = texture_blur; + bssrdf->albedo = albedo.x; + bssrdf->sharpness = sharpness; + bssrdf->N = params.N; + ccl_fetch(sd, flag) |= bssrdf_setup(bssrdf, (ClosureType)type); + } + + bssrdf = bssrdf_alloc(sd, make_float3(0.0f, weight.y, 0.0f)); + if(bssrdf) { + bssrdf->sample_weight = sample_weight; + bssrdf->radius = radius.y; + bssrdf->texture_blur = texture_blur; + bssrdf->albedo = albedo.y; + bssrdf->sharpness = sharpness; + bssrdf->N = params.N; + ccl_fetch(sd, flag) |= bssrdf_setup(bssrdf, (ClosureType)type); + } + + bssrdf = bssrdf_alloc(sd, make_float3(0.0f, 0.0f, weight.z)); + if(bssrdf) { + bssrdf->sample_weight = sample_weight; + bssrdf->radius = radius.z; + bssrdf->texture_blur = texture_blur; + bssrdf->albedo = albedo.z; + bssrdf->sharpness = sharpness; + bssrdf->N = params.N; + ccl_fetch(sd, flag) |= bssrdf_setup(bssrdf, (ClosureType)type); + } + } + } +}; + /* Cubic */ class CubicBSSRDFClosure : public CBSSRDFClosure { public: - CubicBSSRDFClosure() - {} - - void setup() + void setup(ShaderData *sd, int path_flag, float3 weight) { - sc.type = CLOSURE_BSSRDF_CUBIC_ID; - sc.data0 = fabsf(average(radius)); + alloc(sd, path_flag, weight, CLOSURE_BSSRDF_CUBIC_ID); } }; ClosureParam *closure_bssrdf_cubic_params() { static ClosureParam params[] = { - CLOSURE_FLOAT3_PARAM(CubicBSSRDFClosure, sc.N), + CLOSURE_FLOAT3_PARAM(CubicBSSRDFClosure, params.N), CLOSURE_FLOAT3_PARAM(CubicBSSRDFClosure, radius), - CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, sc.data1), - CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, sc.T.x), + CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, params.texture_blur), + CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, params.sharpness), CLOSURE_STRING_KEYPARAM(CubicBSSRDFClosure, label, "label"), CLOSURE_FINISH_PARAM(CubicBSSRDFClosure) }; @@ -81,22 +135,18 @@ CCLOSURE_PREPARE(closure_bssrdf_cubic_prepare, CubicBSSRDFClosure) class GaussianBSSRDFClosure : public CBSSRDFClosure { public: - GaussianBSSRDFClosure() - {} - - void setup() + void setup(ShaderData *sd, int path_flag, float3 weight) { - sc.type = CLOSURE_BSSRDF_GAUSSIAN_ID; - sc.data0 = fabsf(average(radius)); + alloc(sd, path_flag, weight, CLOSURE_BSSRDF_GAUSSIAN_ID); } }; ClosureParam *closure_bssrdf_gaussian_params() { static ClosureParam params[] = { - CLOSURE_FLOAT3_PARAM(GaussianBSSRDFClosure, sc.N), + CLOSURE_FLOAT3_PARAM(GaussianBSSRDFClosure, params.N), CLOSURE_FLOAT3_PARAM(GaussianBSSRDFClosure, radius), - CLOSURE_FLOAT_PARAM(GaussianBSSRDFClosure, sc.data1), + CLOSURE_FLOAT_PARAM(GaussianBSSRDFClosure, params.texture_blur), CLOSURE_STRING_KEYPARAM(GaussianBSSRDFClosure, label, "label"), CLOSURE_FINISH_PARAM(GaussianBSSRDFClosure) }; @@ -109,22 +159,18 @@ CCLOSURE_PREPARE(closure_bssrdf_gaussian_prepare, GaussianBSSRDFClosure) class BurleyBSSRDFClosure : public CBSSRDFClosure { public: - BurleyBSSRDFClosure() - {} - - void setup() + void setup(ShaderData *sd, int path_flag, float3 weight) { - sc.type = CLOSURE_BSSRDF_BURLEY_ID; - sc.data0 = fabsf(average(radius)); + alloc(sd, path_flag, weight, CLOSURE_BSSRDF_BURLEY_ID); } }; ClosureParam *closure_bssrdf_burley_params() { static ClosureParam params[] = { - CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, sc.N), + CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, params.N), CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, radius), - CLOSURE_FLOAT_PARAM(BurleyBSSRDFClosure, sc.data1), + CLOSURE_FLOAT_PARAM(BurleyBSSRDFClosure, params.texture_blur), CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, albedo), CLOSURE_STRING_KEYPARAM(BurleyBSSRDFClosure, label, "label"), CLOSURE_FINISH_PARAM(BurleyBSSRDFClosure) diff --git a/intern/cycles/kernel/osl/osl_bssrdf.h b/intern/cycles/kernel/osl/osl_bssrdf.h deleted file mode 100644 index d81ecade543..00000000000 --- a/intern/cycles/kernel/osl/osl_bssrdf.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Adapted from Open Shading Language with this license: - * - * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al. - * All Rights Reserved. - * - * Modifications Copyright 2011, Blender Foundation. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Sony Pictures Imageworks nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __OSL_BSSRDF_H__ -#define __OSL_BSSRDF_H__ - -#include <OSL/oslclosure.h> -#include <OSL/oslexec.h> -#include <OSL/genclosure.h> - -#include "osl_closures.h" - -#include "kernel_types.h" - -#include "util_types.h" - -CCL_NAMESPACE_BEGIN - -class CBSSRDFClosure : public CClosurePrimitive { -public: - ShaderClosure sc; - float3 radius; - float3 albedo; - - CBSSRDFClosure() : CClosurePrimitive(BSSRDF) { } - int scattering() const { return LABEL_DIFFUSE; } -}; - -CCL_NAMESPACE_END - -#endif /* __OSL_BSSRDF_H__ */ - diff --git a/intern/cycles/kernel/osl/osl_closures.cpp b/intern/cycles/kernel/osl/osl_closures.cpp index 02b1491489c..94de782dca0 100644 --- a/intern/cycles/kernel/osl/osl_closures.cpp +++ b/intern/cycles/kernel/osl/osl_closures.cpp @@ -46,6 +46,7 @@ #include "kernel_montecarlo.h" #include "kernel_random.h" +#include "closure/alloc.h" #include "closure/bsdf_util.h" #include "closure/bsdf_ashikhmin_velvet.h" #include "closure/bsdf_diffuse.h" @@ -66,112 +67,112 @@ using namespace OSL; /* BSDF class definitions */ -BSDF_CLOSURE_CLASS_BEGIN(Diffuse, diffuse, diffuse, LABEL_DIFFUSE) - CLOSURE_FLOAT3_PARAM(DiffuseClosure, sc.N), +BSDF_CLOSURE_CLASS_BEGIN(Diffuse, diffuse, DiffuseBsdf, LABEL_DIFFUSE) + CLOSURE_FLOAT3_PARAM(DiffuseClosure, params.N), BSDF_CLOSURE_CLASS_END(Diffuse, diffuse) -BSDF_CLOSURE_CLASS_BEGIN(Translucent, translucent, translucent, LABEL_DIFFUSE) - CLOSURE_FLOAT3_PARAM(TranslucentClosure, sc.N), +BSDF_CLOSURE_CLASS_BEGIN(Translucent, translucent, DiffuseBsdf, LABEL_DIFFUSE) + CLOSURE_FLOAT3_PARAM(TranslucentClosure, params.N), BSDF_CLOSURE_CLASS_END(Translucent, translucent) -BSDF_CLOSURE_CLASS_BEGIN(OrenNayar, oren_nayar, oren_nayar, LABEL_DIFFUSE) - CLOSURE_FLOAT3_PARAM(OrenNayarClosure, sc.N), - CLOSURE_FLOAT_PARAM(OrenNayarClosure, sc.data0), +BSDF_CLOSURE_CLASS_BEGIN(OrenNayar, oren_nayar, OrenNayarBsdf, LABEL_DIFFUSE) + CLOSURE_FLOAT3_PARAM(OrenNayarClosure, params.N), + CLOSURE_FLOAT_PARAM(OrenNayarClosure, params.roughness), BSDF_CLOSURE_CLASS_END(OrenNayar, oren_nayar) -BSDF_CLOSURE_CLASS_BEGIN(Reflection, reflection, reflection, LABEL_SINGULAR) - CLOSURE_FLOAT3_PARAM(ReflectionClosure, sc.N), +BSDF_CLOSURE_CLASS_BEGIN(Reflection, reflection, MicrofacetBsdf, LABEL_SINGULAR) + CLOSURE_FLOAT3_PARAM(ReflectionClosure, params.N), BSDF_CLOSURE_CLASS_END(Reflection, reflection) -BSDF_CLOSURE_CLASS_BEGIN(Refraction, refraction, refraction, LABEL_SINGULAR) - CLOSURE_FLOAT3_PARAM(RefractionClosure, sc.N), - CLOSURE_FLOAT_PARAM(RefractionClosure, sc.data0), +BSDF_CLOSURE_CLASS_BEGIN(Refraction, refraction, MicrofacetBsdf, LABEL_SINGULAR) + CLOSURE_FLOAT3_PARAM(RefractionClosure, params.N), + CLOSURE_FLOAT_PARAM(RefractionClosure, params.ior), BSDF_CLOSURE_CLASS_END(Refraction, refraction) -BSDF_CLOSURE_CLASS_BEGIN(Transparent, transparent, transparent, LABEL_SINGULAR) +BSDF_CLOSURE_CLASS_BEGIN(Transparent, transparent, ShaderClosure, LABEL_SINGULAR) BSDF_CLOSURE_CLASS_END(Transparent, transparent) -BSDF_CLOSURE_CLASS_BEGIN(AshikhminVelvet, ashikhmin_velvet, ashikhmin_velvet, LABEL_DIFFUSE) - CLOSURE_FLOAT3_PARAM(AshikhminVelvetClosure, sc.N), - CLOSURE_FLOAT_PARAM(AshikhminVelvetClosure, sc.data0), +BSDF_CLOSURE_CLASS_BEGIN(AshikhminVelvet, ashikhmin_velvet, VelvetBsdf, LABEL_DIFFUSE) + CLOSURE_FLOAT3_PARAM(AshikhminVelvetClosure, params.N), + CLOSURE_FLOAT_PARAM(AshikhminVelvetClosure, params.sigma), BSDF_CLOSURE_CLASS_END(AshikhminVelvet, ashikhmin_velvet) -BSDF_CLOSURE_CLASS_BEGIN(AshikhminShirley, ashikhmin_shirley_aniso, ashikhmin_shirley, LABEL_GLOSSY|LABEL_REFLECT) - CLOSURE_FLOAT3_PARAM(AshikhminShirleyClosure, sc.N), - CLOSURE_FLOAT3_PARAM(AshikhminShirleyClosure, sc.T), - CLOSURE_FLOAT_PARAM(AshikhminShirleyClosure, sc.data0), - CLOSURE_FLOAT_PARAM(AshikhminShirleyClosure, sc.data1), +BSDF_CLOSURE_CLASS_BEGIN(AshikhminShirley, ashikhmin_shirley_aniso, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT) + CLOSURE_FLOAT3_PARAM(AshikhminShirleyClosure, params.N), + CLOSURE_FLOAT3_PARAM(AshikhminShirleyClosure, params.T), + CLOSURE_FLOAT_PARAM(AshikhminShirleyClosure, params.alpha_x), + CLOSURE_FLOAT_PARAM(AshikhminShirleyClosure, params.alpha_y), BSDF_CLOSURE_CLASS_END(AshikhminShirley, ashikhmin_shirley_aniso) -BSDF_CLOSURE_CLASS_BEGIN(DiffuseToon, diffuse_toon, diffuse_toon, LABEL_DIFFUSE) - CLOSURE_FLOAT3_PARAM(DiffuseToonClosure, sc.N), - CLOSURE_FLOAT_PARAM(DiffuseToonClosure, sc.data0), - CLOSURE_FLOAT_PARAM(DiffuseToonClosure, sc.data1), +BSDF_CLOSURE_CLASS_BEGIN(DiffuseToon, diffuse_toon, ToonBsdf, LABEL_DIFFUSE) + CLOSURE_FLOAT3_PARAM(DiffuseToonClosure, params.N), + CLOSURE_FLOAT_PARAM(DiffuseToonClosure, params.size), + CLOSURE_FLOAT_PARAM(DiffuseToonClosure, params.smooth), BSDF_CLOSURE_CLASS_END(DiffuseToon, diffuse_toon) -BSDF_CLOSURE_CLASS_BEGIN(GlossyToon, glossy_toon, glossy_toon, LABEL_GLOSSY) - CLOSURE_FLOAT3_PARAM(GlossyToonClosure, sc.N), - CLOSURE_FLOAT_PARAM(GlossyToonClosure, sc.data0), - CLOSURE_FLOAT_PARAM(GlossyToonClosure, sc.data1), +BSDF_CLOSURE_CLASS_BEGIN(GlossyToon, glossy_toon, ToonBsdf, LABEL_GLOSSY) + CLOSURE_FLOAT3_PARAM(GlossyToonClosure, params.N), + CLOSURE_FLOAT_PARAM(GlossyToonClosure, params.size), + CLOSURE_FLOAT_PARAM(GlossyToonClosure, params.smooth), BSDF_CLOSURE_CLASS_END(GlossyToon, glossy_toon) -BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGX, microfacet_ggx, microfacet_ggx, LABEL_GLOSSY|LABEL_REFLECT) - CLOSURE_FLOAT3_PARAM(MicrofacetGGXClosure, sc.N), - CLOSURE_FLOAT_PARAM(MicrofacetGGXClosure, sc.data0), +BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGX, microfacet_ggx, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT) + CLOSURE_FLOAT3_PARAM(MicrofacetGGXClosure, params.N), + CLOSURE_FLOAT_PARAM(MicrofacetGGXClosure, params.alpha_x), BSDF_CLOSURE_CLASS_END(MicrofacetGGX, microfacet_ggx) -BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGXAniso, microfacet_ggx_aniso, microfacet_ggx, LABEL_GLOSSY|LABEL_REFLECT) - CLOSURE_FLOAT3_PARAM(MicrofacetGGXAnisoClosure, sc.N), - CLOSURE_FLOAT3_PARAM(MicrofacetGGXAnisoClosure, sc.T), - CLOSURE_FLOAT_PARAM(MicrofacetGGXAnisoClosure, sc.data0), - CLOSURE_FLOAT_PARAM(MicrofacetGGXAnisoClosure, sc.data1), +BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGXAniso, microfacet_ggx_aniso, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT) + CLOSURE_FLOAT3_PARAM(MicrofacetGGXAnisoClosure, params.N), + CLOSURE_FLOAT3_PARAM(MicrofacetGGXAnisoClosure, params.T), + CLOSURE_FLOAT_PARAM(MicrofacetGGXAnisoClosure, params.alpha_x), + CLOSURE_FLOAT_PARAM(MicrofacetGGXAnisoClosure, params.alpha_y), BSDF_CLOSURE_CLASS_END(MicrofacetGGXAniso, microfacet_ggx_aniso) -BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmann, microfacet_beckmann, microfacet_beckmann, LABEL_GLOSSY|LABEL_REFLECT) - CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannClosure, sc.N), - CLOSURE_FLOAT_PARAM(MicrofacetBeckmannClosure, sc.data0), +BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmann, microfacet_beckmann, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT) + CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannClosure, params.N), + CLOSURE_FLOAT_PARAM(MicrofacetBeckmannClosure, params.alpha_x), BSDF_CLOSURE_CLASS_END(MicrofacetBeckmann, microfacet_beckmann) -BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmannAniso, microfacet_beckmann_aniso, microfacet_beckmann, LABEL_GLOSSY|LABEL_REFLECT) - CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannAnisoClosure, sc.N), - CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannAnisoClosure, sc.T), - CLOSURE_FLOAT_PARAM(MicrofacetBeckmannAnisoClosure, sc.data0), - CLOSURE_FLOAT_PARAM(MicrofacetBeckmannAnisoClosure, sc.data1), +BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmannAniso, microfacet_beckmann_aniso, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT) + CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannAnisoClosure, params.N), + CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannAnisoClosure, params.T), + CLOSURE_FLOAT_PARAM(MicrofacetBeckmannAnisoClosure, params.alpha_x), + CLOSURE_FLOAT_PARAM(MicrofacetBeckmannAnisoClosure, params.alpha_y), BSDF_CLOSURE_CLASS_END(MicrofacetBeckmannAniso, microfacet_beckmann_aniso) -BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGXRefraction, microfacet_ggx_refraction, microfacet_ggx, LABEL_GLOSSY|LABEL_TRANSMIT) - CLOSURE_FLOAT3_PARAM(MicrofacetGGXRefractionClosure, sc.N), - CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, sc.data0), - CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, sc.data2), +BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGXRefraction, microfacet_ggx_refraction, MicrofacetBsdf, LABEL_GLOSSY|LABEL_TRANSMIT) + CLOSURE_FLOAT3_PARAM(MicrofacetGGXRefractionClosure, params.N), + CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, params.alpha_x), + CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, params.ior), BSDF_CLOSURE_CLASS_END(MicrofacetGGXRefraction, microfacet_ggx_refraction) -BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction, microfacet_beckmann, LABEL_GLOSSY|LABEL_TRANSMIT) - CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannRefractionClosure, sc.N), - CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, sc.data0), - CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, sc.data2), +BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction, MicrofacetBsdf, LABEL_GLOSSY|LABEL_TRANSMIT) + CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannRefractionClosure, params.N), + CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, params.alpha_x), + CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, params.ior), BSDF_CLOSURE_CLASS_END(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction) -BSDF_CLOSURE_CLASS_BEGIN(HairReflection, hair_reflection, hair_reflection, LABEL_GLOSSY) - CLOSURE_FLOAT3_PARAM(HairReflectionClosure, sc.N), - CLOSURE_FLOAT_PARAM(HairReflectionClosure, sc.data0), - CLOSURE_FLOAT_PARAM(HairReflectionClosure, sc.data1), - CLOSURE_FLOAT3_PARAM(HairReflectionClosure, sc.T), - CLOSURE_FLOAT_PARAM(HairReflectionClosure, sc.data2), +BSDF_CLOSURE_CLASS_BEGIN(HairReflection, hair_reflection, HairBsdf, LABEL_GLOSSY) + CLOSURE_FLOAT3_PARAM(HairReflectionClosure, unused), + CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.roughness1), + CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.roughness2), + CLOSURE_FLOAT3_PARAM(HairReflectionClosure, params.T), + CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.offset), BSDF_CLOSURE_CLASS_END(HairReflection, hair_reflection) -BSDF_CLOSURE_CLASS_BEGIN(HairTransmission, hair_transmission, hair_transmission, LABEL_GLOSSY) - CLOSURE_FLOAT3_PARAM(HairTransmissionClosure, sc.N), - CLOSURE_FLOAT_PARAM(HairTransmissionClosure, sc.data0), - CLOSURE_FLOAT_PARAM(HairTransmissionClosure, sc.data1), - CLOSURE_FLOAT3_PARAM(HairReflectionClosure, sc.T), - CLOSURE_FLOAT_PARAM(HairReflectionClosure, sc.data2), +BSDF_CLOSURE_CLASS_BEGIN(HairTransmission, hair_transmission, HairBsdf, LABEL_GLOSSY) + CLOSURE_FLOAT3_PARAM(HairTransmissionClosure, unused), + CLOSURE_FLOAT_PARAM(HairTransmissionClosure, params.roughness1), + CLOSURE_FLOAT_PARAM(HairTransmissionClosure, params.roughness2), + CLOSURE_FLOAT3_PARAM(HairReflectionClosure, params.T), + CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.offset), BSDF_CLOSURE_CLASS_END(HairTransmission, hair_transmission) -VOLUME_CLOSURE_CLASS_BEGIN(VolumeHenyeyGreenstein, henyey_greenstein, LABEL_VOLUME_SCATTER) - CLOSURE_FLOAT_PARAM(VolumeHenyeyGreensteinClosure, sc.data0), +VOLUME_CLOSURE_CLASS_BEGIN(VolumeHenyeyGreenstein, henyey_greenstein, HenyeyGreensteinVolume, LABEL_VOLUME_SCATTER) + CLOSURE_FLOAT_PARAM(VolumeHenyeyGreensteinClosure, params.g), VOLUME_CLOSURE_CLASS_END(VolumeHenyeyGreenstein, henyey_greenstein) -VOLUME_CLOSURE_CLASS_BEGIN(VolumeAbsorption, absorption, LABEL_SINGULAR) +VOLUME_CLOSURE_CLASS_BEGIN(VolumeAbsorption, absorption, ShaderClosure, LABEL_SINGULAR) VOLUME_CLOSURE_CLASS_END(VolumeAbsorption, absorption) /* Registration */ @@ -258,69 +259,64 @@ void OSLShader::register_closures(OSLShadingSystem *ss_) volume_absorption_params(), volume_absorption_prepare); } -/* Multiscattering GGX closures */ - -class MicrofacetMultiClosure : public CBSDFClosure { -public: - float3 color; - - /* Technically, the MultiGGX Glass closure may also transmit. - * However, since this is set statically and only used for caustic flags, this is probably as good as it gets. */ - MicrofacetMultiClosure() : CBSDFClosure(LABEL_GLOSSY|LABEL_REFLECT) - { - } +/* BSDF Closure */ - void setup() - { - sc.prim = NULL; - sc.custom1 = color.x; - sc.custom2 = color.y; - sc.custom3 = color.z; +bool CBSDFClosure::skip(const ShaderData *sd, int path_flag, int scattering) +{ + /* caustic options */ + if((scattering & LABEL_GLOSSY) && (path_flag & PATH_RAY_DIFFUSE)) { + KernelGlobals *kg = sd->osl_globals; + + if((!kernel_data.integrator.caustics_reflective && (scattering & LABEL_REFLECT)) || + (!kernel_data.integrator.caustics_refractive && (scattering & LABEL_TRANSMIT))) + { + return true; + } } - void blur(float roughness) - { - } + return false; +} - float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float& pdf) const - { - pdf = 0.0f; - return make_float3(0.0f, 0.0f, 0.0f); - } +/* Multiscattering GGX closures */ - float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float& pdf) const - { - pdf = 0.0f; - return make_float3(0.0f, 0.0f, 0.0f); - } +class MicrofacetMultiClosure : public CBSDFClosure { +public: + MicrofacetBsdf params; + float3 color; - int sample(const float3 &Ng, - const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy, - float randu, float randv, - float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy, - float &pdf, float3 &eval) const + MicrofacetBsdf *alloc(ShaderData *sd, int path_flag, float3 weight) { - pdf = 0; - return LABEL_NONE; + /* Technically, the MultiGGX Glass closure may also transmit. However, + * since this is set statically and only used for caustic flags, this + * is probably as good as it gets. */ + if(!skip(sd, path_flag, LABEL_GLOSSY|LABEL_REFLECT)) { + MicrofacetBsdf *bsdf = (MicrofacetBsdf*)bsdf_alloc_osl(sd, sizeof(MicrofacetBsdf), weight, ¶ms); + MicrofacetExtra *extra = (MicrofacetExtra*)closure_alloc_extra(sd, sizeof(MicrofacetExtra)); + if(bsdf && extra) { + bsdf->extra = extra; + bsdf->extra->color = color; + return bsdf; + } + } + + return NULL; } }; class MicrofacetMultiGGXClosure : public MicrofacetMultiClosure { public: - MicrofacetMultiGGXClosure() : MicrofacetMultiClosure() {} - - void setup() + void setup(ShaderData *sd, int path_flag, float3 weight) { - MicrofacetMultiClosure::setup(); - m_shaderdata_flag = bsdf_microfacet_multi_ggx_setup(&sc); + MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); + sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_setup(bsdf) : 0; } }; ClosureParam *closure_bsdf_microfacet_multi_ggx_params() { static ClosureParam params[] = { - CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, sc.N), - CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, sc.data0), + CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.N), + CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_x), CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, color), CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXClosure, label, "label"), CLOSURE_FINISH_PARAM(MicrofacetMultiGGXClosure) @@ -331,22 +327,20 @@ CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_prepare, MicrofacetMultiGGXCl class MicrofacetMultiGGXAnisoClosure : public MicrofacetMultiClosure { public: - MicrofacetMultiGGXAnisoClosure() : MicrofacetMultiClosure() {} - - void setup() + void setup(ShaderData *sd, int path_flag, float3 weight) { - MicrofacetMultiClosure::setup(); - m_shaderdata_flag = bsdf_microfacet_multi_ggx_aniso_setup(&sc); + MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); + sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_aniso_setup(bsdf) : 0; } }; ClosureParam *closure_bsdf_microfacet_multi_ggx_aniso_params() { static ClosureParam params[] = { - CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, sc.N), - CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, sc.T), - CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, sc.data0), - CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, sc.data1), + CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.N), + CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.T), + CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_x), + CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_y), CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, color), CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXClosure, label, "label"), CLOSURE_FINISH_PARAM(MicrofacetMultiGGXClosure) @@ -359,19 +353,19 @@ class MicrofacetMultiGGXGlassClosure : public MicrofacetMultiClosure { public: MicrofacetMultiGGXGlassClosure() : MicrofacetMultiClosure() {} - void setup() + void setup(ShaderData *sd, int path_flag, float3 weight) { - MicrofacetMultiClosure::setup(); - m_shaderdata_flag = bsdf_microfacet_multi_ggx_glass_setup(&sc); + MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); + sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_glass_setup(bsdf) : 0; } }; ClosureParam *closure_bsdf_microfacet_multi_ggx_glass_params() { static ClosureParam params[] = { - CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, sc.N), - CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, sc.data0), - CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, sc.data2), + CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.N), + CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_x), + CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.ior), CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, color), CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXClosure, label, "label"), CLOSURE_FINISH_PARAM(MicrofacetMultiGGXClosure) diff --git a/intern/cycles/kernel/osl/osl_closures.h b/intern/cycles/kernel/osl/osl_closures.h index c5a1a29b6af..cd7b33703ff 100644 --- a/intern/cycles/kernel/osl/osl_closures.h +++ b/intern/cycles/kernel/osl/osl_closures.h @@ -90,21 +90,7 @@ void name(RendererServices *, int id, void *data) \ class CClosurePrimitive { public: - enum Category { - BSDF, ///< Reflective and/or transmissive surface - BSSRDF, ///< Sub-surface light transfer - Emissive, ///< Light emission - Background, ///< Background emission - Volume, ///< Volume scattering - Holdout, ///< Holdout from alpha - AmbientOcclusion, ///< Ambient occlusion - }; - - CClosurePrimitive (Category category_) : category (category_) {} - virtual ~CClosurePrimitive() {} - virtual void setup() {} - - Category category; + virtual void setup(ShaderData *sd, int path_flag, float3 weight) = 0; OSL::ustring label; }; @@ -113,68 +99,22 @@ public: class CBSDFClosure : public CClosurePrimitive { public: - ShaderClosure sc; - - CBSDFClosure(int scattering) : CClosurePrimitive(BSDF), - m_scattering_label(scattering), m_shaderdata_flag(0) - {} - - int scattering() const { return m_scattering_label; } - int shaderdata_flag() const { return m_shaderdata_flag; } - - virtual void blur(float roughness) = 0; - virtual float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float &pdf) const = 0; - virtual float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float &pdf) const = 0; - - virtual int sample(const float3 &Ng, - const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy, - float randu, float randv, - float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy, - float &pdf, float3 &eval) const = 0; - -protected: - int m_scattering_label; - int m_shaderdata_flag; + bool skip(const ShaderData *sd, int path_flag, int scattering); }; -#define BSDF_CLOSURE_CLASS_BEGIN(Upper, lower, svmlower, TYPE) \ +#define BSDF_CLOSURE_CLASS_BEGIN(Upper, lower, structname, TYPE) \ \ class Upper##Closure : public CBSDFClosure { \ public: \ - Upper##Closure() : CBSDFClosure(TYPE) \ - { \ - } \ + structname params; \ + float3 unused; \ \ - void setup() \ + void setup(ShaderData *sd, int path_flag, float3 weight) \ { \ - sc.prim = NULL; \ - m_shaderdata_flag = bsdf_##lower##_setup(&sc); \ - } \ -\ - void blur(float roughness) \ - { \ - } \ -\ - float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float& pdf) const \ - { \ - pdf = 0.0f; \ - return make_float3(0.0f, 0.0f, 0.0f); \ - } \ -\ - float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float& pdf) const \ - { \ - pdf = 0.0f; \ - return make_float3(0.0f, 0.0f, 0.0f); \ - } \ -\ - int sample(const float3 &Ng, \ - const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy, \ - float randu, float randv, \ - float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy, \ - float &pdf, float3 &eval) const \ - { \ - pdf = 0; \ - return LABEL_NONE; \ + if(!skip(sd, path_flag, TYPE)) { \ + structname *bsdf = (structname*)bsdf_alloc_osl(sd, sizeof(structname), weight, ¶ms); \ + sd->flag |= (bsdf) ? bsdf_##lower##_setup(bsdf) : 0; \ + } \ } \ }; \ \ @@ -193,36 +133,18 @@ static ClosureParam *bsdf_##lower##_params() \ \ CCLOSURE_PREPARE_STATIC(bsdf_##lower##_prepare, Upper##Closure) - /* Volume */ -class CVolumeClosure : public CClosurePrimitive { -public: - ShaderClosure sc; - - CVolumeClosure(int scattering) : CClosurePrimitive(Volume), - m_scattering_label(scattering), m_shaderdata_flag(0) - {} - ~CVolumeClosure() { } - - int scattering() const { return m_scattering_label; } - int shaderdata_flag() const { return m_shaderdata_flag; } - -protected: - int m_scattering_label; - int m_shaderdata_flag; -}; - -#define VOLUME_CLOSURE_CLASS_BEGIN(Upper, lower, TYPE) \ +#define VOLUME_CLOSURE_CLASS_BEGIN(Upper, lower, structname, TYPE) \ \ -class Upper##Closure : public CVolumeClosure { \ +class Upper##Closure : public CBSDFClosure { \ public: \ - Upper##Closure() : CVolumeClosure(TYPE) {} \ + structname params; \ \ - void setup() \ + void setup(ShaderData *sd, int path_flag, float3 weight) \ { \ - sc.prim = NULL; \ - m_shaderdata_flag = volume_##lower##_setup(&sc); \ + structname *volume = (structname*)bsdf_alloc_osl(sd, sizeof(structname), weight, ¶ms); \ + sd->flag |= (volume) ? volume_##lower##_setup(volume) : 0; \ } \ }; \ \ diff --git a/intern/cycles/kernel/osl/osl_shader.cpp b/intern/cycles/kernel/osl/osl_shader.cpp index 6cde7419e10..de02ec8f691 100644 --- a/intern/cycles/kernel/osl/osl_shader.cpp +++ b/intern/cycles/kernel/osl/osl_shader.cpp @@ -23,10 +23,6 @@ #include "geom/geom_object.h" -#include "closure/bsdf_diffuse.h" -#include "closure/bssrdf.h" - -#include "osl_bssrdf.h" #include "osl_closures.h" #include "osl_globals.h" #include "osl_services.h" @@ -141,8 +137,10 @@ static void shaderdata_to_shaderglobals(KernelGlobals *kg, ShaderData *sd, PathS /* Surface */ -static void flatten_surface_closure_tree(ShaderData *sd, int path_flag, - const OSL::ClosureColor *closure, float3 weight = make_float3(1.0f, 1.0f, 1.0f)) +static void flatten_surface_closure_tree(ShaderData *sd, + int path_flag, + const OSL::ClosureColor *closure, + float3 weight = make_float3(1.0f, 1.0f, 1.0f)) { /* OSL gives us a closure tree, we flatten it into arrays per * closure type, for evaluation, sampling, etc later on. */ @@ -164,164 +162,10 @@ static void flatten_surface_closure_tree(ShaderData *sd, int path_flag, CClosurePrimitive *prim = (CClosurePrimitive *)comp->data(); if(prim) { - ShaderClosure sc; - #ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS weight = weight*TO_FLOAT3(comp->w); #endif - sc.weight = weight; - - prim->setup(); - - switch(prim->category) { - case CClosurePrimitive::BSDF: { - CBSDFClosure *bsdf = (CBSDFClosure *)prim; - int scattering = bsdf->scattering(); - int shaderdata_flag = bsdf->shaderdata_flag(); - - /* caustic options */ - if((scattering & LABEL_GLOSSY) && (path_flag & PATH_RAY_DIFFUSE)) { - KernelGlobals *kg = sd->osl_globals; - - if((!kernel_data.integrator.caustics_reflective && (scattering & LABEL_REFLECT)) || - (!kernel_data.integrator.caustics_refractive && (scattering & LABEL_TRANSMIT))) - { - return; - } - } - - /* sample weight */ - float sample_weight = fabsf(average(weight)); - - sc.sample_weight = sample_weight; - - sc.type = bsdf->sc.type; - sc.N = bsdf->sc.N; - sc.T = bsdf->sc.T; - sc.data0 = bsdf->sc.data0; - sc.data1 = bsdf->sc.data1; - sc.data2 = bsdf->sc.data2; - sc.prim = bsdf->sc.prim; - if(shaderdata_flag & SD_BSDF_HAS_CUSTOM) { - sc.custom1 = bsdf->sc.custom1; - sc.custom2 = bsdf->sc.custom2; - sc.custom3 = bsdf->sc.custom3; - } - - /* add */ - if(sc.sample_weight > CLOSURE_WEIGHT_CUTOFF && sd->num_closure < MAX_CLOSURE) { - sd->closure[sd->num_closure++] = sc; - sd->flag |= shaderdata_flag; - } - break; - } - case CClosurePrimitive::Emissive: { - /* sample weight */ - float sample_weight = fabsf(average(weight)); - - sc.sample_weight = sample_weight; - sc.type = CLOSURE_EMISSION_ID; - sc.data0 = 0.0f; - sc.data1 = 0.0f; - sc.data2 = 0.0f; - sc.prim = NULL; - - /* flag */ - if(sd->num_closure < MAX_CLOSURE) { - sd->closure[sd->num_closure++] = sc; - sd->flag |= SD_EMISSION; - } - break; - } - case CClosurePrimitive::AmbientOcclusion: { - /* sample weight */ - float sample_weight = fabsf(average(weight)); - - sc.sample_weight = sample_weight; - sc.type = CLOSURE_AMBIENT_OCCLUSION_ID; - sc.data0 = 0.0f; - sc.data1 = 0.0f; - sc.data2 = 0.0f; - sc.prim = NULL; - - if(sd->num_closure < MAX_CLOSURE) { - sd->closure[sd->num_closure++] = sc; - sd->flag |= SD_AO; - } - break; - } - case CClosurePrimitive::Holdout: { - sc.sample_weight = 0.0f; - sc.type = CLOSURE_HOLDOUT_ID; - sc.data0 = 0.0f; - sc.data1 = 0.0f; - sc.data2 = 0.0f; - sc.prim = NULL; - - if(sd->num_closure < MAX_CLOSURE) { - sd->closure[sd->num_closure++] = sc; - sd->flag |= SD_HOLDOUT; - } - break; - } - case CClosurePrimitive::BSSRDF: { - CBSSRDFClosure *bssrdf = (CBSSRDFClosure *)prim; - float sample_weight = fabsf(average(weight)); - - if(sample_weight > CLOSURE_WEIGHT_CUTOFF && sd->num_closure+2 < MAX_CLOSURE) { - sc.sample_weight = sample_weight; - - sc.type = bssrdf->sc.type; - sc.N = bssrdf->sc.N; - sc.data1 = bssrdf->sc.data1; - sc.T.x = bssrdf->sc.T.x; - sc.prim = NULL; - - /* disable in case of diffuse ancestor, can't see it well then and - * adds considerably noise due to probabilities of continuing path - * getting lower and lower */ - if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR) - bssrdf->radius = make_float3(0.0f, 0.0f, 0.0f); - - float3 albedo = - (bssrdf->sc.type == CLOSURE_BSSRDF_BURLEY_ID) - ? bssrdf->albedo - : make_float3(0.0f, 0.0f, 0.0f); - - /* create one closure for each color channel */ - if(fabsf(weight.x) > 0.0f) { - sc.weight = make_float3(weight.x, 0.0f, 0.0f); - sc.data0 = bssrdf->radius.x; - sc.data1 = 0.0f; - sc.data2 = albedo.x; - sd->flag |= bssrdf_setup(&sc, sc.type); - sd->closure[sd->num_closure++] = sc; - } - - if(fabsf(weight.y) > 0.0f) { - sc.weight = make_float3(0.0f, weight.y, 0.0f); - sc.data0 = bssrdf->radius.y; - sc.data1 = 0.0f; - sc.data2 = albedo.y; - sd->flag |= bssrdf_setup(&sc, sc.type); - sd->closure[sd->num_closure++] = sc; - } - - if(fabsf(weight.z) > 0.0f) { - sc.weight = make_float3(0.0f, 0.0f, weight.z); - sc.data0 = bssrdf->radius.z; - sc.data1 = 0.0f; - sc.data2 = albedo.z; - sd->flag |= bssrdf_setup(&sc, sc.type); - sd->closure[sd->num_closure++] = sc; - } - } - break; - } - case CClosurePrimitive::Background: - case CClosurePrimitive::Volume: - break; /* not relevant */ - } + prim->setup(sd, path_flag, weight); } break; } @@ -351,7 +195,9 @@ void OSLShader::eval_surface(KernelGlobals *kg, ShaderData *sd, PathState *state /* Background */ -static float3 flatten_background_closure_tree(const OSL::ClosureColor *closure) +static void flatten_background_closure_tree(ShaderData *sd, + const OSL::ClosureColor *closure, + float3 weight = make_float3(1.0f, 1.0f, 1.0f)) { /* OSL gives us a closure tree, if we are shading for background there * is only one supported closure type at the moment, which has no evaluation @@ -360,32 +206,32 @@ static float3 flatten_background_closure_tree(const OSL::ClosureColor *closure) switch(closure->id) { case OSL::ClosureColor::MUL: { OSL::ClosureMul *mul = (OSL::ClosureMul *)closure; - - return TO_FLOAT3(mul->weight) * flatten_background_closure_tree(mul->closure); + flatten_background_closure_tree(sd, mul->closure, weight * TO_FLOAT3(mul->weight)); + break; } case OSL::ClosureColor::ADD: { OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure; - return flatten_background_closure_tree(add->closureA) + - flatten_background_closure_tree(add->closureB); + flatten_background_closure_tree(sd, add->closureA, weight); + flatten_background_closure_tree(sd, add->closureB, weight); + break; } default: { OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure; CClosurePrimitive *prim = (CClosurePrimitive *)comp->data(); - if(prim && prim->category == CClosurePrimitive::Background) + if(prim) { #ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS - return TO_FLOAT3(comp->w); -#else - return make_float3(1.0f, 1.0f, 1.0f); + weight = weight*TO_FLOAT3(comp->w); #endif + prim->setup(sd, 0, weight); + } + break; } } - - return make_float3(0.0f, 0.0f, 0.0f); } -float3 OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag, ShaderContext ctx) +void OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag, ShaderContext ctx) { /* setup shader globals from shader data */ OSLThreadData *tdata = kg->osl_tdata; @@ -402,15 +248,14 @@ float3 OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, PathState * /* return background color immediately */ if(globals->Ci) - return flatten_background_closure_tree(globals->Ci); - - return make_float3(0.0f, 0.0f, 0.0f); + flatten_background_closure_tree(sd, globals->Ci); } /* Volume */ static void flatten_volume_closure_tree(ShaderData *sd, - const OSL::ClosureColor *closure, float3 weight = make_float3(1.0f, 1.0f, 1.0f)) + const OSL::ClosureColor *closure, + float3 weight = make_float3(1.0f, 1.0f, 1.0f)) { /* OSL gives us a closure tree, we flatten it into arrays per * closure type, for evaluation, sampling, etc later on. */ @@ -432,60 +277,10 @@ static void flatten_volume_closure_tree(ShaderData *sd, CClosurePrimitive *prim = (CClosurePrimitive *)comp->data(); if(prim) { - ShaderClosure sc; - #ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS weight = weight*TO_FLOAT3(comp->w); #endif - sc.weight = weight; - - prim->setup(); - - switch(prim->category) { - case CClosurePrimitive::Volume: { - CVolumeClosure *volume = (CVolumeClosure *)prim; - /* sample weight */ - float sample_weight = fabsf(average(weight)); - - sc.sample_weight = sample_weight; - sc.type = volume->sc.type; - sc.data0 = volume->sc.data0; - sc.data1 = volume->sc.data1; - - /* add */ - if((sc.sample_weight > CLOSURE_WEIGHT_CUTOFF) && - (sd->num_closure < MAX_CLOSURE)) - { - sd->closure[sd->num_closure++] = sc; - sd->flag |= volume->shaderdata_flag(); - } - break; - } - case CClosurePrimitive::Emissive: { - /* sample weight */ - float sample_weight = fabsf(average(weight)); - - sc.sample_weight = sample_weight; - sc.type = CLOSURE_EMISSION_ID; - sc.data0 = 0.0f; - sc.data1 = 0.0f; - sc.prim = NULL; - - /* flag */ - if(sd->num_closure < MAX_CLOSURE) { - sd->closure[sd->num_closure++] = sc; - sd->flag |= SD_EMISSION; - } - break; - } - case CClosurePrimitive::Holdout: - break; /* not implemented */ - case CClosurePrimitive::Background: - case CClosurePrimitive::BSDF: - case CClosurePrimitive::BSSRDF: - case CClosurePrimitive::AmbientOcclusion: - break; /* not relevant */ - } + prim->setup(sd, 0, weight); } } } @@ -537,40 +332,6 @@ void OSLShader::eval_displacement(KernelGlobals *kg, ShaderData *sd, ShaderConte sd->P = TO_FLOAT3(globals->P); } -/* BSDF Closure */ - -int OSLShader::bsdf_sample(const ShaderData *sd, const ShaderClosure *sc, float randu, float randv, float3& eval, float3& omega_in, differential3& domega_in, float& pdf) -{ - CBSDFClosure *sample_bsdf = (CBSDFClosure *)sc->prim; - - pdf = 0.0f; - - return sample_bsdf->sample(sd->Ng, - sd->I, sd->dI.dx, sd->dI.dy, - randu, randv, - omega_in, domega_in.dx, domega_in.dy, - pdf, eval); -} - -float3 OSLShader::bsdf_eval(const ShaderData *sd, const ShaderClosure *sc, const float3& omega_in, float& pdf) -{ - CBSDFClosure *bsdf = (CBSDFClosure *)sc->prim; - float3 bsdf_eval; - - if(dot(sd->Ng, omega_in) >= 0.0f) - bsdf_eval = bsdf->eval_reflect(sd->I, omega_in, pdf); - else - bsdf_eval = bsdf->eval_transmit(sd->I, omega_in, pdf); - - return bsdf_eval; -} - -void OSLShader::bsdf_blur(ShaderClosure *sc, float roughness) -{ - CBSDFClosure *bsdf = (CBSDFClosure *)sc->prim; - bsdf->blur(roughness); -} - /* Attributes */ int OSLShader::find_attribute(KernelGlobals *kg, const ShaderData *sd, uint id, AttributeElement *elem) diff --git a/intern/cycles/kernel/osl/osl_shader.h b/intern/cycles/kernel/osl/osl_shader.h index 7d26cd40da5..a185b8b8c05 100644 --- a/intern/cycles/kernel/osl/osl_shader.h +++ b/intern/cycles/kernel/osl/osl_shader.h @@ -54,18 +54,10 @@ public: /* eval */ static void eval_surface(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag, ShaderContext ctx); - static float3 eval_background(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag, ShaderContext ctx); + static void eval_background(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag, ShaderContext ctx); static void eval_volume(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag, ShaderContext ctx); static void eval_displacement(KernelGlobals *kg, ShaderData *sd, ShaderContext ctx); - /* sample & eval */ - static int bsdf_sample(const ShaderData *sd, const ShaderClosure *sc, - float randu, float randv, - float3& eval, float3& omega_in, differential3& domega_in, float& pdf); - static float3 bsdf_eval(const ShaderData *sd, const ShaderClosure *sc, - const float3& omega_in, float& pdf); - static void bsdf_blur(ShaderClosure *sc, float roughness); - /* attributes */ static int find_attribute(KernelGlobals *kg, const ShaderData *sd, uint id, AttributeElement *elem); }; |