2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* 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
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* 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
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2015-12-22 11:51:27 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TileComparator {
|
|
|
|
public:
|
|
|
|
TileComparator(TileOrder order, int2 center)
|
|
|
|
: order_(order),
|
|
|
|
center_(center)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool operator()(Tile &a, Tile &b)
|
|
|
|
{
|
|
|
|
switch(order_) {
|
|
|
|
case TILE_CENTER:
|
|
|
|
{
|
|
|
|
float2 dist_a = make_float2(center_.x - (a.x + a.w/2),
|
|
|
|
center_.y - (a.y + a.h/2));
|
|
|
|
float2 dist_b = make_float2(center_.x - (b.x + b.w/2),
|
|
|
|
center_.y - (b.y + b.h/2));
|
|
|
|
return dot(dist_a, dist_a) < dot(dist_b, dist_b);
|
|
|
|
}
|
|
|
|
case TILE_LEFT_TO_RIGHT:
|
|
|
|
return (a.x == b.x)? (a.y < b.y): (a.x < b.x);
|
|
|
|
case TILE_RIGHT_TO_LEFT:
|
|
|
|
return (a.x == b.x)? (a.y < b.y): (a.x > b.x);
|
|
|
|
case TILE_TOP_TO_BOTTOM:
|
|
|
|
return (a.y == b.y)? (a.x < b.x): (a.y > b.y);
|
|
|
|
case TILE_BOTTOM_TO_TOP:
|
|
|
|
default:
|
|
|
|
return (a.y == b.y)? (a.x < b.x): (a.y < b.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
TileOrder order_;
|
|
|
|
int2 center_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace */
|
|
|
|
|
2012-10-23 16:36:53 +00:00
|
|
|
TileManager::TileManager(bool progressive_, int num_samples_, int2 tile_size_, int start_resolution_,
|
2013-09-03 22:39:21 +00:00
|
|
|
bool preserve_tile_device_, bool background_, TileOrder tile_order_, int num_devices_)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
progressive = progressive_;
|
|
|
|
tile_size = tile_size_;
|
2013-01-07 19:55:49 +00:00
|
|
|
tile_order = tile_order_;
|
2012-09-17 10:55:18 +00:00
|
|
|
start_resolution = start_resolution_;
|
2015-03-27 10:47:55 +00:00
|
|
|
num_samples = num_samples_;
|
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) {
|
2015-12-22 11:51:27 +00:00
|
|
|
w = max(1, w/2);
|
|
|
|
h = max(1, h/2);
|
2012-09-17 10:55:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-22 11:51:27 +00:00
|
|
|
/* If sliced is false, splits image into tiles and assigns equal amount of tiles to every render device.
|
|
|
|
* If sliced is true, slice image into as much pieces as how many devices are rendering this image. */
|
|
|
|
int TileManager::gen_tiles(bool sliced)
|
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);
|
2015-12-22 11:51:27 +00:00
|
|
|
int2 center = make_int2(image_w/2, image_h/2);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
state.tiles.clear();
|
|
|
|
|
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);
|
2015-12-22 11:51:27 +00:00
|
|
|
int slice_num = sliced? num: 1;
|
2012-10-24 14:43:29 +00:00
|
|
|
int tile_index = 0;
|
|
|
|
|
2015-12-23 23:29:40 +00:00
|
|
|
state.tiles.clear();
|
2015-12-22 11:51:27 +00:00
|
|
|
state.tiles.resize(num);
|
|
|
|
vector<list<Tile> >::iterator tile_list = state.tiles.begin();
|
2012-10-24 14:43:29 +00:00
|
|
|
|
2015-12-22 11:51:27 +00:00
|
|
|
for(int slice = 0; slice < slice_num; slice++) {
|
|
|
|
int slice_y = (image_h/slice_num)*slice;
|
|
|
|
int slice_h = (slice == slice_num-1)? image_h - slice*(image_h/slice_num): image_h/slice_num;
|
2012-09-04 13:29:07 +00:00
|
|
|
|
|
|
|
int tile_w = (tile_size.x >= image_w)? 1: (image_w + tile_size.x - 1)/tile_size.x;
|
2015-12-22 11:51:27 +00:00
|
|
|
int tile_h = (tile_size.y >= slice_h)? 1: (slice_h + tile_size.y - 1)/tile_size.y;
|
|
|
|
|
|
|
|
int tiles_per_device = (tile_w * tile_h + num - 1) / num;
|
|
|
|
int cur_device = 0, cur_tiles = 0;
|
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;
|
2015-12-22 11:51:27 +00:00
|
|
|
int h = (tile_y == tile_h-1)? slice_h - y: tile_size.y;
|
|
|
|
|
|
|
|
tile_list->push_back(Tile(tile_index, x, y + slice_y, w, h, sliced? slice: cur_device));
|
2012-09-04 13:29:07 +00:00
|
|
|
|
2015-12-22 11:51:27 +00:00
|
|
|
if(!sliced) {
|
|
|
|
cur_tiles++;
|
|
|
|
|
|
|
|
if(cur_tiles == tiles_per_device) {
|
2016-01-01 22:52:37 +00:00
|
|
|
/* Tiles are already generated in Bottom-to-Top order, so no sort is necessary in that case. */
|
|
|
|
if(tile_order != TILE_BOTTOM_TO_TOP) {
|
|
|
|
tile_list->sort(TileComparator(tile_order, center));
|
|
|
|
}
|
2015-12-22 11:51:27 +00:00
|
|
|
tile_list++;
|
|
|
|
cur_tiles = 0;
|
|
|
|
cur_device++;
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 13:29:07 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2015-12-31 12:48:38 +00:00
|
|
|
if(sliced) {
|
|
|
|
tile_list++;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2015-12-22 11:51:27 +00:00
|
|
|
|
|
|
|
return tile_index;
|
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);
|
|
|
|
|
2015-12-22 11:51:27 +00:00
|
|
|
state.num_tiles = gen_tiles(!background);
|
2012-09-04 13:29:07 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-22 11:51:27 +00:00
|
|
|
bool TileManager::next_tile(Tile& tile, int device)
|
2012-10-25 12:03:36 +00:00
|
|
|
{
|
|
|
|
int logical_device = preserve_tile_device? device: 0;
|
|
|
|
|
2015-12-23 23:29:40 +00:00
|
|
|
if((logical_device >= state.tiles.size()) || state.tiles[logical_device].empty())
|
2015-12-22 11:51:27 +00:00
|
|
|
return false;
|
2012-09-04 13:29:07 +00:00
|
|
|
|
2015-12-23 23:29:40 +00:00
|
|
|
tile = Tile(state.tiles[logical_device].front());
|
2015-12-22 11:51:27 +00:00
|
|
|
state.tiles[logical_device].pop_front();
|
|
|
|
state.num_rendered_tiles++;
|
|
|
|
return true;
|
2012-09-04 13:29:07 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|