a136e9ad76b4a3b56fd4795529ea269c82315830
[blender.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.h"
18 #include "integrator.h"
19 #include "film.h"
20 #include "light.h"
21 #include "scene.h"
22 #include "shader.h"
23 #include "sobol.h"
24
25 #include "util_foreach.h"
26 #include "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(min_bounce, "Min Bounce", 2);
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_min_bounce, "Transparent Min Bounce", 2);
43         SOCKET_INT(transparent_max_bounce, "Transparent Max Bounce", 7);
44         SOCKET_BOOLEAN(transparent_shadows, "Transparent Shadows", false);
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_INT(layer_flag, "Layer Flag", ~0);
54         SOCKET_FLOAT(sample_clamp_direct, "Sample Clamp Direct", 0.0f);
55         SOCKET_FLOAT(sample_clamp_indirect, "Sample Clamp Indirect", 0.0f);
56         SOCKET_BOOLEAN(motion_blur, "Motion Blur", false);
57
58         SOCKET_INT(aa_samples, "AA Samples", 0);
59         SOCKET_INT(diffuse_samples, "Diffuse Samples", 1);
60         SOCKET_INT(glossy_samples, "Glossy Samples", 1);
61         SOCKET_INT(transmission_samples, "Transmission Samples", 1);
62         SOCKET_INT(ao_samples, "AO Samples", 1);
63         SOCKET_INT(mesh_light_samples, "Mesh Light Samples", 1);
64         SOCKET_INT(subsurface_samples, "Subsurface Samples", 1);
65         SOCKET_INT(volume_samples, "Volume Samples", 1);
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
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         kintegrator->min_bounce = min_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         kintegrator->transparent_min_bounce = transparent_min_bounce + 1;
113
114         /* Transparent Shadows
115          * We only need to enable transparent shadows, if we actually have 
116          * transparent shaders in the scene. Otherwise we can disable it
117          * to improve performance a bit. */
118         if(transparent_shadows) {
119                 kintegrator->transparent_shadows = false;
120                 foreach(Shader *shader, scene->shaders) {
121                         /* keep this in sync with SD_HAS_TRANSPARENT_SHADOW in shader.cpp */
122                         if((shader->has_surface_transparent && shader->use_transparent_shadow) || shader->has_volume) {
123                                 kintegrator->transparent_shadows = true;
124                                 break;
125                         }
126                 }
127         }
128         else {
129                 kintegrator->transparent_shadows = false;
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         kintegrator->layer_flag = layer_flag << PATH_RAY_LAYER_SHIFT;
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->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
157         if(method == BRANCHED_PATH) {
158                 kintegrator->sample_all_lights_direct = sample_all_lights_direct;
159                 kintegrator->sample_all_lights_indirect = sample_all_lights_indirect;
160         }
161         else {
162                 kintegrator->sample_all_lights_direct = false;
163                 kintegrator->sample_all_lights_indirect = false;
164         }
165
166         kintegrator->sampling_pattern = sampling_pattern;
167         kintegrator->aa_samples = aa_samples;
168
169         /* sobol directions table */
170         int max_samples = 1;
171
172         if(method == BRANCHED_PATH) {
173                 foreach(Light *light, scene->lights)
174                         max_samples = max(max_samples, light->samples);
175
176                 max_samples = max(max_samples, max(diffuse_samples, max(glossy_samples, transmission_samples)));
177                 max_samples = max(max_samples, max(ao_samples, max(mesh_light_samples, subsurface_samples)));
178                 max_samples = max(max_samples, volume_samples);
179         }
180
181         max_samples *= (max_bounce + transparent_max_bounce + 3);
182
183         int dimensions = PRNG_BASE_NUM + max_samples*PRNG_BOUNCE_NUM;
184         dimensions = min(dimensions, SOBOL_MAX_DIMENSIONS);
185
186         uint *directions = dscene->sobol_directions.resize(SOBOL_BITS*dimensions);
187
188         sobol_generate_direction_vectors((uint(*)[SOBOL_BITS])directions, dimensions);
189
190         device->tex_alloc("__sobol_directions", dscene->sobol_directions);
191
192         /* Clamping. */
193         bool use_sample_clamp = (sample_clamp_direct != 0.0f ||
194                                  sample_clamp_indirect != 0.0f);
195         if(use_sample_clamp != scene->film->use_sample_clamp) {
196                 scene->film->use_sample_clamp = use_sample_clamp;
197                 scene->film->tag_update(scene);
198         }
199
200         need_update = false;
201 }
202
203 void Integrator::device_free(Device *device, DeviceScene *dscene)
204 {
205         device->tex_free(dscene->sobol_directions);
206         dscene->sobol_directions.clear();
207 }
208
209 void Integrator::tag_update(Scene *scene)
210 {
211         foreach(Shader *shader, scene->shaders) {
212                 if(shader->has_integrator_dependency) {
213                         scene->shader_manager->need_update = true;
214                         break;
215                 }
216         }
217         need_update = true;
218 }
219
220 CCL_NAMESPACE_END
221