gpulib: remove some unreliable heuristics
[pcsx_rearmed.git] / plugins / gpu_neon / psx_gpu_if.c
CommitLineData
90ca4913 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>
0b4038f8 12#include <assert.h>
f2440716 13#include "../../include/compiler_features.h"
90ca4913 14
0b4038f8 15#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
2da2fc76 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
0b4038f8 22
652c6b8b 23extern const unsigned char cmd_lengths[256];
24#define command_lengths cmd_lengths
25
9ee0fd5b 26static int initialized;
b243416b 27
28#define PCSX
b243416b 29
f2440716 30static noinline void sync_enhancement_buffers(int x, int y, int w, int h);
0b4038f8 31
32#include "../gpulib/gpu.h"
90ca4913 33#include "psx_gpu/psx_gpu.c"
90ca4913 34#include "psx_gpu/psx_gpu_parse.c"
90ca4913 35
36static psx_gpu_struct egpu __attribute__((aligned(256)));
37
7fe57395 38int renderer_do_cmd_list(uint32_t *list, int count, uint32_t *ex_regs,
8412166f 39 int *cycles_sum, int *cycles_last, int *last_cmd)
90ca4913 40{
c1817bd9 41 int ret;
42
43 if (gpu.state.enhancement_active)
7fe57395 44 ret = gpu_parse_enhanced(&egpu, list, count * 4, ex_regs,
8412166f 45 cycles_sum, cycles_last, (u32 *)last_cmd);
c1817bd9 46 else
7fe57395 47 ret = gpu_parse(&egpu, list, count * 4, ex_regs,
8412166f 48 cycles_sum, cycles_last, (u32 *)last_cmd);
b243416b 49
50 ex_regs[1] &= ~0x1ff;
51 ex_regs[1] |= egpu.texture_settings & 0x1ff;
52 return ret;
90ca4913 53}
54
06bc35c8 55#define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
50f9355a 56
308c6e67 57static void *get_enhancement_bufer(int *x, int *y, int *w, int *h,
fa56d360 58 int *vram_h)
a8be0deb 59{
2da2fc76 60 uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x, *y);
61 if (ret == NULL)
62 return NULL;
a8be0deb 63
64 *x *= 2;
65 *y *= 2;
66 *w = *w * 2;
67 *h = *h * 2;
fa56d360 68 *vram_h = 1024;
a8be0deb 69 return ret;
70}
71
9ee0fd5b 72static void map_enhancement_buffer(void)
90ca4913 73{
9ee0fd5b 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).
a8be0deb 77 egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE);
7e9c3060 78 if (egpu.enhancement_buf_ptr == NULL || egpu.enhancement_buf_ptr == (void *)(intptr_t)-1) {
759751e0 79 SysPrintf("failed to map enhancement buffer\n");
7e9c3060 80 egpu.enhancement_buf_ptr = NULL;
a8be0deb 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 }
9ee0fd5b 87}
e929dec5 88
9ee0fd5b 89int renderer_init(void)
90{
91 if (gpu.vram != NULL) {
92 initialize_psx_gpu(&egpu, gpu.vram);
93 initialized = 1;
e929dec5 94 }
e929dec5 95
a8be0deb 96 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
9ee0fd5b 97 map_enhancement_buffer();
98
90ca4913 99 return 0;
100}
101
e929dec5 102void renderer_finish(void)
103{
a8be0deb 104 if (egpu.enhancement_buf_ptr != NULL) {
105 egpu.enhancement_buf_ptr -= 4096 / 2;
106 gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
06bc35c8 107 }
e929dec5 108 egpu.enhancement_buf_ptr = NULL;
06bc35c8 109 egpu.enhancement_current_buf_ptr = NULL;
9ee0fd5b 110 initialized = 0;
e929dec5 111}
112
f2440716 113static noinline void sync_enhancement_buffers(int x, int y, int w, int h)
50f9355a 114{
2da2fc76 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;
a3fcdfcb 121 if (gpu.screen.y < 0)
122 s_h -= gpu.screen.y;
2da2fc76 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);
c2a25f67 137 // 16-byte align for the asm version
138 x2 += x1 & 7;
139 x1 &= ~7;
2da2fc76 140 scale2x_tiles8(dst + y1 * 1024*2 + x1 * 2,
141 src + y1 * 1024 + x1, (x2 - x1 + 7) / 8u, y2 - y1);
50f9355a 142 }
143}
144
90ca4913 145void renderer_sync_ecmds(uint32_t *ecmds)
146{
90ac6fed 147 s32 dummy0 = 0;
148 u32 dummy1 = 0;
7fe57395 149 gpu_parse(&egpu, ecmds + 1, 6 * 4, ecmds, &dummy0, &dummy0, &dummy1);
90ca4913 150}
151
0b4038f8 152void renderer_update_caches(int x, int y, int w, int h, int state_changed)
90ca4913 153{
05740673 154 update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
0b4038f8 155
156 if (gpu.state.enhancement_active) {
157 if (state_changed) {
a3fcdfcb 158 int vres = gpu.screen.vres;
159 if (gpu.screen.y < 0)
160 vres -= gpu.screen.y;
2da2fc76 161 memset(egpu.enhancement_scanouts, 0, sizeof(egpu.enhancement_scanouts));
162 egpu.enhancement_scanout_eselect = 0;
1587b08e 163 update_enhancement_buf_scanouts(&egpu,
a3fcdfcb 164 gpu.screen.src_x, gpu.screen.src_y, gpu.screen.hres, vres);
0b4038f8 165 return;
166 }
50f9355a 167 sync_enhancement_buffers(x, y, w, h);
0b4038f8 168 }
90ca4913 169}
170
171void renderer_flush_queues(void)
172{
173 flush_render_block_buffer(&egpu);
174}
175
5440b88e 176void renderer_set_interlace(int enable, int is_odd)
177{
f1359c57 178 egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
5440b88e 179 if (enable)
f1359c57 180 egpu.render_mode |= RENDER_INTERLACE_ENABLED;
5440b88e 181 if (is_odd)
f1359c57 182 egpu.render_mode |= RENDER_INTERLACE_ODD;
5440b88e 183}
184
7fe57395 185void renderer_notify_screen_change(const struct psx_gpu_screen *screen)
e929dec5 186{
7fe57395 187 int x = screen->src_x, y = screen->src_y;
188 int vres = screen->vres;
2da2fc76 189 if (!gpu.state.enhancement_active || !egpu.enhancement_buf_ptr)
0b4038f8 190 return;
191
7fe57395 192 if (screen->y < 0)
193 vres -= screen->y;
194 update_enhancement_buf_scanouts(&egpu, x, y, screen->hres, vres);
e929dec5 195}
196
c1817bd9 197#include "../../frontend/plugin_lib.h"
198
90ca4913 199void renderer_set_config(const struct rearmed_cbs *cbs)
200{
9ee0fd5b 201 if (!initialized) {
202 initialize_psx_gpu(&egpu, gpu.vram);
203 initialized = 1;
204 }
fa56d360 205 if (cbs->pl_set_gpu_caps)
206 cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
a3c46b7f 207
ed8077c5 208 egpu.allow_dithering = cbs->dithering;
209 egpu.force_dithering = cbs->dithering >> 1;
210 /*
211 if (!egpu.allow_dithering) {
a3c46b7f 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 }
ed8077c5 222 */
0b4038f8 223
6ab6ab97 224 egpu.hack_disable_main = cbs->gpu_neon.enhancement_no_main;
225 egpu.hack_texture_adj = cbs->gpu_neon.enhancement_tex_adj;
0b4038f8 226 if (gpu.state.enhancement_enable) {
227 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
228 map_enhancement_buffer();
229 }
90ca4913 230}
0b4038f8 231
232// vim:ts=2:sw=2:expandtab