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