Added parallel pixel shader for second-pass rendering

This commit is contained in:
Michaël Lemaire 2014-08-18 16:04:46 +02:00
parent 0566f2bdd8
commit b5ee4c432f
7 changed files with 41 additions and 22 deletions

View file

@ -7,21 +7,33 @@
#include "CanvasFragment.h" #include "CanvasFragment.h"
#include "Rasterizer.h" #include "Rasterizer.h"
CanvasPixelShader::CanvasPixelShader(const SoftwareCanvasRenderer &renderer, CanvasPortion *portion, int chunk_size, int chunks_x, int chunks_y): CanvasPixelShader::CanvasPixelShader(const SoftwareCanvasRenderer &renderer, CanvasPortion *portion, int chunk_size, int sub_chunk_size, int chunks_x, int chunks_y):
renderer(renderer), portion(portion), chunk_size(chunk_size), chunks_x(chunks_x), chunks_y(chunks_y) renderer(renderer), portion(portion), chunk_size(chunk_size), sub_chunk_size(sub_chunk_size), chunks_x(chunks_x), chunks_y(chunks_y)
{ {
} }
int CanvasPixelShader::processParallelUnit(int unit) int CanvasPixelShader::processParallelUnit(int unit)
{ {
// Locate the chunk we work on // Locate the chunk we work on
int prev_sub_chunk_size = chunk_size * 2;
int chunk_x = unit % chunks_x; int chunk_x = unit % chunks_x;
int chunk_y = unit / chunks_x; int chunk_y = unit / chunks_x;
int base_x = chunk_x * chunk_size;
int base_y = chunk_y * chunk_size;
int limit_x = portion->getWidth() - base_x;
int limit_y = portion->getHeight() - base_y;
limit_x = (limit_x > chunk_size) ? chunk_size : limit_x;
limit_y = (limit_y > chunk_size) ? chunk_size : limit_y;
for (int x = 0; x < limit_x; x += sub_chunk_size)
{
for (int y = 0; y < limit_y; y += sub_chunk_size)
{
if (x % prev_sub_chunk_size != 0 or y % prev_sub_chunk_size != 0)
{
// Resolve the pixel color // Resolve the pixel color
int x = chunk_x * chunk_size; const CanvasPixel &pixel = portion->at(base_x + x, base_y + y);
int y = chunk_y * chunk_size;
const CanvasPixel &pixel = portion->at(x, y);
int n = pixel.getFragmentCount(); int n = pixel.getFragmentCount();
Color composite = COLOR_BLACK; Color composite = COLOR_BLACK;
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
@ -30,7 +42,10 @@ int CanvasPixelShader::processParallelUnit(int unit)
const Rasterizer &rasterizer = renderer.getRasterizer(fragment.getClient()); const Rasterizer &rasterizer = renderer.getRasterizer(fragment.getClient());
composite.mask(rasterizer.shadeFragment(fragment)); composite.mask(rasterizer.shadeFragment(fragment));
} }
portion->setColor(x, y, composite); portion->setColor(base_x + x, base_y + y, composite);
}
}
}
return 0; return 0;
} }

View file

@ -18,7 +18,7 @@ namespace software {
class CanvasPixelShader: public ParallelWorker class CanvasPixelShader: public ParallelWorker
{ {
public: public:
CanvasPixelShader(const SoftwareCanvasRenderer &renderer, CanvasPortion *portion, int chunk_size, int chunks_x, int chunks_y); CanvasPixelShader(const SoftwareCanvasRenderer &renderer, CanvasPortion *portion, int chunk_size, int sub_chunk_size, int chunks_x, int chunks_y);
virtual int processParallelUnit(int unit); virtual int processParallelUnit(int unit);
@ -26,6 +26,7 @@ private:
const SoftwareCanvasRenderer &renderer; const SoftwareCanvasRenderer &renderer;
CanvasPortion *portion; CanvasPortion *portion;
int chunk_size; int chunk_size;
int sub_chunk_size;
int chunks_x; int chunks_x;
int chunks_y; int chunks_y;
}; };

View file

@ -339,7 +339,7 @@ void Rasterizer::renderScanLines(CanvasPortion *canvas, RenderScanlines* scanlin
Color frag_color = *color; Color frag_color = *color;
if (cury == starty || cury == endy) if (cury == starty || cury == endy)
{ {
frag_color.mask(Color(0.0, 0.0, 0.0, 0.3)); frag_color.mask(Color(0.0, 0.0, 0.0, 0.1));
} }
fragment.setColor(frag_color); fragment.setColor(frag_color);

View file

@ -12,7 +12,7 @@
#define SPHERE_SIZE 20000.0 #define SPHERE_SIZE 20000.0
SkyRasterizer::SkyRasterizer(SoftwareRenderer* renderer, int client_id): SkyRasterizer::SkyRasterizer(SoftwareRenderer* renderer, int client_id):
Rasterizer(renderer, client_id, Color(0.3, 0.7, 1.0)) Rasterizer(renderer, client_id, Color(0.9, 0.9, 1.0))
{ {
} }

View file

@ -77,13 +77,16 @@ void SoftwareCanvasRenderer::rasterize(CanvasPortion *portion, bool threaded)
void SoftwareCanvasRenderer::postProcess(CanvasPortion *portion, bool threaded) void SoftwareCanvasRenderer::postProcess(CanvasPortion *portion, bool threaded)
{ {
// Subdivide in chunks // Subdivide in chunks
int chunk_size = 32; int chunk_size = 64;
int chunks_x = (portion->getWidth() - 1) / chunk_size + 1; int chunks_x = (portion->getWidth() - 1) / chunk_size + 1;
int chunks_y = (portion->getHeight() - 1) / chunk_size + 1; int chunks_y = (portion->getHeight() - 1) / chunk_size + 1;
int units = chunks_x * chunks_y; int units = chunks_x * chunks_y;
// Render chunks in parallel // Render chunks in parallel
CanvasPixelShader shader(*this, portion, chunk_size, chunks_x, chunks_y); 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); ParallelWork work(&shader, units);
work.perform(); work.perform();
}
} }

View file

@ -12,7 +12,7 @@
#include "CanvasFragment.h" #include "CanvasFragment.h"
TerrainRasterizer::TerrainRasterizer(SoftwareRenderer* renderer, int client_id): TerrainRasterizer::TerrainRasterizer(SoftwareRenderer* renderer, int client_id):
Rasterizer(renderer, client_id, Color(0.5, 0.3, 0.3)) Rasterizer(renderer, client_id, Color(1.0, 0.9, 0.9))
{ {
} }

View file

@ -6,7 +6,7 @@
#include "CanvasFragment.h" #include "CanvasFragment.h"
WaterRasterizer::WaterRasterizer(SoftwareRenderer* renderer, int client_id): WaterRasterizer::WaterRasterizer(SoftwareRenderer* renderer, int client_id):
Rasterizer(renderer, client_id, Color(0.1, 0.3, 0.6)) Rasterizer(renderer, client_id, Color(0.9, 0.95, 1.0))
{ {
} }