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