Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

Hollow Knight: quicker where a view needs more tiles than the cache holds (Crossroads_33, King's Pass, Greenpath): the tiles done with this frame make room first, not those still to come, and a block's other tiles are taken from its last decoding; an item hidden in a strip is not made ready; rotations without the library's sine; rows of opaque black texels at once; timing builds of views and of every room (PERF_BENCH, PERF_SURVEY)

Mason Chen94e24fc83053
diff --git a/games/hollowknight/src/gfx.c b/games/hollowknight/src/gfx.c
index c42d8b75..e01d28b9 100644
--- a/games/hollowknight/src/gfx.c
+++ b/games/hollowknight/src/gfx.c
@@ -352,11 +352,31 @@ typedef struct {
static float inst_k(const Inst *in) { return FOCAL / (in->z * (1.0f / 128) - CAM_Z); }
+/* the cosine and sine of a turn in 1/65536 (as cosf and sinf, to a float's precision): its octant, then the series on
+ * [0, pi / 4] */
+static void cos_sin_turn(int rot, float *c, float *s) {
+ uint32_t a = (uint16_t)rot, oct = a >> 13, r = a & 0x1FFF;
+ float x = (float)((oct & 1) ? 0x2000 - r : r) * (6.2831853f / 65536.0f), x2 = x * x;
+ float sn = x * (1 + x2 * (-1.0f / 6 + x2 * (1.0f / 120 + x2 * (-1.0f / 5040 + x2 * (1.0f / 362880)))));
+ float cs = 1 + x2 * (-0.5f + x2 * (1.0f / 24 + x2 * (-1.0f / 720 + x2 * (1.0f / 40320 + x2 * (-1.0f / 3628800)))));
+ switch (oct) {
+ case 0: *c = cs, *s = sn; break;
+ case 1: *c = sn, *s = cs; break;
+ case 2: *c = -sn, *s = cs; break;
+ case 3: *c = -cs, *s = sn; break;
+ case 4: *c = -cs, *s = -sn; break;
+ case 5: *c = -sn, *s = -cs; break;
+ case 6: *c = sn, *s = -cs; break;
+ default: *c = cs, *s = -sn; break;
+ }
+}
+
/* the instance's u and v steps on screen (pixels a texel) */
static void inst_axes(const Inst *in, float k, float *ux, float *uy, float *vx, float *vy) {
float a = f16(in->a) * k, b = f16(in->b) * k;
if (in->flags & F_ROT) {
- float t = in->rot * (6.2831853f / 65536.0f), c = cosf(t), s = sinf(t);
+ float c, s;
+ cos_sin_turn(in->rot, &c, &s);
/* world u = a (c, s), v = b (-s, c); screen y is down */
*ux = a * c, *uy = -a * s, *vx = -b * s, *vy = -b * c;
} else
@@ -764,6 +784,13 @@ static void run_axis(const Ctx *c, int y, int a, int b, uint16_t *tp, int ty, in
}
if (a2 && !mode) {
const uint8_t *rw = t + vr * 8;
+ if ((w2[0] & w2[1]) == 0xFFFFFFFFu && c->pal[3] >= 0xFF000000u) {
+ /* (a row of opaque black texels: nothing left from behind) */
+ cover_run(row, x, e);
+ for (; x < e; x++, p++) acc[p].gt &= 0xFFFF;
+ u = un, x = xn, p = pn;
+ continue;
+ }
for (; x < e; x++, p++, u += du) {
int ur = (u >> 16) & 31, ci = rw[ur >> 2] >> ((ur & 3) * 2) & 3;
if (ci) take_black(acc + p, c->pal[ci]);
@@ -1087,6 +1114,8 @@ static void run_bilinear(const Ctx *c, int y, int a, int b, BiTex *bt) {
__attribute__((noinline)) static void draw_item(int idx, Item *it, int sy0, int sy1) {
const Inst *in = &it->in;
int ya = it->y0 > sy0 ? it->y0 : sy0, yb = it->y1 < sy1 ? it->y1 : sy1;
+ /* (its rows from the first with light left in its box: none, nothing to make ready) */
+ while (ya < yb && open_from(cov[ya - sy0], it->x0, it->x1) >= it->x1) ya++;
if (ya >= yb) return;
/* (its palette, unless another took its place: more than NPAL this frame, it is made again) */
const Pal *pp = &pals[it->pal];
@@ -1425,6 +1454,12 @@ __attribute__((noinline)) static void finish_strip(int sy0, int rows) {
strip_clear();
}
+/* the items that kept a slot for this frame look for their tile again */
+void gfx_slot_reused(int s) {
+ for (int i = 0; i < arena_top; i++)
+ if (arena[i] == s) arena[i] = UNRES_SLOT;
+}
+
/* a rotated item's slots (all its tiles) or an upright one's (a row), in the arena: false if it is full */
static bool arena_take(Item *it, bool keep) {
const Inst *in = &it->in;
@@ -1692,6 +1727,7 @@ void gfx_frame(void) {
#endif
for (int sy0 = 0; sy0 < VIEW_H; sy0 += STRIP_H) {
int sy1 = sy0 + STRIP_H;
+ tex_strip(sy0 / STRIP_H + 1);
memset(cov, 0, sizeof cov);
if (nruns) draw_runs(0, sy0, sy1);
for (int i = nitems - 1; i >= nscene; i--)
diff --git a/games/hollowknight/src/hk.h b/games/hollowknight/src/hk.h
index 2a027af4..dde772ef 100644
--- a/games/hollowknight/src/hk.h
+++ b/games/hollowknight/src/hk.h
@@ -69,7 +69,9 @@ const TexRec *tex_rec(uint16_t t);
const uint8_t *tex_tile(uint16_t t, int tx, int ty);
/* the same, as a cache slot (-1: an empty tile): it holds until the cache runs out of room in a frame */
int tex_slot(uint16_t t, int tx, int ty);
-const uint8_t *tex_slot_px(int s);
+const uint8_t *tex_slot_px(int s); /* (its tile's texels, the slot drawn in this strip) */
+void tex_strip(int s); /* the strip drawn from now on */
+void gfx_slot_reused(int s); /* (gfx.c: a slot drawn earlier this frame now holds another tile) */
/* where a cached tile's texels that are not clear are, 4 bits each: x0, x1 - 1, y0, y1 - 1 (at 2 bits, x in twos) */
uint32_t tex_slot_box(const uint8_t *px);
extern bool g_tex_overload;
diff --git a/games/hollowknight/src/main.c b/games/hollowknight/src/main.c
index 5bca997f..a19f1400 100644
--- a/games/hollowknight/src/main.c
+++ b/games/hollowknight/src/main.c
@@ -18,7 +18,65 @@ uint32_t perf_updates __attribute__((used));
#define TICK_MS 20 /* the game's step (Unity's fixed step) */
#define MAX_TICKS 4 /* steps caught up before a frame is drawn */
-#ifdef PERF_ROOM
+#ifdef PERF_BENCH
+/* timing: a few views (rooms by number), panning a little: each one's ms per frame x 100 (read by the emulator; the
+ * last view there only so that it reads the one before) */
+static const struct { int room; float x, y; } bench[] = {{0, 30, 14.11f}, {0, 186, 68}, {0, 60, 30}, {5, 52, 10}, {35, 14, 104},
+ {10, 22, 25}, {32, 18, 66}, {20, 25, 22}, {1, 82, 10}, {0, 30, 14}};
+uint32_t perf_bench[sizeof bench / sizeof bench[0]] __attribute__((used));
+int main(void) {
+ plat_begin();
+ hk_bin = hk_data;
+ for (unsigned i = 0; i < sizeof bench / sizeof bench[0]; i++) {
+ room_load(bench[i].room);
+ g_cam_x = bench[i].x, g_cam_y = bench[i].y;
+ for (int f = 0; f < 3; f++) gfx_frame();
+ uint32_t t0 = plat_millis();
+ for (int f = 0; f < 12; f++) {
+ g_cam_x += 0.1f;
+ gfx_frame();
+ perf_frames++;
+ }
+ perf_bench[i] = (plat_millis() - t0) * 100 / 12;
+ }
+ return plat_end();
+}
+#elif defined(PERF_SURVEY)
+/* timing: every room, the view at each of its gates and its middle, panning a little; per room: frames, ms, the
+ * slowest spot's ms per frame (x10) and where (read by the emulator) */
+uint32_t perf_survey[NUM_ROOMS][5] __attribute__((used));
+int main(void) {
+ plat_begin();
+ hk_bin = hk_data;
+ for (int r = 0; r < NUM_ROOMS; r++) {
+ if (!room_load(r)) continue;
+ float w = g_room.h->w, h = g_room.h->h;
+ float spots[33][2];
+ int ns = 0, n;
+ const Ent *es = room_ents(&n);
+ spots[ns][0] = w / 2, spots[ns][1] = h / 2, ns++;
+ for (int i = 0; i < n && ns < 33; i++)
+ if (es[i].type == ENT_GATE) spots[ns][0] = (es[i].x0 + es[i].x1) / 2, spots[ns][1] = (es[i].y0 + es[i].y1) / 2, ns++;
+ for (int k = 0; k < ns; k++) {
+ float cx = spots[k][0], cy = spots[k][1];
+ cx = cx < 14.6f ? 14.6f : cx > w - 14.6f ? w - 14.6f : cx;
+ cy = cy < 8.3f ? 8.3f : cy > h - 8.3f ? h - 8.3f : cy;
+ g_cam_x = cx, g_cam_y = cy;
+ gfx_frame(); /* (the tiles in) */
+ uint32_t t0 = plat_millis();
+ for (int f = 0; f < 8; f++) {
+ g_cam_x = cx + 0.15f * f;
+ gfx_frame();
+ perf_frames++;
+ }
+ uint32_t ms = plat_millis() - t0;
+ perf_survey[r][0] += 8, perf_survey[r][1] += ms;
+ if (ms * 10 / 8 > perf_survey[r][2]) perf_survey[r][2] = ms * 10 / 8, perf_survey[r][3] = (uint32_t)cx, perf_survey[r][4] = (uint32_t)cy;
+ }
+ }
+ return plat_end();
+}
+#elif defined(PERF_ROOM)
/* timing: the camera panning across a room */
#ifndef PERF_X
#define PERF_X 30
diff --git a/games/hollowknight/src/tex.c b/games/hollowknight/src/tex.c
index 58420b07..e53cb706 100644
--- a/games/hollowknight/src/tex.c
+++ b/games/hollowknight/src/tex.c
@@ -13,9 +13,13 @@ static uint8_t slot_px[NSLOTS][128];
static uint16_t slot_box[NSLOTS]; /* where each tile's texels that are not clear are (tile_box) */
static uint32_t slot_key[NSLOTS];
static uint16_t slot_next[NSLOTS], slot_frame[NSLOTS];
+static uint8_t slot_strip[NSLOTS]; /* the strip of the frame it was drawn in last */
+static uint8_t cur_strip;
+static bool full; /* the last frame needed all the slots */
static uint16_t head[NHASH];
static uint16_t frame = 1, hand;
static uint8_t block_buf[BLOCK_TILES * 128];
+static int block_tex = -1, block_first; /* (whose tiles block_buf holds) */
#define tcp g_scratch /* TC_N4 */
_Static_assert(TC_N4 <= sizeof g_scratch / 2, "the tile decoder's probabilities: in g_scratch");
#define NONE_T 255
@@ -213,17 +217,26 @@ static void unlink_slot(int s) {
slot_key[s] = 0xFFFFFFFF;
}
-/* a slot to reuse: one unused for a while, else one not drawn this frame, else any */
+/* a slot to reuse: one unused for a while; else one not drawn yet this frame (drawn last frame, maybe out of the view
+ * now), but when the view needs more tiles than there are slots, first one drawn this frame above the strip and the
+ * one before it (done with, until the next frame); else any */
static int victim(void) {
- int last = -1;
+ int ahead = -1, done = -1;
for (int n = 0; n < NSLOTS; n++) {
int s = hand;
hand = (uint16_t)((hand + 1) % NSLOTS);
uint16_t age = (uint16_t)(frame - slot_frame[s]);
if (age > 1) return s;
- if (age == 1 && last < 0) last = s;
+ if (!age && slot_strip[s] + 1 < cur_strip) {
+ if (done < 0) done = s;
+ } else if (age == 1 && ahead < 0)
+ ahead = s;
}
- if (last >= 0) return last;
+ if (done >= 0 && (full || ahead < 0)) {
+ gfx_slot_reused(done); /* (the frame's items that kept it: they look again) */
+ return done;
+ }
+ if (ahead >= 0) return ahead;
g_tex_overload = true;
int s = hand; /* the view needs more tiles than the cache holds */
hand = (uint16_t)((hand + 1) % NSLOTS);
@@ -273,7 +286,11 @@ static int stale_slot(void) {
return -1;
}
-const uint8_t *tex_slot_px(int s) { return slot_px[s]; }
+const uint8_t *tex_slot_px(int s) {
+ slot_strip[s] = cur_strip;
+ return slot_px[s];
+}
+void tex_strip(int s) { cur_strip = (uint8_t)s; }
/* where a block's tiles are (by rank: row by row), and so which of them is each one's left and upper neighbor */
@@ -307,7 +324,7 @@ int tex_slot(uint16_t t, int tx, int ty) {
uint32_t key = (uint32_t)t << 16 | (uint32_t)rank;
int s = find(key);
if (s >= 0) {
- slot_frame[s] = frame;
+ slot_frame[s] = frame, slot_strip[s] = cur_strip;
return s;
}
g_tex_misses++;
@@ -319,12 +336,15 @@ int tex_slot(uint16_t t, int tx, int ty) {
/* (the first block after the map and where the others start) */
const uint8_t *d = sec_tdat + r->off, *starts = d + ((r->flags & TEX_FULL) ? 0 : r->th * row_stride(r));
const uint8_t *src = blk ? d + rd16(starts + 2 * (blk - 1)) : starts + 2 * ((all - 1) / BLOCK_TILES);
- uint8_t left[BLOCK_TILES], up[BLOCK_TILES];
- block_geometry(r, first, ntiles, left, up);
- decode_block(src, ntiles, r->fmt == FMT_ALPHA2, left, up, block_buf);
- g_tex_decodes++;
+ if (block_tex != t || block_first != first) { /* (the block decoded last: its tiles still there) */
+ uint8_t left[BLOCK_TILES], up[BLOCK_TILES];
+ block_geometry(r, first, ntiles, left, up);
+ decode_block(src, ntiles, r->fmt == FMT_ALPHA2, left, up, block_buf);
+ block_tex = t, block_first = first;
+ g_tex_decodes++;
+ }
int want = insert((uint32_t)t << 16 | (uint32_t)rank, block_buf + (rank - first) * bytes, bytes);
- slot_frame[want] = frame;
+ slot_frame[want] = frame, slot_strip[want] = cur_strip;
/* the block's other tiles, where they push out nothing in use */
for (int i = 0; i < ntiles; i++) {
uint32_t k = (uint32_t)t << 16 | (uint32_t)(first + i);
@@ -349,6 +369,8 @@ void tex_frame(void) {
g_tex_overload = false;
g_tex_used = 0;
for (int i = 0; i < NSLOTS; i++) g_tex_used += slot_frame[i] == frame;
+ full = g_tex_used >= NSLOTS - 16;
+ cur_strip = 0;
frame++;
if (!frame) frame = 1;
}