f85155e3625eeab16f63f81aaf1d95ea194f50ad
[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
14 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
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
21
22 extern const unsigned char cmd_lengths[256];
23 #define command_lengths cmd_lengths
24
25 static unsigned int *ex_regs;
26 static int initialized;
27
28 #define PCSX
29 #define SET_Ex(r, v) \
30   ex_regs[r] = v
31
32 static __attribute__((noinline)) void
33 sync_enhancement_buffers(int x, int y, int w, int h);
34
35 #include "../gpulib/gpu.h"
36 #include "psx_gpu/psx_gpu.c"
37 #include "psx_gpu/psx_gpu_parse.c"
38
39 static psx_gpu_struct egpu __attribute__((aligned(256)));
40
41 int do_cmd_list(uint32_t *list, int count,
42  int *cycles_sum, int *cycles_last, int *last_cmd)
43 {
44   int ret;
45
46   if (gpu.state.enhancement_active)
47     ret = gpu_parse_enhanced(&egpu, list, count * 4,
48             cycles_sum, cycles_last, (u32 *)last_cmd);
49   else
50     ret = gpu_parse(&egpu, list, count * 4,
51             cycles_sum, cycles_last, (u32 *)last_cmd);
52
53   ex_regs[1] &= ~0x1ff;
54   ex_regs[1] |= egpu.texture_settings & 0x1ff;
55   return ret;
56 }
57
58 #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
59
60 static void *get_enhancement_bufer(int *x, int *y, int *w, int *h,
61  int *vram_h)
62 {
63   uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x, *y);
64   if (ret == NULL)
65     return NULL;
66
67   *x *= 2;
68   *y *= 2;
69   *w = *w * 2;
70   *h = *h * 2;
71   *vram_h = 1024;
72   return ret;
73 }
74
75 static void map_enhancement_buffer(void)
76 {
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).
80   egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE);
81   if (egpu.enhancement_buf_ptr == NULL || egpu.enhancement_buf_ptr == (void *)(intptr_t)-1) {
82     fprintf(stderr, "failed to map enhancement buffer\n");
83     egpu.enhancement_buf_ptr = NULL;
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   }
90 }
91
92 int renderer_init(void)
93 {
94   if (gpu.vram != NULL) {
95     initialize_psx_gpu(&egpu, gpu.vram);
96     initialized = 1;
97   }
98
99   if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
100     map_enhancement_buffer();
101
102   ex_regs = gpu.ex_regs;
103   return 0;
104 }
105
106 void renderer_finish(void)
107 {
108   if (egpu.enhancement_buf_ptr != NULL) {
109     egpu.enhancement_buf_ptr -= 4096 / 2;
110     gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
111   }
112   egpu.enhancement_buf_ptr = NULL;
113   egpu.enhancement_current_buf_ptr = NULL;
114   initialized = 0;
115 }
116
117 static __attribute__((noinline)) void
118 sync_enhancement_buffers(int x, int y, int w, int h)
119 {
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;
126   if (gpu.screen.y < 0)
127     s_h -= gpu.screen.y;
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);
142     // 16-byte align for the asm version
143     x2 += x1 & 7;
144     x1 &= ~7;
145     scale2x_tiles8(dst + y1 * 1024*2 + x1 * 2,
146         src + y1 * 1024 + x1, (x2 - x1 + 7) / 8u, y2 - y1);
147   }
148 }
149
150 void renderer_sync_ecmds(uint32_t *ecmds)
151 {
152   s32 dummy0 = 0;
153   u32 dummy1 = 0;
154   gpu_parse(&egpu, ecmds + 1, 6 * 4, &dummy0, &dummy0, &dummy1);
155 }
156
157 void renderer_update_caches(int x, int y, int w, int h, int state_changed)
158 {
159   update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
160
161   if (gpu.state.enhancement_active) {
162     if (state_changed) {
163       int vres = gpu.screen.vres;
164       if (gpu.screen.y < 0)
165         vres -= gpu.screen.y;
166       memset(egpu.enhancement_scanouts, 0, sizeof(egpu.enhancement_scanouts));
167       egpu.enhancement_scanout_eselect = 0;
168       update_enhancement_buf_scanouts(&egpu,
169         gpu.screen.src_x, gpu.screen.src_y, gpu.screen.hres, vres);
170       return;
171     }
172     sync_enhancement_buffers(x, y, w, h);
173   }
174 }
175
176 void renderer_flush_queues(void)
177 {
178   flush_render_block_buffer(&egpu);
179 }
180
181 void renderer_set_interlace(int enable, int is_odd)
182 {
183   egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
184   if (enable)
185     egpu.render_mode |= RENDER_INTERLACE_ENABLED;
186   if (is_odd)
187     egpu.render_mode |= RENDER_INTERLACE_ODD;
188 }
189
190 void renderer_notify_res_change(void)
191 {
192   renderer_notify_scanout_change(gpu.screen.src_x, gpu.screen.src_y);
193 }
194
195 void renderer_notify_scanout_change(int x, int y)
196 {
197   int vres = gpu.screen.vres;
198   if (!gpu.state.enhancement_active || !egpu.enhancement_buf_ptr)
199     return;
200
201   if (gpu.screen.y < 0)
202     vres -= gpu.screen.y;
203   update_enhancement_buf_scanouts(&egpu, x, y, gpu.screen.hres, vres);
204 }
205
206 #include "../../frontend/plugin_lib.h"
207
208 void renderer_set_config(const struct rearmed_cbs *cbs)
209 {
210   if (!initialized) {
211     initialize_psx_gpu(&egpu, gpu.vram);
212     initialized = 1;
213   }
214   if (cbs->pl_set_gpu_caps)
215     cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
216   
217   egpu.allow_dithering = cbs->dithering;
218   egpu.force_dithering = cbs->dithering >> 1;
219   /*
220   if (!egpu.allow_dithering) {
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   }
231   */
232
233   egpu.hack_disable_main = cbs->gpu_neon.enhancement_no_main;
234   egpu.hack_texture_adj = cbs->gpu_neon.enhancement_tex_adj;
235   if (gpu.state.enhancement_enable) {
236     if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
237       map_enhancement_buffer();
238   }
239 }
240
241 void renderer_sync(void)
242 {
243 }
244
245 void renderer_notify_update_lace(int updated)
246 {
247 }
248
249 // vim:ts=2:sw=2:expandtab