[osx, canvas] start with small input buffers and grow them as needed

This commit is contained in:
Martin Fouilleul 2023-07-11 17:00:17 +02:00
parent fc07a0d018
commit 98a516ff0a
2 changed files with 220 additions and 192 deletions

View File

@ -34,7 +34,6 @@ typedef enum {
typedef struct mg_mtl_path_elt typedef struct mg_mtl_path_elt
{ {
int pathIndex; int pathIndex;
int localEltIndex;
mg_mtl_seg_kind kind; mg_mtl_seg_kind kind;
vector_float2 p[4]; vector_float2 p[4];
} mg_mtl_path_elt; } mg_mtl_path_elt;

View File

@ -34,8 +34,6 @@ typedef struct mg_mtl_canvas_backend
id<MTLTexture> outTexture; id<MTLTexture> outTexture;
int pathBufferOffset;
int elementBufferOffset;
int bufferIndex; int bufferIndex;
dispatch_semaphore_t bufferSemaphore; dispatch_semaphore_t bufferSemaphore;
@ -57,6 +55,19 @@ typedef struct mg_mtl_canvas_backend
int msaaCount; int msaaCount;
vec2 frameSize; vec2 frameSize;
// encoding context
int eltCap;
int eltCount;
int eltBatchStart;
int pathCap;
int pathCount;
int pathBatchStart;
mg_primitive* primitive;
vec4 pathScreenExtents;
vec4 pathUserExtents;
} mg_mtl_canvas_backend; } mg_mtl_canvas_backend;
typedef struct mg_mtl_image_data typedef struct mg_mtl_image_data
@ -84,20 +95,6 @@ void mg_mtl_print_log(int bufferIndex, id<MTLBuffer> logBuffer, id<MTLBuffer> lo
} }
} }
typedef struct mg_mtl_encoding_context
{
int mtlEltCount;
mg_mtl_path* pathBufferData;
mg_mtl_path_elt* elementBufferData;
int pathIndex;
int localEltIndex;
mg_primitive* primitive;
vec4 pathScreenExtents;
vec4 pathUserExtents;
} mg_mtl_encoding_context;
static void mg_update_path_extents(vec4* extents, vec2 p) static void mg_update_path_extents(vec4* extents, vec2 p)
{ {
extents->x = minimum(extents->x, p.x); extents->x = minimum(extents->x, p.x);
@ -106,27 +103,59 @@ static void mg_update_path_extents(vec4* extents, vec2 p)
extents->w = maximum(extents->w, p.y); extents->w = maximum(extents->w, p.y);
} }
void mg_mtl_canvas_encode_element(mg_mtl_encoding_context* context, mg_path_elt_type kind, vec2* p) id<MTLBuffer> mg_mtl_grow_input_buffer(id<MTLDevice> device, id<MTLBuffer> oldBuffer, int oldCopySize, int newSize)
{ {
mg_mtl_path_elt* mtlElt = &context->elementBufferData[context->mtlEltCount]; @autoreleasepool
context->mtlEltCount++; {
MTLResourceOptions bufferOptions = MTLResourceCPUCacheModeWriteCombined
| MTLResourceStorageModeShared;
mtlElt->pathIndex = context->pathIndex; id<MTLBuffer> newBuffer = [device newBufferWithLength: newSize options: bufferOptions];
memcpy([newBuffer contents], [oldBuffer contents], oldCopySize);
[oldBuffer release];
return(newBuffer);
}
}
void mg_mtl_canvas_encode_element(mg_mtl_canvas_backend* backend, mg_path_elt_type kind, vec2* p)
{
int bufferIndex = backend->bufferIndex;
int bufferCap = [backend->elementBuffer[bufferIndex] length] / sizeof(mg_mtl_path_elt);
if(backend->eltCount >= bufferCap)
{
int newBufferCap = (int)(bufferCap * 1.5);
int newBufferSize = newBufferCap * sizeof(mg_mtl_path_elt);
log_info("growing element buffer to %i elements\n", newBufferCap);
backend->elementBuffer[bufferIndex] = mg_mtl_grow_input_buffer(backend->surface->device,
backend->elementBuffer[bufferIndex],
backend->eltCount * sizeof(mg_mtl_path_elt),
newBufferSize);
}
mg_mtl_path_elt* elements = (mg_mtl_path_elt*)[backend->elementBuffer[bufferIndex] contents];
mg_mtl_path_elt* elt = &elements[backend->eltCount];
backend->eltCount++;
elt->pathIndex = backend->pathCount - backend->pathBatchStart;
int count = 0; int count = 0;
switch(kind) switch(kind)
{ {
case MG_PATH_LINE: case MG_PATH_LINE:
mtlElt->kind = MG_MTL_LINE; elt->kind = MG_MTL_LINE;
count = 2; count = 2;
break; break;
case MG_PATH_QUADRATIC: case MG_PATH_QUADRATIC:
mtlElt->kind = MG_MTL_QUADRATIC; elt->kind = MG_MTL_QUADRATIC;
count = 3; count = 3;
break; break;
case MG_PATH_CUBIC: case MG_PATH_CUBIC:
mtlElt->kind = MG_MTL_CUBIC; elt->kind = MG_MTL_CUBIC;
count = 4; count = 4;
break; break;
@ -134,20 +163,89 @@ void mg_mtl_canvas_encode_element(mg_mtl_encoding_context* context, mg_path_elt_
break; break;
} }
mtlElt->localEltIndex = context->localEltIndex;
for(int i=0; i<count; i++) for(int i=0; i<count; i++)
{ {
mg_update_path_extents(&context->pathUserExtents, p[i]); mg_update_path_extents(&backend->pathUserExtents, p[i]);
vec2 screenP = mg_mat2x3_mul(context->primitive->attributes.transform, p[i]); vec2 screenP = mg_mat2x3_mul(backend->primitive->attributes.transform, p[i]);
mtlElt->p[i] = (vector_float2){screenP.x, screenP.y}; elt->p[i] = (vector_float2){screenP.x, screenP.y};
mg_update_path_extents(&context->pathScreenExtents, screenP); mg_update_path_extents(&backend->pathScreenExtents, screenP);
} }
} }
void mg_mtl_encode_path(mg_mtl_canvas_backend* backend, mg_primitive* primitive, float scale)
{
int bufferIndex = backend->bufferIndex;
int bufferCap = [backend->pathBuffer[bufferIndex] length] / sizeof(mg_mtl_path);
if(backend->pathCount >= bufferCap)
{
int newBufferCap = (int)(bufferCap * 1.5);
int newBufferSize = newBufferCap * sizeof(mg_mtl_path);
log_info("growing path buffer to %i elements\n", newBufferCap);
backend->pathBuffer[bufferIndex] = mg_mtl_grow_input_buffer(backend->surface->device,
backend->pathBuffer[bufferIndex],
backend->eltCount * sizeof(mg_mtl_path),
newBufferSize);
}
mg_mtl_path* pathBufferData = (mg_mtl_path*)[backend->pathBuffer[backend->bufferIndex] contents];
mg_mtl_path* path = &(pathBufferData[backend->pathCount]);
backend->pathCount++;
path->cmd = (mg_mtl_cmd)primitive->cmd;
path->box = (vector_float4){backend->pathScreenExtents.x,
backend->pathScreenExtents.y,
backend->pathScreenExtents.z,
backend->pathScreenExtents.w};
path->clip = (vector_float4){primitive->attributes.clip.x,
primitive->attributes.clip.y,
primitive->attributes.clip.x + primitive->attributes.clip.w,
primitive->attributes.clip.y + primitive->attributes.clip.h};
path->color = (vector_float4){primitive->attributes.color.r,
primitive->attributes.color.g,
primitive->attributes.color.b,
primitive->attributes.color.a};
mp_rect srcRegion = primitive->attributes.srcRegion;
mp_rect destRegion = {backend->pathUserExtents.x,
backend->pathUserExtents.y,
backend->pathUserExtents.z - backend->pathUserExtents.x,
backend->pathUserExtents.w - backend->pathUserExtents.y};
if(!mg_image_is_nil(primitive->attributes.image))
{
vec2 texSize = mg_image_size(primitive->attributes.image);
mg_mat2x3 srcRegionToImage = {1/texSize.x, 0, srcRegion.x/texSize.x,
0, 1/texSize.y, srcRegion.y/texSize.y};
mg_mat2x3 destRegionToSrcRegion = {srcRegion.w/destRegion.w, 0, 0,
0, srcRegion.h/destRegion.h, 0};
mg_mat2x3 userToDestRegion = {1, 0, -destRegion.x,
0, 1, -destRegion.y};
mg_mat2x3 screenToUser = mg_mat2x3_inv(primitive->attributes.transform);
mg_mat2x3 uvTransform = srcRegionToImage;
uvTransform = mg_mat2x3_mul_m(uvTransform, destRegionToSrcRegion);
uvTransform = mg_mat2x3_mul_m(uvTransform, userToDestRegion);
uvTransform = mg_mat2x3_mul_m(uvTransform, screenToUser);
path->uvTransform = simd_matrix(simd_make_float3(uvTransform.m[0]/scale, uvTransform.m[3]/scale, 0),
simd_make_float3(uvTransform.m[1]/scale, uvTransform.m[4]/scale, 0),
simd_make_float3(uvTransform.m[2], uvTransform.m[5], 1));
}
}
bool mg_intersect_hull_legs(vec2 p0, vec2 p1, vec2 p2, vec2 p3, vec2* intersection) bool mg_intersect_hull_legs(vec2 p0, vec2 p1, vec2 p2, vec2 p3, vec2* intersection)
{ {
/*NOTE: check intersection of lines (p0-p1) and (p2-p3) /*NOTE: check intersection of lines (p0-p1) and (p2-p3)
@ -336,9 +434,9 @@ void mg_cubic_split(vec2 p[4], f32 t, vec2 outLeft[4], vec2 outRight[4])
outRight[3] = p[3]; outRight[3] = p[3];
} }
void mg_mtl_render_stroke_line(mg_mtl_encoding_context* context, vec2* p) void mg_mtl_render_stroke_line(mg_mtl_canvas_backend* backend, vec2* p)
{ {
f32 width = context->primitive->attributes.width; f32 width = backend->primitive->attributes.width;
vec2 v = {p[1].x-p[0].x, p[1].y-p[0].y}; vec2 v = {p[1].x-p[0].x, p[1].y-p[0].y};
vec2 n = {v.y, -v.x}; vec2 n = {v.y, -v.x};
@ -350,28 +448,27 @@ void mg_mtl_render_stroke_line(mg_mtl_encoding_context* context, vec2* p)
vec2 joint0[2] = {vec2_add(p[0], vec2_mul(-1, offset)), vec2_add(p[0], offset)}; vec2 joint0[2] = {vec2_add(p[0], vec2_mul(-1, offset)), vec2_add(p[0], offset)};
vec2 joint1[2] = {vec2_add(p[1], offset), vec2_add(p[1], vec2_mul(-1, offset))}; vec2 joint1[2] = {vec2_add(p[1], offset), vec2_add(p[1], vec2_mul(-1, offset))};
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, right); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, right);
mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, left);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, left); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, joint0);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, joint0); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, joint1);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, joint1);
} }
void mg_mtl_render_stroke_quadratic(mg_mtl_encoding_context* context, vec2* p) void mg_mtl_render_stroke_quadratic(mg_mtl_canvas_backend* backend, vec2* p)
{ {
f32 width = context->primitive->attributes.width; f32 width = backend->primitive->attributes.width;
f32 tolerance = minimum(context->primitive->attributes.tolerance, 0.5 * width); f32 tolerance = minimum(backend->primitive->attributes.tolerance, 0.5 * width);
//NOTE: check for degenerate line case //NOTE: check for degenerate line case
const f32 equalEps = 1e-3; const f32 equalEps = 1e-3;
if(vec2_close(p[0], p[1], equalEps)) if(vec2_close(p[0], p[1], equalEps))
{ {
mg_mtl_render_stroke_line(context, p+1); mg_mtl_render_stroke_line(backend, p+1);
return; return;
} }
else if(vec2_close(p[1], p[2], equalEps)) else if(vec2_close(p[1], p[2], equalEps))
{ {
mg_mtl_render_stroke_line(context, p); mg_mtl_render_stroke_line(backend, p);
return; return;
} }
@ -386,8 +483,8 @@ void mg_mtl_render_stroke_quadratic(mg_mtl_encoding_context* context, vec2* p)
vec2 splitLeft[3]; vec2 splitLeft[3];
vec2 splitRight[3]; vec2 splitRight[3];
mg_quadratic_split(p, 0.5, splitLeft, splitRight); mg_quadratic_split(p, 0.5, splitLeft, splitRight);
mg_mtl_render_stroke_quadratic(context, splitLeft); mg_mtl_render_stroke_quadratic(backend, splitLeft);
mg_mtl_render_stroke_quadratic(context, splitRight); mg_mtl_render_stroke_quadratic(backend, splitRight);
} }
else else
{ {
@ -428,8 +525,8 @@ void mg_mtl_render_stroke_quadratic(mg_mtl_encoding_context* context, vec2* p)
vec2 splitLeft[3]; vec2 splitLeft[3];
vec2 splitRight[3]; vec2 splitRight[3];
mg_quadratic_split(p, maxOvershootParameter, splitLeft, splitRight); mg_quadratic_split(p, maxOvershootParameter, splitLeft, splitRight);
mg_mtl_render_stroke_quadratic(context, splitLeft); mg_mtl_render_stroke_quadratic(backend, splitLeft);
mg_mtl_render_stroke_quadratic(context, splitRight); mg_mtl_render_stroke_quadratic(backend, splitRight);
} }
else else
{ {
@ -437,21 +534,21 @@ void mg_mtl_render_stroke_quadratic(mg_mtl_encoding_context* context, vec2* p)
leftHull[0] = leftHull[2]; leftHull[0] = leftHull[2];
leftHull[2] = tmp; leftHull[2] = tmp;
mg_mtl_canvas_encode_element(context, MG_PATH_QUADRATIC, rightHull); mg_mtl_canvas_encode_element(backend, MG_PATH_QUADRATIC, rightHull);
mg_mtl_canvas_encode_element(context, MG_PATH_QUADRATIC, leftHull); mg_mtl_canvas_encode_element(backend, MG_PATH_QUADRATIC, leftHull);
vec2 joint0[2] = {rightHull[2], leftHull[0]}; vec2 joint0[2] = {rightHull[2], leftHull[0]};
vec2 joint1[2] = {leftHull[2], rightHull[0]}; vec2 joint1[2] = {leftHull[2], rightHull[0]};
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, joint0); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, joint0);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, joint1); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, joint1);
} }
} }
} }
void mg_mtl_render_stroke_cubic(mg_mtl_encoding_context* context, vec2* p) void mg_mtl_render_stroke_cubic(mg_mtl_canvas_backend* backend, vec2* p)
{ {
f32 width = context->primitive->attributes.width; f32 width = backend->primitive->attributes.width;
f32 tolerance = minimum(context->primitive->attributes.tolerance, 0.5 * width); f32 tolerance = minimum(backend->primitive->attributes.tolerance, 0.5 * width);
//NOTE: check degenerate line cases //NOTE: check degenerate line cases
f32 equalEps = 1e-3; f32 equalEps = 1e-3;
@ -461,19 +558,19 @@ void mg_mtl_render_stroke_cubic(mg_mtl_encoding_context* context, vec2* p)
||(vec2_close(p[1], p[2], equalEps) && vec2_close(p[2], p[3], equalEps))) ||(vec2_close(p[1], p[2], equalEps) && vec2_close(p[2], p[3], equalEps)))
{ {
vec2 line[2] = {p[0], p[3]}; vec2 line[2] = {p[0], p[3]};
mg_mtl_render_stroke_line(context, line); mg_mtl_render_stroke_line(backend, line);
return; return;
} }
else if(vec2_close(p[0], p[1], equalEps) && vec2_close(p[1], p[3], equalEps)) else if(vec2_close(p[0], p[1], equalEps) && vec2_close(p[1], p[3], equalEps))
{ {
vec2 line[2] = {p[0], vec2_add(vec2_mul(5./9, p[0]), vec2_mul(4./9, p[2]))}; vec2 line[2] = {p[0], vec2_add(vec2_mul(5./9, p[0]), vec2_mul(4./9, p[2]))};
mg_mtl_render_stroke_line(context, line); mg_mtl_render_stroke_line(backend, line);
return; return;
} }
else if(vec2_close(p[0], p[2], equalEps) && vec2_close(p[2], p[3], equalEps)) else if(vec2_close(p[0], p[2], equalEps) && vec2_close(p[2], p[3], equalEps))
{ {
vec2 line[2] = {p[0], vec2_add(vec2_mul(5./9, p[0]), vec2_mul(4./9, p[1]))}; vec2 line[2] = {p[0], vec2_add(vec2_mul(5./9, p[0]), vec2_mul(4./9, p[1]))};
mg_mtl_render_stroke_line(context, line); mg_mtl_render_stroke_line(backend, line);
return; return;
} }
@ -488,8 +585,8 @@ void mg_mtl_render_stroke_cubic(mg_mtl_encoding_context* context, vec2* p)
vec2 splitLeft[4]; vec2 splitLeft[4];
vec2 splitRight[4]; vec2 splitRight[4];
mg_cubic_split(p, 0.5, splitLeft, splitRight); mg_cubic_split(p, 0.5, splitLeft, splitRight);
mg_mtl_render_stroke_cubic(context, splitLeft); mg_mtl_render_stroke_cubic(backend, splitLeft);
mg_mtl_render_stroke_cubic(context, splitRight); mg_mtl_render_stroke_cubic(backend, splitRight);
} }
else else
{ {
@ -530,8 +627,8 @@ void mg_mtl_render_stroke_cubic(mg_mtl_encoding_context* context, vec2* p)
vec2 splitLeft[4]; vec2 splitLeft[4];
vec2 splitRight[4]; vec2 splitRight[4];
mg_cubic_split(p, maxOvershootParameter, splitLeft, splitRight); mg_cubic_split(p, maxOvershootParameter, splitLeft, splitRight);
mg_mtl_render_stroke_cubic(context, splitLeft); mg_mtl_render_stroke_cubic(backend, splitLeft);
mg_mtl_render_stroke_cubic(context, splitRight); mg_mtl_render_stroke_cubic(backend, splitRight);
} }
else else
{ {
@ -542,18 +639,18 @@ void mg_mtl_render_stroke_cubic(mg_mtl_encoding_context* context, vec2* p)
leftHull[1] = leftHull[2]; leftHull[1] = leftHull[2];
leftHull[2] = tmp; leftHull[2] = tmp;
mg_mtl_canvas_encode_element(context, MG_PATH_CUBIC, rightHull); mg_mtl_canvas_encode_element(backend, MG_PATH_CUBIC, rightHull);
mg_mtl_canvas_encode_element(context, MG_PATH_CUBIC, leftHull); mg_mtl_canvas_encode_element(backend, MG_PATH_CUBIC, leftHull);
vec2 joint0[2] = {rightHull[3], leftHull[0]}; vec2 joint0[2] = {rightHull[3], leftHull[0]};
vec2 joint1[2] = {leftHull[3], rightHull[0]}; vec2 joint1[2] = {leftHull[3], rightHull[0]};
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, joint0); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, joint0);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, joint1); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, joint1);
} }
} }
} }
void mg_mtl_render_stroke_element(mg_mtl_encoding_context* context, void mg_mtl_render_stroke_element(mg_mtl_canvas_backend* backend,
mg_path_elt* element, mg_path_elt* element,
vec2 currentPoint, vec2 currentPoint,
vec2* startTangent, vec2* startTangent,
@ -566,17 +663,17 @@ void mg_mtl_render_stroke_element(mg_mtl_encoding_context* context,
switch(element->type) switch(element->type)
{ {
case MG_PATH_LINE: case MG_PATH_LINE:
mg_mtl_render_stroke_line(context, controlPoints); mg_mtl_render_stroke_line(backend, controlPoints);
endPointIndex = 1; endPointIndex = 1;
break; break;
case MG_PATH_QUADRATIC: case MG_PATH_QUADRATIC:
mg_mtl_render_stroke_quadratic(context, controlPoints); mg_mtl_render_stroke_quadratic(backend, controlPoints);
endPointIndex = 2; endPointIndex = 2;
break; break;
case MG_PATH_CUBIC: case MG_PATH_CUBIC:
mg_mtl_render_stroke_cubic(context, controlPoints); mg_mtl_render_stroke_cubic(backend, controlPoints);
endPointIndex = 3; endPointIndex = 3;
break; break;
@ -613,11 +710,11 @@ void mg_mtl_render_stroke_element(mg_mtl_encoding_context* context,
DEBUG_ASSERT(startTangent->x != 0 || startTangent->y != 0); DEBUG_ASSERT(startTangent->x != 0 || startTangent->y != 0);
} }
void mg_mtl_stroke_cap(mg_mtl_encoding_context* context, void mg_mtl_stroke_cap(mg_mtl_canvas_backend* backend,
vec2 p0, vec2 p0,
vec2 direction) vec2 direction)
{ {
mg_attributes* attributes = &context->primitive->attributes; mg_attributes* attributes = &backend->primitive->attributes;
//NOTE(martin): compute the tangent and normal vectors (multiplied by half width) at the cap point //NOTE(martin): compute the tangent and normal vectors (multiplied by half width) at the cap point
f32 dn = sqrt(Square(direction.x) + Square(direction.y)); f32 dn = sqrt(Square(direction.x) + Square(direction.y));
@ -635,18 +732,18 @@ void mg_mtl_stroke_cap(mg_mtl_encoding_context* context,
{p0.x - n0.x, p0.y - n0.y}, {p0.x - n0.x, p0.y - n0.y},
{p0.x + n0.x, p0.y + n0.y}}; {p0.x + n0.x, p0.y + n0.y}};
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, points); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, points);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, points+1); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, points+1);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, points+2); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, points+2);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, points+3); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, points+3);
} }
void mg_mtl_stroke_joint(mg_mtl_encoding_context* context, void mg_mtl_stroke_joint(mg_mtl_canvas_backend* backend,
vec2 p0, vec2 p0,
vec2 t0, vec2 t0,
vec2 t1) vec2 t1)
{ {
mg_attributes* attributes = &context->primitive->attributes; mg_attributes* attributes = &backend->primitive->attributes;
//NOTE(martin): compute the normals at the joint point //NOTE(martin): compute the normals at the joint point
f32 norm_t0 = sqrt(Square(t0.x) + Square(t0.y)); f32 norm_t0 = sqrt(Square(t0.x) + Square(t0.y));
@ -694,10 +791,10 @@ void mg_mtl_stroke_joint(mg_mtl_encoding_context* context,
{p0.x + n1.x*halfW, p0.y + n1.y*halfW}, {p0.x + n1.x*halfW, p0.y + n1.y*halfW},
p0}; p0};
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, points); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, points);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, points+1); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, points+1);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, points+2); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, points+2);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, points+3); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, points+3);
} }
else else
{ {
@ -707,13 +804,13 @@ void mg_mtl_stroke_joint(mg_mtl_encoding_context* context,
{p0.x + n1.x*halfW, p0.y + n1.y*halfW}, {p0.x + n1.x*halfW, p0.y + n1.y*halfW},
p0}; p0};
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, points); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, points);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, points+1); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, points+1);
mg_mtl_canvas_encode_element(context, MG_PATH_LINE, points+2); mg_mtl_canvas_encode_element(backend, MG_PATH_LINE, points+2);
} }
} }
u32 mg_mtl_render_stroke_subpath(mg_mtl_encoding_context* context, u32 mg_mtl_render_stroke_subpath(mg_mtl_canvas_backend* backend,
mg_path_elt* elements, mg_path_elt* elements,
mg_path_descriptor* path, mg_path_descriptor* path,
u32 startIndex, u32 startIndex,
@ -730,7 +827,7 @@ u32 mg_mtl_render_stroke_subpath(mg_mtl_encoding_context* context,
vec2 endTangent = {0, 0}; vec2 endTangent = {0, 0};
//NOTE(martin): render first element and compute first tangent //NOTE(martin): render first element and compute first tangent
mg_mtl_render_stroke_element(context, elements + startIndex, currentPoint, &startTangent, &endTangent, &endPoint); mg_mtl_render_stroke_element(backend, elements + startIndex, currentPoint, &startTangent, &endTangent, &endPoint);
firstTangent = startTangent; firstTangent = startTangent;
previousEndTangent = endTangent; previousEndTangent = endTangent;
@ -738,18 +835,18 @@ u32 mg_mtl_render_stroke_subpath(mg_mtl_encoding_context* context,
//NOTE(martin): render subsequent elements along with their joints //NOTE(martin): render subsequent elements along with their joints
mg_attributes* attributes = &context->primitive->attributes; mg_attributes* attributes = &backend->primitive->attributes;
u32 eltIndex = startIndex + 1; u32 eltIndex = startIndex + 1;
for(; for(;
eltIndex<eltCount && elements[eltIndex].type != MG_PATH_MOVE; eltIndex<eltCount && elements[eltIndex].type != MG_PATH_MOVE;
eltIndex++) eltIndex++)
{ {
mg_mtl_render_stroke_element(context, elements + eltIndex, currentPoint, &startTangent, &endTangent, &endPoint); mg_mtl_render_stroke_element(backend, elements + eltIndex, currentPoint, &startTangent, &endTangent, &endPoint);
if(attributes->joint != MG_JOINT_NONE) if(attributes->joint != MG_JOINT_NONE)
{ {
mg_mtl_stroke_joint(context, currentPoint, previousEndTangent, startTangent); mg_mtl_stroke_joint(backend, currentPoint, previousEndTangent, startTangent);
} }
previousEndTangent = endTangent; previousEndTangent = endTangent;
currentPoint = endPoint; currentPoint = endPoint;
@ -764,19 +861,19 @@ u32 mg_mtl_render_stroke_subpath(mg_mtl_encoding_context* context,
if(attributes->joint != MG_JOINT_NONE) if(attributes->joint != MG_JOINT_NONE)
{ {
//NOTE(martin): add a closing joint if the path is closed //NOTE(martin): add a closing joint if the path is closed
mg_mtl_stroke_joint(context, endPoint, endTangent, firstTangent); mg_mtl_stroke_joint(backend, endPoint, endTangent, firstTangent);
} }
} }
else if(attributes->cap == MG_CAP_SQUARE) else if(attributes->cap == MG_CAP_SQUARE)
{ {
//NOTE(martin): add start and end cap //NOTE(martin): add start and end cap
mg_mtl_stroke_cap(context, startPoint, (vec2){-startTangent.x, -startTangent.y}); mg_mtl_stroke_cap(backend, startPoint, (vec2){-startTangent.x, -startTangent.y});
mg_mtl_stroke_cap(context, endPoint, endTangent); mg_mtl_stroke_cap(backend, endPoint, endTangent);
} }
return(eltIndex); return(eltIndex);
} }
void mg_mtl_render_stroke(mg_mtl_encoding_context* context, void mg_mtl_render_stroke(mg_mtl_canvas_backend* backend,
mg_path_elt* elements, mg_path_elt* elements,
mg_path_descriptor* path) mg_path_descriptor* path)
{ {
@ -796,7 +893,7 @@ void mg_mtl_render_stroke(mg_mtl_encoding_context* context,
} }
if(startIndex < eltCount) if(startIndex < eltCount)
{ {
startIndex = mg_mtl_render_stroke_subpath(context, elements, path, startIndex, startPoint); startIndex = mg_mtl_render_stroke_subpath(backend, elements, path, startIndex, startPoint);
} }
} }
} }
@ -804,8 +901,6 @@ void mg_mtl_render_stroke(mg_mtl_encoding_context* context,
void mg_mtl_render_batch(mg_mtl_canvas_backend* backend, void mg_mtl_render_batch(mg_mtl_canvas_backend* backend,
mg_mtl_surface* surface, mg_mtl_surface* surface,
int pathCount,
int eltCount,
mg_image_data* image, mg_image_data* image,
int tileSize, int tileSize,
int nTilesX, int nTilesX,
@ -813,6 +908,11 @@ void mg_mtl_render_batch(mg_mtl_canvas_backend* backend,
vec2 viewportSize, vec2 viewportSize,
f32 scale) f32 scale)
{ {
int pathBufferOffset = backend->pathBatchStart * sizeof(mg_mtl_path);
int elementBufferOffset = backend->eltBatchStart * sizeof(mg_mtl_path_elt);
int pathCount = backend->pathCount - backend->pathBatchStart;
int eltCount = backend->eltCount - backend->eltBatchStart;
//NOTE: encode GPU commands //NOTE: encode GPU commands
@autoreleasepool @autoreleasepool
{ {
@ -842,7 +942,7 @@ void mg_mtl_render_batch(mg_mtl_canvas_backend* backend,
[pathEncoder setComputePipelineState: backend->pathPipeline]; [pathEncoder setComputePipelineState: backend->pathPipeline];
[pathEncoder setBytes:&pathCount length:sizeof(int) atIndex:0]; [pathEncoder setBytes:&pathCount length:sizeof(int) atIndex:0];
[pathEncoder setBuffer:backend->pathBuffer[backend->bufferIndex] offset:backend->pathBufferOffset atIndex:1]; [pathEncoder setBuffer:backend->pathBuffer[backend->bufferIndex] offset:pathBufferOffset atIndex:1];
[pathEncoder setBuffer:backend->pathQueueBuffer offset:0 atIndex:2]; [pathEncoder setBuffer:backend->pathQueueBuffer offset:0 atIndex:2];
[pathEncoder setBuffer:backend->tileQueueBuffer offset:0 atIndex:3]; [pathEncoder setBuffer:backend->tileQueueBuffer offset:0 atIndex:3];
[pathEncoder setBuffer:backend->tileQueueCountBuffer offset:0 atIndex:4]; [pathEncoder setBuffer:backend->tileQueueCountBuffer offset:0 atIndex:4];
@ -861,7 +961,7 @@ void mg_mtl_render_batch(mg_mtl_canvas_backend* backend,
[segmentEncoder setComputePipelineState: backend->segmentPipeline]; [segmentEncoder setComputePipelineState: backend->segmentPipeline];
[segmentEncoder setBytes:&eltCount length:sizeof(int) atIndex:0]; [segmentEncoder setBytes:&eltCount length:sizeof(int) atIndex:0];
[segmentEncoder setBuffer:backend->elementBuffer[backend->bufferIndex] offset:backend->elementBufferOffset atIndex:1]; [segmentEncoder setBuffer:backend->elementBuffer[backend->bufferIndex] offset:elementBufferOffset atIndex:1];
[segmentEncoder setBuffer:backend->segmentCountBuffer offset:0 atIndex:2]; [segmentEncoder setBuffer:backend->segmentCountBuffer offset:0 atIndex:2];
[segmentEncoder setBuffer:backend->segmentBuffer offset:0 atIndex:3]; [segmentEncoder setBuffer:backend->segmentBuffer offset:0 atIndex:3];
[segmentEncoder setBuffer:backend->pathQueueBuffer offset:0 atIndex:4]; [segmentEncoder setBuffer:backend->pathQueueBuffer offset:0 atIndex:4];
@ -901,7 +1001,7 @@ void mg_mtl_render_batch(mg_mtl_canvas_backend* backend,
[mergeEncoder setComputePipelineState: backend->mergePipeline]; [mergeEncoder setComputePipelineState: backend->mergePipeline];
[mergeEncoder setBytes:&pathCount length:sizeof(int) atIndex:0]; [mergeEncoder setBytes:&pathCount length:sizeof(int) atIndex:0];
[mergeEncoder setBuffer:backend->pathBuffer[backend->bufferIndex] offset:backend->pathBufferOffset atIndex:1]; [mergeEncoder setBuffer:backend->pathBuffer[backend->bufferIndex] offset:pathBufferOffset atIndex:1];
[mergeEncoder setBuffer:backend->pathQueueBuffer offset:0 atIndex:2]; [mergeEncoder setBuffer:backend->pathQueueBuffer offset:0 atIndex:2];
[mergeEncoder setBuffer:backend->tileQueueBuffer offset:0 atIndex:3]; [mergeEncoder setBuffer:backend->tileQueueBuffer offset:0 atIndex:3];
[mergeEncoder setBuffer:backend->tileOpBuffer offset:0 atIndex:4]; [mergeEncoder setBuffer:backend->tileOpBuffer offset:0 atIndex:4];
@ -926,7 +1026,7 @@ void mg_mtl_render_batch(mg_mtl_canvas_backend* backend,
[rasterEncoder setBuffer:backend->screenTilesBuffer offset:0 atIndex:0]; [rasterEncoder setBuffer:backend->screenTilesBuffer offset:0 atIndex:0];
[rasterEncoder setBuffer:backend->tileOpBuffer offset:0 atIndex:1]; [rasterEncoder setBuffer:backend->tileOpBuffer offset:0 atIndex:1];
[rasterEncoder setBuffer:backend->pathBuffer[backend->bufferIndex] offset:backend->pathBufferOffset atIndex:2]; [rasterEncoder setBuffer:backend->pathBuffer[backend->bufferIndex] offset:pathBufferOffset atIndex:2];
[rasterEncoder setBuffer:backend->segmentBuffer offset:0 atIndex:3]; [rasterEncoder setBuffer:backend->segmentBuffer offset:0 atIndex:3];
[rasterEncoder setBytes:&tileSize length:sizeof(int) atIndex:4]; [rasterEncoder setBytes:&tileSize length:sizeof(int) atIndex:4];
[rasterEncoder setBytes:&scale length:sizeof(float) atIndex:5]; [rasterEncoder setBytes:&scale length:sizeof(float) atIndex:5];
@ -947,7 +1047,6 @@ void mg_mtl_render_batch(mg_mtl_canvas_backend* backend,
MTLSize rasterGridSize = MTLSizeMake(viewportSize.x, viewportSize.y, 1); MTLSize rasterGridSize = MTLSizeMake(viewportSize.x, viewportSize.y, 1);
MTLSize rasterGroupSize = MTLSizeMake(MG_MTL_TILE_SIZE, MG_MTL_TILE_SIZE, 1); MTLSize rasterGroupSize = MTLSizeMake(MG_MTL_TILE_SIZE, MG_MTL_TILE_SIZE, 1);
// [rasterEncoder dispatchThreads: rasterGridSize threadsPerThreadgroup: rasterGroupSize];
[rasterEncoder dispatchThreadgroupsWithIndirectBuffer: backend->rasterDispatchBuffer [rasterEncoder dispatchThreadgroupsWithIndirectBuffer: backend->rasterDispatchBuffer
indirectBufferOffset: 0 indirectBufferOffset: 0
@ -973,6 +1072,9 @@ void mg_mtl_render_batch(mg_mtl_canvas_backend* backend,
vertexCount: 3 ]; vertexCount: 3 ];
[renderEncoder endEncoding]; [renderEncoder endEncoding];
} }
backend->pathBatchStart = backend->pathCount;
backend->eltBatchStart = backend->eltCount;
} }
void mg_mtl_canvas_resize(mg_mtl_canvas_backend* backend, vec2 size) void mg_mtl_canvas_resize(mg_mtl_canvas_backend* backend, vec2 size)
@ -1019,18 +1121,11 @@ void mg_mtl_canvas_render(mg_canvas_backend* interface,
{ {
mg_mtl_canvas_backend* backend = (mg_mtl_canvas_backend*)interface; mg_mtl_canvas_backend* backend = (mg_mtl_canvas_backend*)interface;
//NOTE: update rolling buffers //NOTE: update rolling input buffers
dispatch_semaphore_wait(backend->bufferSemaphore, DISPATCH_TIME_FOREVER); dispatch_semaphore_wait(backend->bufferSemaphore, DISPATCH_TIME_FOREVER);
backend->bufferIndex = (backend->bufferIndex + 1) % MG_MTL_INPUT_BUFFERS_COUNT; backend->bufferIndex = (backend->bufferIndex + 1) % MG_MTL_INPUT_BUFFERS_COUNT;
mg_mtl_path_elt* elementBufferData = (mg_mtl_path_elt*)[backend->elementBuffer[backend->bufferIndex] contents]; //NOTE: ensure screen tiles buffer is correct size
mg_mtl_path* pathBufferData = (mg_mtl_path*)[backend->pathBuffer[backend->bufferIndex] contents];
/////////////////////////////////////////////////////////////////////////////////////
//TODO: ensure screen tiles buffer is correct size
/////////////////////////////////////////////////////////////////////////////////////
//NOTE: prepare rendering
mg_mtl_surface* surface = backend->surface; mg_mtl_surface* surface = backend->surface;
mp_rect frame = surface->interface.getFrame((mg_surface_data*)surface); mp_rect frame = surface->interface.getFrame((mg_surface_data*)surface);
@ -1046,6 +1141,7 @@ void mg_mtl_canvas_render(mg_canvas_backend* interface,
mg_mtl_canvas_resize(backend, viewportSize); mg_mtl_canvas_resize(backend, viewportSize);
} }
//NOTE: acquire metal resources for rendering
mg_mtl_surface_acquire_command_buffer(surface); mg_mtl_surface_acquire_command_buffer(surface);
mg_mtl_surface_acquire_drawable(surface); mg_mtl_surface_acquire_drawable(surface);
@ -1068,17 +1164,15 @@ void mg_mtl_canvas_render(mg_canvas_backend* interface,
renderEncoder.label = @"clear pass"; renderEncoder.label = @"clear pass";
[renderEncoder endEncoding]; [renderEncoder endEncoding];
} }
backend->pathBufferOffset = 0; backend->pathCount = 0;
backend->elementBufferOffset = 0; backend->pathBatchStart = 0;
backend->eltCount = 0;
backend->eltBatchStart = 0;
//NOTE: encode and render batches //NOTE: encode and render batches
int pathCount = 0;
vec2 currentPos = {0}; vec2 currentPos = {0};
mg_image currentImage = mg_image_nil(); mg_image currentImage = mg_image_nil();
mg_mtl_encoding_context context = {.mtlEltCount = 0,
.elementBufferData = elementBufferData,
.pathBufferData = pathBufferData};
for(int primitiveIndex = 0; primitiveIndex < primitiveCount; primitiveIndex++) for(int primitiveIndex = 0; primitiveIndex < primitiveCount; primitiveIndex++)
{ {
@ -1090,51 +1184,37 @@ void mg_mtl_canvas_render(mg_canvas_backend* interface,
mg_mtl_render_batch(backend, mg_mtl_render_batch(backend,
surface, surface,
pathCount,
context.mtlEltCount,
imageData, imageData,
tileSize, tileSize,
nTilesX, nTilesX,
nTilesY, nTilesY,
viewportSize, viewportSize,
scale); scale);
backend->pathBufferOffset += pathCount * sizeof(mg_mtl_path);
backend->elementBufferOffset += context.mtlEltCount * sizeof(mg_mtl_path_elt);
pathCount = 0;
context.mtlEltCount = 0;
context.elementBufferData = (mg_mtl_path_elt*)((char*)elementBufferData + backend->elementBufferOffset);
context.pathBufferData = (mg_mtl_path*)((char*)pathBufferData + backend->pathBufferOffset);
} }
currentImage = primitive->attributes.image; currentImage = primitive->attributes.image;
if(primitive->path.count) if(primitive->path.count)
{ {
context.primitive = primitive; backend->primitive = primitive;
context.pathIndex = pathCount; backend->pathScreenExtents = (vec4){FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX};
context.pathScreenExtents = (vec4){FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX}; backend->pathUserExtents = (vec4){FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX};
context.pathUserExtents = (vec4){FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX};
if(primitive->cmd == MG_CMD_STROKE) if(primitive->cmd == MG_CMD_STROKE)
{ {
mg_mtl_render_stroke(&context, pathElements + primitive->path.startIndex, &primitive->path); mg_mtl_render_stroke(backend, pathElements + primitive->path.startIndex, &primitive->path);
} }
else else
{ {
int segCount = 0;
for(int eltIndex = 0; for(int eltIndex = 0;
(eltIndex < primitive->path.count) && (primitive->path.startIndex + eltIndex < eltCount); (eltIndex < primitive->path.count) && (primitive->path.startIndex + eltIndex < eltCount);
eltIndex++) eltIndex++)
{ {
context.localEltIndex = segCount;
mg_path_elt* elt = &pathElements[primitive->path.startIndex + eltIndex]; mg_path_elt* elt = &pathElements[primitive->path.startIndex + eltIndex];
if(elt->type != MG_PATH_MOVE) if(elt->type != MG_PATH_MOVE)
{ {
vec2 p[4] = {currentPos, elt->p[0], elt->p[1], elt->p[2]}; vec2 p[4] = {currentPos, elt->p[0], elt->p[1], elt->p[2]};
mg_mtl_canvas_encode_element(&context, elt->type, p); mg_mtl_canvas_encode_element(backend, elt->type, p);
segCount++;
} }
switch(elt->type) switch(elt->type)
{ {
@ -1156,66 +1236,15 @@ void mg_mtl_canvas_render(mg_canvas_backend* interface,
} }
} }
} }
//NOTE: push path //NOTE: encode path
mg_mtl_path* path = &context.pathBufferData[pathCount]; mg_mtl_encode_path(backend, primitive, scale);
pathCount++;
path->cmd = (mg_mtl_cmd)primitive->cmd;
path->box = (vector_float4){context.pathScreenExtents.x,
context.pathScreenExtents.y,
context.pathScreenExtents.z,
context.pathScreenExtents.w};
path->clip = (vector_float4){primitive->attributes.clip.x,
primitive->attributes.clip.y,
primitive->attributes.clip.x + primitive->attributes.clip.w,
primitive->attributes.clip.y + primitive->attributes.clip.h};
path->color = (vector_float4){primitive->attributes.color.r,
primitive->attributes.color.g,
primitive->attributes.color.b,
primitive->attributes.color.a};
mp_rect srcRegion = primitive->attributes.srcRegion;
mp_rect destRegion = {context.pathUserExtents.x,
context.pathUserExtents.y,
context.pathUserExtents.z - context.pathUserExtents.x,
context.pathUserExtents.w - context.pathUserExtents.y};
if(!mg_image_is_nil(primitive->attributes.image))
{
vec2 texSize = mg_image_size(primitive->attributes.image);
mg_mat2x3 srcRegionToImage = {1/texSize.x, 0, srcRegion.x/texSize.x,
0, 1/texSize.y, srcRegion.y/texSize.y};
mg_mat2x3 destRegionToSrcRegion = {srcRegion.w/destRegion.w, 0, 0,
0, srcRegion.h/destRegion.h, 0};
mg_mat2x3 userToDestRegion = {1, 0, -destRegion.x,
0, 1, -destRegion.y};
mg_mat2x3 screenToUser = mg_mat2x3_inv(primitive->attributes.transform);
mg_mat2x3 uvTransform = srcRegionToImage;
uvTransform = mg_mat2x3_mul_m(uvTransform, destRegionToSrcRegion);
uvTransform = mg_mat2x3_mul_m(uvTransform, userToDestRegion);
uvTransform = mg_mat2x3_mul_m(uvTransform, screenToUser);
path->uvTransform = simd_matrix(simd_make_float3(uvTransform.m[0]/scale, uvTransform.m[3]/scale, 0),
simd_make_float3(uvTransform.m[1]/scale, uvTransform.m[4]/scale, 0),
simd_make_float3(uvTransform.m[2], uvTransform.m[5], 1));
}
} }
} }
mg_image_data* imageData = mg_image_data_from_handle(currentImage); mg_image_data* imageData = mg_image_data_from_handle(currentImage);
mg_mtl_render_batch(backend, mg_mtl_render_batch(backend,
surface, surface,
pathCount,
context.mtlEltCount,
imageData, imageData,
tileSize, tileSize,
nTilesX, nTilesX,
@ -1321,8 +1350,8 @@ void mg_mtl_canvas_image_upload_region(mg_canvas_backend* backendInterface, mg_i
bytesPerRow: 4 * region.w]; bytesPerRow: 4 * region.w];
}} }}
const u32 MG_MTL_PATH_BUFFER_SIZE = (4<<20)*sizeof(mg_mtl_path), const u32 MG_MTL_DEFAULT_PATH_BUFFER_LEN = (4<<10),
MG_MTL_ELEMENT_BUFFER_SIZE = (4<<20)*sizeof(mg_mtl_path_elt), MG_MTL_DEFAULT_ELT_BUFFER_LEN = (4<<10),
MG_MTL_SEGMENT_BUFFER_SIZE = (4<<20)*sizeof(mg_mtl_segment), MG_MTL_SEGMENT_BUFFER_SIZE = (4<<20)*sizeof(mg_mtl_segment),
MG_MTL_PATH_QUEUE_BUFFER_SIZE = (4<<20)*sizeof(mg_mtl_path_queue), MG_MTL_PATH_QUEUE_BUFFER_SIZE = (4<<20)*sizeof(mg_mtl_path_queue),
MG_MTL_TILE_QUEUE_BUFFER_SIZE = (4<<20)*sizeof(mg_mtl_tile_queue), MG_MTL_TILE_QUEUE_BUFFER_SIZE = (4<<20)*sizeof(mg_mtl_tile_queue),
@ -1424,10 +1453,10 @@ mg_canvas_backend* mtl_canvas_backend_create(mg_mtl_surface* surface)
for(int i=0; i<MG_MTL_INPUT_BUFFERS_COUNT; i++) for(int i=0; i<MG_MTL_INPUT_BUFFERS_COUNT; i++)
{ {
backend->pathBuffer[i] = [surface->device newBufferWithLength: MG_MTL_PATH_BUFFER_SIZE backend->pathBuffer[i] = [surface->device newBufferWithLength: MG_MTL_DEFAULT_PATH_BUFFER_LEN * sizeof(mg_mtl_path)
options: bufferOptions]; options: bufferOptions];
backend->elementBuffer[i] = [surface->device newBufferWithLength: MG_MTL_ELEMENT_BUFFER_SIZE backend->elementBuffer[i] = [surface->device newBufferWithLength: MG_MTL_DEFAULT_ELT_BUFFER_LEN * sizeof(mg_mtl_path_elt)
options: bufferOptions]; options: bufferOptions];
} }