2011-04-27 11:58:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011, Blender Foundation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tile.h"
|
|
|
|
|
|
|
|
#include "util_algorithm.h"
|
2012-09-04 13:29:07 +00:00
|
|
|
#include "util_types.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2012-10-23 16:36:53 +00:00
|
|
|
TileManager::TileManager(bool progressive_, int num_samples_, int2 tile_size_, int start_resolution_,
|
2012-10-24 14:43:29 +00:00
|
|
|
bool preserve_tile_device_, bool background_, int num_devices_)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
progressive = progressive_;
|
|
|
|
tile_size = tile_size_;
|
2012-09-17 10:55:18 +00:00
|
|
|
start_resolution = start_resolution_;
|
2012-09-04 13:29:07 +00:00
|
|
|
num_devices = num_devices_;
|
2012-10-23 16:36:53 +00:00
|
|
|
preserve_tile_device = preserve_tile_device_;
|
2012-10-24 14:43:29 +00:00
|
|
|
background = background_;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-12-20 12:25:37 +00:00
|
|
|
BufferParams buffer_params;
|
|
|
|
reset(buffer_params, 0);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TileManager::~TileManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
void TileManager::reset(BufferParams& params_, int num_samples_)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-12-20 12:25:37 +00:00
|
|
|
params = params_;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-09-17 10:55:18 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
num_samples = num_samples_;
|
2011-08-28 13:55:59 +00:00
|
|
|
|
2011-12-21 13:48:35 +00:00
|
|
|
state.buffer = BufferParams();
|
2011-09-16 13:14:02 +00:00
|
|
|
state.sample = -1;
|
2012-09-04 13:29:07 +00:00
|
|
|
state.num_tiles = 0;
|
|
|
|
state.num_rendered_tiles = 0;
|
|
|
|
state.num_samples = 0;
|
2012-09-17 10:55:18 +00:00
|
|
|
state.resolution_divider = divider;
|
2011-04-27 11:58:34 +00:00
|
|
|
state.tiles.clear();
|
|
|
|
}
|
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
void TileManager::set_samples(int num_samples_)
|
2011-08-28 13:55:59 +00:00
|
|
|
{
|
2012-09-04 13:29:07 +00:00
|
|
|
num_samples = num_samples_;
|
2011-08-28 13:55:59 +00:00
|
|
|
}
|
|
|
|
|
2012-10-24 14:43:29 +00:00
|
|
|
/* splits image into tiles and assigns equal amount of tiles to every render device */
|
|
|
|
void TileManager::gen_tiles_global()
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-09-17 10:55:18 +00:00
|
|
|
int resolution = state.resolution_divider;
|
2011-12-20 12:25:37 +00:00
|
|
|
int image_w = max(1, params.width/resolution);
|
|
|
|
int image_h = max(1, params.height/resolution);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
state.tiles.clear();
|
|
|
|
|
2012-10-24 14:43:29 +00:00
|
|
|
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;
|
|
|
|
|
2012-10-23 16:36:53 +00:00
|
|
|
int num_logical_devices = preserve_tile_device? num_devices: 1;
|
2012-10-24 14:43:29 +00:00
|
|
|
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++) {
|
2012-11-05 08:05:14 +00:00
|
|
|
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;
|
2012-10-24 14:43:29 +00:00
|
|
|
|
|
|
|
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();
|
2012-10-23 16:36:53 +00:00
|
|
|
|
2012-10-24 14:43:29 +00:00
|
|
|
int num_logical_devices = preserve_tile_device? num_devices: 1;
|
2012-10-23 16:36:53 +00:00
|
|
|
int num = min(image_h, num_logical_devices);
|
2012-10-24 14:43:29 +00:00
|
|
|
int tile_index = 0;
|
2012-09-04 13:29:07 +00:00
|
|
|
|
|
|
|
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;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
for(int tile_y = 0; tile_y < tile_h; tile_y++) {
|
2012-10-23 16:36:53 +00:00
|
|
|
for(int tile_x = 0; tile_x < tile_w; tile_x++, tile_index++) {
|
2012-11-05 08:05:14 +00:00
|
|
|
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;
|
2012-11-05 11:57:14 +00:00
|
|
|
int h = (tile_y == tile_h-1)? device_h - y: tile_size.y;
|
2012-09-04 13:29:07 +00:00
|
|
|
|
2012-10-23 16:36:53 +00:00
|
|
|
state.tiles.push_back(Tile(tile_index, x, y + device_y, w, h, device));
|
2012-09-04 13:29:07 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-24 14:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
state.num_tiles = state.tiles.size();
|
|
|
|
|
2011-12-21 13:48:35 +00:00
|
|
|
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);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
list<Tile>::iterator TileManager::next_center_tile(int device)
|
|
|
|
{
|
|
|
|
list<Tile>::iterator iter, best = state.tiles.end();
|
|
|
|
|
2012-09-17 10:55:18 +00:00
|
|
|
int resolution = state.resolution_divider;
|
2012-09-04 13:29:07 +00:00
|
|
|
int image_w = max(1, params.width/resolution);
|
|
|
|
int image_h = max(1, params.height/resolution);
|
|
|
|
|
2012-10-23 16:36:53 +00:00
|
|
|
int logical_device = preserve_tile_device? device: 0;
|
2012-09-04 13:29:07 +00:00
|
|
|
|
2012-10-24 14:43:29 +00:00
|
|
|
int64_t centx = image_w / 2, centy = image_h / 2, tot = 1;
|
|
|
|
int64_t mindist = (int64_t) image_w * (int64_t) image_h;
|
2012-09-04 13:29:07 +00:00
|
|
|
|
|
|
|
/* find center of rendering tiles, image center counts for 1 too */
|
|
|
|
for(iter = state.tiles.begin(); iter != state.tiles.end(); iter++) {
|
|
|
|
if(iter->rendering) {
|
|
|
|
Tile &cur_tile = *iter;
|
|
|
|
centx += cur_tile.x + cur_tile.w / 2;
|
|
|
|
centy += cur_tile.y + cur_tile.h / 2;
|
|
|
|
tot++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
centx /= tot;
|
|
|
|
centy /= tot;
|
|
|
|
|
|
|
|
/* closest of the non-rendering tiles */
|
|
|
|
for(iter = state.tiles.begin(); iter != state.tiles.end(); iter++) {
|
2012-10-23 16:36:53 +00:00
|
|
|
if(iter->device == logical_device && iter->rendering == false) {
|
2012-09-04 13:29:07 +00:00
|
|
|
Tile &cur_tile = *iter;
|
|
|
|
|
|
|
|
int64_t distx = centx - (cur_tile.x + cur_tile.w / 2);
|
|
|
|
int64_t disty = centy - (cur_tile.y + cur_tile.h / 2);
|
|
|
|
distx = (int64_t) sqrt((double)distx * distx + disty * disty);
|
|
|
|
|
|
|
|
if(distx < mindist) {
|
|
|
|
best = iter;
|
|
|
|
mindist = distx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2012-10-25 12:03:36 +00:00
|
|
|
list<Tile>::iterator TileManager::next_simple_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();
|
|
|
|
}
|
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
bool TileManager::next_tile(Tile& tile, int device)
|
|
|
|
{
|
|
|
|
list<Tile>::iterator tile_it;
|
|
|
|
|
2012-10-25 12:03:36 +00:00
|
|
|
if(background)
|
|
|
|
tile_it = next_center_tile(device);
|
|
|
|
else
|
|
|
|
tile_it = next_simple_tile(device);
|
2012-09-04 13:29:07 +00:00
|
|
|
|
|
|
|
if(tile_it != state.tiles.end()) {
|
|
|
|
tile_it->rendering = true;
|
|
|
|
tile = *tile_it;
|
|
|
|
state.num_rendered_tiles++;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
bool TileManager::done()
|
|
|
|
{
|
2012-09-17 10:55:18 +00:00
|
|
|
return (state.sample+state.num_samples >= num_samples && state.resolution_divider == 1);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TileManager::next()
|
|
|
|
{
|
|
|
|
if(done())
|
|
|
|
return false;
|
|
|
|
|
2012-09-17 10:55:18 +00:00
|
|
|
if(progressive && state.resolution_divider > 1) {
|
2011-09-16 13:14:02 +00:00
|
|
|
state.sample = 0;
|
2012-09-17 10:55:18 +00:00
|
|
|
state.resolution_divider /= 2;
|
2012-09-04 13:29:07 +00:00
|
|
|
state.num_samples = 1;
|
2011-04-27 11:58:34 +00:00
|
|
|
set_tiles();
|
|
|
|
}
|
|
|
|
else {
|
2011-09-16 13:14:02 +00:00
|
|
|
state.sample++;
|
2012-09-04 13:29:07 +00:00
|
|
|
|
|
|
|
if(progressive)
|
|
|
|
state.num_samples = 1;
|
|
|
|
else
|
|
|
|
state.num_samples = num_samples;
|
|
|
|
|
2012-09-17 10:55:18 +00:00
|
|
|
state.resolution_divider = 1;
|
2011-04-27 11:58:34 +00:00
|
|
|
set_tiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|