foxygit / xexkit Log in
commits tags

/runtime/xex_video.h · 2.95 KB

raw
/*
 * 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. On a progressive output with
 * a display controller (a console, not Xenia) they have the output's size
 * (e.g. 1920x1080) and are shown 1:1 with the display scaler off; else the
 * kernel's surface (often 640x480) is used and its scaler stretches it.
 * Returns 0 on success, -1 on failure (see xex_video_error). */
int xex_video_init(void);

/* Set to 0 before xex_video_init to always use the kernel's surface. */
extern int xex_video_native;
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, ...);

/* Diagnostics: traces the display controller registers. */
void xex_video_dump_registers(const char *label);
/* Diagnostics: raw display register access (byte offsets) and a few LUT entries. */
uint32_t xex_video_read_register(uint32_t offset);
void xex_video_write_register(uint32_t offset, uint32_t value);
void xex_video_dump_lut(void);
/* Nonzero: program the scaler with VdInitializeScalerCommandBuffer at init
 * (hangs on hardware so far). */
extern int xex_video_scaler_via_kernel;

/* Nonzero while the Guide (XAM's surface) is on screen. Don't end the
 * title by itself then: XAM waits for a swap that never comes. */
int xex_video_guide_visible(void);

/* 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 | A (opaque: A = 0xff). */
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) | 0xffu;
}

#ifdef __cplusplus
}
#endif

#endif