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