Merging r46111 through r46136 from trunk into soc-2011-tomato
[blender-staging.git] / intern / cycles / render / integrator.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "device.h"
20 #include "integrator.h"
21 #include "scene.h"
22 #include "sobol.h"
23
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         need_update = true;
51 }
52
53 Integrator::~Integrator()
54 {
55 }
56
57 void Integrator::device_update(Device *device, DeviceScene *dscene)
58 {
59         if(!need_update)
60                 return;
61
62         device_free(device, dscene);
63
64         KernelIntegrator *kintegrator = &dscene->data.integrator;
65
66         /* integrator parameters */
67         kintegrator->max_bounce = max_bounce + 1;
68         if(probalistic_termination)
69                 kintegrator->min_bounce = min_bounce + 1;
70         else
71                 kintegrator->min_bounce = kintegrator->max_bounce;
72
73         kintegrator->max_diffuse_bounce = max_diffuse_bounce + 1;
74         kintegrator->max_glossy_bounce = max_glossy_bounce + 1;
75         kintegrator->max_transmission_bounce = max_transmission_bounce + 1;
76
77         kintegrator->transparent_max_bounce = transparent_max_bounce + 1;
78         if(transparent_probalistic)
79                 kintegrator->transparent_min_bounce = transparent_min_bounce + 1;
80         else
81                 kintegrator->transparent_min_bounce = kintegrator->transparent_max_bounce;
82
83         kintegrator->transparent_shadows = transparent_shadows;
84
85         kintegrator->no_caustics = no_caustics;
86         kintegrator->filter_glossy = (filter_glossy == 0.0f)? FLT_MAX: 1.0f/filter_glossy;
87
88         kintegrator->seed = hash_int(seed);
89         kintegrator->layer_flag = layer_flag << PATH_RAY_LAYER_SHIFT;
90
91         kintegrator->use_ambient_occlusion =
92                 ((dscene->data.film.pass_flag & PASS_AO) || dscene->data.background.ao_factor != 0.0f);
93         
94         kintegrator->sample_clamp = (sample_clamp == 0.0f)? FLT_MAX: sample_clamp*3.0f;
95
96         /* sobol directions table */
97         int dimensions = PRNG_BASE_NUM + (max_bounce + transparent_max_bounce + 2)*PRNG_BOUNCE_NUM;
98         uint *directions = dscene->sobol_directions.resize(SOBOL_BITS*dimensions);
99
100         sobol_generate_direction_vectors((uint(*)[SOBOL_BITS])directions, dimensions);
101
102         device->tex_alloc("__sobol_directions", dscene->sobol_directions);
103
104         need_update = false;
105 }
106
107 void Integrator::device_free(Device *device, DeviceScene *dscene)
108 {
109         device->tex_free(dscene->sobol_directions);
110         dscene->sobol_directions.clear();
111 }
112
113 bool Integrator::modified(const Integrator& integrator)
114 {
115         return !(min_bounce == integrator.min_bounce &&
116                 max_bounce == integrator.max_bounce &&
117                 max_diffuse_bounce == integrator.max_diffuse_bounce &&
118                 max_glossy_bounce == integrator.max_glossy_bounce &&
119                 max_transmission_bounce == integrator.max_transmission_bounce &&
120                 probalistic_termination == integrator.probalistic_termination &&
121                 transparent_min_bounce == integrator.transparent_min_bounce &&
122                 transparent_max_bounce == integrator.transparent_max_bounce &&
123                 transparent_probalistic == integrator.transparent_probalistic &&
124                 transparent_shadows == integrator.transparent_shadows &&
125                 no_caustics == integrator.no_caustics &&
126                 filter_glossy == integrator.filter_glossy &&
127                 layer_flag == integrator.layer_flag &&
128                 seed == integrator.seed &&
129                 sample_clamp == integrator.sample_clamp &&
130                 motion_blur == integrator.motion_blur);
131 }
132
133 void Integrator::tag_update(Scene *scene)
134 {
135         need_update = true;
136 }
137
138 CCL_NAMESPACE_END
139