dfxvideo: patch up some unsafe macros
[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
13#ifdef _WIN32
14#include <mman.h>
15#else
16#include <sys/mman.h>
17#endif
18
19extern const unsigned char cmd_lengths[256];
20#define command_lengths cmd_lengths
21
22static unsigned int *ex_regs;
23static int initialized;
24
25#define PCSX
26#define SET_Ex(r, v) \
27 ex_regs[r] = v
28
29#include "psx_gpu/psx_gpu.c"
30#include "psx_gpu/psx_gpu_parse.c"
31#include "../gpulib/gpu.h"
32
33static psx_gpu_struct egpu __attribute__((aligned(256)));
34
35int do_cmd_list(uint32_t *list, int count, int *last_cmd)
36{
37 int ret;
38
39 if (gpu.state.enhancement_active)
40 ret = gpu_parse_enhanced(&egpu, list, count * 4, (u32 *)last_cmd);
41 else
42 ret = gpu_parse(&egpu, list, count * 4, (u32 *)last_cmd);
43
44 ex_regs[1] &= ~0x1ff;
45 ex_regs[1] |= egpu.texture_settings & 0x1ff;
46 return ret;
47}
48
49#define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
50
51static uint16_t *get_enhancement_bufer(int *x, int *y, int *w, int *h,
52 int *vram_h)
53{
54 uint16_t *ret = select_enhancement_buf_ptr(&egpu, *x);
55
56 *x *= 2;
57 *y *= 2;
58 *w = *w * 2;
59 *h = *h * 2;
60 *vram_h = 1024;
61 return ret;
62}
63
64static void map_enhancement_buffer(void)
65{
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;
73 }
74 else {
75 egpu.enhancement_buf_ptr += 4096 / 2;
76 gpu.get_enhancement_bufer = get_enhancement_bufer;
77 }
78}
79
80int renderer_init(void)
81{
82 if (gpu.vram != NULL) {
83 initialize_psx_gpu(&egpu, gpu.vram);
84 initialized = 1;
85 }
86
87 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
88 map_enhancement_buffer();
89
90 ex_regs = gpu.ex_regs;
91 return 0;
92}
93
94void renderer_finish(void)
95{
96 if (egpu.enhancement_buf_ptr != NULL) {
97 egpu.enhancement_buf_ptr -= 4096 / 2;
98 gpu.munmap(egpu.enhancement_buf_ptr, ENHANCEMENT_BUF_SIZE);
99 }
100 egpu.enhancement_buf_ptr = NULL;
101 egpu.enhancement_current_buf_ptr = NULL;
102 initialized = 0;
103}
104
105static __attribute__((noinline)) void
106sync_enhancement_buffers(int x, int y, int w, int h)
107{
108 const int step_x = 1024 / sizeof(egpu.enhancement_buf_by_x16);
109 u16 *src, *dst;
110 int w1, fb_index;
111
112 w += x & (step_x - 1);
113 x &= ~(step_x - 1);
114 w = (w + step_x - 1) & ~(step_x - 1);
115 if (y + h > 512)
116 h = 512 - y;
117
118 while (w > 0) {
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])
122 break;
123
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);
128
129 x += w1 * step_x;
130 }
131}
132
133void renderer_sync_ecmds(uint32_t *ecmds)
134{
135 gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL);
136}
137
138void renderer_update_caches(int x, int y, int w, int h)
139{
140 update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
141 if (gpu.state.enhancement_active &&
142 !(gpu.status & PSX_GPU_STATUS_RGB24))
143 sync_enhancement_buffers(x, y, w, h);
144}
145
146void renderer_flush_queues(void)
147{
148 flush_render_block_buffer(&egpu);
149}
150
151void renderer_set_interlace(int enable, int is_odd)
152{
153 egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
154 if (enable)
155 egpu.render_mode |= RENDER_INTERLACE_ENABLED;
156 if (is_odd)
157 egpu.render_mode |= RENDER_INTERLACE_ODD;
158}
159
160void renderer_notify_res_change(void)
161{
162 // note: must keep it multiple of 8
163 if (egpu.enhancement_x_threshold != gpu.screen.hres)
164 {
165 egpu.enhancement_x_threshold = gpu.screen.hres;
166 update_enhancement_buf_table_from_hres(&egpu);
167 }
168}
169
170#include "../../frontend/plugin_lib.h"
171
172void renderer_set_config(const struct rearmed_cbs *cbs)
173{
174 static int enhancement_was_on;
175
176 disable_main_render = cbs->gpu_neon.enhancement_no_main;
177 if (egpu.enhancement_buf_ptr != NULL && cbs->gpu_neon.enhancement_enable
178 && !enhancement_was_on)
179 {
180 sync_enhancement_buffers(0, 0, 1024, 512);
181 }
182 enhancement_was_on = cbs->gpu_neon.enhancement_enable;
183
184 if (!initialized) {
185 initialize_psx_gpu(&egpu, gpu.vram);
186 initialized = 1;
187 }
188
189 if (gpu.mmap != NULL && egpu.enhancement_buf_ptr == NULL)
190 map_enhancement_buffer();
191 if (cbs->pl_set_gpu_caps)
192 cbs->pl_set_gpu_caps(GPU_CAP_SUPPORTS_2X);
193
194 egpu.use_dithering = cbs->gpu_neon.allow_dithering;
195 if(!egpu.use_dithering) {
196 egpu.dither_table[0] = dither_table_row(0, 0, 0, 0);
197 egpu.dither_table[1] = dither_table_row(0, 0, 0, 0);
198 egpu.dither_table[2] = dither_table_row(0, 0, 0, 0);
199 egpu.dither_table[3] = dither_table_row(0, 0, 0, 0);
200 } else {
201 egpu.dither_table[0] = dither_table_row(-4, 0, -3, 1);
202 egpu.dither_table[1] = dither_table_row(2, -2, 3, -1);
203 egpu.dither_table[2] = dither_table_row(-3, 1, -4, 0);
204 egpu.dither_table[3] = dither_table_row(3, -1, 2, -2);
205 }
206
207}
208void renderer_sync(void)
209{
210}
211void renderer_notify_update_lace(int updated)
212{
213}