2 * (C) GraÅžvydas "notaz" Ignotas, 2011
4 * This work is licensed under the terms of any of these licenses
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.
15 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
17 extern const unsigned char cmd_lengths[256];
18 #define command_lengths cmd_lengths
20 static unsigned int *ex_regs;
21 static int initialized;
24 #define SET_Ex(r, v) \
27 static __attribute__((noinline)) void
28 sync_enhancement_buffers(int x, int y, int w, int h);
30 #include "../gpulib/gpu.h"
31 #include "psx_gpu/psx_gpu.c"
32 #include "psx_gpu/psx_gpu_parse.c"
34 static psx_gpu_struct egpu __attribute__((aligned(256)));
36 int do_cmd_list(uint32_t *list, int count, int *last_cmd)
40 #if defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD)
41 // the asm doesn't bother to save callee-save vector regs, so do it here
42 __asm__ __volatile__("":::"q4","q5","q6","q7");
45 if (gpu.state.enhancement_active)
46 ret = gpu_parse_enhanced(&egpu, list, count * 4, (u32 *)last_cmd);
48 ret = gpu_parse(&egpu, list, count * 4, (u32 *)last_cmd);
50 #if defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD)
51 __asm__ __volatile__("":::"q4","q5","q6","q7");
55 ex_regs[1] |= egpu.texture_settings & 0x1ff;
59 #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
61 static void *get_enhancement_bufer(int *x, int *y, int *w, int *h,
64 uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x);
74 static void map_enhancement_buffer(void)
76 // currently we use 4x 1024*1024 buffers instead of single 2048*1024
77 // to be able to reuse 1024-width code better (triangle setup,
78 // dithering phase, lines).
79 egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE);
80 if (egpu.enhancement_buf_ptr == NULL) {
81 fprintf(stderr, "failed to map enhancement buffer\n");
82 gpu.get_enhancement_bufer = NULL;
85 egpu.enhancement_buf_ptr += 4096 / 2;
86 gpu.get_enhancement_bufer = get_enhancement_bufer;
90 int renderer_init(void)
92 if (gpu.vram != NULL) {
93 initialize_psx_gpu(&egpu, gpu.vram);
97 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
98 map_enhancement_buffer();
100 ex_regs = gpu.ex_regs;
104 void renderer_finish(void)
106 if (egpu.enhancement_buf_ptr != NULL) {
107 egpu.enhancement_buf_ptr -= 4096 / 2;
108 gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
110 egpu.enhancement_buf_ptr = NULL;
111 egpu.enhancement_current_buf_ptr = NULL;
115 static __attribute__((noinline)) void
116 sync_enhancement_buffers(int x, int y, int w, int h)
118 const int step_x = 1024 / sizeof(egpu.enhancement_buf_by_x16);
119 int hres = egpu.saved_hres;
120 int x_buf, w1, s, fb_index;
123 if (egpu.enhancement_buf_ptr == NULL)
126 w += x & (step_x - 1);
128 w = (w + step_x - 1) & ~(step_x - 1);
132 // find x_buf which is an offset into this enhancement_buf
133 fb_index = egpu.enhancement_buf_by_x16[x / step_x];
134 x_buf = x - egpu.enhancement_buf_start[fb_index];
137 fb_index = egpu.enhancement_buf_by_x16[x / step_x];
138 for (w1 = 0; w > 0 && x_buf < hres; x_buf += step_x, w1++, w -= step_x)
139 if (fb_index != egpu.enhancement_buf_by_x16[x / step_x + w1])
141 // skip further unneeded data, if any
142 for (s = 0; w > 0; s++, w -= step_x)
143 if (fb_index != egpu.enhancement_buf_by_x16[x / step_x + w1 + s])
147 src = gpu.vram + y * 1024 + x;
148 dst = select_enhancement_buf_ptr(&egpu, x);
149 dst += (y * 1024 + x) * 2;
150 scale2x_tiles8(dst, src, w1 * step_x / 8, h);
153 x += (w1 + s) * step_x;
159 void renderer_sync_ecmds(uint32_t *ecmds)
161 gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL);
164 void renderer_update_caches(int x, int y, int w, int h, int state_changed)
166 update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
168 if (gpu.state.enhancement_active) {
171 renderer_notify_res_change();
174 sync_enhancement_buffers(x, y, w, h);
178 void renderer_flush_queues(void)
180 flush_render_block_buffer(&egpu);
183 void renderer_set_interlace(int enable, int is_odd)
185 egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
187 egpu.render_mode |= RENDER_INTERLACE_ENABLED;
189 egpu.render_mode |= RENDER_INTERLACE_ODD;
192 void renderer_notify_res_change(void)
194 renderer_notify_scanout_x_change(gpu.screen.src_x, gpu.screen.hres);
197 void renderer_notify_scanout_x_change(int x, int w)
199 int hres = (w + 15) & ~15;
200 int max_bufs = ARRAY_SIZE(egpu.enhancement_scanout_x);
204 if (!gpu.state.enhancement_active)
207 assert(!(max_bufs & (max_bufs - 1)));
208 if (egpu.saved_hres != hres) {
209 for (i = 0; i < max_bufs; i++)
210 egpu.enhancement_scanout_x[i] = x;
214 if (egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] != x)
216 // maybe triple buffering?
217 for (i = 0; i < max_bufs; i++)
218 if (egpu.enhancement_scanout_x[i] == x)
223 egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] = x;
225 egpu.enhancement_scanout_select++;
226 egpu.enhancement_scanout_select &= max_bufs - 1;
229 egpu.saved_hres = hres;
230 update_enhancement_buf_table_from_hres(&egpu);
231 sync_enhancement_buffers(0, 0, 1024, 512);
235 #include "../../frontend/plugin_lib.h"
237 void renderer_set_config(const struct rearmed_cbs *cbs)
240 initialize_psx_gpu(&egpu, gpu.vram);
243 if (cbs->pl_set_gpu_caps)
244 cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
246 disable_main_render = cbs->gpu_neon.enhancement_no_main;
247 if (gpu.state.enhancement_enable) {
248 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
249 map_enhancement_buffer();
253 // vim:ts=2:sw=2:expandtab