1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/*
* Copyright 2011-2013 Blender Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "tile.h"
#include "util_algorithm.h"
#include "util_types.h"
CCL_NAMESPACE_BEGIN
TileManager::TileManager(bool progressive_, int num_samples_, int2 tile_size_, int start_resolution_,
bool preserve_tile_device_, bool background_, TileOrder tile_order_, int num_devices_)
{
progressive = progressive_;
tile_size = tile_size_;
tile_order = tile_order_;
start_resolution = start_resolution_;
num_samples = num_samples_;
num_devices = num_devices_;
preserve_tile_device = preserve_tile_device_;
background = background_;
BufferParams buffer_params;
reset(buffer_params, 0);
}
TileManager::~TileManager()
{
}
void TileManager::reset(BufferParams& params_, int num_samples_)
{
params = params_;
int divider = 1;
int w = params.width, h = params.height;
if(start_resolution != INT_MAX) {
while(w*h > start_resolution*start_resolution) {
w = max(1, w/2);
h = max(1, h/2);
divider *= 2;
}
}
num_samples = num_samples_;
state.buffer = BufferParams();
state.sample = -1;
state.num_tiles = 0;
state.num_rendered_tiles = 0;
state.num_samples = 0;
state.resolution_divider = divider;
state.tiles.clear();
}
void TileManager::set_samples(int num_samples_)
{
num_samples = num_samples_;
}
/* splits image into tiles and assigns equal amount of tiles to every render device */
void TileManager::gen_tiles_global()
{
int resolution = state.resolution_divider;
int image_w = max(1, params.width/resolution);
int image_h = max(1, params.height/resolution);
state.tiles.clear();
int tile_w = (tile_size.x >= image_w)? 1: (image_w + tile_size.x - 1)/tile_size.x;
int tile_h = (tile_size.y >= image_h)? 1: (image_h + tile_size.y - 1)/tile_size.y;
int num_logical_devices = preserve_tile_device? num_devices: 1;
int num = min(image_h, num_logical_devices);
int tile_index = 0;
int tiles_per_device = (tile_w * tile_h + num - 1) / num;
int cur_device = 0, cur_tiles = 0;
for(int tile_y = 0; tile_y < tile_h; tile_y++) {
for(int tile_x = 0; tile_x < tile_w; tile_x++, tile_index++) {
int x = tile_x * tile_size.x;
int y = tile_y * tile_size.y;
int w = (tile_x == tile_w-1)? image_w - x: tile_size.x;
int h = (tile_y == tile_h-1)? image_h - y: tile_size.y;
state.tiles.push_back(Tile(tile_index, x, y, w, h, cur_device));
cur_tiles++;
if(cur_tiles == tiles_per_device) {
cur_tiles = 0;
cur_device++;
}
}
}
}
/* slices image into as much pieces as how many devices are rendering this image */
void TileManager::gen_tiles_sliced()
{
int resolution = state.resolution_divider;
int image_w = max(1, params.width/resolution);
int image_h = max(1, params.height/resolution);
state.tiles.clear();
int num_logical_devices = preserve_tile_device? num_devices: 1;
int num = min(image_h, num_logical_devices);
int tile_index = 0;
for(int device = 0; device < num; device++) {
int device_y = (image_h/num)*device;
int device_h = (device == num-1)? image_h - device*(image_h/num): image_h/num;
int tile_w = (tile_size.x >= image_w)? 1: (image_w + tile_size.x - 1)/tile_size.x;
int tile_h = (tile_size.y >= device_h)? 1: (device_h + tile_size.y - 1)/tile_size.y;
for(int tile_y = 0; tile_y < tile_h; tile_y++) {
for(int tile_x = 0; tile_x < tile_w; tile_x++, tile_index++) {
int x = tile_x * tile_size.x;
int y = tile_y * tile_size.y;
int w = (tile_x == tile_w-1)? image_w - x: tile_size.x;
int h = (tile_y == tile_h-1)? device_h - y: tile_size.y;
state.tiles.push_back(Tile(tile_index, x, y + device_y, w, h, device));
}
}
}
}
void TileManager::set_tiles()
{
int resolution = state.resolution_divider;
int image_w = max(1, params.width/resolution);
int image_h = max(1, params.height/resolution);
if(background)
gen_tiles_global();
else
gen_tiles_sliced();
state.num_tiles = state.tiles.size();
state.buffer.width = image_w;
state.buffer.height = image_h;
state.buffer.full_x = params.full_x/resolution;
state.buffer.full_y = params.full_y/resolution;
state.buffer.full_width = max(1, params.full_width/resolution);
state.buffer.full_height = max(1, params.full_height/resolution);
}
list<Tile>::iterator TileManager::next_viewport_tile(int device)
{
list<Tile>::iterator iter;
int logical_device = preserve_tile_device? device: 0;
for(iter = state.tiles.begin(); iter != state.tiles.end(); iter++) {
if(iter->device == logical_device && iter->rendering == false)
return iter;
}
return state.tiles.end();
}
list<Tile>::iterator TileManager::next_background_tile(int device, TileOrder tile_order)
{
list<Tile>::iterator iter, best = state.tiles.end();
int resolution = state.resolution_divider;
int logical_device = preserve_tile_device? device: 0;
int64_t cordx = max(1, params.width/resolution);
int64_t cordy = max(1, params.height/resolution);
int64_t mindist = INT_MAX;
int64_t centx = cordx / 2, centy = cordy / 2;
for(iter = state.tiles.begin(); iter != state.tiles.end(); iter++) {
if(iter->device == logical_device && iter->rendering == false) {
Tile &cur_tile = *iter;
int64_t distx = cordx;
int64_t disty = cordy;
switch (tile_order) {
case TILE_CENTER:
distx = centx - (cur_tile.x + (cur_tile.w / 2));
disty = centy - (cur_tile.y + (cur_tile.h / 2));
distx = (int64_t)sqrt((double)(distx * distx + disty * disty));
break;
case TILE_RIGHT_TO_LEFT:
distx = cordx - cur_tile.x;
break;
case TILE_LEFT_TO_RIGHT:
distx = cordx + cur_tile.x;
break;
case TILE_TOP_TO_BOTTOM:
distx = cordx - cur_tile.y;
break;
case TILE_BOTTOM_TO_TOP:
distx = cordx + cur_tile.y;
break;
default:
break;
}
if(distx < mindist) {
best = iter;
mindist = distx;
}
}
}
return best;
}
bool TileManager::next_tile(Tile& tile, int device)
{
list<Tile>::iterator tile_it;
if(background)
tile_it = next_background_tile(device, tile_order);
else
tile_it = next_viewport_tile(device);
if(tile_it != state.tiles.end()) {
tile_it->rendering = true;
tile = *tile_it;
state.num_rendered_tiles++;
return true;
}
return false;
}
bool TileManager::done()
{
return (state.sample+state.num_samples >= num_samples && state.resolution_divider == 1);
}
bool TileManager::next()
{
if(done())
return false;
if(progressive && state.resolution_divider > 1) {
state.sample = 0;
state.resolution_divider /= 2;
state.num_samples = 1;
set_tiles();
}
else {
state.sample++;
if(progressive)
state.num_samples = 1;
else
state.num_samples = num_samples;
state.resolution_divider = 1;
set_tiles();
}
return true;
}
CCL_NAMESPACE_END
|