04e48d44029e3973813c595a02e77f94ff2af41b
[blender-staging.git] / intern / cycles / render / tile.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 "tile.h"
20
21 #include "util_algorithm.h"
22
23 CCL_NAMESPACE_BEGIN
24
25 TileManager::TileManager(bool progressive_, int samples_, int tile_size_, int min_size_)
26 {
27         progressive = progressive_;
28         tile_size = tile_size_;
29         min_size = min_size_;
30
31         BufferParams buffer_params;
32         reset(buffer_params, 0);
33 }
34
35 TileManager::~TileManager()
36 {
37 }
38
39 void TileManager::reset(BufferParams& params_, int samples_)
40 {
41         params = params_;
42
43         start_resolution = 1;
44
45         int w = params.width, h = params.height;
46
47         if(min_size != INT_MAX) {
48                 while(w*h > min_size*min_size) {
49                         w = max(1, w/2); 
50                         h = max(1, h/2); 
51
52                         start_resolution *= 2;
53                 }
54         }
55
56         samples = samples_;
57
58         state.buffer = BufferParams();
59         state.sample = -1;
60         state.resolution = start_resolution;
61         state.tiles.clear();
62 }
63
64 void TileManager::set_samples(int samples_)
65 {
66         samples = samples_;
67 }
68
69 void TileManager::set_tiles()
70 {
71         int resolution = state.resolution;
72         int image_w = max(1, params.width/resolution);
73         int image_h = max(1, params.height/resolution);
74         int tile_w = (tile_size >= image_w)? 1: (image_w + tile_size - 1)/tile_size;
75         int tile_h = (tile_size >= image_h)? 1: (image_h + tile_size - 1)/tile_size;
76         int sub_w = image_w/tile_w;
77         int sub_h = image_h/tile_h;
78
79         state.tiles.clear();
80
81         for(int tile_y = 0; tile_y < tile_h; tile_y++) {
82                 for(int tile_x = 0; tile_x < tile_w; tile_x++) {
83                         int x = tile_x * sub_w;
84                         int y = tile_y * sub_h;
85                         int w = (tile_x == tile_w-1)? image_w - x: sub_w;
86                         int h = (tile_y == tile_h-1)? image_h - y: sub_h;
87
88                         state.tiles.push_back(Tile(x, y, w, h));
89                 }
90         }
91
92         state.buffer.width = image_w;
93         state.buffer.height = image_h;
94
95         state.buffer.full_x = params.full_x/resolution;
96         state.buffer.full_y = params.full_y/resolution;
97         state.buffer.full_width = max(1, params.full_width/resolution);
98         state.buffer.full_height = max(1, params.full_height/resolution);
99 }
100
101 bool TileManager::done()
102 {
103         return (state.sample+1 >= samples && state.resolution == 1);
104 }
105
106 bool TileManager::next()
107 {
108         if(done())
109                 return false;
110
111         if(progressive && state.resolution > 1) {
112                 state.sample = 0;
113                 state.resolution /= 2;
114                 set_tiles();
115         }
116         else {
117                 state.sample++;
118                 state.resolution = 1;
119                 set_tiles();
120         }
121
122         return true;
123 }
124
125 CCL_NAMESPACE_END
126