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