849157d9e91ed2b348f505ec66686b7534255a34
[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.h"
18 #include "integrator.h"
19 #include "light.h"
20 #include "scene.h"
21 #include "sobol.h"
22
23 #include "util_foreach.h"
24 #include "util_hash.h"
25
26 CCL_NAMESPACE_BEGIN
27
28 Integrator::Integrator()
29 {
30         min_bounce = 2;
31         max_bounce = 7;
32
33         max_diffuse_bounce = max_bounce;
34         max_glossy_bounce = max_bounce;
35         max_transmission_bounce = max_bounce;
36         max_volume_bounce = max_bounce;
37         probalistic_termination = true;
38
39         transparent_min_bounce = min_bounce;
40         transparent_max_bounce = max_bounce;
41         transparent_probalistic = true;
42         transparent_shadows = false;
43
44         volume_homogeneous_sampling = 0;
45         volume_max_steps = 1024;
46         volume_step_size = 0.1;
47
48         no_caustics = false;
49         filter_glossy = 0.0f;
50         seed = 0;
51         layer_flag = ~0;
52         sample_clamp_direct = 0.0f;
53         sample_clamp_indirect = 0.0f;
54         motion_blur = false;
55
56         aa_samples = 0;
57         diffuse_samples = 1;
58         glossy_samples = 1;
59         transmission_samples = 1;
60         ao_samples = 1;
61         mesh_light_samples = 1;
62         subsurface_samples = 1;
63         volume_samples = 1;
64         method = PATH;
65
66         sampling_pattern = SAMPLING_PATTERN_SOBOL;
67
68         need_update = true;
69 }
70
71 Integrator::~Integrator()
72 {
73 }
74
75 void Integrator::device_update(Device *device, DeviceScene *dscene, Scene *scene)
76 {
77         if(!need_update)
78                 return;
79
80         device_free(device, dscene);
81
82         KernelIntegrator *kintegrator = &dscene->data.integrator;
83
84         /* integrator parameters */
85         kintegrator->max_bounce = max_bounce + 1;
86         if(probalistic_termination)
87                 kintegrator->min_bounce = min_bounce + 1;
88         else
89                 kintegrator->min_bounce = kintegrator->max_bounce;
90
91         kintegrator->max_diffuse_bounce = max_diffuse_bounce + 1;
92         kintegrator->max_glossy_bounce = max_glossy_bounce + 1;
93         kintegrator->max_transmission_bounce = max_transmission_bounce + 1;
94
95         if(kintegrator->use_volumes)
96                 kintegrator->max_volume_bounce = max_volume_bounce + 1;
97         else
98                 kintegrator->max_volume_bounce = 1;
99
100         kintegrator->transparent_max_bounce = transparent_max_bounce + 1;
101         if(transparent_probalistic)
102                 kintegrator->transparent_min_bounce = transparent_min_bounce + 1;
103         else
104                 kintegrator->transparent_min_bounce = kintegrator->transparent_max_bounce;
105
106         kintegrator->transparent_shadows = transparent_shadows;
107
108         kintegrator->volume_homogeneous_sampling = volume_homogeneous_sampling;
109         kintegrator->volume_max_steps = volume_max_steps;
110         kintegrator->volume_step_size = volume_step_size;
111
112         kintegrator->no_caustics = no_caustics;
113         kintegrator->filter_glossy = (filter_glossy == 0.0f)? FLT_MAX: 1.0f/filter_glossy;
114
115         kintegrator->seed = hash_int(seed);
116         kintegrator->layer_flag = layer_flag << PATH_RAY_LAYER_SHIFT;
117
118         kintegrator->use_ambient_occlusion =
119                 ((dscene->data.film.pass_flag & PASS_AO) || dscene->data.background.ao_factor != 0.0f);
120         
121         kintegrator->sample_clamp_direct = (sample_clamp_direct == 0.0f)? FLT_MAX: sample_clamp_direct*3.0f;
122         kintegrator->sample_clamp_indirect = (sample_clamp_indirect == 0.0f)? FLT_MAX: sample_clamp_indirect*3.0f;
123
124         kintegrator->branched = (method == BRANCHED_PATH);
125         kintegrator->aa_samples = aa_samples;
126         kintegrator->diffuse_samples = diffuse_samples;
127         kintegrator->glossy_samples = glossy_samples;
128         kintegrator->transmission_samples = transmission_samples;
129         kintegrator->ao_samples = ao_samples;
130         kintegrator->mesh_light_samples = mesh_light_samples;
131         kintegrator->subsurface_samples = subsurface_samples;
132         kintegrator->volume_samples = volume_samples;
133         kintegrator->sample_all_lights_indirect = sample_all_lights_indirect;
134
135         kintegrator->sampling_pattern = sampling_pattern;
136
137         /* sobol directions table */
138         int max_samples = 1;
139
140         if(method == BRANCHED_PATH) {
141                 foreach(Light *light, scene->lights)
142                         max_samples = max(max_samples, light->samples);
143
144                 max_samples = max(max_samples, max(diffuse_samples, max(glossy_samples, transmission_samples)));
145                 max_samples = max(max_samples, max(ao_samples, max(mesh_light_samples, subsurface_samples)));
146                 max_samples = max(max_samples, volume_samples);
147         }
148
149         max_samples *= (max_bounce + transparent_max_bounce + 3);
150
151         int dimensions = PRNG_BASE_NUM + max_samples*PRNG_BOUNCE_NUM;
152         dimensions = min(dimensions, SOBOL_MAX_DIMENSIONS);
153
154         uint *directions = dscene->sobol_directions.resize(SOBOL_BITS*dimensions);
155
156         sobol_generate_direction_vectors((uint(*)[SOBOL_BITS])directions, dimensions);
157
158         device->tex_alloc("__sobol_directions", dscene->sobol_directions);
159
160         need_update = false;
161 }
162
163 void Integrator::device_free(Device *device, DeviceScene *dscene)
164 {
165         device->tex_free(dscene->sobol_directions);
166         dscene->sobol_directions.clear();
167 }
168
169 bool Integrator::modified(const Integrator& integrator)
170 {
171         return !(min_bounce == integrator.min_bounce &&
172                 max_bounce == integrator.max_bounce &&
173                 max_diffuse_bounce == integrator.max_diffuse_bounce &&
174                 max_glossy_bounce == integrator.max_glossy_bounce &&
175                 max_transmission_bounce == integrator.max_transmission_bounce &&
176                 max_volume_bounce == integrator.max_volume_bounce &&
177                 probalistic_termination == integrator.probalistic_termination &&
178                 transparent_min_bounce == integrator.transparent_min_bounce &&
179                 transparent_max_bounce == integrator.transparent_max_bounce &&
180                 transparent_probalistic == integrator.transparent_probalistic &&
181                 transparent_shadows == integrator.transparent_shadows &&
182                 volume_homogeneous_sampling == integrator.volume_homogeneous_sampling &&
183                 volume_max_steps == integrator.volume_max_steps &&
184                 volume_step_size == integrator.volume_step_size &&
185                 no_caustics == integrator.no_caustics &&
186                 filter_glossy == integrator.filter_glossy &&
187                 layer_flag == integrator.layer_flag &&
188                 seed == integrator.seed &&
189                 sample_clamp_direct == integrator.sample_clamp_direct &&
190                 sample_clamp_indirect == integrator.sample_clamp_indirect &&
191                 method == integrator.method &&
192                 aa_samples == integrator.aa_samples &&
193                 diffuse_samples == integrator.diffuse_samples &&
194                 glossy_samples == integrator.glossy_samples &&
195                 transmission_samples == integrator.transmission_samples &&
196                 ao_samples == integrator.ao_samples &&
197                 mesh_light_samples == integrator.mesh_light_samples &&
198                 subsurface_samples == integrator.subsurface_samples &&
199                 volume_samples == integrator.volume_samples &&
200                 motion_blur == integrator.motion_blur &&
201                 sampling_pattern == integrator.sampling_pattern &&
202                 sample_all_lights_indirect == integrator.sample_all_lights_indirect);
203 }
204
205 void Integrator::tag_update(Scene *scene)
206 {
207         need_update = true;
208 }
209
210 CCL_NAMESPACE_END
211