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