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