Commit | Line | Data |
---|---|---|
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> | |
3b7b0065 | 12 | #include <assert.h> |
1fce6ce8 | 13 | |
3b7b0065 | 14 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
90ca4913 | 15 | |
652c6b8b | 16 | extern const unsigned char cmd_lengths[256]; |
17 | #define command_lengths cmd_lengths | |
18 | ||
b243416b | 19 | static unsigned int *ex_regs; |
9ee0fd5b | 20 | static int initialized; |
b243416b | 21 | |
22 | #define PCSX | |
23 | #define SET_Ex(r, v) \ | |
24 | ex_regs[r] = v | |
25 | ||
3b7b0065 | 26 | static __attribute__((noinline)) void |
27 | sync_enhancement_buffers(int x, int y, int w, int h); | |
28 | ||
29 | #include "../gpulib/gpu.h" | |
90ca4913 | 30 | #include "psx_gpu/psx_gpu.c" |
90ca4913 | 31 | #include "psx_gpu/psx_gpu_parse.c" |
90ca4913 | 32 | |
33 | static psx_gpu_struct egpu __attribute__((aligned(256))); | |
34 | ||
b243416b | 35 | int do_cmd_list(uint32_t *list, int count, int *last_cmd) |
90ca4913 | 36 | { |
c1817bd9 | 37 | int ret; |
38 | ||
9775fc33 | 39 | #if defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD) |
40 | // the asm doesn't bother to save callee-save vector regs, so do it here | |
41 | __asm__ __volatile__("":::"q4","q5","q6","q7"); | |
42 | #endif | |
43 | ||
c1817bd9 | 44 | if (gpu.state.enhancement_active) |
45 | ret = gpu_parse_enhanced(&egpu, list, count * 4, (u32 *)last_cmd); | |
46 | else | |
47 | ret = gpu_parse(&egpu, list, count * 4, (u32 *)last_cmd); | |
b243416b | 48 | |
9775fc33 | 49 | #if defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD) |
50 | __asm__ __volatile__("":::"q4","q5","q6","q7"); | |
51 | #endif | |
52 | ||
b243416b | 53 | ex_regs[1] &= ~0x1ff; |
54 | ex_regs[1] |= egpu.texture_settings & 0x1ff; | |
55 | return ret; | |
90ca4913 | 56 | } |
57 | ||
06bc35c8 | 58 | #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2) |
50f9355a | 59 | |
5bbe183f | 60 | static void *get_enhancement_bufer(int *x, int *y, int *w, int *h, |
fa56d360 | 61 | int *vram_h) |
a8be0deb | 62 | { |
63 | uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x); | |
64 | ||
65 | *x *= 2; | |
66 | *y *= 2; | |
67 | *w = *w * 2; | |
68 | *h = *h * 2; | |
fa56d360 | 69 | *vram_h = 1024; |
a8be0deb | 70 | return ret; |
71 | } | |
72 | ||
9ee0fd5b | 73 | static void map_enhancement_buffer(void) |
90ca4913 | 74 | { |
9ee0fd5b | 75 | // currently we use 4x 1024*1024 buffers instead of single 2048*1024 |
76 | // to be able to reuse 1024-width code better (triangle setup, | |
77 | // dithering phase, lines). | |
a8be0deb | 78 | egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE); |
79 | if (egpu.enhancement_buf_ptr == NULL) { | |
9ee0fd5b | 80 | fprintf(stderr, "failed to map enhancement buffer\n"); |
a8be0deb | 81 | gpu.get_enhancement_bufer = NULL; |
82 | } | |
83 | else { | |
84 | egpu.enhancement_buf_ptr += 4096 / 2; | |
85 | gpu.get_enhancement_bufer = get_enhancement_bufer; | |
86 | } | |
9ee0fd5b | 87 | } |
e929dec5 | 88 | |
9ee0fd5b | 89 | int renderer_init(void) |
90 | { | |
91 | if (gpu.vram != NULL) { | |
92 | initialize_psx_gpu(&egpu, gpu.vram); | |
93 | initialized = 1; | |
e929dec5 | 94 | } |
e929dec5 | 95 | |
a8be0deb | 96 | if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL) |
9ee0fd5b | 97 | map_enhancement_buffer(); |
98 | ||
99 | ex_regs = gpu.ex_regs; | |
90ca4913 | 100 | return 0; |
101 | } | |
102 | ||
e929dec5 | 103 | void renderer_finish(void) |
104 | { | |
a8be0deb | 105 | if (egpu.enhancement_buf_ptr != NULL) { |
106 | egpu.enhancement_buf_ptr -= 4096 / 2; | |
107 | gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE); | |
06bc35c8 | 108 | } |
e929dec5 | 109 | egpu.enhancement_buf_ptr = NULL; |
06bc35c8 | 110 | egpu.enhancement_current_buf_ptr = NULL; |
9ee0fd5b | 111 | initialized = 0; |
e929dec5 | 112 | } |
113 | ||
50f9355a | 114 | static __attribute__((noinline)) void |
115 | sync_enhancement_buffers(int x, int y, int w, int h) | |
116 | { | |
7956599f | 117 | const int step_x = 1024 / sizeof(egpu.enhancement_buf_by_x16); |
3b7b0065 | 118 | int hres = egpu.saved_hres; |
119 | int x_buf, w1, s, fb_index; | |
50f9355a | 120 | u16 *src, *dst; |
3b7b0065 | 121 | |
122 | if (egpu.enhancement_buf_ptr == NULL) | |
123 | return; | |
50f9355a | 124 | |
7956599f | 125 | w += x & (step_x - 1); |
126 | x &= ~(step_x - 1); | |
127 | w = (w + step_x - 1) & ~(step_x - 1); | |
50f9355a | 128 | if (y + h > 512) |
129 | h = 512 - y; | |
130 | ||
3b7b0065 | 131 | // find x_buf which is an offset into this enhancement_buf |
132 | fb_index = egpu.enhancement_buf_by_x16[x / step_x]; | |
133 | x_buf = x - egpu.enhancement_buf_start[fb_index]; | |
134 | ||
7956599f | 135 | while (w > 0) { |
136 | fb_index = egpu.enhancement_buf_by_x16[x / step_x]; | |
3b7b0065 | 137 | for (w1 = 0; w > 0 && x_buf < hres; x_buf += step_x, w1++, w -= step_x) |
7956599f | 138 | if (fb_index != egpu.enhancement_buf_by_x16[x / step_x + w1]) |
139 | break; | |
3b7b0065 | 140 | // skip further unneeded data, if any |
141 | for (s = 0; w > 0; s++, w -= step_x) | |
142 | if (fb_index != egpu.enhancement_buf_by_x16[x / step_x + w1 + s]) | |
143 | break; | |
7956599f | 144 | |
3b7b0065 | 145 | if (w1 > 0) { |
146 | src = gpu.vram + y * 1024 + x; | |
147 | dst = select_enhancement_buf_ptr(&egpu, x); | |
148 | dst += (y * 1024 + x) * 2; | |
149 | scale2x_tiles8(dst, src, w1 * step_x / 8, h); | |
150 | } | |
7956599f | 151 | |
3b7b0065 | 152 | x += (w1 + s) * step_x; |
9c279d22 | 153 | x &= 0x3ff; |
3b7b0065 | 154 | x_buf = 0; |
50f9355a | 155 | } |
156 | } | |
157 | ||
90ca4913 | 158 | void renderer_sync_ecmds(uint32_t *ecmds) |
159 | { | |
b243416b | 160 | gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL); |
90ca4913 | 161 | } |
162 | ||
3b7b0065 | 163 | void renderer_update_caches(int x, int y, int w, int h, int state_changed) |
90ca4913 | 164 | { |
05740673 | 165 | update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1); |
3b7b0065 | 166 | |
167 | if (gpu.state.enhancement_active) { | |
168 | if (state_changed) { | |
169 | egpu.saved_hres = 0; | |
170 | renderer_notify_res_change(); | |
171 | return; | |
172 | } | |
50f9355a | 173 | sync_enhancement_buffers(x, y, w, h); |
3b7b0065 | 174 | } |
90ca4913 | 175 | } |
176 | ||
177 | void renderer_flush_queues(void) | |
178 | { | |
179 | flush_render_block_buffer(&egpu); | |
180 | } | |
181 | ||
5440b88e | 182 | void renderer_set_interlace(int enable, int is_odd) |
183 | { | |
f1359c57 | 184 | egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD); |
5440b88e | 185 | if (enable) |
f1359c57 | 186 | egpu.render_mode |= RENDER_INTERLACE_ENABLED; |
5440b88e | 187 | if (is_odd) |
f1359c57 | 188 | egpu.render_mode |= RENDER_INTERLACE_ODD; |
5440b88e | 189 | } |
190 | ||
e929dec5 | 191 | void renderer_notify_res_change(void) |
192 | { | |
3b7b0065 | 193 | renderer_notify_scanout_x_change(gpu.screen.src_x, gpu.screen.hres); |
194 | } | |
195 | ||
196 | void renderer_notify_scanout_x_change(int x, int w) | |
197 | { | |
198 | int hres = (w + 15) & ~15; | |
199 | int max_bufs = ARRAY_SIZE(egpu.enhancement_scanout_x); | |
200 | int need_update = 0; | |
201 | int i; | |
202 | ||
203 | if (!gpu.state.enhancement_active) | |
204 | return; | |
205 | ||
206 | assert(!(max_bufs & (max_bufs - 1))); | |
207 | if (egpu.saved_hres != hres) { | |
208 | for (i = 0; i < max_bufs; i++) | |
209 | egpu.enhancement_scanout_x[i] = x; | |
210 | need_update = 1; | |
211 | } | |
212 | ||
213 | if (egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] != x) | |
214 | { | |
215 | // maybe triple buffering? | |
216 | for (i = 0; i < max_bufs; i++) | |
217 | if (egpu.enhancement_scanout_x[i] == x) | |
218 | break; | |
219 | if (i == max_bufs) | |
220 | need_update = 1; | |
221 | ||
222 | egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] = x; | |
223 | } | |
224 | egpu.enhancement_scanout_select++; | |
225 | egpu.enhancement_scanout_select &= max_bufs - 1; | |
226 | if (need_update) | |
7956599f | 227 | { |
3b7b0065 | 228 | egpu.saved_hres = hres; |
77e34391 | 229 | update_enhancement_buf_table_from_hres(&egpu); |
3b7b0065 | 230 | sync_enhancement_buffers(0, 0, 1024, 512); |
7956599f | 231 | } |
e929dec5 | 232 | } |
233 | ||
c1817bd9 | 234 | #include "../../frontend/plugin_lib.h" |
235 | ||
90ca4913 | 236 | void renderer_set_config(const struct rearmed_cbs *cbs) |
237 | { | |
9ee0fd5b | 238 | if (!initialized) { |
239 | initialize_psx_gpu(&egpu, gpu.vram); | |
240 | initialized = 1; | |
241 | } | |
fa56d360 | 242 | if (cbs->pl_set_gpu_caps) |
243 | cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X); | |
5c1cbedc | 244 | |
245 | egpu.use_dithering = cbs->gpu_neon.allow_dithering; | |
246 | if(!egpu.use_dithering) { | |
247 | egpu.dither_table[0] = dither_table_row(0, 0, 0, 0); | |
248 | egpu.dither_table[1] = dither_table_row(0, 0, 0, 0); | |
249 | egpu.dither_table[2] = dither_table_row(0, 0, 0, 0); | |
250 | egpu.dither_table[3] = dither_table_row(0, 0, 0, 0); | |
251 | } else { | |
252 | egpu.dither_table[0] = dither_table_row(-4, 0, -3, 1); | |
253 | egpu.dither_table[1] = dither_table_row(2, -2, 3, -1); | |
254 | egpu.dither_table[2] = dither_table_row(-3, 1, -4, 0); | |
255 | egpu.dither_table[3] = dither_table_row(3, -1, 2, -2); | |
256 | } | |
257 | ||
3b7b0065 | 258 | disable_main_render = cbs->gpu_neon.enhancement_no_main; |
259 | if (gpu.state.enhancement_enable) { | |
260 | if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL) | |
261 | map_enhancement_buffer(); | |
262 | } | |
90ca4913 | 263 | } |
3b7b0065 | 264 | |
c765eb86 JW |
265 | void renderer_sync(void) |
266 | { | |
267 | } | |
3b7b0065 | 268 | |
c765eb86 JW |
269 | void renderer_notify_update_lace(int updated) |
270 | { | |
271 | } | |
3b7b0065 | 272 | |
273 | // vim:ts=2:sw=2:expandtab |