Cycles: improved Beckmann sampling using precomputed data
[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_AO,
396         LIGHT_SPOT,
397         LIGHT_TRIANGLE,
398         LIGHT_STRAND
399 } LightType;
400
401 /* Camera Type */
402
403 enum CameraType {
404         CAMERA_PERSPECTIVE,
405         CAMERA_ORTHOGRAPHIC,
406         CAMERA_PANORAMA
407 };
408
409 /* Panorama Type */
410
411 enum PanoramaType {
412         PANORAMA_EQUIRECTANGULAR,
413         PANORAMA_FISHEYE_EQUIDISTANT,
414         PANORAMA_FISHEYE_EQUISOLID
415 };
416
417 /* Differential */
418
419 typedef struct differential3 {
420         float3 dx;
421         float3 dy;
422 } differential3;
423
424 typedef struct differential {
425         float dx;
426         float dy;
427 } differential;
428
429 /* Ray */
430
431 typedef struct Ray {
432         float3 P;               /* origin */
433         float3 D;               /* direction */
434         float t;                /* length of the ray */
435         float time;             /* time (for motion blur) */
436
437 #ifdef __RAY_DIFFERENTIALS__
438         differential3 dP;
439         differential3 dD;
440 #endif
441 } Ray;
442
443 /* Intersection */
444
445 typedef struct Intersection {
446         float t, u, v;
447         int prim;
448         int object;
449         int type;
450 } Intersection;
451
452 /* Primitives */
453
454 typedef enum PrimitiveType {
455         PRIMITIVE_NONE = 0,
456         PRIMITIVE_TRIANGLE = 1,
457         PRIMITIVE_MOTION_TRIANGLE = 2,
458         PRIMITIVE_CURVE = 4,
459         PRIMITIVE_MOTION_CURVE = 8,
460
461         PRIMITIVE_ALL_TRIANGLE = (PRIMITIVE_TRIANGLE|PRIMITIVE_MOTION_TRIANGLE),
462         PRIMITIVE_ALL_CURVE = (PRIMITIVE_CURVE|PRIMITIVE_MOTION_CURVE),
463         PRIMITIVE_ALL_MOTION = (PRIMITIVE_MOTION_TRIANGLE|PRIMITIVE_MOTION_CURVE),
464         PRIMITIVE_ALL = (PRIMITIVE_ALL_TRIANGLE|PRIMITIVE_ALL_CURVE)
465 } PrimitiveType;
466
467 #define PRIMITIVE_PACK_SEGMENT(type, segment) ((segment << 16) | type)
468 #define PRIMITIVE_UNPACK_SEGMENT(type) (type >> 16)
469
470 /* Attributes */
471
472 #define ATTR_PRIM_TYPES         2
473 #define ATTR_PRIM_CURVE         1
474
475 typedef enum AttributeElement {
476         ATTR_ELEMENT_NONE,
477         ATTR_ELEMENT_OBJECT,
478         ATTR_ELEMENT_MESH,
479         ATTR_ELEMENT_FACE,
480         ATTR_ELEMENT_VERTEX,
481         ATTR_ELEMENT_VERTEX_MOTION,
482         ATTR_ELEMENT_CORNER,
483         ATTR_ELEMENT_CORNER_BYTE,
484         ATTR_ELEMENT_CURVE,
485         ATTR_ELEMENT_CURVE_KEY,
486         ATTR_ELEMENT_CURVE_KEY_MOTION,
487         ATTR_ELEMENT_VOXEL
488 } AttributeElement;
489
490 typedef enum AttributeStandard {
491         ATTR_STD_NONE = 0,
492         ATTR_STD_VERTEX_NORMAL,
493         ATTR_STD_FACE_NORMAL,
494         ATTR_STD_UV,
495         ATTR_STD_UV_TANGENT,
496         ATTR_STD_UV_TANGENT_SIGN,
497         ATTR_STD_GENERATED,
498         ATTR_STD_GENERATED_TRANSFORM,
499         ATTR_STD_POSITION_UNDEFORMED,
500         ATTR_STD_POSITION_UNDISPLACED,
501         ATTR_STD_MOTION_VERTEX_POSITION,
502         ATTR_STD_MOTION_VERTEX_NORMAL,
503         ATTR_STD_PARTICLE,
504         ATTR_STD_CURVE_INTERCEPT,
505         ATTR_STD_PTEX_FACE_ID,
506         ATTR_STD_PTEX_UV,
507         ATTR_STD_VOLUME_DENSITY,
508         ATTR_STD_VOLUME_COLOR,
509         ATTR_STD_VOLUME_FLAME,
510         ATTR_STD_VOLUME_HEAT,
511         ATTR_STD_VOLUME_VELOCITY,
512         ATTR_STD_NUM,
513
514         ATTR_STD_NOT_FOUND = ~0
515 } AttributeStandard;
516
517 /* Closure data */
518
519 #ifdef __MULTI_CLOSURE__
520 #define MAX_CLOSURE 64
521 #else
522 #define MAX_CLOSURE 1
523 #endif
524
525 typedef struct ShaderClosure {
526         ClosureType type;
527         float3 weight;
528
529         float sample_weight;
530
531         float data0;
532         float data1;
533
534         float3 N;
535 #if defined(__ANISOTROPIC__) || defined(__SUBSURFACE__) || defined(__HAIR__)
536         float3 T;
537 #endif
538
539         float data2;
540 #ifdef __OSL__
541         void *prim;
542 #endif
543 } ShaderClosure;
544
545 /* Shader Context
546  *
547  * For OSL we recycle a fixed number of contexts for speed */
548
549 typedef enum ShaderContext {
550         SHADER_CONTEXT_MAIN = 0,
551         SHADER_CONTEXT_INDIRECT = 1,
552         SHADER_CONTEXT_EMISSION = 2,
553         SHADER_CONTEXT_SHADOW = 3,
554         SHADER_CONTEXT_SSS = 4,
555         SHADER_CONTEXT_VOLUME = 5,
556         SHADER_CONTEXT_NUM = 6
557 } ShaderContext;
558
559 /* Shader Data
560  *
561  * Main shader state at a point on the surface or in a volume. All coordinates
562  * are in world space. */
563
564 enum ShaderDataFlag {
565         /* runtime flags */
566         SD_BACKFACING = 1,              /* backside of surface? */
567         SD_EMISSION = 2,                /* have emissive closure? */
568         SD_BSDF = 4,                    /* have bsdf closure? */
569         SD_BSDF_HAS_EVAL = 8,   /* have non-singular bsdf closure? */
570         SD_PHASE_HAS_EVAL = 8,  /* have non-singular phase closure? */
571         SD_BSDF_GLOSSY = 16,    /* have glossy bsdf */
572         SD_BSSRDF = 32,                 /* have bssrdf */
573         SD_HOLDOUT = 64,                /* have holdout closure? */
574         SD_ABSORPTION = 128,    /* have volume absorption closure? */
575         SD_SCATTER = 256,               /* have volume phase closure? */
576         SD_AO = 512,                    /* have ao closure? */
577         SD_TRANSPARENT = 1024,  /* have transparent closure? */
578
579         SD_CLOSURE_FLAGS = (SD_EMISSION|SD_BSDF|SD_BSDF_HAS_EVAL|SD_BSDF_GLOSSY|
580                             SD_BSSRDF|SD_HOLDOUT|SD_ABSORPTION|SD_SCATTER|SD_AO),
581
582         /* shader flags */
583         SD_USE_MIS = 2048,                                      /* direct light sample */
584         SD_HAS_TRANSPARENT_SHADOW = 4096,       /* has transparent shadow */
585         SD_HAS_VOLUME = 8192,                           /* has volume shader */
586         SD_HAS_ONLY_VOLUME = 16384,                     /* has only volume shader, no surface */
587         SD_HETEROGENEOUS_VOLUME = 32768,        /* has heterogeneous volume */
588         SD_HAS_BSSRDF_BUMP = 65536,                     /* bssrdf normal uses bump */
589         SD_VOLUME_EQUIANGULAR = 131072,         /* use equiangular sampling */
590         SD_VOLUME_MIS = 262144,                         /* use multiple importance sampling */
591
592         SD_SHADER_FLAGS = (SD_USE_MIS|SD_HAS_TRANSPARENT_SHADOW|SD_HAS_VOLUME|
593                            SD_HAS_ONLY_VOLUME|SD_HETEROGENEOUS_VOLUME|
594                                            SD_HAS_BSSRDF_BUMP|SD_VOLUME_EQUIANGULAR|SD_VOLUME_MIS),
595
596         /* object flags */
597         SD_HOLDOUT_MASK = 524288,                       /* holdout for camera rays */
598         SD_OBJECT_MOTION = 1048576,                     /* has object motion blur */
599         SD_TRANSFORM_APPLIED = 2097152,         /* vertices have transform applied */
600
601         SD_OBJECT_FLAGS = (SD_HOLDOUT_MASK|SD_OBJECT_MOTION|SD_TRANSFORM_APPLIED)
602 };
603
604 struct KernelGlobals;
605
606 typedef struct ShaderData {
607         /* position */
608         float3 P;
609         /* smooth normal for shading */
610         float3 N;
611         /* true geometric normal */
612         float3 Ng;
613         /* view/incoming direction */
614         float3 I;
615         /* shader id */
616         int shader;
617         /* booleans describing shader, see ShaderDataFlag */
618         int flag;
619
620         /* primitive id if there is one, ~0 otherwise */
621         int prim;
622
623         /* combined type and curve segment for hair */
624         int type;
625
626         /* parametric coordinates
627          * - barycentric weights for triangles */
628         float u, v;
629         /* object id if there is one, ~0 otherwise */
630         int object;
631
632         /* motion blur sample time */
633         float time;
634         
635         /* length of the ray being shaded */
636         float ray_length;
637         
638         /* ray bounce depth */
639         int ray_depth;
640
641         /* ray transparent depth */
642         int transparent_depth;
643
644 #ifdef __RAY_DIFFERENTIALS__
645         /* differential of P. these are orthogonal to Ng, not N */
646         differential3 dP;
647         /* differential of I */
648         differential3 dI;
649         /* differential of u, v */
650         differential du;
651         differential dv;
652 #endif
653 #ifdef __DPDU__
654         /* differential of P w.r.t. parametric coordinates. note that dPdu is
655          * not readily suitable as a tangent for shading on triangles. */
656         float3 dPdu, dPdv;
657 #endif
658
659 #ifdef __OBJECT_MOTION__
660         /* object <-> world space transformations, cached to avoid
661          * re-interpolating them constantly for shading */
662         Transform ob_tfm;
663         Transform ob_itfm;
664 #endif
665
666         /* Closure data, we store a fixed array of closures */
667         ShaderClosure closure[MAX_CLOSURE];
668         int num_closure;
669         float randb_closure;
670
671         /* ray start position, only set for backgrounds */
672         float3 ray_P;
673         differential3 ray_dP;
674
675 #ifdef __OSL__
676         struct KernelGlobals *osl_globals;
677 #endif
678 } ShaderData;
679
680 /* Path State */
681
682 #ifdef __VOLUME__
683 typedef struct VolumeStack {
684         int object;
685         int shader;
686 } VolumeStack;
687 #endif
688
689 typedef struct PathState {
690         /* see enum PathRayFlag */
691         int flag;          
692
693         /* random number generator state */
694         int rng_offset;                 /* dimension offset */
695         int rng_offset_bsdf;    /* dimension offset for picking bsdf */
696         int sample;                     /* path sample number */
697         int num_samples;                /* total number of times this path will be sampled */
698
699         /* bounce counting */
700         int bounce;
701         int diffuse_bounce;
702         int glossy_bounce;
703         int transmission_bounce;
704         int transparent_bounce;
705
706         /* multiple importance sampling */
707         float min_ray_pdf; /* smallest bounce pdf over entire path up to now */
708         float ray_pdf;     /* last bounce pdf */
709 #ifdef __LAMP_MIS__
710         float ray_t;       /* accumulated distance through transparent surfaces */
711 #endif
712
713         /* volume rendering */
714 #ifdef __VOLUME__
715         int volume_bounce;
716         RNG rng_congruential;
717         VolumeStack volume_stack[VOLUME_STACK_SIZE];
718 #endif
719 } PathState;
720
721 /* Constant Kernel Data
722  *
723  * These structs are passed from CPU to various devices, and the struct layout
724  * must match exactly. Structs are padded to ensure 16 byte alignment, and we
725  * do not use float3 because its size may not be the same on all devices. */
726
727 typedef struct KernelCamera {
728         /* type */
729         int type;
730
731         /* panorama */
732         int panorama_type;
733         float fisheye_fov;
734         float fisheye_lens;
735
736         /* matrices */
737         Transform cameratoworld;
738         Transform rastertocamera;
739
740         /* differentials */
741         float4 dx;
742         float4 dy;
743
744         /* depth of field */
745         float aperturesize;
746         float blades;
747         float bladesrotation;
748         float focaldistance;
749
750         /* motion blur */
751         float shuttertime;
752         int have_motion;
753
754         /* clipping */
755         float nearclip;
756         float cliplength;
757
758         /* sensor size */
759         float sensorwidth;
760         float sensorheight;
761
762         /* render size */
763         float width, height;
764         int resolution;
765         int pad1;
766         int pad2;
767         int pad3;
768
769         /* more matrices */
770         Transform screentoworld;
771         Transform rastertoworld;
772         /* work around cuda sm 2.0 crash, this seems to
773          * cross some limit in combination with motion 
774          * Transform ndctoworld; */
775         Transform worldtoscreen;
776         Transform worldtoraster;
777         Transform worldtondc;
778         Transform worldtocamera;
779
780         MotionTransform motion;
781 } KernelCamera;
782
783 typedef struct KernelFilm {
784         float exposure;
785         int pass_flag;
786         int pass_stride;
787         int use_light_pass;
788
789         int pass_combined;
790         int pass_depth;
791         int pass_normal;
792         int pass_motion;
793
794         int pass_motion_weight;
795         int pass_uv;
796         int pass_object_id;
797         int pass_material_id;
798
799         int pass_diffuse_color;
800         int pass_glossy_color;
801         int pass_transmission_color;
802         int pass_subsurface_color;
803         
804         int pass_diffuse_indirect;
805         int pass_glossy_indirect;
806         int pass_transmission_indirect;
807         int pass_subsurface_indirect;
808         
809         int pass_diffuse_direct;
810         int pass_glossy_direct;
811         int pass_transmission_direct;
812         int pass_subsurface_direct;
813         
814         int pass_emission;
815         int pass_background;
816         int pass_ao;
817         float pass_alpha_threshold;
818
819         int pass_shadow;
820         float pass_shadow_scale;
821         int filter_table_offset;
822         int pass_pad2;
823
824         int pass_mist;
825         float mist_start;
826         float mist_inv_depth;
827         float mist_falloff;
828 } KernelFilm;
829
830 typedef struct KernelBackground {
831         /* only shader index */
832         int surface_shader;
833         int volume_shader;
834         int transparent;
835         int pad;
836
837         /* ambient occlusion */
838         float ao_factor;
839         float ao_distance;
840         float ao_pad1, ao_pad2;
841 } KernelBackground;
842
843 typedef struct KernelIntegrator {
844         /* emission */
845         int use_direct_light;
846         int use_ambient_occlusion;
847         int num_distribution;
848         int num_all_lights;
849         float pdf_triangles;
850         float pdf_lights;
851         float inv_pdf_lights;
852         int pdf_background_res;
853
854         /* bounces */
855         int min_bounce;
856         int max_bounce;
857
858         int max_diffuse_bounce;
859         int max_glossy_bounce;
860         int max_transmission_bounce;
861         int max_volume_bounce;
862
863         /* transparent */
864         int transparent_min_bounce;
865         int transparent_max_bounce;
866         int transparent_shadows;
867
868         /* caustics */
869         int no_caustics;
870         float filter_glossy;
871
872         /* seed */
873         int seed;
874
875         /* render layer */
876         int layer_flag;
877
878         /* clamp */
879         float sample_clamp_direct;
880         float sample_clamp_indirect;
881
882         /* branched path */
883         int branched;
884         int diffuse_samples;
885         int glossy_samples;
886         int transmission_samples;
887         int ao_samples;
888         int mesh_light_samples;
889         int subsurface_samples;
890         int sample_all_lights_direct;
891         int sample_all_lights_indirect;
892
893         /* mis */
894         int use_lamp_mis;
895
896         /* sampler */
897         int sampling_pattern;
898         int aa_samples;
899
900         /* volume render */
901         int use_volumes;
902         int volume_max_steps;
903         float volume_step_size;
904         int volume_samples;
905 } KernelIntegrator;
906
907 typedef struct KernelBVH {
908         /* root node */
909         int root;
910         int attributes_map_stride;
911         int have_motion;
912         int have_curves;
913         int have_instancing;
914
915         int pad1, pad2, pad3;
916 } KernelBVH;
917
918 typedef enum CurveFlag {
919         /* runtime flags */
920         CURVE_KN_BACKFACING = 1,                                /* backside of cylinder? */
921         CURVE_KN_ENCLOSEFILTER = 2,                             /* don't consider strands surrounding start point? */
922         CURVE_KN_INTERPOLATE = 4,                               /* render as a curve? */
923         CURVE_KN_ACCURATE = 8,                                  /* use accurate intersections test? */
924         CURVE_KN_INTERSECTCORRECTION = 16,              /* correct for width after determing closest midpoint? */
925         CURVE_KN_TRUETANGENTGNORMAL = 32,               /* use tangent normal for geometry? */
926         CURVE_KN_RIBBONS = 64,                                  /* use flat curve ribbons */
927 } CurveFlag;
928
929 typedef struct KernelCurves {
930         /* strand intersect and normal parameters - many can be changed to flags */
931         int curveflags;
932         int subdivisions;
933
934         float minimum_width;
935         float maximum_width;
936 } KernelCurves;
937
938 typedef struct KernelTables {
939         int blackbody_offset;
940         int beckmann_offset;
941         int pad1, pad2;
942 } KernelTables;
943
944 typedef struct KernelData {
945         KernelCamera cam;
946         KernelFilm film;
947         KernelBackground background;
948         KernelIntegrator integrator;
949         KernelBVH bvh;
950         KernelCurves curve;
951         KernelTables tables;
952 } KernelData;
953
954 CCL_NAMESPACE_END
955
956 #endif /*  __KERNEL_TYPES_H__ */
957