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.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
Doom: draw whole vertical bands, vanilla sprite clipping, sync to the panel refresh
Mason36394ebf738de2d
diff --git a/games/doom/Makefile b/games/doom/Makefileindex bd03c04c..71ac3493 100644--- a/games/doom/Makefile+++ b/games/doom/Makefile@@ -20,7 +20,7 @@ WAD ?=# the engine (Chocolate Doom by way of RP2040 Doom), and the calculator's side of it (src/nw)ENGINE = $(wildcard src/*.c) $(wildcard src/doom/*.c)NW = src/nw/i_system.c src/nw/i_video.c src/nw/i_input.c src/nw/i_sound.c src/nw/saves.c src/nw/main.c-HEADERS = $(wildcard src/*.h src/doom/*.h src/nw/*.h src/compat/*.h src/compat/*/*.h) ../common/epsilon_app.h \+HEADERS = $(wildcard src/*.h src/doom/*.h src/nw/*.h src/nw/*.inc src/compat/*.h src/compat/*/*.h) ../common/epsilon_app.h \../common/epsilon_files.hINC = -include src/nw/doomconf.h -Isrc -Isrc/doom -Isrc/compat -Isrc/nw# (the engine's own code has warnings of its own, left as they are; the calculator's side has none)diff --git a/games/doom/src/doom/am_map.c b/games/doom/src/doom/am_map.cindex f5beb5cd..abc40d09 100644--- a/games/doom/src/doom/am_map.c+++ b/games/doom/src/doom/am_map.c@@ -47,6 +47,9 @@#include "dstrings.h"#include "am_map.h"+#if NUMWORKS+#include "nw/nw_render.h"+#endif// For use if I do walls with outsides/insides@@ -881,8 +884,7 @@ void AM_clearFB(int color)#endif#if NUMWORKS// only the band being drawn is there- int y0 = nw_band_y0, y1 = nw_band_y1 < f_h ? nw_band_y1 : f_h;- if (y1 > y0) memset(fb + y0 * f_w, color, (y1 - y0) * f_w * sizeof(*fb));+ for (int y = 0; y < f_h; y++) memset(fb + y * NW_BAND_W + nw_band_x0, color, nw_band_x1 - nw_band_x0);#elsememset(fb, color, f_w*f_h*sizeof(*fb));#endif@@ -1062,7 +1064,9 @@ AM_drawFline#endif#if NUMWORKS-#define PUTDOT(xx,yy,cc) do { int yy_ = (yy); if (yy_ >= nw_band_y0 && yy_ < nw_band_y1) fb[yy_*f_w+(xx)]=(cc); } while (0)+ // (a line wholly outside the band being drawn has nothing in it)+ if ((fl->a.x < nw_band_x0 && fl->b.x < nw_band_x0) || (fl->a.x >= nw_band_x1 && fl->b.x >= nw_band_x1)) return;+#define PUTDOT(xx,yy,cc) do { int xx_ = (xx); if (xx_ >= nw_band_x0 && xx_ < nw_band_x1) fb[(yy)*NW_BAND_W+xx_]=(cc); } while (0)#else#define PUTDOT(xx,yy,cc) fb[(yy)*f_w+(xx)]=(cc)#endif@@ -1397,8 +1401,8 @@ void AM_drawMarks(void)void AM_drawCrosshair(int color){#if NUMWORKS- int at = (f_w*(f_h+1))/2;- if (at / f_w >= nw_band_y0 && at / f_w < nw_band_y1) fb[at] = color;+ int at = (f_w*(f_h+1))/2, x = at % f_w;+ if (x >= nw_band_x0 && x < nw_band_x1) fb[at / f_w * NW_BAND_W + x] = color;#elsefb[(f_w*(f_h+1))/2] = color; // single point for now#endifdiff --git a/games/doom/src/doom/d_main.c b/games/doom/src/doom/d_main.cindex 39c0d2fe..7b354e2e 100644--- a/games/doom/src/doom/d_main.c+++ b/games/doom/src/doom/d_main.c@@ -136,7 +136,9 @@ boolean main_loop_started = false;#if !USE_MEMMAP_ONLYchar wadfile[1024]; // primary wad file#endif+#if !NUMWORKSchar mapdir[1024]; // directory of development maps+#endifisb_int8_t show_endoom = 1;#if !DOOM_TINYdiff --git a/games/doom/src/doom/g_game.c b/games/doom/src/doom/g_game.cindex 22565d5e..c29d7ea5 100644--- a/games/doom/src/doom/g_game.c+++ b/games/doom/src/doom/g_game.c@@ -2510,6 +2510,9 @@ void G_DoPlayDemo (void)lumpnum = W_GetNumForName(defdemoname);gameaction = ga_nothing;+#if HOST+ { extern void host_demo_start(void); host_demo_start(); }+#endifshould_be_const byte *demobuffer = W_CacheLumpNum(lumpnum, PU_STATIC);#if USE_WHDconst byte* demo_p;diff --git a/games/doom/src/doom/r_bsp.c b/games/doom/src/doom/r_bsp.cindex 16a3e0d1..f638fea1 100644--- a/games/doom/src/doom/r_bsp.c+++ b/games/doom/src/doom/r_bsp.c@@ -65,6 +65,9 @@ R_StoreWallRangevoid R_ClearDrawSegs(void) {#if !NO_DRAWSEGSds_p = drawsegs;+#elif NUMWORKS+ extern void nw_clear_drawsegs(void);+ nw_clear_drawsegs();#endif}@@ -228,20 +231,10 @@ R_ClipPassWallSegment//// R_ClearClipSegs//-#if NUMWORKS-// the columns of the view drawn this time (all of them, unless a frame has more pieces than there is room for-// and is drawn in strips: pd_render.cpp)-extern int nw_strip_x0, nw_strip_x1;-#endifvoid R_ClearClipSegs(void) {solidsegs[0].first = -0x7fff;-#if NUMWORKS- solidsegs[0].last = nw_strip_x0 - 1;- solidsegs[1].first = nw_strip_x1 < viewwidth ? nw_strip_x1 : viewwidth;-#elsesolidsegs[0].last = -1;solidsegs[1].first = viewwidth;-#endifsolidsegs[1].last = 0x7fff;newend = solidsegs + 2;}diff --git a/games/doom/src/doom/r_defs.h b/games/doom/src/doom/r_defs.hindex 8e40bd2b..077506ad 100644--- a/games/doom/src/doom/r_defs.h+++ b/games/doom/src/doom/r_defs.h@@ -47,7 +47,7 @@ typedef struct xy_positioned_s {fixed_t x, y;} xy_positioned_t;-#if !NO_DRAWSEGS+#if !NO_DRAWSEGS || NUMWORKS// Silhouette, needed for clipping Segs (mainly)// and sprites representing things.#define SIL_NONE 0@@ -449,6 +449,22 @@ typedef struct drawseg_s// A vissprite_t is a thing// that will be drawn during a refresh.// I.e. a sprite object that is partly visible.+#if NUMWORKS+// (vanilla's, smaller: sorted through an array of indices, nw_sort_vissprites)+typedef struct vissprite_s+{+ int16_t x1, x2;+ int16_t patch;+ int8_t colormap; // -1: shadow (fuzz)+ uint8_t fuzzbase; // where fuzzpos is at its first column, in vanilla's order of drawing+ fixed_t gx, gy, gz, gzt;+ fixed_t startfrac;+ fixed_t scale;+ fixed_t xiscale;+ fixed_t texturemid;+ int mobjflags;+} vissprite_t;+#elsetypedef struct vissprite_s{// Doubly linked list.@@ -490,6 +506,7 @@ typedef struct vissprite_sint mobjflags;} vissprite_t;+#endif//// Sprites are patches with a special naming conventiondiff --git a/games/doom/src/doom/r_main.c b/games/doom/src/doom/r_main.cindex 97098598..671e9bdd 100644--- a/games/doom/src/doom/r_main.c+++ b/games/doom/src/doom/r_main.c@@ -1198,7 +1198,7 @@ void R_RenderPlayerView(player_t *player) {#if !USE_WHDR_RenderBSPNode(numnodes - 1);#else-#if PICODOOM_RENDER_NEWHOPE+#if PICODOOM_RENDER_NEWHOPE && !NUMWORKS// for pd column lets draw these early since they clip other stuff (and we don't want to discard// them if we run out of column slots)// draw the psprites on top of everything@@ -1227,7 +1227,13 @@ void R_RenderPlayerView(player_t *player) {// Check for new console commands.NetUpdate();+#if NUMWORKS+ // (the sprites, masked walls and weapon are drawn with each band: nw/nw_render.h)+ extern void nw_sort_vissprites(void);+ nw_sort_vissprites();+#elseR_DrawMasked();+#endif// Check for new console commands.NetUpdate();diff --git a/games/doom/src/doom/r_plane.c b/games/doom/src/doom/r_plane.cindex 64352e8f..1f400e19 100644--- a/games/doom/src/doom/r_plane.c+++ b/games/doom/src/doom/r_plane.c@@ -79,8 +79,10 @@ floor_ceiling_clip_t ceilingclip[SCREENWIDTH];// spanstart holds the start of a plane span// initialized to 0 at start//+#if !NO_VISPLANE_GUTSint spanstart[SCREENHEIGHT];int spanstop[SCREENHEIGHT];+#endif//// texture mapping@@ -366,7 +368,6 @@ R_FindPlane#if NUMWORKS// vanilla's visplanes split as in R_CheckPlane, without their top and bottom arrays: the columns each one// has (set as the walls mark them, r_segs.c), and how far it goes-uint8_t nw_plane_used[MAXVISPLANES][SCREENWIDTH / 8];int16_t nw_plane_minx[MAXVISPLANES], nw_plane_maxx[MAXVISPLANES];void nw_plane_new(int pl, int minx, int maxx) {@@ -474,6 +475,7 @@ R_CheckPlane}#endif+#if !NO_VISPLANE_GUTS//// R_MakeSpans//@@ -502,6 +504,7 @@ R_MakeSpansb2--;}}+#endif#if !NO_VISPLANE_GUTSdiff --git a/games/doom/src/doom/r_plane.h b/games/doom/src/doom/r_plane.hindex 36bc034d..7d007181 100644--- a/games/doom/src/doom/r_plane.h+++ b/games/doom/src/doom/r_plane.h@@ -88,7 +88,7 @@ R_CheckPlaneint stop );#endif#if NUMWORKS-extern uint8_t nw_plane_used[MAXVISPLANES][SCREENWIDTH / 8];+extern uint8_t (*const nw_plane_used)[SCREENWIDTH / 8]; /* (in room shared with the bands: pd_render.cpp) */void nw_plane_new(int pl, int minx, int maxx);// (a wall marks a column of a floor or ceiling as vanilla sets its top and bottom)#define NW_PLANE_MARK(pl, x) (nw_plane_used[(pl) - visplanes][(x) >> 3] |= (uint8_t)(1u << ((x) & 7)))diff --git a/games/doom/src/doom/r_segs.c b/games/doom/src/doom/r_segs.cindex 451b68d1..c8ef1af8 100644--- a/games/doom/src/doom/r_segs.c+++ b/games/doom/src/doom/r_segs.c@@ -86,6 +86,35 @@ fixed_t rw_midtexturemid;fixed_t rw_toptexturemid;fixed_t rw_bottomtexturemid;fixed_t rw_maskedtexturemid;+#if NUMWORKS+#include "nw/nw_render.h"+// the seg being drawn, its drawseg as vanilla's, and all of the frame's so far (vanilla's MAXDRAWSEGS counts+// them all; only those that matter to R_DrawMasked are kept, in nw_drawsegs)+static nw_drawseg_t nw_ds;+static int nw_drawseg_total;+static int16_t *nw_maskedcol;+static nw_masked_t *nw_masked_head;+static int nw_tm_mid, nw_tm_top, nw_tm_bottom, nw_tm_sky = -1;+nw_drawseg_t nw_drawsegs[NW_DRAWSEG_MAX];+int nw_drawseg_count;+uint8_t __attribute__((aligned(4))) nw_openings[NW_OPENINGS];+int nw_opening_count;++void nw_clear_drawsegs(void) {+ nw_drawseg_total = nw_drawseg_count = nw_opening_count = 0;+ nw_tm_sky = -1;+}++static int nw_open_alloc(int n) {+ int off = (nw_opening_count + 3) & ~3;+ if (off + n > NW_OPENINGS) {+ nw_record_overflow();+ return 0; // (the frame is drawn again band by band)+ }+ nw_opening_count = off + n;+ return off;+}+#endifint worldtop;int worldbottom;@@ -305,6 +334,10 @@ void R_RenderSegLoop(void) {#endifdc_texturemid = skytexturemid;+#if NUMWORKS+ if (nw_tm_sky < 0) nw_tm_sky = nw_add_tmid(skytexturemid);+ nw_tmid_cur = nw_tm_sky;+#endifdc_x = rw_x;dc_yl = top;@@ -358,9 +391,6 @@ void R_RenderSegLoop(void) {angle = (rw_centerangle + x_to_viewangle(rw_x)) >> ANGLETOFINESHIFT;texturecolumn = rw_offset - FixedMul(finetangent(angle), rw_distance);texturecolumn >>= FRACBITS;-#if HOST- { extern int nw_dbg_x; if (nw_dbg_x == rw_x) printf("TC x=%d off=%d dist=%d cang=%u tc=%d scale=%d\n", rw_x, rw_offset, rw_distance, rw_centerangle, texturecolumn, rw_scale); }-#endif// calculate lightingindex = rw_scale >> LIGHTSCALESHIFT;@@ -395,6 +425,9 @@ void R_RenderSegLoop(void) {dc_yh = yh;dc_texturemid = rw_midtexturemid;dc_source = R_GetColumn(midtexture, texturecolumn);+#if NUMWORKS+ nw_tmid_cur = nw_tm_mid;+#endif#if PD_COLUMNS#if PD_SCALE_SORTpd_add_column2(PDCOL_MID);@@ -426,6 +459,9 @@ void R_RenderSegLoop(void) {dc_yh = mid;dc_texturemid = rw_toptexturemid;dc_source = R_GetColumn(toptexture, texturecolumn);+#if NUMWORKS+ nw_tmid_cur = nw_tm_top;+#endif#if PD_COLUMNSpd_add_column2(PDCOL_TOP);#endif@@ -455,6 +491,9 @@ void R_RenderSegLoop(void) {dc_texturemid = rw_bottomtexturemid;dc_source = R_GetColumn(bottomtexture,texturecolumn);+#if NUMWORKS+ nw_tmid_cur = nw_tm_bottom;+#endif#if PD_COLUMNSpd_add_column2(PDCOL_BOTTOM);#endif@@ -481,6 +520,8 @@ void R_RenderSegLoop(void) {// for backdrawing of masked mid texture#if !NO_DRAWSEGSmaskedtexturecol[rw_x] = texturecolumn;+#elif NUMWORKS+ if (rw_x >= nw_strip_x0 && rw_x < nw_strip_x1) nw_maskedcol[rw_x] = (int16_t)texturecolumn;#else// graham: added drawing here// draw the texture@@ -549,6 +590,10 @@ R_StoreWallRange// don't overflow and crashif (ds_p == &drawsegs[MAXDRAWSEGS])return;+#elif NUMWORKS+ // (vanilla draws no more walls past MAXDRAWSEGS)+ if (nw_drawseg_total == MAXDRAWSEGS)+ return;#endif#ifdef RANGECHECK@@ -589,6 +634,10 @@ R_StoreWallRangeds_p->x1 = rw_x;ds_p->x2 = stop;ds_p->curline = curline;+#elif NUMWORKS+ nw_ds.x1 = rw_x;+ nw_ds.x2 = stop;+ nw_ds.curline = curline;#endifrw_stopx = stop + 1;@@ -598,6 +647,8 @@ R_StoreWallRangeR_ScaleFromGlobalAngle(viewangle + x_to_viewangle(start));#if !NO_DRAWSEGSds_p->scale1 = rw_scale;+#elif NUMWORKS+ nw_ds.scale1 = rw_scale;#endifif (stop > start) {@@ -606,6 +657,9 @@ R_StoreWallRange#if !NO_DRAWSEGSds_p->scale2 = scale2;ds_p->scalestep = rw_scalestep;+#elif NUMWORKS+ nw_ds.scale2 = scale2;+ nw_ds.scalestep = rw_scalestep;#endif} else {// UNUSED: try to fix the stretched line bug@@ -625,6 +679,9 @@ R_StoreWallRange#endif#if !NO_DRAWSEGSds_p->scale2 = ds_p->scale1;+#elif NUMWORKS+ nw_ds.scale2 = nw_ds.scale1;+ nw_ds.scalestep = 0;#endif}@@ -636,6 +693,9 @@ R_StoreWallRangemidtexture = toptexture = bottomtexture = maskedtexture = 0;#if !NO_DRAWSEGSds_p->maskedtexturecol = NULL;+#elif NUMWORKS+ nw_ds.masked = NW_OPEN_NONE;+ nw_masked_head = NULL;#endifif (!backsector) {@@ -653,9 +713,6 @@ R_StoreWallRangerw_midtexturemid = worldtop;}rw_midtexturemid += side_rowoffset(sidedef);-#if HOST- { extern int nw_dbg_x; if (nw_dbg_x >= start && nw_dbg_x <= stop) printf("SEG mid tex %d h %d worldtop %d viewz %d rowoff %d peg %d floor %d tmid %d\n", side_midtexture(sidedef), texture_height(side_midtexture(sidedef)), worldtop, viewz, side_rowoffset(sidedef), (line_flags(linedef) & ML_DONTPEGBOTTOM) != 0, sector_floorheight(frontsector), rw_midtexturemid); }-#endif#if !NO_DRAWSEGSds_p->silhouette = SIL_BOTH;@@ -663,6 +720,13 @@ R_StoreWallRangeds_p->sprbottomclip = minfloorceilingcliparray;ds_p->bsilheight = INT_MAX;ds_p->tsilheight = INT_MIN;+#elif NUMWORKS+ nw_ds.silhouette = SIL_BOTH;+ nw_ds.topclip = NW_OPEN_MAX;+ nw_ds.botclip = NW_OPEN_MIN;+ nw_ds.bsilheight = INT_MAX;+ nw_ds.tsilheight = INT_MIN;+ nw_tm_mid = nw_add_tmid(rw_midtexturemid);#endif} else {#if !NO_DRAWSEGS@@ -699,6 +763,40 @@ R_StoreWallRangeds_p->tsilheight = INT_MIN;ds_p->silhouette |= SIL_TOP;}+#elif NUMWORKS+ // two sided line+ nw_ds.topclip = nw_ds.botclip = NW_OPEN_NONE;+ nw_ds.silhouette = 0;++ if (frontsector->rawfloorheight > backsector->rawfloorheight) {+ nw_ds.silhouette = SIL_BOTTOM;+ nw_ds.bsilheight = sector_floorheight(frontsector);+ } else if (sector_floorheight(backsector) > viewz) {+ nw_ds.silhouette = SIL_BOTTOM;+ nw_ds.bsilheight = INT_MAX;+ // nw_ds.sprbottomclip = negonearray;+ }++ if (frontsector->rawceilingheight < backsector->rawceilingheight) {+ nw_ds.silhouette |= SIL_TOP;+ nw_ds.tsilheight = sector_ceilingheight(frontsector);+ } else if (sector_ceilingheight(backsector) < viewz) {+ nw_ds.silhouette |= SIL_TOP;+ nw_ds.tsilheight = INT_MIN;+ // nw_ds.sprtopclip = screenheightarray;+ }++ if (backsector->rawceilingheight <= frontsector->rawfloorheight) {+ nw_ds.botclip = NW_OPEN_MIN;+ nw_ds.bsilheight = INT_MAX;+ nw_ds.silhouette |= SIL_BOTTOM;+ }++ if (backsector->rawfloorheight >= frontsector->rawceilingheight) {+ nw_ds.topclip = NW_OPEN_MAX;+ nw_ds.tsilheight = INT_MIN;+ nw_ds.silhouette |= SIL_TOP;+ }#endifworldhigh = sector_ceilingheight(backsector) - viewz;worldlow = sector_floorheight(backsector) - viewz;@@ -764,6 +862,10 @@ R_StoreWallRange}rw_toptexturemid += side_rowoffset(sidedef);rw_bottomtexturemid += side_rowoffset(sidedef);+#if NUMWORKS+ if (toptexture) nw_tm_top = nw_add_tmid(rw_toptexturemid);+ if (bottomtexture) nw_tm_bottom = nw_add_tmid(rw_bottomtexturemid);+#endif// allocate space for masked texture tablesif (side_midtexture(sidedef)) {@@ -786,6 +888,20 @@ R_StoreWallRangerw_maskedtexturemid = rw_maskedtexturemid - viewz;}rw_maskedtexturemid += side_rowoffset(seg_sidedef(curline));+#if NUMWORKS+ {+ // (the columns of the strip being recorded)+ int s0 = rw_x > nw_strip_x0 ? rw_x : nw_strip_x0, s1 = rw_stopx < nw_strip_x1 ? rw_stopx : nw_strip_x1;+ int off = nw_open_alloc(sizeof(nw_masked_t) + 2 * (s1 > s0 ? s1 - s0 : 0));+ nw_masked_head = (nw_masked_t *) (nw_openings + off);+ nw_masked_head->texturemid = rw_maskedtexturemid;+ nw_masked_head->texture = maskedtexture_tex;+ nw_masked_head->x0 = (int16_t) s0;+ nw_masked_head->lightnum = 0;+ nw_maskedcol = (int16_t *) (nw_masked_head + 1) - s0;+ nw_ds.masked = (int16_t) off;+ }+#endif#elsemaskedtexturecol = lastopening - rw_x;ds_p->maskedtexturecol = maskedtexturecol;@@ -845,6 +961,9 @@ R_StoreWallRangelightnum--;else if (line_is_vert(seg_linedef(curline)))lightnum++;+#if NUMWORKS+ if (nw_masked_head) nw_masked_head->lightnum = (int8_t) lightnum;+#endifif (lightnum < 0)walllights = scalelight[0];@@ -907,6 +1026,34 @@ R_StoreWallRange#endifR_RenderSegLoop();+#if NUMWORKS+ // save sprite clipping info (for the columns of the strip being recorded)+ int s0 = start > nw_strip_x0 ? start : nw_strip_x0, s1 = rw_stopx < nw_strip_x1 ? rw_stopx : nw_strip_x1;+ if (s1 < s0) s1 = s0;+ if (((nw_ds.silhouette & SIL_TOP) || maskedtexture) && nw_ds.topclip == NW_OPEN_NONE) {+ int off = nw_open_alloc(s1 - s0);+ memcpy(nw_openings + off, ceilingclip + s0, s1 - s0);+ nw_ds.topclip = (int16_t) (off - (s0 - start));+ }+ if (((nw_ds.silhouette & SIL_BOTTOM) || maskedtexture) && nw_ds.botclip == NW_OPEN_NONE) {+ int off = nw_open_alloc(s1 - s0);+ memcpy(nw_openings + off, floorclip + s0, s1 - s0);+ nw_ds.botclip = (int16_t) (off - (s0 - start));+ }+ if (maskedtexture && !(nw_ds.silhouette & SIL_TOP)) {+ nw_ds.silhouette |= SIL_TOP;+ nw_ds.tsilheight = INT_MIN;+ }+ if (maskedtexture && !(nw_ds.silhouette & SIL_BOTTOM)) {+ nw_ds.silhouette |= SIL_BOTTOM;+ nw_ds.bsilheight = INT_MAX;+ }+ if ((nw_ds.silhouette || maskedtexture) && s1 > s0) {+ if (nw_drawseg_count == NW_DRAWSEG_MAX) nw_record_overflow();+ else nw_drawsegs[nw_drawseg_count++] = nw_ds;+ }+ nw_drawseg_total++;+#endif#if !NO_DRAWSEGS@@ -938,7 +1085,15 @@ R_StoreWallRange}#if PD_COLUMNS && USE_WHD+#if NUMWORKSvoid pd_add_column2(pd_column_type type) {+ if (nw_resolve_column()) pd_add_column(type);+}++int nw_resolve_column(void) {+#else+void pd_add_column2(pd_column_type type) {+#endifassert(dc_source.real_id >= 0);int pc = whd_textures[dc_source.real_id].patch_count;if (!pc) {@@ -963,11 +1118,19 @@ void pd_add_column2(pd_column_type type) {// note as pre R_GenerateLookup, a single patch column ignores the patch offsetydc_source = make_drawcolumn(lookup_patch(pnum-firstspritelump), (uint8_t)(dc_source.col - metadata[2]));} else {+#if NUMWORKS+ return 0;+#else#warning untested no patch columnreturn;+#endif}}}+#if NUMWORKS+ return 1;+#elsepd_add_column(type);+#endif}#endif\ No newline at end of filediff --git a/games/doom/src/doom/r_things.c b/games/doom/src/doom/r_things.cindex a9d74d74..7a062427 100644--- a/games/doom/src/doom/r_things.c+++ b/games/doom/src/doom/r_things.c@@ -336,9 +336,14 @@ void R_InitSprites() {// R_ClearSprites// Called at frame start.//+#if NUMWORKS+void nw_clear_vissprites(void);+#endifvoid R_ClearSprites(void) {#if !NO_VISSPRITESvissprite_p = vissprites;+#elif NUMWORKS+ nw_clear_vissprites();#endif}@@ -637,6 +642,20 @@ R_DrawVisSprite}+#if NUMWORKS+#include "nw/nw_render.h"+// vanilla's vissprites (MAXVISSPRITES of them, the others dropped as vanilla does), drawn band by band+static vissprite_t nw_vissprites[MAXVISSPRITES];+static uint8_t nw_vis_order[MAXVISSPRITES];+int nw_vissprite_count;+void nw_clear_vissprites(void) { nw_vissprite_count = 0; }+static vissprite_t *nw_new_vissprite(void) {+ if (nw_vissprite_count == MAXVISSPRITES)+ return &overflowsprite;+ return &nw_vissprites[nw_vissprite_count++];+}+#endif+//// R_ProjectSprite// Generates a vissprite for a thing@@ -735,7 +754,9 @@ void R_ProjectSprite(mobj_t *thing) {return;// store information in a vissprite-#if NO_VISSPRITES+#if NUMWORKS+ vis = nw_new_vissprite();+#elif NO_VISSPRITESvissprite_t vis_a;vis = &vis_a;#else@@ -743,7 +764,7 @@ void R_ProjectSprite(mobj_t *thing) {#endifvis->mobjflags = thing->flags;vis->scale = xscale << detailshift;-#if !DOOM_TINY+#if !DOOM_TINY || NUMWORKSvis->gx = thing->xy.x;vis->gy = thing->xy.y;vis->gz = thing->z;@@ -795,7 +816,9 @@ void R_ProjectSprite(mobj_t *thing) {vis->colormap = spritelights[index];}+#if !NUMWORKSR_DrawSpriteEarly(vis);+#endif}@@ -1178,3 +1201,306 @@ void R_DrawSpriteEarly(vissprite_t *spr) {pd_flag &= ~1;#endif}++#if NUMWORKS+// ---------------------------------------------------------------------------------------------------------------+// The calculator: vanilla's R_DrawMasked, for the columns of one band at a time (nw/nw_render.h)++// sprites back to front, as vanilla's selection sort leaves them (the first of equal scales first)+void nw_sort_vissprites(void) {+ for (int i = 0; i < nw_vissprite_count; i++) {+ int j = i;+ while (j > 0 && nw_vissprites[nw_vis_order[j - 1]].scale > nw_vissprites[i].scale) {+ nw_vis_order[j] = nw_vis_order[j - 1];+ j--;+ }+ nw_vis_order[j] = (uint8_t) i;+ }+}++// the clipping of the masked column being drawn, in screen rows (vanilla's mfloorclip and mceilingclip)+static int16_t nw_clipbot[SCREENWIDTH], nw_cliptop[SCREENWIDTH];+static boolean nw_fuzz;++// vanilla's R_DrawMaskedColumn: the posts of dc_x's column within the clip, to the back end+static void nw_draw_masked_column(maskedcolumn_t column) {+ int topscreen, bottomscreen, yl, yh;+ if (column.real_id >= 0) {+ // a masked middle texture's column: its patch's posts, if a single patch makes it+ dc_source = column;+ if (!nw_resolve_column()) return;+ column = dc_source;+ }+ if (column.real_id >= 0) {+ // more than one patch: drawn whole, as RP2040 Doom does+ topscreen = sprtopscreen;+ bottomscreen = topscreen + spryscale * (texture_height(column.real_id) >> FRACBITS);+ yl = (topscreen + FRACUNIT - 1) >> FRACBITS;+ yh = (bottomscreen - 1) >> FRACBITS;+ if (yh >= nw_clipbot[dc_x]) yh = nw_clipbot[dc_x] - 1;+ if (yl <= nw_cliptop[dc_x]) yl = nw_cliptop[dc_x] + 1;+ if (yl <= yh) {+ dc_source = column;+ nw_masked_column(dc_x, yl, yh, 0, nw_fuzz);+ }+ return;+ }+ patch_t *patch = W_CacheLumpNum(-(int) column.real_id, PU_CACHE);+ dc_source = column;+ int height = patch_height(patch);+ if (patch_fully_opaque(patch)) {+ topscreen = sprtopscreen;+ bottomscreen = topscreen + spryscale * height;+ yl = (topscreen + FRACUNIT - 1) >> FRACBITS;+ yh = (bottomscreen - 1) >> FRACBITS;+ if (yh >= nw_clipbot[dc_x]) yh = nw_clipbot[dc_x] - 1;+ if (yl <= nw_cliptop[dc_x]) yl = nw_cliptop[dc_x] + 1;+ if (yl <= yh) nw_masked_column(dc_x, yl, yh, 0, nw_fuzz);+ return;+ }+ // the posts, from the packed column's run data (as RP2040 Doom reads them)+ th_backwards_bit_input rbi;+ uint data_index = 3 + patch_has_extra(patch);+ data_index += ((uint8_t *) patch)[data_index * 2];+ uint16_t *col_offsets = &((uint16_t *) patch)[data_index];+ uint16_t col_offset = col_offsets[column.col];+ uint next_column;+ if (0xff == (col_offset >> 8)) {+ next_column = (col_offset & 0xff) + 1;+ } else {+ next_column = column.col + 1;+ }+ col_offset = col_offsets[next_column];+ while (0xff == (col_offset >> 8)) col_offset = col_offsets[++next_column];+ data_index = (data_index + patch_width(patch)) * 2 + 2;+ if (patch_byte_addressed(patch)) {+ th_backwards_bit_input_init(&rbi, (const uint8_t *) patch + data_index + col_offset);+ } else {+ th_backwards_bit_input_init_bit_offset(&rbi, (const uint8_t *) patch + data_index, col_offset);+ }+ int prev = 0, base_offset = 0;+ do {+ int top = prev + th_read_backwards_bits(&rbi, bitcount8(height - prev));+ if (top >= height) break;+ base_offset += top - prev;+ int size = th_read_backwards_bits(&rbi, bitcount8(height - top));+ prev = top + size;+ topscreen = sprtopscreen + spryscale * top;+ bottomscreen = topscreen + spryscale * size;+ yl = (topscreen + FRACUNIT - 1) >> FRACBITS;+ yh = (bottomscreen - 1) >> FRACBITS;+ if (yh >= nw_clipbot[dc_x]) yh = nw_clipbot[dc_x] - 1;+ if (yl <= nw_cliptop[dc_x]) yl = nw_cliptop[dc_x] + 1;+ if (yl <= yh) nw_masked_column(dc_x, yl, yh, base_offset, nw_fuzz);+ } while (true);+}++// vanilla's R_DrawVisSprite, for columns x1 to x2 of the sprite+static void nw_draw_vis_sprite(vissprite_t *vis, int x1, int x2) {+ nw_fuzz = vis->colormap < 0;+ dc_colormap_index = nw_fuzz ? 0 : vis->colormap;+ dc_iscale = abs(vis->xiscale) >> detailshift;+ dc_texturemid = vis->texturemid;+ nw_masked_texturemid = dc_texturemid;+ fixed_t frac = vis->startfrac + (x1 - vis->x1) * vis->xiscale;+ spryscale = vis->scale;+ sprtopscreen = centeryfrac - FixedMul(dc_texturemid, spryscale);+ framedrawable_t *fd = lookup_patch(vis->patch);+ for (dc_x = x1; dc_x <= x2; dc_x++, frac += vis->xiscale) {+ nw_draw_masked_column(R_GetPatchColumn(fd, frac >> FRACBITS));+ }+ nw_masked_flush();+}++// vanilla's R_RenderMaskedSegRange+static void nw_masked_seg_range(nw_drawseg_t *ds, int x1, int x2) {+ if (x1 > x2) return;+ nw_masked_t *m = (nw_masked_t *) (nw_openings + ds->masked);+ int16_t *cols = (int16_t *) (m + 1) - m->x0;+ int lightnum = m->lightnum;+ const int8_t *walllights = scalelight[lightnum < 0 ? 0 : lightnum >= LIGHTLEVELS ? LIGHTLEVELS - 1 : lightnum];+ fixed_t step = ds->scalestep;+ spryscale = ds->scale1 + (x1 - ds->x1) * step;+ for (int x = x1; x <= x2; x++) {+ nw_cliptop[x] = ds->topclip == NW_OPEN_MAX ? viewheight+ : ds->topclip == NW_OPEN_MIN ? -1+ : nw_openings[ds->topclip + x - ds->x1] - FLOOR_CEILING_CLIP_OFFSET;+ nw_clipbot[x] = ds->botclip == NW_OPEN_MAX ? viewheight+ : ds->botclip == NW_OPEN_MIN ? -1+ : nw_openings[ds->botclip + x - ds->x1] - FLOOR_CEILING_CLIP_OFFSET;+ }+ dc_texturemid = m->texturemid;+ nw_masked_texturemid = dc_texturemid;+ nw_fuzz = false;+ framedrawable_t *fd = lookup_masked_texture(m->texture);+ int width_mask = whd_textures[m->texture].width - 1;+ if (fixedcolormap) dc_colormap_index = fixedcolormap;+ for (dc_x = x1; dc_x <= x2; dc_x++, spryscale += step) {+ if (cols[dc_x] == SHRT_MAX) continue;+ if (!fixedcolormap) {+ unsigned index = spryscale >> LIGHTSCALESHIFT;+ if (index >= MAXLIGHTSCALE) index = MAXLIGHTSCALE - 1;+ dc_colormap_index = walllights[index];+ }+ sprtopscreen = centeryfrac - FixedMul(dc_texturemid, spryscale);+ dc_iscale = 0xffffffffu / (unsigned) spryscale;+ nw_draw_masked_column(R_GetMaskedColumn(fd, cols[dc_x] & width_mask));+ cols[dc_x] = SHRT_MAX;+ }+ nw_masked_flush();+}++// vanilla's R_DrawSprite: the sprite clipped by the drawsegs in front of it (the masked ones behind it drawn+// first), for the band's columns; from x_from on for the clipping (a fuzzy sprite counts its pixels from its+// first column, its fuzz going on from there)+static void nw_draw_sprite(vissprite_t *spr, int x_from) {+ int a = spr->x1 > nw_band_x0 ? spr->x1 : nw_band_x0;+ int b = spr->x2 < nw_band_x1 - 1 ? spr->x2 : nw_band_x1 - 1;+ int c0 = x_from < a ? x_from : a;+ for (int x = c0; x <= b; x++) nw_clipbot[x] = nw_cliptop[x] = -2;+ for (int i = nw_drawseg_count - 1; i >= 0; i--) {+ nw_drawseg_t *ds = &nw_drawsegs[i];+ if (ds->x1 > spr->x2 || ds->x2 < spr->x1 || (!ds->silhouette && ds->masked == NW_OPEN_NONE))+ continue;+ int r1 = ds->x1 < spr->x1 ? spr->x1 : ds->x1;+ int r2 = ds->x2 > spr->x2 ? spr->x2 : ds->x2;+ fixed_t scale, lowscale;+ if (ds->scale1 > ds->scale2) {+ lowscale = ds->scale2;+ scale = ds->scale1;+ } else {+ lowscale = ds->scale1;+ scale = ds->scale2;+ }+ if (scale < spr->scale+ || (lowscale < spr->scale && !R_PointOnSegSide(spr->gx, spr->gy, (seg_t *) ds->curline))) {+ // masked mid texture behind the sprite: drawn first (in the band)+ if (ds->masked != NW_OPEN_NONE) {+ int m1 = r1 > nw_band_x0 ? r1 : nw_band_x0;+ int m2 = r2 < nw_band_x1 - 1 ? r2 : nw_band_x1 - 1;+ nw_masked_seg_range(ds, m1, m2);+ }+ continue;+ }+ // clip this piece of the sprite+ int silhouette = ds->silhouette;+ if (spr->gz >= ds->bsilheight) silhouette &= ~SIL_BOTTOM;+ if (spr->gzt <= ds->tsilheight) silhouette &= ~SIL_TOP;+ if (r1 < c0) r1 = c0;+ if (r2 > b) r2 = b;+ // (a frame drawn band by band has the openings of the band's columns only)+ if (r1 < nw_strip_x0) r1 = nw_strip_x0;+ if (r2 >= nw_strip_x1) r2 = nw_strip_x1 - 1;+ for (int x = r1; x <= r2; x++) {+ if ((silhouette & SIL_BOTTOM) && nw_clipbot[x] == -2)+ nw_clipbot[x] = ds->botclip == NW_OPEN_MAX ? viewheight+ : ds->botclip == NW_OPEN_MIN ? -1+ : nw_openings[ds->botclip + x - ds->x1] - FLOOR_CEILING_CLIP_OFFSET;+ if ((silhouette & SIL_TOP) && nw_cliptop[x] == -2)+ nw_cliptop[x] = ds->topclip == NW_OPEN_MAX ? viewheight+ : ds->topclip == NW_OPEN_MIN ? -1+ : nw_openings[ds->topclip + x - ds->x1] - FLOOR_CEILING_CLIP_OFFSET;+ }+ }+ for (int x = c0; x <= b; x++) {+ if (nw_clipbot[x] == -2) nw_clipbot[x] = viewheight;+ if (nw_cliptop[x] == -2) nw_cliptop[x] = -1;+ }+ nw_draw_vis_sprite(spr, c0, b);+}++// vanilla's R_DrawPSprite and R_DrawPlayerSprites (on top of everything)+static void nw_draw_psprite(pspdef_t *psp, const int8_t *lights) {+ spriteframeref_t sprframe = sprite_frame(psp->state->sprite, psp->state->frame & FF_FRAMEMASK);+ int lump = spriteframe_unrotated_pic(sprframe);+ boolean flip = (boolean) spriteframe_unrotated_flipped(sprframe);+ fixed_t tx = psp->sx - (SCREENWIDTH / 2) * FRACUNIT - sprite_offset(lump);+ int x1 = (centerxfrac + FixedMul(tx, pspritescale)) >> FRACBITS;+ if (x1 > viewwidth) return;+ tx += sprite_width(lump);+ int x2 = ((centerxfrac + FixedMul(tx, pspritescale)) >> FRACBITS) - 1;+ if (x2 < 0) return;+ vissprite_t avis, *vis = &avis;+ vis->mobjflags = 0;+ vis->texturemid = (BASEYCENTER << FRACBITS) + FRACUNIT / 2 - (psp->sy - sprite_topoffset(lump));+ vis->x1 = x1 < 0 ? 0 : x1;+ vis->x2 = x2 >= viewwidth ? viewwidth - 1 : x2;+ vis->scale = pspritescale << detailshift;+ if (flip) {+ vis->xiscale = -pspriteiscale;+ vis->startfrac = sprite_width(lump) - 1;+ } else {+ vis->xiscale = pspriteiscale;+ vis->startfrac = 0;+ }+ if (vis->x1 > x1) vis->startfrac += vis->xiscale * (vis->x1 - x1);+ vis->patch = lump;+ if (viewplayer->powers[pw_invisibility] > 4 * 32 || viewplayer->powers[pw_invisibility] & 8) {+ vis->colormap = -1; // shadow draw+ } else if (fixedcolormap) {+ vis->colormap = fixedcolormap;+ } else if (psp->state->frame & FF_FULLBRIGHT) {+ vis->colormap = 0;+ } else {+ vis->colormap = lights[MAXLIGHTSCALE - 1];+ }+ int a = vis->x1 > nw_band_x0 ? vis->x1 : nw_band_x0;+ int b = vis->x2 < nw_band_x1 - 1 ? vis->x2 : nw_band_x1 - 1;+ // (a fuzzy weapon counts its pixels from its first column)+ int c0 = vis->colormap < 0 ? vis->x1 : a;+ for (int x = c0; x <= b; x++) {+ nw_clipbot[x] = viewheight;+ nw_cliptop[x] = -1;+ }+ if (c0 <= b) nw_draw_vis_sprite(vis, c0, b);+}++static void nw_draw_player_sprites(void) {+ int lightnum = (mobj_sector(viewplayer->mo)->lightlevel >> LIGHTSEGSHIFT) + extralight;+ const int8_t *lights = scalelight[lightnum < 0 ? 0 : lightnum >= LIGHTLEVELS ? LIGHTLEVELS - 1 : lightnum];+ pspdef_t *psp = viewplayer->psprites;+ for (int i = 0; i < NUMPSPRITES; i++, psp++)+ if (psp->state) nw_draw_psprite(psp, lights);+}++static void nw_draw_player_sprites(void);++// fuzz: where vanilla's fuzzpos is at each fuzzy sprite and at the weapon, the frame drawn in its order+// (the pixels counted, nothing drawn: no band)+static int nw_fuzz_psprites;+int nw_fuzz_prepass(int fuzzpos) {+ int bx0 = nw_band_x0, bx1 = nw_band_x1;+ nw_band_x0 = nw_band_x1 = SCREENWIDTH;+ nw_fuzz_at = fuzzpos;+ for (int k = 0; k < nw_vissprite_count; k++) {+ vissprite_t *spr = &nw_vissprites[nw_vis_order[k]];+ if (spr->colormap >= 0) continue;+ spr->fuzzbase = (uint8_t) nw_fuzz_at;+ nw_draw_sprite(spr, spr->x1);+ }+ nw_fuzz_psprites = nw_fuzz_at;+ if (!viewangleoffset && (viewplayer->powers[pw_invisibility] > 4 * 32 || viewplayer->powers[pw_invisibility] & 8))+ nw_draw_player_sprites();+ nw_band_x0 = bx0;+ nw_band_x1 = bx1;+ return nw_fuzz_at;+}++// the band's sprites, masked walls and weapon (nw_band_x0 to nw_band_x1): vanilla's R_DrawMasked+void nw_draw_masked(void) {+ for (int k = 0; k < nw_vissprite_count; k++) {+ vissprite_t *spr = &nw_vissprites[nw_vis_order[k]];+ if (spr->x2 < nw_band_x0 || spr->x1 >= nw_band_x1) continue;+ if (spr->colormap < 0) nw_fuzz_at = spr->fuzzbase;+ nw_draw_sprite(spr, spr->colormap < 0 ? spr->x1 : nw_band_x1);+ }+ for (int i = nw_drawseg_count - 1; i >= 0; i--) {+ nw_drawseg_t *ds = &nw_drawsegs[i];+ if (ds->masked == NW_OPEN_NONE || ds->x2 < nw_band_x0 || ds->x1 >= nw_band_x1) continue;+ nw_masked_seg_range(ds, ds->x1 > nw_band_x0 ? ds->x1 : nw_band_x0,+ ds->x2 < nw_band_x1 - 1 ? ds->x2 : nw_band_x1 - 1);+ }+ nw_fuzz_at = nw_fuzz_psprites;+ if (!viewangleoffset) nw_draw_player_sprites();+}+#endifdiff --git a/games/doom/src/doom/r_things.h b/games/doom/src/doom/r_things.hindex 5d220528..c5035d59 100644--- a/games/doom/src/doom/r_things.h+++ b/games/doom/src/doom/r_things.h@@ -22,8 +22,10 @@#define __R_THINGS__-#if !NO_VISSPRITES+#if !NO_VISSPRITES || NUMWORKS#define MAXVISSPRITES 128+#endif+#if !NO_VISSPRITESextern vissprite_t vissprites[MAXVISSPRITES];extern vissprite_t* vissprite_p;diff --git a/games/doom/src/doom/st_stuff.c b/games/doom/src/doom/st_stuff.cindex 5af2f7af..93673dbc 100644--- a/games/doom/src/doom/st_stuff.c+++ b/games/doom/src/doom/st_stuff.c@@ -732,6 +732,9 @@ void ST_Ticker (void)st_randomnumber = M_Random();ST_updateWidgets();st_oldhealth = plyr->health;+#if HOST+ if (getenv("FACETRACE")) printf("FACE %d %d %d\n", leveltime, st_faceindex, st_randomnumber);+#endif}diff --git a/games/doom/src/doom1.whx b/games/doom/src/doom1.whxindex 92018840..98f26f76 100644Binary files a/games/doom/src/doom1.whx and b/games/doom/src/doom1.whx differdiff --git a/games/doom/src/nw/doomconf.h b/games/doom/src/nw/doomconf.hindex 745d7161..ea80d44d 100644--- a/games/doom/src/nw/doomconf.h+++ b/games/doom/src/nw/doomconf.h@@ -99,9 +99,13 @@/* the renderer (pd_render.cpp), with room for this many runs of pixels in the view each frame */#define PICODOOM_RENDER_NEWHOPE 1#define MERGE_DISTSCALE0_INTO_VIEWCOSSINANGLE 1-#ifndef NW_RENDER_COL_MAX-#define NW_RENDER_COL_MAX 2700+// the frame's records (nw/nw_render.h): columns of wall and sky, and of floor and ceiling (a frame with more+// is drawn band by band)+#ifndef NW_POOL_KB+#define NW_POOL_KB 32#endif+#define NW_RENDER_COL_MAX 4096+#define NW_PLANE_MAX 16384/* the calculator: no speaker, one core, files in Epsilon's file system */#define NO_USE_SOUND 1diff --git a/games/doom/src/nw/i_video.c b/games/doom/src/nw/i_video.cindex 19c66c94..bb58de90 100644--- a/games/doom/src/nw/i_video.c+++ b/games/doom/src/nw/i_video.c@@ -1,9 +1,16 @@/* Doom on the calculator: the screen.*- * Doom's 320 x 200 screen is made a band of 40 rows at a time (pd_render.cpp), each one in 8 bits as the- * game draws it, then turned into the display's colours through the palette (with its red, gold and green- * flashes) and pushed. The calculator's screen is 320 x 240: every fifth row is drawn twice, which gives- * back the taller pixels Doom had on the monitors of its day. */+ * Doom's 320 x 200 screen is made a band of NW_BAND_W columns at a time (pd_render.cpp), each one whole and+ * in 8 bits as the game draws it, then turned into the display's colours through the palette (with its red,+ * gold and green flashes) and sent. The calculator's screen is 320 x 240: every fifth row is drawn twice,+ * which gives back the taller pixels Doom had on the monitors of its day.+ *+ * The panel refreshes itself from its own memory, a column at a time across the screen (in landscape),+ * 40 times a second or so. A band sent while the refresh goes over it would show the top of the new frame+ * over the bottom of the old one, and a refresh that goes over bands of two frames would show both: the+ * frame tears. So the bands go in the order the refresh takes them, each one once the refresh has gone past+ * it and before the next refresh comes to it: every refresh shows one whole frame. The refresh's period and+ * phase are measured with eadk_display_wait_for_vblank (nw_sync_*). */#include <stdio.h>#include <string.h>@@ -16,6 +23,7 @@#include "z_zone.h"#include "plat.h"#include "nw.h"+#include "nw_render.h"/* what the engine expects to find */boolean screenvisible = true;@@ -31,6 +39,13 @@ semaphore_t render_frame_ready, display_frame_freed;static uint16_t palette[256];static int8_t next_pal = 0; /* the palette to use from the next frame, -1 when it is the one in use */static int8_t last_gamma = -1;+/* the refresh (below) */+int nw_scan_right_to_left; /* (the options: the panel's refresh goes right to left, or with nw_sync_off, no+ syncing at all) */+int nw_sync_off;+static uint32_t nw_sync_when; /* (when it was last measured, in ms) */+static int nw_band_index;+static void nw_sync_measure(void);void pd_init(void);void I_InitGraphics(void) { pd_init(); }@@ -82,6 +97,9 @@ static void make_palette(int num) {}void nw_frame_begin(void) {+ nw_band_index = 0;+ /* (the refresh measured again now and then: the panel's clock is not the processor's) */+ if (!nw_sync_off && (!nw_sync_when || plat_millis() - nw_sync_when > 4000)) nw_sync_measure();if (next_pal >= 0 || last_gamma != usegamma) {static int8_t current;if (next_pal >= 0) current = next_pal;@@ -113,10 +131,86 @@ void nw_frame_end(void) {}#endif-void nw_band_begin(uint8_t *band, int y0, int y1) {+/* ------------------------------------------------------------------ the refresh+ * Times are in 1/256 ms. The refresh starts at nw_sync_t0 + k * nw_sync_period; its pass over band j (in its+ * order) is the j-th fifth of the period. A frame keeps to one refresh r: band j goes once refresh r is past+ * it (after r's start + (j + 1) fifths) and before refresh r + 1 comes to it (before r's start + 1 + j fifths),+ * with a margin each side for the clock (1 ms steps) and the panel's blanking time. */+#define NW_SYNC_MARGIN (2 * 256)+static int32_t nw_sync_period, nw_sync_t0;+static int32_t nw_band_ms, nw_push_ms; /* (how long a band takes to make and to send, on average) */+static int32_t nw_frame_r; /* the refresh this frame keeps to (its start) */+static uint32_t nw_band_start;++static int32_t nw_now(void) { return (int32_t)(plat_millis() * 256 + 128); }++/* the refresh's period and phase: from the time of a few refreshes (none, if there are no refreshes to see) */+static void nw_sync_measure(void) {+ nw_sync_when = plat_millis();+ if (!plat_vblank()) {+ nw_sync_period = 0;+ return;+ }+ uint32_t a = plat_millis();+ int n = nw_sync_period ? 1 : 8;+ for (int i = 0; i < n; i++) plat_vblank();+ uint32_t b = plat_millis();+ if (n == 8) nw_sync_period = (int32_t)((b - a) * 256 / 8);+ nw_sync_t0 = (int32_t)(b * 256 + 128);+ nw_sync_when = b;+ /* (a refresh faster than 100 times a second, or none: the panel does not tell) */+ if (nw_sync_period < 10 * 256) nw_sync_period = 0;+}++/* the start of the last refresh at or before t */+static int32_t nw_refresh_before(int32_t t) {+ int32_t d = t - nw_sync_t0;+ int32_t k = d >= 0 ? d / nw_sync_period : -((-d + nw_sync_period - 1) / nw_sync_period);+ return nw_sync_t0 + k * nw_sync_period;+}++/* band j's window under the refresh starting at r */+static int32_t nw_window_open(int32_t r, int j) { return r + (j + 1) * nw_sync_period / 5 + NW_SYNC_MARGIN; }+static int32_t nw_window_close(int32_t r, int j) {+ return r + nw_sync_period + j * nw_sync_period / 5 - NW_SYNC_MARGIN - nw_push_ms;+}++static void nw_wait_until(int32_t t) {+ while (nw_now() < t) {+ }+}++/* band j is made: when to send it */+static void nw_sync_band(int j) {+ int32_t now = nw_now(), fifth = nw_sync_period / 5;+ if (j == 0) {+ /* the first refresh whose window for band 0 is open now or later, or the one after it if the frame+ * would be late for the last band (the bands to come taking as long as they do on average) */+ int32_t r = nw_refresh_before(now) - nw_sync_period;+ while (nw_window_close(r, 0) < now) r += nw_sync_period;+ int32_t p = now > nw_window_open(r, 0) ? now : nw_window_open(r, 0);+ for (int k = 1; k < 5; k++) {+ p += nw_band_ms + nw_push_ms;+ if (p < nw_window_open(r, k)) p = nw_window_open(r, k);+ }+ if (p > nw_window_close(r, 4)) r += nw_sync_period;+ nw_frame_r = r;+ }+ int32_t open = nw_window_open(nw_frame_r, j);+ if (now < open) {+ nw_wait_until(open);+ } else if (now > nw_window_close(nw_frame_r, j)) {+ /* late: at least not while the refresh goes over the band */+ int32_t r = nw_refresh_before(now);+ int32_t busy0 = r + j * fifth - NW_SYNC_MARGIN - nw_push_ms, busy1 = r + (j + 1) * fifth + NW_SYNC_MARGIN;+ if (now > busy0 && now < busy1) nw_wait_until(busy1);+ }+}++void nw_band_begin(uint8_t *band, int x0, int x1) {nw_band = band;- nw_band_y0 = y0;- nw_band_y1 = y1;+ nw_band_start = plat_millis();+ (void)x0, (void)x1;}/* the band to the screen: Doom's rows 5k to 5k + 4 are the screen's 6k to 6k + 5 (the last one twice) */@@ -127,21 +221,28 @@ uint8_t host_doom_screen[SCREENWIDTH * SCREENHEIGHT]; /* (tests: the frame in Dovoid nw_band_end(int x0, int x1) {int w = x1 - x0;#ifdef HOST- for (int y = nw_band_y0; y < nw_band_y1; y++)- memcpy(host_doom_screen + y * SCREENWIDTH + x0, nw_band + (y - nw_band_y0) * SCREENWIDTH + x0, w);+ for (int y = 0; y < SCREENHEIGHT; y++)+ memcpy(host_doom_screen + y * SCREENWIDTH + x0, nw_band + y * NW_BAND_W, w);#endif- uint16_t lines[SCREENWIDTH * 6];- for (int y = nw_band_y0; y < nw_band_y1; y += 5) {- const uint8_t *src = nw_band + (y - nw_band_y0) * SCREENWIDTH + x0;+ int j = nw_band_index++;+ int32_t made = (int32_t)(plat_millis() - nw_band_start) * 256;+ nw_band_ms += (made - nw_band_ms) / 4;+ if (nw_sync_period && !nw_sync_off) nw_sync_band(j);+ uint32_t t = plat_millis();+ uint16_t lines[NW_BAND_W * 24];+ for (int y = 0; y < SCREENHEIGHT; y += 20) {+ const uint8_t *src = nw_band + y * NW_BAND_W;uint16_t *d = lines;- for (int j = 0; j < 5; j++) {- for (int i = 0; i < w; i++) d[i] = palette[src[i]];- src += SCREENWIDTH;+ for (int k = 0; k < 4; k++) {+ for (int r = 0; r < 5; r++, src += NW_BAND_W, d += w)+ for (int i = 0; i < w; i++) d[i] = palette[src[i]];+ memcpy(d, d - w, w * sizeof *d);d += w;}- memcpy(d, d - w, w * sizeof *d);- plat_push(x0, y / 5 * 6, w, 6, lines);+ plat_push(x0, y / 5 * 6, w, 24, lines);}+ int32_t pushed = (int32_t)(plat_millis() - t) * 256;+ nw_push_ms += (pushed - nw_push_ms) / 4;}/* ------------------------------------------------------------------ errors */diff --git a/games/doom/src/nw/nw_back.inc b/games/doom/src/nw/nw_back.incnew file mode 100644index 00000000..eb4a361c--- /dev/null+++ b/games/doom/src/nw/nw_back.inc@@ -0,0 +1,561 @@+// Doom on the calculator: the renderer's back end (included by pd_render.cpp; nw/nw_render.h tells the+// story). The screen is drawn a band of NW_BAND_W columns at a time, each band whole: floors and ceilings,+// walls, then sprites, masked walls and the weapon in vanilla's order, then the status bar, messages and+// menus, and sent to the display in the order its panel refreshes them (nw/i_video.c).++extern "C" {+#if HOST+uint32_t nw_peak[4]; // (tests: the most plane records, drawsegs, bytes of openings and vissprites a frame had)+uint32_t nw_last_walls, nw_last_planes; // (and the records of the last frame)+#endif+int nw_strip_x0 = 0, nw_strip_x1 = SCREENWIDTH;+int nw_band_x0 = 0, nw_band_x1 = SCREENWIDTH;+int nw_tmid_cur;+fixed_t nw_masked_texturemid;+int nw_fuzz_at;+uint32_t nw_strips;+void nw_clear_drawsegs(void);+void nw_clear_vissprites(void);+int nw_fuzz_prepass(int fuzzpos);+// nw/i_video.c: the band to the display, and which way the panel refreshes+void nw_band_end(int x0, int x1);+extern int nw_scan_right_to_left;+}++// ---------------------------------------------------------------------------------------------------------------+// what the BSP pass records++extern "C" int nw_add_tmid(fixed_t texturemid) {+ (void) texturemid; // (the columns keep their own: pd_column has room)+ return 0;+}++extern "C" void nw_record_overflow(void) {+ if (!nw_overflowed) nw_col_overflows++;+ nw_overflowed = true;+}++// floors and ceilings: a column of a visplane (from the end of list_buffer down)+struct nw_plane_rec {+ uint32_t x : 9;+ uint32_t yl : 8;+ uint32_t yh : 8;+ uint32_t plane : 7;+};+#define NW_PLANE(k) (((nw_plane_rec *) (list_buffer + NW_POOL))[-1 - (k)])+static int nw_plane_count;+// (the most records since the flats' places were last worked out: what is above them is overwritten)+static int nw_wall_hwm, nw_plane_hwm;++// room for walls and planes, the masked columns of a band and a flat+static inline bool nw_room(int walls, int planes) {+ return (walls + NW_SCRATCH) * (int) sizeof(pd_column) + planes * (int) sizeof(nw_plane_rec) + 4096 <= NW_POOL;+}++extern "C" void nw_record_plane(int x, int yl, int yh, int plane) {+ if (yh < yl || x < nw_strip_x0 || x >= nw_strip_x1) return;+ if (!nw_room(render_col_count, nw_plane_count + 1)) {+ nw_record_overflow();+ return;+ }+ nw_plane_rec &r = NW_PLANE(nw_plane_count++);+ r.x = (uint32_t) x;+ r.yl = (uint32_t) yl;+ r.yh = (uint32_t) yh;+ r.plane = (uint32_t) plane;+}++// a record of dc_source's column at x (the fraction of texturemid below a quarter of a unit is the same for all+// the walls, the view's own, and for all the posts of a sprite: *low)+static int32_t nw_wall_low, nw_masked_low;+#if HOST+extern "C" { uint32_t nw_low_mismatch; } // (tests: a texturemid with another fraction: never)+#endif+static void nw_fill(pd_column &c, int x, int yl, int yh, fixed_t texturemid, int32_t *low) {+ c.yl = (uint8_t) yl;+ c.yh = (uint8_t) yh;+ c.scale = (uint32_t) dc_iscale > DDA_MAX ? DDA_MAX : (uint32_t) dc_iscale;+ c.colormap_index = (uint32_t) dc_colormap_index;+ int32_t f = texturemid & 0x3fff;+ if (f && *low < 0) *low = f;+#if HOST+ if (f && f != *low) nw_low_mismatch++;+#endif+ c.lowsel = f != 0;+ c.tm = (texturemid - f) >> 14;+ c.col = (uint8_t) dc_source.col;+ c.fd_num = (uint8_t) dc_source.fd_num;+ c.x_lo = (uint32_t) x & 0xff;+ c.x_hi = (uint32_t) x >> 8;+}++// walls and sky: one column, as vanilla's R_DrawColumn would draw it+#if HOST+// (tests: DUMPCOL="x tic" prints the columns at x in that tic's frame, as the reference does)+static int nw_dump_x = -2, nw_dump_tic;+static bool nw_dumping(int x) {+ if (nw_dump_x == -2) {+ const char *e = getenv("DUMPCOL");+ nw_dump_x = e ? atoi(e) : -1;+ nw_dump_tic = e && strchr(e, ' ') ? atoi(strchr(e, ' ')) : -1;+ }+ return x == nw_dump_x && leveltime == nw_dump_tic && gamestate == GS_LEVEL;+}+#endif++extern "C" void nw_record_wall(void) {+#if HOST+ if (nw_dumping(dc_x)) printf("COL x=%d yl=%d yh=%d iscale=%d texturemid=%d id=%d col=%d E%dM%d\n", dc_x, dc_yl, dc_yh, dc_iscale, dc_texturemid, dc_source.real_id, dc_source.col, gameepisode, gamemap);+#endif+ if (dc_yh < dc_yl || dc_x < nw_strip_x0 || dc_x >= nw_strip_x1) return;+ if (render_col_count == RENDER_COL_MAX || !nw_room(render_col_count + 1, nw_plane_count)) {+ nw_record_overflow();+ return;+ }+ if (render_col_count >= (int) nw_col_peak) nw_col_peak = render_col_count + 1;+ nw_fill(render_cols[render_col_count++], dc_x, dc_yl, dc_yh, dc_texturemid, &nw_wall_low);+}++// a pass over the BSP starts: nothing recorded yet (the drawsegs and vissprites are cleared with vanilla's)+static void nw_pass_start() {+ if (render_col_count > nw_wall_hwm) nw_wall_hwm = render_col_count;+ if (nw_plane_count > nw_plane_hwm) nw_plane_hwm = nw_plane_count;+ render_col_count = 0;+ nw_plane_count = 0;+ nw_overflowed = false;+ nw_wall_low = -1;+}++// ---------------------------------------------------------------------------------------------------------------+// floors and ceilings in the band: the visplane of each pixel marked, then runs of the same visplane along+// each row, which are vanilla's spans; a span coming from the band on the left starts where it started there+// (its texture position worked out at its start, as R_MapPlane does)++#define nw_vbit (nw_shared.band.vbit)+static int8_t nw_carry_plane[MAIN_VIEWHEIGHT];+static int16_t nw_carry_start[MAIN_VIEWHEIGHT];+// (right to left: the carries at the left edge of each band but the first, worked out first, from the left)+#define nw_edge_plane (nw_shared.band.edge_plane)+#define nw_edge_start (nw_shared.band.edge_start)++static void nw_mark_planes(int x0, int x1) {+ memset(nw_vbit, 0, sizeof nw_vbit);+ for (int i = 0; i < nw_plane_count; i++) {+ const nw_plane_rec &r = NW_PLANE(i);+ int x = r.x;+ if (x < x0 || x >= x1) continue;+ uint8_t *p = render_frame_buffer + r.yl * NW_STRIDE + x;+ uint8_t *vb = nw_vbit + r.yl * (NW_BAND_W / 8) + ((x - x0) >> 3);+ uint8_t bit = (uint8_t) (1u << ((x - x0) & 7));+ for (int y = r.yl; y <= (int) r.yh; y++) {+ *p = (uint8_t) r.plane;+ p += NW_STRIDE;+ *vb |= bit;+ vb += NW_BAND_W / 8;+ }+ }+}++// the runs of the band's rows: to flat_runs (drawn when full, and at the end) if draw, and the carries+static void nw_plane_runs(int x0, int x1, bool draw) {+ int numvisplanes = lastvisplane - visplanes;+ if (draw) {+ memset(visplane_heads, -1, numvisplanes * sizeof(visplane_heads[0]));+ nw_flat_run_count = 0;+ }+ for (int y = 0; y < viewheight && y < MAIN_VIEWHEIGHT; y++) {+ const uint8_t *vb = nw_vbit + y * (NW_BAND_W / 8);+ const uint8_t *row = render_frame_buffer + y * NW_STRIDE;+ int carry_plane = nw_carry_plane[y];+ int carry_start = nw_carry_start[y];+ nw_carry_plane[y] = -1;+ int x = x0;+ while (x < x1) {+ int b = x - x0;+ if (!(vb[b >> 3] & (1u << (b & 7)))) {+ if (!vb[b >> 3] && !(b & 7)) x += 8;+ else x++;+ continue;+ }+ int plane = row[x];+ int s = x;+ do {+ x++;+ b = x - x0;+ } while (x < x1 && (vb[b >> 3] & (1u << (b & 7))) && row[x] == plane);+ int start = s == x0 && carry_plane == plane ? carry_start : s;+ if (x == x1) {+ nw_carry_plane[y] = (int8_t) plane;+ nw_carry_start[y] = (int16_t) start;+ }+ if (!draw) continue;+ if (nw_flat_run_count == NW_FLAT_RUNS) nw_flush_planes(numvisplanes);+ int16_t fr_pos = nw_flat_run_count++;+ flat_run &fr = flat_runs[fr_pos];+ fr.x_start = (int16_t) s;+ fr.x_true = (int16_t) start;+ fr.x_end = (int16_t) x;+ fr.y = (uint8_t) y;+ fr.next = visplane_heads[plane];+ visplane_heads[plane] = fr_pos;+ }+ }+ if (draw && nw_flat_run_count) nw_flush_planes(numvisplanes);+}++// before drawing right to left: each band's carries from its left neighbour, worked out left to right+static void nw_plane_edges(uint8_t *band) {+ memset(nw_carry_plane, -1, sizeof nw_carry_plane);+ for (int k = 0; k < SCREENWIDTH / NW_BAND_W - 1; k++) {+ int x0 = k * NW_BAND_W;+ render_frame_buffer = band - x0;+ nw_mark_planes(x0, x0 + NW_BAND_W);+ nw_plane_runs(x0, x0 + NW_BAND_W, false);+ memcpy(nw_edge_plane[k], nw_carry_plane, sizeof nw_carry_plane);+ memcpy(nw_edge_start[k], nw_carry_start, sizeof nw_carry_start);+ }+}++// ---------------------------------------------------------------------------------------------------------------+// walls in the band: by framedrawable, each texture or patch decoded once++static void nw_draw_fd(int fd_num, int head) {+ int id = framedrawables[fd_num].real_id;+ if (id > 0) {+ draw_composite_columns(id, head);+ } else if (id < 0) {+ int16_t heads[WHD_PATCH_MAX_WIDTH];+ uint8_t heights[WHD_PATCH_MAX_WIDTH];+ draw_patch_columns(-id, head, heads, heights, 0);+ }+}++// the columns in nw_bidx, by framedrawable+static void nw_draw_batch(int n) {+ memset(fd_heads, -1, sizeof(fd_heads));+ for (int j = n - 1; j >= 0; j--) {+ const pd_column &c = COL_REC(j);+ nw_blink[j] = fd_heads[c.fd_num];+ fd_heads[c.fd_num] = (int16_t) j;+ }+ for (int fd_num = 0; fd_num < num_framedrawables; fd_num++) {+ if (fd_heads[fd_num] != -1) nw_draw_fd(fd_num, fd_heads[fd_num]);+ }+}++static void nw_band_walls(int x0, int x1) {+ nw_tm_low = nw_wall_low < 0 ? 0 : nw_wall_low;+ int n = 0;+ for (int i = 0; i < render_col_count; i++) {+ int x = COL_X(render_cols[i]);+ if (x < x0 || x >= x1) continue;+ nw_bidx[n++] = (int16_t) i;+ if (n == NW_BATCH) {+ nw_draw_batch(n);+ n = 0;+ }+ }+ if (n) nw_draw_batch(n);+}++// ---------------------------------------------------------------------------------------------------------------+// sprites, masked walls and the weapon (r_things.c works out which columns, in vanilla's order): the columns+// of one at a time gathered after the walls' records, then drawn++static int nw_scratch_base, nw_scratch_count;+// vanilla's fuzzpos, from frame to frame+static int8_t fuzzpos;++extern "C" void nw_masked_column(int x, int yl, int yh, int base_offset, int fuzz) {+ if (fuzz) {+ // vanilla's R_DrawFuzzColumn: its rows, and fuzzpos going on in vanilla's order (counted, not drawn,+ // outside the band)+ if (!yl) yl = 1;+ if (yh == viewheight - 1) yh = viewheight - 2;+ if (yh < yl) return;+ if (x >= nw_band_x0 && x < nw_band_x1) {+ const lighttable_t *darken_map = xcolormaps + 256 * 6;+ uint8_t *p = render_frame_buffer + yl * NW_STRIDE + x;+ int pos = nw_fuzz_at;+ for (int y = yl; y <= yh; y++, p += NW_STRIDE) {+ *p = darken_map[p[fuzzoffset[pos] > 0 ? NW_STRIDE : -NW_STRIDE]];+ if (++pos == FUZZTABLE) pos = 0;+ }+ }+ nw_fuzz_at = (nw_fuzz_at + yh - yl + 1) % FUZZTABLE;+ return;+ }+ if (x < nw_band_x0 || x >= nw_band_x1) return;+#if HOST+ if (nw_dumping(x)) printf("COL x=%d yl=%d yh=%d iscale=%d texturemid=%d (masked)\n", x, yl, yh, dc_iscale,+ nw_masked_texturemid - (base_offset << FRACBITS));+#endif+ if (nw_scratch_count == NW_SCRATCH) nw_masked_flush();+ if (!nw_scratch_count) nw_masked_low = -1;+ nw_fill(render_cols[nw_scratch_base + nw_scratch_count++], x, yl, yh,+ nw_masked_texturemid - (base_offset << FRACBITS), &nw_masked_low);+}++extern "C" void nw_masked_flush(void) {+ if (!nw_scratch_count) return;+ for (int j = 0; j < nw_scratch_count; j++) nw_bidx[j] = (int16_t) (nw_scratch_base + j);+ nw_tm_low = nw_masked_low < 0 ? 0 : nw_masked_low;+ int n = nw_scratch_count;+ nw_scratch_count = 0;+ nw_draw_batch(n);+}++// ---------------------------------------------------------------------------------------------------------------+// screens without the 3D view: their pictures for the band's columns++// a 320 x 200 picture's columns x0 to x1 (each decoded top to bottom, once)+static void nw_draw_splash(int patch_num, int x0, int x1) {+ patch_decode_info pdi;+ get_patch_decoder(patch_num, &pdi);+ const uint16_t *col_offsets = pdi.col_offsets;+ const uint8_t *patch_decoder_table = get_patch_decoder_table(patch_num, pdi.decoder);+ int h = patch_height(pdi.patch);+ for (int col = x0; col < x1; col++) {+ uint16_t col_offset = col_offsets[col];+ if (0xff == (col_offset >> 8)) col_offset = col_offsets[col_offset & 0xff];+ th_bit_input bi;+ if (patch_byte_addressed(pdi.patch)) {+ th_bit_input_init(&bi, pdi.patch + pdi.data_index + col_offset);+ } else {+ th_bit_input_init_bit_offset(&bi, pdi.patch + pdi.data_index, col_offset);+ }+ uint8_t *dest = render_frame_buffer + col;+ uint8_t prev_pixel = 0;+ for (int y = 0; y < h && y < SCREENHEIGHT; y++, dest += NW_STRIDE) {+ uint16_t p = th_decode_table_special_16(pdi.decoder, patch_decoder_table, &bi);+ prev_pixel = p < 256 ? (uint8_t) p : (uint8_t) (prev_pixel + (p & 0xff) - 3);+ *dest = prev_pixel;+ }+ }+}++// a flat tiled over the band (behind the ending's text, and darker behind the keys page)+static void nw_tile_flat(const char *name, int x0, int x1, int light = 0) {+ int flatnum = W_GetNumForName(name) - firstflat;+ uint8_t *flat_data = nullptr;+ for (int slot = 0; slot < cached_flat_slots; slot++) {+ if (FLAT_SLOT_PIC(slot) == flatnum) flat_data = FLAT_SLOT_ADDR(slot);+ }+ if (!flat_data) flat_data = decode_flat_to_slot(0, flatnum);+ const lighttable_t *map = light ? xcolormaps + 256 * light : nullptr;+ for (int y = 0; y < SCREENHEIGHT; y++) {+ uint8_t *dest = render_frame_buffer + y * NW_STRIDE;+ const uint8_t *src = flat_data + (y & 63);+ for (int x = x0; x < x1; x++) dest[x] = map ? map[src[(x & 63) * 64]] : src[(x & 63) * 64];+ }+}++static void nw_draw_background(int x0, int x1, int help) {+ int patch_num = 0;+ if (help) {+ switch (help & 127) {+ case 1: patch_num = W_GetNumForName("HELP1"); break;+ case 2: patch_num = W_GetNumForName("HELP2"); break;+ case 3: patch_num = W_GetNumForName("HELP"); break;+ case 4: nw_tile_flat("FLOOR4_8", x0, x1, 22); return;+ }+ } else {+ switch (gamestate) {+ case GS_INTERMISSION: patch_num = wi_background_patch_num; break;+ case GS_DEMOSCREEN: patch_num = W_GetNumForName(pagename); break;+ case GS_FINALE:+ if (finalestage == F_STAGE_TEXT) {+ nw_tile_flat(finaleflat, x0, x1);+ return;+ } else if (finalestage == F_STAGE_ARTSCREEN) {+ const char *ln = F_ArtScreenLumpName();+ if (ln) patch_num = W_GetNumForName(ln);+ }+ break;+ default: break;+ }+ }+ if (patch_num) {+ nw_draw_splash(patch_num, x0, x1);+ } else {+ for (int y = 0; y < SCREENHEIGHT; y++) memset(render_frame_buffer + y * NW_STRIDE + x0, 0, x1 - x0);+ }+}++#if HOST+// (tests: FUZZRESET=1 starts each demo with vanilla's fuzz where a new game has it, as the reference, which+// plays one demo at a time, does)+extern "C" void host_demo_start(void) {+ if (getenv("FUZZRESET")) fuzzpos = 0;+}+#endif++// ---------------------------------------------------------------------------------------------------------------+// the frame++// the status bar, messages, the menu... over the picture, gathered once a frame+static vpatchlist_t nw_patches[400];++// flats are kept in what the records leave of list_buffer: the blocks written over since the last time+// forgotten, the free ones this frame's slots+static void nw_flats_ready() {+ int lo = ((render_col_count > nw_wall_hwm ? render_col_count : nw_wall_hwm) + NW_SCRATCH) * (int) sizeof(pd_column);+ int hi = NW_POOL - (nw_plane_count > nw_plane_hwm ? nw_plane_count : nw_plane_hwm) * (int) sizeof(nw_plane_rec);+ nw_wall_hwm = render_col_count;+ nw_plane_hwm = nw_plane_count;+ const int blocks = NW_POOL / 4096;+ int slots = 0;+ for (int b = blocks - 1; b >= 0; b--) {+ if (b * 4096 < lo || (b + 1) * 4096 > hi) {+ cached_flat_picnum[b] = 0xff;+ } else {+ nw_slot_addr[slots] = list_buffer + b * 4096;+ nw_slot_block[slots++] = (uint8_t) b;+ }+ }+ cached_flat_picnum[blocks] = 0xff;+ if (!slots) {+ // (no whole block: the 4 KB under the floors and ceilings, for this frame only)+ nw_slot_addr[0] = list_buffer + ((hi - 4096) & ~3);+ nw_slot_block[0] = (uint8_t) blocks;+ slots = 1;+ }+ cached_flat_slots = (uint8_t) slots;+}++// the 3D view's columns x0 to x1+static void nw_draw_view(int x0, int x1) {+ nw_mark_planes(x0, x1);+ nw_plane_runs(x0, x1, true);+ nw_band_walls(x0, x1);+ nw_draw_masked();+}++static void nw_draw_band(uint8_t *band, int x0, int x1, bool view, int help, bool strips) {+ nw_band_x0 = x0;+ nw_band_x1 = x1;+ render_frame_buffer = band - x0;+ I_VideoBuffer = render_frame_buffer;+ nw_band_begin(band, x0, x1);+ if (view) {+ if (automapactive) {+ nw_direct = true;+ AM_Drawer();+ nw_direct = false;+ } else {+ if (strips) {+ // this band's view: the BSP pass again, its columns alone kept+ pd_begin_frame();+ nw_strip_x0 = x0;+ nw_strip_x1 = x1;+ R_RenderPlayerView(&players[displayplayer]);+ render_frame_buffer = band - x0;+ nw_flats_ready();+ if (nw_overflowed) {+ // (not even the band's pieces all found room: what has none is left black)+ for (int y = 0; y < viewheight; y++) memset(render_frame_buffer + y * NW_STRIDE + x0, 0, x1 - x0);+ }+ // (the fuzz of the sprites, the same as in the first pass, is where that pass's count left it)+ nw_strips++;+ }+ nw_scratch_base = render_col_count;+ nw_draw_view(x0, x1);+ nw_strip_x0 = 0;+ nw_strip_x1 = SCREENWIDTH;+ }+ for (int y = viewheight; y < SCREENHEIGHT; y++) memset(render_frame_buffer + y * NW_STRIDE + x0, 0, x1 - x0);+ } else {+ nw_draw_background(x0, x1, help);+ if (gamestate == GS_FINALE && finalestage == F_STAGE_TEXT && !help) {+ nw_direct = true;+ F_TextWrite();+ nw_direct = false;+ } else if ((help & 127) == 4) {+ nw_direct = true;+ M_DrawKeys();+ nw_direct = false;+ }+ }+ V_DrawPatchListBand(nw_patches);+ nw_band_end(x0, x1);+}++void pd_end_frame(int wipe_start) {+ (void) wipe_start;+ int help = inhelpscreens;+ bool view = gamestate == GS_LEVEL && !help;+#if HOST+ // (tests: NWSTRIPS=1 draws every frame band by band, NWRTL=1 right to left)+ static int force_strips = -1;+ if (force_strips < 0) {+ force_strips = getenv("NWSTRIPS") != nullptr;+ nw_scan_right_to_left = getenv("NWRTL") != nullptr;+ }+ if (force_strips) nw_overflowed = true;+#endif+ bool strips = view && !automapactive && nw_overflowed;+ nw_flats_ready();+ int fuzz_end = fuzzpos;+#if HOST+ {+ extern int nw_opening_count, nw_vissprite_count;+ if (view && !strips) {+ if (nw_plane_count > (int) nw_peak[0]) nw_peak[0] = nw_plane_count;+ if (nw_drawseg_count > (int) nw_peak[1]) nw_peak[1] = nw_drawseg_count;+ if (nw_opening_count > (int) nw_peak[2]) nw_peak[2] = nw_opening_count;+ if (nw_vissprite_count > (int) nw_peak[3]) nw_peak[3] = nw_vissprite_count;+ }+ nw_last_walls = view && !strips ? render_col_count : 0;+ nw_last_planes = view && !strips ? nw_plane_count : 0;+ }+ if (view && getenv("FUZZTRACE")) printf("FUZZ %d %d %d%d\n", leveltime, fuzzpos, gameepisode, gamemap);+#endif+ if (view && !automapactive) {+ nw_col_frames[render_col_count / 250 < 40 ? render_col_count / 250 : 39]++;+ fuzz_end = nw_fuzz_prepass(fuzzpos);+ }++ // what is drawn over the picture, gathered once+ nw_patches[0].header.max = count_of(nw_patches) - 1;+ V_BeginPatchList(nw_patches);+ if (!help) {+ if (gamestate == GS_LEVEL) ST_Drawer(false, !pre_wipe_state);+ else if (gamestate == GS_INTERMISSION) WI_Drawer();+ } else if (gamestate == GS_LEVEL) {+ ST_doPaletteStuff();+ }+ ST_FpsDrawer(-1);+ if (!pre_wipe_state && gamestate == GS_LEVEL && gametic && !inhelpscreens) HU_Drawer();+ if (paused) {+ int y = automapactive ? 4 : viewwindowy + 4;+ V_DrawPatchDirect(viewwindowx + (scaledviewwidth - 68) / 2, y, VPATCH_HANDLE(VPATCH_NAME(M_PAUSE)));+ }+ M_Drawer();+ if (pre_wipe_state == PRE_WIPE_EXTRA_FRAME_NEEDED) pre_wipe_state = PRE_WIPE_EXTRA_FRAME_DONE;++ nw_frame_begin();+ uint8_t __aligned(8) band[NW_BAND_W * SCREENHEIGHT];+ const int bands = SCREENWIDTH / NW_BAND_W;+ if (nw_scan_right_to_left) {+ // (a frame drawn band by band has its visplanes in each band's pass: the spans' starts in the bands+ // on the left are not known, and are taken where the band starts)+ bool edges = view && !automapactive && !strips;+ if (edges) nw_plane_edges(band);+ for (int k = bands - 1; k >= 0; k--) {+ if (edges && k) {+ memcpy(nw_carry_plane, nw_edge_plane[k - 1], sizeof nw_carry_plane);+ memcpy(nw_carry_start, nw_edge_start[k - 1], sizeof nw_carry_start);+ } else {+ memset(nw_carry_plane, -1, sizeof nw_carry_plane);+ }+ nw_draw_band(band, k * NW_BAND_W, (k + 1) * NW_BAND_W, view, help, strips);+ }+ } else {+ memset(nw_carry_plane, -1, sizeof nw_carry_plane);+ for (int k = 0; k < bands; k++) nw_draw_band(band, k * NW_BAND_W, (k + 1) * NW_BAND_W, view, help, strips);+ }+ fuzzpos = (int8_t) fuzz_end;+ nw_band_x0 = 0;+ nw_band_x1 = SCREENWIDTH;+ nw_frame_end();+}diff --git a/games/doom/src/nw/nw_render.h b/games/doom/src/nw/nw_render.hnew file mode 100644index 00000000..5f549f64--- /dev/null+++ b/games/doom/src/nw/nw_render.h@@ -0,0 +1,86 @@+/* Doom on the calculator: the renderer, as the BSP pass (r_segs.c, r_things.c, r_plane.c) and the back end+ * that draws the screen (pd_render.cpp) share it.+ *+ * There is no room for a frame buffer. The BSP pass records the frame as vanilla would draw it: every column+ * of wall and sky, every column of floor and ceiling (with vanilla's visplanes, split as R_CheckPlane does),+ * the drawsegs that clip sprites with their silhouettes, and the vissprites. The screen is then drawn a+ * vertical band of NW_BAND_W columns at a time, in the order the panel refreshes them, each band whole before+ * it is sent: its floors and ceilings as vanilla's spans, its walls, then sprites, masked walls and the+ * weapon in vanilla's order with vanilla's clipping (R_DrawMasked). A frame too busy for the records is drawn+ * again for each band, the view rendered for that band's columns only. */+#ifndef NW_RENDER_H+#define NW_RENDER_H+#include "doomtype.h"+#include "m_fixed.h"+#ifdef __cplusplus+extern "C" {+#endif++#define NW_BAND_W 64++/* the columns this pass records (the whole view, or one band when a frame is drawn band by band: the BSP+ * still goes over the whole view, so that everything is as vanilla has it, but only the band is kept) */+extern int nw_strip_x0, nw_strip_x1;+/* the band being drawn */+extern int nw_band_x0, nw_band_x1;++/* the frame's table of wall texture heights (dc_texturemid), shared by the columns of a wall's tier */+int nw_add_tmid(fixed_t texturemid);+extern int nw_tmid_cur;++/* walls and sky (dc_x, dc_yl, dc_yh, dc_iscale, dc_source, dc_colormap_index, nw_tmid_cur), and floors and+ * ceilings: recorded by the BSP pass */+void nw_record_wall(void);+void nw_record_plane(int x, int yl, int yh, int plane);+/* the pass ran out of room for something: the frame is drawn band by band */+void nw_record_overflow(void);++/* drawsegs that matter to R_DrawMasked: with a silhouette or a masked middle texture */+typedef struct {+ const void *curline;+ int16_t x1, x2;+ fixed_t scale1, scale2, scalestep;+ fixed_t bsilheight, tsilheight;+ int16_t topclip, botclip; /* where column x1 would be in nw_openings, or one of these: */+ int16_t masked; /* the masked texture's nw_masked_t in nw_openings, or NW_OPEN_NONE */+ uint8_t silhouette;+} nw_drawseg_t;+#define NW_OPEN_NONE (-32768)+#define NW_OPEN_MIN (-32767) /* every column -1: minfloorceilingcliparray */+#define NW_OPEN_MAX (-32766) /* every column viewheight: maxfloorceilingcliparray */+#define NW_DRAWSEG_MAX 100 /* (E1M1 to E1M9 have up to 86) */+#define NW_OPENINGS (3328) /* (up to 2878 bytes) */+extern nw_drawseg_t nw_drawsegs[NW_DRAWSEG_MAX];+extern int nw_drawseg_count;+extern uint8_t nw_openings[NW_OPENINGS];+/* a masked seg's texture: before its columns (x0 on) in nw_openings */+typedef struct {+ fixed_t texturemid;+ int16_t texture, x0;+ int8_t lightnum;+ uint8_t pad[3];+} nw_masked_t;++/* band time (r_things.c): sprites, masked walls and the weapon of the band, in vanilla's order */+void nw_sort_vissprites(void);+void nw_draw_masked(void);+/* ... their columns go to the back end, a sprite or a masked wall at a time */+void nw_masked_column(int x, int yl, int yh, int base_offset, int fuzz);+void nw_masked_flush(void);+extern fixed_t nw_masked_texturemid;++/* fuzz (the shimmer of invisible things): vanilla's fuzzpos where each fuzzy sprite and the weapon start,+ * from the frame's sprites counted in vanilla's order before the bands; nw_fuzz_at goes on from there */+extern int nw_fuzz_at;+int nw_fuzz_prepass(int fuzzpos);++/* a pass over the BSP starts: nothing recorded yet */+void nw_clear_vissprites(void);+void nw_clear_drawsegs(void);+/* dc_source, a texture's column: its single patch column if it has one (0: nothing to draw) */+int nw_resolve_column(void);++#ifdef __cplusplus+}+#endif+#endifdiff --git a/games/doom/src/nw/plat.h b/games/doom/src/nw/plat.hindex bbbe1676..07864f93 100644--- a/games/doom/src/nw/plat.h+++ b/games/doom/src/nw/plat.h@@ -18,6 +18,8 @@ uint32_t plat_millis(void);void plat_sleep(uint32_t ms);void plat_push(int x, int y, int w, int h, const uint16_t *px);void plat_fill(int x, int y, int w, int h, uint16_t c);+/* waits for the display's next refresh to start (false if it cannot tell) */+bool plat_vblank(void);bool plat_save(const char *name, const void *data, uint32_t len);const uint8_t *plat_load(const char *name, uint32_t *len); /* NULL if absent *//* a file of len bytes to write in place (the calculator's storage is RAM): plat_reserve gives itsdiff --git a/games/doom/src/nw/plat_eadk.c b/games/doom/src/nw/plat_eadk.cindex 46d8d8c8..c78c66e3 100644--- a/games/doom/src/nw/plat_eadk.c+++ b/games/doom/src/nw/plat_eadk.c@@ -14,6 +14,7 @@ void plat_sleep(uint32_t ms) {void plat_push(int x, int y, int w, int h, const uint16_t *px) {eadk_display_push_rect((eadk_rect_t){(uint16_t)x, (uint16_t)y, (uint16_t)w, (uint16_t)h}, px);}+bool plat_vblank(void) { return eadk_display_wait_for_vblank(); }void plat_fill(int x, int y, int w, int h, uint16_t c) {if (w > 0 && h > 0) eadk_display_push_rect_uniform((eadk_rect_t){(uint16_t)x, (uint16_t)y, (uint16_t)w, (uint16_t)h}, c);}diff --git a/games/doom/src/nw/plat_host.c b/games/doom/src/nw/plat_host.cindex bdf68bfb..609a9edd 100644--- a/games/doom/src/nw/plat_host.c+++ b/games/doom/src/nw/plat_host.c@@ -26,6 +26,7 @@ void plat_push(int x, int y, int w, int h, const uint16_t *px) {for (int c = 0; c < w; c++)if ((unsigned)(x + c) < PLAT_W && (unsigned)(y + r) < PLAT_H) host_fb[(y + r) * PLAT_W + x + c] = px[r * w + c];}+bool plat_vblank(void) { return false; }void plat_fill(int x, int y, int w, int h, uint16_t c) {for (int r = 0; r < h; r++)for (int k = 0; k < w; k++)diff --git a/games/doom/src/pd_render.cpp b/games/doom/src/pd_render.cppindex 9fa6acf1..4da9d1bb 100644--- a/games/doom/src/pd_render.cpp+++ b/games/doom/src/pd_render.cpp@@ -168,21 +168,21 @@ int pd_flag;fixed_t pd_scale;#if NUMWORKS-// The calculator has no room for a frame buffer: the screen is drawn a band of NW_BAND_H rows at a time-// (nw/i_video.c pushes each to the display), from column lists made once per frame. render_frame_buffer-// points where row 0 would be, so that the rows of the band are where the drawing code expects them.+// The calculator has no room for a frame buffer: the screen is drawn a band of NW_BAND_W columns at a time+// (nw/nw_render.h, nw/nw_back.inc), and nw/i_video.c sends each band to the display. render_frame_buffer+// points where column 0 would be (NW_STRIDE bytes a row), so that the band's columns are where the drawing+// code expects them.+#include "nw/nw_render.h"extern "C" {extern uint8_t *nw_band;-extern int nw_band_y0, nw_band_y1;extern boolean nw_direct;-void nw_band_begin(uint8_t *band, int y0, int y1);-void nw_band_end(int x0, int x1);+void nw_band_begin(uint8_t *band, int x0, int x1);void nw_frame_begin(void);void nw_frame_end(void);void V_DrawPatchListBand(const vpatchlist_t *patchlist);}-#define NW_BAND_H 40-static uint8_t __aligned(4) visplane_bit[(SCREENWIDTH / 8) * NW_BAND_H];+#define NW_STRIDE NW_BAND_W+static uint8_t __aligned(4) visplane_bit[WHD_PATCH_MAX_WIDTH]; // (RP2040 Doom's, hardly used here)#elseextern uint8_t __aligned(4) frame_buffer[2][SCREENWIDTH * MAIN_VIEWHEIGHT];static uint8_t __aligned(4) visplane_bit[(SCREENWIDTH / 8) * MAIN_VIEWHEIGHT]; // this is also used for patch decoding in core1 (since flats are done by then)@@ -257,26 +257,18 @@ int pd_dump_frame = -1;// <24,0x000000> <8, visplane_idx>#if NUMWORKS-// vanilla's column at its full precision: the whole texturemid, and the iscale (always below 2^24: vanilla-// keeps scales between 1/256 and 64), as R_DrawColumn uses them+// a column of wall, sky, sprite or masked wall as vanilla's R_DrawColumn draws it, at its full precision: its+// rows, iscale (always below 2^24: vanilla keeps scales between 1/256 and 64), colormap, the column of its+// texture or patch (its framedrawable), its x, and its texturemid to the last bit: tm << 14, and the fraction+// of a quarter of a unit (map heights are in quarters) it shares with the others (nw_tm_low) when lowsel is setstruct pd_column {- int16_t next;- uint8_t yl;- uint8_t yh;-- uint32_t scale: 24;- uint32_t colormap_index: 6;- uint32_t spare: 2;-- int32_t texturemid; // TEXTUREMID_PLANE for floors and ceilings-- uint8_t col;- uint8_t fd_num;- union {- uint8_t plane;- uint8_t x;- };- uint8_t spare2;+ uint8_t yl, yh, col, fd_num;+ uint32_t scale : 24;+ uint32_t colormap_index : 6;+ uint32_t lowsel : 1;+ uint32_t x_hi : 1;+ int32_t tm : 24;+ uint32_t x_lo : 8;};#elsestruct pd_column {@@ -307,6 +299,16 @@ struct pd_column {#define TO_COL_LO(c) ((c)&0x1f)#define FROM_COL_HI_LO(h,l) (((h)<<5)|(l))+#if NUMWORKS+// a run of floor or ceiling in the band: x_start to x_end of a span of vanilla's that starts at x_true (in a+// band on the left, if it goes on from there)+struct flat_run {+ int16_t next;+ int16_t x_start, x_end, x_true;+ uint8_t y;+};+static_assert(sizeof(pd_column) == 12, "");+#elsestruct __packed __aligned(2) flat_run {int16_t next;uint32_t x_start: 12;@@ -315,9 +317,6 @@ struct __packed __aligned(2) flat_run {};static_assert(sizeof(flat_run) == 6, "");-#if NUMWORKS-static_assert(sizeof(pd_column) == 16, "");-#elsestatic_assert(sizeof(pd_column) == 12, "");static_assert(sizeof(pd_column) == sizeof(flat_run) * 2, ""); // we pack two into the space of the other#endif@@ -377,30 +376,59 @@ const char *type_name(pd_column column) {}#if NUMWORKS+// the frame's records (nw/nw_back.inc): columns of wall and sky from the start, then those of the sprite or+// masked wall being drawn; columns of floor and ceiling from the end; flats decoded in what is left between#define RENDER_COL_MAX NW_RENDER_COL_MAX-#elif !PICO_RP2350+#define NW_SCRATCH 128+#define NW_POOL (NW_POOL_KB * 1024)+static uint8_t __aligned(4) list_buffer[NW_POOL];+#else+#if !PICO_RP2350#define RENDER_COL_MAX 3600#else#define RENDER_COL_MAX 7200#endifstatic uint8_t __aligned(4) list_buffer[RENDER_COL_MAX * sizeof(pd_column) + 64*64]; // extra 64*64 is for one flat+#endifstatic uint8_t *last_list_buffer_limit = list_buffer + sizeof(list_buffer);//static_assert(text_font_cpy > list_buffer, "");#define MAX_CACHED_FLATS (sizeof(list_buffer) / 4096)+#if NUMWORKS+// the flats kept: in 4 KB blocks of list_buffer (and one more place when no whole block is free); the slots+// are this frame's free blocks+static uint8_t cached_flat_picnum[MAX_CACHED_FLATS + 1];+static uint8_t *nw_slot_addr[MAX_CACHED_FLATS + 1];+static uint8_t nw_slot_block[MAX_CACHED_FLATS + 1];+#define FLAT_SLOT_ADDR(s) (nw_slot_addr[s])+#define FLAT_SLOT_PIC(s) (cached_flat_picnum[nw_slot_block[s]])+#elsestatic uint8_t cached_flat_picnum[MAX_CACHED_FLATS];+#define FLAT_SLOT_ADDR(s) (cached_flat0 - (s) * 4096)+#define FLAT_SLOT_PIC(s) (cached_flat_picnum[s])+#endifstatic uint8_t cached_flat_slots;static uint8_t *cached_flat0;static int16_t render_col_count;#define render_cols ((pd_column *)list_buffer)#if NUMWORKS// the band's runs of floor and ceiling, taken in turn (when they run out, what is gathered is drawn)-#define NW_FLAT_RUNS 512-static flat_run nw_flat_runs[NW_FLAT_RUNS];+#define NW_FLAT_RUNS 256+#define NW_BATCH 512+// what only the BSP pass uses (the columns each visplane has, r_plane.c) and what only the bands use+// (nw/nw_back.inc) share their room+static union {+ uint8_t plane_used[MAXVISPLANES][SCREENWIDTH / 8];+ struct {+ int16_t bidx[NW_BATCH], blink[NW_BATCH];+ flat_run flat_runs[NW_FLAT_RUNS];+ uint8_t vbit[(NW_BAND_W / 8) * MAIN_VIEWHEIGHT];+ int8_t edge_plane[SCREENWIDTH / NW_BAND_W - 1][MAIN_VIEWHEIGHT];+ int16_t edge_start[SCREENWIDTH / NW_BAND_W - 1][MAIN_VIEWHEIGHT];+ } band;+} nw_shared;+extern "C" { uint8_t (*const nw_plane_used)[SCREENWIDTH / 8] = nw_shared.plane_used; }static int16_t nw_flat_run_count;-#define flat_runs nw_flat_runs-// for each column of a band, the next of its framedrawable (the column lists' own links stay as they are,-// for the next band); on the stack of pd_end_frame, which has room to spare where the zone has none-static int16_t *fd_next;+#define flat_runs (nw_shared.band.flat_runs)#else#define flat_runs ((flat_run *)list_buffer)#endif@@ -412,6 +440,7 @@ static int16_t render_col_free;extern "C" { uint32_t nw_col_overflows, nw_col_peak, nw_col_frames[40]; }static bool nw_overflowed; // (this frame's columns did not all find room: it is drawn in strips)#endif+#if !NUMWORKSstatic int16_t alloc_pd_column(int x) {if (render_col_free < 0) {#if NUMWORKS@@ -844,7 +873,20 @@ static void push_down_x(int x, int new_index) {#endif}+#endif+#if HOST+extern "C" { uint32_t nw_cnt_walls, nw_cnt_planes, nw_cnt_masked, nw_max_walls, nw_max_planes, nw_max_masked; }+#endif+#if NUMWORKS+static void nw_pass_start();+#endifvoid pd_begin_frame() {+#if HOST+ if (nw_cnt_walls > nw_max_walls) nw_max_walls = nw_cnt_walls;+ if (nw_cnt_planes > nw_max_planes) nw_max_planes = nw_cnt_planes;+ if (nw_cnt_masked > nw_max_masked) nw_max_masked = nw_cnt_masked;+ nw_cnt_walls = nw_cnt_planes = nw_cnt_masked = 0;+#endifDEBUG_PINS_SET(start_end, 1);if (gamestate == GS_LEVEL) {// render_frame_index ^= 1;@@ -863,6 +905,9 @@ void pd_begin_frame() {#if DUMP_SORTINGif (0) printf("BEGIN FRAME\n");#endif+#if NUMWORKS+ nw_pass_start();+#else// newmemset(column_heads, -1, sizeof(column_heads));memset(visplane_bit, 0, sizeof(visplane_bit)); // todo could do this with dma@@ -871,10 +916,8 @@ void pd_begin_frame() {not_fully_covered_yh = MAIN_VIEWHEIGHT - 1;render_col_count = 0;render_col_free = -1;- pd_frame++;-#if NUMWORKS- nw_overflowed = false;#endif+ pd_frame++;DEBUG_PINS_CLR(start_end, 1);}@@ -933,11 +976,15 @@ void pd_add_span() {}#define FORCE_ISCALE 1-#if HOST-extern "C" { int nw_dbg_x = -1; }-#endif+void pd_add_column(pd_column_type type) {+#if NUMWORKS+#if HOST+ nw_cnt_walls++;+#endif+ nw_record_wall();+#else// --- VALIDATION AND CLAMPINGint count = dc_yh - dc_yl;if (count < 0)@@ -966,19 +1013,7 @@ void pd_add_column(pd_column_type type) {// --------#if HOST- {- // (tests: DUMPCOL="x tic" prints the columns at x in that tic's frame, as the reference does)- static int dump_x = -2, dump_tic;- if (dump_x == -2) {- const char *e = getenv("DUMPCOL");- dump_x = e ? atoi(e) : -1;- dump_tic = e && strchr(e, ' ') ? atoi(strchr(e, ' ')) : -1;- }- nw_dbg_x = leveltime == dump_tic && gamestate == GS_LEVEL ? dump_x : -1;- if (dc_x == dump_x && leveltime == dump_tic && gamestate == GS_LEVEL)- printf("COL x=%d yl=%d yh=%d iscale=%d texturemid=%d (pd_scale %d type %d)\n", dc_x, dc_yl, dc_yh,- (int)iscale, dc_texturemid, pd_scale, (int)type);- }+ nw_cnt_walls++;#endif// todo it would be nice to defer filling this in in case we are entirely clipped - let's get some stats on thatint rc_index = alloc_pd_column(dc_x);@@ -1061,9 +1096,14 @@ void pd_add_column(pd_column_type type) {p.second = std::max(p.second, frac);}#endif+#endif}void pd_add_masked_columns(uint8_t *ys, int seg_count) {+#if !NUMWORKS+#if HOST+ nw_cnt_masked += seg_count;+#endif// --- VALIDATION AND CLAMPINGfixed_t iscale;#if FORCE_ISCALE@@ -1191,6 +1231,7 @@ void pd_add_masked_columns(uint8_t *ys, int seg_count) {p.second = std::max(p.second, frac);}#endif+#endif}static int lightlevel(int visplane) {@@ -1203,6 +1244,12 @@ static int lightlevel(int visplane) {}void pd_add_plane_column(int x, int yl, int yh, fixed_t scale, int floor, int fd_num) {+#if HOST+ nw_cnt_planes++;+#endif+#if NUMWORKS+ nw_record_plane(x, yl, yh, fd_num); // (fd_num is the visplane)+#elseint rc_index = alloc_pd_column(x);if (rc_index < 0) return;int iscale = hw_divider_u32_quotient_inlined(0xffffffff, pd_scale);@@ -1221,8 +1268,10 @@ void pd_add_plane_column(int x, int yl, int yh, fixed_t scale, int floor, int fd#endifrender_cols[rc_index].next = -1;push_down_x(x, rc_index);+#endif}+#if !NUMWORKSstatic void reclip_fuzz_columns() {// re-add the fuzzy columns so they are correctly clippedfor (int x = 0; x < SCREENWIDTH; x++) {@@ -1316,6 +1365,7 @@ static void clip_columns(int yl, int yh) {}}+#endifstatic int translate_picnum(int picnum) {if (whd_flattospecial[picnum] != 0xff) {picnum = whd_specialtoflat[whd_flattranslation[whd_flattospecial[picnum]]];@@ -1324,7 +1374,7 @@ static int translate_picnum(int picnum) {}static uint8_t *decode_flat_to_slot(int cache_slot, int picnum) {- uint8_t *flat_data = cached_flat0 - cache_slot * 4096;+ uint8_t *flat_data = FLAT_SLOT_ADDR(cache_slot);DEBUG_PINS_SET(flat_decode, 1);uint16_t *pos = flat_decoder_buf;uint pos_size = count_of(flat_decoder_buf);@@ -1374,7 +1424,7 @@ static uint8_t *decode_flat_to_slot(int cache_slot, int picnum) {}}// printf("Pass %d, caching slot %d pic (%d)\n", pass, cache_slot, picnum);- cached_flat_picnum[cache_slot] = picnum;+ FLAT_SLOT_PIC(cache_slot) = picnum;DEBUG_PINS_CLR(flat_decode, 1);return flat_data;}@@ -1416,9 +1466,9 @@ static void flush_visplanes(int8_t *flatnum_next, int numvisplanes) {uint8_t *flat_data = nullptr;if (!pass) {for(cache_slot=0;cache_slot<cached_flat_slots;cache_slot++) {- if (cached_flat_picnum[cache_slot] == picnum) {+ if (FLAT_SLOT_PIC(cache_slot) == picnum) {// printf("Pass %d, using slot %d pic (%d)\n", pass, cache_slot, picnum);- flat_data = cached_flat0 - cache_slot * 4096;+ flat_data = FLAT_SLOT_ADDR(cache_slot);}}if (!flat_data) continue;@@ -1463,7 +1513,7 @@ static void flush_visplanes(int8_t *flatnum_next, int numvisplanes) {// stored a column at a time, flat[x * 64 + y])fixed_t distance = 0;for (int16_t fr = visplane_heads[vp]; fr != -1; fr = flat_runs[fr].next) {- int y = flat_runs[fr].y, x1 = flat_runs[fr].x_start;+ int y = flat_runs[fr].y, x1 = flat_runs[fr].x_true;if (y != last_y) {distance = FixedMul(rel_height, yslope[y]);xstep = FixedMul(distance, basexscale);@@ -1488,8 +1538,9 @@ static void flush_visplanes(int8_t *flatnum_next, int numvisplanes) {yfrac = -viewy - FixedMul(finesine(angle), length);uint32_t position = ((xfrac << 10) & 0xffff0000) | ((yfrac >> 6) & 0x0000ffff);uint32_t step = ((xstep << 10) & 0xffff0000) | ((ystep >> 6) & 0x0000ffff);- uint8_t *p = render_frame_buffer + y * SCREENWIDTH + x1;- uint8_t *p_end = render_frame_buffer + y * SCREENWIDTH + flat_runs[fr].x_end;+ position += step * (uint32_t) (flat_runs[fr].x_start - x1);+ uint8_t *p = render_frame_buffer + y * NW_STRIDE + flat_runs[fr].x_start;+ uint8_t *p_end = render_frame_buffer + y * NW_STRIDE + flat_runs[fr].x_end;while (p < p_end) {*p++ = colormap[flat_data[((position >> 20) & 0x0fc0) | ((position >> 10) & 63)]];position += step;@@ -1598,6 +1649,7 @@ static void flush_visplanes(int8_t *flatnum_next, int numvisplanes) {}}+#if !NUMWORKSstatic void draw_visplanes(int16_t fr_list) {if (!lastvisplane) return;int numvisplanes = lastvisplane - visplanes;@@ -1703,6 +1755,7 @@ static void draw_visplanes(int16_t fr_list) {}}+#endifstatic inline void col_render(uint8_t *dest, uint count, const uint8_t *source, fixed_t frac, fixed_t fracstep, const lighttable_t* colormap) {#if 0 && PICO_ON_DEVICEcol_interp->accum[0] = xs->col.frac;@@ -1747,22 +1800,33 @@ static inline void col_render(uint8_t *dest, uint count, const uint8_t *source,#elsedo {*dest = colormap[source[(frac >> FRACBITS) & 127]];- dest += SCREENWIDTH;+ dest += NW_STRIDE;frac += fracstep;} while (count--);#endif}#if NUMWORKS-// a column's link in the band's lists, and its rows in the band-#define COL_LINK(i) (fd_next[(i) & 0x7fffu])-static int nw_view_y0, nw_view_y1; // the rows of the 3D view in the band-#define COL_YL(c) ((c).yl < nw_view_y0 ? nw_view_y0 : (c).yl)-#define COL_YH(c) ((c).yh >= nw_view_y1 ? nw_view_y1 - 1 : (c).yh)+// the columns being drawn: entries 0 to n of nw_bidx (the records' indices), each linked to the next of its+// framedrawable (and then of its texture column) by nw_blink+#define nw_bidx (nw_shared.band.bidx)+#define nw_blink (nw_shared.band.blink)+static int32_t nw_tm_low;+#define COL_REC(i) (render_cols[nw_bidx[i]])+#define COL_X(c) ((c).x_lo | ((c).x_hi << 8))+#define COL_LINK(i) (nw_blink[i])+#define COL_YL(c) ((c).yl)+#define COL_YH(c) ((c).yh)+#define COL_PTR(c, i) (render_frame_buffer + __mul_instruction(COL_YL(c), NW_STRIDE) + COL_X(c))+#define COL_TEXTUREMID(c) ((int32_t)((uint32_t)(int32_t)(c).tm << 14) + ((c).lowsel ? nw_tm_low : 0))#else+#define COL_REC(i) (render_cols[(i) & 0x7fffu])#define COL_LINK(i) (render_cols[(i) & 0x7fffu].next)#define COL_YL(c) ((c).yl)#define COL_YH(c) ((c).yh)+#define COL_PTR(c, i) (render_frame_buffer + __mul_instruction(COL_YL(c), SCREENWIDTH) + c.x + ((i & 0x8000u) >> 7u))+#define COL_TEXTUREMID(c) UP_SHIFT((c).texturemid)+#define NW_STRIDE SCREENWIDTH#endifstruct patch_hash_entry_header {@@ -1972,9 +2036,9 @@ const uint8_t *get_patch_decoder_table(uint patch_num, const uint16_t *decoder,static void draw_patch_columns(int patch_num, int patch_head, int16_t *col_heads, uint8_t *col_height, int translated) {// fix up the sky scale (we had to preserve the original scale for column clipping/sorting)// note: we do this as a rare edge case here, rather than checking in loops- if (patch_num == skytexture_patch) {+ if (!NUMWORKS && patch_num == skytexture_patch) { // (the calculator keeps the sky's own iscale)for(int j=patch_head; j != -1;) {- auto &c = render_cols[j & 0x7fffu];+ auto &c = COL_REC(j);c.scale = 0x10000;j = COL_LINK(j);}@@ -1990,7 +2054,7 @@ static void draw_patch_columns(int patch_num, int patch_head, int16_t *col_headsassert(i != -1);int h = patch_height(pdi.patch);do {- auto &c = render_cols[i & 0x7fffu];+ auto &c = COL_REC(i);#if NUMWORKSuint col = c.col;#else@@ -2005,7 +2069,7 @@ static void draw_patch_columns(int patch_num, int patch_head, int16_t *col_heads// we actually don't know how many pixels are in the column without looking at the run data, however// we can see the last pixel we've used, which means, as a bonus, we won't decode all of a column when we only// use the top- fixed_t texturemid = UP_SHIFT(c.texturemid);+ fixed_t texturemid = COL_TEXTUREMID(c);fixed_t fracstep = DDA_UP_SHIFT(c.scale);if (!fracstep) fracstep = 0x10000;fixed_t start = (texturemid + (COL_YL(c) - centery) * fracstep) >> FRACBITS;@@ -2082,31 +2146,29 @@ static void draw_patch_columns(int patch_num, int patch_head, int16_t *col_headsif (fixedcolormap || patch_num == skytexture_patch) {#if NO_USE_DC_COLORMAP- should_be_const lighttable_t *dc_colormap = colormaps + 256 * (patch_num == 1203 ? 0 : fixedcolormap);+ should_be_const lighttable_t *dc_colormap = colormaps + 256 * (patch_num == skytexture_patch ? 0 : fixedcolormap);#endifdo {- const auto &c = render_cols[i & 0x7fffu];- uint8_t *p = render_frame_buffer + __mul_instruction(COL_YL(c), SCREENWIDTH) + c.x +- ((i & 0x8000u) >> 7u);- assert (c.texturemid != TEXTUREMID_PLANE);+ const auto &c = COL_REC(i);+ uint8_t *p = COL_PTR(c, i);+fixed_t fracstep = DDA_UP_SHIFT(c.scale);if (!fracstep) fracstep = 0x10000;- fixed_t frac = UP_SHIFT(c.texturemid) + (COL_YL(c) - centery) * fracstep;+ fixed_t frac = COL_TEXTUREMID(c) + (COL_YL(c) - centery) * fracstep;col_render(p, COL_YH(c) - COL_YL(c), pixels, frac, fracstep, dc_colormap);i = COL_LINK(i);} while (i != -1);} else {do {- const auto &c = render_cols[i & 0x7fffu];- uint8_t *p = render_frame_buffer + __mul_instruction(COL_YL(c), SCREENWIDTH) + c.x +- ((i & 0x8000u) >> 7u);+ const auto &c = COL_REC(i);+ uint8_t *p = COL_PTR(c, i);#if NO_USE_DC_COLORMAPshould_be_const lighttable_t *dc_colormap = colormaps + 256 * c.colormap_index;#endif- assert (c.texturemid != TEXTUREMID_PLANE);+fixed_t fracstep = DDA_UP_SHIFT(c.scale);if (!fracstep) fracstep = 0x10000;- fixed_t frac = UP_SHIFT(c.texturemid) + (COL_YL(c) - centery) * fracstep;+ fixed_t frac = COL_TEXTUREMID(c) + (COL_YL(c) - centery) * fracstep;// if (get_core_num()) {// for(int yy=COL_YL(c);yy<=COL_YH(c);yy++) {// p[0] = 0xfc;@@ -2137,7 +2199,7 @@ static void draw_composite_columns(int texture_num, int tex_head) {int max = 0;// printf("DCC tex=%d\n", texture_num);do {- auto &c = render_cols[i & 0x7fffu];+ auto &c = COL_REC(i);#if NUMWORKSuint col = c.col;#else@@ -2152,7 +2214,7 @@ static void draw_composite_columns(int texture_num, int tex_head) {// we actaully don't know how many pixels are in the column without looking at the run data, however// we can see the last pixel we've used, which means, as a bonus, we won't decode all of a column when we only// use the top- fixed_t texturemid = UP_SHIFT(c.texturemid);+ fixed_t texturemid = COL_TEXTUREMID(c);fixed_t fracstep = DDA_UP_SHIFT(c.scale);if (!fracstep) fracstep = 0x10000;fixed_t start = (texturemid + (COL_YL(c) - centery) * fracstep) >> FRACBITS;@@ -2399,28 +2461,26 @@ static void draw_composite_columns(int texture_num, int tex_head) {should_be_const lighttable_t *dc_colormap = colormaps + 256 * fixedcolormap;#endifdo {- const auto &c = render_cols[i & 0x7fffu];- uint8_t *p = render_frame_buffer + __mul_instruction(COL_YL(c), SCREENWIDTH) + c.x +- ((i & 0x8000u) >> 7u);- assert (c.texturemid != TEXTUREMID_PLANE);+ const auto &c = COL_REC(i);+ uint8_t *p = COL_PTR(c, i);+fixed_t fracstep = DDA_UP_SHIFT(c.scale);if (!fracstep) fracstep = 0x10000;- fixed_t frac = UP_SHIFT(c.texturemid) + (COL_YL(c) - centery) * fracstep;+ fixed_t frac = COL_TEXTUREMID(c) + (COL_YL(c) - centery) * fracstep;col_render(p, COL_YH(c) - COL_YL(c), pixels, frac, fracstep, dc_colormap);i = COL_LINK(i);} while (i != -1);} else {do {- const auto &c = render_cols[i & 0x7fffu];- uint8_t *p = render_frame_buffer + __mul_instruction(COL_YL(c), SCREENWIDTH) + c.x +- ((i & 0x8000u) >> 7u);+ const auto &c = COL_REC(i);+ uint8_t *p = COL_PTR(c, i);#if NO_USE_DC_COLORMAPshould_be_const lighttable_t *dc_colormap = colormaps + 256 * c.colormap_index;#endif- assert (c.texturemid != TEXTUREMID_PLANE);+fixed_t fracstep = DDA_UP_SHIFT(c.scale);if (!fracstep) fracstep = 0x10000;- fixed_t frac = UP_SHIFT(c.texturemid) + (COL_YL(c) - centery) * fracstep;+ fixed_t frac = COL_TEXTUREMID(c) + (COL_YL(c) - centery) * fracstep;col_render(p, COL_YH(c) - COL_YL(c), pixels, frac, fracstep, dc_colormap);i = COL_LINK(i);} while (i != -1);@@ -2428,15 +2488,15 @@ static void draw_composite_columns(int texture_num, int tex_head) {#elseuint8_t color = texture_num & 0xff;do {- const auto &c = render_cols[i & 0x7fffu];+ const auto &c = COL_REC(i);uint8_t *p =- render_frame_buffer + __mul_instruction(COL_YL(c), SCREENWIDTH) + c.x + ((i & 0x8000u) >> 7u);- assert (c.texturemid != TEXTUREMID_PLANE);+ COL_PTR(c, i);+#if NO_USE_DC_COLORMAPshould_be_const lighttable_t *dc_colormap = colormaps + 256 * c.colormap_index;#endifuint8_t lcolor = dc_colormap[color];- // fixed_t texturemid = UP_SHIFT(c.texturemid);+ // fixed_t texturemid = COL_TEXTUREMID(c);// fixed_t fracstep = DDA_UP_SHIFT(c.scale);// fixed_t start = (texturemid + (COL_YL(c) - centery) * fracstep) >> FRACBITS;// fixed_t end = (texturemid + (COL_YH(c) - centery) * fracstep) >> FRACBITS;@@ -2478,6 +2538,7 @@ static void draw_composite_columns(int texture_num, int tex_head) {}}+#if !NUMWORKS// noinline as it uses allocastatic void __noinline draw_regular_columns(int core) {if (!core) {@@ -2630,6 +2691,7 @@ static void draw_splash(int patch_num, int top, int bottom, uint8_t *dest, int s}}+#endifextern "C" int M_Random();#if !NUMWORKS@@ -3141,44 +3203,6 @@ void pd_end_frame(int wipe_start) {#else // NUMWORKS-// ----------------------------------------------------------------------------------------------------------------// The calculator: the frame drawn a band at a time--// regular columns keep their x (its low 8 bits; the links say if it is 256 or more)-static void nw_freeze_columns() {- for (int x = 0; x < SCREENWIDTH; x++) {- for (int16_t i = column_heads[x]; i >= 0; i = render_cols[i].next) {- auto &c = render_cols[i];- if (c.texturemid != TEXTUREMID_PLANE) {- c.x = (uint8_t)x;- }- }- }-}--// floors and ceilings: their visplane numbers on the band's pixels, with a bit set for each-static void nw_mark_planes(int y0, int y1) {- memset(visplane_bit, 0, (SCREENWIDTH / 8) * (y1 - y0));- for (int x = 0; x < SCREENWIDTH; x++) {- uint8_t *screen_col = render_frame_buffer + x;- uint8_t *vb_col = visplane_bit + x / 8;- uint8_t bit = 1u << (x & 7u);- for (int16_t i = column_heads[x]; i >= 0; i = render_cols[i].next) {- const auto &c = render_cols[i];- if (c.texturemid != TEXTUREMID_PLANE || c.yh < y0 || c.yl >= y1) continue;- int yl = c.yl < y0 ? y0 : c.yl, yh = c.yh >= y1 ? y1 - 1 : c.yh;- uint8_t *p = screen_col + yl * SCREENWIDTH;- uint8_t *vp = vb_col + (yl - y0) * (SCREENWIDTH / 8);- for (int y = yl; y <= yh; y++) {- *p = c.plane;- p += SCREENWIDTH;- *vp |= bit;- vp += SCREENWIDTH / 8;- }- }- }-}-// draws the runs gathered so far, a flat at a time (each flat's visplanes are linked again first, as// flush_visplanes takes them off as it goes)static void nw_flush_planes(int numvisplanes) {@@ -3194,311 +3218,7 @@ static void nw_flush_planes(int numvisplanes) {nw_flat_run_count = 0;}-// the marked pixels of the band as runs of the same visplane, row by row, then drawn-static void nw_draw_planes(int y0, int y1) {- if (!lastvisplane) return;- int numvisplanes = lastvisplane - visplanes;- memset(visplane_heads, -1, numvisplanes * sizeof(visplane_heads[0]));- nw_flat_run_count = 0;- for (int y = y0; y < y1; y++) {- uint8_t *vp = &visplane_bit[(y - y0) * (SCREENWIDTH / 8)];- uint8_t *vp_end = vp + SCREENWIDTH / 8;- uint bit = 0;- uint8_t *p0 = &render_frame_buffer[y * SCREENWIDTH];- uint8_t *p = p0;- while (vp != vp_end) {- if (!*vp) {- vp++;- p += 8 - bit;- bit = 0;- continue;- }- while (!(*vp & (1u << bit))) bit++, p++;- uint x_start = p - p0;- uint8_t plane_num = *p;- assert(plane_num < numvisplanes);- while (bit < 8 && *p == plane_num && *vp & (1u << bit)) {- p++;- bit++;- }- if (bit == 8) {- vp++;- bit = 0;- if (vp < vp_end && *vp == 255 && *p == plane_num) {- uint fourx = plane_num * 0x1010101;- while (vp < vp_end && *vp == 255 && ((uint32_t *) p)[0] == fourx && ((uint32_t *) p)[1] == fourx) {- p += 8;- vp++;- }- }- if (vp < vp_end) {- while (bit < 8 && *p == plane_num && *vp & (1u << bit)) {- p++;- bit++;- }- *vp &= ~((1u << bit) - 1);- }- } else {- *vp &= ~((1u << bit) - 1);- }- if (nw_flat_run_count == NW_FLAT_RUNS) nw_flush_planes(numvisplanes);- int16_t fr_pos = nw_flat_run_count++;- auto &fr = flat_runs[fr_pos];- fr.x_start = x_start;- fr.x_end = p - p0;- fr.y = y;- fr.next = visplane_heads[plane_num];- visplane_heads[plane_num] = fr_pos;- }- }- if (nw_flat_run_count) nw_flush_planes(numvisplanes);-}--// walls and things in the band, by framedrawable: each texture or patch decoded once for the band-static void __noinline nw_draw_columns(int y0, int y1) {- memset(fd_heads, -1, sizeof(fd_heads));- for (int x = 0; x < SCREENWIDTH; x++) {- for (int16_t i = column_heads[x]; i >= 0; i = render_cols[i].next) {- const auto &c = render_cols[i];- if (c.texturemid == TEXTUREMID_PLANE || c.yh < y0 || c.yl >= y1) continue;- fd_next[i] = fd_heads[c.fd_num];- fd_heads[c.fd_num] = (x >> 8) ? (int16_t) (i | 0x8000) : i;- }- }- for (int fd_num = 0; fd_num < num_framedrawables; fd_num++) {- int i = fd_heads[fd_num];- if (i == -1) continue;- int id = framedrawables[fd_num].real_id;- if (id > 0) {- draw_composite_columns(id, i);- } else if (id < 0) {- int16_t heads[WHD_PATCH_MAX_WIDTH];- uint8_t heights[WHD_PATCH_MAX_WIDTH];- draw_patch_columns(-id, i, heads, heights, 0);- }- }-}--// the shimmer of things that are invisible: the pixels around them, darker (a neighbour outside the band-// is the pixel itself)-static void nw_draw_fuzz(int y0, int y1) {- const lighttable_t *darken_map = xcolormaps + 256 * 6;- for (int x = 0; x < SCREENWIDTH; x++) {- for (int16_t i = fuzzy_column_heads[x]; i >= 0; i = render_cols[i].next) {- const auto &c = render_cols[i];- int yl = c.yl, yh = c.yh;- if (yl == 0) yl = 1;- if (yh >= MAIN_VIEWHEIGHT - 1) yh = MAIN_VIEWHEIGHT - 2;- if (yl < y0) yl = y0;- if (yh >= y1) yh = y1 - 1;- uint8_t *p = render_frame_buffer + x + yl * SCREENWIDTH;- for (int y = yl; y <= yh; y++, p += SCREENWIDTH) {- int off = fuzzoffset[fuzzpos];- if ((off < 0 && y == y0) || (off > 0 && y == y1 - 1)) off = 0;- *p = darken_map[p[off]];- if (++fuzzpos == FUZZTABLE) fuzzpos = 0;- }- }- }-}--// columns left without a place to go (more than RENDER_COL_MAX): black-static void nw_clear_uncovered(int y0, int y1) {- if (not_fully_covered_yl > y0) y0 = not_fully_covered_yl;- if (not_fully_covered_yh + 1 < y1) y1 = not_fully_covered_yh + 1;- for (uint i = 0; i < count_of(not_fully_covered_cols); i++) {- if (!not_fully_covered_cols[i]) continue;- for (int j = 0; j < 32; j++) {- if (!(not_fully_covered_cols[i] & (1u << j))) continue;- int x = (i * 32 + j) * 4;- if (x >= SCREENWIDTH) break;- for (int y = y0; y < y1; y++) memset(render_frame_buffer + y * SCREENWIDTH + x, 0, 4);- }- }-}--// a flat tiled over the band (behind the ending's text, and darker behind the keys page)-static void nw_tile_flat(const char *name, int y0, int y1, int light = 0) {- int flatnum = W_GetNumForName(name) - firstflat;- uint8_t *flat_data = nullptr;- for (int slot = 0; slot < cached_flat_slots; slot++) {- if (cached_flat_picnum[slot] == flatnum) flat_data = cached_flat0 - slot * 4096;- }- if (!flat_data) flat_data = decode_flat_to_slot(0, flatnum);- uint8_t *dest = nw_band;- for (int y = y0; y < y1; y++) {- const uint8_t *src = flat_data + (y & 63);- for (int i = 0; i < SCREENWIDTH; i++) *dest++ = src[(i & 63) * 64];- }- if (light) {- const lighttable_t *map = xcolormaps + 256 * light;- for (uint8_t *p = nw_band; p < dest; p++) *p = map[*p];- }-}--// a full screen picture behind everything when there is no level to see-static void nw_draw_background(int y0, int y1) {- int patch_num = 0;- if (inhelpscreens) {- switch (inhelpscreens & 127) {- case 1: patch_num = W_GetNumForName("HELP1"); break;- case 2: patch_num = W_GetNumForName("HELP2"); break;- case 3: patch_num = W_GetNumForName("HELP"); break;- case 4: nw_tile_flat("FLOOR4_8", y0, y1, 22); return;- }- } else {- switch (gamestate) {- case GS_INTERMISSION:- patch_num = wi_background_patch_num;- break;- case GS_DEMOSCREEN:- patch_num = W_GetNumForName(pagename);- break;- case GS_FINALE:- if (finalestage == F_STAGE_TEXT) {- nw_tile_flat(finaleflat, y0, y1); // (the tiled flat behind the text)- return;- } else if (finalestage == F_STAGE_ARTSCREEN) {- const char *ln = F_ArtScreenLumpName();- if (ln) patch_num = W_GetNumForName(ln);- }- break;- default:- break;- }- }- if (patch_num) {- draw_splash(patch_num, y0, y1, nw_band);- } else {- memset(nw_band, 0, (y1 - y0) * SCREENWIDTH);- }-}--// the status bar, messages, the menu... over the picture-static vpatchlist_t nw_patches[400];--// flats are cached in what the columns leave of list_buffer-static void nw_columns_ready(bool view) {- uint8_t *list_buffer_limit = list_buffer + count_of(list_buffer);- if (cached_flat0 != list_buffer_limit - 4096) {- cached_flat0 = list_buffer_limit - 4096;- memset(cached_flat_picnum, -1, sizeof(cached_flat_picnum));- }- last_list_buffer_limit = list_buffer_limit;- int new_cache_flat_slots = 1 + ((int)(cached_flat0 - list_buffer - render_col_count * sizeof(pd_column))) / 4096;- if (new_cache_flat_slots < 1) new_cache_flat_slots = 1;- for (int i = cached_flat_slots; i < new_cache_flat_slots; i++) cached_flat_picnum[i] = 0xff;- cached_flat_slots = new_cache_flat_slots;- if (view) {- nw_col_frames[render_col_count / 250 < 40 ? render_col_count / 250 : 39]++;- nw_freeze_columns();- }-}--// the frame, band by band, for the columns x0 to x1 of the screen-static void nw_draw_bands(uint8_t *band, bool view, bool showing_help, int x0, int x1) {- for (int y0 = 0; y0 < SCREENHEIGHT; y0 += NW_BAND_H) {- int y1 = y0 + NW_BAND_H;- nw_band_begin(band, y0, y1);- render_frame_buffer = nw_band - y0 * SCREENWIDTH;- I_VideoBuffer = render_frame_buffer;- if (view) {- if (y0 < MAIN_VIEWHEIGHT) {- nw_view_y0 = y0;- nw_view_y1 = y1 < MAIN_VIEWHEIGHT ? y1 : MAIN_VIEWHEIGHT;- if (automapactive) {- nw_direct = true;- AM_Drawer();- nw_direct = false;- } else {- nw_mark_planes(nw_view_y0, nw_view_y1);- nw_draw_planes(nw_view_y0, nw_view_y1);- nw_draw_columns(nw_view_y0, nw_view_y1);- nw_draw_fuzz(nw_view_y0, nw_view_y1);- nw_clear_uncovered(nw_view_y0, nw_view_y1);- }- }- if (y1 > MAIN_VIEWHEIGHT) {- int from = y0 > MAIN_VIEWHEIGHT ? y0 : MAIN_VIEWHEIGHT;- memset(render_frame_buffer + from * SCREENWIDTH, 0, (y1 - from) * SCREENWIDTH);- }- } else {- nw_draw_background(y0, y1);- if (gamestate == GS_FINALE && finalestage == F_STAGE_TEXT && !showing_help) {- nw_direct = true;- F_TextWrite();- nw_direct = false;- } else if ((inhelpscreens & 127) == 4) {- nw_direct = true;- M_DrawKeys();- nw_direct = false;- }- }- V_DrawPatchListBand(nw_patches);- nw_band_end(x0, x1);- }-}--// a frame with more pieces than list_buffer has room for is drawn again in strips, each with the view-// rendered for its columns only, halved again as long as needed (vanilla has no such limit, and neither do we)-extern "C" { int nw_strip_x0 = 0, nw_strip_x1 = SCREENWIDTH; uint32_t nw_strips; }-static void nw_draw_strip(uint8_t *band, int x0, int x1) {- pd_begin_frame();- nw_strip_x0 = x0;- nw_strip_x1 = x1;- R_RenderPlayerView(&players[displayplayer]);- nw_strip_x0 = 0;- nw_strip_x1 = SCREENWIDTH;- if (nw_overflowed && x1 - x0 > 8) {- int mid = (x0 + x1) / 2;- nw_draw_strip(band, x0, mid);- nw_draw_strip(band, mid, x1);- return;- }- nw_strips++;- reclip_fuzz_columns();- nw_columns_ready(true);- nw_draw_bands(band, true, false, x0, x1);-}--void pd_end_frame(int wipe_start) {- // these were only clipped as they were inserted (so may be more obscured)- reclip_fuzz_columns();- bool showing_help = inhelpscreens;- bool view = gamestate == GS_LEVEL && !showing_help;- int16_t fd_next_here[RENDER_COL_MAX];- fd_next = fd_next_here;- bool strips = view && !automapactive && nw_overflowed;- if (!strips) nw_columns_ready(view);-- // what is drawn over the picture, gathered once- nw_patches[0].header.max = count_of(nw_patches) - 1;- V_BeginPatchList(nw_patches);- if (!showing_help) {- if (gamestate == GS_LEVEL) ST_Drawer(false, !pre_wipe_state);- else if (gamestate == GS_INTERMISSION) WI_Drawer();- } else if (gamestate == GS_LEVEL) {- ST_doPaletteStuff();- }- ST_FpsDrawer(-1);- if (!pre_wipe_state && gamestate == GS_LEVEL && gametic && !inhelpscreens) HU_Drawer();- if (paused) {- int y = automapactive ? 4 : viewwindowy + 4;- V_DrawPatchDirect(viewwindowx + (scaledviewwidth - 68) / 2, y, VPATCH_HANDLE(VPATCH_NAME(M_PAUSE)));- }- M_Drawer();- if (pre_wipe_state == PRE_WIPE_EXTRA_FRAME_NEEDED) pre_wipe_state = PRE_WIPE_EXTRA_FRAME_DONE;-- nw_frame_begin();- uint8_t __aligned(8) band[SCREENWIDTH * NW_BAND_H];- if (strips) {- nw_draw_strip(band, 0, SCREENWIDTH / 2);- nw_draw_strip(band, SCREENWIDTH / 2, SCREENWIDTH);- } else {- nw_draw_bands(band, view, showing_help, 0, SCREENWIDTH);- }- nw_frame_end();-}+#include "nw/nw_back.inc"#endif // NUMWORKSdiff --git a/games/doom/src/tables.h b/games/doom/src/tables.hindex 5d80afb3..7af687fa 100644--- a/games/doom/src/tables.h+++ b/games/doom/src/tables.h@@ -73,7 +73,14 @@ static inline fixed_t finesine(int x) {// Effective size is 4096.extern const fixed_t _finetangent[FINEANGLES/2];+#if NUMWORKS+// (vanilla's finesine comes right after finetangent: a wall seen exactly side on reads its first entry)+static inline fixed_t finetangent(int x) {+ return x < FINEANGLES / 2 ? _finetangent[x] / (1 << (16 - FRACBITS)) : finesine(x - FINEANGLES / 2);+}+#else#define finetangent(x) (_finetangent[x] / (1 << (16 - FRACBITS)))+#endif// Gamma correction tables.#if !DOOM_TINYdiff --git a/games/doom/src/v_video.c b/games/doom/src/v_video.cindex d3304dd3..12984042 100644--- a/games/doom/src/v_video.c+++ b/games/doom/src/v_video.c@@ -390,11 +390,11 @@ void V_DrawPatchList(const vpatchlist_t *patchlist) {#pragma GCC pop_options#if NUMWORKS-// The calculator draws the screen a band of rows at a time (nw/i_video.c): patches are drawn into the band,-// clipped to its rows, from a list gathered once per frame (V_DrawPatchListBand) or as they come (nw_direct,-// for what draws straight to the screen: the automap's marks, the finale's text).+// The calculator draws the screen a band of NW_BAND_W columns at a time (nw/nw_render.h): patches are drawn+// into the band, clipped to its columns, from a list gathered once per frame (V_DrawPatchListBand) or as they+// come (nw_direct, for what draws straight to the screen: the automap's marks, the finale's text).+#include "nw/nw_render.h"uint8_t *nw_band;-int nw_band_y0, nw_band_y1;boolean nw_direct;static void nw_shared_palettes(void) {@@ -405,23 +405,24 @@ static void nw_shared_palettes(void) {}}-// one patch at (x, y), its rows in the band drawn and the others skipped-static void nw_vpatch(const patch_t *patch, int x, int y, int repeat, int handle) {+// one patch at (x, y), its pixels in columns c0 to c1 (and in the band) drawn+static void nw_vpatch_cols(const patch_t *patch, int x, int y, int c0, int c1) {int w = vpatch_width(patch), h = vpatch_height(patch);- int y0 = nw_band_y0, y1 = nw_band_y1;- if (y >= y1 || y + h <= y0) return;+ if (c0 < nw_band_x0) c0 = nw_band_x0;+ if (c1 > nw_band_x1) c1 = nw_band_x1;+ if (c0 < 0) c0 = 0;+ if (c1 > SCREENWIDTH) c1 = SCREENWIDTH;+ if (x >= c1 || x + w <= c0 || y >= SCREENHEIGHT) return;nw_shared_palettes();const uint8_t *pal = vpatch_has_shared_palette(patch) ? shared_palette8[vpatch_shared_palette(patch)]: vpatch_palette(patch);const uint8_t *data = vpatch_data(patch);- int rw = handle == VPATCH_M_THERMM && repeat ? w - 1 : w; // (the thermometer's middle repeats one pixel narrower)- // rows wholly on the screen need no check of each pixel- boolean safe = x >= 0 && x + w + repeat * rw <= SCREENWIDTH;-#define PUT(px, c) do { int px_ = (px); if (safe || (unsigned)px_ < SCREENWIDTH) row[px_] = (c); } while (0)+ unsigned cw = (unsigned)(c1 - c0);+#define PUT(px, c) do { int px_ = (px); if ((unsigned)(px_ - c0) < cw) row[px_] = (c); } while (0)for (int r = 0; r < h; r++) {int sy = y + r;- if (sy >= y1) break;- uint8_t *row = sy >= y0 ? nw_band + (sy - y0) * SCREENWIDTH : NULL;+ if (sy >= SCREENHEIGHT) break;+ uint8_t *row = sy >= 0 ? nw_band - nw_band_x0 + sy * NW_BAND_W : NULL;switch (vpatch_type(patch)) {case vp4_runs: {int p = 0;@@ -530,16 +531,18 @@ static void nw_vpatch(const patch_t *patch, int x, int y, int repeat, int handledefault:return;}- if (row && repeat) {- for (int i = 0; i < repeat * rw; i++) {- int from = x + i, to = x + rw + i;- if ((unsigned)from < SCREENWIDTH && (unsigned)to < SCREENWIDTH) row[to] = row[from];- }- }}#undef PUT}+// one patch at (x, y), and repeat times again to its right+static void nw_vpatch(const patch_t *patch, int x, int y, int repeat, int handle) {+ int w = vpatch_width(patch);+ int rw = handle == VPATCH_M_THERMM && repeat ? w - 1 : w; // (the thermometer's middle repeats one pixel narrower)+ for (int k = 0; k <= repeat; k++)+ nw_vpatch_cols(patch, x + k * rw, y, x + k * rw, k < repeat ? x + (k + 1) * rw : x + k * rw + w);+}+void V_DrawPatchListBand(const vpatchlist_t *patchlist) {for (int l = 1; l < patchlist[0].header.size; l++) {const patch_t *patch = resolve_vpatch_handle(patchlist[l].entry.patch_handle);diff --git a/games/doom/src/v_video.h b/games/doom/src/v_video.hindex 77b19336..d8dc2069 100644--- a/games/doom/src/v_video.h+++ b/games/doom/src/v_video.h@@ -86,9 +86,8 @@ void V_EndPatchList(void);void V_DrawPatchList(const vpatchlist_t *patchlist);extern uint8_t vpatch_clip_top, vpatch_clip_bottom;#if NUMWORKS-// the band of rows being drawn (nw/i_video.c), and patches drawn into it as they come+// the band being drawn (nw/nw_render.h), and patches drawn into it as they comeextern uint8_t *nw_band;-extern int nw_band_y0, nw_band_y1;extern boolean nw_direct;void V_DrawPatchListBand(const vpatchlist_t *patchlist);#endifdiff --git a/games/doom/tests/play.c b/games/doom/tests/play.cindex 44424362..5b04e94f 100644--- a/games/doom/tests/play.c+++ b/games/doom/tests/play.c@@ -1,7 +1,7 @@/* Plays Doom on a computer, with scripted keys, and saves screenshots.** play [--ms N] [--keys "1000-1500:up,2000:ok,..."] [--shots 3000,6000] [--every MS] [--out DIR]- * [--saves DIR] [--stats]+ * [--saves DIR] [--stats] [--warp EM] [--scan]** Keys are the calculator's (tools/emu.py's names: left up down right ok back home onoff shift alpha xnt* var toolbox backspace sqrt 0-9 plus minus mul div exe). "a-b:key" holds it from a ms to b ms, "a:key"@@ -105,7 +105,9 @@ void host_demo_end(void) {fflush(stdout);}+static void scan_tic(void);void host_tic(void) {+ scan_tic();if (!trace_file || gamestate != GS_LEVEL || !players[consoleplayer].mo) return;player_t *p = &players[consoleplayer];mobj_t *mo = p->mo;@@ -129,8 +131,83 @@ static void dump_frame(void) {}}+/* --scan: in the level --warp starts, the view from all over the map, a frame from the middle of each+ * subsector looking 8 ways round (god mode on), for the most the renderer is asked to hold and draw; the+ * heaviest views are printed (SCAN lines) */+static int scan_on, scan_ss = -1, scan_dir, scan_frames;+static struct { uint32_t load; fixed_t x, y; unsigned angle; } scan_top[8];+extern uint32_t nw_last_walls, nw_last_planes;+static fixed_t scan_x, scan_y;++static void scan_report(void) {+ extern uint32_t nw_col_peak, nw_col_overflows, nw_strips, nw_peak[4];+ printf("SCAN E%dM%d %d frames: walls %u (overflows %u, strips %u) plane records %u drawsegs %u openings %u "+ "vissprites %u\n", gameepisode, gamemap, scan_frames, nw_col_peak, nw_col_overflows, nw_strips, nw_peak[0],+ nw_peak[1], nw_peak[2], nw_peak[3]);+ for (int k = 0; k < 8 && scan_top[k].load; k++)+ printf("SCAN load %u at %d %d angle %u\n", scan_top[k].load, scan_top[k].x >> FRACBITS,+ scan_top[k].y >> FRACBITS, scan_top[k].angle);+ fflush(stdout);+}++static int scan_middle(int i, fixed_t *x, fixed_t *y) {+ subsector_t *sub = &subsectors[i];+ seg_t *line = subsector_firstline(sub), *limit = subsector_linelimit(sub);+ int64_t sx = 0, sy = 0;+ int n = 0;+ for (; line < limit; line += seg_next_step(line), n++) {+ sx += vertex_x(seg_v1(line));+ sy += vertex_y(seg_v1(line));+ }+ if (!n) return 0;+ *x = (fixed_t)(sx / n);+ *y = (fixed_t)(sy / n);+ sector_t *s = subsector_sector(sub);+ return R_PointInSubsector(*x, *y) == sub && sector_ceilingheight(s) - sector_floorheight(s) >= 56 * FRACUNIT;+}++static void scan_tic(void) {+ player_t *p = &players[consoleplayer];+ mobj_t *mo = p->mo;+ if (!scan_on || gamestate != GS_LEVEL || !mo) return;+ p->cheats |= CF_GODMODE;+ if (scan_ss < 0 || ++scan_dir == 8) {+ scan_dir = 0;+ do {+ if (++scan_ss >= (int)numsubsectors) {+ scan_report();+ exit(0);+ }+ } while (!scan_middle(scan_ss, &scan_x, &scan_y));+ }+ P_UnsetThingPosition(mo);+ mo->xy.x = scan_x;+ mo->xy.y = scan_y;+ P_SetThingPosition(mo);+ mo->z = sector_floorheight(mobj_sector(mo));+ p->viewz = mo->z + VIEWHEIGHT;+ mobj_full(mo)->angle = (angle_t)scan_dir * ANG45;+}++static void scan_frame(void) {+ if (!scan_on || scan_ss < 0) return;+ scan_frames++;+ uint32_t load = nw_last_walls * 4 + nw_last_planes; /* (bytes of records, by 4) */+ int k = 7;+ if (load <= scan_top[k].load) return;+ while (k > 0 && load > scan_top[k - 1].load) {+ scan_top[k] = scan_top[k - 1];+ k--;+ }+ scan_top[k].load = load;+ scan_top[k].x = scan_x;+ scan_top[k].y = scan_y;+ scan_top[k].angle = (unsigned)scan_dir * 45;+}+/* each frame drawn: 1/35 s goes by, and screenshots are taken */void host_frame(void) {+ scan_frame();dump_frame();while (next_shot < nshots && host_time >= shots[next_shot]) shot(shots[next_shot++]);if (every && host_time >= next_every) {@@ -160,6 +237,7 @@ int main(int argc, char **argv) {else if (!strcmp(argv[i], "--saves") && i + 1 < argc) saves = argv[++i];else if (!strcmp(argv[i], "--stats")) stats = 1;else if (!strcmp(argv[i], "--warp") && i + 1 < argc) nw_warp = atoi(argv[++i]);+ else if (!strcmp(argv[i], "--scan")) scan_on = 1, end_ms = 0xfffffff;else if (!strcmp(argv[i], "--trace") && i + 1 < argc) trace_file = fopen(argv[++i], "w");else if (!strcmp(argv[i], "--dump-tics") && i + 1 < argc) {for (char *s = argv[++i]; *s && ndump < 256;) {@@ -190,6 +268,10 @@ int main(int argc, char **argv) {nw_zone_fixed_peak, nw_strips);for (int i = 0; i < 40; i++) printf(" %u", nw_col_frames[i]);printf("\n");+ extern uint32_t nw_max_walls, nw_max_planes, nw_max_masked, nw_peak[4], nw_low_mismatch;+ printf("LOWMISMATCH %u\n", nw_low_mismatch);+ printf("MAXES walls %u planes %u masked %u plane records %u drawsegs %u openings %u vissprites %u\n",+ nw_max_walls, nw_max_planes, nw_max_masked, nw_peak[0], nw_peak[1], nw_peak[2], nw_peak[3]);}printf("END time %u quit %d state %d map E%dM%d leveltime %d health %d kills %d\n", host_time, (int)nw_quit,(int)gamestate, gameepisode, gamemap, leveltime, players[consoleplayer].health,diff --git a/games/doom/tools/whd_gen/whd_gen.cpp b/games/doom/tools/whd_gen/whd_gen.cppindex 7269f246..db13b200 100644--- a/games/doom/tools/whd_gen/whd_gen.cpp+++ b/games/doom/tools/whd_gen/whd_gen.cpp@@ -3949,6 +3949,8 @@ texture_index convert_textures(wad &wad) {std::vector<std::vector<int>> column_pixels(mtexture.width);std::vector<int> pixel_patch(mtexture.width * mtexture.height);std::fill(pixel_patch.begin(), pixel_patch.end(), -1);+ std::vector<int> pixel_src(mtexture.width * mtexture.height); // (the patch's row each pixel is from)+ std::vector<int> patch_widths;for (int j = 0; j < mtexture.patchcount; j++) {auto mpatch = get_field_inc<mappatch_t>(tex_lump->data, offset);@@ -3986,12 +3988,19 @@ texture_index convert_textures(wad &wad) {if (y1 < 0) y1 = 0;if (y1 > tex_whd.height) y1 = tex_whd.height;while (post[0] != 0xff) {- int yp = post[0] + mpatch.originy;- for (int k = 3; k < 3 + post[1]; k++, yp++) {+ // as vanilla's R_DrawColumnInCache: a post that starts above the texture is cut short at+ // the top, but its pixels are still taken from its start+ int position = post[0] + mpatch.originy, count = post[1];+ if (position < 0) {+ count += position;+ position = 0;+ }+ int yp = position;+ for (int k = 3; k < 3 + count; k++, yp++) {if (yp >= tex_whd.height) break;- if (yp < 0) continue;pixel_usage[yp * tex_whd.width + xp]++;pixel_patch[yp * tex_whd.width + xp] = j;+ pixel_src[yp * tex_whd.width + xp] = post[0] + k - 3;column_pixels[xp][yp] = post[k];touched_pixels++;@@ -4008,6 +4017,7 @@ texture_index convert_textures(wad &wad) {}if (patch_solid) solid_patches++;#endif+ patch_widths.push_back(ph->width);printf(" Patch %d (%d): %d at %d,%d %dx%d solid = %d\n", j, pname_lookup[mpatch.patch], mpatch.patch,mpatch.originx, mpatch.originy, ph->width, ph->height, patch_solid);} else {@@ -4064,10 +4074,12 @@ texture_index convert_textures(wad &wad) {int last = -1;int run = 0;int localp;+ int run_src = 0;bool had_col_transparent = false;for (int y = 0; y < tex_whd.height; y++) {int p = pixel_patch[y * tex_whd.width + x];- if (p != last) {+ // (a run goes on while the pixels are from consecutive rows of the same patch)+ if (p != last || (p != -1 && pixel_src[y * tex_whd.width + x] != run_src + run)) {if (run) {if (localp == 0xff) {had_col_transparent = true;@@ -4076,9 +4088,10 @@ texture_index convert_textures(wad &wad) {if (run > 128) fail("run is too long %d\n", run);col_patch_runs.push_back(run - 1);col_patch_runs.push_back((uint8_t) mpatches[last].originx);- col_patch_runs.push_back((int8_t) (y-run)-mpatches[last].originy);+ col_patch_runs.push_back((uint8_t) run_src);}}+ run_src = p != -1 ? pixel_src[y * tex_whd.width + x] : 0;last = p;if (p != -1) {auto f = std::find(local_patches.begin(), local_patches.end(), pname_lookup[mpatches[p].patch]);@@ -4107,7 +4120,7 @@ texture_index convert_textures(wad &wad) {if (run > 128) fail("run is too long %d\n", run);col_patch_runs.push_back(128 | (run - 1));col_patch_runs.push_back((uint8_t) mpatches[last].originx);- col_patch_runs.push_back((int8_t) (tex_whd.height-run)-mpatches[last].originy);+ col_patch_runs.push_back((uint8_t) run_src);}}if (had_col_transparent) {@@ -4115,6 +4128,35 @@ texture_index convert_textures(wad &wad) {printf("WARNING: Can't mix transparency with anything but single patch column, will be undefined. tex=%s col %d\n", name.c_str(), x);}}+ // vanilla's R_GenerateLookup: a column that one patch goes over (whatever its rows) is that patch's+ // column as it is; one that more go over is made from them (even if one alone shows)+ int vcount = 0, vlast = -1;+ for (int j = 0; j < (int) mpatches.size(); j++) {+ if (x >= mpatches[j].originx && x < mpatches[j].originx + patch_widths[j]) vcount++, vlast = j;+ }+ if (vcount == 1) {+ int pn = pname_lookup[mpatches[vlast].patch];+ auto f = std::find(local_patches.begin(), local_patches.end(), pn);+ int lp = f - local_patches.begin();+ if (f == local_patches.end()) local_patches.push_back(pn);+ unique_col_patches.insert(lp);+ col_patch_runs = {(uint8_t) lp, (uint8_t) (0x80 | ((tex_whd.height - 1) & 0x7f)),+ (uint8_t) mpatches[vlast].originx, 0};+ } else if (vcount > 1 && col_patch_runs.size() == 4) {+ int len = (col_patch_runs[1] & 0x7f) + 1;+ if (len > 1) {+ // (two runs, or it would be taken for a single patch's column)+ std::vector<uint8_t> two = col_patch_runs;+ two[1] = (uint8_t) (len / 2 - 1);+ two.push_back(col_patch_runs[0]);+ two.push_back((uint8_t) (0x80 | (len - len / 2 - 1)));+ two.push_back(col_patch_runs[2]);+ two.push_back((uint8_t) (col_patch_runs[3] + len / 2));+ col_patch_runs = two;+ } else {+ printf("WARNING: composite column of one pixel %s col %d\n", name.c_str(), x);+ }+ }if (col_patch_runs.size() == 4) {col_patch_runs[3] = 0; // force originy to 0 for single patch column to match DOOM R_GenerateLookup behavior} else if (!had_transparent) {