Cycles: ambient occlusion support, with AO factor and distance, and a render pass.
[blender.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         seed = 0;
45         layer_flag = ~0;
46
47         need_update = true;
48 }
49
50 Integrator::~Integrator()
51 {
52 }
53
54 void Integrator::device_update(Device *device, DeviceScene *dscene)
55 {
56         if(!need_update)
57                 return;
58
59         device_free(device, dscene);
60
61         KernelIntegrator *kintegrator = &dscene->data.integrator;
62
63         /* integrator parameters */
64         kintegrator->max_bounce = max_bounce + 1;
65         if(probalistic_termination)
66                 kintegrator->min_bounce = min_bounce + 1;
67         else
68                 kintegrator->min_bounce = kintegrator->max_bounce;
69
70         kintegrator->max_diffuse_bounce = max_diffuse_bounce + 1;
71         kintegrator->max_glossy_bounce = max_glossy_bounce + 1;
72         kintegrator->max_transmission_bounce = max_transmission_bounce + 1;
73
74         kintegrator->transparent_max_bounce = transparent_max_bounce + 1;
75         if(transparent_probalistic)
76                 kintegrator->transparent_min_bounce = transparent_min_bounce + 1;
77         else
78                 kintegrator->transparent_min_bounce = kintegrator->transparent_max_bounce;
79
80         kintegrator->transparent_shadows = transparent_shadows;
81
82         kintegrator->no_caustics = no_caustics;
83         kintegrator->seed = hash_int(seed);
84         kintegrator->layer_flag = layer_flag << PATH_RAY_LAYER_SHIFT;
85
86         kintegrator->use_ambient_occlusion =
87                 ((dscene->data.film.pass_flag & PASS_AO) || dscene->data.background.ao_factor != 0.0f);
88
89         /* sobol directions table */
90         int dimensions = PRNG_BASE_NUM + (max_bounce + transparent_max_bounce + 2)*PRNG_BOUNCE_NUM;
91         uint *directions = dscene->sobol_directions.resize(SOBOL_BITS*dimensions);
92
93         sobol_generate_direction_vectors((uint(*)[SOBOL_BITS])directions, dimensions);
94
95         device->tex_alloc("__sobol_directions", dscene->sobol_directions);
96
97         need_update = false;
98 }
99
100 void Integrator::device_free(Device *device, DeviceScene *dscene)
101 {
102         device->tex_free(dscene->sobol_directions);
103         dscene->sobol_directions.clear();
104 }
105
106 bool Integrator::modified(const Integrator& integrator)
107 {
108         return !(min_bounce == integrator.min_bounce &&
109                 max_bounce == integrator.max_bounce &&
110                 max_diffuse_bounce == integrator.max_diffuse_bounce &&
111                 max_glossy_bounce == integrator.max_glossy_bounce &&
112                 max_transmission_bounce == integrator.max_transmission_bounce &&
113                 probalistic_termination == integrator.probalistic_termination &&
114                 transparent_min_bounce == integrator.transparent_min_bounce &&
115                 transparent_max_bounce == integrator.transparent_max_bounce &&
116                 transparent_probalistic == integrator.transparent_probalistic &&
117                 transparent_shadows == integrator.transparent_shadows &&
118                 no_caustics == integrator.no_caustics &&
119                 layer_flag == integrator.layer_flag &&
120                 seed == integrator.seed);
121 }
122
123 void Integrator::tag_update(Scene *scene)
124 {
125         need_update = true;
126 }
127
128 CCL_NAMESPACE_END
129