| 90ca4913 |
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> |
| 0b4038f8 |
12 | #include <assert.h> |
| 90ca4913 |
13 | |
| 0b4038f8 |
14 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 2da2fc76 |
15 | #ifndef min |
| 16 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 17 | #endif |
| 18 | #ifndef max |
| 19 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 20 | #endif |
| 0b4038f8 |
21 | |
| 652c6b8b |
22 | extern const unsigned char cmd_lengths[256]; |
| 23 | #define command_lengths cmd_lengths |
| 24 | |
| b243416b |
25 | static unsigned int *ex_regs; |
| 9ee0fd5b |
26 | static int initialized; |
| b243416b |
27 | |
| 28 | #define PCSX |
| 29 | #define SET_Ex(r, v) \ |
| 30 | ex_regs[r] = v |
| 31 | |
| 0b4038f8 |
32 | static __attribute__((noinline)) void |
| 33 | sync_enhancement_buffers(int x, int y, int w, int h); |
| 34 | |
| 35 | #include "../gpulib/gpu.h" |
| 90ca4913 |
36 | #include "psx_gpu/psx_gpu.c" |
| 90ca4913 |
37 | #include "psx_gpu/psx_gpu_parse.c" |
| 90ca4913 |
38 | |
| 39 | static psx_gpu_struct egpu __attribute__((aligned(256))); |
| 40 | |
| 8412166f |
41 | int do_cmd_list(uint32_t *list, int count, |
| 42 | int *cycles_sum, int *cycles_last, int *last_cmd) |
| 90ca4913 |
43 | { |
| c1817bd9 |
44 | int ret; |
| 45 | |
| 46 | if (gpu.state.enhancement_active) |
| 8412166f |
47 | ret = gpu_parse_enhanced(&egpu, list, count * 4, |
| 48 | cycles_sum, cycles_last, (u32 *)last_cmd); |
| c1817bd9 |
49 | else |
| 8412166f |
50 | ret = gpu_parse(&egpu, list, count * 4, |
| 51 | cycles_sum, cycles_last, (u32 *)last_cmd); |
| b243416b |
52 | |
| 53 | ex_regs[1] &= ~0x1ff; |
| 54 | ex_regs[1] |= egpu.texture_settings & 0x1ff; |
| 55 | return ret; |
| 90ca4913 |
56 | } |
| 57 | |
| 06bc35c8 |
58 | #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2) |
| 50f9355a |
59 | |
| 308c6e67 |
60 | static void *get_enhancement_bufer(int *x, int *y, int *w, int *h, |
| fa56d360 |
61 | int *vram_h) |
| a8be0deb |
62 | { |
| 2da2fc76 |
63 | uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x, *y); |
| 64 | if (ret == NULL) |
| 65 | return NULL; |
| a8be0deb |
66 | |
| 67 | *x *= 2; |
| 68 | *y *= 2; |
| 69 | *w = *w * 2; |
| 70 | *h = *h * 2; |
| fa56d360 |
71 | *vram_h = 1024; |
| a8be0deb |
72 | return ret; |
| 73 | } |
| 74 | |
| 9ee0fd5b |
75 | static void map_enhancement_buffer(void) |
| 90ca4913 |
76 | { |
| 9ee0fd5b |
77 | // currently we use 4x 1024*1024 buffers instead of single 2048*1024 |
| 78 | // to be able to reuse 1024-width code better (triangle setup, |
| 79 | // dithering phase, lines). |
| a8be0deb |
80 | egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE); |
| 7e9c3060 |
81 | if (egpu.enhancement_buf_ptr == NULL || egpu.enhancement_buf_ptr == (void *)(intptr_t)-1) { |
| 9ee0fd5b |
82 | fprintf(stderr, "failed to map enhancement buffer\n"); |
| 7e9c3060 |
83 | egpu.enhancement_buf_ptr = NULL; |
| a8be0deb |
84 | gpu.get_enhancement_bufer = NULL; |
| 85 | } |
| 86 | else { |
| 87 | egpu.enhancement_buf_ptr += 4096 / 2; |
| 88 | gpu.get_enhancement_bufer = get_enhancement_bufer; |
| 89 | } |
| 9ee0fd5b |
90 | } |
| e929dec5 |
91 | |
| 9ee0fd5b |
92 | int renderer_init(void) |
| 93 | { |
| 94 | if (gpu.vram != NULL) { |
| 95 | initialize_psx_gpu(&egpu, gpu.vram); |
| 96 | initialized = 1; |
| e929dec5 |
97 | } |
| e929dec5 |
98 | |
| a8be0deb |
99 | if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL) |
| 9ee0fd5b |
100 | map_enhancement_buffer(); |
| 101 | |
| 102 | ex_regs = gpu.ex_regs; |
| 90ca4913 |
103 | return 0; |
| 104 | } |
| 105 | |
| e929dec5 |
106 | void renderer_finish(void) |
| 107 | { |
| a8be0deb |
108 | if (egpu.enhancement_buf_ptr != NULL) { |
| 109 | egpu.enhancement_buf_ptr -= 4096 / 2; |
| 110 | gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE); |
| 06bc35c8 |
111 | } |
| e929dec5 |
112 | egpu.enhancement_buf_ptr = NULL; |
| 06bc35c8 |
113 | egpu.enhancement_current_buf_ptr = NULL; |
| 9ee0fd5b |
114 | initialized = 0; |
| e929dec5 |
115 | } |
| 116 | |
| 50f9355a |
117 | static __attribute__((noinline)) void |
| 118 | sync_enhancement_buffers(int x, int y, int w, int h) |
| 119 | { |
| 2da2fc76 |
120 | int i, right = x + w, bottom = y + h; |
| 121 | const u16 *src = gpu.vram; |
| 122 | // use these because the scanout struct may hold reduced w, h |
| 123 | // due to intersection stuff, see the update_enhancement_buf_scanouts() mess |
| 124 | int s_w = max(gpu.screen.hres, gpu.screen.w); |
| 125 | int s_h = gpu.screen.vres; |
| a3fcdfcb |
126 | if (gpu.screen.y < 0) |
| 127 | s_h -= gpu.screen.y; |
| 2da2fc76 |
128 | s_w = min(s_w, 512); |
| 129 | for (i = 0; i < ARRAY_SIZE(egpu.enhancement_scanouts); i++) { |
| 130 | const struct psx_gpu_scanout *s = &egpu.enhancement_scanouts[i]; |
| 131 | u16 *dst = select_enhancement_buf_by_index(&egpu, i); |
| 132 | int x1, x2, y1, y2; |
| 133 | if (s->w == 0) continue; |
| 134 | if (s->x >= right) continue; |
| 135 | if (s->x + s_w <= x) continue; |
| 136 | if (s->y >= bottom) continue; |
| 137 | if (s->y + s_h <= y) continue; |
| 138 | x1 = max(x, s->x); |
| 139 | x2 = min(right, s->x + s_w); |
| 140 | y1 = max(y, s->y); |
| 141 | y2 = min(bottom, s->y + s_h); |
| c2a25f67 |
142 | // 16-byte align for the asm version |
| 143 | x2 += x1 & 7; |
| 144 | x1 &= ~7; |
| 2da2fc76 |
145 | scale2x_tiles8(dst + y1 * 1024*2 + x1 * 2, |
| 146 | src + y1 * 1024 + x1, (x2 - x1 + 7) / 8u, y2 - y1); |
| 50f9355a |
147 | } |
| 148 | } |
| 149 | |
| 90ca4913 |
150 | void renderer_sync_ecmds(uint32_t *ecmds) |
| 151 | { |
| 90ac6fed |
152 | s32 dummy0 = 0; |
| 153 | u32 dummy1 = 0; |
| 8412166f |
154 | gpu_parse(&egpu, ecmds + 1, 6 * 4, &dummy0, &dummy0, &dummy1); |
| 90ca4913 |
155 | } |
| 156 | |
| 0b4038f8 |
157 | void renderer_update_caches(int x, int y, int w, int h, int state_changed) |
| 90ca4913 |
158 | { |
| 05740673 |
159 | update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1); |
| 0b4038f8 |
160 | |
| 161 | if (gpu.state.enhancement_active) { |
| 162 | if (state_changed) { |
| a3fcdfcb |
163 | int vres = gpu.screen.vres; |
| 164 | if (gpu.screen.y < 0) |
| 165 | vres -= gpu.screen.y; |
| 2da2fc76 |
166 | memset(egpu.enhancement_scanouts, 0, sizeof(egpu.enhancement_scanouts)); |
| 167 | egpu.enhancement_scanout_eselect = 0; |
| 1587b08e |
168 | update_enhancement_buf_scanouts(&egpu, |
| a3fcdfcb |
169 | gpu.screen.src_x, gpu.screen.src_y, gpu.screen.hres, vres); |
| 0b4038f8 |
170 | return; |
| 171 | } |
| 50f9355a |
172 | sync_enhancement_buffers(x, y, w, h); |
| 0b4038f8 |
173 | } |
| 90ca4913 |
174 | } |
| 175 | |
| 176 | void renderer_flush_queues(void) |
| 177 | { |
| 178 | flush_render_block_buffer(&egpu); |
| 179 | } |
| 180 | |
| 5440b88e |
181 | void renderer_set_interlace(int enable, int is_odd) |
| 182 | { |
| f1359c57 |
183 | egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD); |
| 5440b88e |
184 | if (enable) |
| f1359c57 |
185 | egpu.render_mode |= RENDER_INTERLACE_ENABLED; |
| 5440b88e |
186 | if (is_odd) |
| f1359c57 |
187 | egpu.render_mode |= RENDER_INTERLACE_ODD; |
| 5440b88e |
188 | } |
| 189 | |
| e929dec5 |
190 | void renderer_notify_res_change(void) |
| 191 | { |
| 2da2fc76 |
192 | renderer_notify_scanout_change(gpu.screen.src_x, gpu.screen.src_y); |
| 0b4038f8 |
193 | } |
| 194 | |
| 2da2fc76 |
195 | void renderer_notify_scanout_change(int x, int y) |
| 0b4038f8 |
196 | { |
| a3fcdfcb |
197 | int vres = gpu.screen.vres; |
| 2da2fc76 |
198 | if (!gpu.state.enhancement_active || !egpu.enhancement_buf_ptr) |
| 0b4038f8 |
199 | return; |
| 200 | |
| a3fcdfcb |
201 | if (gpu.screen.y < 0) |
| 202 | vres -= gpu.screen.y; |
| 203 | update_enhancement_buf_scanouts(&egpu, x, y, gpu.screen.hres, vres); |
| e929dec5 |
204 | } |
| 205 | |
| c1817bd9 |
206 | #include "../../frontend/plugin_lib.h" |
| 207 | |
| 90ca4913 |
208 | void renderer_set_config(const struct rearmed_cbs *cbs) |
| 209 | { |
| 9ee0fd5b |
210 | if (!initialized) { |
| 211 | initialize_psx_gpu(&egpu, gpu.vram); |
| 212 | initialized = 1; |
| 213 | } |
| fa56d360 |
214 | if (cbs->pl_set_gpu_caps) |
| 215 | cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X); |
| a3c46b7f |
216 | |
| ed8077c5 |
217 | egpu.allow_dithering = cbs->dithering; |
| 218 | egpu.force_dithering = cbs->dithering >> 1; |
| 219 | /* |
| 220 | if (!egpu.allow_dithering) { |
| a3c46b7f |
221 | egpu.dither_table[0] = dither_table_row(0, 0, 0, 0); |
| 222 | egpu.dither_table[1] = dither_table_row(0, 0, 0, 0); |
| 223 | egpu.dither_table[2] = dither_table_row(0, 0, 0, 0); |
| 224 | egpu.dither_table[3] = dither_table_row(0, 0, 0, 0); |
| 225 | } else { |
| 226 | egpu.dither_table[0] = dither_table_row(-4, 0, -3, 1); |
| 227 | egpu.dither_table[1] = dither_table_row(2, -2, 3, -1); |
| 228 | egpu.dither_table[2] = dither_table_row(-3, 1, -4, 0); |
| 229 | egpu.dither_table[3] = dither_table_row(3, -1, 2, -2); |
| 230 | } |
| ed8077c5 |
231 | */ |
| 0b4038f8 |
232 | |
| 6ab6ab97 |
233 | egpu.hack_disable_main = cbs->gpu_neon.enhancement_no_main; |
| 234 | egpu.hack_texture_adj = cbs->gpu_neon.enhancement_tex_adj; |
| 0b4038f8 |
235 | if (gpu.state.enhancement_enable) { |
| 236 | if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL) |
| 237 | map_enhancement_buffer(); |
| 238 | } |
| 90ca4913 |
239 | } |
| 0b4038f8 |
240 | |
| 79c4d434 |
241 | void renderer_sync(void) |
| 242 | { |
| 243 | } |
| 244 | |
| 245 | void renderer_notify_update_lace(int updated) |
| 246 | { |
| 247 | } |
| 248 | |
| 0b4038f8 |
249 | // vim:ts=2:sw=2:expandtab |