7e0b0ff4d0f574d02d0aa5cdba629a3c43abc4aa
[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 #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
372         bsdf_eval_init(bsdf_eval, sc->type, eval*sc->weight, kernel_data.film.use_light_pass);
373
374         if(sd->num_closure > 1 && *pdf != 0.0f) {
375                 float sweight = sc->sample_weight;
376                 _shader_bsdf_multi_eval(sd, *omega_in, pdf, sampled, bsdf_eval, *pdf*sweight, sweight);
377         }
378
379         return label;
380 #else
381         /* sample the single closure that we picked */
382         *pdf = 0.0f;
383         int label = svm_bsdf_sample(sd, &sd->closure, randu, randv, bsdf_eval, omega_in, domega_in, pdf);
384         *bsdf_eval *= sd->closure.weight;
385         return label;
386 #endif
387 }
388
389 __device void shader_bsdf_blur(KernelGlobals *kg, ShaderData *sd, float roughness)
390 {
391 #ifndef __OSL__
392 #ifdef __MULTI_CLOSURE__
393         for(int i = 0; i< sd->num_closure; i++) {
394                 ShaderClosure *sc = &sd->closure[i];
395
396                 if(CLOSURE_IS_BSDF(sc->type))
397                         svm_bsdf_blur(sc, roughness);
398         }
399 #else
400         svm_bsdf_blur(&sd->closure, roughness);
401 #endif
402 #endif
403 }
404
405 __device float3 shader_bsdf_transparency(KernelGlobals *kg, ShaderData *sd)
406 {
407 #ifdef __MULTI_CLOSURE__
408         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
409
410         for(int i = 0; i< sd->num_closure; i++) {
411                 ShaderClosure *sc = &sd->closure[i];
412
413                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID) // todo: make this work for osl
414                         eval += sc->weight;
415         }
416
417         return eval;
418 #else
419         if(sd->closure.type == CLOSURE_BSDF_TRANSPARENT_ID)
420                 return sd->closure.weight;
421         else
422                 return make_float3(0.0f, 0.0f, 0.0f);
423 #endif
424 }
425
426 __device float3 shader_bsdf_diffuse(KernelGlobals *kg, ShaderData *sd)
427 {
428 #ifdef __MULTI_CLOSURE__
429         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
430
431         for(int i = 0; i< sd->num_closure; i++) {
432                 ShaderClosure *sc = &sd->closure[i];
433
434                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
435                         eval += sc->weight;
436         }
437
438         return eval;
439 #else
440         if(CLOSURE_IS_BSDF_DIFFUSE(sd->closure.type))
441                 return sd->closure.weight;
442         else
443                 return make_float3(0.0f, 0.0f, 0.0f);
444 #endif
445 }
446
447 __device float3 shader_bsdf_glossy(KernelGlobals *kg, ShaderData *sd)
448 {
449 #ifdef __MULTI_CLOSURE__
450         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
451
452         for(int i = 0; i< sd->num_closure; i++) {
453                 ShaderClosure *sc = &sd->closure[i];
454
455                 if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
456                         eval += sc->weight;
457         }
458
459         return eval;
460 #else
461         if(CLOSURE_IS_BSDF_GLOSSY(sd->closure.type))
462                 return sd->closure.weight;
463         else
464                 return make_float3(0.0f, 0.0f, 0.0f);
465 #endif
466 }
467
468 __device float3 shader_bsdf_transmission(KernelGlobals *kg, ShaderData *sd)
469 {
470 #ifdef __MULTI_CLOSURE__
471         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
472
473         for(int i = 0; i< sd->num_closure; i++) {
474                 ShaderClosure *sc = &sd->closure[i];
475
476                 if(CLOSURE_IS_BSDF_TRANSMISSION(sc->type))
477                         eval += sc->weight;
478         }
479
480         return eval;
481 #else
482         if(CLOSURE_IS_BSDF_TRANSMISSION(sd->closure.type))
483                 return sd->closure.weight;
484         else
485                 return make_float3(0.0f, 0.0f, 0.0f);
486 #endif
487 }
488
489 /* Emission */
490
491 __device float3 shader_emissive_eval(KernelGlobals *kg, ShaderData *sd)
492 {
493         float3 eval;
494 #ifdef __MULTI_CLOSURE__
495         eval = make_float3(0.0f, 0.0f, 0.0f);
496
497         for(int i = 0; i < sd->num_closure; i++) {
498                 ShaderClosure *sc = &sd->closure[i];
499
500                 if(CLOSURE_IS_EMISSION(sc->type)) {
501 #ifdef __OSL__
502                         eval += OSLShader::emissive_eval(sd)*sc->weight;
503 #else
504                         eval += svm_emissive_eval(sd, sc)*sc->weight;
505 #endif
506                 }
507         }
508 #else
509         eval = svm_emissive_eval(sd, &sd->closure)*sd->closure.weight;
510 #endif
511
512         return eval;
513 }
514
515 /* Holdout */
516
517 __device float3 shader_holdout_eval(KernelGlobals *kg, ShaderData *sd)
518 {
519 #ifdef __MULTI_CLOSURE__
520         float3 weight = make_float3(0.0f, 0.0f, 0.0f);
521
522         for(int i = 0; i < sd->num_closure; i++) {
523                 ShaderClosure *sc = &sd->closure[i];
524
525                 if(CLOSURE_IS_HOLDOUT(sc->type))
526                         weight += sc->weight;
527         }
528
529         return weight;
530 #else
531         if(sd->closure.type == CLOSURE_HOLDOUT_ID)
532                 return make_float3(1.0f, 1.0f, 1.0f);
533
534         return make_float3(0.0f, 0.0f, 0.0f);
535 #endif
536 }
537
538 /* Surface Evaluation */
539
540 __device void shader_eval_surface(KernelGlobals *kg, ShaderData *sd,
541         float randb, int path_flag)
542 {
543 #ifdef __OSL__
544         OSLShader::eval_surface(kg, sd, randb, path_flag);
545 #else
546
547 #ifdef __SVM__
548         svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, randb, path_flag);
549 #else
550         bsdf_diffuse_setup(sd, &sd->closure);
551         sd->closure.weight = make_float3(0.8f, 0.8f, 0.8f);
552 #endif
553
554 #endif
555 }
556
557 /* Background Evaluation */
558
559 __device float3 shader_eval_background(KernelGlobals *kg, ShaderData *sd, int path_flag)
560 {
561 #ifdef __OSL__
562         return OSLShader::eval_background(kg, sd, path_flag);
563 #else
564
565 #ifdef __SVM__
566         svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, 0.0f, path_flag);
567
568 #ifdef __MULTI_CLOSURE__
569         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
570
571         for(int i = 0; i< sd->num_closure; i++) {
572                 const ShaderClosure *sc = &sd->closure[i];
573
574                 if(CLOSURE_IS_BACKGROUND(sc->type))
575                         eval += sc->weight;
576         }
577
578         return eval;
579 #else
580         if(sd->closure.type == CLOSURE_BACKGROUND_ID)
581                 return sd->closure.weight;
582         else
583                 return make_float3(0.0f, 0.0f, 0.0f);
584 #endif
585
586 #else
587         return make_float3(0.8f, 0.8f, 0.8f);
588 #endif
589
590 #endif
591 }
592
593 /* Volume */
594
595 __device float3 shader_volume_eval_phase(KernelGlobals *kg, ShaderData *sd,
596         float3 omega_in, float3 omega_out)
597 {
598 #ifdef __MULTI_CLOSURE__
599         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
600
601         for(int i = 0; i< sd->num_closure; i++) {
602                 const ShaderClosure *sc = &sd->closure[i];
603
604                 if(CLOSURE_IS_VOLUME(sc->type)) {
605 #ifdef __OSL__
606                         eval += OSLShader::volume_eval_phase(sd, omega_in, omega_out);
607 #else
608                         eval += volume_eval_phase(sd, sc, omega_in, omega_out);
609 #endif
610                 }
611         }
612
613         return eval;
614 #else
615         return volume_eval_phase(sd, &sd->closure, omega_in, omega_out);
616 #endif
617 }
618
619 /* Volume Evaluation */
620
621 __device void shader_eval_volume(KernelGlobals *kg, ShaderData *sd,
622         float randb, int path_flag)
623 {
624 #ifdef __SVM__
625 #ifdef __OSL__
626         OSLShader::eval_volume(kg, sd, randb, path_flag);
627 #else
628         svm_eval_nodes(kg, sd, SHADER_TYPE_VOLUME, randb, path_flag);
629 #endif
630 #endif
631 }
632
633 /* Displacement Evaluation */
634
635 __device void shader_eval_displacement(KernelGlobals *kg, ShaderData *sd)
636 {
637         /* this will modify sd->P */
638 #ifdef __SVM__
639 #ifdef __OSL__
640         OSLShader::eval_displacement(kg, sd);
641 #else
642         svm_eval_nodes(kg, sd, SHADER_TYPE_DISPLACEMENT, 0.0f, 0);
643 #endif
644 #endif
645 }
646
647 /* Transparent Shadows */
648
649 #ifdef __TRANSPARENT_SHADOWS__
650 __device bool shader_transparent_shadow(KernelGlobals *kg, Intersection *isect)
651 {
652         int prim = kernel_tex_fetch(__prim_index, isect->prim);
653         float4 Ns = kernel_tex_fetch(__tri_normal, prim);
654         int shader = __float_as_int(Ns.w);
655         int flag = kernel_tex_fetch(__shader_flag, (shader & SHADER_MASK)*2);
656
657         return (flag & SD_HAS_SURFACE_TRANSPARENT) != 0;
658 }
659 #endif
660
661 __device int shader_pass_id(KernelGlobals *kg, ShaderData *sd)
662 {
663         return kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2 + 1);
664 }
665
666 /* Free ShaderData */
667
668 __device void shader_release(KernelGlobals *kg, ShaderData *sd)
669 {
670 #ifdef __OSL__
671         OSLShader::release(kg, sd);
672 #endif
673 }
674
675 CCL_NAMESPACE_END
676