Merging r46096 through r46110 from trunk into soc-2011-tomato
[blender-staging.git] / intern / cycles / kernel / kernel_shader.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 /*
20  * ShaderData, used in four steps:
21  *
22  * Setup from incoming ray, sampled position and background.
23  * Execute for surface, volume or displacement.
24  * Evaluate one or more closures.
25  * Release.
26  *
27  */
28
29 #ifdef __OSL__
30
31 #include "osl_shader.h"
32
33 #else
34
35 #include "svm/bsdf.h"
36 #include "svm/emissive.h"
37 #include "svm/volume.h"
38 #include "svm/svm_bsdf.h"
39 #include "svm/svm.h"
40
41 #endif
42
43 CCL_NAMESPACE_BEGIN
44
45 /* ShaderData setup from incoming ray */
46
47 __device_inline void shader_setup_from_ray(KernelGlobals *kg, ShaderData *sd,
48         const Intersection *isect, const Ray *ray)
49 {
50         /* fetch triangle data */
51         int prim = kernel_tex_fetch(__prim_index, isect->prim);
52         float4 Ns = kernel_tex_fetch(__tri_normal, prim);
53         float3 Ng = make_float3(Ns.x, Ns.y, Ns.z);
54         int shader = __float_as_int(Ns.w);
55
56         /* vectors */
57         sd->P = bvh_triangle_refine(kg, isect, ray);
58         sd->Ng = Ng;
59         sd->N = Ng;
60         sd->I = -ray->D;
61         sd->shader = shader;
62
63         /* triangle */
64 #ifdef __INSTANCING__
65         sd->object = isect->object;
66 #endif
67         sd->prim = prim;
68 #ifdef __UV__
69         sd->u = isect->u;
70         sd->v = isect->v;
71 #endif
72
73         /* smooth normal */
74         if(sd->shader & SHADER_SMOOTH_NORMAL)
75                 sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
76
77         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
78
79 #ifdef __DPDU__
80         /* dPdu/dPdv */
81         triangle_dPdudv(kg, &sd->dPdu, &sd->dPdv, sd->prim);
82 #endif
83
84 #ifdef __INSTANCING__
85         if(sd->object != ~0) {
86                 /* instance transform */
87                 object_normal_transform(kg, sd->object, &sd->N);
88                 object_normal_transform(kg, sd->object, &sd->Ng);
89 #ifdef __DPDU__
90                 object_dir_transform(kg, sd->object, &sd->dPdu);
91                 object_dir_transform(kg, sd->object, &sd->dPdv);
92 #endif
93         }
94         else {
95                 /* non-instanced object index */
96                 sd->object = kernel_tex_fetch(__prim_object, isect->prim);
97         }
98 #endif
99
100         /* backfacing test */
101         bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
102
103         if(backfacing) {
104                 sd->flag |= SD_BACKFACING;
105                 sd->Ng = -sd->Ng;
106                 sd->N = -sd->N;
107 #ifdef __DPDU__
108                 sd->dPdu = -sd->dPdu;
109                 sd->dPdv = -sd->dPdv;
110 #endif
111         }
112
113 #ifdef __RAY_DIFFERENTIALS__
114         /* differentials */
115         differential_transfer(&sd->dP, ray->dP, ray->D, ray->dD, sd->Ng, isect->t);
116         differential_incoming(&sd->dI, ray->dD);
117         differential_dudv(&sd->du, &sd->dv, sd->dPdu, sd->dPdv, sd->dP, sd->Ng);
118 #endif
119 }
120
121 /* ShaderData setup from position sampled on mesh */
122
123 __device void shader_setup_from_sample(KernelGlobals *kg, ShaderData *sd,
124         const float3 P, const float3 Ng, const float3 I,
125         int shader, int object, int prim,  float u, float v)
126 {
127         /* vectors */
128         sd->P = P;
129         sd->N = Ng;
130         sd->Ng = Ng;
131         sd->I = I;
132         sd->shader = shader;
133
134         /* primitive */
135 #ifdef __INSTANCING__
136         sd->object = object;
137 #endif
138         sd->prim = prim;
139 #ifdef __UV__
140         sd->u = u;
141         sd->v = v;
142 #endif
143
144         /* detect instancing, for non-instanced the object index is -object-1 */
145 #ifdef __INSTANCING__
146         bool instanced = false;
147
148         if(sd->prim != ~0) {
149                 if(sd->object >= 0)
150                         instanced = true;
151                 else
152 #endif
153                         sd->object = ~sd->object;
154 #ifdef __INSTANCING__
155         }
156 #endif
157
158         /* smooth normal */
159         if(sd->shader & SHADER_SMOOTH_NORMAL) {
160                 sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
161
162 #ifdef __INSTANCING__
163                 if(instanced)
164                         object_normal_transform(kg, sd->object, &sd->N);
165 #endif
166         }
167
168         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
169
170 #ifdef __DPDU__
171         /* dPdu/dPdv */
172         if(sd->prim == ~0) {
173                 sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
174                 sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
175         }
176         else {
177                 triangle_dPdudv(kg, &sd->dPdu, &sd->dPdv, sd->prim);
178
179 #ifdef __INSTANCING__
180                 if(instanced) {
181                         object_dir_transform(kg, sd->object, &sd->dPdu);
182                         object_dir_transform(kg, sd->object, &sd->dPdv);
183                 }
184 #endif
185         }
186 #endif
187
188         /* backfacing test */
189         if(sd->prim != ~0) {
190                 bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
191
192                 if(backfacing) {
193                         sd->flag |= SD_BACKFACING;
194                         sd->Ng = -sd->Ng;
195                         sd->N = -sd->N;
196 #ifdef __DPDU__
197                         sd->dPdu = -sd->dPdu;
198                         sd->dPdv = -sd->dPdv;
199 #endif
200                 }
201         }
202
203 #ifdef __RAY_DIFFERENTIALS__
204         /* no ray differentials here yet */
205         sd->dP.dx = make_float3(0.0f, 0.0f, 0.0f);
206         sd->dP.dy = make_float3(0.0f, 0.0f, 0.0f);
207         sd->dI.dx = make_float3(0.0f, 0.0f, 0.0f);
208         sd->dI.dy = make_float3(0.0f, 0.0f, 0.0f);
209         sd->du.dx = 0.0f;
210         sd->du.dy = 0.0f;
211         sd->dv.dx = 0.0f;
212         sd->dv.dy = 0.0f;
213 #endif
214 }
215
216 /* ShaderData setup for displacement */
217
218 __device void shader_setup_from_displace(KernelGlobals *kg, ShaderData *sd,
219         int object, int prim, float u, float v)
220 {
221         float3 P, Ng, I = make_float3(0.0f, 0.0f, 0.0f);
222         int shader;
223
224         P = triangle_point_MT(kg, prim, u, v);
225         Ng = triangle_normal_MT(kg, prim, &shader);
226
227         /* force smooth shading for displacement */
228         shader |= SHADER_SMOOTH_NORMAL;
229
230         /* watch out: no instance transform currently */
231
232         shader_setup_from_sample(kg, sd, P, Ng, I, shader, object, prim, u, v);
233 }
234
235 /* ShaderData setup from ray into background */
236
237 __device_inline void shader_setup_from_background(KernelGlobals *kg, ShaderData *sd, const Ray *ray)
238 {
239         /* vectors */
240         sd->P = ray->D;
241         sd->N = -sd->P;
242         sd->Ng = -sd->P;
243         sd->I = -sd->P;
244         sd->shader = kernel_data.background.shader;
245         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
246
247 #ifdef __INSTANCING__
248         sd->object = ~0;
249 #endif
250         sd->prim = ~0;
251 #ifdef __UV__
252         sd->u = 0.0f;
253         sd->v = 0.0f;
254 #endif
255
256 #ifdef __DPDU__
257         /* dPdu/dPdv */
258         sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
259         sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
260 #endif
261
262 #ifdef __RAY_DIFFERENTIALS__
263         /* differentials */
264         sd->dP = ray->dD;
265         differential_incoming(&sd->dI, sd->dP);
266         sd->du.dx = 0.0f;
267         sd->du.dy = 0.0f;
268         sd->dv.dx = 0.0f;
269         sd->dv.dy = 0.0f;
270 #endif
271 }
272
273 /* BSDF */
274
275 #ifdef __MULTI_CLOSURE__
276
277 __device_inline void _shader_bsdf_multi_eval(const ShaderData *sd, const float3 omega_in, float *pdf,
278         int skip_bsdf, BsdfEval *bsdf_eval, float sum_pdf, float sum_sample_weight)
279 {
280         for(int i = 0; i< sd->num_closure; i++) {
281                 if(i == skip_bsdf)
282                         continue;
283
284                 const ShaderClosure *sc = &sd->closure[i];
285
286                 if(CLOSURE_IS_BSDF(sc->type)) {
287                         float bsdf_pdf = 0.0f;
288 #ifdef __OSL__
289                         float3 eval = OSLShader::bsdf_eval(sd, sc, omega_in, bsdf_pdf);
290 #else
291                         float3 eval = svm_bsdf_eval(sd, sc, omega_in, &bsdf_pdf);
292 #endif
293
294                         if(bsdf_pdf != 0.0f) {
295                                 bsdf_eval_accum(bsdf_eval, sc->type, eval*sc->weight);
296                                 sum_pdf += bsdf_pdf*sc->sample_weight;
297                         }
298
299                         sum_sample_weight += sc->sample_weight;
300                 }
301         }
302
303         *pdf = (sum_sample_weight > 0.0f)? sum_pdf/sum_sample_weight: 0.0f;
304 }
305
306 #endif
307
308 __device void shader_bsdf_eval(KernelGlobals *kg, const ShaderData *sd,
309         const float3 omega_in, BsdfEval *eval, float *pdf)
310 {
311 #ifdef __MULTI_CLOSURE__
312         bsdf_eval_init(eval, NBUILTIN_CLOSURES, make_float3(0.0f, 0.0f, 0.0f), kernel_data.film.use_light_pass);
313
314         return _shader_bsdf_multi_eval(sd, omega_in, pdf, -1, eval, 0.0f, 0.0f);
315 #else
316         const ShaderClosure *sc = &sd->closure;
317
318         *pdf = 0.0f;
319         *eval = svm_bsdf_eval(sd, sc, omega_in, pdf)*sc->weight;
320 #endif
321 }
322
323 __device int shader_bsdf_sample(KernelGlobals *kg, const ShaderData *sd,
324         float randu, float randv, BsdfEval *bsdf_eval,
325         float3 *omega_in, differential3 *domega_in, float *pdf)
326 {
327 #ifdef __MULTI_CLOSURE__
328         int sampled = 0;
329
330         if(sd->num_closure > 1) {
331                 /* pick a BSDF closure based on sample weights */
332                 float sum = 0.0f;
333
334                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
335                         const ShaderClosure *sc = &sd->closure[sampled];
336                         
337                         if(CLOSURE_IS_BSDF(sc->type))
338                                 sum += sc->sample_weight;
339                 }
340
341                 float r = sd->randb_closure*sum;
342                 sum = 0.0f;
343
344                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
345                         const ShaderClosure *sc = &sd->closure[sampled];
346                         
347                         if(CLOSURE_IS_BSDF(sc->type)) {
348                                 sum += sd->closure[sampled].sample_weight;
349
350                                 if(r <= sum)
351                                         break;
352                         }
353                 }
354
355                 if(sampled == sd->num_closure) {
356                         *pdf = 0.0f;
357                         return LABEL_NONE;
358                 }
359         }
360
361         const ShaderClosure *sc = &sd->closure[sampled];
362         int label;
363         float3 eval;
364
365         *pdf = 0.0f;
366 #ifdef __OSL__
367         label = OSLShader::bsdf_sample(sd, sc, randu, randv, eval, *omega_in, *domega_in, *pdf);
368 #else
369         label = svm_bsdf_sample(sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
370 #endif
371         if(*pdf != 0.0f) {
372                 bsdf_eval_init(bsdf_eval, sc->type, eval*sc->weight, kernel_data.film.use_light_pass);
373
374                 if(sd->num_closure > 1) {
375                         float sweight = sc->sample_weight;
376                         _shader_bsdf_multi_eval(sd, *omega_in, pdf, sampled, bsdf_eval, *pdf*sweight, sweight);
377                 }
378         }
379
380         return label;
381 #else
382         /* sample the single closure that we picked */
383         *pdf = 0.0f;
384         int label = svm_bsdf_sample(sd, &sd->closure, randu, randv, bsdf_eval, omega_in, domega_in, pdf);
385         *bsdf_eval *= sd->closure.weight;
386         return label;
387 #endif
388 }
389
390 __device void shader_bsdf_blur(KernelGlobals *kg, ShaderData *sd, float roughness)
391 {
392 #ifndef __OSL__
393 #ifdef __MULTI_CLOSURE__
394         for(int i = 0; i< sd->num_closure; i++) {
395                 ShaderClosure *sc = &sd->closure[i];
396
397                 if(CLOSURE_IS_BSDF(sc->type))
398                         svm_bsdf_blur(sc, roughness);
399         }
400 #else
401         svm_bsdf_blur(&sd->closure, roughness);
402 #endif
403 #endif
404 }
405
406 __device float3 shader_bsdf_transparency(KernelGlobals *kg, ShaderData *sd)
407 {
408 #ifdef __MULTI_CLOSURE__
409         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
410
411         for(int i = 0; i< sd->num_closure; i++) {
412                 ShaderClosure *sc = &sd->closure[i];
413
414                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID) // todo: make this work for osl
415                         eval += sc->weight;
416         }
417
418         return eval;
419 #else
420         if(sd->closure.type == CLOSURE_BSDF_TRANSPARENT_ID)
421                 return sd->closure.weight;
422         else
423                 return make_float3(0.0f, 0.0f, 0.0f);
424 #endif
425 }
426
427 __device float3 shader_bsdf_diffuse(KernelGlobals *kg, ShaderData *sd)
428 {
429 #ifdef __MULTI_CLOSURE__
430         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
431
432         for(int i = 0; i< sd->num_closure; i++) {
433                 ShaderClosure *sc = &sd->closure[i];
434
435                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
436                         eval += sc->weight;
437         }
438
439         return eval;
440 #else
441         if(CLOSURE_IS_BSDF_DIFFUSE(sd->closure.type))
442                 return sd->closure.weight;
443         else
444                 return make_float3(0.0f, 0.0f, 0.0f);
445 #endif
446 }
447
448 __device float3 shader_bsdf_glossy(KernelGlobals *kg, ShaderData *sd)
449 {
450 #ifdef __MULTI_CLOSURE__
451         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
452
453         for(int i = 0; i< sd->num_closure; i++) {
454                 ShaderClosure *sc = &sd->closure[i];
455
456                 if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
457                         eval += sc->weight;
458         }
459
460         return eval;
461 #else
462         if(CLOSURE_IS_BSDF_GLOSSY(sd->closure.type))
463                 return sd->closure.weight;
464         else
465                 return make_float3(0.0f, 0.0f, 0.0f);
466 #endif
467 }
468
469 __device float3 shader_bsdf_transmission(KernelGlobals *kg, ShaderData *sd)
470 {
471 #ifdef __MULTI_CLOSURE__
472         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
473
474         for(int i = 0; i< sd->num_closure; i++) {
475                 ShaderClosure *sc = &sd->closure[i];
476
477                 if(CLOSURE_IS_BSDF_TRANSMISSION(sc->type))
478                         eval += sc->weight;
479         }
480
481         return eval;
482 #else
483         if(CLOSURE_IS_BSDF_TRANSMISSION(sd->closure.type))
484                 return sd->closure.weight;
485         else
486                 return make_float3(0.0f, 0.0f, 0.0f);
487 #endif
488 }
489
490 /* Emission */
491
492 __device float3 shader_emissive_eval(KernelGlobals *kg, ShaderData *sd)
493 {
494         float3 eval;
495 #ifdef __MULTI_CLOSURE__
496         eval = make_float3(0.0f, 0.0f, 0.0f);
497
498         for(int i = 0; i < sd->num_closure; i++) {
499                 ShaderClosure *sc = &sd->closure[i];
500
501                 if(CLOSURE_IS_EMISSION(sc->type)) {
502 #ifdef __OSL__
503                         eval += OSLShader::emissive_eval(sd)*sc->weight;
504 #else
505                         eval += svm_emissive_eval(sd, sc)*sc->weight;
506 #endif
507                 }
508         }
509 #else
510         eval = svm_emissive_eval(sd, &sd->closure)*sd->closure.weight;
511 #endif
512
513         return eval;
514 }
515
516 /* Holdout */
517
518 __device float3 shader_holdout_eval(KernelGlobals *kg, ShaderData *sd)
519 {
520 #ifdef __MULTI_CLOSURE__
521         float3 weight = make_float3(0.0f, 0.0f, 0.0f);
522
523         for(int i = 0; i < sd->num_closure; i++) {
524                 ShaderClosure *sc = &sd->closure[i];
525
526                 if(CLOSURE_IS_HOLDOUT(sc->type))
527                         weight += sc->weight;
528         }
529
530         return weight;
531 #else
532         if(sd->closure.type == CLOSURE_HOLDOUT_ID)
533                 return make_float3(1.0f, 1.0f, 1.0f);
534
535         return make_float3(0.0f, 0.0f, 0.0f);
536 #endif
537 }
538
539 /* Surface Evaluation */
540
541 __device void shader_eval_surface(KernelGlobals *kg, ShaderData *sd,
542         float randb, int path_flag)
543 {
544 #ifdef __OSL__
545         OSLShader::eval_surface(kg, sd, randb, path_flag);
546 #else
547
548 #ifdef __SVM__
549         svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, randb, path_flag);
550 #else
551         bsdf_diffuse_setup(sd, &sd->closure);
552         sd->closure.weight = make_float3(0.8f, 0.8f, 0.8f);
553 #endif
554
555 #endif
556 }
557
558 /* Background Evaluation */
559
560 __device float3 shader_eval_background(KernelGlobals *kg, ShaderData *sd, int path_flag)
561 {
562 #ifdef __OSL__
563         return OSLShader::eval_background(kg, sd, path_flag);
564 #else
565
566 #ifdef __SVM__
567         svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, 0.0f, path_flag);
568
569 #ifdef __MULTI_CLOSURE__
570         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
571
572         for(int i = 0; i< sd->num_closure; i++) {
573                 const ShaderClosure *sc = &sd->closure[i];
574
575                 if(CLOSURE_IS_BACKGROUND(sc->type))
576                         eval += sc->weight;
577         }
578
579         return eval;
580 #else
581         if(sd->closure.type == CLOSURE_BACKGROUND_ID)
582                 return sd->closure.weight;
583         else
584                 return make_float3(0.0f, 0.0f, 0.0f);
585 #endif
586
587 #else
588         return make_float3(0.8f, 0.8f, 0.8f);
589 #endif
590
591 #endif
592 }
593
594 /* Volume */
595
596 __device float3 shader_volume_eval_phase(KernelGlobals *kg, ShaderData *sd,
597         float3 omega_in, float3 omega_out)
598 {
599 #ifdef __MULTI_CLOSURE__
600         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
601
602         for(int i = 0; i< sd->num_closure; i++) {
603                 const ShaderClosure *sc = &sd->closure[i];
604
605                 if(CLOSURE_IS_VOLUME(sc->type)) {
606 #ifdef __OSL__
607                         eval += OSLShader::volume_eval_phase(sd, omega_in, omega_out);
608 #else
609                         eval += volume_eval_phase(sd, sc, omega_in, omega_out);
610 #endif
611                 }
612         }
613
614         return eval;
615 #else
616         return volume_eval_phase(sd, &sd->closure, omega_in, omega_out);
617 #endif
618 }
619
620 /* Volume Evaluation */
621
622 __device void shader_eval_volume(KernelGlobals *kg, ShaderData *sd,
623         float randb, int path_flag)
624 {
625 #ifdef __SVM__
626 #ifdef __OSL__
627         OSLShader::eval_volume(kg, sd, randb, path_flag);
628 #else
629         svm_eval_nodes(kg, sd, SHADER_TYPE_VOLUME, randb, path_flag);
630 #endif
631 #endif
632 }
633
634 /* Displacement Evaluation */
635
636 __device void shader_eval_displacement(KernelGlobals *kg, ShaderData *sd)
637 {
638         /* this will modify sd->P */
639 #ifdef __SVM__
640 #ifdef __OSL__
641         OSLShader::eval_displacement(kg, sd);
642 #else
643         svm_eval_nodes(kg, sd, SHADER_TYPE_DISPLACEMENT, 0.0f, 0);
644 #endif
645 #endif
646 }
647
648 /* Transparent Shadows */
649
650 #ifdef __TRANSPARENT_SHADOWS__
651 __device bool shader_transparent_shadow(KernelGlobals *kg, Intersection *isect)
652 {
653         int prim = kernel_tex_fetch(__prim_index, isect->prim);
654         float4 Ns = kernel_tex_fetch(__tri_normal, prim);
655         int shader = __float_as_int(Ns.w);
656         int flag = kernel_tex_fetch(__shader_flag, (shader & SHADER_MASK)*2);
657
658         return (flag & SD_HAS_SURFACE_TRANSPARENT) != 0;
659 }
660 #endif
661
662 __device int shader_pass_id(KernelGlobals *kg, ShaderData *sd)
663 {
664         return kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2 + 1);
665 }
666
667 /* Free ShaderData */
668
669 __device void shader_release(KernelGlobals *kg, ShaderData *sd)
670 {
671 #ifdef __OSL__
672         OSLShader::release(kg, sd);
673 #endif
674 }
675
676 CCL_NAMESPACE_END
677