| 1 | /* |
| 2 | * Copyright (C) 2011 Gilead Kutnick "Exophase" <exophase@gmail.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation; either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #ifndef PSX_GPU_H |
| 16 | #define PSX_GPU_H |
| 17 | |
| 18 | #include "vector_types.h" |
| 19 | |
| 20 | typedef enum |
| 21 | { |
| 22 | PRIMITIVE_TYPE_TRIANGLE = 0, |
| 23 | PRIMITIVE_TYPE_SPRITE = 1, |
| 24 | PRIMITIVE_TYPE_LINE = 2, |
| 25 | PRIMITIVE_TYPE_UNKNOWN = 3 |
| 26 | } primitive_type_enum; |
| 27 | |
| 28 | typedef enum |
| 29 | { |
| 30 | TEXTURE_MODE_4BPP = 0, |
| 31 | TEXTURE_MODE_8BPP = 1, |
| 32 | TEXTURE_MODE_16BPP = 2 |
| 33 | } texture_mode_enum; |
| 34 | |
| 35 | typedef enum |
| 36 | { |
| 37 | BLEND_MODE_AVERAGE = 0, |
| 38 | BLEND_MODE_ADD = 1, |
| 39 | BLEND_MODE_SUBTRACT = 2, |
| 40 | BLEND_MODE_ADD_FOURTH = 3 |
| 41 | } blend_mode_enum; |
| 42 | |
| 43 | typedef enum |
| 44 | { |
| 45 | RENDER_FLAGS_MODULATE_TEXELS = 0x1, |
| 46 | RENDER_FLAGS_BLEND = 0x2, |
| 47 | RENDER_FLAGS_TEXTURE_MAP = 0x4, |
| 48 | RENDER_FLAGS_QUAD = 0x8, |
| 49 | RENDER_FLAGS_SHADE = 0x10, |
| 50 | } render_flags_enum; |
| 51 | |
| 52 | typedef enum |
| 53 | { |
| 54 | RENDER_STATE_DITHER = 0x8, |
| 55 | RENDER_STATE_MASK_EVALUATE = 0x20, |
| 56 | } render_state_enum; |
| 57 | |
| 58 | typedef enum |
| 59 | { |
| 60 | RENDER_INTERLACE_ENABLED = 0x1, |
| 61 | RENDER_INTERLACE_ODD = 0x2, |
| 62 | } render_mode_enum; |
| 63 | |
| 64 | typedef struct |
| 65 | { |
| 66 | u16 left_x; |
| 67 | u16 num_blocks; |
| 68 | u16 right_mask; |
| 69 | u16 y; |
| 70 | } edge_data_struct; |
| 71 | |
| 72 | // 64 (72) bytes total |
| 73 | typedef struct |
| 74 | { |
| 75 | // 16 bytes |
| 76 | union |
| 77 | { |
| 78 | vec_8x16u uv; |
| 79 | vec_8x16u texels; |
| 80 | vec_8x16u draw_mask; |
| 81 | }; |
| 82 | |
| 83 | // 24 bytes |
| 84 | union |
| 85 | { |
| 86 | struct |
| 87 | { |
| 88 | vec_8x8u r; |
| 89 | vec_8x8u g; |
| 90 | vec_8x8u b; |
| 91 | }; |
| 92 | |
| 93 | vec_8x16u pixels; |
| 94 | }; |
| 95 | |
| 96 | // 8 (16) bytes |
| 97 | u32 draw_mask_bits; |
| 98 | u16 *fb_ptr; |
| 99 | |
| 100 | // 16 bytes |
| 101 | vec_8x16u dither_offsets; |
| 102 | } block_struct; |
| 103 | |
| 104 | #define MAX_SPANS 512 |
| 105 | #define MAX_BLOCKS 64 |
| 106 | #define MAX_BLOCKS_PER_ROW 128 |
| 107 | |
| 108 | #define SPAN_DATA_BLOCKS_SIZE 32 |
| 109 | |
| 110 | typedef struct render_block_handler_struct render_block_handler_struct; |
| 111 | |
| 112 | typedef struct |
| 113 | { |
| 114 | // 144 bytes |
| 115 | vec_8x16u test_mask; |
| 116 | |
| 117 | vec_4x32u uvrg; |
| 118 | vec_4x32u uvrg_dx; |
| 119 | vec_4x32u uvrg_dy; |
| 120 | |
| 121 | vec_4x32u u_block_span; |
| 122 | vec_4x32u v_block_span; |
| 123 | vec_4x32u r_block_span; |
| 124 | vec_4x32u g_block_span; |
| 125 | vec_4x32u b_block_span; |
| 126 | |
| 127 | u32 b; |
| 128 | u32 b_dy; |
| 129 | |
| 130 | u32 triangle_area; |
| 131 | |
| 132 | u32 texture_window_settings; |
| 133 | u32 current_texture_mask; |
| 134 | u32 viewport_mask; |
| 135 | u32 dirty_textures_4bpp_mask; |
| 136 | u32 dirty_textures_8bpp_mask; |
| 137 | u32 dirty_textures_8bpp_alternate_mask; |
| 138 | |
| 139 | u32 triangle_color; |
| 140 | u32 dither_table[4]; |
| 141 | |
| 142 | u32 uvrgb_phase; |
| 143 | |
| 144 | struct render_block_handler_struct *render_block_handler; |
| 145 | void *texture_page_ptr; |
| 146 | void *texture_page_base; |
| 147 | u16 *clut_ptr; |
| 148 | u16 *vram_ptr; |
| 149 | u16 *vram_out_ptr; |
| 150 | |
| 151 | u16 render_state_base; |
| 152 | u16 render_state; |
| 153 | |
| 154 | u16 num_spans; |
| 155 | u16 num_blocks; |
| 156 | |
| 157 | s16 viewport_start_x; |
| 158 | s16 viewport_start_y; |
| 159 | s16 viewport_end_x; |
| 160 | s16 viewport_end_y; |
| 161 | |
| 162 | u16 mask_msb; |
| 163 | |
| 164 | u8 triangle_winding; |
| 165 | |
| 166 | u8 display_area_draw_enable; |
| 167 | |
| 168 | u8 current_texture_page; |
| 169 | u8 last_8bpp_texture_page; |
| 170 | |
| 171 | u8 texture_mask_width; |
| 172 | u8 texture_mask_height; |
| 173 | u8 texture_window_x; |
| 174 | u8 texture_window_y; |
| 175 | |
| 176 | u8 primitive_type; |
| 177 | u8 render_mode; |
| 178 | |
| 179 | s16 offset_x; |
| 180 | s16 offset_y; |
| 181 | |
| 182 | u16 clut_settings; |
| 183 | u16 texture_settings; |
| 184 | |
| 185 | u32 *reciprocal_table_ptr; |
| 186 | |
| 187 | // enhancement stuff |
| 188 | u16 *enhancement_buf_ptr; |
| 189 | u16 *enhancement_current_buf_ptr; |
| 190 | u32 enhancement_x_threshold; |
| 191 | s16 saved_viewport_start_x; |
| 192 | s16 saved_viewport_start_y; |
| 193 | s16 saved_viewport_end_x; |
| 194 | s16 saved_viewport_end_y; |
| 195 | u8 enhancement_buf_by_x16[64]; |
| 196 | |
| 197 | // Align up to 64 byte boundary to keep the upcoming buffers cache line |
| 198 | // aligned, also make reachable with single immediate addition |
| 199 | u8 reserved_a[160]; |
| 200 | |
| 201 | // 8KB |
| 202 | block_struct blocks[MAX_BLOCKS_PER_ROW]; |
| 203 | |
| 204 | // 14336 bytes |
| 205 | vec_4x32u span_uvrg_offset[MAX_SPANS]; |
| 206 | edge_data_struct span_edge_data[MAX_SPANS]; |
| 207 | u32 span_b_offset[MAX_SPANS]; |
| 208 | |
| 209 | u8 texture_4bpp_cache[32][256 * 256]; |
| 210 | u8 texture_8bpp_even_cache[16][256 * 256]; |
| 211 | u8 texture_8bpp_odd_cache[16][256 * 256]; |
| 212 | int use_dithering; |
| 213 | } psx_gpu_struct; |
| 214 | |
| 215 | typedef struct __attribute__((aligned(16))) |
| 216 | { |
| 217 | u8 u; |
| 218 | u8 v; |
| 219 | |
| 220 | u8 r; |
| 221 | u8 g; |
| 222 | u8 b; |
| 223 | |
| 224 | u8 reserved[3]; |
| 225 | |
| 226 | s16 x; |
| 227 | s16 y; |
| 228 | |
| 229 | u32 padding; |
| 230 | } vertex_struct; |
| 231 | |
| 232 | void render_block_fill(psx_gpu_struct *psx_gpu, u32 color, u32 x, u32 y, |
| 233 | u32 width, u32 height); |
| 234 | void render_block_copy(psx_gpu_struct *psx_gpu, u16 *source, u32 x, u32 y, |
| 235 | u32 width, u32 height, u32 pitch); |
| 236 | void render_block_move(psx_gpu_struct *psx_gpu, u32 source_x, u32 source_y, |
| 237 | u32 dest_x, u32 dest_y, u32 width, u32 height); |
| 238 | |
| 239 | void render_triangle(psx_gpu_struct *psx_gpu, vertex_struct *vertexes, |
| 240 | u32 flags); |
| 241 | void render_sprite(psx_gpu_struct *psx_gpu, s32 x, s32 y, u32 u, u32 v, |
| 242 | s32 width, s32 height, u32 flags, u32 color); |
| 243 | void render_line(psx_gpu_struct *gpu, vertex_struct *vertexes, u32 flags, |
| 244 | u32 color, int double_resolution); |
| 245 | |
| 246 | u32 texture_region_mask(s32 x1, s32 y1, s32 x2, s32 y2); |
| 247 | |
| 248 | void update_texture_8bpp_cache(psx_gpu_struct *psx_gpu); |
| 249 | void flush_render_block_buffer(psx_gpu_struct *psx_gpu); |
| 250 | |
| 251 | void initialize_psx_gpu(psx_gpu_struct *psx_gpu, u16 *vram); |
| 252 | u32 gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size, u32 *last_command); |
| 253 | |
| 254 | void triangle_benchmark(psx_gpu_struct *psx_gpu); |
| 255 | |
| 256 | void compute_all_gradients(psx_gpu_struct * __restrict__ psx_gpu, |
| 257 | const vertex_struct * __restrict__ a, const vertex_struct * __restrict__ b, |
| 258 | const vertex_struct * __restrict__ c); |
| 259 | |
| 260 | #endif |
| 261 | |