various enhancement tweaks
[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 <sys/mman.h>
13
14extern const unsigned char cmd_lengths[256];
15#define command_lengths cmd_lengths
16
17static unsigned int *ex_regs;
18static int initialized;
19
20#define PCSX
21#define SET_Ex(r, v) \
22 ex_regs[r] = v
23
24#include "psx_gpu/psx_gpu.c"
25#include "psx_gpu/psx_gpu_parse.c"
26#include "../gpulib/gpu.h"
27
28static psx_gpu_struct egpu __attribute__((aligned(256)));
29
30int do_cmd_list(uint32_t *list, int count, int *last_cmd)
31{
32 int ret;
33
34 if (gpu.state.enhancement_active)
35 ret = gpu_parse_enhanced(&egpu, list, count * 4, (u32 *)last_cmd);
36 else
37 ret = gpu_parse(&egpu, list, count * 4, (u32 *)last_cmd);
38
39 ex_regs[1] &= ~0x1ff;
40 ex_regs[1] |= egpu.texture_settings & 0x1ff;
41 return ret;
42}
43
44#define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096 * 2)
45
46static void map_enhancement_buffer(void)
47{
48 // currently we use 4x 1024*1024 buffers instead of single 2048*1024
49 // to be able to reuse 1024-width code better (triangle setup,
50 // dithering phase, lines).
51 gpu.enhancement_bufer = gpu.mmap(ENHANCEMENT_BUF_SIZE);
52 if (gpu.enhancement_bufer == NULL)
53 fprintf(stderr, "failed to map enhancement buffer\n");
54 else
55 gpu.enhancement_bufer += 4096 / 2;
56 egpu.enhancement_buf_ptr = gpu.enhancement_bufer;
57}
58
59int renderer_init(void)
60{
61 if (gpu.vram != NULL) {
62 initialize_psx_gpu(&egpu, gpu.vram);
63 initialized = 1;
64 }
65
66 if (gpu.mmap != NULL && gpu.enhancement_bufer == NULL)
67 map_enhancement_buffer();
68
69 ex_regs = gpu.ex_regs;
70 return 0;
71}
72
73void renderer_finish(void)
74{
75 if (gpu.enhancement_bufer != NULL) {
76 gpu.enhancement_bufer -= 4096 / 2;
77 gpu.munmap(gpu.enhancement_bufer, ENHANCEMENT_BUF_SIZE);
78 }
79 gpu.enhancement_bufer = NULL;
80 egpu.enhancement_buf_ptr = NULL;
81 egpu.enhancement_current_buf_ptr = NULL;
82 initialized = 0;
83}
84
85static __attribute__((noinline)) void
86sync_enhancement_buffers(int x, int y, int w, int h)
87{
88 int xt = egpu.enhancement_x_threshold;
89 u16 *src, *dst;
90 int wb, i;
91
92 w += x & 7;
93 x &= ~7;
94 w = (w + 7) & ~7;
95 if (y + h > 512)
96 h = 512 - y;
97
98 for (i = 0; i < 4 && w > 0; i++) {
99 if (x < 512) {
100 wb = w;
101 if (x + w > 512)
102 wb = 512 - x;
103 src = gpu.vram + xt * i + y * 1024 + x;
104 dst = egpu.enhancement_buf_ptr +
105 (1024*1024 + xt * 2) * i + (y * 1024 + x) * 2;
106 scale2x_tiles8(dst, src, wb / 8, h);
107 }
108
109 x -= xt;
110 if (x < 0) {
111 w += x;
112 x = 0;
113 }
114 }
115}
116
117void renderer_sync_ecmds(uint32_t *ecmds)
118{
119 gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL);
120}
121
122void renderer_update_caches(int x, int y, int w, int h)
123{
124 update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
125 if (gpu.state.enhancement_active && !gpu.status.rgb24)
126 sync_enhancement_buffers(x, y, w, h);
127}
128
129void renderer_flush_queues(void)
130{
131 flush_render_block_buffer(&egpu);
132}
133
134void renderer_set_interlace(int enable, int is_odd)
135{
136 egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
137 if (enable)
138 egpu.render_mode |= RENDER_INTERLACE_ENABLED;
139 if (is_odd)
140 egpu.render_mode |= RENDER_INTERLACE_ODD;
141}
142
143void renderer_notify_res_change(void)
144{
145 // note: must keep it multiple of 8
146 egpu.enhancement_x_threshold = gpu.screen.hres;
147}
148
149#include "../../frontend/plugin_lib.h"
150
151void renderer_set_config(const struct rearmed_cbs *cbs)
152{
153 static int enhancement_was_on;
154
155 disable_main_render = cbs->gpu_neon.enhancement_no_main;
156 if (egpu.enhancement_buf_ptr != NULL && cbs->gpu_neon.enhancement_enable
157 && !enhancement_was_on)
158 {
159 sync_enhancement_buffers(0, 0, 1024, 512);
160 }
161 enhancement_was_on = cbs->gpu_neon.enhancement_enable;
162
163 if (!initialized) {
164 initialize_psx_gpu(&egpu, gpu.vram);
165 initialized = 1;
166 }
167
168 if (gpu.enhancement_bufer == NULL)
169 map_enhancement_buffer();
170}