Fix T41147: Static BVH shading problem
[blender-staging.git] / intern / cycles / kernel / kernel_types.h
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 #ifndef __KERNEL_TYPES_H__
18 #define __KERNEL_TYPES_H__
19
20 #include "kernel_math.h"
21 #include "svm/svm_types.h"
22
23 #ifndef __KERNEL_GPU__
24 #define __KERNEL_CPU__
25 #endif
26
27 CCL_NAMESPACE_BEGIN
28
29 /* constants */
30 #define OBJECT_SIZE             11
31 #define OBJECT_VECTOR_SIZE      6
32 #define LIGHT_SIZE                      4
33 #define FILTER_TABLE_SIZE       256
34 #define RAMP_TABLE_SIZE         256
35 #define PARTICLE_SIZE           5
36 #define TIME_INVALID            FLT_MAX
37
38 #define BSSRDF_MIN_RADIUS                       1e-8f
39 #define BSSRDF_MAX_HITS                         4
40
41 #define BB_DRAPPER                              800.0f
42 #define BB_MAX_TABLE_RANGE              12000.0f
43 #define BB_TABLE_XPOWER                 1.5f
44 #define BB_TABLE_YPOWER                 5.0f
45 #define BB_TABLE_SPACING                2.0f
46
47 #define BECKMANN_TABLE_SIZE             256
48
49 #define TEX_NUM_FLOAT_IMAGES    5
50
51 #define SHADER_NONE                             (~0)
52 #define OBJECT_NONE                             (~0)
53 #define PRIM_NONE                               (~0)
54 #define LAMP_NONE                               (~0)
55
56 #define VOLUME_STACK_SIZE               16
57
58 /* device capabilities */
59 #ifdef __KERNEL_CPU__
60 #define __KERNEL_SHADING__
61 #define __KERNEL_ADV_SHADING__
62 #define __BRANCHED_PATH__
63 #ifdef WITH_OSL
64 #define __OSL__
65 #endif
66 #define __SUBSURFACE__
67 #define __CMJ__
68 #define __VOLUME__
69 #define __SHADOW_RECORD_ALL__
70 #endif
71
72 #ifdef __KERNEL_CUDA__
73 #define __KERNEL_SHADING__
74 #define __KERNEL_ADV_SHADING__
75 #define __BRANCHED_PATH__
76
77 /* Experimental on GPU */
78 //#define __VOLUME__
79 //#define __SUBSURFACE__
80 #endif
81
82 #ifdef __KERNEL_OPENCL__
83
84 /* keep __KERNEL_ADV_SHADING__ in sync with opencl_kernel_use_advanced_shading! */
85
86 #ifdef __KERNEL_OPENCL_NVIDIA__
87 #define __KERNEL_SHADING__
88 #define __KERNEL_ADV_SHADING__
89 #endif
90
91 #ifdef __KERNEL_OPENCL_APPLE__
92 #define __KERNEL_SHADING__
93 //#define __KERNEL_ADV_SHADING__
94 #endif
95
96 #ifdef __KERNEL_OPENCL_AMD__
97 #define __CL_USE_NATIVE__
98 #define __KERNEL_SHADING__
99 //__KERNEL_ADV_SHADING__
100 #define __MULTI_CLOSURE__
101 #define __TRANSPARENT_SHADOWS__
102 #define __PASSES__
103 #define __BACKGROUND_MIS__
104 #define __LAMP_MIS__
105 #define __AO__
106 #define __ANISOTROPIC__
107 //#define __CAMERA_MOTION__
108 //#define __OBJECT_MOTION__
109 //#define __HAIR__
110 //end __KERNEL_ADV_SHADING__
111 #endif
112
113 #ifdef __KERNEL_OPENCL_INTEL_CPU__
114 #define __CL_USE_NATIVE__
115 #define __KERNEL_SHADING__
116 #define __KERNEL_ADV_SHADING__
117 #endif
118
119 #endif
120
121 /* kernel features */
122 #define __SOBOL__
123 #define __INSTANCING__
124 #define __DPDU__
125 #define __UV__
126 #define __BACKGROUND__
127 #define __CAUSTICS_TRICKS__
128 #define __VISIBILITY_FLAG__
129 #define __RAY_DIFFERENTIALS__
130 #define __CAMERA_CLIPPING__
131 #define __INTERSECTION_REFINE__
132 #define __CLAMP_SAMPLE__
133
134 #ifdef __KERNEL_SHADING__
135 #define __SVM__
136 #define __EMISSION__
137 #define __PROCEDURAL_TEXTURES__
138 #define __IMAGE_TEXTURES__
139 #define __EXTRA_NODES__
140 #define __HOLDOUT__
141 #define __NORMAL_MAP__
142 #endif
143
144 #ifdef __KERNEL_ADV_SHADING__
145 #define __MULTI_CLOSURE__
146 #define __TRANSPARENT_SHADOWS__
147 #define __PASSES__
148 #define __BACKGROUND_MIS__
149 #define __LAMP_MIS__
150 #define __AO__
151 #define __ANISOTROPIC__
152 #define __CAMERA_MOTION__
153 #define __OBJECT_MOTION__
154 #define __HAIR__
155 #endif
156
157 /* Random Numbers */
158
159 typedef uint RNG;
160
161 /* Shader Evaluation */
162
163 typedef enum ShaderEvalType {
164         SHADER_EVAL_DISPLACE,
165         SHADER_EVAL_BACKGROUND,
166         /* bake types */
167         SHADER_EVAL_BAKE, /* no real shade, it's used in the code to
168                            * differentiate the type of shader eval from the above
169                            */
170         /* data passes */
171         SHADER_EVAL_NORMAL,
172         SHADER_EVAL_UV,
173         SHADER_EVAL_DIFFUSE_COLOR,
174         SHADER_EVAL_GLOSSY_COLOR,
175         SHADER_EVAL_TRANSMISSION_COLOR,
176         SHADER_EVAL_SUBSURFACE_COLOR,
177         SHADER_EVAL_EMISSION,
178
179         /* light passes */
180         SHADER_EVAL_AO,
181         SHADER_EVAL_COMBINED,
182         SHADER_EVAL_SHADOW,
183         SHADER_EVAL_DIFFUSE_DIRECT,
184         SHADER_EVAL_GLOSSY_DIRECT,
185         SHADER_EVAL_TRANSMISSION_DIRECT,
186         SHADER_EVAL_SUBSURFACE_DIRECT,
187         SHADER_EVAL_DIFFUSE_INDIRECT,
188         SHADER_EVAL_GLOSSY_INDIRECT,
189         SHADER_EVAL_TRANSMISSION_INDIRECT,
190         SHADER_EVAL_SUBSURFACE_INDIRECT,
191
192         /* extra */
193         SHADER_EVAL_ENVIRONMENT,
194 } ShaderEvalType;
195
196 /* Path Tracing
197  * note we need to keep the u/v pairs at even values */
198
199 enum PathTraceDimension {
200         PRNG_FILTER_U = 0,
201         PRNG_FILTER_V = 1,
202         PRNG_LENS_U = 2,
203         PRNG_LENS_V = 3,
204 #ifdef __CAMERA_MOTION__
205         PRNG_TIME = 4,
206         PRNG_UNUSED_0 = 5,
207         PRNG_UNUSED_1 = 6,      /* for some reason (6, 7) is a bad sobol pattern */
208         PRNG_UNUSED_2 = 7,  /* with a low number of samples (< 64) */
209 #endif
210         PRNG_BASE_NUM = 8,
211
212         PRNG_BSDF_U = 0,
213         PRNG_BSDF_V = 1,
214         PRNG_BSDF = 2,
215         PRNG_LIGHT = 3,
216         PRNG_LIGHT_U = 4,
217         PRNG_LIGHT_V = 5,
218         PRNG_UNUSED_3 = 6,
219         PRNG_TERMINATE = 7,
220
221 #ifdef __VOLUME__
222         PRNG_PHASE_U = 8,
223         PRNG_PHASE_V = 9,
224         PRNG_PHASE = 10,
225         PRNG_SCATTER_DISTANCE = 11,
226         PRNG_BOUNCE_NUM = 12,
227 #else
228         PRNG_BOUNCE_NUM = 8,
229 #endif
230 };
231
232 enum SamplingPattern {
233         SAMPLING_PATTERN_SOBOL = 0,
234         SAMPLING_PATTERN_CMJ = 1
235 };
236
237 /* these flags values correspond to raytypes in osl.cpp, so keep them in sync!
238  *
239  * for ray visibility tests in BVH traversal, the upper 20 bits are used for
240  * layer visibility tests. */
241
242 enum PathRayFlag {
243         PATH_RAY_CAMERA = 1,
244         PATH_RAY_REFLECT = 2,
245         PATH_RAY_TRANSMIT = 4,
246         PATH_RAY_DIFFUSE = 8,
247         PATH_RAY_GLOSSY = 16,
248         PATH_RAY_SINGULAR = 32,
249         PATH_RAY_TRANSPARENT = 64,
250
251         PATH_RAY_SHADOW_OPAQUE = 128,
252         PATH_RAY_SHADOW_TRANSPARENT = 256,
253         PATH_RAY_SHADOW = (PATH_RAY_SHADOW_OPAQUE|PATH_RAY_SHADOW_TRANSPARENT),
254
255         PATH_RAY_CURVE = 512, /* visibility flag to define curve segments*/
256
257         /* note that these can use maximum 12 bits, the other are for layers */
258         PATH_RAY_ALL_VISIBILITY = (1|2|4|8|16|32|64|128|256|512),
259
260         PATH_RAY_MIS_SKIP = 1024,
261         PATH_RAY_DIFFUSE_ANCESTOR = 2048,
262         PATH_RAY_GLOSSY_ANCESTOR = 4096,
263         PATH_RAY_BSSRDF_ANCESTOR = 8192,
264         PATH_RAY_SINGLE_PASS_DONE = 16384,
265         PATH_RAY_VOLUME_SCATTER = 32768,
266
267         /* we need layer member flags to be the 20 upper bits */
268         PATH_RAY_LAYER_SHIFT = (32-20)
269 };
270
271 /* Closure Label */
272
273 typedef enum ClosureLabel {
274         LABEL_NONE = 0,
275         LABEL_TRANSMIT = 1,
276         LABEL_REFLECT = 2,
277         LABEL_DIFFUSE = 4,
278         LABEL_GLOSSY = 8,
279         LABEL_SINGULAR = 16,
280         LABEL_TRANSPARENT = 32,
281         LABEL_VOLUME_SCATTER = 64,
282 } ClosureLabel;
283
284 /* Render Passes */
285
286 typedef enum PassType {
287         PASS_NONE = 0,
288         PASS_COMBINED = 1,
289         PASS_DEPTH = 2,
290         PASS_NORMAL = 4,
291         PASS_UV = 8,
292         PASS_OBJECT_ID = 16,
293         PASS_MATERIAL_ID = 32,
294         PASS_DIFFUSE_COLOR = 64,
295         PASS_GLOSSY_COLOR = 128,
296         PASS_TRANSMISSION_COLOR = 256,
297         PASS_DIFFUSE_INDIRECT = 512,
298         PASS_GLOSSY_INDIRECT = 1024,
299         PASS_TRANSMISSION_INDIRECT = 2048,
300         PASS_DIFFUSE_DIRECT = 4096,
301         PASS_GLOSSY_DIRECT = 8192,
302         PASS_TRANSMISSION_DIRECT = 16384,
303         PASS_EMISSION = 32768,
304         PASS_BACKGROUND = 65536,
305         PASS_AO = 131072,
306         PASS_SHADOW = 262144,
307         PASS_MOTION = 524288,
308         PASS_MOTION_WEIGHT = 1048576,
309         PASS_MIST = 2097152,
310         PASS_SUBSURFACE_DIRECT = 4194304,
311         PASS_SUBSURFACE_INDIRECT = 8388608,
312         PASS_SUBSURFACE_COLOR = 16777216,
313         PASS_LIGHT = 33554432, /* no real pass, used to force use_light_pass */
314 } PassType;
315
316 #define PASS_ALL (~0)
317
318 #ifdef __PASSES__
319
320 typedef struct PathRadiance {
321         int use_light_pass;
322
323         float3 emission;
324         float3 background;
325         float3 ao;
326
327         float3 indirect;
328         float3 direct_throughput;
329         float3 direct_emission;
330
331         float3 color_diffuse;
332         float3 color_glossy;
333         float3 color_transmission;
334         float3 color_subsurface;
335
336         float3 direct_diffuse;
337         float3 direct_glossy;
338         float3 direct_transmission;
339         float3 direct_subsurface;
340
341         float3 indirect_diffuse;
342         float3 indirect_glossy;
343         float3 indirect_transmission;
344         float3 indirect_subsurface;
345
346         float3 path_diffuse;
347         float3 path_glossy;
348         float3 path_transmission;
349         float3 path_subsurface;
350
351         float4 shadow;
352         float mist;
353 } PathRadiance;
354
355 typedef struct BsdfEval {
356         int use_light_pass;
357
358         float3 diffuse;
359         float3 glossy;
360         float3 transmission;
361         float3 transparent;
362         float3 subsurface;
363 } BsdfEval;
364
365 #else
366
367 typedef float3 PathRadiance;
368 typedef float3 BsdfEval;
369
370 #endif
371
372 /* Shader Flag */
373
374 typedef enum ShaderFlag {
375         SHADER_SMOOTH_NORMAL = (1 << 31),
376         SHADER_CAST_SHADOW = (1 << 30),
377         SHADER_AREA_LIGHT = (1 << 29),
378         SHADER_USE_MIS = (1 << 28),
379         SHADER_EXCLUDE_DIFFUSE = (1 << 27),
380         SHADER_EXCLUDE_GLOSSY = (1 << 26),
381         SHADER_EXCLUDE_TRANSMIT = (1 << 25),
382         SHADER_EXCLUDE_CAMERA = (1 << 24),
383         SHADER_EXCLUDE_ANY = (SHADER_EXCLUDE_DIFFUSE|SHADER_EXCLUDE_GLOSSY|SHADER_EXCLUDE_TRANSMIT|SHADER_EXCLUDE_CAMERA),
384
385         SHADER_MASK = ~(SHADER_SMOOTH_NORMAL|SHADER_CAST_SHADOW|SHADER_AREA_LIGHT|SHADER_USE_MIS|SHADER_EXCLUDE_ANY)
386 } ShaderFlag;
387
388 /* Light Type */
389
390 typedef enum LightType {
391         LIGHT_POINT,
392         LIGHT_DISTANT,
393         LIGHT_BACKGROUND,
394         LIGHT_AREA,
395         LIGHT_SPOT,
396         LIGHT_TRIANGLE
397 } LightType;
398
399 /* Camera Type */
400
401 enum CameraType {
402         CAMERA_PERSPECTIVE,
403         CAMERA_ORTHOGRAPHIC,
404         CAMERA_PANORAMA
405 };
406
407 /* Panorama Type */
408
409 enum PanoramaType {
410         PANORAMA_EQUIRECTANGULAR,
411         PANORAMA_FISHEYE_EQUIDISTANT,
412         PANORAMA_FISHEYE_EQUISOLID
413 };
414
415 /* Differential */
416
417 typedef struct differential3 {
418         float3 dx;
419         float3 dy;
420 } differential3;
421
422 typedef struct differential {
423         float dx;
424         float dy;
425 } differential;
426
427 /* Ray */
428
429 typedef struct Ray {
430         float3 P;               /* origin */
431         float3 D;               /* direction */
432         float t;                /* length of the ray */
433         float time;             /* time (for motion blur) */
434
435 #ifdef __RAY_DIFFERENTIALS__
436         differential3 dP;
437         differential3 dD;
438 #endif
439 } Ray;
440
441 /* Intersection */
442
443 typedef struct Intersection {
444         float t, u, v;
445         int prim;
446         int object;
447         int type;
448 } Intersection;
449
450 /* Primitives */
451
452 typedef enum PrimitiveType {
453         PRIMITIVE_NONE = 0,
454         PRIMITIVE_TRIANGLE = 1,
455         PRIMITIVE_MOTION_TRIANGLE = 2,
456         PRIMITIVE_CURVE = 4,
457         PRIMITIVE_MOTION_CURVE = 8,
458
459         PRIMITIVE_ALL_TRIANGLE = (PRIMITIVE_TRIANGLE|PRIMITIVE_MOTION_TRIANGLE),
460         PRIMITIVE_ALL_CURVE = (PRIMITIVE_CURVE|PRIMITIVE_MOTION_CURVE),
461         PRIMITIVE_ALL_MOTION = (PRIMITIVE_MOTION_TRIANGLE|PRIMITIVE_MOTION_CURVE),
462         PRIMITIVE_ALL = (PRIMITIVE_ALL_TRIANGLE|PRIMITIVE_ALL_CURVE)
463 } PrimitiveType;
464
465 #define PRIMITIVE_PACK_SEGMENT(type, segment) ((segment << 16) | type)
466 #define PRIMITIVE_UNPACK_SEGMENT(type) (type >> 16)
467
468 /* Attributes */
469
470 #define ATTR_PRIM_TYPES         2
471 #define ATTR_PRIM_CURVE         1
472
473 typedef enum AttributeElement {
474         ATTR_ELEMENT_NONE,
475         ATTR_ELEMENT_OBJECT,
476         ATTR_ELEMENT_MESH,
477         ATTR_ELEMENT_FACE,
478         ATTR_ELEMENT_VERTEX,
479         ATTR_ELEMENT_VERTEX_MOTION,
480         ATTR_ELEMENT_CORNER,
481         ATTR_ELEMENT_CORNER_BYTE,
482         ATTR_ELEMENT_CURVE,
483         ATTR_ELEMENT_CURVE_KEY,
484         ATTR_ELEMENT_CURVE_KEY_MOTION,
485         ATTR_ELEMENT_VOXEL
486 } AttributeElement;
487
488 typedef enum AttributeStandard {
489         ATTR_STD_NONE = 0,
490         ATTR_STD_VERTEX_NORMAL,
491         ATTR_STD_FACE_NORMAL,
492         ATTR_STD_UV,
493         ATTR_STD_UV_TANGENT,
494         ATTR_STD_UV_TANGENT_SIGN,
495         ATTR_STD_GENERATED,
496         ATTR_STD_GENERATED_TRANSFORM,
497         ATTR_STD_POSITION_UNDEFORMED,
498         ATTR_STD_POSITION_UNDISPLACED,
499         ATTR_STD_MOTION_VERTEX_POSITION,
500         ATTR_STD_MOTION_VERTEX_NORMAL,
501         ATTR_STD_PARTICLE,
502         ATTR_STD_CURVE_INTERCEPT,
503         ATTR_STD_PTEX_FACE_ID,
504         ATTR_STD_PTEX_UV,
505         ATTR_STD_VOLUME_DENSITY,
506         ATTR_STD_VOLUME_COLOR,
507         ATTR_STD_VOLUME_FLAME,
508         ATTR_STD_VOLUME_HEAT,
509         ATTR_STD_VOLUME_VELOCITY,
510         ATTR_STD_NUM,
511
512         ATTR_STD_NOT_FOUND = ~0
513 } AttributeStandard;
514
515 /* Closure data */
516
517 #ifdef __MULTI_CLOSURE__
518 #define MAX_CLOSURE 64
519 #else
520 #define MAX_CLOSURE 1
521 #endif
522
523 typedef struct ShaderClosure {
524         ClosureType type;
525         float3 weight;
526
527         float sample_weight;
528
529         float data0;
530         float data1;
531         float data2;
532
533         float3 N;
534         float3 T;
535
536 #ifdef __OSL__
537         void *prim;
538 #endif
539 } ShaderClosure;
540
541 /* Shader Context
542  *
543  * For OSL we recycle a fixed number of contexts for speed */
544
545 typedef enum ShaderContext {
546         SHADER_CONTEXT_MAIN = 0,
547         SHADER_CONTEXT_INDIRECT = 1,
548         SHADER_CONTEXT_EMISSION = 2,
549         SHADER_CONTEXT_SHADOW = 3,
550         SHADER_CONTEXT_SSS = 4,
551         SHADER_CONTEXT_VOLUME = 5,
552         SHADER_CONTEXT_NUM = 6
553 } ShaderContext;
554
555 /* Shader Data
556  *
557  * Main shader state at a point on the surface or in a volume. All coordinates
558  * are in world space. */
559
560 enum ShaderDataFlag {
561         /* runtime flags */
562         SD_BACKFACING = 1,              /* backside of surface? */
563         SD_EMISSION = 2,                /* have emissive closure? */
564         SD_BSDF = 4,                    /* have bsdf closure? */
565         SD_BSDF_HAS_EVAL = 8,   /* have non-singular bsdf closure? */
566         SD_PHASE_HAS_EVAL = 8,  /* have non-singular phase closure? */
567         SD_BSDF_GLOSSY = 16,    /* have glossy bsdf */
568         SD_BSSRDF = 32,                 /* have bssrdf */
569         SD_HOLDOUT = 64,                /* have holdout closure? */
570         SD_ABSORPTION = 128,    /* have volume absorption closure? */
571         SD_SCATTER = 256,               /* have volume phase closure? */
572         SD_AO = 512,                    /* have ao closure? */
573         SD_TRANSPARENT = 1024,  /* have transparent closure? */
574
575         SD_CLOSURE_FLAGS = (SD_EMISSION|SD_BSDF|SD_BSDF_HAS_EVAL|SD_BSDF_GLOSSY|
576                             SD_BSSRDF|SD_HOLDOUT|SD_ABSORPTION|SD_SCATTER|SD_AO),
577
578         /* shader flags */
579         SD_USE_MIS = 2048,                                      /* direct light sample */
580         SD_HAS_TRANSPARENT_SHADOW = 4096,       /* has transparent shadow */
581         SD_HAS_VOLUME = 8192,                           /* has volume shader */
582         SD_HAS_ONLY_VOLUME = 16384,                     /* has only volume shader, no surface */
583         SD_HETEROGENEOUS_VOLUME = 32768,        /* has heterogeneous volume */
584         SD_HAS_BSSRDF_BUMP = 65536,                     /* bssrdf normal uses bump */
585         SD_VOLUME_EQUIANGULAR = 131072,         /* use equiangular sampling */
586         SD_VOLUME_MIS = 262144,                         /* use multiple importance sampling */
587
588         SD_SHADER_FLAGS = (SD_USE_MIS|SD_HAS_TRANSPARENT_SHADOW|SD_HAS_VOLUME|
589                            SD_HAS_ONLY_VOLUME|SD_HETEROGENEOUS_VOLUME|
590                                            SD_HAS_BSSRDF_BUMP|SD_VOLUME_EQUIANGULAR|SD_VOLUME_MIS),
591
592         /* object flags */
593         SD_HOLDOUT_MASK = 524288,                       /* holdout for camera rays */
594         SD_OBJECT_MOTION = 1048576,                     /* has object motion blur */
595         SD_TRANSFORM_APPLIED = 2097152,         /* vertices have transform applied */
596         SD_NEGATIVE_SCALE_APPLIED = 4194304,    /* vertices have negative scale applied */
597
598         SD_OBJECT_FLAGS = (SD_HOLDOUT_MASK|SD_OBJECT_MOTION|SD_TRANSFORM_APPLIED)
599 };
600
601 struct KernelGlobals;
602
603 typedef struct ShaderData {
604         /* position */
605         float3 P;
606         /* smooth normal for shading */
607         float3 N;
608         /* true geometric normal */
609         float3 Ng;
610         /* view/incoming direction */
611         float3 I;
612         /* shader id */
613         int shader;
614         /* booleans describing shader, see ShaderDataFlag */
615         int flag;
616
617         /* primitive id if there is one, ~0 otherwise */
618         int prim;
619
620         /* combined type and curve segment for hair */
621         int type;
622
623         /* parametric coordinates
624          * - barycentric weights for triangles */
625         float u, v;
626         /* object id if there is one, ~0 otherwise */
627         int object;
628
629         /* motion blur sample time */
630         float time;
631         
632         /* length of the ray being shaded */
633         float ray_length;
634         
635         /* ray bounce depth */
636         int ray_depth;
637
638         /* ray transparent depth */
639         int transparent_depth;
640
641 #ifdef __RAY_DIFFERENTIALS__
642         /* differential of P. these are orthogonal to Ng, not N */
643         differential3 dP;
644         /* differential of I */
645         differential3 dI;
646         /* differential of u, v */
647         differential du;
648         differential dv;
649 #endif
650 #ifdef __DPDU__
651         /* differential of P w.r.t. parametric coordinates. note that dPdu is
652          * not readily suitable as a tangent for shading on triangles. */
653         float3 dPdu, dPdv;
654 #endif
655
656 #ifdef __OBJECT_MOTION__
657         /* object <-> world space transformations, cached to avoid
658          * re-interpolating them constantly for shading */
659         Transform ob_tfm;
660         Transform ob_itfm;
661 #endif
662
663         /* Closure data, we store a fixed array of closures */
664         ShaderClosure closure[MAX_CLOSURE];
665         int num_closure;
666         float randb_closure;
667
668         /* ray start position, only set for backgrounds */
669         float3 ray_P;
670         differential3 ray_dP;
671
672 #ifdef __OSL__
673         struct KernelGlobals *osl_globals;
674 #endif
675 } ShaderData;
676
677 /* Path State */
678
679 #ifdef __VOLUME__
680 typedef struct VolumeStack {
681         int object;
682         int shader;
683 } VolumeStack;
684 #endif
685
686 typedef struct PathState {
687         /* see enum PathRayFlag */
688         int flag;          
689
690         /* random number generator state */
691         int rng_offset;                 /* dimension offset */
692         int rng_offset_bsdf;    /* dimension offset for picking bsdf */
693         int sample;                     /* path sample number */
694         int num_samples;                /* total number of times this path will be sampled */
695
696         /* bounce counting */
697         int bounce;
698         int diffuse_bounce;
699         int glossy_bounce;
700         int transmission_bounce;
701         int transparent_bounce;
702
703         /* multiple importance sampling */
704         float min_ray_pdf; /* smallest bounce pdf over entire path up to now */
705         float ray_pdf;     /* last bounce pdf */
706 #ifdef __LAMP_MIS__
707         float ray_t;       /* accumulated distance through transparent surfaces */
708 #endif
709
710         /* volume rendering */
711 #ifdef __VOLUME__
712         int volume_bounce;
713         RNG rng_congruential;
714         VolumeStack volume_stack[VOLUME_STACK_SIZE];
715 #endif
716 } PathState;
717
718 /* Constant Kernel Data
719  *
720  * These structs are passed from CPU to various devices, and the struct layout
721  * must match exactly. Structs are padded to ensure 16 byte alignment, and we
722  * do not use float3 because its size may not be the same on all devices. */
723
724 typedef struct KernelCamera {
725         /* type */
726         int type;
727
728         /* panorama */
729         int panorama_type;
730         float fisheye_fov;
731         float fisheye_lens;
732
733         /* matrices */
734         Transform cameratoworld;
735         Transform rastertocamera;
736
737         /* differentials */
738         float4 dx;
739         float4 dy;
740
741         /* depth of field */
742         float aperturesize;
743         float blades;
744         float bladesrotation;
745         float focaldistance;
746
747         /* motion blur */
748         float shuttertime;
749         int have_motion;
750
751         /* clipping */
752         float nearclip;
753         float cliplength;
754
755         /* sensor size */
756         float sensorwidth;
757         float sensorheight;
758
759         /* render size */
760         float width, height;
761         int resolution;
762         int pad1;
763         int pad2;
764         int pad3;
765
766         /* more matrices */
767         Transform screentoworld;
768         Transform rastertoworld;
769         /* work around cuda sm 2.0 crash, this seems to
770          * cross some limit in combination with motion 
771          * Transform ndctoworld; */
772         Transform worldtoscreen;
773         Transform worldtoraster;
774         Transform worldtondc;
775         Transform worldtocamera;
776
777         MotionTransform motion;
778 } KernelCamera;
779
780 typedef struct KernelFilm {
781         float exposure;
782         int pass_flag;
783         int pass_stride;
784         int use_light_pass;
785
786         int pass_combined;
787         int pass_depth;
788         int pass_normal;
789         int pass_motion;
790
791         int pass_motion_weight;
792         int pass_uv;
793         int pass_object_id;
794         int pass_material_id;
795
796         int pass_diffuse_color;
797         int pass_glossy_color;
798         int pass_transmission_color;
799         int pass_subsurface_color;
800         
801         int pass_diffuse_indirect;
802         int pass_glossy_indirect;
803         int pass_transmission_indirect;
804         int pass_subsurface_indirect;
805         
806         int pass_diffuse_direct;
807         int pass_glossy_direct;
808         int pass_transmission_direct;
809         int pass_subsurface_direct;
810         
811         int pass_emission;
812         int pass_background;
813         int pass_ao;
814         float pass_alpha_threshold;
815
816         int pass_shadow;
817         float pass_shadow_scale;
818         int filter_table_offset;
819         int pass_pad2;
820
821         int pass_mist;
822         float mist_start;
823         float mist_inv_depth;
824         float mist_falloff;
825 } KernelFilm;
826
827 typedef struct KernelBackground {
828         /* only shader index */
829         int surface_shader;
830         int volume_shader;
831         int transparent;
832         int pad;
833
834         /* ambient occlusion */
835         float ao_factor;
836         float ao_distance;
837         float ao_pad1, ao_pad2;
838 } KernelBackground;
839
840 typedef struct KernelIntegrator {
841         /* emission */
842         int use_direct_light;
843         int use_ambient_occlusion;
844         int num_distribution;
845         int num_all_lights;
846         float pdf_triangles;
847         float pdf_lights;
848         float inv_pdf_lights;
849         int pdf_background_res;
850
851         /* bounces */
852         int min_bounce;
853         int max_bounce;
854
855         int max_diffuse_bounce;
856         int max_glossy_bounce;
857         int max_transmission_bounce;
858         int max_volume_bounce;
859
860         /* transparent */
861         int transparent_min_bounce;
862         int transparent_max_bounce;
863         int transparent_shadows;
864
865         /* caustics */
866         int no_caustics;
867         float filter_glossy;
868
869         /* seed */
870         int seed;
871
872         /* render layer */
873         int layer_flag;
874
875         /* clamp */
876         float sample_clamp_direct;
877         float sample_clamp_indirect;
878
879         /* branched path */
880         int branched;
881         int diffuse_samples;
882         int glossy_samples;
883         int transmission_samples;
884         int ao_samples;
885         int mesh_light_samples;
886         int subsurface_samples;
887         int sample_all_lights_direct;
888         int sample_all_lights_indirect;
889
890         /* mis */
891         int use_lamp_mis;
892
893         /* sampler */
894         int sampling_pattern;
895         int aa_samples;
896
897         /* volume render */
898         int use_volumes;
899         int volume_max_steps;
900         float volume_step_size;
901         int volume_samples;
902 } KernelIntegrator;
903
904 typedef struct KernelBVH {
905         /* root node */
906         int root;
907         int attributes_map_stride;
908         int have_motion;
909         int have_curves;
910         int have_instancing;
911
912         int pad1, pad2, pad3;
913 } KernelBVH;
914
915 typedef enum CurveFlag {
916         /* runtime flags */
917         CURVE_KN_BACKFACING = 1,                                /* backside of cylinder? */
918         CURVE_KN_ENCLOSEFILTER = 2,                             /* don't consider strands surrounding start point? */
919         CURVE_KN_INTERPOLATE = 4,                               /* render as a curve? */
920         CURVE_KN_ACCURATE = 8,                                  /* use accurate intersections test? */
921         CURVE_KN_INTERSECTCORRECTION = 16,              /* correct for width after determing closest midpoint? */
922         CURVE_KN_TRUETANGENTGNORMAL = 32,               /* use tangent normal for geometry? */
923         CURVE_KN_RIBBONS = 64,                                  /* use flat curve ribbons */
924 } CurveFlag;
925
926 typedef struct KernelCurves {
927         int curveflags;
928         int subdivisions;
929
930         float minimum_width;
931         float maximum_width;
932 } KernelCurves;
933
934 typedef struct KernelTables {
935         int blackbody_offset;
936         int beckmann_offset;
937         int pad1, pad2;
938 } KernelTables;
939
940 typedef struct KernelData {
941         KernelCamera cam;
942         KernelFilm film;
943         KernelBackground background;
944         KernelIntegrator integrator;
945         KernelBVH bvh;
946         KernelCurves curve;
947         KernelTables tables;
948 } KernelData;
949
950 CCL_NAMESPACE_END
951
952 #endif /*  __KERNEL_TYPES_H__ */
953