diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2012-12-15 14:18:42 +0400 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2012-12-15 14:18:42 +0400 |
commit | 06888b7bebf66c5b05dd1a8106e35c57458252c0 (patch) | |
tree | ab9a5c6412438c8988a4e8116432ecd3b5705df3 /intern/cycles/kernel/osl | |
parent | 35dd893c368548b5d9d7bb56a1b9db07c71165d2 (diff) |
Cycles OSL minor optimizations: recycle shading context, don't do memory
allocations for trace data, avoid some virtual function calls. Only helps
a few percentages.
Diffstat (limited to 'intern/cycles/kernel/osl')
-rw-r--r-- | intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp | 4 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/bsdf_phong_ramp.cpp | 4 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/emissive.cpp | 2 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_closures.cpp | 30 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_closures.h | 12 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_globals.h | 11 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_services.cpp | 53 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_services.h | 7 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_shader.cpp | 144 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_shader.h | 12 |
10 files changed, 121 insertions, 158 deletions
diff --git a/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp b/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp index 7189f99a822..a320bea118a 100644 --- a/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp +++ b/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp @@ -55,7 +55,7 @@ public: void setup() { - sc.N = TO_FLOAT3(N); + sc.prim = this; m_shaderdata_flag = bsdf_diffuse_ramp_setup(&sc); for(int i = 0; i < 8; i++) @@ -101,7 +101,7 @@ public: ClosureParam *closure_bsdf_diffuse_ramp_params() { static ClosureParam params[] = { - CLOSURE_VECTOR_PARAM(DiffuseRampClosure, N), + CLOSURE_FLOAT3_PARAM(DiffuseRampClosure, sc.N), CLOSURE_COLOR_ARRAY_PARAM(DiffuseRampClosure, colors, 8), CLOSURE_STRING_KEYPARAM("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 fb144be7e50..ef656ee7d5f 100644 --- a/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp +++ b/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp @@ -54,7 +54,7 @@ public: void setup() { - sc.N = TO_FLOAT3(N); + sc.prim = this; m_shaderdata_flag = bsdf_phong_ramp_setup(&sc); for(int i = 0; i < 8; i++) @@ -100,7 +100,7 @@ public: ClosureParam *closure_bsdf_phong_ramp_params() { static ClosureParam params[] = { - CLOSURE_VECTOR_PARAM(PhongRampClosure, N), + CLOSURE_FLOAT3_PARAM(PhongRampClosure, sc.N), CLOSURE_FLOAT_PARAM(PhongRampClosure, sc.data0), CLOSURE_COLOR_ARRAY_PARAM(PhongRampClosure, colors, 8), CLOSURE_STRING_KEYPARAM("label"), diff --git a/intern/cycles/kernel/osl/emissive.cpp b/intern/cycles/kernel/osl/emissive.cpp index 37e3e37c00a..7d9fa81dbdd 100644 --- a/intern/cycles/kernel/osl/emissive.cpp +++ b/intern/cycles/kernel/osl/emissive.cpp @@ -65,7 +65,7 @@ public: Color3 eval(const Vec3 &Ng, const Vec3 &omega_out) const { - float3 result = emissive_eval(TO_FLOAT3(Ng), TO_FLOAT3(omega_out)); + float3 result = emissive_simple_eval(TO_FLOAT3(Ng), TO_FLOAT3(omega_out)); return TO_COLOR3(result); } diff --git a/intern/cycles/kernel/osl/osl_closures.cpp b/intern/cycles/kernel/osl/osl_closures.cpp index 1f71fde549e..fa9f770d6ed 100644 --- a/intern/cycles/kernel/osl/osl_closures.cpp +++ b/intern/cycles/kernel/osl/osl_closures.cpp @@ -43,7 +43,7 @@ #include "kernel_types.h" #include "kernel_montecarlo.h" -#include "closure/bsdf.h" +#include "closure/bsdf_util.h" #include "closure/bsdf_ashikhmin_velvet.h" #include "closure/bsdf_diffuse.h" #include "closure/bsdf_microfacet.h" @@ -62,34 +62,34 @@ using namespace OSL; /* BSDF class definitions */ BSDF_CLOSURE_CLASS_BEGIN(Diffuse, diffuse, diffuse, LABEL_DIFFUSE) - CLOSURE_VECTOR_PARAM(DiffuseClosure, N), + CLOSURE_FLOAT3_PARAM(DiffuseClosure, sc.N), BSDF_CLOSURE_CLASS_END(Diffuse, diffuse) BSDF_CLOSURE_CLASS_BEGIN(Translucent, translucent, translucent, LABEL_DIFFUSE) - CLOSURE_VECTOR_PARAM(TranslucentClosure, N), + CLOSURE_FLOAT3_PARAM(TranslucentClosure, sc.N), BSDF_CLOSURE_CLASS_END(Translucent, translucent) BSDF_CLOSURE_CLASS_BEGIN(OrenNayar, oren_nayar, oren_nayar, LABEL_DIFFUSE) - CLOSURE_VECTOR_PARAM(OrenNayarClosure, N), + CLOSURE_FLOAT3_PARAM(OrenNayarClosure, sc.N), CLOSURE_FLOAT_PARAM(OrenNayarClosure, sc.data0), BSDF_CLOSURE_CLASS_END(OrenNayar, oren_nayar) BSDF_CLOSURE_CLASS_BEGIN(Reflection, reflection, reflection, LABEL_SINGULAR) - CLOSURE_VECTOR_PARAM(ReflectionClosure, N), + CLOSURE_FLOAT3_PARAM(ReflectionClosure, sc.N), BSDF_CLOSURE_CLASS_END(Reflection, reflection) BSDF_CLOSURE_CLASS_BEGIN(Refraction, refraction, refraction, LABEL_SINGULAR) - CLOSURE_VECTOR_PARAM(RefractionClosure, N), + CLOSURE_FLOAT3_PARAM(RefractionClosure, sc.N), CLOSURE_FLOAT_PARAM(RefractionClosure, sc.data0), BSDF_CLOSURE_CLASS_END(Refraction, refraction) BSDF_CLOSURE_CLASS_BEGIN(WestinBackscatter, westin_backscatter, westin_backscatter, LABEL_GLOSSY) - CLOSURE_VECTOR_PARAM(WestinBackscatterClosure, N), + CLOSURE_FLOAT3_PARAM(WestinBackscatterClosure, sc.N), CLOSURE_FLOAT_PARAM(WestinBackscatterClosure, sc.data0), BSDF_CLOSURE_CLASS_END(WestinBackscatter, westin_backscatter) BSDF_CLOSURE_CLASS_BEGIN(WestinSheen, westin_sheen, westin_sheen, LABEL_DIFFUSE) - CLOSURE_VECTOR_PARAM(WestinSheenClosure, N), + CLOSURE_FLOAT3_PARAM(WestinSheenClosure, sc.N), CLOSURE_FLOAT_PARAM(WestinSheenClosure, sc.data0), BSDF_CLOSURE_CLASS_END(WestinSheen, westin_sheen) @@ -97,35 +97,35 @@ BSDF_CLOSURE_CLASS_BEGIN(Transparent, transparent, transparent, LABEL_SINGULAR) BSDF_CLOSURE_CLASS_END(Transparent, transparent) BSDF_CLOSURE_CLASS_BEGIN(AshikhminVelvet, ashikhmin_velvet, ashikhmin_velvet, LABEL_DIFFUSE) - CLOSURE_VECTOR_PARAM(AshikhminVelvetClosure, N), + CLOSURE_FLOAT3_PARAM(AshikhminVelvetClosure, sc.N), CLOSURE_FLOAT_PARAM(AshikhminVelvetClosure, sc.data0), BSDF_CLOSURE_CLASS_END(AshikhminVelvet, ashikhmin_velvet) BSDF_CLOSURE_CLASS_BEGIN(Ward, ward, ward, LABEL_GLOSSY) - CLOSURE_VECTOR_PARAM(WardClosure, N), - CLOSURE_VECTOR_PARAM(WardClosure, T), + CLOSURE_FLOAT3_PARAM(WardClosure, sc.N), + CLOSURE_FLOAT3_PARAM(WardClosure, sc.T), CLOSURE_FLOAT_PARAM(WardClosure, sc.data0), CLOSURE_FLOAT_PARAM(WardClosure, sc.data1), BSDF_CLOSURE_CLASS_END(Ward, ward) BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGX, microfacet_ggx, microfacet_ggx, LABEL_GLOSSY) - CLOSURE_VECTOR_PARAM(MicrofacetGGXClosure, N), + CLOSURE_FLOAT3_PARAM(MicrofacetGGXClosure, sc.N), CLOSURE_FLOAT_PARAM(MicrofacetGGXClosure, sc.data0), BSDF_CLOSURE_CLASS_END(MicrofacetGGX, microfacet_ggx) BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmann, microfacet_beckmann, microfacet_beckmann, LABEL_GLOSSY) - CLOSURE_VECTOR_PARAM(MicrofacetBeckmannClosure, N), + CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannClosure, sc.N), CLOSURE_FLOAT_PARAM(MicrofacetBeckmannClosure, sc.data0), BSDF_CLOSURE_CLASS_END(MicrofacetBeckmann, microfacet_beckmann) BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGXRefraction, microfacet_ggx_refraction, microfacet_ggx, LABEL_GLOSSY) - CLOSURE_VECTOR_PARAM(MicrofacetGGXRefractionClosure, N), + CLOSURE_FLOAT3_PARAM(MicrofacetGGXRefractionClosure, sc.N), CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, sc.data0), CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, sc.data1), BSDF_CLOSURE_CLASS_END(MicrofacetGGXRefraction, microfacet_ggx_refraction) BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction, microfacet_beckmann, LABEL_GLOSSY) - CLOSURE_VECTOR_PARAM(MicrofacetBeckmannRefractionClosure, N), + CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannRefractionClosure, sc.N), CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, sc.data0), CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, sc.data1), BSDF_CLOSURE_CLASS_END(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction) diff --git a/intern/cycles/kernel/osl/osl_closures.h b/intern/cycles/kernel/osl/osl_closures.h index 5cd333c806e..fe37c974e95 100644 --- a/intern/cycles/kernel/osl/osl_closures.h +++ b/intern/cycles/kernel/osl/osl_closures.h @@ -70,8 +70,11 @@ void name(RendererServices *, int id, void *data) \ #define CLOSURE_PREPARE_STATIC(name, classname) static CLOSURE_PREPARE(name, classname) -#define TO_VEC3(v) (*(OSL::Vec3 *)&(v)) -#define TO_COLOR3(v) (*(OSL::Color3 *)&(v)) +#define CLOSURE_FLOAT3_PARAM(st, fld) \ + { TypeDesc::TypeVector, reckless_offsetof(st, fld), NULL, sizeof(OSL::Vec3) } + +#define TO_VEC3(v) OSL::Vec3(v.x, v.y, v.z) +#define TO_COLOR3(v) OSL::Color3(v.x, v.y, v.z) #define TO_FLOAT3(v) make_float3(v[0], v[1], v[2]) /* BSDF */ @@ -79,7 +82,6 @@ void name(RendererServices *, int id, void *data) \ class CBSDFClosure : public OSL::ClosurePrimitive { public: ShaderClosure sc; - OSL::Vec3 N, T; CBSDFClosure(int scattering) : OSL::ClosurePrimitive(BSDF), m_scattering_label(scattering), m_shaderdata_flag(0) { } @@ -87,7 +89,6 @@ public: int scattering() const { return m_scattering_label; } int shaderdata_flag() const { return m_shaderdata_flag; } - ClosureType shaderclosure_type() const { return sc.type; } virtual void blur(float roughness) = 0; virtual float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float &pdf) const = 0; @@ -114,8 +115,7 @@ public: \ \ void setup() \ { \ - sc.N = TO_FLOAT3(N); \ - sc.T = TO_FLOAT3(T); \ + sc.prim = NULL; \ m_shaderdata_flag = bsdf_##lower##_setup(&sc); \ } \ \ diff --git a/intern/cycles/kernel/osl/osl_globals.h b/intern/cycles/kernel/osl/osl_globals.h index 1a2a210de88..fb569117698 100644 --- a/intern/cycles/kernel/osl/osl_globals.h +++ b/intern/cycles/kernel/osl/osl_globals.h @@ -75,10 +75,21 @@ struct OSLGlobals { vector<ustring> object_names; }; +/* trace() call result */ +struct OSLTraceData { + Ray ray; + Intersection isect; + ShaderData sd; + bool setup; + bool init; +}; + /* thread key for thread specific data lookup */ struct OSLThreadData { OSL::ShaderGlobals globals; OSL::PerThreadInfo *thread_info; + OSLTraceData tracedata; + OSL::ShadingContext *context[SHADER_CONTEXT_NUM]; }; CCL_NAMESPACE_END diff --git a/intern/cycles/kernel/osl/osl_services.cpp b/intern/cycles/kernel/osl/osl_services.cpp index b584a54b1b7..d6e52e28c62 100644 --- a/intern/cycles/kernel/osl/osl_services.cpp +++ b/intern/cycles/kernel/osl/osl_services.cpp @@ -47,7 +47,7 @@ CCL_NAMESPACE_BEGIN /* RenderServices implementation */ -#define TO_MATRIX44(m) (*(OSL::Matrix44 *)&(m)) +#define COPY_MATRIX44(m1, m2) memcpy(m1, m2, sizeof(*m2)) /* static ustrings */ ustring OSLRenderServices::u_distance("distance"); @@ -121,7 +121,7 @@ bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, OSL::TransformationPtr Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM); #endif tfm = transform_transpose(tfm); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } @@ -151,7 +151,7 @@ bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::Transform Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM); #endif itfm = transform_transpose(itfm); - result = TO_MATRIX44(itfm); + COPY_MATRIX44(&result, &itfm); return true; } @@ -166,22 +166,22 @@ bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, ustring from, float ti if (from == u_ndc) { Transform tfm = transform_transpose(transform_quick_inverse(kernel_data.cam.worldtondc)); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (from == u_raster) { Transform tfm = transform_transpose(kernel_data.cam.rastertoworld); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (from == u_screen) { Transform tfm = transform_transpose(kernel_data.cam.screentoworld); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (from == u_camera) { Transform tfm = transform_transpose(kernel_data.cam.cameratoworld); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } @@ -194,22 +194,22 @@ bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, ustring to, fl if (to == u_ndc) { Transform tfm = transform_transpose(kernel_data.cam.worldtondc); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (to == u_raster) { Transform tfm = transform_transpose(kernel_data.cam.worldtoraster); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (to == u_screen) { Transform tfm = transform_transpose(kernel_data.cam.worldtoscreen); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (to == u_camera) { Transform tfm = transform_transpose(kernel_data.cam.worldtocamera); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } @@ -232,7 +232,7 @@ bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, OSL::TransformationPtr Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM); #endif tfm = transform_transpose(tfm); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } @@ -257,7 +257,7 @@ bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::Transform Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM); #endif tfm = transform_transpose(tfm); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } @@ -272,22 +272,22 @@ bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, ustring from) if (from == u_ndc) { Transform tfm = transform_transpose(transform_quick_inverse(kernel_data.cam.worldtondc)); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (from == u_raster) { Transform tfm = transform_transpose(kernel_data.cam.rastertoworld); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (from == u_screen) { Transform tfm = transform_transpose(kernel_data.cam.screentoworld); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (from == u_camera) { Transform tfm = transform_transpose(kernel_data.cam.cameratoworld); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } @@ -300,22 +300,22 @@ bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, ustring to) if (to == u_ndc) { Transform tfm = transform_transpose(kernel_data.cam.worldtondc); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (to == u_raster) { Transform tfm = transform_transpose(kernel_data.cam.worldtoraster); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (to == u_screen) { Transform tfm = transform_transpose(kernel_data.cam.worldtoscreen); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } else if (to == u_camera) { Transform tfm = transform_transpose(kernel_data.cam.worldtocamera); - result = TO_MATRIX44(tfm); + COPY_MATRIX44(&result, &tfm); return true; } @@ -815,13 +815,10 @@ bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg, ray.dD.dy = TO_FLOAT3(dRdy); /* allocate trace data */ - TraceData *tracedata = new TraceData(); + OSLTraceData *tracedata = (OSLTraceData*)sg->tracedata; tracedata->ray = ray; tracedata->setup = false; - - if(sg->tracedata) - delete (TraceData*)sg->tracedata; - sg->tracedata = tracedata; + tracedata->init = true; /* raytrace */ return scene_intersect(kernel_globals, &ray, ~0, &tracedata->isect); @@ -831,9 +828,9 @@ bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg, bool OSLRenderServices::getmessage(OSL::ShaderGlobals *sg, ustring source, ustring name, TypeDesc type, void *val, bool derivatives) { - TraceData *tracedata = (TraceData*)sg->tracedata; + OSLTraceData *tracedata = (OSLTraceData*)sg->tracedata; - if(source == u_trace && tracedata) { + if(source == u_trace && tracedata->init) { if(name == u_hit) { return set_attribute_int((tracedata->isect.prim != ~0), type, derivatives, val); } diff --git a/intern/cycles/kernel/osl/osl_services.h b/intern/cycles/kernel/osl/osl_services.h index e687700b383..cd4a4163209 100644 --- a/intern/cycles/kernel/osl/osl_services.h +++ b/intern/cycles/kernel/osl/osl_services.h @@ -106,13 +106,6 @@ public: static bool get_object_standard_attribute(KernelGlobals *kg, ShaderData *sd, ustring name, TypeDesc type, bool derivatives, void *val); - struct TraceData { - Ray ray; - Intersection isect; - ShaderData sd; - bool setup; - }; - static ustring u_distance; static ustring u_index; static ustring u_camera; diff --git a/intern/cycles/kernel/osl/osl_shader.cpp b/intern/cycles/kernel/osl/osl_shader.cpp index 32712b25e92..f5c04b6755e 100644 --- a/intern/cycles/kernel/osl/osl_shader.cpp +++ b/intern/cycles/kernel/osl/osl_shader.cpp @@ -51,8 +51,13 @@ void OSLShader::thread_init(KernelGlobals *kg, KernelGlobals *kernel_globals, OS OSLThreadData *tdata = new OSLThreadData(); memset(&tdata->globals, 0, sizeof(OSL::ShaderGlobals)); + tdata->globals.tracedata = &tdata->tracedata; + tdata->globals.flipHandedness = false; tdata->thread_info = ss->create_thread_info(); + for(int i = 0; i < SHADER_CONTEXT_NUM; i++) + tdata->context[i] = ss->get_context(tdata->thread_info); + kg->osl_ss = (OSLShadingSystem*)ss; kg->osl_tdata = tdata; } @@ -65,6 +70,9 @@ void OSLShader::thread_free(KernelGlobals *kg) OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss; OSLThreadData *tdata = kg->osl_tdata; + for(int i = 0; i < SHADER_CONTEXT_NUM; i++) + ss->release_context(tdata->context[i]); + ss->destroy_thread_info(tdata->thread_info); delete tdata; @@ -77,8 +85,10 @@ void OSLShader::thread_free(KernelGlobals *kg) /* Globals */ static void shaderdata_to_shaderglobals(KernelGlobals *kg, ShaderData *sd, - int path_flag, OSL::ShaderGlobals *globals) + int path_flag, OSLThreadData *tdata) { + OSL::ShaderGlobals *globals = &tdata->globals; + /* copy from shader data to shader globals */ globals->P = TO_VEC3(sd->P); globals->dPdx = TO_VEC3(sd->dP.dx); @@ -100,15 +110,11 @@ static void shaderdata_to_shaderglobals(KernelGlobals *kg, ShaderData *sd, globals->time = sd->time; /* booleans */ - globals->raytype = path_flag; /* todo: add our own ray types */ + globals->raytype = path_flag; globals->backfacing = (sd->flag & SD_BACKFACING); - /* don't know yet if we need this */ - globals->flipHandedness = false; - /* shader data to be used in services callbacks */ globals->renderstate = sd; - globals->tracedata = NULL; /* hacky, we leave it to services to fetch actual object matrix */ globals->shader2common = sd; @@ -116,6 +122,9 @@ static void shaderdata_to_shaderglobals(KernelGlobals *kg, ShaderData *sd, /* must be set to NULL before execute */ globals->Ci = NULL; + + /* clear trace data */ + tdata->tracedata.init = false; } /* Surface */ @@ -132,14 +141,10 @@ static void flatten_surface_closure_tree(ShaderData *sd, bool no_glossy, if (prim) { ShaderClosure sc; - sc.prim = prim; sc.weight = weight; switch (prim->category()) { case OSL::ClosurePrimitive::BSDF: { - if (sd->num_closure == MAX_CLOSURE) - return; - CBSDFClosure *bsdf = (CBSDFClosure *)prim; int scattering = bsdf->scattering(); @@ -151,8 +156,13 @@ static void flatten_surface_closure_tree(ShaderData *sd, bool no_glossy, float sample_weight = fabsf(average(weight)); sc.sample_weight = sample_weight; - sc.type = bsdf->shaderclosure_type(); - sc.N = bsdf->sc.N; /* needed for AO */ + + 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.prim = bsdf->sc.prim; /* add */ if(sc.sample_weight > 1e-5f && sd->num_closure < MAX_CLOSURE) { @@ -170,6 +180,7 @@ static void flatten_surface_closure_tree(ShaderData *sd, bool no_glossy, sc.sample_weight = sample_weight; sc.type = CLOSURE_EMISSION_ID; + sc.prim = NULL; /* flag */ if(sd->num_closure < MAX_CLOSURE) { @@ -179,14 +190,12 @@ static void flatten_surface_closure_tree(ShaderData *sd, bool no_glossy, break; } case AmbientOcclusion: { - if (sd->num_closure == MAX_CLOSURE) - return; - /* sample weight */ float sample_weight = fabsf(average(weight)); sc.sample_weight = sample_weight; sc.type = CLOSURE_AMBIENT_OCCLUSION_ID; + sc.prim = NULL; if(sd->num_closure < MAX_CLOSURE) { sd->closure[sd->num_closure++] = sc; @@ -195,11 +204,9 @@ static void flatten_surface_closure_tree(ShaderData *sd, bool no_glossy, break; } case OSL::ClosurePrimitive::Holdout: - if (sd->num_closure == MAX_CLOSURE) - return; - sc.sample_weight = 0.0f; sc.type = CLOSURE_HOLDOUT_ID; + sc.prim = NULL; if(sd->num_closure < MAX_CLOSURE) { sd->closure[sd->num_closure++] = sc; @@ -226,26 +233,20 @@ static void flatten_surface_closure_tree(ShaderData *sd, bool no_glossy, } } -void OSLShader::eval_surface(KernelGlobals *kg, ShaderData *sd, float randb, int path_flag) +void OSLShader::eval_surface(KernelGlobals *kg, ShaderData *sd, float randb, int path_flag, ShaderContext ctx) { - /* gather pointers */ - OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss; - OSLThreadData *tdata = kg->osl_tdata; - OSL::ShaderGlobals *globals = &tdata->globals; - OSL::ShadingContext *ctx = (OSL::ShadingContext *)sd->osl_ctx; - /* setup shader globals from shader data */ - shaderdata_to_shaderglobals(kg, sd, path_flag, globals); + OSLThreadData *tdata = kg->osl_tdata; + shaderdata_to_shaderglobals(kg, sd, path_flag, tdata); /* execute shader for this point */ + OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss; + OSL::ShaderGlobals *globals = &tdata->globals; + OSL::ShadingContext *octx = tdata->context[(int)ctx]; int shader = sd->shader & SHADER_MASK; if (kg->osl->surface_state[shader]) - ss->execute(*ctx, *(kg->osl->surface_state[shader]), *globals); - - /* free trace data */ - if(globals->tracedata) - delete (OSLRenderServices::TraceData*)globals->tracedata; + ss->execute(*octx, *(kg->osl->surface_state[shader]), *globals); /* flatten closure tree */ sd->num_closure = 0; @@ -287,24 +288,19 @@ static float3 flatten_background_closure_tree(const OSL::ClosureColor *closure) return make_float3(0.0f, 0.0f, 0.0f); } -float3 OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, int path_flag) +float3 OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, int path_flag, ShaderContext ctx) { - /* gather pointers */ - OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss; - OSLThreadData *tdata = kg->osl_tdata; - OSL::ShaderGlobals *globals = &tdata->globals; - OSL::ShadingContext *ctx = (OSL::ShadingContext *)sd->osl_ctx; - /* setup shader globals from shader data */ - shaderdata_to_shaderglobals(kg, sd, path_flag, globals); + OSLThreadData *tdata = kg->osl_tdata; + shaderdata_to_shaderglobals(kg, sd, path_flag, tdata); /* execute shader for this point */ - if (kg->osl->background_state) - ss->execute(*ctx, *(kg->osl->background_state), *globals); + OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss; + OSL::ShaderGlobals *globals = &tdata->globals; + OSL::ShadingContext *octx = tdata->context[(int)ctx]; - /* free trace data */ - if(globals->tracedata) - delete (OSLRenderServices::TraceData*)globals->tracedata; + if (kg->osl->background_state) + ss->execute(*octx, *(kg->osl->background_state), *globals); /* return background color immediately */ if (globals->Ci) @@ -327,19 +323,16 @@ static void flatten_volume_closure_tree(ShaderData *sd, if (prim) { ShaderClosure sc; - sc.prim = prim; sc.weight = weight; switch (prim->category()) { case OSL::ClosurePrimitive::Volume: { - if (sd->num_closure == MAX_CLOSURE) - return; - /* sample weight */ float sample_weight = fabsf(average(weight)); sc.sample_weight = sample_weight; sc.type = CLOSURE_VOLUME_ID; + sc.prim = NULL; /* add */ if(sc.sample_weight > 1e-5f && sd->num_closure < MAX_CLOSURE) @@ -368,26 +361,20 @@ static void flatten_volume_closure_tree(ShaderData *sd, } } -void OSLShader::eval_volume(KernelGlobals *kg, ShaderData *sd, float randb, int path_flag) +void OSLShader::eval_volume(KernelGlobals *kg, ShaderData *sd, float randb, int path_flag, ShaderContext ctx) { - /* gather pointers */ - OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss; - OSLThreadData *tdata = kg->osl_tdata; - OSL::ShaderGlobals *globals = &tdata->globals; - OSL::ShadingContext *ctx = (OSL::ShadingContext *)sd->osl_ctx; - /* setup shader globals from shader data */ - shaderdata_to_shaderglobals(kg, sd, path_flag, globals); + OSLThreadData *tdata = kg->osl_tdata; + shaderdata_to_shaderglobals(kg, sd, path_flag, tdata); /* execute shader */ + OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss; + OSL::ShaderGlobals *globals = &tdata->globals; + OSL::ShadingContext *octx = tdata->context[(int)ctx]; int shader = sd->shader & SHADER_MASK; if (kg->osl->volume_state[shader]) - ss->execute(*ctx, *(kg->osl->volume_state[shader]), *globals); - - /* free trace data */ - if(globals->tracedata) - delete (OSLRenderServices::TraceData*)globals->tracedata; + ss->execute(*octx, *(kg->osl->volume_state[shader]), *globals); if (globals->Ci) flatten_volume_closure_tree(sd, globals->Ci); @@ -395,46 +382,25 @@ void OSLShader::eval_volume(KernelGlobals *kg, ShaderData *sd, float randb, int /* Displacement */ -void OSLShader::eval_displacement(KernelGlobals *kg, ShaderData *sd) +void OSLShader::eval_displacement(KernelGlobals *kg, ShaderData *sd, ShaderContext ctx) { - /* gather pointers */ - OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss; - OSLThreadData *tdata = kg->osl_tdata; - OSL::ShaderGlobals *globals = &tdata->globals; - OSL::ShadingContext *ctx = (OSL::ShadingContext *)sd->osl_ctx; - /* setup shader globals from shader data */ - shaderdata_to_shaderglobals(kg, sd, 0, globals); + OSLThreadData *tdata = kg->osl_tdata; + shaderdata_to_shaderglobals(kg, sd, 0, tdata); /* execute shader */ + OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss; + OSL::ShaderGlobals *globals = &tdata->globals; + OSL::ShadingContext *octx = tdata->context[(int)ctx]; int shader = sd->shader & SHADER_MASK; if (kg->osl->displacement_state[shader]) - ss->execute(*ctx, *(kg->osl->displacement_state[shader]), *globals); - - /* free trace data */ - if(globals->tracedata) - delete (OSLRenderServices::TraceData*)globals->tracedata; + ss->execute(*octx, *(kg->osl->displacement_state[shader]), *globals); /* get back position */ sd->P = TO_FLOAT3(globals->P); } -void OSLShader::init(KernelGlobals *kg, ShaderData *sd) -{ - OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss; - OSLThreadData *tdata = kg->osl_tdata; - - sd->osl_ctx = ss->get_context(tdata->thread_info); -} - -void OSLShader::release(KernelGlobals *kg, ShaderData *sd) -{ - OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss; - - ss->release_context((OSL::ShadingContext *)sd->osl_ctx); -} - /* 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) diff --git a/intern/cycles/kernel/osl/osl_shader.h b/intern/cycles/kernel/osl/osl_shader.h index e614f240dc1..2e46a2de42c 100644 --- a/intern/cycles/kernel/osl/osl_shader.h +++ b/intern/cycles/kernel/osl/osl_shader.h @@ -55,10 +55,10 @@ public: static void thread_free(KernelGlobals *kg); /* eval */ - static void eval_surface(KernelGlobals *kg, ShaderData *sd, float randb, int path_flag); - static float3 eval_background(KernelGlobals *kg, ShaderData *sd, int path_flag); - static void eval_volume(KernelGlobals *kg, ShaderData *sd, float randb, int path_flag); - static void eval_displacement(KernelGlobals *kg, ShaderData *sd); + static void eval_surface(KernelGlobals *kg, ShaderData *sd, float randb, int path_flag, ShaderContext ctx); + static float3 eval_background(KernelGlobals *kg, ShaderData *sd, int path_flag, ShaderContext ctx); + static void eval_volume(KernelGlobals *kg, ShaderData *sd, float randb, 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, @@ -73,10 +73,6 @@ public: static float3 volume_eval_phase(const ShaderClosure *sc, const float3 omega_in, const float3 omega_out); - /* release */ - static void init(KernelGlobals *kg, ShaderData *sd); - static void release(KernelGlobals *kg, ShaderData *sd); - /* attributes */ static int find_attribute(KernelGlobals *kg, const ShaderData *sd, uint id); }; |