Cycles Volume Render: support for rendering of homogeneous volume with absorption.
[blender.git] / intern / cycles / kernel / svm / svm_closure.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 CCL_NAMESPACE_BEGIN
18
19 /* Closure Nodes */
20
21 ccl_device void svm_node_glass_setup(ShaderData *sd, ShaderClosure *sc, int type, float eta, float roughness, bool refract)
22 {
23         if(type == CLOSURE_BSDF_SHARP_GLASS_ID) {
24                 if(refract) {
25                         sc->data0 = eta;
26                         sc->data1 = 0.0f;
27                         sd->flag |= bsdf_refraction_setup(sc);
28                 }
29                 else
30                         sd->flag |= bsdf_reflection_setup(sc);
31         }
32         else if(type == CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID) {
33                 sc->data0 = roughness;
34                 sc->data1 = eta;
35
36                 if(refract)
37                         sd->flag |= bsdf_microfacet_beckmann_refraction_setup(sc);
38                 else
39                         sd->flag |= bsdf_microfacet_beckmann_setup(sc);
40         }
41         else {
42                 sc->data0 = roughness;
43                 sc->data1 = eta;
44
45                 if(refract)
46                         sd->flag |= bsdf_microfacet_ggx_refraction_setup(sc);
47                 else
48                         sd->flag |= bsdf_microfacet_ggx_setup(sc);
49         }
50 }
51
52 ccl_device_inline ShaderClosure *svm_node_closure_get_non_bsdf(ShaderData *sd, ClosureType type, float mix_weight)
53 {
54 #ifdef __MULTI_CLOSURE__
55         ShaderClosure *sc = &sd->closure[sd->num_closure];
56
57         if(sd->num_closure < MAX_CLOSURE) {
58                 sc->weight *= mix_weight;
59                 sc->type = type;
60 #ifdef __OSL__
61                 sc->prim = NULL;
62 #endif
63                 sd->num_closure++;
64                 return sc;
65         }
66
67         return NULL;
68 #else
69         return &sd->closure;
70 #endif
71 }
72
73 ccl_device_inline ShaderClosure *svm_node_closure_get_bsdf(ShaderData *sd, float mix_weight)
74 {
75 #ifdef __MULTI_CLOSURE__
76         ShaderClosure *sc = &sd->closure[sd->num_closure];
77         float3 weight = sc->weight * mix_weight;
78         float sample_weight = fabsf(average(weight));
79
80         if(sample_weight > CLOSURE_WEIGHT_CUTOFF && sd->num_closure < MAX_CLOSURE) {
81                 sc->weight = weight;
82                 sc->sample_weight = sample_weight;
83                 sd->num_closure++;
84 #ifdef __OSL__
85                 sc->prim = NULL;
86 #endif
87                 return sc;
88         }
89
90         return NULL;
91 #else
92         return &sd->closure;
93 #endif
94 }
95
96 ccl_device_inline ShaderClosure *svm_node_closure_get_absorption(ShaderData *sd, float mix_weight)
97 {
98 #ifdef __MULTI_CLOSURE__
99         ShaderClosure *sc = &sd->closure[sd->num_closure];
100         float3 weight = (make_float3(1.0f, 1.0f, 1.0f) - sc->weight) * mix_weight;
101         float sample_weight = fabsf(average(weight));
102
103         if(sample_weight > CLOSURE_WEIGHT_CUTOFF && sd->num_closure < MAX_CLOSURE) {
104                 sc->weight = weight;
105                 sc->sample_weight = sample_weight;
106                 sd->num_closure++;
107 #ifdef __OSL__
108                 sc->prim = NULL;
109 #endif
110                 return sc;
111         }
112
113         return NULL;
114 #else
115         return &sd->closure;
116 #endif
117 }
118
119 ccl_device void svm_node_closure_bsdf(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, float randb, int path_flag, int *offset)
120 {
121         uint type, param1_offset, param2_offset;
122
123 #ifdef __MULTI_CLOSURE__
124         uint mix_weight_offset;
125         decode_node_uchar4(node.y, &type, &param1_offset, &param2_offset, &mix_weight_offset);
126         float mix_weight = (stack_valid(mix_weight_offset)? stack_load_float(stack, mix_weight_offset): 1.0f);
127
128         /* note we read this extra node before weight check, so offset is added */
129         uint4 data_node = read_node(kg, offset);
130
131         if(mix_weight == 0.0f)
132                 return;
133
134         float3 N = stack_valid(data_node.x)? stack_load_float3(stack, data_node.x): sd->N; 
135 #else
136         decode_node_uchar4(node.y, &type, &param1_offset, &param2_offset, NULL);
137         float mix_weight = 1.0f;
138
139         uint4 data_node = read_node(kg, offset);
140         float3 N = stack_valid(data_node.x)? stack_load_float3(stack, data_node.x): sd->N; 
141 #endif
142
143         float param1 = (stack_valid(param1_offset))? stack_load_float(stack, param1_offset): __uint_as_float(node.z);
144         float param2 = (stack_valid(param2_offset))? stack_load_float(stack, param2_offset): __uint_as_float(node.w);
145
146         switch(type) {
147                 case CLOSURE_BSDF_DIFFUSE_ID: {
148                         ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
149
150                         if(sc) {
151                                 sc->N = N;
152
153                                 float roughness = param1;
154
155                                 if(roughness == 0.0f) {
156                                         sc->data0 = 0.0f;
157                                         sc->data1 = 0.0f;
158                                         sd->flag |= bsdf_diffuse_setup(sc);
159                                 }
160                                 else {
161                                         sc->data0 = roughness;
162                                         sc->data1 = 0.0f;
163                                         sd->flag |= bsdf_oren_nayar_setup(sc);
164                                 }
165                         }
166                         break;
167                 }
168                 case CLOSURE_BSDF_TRANSLUCENT_ID: {
169                         ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
170
171                         if(sc) {
172                                 sc->data0 = 0.0f;
173                                 sc->data1 = 0.0f;
174                                 sc->N = N;
175                                 sd->flag |= bsdf_translucent_setup(sc);
176                         }
177                         break;
178                 }
179                 case CLOSURE_BSDF_TRANSPARENT_ID: {
180                         ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
181
182                         if(sc) {
183                                 sc->data0 = 0.0f;
184                                 sc->data1 = 0.0f;
185                                 sc->N = N;
186                                 sd->flag |= bsdf_transparent_setup(sc);
187                         }
188                         break;
189                 }
190                 case CLOSURE_BSDF_REFLECTION_ID:
191                 case CLOSURE_BSDF_MICROFACET_GGX_ID:
192                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ID: {
193 #ifdef __CAUSTICS_TRICKS__
194                         if(kernel_data.integrator.no_caustics && (path_flag & PATH_RAY_DIFFUSE))
195                                 break;
196 #endif
197                         ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
198
199                         if(sc) {
200                                 sc->N = N;
201                                 sc->data0 = param1;
202                                 sc->data1 = 0.0f;
203
204                                 /* setup bsdf */
205                                 if(type == CLOSURE_BSDF_REFLECTION_ID)
206                                         sd->flag |= bsdf_reflection_setup(sc);
207                                 else if(type == CLOSURE_BSDF_MICROFACET_BECKMANN_ID)
208                                         sd->flag |= bsdf_microfacet_beckmann_setup(sc);
209                                 else
210                                         sd->flag |= bsdf_microfacet_ggx_setup(sc);
211                         }
212
213                         break;
214                 }
215                 case CLOSURE_BSDF_REFRACTION_ID:
216                 case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
217                 case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID: {
218 #ifdef __CAUSTICS_TRICKS__
219                         if(kernel_data.integrator.no_caustics && (path_flag & PATH_RAY_DIFFUSE))
220                                 break;
221 #endif
222                         ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
223
224                         if(sc) {
225                                 sc->N = N;
226                                 sc->data0 = param1;
227
228                                 float eta = fmaxf(param2, 1.0f + 1e-5f);
229                                 sc->data1 = (sd->flag & SD_BACKFACING)? 1.0f/eta: eta;
230
231                                 /* setup bsdf */
232                                 if(type == CLOSURE_BSDF_REFRACTION_ID)
233                                         sd->flag |= bsdf_refraction_setup(sc);
234                                 else if(type == CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID)
235                                         sd->flag |= bsdf_microfacet_beckmann_refraction_setup(sc);
236                                 else
237                                         sd->flag |= bsdf_microfacet_ggx_refraction_setup(sc);
238                         }
239
240                         break;
241                 }
242                 case CLOSURE_BSDF_SHARP_GLASS_ID:
243                 case CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID:
244                 case CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID: {
245 #ifdef __CAUSTICS_TRICKS__
246                         if(kernel_data.integrator.no_caustics && (path_flag & PATH_RAY_DIFFUSE))
247                                 break;
248 #endif
249                         /* index of refraction */
250                         float eta = fmaxf(param2, 1.0f + 1e-5f);
251                         eta = (sd->flag & SD_BACKFACING)? 1.0f/eta: eta;
252
253                         /* fresnel */
254                         float cosNO = dot(N, sd->I);
255                         float fresnel = fresnel_dielectric_cos(cosNO, eta);
256                         float roughness = param1;
257
258 #ifdef __MULTI_CLOSURE__
259                         /* reflection */
260                         ShaderClosure *sc = &sd->closure[sd->num_closure];
261                         float3 weight = sc->weight;
262                         float sample_weight = sc->sample_weight;
263
264                         sc = svm_node_closure_get_bsdf(sd, mix_weight*fresnel);
265
266                         if(sc) {
267                                 sc->N = N;
268                                 svm_node_glass_setup(sd, sc, type, eta, roughness, false);
269                         }
270
271                         /* refraction */
272                         sc = &sd->closure[sd->num_closure];
273                         sc->weight = weight;
274                         sc->sample_weight = sample_weight;
275
276                         sc = svm_node_closure_get_bsdf(sd, mix_weight*(1.0f - fresnel));
277
278                         if(sc) {
279                                 sc->N = N;
280                                 svm_node_glass_setup(sd, sc, type, eta, roughness, true);
281                         }
282 #else
283                         ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
284
285                         if(sc) {
286                                 sc->N = N;
287                                 bool refract = (randb > fresnel);
288                                 svm_node_glass_setup(sd, sc, type, eta, roughness, refract);
289                         }
290 #endif
291
292                         break;
293                 }
294                 case CLOSURE_BSDF_WARD_ID: {
295 #ifdef __CAUSTICS_TRICKS__
296                         if(kernel_data.integrator.no_caustics && (path_flag & PATH_RAY_DIFFUSE))
297                                 break;
298 #endif
299                         ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
300
301                         if(sc) {
302                                 sc->N = N;
303
304 #ifdef __ANISOTROPIC__
305                                 sc->T = stack_load_float3(stack, data_node.y);
306
307                                 /* rotate tangent */
308                                 float rotation = stack_load_float(stack, data_node.z);
309
310                                 if(rotation != 0.0f)
311                                         sc->T = rotate_around_axis(sc->T, sc->N, rotation * M_2PI_F);
312
313                                 /* compute roughness */
314                                 float roughness = param1;
315                                 float anisotropy = clamp(param2, -0.99f, 0.99f);
316
317                                 if(anisotropy < 0.0f) {
318                                         sc->data0 = roughness/(1.0f + anisotropy);
319                                         sc->data1 = roughness*(1.0f + anisotropy);
320                                 }
321                                 else {
322                                         sc->data0 = roughness*(1.0f - anisotropy);
323                                         sc->data1 = roughness/(1.0f - anisotropy);
324                                 }
325
326                                 sd->flag |= bsdf_ward_setup(sc);
327 #else
328                                 sd->flag |= bsdf_diffuse_setup(sc);
329 #endif
330                         }
331                         break;
332                 }
333                 case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID: {
334                         ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
335
336                         if(sc) {
337                                 sc->N = N;
338
339                                 /* sigma */
340                                 sc->data0 = clamp(param1, 0.0f, 1.0f);
341                                 sc->data1 = 0.0f;
342                                 sd->flag |= bsdf_ashikhmin_velvet_setup(sc);
343                         }
344                         break;
345                 }
346                 case CLOSURE_BSDF_DIFFUSE_TOON_ID:
347                 case CLOSURE_BSDF_GLOSSY_TOON_ID: {
348                         ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
349
350                         if(sc) {
351                                 /* Normal, Size and Smooth */
352                                 sc->N = N;
353                                 sc->data0 = param1;
354                                 sc->data1 = param2;
355                                 
356                                 if (type == CLOSURE_BSDF_DIFFUSE_TOON_ID)
357                                         sd->flag |= bsdf_diffuse_toon_setup(sc);
358                                 else
359                                         sd->flag |= bsdf_glossy_toon_setup(sc);
360                         }
361                         break;
362                 }
363 #ifdef __HAIR__
364                 case CLOSURE_BSDF_HAIR_REFLECTION_ID:
365                 case CLOSURE_BSDF_HAIR_TRANSMISSION_ID: {
366                         
367                         if(sd->flag & SD_BACKFACING && sd->segment != ~0) {
368                                 ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
369                                 if(sc) {
370                                         sc->weight = make_float3(1.0f,1.0f,1.0f);
371                                         sc->N = N;
372                                         sd->flag |= bsdf_transparent_setup(sc);
373                                 }
374                         }
375                         else {
376                                 ShaderClosure *sc = &sd->closure[sd->num_closure];
377                                 sc = svm_node_closure_get_bsdf(sd, mix_weight);
378
379                                 if(sc) {
380                                         sc->N = N;
381                                         sc->data0 = param1;
382                                         sc->data1 = param2;
383                                         sc->offset = -stack_load_float(stack, data_node.z);
384                                         if(sd->segment == ~0) {
385                                                 sc->T = normalize(sd->dPdv);
386                                                 sc->offset = 0.0f;
387                                         }
388                                         else
389                                                 sc->T = sd->dPdu;
390                                         if(type == CLOSURE_BSDF_HAIR_REFLECTION_ID) {
391                                                 sd->flag |= bsdf_hair_reflection_setup(sc);
392                                         }
393                                         else {
394                                                 sd->flag |= bsdf_hair_transmission_setup(sc);
395                                         }
396                                 }
397                         }
398
399                         break;
400                 }
401 #endif
402
403 #ifdef __SUBSURFACE__
404                 case CLOSURE_BSSRDF_CUBIC_ID:
405                 case CLOSURE_BSSRDF_GAUSSIAN_ID: {
406                         ShaderClosure *sc = &sd->closure[sd->num_closure];
407                         float3 weight = sc->weight * mix_weight;
408                         float sample_weight = fabsf(average(weight));
409                         
410                         /* disable in case of diffuse ancestor, can't see it well then and
411                          * adds considerably noise due to probabilities of continuing path
412                          * getting lower and lower */
413                         if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR)
414                                 param1 = 0.0f;
415
416                         if(sample_weight > CLOSURE_WEIGHT_CUTOFF && sd->num_closure+2 < MAX_CLOSURE) {
417                                 /* radius * scale */
418                                 float3 radius = stack_load_float3(stack, data_node.z)*param1;
419                                 /* sharpness */
420                                 float sharpness = stack_load_float(stack, data_node.w);
421                                 /* texture color blur */
422                                 float texture_blur = param2;
423
424                                 /* create one closure per color channel */
425                                 if(fabsf(weight.x) > 0.0f) {
426                                         sc->weight = make_float3(weight.x, 0.0f, 0.0f);
427                                         sc->sample_weight = sample_weight;
428                                         sc->data0 = radius.x;
429                                         sc->data1 = texture_blur;
430                                         sc->T.x = sharpness;
431 #ifdef __OSL__
432                                         sc->prim = NULL;
433 #endif
434                                         sc->N = N;
435                                         sd->flag |= bssrdf_setup(sc, (ClosureType)type);
436
437                                         sd->num_closure++;
438                                         sc++;
439                                 }
440
441                                 if(fabsf(weight.y) > 0.0f) {
442                                         sc->weight = make_float3(0.0f, weight.y, 0.0f);
443                                         sc->sample_weight = sample_weight;
444                                         sc->data0 = radius.y;
445                                         sc->data1 = texture_blur;
446                                         sc->T.x = sharpness;
447 #ifdef __OSL__
448                                         sc->prim = NULL;
449 #endif
450                                         sc->N = N;
451                                         sd->flag |= bssrdf_setup(sc, (ClosureType)type);
452
453                                         sd->num_closure++;
454                                         sc++;
455                                 }
456
457                                 if(fabsf(weight.z) > 0.0f) {
458                                         sc->weight = make_float3(0.0f, 0.0f, weight.z);
459                                         sc->sample_weight = sample_weight;
460                                         sc->data0 = radius.z;
461                                         sc->data1 = texture_blur;
462                                         sc->T.x = sharpness;
463 #ifdef __OSL__
464                                         sc->prim = NULL;
465 #endif
466                                         sc->N = N;
467                                         sd->flag |= bssrdf_setup(sc, (ClosureType)type);
468
469                                         sd->num_closure++;
470                                         sc++;
471                                 }
472                         }
473
474                         break;
475                 }
476 #endif
477                 default:
478                         break;
479         }
480 }
481
482 ccl_device void svm_node_closure_volume(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int path_flag)
483 {
484         uint type, param1_offset, param2_offset;
485
486 #ifdef __MULTI_CLOSURE__
487         uint mix_weight_offset;
488         decode_node_uchar4(node.y, &type, &param1_offset, &param2_offset, &mix_weight_offset);
489         float mix_weight = (stack_valid(mix_weight_offset)? stack_load_float(stack, mix_weight_offset): 1.0f);
490
491         if(mix_weight == 0.0f)
492                 return;
493 #else
494         decode_node_uchar4(node.y, &type, &param1_offset, &param2_offset, NULL);
495         float mix_weight = 1.0f;
496 #endif
497
498         float param1 = (stack_valid(param1_offset))? stack_load_float(stack, param1_offset): __uint_as_float(node.z);
499         float param2 = (stack_valid(param2_offset))? stack_load_float(stack, param2_offset): __uint_as_float(node.w);
500         float density = param1;
501
502         switch(type) {
503                 case CLOSURE_VOLUME_ABSORPTION_ID: {
504                         ShaderClosure *sc = svm_node_closure_get_absorption(sd, mix_weight * density);
505
506                         if(sc) {
507                                 sd->flag |= volume_absorption_setup(sc);
508                         }
509                         break;
510                 }
511                 case CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID: {
512                         ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight * density);
513
514                         if(sc) {
515                                 float g = param2;
516                                 sc->data0 = g;
517                                 sd->flag |= volume_henyey_greenstein_setup(sc);
518                         }
519                         break;
520                 }
521                 default:
522                         break;
523         }
524 }
525
526 ccl_device void svm_node_closure_emission(ShaderData *sd, float *stack, uint4 node)
527 {
528 #ifdef __MULTI_CLOSURE__
529         uint mix_weight_offset = node.y;
530
531         if(stack_valid(mix_weight_offset)) {
532                 float mix_weight = stack_load_float(stack, mix_weight_offset);
533
534                 if(mix_weight == 0.0f)
535                         return;
536
537                 svm_node_closure_get_non_bsdf(sd, CLOSURE_EMISSION_ID, mix_weight);
538         }
539         else
540                 svm_node_closure_get_non_bsdf(sd, CLOSURE_EMISSION_ID, 1.0f);
541 #else
542         ShaderClosure *sc = &sd->closure;
543         sc->type = CLOSURE_EMISSION_ID;
544 #endif
545
546         sd->flag |= SD_EMISSION;
547 }
548
549 ccl_device void svm_node_closure_background(ShaderData *sd, float *stack, uint4 node)
550 {
551 #ifdef __MULTI_CLOSURE__
552         uint mix_weight_offset = node.y;
553
554         if(stack_valid(mix_weight_offset)) {
555                 float mix_weight = stack_load_float(stack, mix_weight_offset);
556
557                 if(mix_weight == 0.0f)
558                         return;
559
560                 svm_node_closure_get_non_bsdf(sd, CLOSURE_BACKGROUND_ID, mix_weight);
561         }
562         else
563                 svm_node_closure_get_non_bsdf(sd, CLOSURE_BACKGROUND_ID, 1.0f);
564 #else
565         ShaderClosure *sc = &sd->closure;
566         sc->type = CLOSURE_BACKGROUND_ID;
567 #endif
568 }
569
570 ccl_device void svm_node_closure_holdout(ShaderData *sd, float *stack, uint4 node)
571 {
572 #ifdef __MULTI_CLOSURE__
573         uint mix_weight_offset = node.y;
574
575         if(stack_valid(mix_weight_offset)) {
576                 float mix_weight = stack_load_float(stack, mix_weight_offset);
577
578                 if(mix_weight == 0.0f)
579                         return;
580
581                 svm_node_closure_get_non_bsdf(sd, CLOSURE_HOLDOUT_ID, mix_weight);
582         }
583         else
584                 svm_node_closure_get_non_bsdf(sd, CLOSURE_HOLDOUT_ID, 1.0f);
585 #else
586         ShaderClosure *sc = &sd->closure;
587         sc->type = CLOSURE_HOLDOUT_ID;
588 #endif
589
590         sd->flag |= SD_HOLDOUT;
591 }
592
593 ccl_device void svm_node_closure_ambient_occlusion(ShaderData *sd, float *stack, uint4 node)
594 {
595 #ifdef __MULTI_CLOSURE__
596         uint mix_weight_offset = node.y;
597
598         if(stack_valid(mix_weight_offset)) {
599                 float mix_weight = stack_load_float(stack, mix_weight_offset);
600
601                 if(mix_weight == 0.0f)
602                         return;
603
604                 svm_node_closure_get_non_bsdf(sd, CLOSURE_AMBIENT_OCCLUSION_ID, mix_weight);
605         }
606         else
607                 svm_node_closure_get_non_bsdf(sd, CLOSURE_AMBIENT_OCCLUSION_ID, 1.0f);
608 #else
609         ShaderClosure *sc = &sd->closure;
610         sc->type = CLOSURE_AMBIENT_OCCLUSION_ID;
611 #endif
612
613         sd->flag |= SD_AO;
614 }
615
616 /* Closure Nodes */
617
618 ccl_device_inline void svm_node_closure_store_weight(ShaderData *sd, float3 weight)
619 {
620 #ifdef __MULTI_CLOSURE__
621         if(sd->num_closure < MAX_CLOSURE)
622                 sd->closure[sd->num_closure].weight = weight;
623 #else
624         sd->closure.weight = weight;
625 #endif
626 }
627
628 ccl_device void svm_node_closure_set_weight(ShaderData *sd, uint r, uint g, uint b)
629 {
630         float3 weight = make_float3(__uint_as_float(r), __uint_as_float(g), __uint_as_float(b));
631         svm_node_closure_store_weight(sd, weight);
632 }
633
634 ccl_device void svm_node_emission_set_weight_total(KernelGlobals *kg, ShaderData *sd, uint r, uint g, uint b)
635 {
636         float3 weight = make_float3(__uint_as_float(r), __uint_as_float(g), __uint_as_float(b));
637
638         if(sd->object != ~0)
639                 weight /= object_surface_area(kg, sd->object);
640
641         svm_node_closure_store_weight(sd, weight);
642 }
643
644 ccl_device void svm_node_closure_weight(ShaderData *sd, float *stack, uint weight_offset)
645 {
646         float3 weight = stack_load_float3(stack, weight_offset);
647
648         svm_node_closure_store_weight(sd, weight);
649 }
650
651 ccl_device void svm_node_emission_weight(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
652 {
653         uint color_offset = node.y;
654         uint strength_offset = node.z;
655         uint total_power = node.w;
656
657         float strength = stack_load_float(stack, strength_offset);
658         float3 weight = stack_load_float3(stack, color_offset)*strength;
659
660         if(total_power && sd->object != ~0)
661                 weight /= object_surface_area(kg, sd->object);
662
663         svm_node_closure_store_weight(sd, weight);
664 }
665
666 ccl_device void svm_node_mix_closure(ShaderData *sd, float *stack,
667         uint4 node, int *offset, float *randb)
668 {
669 #ifdef __MULTI_CLOSURE__
670         /* fetch weight from blend input, previous mix closures,
671          * and write to stack to be used by closure nodes later */
672         uint weight_offset, in_weight_offset, weight1_offset, weight2_offset;
673         decode_node_uchar4(node.y, &weight_offset, &in_weight_offset, &weight1_offset, &weight2_offset);
674
675         float weight = stack_load_float(stack, weight_offset);
676         weight = clamp(weight, 0.0f, 1.0f);
677
678         float in_weight = (stack_valid(in_weight_offset))? stack_load_float(stack, in_weight_offset): 1.0f;
679
680         if(stack_valid(weight1_offset))
681                 stack_store_float(stack, weight1_offset, in_weight*(1.0f - weight));
682         if(stack_valid(weight2_offset))
683                 stack_store_float(stack, weight2_offset, in_weight*weight);
684 #else
685         /* pick a closure and make the random number uniform over 0..1 again.
686          * closure 1 starts on the next node, for closure 2 the start is at an
687          * offset from the current node, so we jump */
688         uint weight_offset = node.y;
689         uint node_jump = node.z;
690         float weight = stack_load_float(stack, weight_offset);
691         weight = clamp(weight, 0.0f, 1.0f);
692
693         if(*randb < weight) {
694                 *offset += node_jump;
695                 *randb = *randb/weight;
696         }
697         else
698                 *randb = (*randb - weight)/(1.0f - weight);
699 #endif
700 }
701
702 ccl_device void svm_node_add_closure(ShaderData *sd, float *stack, uint unused,
703         uint node_jump, int *offset, float *randb, float *closure_weight)
704 {
705 #ifdef __MULTI_CLOSURE__
706         /* nothing to do, handled in compiler */
707 #else
708         /* pick one of the two closures with probability 0.5. sampling quality
709          * is not going to be great, for that we'd need to evaluate the weights
710          * of the two closures being added */
711         float weight = 0.5f;
712
713         if(*randb < weight) {
714                 *offset += node_jump;
715                 *randb = *randb/weight;
716         }
717         else
718                 *randb = (*randb - weight)/(1.0f - weight);
719         
720         *closure_weight *= 2.0f;
721 #endif
722 }
723
724 /* (Bump) normal */
725
726 ccl_device void svm_node_set_normal(KernelGlobals *kg, ShaderData *sd, float *stack, uint in_direction, uint out_normal)
727 {
728         float3 normal = stack_load_float3(stack, in_direction);
729         sd->N = normal;
730         stack_store_float3(stack, out_normal, normal);
731 }
732
733 CCL_NAMESPACE_END
734