paysages3d/src/render/software/CanvasPixelShader.cpp

65 lines
2.5 KiB
C++
Raw Normal View History

2014-08-18 10:17:16 +00:00
#include "CanvasPixelShader.h"
#include "Color.h"
#include "SoftwareCanvasRenderer.h"
#include "CanvasPortion.h"
#include "CanvasPixel.h"
#include "CanvasFragment.h"
#include "Rasterizer.h"
#include "RenderProgress.h"
2014-08-18 10:17:16 +00:00
CanvasPixelShader::CanvasPixelShader(const SoftwareCanvasRenderer &renderer, CanvasPortion *portion,
RenderProgress *progress, int chunk_size, int sub_chunk_size, int chunks_x,
int chunks_y)
: renderer(renderer), portion(portion), progress(progress), chunk_size(chunk_size), sub_chunk_size(sub_chunk_size),
chunks_x(chunks_x), chunks_y(chunks_y) {
2014-08-18 10:17:16 +00:00
}
void CanvasPixelShader::processParallelUnit(int unit) {
2014-08-18 10:17:16 +00:00
// Locate the chunk we work on
int prev_sub_chunk_size = sub_chunk_size * 2;
2014-08-18 14:25:44 +00:00
int chunk_x = unit / chunks_y;
int chunk_y = unit % chunks_y;
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;
2014-08-18 10:17:16 +00:00
limit_x = (limit_x > chunk_size) ? chunk_size : limit_x;
limit_y = (limit_y > chunk_size) ? chunk_size : limit_y;
2014-08-18 14:25:44 +00:00
// Iterate on sub-chunks
for (int x = 0; x < limit_x; x += sub_chunk_size) {
int done = 0;
for (int y = 0; y < limit_y; y += sub_chunk_size) {
if (interrupted) {
return;
2014-08-19 07:18:55 +00:00
}
if (sub_chunk_size == chunk_size or x % prev_sub_chunk_size != 0 or y % prev_sub_chunk_size != 0) {
// Resolve the pixel color
const CanvasPixel &pixel = portion->at(base_x + x, base_y + y);
int n = pixel.getFragmentCount();
Color composite = COLOR_BLACK;
const CanvasFragment *previous = NULL;
for (int i = 0; i < n; i++) {
const CanvasFragment &fragment = pixel.getFragment(i);
const Rasterizer &rasterizer = renderer.getRasterizer(fragment.getClient());
composite.mask(rasterizer.shadeFragment(fragment, previous));
previous = &fragment;
}
2014-08-18 14:25:44 +00:00
// Fill the square area
for (int fx = 0; fx + x < limit_x and fx < sub_chunk_size; fx++) {
for (int fy = 0; fy + y < limit_y and fy < sub_chunk_size; fy++) {
2014-08-18 14:25:44 +00:00
portion->setColor(base_x + x + fx, base_y + y + fy, composite);
}
}
done++;
}
}
progress->add(done);
2014-08-18 10:17:16 +00:00
}
}