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.

Snow Rider 3D: fixed-point corners on calculators without an FPU

Mason3638c15175c7845
diff --git a/games/snowrider/src/draw.c b/games/snowrider/src/draw.c
index 8ba92133..1330e4f4 100644
--- a/games/snowrider/src/draw.c
+++ b/games/snowrider/src/draw.c
@@ -135,6 +135,80 @@ static void draw_mesh(const RMesh *rm, const float W[12]) {
}
}
+#if NP_SOFT_FLOAT
+#include "np_fixed.h"
+/* (calculators without a floating-point unit) draw_mesh with each corner moved, bent and projected in integers: the
+ * mesh's matrix and the camera's turn read once into fixed point, the corners in 16.16 metres and pixels */
+static SVi vs[MAXVB];
+static int32_t vc[MAXVB][3]; /* a corner in the camera's space */
+static int ka_of(float m) { /* (the bits after the point that keep m times 2^15 within 2^45) */
+ union {
+ float f;
+ uint32_t u;
+ } v = {m};
+ int e = (int)(v.u >> 23 & 255) - 127;
+ return 29 - e > 40 ? 40 : 29 - e;
+}
+static inline int64_t shr(int64_t v, int k) { return k >= 0 ? v >> k : v << -k; }
+static void draw_mesh_fx(const RMesh *rm, const float W[12]) {
+ float s = rm->s;
+ const float *R = cam_rot;
+ int nv = rm->nv > MAXVB ? MAXVB : rm->nv;
+ float am = 0;
+ for (int k = 0; k < 12; k++)
+ if (k % 4 != 3 && fabsf(W[k] * s) > am) am = fabsf(W[k] * s);
+ int ka = ka_of(am > 0 ? am : 1);
+ int64_t A[12];
+ for (int k = 0; k < 12; k++) A[k] = k % 4 == 3 ? 0 : fx_from_float(W[k] * s, ka);
+ int64_t t0 = fx_from_float(W[3] - cam_pos[0], 16), t1 = fx_from_float(W[7] - cam_pos[1], 16),
+ t2 = fx_from_float(W[11] - cam_pos[2], 16), curv = fx_from_float(CURVATURE, 16);
+ int32_t r[9];
+ for (int k = 0; k < 9; k++) r[k] = (int32_t)fx_from_float(R[k], 30);
+ int64_t f16 = fx_from_float(cam.f, 16), cx = fx_from_float(cam.cx, 16), cy = fx_from_float(cam.cy, 16);
+ for (int i = 0; i < nv; i++) {
+ const int16_t *v = rm->v + i * 3;
+ int64_t wx = shr(A[0] * v[0] + A[1] * v[1] + A[2] * v[2], ka - 16) + t0;
+ int64_t wy = shr(A[4] * v[0] + A[5] * v[1] + A[6] * v[2], ka - 16) + t1;
+ int64_t dz = shr(A[8] * v[0] + A[9] * v[1] + A[10] * v[2], ka - 16) + t2;
+ wy -= (dz * dz >> 16) * curv >> 16;
+ int32_t c0 = fx_sat32((r[0] * wx + r[1] * wy + r[2] * dz) >> 30), c1 = fx_sat32((r[3] * wx + r[4] * wy + r[5] * dz) >> 30);
+ int32_t c2 = fx_sat32((r[6] * wx + r[7] * wy + r[8] * dz) >> 30);
+ vc[i][0] = c0, vc[i][1] = c1, vc[i][2] = c2;
+ vin[i] = c2 >= 6554; /* (0.1 m) */
+ if (vin[i]) {
+ int sh = __builtin_clz((uint32_t)c2);
+ int64_t rz = fx_rcp((uint32_t)c2 << sh); /* 2^61 / (z 2^16 2^sh) */
+ int64_t qx = (int64_t)c0 * rz >> (45 - sh), qy = (int64_t)c1 * rz >> (45 - sh); /* x / z, y / z: 16.16 */
+ vs[i].x = fx_sat32(cx + (qx * f16 >> 16)), vs[i].y = fx_sat32(cy - (qy * f16 >> 16));
+ vs[i].z = (int64_t)R_ZK * rz >> (29 - sh);
+ }
+ }
+ float det = m_det(W);
+ bool flip = det < 0;
+ float inv = 1 / (127 * cbrtf(fabsf(det) > 1e-9f ? fabsf(det) : 1));
+ int32_t l0 = (int32_t)fx_from_float((W[0] * light[0] + W[4] * light[1] + W[8] * light[2]) * inv, 24);
+ int32_t l1 = (int32_t)fx_from_float((W[1] * light[0] + W[5] * light[1] + W[9] * light[2]) * inv, 24);
+ int32_t l2 = (int32_t)fx_from_float((W[2] * light[0] + W[6] * light[1] + W[10] * light[2]) * inv, 24);
+ for (int i = 0; i < rm->nt; i++) {
+ int t0 = corner_of(rm->t, rm->nv, i * 3), t1 = corner_of(rm->t, rm->nv, i * 3 + 1), t2 = corner_of(rm->t, rm->nv, i * 3 + 2);
+ if (t0 >= nv || t1 >= nv || t2 >= nv) continue;
+ int a = t0, b = flip ? t2 : t1, c = flip ? t1 : t2;
+ int in = vin[a] + vin[b] + vin[c];
+ if (!in) continue;
+ const int8_t *n = rm->n + i * 3;
+ int32_t d = n[0] * l0 + n[1] * l1 + n[2] * l2;
+ int sh = d <= 0 ? 0 : d >= 1 << 24 ? SHADES - 1 : (int)(((int64_t)d * (SHADES - 1) + (1 << 23)) >> 24);
+ uint16_t col = shade_lut[rm->c[i]][sh];
+ if (in == 3 && r_tri_screen_fx(&vs[a], &vs[b], &vs[c], col)) continue;
+ V3 p[3];
+ const int k3[3] = {a, b, c};
+ for (int j = 0; j < 3; j++)
+ p[j] = (V3){fx_to_float(vc[k3[j]][0], 16), fx_to_float(vc[k3[j]][1], 16), fx_to_float(vc[k3[j]][2], 16)};
+ r_poly_cam(p, 3, col, 0, 255);
+ }
+}
+#endif
+
static void draw_prefab(int prefab, const float m[12]) {
const Prefab *pf = &prefabs[prefab];
const float *R = cam_rot;
@@ -150,7 +224,11 @@ static void draw_prefab(int prefab, const float m[12]) {
if (vz + r < 0.1f || vz - r > VIEW) continue;
if (fabsf(vx) - r * 1.5f > (vz + r) * (SCREEN_W / 2 + 8) / focal) continue;
if (fabsf(vy) - r * 1.5f > (vz + r) * (SCREEN_H / 2 + 8) / focal) continue;
+#if NP_SOFT_FLOAT
+ draw_mesh_fx(&rmeshes[vz - r > lod ? pt->far : pt->near], W);
+#else
draw_mesh(&rmeshes[vz - r > lod ? pt->far : pt->near], W);
+#endif
}
}
diff --git a/games/snowrider/src/r3d.c b/games/snowrider/src/r3d.c
index 1bb9ed9a..7ae3eab3 100644
--- a/games/snowrider/src/r3d.c
+++ b/games/snowrider/src/r3d.c
@@ -192,6 +192,54 @@ bool r_tri_screen(const V3 *A, const V3 *B, const V3 *C, uint16_t color) {
return true;
}
+#if NP_SOFT_FLOAT
+#include "np_fixed.h"
+/* (the same in integers: twice the area and the depth plane from the corners in 16.16, the plane kept as floats) */
+bool r_tri_screen_fx(const SVi *A, const SVi *B, const SVi *C, uint16_t color) {
+ const int32_t g = (int32_t)(GUARD * 65536), cx = (int32_t)(cam.cx * 65536), cy = (int32_t)(cam.cy * 65536);
+ if (A->x - cx > g || A->x - cx < -g || A->y - cy > g || A->y - cy < -g || B->x - cx > g || B->x - cx < -g ||
+ B->y - cy > g || B->y - cy < -g || C->x - cx > g || C->x - cx < -g || C->y - cy > g || C->y - cy < -g)
+ return false;
+ int64_t x1 = B->x - A->x, y1 = B->y - A->y, x2 = C->x - A->x, y2 = C->y - A->y;
+ int64_t det = x1 * y2 - x2 * y1;
+ if (det < 42949673) return true; /* (0.01 square pixels) */
+ int32_t x0 = A->x < B->x ? A->x : B->x, xm = A->x > B->x ? A->x : B->x;
+ int32_t y0 = A->y < B->y ? A->y : B->y, ym = A->y > B->y ? A->y : B->y;
+ if (C->x < x0) x0 = C->x;
+ if (C->x > xm) xm = C->x;
+ if (C->y < y0) y0 = C->y;
+ if (C->y > ym) ym = C->y;
+ if (xm < 0 || x0 >= SCREEN_W << 16 || ym < 0 || y0 >= SCREEN_H << 16) return true;
+ int iy0 = y0 < 0 ? 0 : (y0 + 32768) >> 16, iy1 = ym >= SCREEN_H << 16 ? SCREEN_H : (ym + 32768) >> 16;
+ if (iy0 >= iy1) return true;
+ if (npolys + nalpha >= MAXP || nverts + 3 > MAXV) return true;
+ int64_t w1 = B->z - A->z, w2 = C->z - A->z;
+ int64_t a = fx_div(w1 * y2 - w2 * y1, det, 16), b = fx_div(x1 * w2 - x2 * w1, det, 16);
+ Poly *q = &polys[npolys];
+ if (a > (int64_t)1 << 34 || a < -((int64_t)1 << 34) || b > (int64_t)1 << 34 || b < -((int64_t)1 << 34)) {
+ /* (seen nearly edge-on: as r_tri_screen) */
+ float fx1 = fx_to_float(x1, 16), fy1 = fx_to_float(y1, 16), fx2 = fx_to_float(x2, 16), fy2 = fx_to_float(y2, 16);
+ float fw1 = fx_to_float(w1, 16), fw2 = fx_to_float(w2, 16), inv = 1 / fx_to_float(det, 32);
+ q->a = (fw1 * fy2 - fw2 * fy1) * inv, q->b = (fx1 * fw2 - fx2 * fw1) * inv;
+ q->c = fx_to_float(A->z, 16) - q->a * fx_to_float(A->x, 16) - q->b * fx_to_float(A->y, 16);
+ } else {
+ q->a = fx_to_float(a, 16), q->b = fx_to_float(b, 16);
+ q->c = fx_to_float(A->z - (a * A->x >> 16) - (b * A->y >> 16), 16);
+ }
+ q->alpha = 255, q->color = color;
+ q->v0 = (uint16_t)nverts, q->n = 3;
+ const SVi *v[3] = {A, B, C};
+ for (int i = 0; i < 3; i++) {
+ int32_t px = v[i]->x * 8 + 32768, py = v[i]->y * 8 + 32768; /* (as (int16_t)(x * SUB + 0.5f)) */
+ pos[nverts + i] = (Pos){(int16_t)(px < 0 ? -(-px >> 16) : px >> 16), (int16_t)(py < 0 ? -(-py >> 16) : py >> 16)};
+ }
+ q->y0 = (uint8_t)iy0, q->y1 = (uint8_t)iy1;
+ nverts += 3;
+ npolys++;
+ return true;
+}
+#endif
+
/* ---------------------------------------------------------------- drawing a band */
/* an opaque run of n pixels: each one nearer than what is there takes the colour (two at a time on the calculator:
* its DSP compares and picks both halves of a word at once) */
@@ -354,7 +402,6 @@ __attribute__((noinline)) static void raster_tri(const Poly *q) {
/* ---------------------------------------------------------------- the same in integers
* (calculators without a floating-point unit: a polygon's depth plane read once a band, its edges stepped exactly from
* the corners' 1/8 pixels) */
-#include "np_fixed.h"
/* the depth plane at pixel (x, y) (its middle), 16.16 */
typedef struct {
diff --git a/games/snowrider/src/r3d.h b/games/snowrider/src/r3d.h
index c4e18dcc..bf79de83 100644
--- a/games/snowrider/src/r3d.h
+++ b/games/snowrider/src/r3d.h
@@ -55,6 +55,15 @@ void r_poly_cam(const V3 *v, int n, uint16_t c565, int flags, int alpha);
/* a triangle of a mesh, its corners on the screen (r_to_screen), clockwise from the front: left out when it is
* turned away or off the screen; false when it reaches too far off the screen to be drawn so (r_poly_cam then) */
bool r_tri_screen(const V3 *a, const V3 *b, const V3 *c, uint16_t c565);
+#if NP_SOFT_FLOAT
+/* (calculators without a floating-point unit) r_tri_screen with the corners in integers: x and y in 16.16 pixels, z
+ * the depth R_ZK / z in 16.16 */
+typedef struct {
+ int32_t x, y;
+ int64_t z;
+} SVi;
+bool r_tri_screen_fx(const SVi *a, const SVi *b, const SVi *c, uint16_t c565);
+#endif
int r_count(void); /* polygons this frame */
bool r_full(void); /* the list is (nearly) full: what comes next may be left out */
diff --git a/platform/include/np_fixed.h b/platform/include/np_fixed.h
index 4101a641..5ae50713 100644
--- a/platform/include/np_fixed.h
+++ b/platform/include/np_fixed.h
@@ -21,6 +21,19 @@ static inline int64_t fx_from_float(float f, int k) {
return neg ? -m : m;
}
+/* v / 2^k as a float (rounded toward zero), built from its bits */
+static inline float fx_to_float(int64_t v, int k) {
+ if (!v) return 0;
+ uint64_t m = v < 0 ? -(uint64_t)v : (uint64_t)v;
+ int e = 63 - __builtin_clzll(m);
+ uint32_t mant = (uint32_t)(e >= 23 ? m >> (e - 23) : m << (23 - e));
+ union {
+ uint32_t u;
+ float f;
+ } r = {(v < 0 ? 0x80000000u : 0) | (uint32_t)(e - k + 127) << 23 | (mant & 0x7FFFFF)};
+ return r.f;
+}
+
/* floor(sqrt(v)), a bit at a time */
static inline uint32_t fx_isqrt(uint32_t v) {
uint32_t r = 0, b = 1u << 30;
@@ -60,4 +73,17 @@ static inline int32_t fx_rcp(uint32_t n) {
}
return r;
}
+
+/* num / den * 2^k (den not 0, the result within an int64_t), to 30 bits: both brought to 31 bits, then fx_rcp */
+static inline int64_t fx_div(int64_t num, int64_t den, int k) {
+ if (!num) return 0;
+ bool neg = (num < 0) != (den < 0);
+ uint64_t n = num < 0 ? -(uint64_t)num : (uint64_t)num, d = den < 0 ? -(uint64_t)den : (uint64_t)den;
+ int nn = __builtin_clzll(n), nd = __builtin_clzll(d);
+ uint32_t n31 = (uint32_t)(n << nn >> 33), d32 = (uint32_t)(d << nd >> 32);
+ int64_t q = (int64_t)n31 * fx_rcp(d32); /* (n is n31 2^(33 - nn), d is d32 2^(32 - nd)) */
+ int sh = nd - nn - 60 + k;
+ q = sh >= 0 ? q << (sh > 62 ? 62 : sh) : sh <= -63 ? 0 : q >> -sh;
+ return neg ? -q : q;
+}
#endif