Fix T52027: OSL getattribute() crash, when optimizer calls it before rendering.
[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_index("particle:index");
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 #ifdef __HAIR__
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 #endif
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
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_index) {
656                 int particle_id = object_particle_id(kg, sd->object);
657                 float f = particle_index(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 #ifdef __HAIR__
704                      && sd->type & PRIMITIVE_ALL_TRIANGLE)
705 #else
706                 )
707 #endif
708         {
709                 float3 P[3];
710
711                 if(sd->type & PRIMITIVE_TRIANGLE)
712                         triangle_vertices(kg, sd->prim, P);
713                 else
714                         motion_triangle_vertices(kg, sd->object, sd->prim, sd->time, P);
715
716                 if(!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
717                         object_position_transform(kg, sd, &P[0]);
718                         object_position_transform(kg, sd, &P[1]);
719                         object_position_transform(kg, sd, &P[2]);
720                 }
721
722                 return set_attribute_float3_3(P, type, derivatives, val);
723         }
724         else if(name == u_geom_name) {
725                 ustring object_name = kg->osl->object_names[sd->object];
726                 return set_attribute_string(object_name, type, derivatives, val);
727         }
728         else if(name == u_is_smooth) {
729                 float f = ((sd->shader & SHADER_SMOOTH_NORMAL) != 0);
730                 return set_attribute_float(f, type, derivatives, val);
731         }
732 #ifdef __HAIR__
733         /* Hair Attributes */
734         else if(name == u_is_curve) {
735                 float f = (sd->type & PRIMITIVE_ALL_CURVE) != 0;
736                 return set_attribute_float(f, type, derivatives, val);
737         }
738         else if(name == u_curve_thickness) {
739                 float f = curve_thickness(kg, sd);
740                 return set_attribute_float(f, type, derivatives, val);
741         }
742         else if(name == u_curve_tangent_normal) {
743                 float3 f = curve_tangent_normal(kg, sd);
744                 return set_attribute_float3(f, type, derivatives, val);
745         }
746 #endif
747         else
748                 return false;
749 }
750
751 bool OSLRenderServices::get_background_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
752                                                  TypeDesc type, bool derivatives, void *val)
753 {
754         if(name == u_path_ray_length) {
755                 /* Ray Length */
756                 float f = sd->ray_length;
757                 return set_attribute_float(f, type, derivatives, val);
758         }
759         else if(name == u_path_ray_depth) {
760                 /* Ray Depth */
761                 PathState *state = sd->osl_path_state;
762                 int f = state->bounce;
763                 return set_attribute_int(f, type, derivatives, val);
764         }
765         else if(name == u_path_diffuse_depth) {
766                 /* Diffuse Ray Depth */
767                 PathState *state = sd->osl_path_state;
768                 int f = state->diffuse_bounce;
769                 return set_attribute_int(f, type, derivatives, val);
770         }
771         else if(name == u_path_glossy_depth) {
772                 /* Glossy Ray Depth */
773                 PathState *state = sd->osl_path_state;
774                 int f = state->glossy_bounce;
775                 return set_attribute_int(f, type, derivatives, val);
776         }
777         else if(name == u_path_transmission_depth) {
778                 /* Transmission Ray Depth */
779                 PathState *state = sd->osl_path_state;
780                 int f = state->transmission_bounce;
781                 return set_attribute_int(f, type, derivatives, val);
782         }
783         else if(name == u_path_transparent_depth) {
784                 /* Transparent Ray Depth */
785                 PathState *state = sd->osl_path_state;
786                 int f = state->transparent_bounce;
787                 return set_attribute_int(f, type, derivatives, val);
788         }
789         else if(name == u_path_transmission_depth) {
790                 /* Transmission Ray Depth */
791                 PathState *state = sd->osl_path_state;
792                 int f = state->transmission_bounce;
793                 return set_attribute_int(f, type, derivatives, val);
794         }
795         else if(name == u_ndc) {
796                 /* NDC coordinates with special exception for otho */
797                 OSLThreadData *tdata = kg->osl_tdata;
798                 OSL::ShaderGlobals *globals = &tdata->globals;
799                 float3 ndc[3];
800
801                 if((globals->raytype & PATH_RAY_CAMERA) && sd->object == OBJECT_NONE && kernel_data.cam.type == CAMERA_ORTHOGRAPHIC) {
802                         ndc[0] = camera_world_to_ndc(kg, sd, sd->ray_P);
803
804                         if(derivatives) {
805                                 ndc[1] = camera_world_to_ndc(kg, sd, sd->ray_P + sd->ray_dP.dx) - ndc[0];
806                                 ndc[2] = camera_world_to_ndc(kg, sd, sd->ray_P + sd->ray_dP.dy) - ndc[0];
807                         }
808                 }
809                 else {
810                         ndc[0] = camera_world_to_ndc(kg, sd, sd->P);
811
812                         if(derivatives) {
813                                 ndc[1] = camera_world_to_ndc(kg, sd, sd->P + sd->dP.dx) - ndc[0];
814                                 ndc[2] = camera_world_to_ndc(kg, sd, sd->P + sd->dP.dy) - ndc[0];
815                         }
816                 }
817
818                 return set_attribute_float3(ndc, type, derivatives, val);
819         }
820         else
821                 return false;
822 }
823
824 bool OSLRenderServices::get_attribute(OSL::ShaderGlobals *sg, bool derivatives, ustring object_name,
825                                       TypeDesc type, ustring name, void *val)
826 {
827         if(sg == NULL || sg->renderstate == NULL)
828                 return false;
829
830         ShaderData *sd = (ShaderData *)(sg->renderstate);
831         return get_attribute(sd, derivatives, object_name, type, name, val);
832 }
833
834 bool OSLRenderServices::get_attribute(ShaderData *sd, bool derivatives, ustring object_name,
835                                       TypeDesc type, ustring name, void *val)
836 {
837         KernelGlobals *kg = sd->osl_globals;
838         int prim_type = 0;
839         int object;
840
841         /* lookup of attribute on another object */
842         if(object_name != u_empty) {
843                 OSLGlobals::ObjectNameMap::iterator it = kg->osl->object_name_map.find(object_name);
844
845                 if(it == kg->osl->object_name_map.end())
846                         return false;
847
848                 object = it->second;
849         }
850         else {
851                 object = sd->object;
852                 prim_type = attribute_primitive_type(kg, sd);
853
854                 if(object == OBJECT_NONE)
855                         return get_background_attribute(kg, sd, name, type, derivatives, val);
856         }
857
858         /* find attribute on object */
859         object = object*ATTR_PRIM_TYPES + prim_type;
860         OSLGlobals::AttributeMap& attribute_map = kg->osl->attribute_map[object];
861         OSLGlobals::AttributeMap::iterator it = attribute_map.find(name);
862
863         if(it != attribute_map.end()) {
864                 const OSLGlobals::Attribute& attr = it->second;
865
866                 if(attr.desc.element != ATTR_ELEMENT_OBJECT) {
867                         /* triangle and vertex attributes */
868                         if(get_mesh_element_attribute(kg, sd, attr, type, derivatives, val))
869                                 return true;
870                         else
871                                 return get_mesh_attribute(kg, sd, attr, type, derivatives, val);
872                 }
873                 else {
874                         /* object attribute */
875                         get_object_attribute(attr, derivatives, val);
876                         return true;
877                 }
878         }
879         else {
880                 /* not found in attribute, check standard object info */
881                 bool is_std_object_attribute = get_object_standard_attribute(kg, sd, name, type, derivatives, val);
882
883                 if(is_std_object_attribute)
884                         return true;
885
886                 return get_background_attribute(kg, sd, name, type, derivatives, val);
887         }
888
889         return false;
890 }
891
892 bool OSLRenderServices::get_userdata(bool derivatives, ustring name, TypeDesc type, 
893                                      OSL::ShaderGlobals *sg, void *val)
894 {
895         return false; /* disabled by lockgeom */
896 }
897
898 bool OSLRenderServices::has_userdata(ustring name, TypeDesc type, OSL::ShaderGlobals *sg)
899 {
900         return false; /* never called by OSL */
901 }
902
903 bool OSLRenderServices::texture(ustring filename,
904                                 TextureHandle *texture_handle,
905                                 TexturePerthread *texture_thread_info,
906                                 TextureOpt &options,
907                                 OSL::ShaderGlobals *sg,
908                                 float s, float t,
909                                 float dsdx, float dtdx, float dsdy, float dtdy,
910                                 int nchannels,
911                                 float *result,
912                                 float *dresultds,
913                                 float *dresultdt)
914 {
915         OSL::TextureSystem *ts = osl_ts;
916         ShaderData *sd = (ShaderData *)(sg->renderstate);
917         KernelGlobals *kg = sd->osl_globals;
918
919         if(texture_thread_info == NULL) {
920                 OSLThreadData *tdata = kg->osl_tdata;
921                 texture_thread_info = tdata->oiio_thread_info;
922         }
923
924 #ifdef WITH_PTEX
925         /* todo: this is just a quick hack, only works with particular files and options */
926         if(string_endswith(filename.string(), ".ptx")) {
927                 float2 uv;
928                 int faceid;
929
930                 if(!primitive_ptex(kg, sd, &uv, &faceid))
931                         return false;
932
933                 float u = uv.x;
934                 float v = uv.y;
935                 float dudx = 0.0f;
936                 float dvdx = 0.0f;
937                 float dudy = 0.0f;
938                 float dvdy = 0.0f;
939
940                 Ptex::String error;
941                 PtexPtr<PtexTexture> r(ptex_cache->get(filename.c_str(), error));
942
943                 if(!r) {
944                         //std::cerr << error.c_str() << std::endl;
945                         return false;
946                 }
947
948                 bool mipmaplerp = false;
949                 float sharpness = 1.0f;
950                 PtexFilter::Options opts(PtexFilter::f_bicubic, mipmaplerp, sharpness);
951                 PtexPtr<PtexFilter> f(PtexFilter::getFilter(r, opts));
952
953                 f->eval(result, options.firstchannel, nchannels, faceid, u, v, dudx, dvdx, dudy, dvdy);
954
955                 for(int c = r->numChannels(); c < nchannels; c++)
956                         result[c] = result[0];
957
958                 return true;
959         }
960 #endif
961         bool status;
962
963         if(filename.length() && filename[0] == '@') {
964                 int slot = atoi(filename.c_str() + 1);
965                 float4 rgba = kernel_tex_image_interp(slot, s, 1.0f - t);
966
967                 result[0] = rgba[0];
968                 if(nchannels > 1)
969                         result[1] = rgba[1];
970                 if(nchannels > 2)
971                         result[2] = rgba[2];
972                 if(nchannels > 3)
973                         result[3] = rgba[3];
974                 status = true;
975         }
976         else {
977                 if(texture_handle != NULL) {
978                         status = ts->texture(texture_handle,
979                                              texture_thread_info,
980                                              options,
981                                              s, t,
982                                              dsdx, dtdx,
983                                              dsdy, dtdy,
984                                              nchannels,
985                                              result,
986                                              dresultds, dresultdt);
987                 }
988                 else {
989                         status = ts->texture(filename,
990                                              options,
991                                              s, t,
992                                              dsdx, dtdx,
993                                              dsdy, dtdy,
994                                              nchannels,
995                                              result,
996                                              dresultds, dresultdt);
997                 }
998         }
999
1000         if(!status) {
1001                 if(nchannels == 3 || nchannels == 4) {
1002                         result[0] = 1.0f;
1003                         result[1] = 0.0f;
1004                         result[2] = 1.0f;
1005
1006                         if(nchannels == 4)
1007                                 result[3] = 1.0f;
1008                 }
1009                 /* This might be slow, but prevents error messages leak and
1010                  * other nasty stuff happening.
1011                  */
1012                 string err = ts->geterror();
1013                 (void)err;
1014         }
1015
1016         return status;
1017 }
1018
1019 bool OSLRenderServices::texture3d(ustring filename,
1020                                   TextureHandle *texture_handle,
1021                                   TexturePerthread *texture_thread_info,
1022                                   TextureOpt &options,
1023                                   OSL::ShaderGlobals *sg,
1024                                   const OSL::Vec3 &P,
1025                                   const OSL::Vec3 &dPdx,
1026                                   const OSL::Vec3 &dPdy,
1027                                   const OSL::Vec3 &dPdz,
1028                                   int nchannels,
1029                                   float *result,
1030                                   float *dresultds,
1031                                   float *dresultdt,
1032                                   float *dresultdr)
1033 {
1034         OSL::TextureSystem *ts = osl_ts;
1035         ShaderData *sd = (ShaderData *)(sg->renderstate);
1036         KernelGlobals *kg = sd->osl_globals;
1037
1038         if(texture_thread_info == NULL) {
1039                 OSLThreadData *tdata = kg->osl_tdata;
1040                 texture_thread_info = tdata->oiio_thread_info;
1041         }
1042
1043         bool status;
1044         if(filename.length() && filename[0] == '@') {
1045                 int slot = atoi(filename.c_str() + 1);
1046                 float4 rgba = kernel_tex_image_interp_3d(slot, P.x, P.y, P.z);
1047
1048                 result[0] = rgba[0];
1049                 if(nchannels > 1)
1050                         result[1] = rgba[1];
1051                 if(nchannels > 2)
1052                         result[2] = rgba[2];
1053                 if(nchannels > 3)
1054                         result[3] = rgba[3];
1055                 status = true;
1056         }
1057         else {
1058                 if(texture_handle != NULL) {
1059                         status = ts->texture3d(texture_handle,
1060                                                texture_thread_info,
1061                                                options,
1062                                                P,
1063                                                dPdx, dPdy, dPdz,
1064                                                nchannels,
1065                                                result,
1066                                                dresultds, dresultdt, dresultdr);
1067                 }
1068                 else {
1069                         status = ts->texture3d(filename,
1070                                                options,
1071                                                P,
1072                                                dPdx, dPdy, dPdz,
1073                                                nchannels,
1074                                                result,
1075                                                dresultds, dresultdt, dresultdr);
1076                 }
1077         }
1078
1079         if(!status) {
1080                 if(nchannels == 3 || nchannels == 4) {
1081                         result[0] = 1.0f;
1082                         result[1] = 0.0f;
1083                         result[2] = 1.0f;
1084
1085                         if(nchannels == 4)
1086                                 result[3] = 1.0f;
1087                 }
1088                 /* This might be slow, but prevents error messages leak and
1089                  * other nasty stuff happening.
1090                  */
1091                 string err = ts->geterror();
1092                 (void)err;
1093         }
1094
1095         return status;
1096 }
1097
1098 bool OSLRenderServices::environment(ustring filename, TextureOpt &options,
1099                                     OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
1100                                     const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy,
1101                                     int nchannels, float *result)
1102 {
1103         OSL::TextureSystem *ts = osl_ts;
1104         ShaderData *sd = (ShaderData *)(sg->renderstate);
1105         KernelGlobals *kg = sd->osl_globals;
1106         OSLThreadData *tdata = kg->osl_tdata;
1107         OIIO::TextureSystem::Perthread *thread_info = tdata->oiio_thread_info;
1108
1109         OIIO::TextureSystem::TextureHandle *th = ts->get_texture_handle(filename, thread_info);
1110
1111         bool status = ts->environment(th, thread_info,
1112                                       options, R, dRdx, dRdy,
1113                                       nchannels, result);
1114
1115         if(!status) {
1116                 if(nchannels == 3 || nchannels == 4) {
1117                         result[0] = 1.0f;
1118                         result[1] = 0.0f;
1119                         result[2] = 1.0f;
1120
1121                         if(nchannels == 4)
1122                                 result[3] = 1.0f;
1123                 }
1124         }
1125
1126         return status;
1127 }
1128
1129 bool OSLRenderServices::get_texture_info(OSL::ShaderGlobals *sg, ustring filename, int subimage,
1130                                          ustring dataname,
1131                                          TypeDesc datatype, void *data)
1132 {
1133         OSL::TextureSystem *ts = osl_ts;
1134         return ts->get_texture_info(filename, subimage, dataname, datatype, data);
1135 }
1136
1137 int OSLRenderServices::pointcloud_search(OSL::ShaderGlobals *sg, ustring filename, const OSL::Vec3 &center,
1138                                          float radius, int max_points, bool sort,
1139                                          size_t *out_indices, float *out_distances, int derivs_offset)
1140 {
1141         return 0;
1142 }
1143
1144 int OSLRenderServices::pointcloud_get(OSL::ShaderGlobals *sg, ustring filename, size_t *indices, int count,
1145                                       ustring attr_name, TypeDesc attr_type, void *out_data)
1146 {
1147         return 0;
1148 }
1149
1150 bool OSLRenderServices::pointcloud_write(OSL::ShaderGlobals *sg,
1151                                          ustring filename, const OSL::Vec3 &pos,
1152                                          int nattribs, const ustring *names,
1153                                          const TypeDesc *types,
1154                                          const void **data)
1155 {
1156         return false;
1157 }
1158
1159 bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg,
1160         const OSL::Vec3 &P, const OSL::Vec3 &dPdx,
1161         const OSL::Vec3 &dPdy, const OSL::Vec3 &R,
1162         const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy)
1163 {
1164         /* todo: options.shader support, maybe options.traceset */
1165         ShaderData *sd = (ShaderData *)(sg->renderstate);
1166
1167         /* setup ray */
1168         Ray ray;
1169
1170         ray.P = TO_FLOAT3(P);
1171         ray.D = TO_FLOAT3(R);
1172         ray.t = (options.maxdist == 1.0e30f)? FLT_MAX: options.maxdist - options.mindist;
1173         ray.time = sd->time;
1174
1175         if(options.mindist == 0.0f) {
1176                 /* avoid self-intersections */
1177                 if(ray.P == sd->P) {
1178                         bool transmit = (dot(sd->Ng, ray.D) < 0.0f);
1179                         ray.P = ray_offset(sd->P, (transmit)? -sd->Ng: sd->Ng);
1180                 }
1181         }
1182         else {
1183                 /* offset for minimum distance */
1184                 ray.P += options.mindist*ray.D;
1185         }
1186
1187         /* ray differentials */
1188         ray.dP.dx = TO_FLOAT3(dPdx);
1189         ray.dP.dy = TO_FLOAT3(dPdy);
1190         ray.dD.dx = TO_FLOAT3(dRdx);
1191         ray.dD.dy = TO_FLOAT3(dRdy);
1192
1193         /* allocate trace data */
1194         OSLTraceData *tracedata = (OSLTraceData*)sg->tracedata;
1195         tracedata->ray = ray;
1196         tracedata->setup = false;
1197         tracedata->init = true;
1198         tracedata->sd.osl_globals = sd->osl_globals;
1199
1200         /* raytrace */
1201         return scene_intersect(sd->osl_globals, ray, PATH_RAY_ALL_VISIBILITY, &tracedata->isect, NULL, 0.0f, 0.0f);
1202 }
1203
1204
1205 bool OSLRenderServices::getmessage(OSL::ShaderGlobals *sg, ustring source, ustring name,
1206         TypeDesc type, void *val, bool derivatives)
1207 {
1208         OSLTraceData *tracedata = (OSLTraceData*)sg->tracedata;
1209
1210         if(source == u_trace && tracedata->init) {
1211                 if(name == u_hit) {
1212                         return set_attribute_int((tracedata->isect.prim != PRIM_NONE), type, derivatives, val);
1213                 }
1214                 else if(tracedata->isect.prim != PRIM_NONE) {
1215                         if(name == u_hitdist) {
1216                                 float f[3] = {tracedata->isect.t, 0.0f, 0.0f};
1217                                 return set_attribute_float(f, type, derivatives, val);
1218                         }
1219                         else {
1220                                 ShaderData *sd = &tracedata->sd;
1221                                 KernelGlobals *kg = sd->osl_globals;
1222
1223                                 if(!tracedata->setup) {
1224                                         /* lazy shader data setup */
1225                                         shader_setup_from_ray(kg, sd, &tracedata->isect, &tracedata->ray);
1226                                         tracedata->setup = true;
1227                                 }
1228
1229                                 if(name == u_N) {
1230                                         return set_attribute_float3(sd->N, type, derivatives, val);
1231                                 }
1232                                 else if(name == u_Ng) {
1233                                         return set_attribute_float3(sd->Ng, type, derivatives, val);
1234                                 }
1235                                 else if(name == u_P) {
1236                                         float3 f[3] = {sd->P, sd->dP.dx, sd->dP.dy};
1237                                         return set_attribute_float3(f, type, derivatives, val);
1238                                 }
1239                                 else if(name == u_I) {
1240                                         float3 f[3] = {sd->I, sd->dI.dx, sd->dI.dy};
1241                                         return set_attribute_float3(f, type, derivatives, val);
1242                                 }
1243                                 else if(name == u_u) {
1244                                         float f[3] = {sd->u, sd->du.dx, sd->du.dy};
1245                                         return set_attribute_float(f, type, derivatives, val);
1246                                 }
1247                                 else if(name == u_v) {
1248                                         float f[3] = {sd->v, sd->dv.dx, sd->dv.dy};
1249                                         return set_attribute_float(f, type, derivatives, val);
1250                                 }
1251
1252                                 return get_attribute(sd, derivatives, u_empty, type, name, val);
1253                         }
1254                 }
1255         }
1256
1257         return false;
1258 }
1259
1260 CCL_NAMESPACE_END