Merge pull request #12 from linebender/fills

Make fills work in sort-middle pipeline
This commit is contained in:
Raph Levien 2020-05-23 10:26:29 -07:00 committed by GitHub
commit b5e96b5b87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 363 additions and 94 deletions

View file

@ -3,7 +3,14 @@ use piet_gpu_derive::piet_gpu;
piet_gpu! { piet_gpu! {
#[gpu_write] #[gpu_write]
mod annotated { mod annotated {
struct AnnoLineSeg { struct AnnoFillLineSeg {
p0: [f32; 2],
p1: [f32; 2],
// A note: the layout of this struct is shared with
// AnnoStrokeLineSeg. In that case, we actually write
// [0.0, 0.0] as the stroke field, to minimize divergence.
}
struct AnnoStrokeLineSeg {
p0: [f32; 2], p0: [f32; 2],
p1: [f32; 2], p1: [f32; 2],
// halfwidth in both x and y for binning // halfwidth in both x and y for binning
@ -35,8 +42,8 @@ piet_gpu! {
} }
enum Annotated { enum Annotated {
Nop, Nop,
// The segments need a flag to indicate fill/stroke FillLine(AnnoFillLineSeg),
Line(AnnoLineSeg), StrokeLine(AnnoStrokeLineSeg),
Quad(AnnoQuadSeg), Quad(AnnoQuadSeg),
Cubic(AnnoCubicSeg), Cubic(AnnoCubicSeg),
Stroke(AnnoStroke), Stroke(AnnoStroke),

View file

@ -7,6 +7,9 @@ piet_gpu! {
mod bins { mod bins {
struct BinInstance { struct BinInstance {
element_ix: u32, element_ix: u32,
// Right edge of the bounding box of the associated fill
// element; used in backdrop computation.
right_edge: f32,
} }
struct BinChunk { struct BinChunk {

View file

@ -85,8 +85,15 @@ piet_gpu! {
} }
enum Element { enum Element {
Nop, Nop,
// The segments need a flag to indicate fill/stroke // Another approach to encoding would be to use a single
Line(LineSeg), // variant but have a bool for fill/stroke. This could be
// packed into the tag, so the on-the-wire representation
// would be very similar to what's here.
StrokeLine(LineSeg),
FillLine(LineSeg),
// Note: we'll need to handle the stroke/fill distinction
// for these as well, when we do flattening on the GPU.
Quad(QuadSeg), Quad(QuadSeg),
Cubic(CubicSeg), Cubic(CubicSeg),
Stroke(Stroke), Stroke(Stroke),

View file

@ -1,6 +1,10 @@
// Code auto-generated by piet-gpu-derive // Code auto-generated by piet-gpu-derive
struct AnnoLineSegRef { struct AnnoFillLineSegRef {
uint offset;
};
struct AnnoStrokeLineSegRef {
uint offset; uint offset;
}; };
@ -24,16 +28,27 @@ struct AnnotatedRef {
uint offset; uint offset;
}; };
struct AnnoLineSeg { struct AnnoFillLineSeg {
vec2 p0;
vec2 p1;
};
#define AnnoFillLineSeg_size 16
AnnoFillLineSegRef AnnoFillLineSeg_index(AnnoFillLineSegRef ref, uint index) {
return AnnoFillLineSegRef(ref.offset + index * AnnoFillLineSeg_size);
}
struct AnnoStrokeLineSeg {
vec2 p0; vec2 p0;
vec2 p1; vec2 p1;
vec2 stroke; vec2 stroke;
}; };
#define AnnoLineSeg_size 24 #define AnnoStrokeLineSeg_size 24
AnnoLineSegRef AnnoLineSeg_index(AnnoLineSegRef ref, uint index) { AnnoStrokeLineSegRef AnnoStrokeLineSeg_index(AnnoStrokeLineSegRef ref, uint index) {
return AnnoLineSegRef(ref.offset + index * AnnoLineSeg_size); return AnnoStrokeLineSegRef(ref.offset + index * AnnoStrokeLineSeg_size);
} }
struct AnnoQuadSeg { struct AnnoQuadSeg {
@ -87,18 +102,39 @@ AnnoStrokeRef AnnoStroke_index(AnnoStrokeRef ref, uint index) {
} }
#define Annotated_Nop 0 #define Annotated_Nop 0
#define Annotated_Line 1 #define Annotated_FillLine 1
#define Annotated_Quad 2 #define Annotated_StrokeLine 2
#define Annotated_Cubic 3 #define Annotated_Quad 3
#define Annotated_Stroke 4 #define Annotated_Cubic 4
#define Annotated_Fill 5 #define Annotated_Stroke 5
#define Annotated_Fill 6
#define Annotated_size 44 #define Annotated_size 44
AnnotatedRef Annotated_index(AnnotatedRef ref, uint index) { AnnotatedRef Annotated_index(AnnotatedRef ref, uint index) {
return AnnotatedRef(ref.offset + index * Annotated_size); return AnnotatedRef(ref.offset + index * Annotated_size);
} }
AnnoLineSeg AnnoLineSeg_read(AnnoLineSegRef ref) { AnnoFillLineSeg AnnoFillLineSeg_read(AnnoFillLineSegRef ref) {
uint ix = ref.offset >> 2;
uint raw0 = annotated[ix + 0];
uint raw1 = annotated[ix + 1];
uint raw2 = annotated[ix + 2];
uint raw3 = annotated[ix + 3];
AnnoFillLineSeg s;
s.p0 = vec2(uintBitsToFloat(raw0), uintBitsToFloat(raw1));
s.p1 = vec2(uintBitsToFloat(raw2), uintBitsToFloat(raw3));
return s;
}
void AnnoFillLineSeg_write(AnnoFillLineSegRef ref, AnnoFillLineSeg s) {
uint ix = ref.offset >> 2;
annotated[ix + 0] = floatBitsToUint(s.p0.x);
annotated[ix + 1] = floatBitsToUint(s.p0.y);
annotated[ix + 2] = floatBitsToUint(s.p1.x);
annotated[ix + 3] = floatBitsToUint(s.p1.y);
}
AnnoStrokeLineSeg AnnoStrokeLineSeg_read(AnnoStrokeLineSegRef ref) {
uint ix = ref.offset >> 2; uint ix = ref.offset >> 2;
uint raw0 = annotated[ix + 0]; uint raw0 = annotated[ix + 0];
uint raw1 = annotated[ix + 1]; uint raw1 = annotated[ix + 1];
@ -106,14 +142,14 @@ AnnoLineSeg AnnoLineSeg_read(AnnoLineSegRef ref) {
uint raw3 = annotated[ix + 3]; uint raw3 = annotated[ix + 3];
uint raw4 = annotated[ix + 4]; uint raw4 = annotated[ix + 4];
uint raw5 = annotated[ix + 5]; uint raw5 = annotated[ix + 5];
AnnoLineSeg s; AnnoStrokeLineSeg s;
s.p0 = vec2(uintBitsToFloat(raw0), uintBitsToFloat(raw1)); s.p0 = vec2(uintBitsToFloat(raw0), uintBitsToFloat(raw1));
s.p1 = vec2(uintBitsToFloat(raw2), uintBitsToFloat(raw3)); s.p1 = vec2(uintBitsToFloat(raw2), uintBitsToFloat(raw3));
s.stroke = vec2(uintBitsToFloat(raw4), uintBitsToFloat(raw5)); s.stroke = vec2(uintBitsToFloat(raw4), uintBitsToFloat(raw5));
return s; return s;
} }
void AnnoLineSeg_write(AnnoLineSegRef ref, AnnoLineSeg s) { void AnnoStrokeLineSeg_write(AnnoStrokeLineSegRef ref, AnnoStrokeLineSeg s) {
uint ix = ref.offset >> 2; uint ix = ref.offset >> 2;
annotated[ix + 0] = floatBitsToUint(s.p0.x); annotated[ix + 0] = floatBitsToUint(s.p0.x);
annotated[ix + 1] = floatBitsToUint(s.p0.y); annotated[ix + 1] = floatBitsToUint(s.p0.y);
@ -239,8 +275,12 @@ uint Annotated_tag(AnnotatedRef ref) {
return annotated[ref.offset >> 2]; return annotated[ref.offset >> 2];
} }
AnnoLineSeg Annotated_Line_read(AnnotatedRef ref) { AnnoFillLineSeg Annotated_FillLine_read(AnnotatedRef ref) {
return AnnoLineSeg_read(AnnoLineSegRef(ref.offset + 4)); return AnnoFillLineSeg_read(AnnoFillLineSegRef(ref.offset + 4));
}
AnnoStrokeLineSeg Annotated_StrokeLine_read(AnnotatedRef ref) {
return AnnoStrokeLineSeg_read(AnnoStrokeLineSegRef(ref.offset + 4));
} }
AnnoQuadSeg Annotated_Quad_read(AnnotatedRef ref) { AnnoQuadSeg Annotated_Quad_read(AnnotatedRef ref) {
@ -263,9 +303,14 @@ void Annotated_Nop_write(AnnotatedRef ref) {
annotated[ref.offset >> 2] = Annotated_Nop; annotated[ref.offset >> 2] = Annotated_Nop;
} }
void Annotated_Line_write(AnnotatedRef ref, AnnoLineSeg s) { void Annotated_FillLine_write(AnnotatedRef ref, AnnoFillLineSeg s) {
annotated[ref.offset >> 2] = Annotated_Line; annotated[ref.offset >> 2] = Annotated_FillLine;
AnnoLineSeg_write(AnnoLineSegRef(ref.offset + 4), s); AnnoFillLineSeg_write(AnnoFillLineSegRef(ref.offset + 4), s);
}
void Annotated_StrokeLine_write(AnnotatedRef ref, AnnoStrokeLineSeg s) {
annotated[ref.offset >> 2] = Annotated_StrokeLine;
AnnoStrokeLineSeg_write(AnnoStrokeLineSegRef(ref.offset + 4), s);
} }
void Annotated_Quad_write(AnnotatedRef ref, AnnoQuadSeg s) { void Annotated_Quad_write(AnnotatedRef ref, AnnoQuadSeg s) {

View file

@ -11,24 +11,35 @@ layout(set = 0, binding = 0) buffer AnnotatedBuf {
uint[] annotated; uint[] annotated;
}; };
layout(set = 0, binding = 1) buffer AllocBuf { // This is for scanning forward for right_edge data.
layout(set = 0, binding = 1) buffer StateBuf {
uint[] state;
};
layout(set = 0, binding = 2) buffer AllocBuf {
uint n_elements; uint n_elements;
// Will be incremented atomically to claim tiles // Will be incremented atomically to claim tiles
uint tile_ix; uint tile_ix;
uint alloc; uint alloc;
}; };
layout(set = 0, binding = 2) buffer BinsBuf { layout(set = 0, binding = 3) buffer BinsBuf {
uint[] bins; uint[] bins;
}; };
#include "annotated.h" #include "annotated.h"
#include "state.h"
#include "bins.h" #include "bins.h"
// scale factors useful for converting coordinates to bins // scale factors useful for converting coordinates to bins
#define SX (1.0 / float(N_TILE_X * TILE_WIDTH_PX)) #define SX (1.0 / float(N_TILE_X * TILE_WIDTH_PX))
#define SY (1.0 / float(N_TILE_Y * TILE_HEIGHT_PX)) #define SY (1.0 / float(N_TILE_Y * TILE_HEIGHT_PX))
#define TSY (1.0 / float(TILE_HEIGHT_PX))
// Constant not available in GLSL. Also consider uintBitsToFloat(0x7f800000)
#define INFINITY (1.0 / 0.0)
// Note: cudaraster has N_TILE + 1 to cut down on bank conflicts. // Note: cudaraster has N_TILE + 1 to cut down on bank conflicts.
shared uint bitmaps[N_SLICE][N_TILE]; shared uint bitmaps[N_SLICE][N_TILE];
shared uint count[N_SLICE][N_TILE]; shared uint count[N_SLICE][N_TILE];
@ -37,6 +48,14 @@ shared uint sh_chunk_start[N_TILE];
shared uint sh_chunk_end[N_TILE]; shared uint sh_chunk_end[N_TILE];
shared uint sh_chunk_jump[N_TILE]; shared uint sh_chunk_jump[N_TILE];
shared float sh_right_edge[N_TILE];
#define StateBuf_stride (8 + 2 * State_size)
uint state_right_edge_index(uint partition_ix) {
return 2 + partition_ix * (StateBuf_stride / 4);
}
void main() { void main() {
BinChunkRef chunk_ref = BinChunkRef((gl_LocalInvocationID.x * N_WG + gl_WorkGroupID.x) * BIN_INITIAL_ALLOC); BinChunkRef chunk_ref = BinChunkRef((gl_LocalInvocationID.x * N_WG + gl_WorkGroupID.x) * BIN_INITIAL_ALLOC);
uint wr_limit = chunk_ref.offset + BIN_INITIAL_ALLOC; uint wr_limit = chunk_ref.offset + BIN_INITIAL_ALLOC;
@ -65,13 +84,17 @@ void main() {
tag = Annotated_tag(ref); tag = Annotated_tag(ref);
} }
int x0 = 0, y0 = 0, x1 = 0, y1 = 0; int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
float my_right_edge = INFINITY;
bool crosses_edge = false;
switch (tag) { switch (tag) {
case Annotated_Line: case Annotated_FillLine:
AnnoLineSeg line = Annotated_Line_read(ref); case Annotated_StrokeLine:
AnnoStrokeLineSeg line = Annotated_StrokeLine_read(ref);
x0 = int(floor((min(line.p0.x, line.p1.x) - line.stroke.x) * SX)); x0 = int(floor((min(line.p0.x, line.p1.x) - line.stroke.x) * SX));
y0 = int(floor((min(line.p0.y, line.p1.y) - line.stroke.y) * SY)); y0 = int(floor((min(line.p0.y, line.p1.y) - line.stroke.y) * SY));
x1 = int(ceil((max(line.p0.x, line.p1.x) + line.stroke.x) * SX)); x1 = int(ceil((max(line.p0.x, line.p1.x) + line.stroke.x) * SX));
y1 = int(ceil((max(line.p0.y, line.p1.y) + line.stroke.y) * SY)); y1 = int(ceil((max(line.p0.y, line.p1.y) + line.stroke.y) * SY));
crosses_edge = tag == Annotated_FillLine && ceil(line.p0.y * TSY) != ceil(line.p1.y * TSY);
break; break;
case Annotated_Fill: case Annotated_Fill:
case Annotated_Stroke: case Annotated_Stroke:
@ -82,8 +105,41 @@ void main() {
y0 = int(floor(fill.bbox.y * SY)); y0 = int(floor(fill.bbox.y * SY));
x1 = int(ceil(fill.bbox.z * SX)); x1 = int(ceil(fill.bbox.z * SX));
y1 = int(ceil(fill.bbox.w * SY)); y1 = int(ceil(fill.bbox.w * SY));
// It probably makes more sense to track x1, to avoid having to redo
// the rounding to tile coords.
my_right_edge = fill.bbox.z;
break; break;
} }
// If the last element in this partition is a fill edge, then we need to do a
// look-forward to find the right edge of its corresponding fill. That data is
// recorded in aggregates computed in the element processing pass.
if (gl_LocalInvocationID.x == N_TILE - 1 && tag == Annotated_FillLine) {
uint aggregate_ix = (my_tile + 1) * ELEMENT_BINNING_RATIO;
// This is sequential but the expectation is that the amount of
// look-forward is small (performance may degrade in the case
// of massively complex paths).
do {
my_right_edge = uintBitsToFloat(state[state_right_edge_index(aggregate_ix)]);
aggregate_ix++;
} while (isinf(my_right_edge));
}
// Now propagate right_edge backward, from fill to segment.
for (uint i = 0; i < LG_N_TILE; i++) {
// Note: we could try to cut down on write bandwidth here if the value hasn't
// changed, but not sure it's worth the complexity to track.
sh_right_edge[gl_LocalInvocationID.x] = my_right_edge;
barrier();
if (gl_LocalInvocationID.x + (1 << i) < N_TILE && isinf(my_right_edge)) {
my_right_edge = sh_right_edge[gl_LocalInvocationID.x + (1 << i)];
}
barrier();
}
if (crosses_edge) {
x1 = int(ceil(my_right_edge * SX));
}
// At this point, we run an iterator over the coverage area, // At this point, we run an iterator over the coverage area,
// trying to keep divergence low. // trying to keep divergence low.
// Right now, it's just a bbox, but we'll get finer with // Right now, it's just a bbox, but we'll get finer with
@ -118,9 +174,9 @@ void main() {
uint chunk_new_start; uint chunk_new_start;
// Refactor to reduce code duplication? // Refactor to reduce code duplication?
if (chunk_n > 0) { if (chunk_n > 0) {
uint next_chunk = chunk_ref.offset + BinChunk_size + chunk_n * 4; uint next_chunk = chunk_ref.offset + BinChunk_size + chunk_n * BinInstance_size;
if (next_chunk + BinChunk_size + min(24, element_count * 4) > wr_limit) { if (next_chunk + BinChunk_size + min(24, element_count * BinInstance_size) > wr_limit) {
uint alloc_amount = max(BIN_ALLOC, BinChunk_size + element_count * 4); uint alloc_amount = max(BIN_ALLOC, BinChunk_size + element_count * BinInstance_size);
// could try to reduce fragmentation if BIN_ALLOC is only a bit above needed // could try to reduce fragmentation if BIN_ALLOC is only a bit above needed
next_chunk = atomicAdd(alloc, alloc_amount); next_chunk = atomicAdd(alloc, alloc_amount);
wr_limit = next_chunk + alloc_amount; wr_limit = next_chunk + alloc_amount;
@ -129,10 +185,10 @@ void main() {
chunk_ref = BinChunkRef(next_chunk); chunk_ref = BinChunkRef(next_chunk);
} }
BinInstanceRef instance_ref = BinInstanceRef(chunk_ref.offset + BinChunk_size); BinInstanceRef instance_ref = BinInstanceRef(chunk_ref.offset + BinChunk_size);
if (instance_ref.offset + element_count * 4 > wr_limit) { if (instance_ref.offset + element_count * BinInstance_size > wr_limit) {
chunk_end = wr_limit; chunk_end = wr_limit;
chunk_n = (wr_limit - instance_ref.offset) / 4; chunk_n = (wr_limit - instance_ref.offset) / BinInstance_size;
uint alloc_amount = max(BIN_ALLOC, BinChunk_size + (element_count - chunk_n) * 4); uint alloc_amount = max(BIN_ALLOC, BinChunk_size + (element_count - chunk_n) * BinInstance_size);
chunk_new_start = atomicAdd(alloc, alloc_amount); chunk_new_start = atomicAdd(alloc, alloc_amount);
wr_limit = chunk_new_start + alloc_amount; wr_limit = chunk_new_start + alloc_amount;
BinChunk_write(chunk_ref, BinChunk(chunk_n, BinChunkRef(chunk_new_start))); BinChunk_write(chunk_ref, BinChunk(chunk_n, BinChunkRef(chunk_new_start)));
@ -141,6 +197,7 @@ void main() {
chunk_n = element_count - chunk_n; chunk_n = element_count - chunk_n;
} else { } else {
chunk_end = ~0; chunk_end = ~0;
chunk_new_start = ~0;
chunk_n = element_count; chunk_n = element_count;
} }
sh_chunk_start[gl_LocalInvocationID.x] = instance_ref.offset; sh_chunk_start[gl_LocalInvocationID.x] = instance_ref.offset;
@ -161,11 +218,11 @@ void main() {
if (my_slice > 0) { if (my_slice > 0) {
idx += count[my_slice - 1][bin_ix]; idx += count[my_slice - 1][bin_ix];
} }
uint out_offset = sh_chunk_start[bin_ix] + idx * 4; uint out_offset = sh_chunk_start[bin_ix] + idx * BinInstance_size;
if (out_offset >= sh_chunk_end[bin_ix]) { if (out_offset >= sh_chunk_end[bin_ix]) {
out_offset += sh_chunk_jump[bin_ix]; out_offset += sh_chunk_jump[bin_ix];
} }
BinInstance_write(BinInstanceRef(out_offset), BinInstance(element_ix)); BinInstance_write(BinInstanceRef(out_offset), BinInstance(element_ix, my_right_edge));
} }
x++; x++;
if (x == x1) { if (x == x1) {

Binary file not shown.

View file

@ -10,9 +10,10 @@ struct BinChunkRef {
struct BinInstance { struct BinInstance {
uint element_ix; uint element_ix;
float right_edge;
}; };
#define BinInstance_size 4 #define BinInstance_size 8
BinInstanceRef BinInstance_index(BinInstanceRef ref, uint index) { BinInstanceRef BinInstance_index(BinInstanceRef ref, uint index) {
return BinInstanceRef(ref.offset + index * BinInstance_size); return BinInstanceRef(ref.offset + index * BinInstance_size);
@ -32,14 +33,17 @@ BinChunkRef BinChunk_index(BinChunkRef ref, uint index) {
BinInstance BinInstance_read(BinInstanceRef ref) { BinInstance BinInstance_read(BinInstanceRef ref) {
uint ix = ref.offset >> 2; uint ix = ref.offset >> 2;
uint raw0 = bins[ix + 0]; uint raw0 = bins[ix + 0];
uint raw1 = bins[ix + 1];
BinInstance s; BinInstance s;
s.element_ix = raw0; s.element_ix = raw0;
s.right_edge = uintBitsToFloat(raw1);
return s; return s;
} }
void BinInstance_write(BinInstanceRef ref, BinInstance s) { void BinInstance_write(BinInstanceRef ref, BinInstance s) {
uint ix = ref.offset >> 2; uint ix = ref.offset >> 2;
bins[ix + 0] = s.element_ix; bins[ix + 0] = s.element_ix;
bins[ix + 1] = floatBitsToUint(s.right_edge);
} }
BinChunk BinChunk_read(BinChunkRef ref) { BinChunk BinChunk_read(BinChunkRef ref) {

View file

@ -12,7 +12,7 @@ build image.spv: glsl image.comp | scene.h
build elements.spv: glsl elements.comp | scene.h state.h annotated.h build elements.spv: glsl elements.comp | scene.h state.h annotated.h
build binning.spv: glsl binning.comp | annotated.h bins.h setup.h build binning.spv: glsl binning.comp | annotated.h state.h bins.h setup.h
build coarse.spv: glsl coarse.comp | annotated.h bins.h ptcl.h setup.h build coarse.spv: glsl coarse.comp | annotated.h bins.h ptcl.h setup.h

View file

@ -30,6 +30,7 @@ layout(set = 0, binding = 3) buffer PtclBuf {
#define N_RINGBUF 512 #define N_RINGBUF 512
shared uint sh_elements[N_RINGBUF]; shared uint sh_elements[N_RINGBUF];
shared float sh_right_edge[N_RINGBUF];
shared uint sh_chunk[N_WG]; shared uint sh_chunk[N_WG];
shared uint sh_chunk_next[N_WG]; shared uint sh_chunk_next[N_WG];
shared uint sh_chunk_n[N_WG]; shared uint sh_chunk_n[N_WG];
@ -41,6 +42,8 @@ shared uint sh_selected_n;
shared uint sh_elements_ref; shared uint sh_elements_ref;
shared uint sh_bitmaps[N_SLICE][N_TILE]; shared uint sh_bitmaps[N_SLICE][N_TILE];
shared uint sh_backdrop[N_SLICE][N_TILE];
shared uint sh_bd_sign[N_SLICE];
// scale factors useful for converting coordinates to tiles // scale factors useful for converting coordinates to tiles
#define SX (1.0 / float(TILE_WIDTH_PX)) #define SX (1.0 / float(TILE_WIDTH_PX))
@ -77,6 +80,14 @@ void alloc_chunk(inout uint chunk_n_segs, inout SegChunkRef seg_chunk_ref,
} }
} }
// Accumulate delta to backdrop.
//
// Each bit for which bd_bitmap is 1 and bd_sign is 1 counts as +1, and each
// bit for which bd_bitmap is 1 and bd_sign is 0 counts as -1.
int count_backdrop(uint bd_bitmap, uint bd_sign) {
return bitCount(bd_bitmap & bd_sign) - bitCount(bd_bitmap & ~bd_sign);
}
void main() { void main() {
// Could use either linear or 2d layouts for both dispatch and // Could use either linear or 2d layouts for both dispatch and
// invocations within the workgroup. We'll use variables to abstract. // invocations within the workgroup. We'll use variables to abstract.
@ -109,10 +120,14 @@ void main() {
sh_first_el[th_ix] = chunk.n > 0 ? sh_first_el[th_ix] = chunk.n > 0 ?
BinInstance_read(BinInstanceRef(start_chunk + BinChunk_size)).element_ix : ~0; BinInstance_read(BinInstanceRef(start_chunk + BinChunk_size)).element_ix : ~0;
} }
uint count = 0; if (th_ix < N_SLICE) {
sh_bd_sign[th_ix] = 0;
}
int backdrop = 0;
while (true) { while (true) {
for (uint i = 0; i < N_SLICE; i++) { for (uint i = 0; i < N_SLICE; i++) {
sh_bitmaps[i][th_ix] = 0; sh_bitmaps[i][th_ix] = 0;
sh_backdrop[i][th_ix] = 0;
} }
while (wr_ix - rd_ix <= N_TILE) { while (wr_ix - rd_ix <= N_TILE) {
@ -157,8 +172,10 @@ void main() {
} }
BinInstanceRef inst_ref = BinInstanceRef(sh_elements_ref); BinInstanceRef inst_ref = BinInstanceRef(sh_elements_ref);
if (th_ix < chunk_n) { if (th_ix < chunk_n) {
uint el = BinInstance_read(BinInstance_index(inst_ref, th_ix)).element_ix; BinInstance inst = BinInstance_read(BinInstance_index(inst_ref, th_ix));
sh_elements[(wr_ix + th_ix) % N_RINGBUF] = el; uint wr_el_ix = (wr_ix + th_ix) % N_RINGBUF;
sh_elements[wr_el_ix] = inst.element_ix;
sh_right_edge[wr_el_ix] = inst.right_edge;
} }
wr_ix += chunk_n; wr_ix += chunk_n;
} }
@ -169,8 +186,11 @@ void main() {
// Read one element, compute coverage. // Read one element, compute coverage.
uint tag = Annotated_Nop; uint tag = Annotated_Nop;
AnnotatedRef ref; AnnotatedRef ref;
float right_edge = 0.0;
if (th_ix + rd_ix < wr_ix) { if (th_ix + rd_ix < wr_ix) {
uint element_ix = sh_elements[(rd_ix + th_ix) % N_RINGBUF]; uint rd_el_ix = (rd_ix + th_ix) % N_RINGBUF;
uint element_ix = sh_elements[rd_el_ix];
right_edge = sh_right_edge[rd_el_ix];
ref = AnnotatedRef(element_ix * Annotated_size); ref = AnnotatedRef(element_ix * Annotated_size);
tag = Annotated_tag(ref); tag = Annotated_tag(ref);
} }
@ -179,15 +199,26 @@ void main() {
float a, b, c; float a, b, c;
// Bounding box of element in pixel coordinates. // Bounding box of element in pixel coordinates.
float xmin, xmax, ymin, ymax; float xmin, xmax, ymin, ymax;
uint my_slice = th_ix / 32;
uint my_mask = 1 << (th_ix & 31);
switch (tag) { switch (tag) {
case Annotated_Line: case Annotated_FillLine:
AnnoLineSeg line = Annotated_Line_read(ref); case Annotated_StrokeLine:
AnnoStrokeLineSeg line = Annotated_StrokeLine_read(ref);
xmin = min(line.p0.x, line.p1.x) - line.stroke.x; xmin = min(line.p0.x, line.p1.x) - line.stroke.x;
xmax = max(line.p0.x, line.p1.x) + line.stroke.x; xmax = max(line.p0.x, line.p1.x) + line.stroke.x;
ymin = min(line.p0.y, line.p1.y) - line.stroke.y; ymin = min(line.p0.y, line.p1.y) - line.stroke.y;
ymax = max(line.p0.y, line.p1.y) + line.stroke.y; ymax = max(line.p0.y, line.p1.y) + line.stroke.y;
float dx = line.p1.x - line.p0.x; float dx = line.p1.x - line.p0.x;
float dy = line.p1.y - line.p0.y; float dy = line.p1.y - line.p0.y;
if (tag == Annotated_FillLine) {
// Set bit for backdrop sign calculation, 1 is +1, 0 is -1.
if (dy < 0) {
atomicOr(sh_bd_sign[my_slice], my_mask);
} else {
atomicAnd(sh_bd_sign[my_slice], ~my_mask);
}
}
// Set up for per-scanline coverage formula, below. // Set up for per-scanline coverage formula, below.
float invslope = abs(dy) < 1e-9 ? 1e9 : dx / dy; float invslope = abs(dy) < 1e-9 ? 1e9 : dx / dy;
c = abs(invslope) * (0.5 * float(TILE_HEIGHT_PX) + line.stroke.y) * SX; c = abs(invslope) * (0.5 * float(TILE_HEIGHT_PX) + line.stroke.y) * SX;
@ -214,20 +245,20 @@ void main() {
break; break;
} }
// Draw the coverage area into the bitmaks. This uses an algorithm // Draw the coverage area into the bitmasks. This uses an algorithm
// that computes the coverage of a span for given scanline. // that computes the coverage of a span for given scanline.
// Compute bounding box in tiles and clip to this bin. // Compute bounding box in tiles and clip to this bin.
int x0 = int(floor((xmin - xy0.x) * SX)); int x0 = int(floor((xmin - xy0.x) * SX));
int x1 = int(ceil((xmax - xy0.x) * SX)); int x1 = int(ceil((xmax - xy0.x) * SX));
int xr = int(ceil((right_edge - xy0.x) * SX));
int y0 = int(floor((ymin - xy0.y) * SY)); int y0 = int(floor((ymin - xy0.y) * SY));
int y1 = int(ceil((ymax - xy0.y) * SY)); int y1 = int(ceil((ymax - xy0.y) * SY));
x0 = clamp(x0, 0, N_TILE_X); x0 = clamp(x0, 0, N_TILE_X);
x1 = clamp(x1, x0, N_TILE_X); x1 = clamp(x1, x0, N_TILE_X);
xr = clamp(xr, 0, N_TILE_X);
y0 = clamp(y0, 0, N_TILE_Y); y0 = clamp(y0, 0, N_TILE_Y);
y1 = clamp(y1, y0, N_TILE_Y); y1 = clamp(y1, y0, N_TILE_Y);
uint my_slice = th_ix / 32;
uint my_mask = 1 << (th_ix & 31);
float t = a + b * float(y0); float t = a + b * float(y0);
for (uint y = y0; y < y1; y++) { for (uint y = y0; y < y1; y++) {
uint xx0 = clamp(int(floor(t - c)), x0, x1); uint xx0 = clamp(int(floor(t - c)), x0, x1);
@ -235,6 +266,15 @@ void main() {
for (uint x = xx0; x < xx1; x++) { for (uint x = xx0; x < xx1; x++) {
atomicOr(sh_bitmaps[my_slice][y * N_TILE_X + x], my_mask); atomicOr(sh_bitmaps[my_slice][y * N_TILE_X + x], my_mask);
} }
if (tag == Annotated_FillLine && ymin <= xy0.y + float(y * TILE_HEIGHT_PX)) {
// Assign backdrop to all tiles to the right of the ray crossing the
// top edge of this tile, up to the right edge of the fill bbox.
float xray = t - 0.5 * b;
xx0 = max(int(ceil(xray)), 0);
for (uint x = xx0; x < xr; x++) {
atomicOr(sh_backdrop[my_slice][y * N_TILE_X + x], my_mask);
}
}
t += b; t += b;
} }
barrier(); barrier();
@ -242,13 +282,18 @@ void main() {
// Output elements for this tile, based on bitmaps. // Output elements for this tile, based on bitmaps.
uint slice_ix = 0; uint slice_ix = 0;
uint bitmap = sh_bitmaps[0][th_ix]; uint bitmap = sh_bitmaps[0][th_ix];
uint bd_bitmap = sh_backdrop[0][th_ix];
uint bd_sign = sh_bd_sign[0];
while (true) { while (true) {
if (bitmap == 0) { if (bitmap == 0) {
backdrop += count_backdrop(bd_bitmap, bd_sign);
slice_ix++; slice_ix++;
if (slice_ix == N_SLICE) { if (slice_ix == N_SLICE) {
break; break;
} }
bitmap = sh_bitmaps[slice_ix][th_ix]; bitmap = sh_bitmaps[slice_ix][th_ix];
bd_bitmap = sh_backdrop[slice_ix][th_ix];
bd_sign = sh_bd_sign[slice_ix];
if (bitmap == 0) { if (bitmap == 0) {
continue; continue;
} }
@ -256,6 +301,13 @@ void main() {
uint element_ref_ix = slice_ix * 32 + findLSB(bitmap); uint element_ref_ix = slice_ix * 32 + findLSB(bitmap);
uint element_ix = sh_elements[(rd_ix + element_ref_ix) % N_RINGBUF]; uint element_ix = sh_elements[(rd_ix + element_ref_ix) % N_RINGBUF];
// Bits up to and including the lsb
uint bd_mask = (bitmap - 1) ^ bitmap;
backdrop += count_backdrop(bd_bitmap & bd_mask, bd_sign);
// Clear bits that have been consumed.
bd_bitmap &= ~bd_mask;
bitmap &= ~bd_mask;
// At this point, we read the element again from global memory. // At this point, we read the element again from global memory.
// If that turns out to be expensive, maybe we can pack it into // If that turns out to be expensive, maybe we can pack it into
// shared memory (or perhaps just the tag). // shared memory (or perhaps just the tag).
@ -263,15 +315,58 @@ void main() {
tag = Annotated_tag(ref); tag = Annotated_tag(ref);
switch (tag) { switch (tag) {
case Annotated_Line: case Annotated_FillLine:
AnnoLineSeg line = Annotated_Line_read(ref); AnnoFillLineSeg fill_line = Annotated_FillLine_read(ref);
// This is basically the same logic as piet-metal, but should be made numerically robust.
vec2 tile_xy = vec2(tile_x * TILE_WIDTH_PX, tile_y * TILE_HEIGHT_PX);
float yEdge = mix(fill_line.p0.y, fill_line.p1.y, (tile_xy.x - fill_line.p0.x) / (fill_line.p1.x - fill_line.p0.x));
if (min(fill_line.p0.x, fill_line.p1.x) < tile_xy.x && yEdge >= tile_xy.y && yEdge < tile_xy.y + TILE_HEIGHT_PX) {
Segment edge_seg;
if (fill_line.p0.x > fill_line.p1.x) {
fill_line.p1 = vec2(tile_xy.x, yEdge);
edge_seg.start = fill_line.p1;
edge_seg.end = vec2(tile_xy.x, tile_xy.y + TILE_HEIGHT_PX);
} else {
fill_line.p0 = vec2(tile_xy.x, yEdge);
edge_seg.start = vec2(tile_xy.x, tile_xy.y + TILE_HEIGHT_PX);
edge_seg.end = fill_line.p0;
}
alloc_chunk(chunk_n_segs, seg_chunk_ref, first_seg_chunk, seg_limit);
Segment_write(SegmentRef(seg_chunk_ref.offset + SegChunk_size + Segment_size * chunk_n_segs), edge_seg);
chunk_n_segs++;
}
Segment fill_seg = Segment(fill_line.p0, fill_line.p1);
alloc_chunk(chunk_n_segs, seg_chunk_ref, first_seg_chunk, seg_limit);
Segment_write(SegmentRef(seg_chunk_ref.offset + SegChunk_size + Segment_size * chunk_n_segs), fill_seg);
chunk_n_segs++;
break;
case Annotated_StrokeLine:
AnnoStrokeLineSeg line = Annotated_StrokeLine_read(ref);
Segment seg = Segment(line.p0, line.p1); Segment seg = Segment(line.p0, line.p1);
alloc_chunk(chunk_n_segs, seg_chunk_ref, first_seg_chunk, seg_limit); alloc_chunk(chunk_n_segs, seg_chunk_ref, first_seg_chunk, seg_limit);
Segment_write(SegmentRef(seg_chunk_ref.offset + SegChunk_size + Segment_size * chunk_n_segs), seg); Segment_write(SegmentRef(seg_chunk_ref.offset + SegChunk_size + Segment_size * chunk_n_segs), seg);
chunk_n_segs++; chunk_n_segs++;
break; break;
case Annotated_Fill: case Annotated_Fill:
chunk_n_segs = 0; if (chunk_n_segs > 0) {
AnnoFill fill = Annotated_Fill_read(ref);
SegChunk_write(seg_chunk_ref, SegChunk(chunk_n_segs, SegChunkRef(0)));
seg_chunk_ref.offset += SegChunk_size + Segment_size * chunk_n_segs;
CmdFill cmd_fill;
cmd_fill.seg_ref = first_seg_chunk.offset;
cmd_fill.backdrop = backdrop;
cmd_fill.rgba_color = fill.rgba_color;
alloc_cmd(cmd_ref, cmd_limit);
Cmd_Fill_write(cmd_ref, cmd_fill);
cmd_ref.offset += Cmd_size;
chunk_n_segs = 0;
} else if (backdrop != 0) {
AnnoFill fill = Annotated_Fill_read(ref);
alloc_cmd(cmd_ref, cmd_limit);
Cmd_Solid_write(cmd_ref, CmdSolid(fill.rgba_color));
cmd_ref.offset += Cmd_size;
}
backdrop = 0;
break; break;
case Annotated_Stroke: case Annotated_Stroke:
if (chunk_n_segs > 0) { if (chunk_n_segs > 0) {
@ -289,9 +384,6 @@ void main() {
} }
break; break;
} }
// clear LSB
bitmap &= bitmap - 1;
} }
barrier(); barrier();

Binary file not shown.

View file

@ -10,7 +10,7 @@
#define N_ROWS 4 #define N_ROWS 4
#define WG_SIZE 32 #define WG_SIZE 32
#define LG_WG_SIZE 5 #define LG_WG_SIZE 5
#define TILE_SIZE (WG_SIZE * N_ROWS) #define PARTITION_SIZE (WG_SIZE * N_ROWS)
layout(local_size_x = WG_SIZE, local_size_y = 1) in; layout(local_size_x = WG_SIZE, local_size_y = 1) in;
@ -34,14 +34,14 @@ layout(set = 0, binding = 2) buffer AnnotatedBuf {
#include "state.h" #include "state.h"
#include "annotated.h" #include "annotated.h"
#define StateBuf_stride (4 + 2 * State_size) #define StateBuf_stride (8 + 2 * State_size)
StateRef state_aggregate_ref(uint partition_ix) { StateRef state_aggregate_ref(uint partition_ix) {
return StateRef(8 + partition_ix * StateBuf_stride); return StateRef(12 + partition_ix * StateBuf_stride);
} }
StateRef state_prefix_ref(uint partition_ix) { StateRef state_prefix_ref(uint partition_ix) {
return StateRef(8 + partition_ix * StateBuf_stride + State_size); return StateRef(12 + partition_ix * StateBuf_stride + State_size);
} }
uint state_flag_index(uint partition_ix) { uint state_flag_index(uint partition_ix) {
@ -86,7 +86,7 @@ State combine_state(State a, State b) {
return c; return c;
} }
State map_element(ElementRef ref) { State map_element(ElementRef ref, inout bool is_fill) {
// TODO: it would *probably* be more efficient to make the memory read patterns less // TODO: it would *probably* be more efficient to make the memory read patterns less
// divergent, though it would be more wasted memory. // divergent, though it would be more wasted memory.
uint tag = Element_tag(ref); uint tag = Element_tag(ref);
@ -96,9 +96,11 @@ State map_element(ElementRef ref) {
c.translate = vec2(0.0, 0.0); c.translate = vec2(0.0, 0.0);
c.linewidth = 1.0; // TODO should be 0.0 c.linewidth = 1.0; // TODO should be 0.0
c.flags = 0; c.flags = 0;
is_fill = false;
switch (tag) { switch (tag) {
case Element_Line: case Element_FillLine:
LineSeg line = Element_Line_read(ref); case Element_StrokeLine:
LineSeg line = Element_FillLine_read(ref);
c.bbox.xy = min(line.p0, line.p1); c.bbox.xy = min(line.p0, line.p1);
c.bbox.zw = max(line.p0, line.p1); c.bbox.zw = max(line.p0, line.p1);
break; break;
@ -113,6 +115,8 @@ State map_element(ElementRef ref) {
c.bbox.zw = max(max(cubic.p0, cubic.p1), max(cubic.p2, cubic.p3)); c.bbox.zw = max(max(cubic.p0, cubic.p1), max(cubic.p2, cubic.p3));
break; break;
case Element_Fill: case Element_Fill:
is_fill = true;
// fall-through
case Element_Stroke: case Element_Stroke:
c.flags = FLAG_RESET_BBOX; c.flags = FLAG_RESET_BBOX;
break; break;
@ -145,6 +149,8 @@ shared vec4 sh_bbox[WG_SIZE];
shared float sh_width[WG_SIZE]; shared float sh_width[WG_SIZE];
shared uint sh_flags[WG_SIZE]; shared uint sh_flags[WG_SIZE];
shared uint sh_min_fill;
shared uint sh_tile_ix; shared uint sh_tile_ix;
shared State sh_prefix; shared State sh_prefix;
@ -154,19 +160,27 @@ void main() {
// 4.4 of prefix sum paper). // 4.4 of prefix sum paper).
if (gl_LocalInvocationID.x == 0) { if (gl_LocalInvocationID.x == 0) {
sh_tile_ix = atomicAdd(state[0], 1); sh_tile_ix = atomicAdd(state[0], 1);
sh_min_fill = ~0;
} }
barrier(); barrier();
uint tile_ix = sh_tile_ix; uint tile_ix = sh_tile_ix;
uint ix = tile_ix * TILE_SIZE + gl_LocalInvocationID.x * N_ROWS; uint ix = tile_ix * PARTITION_SIZE + gl_LocalInvocationID.x * N_ROWS;
ElementRef ref = ElementRef(ix * Element_size); ElementRef ref = ElementRef(ix * Element_size);
th_state[0] = map_element(ref); bool is_fill;
uint my_min_fill = ~0;
th_state[0] = map_element(ref, is_fill);
if (is_fill) my_min_fill = ix;
for (uint i = 1; i < N_ROWS; i++) { for (uint i = 1; i < N_ROWS; i++) {
// discussion question: would it be faster to load using more coherent patterns // discussion question: would it be faster to load using more coherent patterns
// into thread memory? This is kinda strided. // into thread memory? This is kinda strided.
th_state[i] = combine_state(th_state[i - 1], map_element(Element_index(ref, i))); th_state[i] = combine_state(th_state[i - 1], map_element(Element_index(ref, i), is_fill));
if (is_fill && my_min_fill == ~0) {
my_min_fill = ix + i;
}
} }
atomicMin(sh_min_fill, my_min_fill);
State agg = th_state[N_ROWS - 1]; State agg = th_state[N_ROWS - 1];
sh_mat[gl_LocalInvocationID.x] = agg.mat; sh_mat[gl_LocalInvocationID.x] = agg.mat;
sh_translate[gl_LocalInvocationID.x] = agg.translate; sh_translate[gl_LocalInvocationID.x] = agg.translate;
@ -238,6 +252,7 @@ void main() {
} }
} }
barrier(); barrier();
my_min_fill = sh_min_fill;
if (tile_ix != 0) { if (tile_ix != 0) {
exclusive = sh_prefix; exclusive = sh_prefix;
} }
@ -253,8 +268,14 @@ void main() {
other.flags = sh_flags[ix]; other.flags = sh_flags[ix];
row = combine_state(row, other); row = combine_state(row, other);
} }
if (my_min_fill == ~0 && gl_LocalInvocationID.x == 0) {
state[state_flag_index(tile_ix) + 1] = 0x7f800000; // infinity
}
for (uint i = 0; i < N_ROWS; i++) { for (uint i = 0; i < N_ROWS; i++) {
State st = combine_state(row, th_state[i]); State st = combine_state(row, th_state[i]);
if (my_min_fill == ix + i) {
state[state_flag_index(tile_ix) + 1] = floatBitsToUint(st.bbox.z);
}
// We write the state now for development purposes, but the // We write the state now for development purposes, but the
// actual goal is to write transformed and annotated elements. // actual goal is to write transformed and annotated elements.
//State_write(StateRef((ix + i) * State_size), st); //State_write(StateRef((ix + i) * State_size), st);
@ -266,13 +287,22 @@ void main() {
AnnotatedRef out_ref = AnnotatedRef((ix + i) * Annotated_size); AnnotatedRef out_ref = AnnotatedRef((ix + i) * Annotated_size);
uint tag = Element_tag(this_ref); uint tag = Element_tag(this_ref);
switch (tag) { switch (tag) {
case Element_Line: case Element_FillLine:
LineSeg line = Element_Line_read(this_ref); case Element_StrokeLine:
AnnoLineSeg anno_line; LineSeg line = Element_StrokeLine_read(this_ref);
AnnoStrokeLineSeg anno_line;
anno_line.p0 = st.mat.xz * line.p0.x + st.mat.yw * line.p0.y + st.translate; anno_line.p0 = st.mat.xz * line.p0.x + st.mat.yw * line.p0.y + st.translate;
anno_line.p1 = st.mat.xz * line.p1.x + st.mat.yw * line.p1.y + st.translate; anno_line.p1 = st.mat.xz * line.p1.x + st.mat.yw * line.p1.y + st.translate;
anno_line.stroke = get_linewidth(st); if (tag == Element_StrokeLine) {
Annotated_Line_write(out_ref, anno_line); anno_line.stroke = get_linewidth(st);
} else {
anno_line.stroke = vec2(0.0);
}
// We do encoding a bit by hand to minimize divergence. Another approach
// would be to have a fill/stroke bool.
uint out_tag = tag == Element_FillLine ? Annotated_FillLine : Annotated_StrokeLine;
annotated[out_ref.offset >> 2] = out_tag;
AnnoStrokeLineSeg_write(AnnoStrokeLineSegRef(out_ref.offset + 4), anno_line);
break; break;
case Element_Stroke: case Element_Stroke:
Stroke stroke = Element_Stroke_read(this_ref); Stroke stroke = Element_Stroke_read(this_ref);

Binary file not shown.

View file

@ -238,13 +238,14 @@ TransformRef Transform_index(TransformRef ref, uint index) {
} }
#define Element_Nop 0 #define Element_Nop 0
#define Element_Line 1 #define Element_StrokeLine 1
#define Element_Quad 2 #define Element_FillLine 2
#define Element_Cubic 3 #define Element_Quad 3
#define Element_Stroke 4 #define Element_Cubic 4
#define Element_Fill 5 #define Element_Stroke 5
#define Element_SetLineWidth 6 #define Element_Fill 6
#define Element_Transform 7 #define Element_SetLineWidth 7
#define Element_Transform 8
#define Element_size 36 #define Element_size 36
ElementRef Element_index(ElementRef ref, uint index) { ElementRef Element_index(ElementRef ref, uint index) {
@ -446,7 +447,11 @@ uint Element_tag(ElementRef ref) {
return scene[ref.offset >> 2]; return scene[ref.offset >> 2];
} }
LineSeg Element_Line_read(ElementRef ref) { LineSeg Element_StrokeLine_read(ElementRef ref) {
return LineSeg_read(LineSegRef(ref.offset + 4));
}
LineSeg Element_FillLine_read(ElementRef ref) {
return LineSeg_read(LineSegRef(ref.offset + 4)); return LineSeg_read(LineSegRef(ref.offset + 4));
} }

View file

@ -51,9 +51,14 @@
#define N_TILE_X 16 #define N_TILE_X 16
#define N_TILE_Y 16 #define N_TILE_Y 16
#define N_TILE (N_TILE_X * N_TILE_Y) #define N_TILE (N_TILE_X * N_TILE_Y)
#define LG_N_TILE 8
#define N_SLICE (N_TILE / 32) #define N_SLICE (N_TILE / 32)
// Number of workgroups for binning kernel // Number of workgroups for binning kernel
#define N_WG 16 #define N_WG 16
// This is the ratio of the number of elements in a binning workgroup
// over the number of elements in a partition workgroup.
#define ELEMENT_BINNING_RATIO 2
#define BIN_INITIAL_ALLOC 64 #define BIN_INITIAL_ALLOC 64
#define BIN_ALLOC 256 #define BIN_ALLOC 256

View file

@ -185,10 +185,10 @@ impl<D: Device> Renderer<D> {
]) ])
?; ?;
let bin_code = include_bytes!("../shader/binning.spv"); let bin_code = include_bytes!("../shader/binning.spv");
let bin_pipeline = device.create_simple_compute_pipeline(bin_code, 3, 0)?; let bin_pipeline = device.create_simple_compute_pipeline(bin_code, 4, 0)?;
let bin_ds = device.create_descriptor_set( let bin_ds = device.create_descriptor_set(
&bin_pipeline, &bin_pipeline,
&[&anno_buf, &bin_alloc_buf_dev, &bin_buf], &[&anno_buf, &state_buf, &bin_alloc_buf_dev, &bin_buf],
&[], &[],
)?; )?;

View file

@ -61,8 +61,8 @@ impl PicoSvg {
for item in &self.items { for item in &self.items {
match item { match item {
Item::Fill(fill_item) => { Item::Fill(fill_item) => {
//rc.fill(&fill_item.path, &fill_item.color); rc.fill(&fill_item.path, &fill_item.color);
rc.stroke(&fill_item.path, &fill_item.color, 1.0); //rc.stroke(&fill_item.path, &fill_item.color, 1.0);
} }
Item::Stroke(stroke_item) => { Item::Stroke(stroke_item) => {
rc.stroke(&stroke_item.path, &stroke_item.color, stroke_item.width); rc.stroke(&stroke_item.path, &stroke_item.color, stroke_item.width);

View file

@ -94,7 +94,7 @@ impl RenderContext for PietGpuRenderContext {
} }
let brush = brush.make_brush(self, || shape.bounding_box()).into_owned(); let brush = brush.make_brush(self, || shape.bounding_box()).into_owned();
let path = shape.to_bez_path(TOLERANCE); let path = shape.to_bez_path(TOLERANCE);
self.encode_path(path); self.encode_path(path, false);
match brush { match brush {
PietGpuBrush::Solid(rgba_color) => { PietGpuBrush::Solid(rgba_color) => {
let stroke = Stroke { rgba_color }; let stroke = Stroke { rgba_color };
@ -116,7 +116,7 @@ impl RenderContext for PietGpuRenderContext {
fn fill(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>) { fn fill(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>) {
let brush = brush.make_brush(self, || shape.bounding_box()).into_owned(); let brush = brush.make_brush(self, || shape.bounding_box()).into_owned();
let path = shape.to_bez_path(TOLERANCE); let path = shape.to_bez_path(TOLERANCE);
self.encode_path(path); self.encode_path(path, true);
match brush { match brush {
PietGpuBrush::Solid(rgba_color) => { PietGpuBrush::Solid(rgba_color) => {
let fill = Fill { rgba_color }; let fill = Fill { rgba_color };
@ -198,7 +198,15 @@ impl RenderContext for PietGpuRenderContext {
} }
impl PietGpuRenderContext { impl PietGpuRenderContext {
fn encode_path(&mut self, path: impl Iterator<Item = PathEl>) { fn encode_line_seg(&mut self, seg: LineSeg, is_fill: bool) {
if is_fill {
self.elements.push(Element::FillLine(seg));
} else {
self.elements.push(Element::StrokeLine(seg));
}
}
fn encode_path(&mut self, path: impl Iterator<Item = PathEl>, is_fill: bool) {
let flatten = true; let flatten = true;
if flatten { if flatten {
let mut start_pt = None; let mut start_pt = None;
@ -207,6 +215,7 @@ impl PietGpuRenderContext {
match el { match el {
PathEl::MoveTo(p) => { PathEl::MoveTo(p) => {
let scene_pt = to_f32_2(p); let scene_pt = to_f32_2(p);
start_pt = Some(scene_pt);
last_pt = Some(scene_pt); last_pt = Some(scene_pt);
} }
PathEl::LineTo(p) => { PathEl::LineTo(p) => {
@ -215,16 +224,18 @@ impl PietGpuRenderContext {
p0: last_pt.unwrap(), p0: last_pt.unwrap(),
p1: scene_pt, p1: scene_pt,
}; };
self.elements.push(Element::Line(seg)); self.encode_line_seg(seg, is_fill);
last_pt = Some(scene_pt); last_pt = Some(scene_pt);
} }
PathEl::ClosePath => { PathEl::ClosePath => {
if let (Some(start), Some(last)) = (start_pt.take(), last_pt.take()) { if let (Some(start), Some(last)) = (start_pt.take(), last_pt.take()) {
let seg = LineSeg { if last != start {
p0: last, let seg = LineSeg {
p1: start, p0: last,
}; p1: start,
self.elements.push(Element::Line(seg)); };
self.encode_line_seg(seg, is_fill);
}
} }
} }
_ => (), _ => (),
@ -238,6 +249,7 @@ impl PietGpuRenderContext {
match el { match el {
PathEl::MoveTo(p) => { PathEl::MoveTo(p) => {
let scene_pt = to_f32_2(p); let scene_pt = to_f32_2(p);
start_pt = Some(scene_pt);
last_pt = Some(scene_pt); last_pt = Some(scene_pt);
} }
PathEl::LineTo(p) => { PathEl::LineTo(p) => {
@ -246,7 +258,7 @@ impl PietGpuRenderContext {
p0: last_pt.unwrap(), p0: last_pt.unwrap(),
p1: scene_pt, p1: scene_pt,
}; };
self.elements.push(Element::Line(seg)); self.encode_line_seg(seg, is_fill);
last_pt = Some(scene_pt); last_pt = Some(scene_pt);
} }
PathEl::QuadTo(p1, p2) => { PathEl::QuadTo(p1, p2) => {
@ -275,11 +287,13 @@ impl PietGpuRenderContext {
} }
PathEl::ClosePath => { PathEl::ClosePath => {
if let (Some(start), Some(last)) = (start_pt.take(), last_pt.take()) { if let (Some(start), Some(last)) = (start_pt.take(), last_pt.take()) {
let seg = LineSeg { if last != start {
p0: last, let seg = LineSeg {
p1: start, p0: last,
}; p1: start,
self.elements.push(Element::Line(seg)); };
self.encode_line_seg(seg, is_fill);
}
} }
} }
} }