svn merge ^/trunk/blender -r43685:43693
[blender.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
30 #ifdef __OSL__
31
32 #include "osl_shader.h"
33
34 #else
35
36 #include "svm/bsdf.h"
37 #include "svm/emissive.h"
38 #include "svm/volume.h"
39 #include "svm/svm_bsdf.h"
40 #include "svm/svm.h"
41
42 #endif
43
44 CCL_NAMESPACE_BEGIN
45
46 /* ShaderData setup from incoming ray */
47
48 __device_inline void shader_setup_from_ray(KernelGlobals *kg, ShaderData *sd,
49         const Intersection *isect, const Ray *ray)
50 {
51         /* fetch triangle data */
52         int prim = kernel_tex_fetch(__prim_index, isect->prim);
53         float4 Ns = kernel_tex_fetch(__tri_normal, prim);
54         float3 Ng = make_float3(Ns.x, Ns.y, Ns.z);
55         int shader = __float_as_int(Ns.w);
56
57         /* vectors */
58         sd->P = bvh_triangle_refine(kg, isect, ray);
59         sd->Ng = Ng;
60         sd->N = Ng;
61         sd->I = -ray->D;
62         sd->shader = shader;
63
64         /* triangle */
65 #ifdef __INSTANCING__
66         sd->object = isect->object;
67 #endif
68         sd->prim = prim;
69 #ifdef __UV__
70         sd->u = isect->u;
71         sd->v = isect->v;
72 #endif
73
74         /* smooth normal */
75         if(sd->shader & SHADER_SMOOTH_NORMAL)
76                 sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
77
78         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
79
80 #ifdef __DPDU__
81         /* dPdu/dPdv */
82         triangle_dPdudv(kg, &sd->dPdu, &sd->dPdv, sd->prim);
83 #endif
84
85 #ifdef __INSTANCING__
86         if(sd->object != ~0) {
87                 /* instance transform */
88                 object_normal_transform(kg, sd->object, &sd->N);
89                 object_normal_transform(kg, sd->object, &sd->Ng);
90 #ifdef __DPDU__
91                 object_dir_transform(kg, sd->object, &sd->dPdu);
92                 object_dir_transform(kg, sd->object, &sd->dPdv);
93 #endif
94         }
95         else {
96                 /* non-instanced object index */
97                 sd->object = kernel_tex_fetch(__prim_object, isect->prim);
98         }
99 #endif
100
101         /* backfacing test */
102         bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
103
104         if(backfacing) {
105                 sd->flag |= SD_BACKFACING;
106                 sd->Ng = -sd->Ng;
107                 sd->N = -sd->N;
108 #ifdef __DPDU__
109                 sd->dPdu = -sd->dPdu;
110                 sd->dPdv = -sd->dPdv;
111 #endif
112         }
113
114 #ifdef __RAY_DIFFERENTIALS__
115         /* differentials */
116         differential_transfer(&sd->dP, ray->dP, ray->D, ray->dD, sd->Ng, isect->t);
117         differential_incoming(&sd->dI, ray->dD);
118         differential_dudv(&sd->du, &sd->dv, sd->dPdu, sd->dPdv, sd->dP, sd->Ng);
119 #endif
120 }
121
122 /* ShaderData setup from position sampled on mesh */
123
124 __device void shader_setup_from_sample(KernelGlobals *kg, ShaderData *sd,
125         const float3 P, const float3 Ng, const float3 I,
126         int shader, int object, int prim,  float u, float v)
127 {
128         /* vectors */
129         sd->P = P;
130         sd->N = Ng;
131         sd->Ng = Ng;
132         sd->I = I;
133         sd->shader = shader;
134
135         /* primitive */
136 #ifdef __INSTANCING__
137         sd->object = object;
138 #endif
139         sd->prim = prim;
140 #ifdef __UV__
141         sd->u = u;
142         sd->v = v;
143 #endif
144
145         /* detect instancing, for non-instanced the object index is -object-1 */
146 #ifdef __INSTANCING__
147         bool instanced = false;
148
149         if(sd->prim != ~0) {
150                 if(sd->object >= 0)
151                         instanced = true;
152                 else
153 #endif
154                         sd->object = ~sd->object;
155 #ifdef __INSTANCING__
156         }
157 #endif
158
159         /* smooth normal */
160         if(sd->shader & SHADER_SMOOTH_NORMAL) {
161                 sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
162
163 #ifdef __INSTANCING__
164                 if(instanced)
165                         object_normal_transform(kg, sd->object, &sd->N);
166 #endif
167         }
168
169         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
170
171 #ifdef __DPDU__
172         /* dPdu/dPdv */
173         if(sd->prim == ~0) {
174                 sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
175                 sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
176         }
177         else {
178                 triangle_dPdudv(kg, &sd->dPdu, &sd->dPdv, sd->prim);
179
180 #ifdef __INSTANCING__
181                 if(instanced) {
182                         object_dir_transform(kg, sd->object, &sd->dPdu);
183                         object_dir_transform(kg, sd->object, &sd->dPdv);
184                 }
185 #endif
186         }
187 #endif
188
189         /* backfacing test */
190         if(sd->prim != ~0) {
191                 bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
192
193                 if(backfacing) {
194                         sd->flag |= SD_BACKFACING;
195                         sd->Ng = -sd->Ng;
196                         sd->N = -sd->N;
197 #ifdef __DPDU__
198                         sd->dPdu = -sd->dPdu;
199                         sd->dPdv = -sd->dPdv;
200 #endif
201                 }
202         }
203
204 #ifdef __RAY_DIFFERENTIALS__
205         /* no ray differentials here yet */
206         sd->dP.dx = make_float3(0.0f, 0.0f, 0.0f);
207         sd->dP.dy = make_float3(0.0f, 0.0f, 0.0f);
208         sd->dI.dx = make_float3(0.0f, 0.0f, 0.0f);
209         sd->dI.dy = make_float3(0.0f, 0.0f, 0.0f);
210         sd->du.dx = 0.0f;
211         sd->du.dy = 0.0f;
212         sd->dv.dx = 0.0f;
213         sd->dv.dy = 0.0f;
214 #endif
215 }
216
217 /* ShaderData setup for displacement */
218
219 __device void shader_setup_from_displace(KernelGlobals *kg, ShaderData *sd,
220         int object, int prim, float u, float v)
221 {
222         float3 P, Ng, I = make_float3(0.0f, 0.0f, 0.0f);
223         int shader;
224
225         P = triangle_point_MT(kg, prim, u, v);
226         Ng = triangle_normal_MT(kg, prim, &shader);
227
228         /* force smooth shading for displacement */
229         shader |= SHADER_SMOOTH_NORMAL;
230
231         /* watch out: no instance transform currently */
232
233         shader_setup_from_sample(kg, sd, P, Ng, I, shader, object, prim, u, v);
234 }
235
236 /* ShaderData setup from ray into background */
237
238 __device_inline void shader_setup_from_background(KernelGlobals *kg, ShaderData *sd, const Ray *ray)
239 {
240         /* vectors */
241         sd->P = ray->D;
242         sd->N = -sd->P;
243         sd->Ng = -sd->P;
244         sd->I = -sd->P;
245         sd->shader = kernel_data.background.shader;
246         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
247
248 #ifdef __INSTANCING__
249         sd->object = ~0;
250 #endif
251         sd->prim = ~0;
252 #ifdef __UV__
253         sd->u = 0.0f;
254         sd->v = 0.0f;
255 #endif
256
257 #ifdef __DPDU__
258         /* dPdu/dPdv */
259         sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
260         sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
261 #endif
262
263 #ifdef __RAY_DIFFERENTIALS__
264         /* differentials */
265         sd->dP = ray->dD;
266         differential_incoming(&sd->dI, sd->dP);
267         sd->du.dx = 0.0f;
268         sd->du.dy = 0.0f;
269         sd->dv.dx = 0.0f;
270         sd->dv.dy = 0.0f;
271 #endif
272 }
273
274 /* BSDF */
275
276 #ifdef __MULTI_CLOSURE__
277
278 __device_inline void _shader_bsdf_multi_eval(const ShaderData *sd, const float3 omega_in, float *pdf,
279         int skip_bsdf, BsdfEval *bsdf_eval, float sum_pdf, float sum_sample_weight)
280 {
281         for(int i = 0; i< sd->num_closure; i++) {
282                 if(i == skip_bsdf)
283                         continue;
284
285                 const ShaderClosure *sc = &sd->closure[i];
286
287                 if(CLOSURE_IS_BSDF(sc->type)) {
288                         float bsdf_pdf = 0.0f;
289 #ifdef __OSL__
290                         float3 eval = OSLShader::bsdf_eval(sd, sc, omega_in, bsdf_pdf);
291 #else
292                         float3 eval = svm_bsdf_eval(sd, sc, omega_in, &bsdf_pdf);
293 #endif
294
295                         if(bsdf_pdf != 0.0f) {
296                                 bsdf_eval_accum(bsdf_eval, sc->type, eval*sc->weight);
297                                 sum_pdf += bsdf_pdf*sc->sample_weight;
298                         }
299
300                         sum_sample_weight += sc->sample_weight;
301                 }
302         }
303
304         *pdf = sum_pdf/sum_sample_weight;
305 }
306
307 #endif
308
309 __device void shader_bsdf_eval(KernelGlobals *kg, const ShaderData *sd,
310         const float3 omega_in, BsdfEval *eval, float *pdf)
311 {
312 #ifdef __MULTI_CLOSURE__
313         bsdf_eval_init(eval, NBUILTIN_CLOSURES, make_float3(0.0f, 0.0f, 0.0f), kernel_data.film.use_light_pass);
314
315         return _shader_bsdf_multi_eval(sd, omega_in, pdf, -1, eval, 0.0f, 0.0f);
316 #else
317         const ShaderClosure *sc = &sd->closure;
318
319         *pdf = 0.0f;
320         *eval = svm_bsdf_eval(sd, sc, omega_in, pdf)*sc->weight;
321 #endif
322 }
323
324 __device int shader_bsdf_sample(KernelGlobals *kg, const ShaderData *sd,
325         float randu, float randv, BsdfEval *bsdf_eval,
326         float3 *omega_in, differential3 *domega_in, float *pdf)
327 {
328 #ifdef __MULTI_CLOSURE__
329         int sampled = 0;
330
331         if(sd->num_closure > 1) {
332                 /* pick a BSDF closure based on sample weights */
333                 float sum = 0.0f;
334
335                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
336                         const ShaderClosure *sc = &sd->closure[sampled];
337                         
338                         if(CLOSURE_IS_BSDF(sc->type))
339                                 sum += sc->sample_weight;
340                 }
341
342                 float r = sd->randb_closure*sum;
343                 sum = 0.0f;
344
345                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
346                         const ShaderClosure *sc = &sd->closure[sampled];
347                         
348                         if(CLOSURE_IS_BSDF(sc->type)) {
349                                 sum += sd->closure[sampled].sample_weight;
350
351                                 if(r <= sum)
352                                         break;
353                         }
354                 }
355
356                 if(sampled == sd->num_closure) {
357                         *pdf = 0.0f;
358                         return LABEL_NONE;
359                 }
360         }
361
362         const ShaderClosure *sc = &sd->closure[sampled];
363         int label;
364         float3 eval;
365
366         *pdf = 0.0f;
367 #ifdef __OSL__
368         label = OSLShader::bsdf_sample(sd, sc, randu, randv, eval, *omega_in, *domega_in, *pdf);
369 #else
370         label = svm_bsdf_sample(sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
371 #endif
372
373         bsdf_eval_init(bsdf_eval, sc->type, eval*sc->weight, kernel_data.film.use_light_pass);
374
375         if(sd->num_closure > 1 && *pdf != 0.0f) {
376                 float sweight = sc->sample_weight;
377                 _shader_bsdf_multi_eval(sd, *omega_in, pdf, sampled, bsdf_eval, *pdf*sweight, sweight);
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