a8d4bc3d8551b739ed6355637d0c2256f8ed10c3
[blender.git] / intern / cycles / kernel / osl / osl_services.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include <string.h>
20
21 #include "mesh.h"
22 #include "object.h"
23 #include "scene.h"
24
25 #include "osl_closures.h"
26 #include "osl_globals.h"
27 #include "osl_services.h"
28 #include "osl_shader.h"
29
30 #include "util_foreach.h"
31 #include "util_string.h"
32
33 #include "kernel_compat_cpu.h"
34 #include "kernel_globals.h"
35 #include "kernel_montecarlo.h"
36 #include "kernel_projection.h"
37 #include "kernel_differential.h"
38 #include "kernel_object.h"
39 #include "kernel_random.h"
40 #include "kernel_bvh.h"
41 #include "kernel_triangle.h"
42 #include "kernel_curve.h"
43 #include "kernel_primitive.h"
44 #include "kernel_projection.h"
45 #include "kernel_accumulate.h"
46 #include "kernel_shader.h"
47
48 CCL_NAMESPACE_BEGIN
49
50 /* RenderServices implementation */
51
52 #define COPY_MATRIX44(m1, m2) memcpy(m1, m2, sizeof(*m2))
53
54 /* static ustrings */
55 ustring OSLRenderServices::u_distance("distance");
56 ustring OSLRenderServices::u_index("index");
57 ustring OSLRenderServices::u_camera("camera");
58 ustring OSLRenderServices::u_screen("screen");
59 ustring OSLRenderServices::u_raster("raster");
60 ustring OSLRenderServices::u_ndc("NDC");
61 ustring OSLRenderServices::u_object_location("object:location");
62 ustring OSLRenderServices::u_object_index("object:index");
63 ustring OSLRenderServices::u_geom_dupli_generated("geom:dupli_generated");
64 ustring OSLRenderServices::u_geom_dupli_uv("geom:dupli_uv");
65 ustring OSLRenderServices::u_material_index("material:index");
66 ustring OSLRenderServices::u_object_random("object:random");
67 ustring OSLRenderServices::u_particle_index("particle:index");
68 ustring OSLRenderServices::u_particle_age("particle:age");
69 ustring OSLRenderServices::u_particle_lifetime("particle:lifetime");
70 ustring OSLRenderServices::u_particle_location("particle:location");
71 ustring OSLRenderServices::u_particle_rotation("particle:rotation");
72 ustring OSLRenderServices::u_particle_size("particle:size");
73 ustring OSLRenderServices::u_particle_velocity("particle:velocity");
74 ustring OSLRenderServices::u_particle_angular_velocity("particle:angular_velocity");
75 ustring OSLRenderServices::u_geom_numpolyvertices("geom:numpolyvertices");
76 ustring OSLRenderServices::u_geom_trianglevertices("geom:trianglevertices");
77 ustring OSLRenderServices::u_geom_polyvertices("geom:polyvertices");
78 ustring OSLRenderServices::u_geom_name("geom:name");
79 ustring OSLRenderServices::u_is_smooth("geom:is_smooth");
80 #ifdef __HAIR__
81 ustring OSLRenderServices::u_is_curve("geom:is_curve");
82 ustring OSLRenderServices::u_curve_thickness("geom:curve_thickness");
83 ustring OSLRenderServices::u_curve_tangent_normal("geom:curve_tangent_normal");
84 #endif
85 ustring OSLRenderServices::u_path_ray_length("path:ray_length");
86 ustring OSLRenderServices::u_trace("trace");
87 ustring OSLRenderServices::u_hit("hit");
88 ustring OSLRenderServices::u_hitdist("hitdist");
89 ustring OSLRenderServices::u_N("N");
90 ustring OSLRenderServices::u_Ng("Ng");
91 ustring OSLRenderServices::u_P("P");
92 ustring OSLRenderServices::u_I("I");
93 ustring OSLRenderServices::u_u("u");
94 ustring OSLRenderServices::u_v("v");
95 ustring OSLRenderServices::u_empty;
96
97 OSLRenderServices::OSLRenderServices()
98 {
99         kernel_globals = NULL;
100 }
101
102 OSLRenderServices::~OSLRenderServices()
103 {
104 }
105
106 void OSLRenderServices::thread_init(KernelGlobals *kernel_globals_)
107 {
108         kernel_globals = kernel_globals_;
109 }
110
111 bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform, float time)
112 {
113         /* this is only used for shader and object space, we don't really have
114          * a concept of shader space, so we just use object space for both. */
115         if (xform) {
116                 const ShaderData *sd = (const ShaderData *)xform;
117                 KernelGlobals *kg = sd->osl_globals;
118                 int object = sd->object;
119
120                 if (object != ~0) {
121 #ifdef __OBJECT_MOTION__
122                         Transform tfm;
123
124                         if(time == sd->time)
125                                 tfm = sd->ob_tfm;
126                         else
127                                 tfm = object_fetch_transform_motion_test(kg, object, time, NULL);
128 #else
129                         Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM);
130 #endif
131                         tfm = transform_transpose(tfm);
132                         COPY_MATRIX44(&result, &tfm);
133
134                         return true;
135                 }
136         }
137
138         return false;
139 }
140
141 bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform, float time)
142 {
143         /* this is only used for shader and object space, we don't really have
144          * a concept of shader space, so we just use object space for both. */
145         if (xform) {
146                 const ShaderData *sd = (const ShaderData *)xform;
147                 KernelGlobals *kg = sd->osl_globals;
148                 int object = sd->object;
149
150                 if (object != ~0) {
151 #ifdef __OBJECT_MOTION__
152                         Transform itfm;
153
154                         if(time == sd->time)
155                                 itfm = sd->ob_itfm;
156                         else
157                                 object_fetch_transform_motion_test(kg, object, time, &itfm);
158 #else
159                         Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
160 #endif
161                         itfm = transform_transpose(itfm);
162                         COPY_MATRIX44(&result, &itfm);
163
164                         return true;
165                 }
166         }
167
168         return false;
169 }
170
171 bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, ustring from, float time)
172 {
173         KernelGlobals *kg = kernel_globals;
174
175         if (from == u_ndc) {
176                 Transform tfm = transform_transpose(transform_quick_inverse(kernel_data.cam.worldtondc));
177                 COPY_MATRIX44(&result, &tfm);
178                 return true;
179         }
180         else if (from == u_raster) {
181                 Transform tfm = transform_transpose(kernel_data.cam.rastertoworld);
182                 COPY_MATRIX44(&result, &tfm);
183                 return true;
184         }
185         else if (from == u_screen) {
186                 Transform tfm = transform_transpose(kernel_data.cam.screentoworld);
187                 COPY_MATRIX44(&result, &tfm);
188                 return true;
189         }
190         else if (from == u_camera) {
191                 Transform tfm = transform_transpose(kernel_data.cam.cameratoworld);
192                 COPY_MATRIX44(&result, &tfm);
193                 return true;
194         }
195
196         return false;
197 }
198
199 bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, ustring to, float time)
200 {
201         KernelGlobals *kg = kernel_globals;
202
203         if (to == u_ndc) {
204                 Transform tfm = transform_transpose(kernel_data.cam.worldtondc);
205                 COPY_MATRIX44(&result, &tfm);
206                 return true;
207         }
208         else if (to == u_raster) {
209                 Transform tfm = transform_transpose(kernel_data.cam.worldtoraster);
210                 COPY_MATRIX44(&result, &tfm);
211                 return true;
212         }
213         else if (to == u_screen) {
214                 Transform tfm = transform_transpose(kernel_data.cam.worldtoscreen);
215                 COPY_MATRIX44(&result, &tfm);
216                 return true;
217         }
218         else if (to == u_camera) {
219                 Transform tfm = transform_transpose(kernel_data.cam.worldtocamera);
220                 COPY_MATRIX44(&result, &tfm);
221                 return true;
222         }
223
224         return false;
225 }
226
227 bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform)
228 {
229         /* this is only used for shader and object space, we don't really have
230          * a concept of shader space, so we just use object space for both. */
231         if (xform) {
232                 const ShaderData *sd = (const ShaderData *)xform;
233                 int object = sd->object;
234
235                 if (object != ~0) {
236 #ifdef __OBJECT_MOTION__
237                         Transform tfm = sd->ob_tfm;
238 #else
239                         KernelGlobals *kg = sd->osl_globals;
240                         Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM);
241 #endif
242                         tfm = transform_transpose(tfm);
243                         COPY_MATRIX44(&result, &tfm);
244
245                         return true;
246                 }
247         }
248
249         return false;
250 }
251
252 bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform)
253 {
254         /* this is only used for shader and object space, we don't really have
255          * a concept of shader space, so we just use object space for both. */
256         if (xform) {
257                 const ShaderData *sd = (const ShaderData *)xform;
258                 int object = sd->object;
259
260                 if (object != ~0) {
261 #ifdef __OBJECT_MOTION__
262                         Transform tfm = sd->ob_itfm;
263 #else
264                         KernelGlobals *kg = sd->osl_globals;
265                         Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
266 #endif
267                         tfm = transform_transpose(tfm);
268                         COPY_MATRIX44(&result, &tfm);
269
270                         return true;
271                 }
272         }
273
274         return false;
275 }
276
277 bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, ustring from)
278 {
279         KernelGlobals *kg = kernel_globals;
280
281         if (from == u_ndc) {
282                 Transform tfm = transform_transpose(transform_quick_inverse(kernel_data.cam.worldtondc));
283                 COPY_MATRIX44(&result, &tfm);
284                 return true;
285         }
286         else if (from == u_raster) {
287                 Transform tfm = transform_transpose(kernel_data.cam.rastertoworld);
288                 COPY_MATRIX44(&result, &tfm);
289                 return true;
290         }
291         else if (from == u_screen) {
292                 Transform tfm = transform_transpose(kernel_data.cam.screentoworld);
293                 COPY_MATRIX44(&result, &tfm);
294                 return true;
295         }
296         else if (from == u_camera) {
297                 Transform tfm = transform_transpose(kernel_data.cam.cameratoworld);
298                 COPY_MATRIX44(&result, &tfm);
299                 return true;
300         }
301
302         return false;
303 }
304
305 bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, ustring to)
306 {
307         KernelGlobals *kg = kernel_globals;
308         
309         if (to == u_ndc) {
310                 Transform tfm = transform_transpose(kernel_data.cam.worldtondc);
311                 COPY_MATRIX44(&result, &tfm);
312                 return true;
313         }
314         else if (to == u_raster) {
315                 Transform tfm = transform_transpose(kernel_data.cam.worldtoraster);
316                 COPY_MATRIX44(&result, &tfm);
317                 return true;
318         }
319         else if (to == u_screen) {
320                 Transform tfm = transform_transpose(kernel_data.cam.worldtoscreen);
321                 COPY_MATRIX44(&result, &tfm);
322                 return true;
323         }
324         else if (to == u_camera) {
325                 Transform tfm = transform_transpose(kernel_data.cam.worldtocamera);
326                 COPY_MATRIX44(&result, &tfm);
327                 return true;
328         }
329         
330         return false;
331 }
332
333 bool OSLRenderServices::get_array_attribute(void *renderstate, bool derivatives, 
334                                             ustring object, TypeDesc type, ustring name,
335                                             int index, void *val)
336 {
337         return false;
338 }
339
340 static bool set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, void *val)
341 {
342         if (type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
343             type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
344         {
345                 float *fval = (float *)val;
346
347                 fval[0] = f[0].x;
348                 fval[1] = f[0].y;
349                 fval[2] = f[0].z;
350
351                 if (derivatives) {
352                         fval[3] = f[1].x;
353                         fval[4] = f[1].y;
354                         fval[5] = f[1].z;
355
356                         fval[6] = f[2].x;
357                         fval[7] = f[2].y;
358                         fval[8] = f[2].z;
359                 }
360
361                 return true;
362         }
363         else if(type == TypeDesc::TypeFloat) {
364                 float *fval = (float *)val;
365                 fval[0] = average(f[0]);
366
367                 if (derivatives) {
368                         fval[1] = average(f[1]);
369                         fval[2] = average(f[2]);
370                 }
371
372                 return true;
373         }
374
375         return false;
376 }
377
378 static bool set_attribute_float3(float3 f, TypeDesc type, bool derivatives, void *val)
379 {
380         float3 fv[3];
381
382         fv[0] = f;
383         fv[1] = make_float3(0.0f, 0.0f, 0.0f);
384         fv[2] = make_float3(0.0f, 0.0f, 0.0f);
385
386         return set_attribute_float3(fv, type, derivatives, val);
387 }
388
389 static bool set_attribute_float(float f[3], TypeDesc type, bool derivatives, void *val)
390 {
391         if (type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
392             type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
393         {
394                 float *fval = (float *)val;
395                 fval[0] = f[0];
396                 fval[1] = f[1];
397                 fval[2] = f[2];
398
399                 if (derivatives) {
400                         fval[3] = f[1];
401                         fval[4] = f[1];
402                         fval[5] = f[1];
403
404                         fval[6] = f[2];
405                         fval[7] = f[2];
406                         fval[8] = f[2];
407                 }
408
409                 return true;
410         }
411         else if(type == TypeDesc::TypeFloat) {
412                 float *fval = (float *)val;
413                 fval[0] = f[0];
414
415                 if (derivatives) {
416                         fval[1] = f[1];
417                         fval[2] = f[2];
418                 }
419
420                 return true;
421         }
422
423         return false;
424 }
425
426 static bool set_attribute_float(float f, TypeDesc type, bool derivatives, void *val)
427 {
428         float fv[3];
429
430         fv[0] = f;
431         fv[1] = 0.0f;
432         fv[2] = 0.0f;
433
434         return set_attribute_float(fv, type, derivatives, val);
435 }
436
437 static bool set_attribute_int(int i, TypeDesc type, bool derivatives, void *val)
438 {
439         if(type.basetype == TypeDesc::INT && type.aggregate == TypeDesc::SCALAR && type.arraylen == 0) {
440                 int *ival = (int *)val;
441                 ival[0] = i;
442
443                 if (derivatives) {
444                         ival[1] = 0;
445                         ival[2] = 0;
446                 }
447
448                 return true;
449         }
450
451         return false;
452 }
453
454 static bool set_attribute_string(ustring str, TypeDesc type, bool derivatives, void *val)
455 {
456         if(type.basetype == TypeDesc::INT && type.aggregate == TypeDesc::SCALAR && type.arraylen == 0) {
457                 ustring *sval = (ustring *)val;
458                 sval[0] = str;
459
460                 if (derivatives) {
461                         sval[1] = OSLRenderServices::u_empty;
462                         sval[2] = OSLRenderServices::u_empty;
463                 }
464
465                 return true;
466         }
467
468         return false;
469 }
470
471 static bool set_attribute_float3_3(float3 P[3], TypeDesc type, bool derivatives, void *val)
472 {
473         if(type.vecsemantics == TypeDesc::POINT && type.arraylen >= 3) {
474                 float *fval = (float *)val;
475
476                 fval[0] = P[0].x;
477                 fval[1] = P[0].y;
478                 fval[2] = P[0].z;
479
480                 fval[3] = P[1].x;
481                 fval[4] = P[1].y;
482                 fval[5] = P[1].z;
483
484                 fval[6] = P[2].x;
485                 fval[7] = P[2].y;
486                 fval[8] = P[2].z;
487
488                 if(type.arraylen > 3)
489                         memset(fval + 3*3, 0, sizeof(float)*3*(type.arraylen - 3));
490                 if (derivatives)
491                         memset(fval + type.arraylen*3, 0, sizeof(float)*2*3*type.arraylen);
492
493                 return true;
494         }
495
496         return false;
497 }
498
499 static bool get_mesh_attribute(KernelGlobals *kg, const ShaderData *sd, const OSLGlobals::Attribute& attr,
500                                const TypeDesc& type, bool derivatives, void *val)
501 {
502         if (attr.type == TypeDesc::TypePoint || attr.type == TypeDesc::TypeVector ||
503             attr.type == TypeDesc::TypeNormal || attr.type == TypeDesc::TypeColor)
504         {
505                 float3 fval[3];
506                 fval[0] = primitive_attribute_float3(kg, sd, attr.elem, attr.offset,
507                                                      (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL);
508                 return set_attribute_float3(fval, type, derivatives, val);
509         }
510         else if (attr.type == TypeDesc::TypeFloat) {
511                 float fval[3];
512                 fval[0] = primitive_attribute_float(kg, sd, attr.elem, attr.offset,
513                                                     (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL);
514                 return set_attribute_float(fval, type, derivatives, val);
515         }
516         else {
517                 return false;
518         }
519 }
520
521 static void get_object_attribute(const OSLGlobals::Attribute& attr, bool derivatives, void *val)
522 {
523         size_t datasize = attr.value.datasize();
524
525         memcpy(val, attr.value.data(), datasize);
526         if (derivatives)
527                 memset((char *)val + datasize, 0, datasize * 2);
528 }
529
530 bool OSLRenderServices::get_object_standard_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
531                                                       TypeDesc type, bool derivatives, void *val)
532 {
533         /* todo: turn this into hash table? */
534
535         /* Object Attributes */
536         if (name == u_object_location) {
537                 float3 f = object_location(kg, sd);
538                 return set_attribute_float3(f, type, derivatives, val);
539         }
540         else if (name == u_object_index) {
541                 float f = object_pass_id(kg, sd->object);
542                 return set_attribute_float(f, type, derivatives, val);
543         }
544         else if (name == u_geom_dupli_generated) {
545                 float3 f = object_dupli_generated(kg, sd->object);
546                 return set_attribute_float3(f, type, derivatives, val);
547         }
548         else if (name == u_geom_dupli_uv) {
549                 float3 f = object_dupli_uv(kg, sd->object);
550                 return set_attribute_float3(f, type, derivatives, val);
551         }
552         else if (name == u_material_index) {
553                 float f = shader_pass_id(kg, sd);
554                 return set_attribute_float(f, type, derivatives, val);
555         }
556         else if (name == u_object_random) {
557                 float f = object_random_number(kg, sd->object);
558                 return set_attribute_float(f, type, derivatives, val);
559         }
560
561         /* Particle Attributes */
562         else if (name == u_particle_index) {
563                 uint particle_id = object_particle_id(kg, sd->object);
564                 float f = particle_index(kg, particle_id);
565                 return set_attribute_float(f, type, derivatives, val);
566         }
567         else if (name == u_particle_age) {
568                 uint particle_id = object_particle_id(kg, sd->object);
569                 float f = particle_age(kg, particle_id);
570                 return set_attribute_float(f, type, derivatives, val);
571         }
572         else if (name == u_particle_lifetime) {
573                 uint particle_id = object_particle_id(kg, sd->object);
574                 float f= particle_lifetime(kg, particle_id);
575                 return set_attribute_float(f, type, derivatives, val);
576         }
577         else if (name == u_particle_location) {
578                 uint particle_id = object_particle_id(kg, sd->object);
579                 float3 f = particle_location(kg, particle_id);
580                 return set_attribute_float3(f, type, derivatives, val);
581         }
582 #if 0   /* unsupported */
583         else if (name == u_particle_rotation) {
584                 uint particle_id = object_particle_id(kg, sd->object);
585                 float4 f = particle_rotation(kg, particle_id);
586                 return set_attribute_float4(f, type, derivatives, val);
587         }
588 #endif
589         else if (name == u_particle_size) {
590                 uint particle_id = object_particle_id(kg, sd->object);
591                 float f = particle_size(kg, particle_id);
592                 return set_attribute_float(f, type, derivatives, val);
593         }
594         else if (name == u_particle_velocity) {
595                 uint particle_id = object_particle_id(kg, sd->object);
596                 float3 f = particle_velocity(kg, particle_id);
597                 return set_attribute_float3(f, type, derivatives, val);
598         }
599         else if (name == u_particle_angular_velocity) {
600                 uint particle_id = object_particle_id(kg, sd->object);
601                 float3 f = particle_angular_velocity(kg, particle_id);
602                 return set_attribute_float3(f, type, derivatives, val);
603         }
604         
605         /* Geometry Attributes */
606         else if (name == u_geom_numpolyvertices) {
607                 return set_attribute_int(3, type, derivatives, val);
608         }
609         else if ((name == u_geom_trianglevertices || name == u_geom_polyvertices)
610 #ifdef __HAIR__
611                      && sd->segment == ~0) {
612 #else
613                 ) {
614 #endif
615                 float3 P[3];
616                 triangle_vertices(kg, sd->prim, P);
617
618                 if(!(sd->flag & SD_TRANSFORM_APPLIED)) {
619                         object_position_transform(kg, sd, &P[0]);
620                         object_position_transform(kg, sd, &P[1]);
621                         object_position_transform(kg, sd, &P[2]);
622                 }
623
624                 return set_attribute_float3_3(P, type, derivatives, val);
625         }
626         else if(name == u_geom_name) {
627                 ustring object_name = kg->osl->object_names[sd->object];
628                 return set_attribute_string(object_name, type, derivatives, val);
629         }
630         else if (name == u_is_smooth) {
631                 float f = ((sd->shader & SHADER_SMOOTH_NORMAL) != 0);
632                 return set_attribute_float(f, type, derivatives, val);
633         }
634 #ifdef __HAIR__
635         /* Hair Attributes */
636         else if (name == u_is_curve) {
637                 float f = (sd->segment != ~0);
638                 return set_attribute_float(f, type, derivatives, val);
639         }
640         else if (name == u_curve_thickness) {
641                 float f = curve_thickness(kg, sd);
642                 return set_attribute_float(f, type, derivatives, val);
643         }
644         else if (name == u_curve_tangent_normal) {
645                 float3 f = curve_tangent_normal(kg, sd);
646                 return set_attribute_float3(f, type, derivatives, val);
647         }
648 #endif
649         else
650                 return false;
651 }
652
653 bool OSLRenderServices::get_background_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
654                                                  TypeDesc type, bool derivatives, void *val)
655 {
656         /* Ray Length */
657         if (name == u_path_ray_length) {
658                 float f = sd->ray_length;
659                 return set_attribute_float(f, type, derivatives, val);
660         }
661         
662         else
663                 return false;
664 }
665
666 bool OSLRenderServices::get_attribute(void *renderstate, bool derivatives, ustring object_name,
667                                       TypeDesc type, ustring name, void *val)
668 {
669         ShaderData *sd = (ShaderData *)renderstate;
670         KernelGlobals *kg = sd->osl_globals;
671         int object, prim, segment;
672
673         /* lookup of attribute on another object */
674         if (object_name != u_empty || sd == NULL) {
675                 OSLGlobals::ObjectNameMap::iterator it = kg->osl->object_name_map.find(object_name);
676
677                 if (it == kg->osl->object_name_map.end())
678                         return false;
679
680                 object = it->second;
681                 prim = ~0;
682                 segment = ~0;
683         }
684         else {
685                 object = sd->object;
686                 prim = sd->prim;
687 #ifdef __HAIR__
688                 segment = sd->segment;
689 #else
690                 segment = ~0;
691 #endif
692
693                 if (object == ~0)
694                         return get_background_attribute(kg, sd, name, type, derivatives, val);
695         }
696
697         /* find attribute on object */
698         object = object*ATTR_PRIM_TYPES + (segment != ~0);
699         OSLGlobals::AttributeMap& attribute_map = kg->osl->attribute_map[object];
700         OSLGlobals::AttributeMap::iterator it = attribute_map.find(name);
701
702         if (it != attribute_map.end()) {
703                 const OSLGlobals::Attribute& attr = it->second;
704
705                 if (attr.elem != ATTR_ELEMENT_VALUE) {
706                         /* triangle and vertex attributes */
707                         if (prim != ~0)
708                                 return get_mesh_attribute(kg, sd, attr, type, derivatives, val);
709                 }
710                 else {
711                         /* object attribute */
712                         get_object_attribute(attr, derivatives, val);
713                         return true;
714                 }
715         }
716         else {
717                 /* not found in attribute, check standard object info */
718                 bool is_std_object_attribute = get_object_standard_attribute(kg, sd, name, type, derivatives, val);
719
720                 if (is_std_object_attribute)
721                         return true;
722
723                 return get_background_attribute(kg, sd, name, type, derivatives, val);
724         }
725
726         return false;
727 }
728
729 bool OSLRenderServices::get_userdata(bool derivatives, ustring name, TypeDesc type, 
730                                      void *renderstate, void *val)
731 {
732         return false; /* disabled by lockgeom */
733 }
734
735 bool OSLRenderServices::has_userdata(ustring name, TypeDesc type, void *renderstate)
736 {
737         return false; /* never called by OSL */
738 }
739
740 bool OSLRenderServices::texture(ustring filename, TextureOpt &options,
741                                 OSL::ShaderGlobals *sg,
742                                 float s, float t, float dsdx, float dtdx,
743                                 float dsdy, float dtdy, float *result)
744 {
745         OSL::TextureSystem *ts = kernel_globals->osl->ts;
746         bool status = ts->texture(filename, options, s, t, dsdx, dtdx, dsdy, dtdy, result);
747
748         if(!status) {
749                 if(options.nchannels == 3 || options.nchannels == 4) {
750                         result[0] = 1.0f;
751                         result[1] = 0.0f;
752                         result[2] = 1.0f;
753
754                         if(options.nchannels == 4)
755                                 result[3] = 1.0f;
756                 }
757         }
758
759         return status;
760 }
761
762 bool OSLRenderServices::texture3d(ustring filename, TextureOpt &options,
763                                   OSL::ShaderGlobals *sg, const OSL::Vec3 &P,
764                                   const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy,
765                                   const OSL::Vec3 &dPdz, float *result)
766 {
767         OSL::TextureSystem *ts = kernel_globals->osl->ts;
768         bool status = ts->texture3d(filename, options, P, dPdx, dPdy, dPdz, result);
769
770         if(!status) {
771                 if(options.nchannels == 3 || options.nchannels == 4) {
772                         result[0] = 1.0f;
773                         result[1] = 0.0f;
774                         result[2] = 1.0f;
775
776                         if(options.nchannels == 4)
777                                 result[3] = 1.0f;
778                 }
779
780         }
781
782         return status;
783 }
784
785 bool OSLRenderServices::environment(ustring filename, TextureOpt &options,
786                                     OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
787                                     const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy, float *result)
788 {
789         OSL::TextureSystem *ts = kernel_globals->osl->ts;
790         bool status = ts->environment(filename, options, R, dRdx, dRdy, result);
791
792         if(!status) {
793                 if(options.nchannels == 3 || options.nchannels == 4) {
794                         result[0] = 1.0f;
795                         result[1] = 0.0f;
796                         result[2] = 1.0f;
797
798                         if(options.nchannels == 4)
799                                 result[3] = 1.0f;
800                 }
801         }
802
803         return status;
804 }
805
806 bool OSLRenderServices::get_texture_info(ustring filename, int subimage,
807                                          ustring dataname,
808                                          TypeDesc datatype, void *data)
809 {
810         OSL::TextureSystem *ts = kernel_globals->osl->ts;
811         return ts->get_texture_info(filename, subimage, dataname, datatype, data);
812 }
813
814 int OSLRenderServices::pointcloud_search(OSL::ShaderGlobals *sg, ustring filename, const OSL::Vec3 &center,
815                                          float radius, int max_points, bool sort,
816                                          size_t *out_indices, float *out_distances, int derivs_offset)
817 {
818         return 0;
819 }
820
821 int OSLRenderServices::pointcloud_get(ustring filename, size_t *indices, int count,
822                                       ustring attr_name, TypeDesc attr_type, void *out_data)
823 {
824         return 0;
825 }
826
827 bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg,
828         const OSL::Vec3 &P, const OSL::Vec3 &dPdx,
829         const OSL::Vec3 &dPdy, const OSL::Vec3 &R,
830         const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy)
831 {
832         /* todo: options.shader support, maybe options.traceset */
833         ShaderData *sd = (ShaderData *)(sg->renderstate);
834
835         /* setup ray */
836         Ray ray;
837
838         ray.P = TO_FLOAT3(P);
839         ray.D = TO_FLOAT3(R);
840         ray.t = (options.maxdist == 1.0e30)? FLT_MAX: options.maxdist - options.mindist;
841         ray.time = sd->time;
842
843         if(options.mindist == 0.0f) {
844                 /* avoid self-intersections */
845                 if(ray.P == sd->P) {
846                         bool transmit = (dot(sd->Ng, ray.D) < 0.0f);
847                         ray.P = ray_offset(sd->P, (transmit)? -sd->Ng: sd->Ng);
848                 }
849         }
850         else {
851                 /* offset for minimum distance */
852                 ray.P += options.mindist*ray.D;
853         }
854
855         /* ray differentials */
856         ray.dP.dx = TO_FLOAT3(dPdx);
857         ray.dP.dy = TO_FLOAT3(dPdy);
858         ray.dD.dx = TO_FLOAT3(dRdx);
859         ray.dD.dy = TO_FLOAT3(dRdy);
860
861         /* allocate trace data */
862         OSLTraceData *tracedata = (OSLTraceData*)sg->tracedata;
863         tracedata->ray = ray;
864         tracedata->setup = false;
865         tracedata->init = true;
866
867         /* raytrace */
868         return scene_intersect(sd->osl_globals, &ray, ~0, &tracedata->isect, NULL, 0.0f, 0.0f);
869 }
870
871
872 bool OSLRenderServices::getmessage(OSL::ShaderGlobals *sg, ustring source, ustring name,
873         TypeDesc type, void *val, bool derivatives)
874 {
875         OSLTraceData *tracedata = (OSLTraceData*)sg->tracedata;
876
877         if(source == u_trace && tracedata->init) {
878                 if(name == u_hit) {
879                         return set_attribute_int((tracedata->isect.prim != ~0), type, derivatives, val);
880                 }
881                 else if(tracedata->isect.prim != ~0) {
882                         if(name == u_hitdist) {
883                                 float f[3] = {tracedata->isect.t, 0.0f, 0.0f};
884                                 return set_attribute_float(f, type, derivatives, val);
885                         }
886                         else {
887                                 ShaderData *sd = &tracedata->sd;
888                                 KernelGlobals *kg = sd->osl_globals;
889
890                                 if(!tracedata->setup) {
891                                         /* lazy shader data setup */
892                                         shader_setup_from_ray(kg, sd, &tracedata->isect, &tracedata->ray);
893                                         tracedata->setup = true;
894                                 }
895
896                                 if(name == u_N) {
897                                         return set_attribute_float3(sd->N, type, derivatives, val);
898                                 }
899                                 else if(name == u_Ng) {
900                                         return set_attribute_float3(sd->Ng, type, derivatives, val);
901                                 }
902                                 else if(name == u_P) {
903                                         float3 f[3] = {sd->P, sd->dP.dx, sd->dP.dy};
904                                         return set_attribute_float3(f, type, derivatives, val);
905                                 }
906                                 else if(name == u_I) {
907                                         float3 f[3] = {sd->I, sd->dI.dx, sd->dI.dy};
908                                         return set_attribute_float3(f, type, derivatives, val);
909                                 }
910                                 else if(name == u_u) {
911                                         float f[3] = {sd->u, sd->du.dx, sd->du.dy};
912                                         return set_attribute_float(f, type, derivatives, val);
913                                 }
914                                 else if(name == u_v) {
915                                         float f[3] = {sd->v, sd->dv.dx, sd->dv.dy};
916                                         return set_attribute_float(f, type, derivatives, val);
917                                 }
918
919                                 return get_attribute(sd, derivatives, u_empty, type, name, val);
920                         }
921                 }
922         }
923
924         return false;
925 }
926
927 CCL_NAMESPACE_END