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.
19 extern const unsigned char cmd_lengths[256];
20 #define command_lengths cmd_lengths
22 static unsigned int *ex_regs;
23 static int initialized;
26 #define SET_Ex(r, v) \
29 #include "psx_gpu/psx_gpu.c"
30 #include "psx_gpu/psx_gpu_parse.c"
31 #include "../gpulib/gpu.h"
33 static psx_gpu_struct egpu __attribute__((aligned(256)));
35 int do_cmd_list(uint32_t *list, int count, int *last_cmd)
39 if (gpu.state.enhancement_active)
40 ret = gpu_parse_enhanced(&egpu, list, count * 4, (u32 *)last_cmd);
42 ret = gpu_parse(&egpu, list, count * 4, (u32 *)last_cmd);
45 ex_regs[1] |= egpu.texture_settings & 0x1ff;
49 #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
51 static uint16_t *get_enhancement_bufer(int *x, int *y, int *w, int *h,
54 uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x);
64 static void map_enhancement_buffer(void)
66 // currently we use 4x 1024*1024 buffers instead of single 2048*1024
67 // to be able to reuse 1024-width code better (triangle setup,
68 // dithering phase, lines).
69 egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE);
70 if (egpu.enhancement_buf_ptr == NULL) {
71 fprintf(stderr, "failed to map enhancement buffer\n");
72 gpu.get_enhancement_bufer = NULL;
75 egpu.enhancement_buf_ptr += 4096 / 2;
76 gpu.get_enhancement_bufer = get_enhancement_bufer;
80 int renderer_init(void)
82 if (gpu.vram != NULL) {
83 initialize_psx_gpu(&egpu, gpu.vram);
87 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
88 map_enhancement_buffer();
90 ex_regs = gpu.ex_regs;
94 void renderer_finish(void)
96 if (egpu.enhancement_buf_ptr != NULL) {
97 egpu.enhancement_buf_ptr -= 4096 / 2;
98 gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
100 egpu.enhancement_buf_ptr = NULL;
101 egpu.enhancement_current_buf_ptr = NULL;
105 static __attribute__((noinline)) void
106 sync_enhancement_buffers(int x, int y, int w, int h)
108 const int step_x = 1024 / sizeof(egpu.enhancement_buf_by_x16);
112 w += x & (step_x - 1);
114 w = (w + step_x - 1) & ~(step_x - 1);
119 fb_index = egpu.enhancement_buf_by_x16[x / step_x];
120 for (w1 = 0; w > 0; w1++, w -= step_x)
121 if (fb_index != egpu.enhancement_buf_by_x16[x / step_x + w1])
124 src = gpu.vram + y * 1024 + x;
125 dst = select_enhancement_buf_ptr(&egpu, x);
126 dst += (y * 1024 + x) * 2;
127 scale2x_tiles8(dst, src, w1 * step_x / 8, h);
133 void renderer_sync_ecmds(uint32_t *ecmds)
135 gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL);
138 void renderer_update_caches(int x, int y, int w, int h)
140 update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
141 if (gpu.state.enhancement_active && !gpu.status.rgb24)
142 sync_enhancement_buffers(x, y, w, h);
145 void renderer_flush_queues(void)
147 flush_render_block_buffer(&egpu);
150 void renderer_set_interlace(int enable, int is_odd)
152 egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
154 egpu.render_mode |= RENDER_INTERLACE_ENABLED;
156 egpu.render_mode |= RENDER_INTERLACE_ODD;
159 void renderer_notify_res_change(void)
161 // note: must keep it multiple of 8
162 if (egpu.enhancement_x_threshold != gpu.screen.hres)
164 egpu.enhancement_x_threshold = gpu.screen.hres;
165 update_enhancement_buf_table_from_hres(&egpu);
169 #include "../../frontend/plugin_lib.h"
171 void renderer_set_config(const struct rearmed_cbs *cbs)
173 static int enhancement_was_on;
175 disable_main_render = cbs->gpu_neon.enhancement_no_main;
176 if (egpu.enhancement_buf_ptr != NULL && cbs->gpu_neon.enhancement_enable
177 && !enhancement_was_on)
179 sync_enhancement_buffers(0, 0, 1024, 512);
181 enhancement_was_on = cbs->gpu_neon.enhancement_enable;
184 initialize_psx_gpu(&egpu, gpu.vram);
188 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
189 map_enhancement_buffer();
190 if (cbs->pl_set_gpu_caps)
191 cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
193 egpu.use_dithering = cbs->gpu_neon.allow_dithering;
194 if(!egpu.use_dithering) {
195 egpu.dither_table[0] = dither_table_row(0, 0, 0, 0);
196 egpu.dither_table[1] = dither_table_row(0, 0, 0, 0);
197 egpu.dither_table[2] = dither_table_row(0, 0, 0, 0);
198 egpu.dither_table[3] = dither_table_row(0, 0, 0, 0);
200 egpu.dither_table[0] = dither_table_row(-4, 0, -3, 1);
201 egpu.dither_table[1] = dither_table_row(2, -2, 3, -1);
202 egpu.dither_table[2] = dither_table_row(-3, 1, -4, 0);
203 egpu.dither_table[3] = dither_table_row(3, -1, 2, -2);