Cycles: svn merge -r41225:41232 ^/trunk/blender
[blender.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         reset(0, 0, 0);
32 }
33
34 TileManager::~TileManager()
35 {
36 }
37
38 void TileManager::reset(int width_, int height_, int samples_)
39 {
40         full_width = width_;
41         full_height = height_;
42
43         start_resolution = 1;
44
45         int w = width_, h = 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.width = 0;
59         state.height = 0;
60         state.sample = -1;
61         state.resolution = start_resolution;
62         state.tiles.clear();
63 }
64
65 void TileManager::set_samples(int samples_)
66 {
67         samples = samples_;
68 }
69
70 void TileManager::set_tiles()
71 {
72         int resolution = state.resolution;
73         int image_w = max(1, full_width/resolution);
74         int image_h = max(1, full_height/resolution);
75         int tile_w = (image_w + tile_size - 1)/tile_size;
76         int tile_h = (image_h + tile_size - 1)/tile_size;
77         int sub_w = image_w/tile_w;
78         int sub_h = image_h/tile_h;
79
80         state.tiles.clear();
81
82         for(int tile_y = 0; tile_y < tile_h; tile_y++) {
83                 for(int tile_x = 0; tile_x < tile_w; tile_x++) {
84                         int x = tile_x * sub_w;
85                         int y = tile_y * sub_h;
86                         int w = (tile_x == tile_w-1)? image_w - x: sub_w;
87                         int h = (tile_y == tile_h-1)? image_h - y: sub_h;
88
89                         state.tiles.push_back(Tile(x, y, w, h));
90                 }
91         }
92
93         state.width = image_w;
94         state.height = image_h;
95 }
96
97 bool TileManager::done()
98 {
99         return (state.sample+1 >= samples && state.resolution == 1);
100 }
101
102 bool TileManager::next()
103 {
104         if(done())
105                 return false;
106
107         if(progressive && state.resolution > 1) {
108                 state.sample = 0;
109                 state.resolution /= 2;
110                 set_tiles();
111         }
112         else {
113                 state.sample++;
114                 state.resolution = 1;
115                 set_tiles();
116         }
117
118         return true;
119 }
120
121 CCL_NAMESPACE_END
122