gpu: handle wrapping somewhat
[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;
47c15995 154 x &= 0x3ff;
0b4038f8 155 x_buf = 0;
50f9355a 156 }
157}
158
90ca4913 159void renderer_sync_ecmds(uint32_t *ecmds)
160{
b243416b 161 gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL);
90ca4913 162}
163
0b4038f8 164void renderer_update_caches(int x, int y, int w, int h, int state_changed)
90ca4913 165{
05740673 166 update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
0b4038f8 167
168 if (gpu.state.enhancement_active) {
169 if (state_changed) {
170 egpu.saved_hres = 0;
171 renderer_notify_res_change();
172 return;
173 }
50f9355a 174 sync_enhancement_buffers(x, y, w, h);
0b4038f8 175 }
90ca4913 176}
177
178void renderer_flush_queues(void)
179{
180 flush_render_block_buffer(&egpu);
181}
182
5440b88e 183void renderer_set_interlace(int enable, int is_odd)
184{
f1359c57 185 egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
5440b88e 186 if (enable)
f1359c57 187 egpu.render_mode |= RENDER_INTERLACE_ENABLED;
5440b88e 188 if (is_odd)
f1359c57 189 egpu.render_mode |= RENDER_INTERLACE_ODD;
5440b88e 190}
191
e929dec5 192void renderer_notify_res_change(void)
193{
0b4038f8 194 renderer_notify_scanout_x_change(gpu.screen.src_x, gpu.screen.hres);
195}
196
197void renderer_notify_scanout_x_change(int x, int w)
198{
199 int hres = (w + 15) & ~15;
200 int max_bufs = ARRAY_SIZE(egpu.enhancement_scanout_x);
201 int need_update = 0;
202 int i;
203
204 if (!gpu.state.enhancement_active)
205 return;
206
207 assert(!(max_bufs & (max_bufs - 1)));
208 if (egpu.saved_hres != hres) {
209 for (i = 0; i < max_bufs; i++)
210 egpu.enhancement_scanout_x[i] = x;
211 need_update = 1;
212 }
213
214 if (egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] != x)
7956599f 215 {
0b4038f8 216 // maybe triple buffering?
217 for (i = 0; i < max_bufs; i++)
218 if (egpu.enhancement_scanout_x[i] == x)
219 break;
220 if (i == max_bufs)
221 need_update = 1;
222
223 egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] = x;
224 }
225 egpu.enhancement_scanout_select++;
226 egpu.enhancement_scanout_select &= max_bufs - 1;
227 if (need_update)
228 {
229 egpu.saved_hres = hres;
77e34391 230 update_enhancement_buf_table_from_hres(&egpu);
0b4038f8 231 sync_enhancement_buffers(0, 0, 1024, 512);
7956599f 232 }
e929dec5 233}
234
c1817bd9 235#include "../../frontend/plugin_lib.h"
236
90ca4913 237void renderer_set_config(const struct rearmed_cbs *cbs)
238{
9ee0fd5b 239 if (!initialized) {
240 initialize_psx_gpu(&egpu, gpu.vram);
241 initialized = 1;
242 }
fa56d360 243 if (cbs->pl_set_gpu_caps)
244 cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
0b4038f8 245
246 disable_main_render = cbs->gpu_neon.enhancement_no_main;
247 if (gpu.state.enhancement_enable) {
248 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
249 map_enhancement_buffer();
250 }
90ca4913 251}
0b4038f8 252
253// vim:ts=2:sw=2:expandtab