uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit 3d49536d7629da9fc49dee095003b504af18faf9
parent 6821bea9c0a0f6f356d2b406de55c4968445b9c3
Author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
Date:   Tue, 21 Sep 2021 19:39:00 +0200

ppu: keep track of the vertical region where redraw is supposed to happen

Diffstat:
Msrc/devices/ppu.c | 38+++++++++++++++++++++++---------------
Msrc/devices/ppu.h | 8+++++---
Msrc/uxnemu.c | 22+++++++++++++++-------
3 files changed, 43 insertions(+), 25 deletions(-)

diff --git a/src/devices/ppu.c b/src/devices/ppu.c @@ -19,6 +19,14 @@ static Uint8 blending[5][16] = { {2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2}, {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0}}; +void +ppu_frame(Ppu *p) +{ + p->redraw = 0; + p->i0 = p->width / PPW + p->height * p->stride; + p->i1 = 0; +} + static void ppu_clear(Ppu *p) { @@ -27,55 +35,55 @@ ppu_clear(Ppu *p) p->dat[i] = 0; } -unsigned int +void ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color) { - unsigned int ret, i = x / PPW + y * p->stride, shift = x % PPW * 4; + unsigned int v, i = x / PPW + y * p->stride, shift = x % PPW * 4; if(x >= p->width || y >= p->height) - return 0; - ret = p->dat[i]; + return; + v = p->dat[i]; if(fg) shift += 2; p->dat[i] &= ~(3 << shift); p->dat[i] |= color << shift; - return ret ^ p->dat[i]; + if((v ^ p->dat[i]) != 0){ + p->redraw = 1; + p->i0 = p->i0 < i ? p->i0 : i; + p->i1 = p->i1 > i ? p->i1 : i; + } } -unsigned int +void ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) { Uint16 v, h; - int ret = 0; for(v = 0; v < 8; v++) for(h = 0; h < 8; h++) { Uint8 ch1 = (sprite[v] >> (7 - h)) & 0x1; if(ch1 || blending[4][color]) - ret |= ppu_pixel(p, + ppu_pixel(p, fg, x + (flipx ? 7 - h : h), y + (flipy ? 7 - v : v), blending[ch1][color]); } - return ret; } -unsigned int +void ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) { Uint16 v, h; - int ret = 0; for(v = 0; v < 8; v++) for(h = 0; h < 8; h++) { Uint8 ch1 = ((sprite[v] >> (7 - h)) & 0x1); Uint8 ch2 = ((sprite[v + 8] >> (7 - h)) & 0x1); Uint8 ch = ch1 + ch2 * 2; if(ch || blending[4][color]) - ret |= ppu_pixel(p, + ppu_pixel(p, fg, x + (flipx ? 7 - h : h), y + (flipy ? 7 - v : v), blending[ch][color]); } - return ret; } /* output */ @@ -89,5 +97,6 @@ ppu_set_size(Ppu *p, Uint16 width, Uint16 height) p->dat = realloc(p->dat, p->stride * p->height * sizeof(unsigned int)); if(p->dat == NULL) return 0; ppu_clear(p); + ppu_frame(p); return 1; -} -\ No newline at end of file +} diff --git a/src/devices/ppu.h b/src/devices/ppu.h @@ -23,10 +23,12 @@ typedef unsigned int Uint32; typedef struct Ppu { Uint16 width, height; + int i0, i1, redraw; unsigned int *dat, stride; } Ppu; +void ppu_frame(Ppu *p); int ppu_set_size(Ppu *p, Uint16 width, Uint16 height); -unsigned int ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color); -unsigned int ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); -unsigned int ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); +void ppu_pixel(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color); +void ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); +void ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); diff --git a/src/uxnemu.c b/src/uxnemu.c @@ -122,17 +122,25 @@ get_pixel(int x, int y) static void redraw(Uxn *u) { - Uint16 x, y; + Uint16 x, y, y0 = 0, y1 = ppu.height; + SDL_Rect up = gRect; if(devsystem->dat[0xe]) inspect(&ppu, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram.dat); - for(y = 0; y < ppu.height; ++y) + if(ppu.redraw) { + y0 = ppu.i0 / ppu.stride; + y1 = ppu.i1 / ppu.stride + 1; + up.y += y0; + up.h = y1 - y0; + } + for(y = y0; y < y1; ++y) for(x = 0; x < ppu.width; ++x) ppu_screen[x + y * ppu.width] = palette[get_pixel(x, y)]; - SDL_UpdateTexture(gTexture, &gRect, ppu_screen, ppu.width * sizeof(Uint32)); + SDL_UpdateTexture(gTexture, &up, ppu_screen + y0 * ppu.width, ppu.width * sizeof(Uint32)); SDL_RenderClear(gRenderer); SDL_RenderCopy(gRenderer, gTexture, NULL, NULL); SDL_RenderPresent(gRenderer); reqdraw = 0; + ppu_frame(&ppu); } static void @@ -353,7 +361,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w) Uint16 x = peek16(d->dat, 0x8); Uint16 y = peek16(d->dat, 0xa); Uint8 layer = d->dat[0xe] & 0x40; - reqdraw |= ppu_pixel(&ppu, layer, x, y, d->dat[0xe] & 0x3); + ppu_pixel(&ppu, layer, x, y, d->dat[0xe] & 0x3); if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 1); /* auto x+1 */ if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */ break; @@ -364,10 +372,10 @@ screen_talk(Device *d, Uint8 b0, Uint8 w) Uint8 layer = d->dat[0xf] & 0x40; Uint8 *addr = &d->mem[peek16(d->dat, 0xc)]; if(d->dat[0xf] & 0x80) { - reqdraw |= ppu_2bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20); + ppu_2bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20); if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 16); /* auto addr+16 */ } else { - reqdraw |= ppu_1bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20); + ppu_1bpp(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20); if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 8); /* auto addr+8 */ } if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 8); /* auto x+8 */ @@ -529,7 +537,7 @@ run(Uxn *u) } } uxn_eval(u, peek16(devscreen->dat, 0)); - if(reqdraw || devsystem->dat[0xe]) + if(reqdraw || ppu.redraw || devsystem->dat[0xe]) redraw(u); if(!BENCH) { elapsed = (SDL_GetPerformanceCounter() - start) / (double)SDL_GetPerformanceFrequency() * 1000.0f;