X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=plugins%2Fgpulib%2Fgpu.c;h=15810b82d7cf2ac1c5dad87af6d172f7c9ae3027;hb=abf094858889145af1fde9840429dfb8a0e70a39;hp=931583f3930976c361e29252b71c1af05daab9ef;hpb=308c6e678a2f0a56a9dee35307070550354f580c;p=pcsx_rearmed.git diff --git a/plugins/gpulib/gpu.c b/plugins/gpulib/gpu.c index 931583f3..15810b82 100644 --- a/plugins/gpulib/gpu.c +++ b/plugins/gpulib/gpu.c @@ -12,6 +12,7 @@ #include #include #include "gpu.h" +#include "../../libpcsxcore/gpu.h" // meh #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #ifdef __GNUC__ @@ -24,13 +25,8 @@ #define noinline #endif -#define gpu_log(fmt, ...) \ - printf("%d:%03d: " fmt, *gpu.state.frame_count, *gpu.state.hcnt, ##__VA_ARGS__) - //#define log_io gpu_log #define log_io(...) -//#define log_anomaly gpu_log -#define log_anomaly(...) struct psx_gpu gpu; @@ -63,6 +59,8 @@ static noinline void do_reset(void) gpu.screen.hres = gpu.screen.w = 256; gpu.screen.vres = gpu.screen.h = 240; gpu.screen.x = gpu.screen.y = 0; + renderer_sync_ecmds(gpu.ex_regs); + renderer_notify_res_change(); } static noinline void update_width(void) @@ -183,8 +181,12 @@ static noinline int decide_frameskip_allow(uint32_t cmd_e3) return gpu.frameskip.allow; } +static void flush_cmd_buffer(void); + static noinline void get_gpu_info(uint32_t data) { + if (unlikely(gpu.cmd_len > 0)) + flush_cmd_buffer(); switch (data & 0x0f) { case 0x02: case 0x03: @@ -225,9 +227,11 @@ long GPUinit(void) ret = vout_init(); ret |= renderer_init(); + memset(&gpu.state, 0, sizeof(gpu.state)); + memset(&gpu.frameskip, 0, sizeof(gpu.frameskip)); + gpu.zero = 0; gpu.state.frame_count = &gpu.zero; gpu.state.hcnt = &gpu.zero; - gpu.frameskip.active = 0; gpu.cmd_len = 0; do_reset(); @@ -287,6 +291,7 @@ void GPUwriteStatus(uint32_t data) case 0x05: gpu.screen.src_x = data & 0x3ff; gpu.screen.src_y = (data >> 10) & 0x1ff; + renderer_notify_scanout_x_change(gpu.screen.src_x, gpu.screen.hres); if (gpu.frameskip.set) { decide_frameskip_allow(gpu.ex_regs[3]); if (gpu.frameskip.last_flip_frame != *gpu.state.frame_count) { @@ -332,23 +337,33 @@ const unsigned char cmd_lengths[256] = 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 0, 0, 0, 0, // 60 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, - 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // a0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // c0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 80 + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // a0 + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // c0 + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // e0 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; #define VRAM_MEM_XY(x, y) &gpu.vram[(y) * 1024 + (x)] -static inline void do_vram_line(int x, int y, uint16_t *mem, int l, int is_read) +static void cpy_msb(uint16_t *dst, const uint16_t *src, int l, uint16_t msb) +{ + int i; + for (i = 0; i < l; i++) + dst[i] = src[i] | msb; +} + +static inline void do_vram_line(int x, int y, uint16_t *mem, int l, + int is_read, uint16_t msb) { uint16_t *vram = VRAM_MEM_XY(x, y); - if (is_read) + if (unlikely(is_read)) memcpy(mem, vram, l * 2); + else if (unlikely(msb)) + cpy_msb(vram, mem, l, msb); else memcpy(vram, mem, l * 2); } @@ -356,6 +371,7 @@ static inline void do_vram_line(int x, int y, uint16_t *mem, int l, int is_read) static int do_vram_io(uint32_t *data, int count, int is_read) { int count_initial = count; + uint16_t msb = gpu.ex_regs[6] << 15; uint16_t *sdata = (uint16_t *)data; int x = gpu.dma.x, y = gpu.dma.y; int w = gpu.dma.w, h = gpu.dma.h; @@ -368,7 +384,7 @@ static int do_vram_io(uint32_t *data, int count, int is_read) if (count < l) l = count; - do_vram_line(x + o, y, sdata, l, is_read); + do_vram_line(x + o, y, sdata, l, is_read, msb); if (o + l < w) o += l; @@ -383,13 +399,13 @@ static int do_vram_io(uint32_t *data, int count, int is_read) for (; h > 0 && count >= w; sdata += w, count -= w, y++, h--) { y &= 511; - do_vram_line(x, y, sdata, w, is_read); + do_vram_line(x, y, sdata, w, is_read, msb); } if (h > 0) { if (count > 0) { y &= 511; - do_vram_line(x, y, sdata, count, is_read); + do_vram_line(x, y, sdata, count, is_read, msb); o = count; count = 0; } @@ -426,15 +442,66 @@ static void start_vram_transfer(uint32_t pos_word, uint32_t size_word, int is_re log_io("start_vram_transfer %c (%d, %d) %dx%d\n", is_read ? 'r' : 'w', gpu.dma.x, gpu.dma.y, gpu.dma.w, gpu.dma.h); + if (gpu.gpu_state_change) + gpu.gpu_state_change(PGS_VRAM_TRANSFER_START); } static void finish_vram_transfer(int is_read) { if (is_read) gpu.status &= ~PSX_GPU_STATUS_IMG; - else + else { + gpu.state.fb_dirty = 1; renderer_update_caches(gpu.dma_start.x, gpu.dma_start.y, - gpu.dma_start.w, gpu.dma_start.h); + gpu.dma_start.w, gpu.dma_start.h, 0); + } + if (gpu.gpu_state_change) + gpu.gpu_state_change(PGS_VRAM_TRANSFER_END); +} + +static void do_vram_copy(const uint32_t *params) +{ + const uint32_t sx = LE32TOH(params[0]) & 0x3FF; + const uint32_t sy = (LE32TOH(params[0]) >> 16) & 0x1FF; + const uint32_t dx = LE32TOH(params[1]) & 0x3FF; + const uint32_t dy = (LE32TOH(params[1]) >> 16) & 0x1FF; + uint32_t w = ((LE32TOH(params[2]) - 1) & 0x3FF) + 1; + uint32_t h = (((LE32TOH(params[2]) >> 16) - 1) & 0x1FF) + 1; + uint16_t msb = gpu.ex_regs[6] << 15; + uint16_t lbuf[128]; + uint32_t x, y; + + if (sx == dx && sy == dy && msb == 0) + return; + + renderer_flush_queues(); + + if (unlikely((sx < dx && dx < sx + w) || sx + w > 1024 || dx + w > 1024 || msb)) + { + for (y = 0; y < h; y++) + { + const uint16_t *src = VRAM_MEM_XY(0, (sy + y) & 0x1ff); + uint16_t *dst = VRAM_MEM_XY(0, (dy + y) & 0x1ff); + for (x = 0; x < w; x += ARRAY_SIZE(lbuf)) + { + uint32_t x1, w1 = w - x; + if (w1 > ARRAY_SIZE(lbuf)) + w1 = ARRAY_SIZE(lbuf); + for (x1 = 0; x1 < w1; x1++) + lbuf[x1] = src[(sx + x + x1) & 0x3ff]; + for (x1 = 0; x1 < w1; x1++) + dst[(dx + x + x1) & 0x3ff] = lbuf[x1] | msb; + } + } + } + else + { + uint32_t sy1 = sy, dy1 = dy; + for (y = 0; y < h; y++, sy1++, dy1++) + memcpy(VRAM_MEM_XY(dx, dy1 & 0x1ff), VRAM_MEM_XY(sx, sy1 & 0x1ff), w * 2); + } + + renderer_update_caches(dx, dy, w, h, 0); } static noinline int do_cmd_list_skip(uint32_t *data, int count, int *last_cmd) @@ -492,7 +559,7 @@ static noinline int do_cmd_list_skip(uint32_t *data, int count, int *last_cmd) cmd = -1; break; // incomplete cmd } - if (0xa0 <= cmd && cmd <= 0xdf) + if (0x80 <= cmd && cmd <= 0xdf) break; // image i/o pos += len; @@ -532,6 +599,16 @@ static noinline int do_cmd_buffer(uint32_t *data, int count) pos += 3; continue; } + else if ((cmd & 0xe0) == 0x80) { + if (unlikely((pos+3) >= count)) { + cmd = -1; // incomplete cmd, can't consume yet + break; + } + do_vram_copy(data + pos + 1); + vram_dirty = 1; + pos += 4; + continue; + } // 0xex cmds might affect frameskip.allow, so pass to do_cmd_list_skip if (gpu.frameskip.active && (gpu.frameskip.allow || ((LE32TOH(data[pos]) >> 24) & 0xf0) == 0xe0)) @@ -558,12 +635,16 @@ static noinline int do_cmd_buffer(uint32_t *data, int count) return count - pos; } -static void flush_cmd_buffer(void) +static noinline void flush_cmd_buffer(void) { int left = do_cmd_buffer(gpu.cmd_buffer, gpu.cmd_len); if (left > 0) memmove(gpu.cmd_buffer, gpu.cmd_buffer + gpu.cmd_len - left, left * 4); - gpu.cmd_len = left; + if (left != gpu.cmd_len) { + if (!gpu.dma.h && gpu.gpu_state_change) + gpu.gpu_state_change(PGS_PRIMITIVE_START); + gpu.cmd_len = left; + } } void GPUwriteDataMem(uint32_t *mem, int count) @@ -615,6 +696,10 @@ long GPUdmaChain(uint32_t *rambase, uint32_t start_addr, uint32_t *progress_addr log_io(".chain %08lx #%d+%d\n", (long)(list - rambase) * 4, len, gpu.cmd_len); if (unlikely(gpu.cmd_len > 0)) { + if (gpu.cmd_len + len > ARRAY_SIZE(gpu.cmd_buffer)) { + log_anomaly("cmd_buffer overflow, likely garbage commands\n"); + gpu.cmd_len = 0; + } memcpy(gpu.cmd_buffer + gpu.cmd_len, list + 1, len * 4); gpu.cmd_len += len; flush_cmd_buffer(); @@ -740,7 +825,7 @@ long GPUfreeze(uint32_t type, struct GPUFreeze *freeze) GPUwriteStatus((i << 24) | (gpu.regs[i] ^ 1)); } renderer_sync_ecmds(gpu.ex_regs); - renderer_update_caches(0, 0, 1024, 512); + renderer_update_caches(0, 0, 1024, 512, 1); break; } @@ -753,6 +838,7 @@ void GPUupdateLace(void) flush_cmd_buffer(); renderer_flush_queues(); +#ifndef RAW_FB_DISPLAY if (gpu.status & PSX_GPU_STATUS_BLANKING) { if (!gpu.state.blanked) { vout_blank(); @@ -764,6 +850,7 @@ void GPUupdateLace(void) if (!gpu.state.fb_dirty) return; +#endif if (gpu.frameskip.set) { if (!gpu.frameskip.frame_ready) { @@ -775,6 +862,9 @@ void GPUupdateLace(void) } vout_update(); + if (gpu.state.enhancement_active && !gpu.state.enhancement_was_active) + renderer_update_caches(0, 0, 1024, 512, 1); + gpu.state.enhancement_was_active = gpu.state.enhancement_active; gpu.state.fb_dirty = 0; gpu.state.blanked = 0; } @@ -801,6 +891,14 @@ void GPUvBlank(int is_vblank, int lcf) } } +void GPUgetScreenInfo(int *y, int *base_hres) +{ + *y = gpu.screen.y; + *base_hres = gpu.screen.vres; + if (gpu.status & PSX_GPU_STATUS_DHEIGHT) + *base_hres >>= 1; +} + #include "../../frontend/plugin_lib.h" void GPUrearmedCallbacks(const struct rearmed_cbs *cbs) @@ -825,6 +923,7 @@ void GPUrearmedCallbacks(const struct rearmed_cbs *cbs) gpu.mmap = cbs->mmap; gpu.munmap = cbs->munmap; + gpu.gpu_state_change = cbs->gpu_state_change; // delayed vram mmap if (gpu.vram == NULL)