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; |
153 | x_buf = 0; | |
50f9355a | 154 | } |
155 | } | |
156 | ||
90ca4913 | 157 | void renderer_sync_ecmds(uint32_t *ecmds) |
158 | { | |
b243416b | 159 | gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL); |
90ca4913 | 160 | } |
161 | ||
3b7b0065 | 162 | void renderer_update_caches(int x, int y, int w, int h, int state_changed) |
90ca4913 | 163 | { |
05740673 | 164 | update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1); |
3b7b0065 | 165 | |
166 | if (gpu.state.enhancement_active) { | |
167 | if (state_changed) { | |
168 | egpu.saved_hres = 0; | |
169 | renderer_notify_res_change(); | |
170 | return; | |
171 | } | |
50f9355a | 172 | sync_enhancement_buffers(x, y, w, h); |
3b7b0065 | 173 | } |
90ca4913 | 174 | } |
175 | ||
176 | void renderer_flush_queues(void) | |
177 | { | |
178 | flush_render_block_buffer(&egpu); | |
179 | } | |
180 | ||
5440b88e | 181 | void renderer_set_interlace(int enable, int is_odd) |
182 | { | |
f1359c57 | 183 | egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD); |
5440b88e | 184 | if (enable) |
f1359c57 | 185 | egpu.render_mode |= RENDER_INTERLACE_ENABLED; |
5440b88e | 186 | if (is_odd) |
f1359c57 | 187 | egpu.render_mode |= RENDER_INTERLACE_ODD; |
5440b88e | 188 | } |
189 | ||
e929dec5 | 190 | void renderer_notify_res_change(void) |
191 | { | |
3b7b0065 | 192 | renderer_notify_scanout_x_change(gpu.screen.src_x, gpu.screen.hres); |
193 | } | |
194 | ||
195 | void renderer_notify_scanout_x_change(int x, int w) | |
196 | { | |
197 | int hres = (w + 15) & ~15; | |
198 | int max_bufs = ARRAY_SIZE(egpu.enhancement_scanout_x); | |
199 | int need_update = 0; | |
200 | int i; | |
201 | ||
202 | if (!gpu.state.enhancement_active) | |
203 | return; | |
204 | ||
205 | assert(!(max_bufs & (max_bufs - 1))); | |
206 | if (egpu.saved_hres != hres) { | |
207 | for (i = 0; i < max_bufs; i++) | |
208 | egpu.enhancement_scanout_x[i] = x; | |
209 | need_update = 1; | |
210 | } | |
211 | ||
212 | if (egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] != x) | |
213 | { | |
214 | // maybe triple buffering? | |
215 | for (i = 0; i < max_bufs; i++) | |
216 | if (egpu.enhancement_scanout_x[i] == x) | |
217 | break; | |
218 | if (i == max_bufs) | |
219 | need_update = 1; | |
220 | ||
221 | egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] = x; | |
222 | } | |
223 | egpu.enhancement_scanout_select++; | |
224 | egpu.enhancement_scanout_select &= max_bufs - 1; | |
225 | if (need_update) | |
7956599f | 226 | { |
3b7b0065 | 227 | egpu.saved_hres = hres; |
77e34391 | 228 | update_enhancement_buf_table_from_hres(&egpu); |
3b7b0065 | 229 | sync_enhancement_buffers(0, 0, 1024, 512); |
7956599f | 230 | } |
e929dec5 | 231 | } |
232 | ||
c1817bd9 | 233 | #include "../../frontend/plugin_lib.h" |
234 | ||
90ca4913 | 235 | void renderer_set_config(const struct rearmed_cbs *cbs) |
236 | { | |
9ee0fd5b | 237 | if (!initialized) { |
238 | initialize_psx_gpu(&egpu, gpu.vram); | |
239 | initialized = 1; | |
240 | } | |
fa56d360 | 241 | if (cbs->pl_set_gpu_caps) |
242 | cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X); | |
5c1cbedc | 243 | |
244 | egpu.use_dithering = cbs->gpu_neon.allow_dithering; | |
245 | if(!egpu.use_dithering) { | |
246 | egpu.dither_table[0] = dither_table_row(0, 0, 0, 0); | |
247 | egpu.dither_table[1] = dither_table_row(0, 0, 0, 0); | |
248 | egpu.dither_table[2] = dither_table_row(0, 0, 0, 0); | |
249 | egpu.dither_table[3] = dither_table_row(0, 0, 0, 0); | |
250 | } else { | |
251 | egpu.dither_table[0] = dither_table_row(-4, 0, -3, 1); | |
252 | egpu.dither_table[1] = dither_table_row(2, -2, 3, -1); | |
253 | egpu.dither_table[2] = dither_table_row(-3, 1, -4, 0); | |
254 | egpu.dither_table[3] = dither_table_row(3, -1, 2, -2); | |
255 | } | |
256 | ||
3b7b0065 | 257 | disable_main_render = cbs->gpu_neon.enhancement_no_main; |
258 | if (gpu.state.enhancement_enable) { | |
259 | if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL) | |
260 | map_enhancement_buffer(); | |
261 | } | |
90ca4913 | 262 | } |
3b7b0065 | 263 | |
c765eb86 JW |
264 | void renderer_sync(void) |
265 | { | |
266 | } | |
3b7b0065 | 267 | |
c765eb86 JW |
268 | void renderer_notify_update_lace(int updated) |
269 | { | |
270 | } | |
3b7b0065 | 271 | |
272 | // vim:ts=2:sw=2:expandtab |