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