Cleanup: remove Cycles layer bits checking in the kernel.
[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_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
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
69         static NodeEnum method_enum;
70         method_enum.insert("path", PATH);
71         method_enum.insert("branched_path", BRANCHED_PATH);
72         SOCKET_ENUM(method, "Method", method_enum, PATH);
73
74         static NodeEnum sampling_pattern_enum;
75         sampling_pattern_enum.insert("sobol", SAMPLING_PATTERN_SOBOL);
76         sampling_pattern_enum.insert("cmj", SAMPLING_PATTERN_CMJ);
77         SOCKET_ENUM(sampling_pattern, "Sampling Pattern", sampling_pattern_enum, SAMPLING_PATTERN_SOBOL);
78
79         return type;
80 }
81
82 Integrator::Integrator()
83 : Node(node_type)
84 {
85         need_update = true;
86 }
87
88 Integrator::~Integrator()
89 {
90 }
91
92 void Integrator::device_update(Device *device, DeviceScene *dscene, Scene *scene)
93 {
94         if(!need_update)
95                 return;
96
97         device_free(device, dscene);
98
99         KernelIntegrator *kintegrator = &dscene->data.integrator;
100
101         /* integrator parameters */
102         kintegrator->max_bounce = max_bounce + 1;
103         kintegrator->min_bounce = min_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         kintegrator->transparent_min_bounce = transparent_min_bounce + 1;
112
113         /* Transparent Shadows
114          * We only need to enable transparent shadows, if we actually have 
115          * transparent shaders in the scene. Otherwise we can disable it
116          * to improve performance a bit. */
117         if(transparent_shadows) {
118                 kintegrator->transparent_shadows = false;
119                 foreach(Shader *shader, scene->shaders) {
120                         /* keep this in sync with SD_HAS_TRANSPARENT_SHADOW in shader.cpp */
121                         if((shader->has_surface_transparent && shader->use_transparent_shadow) || shader->has_volume) {
122                                 kintegrator->transparent_shadows = true;
123                                 break;
124                         }
125                 }
126         }
127         else {
128                 kintegrator->transparent_shadows = false;
129         }
130
131         kintegrator->volume_max_steps = volume_max_steps;
132         kintegrator->volume_step_size = volume_step_size;
133
134         kintegrator->caustics_reflective = caustics_reflective;
135         kintegrator->caustics_refractive = caustics_refractive;
136         kintegrator->filter_glossy = (filter_glossy == 0.0f)? FLT_MAX: 1.0f/filter_glossy;
137
138         kintegrator->seed = hash_int(seed);
139
140         kintegrator->use_ambient_occlusion =
141                 ((Pass::contains(scene->film->passes, PASS_AO)) || dscene->data.background.ao_factor != 0.0f);
142         
143         kintegrator->sample_clamp_direct = (sample_clamp_direct == 0.0f)? FLT_MAX: sample_clamp_direct*3.0f;
144         kintegrator->sample_clamp_indirect = (sample_clamp_indirect == 0.0f)? FLT_MAX: sample_clamp_indirect*3.0f;
145
146         kintegrator->branched = (method == BRANCHED_PATH);
147         kintegrator->diffuse_samples = diffuse_samples;
148         kintegrator->glossy_samples = glossy_samples;
149         kintegrator->transmission_samples = transmission_samples;
150         kintegrator->ao_samples = ao_samples;
151         kintegrator->mesh_light_samples = mesh_light_samples;
152         kintegrator->subsurface_samples = subsurface_samples;
153         kintegrator->volume_samples = volume_samples;
154
155         if(method == BRANCHED_PATH) {
156                 kintegrator->sample_all_lights_direct = sample_all_lights_direct;
157                 kintegrator->sample_all_lights_indirect = sample_all_lights_indirect;
158         }
159         else {
160                 kintegrator->sample_all_lights_direct = false;
161                 kintegrator->sample_all_lights_indirect = false;
162         }
163
164         kintegrator->sampling_pattern = sampling_pattern;
165         kintegrator->aa_samples = aa_samples;
166
167         /* sobol directions table */
168         int max_samples = 1;
169
170         if(method == BRANCHED_PATH) {
171                 foreach(Light *light, scene->lights)
172                         max_samples = max(max_samples, light->samples);
173
174                 max_samples = max(max_samples, max(diffuse_samples, max(glossy_samples, transmission_samples)));
175                 max_samples = max(max_samples, max(ao_samples, max(mesh_light_samples, subsurface_samples)));
176                 max_samples = max(max_samples, volume_samples);
177         }
178
179         max_samples *= (max_bounce + transparent_max_bounce + 3);
180
181         int dimensions = PRNG_BASE_NUM + max_samples*PRNG_BOUNCE_NUM;
182         dimensions = min(dimensions, SOBOL_MAX_DIMENSIONS);
183
184         uint *directions = dscene->sobol_directions.resize(SOBOL_BITS*dimensions);
185
186         sobol_generate_direction_vectors((uint(*)[SOBOL_BITS])directions, dimensions);
187
188         device->tex_alloc("__sobol_directions", dscene->sobol_directions);
189
190         /* Clamping. */
191         bool use_sample_clamp = (sample_clamp_direct != 0.0f ||
192                                  sample_clamp_indirect != 0.0f);
193         if(use_sample_clamp != scene->film->use_sample_clamp) {
194                 scene->film->use_sample_clamp = use_sample_clamp;
195                 scene->film->tag_update(scene);
196         }
197
198         need_update = false;
199 }
200
201 void Integrator::device_free(Device *device, DeviceScene *dscene)
202 {
203         device->tex_free(dscene->sobol_directions);
204         dscene->sobol_directions.clear();
205 }
206
207 void Integrator::tag_update(Scene *scene)
208 {
209         foreach(Shader *shader, scene->shaders) {
210                 if(shader->has_integrator_dependency) {
211                         scene->shader_manager->need_update = true;
212                         break;
213                 }
214         }
215         need_update = true;
216 }
217
218 CCL_NAMESPACE_END
219