Skip to content

Commit

Permalink
Remove experimental tile rendering code
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Oct 11, 2023
1 parent 2945e20 commit 93e8366
Show file tree
Hide file tree
Showing 10 changed files with 0 additions and 936 deletions.
103 changes: 0 additions & 103 deletions Sources/vger/commands.h

This file was deleted.

6 changes: 0 additions & 6 deletions Sources/vger/include/vger.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ void vgerCubicApproxTo(vgerContext vg, vector_float2 b, vector_float2 c, vector_
/// Returns false if there were too many primitives to render the fill.
bool vgerFill(vgerContext, vgerPaintIndex paint);

/// Fills the current path (for experimental tile rendering).
void vgerFillForTile(vgerContext vg, vgerPaintIndex paint);

#pragma mark - Transforms

/// Translates current coordinate system.
Expand Down Expand Up @@ -197,9 +194,6 @@ void vgerEncode(vgerContext, id<MTLCommandBuffer> buf, MTLRenderPassDescriptor*
/// Encode drawing commands to a metal command buffer for the glow pass.
void vgerEncodeGlowPass(vgerContext, id<MTLCommandBuffer> buf, MTLRenderPassDescriptor* pass);

/// Experimental!
void vgerEncodeTileRender(vgerContext vg, id<MTLCommandBuffer> buf, id<MTLTexture> renderTexture);

/// For debugging.
id<MTLTexture> vgerGetGlyphAtlas(vgerContext);

Expand Down
209 changes: 0 additions & 209 deletions Sources/vger/vger.metal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ using namespace metal;

#include "include/vger.h"
#include "sdf.h"
#include "commands.h"
#include "paint.h"

#define SQRT_2 1.414213562373095
Expand Down Expand Up @@ -157,211 +156,3 @@ fragment float4 vger_fragment(VertexOut in [[ stage_in ]],
return mix(float4(color.rgb,0.0), color, 1.0-smoothstep(-fw/2,fw/2,d) );

}

kernel void vger_tile_clear(device uint *tileLengths [[buffer(0)]],
uint2 gid [[thread_position_in_grid]]) {

tileLengths[gid.y * MAX_TILES_WIDTH + gid.x] = 0;
}

fragment float4 vger_tile_fragment(VertexOut in [[ stage_in ]],
const device vgerPrim* prims,
const device float2* cvs,
const device vgerPaint* paints,
device Tile *tiles,
device uint *tileLengths [[ /*raster_order_group(0)*/ ]]) {

device auto& prim = prims[in.primIndex];
uint x = (uint) in.position.x;
uint y = MAX_TILES_WIDTH - (uint) in.position.y - 1;
uint tileIx = y * MAX_TILES_WIDTH + x;

// Always accessing tileLengths seems to work around the compiler bug.
uint length = tileLengths[tileIx];

device Tile& tile = tiles[tileIx];

switch(prim.type) {
case vgerRect:
tile.append(vgerCmdRect{vgerOpRect, prim.cvs[0], prim.cvs[1], prim.radius}, length);
break;
case vgerPathFill:
for(int i=0; i<prim.count; i++) {
int j = prim.start + 3*i;
auto a = cvs[j];
auto b = cvs[j+1];
auto c = cvs[j+2];

auto m = in.t.y - TILE_SIZE_PIXELS/2;
if(a.y < m and b.y < m and c.y < m) {
continue;
}
m = in.t.y + TILE_SIZE_PIXELS/2;
if(a.y > m and b.y > m and c.y > m) {
continue;
}

m = in.t.x - TILE_SIZE_PIXELS/2;
if(a.x < m and b.x < m and c.x < m) {
continue;
}

tile.append(vgerCmdBezFillIndirect{vgerOpBezIndirect,j}, length);
}
break;
case vgerSegment:
tile.append(vgerCmdSegment{vgerOpSegment, prim.cvs[0], prim.cvs[1], prim.width}, length);
break;
case vgerCircle:
tile.append(vgerCmdCircle{vgerOpCircle, prim.cvs[0], prim.radius}, length);
break;
case vgerBezier:
tile.append(vgerCmdBezStroke{vgerOpBezStroke, prim.cvs[0], prim.cvs[1], prim.cvs[2], prim.width}, length);
default:
break;
}

tile.append(vgerCmdSolid{vgerOpSolid,
pack_float_to_srgb_unorm4x8(paints[prim.paint].innerColor)},
length);

tileLengths[tileIx] = length;

// This is just for debugging so we can see what was rendered
// in the coarse rasterization.
return float4(in.position.x/32, 0, (MAX_TILES_WIDTH-in.position.y)/32, 1);

}

kernel void vger_tile_render(texture2d<half, access::write> outTexture [[texture(0)]],
const device Tile *tiles [[buffer(0)]],
device uint *tileLengths [[buffer(1)]],
device float2 *cvs,
uint2 gid [[thread_position_in_grid]],
uint2 tgid [[threadgroup_position_in_grid]]) {

uint tileIx = tgid.y * MAX_TILES_WIDTH + tgid.x;
const device char *src = tiles[tileIx].commands;
const device char *end = src + tileLengths[tileIx];
uint x = gid.x;
uint y = gid.y;
float2 xy = float2(x, y);

if(x >= outTexture.get_width() || y >= outTexture.get_height()) {
return;
}

// Show overflows.
if(tileLengths[tileIx] > 4000) {
outTexture.write(half4(1.0, 0.0, 1.0, 1.0), uint2{gid.x, outTexture.get_height() - gid.y - 1});
return;
}

half3 rgb = half3(0.0);
float d = 1e9;

while(src < end) {
vgerOp op = *(device vgerOp*) src;

if(op == vgerOpEnd) {
break;
}

switch(op) {
case vgerOpSegment: {
vgerCmdSegment cmd = *(device vgerCmdSegment*) src;

d = sdSegment2(xy, cmd.a, cmd.b, cmd.width);

src += sizeof(vgerCmdSegment);
break;
}

case vgerOpRect: {
vgerCmdRect cmd = *(device vgerCmdRect*) src;
auto center = .5*(cmd.a + cmd.b);
auto size = cmd.b - cmd.a;
d = sdBox(xy - center, .5*size, cmd.radius);

src += sizeof(vgerCmdRect);
break;
}

case vgerOpLine: {
vgerCmdLineFill cmd = *(device vgerCmdLineFill*) src;

if(lineTest(xy, cmd.a, cmd.b)) {
d = -d;
}

src += sizeof(vgerCmdLineFill);
break;
}

case vgerOpBez: {
vgerCmdBezFill cmd = *(device vgerCmdBezFill*) src;
d = copysign(min(abs(d), sdBezierApprox2(xy, cmd.a, cmd.b, cmd.c)), d);

if(lineTest(xy, cmd.a, cmd.c)) {
d = -d;
}

if(bezierTest(xy, cmd.a, cmd.b, cmd.c)) {
d = -d;
}

src += sizeof(vgerCmdBezFill);
break;
}

case vgerOpBezIndirect: {
vgerCmdBezFillIndirect cmd = *(device vgerCmdBezFillIndirect*) src;
auto a = cvs[cmd.index];
auto b = cvs[cmd.index+1];
auto c = cvs[cmd.index+2];
d = copysign(min(abs(d), sdBezierApprox2(xy, a, b, c)), d);

if(lineTest(xy, a, c)) {
d = -d;
}

if(bezierTest(xy, a, b, c)) {
d = -d;
}

src += sizeof(vgerCmdBezFillIndirect);
break;
}

case vgerOpFillTile: {
vgerCmdSolid cmd = *(device vgerCmdSolid*) src;
half4 c = unpack_unorm4x8_srgb_to_half(cmd.color);
rgb = c.rgb;
d = 1e9;
src += sizeof(vgerCmdSolid);
break;
}

case vgerOpSolid: {
vgerCmdSolid cmd = *(device vgerCmdSolid*) src;
half4 c = unpack_unorm4x8_srgb_to_half(cmd.color);
rgb = mix(rgb, c.rgb, 1.0-smoothstep(-.5,.5,d) );
d = 1e9;
src += sizeof(vgerCmdSolid);
break;
}

default:
outTexture.write(half4(1.0, 0.0, 1.0, 1.0), gid);
return;

}
}

// Linear to sRGB conversion. Note that if we had writable sRGB textures
// we could let this be done in the write call.
rgb = select(1.055 * pow(rgb, 1/2.4) - 0.055, 12.92 * rgb, rgb < 0.0031308);
half4 rgba = half4(rgb, 1.0);
outTexture.write(rgba, uint2{gid.x, outTexture.get_height() - gid.y - 1});

}
Loading

0 comments on commit 93e8366

Please sign in to comment.