c337c19ced18473cae122351a532af3c10d2e64f
[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/background.h"
19 #include "render/integrator.h"
20 #include "render/film.h"
21 #include "render/light.h"
22 #include "render/scene.h"
23 #include "render/shader.h"
24 #include "render/sobol.h"
25
26 #include "util/util_foreach.h"
27 #include "util/util_hash.h"
28
29 CCL_NAMESPACE_BEGIN
30
31 NODE_DEFINE(Integrator)
32 {
33         NodeType *type = NodeType::add("integrator", create);
34
35         SOCKET_INT(max_bounce, "Max Bounce", 7);
36
37         SOCKET_INT(max_diffuse_bounce, "Max Diffuse Bounce", 7);
38         SOCKET_INT(max_glossy_bounce, "Max Glossy Bounce", 7);
39         SOCKET_INT(max_transmission_bounce, "Max Transmission Bounce", 7);
40         SOCKET_INT(max_volume_bounce, "Max Volume Bounce", 7);
41
42         SOCKET_INT(transparent_max_bounce, "Transparent Max Bounce", 7);
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         kintegrator->transparent_shadows = false;
125         foreach(Shader *shader, scene->shaders) {
126                 /* keep this in sync with SD_HAS_TRANSPARENT_SHADOW in shader.cpp */
127                 if((shader->has_surface_transparent && shader->use_transparent_shadow) || shader->has_volume) {
128                         kintegrator->transparent_shadows = true;
129                         break;
130                 }
131         }
132
133         kintegrator->volume_max_steps = volume_max_steps;
134         kintegrator->volume_step_size = volume_step_size;
135
136         kintegrator->caustics_reflective = caustics_reflective;
137         kintegrator->caustics_refractive = caustics_refractive;
138         kintegrator->filter_glossy = (filter_glossy == 0.0f)? FLT_MAX: 1.0f/filter_glossy;
139
140         kintegrator->seed = hash_int(seed);
141
142         kintegrator->use_ambient_occlusion =
143                 ((Pass::contains(scene->film->passes, PASS_AO)) || dscene->data.background.ao_factor != 0.0f);
144         
145         kintegrator->sample_clamp_direct = (sample_clamp_direct == 0.0f)? FLT_MAX: sample_clamp_direct*3.0f;
146         kintegrator->sample_clamp_indirect = (sample_clamp_indirect == 0.0f)? FLT_MAX: sample_clamp_indirect*3.0f;
147
148         kintegrator->branched = (method == BRANCHED_PATH);
149         kintegrator->volume_decoupled = device->info.has_volume_decoupled;
150         kintegrator->diffuse_samples = diffuse_samples;
151         kintegrator->glossy_samples = glossy_samples;
152         kintegrator->transmission_samples = transmission_samples;
153         kintegrator->ao_samples = ao_samples;
154         kintegrator->mesh_light_samples = mesh_light_samples;
155         kintegrator->subsurface_samples = subsurface_samples;
156         kintegrator->volume_samples = volume_samples;
157         kintegrator->start_sample = start_sample;
158
159         if(method == BRANCHED_PATH) {
160                 kintegrator->sample_all_lights_direct = sample_all_lights_direct;
161                 kintegrator->sample_all_lights_indirect = sample_all_lights_indirect;
162         }
163         else {
164                 kintegrator->sample_all_lights_direct = false;
165                 kintegrator->sample_all_lights_indirect = false;
166         }
167
168         kintegrator->sampling_pattern = sampling_pattern;
169         kintegrator->aa_samples = aa_samples;
170
171         if(light_sampling_threshold > 0.0f) {
172                 kintegrator->light_inv_rr_threshold = 1.0f / light_sampling_threshold;
173         }
174         else {
175                 kintegrator->light_inv_rr_threshold = 0.0f;
176         }
177
178         /* sobol directions table */
179         int max_samples = 1;
180
181         if(method == BRANCHED_PATH) {
182                 foreach(Light *light, scene->lights)
183                         max_samples = max(max_samples, light->samples);
184
185                 max_samples = max(max_samples, max(diffuse_samples, max(glossy_samples, transmission_samples)));
186                 max_samples = max(max_samples, max(ao_samples, max(mesh_light_samples, subsurface_samples)));
187                 max_samples = max(max_samples, volume_samples);
188         }
189
190         uint total_bounces = max_bounce + transparent_max_bounce + 3 +
191                              max(BSSRDF_MAX_HITS, BSSRDF_MAX_BOUNCES);
192
193         max_samples *= total_bounces;
194
195         int dimensions = PRNG_BASE_NUM + max_samples*PRNG_BOUNCE_NUM;
196         dimensions = min(dimensions, SOBOL_MAX_DIMENSIONS);
197
198         uint *directions = dscene->sobol_directions.alloc(SOBOL_BITS*dimensions);
199
200         sobol_generate_direction_vectors((uint(*)[SOBOL_BITS])directions, dimensions);
201
202         dscene->sobol_directions.copy_to_device();
203
204         /* Clamping. */
205         bool use_sample_clamp = (sample_clamp_direct != 0.0f ||
206                                  sample_clamp_indirect != 0.0f);
207         if(use_sample_clamp != scene->film->use_sample_clamp) {
208                 scene->film->use_sample_clamp = use_sample_clamp;
209                 scene->film->tag_update(scene);
210         }
211
212         need_update = false;
213 }
214
215 void Integrator::device_free(Device *, DeviceScene *dscene)
216 {
217         dscene->sobol_directions.free();
218 }
219
220 bool Integrator::modified(const Integrator& integrator)
221 {
222         return !Node::equals(integrator);
223 }
224
225 void Integrator::tag_update(Scene *scene)
226 {
227         foreach(Shader *shader, scene->shaders) {
228                 if(shader->has_integrator_dependency) {
229                         scene->shader_manager->need_update = true;
230                         break;
231                 }
232         }
233         need_update = true;
234 }
235
236 CCL_NAMESPACE_END
237