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