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