nkeynes@31: /** nkeynes@315: * $Id: pvr2.h,v 1.25 2007-01-23 11:19:32 nkeynes Exp $ nkeynes@31: * nkeynes@103: * PVR2 (video chip) functions and macros. nkeynes@31: * nkeynes@31: * Copyright (c) 2005 Nathan Keynes. nkeynes@31: * nkeynes@31: * This program is free software; you can redistribute it and/or modify nkeynes@31: * it under the terms of the GNU General Public License as published by nkeynes@31: * the Free Software Foundation; either version 2 of the License, or nkeynes@31: * (at your option) any later version. nkeynes@31: * nkeynes@31: * This program is distributed in the hope that it will be useful, nkeynes@31: * but WITHOUT ANY WARRANTY; without even the implied warranty of nkeynes@31: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nkeynes@31: * GNU General Public License for more details. nkeynes@31: */ nkeynes@31: nkeynes@103: #include "dream.h" nkeynes@103: #include "mem.h" nkeynes@144: #include "display.h" nkeynes@103: #include "pvr2/pvr2mmio.h" nkeynes@103: #include nkeynes@1: nkeynes@189: typedef unsigned int pvraddr_t; nkeynes@189: typedef unsigned int pvr64addr_t; nkeynes@1: nkeynes@1: #define DISPMODE_DE 0x00000001 /* Display enable */ nkeynes@1: #define DISPMODE_SD 0x00000002 /* Scan double */ nkeynes@1: #define DISPMODE_COL 0x0000000C /* Colour mode */ nkeynes@1: #define DISPMODE_CD 0x08000000 /* Clock double */ nkeynes@1: nkeynes@94: #define COLFMT_RGB15 0x00000000 nkeynes@94: #define COLFMT_RGB16 0x00000004 nkeynes@94: #define COLFMT_RGB24 0x00000008 nkeynes@94: #define COLFMT_RGB32 0x0000000C nkeynes@1: nkeynes@1: #define DISPSIZE_MODULO 0x3FF00000 /* line skip +1 (32-bit words)*/ nkeynes@1: #define DISPSIZE_LPF 0x000FFC00 /* lines per field */ nkeynes@1: #define DISPSIZE_PPL 0x000003FF /* pixel words (32 bit) per line */ nkeynes@1: nkeynes@103: #define DISPCFG_VP 0x00000001 /* V-sync polarity */ nkeynes@103: #define DISPCFG_HP 0x00000002 /* H-sync polarity */ nkeynes@103: #define DISPCFG_I 0x00000010 /* Interlace enable */ nkeynes@103: #define DISPCFG_BS 0x000000C0 /* Broadcast standard */ nkeynes@103: #define DISPCFG_VO 0x00000100 /* Video output enable */ nkeynes@1: nkeynes@1: #define BS_NTSC 0x00000000 nkeynes@1: #define BS_PAL 0x00000040 nkeynes@1: #define BS_PALM 0x00000080 /* ? */ nkeynes@1: #define BS_PALN 0x000000C0 /* ? */ nkeynes@1: nkeynes@103: #define PVR2_RAM_BASE 0x05000000 nkeynes@103: #define PVR2_RAM_BASE_INT 0x04000000 nkeynes@103: #define PVR2_RAM_SIZE (8 * 1024 * 1024) nkeynes@103: #define PVR2_RAM_PAGES (PVR2_RAM_SIZE>>12) nkeynes@189: #define PVR2_RAM_MASK 0x7FFFFF nkeynes@103: nkeynes@222: #define RENDER_ZONLY 0 nkeynes@222: #define RENDER_NORMAL 1 /* Render non-modified polygons */ nkeynes@222: #define RENDER_CHEAPMOD 2 /* Render cheap-modified polygons */ nkeynes@222: #define RENDER_FULLMOD 3 /* Render the fully-modified version of the polygons */ nkeynes@222: nkeynes@1: void pvr2_next_frame( void ); nkeynes@19: void pvr2_set_base_address( uint32_t ); nkeynes@133: int pvr2_get_frame_count( void ); nkeynes@295: gboolean pvr2_save_next_scene( const gchar *filename ); nkeynes@56: nkeynes@103: #define PVR2_CMD_END_OF_LIST 0x00 nkeynes@103: #define PVR2_CMD_USER_CLIP 0x20 nkeynes@103: #define PVR2_CMD_POLY_OPAQUE 0x80 nkeynes@103: #define PVR2_CMD_MOD_OPAQUE 0x81 nkeynes@103: #define PVR2_CMD_POLY_TRANS 0x82 nkeynes@103: #define PVR2_CMD_MOD_TRANS 0x83 nkeynes@103: #define PVR2_CMD_POLY_PUNCHOUT 0x84 nkeynes@103: #define PVR2_CMD_VERTEX 0xE0 nkeynes@103: #define PVR2_CMD_VERTEX_LAST 0xF0 nkeynes@103: nkeynes@103: #define PVR2_POLY_TEXTURED 0x00000008 nkeynes@103: #define PVR2_POLY_SPECULAR 0x00000004 nkeynes@103: #define PVR2_POLY_SHADED 0x00000002 nkeynes@103: #define PVR2_POLY_UV_16BIT 0x00000001 nkeynes@103: nkeynes@133: #define PVR2_POLY_MODE_CLAMP_RGB 0x00200000 nkeynes@133: #define PVR2_POLY_MODE_ALPHA 0x00100000 nkeynes@133: #define PVR2_POLY_MODE_TEXALPHA 0x00080000 nkeynes@133: #define PVR2_POLY_MODE_FLIP_S 0x00040000 nkeynes@133: #define PVR2_POLY_MODE_FLIP_T 0x00020000 nkeynes@133: #define PVR2_POLY_MODE_CLAMP_S 0x00010000 nkeynes@133: #define PVR2_POLY_MODE_CLAMP_T 0x00008000 nkeynes@133: nkeynes@103: #define PVR2_TEX_FORMAT_ARGB1555 0x00000000 nkeynes@103: #define PVR2_TEX_FORMAT_RGB565 0x08000000 nkeynes@103: #define PVR2_TEX_FORMAT_ARGB4444 0x10000000 nkeynes@103: #define PVR2_TEX_FORMAT_YUV422 0x18000000 nkeynes@103: #define PVR2_TEX_FORMAT_BUMPMAP 0x20000000 nkeynes@103: #define PVR2_TEX_FORMAT_IDX4 0x28000000 nkeynes@103: #define PVR2_TEX_FORMAT_IDX8 0x30000000 nkeynes@103: nkeynes@103: #define PVR2_TEX_MIPMAP 0x80000000 nkeynes@103: #define PVR2_TEX_COMPRESSED 0x40000000 nkeynes@103: #define PVR2_TEX_FORMAT_MASK 0x38000000 nkeynes@103: #define PVR2_TEX_UNTWIDDLED 0x04000000 nkeynes@284: #define PVR2_TEX_STRIDE 0x02000000 nkeynes@103: nkeynes@108: #define PVR2_TEX_ADDR(x) ( ((x)&0x01FFFFF)<<3 ); nkeynes@103: #define PVR2_TEX_IS_MIPMAPPED(x) ( (x) & PVR2_TEX_MIPMAP ) nkeynes@103: #define PVR2_TEX_IS_COMPRESSED(x) ( (x) & PVR2_TEX_COMPRESSED ) nkeynes@103: #define PVR2_TEX_IS_TWIDDLED(x) (((x) & PVR2_TEX_UNTWIDDLED) == 0) nkeynes@284: #define PVR2_TEX_IS_STRIDE(x) (((x) & 0x06000000) == 0x06000000) nkeynes@103: nkeynes@103: /****************************** Frame Buffer *****************************/ nkeynes@103: nkeynes@103: /** nkeynes@103: * Write to the interleaved memory address space (aka 64-bit address space). nkeynes@103: */ nkeynes@103: void pvr2_vram64_write( sh4addr_t dest, char *src, uint32_t length ); nkeynes@103: nkeynes@103: /** nkeynes@282: * Write to the interleaved memory address space (aka 64-bit address space), nkeynes@282: * using a line length and stride. nkeynes@282: */ nkeynes@282: void pvr2_vram64_write_stride( sh4addr_t dest, char *src, uint32_t line_bytes, nkeynes@282: uint32_t line_stride_bytes, uint32_t line_count ); nkeynes@282: nkeynes@282: /** nkeynes@103: * Read from the interleaved memory address space (aka 64-bit address space) nkeynes@103: */ nkeynes@103: void pvr2_vram64_read( char *dest, sh4addr_t src, uint32_t length ); nkeynes@103: nkeynes@127: /** nkeynes@310: * Read a twiddled image from interleaved memory address space (aka 64-bit address nkeynes@310: * space), writing the image to the destination buffer in detwiddled format. nkeynes@310: * Width and height must be powers of 2 nkeynes@315: * This version reads 4-bit pixels. nkeynes@315: */ nkeynes@315: void pvr2_vram64_read_twiddled_4( char *dest, sh4addr_t src, uint32_t width, uint32_t height ); nkeynes@315: nkeynes@315: nkeynes@315: /** nkeynes@315: * Read a twiddled image from interleaved memory address space (aka 64-bit address nkeynes@315: * space), writing the image to the destination buffer in detwiddled format. nkeynes@315: * Width and height must be powers of 2 nkeynes@310: * This version reads 8-bit pixels. nkeynes@310: */ nkeynes@310: void pvr2_vram64_read_twiddled_8( char *dest, sh4addr_t src, uint32_t width, uint32_t height ); nkeynes@310: nkeynes@310: /** nkeynes@310: * Read a twiddled image from interleaved memory address space (aka 64-bit address nkeynes@310: * space), writing the image to the destination buffer in detwiddled format. nkeynes@310: * Width and height must be powers of 2, and src must be 16-bit aligned. nkeynes@310: * This version reads 16-bit pixels. nkeynes@310: */ nkeynes@310: void pvr2_vram64_read_twiddled_16( char *dest, sh4addr_t src, uint32_t width, uint32_t height ); nkeynes@310: nkeynes@310: /** nkeynes@284: * Read an image from the interleaved memory address space (aka 64-bit address space) nkeynes@284: * where the source and destination line sizes may differ. Note that both byte nkeynes@284: * counts must be a multiple of 4, and the src address must be 32-bit aligned. nkeynes@284: */ nkeynes@292: void pvr2_vram64_read_stride( char *dest, uint32_t dest_line_bytes, sh4addr_t srcaddr, nkeynes@292: uint32_t src_line_bytes, uint32_t line_count ); nkeynes@284: /** nkeynes@127: * Dump a portion of vram to a stream from the interleaved memory address nkeynes@127: * space. nkeynes@127: */ nkeynes@127: void pvr2_vram64_dump( sh4addr_t addr, uint32_t length, FILE *f ); nkeynes@127: nkeynes@315: nkeynes@315: /** nkeynes@315: * Describes a rendering buffer that's actually held in GL, for when we need nkeynes@315: * to fetch the bits back to vram. nkeynes@315: */ nkeynes@315: typedef struct pvr2_render_buffer { nkeynes@315: sh4addr_t render_addr; /* The actual address rendered to in pvr ram */ nkeynes@315: uint32_t size; /* Length of rendering region in bytes */ nkeynes@315: int width, height; nkeynes@315: int colour_format; nkeynes@315: } *pvr2_render_buffer_t; nkeynes@315: nkeynes@315: /** nkeynes@315: * Flush the indicated render buffer back to PVR. Caller is responsible for nkeynes@315: * tracking whether there is actually anything in the buffer. nkeynes@315: * nkeynes@315: * @param buffer A render buffer indicating the address to store to, and the nkeynes@315: * format the data needs to be in. nkeynes@315: * @param backBuffer TRUE to flush the back buffer, FALSE for nkeynes@315: * the front buffer. nkeynes@315: */ nkeynes@315: void pvr2_render_buffer_copy_to_sh4( pvr2_render_buffer_t buffer, nkeynes@315: gboolean backBuffer ); nkeynes@315: nkeynes@315: /** nkeynes@315: * Copy data from PVR ram into the GL render buffer. nkeynes@315: * nkeynes@315: * @param buffer A render buffer indicating the address to read from, and the nkeynes@315: * format the data is in. nkeynes@315: * @param backBuffer TRUE to write the back buffer, FALSE for nkeynes@315: * the front buffer. nkeynes@315: */ nkeynes@315: void pvr2_render_buffer_copy_from_sh4( pvr2_render_buffer_t buffer, nkeynes@315: gboolean backBuffer ); nkeynes@315: nkeynes@315: nkeynes@315: /** nkeynes@315: * Invalidate any caching on the supplied SH4 address nkeynes@315: */ nkeynes@315: gboolean pvr2_render_buffer_invalidate( sh4addr_t addr ); nkeynes@315: nkeynes@315: nkeynes@315: nkeynes@103: /**************************** Tile Accelerator ***************************/ nkeynes@56: /** nkeynes@56: * Process the data in the supplied buffer as an array of TA command lists. nkeynes@56: * Any excess bytes are held pending until a complete list is sent nkeynes@56: */ nkeynes@100: void pvr2_ta_write( char *buf, uint32_t length ); nkeynes@100: nkeynes@100: nkeynes@103: /** nkeynes@103: * (Re)initialize the tile accelerator in preparation for the next scene. nkeynes@103: * Normally called immediately before commencing polygon transmission. nkeynes@103: */ nkeynes@103: void pvr2_ta_init( void ); nkeynes@103: nkeynes@282: nkeynes@282: /****************************** YUV Converter ****************************/ nkeynes@282: nkeynes@282: /** nkeynes@282: * Process a block of YUV data. nkeynes@282: */ nkeynes@282: void pvr2_yuv_write( char *buf, uint32_t length ); nkeynes@282: nkeynes@282: /** nkeynes@282: * Initialize the YUV converter. nkeynes@282: */ nkeynes@284: void pvr2_yuv_init( uint32_t target_addr ); nkeynes@284: nkeynes@284: void pvr2_yuv_set_config( uint32_t config ); nkeynes@282: nkeynes@103: /********************************* Renderer ******************************/ nkeynes@103: nkeynes@103: /** nkeynes@103: * Initialize the rendering pipeline. nkeynes@103: * @return TRUE on success, FALSE on failure. nkeynes@103: */ nkeynes@103: gboolean pvr2_render_init( void ); nkeynes@103: nkeynes@103: /** nkeynes@103: * Render the current scene stored in PVR ram to the GL back buffer. nkeynes@103: */ nkeynes@100: void pvr2_render_scene( void ); nkeynes@103: nkeynes@103: /** nkeynes@103: * Display the scene rendered to the supplied address. nkeynes@103: * @return TRUE if there was an available render that was displayed, nkeynes@103: * otherwise FALSE (and no action was taken) nkeynes@103: */ nkeynes@103: gboolean pvr2_render_display_frame( uint32_t address ); nkeynes@103: nkeynes@219: nkeynes@219: void render_backplane( uint32_t *polygon, uint32_t width, uint32_t height, uint32_t mode ); nkeynes@219: nkeynes@219: void render_set_context( uint32_t *context, int render_mode ); nkeynes@219: nkeynes@219: void pvr2_render_tilebuffer( int width, int height, int clipx1, int clipy1, nkeynes@219: int clipx2, int clipy2 ); nkeynes@219: nkeynes@103: /****************************** Texture Cache ****************************/ nkeynes@103: nkeynes@103: /** nkeynes@108: * Initialize the texture cache. nkeynes@103: */ nkeynes@103: void texcache_init( void ); nkeynes@103: nkeynes@108: /** nkeynes@108: * Initialize the GL side of the texture cache (texture ids and such). nkeynes@108: */ nkeynes@108: void texcache_gl_init( void ); nkeynes@103: nkeynes@103: /** nkeynes@103: * Flush all textures and delete. The cache will be non-functional until nkeynes@103: * the next call to texcache_init(). This would typically be done if nkeynes@103: * switching GL targets. nkeynes@103: */ nkeynes@103: void texcache_shutdown( void ); nkeynes@103: nkeynes@103: /** nkeynes@103: * Evict all textures contained in the page identified by a texture address. nkeynes@103: */ nkeynes@103: void texcache_invalidate_page( uint32_t texture_addr ); nkeynes@103: nkeynes@103: /** nkeynes@103: * Return a texture ID for the texture specified at the supplied address nkeynes@103: * and given parameters (the same sequence of bytes could in theory have nkeynes@103: * multiple interpretations). We use the texture address as the primary nkeynes@103: * index, but allow for multiple instances at each address. The texture nkeynes@103: * will be bound to the GL_TEXTURE_2D target before being returned. nkeynes@103: * nkeynes@103: * If the texture has already been bound, return the ID to which it was nkeynes@103: * bound. Otherwise obtain an unused texture ID and set it up appropriately. nkeynes@103: */ nkeynes@103: GLuint texcache_get_texture( uint32_t texture_addr, int width, int height, nkeynes@103: int mode ); nkeynes@221: nkeynes@221: /************************* Rendering support macros **************************/ nkeynes@221: #define POLY1_DEPTH_MODE(poly1) ( pvr2_poly_depthmode[(poly1)>>29] ) nkeynes@221: #define POLY1_DEPTH_ENABLE(poly1) (((poly1)&0x04000000) == 0 ) nkeynes@221: #define POLY1_CULL_MODE(poly1) (((poly1)>>27)&0x03) nkeynes@221: #define POLY1_TEXTURED(poly1) (((poly1)&0x02000000)) nkeynes@221: #define POLY1_SPECULAR(poly1) (((poly1)&0x01000000)) nkeynes@221: #define POLY1_SHADE_MODEL(poly1) (((poly1)&0x00800000) ? GL_SMOOTH : GL_FLAT) nkeynes@221: #define POLY1_UV16(poly1) (((poly1)&0x00400000)) nkeynes@221: #define POLY1_SINGLE_TILE(poly1) (((poly1)&0x00200000)) nkeynes@221: nkeynes@221: #define POLY2_SRC_BLEND(poly2) ( pvr2_poly_srcblend[(poly2) >> 29] ) nkeynes@221: #define POLY2_DEST_BLEND(poly2) ( pvr2_poly_dstblend[((poly2)>>26)&0x07] ) nkeynes@282: #define POLY2_SRC_BLEND_TARGET(poly2) ((poly2)&0x02000000) nkeynes@282: #define POLY2_DEST_BLEND_TARGET(poly2) ((poly2)&0x01000000) nkeynes@221: #define POLY2_COLOUR_CLAMP_ENABLE(poly2) ((poly2)&0x00200000) nkeynes@221: #define POLY2_ALPHA_ENABLE(poly2) ((poly2)&0x001000000) nkeynes@221: #define POLY2_TEX_ALPHA_ENABLE(poly2) (((poly2)&0x00080000) == 0 ) nkeynes@308: #define POLY2_TEX_CLAMP_U(poly2) ((poly2)&0x00010000) nkeynes@308: #define POLY2_TEX_CLAMP_V(poly2) ((poly2)&0x00008000) nkeynes@221: #define POLY2_TEX_WIDTH(poly2) ( 1<< ((((poly2) >> 3) & 0x07 ) + 3) ) nkeynes@221: #define POLY2_TEX_HEIGHT(poly2) ( 1<< (((poly2) & 0x07 ) + 3) ) nkeynes@221: #define POLY2_TEX_BLEND(poly2) ( pvr2_poly_texblend[((poly2) >> 6)&0x03] ) nkeynes@221: extern int pvr2_poly_depthmode[8]; nkeynes@221: extern int pvr2_poly_srcblend[8]; nkeynes@221: extern int pvr2_poly_dstblend[8]; nkeynes@221: extern int pvr2_poly_texblend[4]; nkeynes@221: extern int pvr2_render_colour_format[8]; nkeynes@221: nkeynes@221: float halftofloat(uint16_t half);