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