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