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.
13 #include "../../include/compiler_features.h"
15 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
17 #define min(a, b) ((a) < (b) ? (a) : (b))
20 #define max(a, b) ((a) > (b) ? (a) : (b))
23 extern const unsigned char cmd_lengths[256];
24 #define command_lengths cmd_lengths
26 static int initialized;
30 static noinline void sync_enhancement_buffers(int x, int y, int w, int h);
32 #include "../gpulib/gpu.h"
33 #include "psx_gpu/psx_gpu.c"
34 #include "psx_gpu/psx_gpu_parse.c"
36 static psx_gpu_struct egpu __attribute__((aligned(256)));
38 int renderer_do_cmd_list(uint32_t *list, int count, uint32_t *ex_regs,
39 int *cycles_sum, int *cycles_last, int *last_cmd)
43 if (gpu.state.enhancement_active)
44 ret = gpu_parse_enhanced(&egpu, list, count * 4, ex_regs,
45 cycles_sum, cycles_last, (u32 *)last_cmd);
47 ret = gpu_parse(&egpu, list, count * 4, ex_regs,
48 cycles_sum, cycles_last, (u32 *)last_cmd);
51 ex_regs[1] |= egpu.texture_settings & 0x1ff;
55 #define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
57 static void *get_enhancement_bufer(int *x, int *y, int *w, int *h,
60 uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x, *y);
72 static void map_enhancement_buffer(void)
74 // currently we use 4x 1024*1024 buffers instead of single 2048*1024
75 // to be able to reuse 1024-width code better (triangle setup,
76 // dithering phase, lines).
77 egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE);
78 if (egpu.enhancement_buf_ptr == NULL || egpu.enhancement_buf_ptr == (void *)(intptr_t)-1) {
79 SysPrintf("failed to map enhancement buffer\n");
80 egpu.enhancement_buf_ptr = NULL;
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();
102 void renderer_finish(void)
104 if (egpu.enhancement_buf_ptr != NULL) {
105 egpu.enhancement_buf_ptr -= 4096 / 2;
106 gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
108 egpu.enhancement_buf_ptr = NULL;
109 egpu.enhancement_current_buf_ptr = NULL;
113 static noinline void sync_enhancement_buffers(int x, int y, int w, int h)
115 int i, right = x + w, bottom = y + h;
116 const u16 *src = gpu.vram;
117 // use these because the scanout struct may hold reduced w, h
118 // due to intersection stuff, see the update_enhancement_buf_scanouts() mess
119 int s_w = max(gpu.screen.hres, gpu.screen.w);
120 int s_h = gpu.screen.vres;
121 if (gpu.screen.y < 0)
124 for (i = 0; i < ARRAY_SIZE(egpu.enhancement_scanouts); i++) {
125 const struct psx_gpu_scanout *s = &egpu.enhancement_scanouts[i];
126 u16 *dst = select_enhancement_buf_by_index(&egpu, i);
128 if (s->w == 0) continue;
129 if (s->x >= right) continue;
130 if (s->x + s_w <= x) continue;
131 if (s->y >= bottom) continue;
132 if (s->y + s_h <= y) continue;
134 x2 = min(right, s->x + s_w);
136 y2 = min(bottom, s->y + s_h);
137 // 16-byte align for the asm version
140 scale2x_tiles8(dst + y1 * 1024*2 + x1 * 2,
141 src + y1 * 1024 + x1, (x2 - x1 + 7) / 8u, y2 - y1);
145 void renderer_sync_ecmds(uint32_t *ecmds)
149 gpu_parse(&egpu, ecmds + 1, 6 * 4, ecmds, &dummy0, &dummy0, &dummy1);
152 void renderer_update_caches(int x, int y, int w, int h, int state_changed)
154 update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
156 if (gpu.state.enhancement_active) {
158 int vres = gpu.screen.vres;
159 if (gpu.screen.y < 0)
160 vres -= gpu.screen.y;
161 memset(egpu.enhancement_scanouts, 0, sizeof(egpu.enhancement_scanouts));
162 egpu.enhancement_scanout_eselect = 0;
163 update_enhancement_buf_scanouts(&egpu,
164 gpu.screen.src_x, gpu.screen.src_y, gpu.screen.hres, vres);
167 sync_enhancement_buffers(x, y, w, h);
171 void renderer_flush_queues(void)
173 flush_render_block_buffer(&egpu);
176 void renderer_set_interlace(int enable, int is_odd)
178 egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
180 egpu.render_mode |= RENDER_INTERLACE_ENABLED;
182 egpu.render_mode |= RENDER_INTERLACE_ODD;
185 void renderer_notify_screen_change(const struct psx_gpu_screen *screen)
187 int x = screen->src_x, y = screen->src_y;
188 int vres = screen->vres;
189 if (!gpu.state.enhancement_active || !egpu.enhancement_buf_ptr)
194 update_enhancement_buf_scanouts(&egpu, x, y, screen->hres, vres);
197 #include "../../frontend/plugin_lib.h"
199 void renderer_set_config(const struct rearmed_cbs *cbs)
202 initialize_psx_gpu(&egpu, gpu.vram);
205 if (cbs->pl_set_gpu_caps)
206 cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
208 egpu.allow_dithering = cbs->dithering;
209 egpu.force_dithering = cbs->dithering >> 1;
211 if (!egpu.allow_dithering) {
212 egpu.dither_table[0] = dither_table_row(0, 0, 0, 0);
213 egpu.dither_table[1] = dither_table_row(0, 0, 0, 0);
214 egpu.dither_table[2] = dither_table_row(0, 0, 0, 0);
215 egpu.dither_table[3] = dither_table_row(0, 0, 0, 0);
217 egpu.dither_table[0] = dither_table_row(-4, 0, -3, 1);
218 egpu.dither_table[1] = dither_table_row(2, -2, 3, -1);
219 egpu.dither_table[2] = dither_table_row(-3, 1, -4, 0);
220 egpu.dither_table[3] = dither_table_row(3, -1, 2, -2);
224 egpu.hack_disable_main = cbs->gpu_neon.enhancement_no_main;
225 egpu.hack_texture_adj = cbs->gpu_neon.enhancement_tex_adj;
226 if (gpu.state.enhancement_enable) {
227 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
228 map_enhancement_buffer();
232 // vim:ts=2:sw=2:expandtab