Merge branch 'master' into blender2.8
[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 /* TODO(sergey): There is a bit of headers dependency hell going on
18  * here, so for now we just put here. In the future it might be better
19  * to have dedicated file for such tweaks.
20  */
21 #if (defined(__GNUC__) && !defined(__clang__)) && defined(NDEBUG)
22 #  pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
23 #  pragma GCC diagnostic ignored "-Wuninitialized"
24 #endif
25
26 #include <string.h>
27
28 #include "render/mesh.h"
29 #include "render/object.h"
30 #include "render/scene.h"
31
32 #include "kernel/osl/osl_closures.h"
33 #include "kernel/osl/osl_globals.h"
34 #include "kernel/osl/osl_services.h"
35 #include "kernel/osl/osl_shader.h"
36
37 #include "util/util_foreach.h"
38 #include "util/util_logging.h"
39 #include "util/util_string.h"
40
41 #include "kernel/kernel_compat_cpu.h"
42 #include "kernel/split/kernel_split_data_types.h"
43 #include "kernel/kernel_globals.h"
44 #include "kernel/kernel_random.h"
45 #include "kernel/kernel_projection.h"
46 #include "kernel/kernel_differential.h"
47 #include "kernel/kernel_montecarlo.h"
48 #include "kernel/kernel_camera.h"
49 #include "kernel/kernels/cpu/kernel_cpu_image.h"
50 #include "kernel/geom/geom.h"
51 #include "kernel/bvh/bvh.h"
52
53 #include "kernel/kernel_projection.h"
54 #include "kernel/kernel_accumulate.h"
55 #include "kernel/kernel_shader.h"
56
57 #ifdef WITH_PTEX
58 #  include <Ptexture.h>
59 #endif
60
61 CCL_NAMESPACE_BEGIN
62
63 /* RenderServices implementation */
64
65 #define COPY_MATRIX44(m1, m2)  { \
66         CHECK_TYPE(m1, OSL::Matrix44*); \
67         CHECK_TYPE(m2, Transform*); \
68         memcpy(m1, m2, sizeof(*m2)); \
69 } (void)0
70
71 /* static ustrings */
72 ustring OSLRenderServices::u_distance("distance");
73 ustring OSLRenderServices::u_index("index");
74 ustring OSLRenderServices::u_world("world");
75 ustring OSLRenderServices::u_camera("camera");
76 ustring OSLRenderServices::u_screen("screen");
77 ustring OSLRenderServices::u_raster("raster");
78 ustring OSLRenderServices::u_ndc("NDC");
79 ustring OSLRenderServices::u_object_location("object:location");
80 ustring OSLRenderServices::u_object_index("object:index");
81 ustring OSLRenderServices::u_geom_dupli_generated("geom:dupli_generated");
82 ustring OSLRenderServices::u_geom_dupli_uv("geom:dupli_uv");
83 ustring OSLRenderServices::u_material_index("material:index");
84 ustring OSLRenderServices::u_object_random("object:random");
85 ustring OSLRenderServices::u_particle_random("particle:random");
86 ustring OSLRenderServices::u_particle_age("particle:age");
87 ustring OSLRenderServices::u_particle_lifetime("particle:lifetime");
88 ustring OSLRenderServices::u_particle_location("particle:location");
89 ustring OSLRenderServices::u_particle_rotation("particle:rotation");
90 ustring OSLRenderServices::u_particle_size("particle:size");
91 ustring OSLRenderServices::u_particle_velocity("particle:velocity");
92 ustring OSLRenderServices::u_particle_angular_velocity("particle:angular_velocity");
93 ustring OSLRenderServices::u_geom_numpolyvertices("geom:numpolyvertices");
94 ustring OSLRenderServices::u_geom_trianglevertices("geom:trianglevertices");
95 ustring OSLRenderServices::u_geom_polyvertices("geom:polyvertices");
96 ustring OSLRenderServices::u_geom_name("geom:name");
97 ustring OSLRenderServices::u_geom_undisplaced("geom:undisplaced");
98 ustring OSLRenderServices::u_is_smooth("geom:is_smooth");
99 ustring OSLRenderServices::u_is_curve("geom:is_curve");
100 ustring OSLRenderServices::u_curve_thickness("geom:curve_thickness");
101 ustring OSLRenderServices::u_curve_tangent_normal("geom:curve_tangent_normal");
102 ustring OSLRenderServices::u_curve_random("geom:curve_random");
103 ustring OSLRenderServices::u_path_ray_length("path:ray_length");
104 ustring OSLRenderServices::u_path_ray_depth("path:ray_depth");
105 ustring OSLRenderServices::u_path_diffuse_depth("path:diffuse_depth");
106 ustring OSLRenderServices::u_path_glossy_depth("path:glossy_depth");
107 ustring OSLRenderServices::u_path_transparent_depth("path:transparent_depth");
108 ustring OSLRenderServices::u_path_transmission_depth("path:transmission_depth");
109 ustring OSLRenderServices::u_trace("trace");
110 ustring OSLRenderServices::u_hit("hit");
111 ustring OSLRenderServices::u_hitdist("hitdist");
112 ustring OSLRenderServices::u_N("N");
113 ustring OSLRenderServices::u_Ng("Ng");
114 ustring OSLRenderServices::u_P("P");
115 ustring OSLRenderServices::u_I("I");
116 ustring OSLRenderServices::u_u("u");
117 ustring OSLRenderServices::u_v("v");
118 ustring OSLRenderServices::u_empty;
119 ustring OSLRenderServices::u_at_bevel("@bevel");
120
121 OSLRenderServices::OSLRenderServices()
122 {
123         kernel_globals = NULL;
124         osl_ts = NULL;
125
126 #ifdef WITH_PTEX
127         size_t maxmem = 16384 * 1024;
128         ptex_cache = PtexCache::create(0, maxmem);
129 #endif
130 }
131
132 OSLRenderServices::~OSLRenderServices()
133 {
134         if(osl_ts) {
135                 VLOG(2) << "OSL texture system stats:\n"
136                         << osl_ts->getstats();
137         }
138 #ifdef WITH_PTEX
139         ptex_cache->release();
140 #endif
141 }
142
143 void OSLRenderServices::thread_init(KernelGlobals *kernel_globals_, OSL::TextureSystem *osl_ts_)
144 {
145         kernel_globals = kernel_globals_;
146         osl_ts = osl_ts_;
147 }
148
149 bool OSLRenderServices::get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time)
150 {
151         /* this is only used for shader and object space, we don't really have
152          * a concept of shader space, so we just use object space for both. */
153         if(xform) {
154                 const ShaderData *sd = (const ShaderData *)xform;
155                 KernelGlobals *kg = sd->osl_globals;
156                 int object = sd->object;
157
158                 if(object != OBJECT_NONE) {
159 #ifdef __OBJECT_MOTION__
160                         Transform tfm;
161
162                         if(time == sd->time)
163                                 tfm = sd->ob_tfm;
164                         else
165                                 tfm = object_fetch_transform_motion_test(kg, object, time, NULL);
166 #else
167                         Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM);
168 #endif
169                         tfm = transform_transpose(tfm);
170                         COPY_MATRIX44(&result, &tfm);
171
172                         return true;
173                 }
174                 else if(sd->type == PRIMITIVE_LAMP) {
175                         Transform tfm = transform_transpose(sd->ob_tfm);
176                         COPY_MATRIX44(&result, &tfm);
177
178                         return true;
179                 }
180         }
181
182         return false;
183 }
184
185 bool OSLRenderServices::get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time)
186 {
187         /* this is only used for shader and object space, we don't really have
188          * a concept of shader space, so we just use object space for both. */
189         if(xform) {
190                 const ShaderData *sd = (const ShaderData *)xform;
191                 KernelGlobals *kg = sd->osl_globals;
192                 int object = sd->object;
193
194                 if(object != OBJECT_NONE) {
195 #ifdef __OBJECT_MOTION__
196                         Transform itfm;
197
198                         if(time == sd->time)
199                                 itfm = sd->ob_itfm;
200                         else
201                                 object_fetch_transform_motion_test(kg, object, time, &itfm);
202 #else
203                         Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
204 #endif
205                         itfm = transform_transpose(itfm);
206                         COPY_MATRIX44(&result, &itfm);
207
208                         return true;
209                 }
210                 else if(sd->type == PRIMITIVE_LAMP) {
211                         Transform tfm = transform_transpose(sd->ob_itfm);
212                         COPY_MATRIX44(&result, &tfm);
213
214                         return true;
215                 }
216         }
217
218         return false;
219 }
220
221 bool OSLRenderServices::get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from, float time)
222 {
223         KernelGlobals *kg = kernel_globals;
224
225         if(from == u_ndc) {
226                 Transform tfm = transform_transpose(transform_quick_inverse(kernel_data.cam.worldtondc));
227                 COPY_MATRIX44(&result, &tfm);
228                 return true;
229         }
230         else if(from == u_raster) {
231                 Transform tfm = transform_transpose(kernel_data.cam.rastertoworld);
232                 COPY_MATRIX44(&result, &tfm);
233                 return true;
234         }
235         else if(from == u_screen) {
236                 Transform tfm = transform_transpose(kernel_data.cam.screentoworld);
237                 COPY_MATRIX44(&result, &tfm);
238                 return true;
239         }
240         else if(from == u_camera) {
241                 Transform tfm = transform_transpose(kernel_data.cam.cameratoworld);
242                 COPY_MATRIX44(&result, &tfm);
243                 return true;
244         }
245         else if(from == u_world) {
246                 result.makeIdentity();
247                 return true;
248         }
249
250         return false;
251 }
252
253 bool OSLRenderServices::get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring to, float time)
254 {
255         KernelGlobals *kg = kernel_globals;
256
257         if(to == u_ndc) {
258                 Transform tfm = transform_transpose(kernel_data.cam.worldtondc);
259                 COPY_MATRIX44(&result, &tfm);
260                 return true;
261         }
262         else if(to == u_raster) {
263                 Transform tfm = transform_transpose(kernel_data.cam.worldtoraster);
264                 COPY_MATRIX44(&result, &tfm);
265                 return true;
266         }
267         else if(to == u_screen) {
268                 Transform tfm = transform_transpose(kernel_data.cam.worldtoscreen);
269                 COPY_MATRIX44(&result, &tfm);
270                 return true;
271         }
272         else if(to == u_camera) {
273                 Transform tfm = transform_transpose(kernel_data.cam.worldtocamera);
274                 COPY_MATRIX44(&result, &tfm);
275                 return true;
276         }
277         else if(to == u_world) {
278                 result.makeIdentity();
279                 return true;
280         }
281
282         return false;
283 }
284
285 bool OSLRenderServices::get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform)
286 {
287         /* this is only used for shader and object space, we don't really have
288          * a concept of shader space, so we just use object space for both. */
289         if(xform) {
290                 const ShaderData *sd = (const ShaderData *)xform;
291                 int object = sd->object;
292
293                 if(object != OBJECT_NONE) {
294 #ifdef __OBJECT_MOTION__
295                         Transform tfm = sd->ob_tfm;
296 #else
297                         KernelGlobals *kg = sd->osl_globals;
298                         Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM);
299 #endif
300                         tfm = transform_transpose(tfm);
301                         COPY_MATRIX44(&result, &tfm);
302
303                         return true;
304                 }
305                 else if(sd->type == PRIMITIVE_LAMP) {
306                         Transform tfm = transform_transpose(sd->ob_tfm);
307                         COPY_MATRIX44(&result, &tfm);
308
309                         return true;
310                 }
311         }
312
313         return false;
314 }
315
316 bool OSLRenderServices::get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform)
317 {
318         /* this is only used for shader and object space, we don't really have
319          * a concept of shader space, so we just use object space for both. */
320         if(xform) {
321                 const ShaderData *sd = (const ShaderData *)xform;
322                 int object = sd->object;
323
324                 if(object != OBJECT_NONE) {
325 #ifdef __OBJECT_MOTION__
326                         Transform tfm = sd->ob_itfm;
327 #else
328                         KernelGlobals *kg = sd->osl_globals;
329                         Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
330 #endif
331                         tfm = transform_transpose(tfm);
332                         COPY_MATRIX44(&result, &tfm);
333
334                         return true;
335                 }
336                 else if(sd->type == PRIMITIVE_LAMP) {
337                         Transform tfm = transform_transpose(sd->ob_itfm);
338                         COPY_MATRIX44(&result, &tfm);
339
340                         return true;
341                 }
342         }
343
344         return false;
345 }
346
347 bool OSLRenderServices::get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from)
348 {
349         KernelGlobals *kg = kernel_globals;
350
351         if(from == u_ndc) {
352                 Transform tfm = transform_transpose(transform_quick_inverse(kernel_data.cam.worldtondc));
353                 COPY_MATRIX44(&result, &tfm);
354                 return true;
355         }
356         else if(from == u_raster) {
357                 Transform tfm = transform_transpose(kernel_data.cam.rastertoworld);
358                 COPY_MATRIX44(&result, &tfm);
359                 return true;
360         }
361         else if(from == u_screen) {
362                 Transform tfm = transform_transpose(kernel_data.cam.screentoworld);
363                 COPY_MATRIX44(&result, &tfm);
364                 return true;
365         }
366         else if(from == u_camera) {
367                 Transform tfm = transform_transpose(kernel_data.cam.cameratoworld);
368                 COPY_MATRIX44(&result, &tfm);
369                 return true;
370         }
371
372         return false;
373 }
374
375 bool OSLRenderServices::get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring to)
376 {
377         KernelGlobals *kg = kernel_globals;
378         
379         if(to == u_ndc) {
380                 Transform tfm = transform_transpose(kernel_data.cam.worldtondc);
381                 COPY_MATRIX44(&result, &tfm);
382                 return true;
383         }
384         else if(to == u_raster) {
385                 Transform tfm = transform_transpose(kernel_data.cam.worldtoraster);
386                 COPY_MATRIX44(&result, &tfm);
387                 return true;
388         }
389         else if(to == u_screen) {
390                 Transform tfm = transform_transpose(kernel_data.cam.worldtoscreen);
391                 COPY_MATRIX44(&result, &tfm);
392                 return true;
393         }
394         else if(to == u_camera) {
395                 Transform tfm = transform_transpose(kernel_data.cam.worldtocamera);
396                 COPY_MATRIX44(&result, &tfm);
397                 return true;
398         }
399         
400         return false;
401 }
402
403 bool OSLRenderServices::get_array_attribute(OSL::ShaderGlobals *sg, bool derivatives, 
404                                             ustring object, TypeDesc type, ustring name,
405                                             int index, void *val)
406 {
407         return false;
408 }
409
410 static bool set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, void *val)
411 {
412         if(type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
413            type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
414         {
415                 float *fval = (float *)val;
416
417                 fval[0] = f[0].x;
418                 fval[1] = f[0].y;
419                 fval[2] = f[0].z;
420
421                 if(derivatives) {
422                         fval[3] = f[1].x;
423                         fval[4] = f[1].y;
424                         fval[5] = f[1].z;
425
426                         fval[6] = f[2].x;
427                         fval[7] = f[2].y;
428                         fval[8] = f[2].z;
429                 }
430
431                 return true;
432         }
433         else if(type == TypeDesc::TypeFloat) {
434                 float *fval = (float *)val;
435                 fval[0] = average(f[0]);
436
437                 if(derivatives) {
438                         fval[1] = average(f[1]);
439                         fval[2] = average(f[2]);
440                 }
441
442                 return true;
443         }
444
445         return false;
446 }
447
448 static bool set_attribute_float3(float3 f, TypeDesc type, bool derivatives, void *val)
449 {
450         float3 fv[3];
451
452         fv[0] = f;
453         fv[1] = make_float3(0.0f, 0.0f, 0.0f);
454         fv[2] = make_float3(0.0f, 0.0f, 0.0f);
455
456         return set_attribute_float3(fv, type, derivatives, val);
457 }
458
459 static bool set_attribute_float(float f[3], TypeDesc type, bool derivatives, void *val)
460 {
461         if(type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
462            type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
463         {
464                 float *fval = (float *)val;
465                 fval[0] = f[0];
466                 fval[1] = f[1];
467                 fval[2] = f[2];
468
469                 if(derivatives) {
470                         fval[3] = f[1];
471                         fval[4] = f[1];
472                         fval[5] = f[1];
473
474                         fval[6] = f[2];
475                         fval[7] = f[2];
476                         fval[8] = f[2];
477                 }
478
479                 return true;
480         }
481         else if(type == TypeDesc::TypeFloat) {
482                 float *fval = (float *)val;
483                 fval[0] = f[0];
484
485                 if(derivatives) {
486                         fval[1] = f[1];
487                         fval[2] = f[2];
488                 }
489
490                 return true;
491         }
492
493         return false;
494 }
495
496 static bool set_attribute_float(float f, TypeDesc type, bool derivatives, void *val)
497 {
498         float fv[3];
499
500         fv[0] = f;
501         fv[1] = 0.0f;
502         fv[2] = 0.0f;
503
504         return set_attribute_float(fv, type, derivatives, val);
505 }
506
507 static bool set_attribute_int(int i, TypeDesc type, bool derivatives, void *val)
508 {
509         if(type.basetype == TypeDesc::INT && type.aggregate == TypeDesc::SCALAR && type.arraylen == 0) {
510                 int *ival = (int *)val;
511                 ival[0] = i;
512
513                 if(derivatives) {
514                         ival[1] = 0;
515                         ival[2] = 0;
516                 }
517
518                 return true;
519         }
520
521         return false;
522 }
523
524 static bool set_attribute_string(ustring str, TypeDesc type, bool derivatives, void *val)
525 {
526         if(type.basetype == TypeDesc::STRING && type.aggregate == TypeDesc::SCALAR && type.arraylen == 0) {
527                 ustring *sval = (ustring *)val;
528                 sval[0] = str;
529
530                 if(derivatives) {
531                         sval[1] = OSLRenderServices::u_empty;
532                         sval[2] = OSLRenderServices::u_empty;
533                 }
534
535                 return true;
536         }
537
538         return false;
539 }
540
541 static bool set_attribute_float3_3(float3 P[3], TypeDesc type, bool derivatives, void *val)
542 {
543         if(type.vecsemantics == TypeDesc::POINT && type.arraylen >= 3) {
544                 float *fval = (float *)val;
545
546                 fval[0] = P[0].x;
547                 fval[1] = P[0].y;
548                 fval[2] = P[0].z;
549
550                 fval[3] = P[1].x;
551                 fval[4] = P[1].y;
552                 fval[5] = P[1].z;
553
554                 fval[6] = P[2].x;
555                 fval[7] = P[2].y;
556                 fval[8] = P[2].z;
557
558                 if(type.arraylen > 3)
559                         memset(fval + 3*3, 0, sizeof(float)*3*(type.arraylen - 3));
560                 if(derivatives)
561                         memset(fval + type.arraylen*3, 0, sizeof(float)*2*3*type.arraylen);
562
563                 return true;
564         }
565
566         return false;
567 }
568
569 static bool set_attribute_matrix(const Transform& tfm, TypeDesc type, void *val)
570 {
571         if(type == TypeDesc::TypeMatrix) {
572                 Transform transpose = transform_transpose(tfm);
573                 memcpy(val, &transpose, sizeof(Transform));
574                 return true;
575         }
576
577         return false;
578 }
579
580 static bool get_mesh_element_attribute(KernelGlobals *kg, const ShaderData *sd, const OSLGlobals::Attribute& attr,
581                                const TypeDesc& type, bool derivatives, void *val)
582 {
583         if(attr.type == TypeDesc::TypePoint || attr.type == TypeDesc::TypeVector ||
584            attr.type == TypeDesc::TypeNormal || attr.type == TypeDesc::TypeColor)
585         {
586                 float3 fval[3];
587                 fval[0] = primitive_attribute_float3(kg, sd, attr.desc,
588                                                      (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL);
589                 return set_attribute_float3(fval, type, derivatives, val);
590         }
591         else if(attr.type == TypeDesc::TypeFloat) {
592                 float fval[3];
593                 fval[0] = primitive_attribute_float(kg, sd, attr.desc,
594                                                     (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL);
595                 return set_attribute_float(fval, type, derivatives, val);
596         }
597         else {
598                 return false;
599         }
600 }
601
602 static bool get_mesh_attribute(KernelGlobals *kg, const ShaderData *sd, const OSLGlobals::Attribute& attr,
603                                const TypeDesc& type, bool derivatives, void *val)
604 {
605         if(attr.type == TypeDesc::TypeMatrix) {
606                 Transform tfm = primitive_attribute_matrix(kg, sd, attr.desc);
607                 return set_attribute_matrix(tfm, type, val);
608         }
609         else {
610                 return false;
611         }
612 }
613
614 static void get_object_attribute(const OSLGlobals::Attribute& attr, bool derivatives, void *val)
615 {
616         size_t datasize = attr.value.datasize();
617
618         memcpy(val, attr.value.data(), datasize);
619         if(derivatives)
620                 memset((char *)val + datasize, 0, datasize * 2);
621 }
622
623 bool OSLRenderServices::get_object_standard_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
624                                                       TypeDesc type, bool derivatives, void *val)
625 {
626         /* todo: turn this into hash table? */
627
628         /* Object Attributes */
629         if(name == u_object_location) {
630                 float3 f = object_location(kg, sd);
631                 return set_attribute_float3(f, type, derivatives, val);
632         }
633         else if(name == u_object_index) {
634                 float f = object_pass_id(kg, sd->object);
635                 return set_attribute_float(f, type, derivatives, val);
636         }
637         else if(name == u_geom_dupli_generated) {
638                 float3 f = object_dupli_generated(kg, sd->object);
639                 return set_attribute_float3(f, type, derivatives, val);
640         }
641         else if(name == u_geom_dupli_uv) {
642                 float3 f = object_dupli_uv(kg, sd->object);
643                 return set_attribute_float3(f, type, derivatives, val);
644         }
645         else if(name == u_material_index) {
646                 float f = shader_pass_id(kg, sd);
647                 return set_attribute_float(f, type, derivatives, val);
648         }
649         else if(name == u_object_random) {
650                 float f = object_random_number(kg, sd->object);
651                 return set_attribute_float(f, type, derivatives, val);
652         }
653
654         /* Particle Attributes */
655         else if(name == u_particle_random) {
656                 int particle_id = object_particle_id(kg, sd->object);
657                 float f = particle_random(kg, particle_id);
658                 return set_attribute_float(f, type, derivatives, val);
659         }
660         else if(name == u_particle_age) {
661                 int particle_id = object_particle_id(kg, sd->object);
662                 float f = particle_age(kg, particle_id);
663                 return set_attribute_float(f, type, derivatives, val);
664         }
665         else if(name == u_particle_lifetime) {
666                 int particle_id = object_particle_id(kg, sd->object);
667                 float f = particle_lifetime(kg, particle_id);
668                 return set_attribute_float(f, type, derivatives, val);
669         }
670         else if(name == u_particle_location) {
671                 int particle_id = object_particle_id(kg, sd->object);
672                 float3 f = particle_location(kg, particle_id);
673                 return set_attribute_float3(f, type, derivatives, val);
674         }
675 #if 0   /* unsupported */
676         else if(name == u_particle_rotation) {
677                 int particle_id = object_particle_id(kg, sd->object);
678                 float4 f = particle_rotation(kg, particle_id);
679                 return set_attribute_float4(f, type, derivatives, val);
680         }
681 #endif
682         else if(name == u_particle_size) {
683                 int particle_id = object_particle_id(kg, sd->object);
684                 float f = particle_size(kg, particle_id);
685                 return set_attribute_float(f, type, derivatives, val);
686         }
687         else if(name == u_particle_velocity) {
688                 int particle_id = object_particle_id(kg, sd->object);
689                 float3 f = particle_velocity(kg, particle_id);
690                 return set_attribute_float3(f, type, derivatives, val);
691         }
692         else if(name == u_particle_angular_velocity) {
693                 int particle_id = object_particle_id(kg, sd->object);
694                 float3 f = particle_angular_velocity(kg, particle_id);
695                 return set_attribute_float3(f, type, derivatives, val);
696         }
697         
698         /* Geometry Attributes */
699         else if(name == u_geom_numpolyvertices) {
700                 return set_attribute_int(3, type, derivatives, val);
701         }
702         else if((name == u_geom_trianglevertices || name == u_geom_polyvertices)
703                      && sd->type & PRIMITIVE_ALL_TRIANGLE)
704         {
705                 float3 P[3];
706
707                 if(sd->type & PRIMITIVE_TRIANGLE)
708                         triangle_vertices(kg, sd->prim, P);
709                 else
710                         motion_triangle_vertices(kg, sd->object, sd->prim, sd->time, P);
711
712                 if(!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
713                         object_position_transform(kg, sd, &P[0]);
714                         object_position_transform(kg, sd, &P[1]);
715                         object_position_transform(kg, sd, &P[2]);
716                 }
717
718                 return set_attribute_float3_3(P, type, derivatives, val);
719         }
720         else if(name == u_geom_name) {
721                 ustring object_name = kg->osl->object_names[sd->object];
722                 return set_attribute_string(object_name, type, derivatives, val);
723         }
724         else if(name == u_is_smooth) {
725                 float f = ((sd->shader & SHADER_SMOOTH_NORMAL) != 0);
726                 return set_attribute_float(f, type, derivatives, val);
727         }
728         /* Hair Attributes */
729         else if(name == u_is_curve) {
730                 float f = (sd->type & PRIMITIVE_ALL_CURVE) != 0;
731                 return set_attribute_float(f, type, derivatives, val);
732         }
733         else if(name == u_curve_thickness) {
734                 float f = curve_thickness(kg, sd);
735                 return set_attribute_float(f, type, derivatives, val);
736         }
737         else if(name == u_curve_tangent_normal) {
738                 float3 f = curve_tangent_normal(kg, sd);
739                 return set_attribute_float3(f, type, derivatives, val);
740         }
741         else
742                 return false;
743 }
744
745 bool OSLRenderServices::get_background_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
746                                                  TypeDesc type, bool derivatives, void *val)
747 {
748         if(name == u_path_ray_length) {
749                 /* Ray Length */
750                 float f = sd->ray_length;
751                 return set_attribute_float(f, type, derivatives, val);
752         }
753         else if(name == u_path_ray_depth) {
754                 /* Ray Depth */
755                 PathState *state = sd->osl_path_state;
756                 int f = state->bounce;
757                 return set_attribute_int(f, type, derivatives, val);
758         }
759         else if(name == u_path_diffuse_depth) {
760                 /* Diffuse Ray Depth */
761                 PathState *state = sd->osl_path_state;
762                 int f = state->diffuse_bounce;
763                 return set_attribute_int(f, type, derivatives, val);
764         }
765         else if(name == u_path_glossy_depth) {
766                 /* Glossy Ray Depth */
767                 PathState *state = sd->osl_path_state;
768                 int f = state->glossy_bounce;
769                 return set_attribute_int(f, type, derivatives, val);
770         }
771         else if(name == u_path_transmission_depth) {
772                 /* Transmission Ray Depth */
773                 PathState *state = sd->osl_path_state;
774                 int f = state->transmission_bounce;
775                 return set_attribute_int(f, type, derivatives, val);
776         }
777         else if(name == u_path_transparent_depth) {
778                 /* Transparent Ray Depth */
779                 PathState *state = sd->osl_path_state;
780                 int f = state->transparent_bounce;
781                 return set_attribute_int(f, type, derivatives, val);
782         }
783         else if(name == u_path_transmission_depth) {
784                 /* Transmission Ray Depth */
785                 PathState *state = sd->osl_path_state;
786                 int f = state->transmission_bounce;
787                 return set_attribute_int(f, type, derivatives, val);
788         }
789         else if(name == u_ndc) {
790                 /* NDC coordinates with special exception for otho */
791                 OSLThreadData *tdata = kg->osl_tdata;
792                 OSL::ShaderGlobals *globals = &tdata->globals;
793                 float3 ndc[3];
794
795                 if((globals->raytype & PATH_RAY_CAMERA) && sd->object == OBJECT_NONE && kernel_data.cam.type == CAMERA_ORTHOGRAPHIC) {
796                         ndc[0] = camera_world_to_ndc(kg, sd, sd->ray_P);
797
798                         if(derivatives) {
799                                 ndc[1] = camera_world_to_ndc(kg, sd, sd->ray_P + sd->ray_dP.dx) - ndc[0];
800                                 ndc[2] = camera_world_to_ndc(kg, sd, sd->ray_P + sd->ray_dP.dy) - ndc[0];
801                         }
802                 }
803                 else {
804                         ndc[0] = camera_world_to_ndc(kg, sd, sd->P);
805
806                         if(derivatives) {
807                                 ndc[1] = camera_world_to_ndc(kg, sd, sd->P + sd->dP.dx) - ndc[0];
808                                 ndc[2] = camera_world_to_ndc(kg, sd, sd->P + sd->dP.dy) - ndc[0];
809                         }
810                 }
811
812                 return set_attribute_float3(ndc, type, derivatives, val);
813         }
814         else
815                 return false;
816 }
817
818 bool OSLRenderServices::get_attribute(OSL::ShaderGlobals *sg, bool derivatives, ustring object_name,
819                                       TypeDesc type, ustring name, void *val)
820 {
821         if(sg == NULL || sg->renderstate == NULL)
822                 return false;
823
824         ShaderData *sd = (ShaderData *)(sg->renderstate);
825         return get_attribute(sd, derivatives, object_name, type, name, val);
826 }
827
828 bool OSLRenderServices::get_attribute(ShaderData *sd, bool derivatives, ustring object_name,
829                                       TypeDesc type, ustring name, void *val)
830 {
831         KernelGlobals *kg = sd->osl_globals;
832         int prim_type = 0;
833         int object;
834
835         /* lookup of attribute on another object */
836         if(object_name != u_empty) {
837                 OSLGlobals::ObjectNameMap::iterator it = kg->osl->object_name_map.find(object_name);
838
839                 if(it == kg->osl->object_name_map.end())
840                         return false;
841
842                 object = it->second;
843         }
844         else {
845                 object = sd->object;
846                 prim_type = attribute_primitive_type(kg, sd);
847
848                 if(object == OBJECT_NONE)
849                         return get_background_attribute(kg, sd, name, type, derivatives, val);
850         }
851
852         /* find attribute on object */
853         object = object*ATTR_PRIM_TYPES + prim_type;
854         OSLGlobals::AttributeMap& attribute_map = kg->osl->attribute_map[object];
855         OSLGlobals::AttributeMap::iterator it = attribute_map.find(name);
856
857         if(it != attribute_map.end()) {
858                 const OSLGlobals::Attribute& attr = it->second;
859
860                 if(attr.desc.element != ATTR_ELEMENT_OBJECT) {
861                         /* triangle and vertex attributes */
862                         if(get_mesh_element_attribute(kg, sd, attr, type, derivatives, val))
863                                 return true;
864                         else
865                                 return get_mesh_attribute(kg, sd, attr, type, derivatives, val);
866                 }
867                 else {
868                         /* object attribute */
869                         get_object_attribute(attr, derivatives, val);
870                         return true;
871                 }
872         }
873         else {
874                 /* not found in attribute, check standard object info */
875                 bool is_std_object_attribute = get_object_standard_attribute(kg, sd, name, type, derivatives, val);
876
877                 if(is_std_object_attribute)
878                         return true;
879
880                 return get_background_attribute(kg, sd, name, type, derivatives, val);
881         }
882
883         return false;
884 }
885
886 bool OSLRenderServices::get_userdata(bool derivatives, ustring name, TypeDesc type, 
887                                      OSL::ShaderGlobals *sg, void *val)
888 {
889         return false; /* disabled by lockgeom */
890 }
891
892 bool OSLRenderServices::has_userdata(ustring name, TypeDesc type, OSL::ShaderGlobals *sg)
893 {
894         return false; /* never called by OSL */
895 }
896
897 bool OSLRenderServices::texture(ustring filename,
898                                 TextureHandle *texture_handle,
899                                 TexturePerthread *texture_thread_info,
900                                 TextureOpt &options,
901                                 OSL::ShaderGlobals *sg,
902                                 float s, float t,
903                                 float dsdx, float dtdx, float dsdy, float dtdy,
904                                 int nchannels,
905                                 float *result,
906                                 float *dresultds,
907                                 float *dresultdt)
908 {
909         OSL::TextureSystem *ts = osl_ts;
910         ShaderData *sd = (ShaderData *)(sg->renderstate);
911         KernelGlobals *kg = sd->osl_globals;
912
913         if(texture_thread_info == NULL) {
914                 OSLThreadData *tdata = kg->osl_tdata;
915                 texture_thread_info = tdata->oiio_thread_info;
916         }
917
918 #ifdef WITH_PTEX
919         /* todo: this is just a quick hack, only works with particular files and options */
920         if(string_endswith(filename.string(), ".ptx")) {
921                 float2 uv;
922                 int faceid;
923
924                 if(!primitive_ptex(kg, sd, &uv, &faceid))
925                         return false;
926
927                 float u = uv.x;
928                 float v = uv.y;
929                 float dudx = 0.0f;
930                 float dvdx = 0.0f;
931                 float dudy = 0.0f;
932                 float dvdy = 0.0f;
933
934                 Ptex::String error;
935                 PtexPtr<PtexTexture> r(ptex_cache->get(filename.c_str(), error));
936
937                 if(!r) {
938                         //std::cerr << error.c_str() << std::endl;
939                         return false;
940                 }
941
942                 bool mipmaplerp = false;
943                 float sharpness = 1.0f;
944                 PtexFilter::Options opts(PtexFilter::f_bicubic, mipmaplerp, sharpness);
945                 PtexPtr<PtexFilter> f(PtexFilter::getFilter(r, opts));
946
947                 f->eval(result, options.firstchannel, nchannels, faceid, u, v, dudx, dvdx, dudy, dvdy);
948
949                 for(int c = r->numChannels(); c < nchannels; c++)
950                         result[c] = result[0];
951
952                 return true;
953         }
954 #endif
955         bool status = false;
956
957         if(filename.length() && filename[0] == '@') {
958                 if(filename == u_at_bevel) {
959                         /* Bevel shader hack. */
960                         if(nchannels >= 3) {
961                                 PathState *state = sd->osl_path_state;
962                                 int num_samples = (int)s;
963                                 float radius = t;
964                                 float3 N = svm_bevel(kg, sd, state, radius, num_samples);
965                                 result[0] = N.x;
966                                 result[1] = N.y;
967                                 result[2] = N.z;
968                                 status = true;
969                         }
970                 }
971                 else {
972                         /* Packed texture. */
973                         int slot = atoi(filename.c_str() + 1);
974                         float4 rgba = kernel_tex_image_interp(kg, slot, s, 1.0f - t);
975
976                         result[0] = rgba[0];
977                         if(nchannels > 1)
978                                 result[1] = rgba[1];
979                         if(nchannels > 2)
980                                 result[2] = rgba[2];
981                         if(nchannels > 3)
982                                 result[3] = rgba[3];
983                         status = true;
984                 }
985         }
986         else {
987                 if(texture_handle != NULL) {
988                         status = ts->texture(texture_handle,
989                                              texture_thread_info,
990                                              options,
991                                              s, t,
992                                              dsdx, dtdx,
993                                              dsdy, dtdy,
994                                              nchannels,
995                                              result,
996                                              dresultds, dresultdt);
997                 }
998                 else {
999                         status = ts->texture(filename,
1000                                              options,
1001                                              s, t,
1002                                              dsdx, dtdx,
1003                                              dsdy, dtdy,
1004                                              nchannels,
1005                                              result,
1006                                              dresultds, dresultdt);
1007                 }
1008         }
1009
1010         if(!status) {
1011                 if(nchannels == 3 || nchannels == 4) {
1012                         result[0] = 1.0f;
1013                         result[1] = 0.0f;
1014                         result[2] = 1.0f;
1015
1016                         if(nchannels == 4)
1017                                 result[3] = 1.0f;
1018                 }
1019                 /* This might be slow, but prevents error messages leak and
1020                  * other nasty stuff happening.
1021                  */
1022                 string err = ts->geterror();
1023                 (void)err;
1024         }
1025
1026         return status;
1027 }
1028
1029 bool OSLRenderServices::texture3d(ustring filename,
1030                                   TextureHandle *texture_handle,
1031                                   TexturePerthread *texture_thread_info,
1032                                   TextureOpt &options,
1033                                   OSL::ShaderGlobals *sg,
1034                                   const OSL::Vec3 &P,
1035                                   const OSL::Vec3 &dPdx,
1036                                   const OSL::Vec3 &dPdy,
1037                                   const OSL::Vec3 &dPdz,
1038                                   int nchannels,
1039                                   float *result,
1040                                   float *dresultds,
1041                                   float *dresultdt,
1042                                   float *dresultdr)
1043 {
1044         OSL::TextureSystem *ts = osl_ts;
1045         ShaderData *sd = (ShaderData *)(sg->renderstate);
1046         KernelGlobals *kg = sd->osl_globals;
1047
1048         if(texture_thread_info == NULL) {
1049                 OSLThreadData *tdata = kg->osl_tdata;
1050                 texture_thread_info = tdata->oiio_thread_info;
1051         }
1052
1053         bool status;
1054         if(filename.length() && filename[0] == '@') {
1055                 int slot = atoi(filename.c_str() + 1);
1056                 float4 rgba = kernel_tex_image_interp_3d(kg, slot, P.x, P.y, P.z, INTERPOLATION_NONE);
1057
1058                 result[0] = rgba[0];
1059                 if(nchannels > 1)
1060                         result[1] = rgba[1];
1061                 if(nchannels > 2)
1062                         result[2] = rgba[2];
1063                 if(nchannels > 3)
1064                         result[3] = rgba[3];
1065                 status = true;
1066         }
1067         else {
1068                 if(texture_handle != NULL) {
1069                         status = ts->texture3d(texture_handle,
1070                                                texture_thread_info,
1071                                                options,
1072                                                P,
1073                                                dPdx, dPdy, dPdz,
1074                                                nchannels,
1075                                                result,
1076                                                dresultds, dresultdt, dresultdr);
1077                 }
1078                 else {
1079                         status = ts->texture3d(filename,
1080                                                options,
1081                                                P,
1082                                                dPdx, dPdy, dPdz,
1083                                                nchannels,
1084                                                result,
1085                                                dresultds, dresultdt, dresultdr);
1086                 }
1087         }
1088
1089         if(!status) {
1090                 if(nchannels == 3 || nchannels == 4) {
1091                         result[0] = 1.0f;
1092                         result[1] = 0.0f;
1093                         result[2] = 1.0f;
1094
1095                         if(nchannels == 4)
1096                                 result[3] = 1.0f;
1097                 }
1098                 /* This might be slow, but prevents error messages leak and
1099                  * other nasty stuff happening.
1100                  */
1101                 string err = ts->geterror();
1102                 (void)err;
1103         }
1104
1105         return status;
1106 }
1107
1108 bool OSLRenderServices::environment(ustring filename, TextureOpt &options,
1109                                     OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
1110                                     const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy,
1111                                     int nchannels, float *result)
1112 {
1113         OSL::TextureSystem *ts = osl_ts;
1114         ShaderData *sd = (ShaderData *)(sg->renderstate);
1115         KernelGlobals *kg = sd->osl_globals;
1116         OSLThreadData *tdata = kg->osl_tdata;
1117         OIIO::TextureSystem::Perthread *thread_info = tdata->oiio_thread_info;
1118
1119         OIIO::TextureSystem::TextureHandle *th = ts->get_texture_handle(filename, thread_info);
1120
1121         bool status = ts->environment(th, thread_info,
1122                                       options, R, dRdx, dRdy,
1123                                       nchannels, result);
1124
1125         if(!status) {
1126                 if(nchannels == 3 || nchannels == 4) {
1127                         result[0] = 1.0f;
1128                         result[1] = 0.0f;
1129                         result[2] = 1.0f;
1130
1131                         if(nchannels == 4)
1132                                 result[3] = 1.0f;
1133                 }
1134         }
1135
1136         return status;
1137 }
1138
1139 bool OSLRenderServices::get_texture_info(OSL::ShaderGlobals *sg, ustring filename, int subimage,
1140                                          ustring dataname,
1141                                          TypeDesc datatype, void *data)
1142 {
1143         OSL::TextureSystem *ts = osl_ts;
1144         return ts->get_texture_info(filename, subimage, dataname, datatype, data);
1145 }
1146
1147 int OSLRenderServices::pointcloud_search(OSL::ShaderGlobals *sg, ustring filename, const OSL::Vec3 &center,
1148                                          float radius, int max_points, bool sort,
1149                                          size_t *out_indices, float *out_distances, int derivs_offset)
1150 {
1151         return 0;
1152 }
1153
1154 int OSLRenderServices::pointcloud_get(OSL::ShaderGlobals *sg, ustring filename, size_t *indices, int count,
1155                                       ustring attr_name, TypeDesc attr_type, void *out_data)
1156 {
1157         return 0;
1158 }
1159
1160 bool OSLRenderServices::pointcloud_write(OSL::ShaderGlobals *sg,
1161                                          ustring filename, const OSL::Vec3 &pos,
1162                                          int nattribs, const ustring *names,
1163                                          const TypeDesc *types,
1164                                          const void **data)
1165 {
1166         return false;
1167 }
1168
1169 bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg,
1170         const OSL::Vec3 &P, const OSL::Vec3 &dPdx,
1171         const OSL::Vec3 &dPdy, const OSL::Vec3 &R,
1172         const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy)
1173 {
1174         /* todo: options.shader support, maybe options.traceset */
1175         ShaderData *sd = (ShaderData *)(sg->renderstate);
1176
1177         /* setup ray */
1178         Ray ray;
1179
1180         ray.P = TO_FLOAT3(P);
1181         ray.D = TO_FLOAT3(R);
1182         ray.t = (options.maxdist == 1.0e30f)? FLT_MAX: options.maxdist - options.mindist;
1183         ray.time = sd->time;
1184
1185         if(options.mindist == 0.0f) {
1186                 /* avoid self-intersections */
1187                 if(ray.P == sd->P) {
1188                         bool transmit = (dot(sd->Ng, ray.D) < 0.0f);
1189                         ray.P = ray_offset(sd->P, (transmit)? -sd->Ng: sd->Ng);
1190                 }
1191         }
1192         else {
1193                 /* offset for minimum distance */
1194                 ray.P += options.mindist*ray.D;
1195         }
1196
1197         /* ray differentials */
1198         ray.dP.dx = TO_FLOAT3(dPdx);
1199         ray.dP.dy = TO_FLOAT3(dPdy);
1200         ray.dD.dx = TO_FLOAT3(dRdx);
1201         ray.dD.dy = TO_FLOAT3(dRdy);
1202
1203         /* allocate trace data */
1204         OSLTraceData *tracedata = (OSLTraceData*)sg->tracedata;
1205         tracedata->ray = ray;
1206         tracedata->setup = false;
1207         tracedata->init = true;
1208         tracedata->sd.osl_globals = sd->osl_globals;
1209
1210         /* Raytrace, leaving out shadow opaque to avoid early exit. */
1211         uint visibility = PATH_RAY_ALL_VISIBILITY - PATH_RAY_SHADOW_OPAQUE;
1212         return scene_intersect(sd->osl_globals, ray, visibility, &tracedata->isect, NULL, 0.0f, 0.0f);
1213 }
1214
1215
1216 bool OSLRenderServices::getmessage(OSL::ShaderGlobals *sg, ustring source, ustring name,
1217         TypeDesc type, void *val, bool derivatives)
1218 {
1219         OSLTraceData *tracedata = (OSLTraceData*)sg->tracedata;
1220
1221         if(source == u_trace && tracedata->init) {
1222                 if(name == u_hit) {
1223                         return set_attribute_int((tracedata->isect.prim != PRIM_NONE), type, derivatives, val);
1224                 }
1225                 else if(tracedata->isect.prim != PRIM_NONE) {
1226                         if(name == u_hitdist) {
1227                                 float f[3] = {tracedata->isect.t, 0.0f, 0.0f};
1228                                 return set_attribute_float(f, type, derivatives, val);
1229                         }
1230                         else {
1231                                 ShaderData *sd = &tracedata->sd;
1232                                 KernelGlobals *kg = sd->osl_globals;
1233
1234                                 if(!tracedata->setup) {
1235                                         /* lazy shader data setup */
1236                                         shader_setup_from_ray(kg, sd, &tracedata->isect, &tracedata->ray);
1237                                         tracedata->setup = true;
1238                                 }
1239
1240                                 if(name == u_N) {
1241                                         return set_attribute_float3(sd->N, type, derivatives, val);
1242                                 }
1243                                 else if(name == u_Ng) {
1244                                         return set_attribute_float3(sd->Ng, type, derivatives, val);
1245                                 }
1246                                 else if(name == u_P) {
1247                                         float3 f[3] = {sd->P, sd->dP.dx, sd->dP.dy};
1248                                         return set_attribute_float3(f, type, derivatives, val);
1249                                 }
1250                                 else if(name == u_I) {
1251                                         float3 f[3] = {sd->I, sd->dI.dx, sd->dI.dy};
1252                                         return set_attribute_float3(f, type, derivatives, val);
1253                                 }
1254                                 else if(name == u_u) {
1255                                         float f[3] = {sd->u, sd->du.dx, sd->du.dy};
1256                                         return set_attribute_float(f, type, derivatives, val);
1257                                 }
1258                                 else if(name == u_v) {
1259                                         float f[3] = {sd->v, sd->dv.dx, sd->dv.dy};
1260                                         return set_attribute_float(f, type, derivatives, val);
1261                                 }
1262
1263                                 return get_attribute(sd, derivatives, u_empty, type, name, val);
1264                         }
1265                 }
1266         }
1267
1268         return false;
1269 }
1270
1271 CCL_NAMESPACE_END