2014-06-05 15:12:49 +00:00
|
|
|
#include "SoftwareCanvasRenderer.h"
|
|
|
|
|
|
|
|
#include "Rasterizer.h"
|
|
|
|
#include "SoftwareRenderer.h"
|
|
|
|
#include "Canvas.h"
|
2014-06-12 15:45:59 +00:00
|
|
|
#include "TerrainRasterizer.h"
|
|
|
|
#include "WaterRasterizer.h"
|
|
|
|
#include "SkyRasterizer.h"
|
|
|
|
#include "CameraDefinition.h"
|
2014-08-18 10:17:16 +00:00
|
|
|
#include "ParallelWork.h"
|
|
|
|
#include "CanvasPortion.h"
|
|
|
|
#include "CanvasPixelShader.h"
|
2014-08-20 12:23:35 +00:00
|
|
|
#include "RenderConfig.h"
|
2014-08-21 10:36:28 +00:00
|
|
|
#include "ColorProfile.h"
|
|
|
|
#include "CanvasPreview.h"
|
2014-06-05 15:12:49 +00:00
|
|
|
|
|
|
|
SoftwareCanvasRenderer::SoftwareCanvasRenderer()
|
|
|
|
{
|
|
|
|
started = false;
|
2014-08-19 12:20:37 +00:00
|
|
|
interrupted = false;
|
2014-06-05 15:12:49 +00:00
|
|
|
canvas = new Canvas();
|
2014-08-20 13:58:37 +00:00
|
|
|
progress = 0.0;
|
2014-08-21 10:36:28 +00:00
|
|
|
samples = 1;
|
2014-06-12 15:45:59 +00:00
|
|
|
|
2014-06-18 20:10:46 +00:00
|
|
|
rasterizers.push_back(new SkyRasterizer(this, 0));
|
2014-06-12 15:45:59 +00:00
|
|
|
rasterizers.push_back(new WaterRasterizer(this, 1));
|
2014-06-18 20:10:46 +00:00
|
|
|
rasterizers.push_back(new TerrainRasterizer(this, 2));
|
2014-08-19 07:18:55 +00:00
|
|
|
|
|
|
|
current_work = NULL;
|
2014-06-05 15:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SoftwareCanvasRenderer::~SoftwareCanvasRenderer()
|
|
|
|
{
|
|
|
|
delete canvas;
|
2014-06-12 15:45:59 +00:00
|
|
|
|
|
|
|
for (auto &rasterizer: rasterizers)
|
|
|
|
{
|
|
|
|
delete rasterizer;
|
|
|
|
}
|
2014-06-05 15:12:49 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 12:23:35 +00:00
|
|
|
void SoftwareCanvasRenderer::setConfig(const RenderConfig &config)
|
|
|
|
{
|
|
|
|
if (not started)
|
|
|
|
{
|
|
|
|
setSize(config.width, config.height, config.antialias);
|
|
|
|
render_quality = config.quality;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 15:12:49 +00:00
|
|
|
void SoftwareCanvasRenderer::setSize(int width, int height, int samples)
|
|
|
|
{
|
|
|
|
if (not started)
|
|
|
|
{
|
|
|
|
canvas->setSize(width * samples, height * samples);
|
2014-08-21 10:36:28 +00:00
|
|
|
this->samples = samples;
|
2014-06-05 15:12:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareCanvasRenderer::render()
|
|
|
|
{
|
|
|
|
started = true;
|
2014-08-20 13:58:37 +00:00
|
|
|
progress = 0.0;
|
2014-06-05 15:12:49 +00:00
|
|
|
|
2014-06-12 15:45:59 +00:00
|
|
|
render_camera->setRenderSize(canvas->getWidth(), canvas->getHeight());
|
|
|
|
|
2014-06-18 18:10:05 +00:00
|
|
|
prepare();
|
|
|
|
|
2014-08-18 15:33:15 +00:00
|
|
|
// Iterate portions
|
|
|
|
int nx = canvas->getHorizontalPortionCount();
|
|
|
|
int ny = canvas->getVerticalPortionCount();
|
2014-08-20 13:58:37 +00:00
|
|
|
int i = 0;
|
|
|
|
int n = nx * ny;
|
2014-08-18 15:33:15 +00:00
|
|
|
for (int y = 0; y < ny; y++)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < nx; x++)
|
|
|
|
{
|
|
|
|
CanvasPortion *portion = canvas->at(x, y);
|
2014-08-19 07:18:55 +00:00
|
|
|
|
2014-08-19 12:20:37 +00:00
|
|
|
if (not interrupted)
|
2014-08-19 07:18:55 +00:00
|
|
|
{
|
2014-08-20 13:58:37 +00:00
|
|
|
progress_segment = 0.2 / (double)n;
|
2014-08-19 07:18:55 +00:00
|
|
|
portion->preparePixels();
|
2014-08-20 13:58:37 +00:00
|
|
|
rasterize(portion);
|
2014-08-19 07:18:55 +00:00
|
|
|
}
|
|
|
|
|
2014-08-19 12:20:37 +00:00
|
|
|
if (not interrupted)
|
2014-08-19 07:18:55 +00:00
|
|
|
{
|
2014-08-20 13:58:37 +00:00
|
|
|
progress_segment = 0.8 / (double)n;
|
|
|
|
applyPixelShader(portion);
|
2014-08-19 07:18:55 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 15:33:15 +00:00
|
|
|
portion->discardPixels();
|
2014-08-20 13:58:37 +00:00
|
|
|
i++;
|
|
|
|
progress = (double)i / (double)n;
|
2014-08-18 15:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-05 15:12:49 +00:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:18:55 +00:00
|
|
|
void SoftwareCanvasRenderer::interrupt()
|
|
|
|
{
|
2014-08-19 12:20:37 +00:00
|
|
|
interrupted = true;
|
2014-08-19 07:18:55 +00:00
|
|
|
|
|
|
|
if (current_work)
|
|
|
|
{
|
|
|
|
current_work->interrupt();
|
|
|
|
}
|
2014-08-20 13:58:37 +00:00
|
|
|
for (auto &rasterizer:rasterizers)
|
2014-08-19 09:44:54 +00:00
|
|
|
{
|
|
|
|
rasterizer->interrupt();
|
|
|
|
}
|
2014-08-19 07:18:55 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 10:17:16 +00:00
|
|
|
const Rasterizer &SoftwareCanvasRenderer::getRasterizer(int client_id) const
|
|
|
|
{
|
2014-08-20 13:58:37 +00:00
|
|
|
return *(rasterizers[client_id]);
|
2014-08-18 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 10:36:28 +00:00
|
|
|
bool SoftwareCanvasRenderer::saveToDisk(const std::string &filepath) const
|
|
|
|
{
|
|
|
|
return getCanvas()->saveToDisk(filepath, *getCanvas()->getPreview()->getToneMapping(), samples);
|
|
|
|
}
|
|
|
|
|
2014-08-20 13:58:37 +00:00
|
|
|
void SoftwareCanvasRenderer::rasterize(CanvasPortion *portion)
|
2014-06-12 15:45:59 +00:00
|
|
|
{
|
2014-08-20 13:58:37 +00:00
|
|
|
for (auto &rasterizer:rasterizers)
|
2014-06-05 15:12:49 +00:00
|
|
|
{
|
2014-06-12 15:45:59 +00:00
|
|
|
rasterizer->rasterizeToCanvas(portion);
|
2014-08-20 13:58:37 +00:00
|
|
|
progress += progress_segment / (double)rasterizers.size();
|
2014-06-05 15:12:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 13:58:37 +00:00
|
|
|
void SoftwareCanvasRenderer::applyPixelShader(CanvasPortion *portion)
|
2014-06-05 15:12:49 +00:00
|
|
|
{
|
2014-08-18 10:17:16 +00:00
|
|
|
// Subdivide in chunks
|
2014-08-18 14:04:46 +00:00
|
|
|
int chunk_size = 64;
|
2014-08-18 13:20:04 +00:00
|
|
|
int chunks_x = (portion->getWidth() - 1) / chunk_size + 1;
|
|
|
|
int chunks_y = (portion->getHeight() - 1) / chunk_size + 1;
|
2014-08-18 10:17:16 +00:00
|
|
|
int units = chunks_x * chunks_y;
|
|
|
|
|
2014-08-20 13:58:37 +00:00
|
|
|
// Estimate chunks
|
|
|
|
int n = 0;
|
|
|
|
for (int sub_chunk_size = chunk_size; sub_chunk_size >= 1; sub_chunk_size /= 2)
|
|
|
|
{
|
|
|
|
n += chunk_size / sub_chunk_size;
|
|
|
|
}
|
|
|
|
|
2014-08-18 10:17:16 +00:00
|
|
|
// Render chunks in parallel
|
2014-08-18 14:04:46 +00:00
|
|
|
for (int sub_chunk_size = chunk_size; sub_chunk_size >= 1; sub_chunk_size /= 2)
|
|
|
|
{
|
2014-08-19 12:20:37 +00:00
|
|
|
if (interrupted)
|
2014-08-19 07:18:55 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-18 14:04:46 +00:00
|
|
|
CanvasPixelShader shader(*this, portion, chunk_size, sub_chunk_size, chunks_x, chunks_y);
|
|
|
|
ParallelWork work(&shader, units);
|
2014-08-19 07:18:55 +00:00
|
|
|
|
|
|
|
current_work = &work;
|
2014-08-18 14:04:46 +00:00
|
|
|
work.perform();
|
2014-08-19 07:18:55 +00:00
|
|
|
current_work = NULL;
|
2014-08-20 13:58:37 +00:00
|
|
|
progress += progress_segment * (double)(chunk_size / sub_chunk_size) / (double)n;
|
2014-08-18 14:04:46 +00:00
|
|
|
}
|
2014-06-05 15:12:49 +00:00
|
|
|
}
|