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