Cycles: svn merge -r41225:41232 ^/trunk/blender
[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);
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-1;
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);
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         if(shader >= 0)
230                 shader = -shader;
231
232         /* watch out: no instance transform currently */
233
234         shader_setup_from_sample(kg, sd, P, Ng, I, shader, object, prim, u, v);
235 }
236
237 /* ShaderData setup from ray into background */
238
239 __device_inline void shader_setup_from_background(KernelGlobals *kg, ShaderData *sd, const Ray *ray)
240 {
241         /* vectors */
242         sd->P = ray->D;
243         sd->N = -sd->P;
244         sd->Ng = -sd->P;
245         sd->I = -sd->P;
246         sd->shader = kernel_data.background.shader;
247         sd->flag = kernel_tex_fetch(__shader_flag, sd->shader & SHADER_MASK);
248
249 #ifdef __INSTANCING__
250         sd->object = ~0;
251 #endif
252         sd->prim = ~0;
253 #ifdef __UV__
254         sd->u = 0.0f;
255         sd->v = 0.0f;
256 #endif
257
258 #ifdef __DPDU__
259         /* dPdu/dPdv */
260         sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
261         sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
262 #endif
263
264 #ifdef __RAY_DIFFERENTIALS__
265         /* differentials */
266         sd->dP = ray->dD;
267         differential_incoming(&sd->dI, sd->dP);
268         sd->du.dx = 0.0f;
269         sd->du.dy = 0.0f;
270         sd->dv.dx = 0.0f;
271         sd->dv.dy = 0.0f;
272 #endif
273 }
274
275 /* BSDF */
276
277 #ifdef __MULTI_CLOSURE__
278
279 __device_inline float3 _shader_bsdf_multi_eval(const ShaderData *sd, const float3 omega_in, float *pdf,
280         int skip_bsdf, float3 sum_eval, float sum_pdf, float sum_sample_weight)
281 {
282         for(int i = 0; i< sd->num_closure; i++) {
283                 if(i == skip_bsdf)
284                         continue;
285
286                 const ShaderClosure *sc = &sd->closure[i];
287
288                 if(CLOSURE_IS_BSDF(sc->type)) {
289                         float bsdf_pdf = 0.0f;
290 #ifdef __OSL__
291                         float3 eval = OSLShader::bsdf_eval(sd, sc, omega_in, bsdf_pdf);
292 #else
293                         float3 eval = svm_bsdf_eval(sd, sc, omega_in, &bsdf_pdf);
294 #endif
295
296                         if(bsdf_pdf != 0.0f) {
297                                 sum_eval += eval*sc->weight;
298                                 sum_pdf += bsdf_pdf*sc->sample_weight;
299                         }
300
301                         sum_sample_weight += sc->sample_weight;
302                 }
303         }
304
305         *pdf = sum_pdf/sum_sample_weight;
306         return sum_eval;
307 }
308
309 #endif
310
311 __device float3 shader_bsdf_eval(KernelGlobals *kg, const ShaderData *sd,
312         const float3 omega_in, float *pdf)
313 {
314 #ifdef __MULTI_CLOSURE__
315         return _shader_bsdf_multi_eval(sd, omega_in, pdf, -1, make_float3(0.0f, 0.0f, 0.0f), 0.0f, 0.0f);
316 #else
317         const ShaderClosure *sc = &sd->closure;
318         *pdf = 0.0f;
319         return 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, float3 *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
364         *pdf = 0.0f;
365 #ifdef __OSL__
366         label = OSLShader::bsdf_sample(sd, sc, randu, randv, *eval, *omega_in, *domega_in, *pdf);
367 #else
368         label = svm_bsdf_sample(sd, sc, randu, randv, eval, omega_in, domega_in, pdf);
369 #endif
370
371         *eval *= sc->weight;
372
373         if(sd->num_closure > 1 && *pdf != 0.0f) {
374                 float sweight = sc->sample_weight;
375                 *eval = _shader_bsdf_multi_eval(sd, *omega_in, pdf, sampled, *eval, *pdf*sweight, sweight);
376         }
377
378         return label;
379 #else
380         /* sample the single closure that we picked */
381         *pdf = 0.0f;
382         int label = svm_bsdf_sample(sd, &sd->closure, randu, randv, eval, omega_in, domega_in, pdf);
383         *eval *= sd->closure.weight;
384         return label;
385 #endif
386 }
387
388 __device void shader_bsdf_blur(KernelGlobals *kg, ShaderData *sd, float roughness)
389 {
390 #ifndef __OSL__
391 #ifdef __MULTI_CLOSURE__
392         for(int i = 0; i< sd->num_closure; i++) {
393                 ShaderClosure *sc = &sd->closure[i];
394
395                 if(CLOSURE_IS_BSDF(sc->type))
396                         svm_bsdf_blur(sc, roughness);
397         }
398 #else
399         svm_bsdf_blur(&sd->closure, roughness);
400 #endif
401 #endif
402 }
403
404 __device float3 shader_bsdf_transparency(KernelGlobals *kg, ShaderData *sd)
405 {
406 #ifdef __MULTI_CLOSURE__
407         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
408
409         for(int i = 0; i< sd->num_closure; i++) {
410                 ShaderClosure *sc = &sd->closure[i];
411
412                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID) // todo: make this work for osl
413                         eval += sc->weight;
414         }
415
416         return eval;
417 #else
418         if(sd->closure.type == CLOSURE_BSDF_TRANSPARENT_ID)
419                 return sd->closure.weight;
420         else
421                 return make_float3(0.0f, 0.0f, 0.0f);
422 #endif
423 }
424
425
426 /* Emission */
427
428 __device float3 shader_emissive_eval(KernelGlobals *kg, ShaderData *sd)
429 {
430         float3 eval;
431 #ifdef __MULTI_CLOSURE__
432         eval = make_float3(0.0f, 0.0f, 0.0f);
433
434         for(int i = 0; i < sd->num_closure; i++) {
435                 ShaderClosure *sc = &sd->closure[i];
436
437                 if(CLOSURE_IS_EMISSION(sc->type)) {
438 #ifdef __OSL__
439                         eval += OSLShader::emissive_eval(sd)*sc->weight;
440 #else
441                         eval += svm_emissive_eval(sd, sc)*sc->weight;
442 #endif
443                 }
444         }
445 #else
446         eval = svm_emissive_eval(sd, &sd->closure)*sd->closure.weight;
447 #endif
448
449         return eval;
450 }
451
452 /* Holdout */
453
454 __device float3 shader_holdout_eval(KernelGlobals *kg, ShaderData *sd)
455 {
456 #ifdef __MULTI_CLOSURE__
457         float3 weight = make_float3(0.0f, 0.0f, 0.0f);
458
459         for(int i = 0; i < sd->num_closure; i++) {
460                 ShaderClosure *sc = &sd->closure[i];
461
462                 if(CLOSURE_IS_HOLDOUT(sc->type))
463                         weight += sc->weight;
464         }
465
466         return weight;
467 #else
468         if(sd->closure.type == CLOSURE_HOLDOUT_ID)
469                 return make_float3(1.0f, 1.0f, 1.0f);
470
471         return make_float3(0.0f, 0.0f, 0.0f);
472 #endif
473 }
474
475 /* Surface Evaluation */
476
477 __device void shader_eval_surface(KernelGlobals *kg, ShaderData *sd,
478         float randb, int path_flag)
479 {
480 #ifdef __OSL__
481         OSLShader::eval_surface(kg, sd, randb, path_flag);
482 #else
483
484 #ifdef __SVM__
485         svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, randb, path_flag);
486 #else
487         bsdf_diffuse_setup(sd, &sd->closure);
488         sd->closure.weight = make_float3(0.8f, 0.8f, 0.8f);
489 #endif
490
491 #endif
492 }
493
494 /* Background Evaluation */
495
496 __device float3 shader_eval_background(KernelGlobals *kg, ShaderData *sd, int path_flag)
497 {
498 #ifdef __OSL__
499         return OSLShader::eval_background(kg, sd, path_flag);
500 #else
501
502 #ifdef __SVM__
503         svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, 0.0f, path_flag);
504
505 #ifdef __MULTI_CLOSURE__
506         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
507
508         for(int i = 0; i< sd->num_closure; i++) {
509                 const ShaderClosure *sc = &sd->closure[i];
510
511                 if(CLOSURE_IS_BACKGROUND(sc->type))
512                         eval += sc->weight;
513         }
514
515         return eval;
516 #else
517         if(sd->closure.type == CLOSURE_BACKGROUND_ID)
518                 return sd->closure.weight;
519         else
520                 return make_float3(0.0f, 0.0f, 0.0f);
521 #endif
522
523 #else
524         return make_float3(0.8f, 0.8f, 0.8f);
525 #endif
526
527 #endif
528 }
529
530 /* Volume */
531
532 __device float3 shader_volume_eval_phase(KernelGlobals *kg, ShaderData *sd,
533         float3 omega_in, float3 omega_out)
534 {
535 #ifdef __MULTI_CLOSURE__
536         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
537
538         for(int i = 0; i< sd->num_closure; i++) {
539                 const ShaderClosure *sc = &sd->closure[i];
540
541                 if(CLOSURE_IS_VOLUME(sc->type)) {
542 #ifdef __OSL__
543                         eval += OSLShader::volume_eval_phase(sd, omega_in, omega_out);
544 #else
545                         eval += volume_eval_phase(sd, sc, omega_in, omega_out);
546 #endif
547                 }
548         }
549
550         return eval;
551 #else
552         return volume_eval_phase(sd, &sd->closure, omega_in, omega_out);
553 #endif
554 }
555
556 /* Volume Evaluation */
557
558 __device void shader_eval_volume(KernelGlobals *kg, ShaderData *sd,
559         float randb, int path_flag)
560 {
561 #ifdef __SVM__
562 #ifdef __OSL__
563         OSLShader::eval_volume(kg, sd, randb, path_flag);
564 #else
565         svm_eval_nodes(kg, sd, SHADER_TYPE_VOLUME, randb, path_flag);
566 #endif
567 #endif
568 }
569
570 /* Displacement Evaluation */
571
572 __device void shader_eval_displacement(KernelGlobals *kg, ShaderData *sd)
573 {
574         /* this will modify sd->P */
575 #ifdef __SVM__
576 #ifdef __OSL__
577         OSLShader::eval_displacement(kg, sd);
578 #else
579         svm_eval_nodes(kg, sd, SHADER_TYPE_DISPLACEMENT, 0.0f, 0);
580 #endif
581 #endif
582 }
583
584 /* Transparent Shadows */
585
586 #ifdef __TRANSPARENT_SHADOWS__
587 __device bool shader_transparent_shadow(KernelGlobals *kg, Intersection *isect)
588 {
589         int prim = kernel_tex_fetch(__prim_index, isect->prim);
590         float4 Ns = kernel_tex_fetch(__tri_normal, prim);
591         int shader = __float_as_int(Ns.w);
592         int flag = kernel_tex_fetch(__shader_flag, shader & SHADER_MASK);
593
594         return (flag & SD_HAS_SURFACE_TRANSPARENT) != 0;
595 }
596 #endif
597
598 /* Free ShaderData */
599
600 __device void shader_release(KernelGlobals *kg, ShaderData *sd)
601 {
602 #ifdef __OSL__
603         OSLShader::release(kg, sd);
604 #endif
605 }
606
607 CCL_NAMESPACE_END
608