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