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