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