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