| 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 | |
| 14 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 15 | |
| 16 | extern const unsigned char cmd_lengths[256]; |
| 17 | #define command_lengths cmd_lengths |
| 18 | |
| 19 | static unsigned int *ex_regs; |
| 20 | static int initialized; |
| 21 | |
| 22 | #define PCSX |
| 23 | #define SET_Ex(r, v) \ |
| 24 | ex_regs[r] = v |
| 25 | |
| 26 | static __attribute__((noinline)) void |
| 27 | sync_enhancement_buffers(int x, int y, int w, int h); |
| 28 | |
| 29 | #include "../gpulib/gpu.h" |
| 30 | #include "psx_gpu/psx_gpu.c" |
| 31 | #include "psx_gpu/psx_gpu_parse.c" |
| 32 | |
| 33 | static psx_gpu_struct egpu __attribute__((aligned(256))); |
| 34 | |
| 35 | int do_cmd_list(uint32_t *list, int count, int *last_cmd) |
| 36 | { |
| 37 | int ret; |
| 38 | |
| 39 | #if defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD) |
| 40 | // the asm doesn't bother to save callee-save vector regs, so do it here |
| 41 | __asm__ __volatile__("":::"q4","q5","q6","q7"); |
| 42 | #endif |
| 43 | |
| 44 | if (gpu.state.enhancement_active) |
| 45 | ret = gpu_parse_enhanced(&egpu, list, count * 4, (u32 *)last_cmd); |
| 46 | else |
| 47 | ret = gpu_parse(&egpu, list, count * 4, (u32 *)last_cmd); |
| 48 | |
| 49 | #if defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD) |
| 50 | __asm__ __volatile__("":::"q4","q5","q6","q7"); |
| 51 | #endif |
| 52 | |
| 53 | ex_regs[1] &= ~0x1ff; |
| 54 | ex_regs[1] |= egpu.texture_settings & 0x1ff; |
| 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2) |
| 59 | |
| 60 | static void *get_enhancement_bufer(int *x, int *y, int *w, int *h, |
| 61 | int *vram_h) |
| 62 | { |
| 63 | uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x); |
| 64 | |
| 65 | *x *= 2; |
| 66 | *y *= 2; |
| 67 | *w = *w * 2; |
| 68 | *h = *h * 2; |
| 69 | *vram_h = 1024; |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | static void map_enhancement_buffer(void) |
| 74 | { |
| 75 | // currently we use 4x 1024*1024 buffers instead of single 2048*1024 |
| 76 | // to be able to reuse 1024-width code better (triangle setup, |
| 77 | // dithering phase, lines). |
| 78 | egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE); |
| 79 | if (egpu.enhancement_buf_ptr == NULL) { |
| 80 | fprintf(stderr, "failed to map enhancement buffer\n"); |
| 81 | gpu.get_enhancement_bufer = NULL; |
| 82 | } |
| 83 | else { |
| 84 | egpu.enhancement_buf_ptr += 4096 / 2; |
| 85 | gpu.get_enhancement_bufer = get_enhancement_bufer; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | int renderer_init(void) |
| 90 | { |
| 91 | if (gpu.vram != NULL) { |
| 92 | initialize_psx_gpu(&egpu, gpu.vram); |
| 93 | initialized = 1; |
| 94 | } |
| 95 | |
| 96 | if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL) |
| 97 | map_enhancement_buffer(); |
| 98 | |
| 99 | ex_regs = gpu.ex_regs; |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | void renderer_finish(void) |
| 104 | { |
| 105 | if (egpu.enhancement_buf_ptr != NULL) { |
| 106 | egpu.enhancement_buf_ptr -= 4096 / 2; |
| 107 | gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE); |
| 108 | } |
| 109 | egpu.enhancement_buf_ptr = NULL; |
| 110 | egpu.enhancement_current_buf_ptr = NULL; |
| 111 | initialized = 0; |
| 112 | } |
| 113 | |
| 114 | static __attribute__((noinline)) void |
| 115 | sync_enhancement_buffers(int x, int y, int w, int h) |
| 116 | { |
| 117 | const int step_x = 1024 / sizeof(egpu.enhancement_buf_by_x16); |
| 118 | int hres = egpu.saved_hres; |
| 119 | int x_buf, w1, s, fb_index; |
| 120 | u16 *src, *dst; |
| 121 | |
| 122 | if (egpu.enhancement_buf_ptr == NULL) |
| 123 | return; |
| 124 | |
| 125 | w += x & (step_x - 1); |
| 126 | x &= ~(step_x - 1); |
| 127 | w = (w + step_x - 1) & ~(step_x - 1); |
| 128 | if (y + h > 512) |
| 129 | h = 512 - y; |
| 130 | |
| 131 | // find x_buf which is an offset into this enhancement_buf |
| 132 | fb_index = egpu.enhancement_buf_by_x16[x / step_x]; |
| 133 | x_buf = x - egpu.enhancement_buf_start[fb_index]; |
| 134 | |
| 135 | while (w > 0) { |
| 136 | fb_index = egpu.enhancement_buf_by_x16[x / step_x]; |
| 137 | for (w1 = 0; w > 0 && x_buf < hres; x_buf += step_x, w1++, w -= step_x) |
| 138 | if (fb_index != egpu.enhancement_buf_by_x16[x / step_x + w1]) |
| 139 | break; |
| 140 | // skip further unneeded data, if any |
| 141 | for (s = 0; w > 0; s++, w -= step_x) |
| 142 | if (fb_index != egpu.enhancement_buf_by_x16[x / step_x + w1 + s]) |
| 143 | break; |
| 144 | |
| 145 | if (w1 > 0) { |
| 146 | src = gpu.vram + y * 1024 + x; |
| 147 | dst = select_enhancement_buf_ptr(&egpu, x); |
| 148 | dst += (y * 1024 + x) * 2; |
| 149 | scale2x_tiles8(dst, src, w1 * step_x / 8, h); |
| 150 | } |
| 151 | |
| 152 | x += (w1 + s) * step_x; |
| 153 | x_buf = 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void renderer_sync_ecmds(uint32_t *ecmds) |
| 158 | { |
| 159 | gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL); |
| 160 | } |
| 161 | |
| 162 | void renderer_update_caches(int x, int y, int w, int h, int state_changed) |
| 163 | { |
| 164 | update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1); |
| 165 | |
| 166 | if (gpu.state.enhancement_active) { |
| 167 | if (state_changed) { |
| 168 | egpu.saved_hres = 0; |
| 169 | renderer_notify_res_change(); |
| 170 | return; |
| 171 | } |
| 172 | sync_enhancement_buffers(x, y, w, h); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void renderer_flush_queues(void) |
| 177 | { |
| 178 | flush_render_block_buffer(&egpu); |
| 179 | } |
| 180 | |
| 181 | void renderer_set_interlace(int enable, int is_odd) |
| 182 | { |
| 183 | egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD); |
| 184 | if (enable) |
| 185 | egpu.render_mode |= RENDER_INTERLACE_ENABLED; |
| 186 | if (is_odd) |
| 187 | egpu.render_mode |= RENDER_INTERLACE_ODD; |
| 188 | } |
| 189 | |
| 190 | void renderer_notify_res_change(void) |
| 191 | { |
| 192 | renderer_notify_scanout_x_change(gpu.screen.src_x, gpu.screen.hres); |
| 193 | } |
| 194 | |
| 195 | void renderer_notify_scanout_x_change(int x, int w) |
| 196 | { |
| 197 | int hres = (w + 15) & ~15; |
| 198 | int max_bufs = ARRAY_SIZE(egpu.enhancement_scanout_x); |
| 199 | int need_update = 0; |
| 200 | int i; |
| 201 | |
| 202 | if (!gpu.state.enhancement_active) |
| 203 | return; |
| 204 | |
| 205 | assert(!(max_bufs & (max_bufs - 1))); |
| 206 | if (egpu.saved_hres != hres) { |
| 207 | for (i = 0; i < max_bufs; i++) |
| 208 | egpu.enhancement_scanout_x[i] = x; |
| 209 | need_update = 1; |
| 210 | } |
| 211 | |
| 212 | if (egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] != x) |
| 213 | { |
| 214 | // maybe triple buffering? |
| 215 | for (i = 0; i < max_bufs; i++) |
| 216 | if (egpu.enhancement_scanout_x[i] == x) |
| 217 | break; |
| 218 | if (i == max_bufs) |
| 219 | need_update = 1; |
| 220 | |
| 221 | egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] = x; |
| 222 | } |
| 223 | egpu.enhancement_scanout_select++; |
| 224 | egpu.enhancement_scanout_select &= max_bufs - 1; |
| 225 | if (need_update) |
| 226 | { |
| 227 | egpu.saved_hres = hres; |
| 228 | update_enhancement_buf_table_from_hres(&egpu); |
| 229 | sync_enhancement_buffers(0, 0, 1024, 512); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | #include "../../frontend/plugin_lib.h" |
| 234 | |
| 235 | void renderer_set_config(const struct rearmed_cbs *cbs) |
| 236 | { |
| 237 | if (!initialized) { |
| 238 | initialize_psx_gpu(&egpu, gpu.vram); |
| 239 | initialized = 1; |
| 240 | } |
| 241 | if (cbs->pl_set_gpu_caps) |
| 242 | cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X); |
| 243 | |
| 244 | egpu.use_dithering = cbs->gpu_neon.allow_dithering; |
| 245 | if(!egpu.use_dithering) { |
| 246 | egpu.dither_table[0] = dither_table_row(0, 0, 0, 0); |
| 247 | egpu.dither_table[1] = dither_table_row(0, 0, 0, 0); |
| 248 | egpu.dither_table[2] = dither_table_row(0, 0, 0, 0); |
| 249 | egpu.dither_table[3] = dither_table_row(0, 0, 0, 0); |
| 250 | } else { |
| 251 | egpu.dither_table[0] = dither_table_row(-4, 0, -3, 1); |
| 252 | egpu.dither_table[1] = dither_table_row(2, -2, 3, -1); |
| 253 | egpu.dither_table[2] = dither_table_row(-3, 1, -4, 0); |
| 254 | egpu.dither_table[3] = dither_table_row(3, -1, 2, -2); |
| 255 | } |
| 256 | |
| 257 | disable_main_render = cbs->gpu_neon.enhancement_no_main; |
| 258 | if (gpu.state.enhancement_enable) { |
| 259 | if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL) |
| 260 | map_enhancement_buffer(); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void renderer_sync(void) |
| 265 | { |
| 266 | } |
| 267 | |
| 268 | void renderer_notify_update_lace(int updated) |
| 269 | { |
| 270 | } |
| 271 | |
| 272 | // vim:ts=2:sw=2:expandtab |