Revert "Use one context per OSL thread. Not sure if this actually works, but the...
authorLukas Toenne <lukas.toenne@googlemail.com>
Tue, 4 Sep 2012 17:28:36 +0000 (17:28 +0000)
committerLukas Toenne <lukas.toenne@googlemail.com>
Tue, 4 Sep 2012 17:28:36 +0000 (17:28 +0000)
This does not actually work: The context must not be shared between threads, but using the same context between different samples actually seems to prevent OSL from switching between shaders. The proper solution would be to ensure memory pooling works correctly.

This reverts commit 69f87e69258d6266dcb20f09f7e3d4021e663432.

intern/cycles/kernel/kernel_emission.h
intern/cycles/kernel/kernel_path.h
intern/cycles/kernel/kernel_shader.h
intern/cycles/kernel/osl/osl_globals.h
intern/cycles/kernel/osl/osl_shader.cpp
intern/cycles/kernel/osl/osl_shader.h

index 9032d7d4a4109aedd15df0f41ed1ad121654079b..53d53b4bedd498b71aed4e21aaa56dec566fca45 100644 (file)
@@ -54,6 +54,8 @@ __device float3 direct_emissive_eval(KernelGlobals *kg, float rando,
                        eval = make_float3(0.0f, 0.0f, 0.0f);
        }
 
+       shader_release(kg, &sd);
+
        return eval;
 }
 
@@ -162,6 +164,7 @@ __device float3 indirect_background(KernelGlobals *kg, Ray *ray, int path_flag,
        ShaderData sd;
        shader_setup_from_background(kg, &sd, ray);
        float3 L = shader_eval_background(kg, &sd, path_flag);
+       shader_release(kg, &sd);
 
 #ifdef __BACKGROUND_MIS__
        /* check if background light exists or if we should skip pdf */
index 8e3a0c6e628aef8c06a7f8dba3ba470ba340ab47..6c81fa4cfcad6d5e91be5b4baed2d1cf7c7f11be 100644 (file)
@@ -395,6 +395,8 @@ __device float4 kernel_path_progressive(KernelGlobals *kg, RNG *rng, int sample,
                label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
                        &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
 
+               shader_release(kg, &sd);
+
                if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
                        break;
 
@@ -567,6 +569,8 @@ __device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, int sample, Ray
                label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
                        &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
 
+               shader_release(kg, &sd);
+
                if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
                        break;
 
@@ -858,6 +862,7 @@ __device float4 kernel_path_non_progressive(KernelGlobals *kg, RNG *rng, int sam
 
                /* continue in case of transparency */
                throughput *= shader_bsdf_transparency(kg, &sd);
+               shader_release(kg, &sd);
 
                if(is_zero(throughput))
                        break;
index e3e45e62451dec8a3d5fc762fde905e7eae02db8..5ab3c2c25450718eab7835bedacca46efbe9d6a3 100644 (file)
@@ -773,5 +773,15 @@ __device void shader_merge_closures(KernelGlobals *kg, ShaderData *sd)
 }
 #endif
 
+/* Free ShaderData */
+
+__device void shader_release(KernelGlobals *kg, ShaderData *sd)
+{
+#ifdef __OSL__
+       if (kernel_osl_use(kg))
+               OSLShader::release(kg, sd);
+#endif
+}
+
 CCL_NAMESPACE_END
 
index 17cee67517793d8c2ede3507c95c6906802bc6b5..075c68824e639ce581c650091ac3d2284d33617f 100644 (file)
@@ -64,7 +64,6 @@ struct OSLGlobals {
        struct ThreadData {
                OSL::ShaderGlobals globals;
                OSL::PerThreadInfo *thread_info;
-               OSL::ShadingContext *ctx;
        };
 
        static tls_ptr(ThreadData, thread_data);
index 7d6732b9e56b3ca762d51f9cda080d7abbac2e05..a7c21e7b595bcfbc3f0b001c2257258bb278c4be 100644 (file)
@@ -42,7 +42,6 @@ void OSLShader::thread_init(KernelGlobals *kg)
 
        memset(&tdata->globals, 0, sizeof(OSL::ShaderGlobals));
        tdata->thread_info = ss->create_thread_info();
-       tdata->ctx = ss->get_context(tdata->thread_info);
 
        tls_set(kg->osl.thread_data, tdata);
 
@@ -55,7 +54,6 @@ void OSLShader::thread_free(KernelGlobals *kg)
 
        OSLGlobals::ThreadData *tdata = tls_get(OSLGlobals::ThreadData, kg->osl.thread_data);
 
-       ss->release_context(tdata->ctx);
        ss->destroy_thread_info(tdata->thread_info);
 
        delete tdata;
@@ -206,7 +204,7 @@ void OSLShader::eval_surface(KernelGlobals *kg, ShaderData *sd, float randb, int
        OSL::ShadingSystem *ss = kg->osl.ss;
        OSLGlobals::ThreadData *tdata = tls_get(OSLGlobals::ThreadData, kg->osl.thread_data);
        OSL::ShaderGlobals *globals = &tdata->globals;
-       OSL::ShadingContext *ctx = tdata->ctx;
+       OSL::ShadingContext *ctx = ss->get_context(tdata->thread_info);
 
        /* setup shader globals from shader data */
        sd->osl_ctx = ctx;
@@ -264,7 +262,7 @@ float3 OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, int path_fl
        OSL::ShadingSystem *ss = kg->osl.ss;
        OSLGlobals::ThreadData *tdata = tls_get(OSLGlobals::ThreadData, kg->osl.thread_data);
        OSL::ShaderGlobals *globals = &tdata->globals;
-       OSL::ShadingContext *ctx = tdata->ctx;
+       OSL::ShadingContext *ctx = ss->get_context(tdata->thread_info);
 
        /* setup shader globals from shader data */
        sd->osl_ctx = ctx;
@@ -341,7 +339,7 @@ void OSLShader::eval_volume(KernelGlobals *kg, ShaderData *sd, float randb, int
        OSL::ShadingSystem *ss = kg->osl.ss;
        OSLGlobals::ThreadData *tdata = tls_get(OSLGlobals::ThreadData, kg->osl.thread_data);
        OSL::ShaderGlobals *globals = &tdata->globals;
-       OSL::ShadingContext *ctx = tdata->ctx;
+       OSL::ShadingContext *ctx = ss->get_context(tdata->thread_info);
 
        /* setup shader globals from shader data */
        sd->osl_ctx = ctx;
@@ -365,7 +363,7 @@ void OSLShader::eval_displacement(KernelGlobals *kg, ShaderData *sd)
        OSL::ShadingSystem *ss = kg->osl.ss;
        OSLGlobals::ThreadData *tdata = tls_get(OSLGlobals::ThreadData, kg->osl.thread_data);
        OSL::ShaderGlobals *globals = &tdata->globals;
-       OSL::ShadingContext *ctx = tdata->ctx;
+       OSL::ShadingContext *ctx = ss->get_context(tdata->thread_info);
 
        /* setup shader globals from shader data */
        sd->osl_ctx = ctx;
@@ -381,6 +379,13 @@ void OSLShader::eval_displacement(KernelGlobals *kg, ShaderData *sd)
        sd->P = TO_FLOAT3(globals->P);
 }
 
+void OSLShader::release(KernelGlobals *kg, const ShaderData *sd)
+{
+       OSL::ShadingSystem *ss = 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)
index a888335aad6458a923c89d117715031ae6cccfd7..815557ed203a6d1d218b15547f18aca1334574c2 100644 (file)
@@ -77,6 +77,9 @@ public:
 
        static float3 volume_eval_phase(const ShaderData *sd, const ShaderClosure *sc,
                                        const float3 omega_in, const float3 omega_out);
+
+       /* release */
+       static void release(KernelGlobals *kg, const ShaderData *sd);
 };
 
 CCL_NAMESPACE_END