Cycles: remove min bounces, modify RR to terminate less.
[blender-staging.git] / intern / cycles / render / integrator.cpp
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 #include "device/device.h"
18 #include "render/integrator.h"
19 #include "render/film.h"
20 #include "render/light.h"
21 #include "render/scene.h"
22 #include "render/shader.h"
23 #include "render/sobol.h"
24
25 #include "util/util_foreach.h"
26 #include "util/util_hash.h"
27
28 CCL_NAMESPACE_BEGIN
29
30 NODE_DEFINE(Integrator)
31 {
32         NodeType *type = NodeType::add("integrator", create);
33
34         SOCKET_INT(max_bounce, "Max Bounce", 7);
35
36         SOCKET_INT(max_diffuse_bounce, "Max Diffuse Bounce", 7);
37         SOCKET_INT(max_glossy_bounce, "Max Glossy Bounce", 7);
38         SOCKET_INT(max_transmission_bounce, "Max Transmission Bounce", 7);
39         SOCKET_INT(max_volume_bounce, "Max Volume Bounce", 7);
40
41         SOCKET_INT(transparent_max_bounce, "Transparent Max Bounce", 7);
42         SOCKET_BOOLEAN(transparent_shadows, "Transparent Shadows", false);
43
44         SOCKET_INT(ao_bounces, "AO Bounces", 0);
45
46         SOCKET_INT(volume_max_steps, "Volume Max Steps", 1024);
47         SOCKET_FLOAT(volume_step_size, "Volume Step Size", 0.1f);
48
49         SOCKET_BOOLEAN(caustics_reflective, "Reflective Caustics", true);
50         SOCKET_BOOLEAN(caustics_refractive, "Refractive Caustics", true);
51         SOCKET_FLOAT(filter_glossy, "Filter Glossy", 0.0f);
52         SOCKET_INT(seed, "Seed", 0);
53         SOCKET_FLOAT(sample_clamp_direct, "Sample Clamp Direct", 0.0f);
54         SOCKET_FLOAT(sample_clamp_indirect, "Sample Clamp Indirect", 0.0f);
55         SOCKET_BOOLEAN(motion_blur, "Motion Blur", false);
56
57         SOCKET_INT(aa_samples, "AA Samples", 0);
58         SOCKET_INT(diffuse_samples, "Diffuse Samples", 1);
59         SOCKET_INT(glossy_samples, "Glossy Samples", 1);
60         SOCKET_INT(transmission_samples, "Transmission Samples", 1);
61         SOCKET_INT(ao_samples, "AO Samples", 1);
62         SOCKET_INT(mesh_light_samples, "Mesh Light Samples", 1);
63         SOCKET_INT(subsurface_samples, "Subsurface Samples", 1);
64         SOCKET_INT(volume_samples, "Volume Samples", 1);
65         SOCKET_INT(start_sample, "Start Sample", 0);
66
67         SOCKET_BOOLEAN(sample_all_lights_direct, "Sample All Lights Direct", true);
68         SOCKET_BOOLEAN(sample_all_lights_indirect, "Sample All Lights Indirect", true);
69         SOCKET_FLOAT(light_sampling_threshold, "Light Sampling Threshold", 0.05f);
70
71         static NodeEnum method_enum;
72         method_enum.insert("path", PATH);
73         method_enum.insert("branched_path", BRANCHED_PATH);
74         SOCKET_ENUM(method, "Method", method_enum, PATH);
75
76         static NodeEnum sampling_pattern_enum;
77         sampling_pattern_enum.insert("sobol", SAMPLING_PATTERN_SOBOL);
78         sampling_pattern_enum.insert("cmj", SAMPLING_PATTERN_CMJ);
79         SOCKET_ENUM(sampling_pattern, "Sampling Pattern", sampling_pattern_enum, SAMPLING_PATTERN_SOBOL);
80
81         return type;
82 }
83
84 Integrator::Integrator()
85 : Node(node_type)
86 {
87         need_update = true;
88 }
89
90 Integrator::~Integrator()
91 {
92 }
93
94 void Integrator::device_update(Device *device, DeviceScene *dscene, Scene *scene)
95 {
96         if(!need_update)
97                 return;
98
99         device_free(device, dscene);
100
101         KernelIntegrator *kintegrator = &dscene->data.integrator;
102
103         /* integrator parameters */
104         kintegrator->max_bounce = max_bounce + 1;
105
106         kintegrator->max_diffuse_bounce = max_diffuse_bounce + 1;
107         kintegrator->max_glossy_bounce = max_glossy_bounce + 1;
108         kintegrator->max_transmission_bounce = max_transmission_bounce + 1;
109         kintegrator->max_volume_bounce = max_volume_bounce + 1;
110
111         kintegrator->transparent_max_bounce = transparent_max_bounce + 1;
112
113         if(ao_bounces == 0) {
114                 kintegrator->ao_bounces = INT_MAX;
115         }
116         else {
117                 kintegrator->ao_bounces = ao_bounces - 1;
118         }
119
120         /* Transparent Shadows
121          * We only need to enable transparent shadows, if we actually have 
122          * transparent shaders in the scene. Otherwise we can disable it
123          * to improve performance a bit. */
124         if(transparent_shadows) {
125                 kintegrator->transparent_shadows = false;
126                 foreach(Shader *shader, scene->shaders) {
127                         /* keep this in sync with SD_HAS_TRANSPARENT_SHADOW in shader.cpp */
128                         if((shader->has_surface_transparent && shader->use_transparent_shadow) || shader->has_volume) {
129                                 kintegrator->transparent_shadows = true;
130                                 break;
131                         }
132                 }
133         }
134         else {
135                 kintegrator->transparent_shadows = false;
136         }
137
138         kintegrator->volume_max_steps = volume_max_steps;
139         kintegrator->volume_step_size = volume_step_size;
140
141         kintegrator->caustics_reflective = caustics_reflective;
142         kintegrator->caustics_refractive = caustics_refractive;
143         kintegrator->filter_glossy = (filter_glossy == 0.0f)? FLT_MAX: 1.0f/filter_glossy;
144
145         kintegrator->seed = hash_int(seed);
146
147         kintegrator->use_ambient_occlusion =
148                 ((Pass::contains(scene->film->passes, PASS_AO)) || dscene->data.background.ao_factor != 0.0f);
149         
150         kintegrator->sample_clamp_direct = (sample_clamp_direct == 0.0f)? FLT_MAX: sample_clamp_direct*3.0f;
151         kintegrator->sample_clamp_indirect = (sample_clamp_indirect == 0.0f)? FLT_MAX: sample_clamp_indirect*3.0f;
152
153         kintegrator->branched = (method == BRANCHED_PATH);
154         kintegrator->diffuse_samples = diffuse_samples;
155         kintegrator->glossy_samples = glossy_samples;
156         kintegrator->transmission_samples = transmission_samples;
157         kintegrator->ao_samples = ao_samples;
158         kintegrator->mesh_light_samples = mesh_light_samples;
159         kintegrator->subsurface_samples = subsurface_samples;
160         kintegrator->volume_samples = volume_samples;
161         kintegrator->start_sample = start_sample;
162
163         if(method == BRANCHED_PATH) {
164                 kintegrator->sample_all_lights_direct = sample_all_lights_direct;
165                 kintegrator->sample_all_lights_indirect = sample_all_lights_indirect;
166         }
167         else {
168                 kintegrator->sample_all_lights_direct = false;
169                 kintegrator->sample_all_lights_indirect = false;
170         }
171
172         kintegrator->sampling_pattern = sampling_pattern;
173         kintegrator->aa_samples = aa_samples;
174
175         if(light_sampling_threshold > 0.0f) {
176                 kintegrator->light_inv_rr_threshold = 1.0f / light_sampling_threshold;
177         }
178         else {
179                 kintegrator->light_inv_rr_threshold = 0.0f;
180         }
181
182         /* sobol directions table */
183         int max_samples = 1;
184
185         if(method == BRANCHED_PATH) {
186                 foreach(Light *light, scene->lights)
187                         max_samples = max(max_samples, light->samples);
188
189                 max_samples = max(max_samples, max(diffuse_samples, max(glossy_samples, transmission_samples)));
190                 max_samples = max(max_samples, max(ao_samples, max(mesh_light_samples, subsurface_samples)));
191                 max_samples = max(max_samples, volume_samples);
192         }
193
194         max_samples *= (max_bounce + transparent_max_bounce + 3 + BSSRDF_MAX_HITS);
195
196         int dimensions = PRNG_BASE_NUM + max_samples*PRNG_BOUNCE_NUM;
197         dimensions = min(dimensions, SOBOL_MAX_DIMENSIONS);
198
199         uint *directions = dscene->sobol_directions.resize(SOBOL_BITS*dimensions);
200
201         sobol_generate_direction_vectors((uint(*)[SOBOL_BITS])directions, dimensions);
202
203         device->tex_alloc("__sobol_directions", dscene->sobol_directions);
204
205         /* Clamping. */
206         bool use_sample_clamp = (sample_clamp_direct != 0.0f ||
207                                  sample_clamp_indirect != 0.0f);
208         if(use_sample_clamp != scene->film->use_sample_clamp) {
209                 scene->film->use_sample_clamp = use_sample_clamp;
210                 scene->film->tag_update(scene);
211         }
212
213         need_update = false;
214 }
215
216 void Integrator::device_free(Device *device, DeviceScene *dscene)
217 {
218         device->tex_free(dscene->sobol_directions);
219         dscene->sobol_directions.clear();
220 }
221
222 bool Integrator::modified(const Integrator& integrator)
223 {
224         return !Node::equals(integrator);
225 }
226
227 void Integrator::tag_update(Scene *scene)
228 {
229         foreach(Shader *shader, scene->shaders) {
230                 if(shader->has_integrator_dependency) {
231                         scene->shader_manager->need_update = true;
232                         break;
233                 }
234         }
235         need_update = true;
236 }
237
238 CCL_NAMESPACE_END
239