Cycles: CUDA bicubic and tricubic texture interpolation support.
[blender.git] / intern / cycles / kernel / osl / osl_services.cpp
index d5edd04d8a59b1f9e86d8357b1a4f5b9ea4b2e6a..8ae004031e17d86929be9a118ce9da454902e719 100644 (file)
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
- * limitations under the License
+ * limitations under the License.
  */
 
-#include <string.h>
-
-#include "mesh.h"
-#include "object.h"
-#include "scene.h"
-
-#include "osl_closures.h"
-#include "osl_globals.h"
-#include "osl_services.h"
-#include "osl_shader.h"
-
-#include "util_foreach.h"
-#include "util_string.h"
-
-#include "kernel_compat_cpu.h"
-#include "kernel_globals.h"
-#include "kernel_random.h"
-#include "kernel_projection.h"
-#include "kernel_differential.h"
-#include "kernel_montecarlo.h"
-#include "kernel_camera.h"
+/* TODO(sergey): There is a bit of headers dependency hell going on
+ * here, so for now we just put here. In the future it might be better
+ * to have dedicated file for such tweaks.
+ */
+#if (defined(__GNUC__) && !defined(__clang__)) && defined(NDEBUG)
+#  pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
+#  pragma GCC diagnostic ignored "-Wuninitialized"
+#endif
 
-#include "geom/geom.h"
+#include <string.h>
 
-#include "kernel_projection.h"
-#include "kernel_accumulate.h"
-#include "kernel_shader.h"
+#include "render/mesh.h"
+#include "render/object.h"
+#include "render/scene.h"
+
+#include "kernel/osl/osl_closures.h"
+#include "kernel/osl/osl_globals.h"
+#include "kernel/osl/osl_services.h"
+#include "kernel/osl/osl_shader.h"
+
+#include "util/util_foreach.h"
+#include "util/util_logging.h"
+#include "util/util_string.h"
+
+#include "kernel/kernel_compat_cpu.h"
+#include "kernel/split/kernel_split_data_types.h"
+#include "kernel/kernel_globals.h"
+#include "kernel/kernel_random.h"
+#include "kernel/kernel_projection.h"
+#include "kernel/kernel_differential.h"
+#include "kernel/kernel_montecarlo.h"
+#include "kernel/kernel_camera.h"
+#include "kernel/kernels/cpu/kernel_cpu_image.h"
+#include "kernel/geom/geom.h"
+#include "kernel/bvh/bvh.h"
+
+#include "kernel/kernel_projection.h"
+#include "kernel/kernel_accumulate.h"
+#include "kernel/kernel_shader.h"
 
 #ifdef WITH_PTEX
-#include <Ptexture.h>
+#  include <Ptexture.h>
 #endif
 
 CCL_NAMESPACE_BEGIN
 
 /* RenderServices implementation */
 
-#define COPY_MATRIX44(m1, m2) memcpy(m1, m2, sizeof(*m2))
+#define COPY_MATRIX44(m1, m2)  { \
+       CHECK_TYPE(m1, OSL::Matrix44*); \
+       CHECK_TYPE(m2, Transform*); \
+       memcpy(m1, m2, sizeof(*m2)); \
+} (void)0
 
 /* static ustrings */
 ustring OSLRenderServices::u_distance("distance");
 ustring OSLRenderServices::u_index("index");
+ustring OSLRenderServices::u_world("world");
 ustring OSLRenderServices::u_camera("camera");
 ustring OSLRenderServices::u_screen("screen");
 ustring OSLRenderServices::u_raster("raster");
@@ -77,6 +94,7 @@ ustring OSLRenderServices::u_geom_numpolyvertices("geom:numpolyvertices");
 ustring OSLRenderServices::u_geom_trianglevertices("geom:trianglevertices");
 ustring OSLRenderServices::u_geom_polyvertices("geom:polyvertices");
 ustring OSLRenderServices::u_geom_name("geom:name");
+ustring OSLRenderServices::u_geom_undisplaced("geom:undisplaced");
 ustring OSLRenderServices::u_is_smooth("geom:is_smooth");
 #ifdef __HAIR__
 ustring OSLRenderServices::u_is_curve("geom:is_curve");
@@ -85,6 +103,10 @@ ustring OSLRenderServices::u_curve_tangent_normal("geom:curve_tangent_normal");
 #endif
 ustring OSLRenderServices::u_path_ray_length("path:ray_length");
 ustring OSLRenderServices::u_path_ray_depth("path:ray_depth");
+ustring OSLRenderServices::u_path_diffuse_depth("path:diffuse_depth");
+ustring OSLRenderServices::u_path_glossy_depth("path:glossy_depth");
+ustring OSLRenderServices::u_path_transparent_depth("path:transparent_depth");
+ustring OSLRenderServices::u_path_transmission_depth("path:transmission_depth");
 ustring OSLRenderServices::u_trace("trace");
 ustring OSLRenderServices::u_hit("hit");
 ustring OSLRenderServices::u_hitdist("hitdist");
@@ -109,6 +131,10 @@ OSLRenderServices::OSLRenderServices()
 
 OSLRenderServices::~OSLRenderServices()
 {
+       if(osl_ts) {
+               VLOG(2) << "OSL texture system stats:\n"
+                       << osl_ts->getstats();
+       }
 #ifdef WITH_PTEX
        ptex_cache->release();
 #endif
@@ -120,16 +146,16 @@ void OSLRenderServices::thread_init(KernelGlobals *kernel_globals_, OSL::Texture
        osl_ts = osl_ts_;
 }
 
-bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform, float time)
+bool OSLRenderServices::get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time)
 {
        /* this is only used for shader and object space, we don't really have
         * a concept of shader space, so we just use object space for both. */
-       if (xform) {
+       if(xform) {
                const ShaderData *sd = (const ShaderData *)xform;
                KernelGlobals *kg = sd->osl_globals;
                int object = sd->object;
 
-               if (object != OBJECT_NONE) {
+               if(object != OBJECT_NONE) {
 #ifdef __OBJECT_MOTION__
                        Transform tfm;
 
@@ -143,6 +169,12 @@ bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, OSL::TransformationPtr
                        tfm = transform_transpose(tfm);
                        COPY_MATRIX44(&result, &tfm);
 
+                       return true;
+               }
+               else if(sd->type == PRIMITIVE_LAMP) {
+                       Transform tfm = transform_transpose(sd->ob_tfm);
+                       COPY_MATRIX44(&result, &tfm);
+
                        return true;
                }
        }
@@ -150,16 +182,16 @@ bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, OSL::TransformationPtr
        return false;
 }
 
-bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform, float time)
+bool OSLRenderServices::get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time)
 {
        /* this is only used for shader and object space, we don't really have
         * a concept of shader space, so we just use object space for both. */
-       if (xform) {
+       if(xform) {
                const ShaderData *sd = (const ShaderData *)xform;
                KernelGlobals *kg = sd->osl_globals;
                int object = sd->object;
 
-               if (object != OBJECT_NONE) {
+               if(object != OBJECT_NONE) {
 #ifdef __OBJECT_MOTION__
                        Transform itfm;
 
@@ -173,6 +205,12 @@ bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::Transform
                        itfm = transform_transpose(itfm);
                        COPY_MATRIX44(&result, &itfm);
 
+                       return true;
+               }
+               else if(sd->type == PRIMITIVE_LAMP) {
+                       Transform tfm = transform_transpose(sd->ob_itfm);
+                       COPY_MATRIX44(&result, &tfm);
+
                        return true;
                }
        }
@@ -180,71 +218,79 @@ bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::Transform
        return false;
 }
 
-bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, ustring from, float time)
+bool OSLRenderServices::get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from, float time)
 {
        KernelGlobals *kg = kernel_globals;
 
-       if (from == u_ndc) {
+       if(from == u_ndc) {
                Transform tfm = transform_transpose(transform_quick_inverse(kernel_data.cam.worldtondc));
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (from == u_raster) {
+       else if(from == u_raster) {
                Transform tfm = transform_transpose(kernel_data.cam.rastertoworld);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (from == u_screen) {
+       else if(from == u_screen) {
                Transform tfm = transform_transpose(kernel_data.cam.screentoworld);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (from == u_camera) {
+       else if(from == u_camera) {
                Transform tfm = transform_transpose(kernel_data.cam.cameratoworld);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
+       else if(from == u_world) {
+               result.makeIdentity();
+               return true;
+       }
 
        return false;
 }
 
-bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, ustring to, float time)
+bool OSLRenderServices::get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring to, float time)
 {
        KernelGlobals *kg = kernel_globals;
 
-       if (to == u_ndc) {
+       if(to == u_ndc) {
                Transform tfm = transform_transpose(kernel_data.cam.worldtondc);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (to == u_raster) {
+       else if(to == u_raster) {
                Transform tfm = transform_transpose(kernel_data.cam.worldtoraster);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (to == u_screen) {
+       else if(to == u_screen) {
                Transform tfm = transform_transpose(kernel_data.cam.worldtoscreen);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (to == u_camera) {
+       else if(to == u_camera) {
                Transform tfm = transform_transpose(kernel_data.cam.worldtocamera);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
+       else if(to == u_world) {
+               result.makeIdentity();
+               return true;
+       }
 
        return false;
 }
 
-bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform)
+bool OSLRenderServices::get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform)
 {
        /* this is only used for shader and object space, we don't really have
         * a concept of shader space, so we just use object space for both. */
-       if (xform) {
+       if(xform) {
                const ShaderData *sd = (const ShaderData *)xform;
                int object = sd->object;
 
-               if (object != OBJECT_NONE) {
+               if(object != OBJECT_NONE) {
 #ifdef __OBJECT_MOTION__
                        Transform tfm = sd->ob_tfm;
 #else
@@ -254,6 +300,12 @@ bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, OSL::TransformationPtr
                        tfm = transform_transpose(tfm);
                        COPY_MATRIX44(&result, &tfm);
 
+                       return true;
+               }
+               else if(sd->type == PRIMITIVE_LAMP) {
+                       Transform tfm = transform_transpose(sd->ob_tfm);
+                       COPY_MATRIX44(&result, &tfm);
+
                        return true;
                }
        }
@@ -261,15 +313,15 @@ bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, OSL::TransformationPtr
        return false;
 }
 
-bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform)
+bool OSLRenderServices::get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform)
 {
        /* this is only used for shader and object space, we don't really have
         * a concept of shader space, so we just use object space for both. */
-       if (xform) {
+       if(xform) {
                const ShaderData *sd = (const ShaderData *)xform;
                int object = sd->object;
 
-               if (object != OBJECT_NONE) {
+               if(object != OBJECT_NONE) {
 #ifdef __OBJECT_MOTION__
                        Transform tfm = sd->ob_itfm;
 #else
@@ -279,6 +331,12 @@ bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::Transform
                        tfm = transform_transpose(tfm);
                        COPY_MATRIX44(&result, &tfm);
 
+                       return true;
+               }
+               else if(sd->type == PRIMITIVE_LAMP) {
+                       Transform tfm = transform_transpose(sd->ob_itfm);
+                       COPY_MATRIX44(&result, &tfm);
+
                        return true;
                }
        }
@@ -286,26 +344,26 @@ bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::Transform
        return false;
 }
 
-bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, ustring from)
+bool OSLRenderServices::get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from)
 {
        KernelGlobals *kg = kernel_globals;
 
-       if (from == u_ndc) {
+       if(from == u_ndc) {
                Transform tfm = transform_transpose(transform_quick_inverse(kernel_data.cam.worldtondc));
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (from == u_raster) {
+       else if(from == u_raster) {
                Transform tfm = transform_transpose(kernel_data.cam.rastertoworld);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (from == u_screen) {
+       else if(from == u_screen) {
                Transform tfm = transform_transpose(kernel_data.cam.screentoworld);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (from == u_camera) {
+       else if(from == u_camera) {
                Transform tfm = transform_transpose(kernel_data.cam.cameratoworld);
                COPY_MATRIX44(&result, &tfm);
                return true;
@@ -314,26 +372,26 @@ bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, ustring from)
        return false;
 }
 
-bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, ustring to)
+bool OSLRenderServices::get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring to)
 {
        KernelGlobals *kg = kernel_globals;
        
-       if (to == u_ndc) {
+       if(to == u_ndc) {
                Transform tfm = transform_transpose(kernel_data.cam.worldtondc);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (to == u_raster) {
+       else if(to == u_raster) {
                Transform tfm = transform_transpose(kernel_data.cam.worldtoraster);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (to == u_screen) {
+       else if(to == u_screen) {
                Transform tfm = transform_transpose(kernel_data.cam.worldtoscreen);
                COPY_MATRIX44(&result, &tfm);
                return true;
        }
-       else if (to == u_camera) {
+       else if(to == u_camera) {
                Transform tfm = transform_transpose(kernel_data.cam.worldtocamera);
                COPY_MATRIX44(&result, &tfm);
                return true;
@@ -342,7 +400,7 @@ bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, ustring to)
        return false;
 }
 
-bool OSLRenderServices::get_array_attribute(void *renderstate, bool derivatives, 
+bool OSLRenderServices::get_array_attribute(OSL::ShaderGlobals *sg, bool derivatives, 
                                             ustring object, TypeDesc type, ustring name,
                                             int index, void *val)
 {
@@ -351,8 +409,8 @@ bool OSLRenderServices::get_array_attribute(void *renderstate, bool derivatives,
 
 static bool set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, void *val)
 {
-       if (type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
-           type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
+       if(type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
+          type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
        {
                float *fval = (float *)val;
 
@@ -360,7 +418,7 @@ static bool set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, v
                fval[1] = f[0].y;
                fval[2] = f[0].z;
 
-               if (derivatives) {
+               if(derivatives) {
                        fval[3] = f[1].x;
                        fval[4] = f[1].y;
                        fval[5] = f[1].z;
@@ -376,7 +434,7 @@ static bool set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, v
                float *fval = (float *)val;
                fval[0] = average(f[0]);
 
-               if (derivatives) {
+               if(derivatives) {
                        fval[1] = average(f[1]);
                        fval[2] = average(f[2]);
                }
@@ -400,15 +458,15 @@ static bool set_attribute_float3(float3 f, TypeDesc type, bool derivatives, void
 
 static bool set_attribute_float(float f[3], TypeDesc type, bool derivatives, void *val)
 {
-       if (type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
-           type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
+       if(type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
+          type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
        {
                float *fval = (float *)val;
                fval[0] = f[0];
                fval[1] = f[1];
                fval[2] = f[2];
 
-               if (derivatives) {
+               if(derivatives) {
                        fval[3] = f[1];
                        fval[4] = f[1];
                        fval[5] = f[1];
@@ -424,7 +482,7 @@ static bool set_attribute_float(float f[3], TypeDesc type, bool derivatives, voi
                float *fval = (float *)val;
                fval[0] = f[0];
 
-               if (derivatives) {
+               if(derivatives) {
                        fval[1] = f[1];
                        fval[2] = f[2];
                }
@@ -452,7 +510,7 @@ static bool set_attribute_int(int i, TypeDesc type, bool derivatives, void *val)
                int *ival = (int *)val;
                ival[0] = i;
 
-               if (derivatives) {
+               if(derivatives) {
                        ival[1] = 0;
                        ival[2] = 0;
                }
@@ -465,11 +523,11 @@ static bool set_attribute_int(int i, TypeDesc type, bool derivatives, void *val)
 
 static bool set_attribute_string(ustring str, TypeDesc type, bool derivatives, void *val)
 {
-       if(type.basetype == TypeDesc::INT && type.aggregate == TypeDesc::SCALAR && type.arraylen == 0) {
+       if(type.basetype == TypeDesc::STRING && type.aggregate == TypeDesc::SCALAR && type.arraylen == 0) {
                ustring *sval = (ustring *)val;
                sval[0] = str;
 
-               if (derivatives) {
+               if(derivatives) {
                        sval[1] = OSLRenderServices::u_empty;
                        sval[2] = OSLRenderServices::u_empty;
                }
@@ -499,7 +557,7 @@ static bool set_attribute_float3_3(float3 P[3], TypeDesc type, bool derivatives,
 
                if(type.arraylen > 3)
                        memset(fval + 3*3, 0, sizeof(float)*3*(type.arraylen - 3));
-               if (derivatives)
+               if(derivatives)
                        memset(fval + type.arraylen*3, 0, sizeof(float)*2*3*type.arraylen);
 
                return true;
@@ -522,16 +580,17 @@ static bool set_attribute_matrix(const Transform& tfm, TypeDesc type, void *val)
 static bool get_mesh_element_attribute(KernelGlobals *kg, const ShaderData *sd, const OSLGlobals::Attribute& attr,
                                const TypeDesc& type, bool derivatives, void *val)
 {
-       if (attr.type == TypeDesc::TypePoint || attr.type == TypeDesc::TypeVector ||
-           attr.type == TypeDesc::TypeNormal || attr.type == TypeDesc::TypeColor) {
+       if(attr.type == TypeDesc::TypePoint || attr.type == TypeDesc::TypeVector ||
+          attr.type == TypeDesc::TypeNormal || attr.type == TypeDesc::TypeColor)
+       {
                float3 fval[3];
-               fval[0] = primitive_attribute_float3(kg, sd, attr.elem, attr.offset,
+               fval[0] = primitive_attribute_float3(kg, sd, attr.desc,
                                                     (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL);
                return set_attribute_float3(fval, type, derivatives, val);
        }
-       else if (attr.type == TypeDesc::TypeFloat) {
+       else if(attr.type == TypeDesc::TypeFloat) {
                float fval[3];
-               fval[0] = primitive_attribute_float(kg, sd, attr.elem, attr.offset,
+               fval[0] = primitive_attribute_float(kg, sd, attr.desc,
                                                    (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL);
                return set_attribute_float(fval, type, derivatives, val);
        }
@@ -543,8 +602,8 @@ static bool get_mesh_element_attribute(KernelGlobals *kg, const ShaderData *sd,
 static bool get_mesh_attribute(KernelGlobals *kg, const ShaderData *sd, const OSLGlobals::Attribute& attr,
                                const TypeDesc& type, bool derivatives, void *val)
 {
-       if (attr.type == TypeDesc::TypeMatrix) {
-               Transform tfm = primitive_attribute_matrix(kg, sd, attr.offset);
+       if(attr.type == TypeDesc::TypeMatrix) {
+               Transform tfm = primitive_attribute_matrix(kg, sd, attr.desc);
                return set_attribute_matrix(tfm, type, val);
        }
        else {
@@ -557,7 +616,7 @@ static void get_object_attribute(const OSLGlobals::Attribute& attr, bool derivat
        size_t datasize = attr.value.datasize();
 
        memcpy(val, attr.value.data(), datasize);
-       if (derivatives)
+       if(derivatives)
                memset((char *)val + datasize, 0, datasize * 2);
 }
 
@@ -567,85 +626,86 @@ bool OSLRenderServices::get_object_standard_attribute(KernelGlobals *kg, ShaderD
        /* todo: turn this into hash table? */
 
        /* Object Attributes */
-       if (name == u_object_location) {
+       if(name == u_object_location) {
                float3 f = object_location(kg, sd);
                return set_attribute_float3(f, type, derivatives, val);
        }
-       else if (name == u_object_index) {
+       else if(name == u_object_index) {
                float f = object_pass_id(kg, sd->object);
                return set_attribute_float(f, type, derivatives, val);
        }
-       else if (name == u_geom_dupli_generated) {
+       else if(name == u_geom_dupli_generated) {
                float3 f = object_dupli_generated(kg, sd->object);
                return set_attribute_float3(f, type, derivatives, val);
        }
-       else if (name == u_geom_dupli_uv) {
+       else if(name == u_geom_dupli_uv) {
                float3 f = object_dupli_uv(kg, sd->object);
                return set_attribute_float3(f, type, derivatives, val);
        }
-       else if (name == u_material_index) {
+       else if(name == u_material_index) {
                float f = shader_pass_id(kg, sd);
                return set_attribute_float(f, type, derivatives, val);
        }
-       else if (name == u_object_random) {
+       else if(name == u_object_random) {
                float f = object_random_number(kg, sd->object);
                return set_attribute_float(f, type, derivatives, val);
        }
 
        /* Particle Attributes */
-       else if (name == u_particle_index) {
-               uint particle_id = object_particle_id(kg, sd->object);
+       else if(name == u_particle_index) {
+               int particle_id = object_particle_id(kg, sd->object);
                float f = particle_index(kg, particle_id);
                return set_attribute_float(f, type, derivatives, val);
        }
-       else if (name == u_particle_age) {
-               uint particle_id = object_particle_id(kg, sd->object);
+       else if(name == u_particle_age) {
+               int particle_id = object_particle_id(kg, sd->object);
                float f = particle_age(kg, particle_id);
                return set_attribute_float(f, type, derivatives, val);
        }
-       else if (name == u_particle_lifetime) {
-               uint particle_id = object_particle_id(kg, sd->object);
-               float f= particle_lifetime(kg, particle_id);
+       else if(name == u_particle_lifetime) {
+               int particle_id = object_particle_id(kg, sd->object);
+               float f = particle_lifetime(kg, particle_id);
                return set_attribute_float(f, type, derivatives, val);
        }
-       else if (name == u_particle_location) {
-               uint particle_id = object_particle_id(kg, sd->object);
+       else if(name == u_particle_location) {
+               int particle_id = object_particle_id(kg, sd->object);
                float3 f = particle_location(kg, particle_id);
                return set_attribute_float3(f, type, derivatives, val);
        }
 #if 0  /* unsupported */
-       else if (name == u_particle_rotation) {
-               uint particle_id = object_particle_id(kg, sd->object);
+       else if(name == u_particle_rotation) {
+               int particle_id = object_particle_id(kg, sd->object);
                float4 f = particle_rotation(kg, particle_id);
                return set_attribute_float4(f, type, derivatives, val);
        }
 #endif
-       else if (name == u_particle_size) {
-               uint particle_id = object_particle_id(kg, sd->object);
+       else if(name == u_particle_size) {
+               int particle_id = object_particle_id(kg, sd->object);
                float f = particle_size(kg, particle_id);
                return set_attribute_float(f, type, derivatives, val);
        }
-       else if (name == u_particle_velocity) {
-               uint particle_id = object_particle_id(kg, sd->object);
+       else if(name == u_particle_velocity) {
+               int particle_id = object_particle_id(kg, sd->object);
                float3 f = particle_velocity(kg, particle_id);
                return set_attribute_float3(f, type, derivatives, val);
        }
-       else if (name == u_particle_angular_velocity) {
-               uint particle_id = object_particle_id(kg, sd->object);
+       else if(name == u_particle_angular_velocity) {
+               int particle_id = object_particle_id(kg, sd->object);
                float3 f = particle_angular_velocity(kg, particle_id);
                return set_attribute_float3(f, type, derivatives, val);
        }
        
        /* Geometry Attributes */
-       else if (name == u_geom_numpolyvertices) {
+       else if(name == u_geom_numpolyvertices) {
                return set_attribute_int(3, type, derivatives, val);
        }
-       else if ((name == u_geom_trianglevertices || name == u_geom_polyvertices)
+       else if((name == u_geom_trianglevertices || name == u_geom_polyvertices)
 #ifdef __HAIR__
-                    && sd->type & PRIMITIVE_ALL_TRIANGLE) {
+                    && sd->type & PRIMITIVE_ALL_TRIANGLE)
 #else
-               ) {
+               )
 #endif
+       {
                float3 P[3];
 
                if(sd->type & PRIMITIVE_TRIANGLE)
@@ -653,7 +713,7 @@ bool OSLRenderServices::get_object_standard_attribute(KernelGlobals *kg, ShaderD
                else
                        motion_triangle_vertices(kg, sd->object, sd->prim, sd->time, P);
 
-               if(!(sd->flag & SD_TRANSFORM_APPLIED)) {
+               if(!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
                        object_position_transform(kg, sd, &P[0]);
                        object_position_transform(kg, sd, &P[1]);
                        object_position_transform(kg, sd, &P[2]);
@@ -665,21 +725,21 @@ bool OSLRenderServices::get_object_standard_attribute(KernelGlobals *kg, ShaderD
                ustring object_name = kg->osl->object_names[sd->object];
                return set_attribute_string(object_name, type, derivatives, val);
        }
-       else if (name == u_is_smooth) {
+       else if(name == u_is_smooth) {
                float f = ((sd->shader & SHADER_SMOOTH_NORMAL) != 0);
                return set_attribute_float(f, type, derivatives, val);
        }
 #ifdef __HAIR__
        /* Hair Attributes */
-       else if (name == u_is_curve) {
+       else if(name == u_is_curve) {
                float f = (sd->type & PRIMITIVE_ALL_CURVE) != 0;
                return set_attribute_float(f, type, derivatives, val);
        }
-       else if (name == u_curve_thickness) {
+       else if(name == u_curve_thickness) {
                float f = curve_thickness(kg, sd);
                return set_attribute_float(f, type, derivatives, val);
        }
-       else if (name == u_curve_tangent_normal) {
+       else if(name == u_curve_tangent_normal) {
                float3 f = curve_tangent_normal(kg, sd);
                return set_attribute_float3(f, type, derivatives, val);
        }
@@ -691,17 +751,48 @@ bool OSLRenderServices::get_object_standard_attribute(KernelGlobals *kg, ShaderD
 bool OSLRenderServices::get_background_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
                                                  TypeDesc type, bool derivatives, void *val)
 {
-       if (name == u_path_ray_length) {
+       if(name == u_path_ray_length) {
                /* Ray Length */
                float f = sd->ray_length;
                return set_attribute_float(f, type, derivatives, val);
        }
-       else if (name == u_path_ray_depth) {
+       else if(name == u_path_ray_depth) {
                /* Ray Depth */
-               int f = sd->ray_depth;
+               PathState *state = sd->osl_path_state;
+               int f = state->bounce;
+               return set_attribute_int(f, type, derivatives, val);
+       }
+       else if(name == u_path_diffuse_depth) {
+               /* Diffuse Ray Depth */
+               PathState *state = sd->osl_path_state;
+               int f = state->diffuse_bounce;
+               return set_attribute_int(f, type, derivatives, val);
+       }
+       else if(name == u_path_glossy_depth) {
+               /* Glossy Ray Depth */
+               PathState *state = sd->osl_path_state;
+               int f = state->glossy_bounce;
                return set_attribute_int(f, type, derivatives, val);
        }
-       else if (name == u_ndc) {
+       else if(name == u_path_transmission_depth) {
+               /* Transmission Ray Depth */
+               PathState *state = sd->osl_path_state;
+               int f = state->transmission_bounce;
+               return set_attribute_int(f, type, derivatives, val);
+       }
+       else if(name == u_path_transparent_depth) {
+               /* Transparent Ray Depth */
+               PathState *state = sd->osl_path_state;
+               int f = state->transparent_bounce;
+               return set_attribute_int(f, type, derivatives, val);
+       }
+       else if(name == u_path_transmission_depth) {
+               /* Transmission Ray Depth */
+               PathState *state = sd->osl_path_state;
+               int f = state->transmission_bounce;
+               return set_attribute_int(f, type, derivatives, val);
+       }
+       else if(name == u_ndc) {
                /* NDC coordinates with special exception for otho */
                OSLThreadData *tdata = kg->osl_tdata;
                OSL::ShaderGlobals *globals = &tdata->globals;
@@ -730,45 +821,53 @@ bool OSLRenderServices::get_background_attribute(KernelGlobals *kg, ShaderData *
                return false;
 }
 
-bool OSLRenderServices::get_attribute(void *renderstate, bool derivatives, ustring object_name,
+bool OSLRenderServices::get_attribute(OSL::ShaderGlobals *sg, bool derivatives, ustring object_name,
+                                      TypeDesc type, ustring name, void *val)
+{
+       if(sg == NULL || sg->renderstate == NULL)
+               return false;
+
+       ShaderData *sd = (ShaderData *)(sg->renderstate);
+       return get_attribute(sd, derivatives, object_name, type, name, val);
+}
+
+bool OSLRenderServices::get_attribute(ShaderData *sd, bool derivatives, ustring object_name,
                                       TypeDesc type, ustring name, void *val)
 {
-       ShaderData *sd = (ShaderData *)renderstate;
        KernelGlobals *kg = sd->osl_globals;
-       bool is_curve;
-       int object, prim;
+       int prim_type = 0;
+       int object;
 
        /* lookup of attribute on another object */
-       if (object_name != u_empty) {
+       if(object_name != u_empty) {
                OSLGlobals::ObjectNameMap::iterator it = kg->osl->object_name_map.find(object_name);
 
-               if (it == kg->osl->object_name_map.end())
+               if(it == kg->osl->object_name_map.end())
                        return false;
 
                object = it->second;
-               prim = PRIM_NONE;
-               is_curve = false;
        }
        else {
                object = sd->object;
-               prim = sd->prim;
-               is_curve = (sd->type & PRIMITIVE_ALL_CURVE) != 0;
+               prim_type = attribute_primitive_type(kg, sd);
 
-               if (object == OBJECT_NONE)
+               if(object == OBJECT_NONE)
                        return get_background_attribute(kg, sd, name, type, derivatives, val);
        }
 
        /* find attribute on object */
-       object = object*ATTR_PRIM_TYPES + (is_curve == true);
+       object = object*ATTR_PRIM_TYPES + prim_type;
        OSLGlobals::AttributeMap& attribute_map = kg->osl->attribute_map[object];
        OSLGlobals::AttributeMap::iterator it = attribute_map.find(name);
 
-       if (it != attribute_map.end()) {
+       if(it != attribute_map.end()) {
                const OSLGlobals::Attribute& attr = it->second;
 
-               if (attr.elem != ATTR_ELEMENT_OBJECT) {
+               if(attr.desc.element != ATTR_ELEMENT_OBJECT) {
                        /* triangle and vertex attributes */
-                       if(!get_mesh_element_attribute(kg, sd, attr, type, derivatives, val))
+                       if(get_mesh_element_attribute(kg, sd, attr, type, derivatives, val))
+                               return true;
+                       else
                                return get_mesh_attribute(kg, sd, attr, type, derivatives, val);
                }
                else {
@@ -781,7 +880,7 @@ bool OSLRenderServices::get_attribute(void *renderstate, bool derivatives, ustri
                /* not found in attribute, check standard object info */
                bool is_std_object_attribute = get_object_standard_attribute(kg, sd, name, type, derivatives, val);
 
-               if (is_std_object_attribute)
+               if(is_std_object_attribute)
                        return true;
 
                return get_background_attribute(kg, sd, name, type, derivatives, val);
@@ -791,25 +890,37 @@ bool OSLRenderServices::get_attribute(void *renderstate, bool derivatives, ustri
 }
 
 bool OSLRenderServices::get_userdata(bool derivatives, ustring name, TypeDesc type, 
-                                     void *renderstate, void *val)
+                                     OSL::ShaderGlobals *sg, void *val)
 {
        return false; /* disabled by lockgeom */
 }
 
-bool OSLRenderServices::has_userdata(ustring name, TypeDesc type, void *renderstate)
+bool OSLRenderServices::has_userdata(ustring name, TypeDesc type, OSL::ShaderGlobals *sg)
 {
        return false; /* never called by OSL */
 }
 
-bool OSLRenderServices::texture(ustring filename, TextureOpt &options,
+bool OSLRenderServices::texture(ustring filename,
+                                TextureHandle *texture_handle,
+                                TexturePerthread *texture_thread_info,
+                                TextureOpt &options,
                                 OSL::ShaderGlobals *sg,
-                                float s, float t, float dsdx, float dtdx,
-                                float dsdy, float dtdy, float *result)
+                                float s, float t,
+                                float dsdx, float dtdx, float dsdy, float dtdy,
+                                int nchannels,
+                                float *result,
+                                float *dresultds,
+                                float *dresultdt)
 {
        OSL::TextureSystem *ts = osl_ts;
        ShaderData *sd = (ShaderData *)(sg->renderstate);
        KernelGlobals *kg = sd->osl_globals;
 
+       if(texture_thread_info == NULL) {
+               OSLThreadData *tdata = kg->osl_tdata;
+               texture_thread_info = tdata->oiio_thread_info;
+       }
+
 #ifdef WITH_PTEX
        /* todo: this is just a quick hack, only works with particular files and options */
        if(string_endswith(filename.string(), ".ptx")) {
@@ -839,63 +950,146 @@ bool OSLRenderServices::texture(ustring filename, TextureOpt &options,
                PtexFilter::Options opts(PtexFilter::f_bicubic, mipmaplerp, sharpness);
                PtexPtr<PtexFilter> f(PtexFilter::getFilter(r, opts));
 
-               f->eval(result, options.firstchannel, options.nchannels, faceid, u, v, dudx, dvdx, dudy, dvdy);
+               f->eval(result, options.firstchannel, nchannels, faceid, u, v, dudx, dvdx, dudy, dvdy);
 
-               for(int c = r->numChannels(); c < options.nchannels; c++)
+               for(int c = r->numChannels(); c < nchannels; c++)
                        result[c] = result[0];
 
                return true;
        }
 #endif
+       bool status;
 
-       OSLThreadData *tdata = kg->osl_tdata;
-       OIIO::TextureSystem::Perthread *thread_info = tdata->oiio_thread_info;
-
-       OIIO::TextureSystem::TextureHandle *th = ts->get_texture_handle(filename, thread_info);
+       if(filename.length() && filename[0] == '@') {
+               int slot = atoi(filename.c_str() + 1);
+               float4 rgba = kernel_tex_image_interp(kg, slot, s, 1.0f - t);
 
-       bool status = ts->texture(th, thread_info,
-                                 options, s, t, dsdx, dtdx, dsdy, dtdy, result);
+               result[0] = rgba[0];
+               if(nchannels > 1)
+                       result[1] = rgba[1];
+               if(nchannels > 2)
+                       result[2] = rgba[2];
+               if(nchannels > 3)
+                       result[3] = rgba[3];
+               status = true;
+       }
+       else {
+               if(texture_handle != NULL) {
+                       status = ts->texture(texture_handle,
+                                            texture_thread_info,
+                                            options,
+                                            s, t,
+                                            dsdx, dtdx,
+                                            dsdy, dtdy,
+                                            nchannels,
+                                            result,
+                                            dresultds, dresultdt);
+               }
+               else {
+                       status = ts->texture(filename,
+                                            options,
+                                            s, t,
+                                            dsdx, dtdx,
+                                            dsdy, dtdy,
+                                            nchannels,
+                                            result,
+                                            dresultds, dresultdt);
+               }
+       }
 
        if(!status) {
-               if(options.nchannels == 3 || options.nchannels == 4) {
+               if(nchannels == 3 || nchannels == 4) {
                        result[0] = 1.0f;
                        result[1] = 0.0f;
                        result[2] = 1.0f;
 
-                       if(options.nchannels == 4)
+                       if(nchannels == 4)
                                result[3] = 1.0f;
                }
+               /* This might be slow, but prevents error messages leak and
+                * other nasty stuff happening.
+                */
+               string err = ts->geterror();
+               (void)err;
        }
 
        return status;
 }
 
-bool OSLRenderServices::texture3d(ustring filename, TextureOpt &options,
-                                  OSL::ShaderGlobals *sg, const OSL::Vec3 &P,
-                                  const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy,
-                                  const OSL::Vec3 &dPdz, float *result)
+bool OSLRenderServices::texture3d(ustring filename,
+                                  TextureHandle *texture_handle,
+                                  TexturePerthread *texture_thread_info,
+                                  TextureOpt &options,
+                                  OSL::ShaderGlobals *sg,
+                                  const OSL::Vec3 &P,
+                                  const OSL::Vec3 &dPdx,
+                                  const OSL::Vec3 &dPdy,
+                                  const OSL::Vec3 &dPdz,
+                                  int nchannels,
+                                  float *result,
+                                  float *dresultds,
+                                  float *dresultdt,
+                                  float *dresultdr)
 {
        OSL::TextureSystem *ts = osl_ts;
        ShaderData *sd = (ShaderData *)(sg->renderstate);
        KernelGlobals *kg = sd->osl_globals;
-       OSLThreadData *tdata = kg->osl_tdata;
-       OIIO::TextureSystem::Perthread *thread_info = tdata->oiio_thread_info;
 
-       OIIO::TextureSystem::TextureHandle *th =  ts->get_texture_handle(filename, thread_info);
+       if(texture_thread_info == NULL) {
+               OSLThreadData *tdata = kg->osl_tdata;
+               texture_thread_info = tdata->oiio_thread_info;
+       }
+
+       bool status;
+       if(filename.length() && filename[0] == '@') {
+               int slot = atoi(filename.c_str() + 1);
+               float4 rgba = kernel_tex_image_interp_3d(kg, slot, P.x, P.y, P.z, INTERPOLATION_NONE);
 
-       bool status = ts->texture3d(th, thread_info,
-                                   options, P, dPdx, dPdy, dPdz, result);
+               result[0] = rgba[0];
+               if(nchannels > 1)
+                       result[1] = rgba[1];
+               if(nchannels > 2)
+                       result[2] = rgba[2];
+               if(nchannels > 3)
+                       result[3] = rgba[3];
+               status = true;
+       }
+       else {
+               if(texture_handle != NULL) {
+                       status = ts->texture3d(texture_handle,
+                                              texture_thread_info,
+                                              options,
+                                              P,
+                                              dPdx, dPdy, dPdz,
+                                              nchannels,
+                                              result,
+                                              dresultds, dresultdt, dresultdr);
+               }
+               else {
+                       status = ts->texture3d(filename,
+                                              options,
+                                              P,
+                                              dPdx, dPdy, dPdz,
+                                              nchannels,
+                                              result,
+                                              dresultds, dresultdt, dresultdr);
+               }
+       }
 
        if(!status) {
-               if(options.nchannels == 3 || options.nchannels == 4) {
+               if(nchannels == 3 || nchannels == 4) {
                        result[0] = 1.0f;
                        result[1] = 0.0f;
                        result[2] = 1.0f;
 
-                       if(options.nchannels == 4)
+                       if(nchannels == 4)
                                result[3] = 1.0f;
                }
-
+               /* This might be slow, but prevents error messages leak and
+                * other nasty stuff happening.
+                */
+               string err = ts->geterror();
+               (void)err;
        }
 
        return status;
@@ -903,7 +1097,8 @@ bool OSLRenderServices::texture3d(ustring filename, TextureOpt &options,
 
 bool OSLRenderServices::environment(ustring filename, TextureOpt &options,
                                     OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
-                                    const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy, float *result)
+                                    const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy,
+                                    int nchannels, float *result)
 {
        OSL::TextureSystem *ts = osl_ts;
        ShaderData *sd = (ShaderData *)(sg->renderstate);
@@ -911,17 +1106,19 @@ bool OSLRenderServices::environment(ustring filename, TextureOpt &options,
        OSLThreadData *tdata = kg->osl_tdata;
        OIIO::TextureSystem::Perthread *thread_info = tdata->oiio_thread_info;
 
-       OIIO::TextureSystem::TextureHandle *th =  ts->get_texture_handle(filename, thread_info);
+       OIIO::TextureSystem::TextureHandle *th = ts->get_texture_handle(filename, thread_info);
+
        bool status = ts->environment(th, thread_info,
-                                     options, R, dRdx, dRdy, result);
+                                     options, R, dRdx, dRdy,
+                                     nchannels, result);
 
        if(!status) {
-               if(options.nchannels == 3 || options.nchannels == 4) {
+               if(nchannels == 3 || nchannels == 4) {
                        result[0] = 1.0f;
                        result[1] = 0.0f;
                        result[2] = 1.0f;
 
-                       if(options.nchannels == 4)
+                       if(nchannels == 4)
                                result[3] = 1.0f;
                }
        }
@@ -929,7 +1126,7 @@ bool OSLRenderServices::environment(ustring filename, TextureOpt &options,
        return status;
 }
 
-bool OSLRenderServices::get_texture_info(ustring filename, int subimage,
+bool OSLRenderServices::get_texture_info(OSL::ShaderGlobals *sg, ustring filename, int subimage,
                                          ustring dataname,
                                          TypeDesc datatype, void *data)
 {
@@ -972,7 +1169,7 @@ bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg,
 
        ray.P = TO_FLOAT3(P);
        ray.D = TO_FLOAT3(R);
-       ray.t = (options.maxdist == 1.0e30)? FLT_MAX: options.maxdist - options.mindist;
+       ray.t = (options.maxdist == 1.0e30f)? FLT_MAX: options.maxdist - options.mindist;
        ray.time = sd->time;
 
        if(options.mindist == 0.0f) {
@@ -1000,12 +1197,9 @@ bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg,
        tracedata->init = true;
        tracedata->sd.osl_globals = sd->osl_globals;
 
-       /* raytrace */
-#ifdef __HAIR__
-       return scene_intersect(sd->osl_globals, &ray, PATH_RAY_ALL_VISIBILITY, &tracedata->isect, NULL, 0.0f, 0.0f);
-#else
-       return scene_intersect(sd->osl_globals, &ray, PATH_RAY_ALL_VISIBILITY, &tracedata->isect);
-#endif
+       /* Raytrace, leaving out shadow opaque to avoid early exit. */
+       uint visibility = PATH_RAY_ALL_VISIBILITY - PATH_RAY_SHADOW_OPAQUE;
+       return scene_intersect(sd->osl_globals, ray, visibility, &tracedata->isect, NULL, 0.0f, 0.0f);
 }
 
 
@@ -1029,10 +1223,7 @@ bool OSLRenderServices::getmessage(OSL::ShaderGlobals *sg, ustring source, ustri
 
                                if(!tracedata->setup) {
                                        /* lazy shader data setup */
-                                       ShaderData *original_sd = (ShaderData *)(sg->renderstate);
-                                       int bounce = original_sd->ray_depth + 1;
-
-                                       shader_setup_from_ray(kg, sd, &tracedata->isect, &tracedata->ray, bounce);
+                                       shader_setup_from_ray(kg, sd, &tracedata->isect, &tracedata->ray);
                                        tracedata->setup = true;
                                }