gpulib: remove some unreliable heuristics
[pcsx_rearmed.git] / plugins / gpu_neon / psx_gpu_if.c
... / ...
CommitLineData
1/*
2 * (C) GraÅžvydas "notaz" Ignotas, 2011
3 *
4 * This work is licensed under the terms of any of these licenses
5 * (at your option):
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.
9 */
10
11#include <stdio.h>
12#include <assert.h>
13#include "../../include/compiler_features.h"
14
15#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
16#ifndef min
17#define min(a, b) ((a) < (b) ? (a) : (b))
18#endif
19#ifndef max
20#define max(a, b) ((a) > (b) ? (a) : (b))
21#endif
22
23extern const unsigned char cmd_lengths[256];
24#define command_lengths cmd_lengths
25
26static int initialized;
27
28#define PCSX
29
30static noinline void sync_enhancement_buffers(int x, int y, int w, int h);
31
32#include "../gpulib/gpu.h"
33#include "psx_gpu/psx_gpu.c"
34#include "psx_gpu/psx_gpu_parse.c"
35
36static psx_gpu_struct egpu __attribute__((aligned(256)));
37
38int renderer_do_cmd_list(uint32_t *list, int count, uint32_t *ex_regs,
39 int *cycles_sum, int *cycles_last, int *last_cmd)
40{
41 int ret;
42
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);
46 else
47 ret = gpu_parse(&egpu, list, count * 4, ex_regs,
48 cycles_sum, cycles_last, (u32 *)last_cmd);
49
50 ex_regs[1] &= ~0x1ff;
51 ex_regs[1] |= egpu.texture_settings & 0x1ff;
52 return ret;
53}
54
55#define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
56
57static void *get_enhancement_bufer(int *x, int *y, int *w, int *h,
58 int *vram_h)
59{
60 uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x, *y);
61 if (ret == NULL)
62 return NULL;
63
64 *x *= 2;
65 *y *= 2;
66 *w = *w * 2;
67 *h = *h * 2;
68 *vram_h = 1024;
69 return ret;
70}
71
72static void map_enhancement_buffer(void)
73{
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;
82 }
83 else {
84 egpu.enhancement_buf_ptr += 4096 / 2;
85 gpu.get_enhancement_bufer = get_enhancement_bufer;
86 }
87}
88
89int renderer_init(void)
90{
91 if (gpu.vram != NULL) {
92 initialize_psx_gpu(&egpu, gpu.vram);
93 initialized = 1;
94 }
95
96 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
97 map_enhancement_buffer();
98
99 return 0;
100}
101
102void renderer_finish(void)
103{
104 if (egpu.enhancement_buf_ptr != NULL) {
105 egpu.enhancement_buf_ptr -= 4096 / 2;
106 gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
107 }
108 egpu.enhancement_buf_ptr = NULL;
109 egpu.enhancement_current_buf_ptr = NULL;
110 initialized = 0;
111}
112
113static noinline void sync_enhancement_buffers(int x, int y, int w, int h)
114{
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)
122 s_h -= gpu.screen.y;
123 s_w = min(s_w, 512);
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);
127 int x1, x2, y1, y2;
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;
133 x1 = max(x, s->x);
134 x2 = min(right, s->x + s_w);
135 y1 = max(y, s->y);
136 y2 = min(bottom, s->y + s_h);
137 // 16-byte align for the asm version
138 x2 += x1 & 7;
139 x1 &= ~7;
140 scale2x_tiles8(dst + y1 * 1024*2 + x1 * 2,
141 src + y1 * 1024 + x1, (x2 - x1 + 7) / 8u, y2 - y1);
142 }
143}
144
145void renderer_sync_ecmds(uint32_t *ecmds)
146{
147 s32 dummy0 = 0;
148 u32 dummy1 = 0;
149 gpu_parse(&egpu, ecmds + 1, 6 * 4, ecmds, &dummy0, &dummy0, &dummy1);
150}
151
152void renderer_update_caches(int x, int y, int w, int h, int state_changed)
153{
154 update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
155
156 if (gpu.state.enhancement_active) {
157 if (state_changed) {
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);
165 return;
166 }
167 sync_enhancement_buffers(x, y, w, h);
168 }
169}
170
171void renderer_flush_queues(void)
172{
173 flush_render_block_buffer(&egpu);
174}
175
176void renderer_set_interlace(int enable, int is_odd)
177{
178 egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
179 if (enable)
180 egpu.render_mode |= RENDER_INTERLACE_ENABLED;
181 if (is_odd)
182 egpu.render_mode |= RENDER_INTERLACE_ODD;
183}
184
185void renderer_notify_screen_change(const struct psx_gpu_screen *screen)
186{
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)
190 return;
191
192 if (screen->y < 0)
193 vres -= screen->y;
194 update_enhancement_buf_scanouts(&egpu, x, y, screen->hres, vres);
195}
196
197#include "../../frontend/plugin_lib.h"
198
199void renderer_set_config(const struct rearmed_cbs *cbs)
200{
201 if (!initialized) {
202 initialize_psx_gpu(&egpu, gpu.vram);
203 initialized = 1;
204 }
205 if (cbs->pl_set_gpu_caps)
206 cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
207
208 egpu.allow_dithering = cbs->dithering;
209 egpu.force_dithering = cbs->dithering >> 1;
210 /*
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);
216 } else {
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);
221 }
222 */
223
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();
229 }
230}
231
232// vim:ts=2:sw=2:expandtab