improve ARM feature detection
[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 <sys/mman.h>
13
14 extern const unsigned char cmd_lengths[256];
15 #define command_lengths cmd_lengths
16
17 static unsigned int *ex_regs;
18 static int initialized;
19
20 #define PCSX
21 #define SET_Ex(r, v) \
22   ex_regs[r] = v
23
24 #include "psx_gpu/psx_gpu.c"
25 #include "psx_gpu/psx_gpu_parse.c"
26 #include "../gpulib/gpu.h"
27
28 static psx_gpu_struct egpu __attribute__((aligned(256)));
29
30 int do_cmd_list(uint32_t *list, int count, int *last_cmd)
31 {
32   int ret;
33
34   if (gpu.state.enhancement_active)
35     ret = gpu_parse_enhanced(&egpu, list, count * 4, (u32 *)last_cmd);
36   else
37     ret = gpu_parse(&egpu, list, count * 4, (u32 *)last_cmd);
38
39   ex_regs[1] &= ~0x1ff;
40   ex_regs[1] |= egpu.texture_settings & 0x1ff;
41   return ret;
42 }
43
44 #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
45
46 static uint16_t *get_enhancement_bufer(int *x, int *y, int *w, int *h,
47  int *vram_h)
48 {
49   uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x);
50
51   *x *= 2;
52   *y *= 2;
53   *w = *w * 2;
54   *h = *h * 2;
55   *vram_h = 1024;
56   return ret;
57 }
58
59 static void map_enhancement_buffer(void)
60 {
61   // currently we use 4x 1024*1024 buffers instead of single 2048*1024
62   // to be able to reuse 1024-width code better (triangle setup,
63   // dithering phase, lines).
64   egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE);
65   if (egpu.enhancement_buf_ptr == NULL) {
66     fprintf(stderr, "failed to map enhancement buffer\n");
67     gpu.get_enhancement_bufer = NULL;
68   }
69   else {
70     egpu.enhancement_buf_ptr += 4096 / 2;
71     gpu.get_enhancement_bufer = get_enhancement_bufer;
72   }
73 }
74
75 int renderer_init(void)
76 {
77   if (gpu.vram != NULL) {
78     initialize_psx_gpu(&egpu, gpu.vram);
79     initialized = 1;
80   }
81
82   if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
83     map_enhancement_buffer();
84
85   ex_regs = gpu.ex_regs;
86   return 0;
87 }
88
89 void renderer_finish(void)
90 {
91   if (egpu.enhancement_buf_ptr != NULL) {
92     egpu.enhancement_buf_ptr -= 4096 / 2;
93     gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
94   }
95   egpu.enhancement_buf_ptr = NULL;
96   egpu.enhancement_current_buf_ptr = NULL;
97   initialized = 0;
98 }
99
100 static __attribute__((noinline)) void
101 sync_enhancement_buffers(int x, int y, int w, int h)
102 {
103   const int step_x = 1024 / sizeof(egpu.enhancement_buf_by_x16);
104   u16 *src, *dst;
105   int w1, fb_index;
106
107   w += x & (step_x - 1);
108   x &= ~(step_x - 1);
109   w = (w + step_x - 1) & ~(step_x - 1);
110   if (y + h > 512)
111     h = 512 - y;
112
113   while (w > 0) {
114     fb_index = egpu.enhancement_buf_by_x16[x / step_x];
115     for (w1 = 0; w > 0; w1++, w -= step_x)
116       if (fb_index != egpu.enhancement_buf_by_x16[x / step_x + w1])
117         break;
118
119     src = gpu.vram + y * 1024 + x;
120     dst = select_enhancement_buf_ptr(&egpu, x);
121     dst += (y * 1024 + x) * 2;
122     scale2x_tiles8(dst, src, w1 * step_x / 8, h);
123
124     x += w1 * step_x;
125   }
126 }
127
128 void renderer_sync_ecmds(uint32_t *ecmds)
129 {
130   gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL);
131 }
132
133 void renderer_update_caches(int x, int y, int w, int h)
134 {
135   update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
136   if (gpu.state.enhancement_active && !gpu.status.rgb24)
137     sync_enhancement_buffers(x, y, w, h);
138 }
139
140 void renderer_flush_queues(void)
141 {
142   flush_render_block_buffer(&egpu);
143 }
144
145 void renderer_set_interlace(int enable, int is_odd)
146 {
147   egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
148   if (enable)
149     egpu.render_mode |= RENDER_INTERLACE_ENABLED;
150   if (is_odd)
151     egpu.render_mode |= RENDER_INTERLACE_ODD;
152 }
153
154 void renderer_notify_res_change(void)
155 {
156   // note: must keep it multiple of 8
157   if (egpu.enhancement_x_threshold != gpu.screen.hres)
158   {
159     egpu.enhancement_x_threshold = gpu.screen.hres;
160     update_enhancement_buf_table_from_hres(&egpu);
161   }
162 }
163
164 #include "../../frontend/plugin_lib.h"
165
166 void renderer_set_config(const struct rearmed_cbs *cbs)
167 {
168   static int enhancement_was_on;
169
170   disable_main_render = cbs->gpu_neon.enhancement_no_main;
171   if (egpu.enhancement_buf_ptr != NULL && cbs->gpu_neon.enhancement_enable
172       && !enhancement_was_on)
173   {
174     sync_enhancement_buffers(0, 0, 1024, 512);
175   }
176   enhancement_was_on = cbs->gpu_neon.enhancement_enable;
177
178   if (!initialized) {
179     initialize_psx_gpu(&egpu, gpu.vram);
180     initialized = 1;
181   }
182
183   if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
184     map_enhancement_buffer();
185   if (cbs->pl_set_gpu_caps)
186     cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
187 }