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