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