Cycles: CUDA bicubic and tricubic texture interpolation support.
[blender.git] / intern / cycles / kernel / osl / osl_services.cpp
index ebe739ebd0e2918cf749ac0cfcd226bb9ebe1765..8ae004031e17d86929be9a118ce9da454902e719 100644 (file)
 
 #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_logging.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"
-#include "kernels/cpu/kernel_cpu_image.h"
-#include "geom/geom.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>
@@ -92,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");
@@ -100,6 +103,8 @@ 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");
@@ -126,8 +131,10 @@ OSLRenderServices::OSLRenderServices()
 
 OSLRenderServices::~OSLRenderServices()
 {
-       VLOG(2) << "OSL texture system stats:\n"
-               << osl_ts->getstats();
+       if(osl_ts) {
+               VLOG(2) << "OSL texture system stats:\n"
+                       << osl_ts->getstats();
+       }
 #ifdef WITH_PTEX
        ptex_cache->release();
 #endif
@@ -162,6 +169,12 @@ bool OSLRenderServices::get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result
                        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;
                }
        }
@@ -192,6 +205,12 @@ bool OSLRenderServices::get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44
                        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;
                }
        }
@@ -281,6 +300,12 @@ bool OSLRenderServices::get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result
                        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;
                }
        }
@@ -306,6 +331,12 @@ bool OSLRenderServices::get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44
                        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;
                }
        }
@@ -553,13 +584,13 @@ static bool get_mesh_element_attribute(KernelGlobals *kg, const ShaderData *sd,
           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) {
                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);
        }
@@ -572,7 +603,7 @@ static bool get_mesh_attribute(KernelGlobals *kg, const ShaderData *sd, const OS
                                const TypeDesc& type, bool derivatives, void *val)
 {
        if(attr.type == TypeDesc::TypeMatrix) {
-               Transform tfm = primitive_attribute_matrix(kg, sd, attr.offset);
+               Transform tfm = primitive_attribute_matrix(kg, sd, attr.desc);
                return set_attribute_matrix(tfm, type, val);
        }
        else {
@@ -682,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]);
@@ -731,6 +762,24 @@ bool OSLRenderServices::get_background_attribute(KernelGlobals *kg, ShaderData *
                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_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;
@@ -775,7 +824,7 @@ bool OSLRenderServices::get_background_attribute(KernelGlobals *kg, ShaderData *
 bool OSLRenderServices::get_attribute(OSL::ShaderGlobals *sg, bool derivatives, ustring object_name,
                                       TypeDesc type, ustring name, void *val)
 {
-       if(sg->renderstate == NULL)
+       if(sg == NULL || sg->renderstate == NULL)
                return false;
 
        ShaderData *sd = (ShaderData *)(sg->renderstate);
@@ -786,7 +835,7 @@ bool OSLRenderServices::get_attribute(ShaderData *sd, bool derivatives, ustring
                                       TypeDesc type, ustring name, void *val)
 {
        KernelGlobals *kg = sd->osl_globals;
-       bool is_curve;
+       int prim_type = 0;
        int object;
 
        /* lookup of attribute on another object */
@@ -797,25 +846,24 @@ bool OSLRenderServices::get_attribute(ShaderData *sd, bool derivatives, ustring
                        return false;
 
                object = it->second;
-               is_curve = false;
        }
        else {
                object = sd->object;
-               is_curve = (sd->type & PRIMITIVE_ALL_CURVE) != 0;
+               prim_type = attribute_primitive_type(kg, sd);
 
                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()) {
                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))
                                return true;
@@ -912,9 +960,9 @@ bool OSLRenderServices::texture(ustring filename,
 #endif
        bool status;
 
-       if(filename[0] == '@') {
+       if(filename.length() && filename[0] == '@') {
                int slot = atoi(filename.c_str() + 1);
-               float4 rgba = kernel_tex_image_interp(slot, s, 1.0f - t);
+               float4 rgba = kernel_tex_image_interp(kg, slot, s, 1.0f - t);
 
                result[0] = rgba[0];
                if(nchannels > 1)
@@ -993,9 +1041,9 @@ bool OSLRenderServices::texture3d(ustring filename,
        }
 
        bool status;
-       if(filename[0] == '@') {
+       if(filename.length() && filename[0] == '@') {
                int slot = atoi(filename.c_str() + 1);
-               float4 rgba = kernel_tex_image_interp_3d(slot, P.x, P.y, P.z);
+               float4 rgba = kernel_tex_image_interp_3d(kg, slot, P.x, P.y, P.z, INTERPOLATION_NONE);
 
                result[0] = rgba[0];
                if(nchannels > 1)
@@ -1149,8 +1197,9 @@ bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg,
        tracedata->init = true;
        tracedata->sd.osl_globals = sd->osl_globals;
 
-       /* raytrace */
-       return scene_intersect(sd->osl_globals, &ray, PATH_RAY_ALL_VISIBILITY, &tracedata->isect, NULL, 0.0f, 0.0f);
+       /* 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);
 }