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