| 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 | #define MAX_SPANS 512 |
| 19 | #define MAX_BLOCKS 64 |
| 20 | #define MAX_BLOCKS_PER_ROW 128 |
| 21 | |
| 22 | #define SPAN_DATA_BLOCKS_SIZE 32 |
| 23 | |
| 24 | #ifndef __ASSEMBLER__ |
| 25 | |
| 26 | #include "vector_types.h" |
| 27 | |
| 28 | typedef enum |
| 29 | { |
| 30 | PRIMITIVE_TYPE_TRIANGLE = 0, |
| 31 | PRIMITIVE_TYPE_SPRITE = 1, |
| 32 | PRIMITIVE_TYPE_LINE = 2, |
| 33 | PRIMITIVE_TYPE_UNKNOWN = 3 |
| 34 | } primitive_type_enum; |
| 35 | |
| 36 | typedef enum |
| 37 | { |
| 38 | TEXTURE_MODE_4BPP = 0, |
| 39 | TEXTURE_MODE_8BPP = 1, |
| 40 | TEXTURE_MODE_16BPP = 2 |
| 41 | } texture_mode_enum; |
| 42 | |
| 43 | typedef enum |
| 44 | { |
| 45 | BLEND_MODE_AVERAGE = 0, |
| 46 | BLEND_MODE_ADD = 1, |
| 47 | BLEND_MODE_SUBTRACT = 2, |
| 48 | BLEND_MODE_ADD_FOURTH = 3 |
| 49 | } blend_mode_enum; |
| 50 | |
| 51 | typedef enum |
| 52 | { |
| 53 | RENDER_FLAGS_MODULATE_TEXELS = 0x1, |
| 54 | RENDER_FLAGS_BLEND = 0x2, |
| 55 | RENDER_FLAGS_TEXTURE_MAP = 0x4, |
| 56 | RENDER_FLAGS_QUAD = 0x8, |
| 57 | RENDER_FLAGS_SHADE = 0x10, |
| 58 | } render_flags_enum; |
| 59 | |
| 60 | typedef enum |
| 61 | { |
| 62 | RENDER_STATE_DITHER = 0x8, |
| 63 | RENDER_STATE_MASK_EVALUATE = 0x20, |
| 64 | } render_state_enum; |
| 65 | |
| 66 | typedef enum |
| 67 | { |
| 68 | RENDER_INTERLACE_ENABLED = 0x1, |
| 69 | RENDER_INTERLACE_ODD = 0x2, |
| 70 | } render_mode_enum; |
| 71 | |
| 72 | typedef struct |
| 73 | { |
| 74 | u16 left_x; |
| 75 | u16 num_blocks; |
| 76 | u16 right_mask; |
| 77 | u16 y; |
| 78 | } edge_data_struct; |
| 79 | |
| 80 | // 64 (72) bytes total |
| 81 | typedef struct |
| 82 | { |
| 83 | // 16 bytes |
| 84 | union |
| 85 | { |
| 86 | vec_8x16u uv; |
| 87 | vec_8x16u texels; |
| 88 | vec_8x16u draw_mask; |
| 89 | }; |
| 90 | |
| 91 | // 24 bytes |
| 92 | union |
| 93 | { |
| 94 | struct |
| 95 | { |
| 96 | vec_8x8u r; |
| 97 | vec_8x8u g; |
| 98 | vec_8x8u b; |
| 99 | }; |
| 100 | |
| 101 | vec_8x16u pixels; |
| 102 | }; |
| 103 | |
| 104 | // 8 (16) bytes |
| 105 | u32 draw_mask_bits; |
| 106 | u16 *fb_ptr; |
| 107 | |
| 108 | // 16 bytes |
| 109 | vec_8x16u dither_offsets; |
| 110 | } block_struct; |
| 111 | |
| 112 | typedef struct render_block_handler_struct render_block_handler_struct; |
| 113 | |
| 114 | typedef struct |
| 115 | { |
| 116 | // 144 bytes |
| 117 | vec_8x16u test_mask; |
| 118 | |
| 119 | vec_4x32u uvrg; |
| 120 | vec_4x32u uvrg_dx; |
| 121 | vec_4x32u uvrg_dy; |
| 122 | |
| 123 | vec_4x32u u_block_span; |
| 124 | vec_4x32u v_block_span; |
| 125 | vec_4x32u r_block_span; |
| 126 | vec_4x32u g_block_span; |
| 127 | vec_4x32u b_block_span; |
| 128 | |
| 129 | u32 b; |
| 130 | u32 b_dy; |
| 131 | |
| 132 | u32 triangle_area; |
| 133 | |
| 134 | u32 texture_window_settings; |
| 135 | u32 current_texture_mask; |
| 136 | u32 viewport_mask; |
| 137 | u32 dirty_textures_4bpp_mask; |
| 138 | u32 dirty_textures_8bpp_mask; |
| 139 | u32 dirty_textures_8bpp_alternate_mask; |
| 140 | |
| 141 | u32 triangle_color; |
| 142 | u32 dither_table[4]; |
| 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 | u32 uvrgb_phase; |
| 152 | |
| 153 | u16 render_state_base; |
| 154 | u16 render_state; |
| 155 | |
| 156 | u16 num_spans; |
| 157 | u16 num_blocks; |
| 158 | |
| 159 | s16 viewport_start_x; |
| 160 | s16 viewport_start_y; |
| 161 | s16 viewport_end_x; |
| 162 | s16 viewport_end_y; |
| 163 | |
| 164 | u16 mask_msb; |
| 165 | |
| 166 | u8 triangle_winding; |
| 167 | |
| 168 | u8 display_area_draw_enable; |
| 169 | |
| 170 | u8 current_texture_page; |
| 171 | u8 last_8bpp_texture_page; |
| 172 | |
| 173 | u8 texture_mask_width; |
| 174 | u8 texture_mask_height; |
| 175 | u8 texture_window_x; |
| 176 | u8 texture_window_y; |
| 177 | |
| 178 | u8 primitive_type; |
| 179 | u8 render_mode; |
| 180 | |
| 181 | s16 offset_x; |
| 182 | s16 offset_y; |
| 183 | |
| 184 | u16 clut_settings; |
| 185 | u16 texture_settings; |
| 186 | |
| 187 | u32 *reciprocal_table_ptr; |
| 188 | |
| 189 | // enhancement stuff |
| 190 | u16 *enhancement_buf_ptr; // main alloc |
| 191 | u16 *enhancement_current_buf_ptr; // offset into above, 4 bufs |
| 192 | u32 saved_hres; |
| 193 | s16 saved_viewport_start_x; |
| 194 | s16 saved_viewport_start_y; |
| 195 | s16 saved_viewport_end_x; |
| 196 | s16 saved_viewport_end_y; |
| 197 | struct psx_gpu_scanout { |
| 198 | u16 x, y, w, h; |
| 199 | } enhancement_scanouts[4]; // 0-3 specifying which buf to use |
| 200 | u16 enhancement_scanout_eselect; // eviction selector |
| 201 | u16 enhancement_current_buf; |
| 202 | |
| 203 | // Align up to 64 byte boundary to keep the upcoming buffers cache line |
| 204 | // aligned, also make reachable with single immediate addition |
| 205 | u8 reserved_a[188 + 9*4 - 9*sizeof(void *)]; |
| 206 | |
| 207 | // 8KB |
| 208 | block_struct blocks[MAX_BLOCKS_PER_ROW]; |
| 209 | |
| 210 | // 14336 bytes |
| 211 | vec_4x32u span_uvrg_offset[MAX_SPANS]; |
| 212 | edge_data_struct span_edge_data[MAX_SPANS]; |
| 213 | u32 span_b_offset[MAX_SPANS]; |
| 214 | |
| 215 | u8 texture_4bpp_cache[32][256 * 256]; |
| 216 | u8 texture_8bpp_even_cache[16][256 * 256]; |
| 217 | u8 texture_8bpp_odd_cache[16][256 * 256]; |
| 218 | int use_dithering; |
| 219 | } psx_gpu_struct; |
| 220 | |
| 221 | typedef struct __attribute__((aligned(16))) |
| 222 | { |
| 223 | u8 u; |
| 224 | u8 v; |
| 225 | |
| 226 | u8 r; |
| 227 | u8 g; |
| 228 | u8 b; |
| 229 | |
| 230 | u8 reserved[3]; |
| 231 | |
| 232 | s16 x; |
| 233 | s16 y; |
| 234 | |
| 235 | u32 padding; |
| 236 | } vertex_struct; |
| 237 | |
| 238 | void render_block_fill(psx_gpu_struct *psx_gpu, u32 color, u32 x, u32 y, |
| 239 | u32 width, u32 height); |
| 240 | void render_block_copy(psx_gpu_struct *psx_gpu, u16 *source, u32 x, u32 y, |
| 241 | u32 width, u32 height, u32 pitch); |
| 242 | void render_block_move(psx_gpu_struct *psx_gpu, u32 source_x, u32 source_y, |
| 243 | u32 dest_x, u32 dest_y, u32 width, u32 height); |
| 244 | |
| 245 | void render_triangle(psx_gpu_struct *psx_gpu, vertex_struct *vertexes, |
| 246 | u32 flags); |
| 247 | void render_sprite(psx_gpu_struct *psx_gpu, s32 x, s32 y, u32 u, u32 v, |
| 248 | s32 *width, s32 *height, u32 flags, u32 color); |
| 249 | void render_line(psx_gpu_struct *gpu, vertex_struct *vertexes, u32 flags, |
| 250 | u32 color, int double_resolution); |
| 251 | |
| 252 | u32 texture_region_mask(s32 x1, s32 y1, s32 x2, s32 y2); |
| 253 | |
| 254 | void update_texture_8bpp_cache(psx_gpu_struct *psx_gpu); |
| 255 | void flush_render_block_buffer(psx_gpu_struct *psx_gpu); |
| 256 | |
| 257 | void initialize_psx_gpu(psx_gpu_struct *psx_gpu, u16 *vram); |
| 258 | u32 gpu_parse(psx_gpu_struct *psx_gpu, u32 *list, u32 size, |
| 259 | s32 *cpu_cycles_sum_out, s32 *cpu_cycles_last, u32 *last_command); |
| 260 | |
| 261 | void triangle_benchmark(psx_gpu_struct *psx_gpu); |
| 262 | |
| 263 | void compute_all_gradients(psx_gpu_struct * __restrict__ psx_gpu, |
| 264 | const vertex_struct * __restrict__ a, const vertex_struct * __restrict__ b, |
| 265 | const vertex_struct * __restrict__ c); |
| 266 | |
| 267 | #endif // __ASSEMBLER__ |
| 268 | #endif // PSX_GPU_H |