diff --git a/piet-gpu/shader/coarse.comp b/piet-gpu/shader/coarse.comp index 3ca7b5f..4e4ff19 100644 --- a/piet-gpu/shader/coarse.comp +++ b/piet-gpu/shader/coarse.comp @@ -42,10 +42,16 @@ shared uint sh_elements_ref; shared uint sh_bitmaps[N_SLICE][N_TILE]; +// scale factors useful for converting coordinates to tiles +#define SX (1.0 / float(TILE_WIDTH_PX)) +#define SY (1.0 / float(TILE_HEIGHT_PX)) + void main() { // Could use either linear or 2d layouts for both dispatch and // invocations within the workgroup. We'll use variables to abstract. uint bin_ix = N_TILE_X * gl_WorkGroupID.y + gl_WorkGroupID.x; + // Top left coordinates of this bin. + vec2 xy0 = vec2(N_TILE_X * TILE_WIDTH_PX * gl_WorkGroupID.x, N_TILE_Y * TILE_HEIGHT_PX * gl_WorkGroupID.y); uint th_ix = gl_LocalInvocationID.x; uint wr_ix = 0; uint rd_ix = 0; @@ -109,20 +115,90 @@ void main() { if (th_ix < chunk_n) { uint el = BinInstance_read(BinInstance_index(inst_ref, th_ix)).element_ix; sh_elements[(wr_ix + th_ix) % N_RINGBUF] = el; - probe = el; } wr_ix += chunk_n; } // We've done the merge and filled the buffer. + + // Read one element, compute coverage. uint tag = Annotated_Nop; AnnotatedRef ref; if (th_ix + rd_ix < wr_ix) { - uint element_ix = (sh_elements[rd_ix] + th_ix) % N_RINGBUF; + uint element_ix = sh_elements[(rd_ix + th_ix) % N_RINGBUF]; ref = AnnotatedRef(element_ix * Annotated_size); tag = Annotated_tag(ref); - probe = tag; } + + int x0 = 0, y0 = 0, x1 = 0, y1 = 0; + switch (tag) { + case Annotated_Line: + AnnoLineSeg line = Annotated_Line_read(ref); + x0 = int(floor((min(line.p0.x, line.p1.x) - line.stroke.x - xy0.x) * SX)); + y0 = int(floor((min(line.p0.y, line.p1.y) - line.stroke.y - xy0.y) * SY)); + x1 = int(ceil((max(line.p0.x, line.p1.x) + line.stroke.x - xy0.x) * SX)); + y1 = int(ceil((max(line.p0.y, line.p1.y) + line.stroke.y - xy0.y) * SY)); + break; + case Annotated_Fill: + case Annotated_Stroke: + // Note: we take advantage of the fact that fills and strokes + // have compatible layout. + AnnoFill fill = Annotated_Fill_read(ref); + x0 = int(floor((fill.bbox.x - xy0.x) * SX)); + y0 = int(floor((fill.bbox.y - xy0.y) * SY)); + x1 = int(ceil((fill.bbox.z - xy0.x) * SX)); + y1 = int(ceil((fill.bbox.w - xy0.y) * SY)); + break; + } + // At this point, we run an iterator over the coverage area, + // trying to keep divergence low. + // Right now, it's just a bbox, but we'll get finer with + // segments. + x0 = clamp(x0, 0, N_TILE_X); + x1 = clamp(x1, x0, N_TILE_X); + y0 = clamp(y0, 0, N_TILE_Y); + y1 = clamp(y1, y0, N_TILE_Y); + // This loop draws a rectangle to the coverage bitmasks. For + // line segments, draw more precisely. + if (x0 == x1) y1 = y0; + int x = x0, y = y0; + uint my_slice = th_ix / 32; + uint my_mask = 1 << (th_ix & 31); + while (y < y1) { + atomicOr(sh_bitmaps[my_slice][y * N_TILE_X + x], my_mask); + x++; + if (x == x1) { + x = x0; + y++; + } + } + + // Output elements for this tile, based on bitmaps. + uint slice_ix = 0; + uint bitmap = sh_bitmaps[0][th_ix]; + while (true) { + if (bitmap == 0) { + slice_ix++; + if (slice_ix == N_SLICE) { + break; + } + bitmap = sh_bitmaps[slice_ix][th_ix]; + if (bitmap == 0) { + continue; + } + } + uint element_ref_ix = slice_ix * 32 + findLSB(bitmap); + uint element_ix = sh_elements[(rd_ix + element_ref_ix) % N_RINGBUF]; + + // At this point, we read the element again from global memory. + // If that turns out to be expensive, maybe we can pack it into + // shared memory (or perhaps just the tag). + probe += 1; + + // clear LSB + bitmap &= bitmap - 1; + } + rd_ix += N_TILE; } while (wr_ix > rd_ix); ptcl[bin_ix * N_TILE + th_ix] = probe; diff --git a/piet-gpu/shader/coarse.spv b/piet-gpu/shader/coarse.spv index 6bd6f61..ded68da 100644 Binary files a/piet-gpu/shader/coarse.spv and b/piet-gpu/shader/coarse.spv differ