psx_gpu: start handling vram loads/moves for enhancement
[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>
50f9355a 12#include <sys/mman.h>
90ca4913 13
652c6b8b 14extern const unsigned char cmd_lengths[256];
15#define command_lengths cmd_lengths
16
b243416b 17static unsigned int *ex_regs;
18
19#define PCSX
20#define SET_Ex(r, v) \
21 ex_regs[r] = v
22
90ca4913 23#include "psx_gpu/psx_gpu.c"
90ca4913 24#include "psx_gpu/psx_gpu_parse.c"
62d7fa95 25#include "../gpulib/gpu.h"
90ca4913 26
27static psx_gpu_struct egpu __attribute__((aligned(256)));
28
b243416b 29int do_cmd_list(uint32_t *list, int count, int *last_cmd)
90ca4913 30{
c1817bd9 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);
b243416b 37
38 ex_regs[1] &= ~0x1ff;
39 ex_regs[1] |= egpu.texture_settings & 0x1ff;
40 return ret;
90ca4913 41}
42
50f9355a 43#define ENHANCEMENT_BUF_SIZE (1024 * 1024 * 2 * 4 + 4096)
44
90ca4913 45int renderer_init(void)
46{
47 initialize_psx_gpu(&egpu, gpu.vram);
b243416b 48 ex_regs = gpu.ex_regs;
e929dec5 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).
50f9355a 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) {
e929dec5 57 printf("OOM for enhancement buffer\n");
50f9355a 58 gpu.enhancement_bufer = NULL;
59 }
e929dec5 60 }
61 egpu.enhancement_buf_ptr = gpu.enhancement_bufer;
62
90ca4913 63 return 0;
64}
65
e929dec5 66void renderer_finish(void)
67{
50f9355a 68 if (gpu.enhancement_bufer != NULL)
69 munmap(gpu.enhancement_bufer, ENHANCEMENT_BUF_SIZE);
e929dec5 70 gpu.enhancement_bufer = NULL;
71 egpu.enhancement_buf_ptr = NULL;
72}
73
50f9355a 74static __attribute__((noinline)) void
75sync_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
90ca4913 106void renderer_sync_ecmds(uint32_t *ecmds)
107{
b243416b 108 gpu_parse(&egpu, ecmds + 1, 6 * 4, NULL);
90ca4913 109}
110
05740673 111void renderer_update_caches(int x, int y, int w, int h)
90ca4913 112{
05740673 113 update_texture_cache_region(&egpu, x, y, x + w - 1, y + h - 1);
50f9355a 114 if (gpu.state.enhancement_active && !gpu.status.rgb24)
115 sync_enhancement_buffers(x, y, w, h);
90ca4913 116}
117
118void renderer_flush_queues(void)
119{
120 flush_render_block_buffer(&egpu);
121}
122
5440b88e 123void renderer_set_interlace(int enable, int is_odd)
124{
f1359c57 125 egpu.render_mode &= ~(RENDER_INTERLACE_ENABLED|RENDER_INTERLACE_ODD);
5440b88e 126 if (enable)
f1359c57 127 egpu.render_mode |= RENDER_INTERLACE_ENABLED;
5440b88e 128 if (is_odd)
f1359c57 129 egpu.render_mode |= RENDER_INTERLACE_ODD;
5440b88e 130}
131
e929dec5 132void renderer_notify_res_change(void)
133{
50f9355a 134 // note: must keep it multiple of 8
e929dec5 135 egpu.enhancement_x_threshold = gpu.screen.hres;
136}
137
c1817bd9 138#include "../../frontend/plugin_lib.h"
139
90ca4913 140void renderer_set_config(const struct rearmed_cbs *cbs)
141{
50f9355a 142 static int enhancement_was_on;
143
c1817bd9 144 disable_main_render = cbs->gpu_neon.enhancement_no_main;
50f9355a 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;
90ca4913 151}