gpu: handle wrapping somewhat
[pcsx_rearmed.git] / plugins / gpu_neon / psx_gpu_if.c
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 #include <sys/mman.h>
14
15 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
16
17 extern const unsigned char cmd_lengths[256];
18 #define command_lengths cmd_lengths
19
20 static unsigned int *ex_regs;
21 static int initialized;
22
23 #define PCSX
24 #define SET_Ex(r, v) \
25   ex_regs[r] = v
26
27 static __attribute__((noinline)) void
28 sync_enhancement_buffers(int x, int y, int w, int h);
29
30 #include "../gpulib/gpu.h"
31 #include "psx_gpu/psx_gpu.c"
32 #include "psx_gpu/psx_gpu_parse.c"
33
34 static psx_gpu_struct egpu __attribute__((aligned(256)));
35
36 int do_cmd_list(uint32_t *list, int count, int *last_cmd)
37 {
38   int ret;
39
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
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);
49
50 #if defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD)
51   __asm__ __volatile__("":::"q4","q5","q6","q7");
52 #endif
53
54   ex_regs[1] &= ~0x1ff;
55   ex_regs[1] |= egpu.texture_settings & 0x1ff;
56   return ret;
57 }
58
59 #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
60
61 static void *get_enhancement_bufer(int *x, int *y, int *w, int *h,
62  int *vram_h)
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;
70   *vram_h = 1024;
71   return ret;
72 }
73
74 static void map_enhancement_buffer(void)
75 {
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).
79   egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE);
80   if (egpu.enhancement_buf_ptr == NULL) {
81     fprintf(stderr, "failed to map enhancement buffer\n");
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   }
88 }
89
90 int renderer_init(void)
91 {
92   if (gpu.vram != NULL) {
93     initialize_psx_gpu(&egpu, gpu.vram);
94     initialized = 1;
95   }
96
97   if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
98     map_enhancement_buffer();
99
100   ex_regs = gpu.ex_regs;
101   return 0;
102 }
103
104 void renderer_finish(void)
105 {
106   if (egpu.enhancement_buf_ptr != NULL) {
107     egpu.enhancement_buf_ptr -= 4096 / 2;
108     gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
109   }
110   egpu.enhancement_buf_ptr = NULL;
111   egpu.enhancement_current_buf_ptr = NULL;
112   initialized = 0;
113 }
114
115 static __attribute__((noinline)) void
116 sync_enhancement_buffers(int x, int y, int w, int h)
117 {
118   const int step_x = 1024 / sizeof(egpu.enhancement_buf_by_x16);
119   int hres = egpu.saved_hres;
120   int x_buf, w1, s, fb_index;
121   u16 *src, *dst;
122
123   if (egpu.enhancement_buf_ptr == NULL)
124     return;
125
126   w += x & (step_x - 1);
127   x &= ~(step_x - 1);
128   w = (w + step_x - 1) & ~(step_x - 1);
129   if (y + h > 512)
130     h = 512 - y;
131
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
136   while (w > 0) {
137     fb_index = egpu.enhancement_buf_by_x16[x / step_x];
138     for (w1 = 0; w > 0 && x_buf < hres; x_buf += step_x, w1++, w -= step_x)
139       if (fb_index != egpu.enhancement_buf_by_x16[x / step_x + w1])
140         break;
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;
145
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     }
152
153     x += (w1 + s) * step_x;
154     x &= 0x3ff;
155     x_buf = 0;
156   }
157 }
158
159 void renderer_sync_ecmds(uint32_t *ecmds)
160 {
161   gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL);
162 }
163
164 void renderer_update_caches(int x, int y, int w, int h, int state_changed)
165 {
166   update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
167
168   if (gpu.state.enhancement_active) {
169     if (state_changed) {
170       egpu.saved_hres = 0;
171       renderer_notify_res_change();
172       return;
173     }
174     sync_enhancement_buffers(x, y, w, h);
175   }
176 }
177
178 void renderer_flush_queues(void)
179 {
180   flush_render_block_buffer(&egpu);
181 }
182
183 void renderer_set_interlace(int enable, int is_odd)
184 {
185   egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
186   if (enable)
187     egpu.render_mode |= RENDER_INTERLACE_ENABLED;
188   if (is_odd)
189     egpu.render_mode |= RENDER_INTERLACE_ODD;
190 }
191
192 void renderer_notify_res_change(void)
193 {
194   renderer_notify_scanout_x_change(gpu.screen.src_x, gpu.screen.hres);
195 }
196
197 void 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)
215   {
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;
230     update_enhancement_buf_table_from_hres(&egpu);
231     sync_enhancement_buffers(0, 0, 1024, 512);
232   }
233 }
234
235 #include "../../frontend/plugin_lib.h"
236
237 void renderer_set_config(const struct rearmed_cbs *cbs)
238 {
239   if (!initialized) {
240     initialize_psx_gpu(&egpu, gpu.vram);
241     initialized = 1;
242   }
243   if (cbs->pl_set_gpu_caps)
244     cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
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   }
251 }
252
253 // vim:ts=2:sw=2:expandtab