gpu_neon: try to make the compiler save some callee-save regs
[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 defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD)
35   // the asm doesn't bother to save callee-save vector regs, so do it here
36   __asm__ __volatile__("":::"q4","q5","q6","q7");
37 #endif
38
39   if (gpu.state.enhancement_active)
40     ret = gpu_parse_enhanced(&egpu, list, count * 4, (u32 *)last_cmd);
41   else
42     ret = gpu_parse(&egpu, list, count * 4, (u32 *)last_cmd);
43
44 #if defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD)
45   __asm__ __volatile__("":::"q4","q5","q6","q7");
46 #endif
47
48   ex_regs[1] &= ~0x1ff;
49   ex_regs[1] |= egpu.texture_settings & 0x1ff;
50   return ret;
51 }
52
53 #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
54
55 static uint16_t *get_enhancement_bufer(int *x, int *y, int *w, int *h,
56  int *vram_h)
57 {
58   uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x);
59
60   *x *= 2;
61   *y *= 2;
62   *w = *w * 2;
63   *h = *h * 2;
64   *vram_h = 1024;
65   return ret;
66 }
67
68 static void map_enhancement_buffer(void)
69 {
70   // currently we use 4x 1024*1024 buffers instead of single 2048*1024
71   // to be able to reuse 1024-width code better (triangle setup,
72   // dithering phase, lines).
73   egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE);
74   if (egpu.enhancement_buf_ptr == NULL) {
75     fprintf(stderr, "failed to map enhancement buffer\n");
76     gpu.get_enhancement_bufer = NULL;
77   }
78   else {
79     egpu.enhancement_buf_ptr += 4096 / 2;
80     gpu.get_enhancement_bufer = get_enhancement_bufer;
81   }
82 }
83
84 int renderer_init(void)
85 {
86   if (gpu.vram != NULL) {
87     initialize_psx_gpu(&egpu, gpu.vram);
88     initialized = 1;
89   }
90
91   if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
92     map_enhancement_buffer();
93
94   ex_regs = gpu.ex_regs;
95   return 0;
96 }
97
98 void renderer_finish(void)
99 {
100   if (egpu.enhancement_buf_ptr != NULL) {
101     egpu.enhancement_buf_ptr -= 4096 / 2;
102     gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
103   }
104   egpu.enhancement_buf_ptr = NULL;
105   egpu.enhancement_current_buf_ptr = NULL;
106   initialized = 0;
107 }
108
109 static __attribute__((noinline)) void
110 sync_enhancement_buffers(int x, int y, int w, int h)
111 {
112   const int step_x = 1024 / sizeof(egpu.enhancement_buf_by_x16);
113   u16 *src, *dst;
114   int w1, fb_index;
115
116   w += x & (step_x - 1);
117   x &= ~(step_x - 1);
118   w = (w + step_x - 1) & ~(step_x - 1);
119   if (y + h > 512)
120     h = 512 - y;
121
122   while (w > 0) {
123     fb_index = egpu.enhancement_buf_by_x16[x / step_x];
124     for (w1 = 0; w > 0; w1++, w -= step_x)
125       if (fb_index != egpu.enhancement_buf_by_x16[x / step_x + w1])
126         break;
127
128     src = gpu.vram + y * 1024 + x;
129     dst = select_enhancement_buf_ptr(&egpu, x);
130     dst += (y * 1024 + x) * 2;
131     scale2x_tiles8(dst, src, w1 * step_x / 8, h);
132
133     x += w1 * step_x;
134   }
135 }
136
137 void renderer_sync_ecmds(uint32_t *ecmds)
138 {
139   gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL);
140 }
141
142 void renderer_update_caches(int x, int y, int w, int h)
143 {
144   update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
145   if (gpu.state.enhancement_active && !(gpu.status & PSX_GPU_STATUS_RGB24))
146     sync_enhancement_buffers(x, y, w, h);
147 }
148
149 void renderer_flush_queues(void)
150 {
151   flush_render_block_buffer(&egpu);
152 }
153
154 void renderer_set_interlace(int enable, int is_odd)
155 {
156   egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
157   if (enable)
158     egpu.render_mode |= RENDER_INTERLACE_ENABLED;
159   if (is_odd)
160     egpu.render_mode |= RENDER_INTERLACE_ODD;
161 }
162
163 void renderer_notify_res_change(void)
164 {
165   // note: must keep it multiple of 8
166   if (egpu.enhancement_x_threshold != gpu.screen.hres)
167   {
168     egpu.enhancement_x_threshold = gpu.screen.hres;
169     update_enhancement_buf_table_from_hres(&egpu);
170   }
171 }
172
173 #include "../../frontend/plugin_lib.h"
174
175 void renderer_set_config(const struct rearmed_cbs *cbs)
176 {
177   static int enhancement_was_on;
178
179   disable_main_render = cbs->gpu_neon.enhancement_no_main;
180   if (egpu.enhancement_buf_ptr != NULL && cbs->gpu_neon.enhancement_enable
181       && !enhancement_was_on)
182   {
183     sync_enhancement_buffers(0, 0, 1024, 512);
184   }
185   enhancement_was_on = cbs->gpu_neon.enhancement_enable;
186
187   if (!initialized) {
188     initialize_psx_gpu(&egpu, gpu.vram);
189     initialized = 1;
190   }
191
192   if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
193     map_enhancement_buffer();
194   if (cbs->pl_set_gpu_caps)
195     cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
196 }