Cycles: Implement denoising option for reducing noise in the rendered image
[blender.git] / intern / cycles / kernel / split / kernel_branched.h
1 /*
2  * Copyright 2011-2017 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 #ifdef __BRANCHED_PATH__
20
21 /* sets up the various state needed to do an indirect loop */
22 ccl_device_inline void kernel_split_branched_path_indirect_loop_init(KernelGlobals *kg, int ray_index)
23 {
24         SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
25
26         /* save a copy of the state to restore later */
27 #define BRANCHED_STORE(name) \
28                 branched_state->name = kernel_split_state.name[ray_index];
29
30         BRANCHED_STORE(path_state);
31         BRANCHED_STORE(throughput);
32         BRANCHED_STORE(ray);
33         BRANCHED_STORE(sd);
34         BRANCHED_STORE(isect);
35         BRANCHED_STORE(ray_state);
36
37 #undef BRANCHED_STORE
38
39         /* set loop counters to intial position */
40         branched_state->next_closure = 0;
41         branched_state->next_sample = 0;
42 }
43
44 /* ends an indirect loop and restores the previous state */
45 ccl_device_inline void kernel_split_branched_path_indirect_loop_end(KernelGlobals *kg, int ray_index)
46 {
47         SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
48
49         /* restore state */
50 #define BRANCHED_RESTORE(name) \
51                 kernel_split_state.name[ray_index] = branched_state->name;
52
53         BRANCHED_RESTORE(path_state);
54         BRANCHED_RESTORE(throughput);
55         BRANCHED_RESTORE(ray);
56         BRANCHED_RESTORE(sd);
57         BRANCHED_RESTORE(isect);
58         BRANCHED_RESTORE(ray_state);
59
60 #undef BRANCHED_RESTORE
61
62         /* leave indirect loop */
63         REMOVE_RAY_FLAG(kernel_split_state.ray_state, ray_index, RAY_BRANCHED_INDIRECT);
64 }
65
66 /* bounce off surface and integrate indirect light */
67 ccl_device_noinline bool kernel_split_branched_path_surface_indirect_light_iter(KernelGlobals *kg,
68                                                                                 int ray_index,
69                                                                                 float num_samples_adjust,
70                                                                                 ShaderData *saved_sd,
71                                                                                 bool reset_path_state)
72 {
73         SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
74
75         ShaderData *sd = saved_sd;
76         RNG rng = kernel_split_state.rng[ray_index];
77         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
78         float3 throughput = branched_state->throughput;
79         ccl_global PathState *ps = &kernel_split_state.path_state[ray_index];
80
81         float sum_sample_weight = 0.0f;
82 #ifdef __DENOISING_FEATURES__
83         if(ps->denoising_feature_weight > 0.0f) {
84                 for(int i = 0; i < sd->num_closure; i++) {
85                         const ShaderClosure *sc = &sd->closure[i];
86
87                         /* transparency is not handled here, but in outer loop */
88                         if(!CLOSURE_IS_BSDF(sc->type) || CLOSURE_IS_BSDF_TRANSPARENT(sc->type)) {
89                                 continue;
90                         }
91
92                         sum_sample_weight += sc->sample_weight;
93                 }
94         }
95         else {
96                 sum_sample_weight = 1.0f;
97         }
98 #endif  /* __DENOISING_FEATURES__ */
99
100         for(int i = branched_state->next_closure; i < sd->num_closure; i++) {
101                 const ShaderClosure *sc = &sd->closure[i];
102
103                 if(!CLOSURE_IS_BSDF(sc->type))
104                         continue;
105                 /* transparency is not handled here, but in outer loop */
106                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
107                         continue;
108
109                 int num_samples;
110
111                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
112                         num_samples = kernel_data.integrator.diffuse_samples;
113                 else if(CLOSURE_IS_BSDF_BSSRDF(sc->type))
114                         num_samples = 1;
115                 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
116                         num_samples = kernel_data.integrator.glossy_samples;
117                 else
118                         num_samples = kernel_data.integrator.transmission_samples;
119
120                 num_samples = ceil_to_int(num_samples_adjust*num_samples);
121
122                 float num_samples_inv = num_samples_adjust/num_samples;
123                 RNG bsdf_rng = cmj_hash(rng, i);
124
125                 for(int j = branched_state->next_sample; j < num_samples; j++) {
126                         if(reset_path_state) {
127                                 *ps = branched_state->path_state;
128                         }
129
130                         ccl_global float3 *tp = &kernel_split_state.throughput[ray_index];
131                         *tp = throughput;
132
133                         ccl_global Ray *bsdf_ray = &kernel_split_state.ray[ray_index];
134
135                         if(!kernel_branched_path_surface_bounce(kg,
136                                                                 &bsdf_rng,
137                                                                 sd,
138                                                                 sc,
139                                                                 j,
140                                                                 num_samples,
141                                                                 tp,
142                                                                 ps,
143                                                                 L,
144                                                                 bsdf_ray,
145                                                                 sum_sample_weight))
146                         {
147                                 continue;
148                         }
149
150                         /* update state for next iteration */
151                         branched_state->next_closure = i;
152                         branched_state->next_sample = j+1;
153                         branched_state->num_samples = num_samples;
154
155                         /* start the indirect path */
156                         *tp *= num_samples_inv;
157
158                         return true;
159                 }
160
161                 branched_state->next_sample = 0;
162         }
163
164         return false;
165 }
166
167 #endif  /* __BRANCHED_PATH__ */
168
169 CCL_NAMESPACE_END
170