Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions dm/gfx/nvn/nvn_cover.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,51 @@ static inline int nvn_cover(const float (*v)[4], int n, int prim, int cull, int
}
return 0;
}

// Window copy (DM_NVN_WINDOW_COPY): a full-screen quad drawn as two triangles, which nvn_cover refuses because no single triangle holds the four corners. Six vertices (TRIANGLES), clip coordinates
// as nvn_cover takes them. Conditions: the same scissor / viewport / w / z conditions; neither triangle is degenerate or culled and both wind alike; they share one edge exactly (the same two x, y pairs, so the
// rasterizer's rule leaves no gap along it) and the other two vertices lie on opposite sides of it; the four vertices in order P, A, Q, B form a convex ring. A convex quad that holds the four NDC corners
// holds the square between them, and the square holds every pixel center of the block. Anything else does not hold.
static inline int nvn_cover_quad(const float (*v)[4], int n, int cull, int front_ccw, int has_vp, const float vp[4], int has_sc, const int sc[4], long W, long H) {
if (W <= 0 || H <= 0 || n != 6 || cull == NVN_COVER_CULL_BOTH) return 0;
if (has_sc && !(sc[0] <= 0 && sc[1] <= 0 && (long)sc[0] + sc[2] >= W && (long)sc[1] + sc[3] >= H)) return 0;
if (has_vp && !(vp[0] == 0 && vp[1] == 0 && vp[2] == (float)W && vp[3] == (float)H)) return 0;
for (int k = 0; k < n; k++)
if (v[k][3] != 1.0f || !(v[k][2] >= 0.0f && v[k][2] <= 1.0f) || !isfinite(v[k][0]) || !isfinite(v[k][1])) return 0;
#define NQ_X(i) ((double)v[i][0])
#define NQ_Y(i) ((double)v[i][1])
#define NQ_CROSS(a, b, c) ((NQ_X(b) - NQ_X(a)) * (NQ_Y(c) - NQ_Y(a)) - (NQ_Y(b) - NQ_Y(a)) * (NQ_X(c) - NQ_X(a)))
#define NQ_SAME(a, b) (v[a][0] == v[b][0] && v[a][1] == v[b][1])
double d0 = NQ_CROSS(0, 1, 2), d1 = NQ_CROSS(3, 4, 5);
if (d0 == 0 || d1 == 0 || (d0 > 0) != (d1 > 0)) return 0;
int front = front_ccw ? d0 > 0 : d0 < 0;
if ((cull == NVN_COVER_CULL_BACK && !front) || (cull == NVN_COVER_CULL_FRONT && front)) return 0;
// The shared edge: exactly two vertices of the first triangle equal to vertices of the second; the first triangle is rotated to (P, A, B) with A, B the shared ones
int sh0[3] = {-1, -1, -1}, sh1[3] = {-1, -1, -1}, ns = 0;
for (int i = 0; i < 3; i++) for (int j = 3; j < 6; j++) if (NQ_SAME(i, j)) { sh0[i] = j; sh1[j - 3] = i; ns++; }
if (ns != 2) return 0;
int p = -1; for (int i = 0; i < 3; i++) if (sh0[i] < 0) p = i;
int q = -1; for (int j = 0; j < 3; j++) if (sh1[j] < 0) q = j + 3;
if (p < 0 || q < 0) return 0;
int a = (p + 1) % 3, b = (p + 2) % 3; // t0 = (P, A, B): the shared edge A→B follows t0's winding, so t1 must run it as B→A
int qa = -1, qb = -1; for (int j = 0; j < 3; j++) { if (NQ_SAME(a, 3 + j)) qa = 3 + j; if (NQ_SAME(b, 3 + j)) qb = 3 + j; }
if (qa < 0 || qb < 0 || (qb - 3 + 1) % 3 != qa - 3) return 0; // in t1, B is followed by A
double sp = NQ_CROSS(a, b, p), sq = NQ_CROSS(a, b, q);
if (sp == 0 || sq == 0 || (sp > 0) == (sq > 0)) return 0;
int ring[4] = {p, a, q, b}; double sgn = d0 > 0 ? 1 : -1;
for (int i = 0; i < 4; i++) { // convex: every turn has the winding's sign (a straight one is allowed)
int u = ring[i], w = ring[(i + 1) % 4], x = ring[(i + 2) % 4];
if (sgn * NQ_CROSS(u, w, x) < 0) return 0;
}
static const double C[4][2] = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
for (int c = 0; c < 4; c++) for (int i = 0; i < 4; i++) {
int u = ring[i], w = ring[(i + 1) % 4];
double e = (NQ_X(w) - NQ_X(u)) * (C[c][1] - NQ_Y(u)) - (NQ_Y(w) - NQ_Y(u)) * (C[c][0] - NQ_X(u));
if (sgn * e < 0) return 0;
}
#undef NQ_X
#undef NQ_Y
#undef NQ_CROSS
#undef NQ_SAME
return 1;
}
310 changes: 297 additions & 13 deletions dm/gfx/nvn/nvn_mtl.m

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions dm/gfx/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,10 @@ t_logic: t_logic.m $(MTL_T_DEP)
clang $(CFLAGS) -fobjc-arc -o $@ t_logic.m $(MTL_T_SRC) $(FW)
check_t_logic: t_logic
@set -e; work=$$(mktemp -d); trap 'rm -rf "$$work"' EXIT; python3 test_dmsh_logic.py --fixture "$$work"; MTL_DEBUG_LAYER=1 ./t_logic "$$work"

# Window-copy retirement, present references and late-access guards.
t_window_copy: t_window_copy.m $(MTL_T_DEP)
clang $(CFLAGS) -fobjc-arc -o $@ t_window_copy.m $(MTL_T_SRC) $(FW)
.PHONY: check_t_window_copy
check_t_window_copy: t_window_copy
MTL_DEBUG_LAYER=1 ./t_window_copy
28 changes: 28 additions & 0 deletions dm/gfx/tests/t_cover.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ int main(void) {
t("neg: full-screen quad strip (no single triangle covers it; conservative)", 0, nvn_cover(quad, 4, 5, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: both faces culled", 0, nvn_cover(big, 3, 4, 3, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: primitive is lines (not triangles)", 0, nvn_cover(big, 3, 1, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
// Two-triangle quad (nvn_cover_quad): P, A, B | B, A, Q with the diagonal A-B shared
const float q_full[6][4] = {{-1, -1, 0.5f, 1}, {1, -1, 0.5f, 1}, {1, 1, 0.5f, 1}, {1, 1, 0.5f, 1}, {1, -1, 0.5f, 1}, {-1, 1, 0.5f, 1}}; // not a quad on purpose: see below
const float qd[6][4] = {{-1, -1, 0.5f, 1}, {1, -1, 0.5f, 1}, {1, 1, 0.5f, 1}, {-1, -1, 0.5f, 1}, {1, 1, 0.5f, 1}, {-1, 1, 0.5f, 1}}; // counterclockwise, diagonal (-1,-1)-(1,1)
const float qd_cw[6][4] = {{-1, -1, 0.5f, 1}, {1, 1, 0.5f, 1}, {1, -1, 0.5f, 1}, {-1, -1, 0.5f, 1}, {-1, 1, 0.5f, 1}, {1, 1, 0.5f, 1}}; // the same quad clockwise
const float qd_big[6][4] = {{-1.5f, -1.5f, 0, 1}, {2, -1.5f, 0, 1}, {2, 2, 0, 1}, {-1.5f, -1.5f, 0, 1}, {2, 2, 0, 1}, {-1.5f, 2, 0, 1}};
const float qd_small[6][4] = {{-1, -1, 0.5f, 1}, {0.9f, -1, 0.5f, 1}, {0.9f, 1, 0.5f, 1}, {-1, -1, 0.5f, 1}, {0.9f, 1, 0.5f, 1}, {-1, 1, 0.5f, 1}}; // misses the right strip
const float qd_gap[6][4] = {{-1, -1, 0.5f, 1}, {1, -1, 0.5f, 1}, {1, 1, 0.5f, 1}, {-1, -1, 0.5f, 1}, {1.001f, 1, 0.5f, 1}, {-1, 1, 0.5f, 1}}; // the diagonal's end is not shared exactly
const float qd_fold[6][4] = {{-1, -1, 0.5f, 1}, {1, -1, 0.5f, 1}, {1, 1, 0.5f, 1}, {-1, -1, 0.5f, 1}, {1, 1, 0.5f, 1}, {1, -1, 0.5f, 1}}; // second triangle folds back onto the first
const float qd_z[6][4] = {{-1, -1, 1.5f, 1}, {1, -1, 1.5f, 1}, {1, 1, 1.5f, 1}, {-1, -1, 1.5f, 1}, {1, 1, 1.5f, 1}, {-1, 1, 1.5f, 1}};
const float qd_w[6][4] = {{-2, -2, 0.5f, 2}, {2, -2, 0.5f, 2}, {2, 2, 0.5f, 2}, {-2, -2, 0.5f, 2}, {2, 2, 0.5f, 2}, {-2, 2, 0.5f, 2}};
const float qd_dart[6][4] = {{-1, -1, 0.5f, 1}, {3, 0, 0.5f, 1}, {0, 0, 0.5f, 1}, {-1, -1, 0.5f, 1}, {0, 0, 0.5f, 1}, {0, 3, 0.5f, 1}}; // a reflex ring around the diagonal: not convex
t("quad of two triangles, viewport = attachment", 1, nvn_cover_quad(qd, 6, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
t("quad of two triangles, no viewport, full scissor, back-face culling", 1, nvn_cover_quad(qd, 6, 2, 1, 0, NULL, 1, sc_full, 1600, 900));
t("quad larger than the screen", 1, nvn_cover_quad(qd_big, 6, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
t("quad clockwise, clockwise is front", 1, nvn_cover_quad(qd_cw, 6, 2, 0, 1, vp_full, 0, NULL, 1600, 900));
t("neg: quad clockwise, counterclockwise is front, back-face culling", 0, nvn_cover_quad(qd_cw, 6, 2, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: the same two triangles, nvn_cover refuses (neither holds all corners)", 0, nvn_cover(qd, 3, 4, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: quad misses the right strip", 0, nvn_cover_quad(qd_small, 6, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: diagonal end not shared exactly (a possible gap)", 0, nvn_cover_quad(qd_gap, 6, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: second triangle folds back over the first", 0, nvn_cover_quad(qd_fold, 6, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: reflex ring (not convex)", 0, nvn_cover_quad(qd_dart, 6, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: z outside [0,1]", 0, nvn_cover_quad(qd_z, 6, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: w != 1", 0, nvn_cover_quad(qd_w, 6, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: viewport covers half", 0, nvn_cover_quad(qd, 6, 0, 1, 1, vp_half, 0, NULL, 1600, 900));
t("neg: scissor covers the top half", 0, nvn_cover_quad(qd, 6, 0, 1, 1, vp_full, 1, sc_part, 1600, 900));
t("neg: both faces culled", 0, nvn_cover_quad(qd, 6, 3, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: five vertices", 0, nvn_cover_quad(qd, 5, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
t("neg: two triangles sharing only a corner", 0, nvn_cover_quad(q_full, 6, 0, 1, 1, vp_full, 0, NULL, 1600, 900));
printf("== %d checks, %d failed ==\n", total, fail);
return fail != 0;
}
6 changes: 3 additions & 3 deletions dm/gfx/tests/t_image_primitive_id.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ void svc_thread_dump(void) {}
static prog_t program = {.serial = 99902};
int main(void) { @autoreleasepool {
mtl_init();
image.d = (nvn_texdesc){.target = NVN_TEXTURE_TARGET_2D_ARRAY, .format = NVN_FORMAT_RGBA8, .width = 8, .height = 8, .depth = 4, .levels = 1, .samples = 1};
MTLTextureDescriptor *td = tex_desc(&image, 0); td.usage = MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
id<MTLTexture> texture = [g_dev newTextureWithDescriptor:td]; res_add(texture); mtl_tex_init(&image); serial_put(image.serial, texture); mtl_reg_image(701, &image, NULL);
NSError *error = nil;
// A .metallib loaded as the host loads game shaders (lib_from_file)
static const char *src = "#include <metal_stdlib>\nusing namespace metal;\n"
Expand All @@ -32,6 +29,9 @@ int main(void) { @autoreleasepool {
}
id<MTLLibrary> lib = lib_from_file(ml, &error);
if (!lib) { fprintf(stderr, "%s\n", error.localizedDescription.UTF8String); return 1; }
image.d = (nvn_texdesc){.target = NVN_TEXTURE_TARGET_2D_ARRAY, .format = NVN_FORMAT_RGBA8, .width = 8, .height = 8, .depth = 4, .levels = 1, .samples = 1};
MTLTextureDescriptor *td = tex_desc(&image, 0); td.usage = MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
id<MTLTexture> texture = [g_dev newTextureWithDescriptor:td]; res_add(texture); mtl_tex_init(&image); serial_put(image.serial, texture); mtl_reg_image(701, &image, NULL);
NvProg *pr = [NvProg new]; pr->sh[0] = [NvShader new]; pr->sh[0]->fn = fn_spec(lib, @"v", nil, &error); // through the Metal 4 bases, as the host loads shaders
NvShader *fs = [NvShader new]; pr->sh[1] = fs; fs->stage = 1; fs->fn = fn_spec(lib, @"f", nil, &error);
fs->rf.reads_prim_id = 1; // as the reflection records it (raster.primitive_id): pso_build_ex builds the pipeline whole
Expand Down
2 changes: 1 addition & 1 deletion dm/gfx/tests/t_image_usage.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static void image_case(int explicit_image) { @autoreleasepool {
CHECK(rid == image.gpuResourceID._impl, "image argument names the real view");
os_unfair_lock_unlock(&g_rmu);
mtl_sub_commit(); mtl_drain();
uint16_t pixels[64]; read_level(upgraded, 0, pixels);
uint16_t pixels[64] = {0}; read_level(upgraded, 0, pixels);
int same = 1; for (int i = 0; i < 64; i++) same &= pixels[i] == 0x3800; CHECK(same, "upgrade preserves the clear from the pass it ended");
read_level(upgraded, 1, pixels); same = 1; for (int i = 0; i < 16; i++) same &= pixels[i] == 0x3a00; CHECK(same, "upgrade preserves the other mip");
NSError *error = nil;
Expand Down
Loading
Loading