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