2313feac0894c72ee47b07abf1e6f4c25f754655
[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 ccl_device_inline bool kernel_split_branched_indirect_start_shared(KernelGlobals *kg, int ray_index)
67 {
68         ccl_global char *ray_state = kernel_split_state.ray_state;
69
70         int inactive_ray = dequeue_ray_index(QUEUE_INACTIVE_RAYS,
71                 kernel_split_state.queue_data, kernel_split_params.queue_size, kernel_split_params.queue_index);
72
73         if(!IS_STATE(ray_state, inactive_ray, RAY_INACTIVE)) {
74                 return false;
75         }
76
77 #define SPLIT_DATA_ENTRY(type, name, num) \
78                 kernel_split_state.name[inactive_ray] = kernel_split_state.name[ray_index];
79         SPLIT_DATA_ENTRIES_BRANCHED_SHARED
80 #undef SPLIT_DATA_ENTRY
81
82         kernel_split_state.branched_state[inactive_ray].shared_sample_count = 0;
83         kernel_split_state.branched_state[inactive_ray].original_ray = ray_index;
84         kernel_split_state.branched_state[inactive_ray].waiting_on_shared_samples = false;
85
86         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
87         PathRadiance *inactive_L = &kernel_split_state.path_radiance[inactive_ray];
88
89         path_radiance_init(inactive_L, kernel_data.film.use_light_pass);
90         path_radiance_copy_indirect(inactive_L, L);
91
92         ray_state[inactive_ray] = RAY_REGENERATED;
93         ADD_RAY_FLAG(ray_state, inactive_ray, RAY_BRANCHED_INDIRECT_SHARED);
94         ADD_RAY_FLAG(ray_state, inactive_ray, IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT));
95
96         atomic_fetch_and_inc_uint32((ccl_global uint*)&kernel_split_state.branched_state[ray_index].shared_sample_count);
97
98         return true;
99 }
100
101 /* bounce off surface and integrate indirect light */
102 ccl_device_noinline bool kernel_split_branched_path_surface_indirect_light_iter(KernelGlobals *kg,
103                                                                                 int ray_index,
104                                                                                 float num_samples_adjust,
105                                                                                 ShaderData *saved_sd,
106                                                                                 bool reset_path_state,
107                                                                                 bool wait_for_shared)
108 {
109         SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
110
111         ShaderData *sd = saved_sd;
112         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
113         float3 throughput = branched_state->throughput;
114         ccl_global PathState *ps = &kernel_split_state.path_state[ray_index];
115
116         float sum_sample_weight = 0.0f;
117 #ifdef __DENOISING_FEATURES__
118         if(ps->denoising_feature_weight > 0.0f) {
119                 for(int i = 0; i < sd->num_closure; i++) {
120                         const ShaderClosure *sc = &sd->closure[i];
121
122                         /* transparency is not handled here, but in outer loop */
123                         if(!CLOSURE_IS_BSDF(sc->type) || CLOSURE_IS_BSDF_TRANSPARENT(sc->type)) {
124                                 continue;
125                         }
126
127                         sum_sample_weight += sc->sample_weight;
128                 }
129         }
130         else {
131                 sum_sample_weight = 1.0f;
132         }
133 #endif  /* __DENOISING_FEATURES__ */
134
135         for(int i = branched_state->next_closure; i < sd->num_closure; i++) {
136                 const ShaderClosure *sc = &sd->closure[i];
137
138                 if(!CLOSURE_IS_BSDF(sc->type))
139                         continue;
140                 /* transparency is not handled here, but in outer loop */
141                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
142                         continue;
143
144                 int num_samples;
145
146                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
147                         num_samples = kernel_data.integrator.diffuse_samples;
148                 else if(CLOSURE_IS_BSDF_BSSRDF(sc->type))
149                         num_samples = 1;
150                 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
151                         num_samples = kernel_data.integrator.glossy_samples;
152                 else
153                         num_samples = kernel_data.integrator.transmission_samples;
154
155                 num_samples = ceil_to_int(num_samples_adjust*num_samples);
156
157                 float num_samples_inv = num_samples_adjust/num_samples;
158
159                 for(int j = branched_state->next_sample; j < num_samples; j++) {
160                         if(reset_path_state) {
161                                 *ps = branched_state->path_state;
162                         }
163
164                         ps->rng_hash = cmj_hash(branched_state->path_state.rng_hash, i);
165
166                         ccl_global float3 *tp = &kernel_split_state.throughput[ray_index];
167                         *tp = throughput;
168
169                         ccl_global Ray *bsdf_ray = &kernel_split_state.ray[ray_index];
170
171                         if(!kernel_branched_path_surface_bounce(kg,
172                                                                 sd,
173                                                                 sc,
174                                                                 j,
175                                                                 num_samples,
176                                                                 tp,
177                                                                 ps,
178                                                                 &L->state,
179                                                                 bsdf_ray,
180                                                                 sum_sample_weight))
181                         {
182                                 continue;
183                         }
184
185                         ps->rng_hash = branched_state->path_state.rng_hash;
186
187                         /* update state for next iteration */
188                         branched_state->next_closure = i;
189                         branched_state->next_sample = j+1;
190
191                         /* start the indirect path */
192                         *tp *= num_samples_inv;
193
194                         if(kernel_split_branched_indirect_start_shared(kg, ray_index)) {
195                                 continue;
196                         }
197
198                         return true;
199                 }
200
201                 branched_state->next_sample = 0;
202         }
203
204         branched_state->next_closure = sd->num_closure;
205
206         if(wait_for_shared) {
207                 branched_state->waiting_on_shared_samples = (branched_state->shared_sample_count > 0);
208                 if(branched_state->waiting_on_shared_samples) {
209                         return true;
210                 }
211         }
212
213         return false;
214 }
215
216 #endif  /* __BRANCHED_PATH__ */
217
218 CCL_NAMESPACE_END
219