Jo Engine 2026.11.6
Jo Sega Saturn Engine
Loading...
Searching...
No Matches
vdp1_command_pipeline.h
Go to the documentation of this file.
1/*
2** Jo Sega Saturn Engine
3** Copyright (c) 2012-2026, Johannes Fetz (johannesfetz@gmail.com)
4** All rights reserved.
5**
6** Redistribution and use in source and binary forms, with or without
7** modification, are permitted provided that the following conditions are met:
8** * Redistributions of source code must retain the above copyright
9** notice, this list of conditions and the following disclaimer.
10** * Redistributions in binary form must reproduce the above copyright
11** notice, this list of conditions and the following disclaimer in the
12** documentation and/or other materials provided with the distribution.
13** * Neither the name of the Johannes Fetz nor the
14** names of its contributors may be used to endorse or promote products
15** derived from this software without specific prior written permission.
16**
17** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
34
35#ifndef __JO_VDP1_COMMAND_PIPELINE_H__
36# define __JO_VDP1_COMMAND_PIPELINE_H__
37
38/*
39** VDP1 VRAM layout:
40**
41** 0x00000 : jump command to the table to draw
42** 0x00020 : command table A
43** 0x08000 : command table B
44** 0x10000 : textures (JO_VDP1_TEXTURE_DEF_BASE_ADDRESS)
45** 0x7E4F8 : gouraud tables of the sprites, 2 sets of 32 (JO_VDP1_USER_AREA_END_ADDR, jo_set_gouraud_shading_colors())
46** 0x7E700 : gouraud tables of the clipped and subdivided 3D polygons
47** 0x7FF00 : gouraud tables used for 3D lighting
48*/
49
56#ifndef JO_VDP1_MAX_COMMANDS
57# define JO_VDP1_MAX_COMMANDS (1000)
58#endif
59
61typedef struct
62{
63 unsigned short ctrl;
64 unsigned short link;
65 unsigned short pmod;
66 unsigned short colr;
67 unsigned short srca;
68 unsigned short size;
69 short xa;
70 short ya;
71 short xb;
72 short yb;
73 short xc;
74 short yc;
75 short xd;
76 short yd;
77 unsigned short grda;
78 unsigned short reserved;
80
82# define JO_VDP1_CMD_NORMAL_SPRITE (0x0000)
83# define JO_VDP1_CMD_SCALED_SPRITE (0x0001)
84# define JO_VDP1_CMD_DISTORTED_SPRITE (0x0002)
85# define JO_VDP1_CMD_POLYGON (0x0004)
86# define JO_VDP1_CMD_POLYLINE (0x0005)
87# define JO_VDP1_CMD_LINE (0x0006)
88# define JO_VDP1_CMD_USER_CLIP (0x0008)
89# define JO_VDP1_CMD_SYSTEM_CLIP (0x0009)
90# define JO_VDP1_CMD_LOCAL_COORD (0x000A)
91# define JO_VDP1_CMD_SKIP_ASSIGN (0x5000)
92# define JO_VDP1_CMD_END (0x8000)
93
94/*
95** Drawing function (low bits of the "direction" attribute)
96*/
98# define JO_VDP1_FUNCTION_SPRITE (1)
100# define JO_VDP1_FUNCTION_TEXTURE (2)
102# define JO_VDP1_FUNCTION_POLYGON (4)
104# define JO_VDP1_FUNCTION_POLYLINE (5)
106# define JO_VDP1_FUNCTION_LINE (6)
108# define JO_VDP1_FUNCTION_USER_CLIP (8)
110# define JO_VDP1_FUNCTION_SYSTEM_CLIP (9)
112# define JO_VDP1_FUNCTION_LOCAL_COORDINATES (10)
113
115# define JO_VDP1_DIRECTION_FUNCTION_MASK (0x0f)
117# define JO_VDP1_DIRECTION_FLIP_MASK (0x30)
119# define JO_VDP1_H_FLIP (0x10)
120
121/*
122** Draw mode (CMDPMOD register layout)
123*/
125# define JO_VDP1_MSB_ON (1 << 15)
127# define JO_VDP1_HIGH_SPEED_SHRINK (1 << 12)
131# define JO_VDP1_PRE_CLIPPING_DISABLE (1 << 11)
133# define JO_VDP1_NO_WINDOW (0)
135# define JO_VDP1_WINDOW_IN (2 << 9)
137# define JO_VDP1_WINDOW_OUT (3 << 9)
139# define JO_VDP1_MESH (1 << 8)
141# define JO_VDP1_NO_MESH (0)
143# define JO_VDP1_END_CODE_DISABLE (1 << 7)
145# define JO_VDP1_RGB_END_CODE (0x7FFF)
147# define JO_VDP1_TRANSPARENT_PIXEL_DISABLE (1 << 6)
149# define JO_VDP1_COLOR_MODE_16_BANK (0 << 3)
151# define JO_VDP1_COLOR_MODE_16_LOOKUP (1 << 3)
153# define JO_VDP1_COLOR_MODE_64_BANK (2 << 3)
155# define JO_VDP1_COLOR_MODE_128_BANK (3 << 3)
157# define JO_VDP1_COLOR_MODE_256_BANK (4 << 3)
159# define JO_VDP1_COLOR_MODE_RGB (5 << 3)
161# define JO_VDP1_COLOR_MODE_MASK (7 << 3)
163# define JO_VDP1_COLOR_CALC_REPLACE (0)
165# define JO_VDP1_COLOR_CALC_SHADOW (1)
167# define JO_VDP1_COLOR_CALC_HALF_LUMINANCE (2)
169# define JO_VDP1_COLOR_CALC_HALF_TRANSPARENT (3)
171# define JO_VDP1_COLOR_CALC_GOURAUD (4)
173# define JO_VDP1_COLOR_CALC_MASK (7)
175# define JO_VDP1_NO_GOURAUD (0)
177# define JO_VDP1_NO_PALETTE (0)
178
179/*
180** Texture size: bytes = width * height * 4 >> shift
181*/
183# define JO_VDP1_TEXTURE_SIZE_SHIFT_16_COLORS (3)
185# define JO_VDP1_TEXTURE_SIZE_SHIFT_256_COLORS (2)
187# define JO_VDP1_TEXTURE_SIZE_SHIFT_32K_COLORS (1)
188
190# define JO_VDP1_OPTION_USE_TEXTURE (1 << 2)
191
192/*
193** Direction attribute: drawing function | flip | (options << 16) | (draw mode bits << 24)
194*/
196# define JO_VDP1_SPRITE_NO_FLIP ((0) | JO_VDP1_FUNCTION_TEXTURE | (JO_VDP1_OPTION_USE_TEXTURE << 16))
198# define JO_VDP1_SPRITE_H_FLIP ((1 << 4) | JO_VDP1_FUNCTION_TEXTURE | (JO_VDP1_OPTION_USE_TEXTURE << 16))
200# define JO_VDP1_SPRITE_V_FLIP ((1 << 5) | JO_VDP1_FUNCTION_TEXTURE | (JO_VDP1_OPTION_USE_TEXTURE << 16))
202# define JO_VDP1_SPRITE_HV_FLIP ((3 << 4) | JO_VDP1_FUNCTION_TEXTURE | (JO_VDP1_OPTION_USE_TEXTURE << 16))
204# define JO_VDP1_POLYGON ((int)(JO_VDP1_FUNCTION_POLYGON | ((unsigned int)(JO_VDP1_END_CODE_DISABLE | JO_VDP1_TRANSPARENT_PIXEL_DISABLE) << 24)))
206# define JO_VDP1_POLYLINE ((int)(JO_VDP1_FUNCTION_POLYLINE | ((unsigned int)(JO_VDP1_END_CODE_DISABLE | JO_VDP1_TRANSPARENT_PIXEL_DISABLE) << 24)))
208# define JO_VDP1_LINE ((int)(JO_VDP1_FUNCTION_LINE | ((unsigned int)(JO_VDP1_END_CODE_DISABLE | JO_VDP1_TRANSPARENT_PIXEL_DISABLE) << 24)))
209
213# define JO_VDP1_DIRECTION_COMMAND(DIRECTION) ((DIRECTION) & (JO_VDP1_DIRECTION_FUNCTION_MASK | JO_VDP1_DIRECTION_FLIP_MASK))
217# define JO_VDP1_DIRECTION_OPTIONS(DIRECTION) (((DIRECTION) >> 16) & 0x1c)
221# define JO_VDP1_DIRECTION_DRAW_MODE(DIRECTION) (((DIRECTION) >> 24) & (JO_VDP1_END_CODE_DISABLE | JO_VDP1_TRANSPARENT_PIXEL_DISABLE))
222
224typedef struct
225{
226 unsigned short texture_id; // Sprite id (__jo_sprite_def index)
227 unsigned short draw_mode; // JO_VDP1_COLOR_MODE_xxx | JO_VDP1_COLOR_CALC_xxx | ...
228 unsigned short color; // RGB color or palette
229 unsigned short gouraud_table; // Gouraud table address / 8
230 unsigned short direction; // JO_VDP1_FUNCTION_xxx | flip
232
240# define JO_VDP1_SPRITE_ATTRIBUTE(TEXTURE, COLOR, GOURAUD, DRAW_MODE, DIRECTION) \
241 {(TEXTURE), (DRAW_MODE) | JO_VDP1_DIRECTION_DRAW_MODE(DIRECTION), (COLOR), (GOURAUD), JO_VDP1_DIRECTION_COMMAND(DIRECTION)}
242
244typedef struct
245{
249 jo_fixed scale_x; // Scale (negative value: perspective scale)
250 jo_fixed scale_y; // Vertical scale (only for __jo_vdp1_draw_sprite_hv())
252
254# define JO_VDP1_LIGHT_TABLE_OFFSET (0x7FF00)
257# define JO_VDP1_CLIP_GOURAUD_TABLE_OFFSET (0x7E700)
259# define JO_VDP1_CLIP_GOURAUD_TABLE_COUNT (768)
260
262typedef struct
263{
264 short left;
265 short top;
266 short right;
267 short bottom;
268 short center_x;
269 short center_y;
271 unsigned short clip_mode; // Draw mode bits of the 3D polygons: JO_VDP1_WINDOW_IN (clipped to the window), 0
272 short origin_x; // The local coordinates of the window (its commands are relative to them): its top
273 short origin_y; // left corner when it is a screen of its own (see screen), else 0
274 bool screen; // Its rectangle is its screen (clipped, windows apart: see jo_3d_window_clip())
276
279# define JO_VDP1_WINDOW_COUNT (4)
280
283# define JO_VDP1_OFF_SCREEN_WINDOW (3)
284
286extern int __jo_vdp1_current_window;
287
290
292extern unsigned short __jo_vdp1_display_level;
293
300
302# define JO_VDP1_MAX_KEY (0xffff)
303
305extern int __jo_vdp1_depth_bias;
306
309
311extern bool __jo_vdp1_wireframe;
313extern bool __jo_vdp1_windows_apart;
314
319{
320 // Variable shift: computed once instead of for each sprite (the SH-2 can only shift by 1, 2, 8 or 16 bits)
322}
323
334void __jo_vdp1_set_window(const short left, const short top, const short right, const short bottom,
335 const short zlimit, const short center_x, const short center_y);
336
343void jo_3d_set_depth_limit(const int z);
344
346extern bool __jo_vdp1_frame_clear;
347
359static __jo_force_inline void jo_3d_set_frame_clear(const bool enabled)
360{
361 __jo_vdp1_frame_clear = enabled;
362}
363
365void __jo_vdp1_set_window_clip(const bool clip);
366
370static __jo_force_inline void __jo_vdp1_select_window(const int window)
371{
372 __jo_vdp1_current_window = (window >= 0 && window < JO_VDP1_WINDOW_COUNT) ? window : 0;
373}
374
377
385
388
389#endif /* !__JO_VDP1_COMMAND_PIPELINE_H__ */
390
391/*
392** END OF FILE
393*/
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39
int jo_fixed
Fixed point Q16.16 number.
Definition types.h:59
unsigned short draw_mode
Definition vdp1_command_pipeline.h:227
short bottom
Definition vdp1_command_pipeline.h:267
void __jo_vdp1_set_window(const short left, const short top, const short right, const short bottom, const short zlimit, const short center_x, const short center_y)
Set the current 3D window (internal usage).
short yc
Definition vdp1_command_pipeline.h:74
short left
Definition vdp1_command_pipeline.h:264
unsigned short clip_mode
Definition vdp1_command_pipeline.h:271
void jo_3d_set_depth_limit(const int z)
Depth limit of the current window (the far clipping distance of jo_3d_window()): the polygons beyond ...
short xd
Definition vdp1_command_pipeline.h:75
short ya
Definition vdp1_command_pipeline.h:70
unsigned short grda
Definition vdp1_command_pipeline.h:77
jo_fixed scale_x
Definition vdp1_command_pipeline.h:249
jo_fixed x
Definition vdp1_command_pipeline.h:246
jo_fixed __jo_vdp1_near_z
Near clipping distance (internal usage, see __jo_vdp1_near_clip()).
bool __jo_vdp1_frame_clear
The frame buffer is cleared at the start of each frame (see jo_3d_set_frame_clear()).
short origin_x
Definition vdp1_command_pipeline.h:272
short yb
Definition vdp1_command_pipeline.h:72
short yd
Definition vdp1_command_pipeline.h:76
bool __jo_vdp1_wireframe
Wireframe mode (internal usage, see jo_3d_set_wireframe()).
jo_fixed zlimit
Definition vdp1_command_pipeline.h:270
unsigned short gouraud_table
Definition vdp1_command_pipeline.h:229
jo_fixed __jo_vdp1_projection_distance
Projection distance in Q16.16 (internal usage, see jo_3d_perspective_angle()).
static __jo_force_inline void __jo_vdp1_update_near_clip(void)
Compute the near clipping distance (internal usage).
Definition vdp1_command_pipeline.h:318
void __jo_vdp1_set_window_clip(const bool clip)
The 3D polygons of the current window clipped to its rectangle (internal usage, see jo_3d_window_clip...
short xb
Definition vdp1_command_pipeline.h:71
int jo_vdp1_command_count(void)
VDP1 commands of the last frame (the polygons, their parts, the sprites: see jo_vdp1_command_max()).
short xa
Definition vdp1_command_pipeline.h:69
unsigned short ctrl
Definition vdp1_command_pipeline.h:63
int __jo_vdp1_depth_bias
Depth added to the keys of the commands (see jo_3d_set_depth_bias()).
unsigned short __jo_vdp1_display_level
Near clipping divisor (internal usage, see jo_3d_display_level()).
static __jo_force_inline void __jo_vdp1_select_window(const int window)
Select the 3D window used by the next commands (internal usage).
Definition vdp1_command_pipeline.h:370
short right
Definition vdp1_command_pipeline.h:266
int __jo_vdp1_current_window
Current 3D window (internal usage).
bool __jo_vdp1_windows_apart
The windows drawn one after the other (internal usage, see jo_3d_set_windows_apart()).
unsigned short direction
Definition vdp1_command_pipeline.h:230
unsigned short color
Definition vdp1_command_pipeline.h:228
unsigned short size
Definition vdp1_command_pipeline.h:68
#define JO_VDP1_WINDOW_COUNT
Number of 3D windows: near (default), far, a second far window and the window of the transparency lay...
Definition vdp1_command_pipeline.h:279
unsigned short texture_id
Definition vdp1_command_pipeline.h:226
unsigned short link
Definition vdp1_command_pipeline.h:64
short center_x
Definition vdp1_command_pipeline.h:268
static __jo_force_inline void jo_3d_set_frame_clear(const bool enabled)
Clear the frame buffer at the start of each frame (the default), or not.
Definition vdp1_command_pipeline.h:359
jo_fixed y
Definition vdp1_command_pipeline.h:247
jo_fixed scale_y
Definition vdp1_command_pipeline.h:250
short top
Definition vdp1_command_pipeline.h:265
unsigned short reserved
Definition vdp1_command_pipeline.h:78
void jo_vdp1_redraw_last_frame(void)
Draw the last frame again (from a VBLANK callback while the main loop doesn't run: a loading)....
unsigned short pmod
Definition vdp1_command_pipeline.h:65
bool screen
Definition vdp1_command_pipeline.h:274
short xc
Definition vdp1_command_pipeline.h:73
int jo_vdp1_frame_command_count(void)
VDP1 commands of the frame being made now by the master CPU (the polygons, their parts,...
short center_y
Definition vdp1_command_pipeline.h:269
short origin_y
Definition vdp1_command_pipeline.h:273
unsigned short colr
Definition vdp1_command_pipeline.h:66
jo_fixed z
Definition vdp1_command_pipeline.h:248
int jo_vdp1_command_max(void)
Most VDP1 commands in a frame (the next ones are not drawn).
unsigned short srca
Definition vdp1_command_pipeline.h:67
VDP1 command (32 bytes).
Definition vdp1_command_pipeline.h:62
Sprite attribute.
Definition vdp1_command_pipeline.h:225
Sprite position (internal usage).
Definition vdp1_command_pipeline.h:245
3D window (internal usage)
Definition vdp1_command_pipeline.h:263