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