| 1 | /* |
| 2 | * (C) GraÅžvydas "notaz" Ignotas, 2011 |
| 3 | * |
| 4 | * This work is licensed under the terms of any of these licenses |
| 5 | * (at your option): |
| 6 | * - GNU GPL, version 2 or later. |
| 7 | * - GNU LGPL, version 2.1 or later. |
| 8 | * See the COPYING file in the top-level directory. |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <assert.h> |
| 13 | #include <sys/mman.h> |
| 14 | |
| 15 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 16 | #ifndef min |
| 17 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 18 | #endif |
| 19 | #ifndef max |
| 20 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 21 | #endif |
| 22 | |
| 23 | extern const unsigned char cmd_lengths[256]; |
| 24 | #define command_lengths cmd_lengths |
| 25 | |
| 26 | static unsigned int *ex_regs; |
| 27 | static int initialized; |
| 28 | |
| 29 | #define PCSX |
| 30 | #define SET_Ex(r, v) \ |
| 31 | ex_regs[r] = v |
| 32 | |
| 33 | static __attribute__((noinline)) void |
| 34 | sync_enhancement_buffers(int x, int y, int w, int h); |
| 35 | |
| 36 | #include "../gpulib/gpu.h" |
| 37 | #include "psx_gpu/psx_gpu.c" |
| 38 | #include "psx_gpu/psx_gpu_parse.c" |
| 39 | |
| 40 | static psx_gpu_struct egpu __attribute__((aligned(256))); |
| 41 | |
| 42 | int do_cmd_list(uint32_t *list, int count, int *last_cmd) |
| 43 | { |
| 44 | int ret; |
| 45 | |
| 46 | #if defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD) |
| 47 | // the asm doesn't bother to save callee-save vector regs, so do it here |
| 48 | __asm__ __volatile__("":::"q4","q5","q6","q7"); |
| 49 | #endif |
| 50 | |
| 51 | if (gpu.state.enhancement_active) |
| 52 | ret = gpu_parse_enhanced(&egpu, list, count * 4, (u32 *)last_cmd); |
| 53 | else |
| 54 | ret = gpu_parse(&egpu, list, count * 4, (u32 *)last_cmd); |
| 55 | |
| 56 | #if defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD) |
| 57 | __asm__ __volatile__("":::"q4","q5","q6","q7"); |
| 58 | #endif |
| 59 | |
| 60 | ex_regs[1] &= ~0x1ff; |
| 61 | ex_regs[1] |= egpu.texture_settings & 0x1ff; |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2) |
| 66 | |
| 67 | static void *get_enhancement_bufer(int *x, int *y, int *w, int *h, |
| 68 | int *vram_h) |
| 69 | { |
| 70 | uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x, *y); |
| 71 | if (ret == NULL) |
| 72 | return NULL; |
| 73 | |
| 74 | *x *= 2; |
| 75 | *y *= 2; |
| 76 | *w = *w * 2; |
| 77 | *h = *h * 2; |
| 78 | *vram_h = 1024; |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | static void map_enhancement_buffer(void) |
| 83 | { |
| 84 | // currently we use 4x 1024*1024 buffers instead of single 2048*1024 |
| 85 | // to be able to reuse 1024-width code better (triangle setup, |
| 86 | // dithering phase, lines). |
| 87 | egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE); |
| 88 | if (egpu.enhancement_buf_ptr == NULL) { |
| 89 | fprintf(stderr, "failed to map enhancement buffer\n"); |
| 90 | gpu.get_enhancement_bufer = NULL; |
| 91 | } |
| 92 | else { |
| 93 | egpu.enhancement_buf_ptr += 4096 / 2; |
| 94 | gpu.get_enhancement_bufer = get_enhancement_bufer; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | int renderer_init(void) |
| 99 | { |
| 100 | if (gpu.vram != NULL) { |
| 101 | initialize_psx_gpu(&egpu, gpu.vram); |
| 102 | initialized = 1; |
| 103 | } |
| 104 | |
| 105 | if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL) |
| 106 | map_enhancement_buffer(); |
| 107 | |
| 108 | ex_regs = gpu.ex_regs; |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | void renderer_finish(void) |
| 113 | { |
| 114 | if (egpu.enhancement_buf_ptr != NULL) { |
| 115 | egpu.enhancement_buf_ptr -= 4096 / 2; |
| 116 | gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE); |
| 117 | } |
| 118 | egpu.enhancement_buf_ptr = NULL; |
| 119 | egpu.enhancement_current_buf_ptr = NULL; |
| 120 | initialized = 0; |
| 121 | } |
| 122 | |
| 123 | static __attribute__((noinline)) void |
| 124 | sync_enhancement_buffers(int x, int y, int w, int h) |
| 125 | { |
| 126 | int i, right = x + w, bottom = y + h; |
| 127 | const u16 *src = gpu.vram; |
| 128 | // use these because the scanout struct may hold reduced w, h |
| 129 | // due to intersection stuff, see the update_enhancement_buf_scanouts() mess |
| 130 | int s_w = max(gpu.screen.hres, gpu.screen.w); |
| 131 | int s_h = gpu.screen.vres; |
| 132 | s_w = min(s_w, 512); |
| 133 | for (i = 0; i < ARRAY_SIZE(egpu.enhancement_scanouts); i++) { |
| 134 | const struct psx_gpu_scanout *s = &egpu.enhancement_scanouts[i]; |
| 135 | u16 *dst = select_enhancement_buf_by_index(&egpu, i); |
| 136 | int x1, x2, y1, y2; |
| 137 | if (s->w == 0) continue; |
| 138 | if (s->x >= right) continue; |
| 139 | if (s->x + s_w <= x) continue; |
| 140 | if (s->y >= bottom) continue; |
| 141 | if (s->y + s_h <= y) continue; |
| 142 | x1 = max(x, s->x); |
| 143 | x2 = min(right, s->x + s_w); |
| 144 | y1 = max(y, s->y); |
| 145 | y2 = min(bottom, s->y + s_h); |
| 146 | scale2x_tiles8(dst + y1 * 1024*2 + x1 * 2, |
| 147 | src + y1 * 1024 + x1, (x2 - x1 + 7) / 8u, y2 - y1); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void renderer_sync_ecmds(uint32_t *ecmds) |
| 152 | { |
| 153 | gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL); |
| 154 | } |
| 155 | |
| 156 | void renderer_update_caches(int x, int y, int w, int h, int state_changed) |
| 157 | { |
| 158 | update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1); |
| 159 | |
| 160 | if (gpu.state.enhancement_active) { |
| 161 | if (state_changed) { |
| 162 | memset(egpu.enhancement_scanouts, 0, sizeof(egpu.enhancement_scanouts)); |
| 163 | egpu.enhancement_scanout_eselect = 0; |
| 164 | update_enhancement_buf_scanouts(&egpu, |
| 165 | gpu.screen.src_x, gpu.screen.src_y, gpu.screen.hres, gpu.screen.vres); |
| 166 | return; |
| 167 | } |
| 168 | sync_enhancement_buffers(x, y, w, h); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void renderer_flush_queues(void) |
| 173 | { |
| 174 | flush_render_block_buffer(&egpu); |
| 175 | } |
| 176 | |
| 177 | void renderer_set_interlace(int enable, int is_odd) |
| 178 | { |
| 179 | egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD); |
| 180 | if (enable) |
| 181 | egpu.render_mode |= RENDER_INTERLACE_ENABLED; |
| 182 | if (is_odd) |
| 183 | egpu.render_mode |= RENDER_INTERLACE_ODD; |
| 184 | } |
| 185 | |
| 186 | void renderer_notify_res_change(void) |
| 187 | { |
| 188 | renderer_notify_scanout_change(gpu.screen.src_x, gpu.screen.src_y); |
| 189 | } |
| 190 | |
| 191 | void renderer_notify_scanout_change(int x, int y) |
| 192 | { |
| 193 | if (!gpu.state.enhancement_active || !egpu.enhancement_buf_ptr) |
| 194 | return; |
| 195 | |
| 196 | update_enhancement_buf_scanouts(&egpu, x, y, gpu.screen.hres, gpu.screen.vres); |
| 197 | } |
| 198 | |
| 199 | #include "../../frontend/plugin_lib.h" |
| 200 | |
| 201 | void renderer_set_config(const struct rearmed_cbs *cbs) |
| 202 | { |
| 203 | if (!initialized) { |
| 204 | initialize_psx_gpu(&egpu, gpu.vram); |
| 205 | initialized = 1; |
| 206 | } |
| 207 | if (cbs->pl_set_gpu_caps) |
| 208 | cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X); |
| 209 | |
| 210 | disable_main_render = cbs->gpu_neon.enhancement_no_main; |
| 211 | if (gpu.state.enhancement_enable) { |
| 212 | if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL) |
| 213 | map_enhancement_buffer(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // vim:ts=2:sw=2:expandtab |