Skip to content
48 changes: 39 additions & 9 deletions vk/src/mali_soft.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,28 @@ bop(VkBlendOp op, float src, float dst)
}
}

/* An edge of the triangle as the weight functions traverse it (v_a -> v_b), already multiplied by the sign that makes
* the interior weights positive. In framebuffer coordinates (y grows down) a top edge is horizontal with the interior
* below it, and a left edge runs upwards: those own the samples that lie exactly on them. The other edges must not,
* or the neighbour on the shared edge covers the same sample too. */
static bool
edge_is_top_left(float ex, float ey)
{
return ey < 0.0f || (ey == 0.0f && ex > 0.0f);
}

/* Always evaluate shared endpoints in the same order, so reversed edges are exact
* negatives even when floating-point evaluation rounds. Use framebuffer-space
* values before normalizing by the (different) triangle areas. */
static double
edge_value(float ax, float ay, float bx, float by, float px, float py)
{
if (ax > bx || (ax == bx && ay > by))
return -edge_value(bx, by, ax, ay, px, py);
return ((double)bx - ax) * ((double)py - ay) -
((double)by - ay) * ((double)px - ax);
}

/* ---- one triangle: viewport, cull, scissor, depth, fragment, blend, store ---- */
struct rctx {
const struct mali_frame *f;
Expand Down Expand Up @@ -397,8 +419,8 @@ raster_tri(struct rctx *c, const float clip[3][4], const float vary[3][MALI_SOFT
sz[i] = clip[i][2] / clip[i][3];
iw[i] = 1.0f / clip[i][3];
}
const float area = (sx[1] - sx[0]) * (sy[2] - sy[0]) - (sx[2] - sx[0]) * (sy[1] - sy[0]);
if (fabsf(area) < 1e-9f)
const double area = edge_value(sx[0], sy[0], sx[1], sy[1], sx[2], sy[2]);
if (fabs(area) < 1e-9)
return;
/* signed area in framebuffer coordinates (y down): the positive winding is counter-clockwise in NDC */
const bool front = (area > 0.0f) == front_ccw;
Expand All @@ -413,7 +435,7 @@ raster_tri(struct rctx *c, const float clip[3][4], const float vary[3][MALI_SOFT
d->vp[4] + sz[1] * (d->vp[5] - d->vp[4]),
d->vp[4] + sz[2] * (d->vp[5] - d->vp[4])};
const float s = area > 0 ? 1.0f : -1.0f; /* make the edge functions positive inside */
const float iarea = 1.0f / (area * s);
const double iarea = 1.0 / (area * s);

int x0 = (int)floorf(fminf(sx[0], fminf(sx[1], sx[2])));
int x1 = (int)ceilf(fmaxf(sx[0], fmaxf(sx[1], sx[2])));
Expand Down Expand Up @@ -444,13 +466,21 @@ raster_tri(struct rctx *c, const float clip[3][4], const float vary[3][MALI_SOFT
for (unsigned y = (unsigned)y0; y < ye; y++) {
for (unsigned x = (unsigned)x0; x < xe; x++) {
const float px = (float)x + 0.5f, py = (float)y + 0.5f;
float w0 = ((sx[2] - sx[1]) * (py - sy[1]) - (sy[2] - sy[1]) * (px - sx[1])) * s;
float w1 = ((sx[0] - sx[2]) * (py - sy[2]) - (sy[0] - sy[2]) * (px - sx[2])) * s;
float w2 = ((sx[1] - sx[0]) * (py - sy[0]) - (sy[1] - sy[0]) * (px - sx[0])) * s;
w0 *= iarea; w1 *= iarea; w2 *= iarea;
if (w0 < 0 || w1 < 0 || w2 < 0)
const double e0 = edge_value(sx[1], sy[1], sx[2], sy[2], px, py) * s;
const double e1 = edge_value(sx[2], sy[2], sx[0], sy[0], px, py) * s;
const double e2 = edge_value(sx[0], sy[0], sx[1], sy[1], px, py) * s;
if (e0 < 0 || e1 < 0 || e2 < 0)
continue;
/* Vulkan's fill rule: a sample exactly on an edge is covered only when that edge is a top or left edge, so
* two triangles sharing a diagonal cover the seam exactly once instead of both (with blending that is a
* line along every shared edge; with the depth test on the second write usually hid it). */
if ((e0 == 0 && !edge_is_top_left((sx[2] - sx[1]) * s, (sy[2] - sy[1]) * s)) ||
(e1 == 0 && !edge_is_top_left((sx[0] - sx[2]) * s, (sy[0] - sy[2]) * s)) ||
(e2 == 0 && !edge_is_top_left((sx[1] - sx[0]) * s, (sy[1] - sy[0]) * s)))
continue;
const float zn = w0 * sz[0] + w1 * sz[1] + w2 * sz[2]; /* NDC depth, clipped against [0, 1] */
const float w0 = (float)(e0 * iarea), w1 = (float)(e1 * iarea), w2 = (float)(e2 * iarea);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
/* Nonnegative edge weights preserve zero and constant far-plane depth. */
const float zn = (float)((e0 * sz[0] + e1 * sz[1] + e2 * sz[2]) / (e0 + e1 + e2));
if (depth_clip && (zn < 0.0f || zn > 1.0f))
continue;
/* the framebuffer depth the test and the store see is the viewport transform of the NDC depth */
Expand Down
20 changes: 10 additions & 10 deletions vk/tests/golden/soft-frame.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,37 +81,37 @@ case mrt-rt1 128x128
case mrt4-rt1-bgra8 64x64
row0: 000000 000000
row1: 000000 00bf80
stats draws=1 tris=2 frags=1056 unsupported=0
stats draws=1 tris=2 frags=1024 unsupported=0
case mrt4-rt2-rgba16f 64x64
row0: 000000 000000
row1: 000000 8040ff
stats draws=1 tris=2 frags=1056 unsupported=0
stats draws=1 tris=2 frags=1024 unsupported=0
case mrt4-rt3-load-rgba32f 64x64
row0: 4080bf 4080bf
row1: 4080bf 808080
stats draws=1 tris=2 frags=1056 unsupported=0
stats draws=1 tris=2 frags=1024 unsupported=0
case mrt-blend-rt1-rgba16f 64x64
row0: 804020 804020
row1: 804020 b4daff
stats draws=1 tris=2 frags=1056 unsupported=0
row1: 804020 b2d9ff
stats draws=1 tris=2 frags=1024 unsupported=0
case mrt-blend-rt2-constant 64x64
row0: 0d2640 0d2640
row1: 0d2640 592d08
stats draws=1 tris=2 frags=1056 unsupported=0
stats draws=1 tris=2 frags=1024 unsupported=0
case mrt-blend-rt3-subtract 64x64
row0: e6e6e6 e6e6e6
row1: e6e6e6 e6e698
stats draws=1 tris=2 frags=1056 unsupported=0
row1: e6e6e6 e6e69a
stats draws=1 tris=2 frags=1024 unsupported=0
case load-partial 128x128
row0: 0000ff 008080 008080 0000ff
row1: 0000ff 008080 008080 0000ff
row2: 0000ff 008080 ff0000 ff0000
row3: 0000ff 008080 ff0000 ff0000
stats draws=1 tris=2 frags=4160 unsupported=0
stats draws=1 tris=2 frags=4096 unsupported=0
case halves 128x128
row0: 202080 602080 9f2080 df2080
row1: 206080 606080 9f6080 df6080
row2: 209f80 609f80 9f9f80 df9f80
row3: 20df80 60df80 9fdf80 dfdf80
stats draws=1 tris=2 frags=8256 unsupported=0
stats draws=1 tris=2 frags=8192 unsupported=0
PASS
103 changes: 99 additions & 4 deletions vk/tests/t_soft_frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ case_rgba8(void)
CHECK(c[1] > 0.9f && c[0] < 0.1f, "bottom-left is not the green gradient corner: %f,%f,%f", c[0], c[1], c[2]);
tilemap("rgba8-gradient", &s);
p_DestroyPipeline(dev, s.pipe, NULL); s.pipe = 0;

/* The shared diagonal crosses 256 pixel centres. With depth testing off, shading it twice yields 65792
* fragments, while Vulkan's top-left rule gives exactly one fragment per pixel. */
CHECK(scene_pipe(&s, &PROG_GRAD, 0, 0, 0, 0xf), "pipe seam");
CHECK(scene_draw(&s, 0, 6), "draw quad, depth off");
const unsigned seam_frags = mali_soft_last()->frags;
CHECK(seam_frags == s.w * s.h, "shared edge shaded once: %u fragments, want %u", seam_frags, s.w * s.h);
p_DestroyPipeline(dev, s.pipe, NULL); s.pipe = 0;

/* depth, one pass: far red (z .9), near green (z .1), far blue (z .9) again -> the centre stays green */
CHECK(scene_pipe(&s, &PROG_COL, 0, 0, 1, 0xf), "pipe colour");
const unsigned df[3] = {RED, GREEN, BLUE}, dc[3] = {3, 3, 3};
Expand Down Expand Up @@ -560,7 +569,8 @@ static const struct mali_soft_program PROG_BLEND = {.name = "mrt-blend", .vs = v
* c == NULL: the corner colours differ so every 32x32 tile of a covered area has its own value). */
static int
hand_quad(struct mali_frame *f, const struct mali_hw *hw, const void *vskey, const void *fskey, float x0, float y0,
float x1, float y1, const float *c, const struct mali_fixed *fx, float fspush)
float x1, float y1, const float *c, const struct mali_fixed *fx, float fspush,
const float (*vertices)[2], const float *viewport)
{
uint64_t va;
float (*v)[8] = hw->alloc(hw->user, 6 * sizeof *v, 64, &va);
Expand All @@ -569,7 +579,7 @@ hand_quad(struct mali_frame *f, const struct mali_hw *hw, const void *vskey, con
const float cc[4][4] = {{1, 0, 0, 1}, {0, 1, 0, 1}, {0, 0, 1, 1}, {1, 1, 1, 1}};
const unsigned ci[6] = {0, 1, 2, 2, 1, 3};
for (unsigned i = 0; i < 6; i++) {
v[i][0] = px[i][0]; v[i][1] = px[i][1]; v[i][2] = 0.5f; v[i][3] = 1;
v[i][0] = vertices ? vertices[i][0] : px[i][0]; v[i][1] = vertices ? vertices[i][1] : px[i][1]; v[i][2] = viewport ? 1.0f : 0.5f; v[i][3] = 1;
memcpy(&v[i][4], c ? c : cc[ci[i]], 4 * sizeof(float));
if (c) v[i][7] = 1;
}
Expand All @@ -578,7 +588,8 @@ hand_quad(struct mali_frame *f, const struct mali_hw *hw, const void *vskey, con
d.gfx.vs = vskey; d.gfx.fs = fskey; d.gfx.prim = 8; d.gfx.nattr = 2;
d.gfx.attr[0] = (struct mali_attr){.binding = 0, .offset = 0, .stride = 32, .vk_format = VK_FORMAT_R32G32B32A32_SFLOAT};
d.gfx.attr[1] = (struct mali_attr){.binding = 0, .offset = 16, .stride = 32, .vk_format = VK_FORMAT_R32G32B32A32_SFLOAT};
d.vp[2] = (float)f->width; d.vp[3] = (float)f->height; d.vp[5] = 1.0f;
d.vp[2] = (float)f->width; d.vp[3] = (float)f->height; d.vp[5] = 1.0f;
if (viewport) memcpy(d.vp, viewport, 4 * sizeof(float));
d.sc[2] = (int32_t)f->width; d.sc[3] = (int32_t)f->height;
d.vb_va[0] = va; d.vb_size[0] = 6 * sizeof *v;
d.count = 6; d.instance_count = 1;
Expand Down Expand Up @@ -614,13 +625,94 @@ hand_frame(const struct scene *const *rt, unsigned n, const struct mali_att *att
}
f.area[0] = ax; f.area[1] = ay; f.area[2] = aw; f.area[3] = ah;
mali_soft_bind_program(vk, fk, prog);
if (!hand_quad(&f, hw, vk, fk, qx0, qy0, qx1, qy1, col, fx, fspush)) { mali_frame_finish(&f); return 0; }
if (!hand_quad(&f, hw, vk, fk, qx0, qy0, qx1, qy1, col, fx, fspush, NULL, NULL)) { mali_frame_finish(&f); return 0; }
const bool ok = mali_soft_frame(hw, &f);
mali_frame_finish(&f);
CHECK(ok, "soft frame: %s", mali_soft_last()->why);
return 1;
}

static int
case_unequal_edges(void)
{
struct scene s;
const float black[4] = {0, 0, 0, 1}, white[4] = {1, 1, 1, 1};
CHECK(scene_init(&s, 4096, 4, VK_FORMAT_R8G8B8A8_UNORM, 0, black, 1), "init edge scene");
const struct mali_hw *hw = mali_soft_device_hw(dev);
CHECK(hw, "device hw context");
struct mali_frame f = {.width = 4096, .height = 4, .samples = 1, .has_colour = true, .ncolour = 1,
.area = {0, 0, 4096, 4}, .colour = {.clear = true, .clear_color = {0, 0, 0, 1}}};
CHECK(hand_target(&s, &f.colour), "target");
/* A,C,B and A,B,D: the sample (1024.5,0.5) is just inside the large triangle. */
const float xy[6][2] = {
{1024.4990234375f, 0}, {3072.5f, 0}, {1024.4990234375f, 4},
{1024.4990234375f, 0}, {1024.4990234375f, 4}, {1023.4990234375f, 0}
};
float clip[6][2];
for (unsigned i = 0; i < 6; i++) {
clip[i][0] = xy[i][0] / 2048.0f - 1.0f;
clip[i][1] = xy[i][1] / 2.0f - 1.0f;
}
mali_soft_bind_program((const void *)&key_mrt_vs, (const void *)&key_mrt_fs, &PROG_MRT);
CHECK(hand_quad(&f, hw, &key_mrt_vs, &key_mrt_fs, 0, 0, 0, 0, white, NULL, 0, clip, NULL), "triangles");
CHECK(mali_soft_frame(hw, &f), "edge frame");
mali_frame_finish(&f);
float c[4]; readpx(&s, 1024, 0, c);
CHECK(c[0] > 0.9f, "unequal-area shared edge lost interior sample: %f", c[0]);
scene_free(&s);
return 1;
}

static int
case_left_edge(void)
{
struct scene s;
const float black[4] = {0, 0, 0, 1}, white[4] = {1, 1, 1, 1};
CHECK(scene_init(&s, 4, 4, VK_FORMAT_R8G8B8A8_UNORM, 0, black, 1), "init left edge scene");
const struct mali_hw *hw = mali_soft_device_hw(dev);
struct mali_frame f = {.width = 4, .height = 4, .samples = 1, .has_colour = true, .ncolour = 1,
.area = {0, 0, 4, 4}, .colour = {.clear = true, .clear_color = {0, 0, 0, 1}}};
CHECK(hw && hand_target(&s, &f.colour), "target");
mali_soft_bind_program((const void *)&key_mrt_vs, (const void *)&key_mrt_fs, &PROG_MRT);
CHECK(hand_quad(&f, hw, &key_mrt_vs, &key_mrt_fs, -.75f, -.75f, .25f, .25f,
white, NULL, 0, NULL, NULL), "quad");
CHECK(mali_soft_frame(hw, &f), "left edge frame");
mali_frame_finish(&f);
float c[4];
readpx(&s, 0, 1, c); CHECK(c[0] > .9f, "left edge excluded: %f", c[0]);
readpx(&s, 2, 1, c); CHECK(c[0] < .1f, "right edge included: %f", c[0]);
scene_free(&s);
return 1;
}

static int
case_far_depth_edge(void)
{
struct scene s;
const float black[4] = {0, 0, 0, 1}, white[4] = {1, 1, 1, 1};
CHECK(scene_init(&s, 1, 1, VK_FORMAT_R8G8B8A8_UNORM, 0, black, 1), "init far depth scene");
const struct mali_hw *hw = mali_soft_device_hw(dev);
struct mali_frame f = {.width = 1, .height = 1, .samples = 1, .has_colour = true, .ncolour = 1,
.area = {0, 0, 1, 1}, .colour = {.clear = true, .clear_color = {0, 0, 0, 1}}};
CHECK(hw && hand_target(&s, &f.colour), "target");
const float viewport[4] = {-6002, -6001, 8192, 8192};
const float xy[6][2] = {{0, 0}, {8192, 8191}, {8191, 8189.5f},
{0, 0}, {8192, 8191}, {8191, 8189.5f}};
float clip[6][2];
for (unsigned i = 0; i < 6; i++) {
clip[i][0] = xy[i][0] / 4096.0f - 1.0f;
clip[i][1] = xy[i][1] / 4096.0f - 1.0f;
}
mali_soft_bind_program((const void *)&key_mrt_vs, (const void *)&key_mrt_fs, &PROG_MRT);
CHECK(hand_quad(&f, hw, &key_mrt_vs, &key_mrt_fs, 0, 0, 0, 0, white, NULL, 0, clip, viewport), "far triangle");
CHECK(mali_soft_frame(hw, &f), "far depth frame");
mali_frame_finish(&f);
float c[4]; readpx(&s, 0, 0, c);
CHECK(c[0] > 0.9f, "far-plane interior sample was clipped: %f", c[0]);
scene_free(&s);
return 1;
}

static int
near4(const float c[4], float r, float g, float b)
{
Expand Down Expand Up @@ -958,6 +1050,9 @@ main(int argc, char **argv)
if (!only || !strcmp(only, "mrt-blend")) ok &= case_blend();
if (!only || !strcmp(only, "load")) ok &= case_load();
if (!only || !strcmp(only, "halves")) ok &= case_halves();
if (!only || !strcmp(only, "unequal-edges")) ok &= case_unequal_edges();
if (!only || !strcmp(only, "far-depth-edge")) ok &= case_far_depth_edge();
if (!only || !strcmp(only, "left-edge")) ok &= case_left_edge();
if (!only || !strcmp(only, "fan")) ok &= case_fan();
p_DeviceWaitIdle(dev);
printf("%s\n", ok && !g_fail ? "PASS" : "FAILED");
Expand Down