Cleanup: Fix Cycles Apache header.
[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::ShaderGlobals *sg, 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::ShaderGlobals *sg, 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::ShaderGlobals *sg, 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::ShaderGlobals *sg, 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::ShaderGlobals *sg, 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::ShaderGlobals *sg, 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::ShaderGlobals *sg, 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::ShaderGlobals *sg, 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(OSL::ShaderGlobals *sg, 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::STRING && 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                 /* Transparent 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(OSL::ShaderGlobals *sg, bool derivatives, ustring object_name,
755                                       TypeDesc type, ustring name, void *val)
756 {
757         if (sg->renderstate == NULL)
758                 return false;
759
760         ShaderData *sd = (ShaderData *)(sg->renderstate);
761         return get_attribute(sd, derivatives, object_name, type, name, val);
762 }
763
764 bool OSLRenderServices::get_attribute(ShaderData *sd, bool derivatives, ustring object_name,
765                                       TypeDesc type, ustring name, void *val)
766 {
767         KernelGlobals *kg = sd->osl_globals;
768         bool is_curve;
769         int object;
770
771         /* lookup of attribute on another object */
772         if (object_name != u_empty) {
773                 OSLGlobals::ObjectNameMap::iterator it = kg->osl->object_name_map.find(object_name);
774
775                 if (it == kg->osl->object_name_map.end())
776                         return false;
777
778                 object = it->second;
779                 is_curve = false;
780         }
781         else {
782                 object = sd->object;
783                 is_curve = (sd->type & PRIMITIVE_ALL_CURVE) != 0;
784
785                 if (object == OBJECT_NONE)
786                         return get_background_attribute(kg, sd, name, type, derivatives, val);
787         }
788
789         /* find attribute on object */
790         object = object*ATTR_PRIM_TYPES + (is_curve == true);
791         OSLGlobals::AttributeMap& attribute_map = kg->osl->attribute_map[object];
792         OSLGlobals::AttributeMap::iterator it = attribute_map.find(name);
793
794         if (it != attribute_map.end()) {
795                 const OSLGlobals::Attribute& attr = it->second;
796
797                 if (attr.elem != ATTR_ELEMENT_OBJECT) {
798                         /* triangle and vertex attributes */
799                         if(get_mesh_element_attribute(kg, sd, attr, type, derivatives, val))
800                                 return true;
801                         else
802                                 return get_mesh_attribute(kg, sd, attr, type, derivatives, val);
803                 }
804                 else {
805                         /* object attribute */
806                         get_object_attribute(attr, derivatives, val);
807                         return true;
808                 }
809         }
810         else {
811                 /* not found in attribute, check standard object info */
812                 bool is_std_object_attribute = get_object_standard_attribute(kg, sd, name, type, derivatives, val);
813
814                 if (is_std_object_attribute)
815                         return true;
816
817                 return get_background_attribute(kg, sd, name, type, derivatives, val);
818         }
819
820         return false;
821 }
822
823 bool OSLRenderServices::get_userdata(bool derivatives, ustring name, TypeDesc type, 
824                                      OSL::ShaderGlobals *sg, void *val)
825 {
826         return false; /* disabled by lockgeom */
827 }
828
829 bool OSLRenderServices::has_userdata(ustring name, TypeDesc type, OSL::ShaderGlobals *sg)
830 {
831         return false; /* never called by OSL */
832 }
833
834 bool OSLRenderServices::texture(ustring filename, TextureOpt &options,
835                                 OSL::ShaderGlobals *sg,
836                                 float s, float t, float dsdx, float dtdx,
837                                 float dsdy, float dtdy, int nchannels, float *result)
838 {
839         OSL::TextureSystem *ts = osl_ts;
840         ShaderData *sd = (ShaderData *)(sg->renderstate);
841         KernelGlobals *kg = sd->osl_globals;
842
843 #ifdef WITH_PTEX
844         /* todo: this is just a quick hack, only works with particular files and options */
845         if(string_endswith(filename.string(), ".ptx")) {
846                 float2 uv;
847                 int faceid;
848
849                 if(!primitive_ptex(kg, sd, &uv, &faceid))
850                         return false;
851
852                 float u = uv.x;
853                 float v = uv.y;
854                 float dudx = 0.0f;
855                 float dvdx = 0.0f;
856                 float dudy = 0.0f;
857                 float dvdy = 0.0f;
858
859                 Ptex::String error;
860                 PtexPtr<PtexTexture> r(ptex_cache->get(filename.c_str(), error));
861
862                 if(!r) {
863                         //std::cerr << error.c_str() << std::endl;
864                         return false;
865                 }
866
867                 bool mipmaplerp = false;
868                 float sharpness = 1.0f;
869                 PtexFilter::Options opts(PtexFilter::f_bicubic, mipmaplerp, sharpness);
870                 PtexPtr<PtexFilter> f(PtexFilter::getFilter(r, opts));
871
872                 f->eval(result, options.firstchannel, nchannels, faceid, u, v, dudx, dvdx, dudy, dvdy);
873
874                 for(int c = r->numChannels(); c < nchannels; c++)
875                         result[c] = result[0];
876
877                 return true;
878         }
879 #endif
880         bool status;
881
882         if(filename[0] == '@' && filename.find('.') == -1) {
883                 int slot = atoi(filename.c_str() + 1);
884                 float4 rgba = kernel_tex_image_interp(slot, s, 1.0f - t);
885
886                 result[0] = rgba[0];
887                 if(nchannels > 1)
888                         result[1] = rgba[1];
889                 if(nchannels > 2)
890                         result[2] = rgba[2];
891                 if(nchannels > 3)
892                         result[3] = rgba[3];
893                 status = true;
894         }
895         else {
896                 OSLThreadData *tdata = kg->osl_tdata;
897                 OIIO::TextureSystem::Perthread *thread_info = tdata->oiio_thread_info;
898
899                 OIIO::TextureSystem::TextureHandle *th = ts->get_texture_handle(filename, thread_info);
900
901 #if OIIO_VERSION < 10500
902                 status = ts->texture(th, thread_info,
903                                      options, s, t, dsdx, dtdx, dsdy, dtdy,
904                                      result);
905 #else
906                 status = ts->texture(th, thread_info,
907                                      options, s, t, dsdx, dtdx, dsdy, dtdy,
908                                      nchannels, result);
909 #endif
910         }
911
912         if(!status) {
913                 if(nchannels == 3 || nchannels == 4) {
914                         result[0] = 1.0f;
915                         result[1] = 0.0f;
916                         result[2] = 1.0f;
917
918                         if(nchannels == 4)
919                                 result[3] = 1.0f;
920                 }
921         }
922
923         return status;
924 }
925
926 bool OSLRenderServices::texture3d(ustring filename, TextureOpt &options,
927                                   OSL::ShaderGlobals *sg, const OSL::Vec3 &P,
928                                   const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy,
929                                   const OSL::Vec3 &dPdz, int nchannels, float *result)
930 {
931         OSL::TextureSystem *ts = osl_ts;
932         ShaderData *sd = (ShaderData *)(sg->renderstate);
933         KernelGlobals *kg = sd->osl_globals;
934         OSLThreadData *tdata = kg->osl_tdata;
935         OIIO::TextureSystem::Perthread *thread_info = tdata->oiio_thread_info;
936
937         OIIO::TextureSystem::TextureHandle *th =  ts->get_texture_handle(filename, thread_info);
938
939 #if OIIO_VERSION < 10500
940         bool status = ts->texture3d(th, thread_info,
941                                     options, P, dPdx, dPdy, dPdz, result);
942 #else
943         bool status = ts->texture3d(th, thread_info,
944                                     options, P, dPdx, dPdy, dPdz,
945                                     nchannels, result);
946 #endif
947
948         if(!status) {
949                 if(nchannels == 3 || nchannels == 4) {
950                         result[0] = 1.0f;
951                         result[1] = 0.0f;
952                         result[2] = 1.0f;
953
954                         if(nchannels == 4)
955                                 result[3] = 1.0f;
956                 }
957
958         }
959
960         return status;
961 }
962
963 bool OSLRenderServices::environment(ustring filename, TextureOpt &options,
964                                     OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
965                                     const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy,
966                                     int nchannels, float *result)
967 {
968         OSL::TextureSystem *ts = osl_ts;
969         ShaderData *sd = (ShaderData *)(sg->renderstate);
970         KernelGlobals *kg = sd->osl_globals;
971         OSLThreadData *tdata = kg->osl_tdata;
972         OIIO::TextureSystem::Perthread *thread_info = tdata->oiio_thread_info;
973
974         OIIO::TextureSystem::TextureHandle *th =  ts->get_texture_handle(filename, thread_info);
975
976 #if OIIO_VERSION < 10500
977         bool status = ts->environment(th, thread_info,
978                                       options, R, dRdx, dRdy, result);
979 #else
980         bool status = ts->environment(th, thread_info,
981                                       options, R, dRdx, dRdy,
982                                       nchannels, result);
983 #endif
984
985         if(!status) {
986                 if(nchannels == 3 || nchannels == 4) {
987                         result[0] = 1.0f;
988                         result[1] = 0.0f;
989                         result[2] = 1.0f;
990
991                         if(nchannels == 4)
992                                 result[3] = 1.0f;
993                 }
994         }
995
996         return status;
997 }
998
999 bool OSLRenderServices::get_texture_info(OSL::ShaderGlobals *sg, ustring filename, int subimage,
1000                                          ustring dataname,
1001                                          TypeDesc datatype, void *data)
1002 {
1003         OSL::TextureSystem *ts = osl_ts;
1004         return ts->get_texture_info(filename, subimage, dataname, datatype, data);
1005 }
1006
1007 int OSLRenderServices::pointcloud_search(OSL::ShaderGlobals *sg, ustring filename, const OSL::Vec3 &center,
1008                                          float radius, int max_points, bool sort,
1009                                          size_t *out_indices, float *out_distances, int derivs_offset)
1010 {
1011         return 0;
1012 }
1013
1014 int OSLRenderServices::pointcloud_get(OSL::ShaderGlobals *sg, ustring filename, size_t *indices, int count,
1015                                       ustring attr_name, TypeDesc attr_type, void *out_data)
1016 {
1017         return 0;
1018 }
1019
1020 bool OSLRenderServices::pointcloud_write(OSL::ShaderGlobals *sg,
1021                                          ustring filename, const OSL::Vec3 &pos,
1022                                          int nattribs, const ustring *names,
1023                                          const TypeDesc *types,
1024                                          const void **data)
1025 {
1026         return false;
1027 }
1028
1029 bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg,
1030         const OSL::Vec3 &P, const OSL::Vec3 &dPdx,
1031         const OSL::Vec3 &dPdy, const OSL::Vec3 &R,
1032         const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy)
1033 {
1034         /* todo: options.shader support, maybe options.traceset */
1035         ShaderData *sd = (ShaderData *)(sg->renderstate);
1036
1037         /* setup ray */
1038         Ray ray;
1039
1040         ray.P = TO_FLOAT3(P);
1041         ray.D = TO_FLOAT3(R);
1042         ray.t = (options.maxdist == 1.0e30f)? FLT_MAX: options.maxdist - options.mindist;
1043         ray.time = sd->time;
1044
1045         if(options.mindist == 0.0f) {
1046                 /* avoid self-intersections */
1047                 if(ray.P == sd->P) {
1048                         bool transmit = (dot(sd->Ng, ray.D) < 0.0f);
1049                         ray.P = ray_offset(sd->P, (transmit)? -sd->Ng: sd->Ng);
1050                 }
1051         }
1052         else {
1053                 /* offset for minimum distance */
1054                 ray.P += options.mindist*ray.D;
1055         }
1056
1057         /* ray differentials */
1058         ray.dP.dx = TO_FLOAT3(dPdx);
1059         ray.dP.dy = TO_FLOAT3(dPdy);
1060         ray.dD.dx = TO_FLOAT3(dRdx);
1061         ray.dD.dy = TO_FLOAT3(dRdy);
1062
1063         /* allocate trace data */
1064         OSLTraceData *tracedata = (OSLTraceData*)sg->tracedata;
1065         tracedata->ray = ray;
1066         tracedata->setup = false;
1067         tracedata->init = true;
1068         tracedata->sd.osl_globals = sd->osl_globals;
1069
1070         /* raytrace */
1071         return scene_intersect(sd->osl_globals, &ray, PATH_RAY_ALL_VISIBILITY, &tracedata->isect, NULL, 0.0f, 0.0f);
1072 }
1073
1074
1075 bool OSLRenderServices::getmessage(OSL::ShaderGlobals *sg, ustring source, ustring name,
1076         TypeDesc type, void *val, bool derivatives)
1077 {
1078         OSLTraceData *tracedata = (OSLTraceData*)sg->tracedata;
1079
1080         if(source == u_trace && tracedata->init) {
1081                 if(name == u_hit) {
1082                         return set_attribute_int((tracedata->isect.prim != PRIM_NONE), type, derivatives, val);
1083                 }
1084                 else if(tracedata->isect.prim != PRIM_NONE) {
1085                         if(name == u_hitdist) {
1086                                 float f[3] = {tracedata->isect.t, 0.0f, 0.0f};
1087                                 return set_attribute_float(f, type, derivatives, val);
1088                         }
1089                         else {
1090                                 ShaderData *sd = &tracedata->sd;
1091                                 KernelGlobals *kg = sd->osl_globals;
1092
1093                                 if(!tracedata->setup) {
1094                                         /* lazy shader data setup */
1095                                         ShaderData *original_sd = (ShaderData *)(sg->renderstate);
1096                                         int bounce = original_sd->ray_depth + 1;
1097                                         int transparent_bounce = original_sd->transparent_depth;
1098
1099                                         shader_setup_from_ray(kg, sd, &tracedata->isect, &tracedata->ray, bounce, transparent_bounce);
1100                                         tracedata->setup = true;
1101                                 }
1102
1103                                 if(name == u_N) {
1104                                         return set_attribute_float3(sd->N, type, derivatives, val);
1105                                 }
1106                                 else if(name == u_Ng) {
1107                                         return set_attribute_float3(sd->Ng, type, derivatives, val);
1108                                 }
1109                                 else if(name == u_P) {
1110                                         float3 f[3] = {sd->P, sd->dP.dx, sd->dP.dy};
1111                                         return set_attribute_float3(f, type, derivatives, val);
1112                                 }
1113                                 else if(name == u_I) {
1114                                         float3 f[3] = {sd->I, sd->dI.dx, sd->dI.dy};
1115                                         return set_attribute_float3(f, type, derivatives, val);
1116                                 }
1117                                 else if(name == u_u) {
1118                                         float f[3] = {sd->u, sd->du.dx, sd->du.dy};
1119                                         return set_attribute_float(f, type, derivatives, val);
1120                                 }
1121                                 else if(name == u_v) {
1122                                         float f[3] = {sd->v, sd->dv.dx, sd->dv.dy};
1123                                         return set_attribute_float(f, type, derivatives, val);
1124                                 }
1125
1126                                 return get_attribute(sd, derivatives, u_empty, type, name, val);
1127                         }
1128                 }
1129         }
1130
1131         return false;
1132 }
1133
1134 CCL_NAMESPACE_END