/*
* Scanout of CPU-drawn frames for a title: the title takes the GPU engines
* like Direct3D does and flips between two tiled 32 bpp front buffers at
* vertical blank. C port of xenon-forge's xbox360-video crate
* (https://github.com/landaire/xenon-forge, MIT/Apache-2.0).
*/
#ifndef XEX_VIDEO_H
#define XEX_VIDEO_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
/* Back buffer: tiled surface, 32x32-pixel tiles of 4 KB (see
* xex_video_tiled_index). Write-combined: write in address order. */
uint32_t *pixels;
/* Surface width in pixels (multiple of 32), for tiled indexing. */
int pitch;
/* Displayed window inside the surface (tile aligned). */
int window_x, window_y, window_width, window_height;
} xex_frame;
/* Takes the GPU and creates two front buffers of the current display
* size. Returns 0 on success, -1 on failure (see xex_video_error). */
int xex_video_init(void);
const char *xex_video_error(void);
/* Waits until the previous flip has been latched (so the back buffer is
* off screen) and returns the back buffer. */
xex_frame xex_video_begin(void);
/* Flips to the back buffer at the next vertical blank. */
void xex_video_present(void);
/* Optional trace of initialization and flips (diagnostics). */
extern void (*xex_video_trace)(const char *fmt, ...);
/* Gives the GPU back to the system (also done at exit). */
void xex_video_shutdown(void);
/* Word index of pixel (x, y) in a tiled surface of `pitch` pixels. The
* pixel word is B << 24 | G << 16 | R << 8. */
static inline int xex_video_tiled_index(int x, int y, int pitch)
{
return (((y >> 5) * 32 * pitch + ((x >> 5) << 10)
+ (x & 3) + ((y & 1) << 2) + (((x & 31) >> 2) << 3) + (((y & 31) >> 1) << 6))
^ ((y & 8) << 2));
}
static inline uint32_t xex_video_rgb(uint8_t r, uint8_t g, uint8_t b)
{
return ((uint32_t)b << 24) | ((uint32_t)g << 16) | ((uint32_t)r << 8);
}
#ifdef __cplusplus
}
#endif
#endif