gpu: start doing some basic gpu timing
[pcsx_rearmed.git] / plugins / gpulib / gpu.h
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 #ifndef __GPULIB_GPU_H__
12 #define __GPULIB_GPU_H__
13
14 #include <stdint.h>
15
16 //#define RAW_FB_DISPLAY
17
18 #define gpu_log(fmt, ...) \
19   printf("%d:%03d: " fmt, *gpu.state.frame_count, *gpu.state.hcnt, ##__VA_ARGS__)
20
21 //#define log_anomaly gpu_log
22 #define log_anomaly(...)
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #define CMD_BUFFER_LEN          1024
29
30 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
31 #define HTOLE32(x) __builtin_bswap32(x)
32 #define HTOLE16(x) __builtin_bswap16(x)
33 #define LE32TOH(x) __builtin_bswap32(x)
34 #define LE16TOH(x) __builtin_bswap16(x)
35 #else
36 #define HTOLE32(x) (x)
37 #define HTOLE16(x) (x)
38 #define LE32TOH(x) (x)
39 #define LE16TOH(x) (x)
40 #endif
41
42 #define BIT(x) (1 << (x))
43
44 #define PSX_GPU_STATUS_DHEIGHT          BIT(19)
45 #define PSX_GPU_STATUS_PAL              BIT(20)
46 #define PSX_GPU_STATUS_RGB24            BIT(21)
47 #define PSX_GPU_STATUS_INTERLACE        BIT(22)
48 #define PSX_GPU_STATUS_BLANKING         BIT(23)
49 #define PSX_GPU_STATUS_IMG                      BIT(27)
50 #define PSX_GPU_STATUS_DMA(x)           ((x) << 29)
51 #define PSX_GPU_STATUS_DMA_MASK         (BIT(29) | BIT(30))
52
53 struct psx_gpu {
54   uint32_t cmd_buffer[CMD_BUFFER_LEN];
55   uint32_t regs[16];
56   uint16_t *vram;
57   uint32_t status;
58   uint32_t gp0;
59   uint32_t ex_regs[8];
60   struct {
61     int hres, vres;
62     int x, y, w, h;
63     int x1, x2;
64     int y1, y2;
65     int src_x, src_y;
66   } screen;
67   struct {
68     int x, y, w, h;
69     short int offset, is_read;
70   } dma, dma_start;
71   int cmd_len;
72   uint32_t zero;
73   struct {
74     uint32_t fb_dirty:1;
75     uint32_t old_interlace:1;
76     uint32_t allow_interlace:2;
77     uint32_t blanked:1;
78     uint32_t enhancement_enable:1;
79     uint32_t enhancement_active:1;
80     uint32_t enhancement_was_active:1;
81     uint32_t downscale_enable:1;
82     uint32_t downscale_active:1;
83     uint32_t dims_changed:1;
84     uint32_t *frame_count;
85     uint32_t *hcnt; /* hsync count */
86     struct {
87       uint32_t addr;
88       uint32_t cycles;
89       uint32_t frame;
90       uint32_t hcnt;
91     } last_list;
92     uint32_t last_vram_read_frame;
93     uint32_t w_out_old, h_out_old, status_vo_old;
94     short screen_centering_type;
95     short screen_centering_type_default;
96     int screen_centering_x;
97     int screen_centering_y;
98   } state;
99   struct {
100     int32_t set:3; /* -1 auto, 0 off, 1-3 fixed */
101     int32_t cnt:3; /* amount skipped in a row */
102     uint32_t active:1;
103     uint32_t allow:1;
104     uint32_t frame_ready:1;
105     const int *advice;
106     uint32_t last_flip_frame;
107     uint32_t pending_fill[3];
108   } frameskip;
109   void *(*get_enhancement_bufer)
110     (int *x, int *y, int *w, int *h, int *vram_h);
111   uint16_t *(*get_downscale_buffer)
112     (int *x, int *y, int *w, int *h, int *vram_h);
113   void *(*mmap)(unsigned int size);
114   void  (*munmap)(void *ptr, unsigned int size);
115   void  (*gpu_state_change)(int what); // psx_gpu_state
116 };
117
118 extern struct psx_gpu gpu;
119
120 extern const unsigned char cmd_lengths[256];
121
122 int do_cmd_list(uint32_t *list, int count, int *cycles, int *last_cmd);
123
124 struct rearmed_cbs;
125
126 int  renderer_init(void);
127 void renderer_finish(void);
128 void renderer_sync_ecmds(uint32_t * ecmds);
129 void renderer_update_caches(int x, int y, int w, int h, int state_changed);
130 void renderer_flush_queues(void);
131 void renderer_set_interlace(int enable, int is_odd);
132 void renderer_set_config(const struct rearmed_cbs *config);
133 void renderer_notify_res_change(void);
134 void renderer_notify_scanout_change(int x, int y);
135
136 int  vout_init(void);
137 int  vout_finish(void);
138 void vout_update(void);
139 void vout_blank(void);
140 void vout_set_config(const struct rearmed_cbs *config);
141
142 /* listing these here for correct linkage if rasterizer uses c++ */
143 struct GPUFreeze;
144
145 long GPUinit(void);
146 long GPUshutdown(void);
147 void GPUwriteDataMem(uint32_t *mem, int count);
148 long GPUdmaChain(uint32_t *rambase, uint32_t addr, uint32_t *progress_addr);
149 void GPUwriteData(uint32_t data);
150 void GPUreadDataMem(uint32_t *mem, int count);
151 uint32_t GPUreadData(void);
152 uint32_t GPUreadStatus(void);
153 void GPUwriteStatus(uint32_t data);
154 long GPUfreeze(uint32_t type, struct GPUFreeze *freeze);
155 void GPUupdateLace(void);
156 long GPUopen(unsigned long *disp, char *cap, char *cfg);
157 long GPUclose(void);
158 void GPUvBlank(int is_vblank, int lcf);
159 void GPUgetScreenInfo(int *y, int *base_hres);
160 void GPUrearmedCallbacks(const struct rearmed_cbs *cbs_);
161
162 #ifdef __cplusplus
163 }
164 #endif
165
166 #endif /* __GPULIB_GPU_H__ */