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