paysages3d/src/render/software/SoftwareCanvasRenderer.cpp

165 lines
3.9 KiB
C++
Raw Normal View History

#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"
#include "RenderConfig.h"
#include "ColorProfile.h"
#include "CanvasPreview.h"
SoftwareCanvasRenderer::SoftwareCanvasRenderer()
{
started = false;
interrupted = false;
canvas = new Canvas();
2014-08-20 13:58:37 +00:00
progress = 0.0;
samples = 1;
2014-06-12 15:45:59 +00:00
rasterizers.push_back(new SkyRasterizer(this, 0));
2014-06-12 15:45:59 +00:00
rasterizers.push_back(new WaterRasterizer(this, 1));
rasterizers.push_back(new TerrainRasterizer(this, 2));
2014-08-19 07:18:55 +00:00
current_work = NULL;
}
SoftwareCanvasRenderer::~SoftwareCanvasRenderer()
{
delete canvas;
2014-06-12 15:45:59 +00:00
for (auto &rasterizer: rasterizers)
{
delete rasterizer;
}
}
void SoftwareCanvasRenderer::setConfig(const RenderConfig &config)
{
if (not started)
{
setSize(config.width, config.height, config.antialias);
render_quality = config.quality;
}
}
void SoftwareCanvasRenderer::setSize(int width, int height, int samples)
{
if (not started)
{
canvas->setSize(width * samples, height * samples);
this->samples = samples;
}
}
void SoftwareCanvasRenderer::render()
{
started = true;
2014-08-20 13:58:37 +00:00
progress = 0.0;
2014-06-12 15:45:59 +00:00
render_camera->setRenderSize(canvas->getWidth(), canvas->getHeight());
2014-06-18 18:10:05 +00:00
prepare();
// 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;
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
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
}
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
}
portion->discardPixels();
2014-08-20 13:58:37 +00:00
i++;
progress = (double)i / (double)n;
}
}
}
2014-08-19 07:18:55 +00:00
void SoftwareCanvasRenderer::interrupt()
{
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)
{
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
}
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-12 15:45:59 +00:00
rasterizer->rasterizeToCanvas(portion);
2014-08-20 13:58:37 +00:00
progress += progress_segment / (double)rasterizers.size();
}
}
2014-08-20 13:58:37 +00:00
void SoftwareCanvasRenderer::applyPixelShader(CanvasPortion *portion)
{
2014-08-18 10:17:16 +00:00
// Subdivide in chunks
int chunk_size = 64;
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
for (int sub_chunk_size = chunk_size; sub_chunk_size >= 1; sub_chunk_size /= 2)
{
if (interrupted)
2014-08-19 07:18:55 +00:00
{
break;
}
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;
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;
}
}