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