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-06-05 15:12:49 +00:00
|
|
|
|
|
|
|
SoftwareCanvasRenderer::SoftwareCanvasRenderer()
|
|
|
|
{
|
|
|
|
started = false;
|
|
|
|
canvas = new Canvas();
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareCanvasRenderer::setSize(int width, int height, int samples)
|
|
|
|
{
|
|
|
|
if (not started)
|
|
|
|
{
|
|
|
|
canvas->setSize(width * samples, height * samples);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareCanvasRenderer::render()
|
|
|
|
{
|
|
|
|
// TEMP
|
|
|
|
started = true;
|
2014-06-18 20:10:46 +00:00
|
|
|
render_width = canvas->getWidth();
|
|
|
|
render_height = canvas->getHeight();
|
|
|
|
render_quality = 3;
|
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:16:17 +00:00
|
|
|
// TODO Iterate portions
|
|
|
|
CanvasPortion *portion = canvas->at(0, 0);
|
|
|
|
portion->preparePixels();
|
2014-06-05 15:12:49 +00:00
|
|
|
rasterize(portion, true);
|
|
|
|
postProcess(portion, true);
|
|
|
|
}
|
|
|
|
|
2014-06-12 15:45:59 +00:00
|
|
|
const std::vector<Rasterizer *> &SoftwareCanvasRenderer::getRasterizers() const
|
2014-06-05 15:12:49 +00:00
|
|
|
{
|
2014-06-12 15:45:59 +00:00
|
|
|
return rasterizers;
|
|
|
|
}
|
2014-06-05 15:12:49 +00:00
|
|
|
|
2014-08-18 10:17:16 +00:00
|
|
|
const Rasterizer &SoftwareCanvasRenderer::getRasterizer(int client_id) const
|
|
|
|
{
|
|
|
|
return *(getRasterizers()[client_id]);
|
|
|
|
}
|
|
|
|
|
2014-06-12 15:45:59 +00:00
|
|
|
void SoftwareCanvasRenderer::rasterize(CanvasPortion *portion, bool threaded)
|
|
|
|
{
|
|
|
|
for (auto &rasterizer:getRasterizers())
|
2014-06-05 15:12:49 +00:00
|
|
|
{
|
2014-06-12 15:45:59 +00:00
|
|
|
rasterizer->rasterizeToCanvas(portion);
|
2014-06-05 15:12:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareCanvasRenderer::postProcess(CanvasPortion *portion, bool threaded)
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
CanvasPixelShader shader(*this, portion, chunk_size, sub_chunk_size, chunks_x, chunks_y);
|
|
|
|
ParallelWork work(&shader, units);
|
|
|
|
work.perform();
|
|
|
|
}
|
2014-06-05 15:12:49 +00:00
|
|
|
}
|