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 #define min(a, b) ((a) < (b) ? (a) : (b))
19 #define max(a, b) ((a) > (b) ? (a) : (b))
22 extern const unsigned char cmd_lengths[256];
23 #define command_lengths cmd_lengths
25 static unsigned int *ex_regs;
26 static int initialized;
29 #define SET_Ex(r, v) \
32 static __attribute__((noinline)) void
33 sync_enhancement_buffers(int x, int y, int w, int h);
35 #include "../gpulib/gpu.h"
36 #include "psx_gpu/psx_gpu.c"
37 #include "psx_gpu/psx_gpu_parse.c"
39 static psx_gpu_struct egpu __attribute__((aligned(256)));
41 int do_cmd_list(uint32_t *list, int count,
42 int *cycles_sum, int *cycles_last, int *last_cmd)
46 if (gpu.state.enhancement_active)
47 ret = gpu_parse_enhanced(&egpu, list, count * 4,
48 cycles_sum, cycles_last, (u32 *)last_cmd);
50 ret = gpu_parse(&egpu, list, count * 4,
51 cycles_sum, cycles_last, (u32 *)last_cmd);
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, *y);
75 static void map_enhancement_buffer(void)
77 // currently we use 4x 1024*1024 buffers instead of single 2048*1024
78 // to be able to reuse 1024-width code better (triangle setup,
79 // dithering phase, lines).
80 egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE);
81 if (egpu.enhancement_buf_ptr == NULL || egpu.enhancement_buf_ptr == (void *)(intptr_t)-1) {
82 fprintf(stderr, "failed to map enhancement buffer\n");
83 egpu.enhancement_buf_ptr = NULL;
84 gpu.get_enhancement_bufer = NULL;
87 egpu.enhancement_buf_ptr += 4096 / 2;
88 gpu.get_enhancement_bufer = get_enhancement_bufer;
92 int renderer_init(void)
94 if (gpu.vram != NULL) {
95 initialize_psx_gpu(&egpu, gpu.vram);
99 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
100 map_enhancement_buffer();
102 ex_regs = gpu.ex_regs;
106 void renderer_finish(void)
108 if (egpu.enhancement_buf_ptr != NULL) {
109 egpu.enhancement_buf_ptr -= 4096 / 2;
110 gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
112 egpu.enhancement_buf_ptr = NULL;
113 egpu.enhancement_current_buf_ptr = NULL;
117 static __attribute__((noinline)) void
118 sync_enhancement_buffers(int x, int y, int w, int h)
120 int i, right = x + w, bottom = y + h;
121 const u16 *src = gpu.vram;
122 // use these because the scanout struct may hold reduced w, h
123 // due to intersection stuff, see the update_enhancement_buf_scanouts() mess
124 int s_w = max(gpu.screen.hres, gpu.screen.w);
125 int s_h = gpu.screen.vres;
126 if (gpu.screen.y < 0)
129 for (i = 0; i < ARRAY_SIZE(egpu.enhancement_scanouts); i++) {
130 const struct psx_gpu_scanout *s = &egpu.enhancement_scanouts[i];
131 u16 *dst = select_enhancement_buf_by_index(&egpu, i);
133 if (s->w == 0) continue;
134 if (s->x >= right) continue;
135 if (s->x + s_w <= x) continue;
136 if (s->y >= bottom) continue;
137 if (s->y + s_h <= y) continue;
139 x2 = min(right, s->x + s_w);
141 y2 = min(bottom, s->y + s_h);
142 // 16-byte align for the asm version
145 scale2x_tiles8(dst + y1 * 1024*2 + x1 * 2,
146 src + y1 * 1024 + x1, (x2 - x1 + 7) / 8u, y2 - y1);
150 void renderer_sync_ecmds(uint32_t *ecmds)
154 gpu_parse(&egpu, ecmds + 1, 6 * 4, &dummy0, &dummy0, &dummy1);
157 void renderer_update_caches(int x, int y, int w, int h, int state_changed)
159 update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
161 if (gpu.state.enhancement_active) {
163 int vres = gpu.screen.vres;
164 if (gpu.screen.y < 0)
165 vres -= gpu.screen.y;
166 memset(egpu.enhancement_scanouts, 0, sizeof(egpu.enhancement_scanouts));
167 egpu.enhancement_scanout_eselect = 0;
168 update_enhancement_buf_scanouts(&egpu,
169 gpu.screen.src_x, gpu.screen.src_y, gpu.screen.hres, vres);
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_change(gpu.screen.src_x, gpu.screen.src_y);
195 void renderer_notify_scanout_change(int x, int y)
197 int vres = gpu.screen.vres;
198 if (!gpu.state.enhancement_active || !egpu.enhancement_buf_ptr)
201 if (gpu.screen.y < 0)
202 vres -= gpu.screen.y;
203 update_enhancement_buf_scanouts(&egpu, x, y, gpu.screen.hres, vres);
206 #include "../../frontend/plugin_lib.h"
208 void renderer_set_config(const struct rearmed_cbs *cbs)
211 initialize_psx_gpu(&egpu, gpu.vram);
214 if (cbs->pl_set_gpu_caps)
215 cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
217 egpu.allow_dithering = cbs->dithering;
218 egpu.force_dithering = cbs->dithering >> 1;
220 if (!egpu.allow_dithering) {
221 egpu.dither_table[0] = dither_table_row(0, 0, 0, 0);
222 egpu.dither_table[1] = dither_table_row(0, 0, 0, 0);
223 egpu.dither_table[2] = dither_table_row(0, 0, 0, 0);
224 egpu.dither_table[3] = dither_table_row(0, 0, 0, 0);
226 egpu.dither_table[0] = dither_table_row(-4, 0, -3, 1);
227 egpu.dither_table[1] = dither_table_row(2, -2, 3, -1);
228 egpu.dither_table[2] = dither_table_row(-3, 1, -4, 0);
229 egpu.dither_table[3] = dither_table_row(3, -1, 2, -2);
233 egpu.hack_disable_main = cbs->gpu_neon.enhancement_no_main;
234 egpu.hack_texture_adj = cbs->gpu_neon.enhancement_tex_adj;
235 if (gpu.state.enhancement_enable) {
236 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
237 map_enhancement_buffer();
241 void renderer_sync(void)
245 void renderer_notify_update_lace(int updated)
249 // vim:ts=2:sw=2:expandtab