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.
14 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
16 extern const unsigned char cmd_lengths[256];
17 #define command_lengths cmd_lengths
19 static unsigned int *ex_regs;
20 static int initialized;
23 #define SET_Ex(r, v) \
26 static __attribute__((noinline)) void
27 sync_enhancement_buffers(int x, int y, int w, int h);
29 #include "../gpulib/gpu.h"
30 #include "psx_gpu/psx_gpu.c"
31 #include "psx_gpu/psx_gpu_parse.c"
33 static psx_gpu_struct egpu __attribute__((aligned(256)));
35 int do_cmd_list(uint32_t *list, int count, int *last_cmd)
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");
44 if (gpu.state.enhancement_active)
45 ret = gpu_parse_enhanced(&egpu, list, count * 4, (u32 *)last_cmd);
47 ret = gpu_parse(&egpu, list, count * 4, (u32 *)last_cmd);
49 #if defined(__arm__) && defined(NEON_BUILD) && !defined(SIMD_BUILD)
50 __asm__ __volatile__("":::"q4","q5","q6","q7");
54 ex_regs[1] |= egpu.texture_settings & 0x1ff;
58 #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
60 static void *get_enhancement_bufer(int *x, int *y, int *w, int *h,
63 uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x);
73 static void map_enhancement_buffer(void)
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).
78 egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE);
79 if (egpu.enhancement_buf_ptr == NULL) {
80 fprintf(stderr, "failed to map enhancement buffer\n");
81 gpu.get_enhancement_bufer = NULL;
84 egpu.enhancement_buf_ptr += 4096 / 2;
85 gpu.get_enhancement_bufer = get_enhancement_bufer;
89 int renderer_init(void)
91 if (gpu.vram != NULL) {
92 initialize_psx_gpu(&egpu, gpu.vram);
96 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
97 map_enhancement_buffer();
99 ex_regs = gpu.ex_regs;
103 void renderer_finish(void)
105 if (egpu.enhancement_buf_ptr != NULL) {
106 egpu.enhancement_buf_ptr -= 4096 / 2;
107 gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
109 egpu.enhancement_buf_ptr = NULL;
110 egpu.enhancement_current_buf_ptr = NULL;
114 static __attribute__((noinline)) void
115 sync_enhancement_buffers(int x, int y, int w, int h)
117 const int step_x = 1024 / sizeof(egpu.enhancement_buf_by_x16);
118 int hres = egpu.saved_hres;
119 int x_buf, w1, s, fb_index;
122 if (egpu.enhancement_buf_ptr == NULL)
125 w += x & (step_x - 1);
127 w = (w + step_x - 1) & ~(step_x - 1);
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];
136 fb_index = egpu.enhancement_buf_by_x16[x / step_x];
137 for (w1 = 0; w > 0 && x_buf < hres; x_buf += step_x, w1++, w -= step_x)
138 if (fb_index != egpu.enhancement_buf_by_x16[x / step_x + w1])
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])
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);
152 x += (w1 + s) * step_x;
157 void renderer_sync_ecmds(uint32_t *ecmds)
159 gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL);
162 void renderer_update_caches(int x, int y, int w, int h, int state_changed)
164 update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
166 if (gpu.state.enhancement_active) {
169 renderer_notify_res_change();
172 sync_enhancement_buffers(x, y, w, h);
176 void renderer_flush_queues(void)
178 flush_render_block_buffer(&egpu);
181 void renderer_set_interlace(int enable, int is_odd)
183 egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
185 egpu.render_mode |= RENDER_INTERLACE_ENABLED;
187 egpu.render_mode |= RENDER_INTERLACE_ODD;
190 void renderer_notify_res_change(void)
192 renderer_notify_scanout_x_change(gpu.screen.src_x, gpu.screen.hres);
195 void renderer_notify_scanout_x_change(int x, int w)
197 int hres = (w + 15) & ~15;
198 int max_bufs = ARRAY_SIZE(egpu.enhancement_scanout_x);
202 if (!gpu.state.enhancement_active)
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;
212 if (egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] != x)
214 // maybe triple buffering?
215 for (i = 0; i < max_bufs; i++)
216 if (egpu.enhancement_scanout_x[i] == x)
221 egpu.enhancement_scanout_x[egpu.enhancement_scanout_select] = x;
223 egpu.enhancement_scanout_select++;
224 egpu.enhancement_scanout_select &= max_bufs - 1;
227 egpu.saved_hres = hres;
228 update_enhancement_buf_table_from_hres(&egpu);
229 sync_enhancement_buffers(0, 0, 1024, 512);
233 #include "../../frontend/plugin_lib.h"
235 void renderer_set_config(const struct rearmed_cbs *cbs)
238 initialize_psx_gpu(&egpu, gpu.vram);
241 if (cbs->pl_set_gpu_caps)
242 cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
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);
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);
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();
264 void renderer_sync(void)
268 void renderer_notify_update_lace(int updated)
272 // vim:ts=2:sw=2:expandtab