Cycles: more code refactoring to rename things internally as well. Also change
[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         probalistic_termination = true;
37
38         transparent_min_bounce = min_bounce;
39         transparent_max_bounce = max_bounce;
40         transparent_probalistic = true;
41         transparent_shadows = false;
42
43         no_caustics = false;
44         filter_glossy = 0.0f;
45         seed = 0;
46         layer_flag = ~0;
47         sample_clamp = 0.0f;
48         motion_blur = false;
49
50         aa_samples = 0;
51         diffuse_samples = 1;
52         glossy_samples = 1;
53         transmission_samples = 1;
54         ao_samples = 1;
55         mesh_light_samples = 1;
56         subsurface_samples = 1;
57         method = PATH;
58
59         sampling_pattern = SAMPLING_PATTERN_SOBOL;
60
61         need_update = true;
62 }
63
64 Integrator::~Integrator()
65 {
66 }
67
68 void Integrator::device_update(Device *device, DeviceScene *dscene, Scene *scene)
69 {
70         if(!need_update)
71                 return;
72
73         device_free(device, dscene);
74
75         KernelIntegrator *kintegrator = &dscene->data.integrator;
76
77         /* integrator parameters */
78         kintegrator->max_bounce = max_bounce + 1;
79         if(probalistic_termination)
80                 kintegrator->min_bounce = min_bounce + 1;
81         else
82                 kintegrator->min_bounce = kintegrator->max_bounce;
83
84         kintegrator->max_diffuse_bounce = max_diffuse_bounce + 1;
85         kintegrator->max_glossy_bounce = max_glossy_bounce + 1;
86         kintegrator->max_transmission_bounce = max_transmission_bounce + 1;
87
88         kintegrator->transparent_max_bounce = transparent_max_bounce + 1;
89         if(transparent_probalistic)
90                 kintegrator->transparent_min_bounce = transparent_min_bounce + 1;
91         else
92                 kintegrator->transparent_min_bounce = kintegrator->transparent_max_bounce;
93
94         kintegrator->transparent_shadows = transparent_shadows;
95
96         kintegrator->no_caustics = no_caustics;
97         kintegrator->filter_glossy = (filter_glossy == 0.0f)? FLT_MAX: 1.0f/filter_glossy;
98
99         kintegrator->seed = hash_int(seed);
100         kintegrator->layer_flag = layer_flag << PATH_RAY_LAYER_SHIFT;
101
102         kintegrator->use_ambient_occlusion =
103                 ((dscene->data.film.pass_flag & PASS_AO) || dscene->data.background.ao_factor != 0.0f);
104         
105         kintegrator->sample_clamp = (sample_clamp == 0.0f)? FLT_MAX: sample_clamp*3.0f;
106
107         kintegrator->branched = (method == BRANCHED_PATH);
108         kintegrator->aa_samples = aa_samples;
109         kintegrator->diffuse_samples = diffuse_samples;
110         kintegrator->glossy_samples = glossy_samples;
111         kintegrator->transmission_samples = transmission_samples;
112         kintegrator->ao_samples = ao_samples;
113         kintegrator->mesh_light_samples = mesh_light_samples;
114         kintegrator->subsurface_samples = subsurface_samples;
115
116         kintegrator->sampling_pattern = sampling_pattern;
117
118         /* sobol directions table */
119         int max_samples = 1;
120
121         if(method == BRANCHED_PATH) {
122                 foreach(Light *light, scene->lights)
123                         max_samples = max(max_samples, light->samples);
124
125                 max_samples = max(max_samples, max(diffuse_samples, max(glossy_samples, transmission_samples)));
126                 max_samples = max(max_samples, max(ao_samples, max(mesh_light_samples, subsurface_samples)));
127         }
128
129         max_samples *= (max_bounce + transparent_max_bounce + 2);
130
131         int dimensions = PRNG_BASE_NUM + max_samples*PRNG_BOUNCE_NUM;
132         dimensions = min(dimensions, SOBOL_MAX_DIMENSIONS);
133
134         uint *directions = dscene->sobol_directions.resize(SOBOL_BITS*dimensions);
135
136         sobol_generate_direction_vectors((uint(*)[SOBOL_BITS])directions, dimensions);
137
138         device->tex_alloc("__sobol_directions", dscene->sobol_directions);
139
140         need_update = false;
141 }
142
143 void Integrator::device_free(Device *device, DeviceScene *dscene)
144 {
145         device->tex_free(dscene->sobol_directions);
146         dscene->sobol_directions.clear();
147 }
148
149 bool Integrator::modified(const Integrator& integrator)
150 {
151         return !(min_bounce == integrator.min_bounce &&
152                 max_bounce == integrator.max_bounce &&
153                 max_diffuse_bounce == integrator.max_diffuse_bounce &&
154                 max_glossy_bounce == integrator.max_glossy_bounce &&
155                 max_transmission_bounce == integrator.max_transmission_bounce &&
156                 probalistic_termination == integrator.probalistic_termination &&
157                 transparent_min_bounce == integrator.transparent_min_bounce &&
158                 transparent_max_bounce == integrator.transparent_max_bounce &&
159                 transparent_probalistic == integrator.transparent_probalistic &&
160                 transparent_shadows == integrator.transparent_shadows &&
161                 no_caustics == integrator.no_caustics &&
162                 filter_glossy == integrator.filter_glossy &&
163                 layer_flag == integrator.layer_flag &&
164                 seed == integrator.seed &&
165                 sample_clamp == integrator.sample_clamp &&
166                 method == integrator.method &&
167                 aa_samples == integrator.aa_samples &&
168                 diffuse_samples == integrator.diffuse_samples &&
169                 glossy_samples == integrator.glossy_samples &&
170                 transmission_samples == integrator.transmission_samples &&
171                 ao_samples == integrator.ao_samples &&
172                 mesh_light_samples == integrator.mesh_light_samples &&
173                 subsurface_samples == integrator.subsurface_samples &&
174                 motion_blur == integrator.motion_blur &&
175                 sampling_pattern == integrator.sampling_pattern);
176 }
177
178 void Integrator::tag_update(Scene *scene)
179 {
180         need_update = true;
181 }
182
183 CCL_NAMESPACE_END
184