Jo Engine 2026.11.1
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** 0x7E700 : gouraud tables of the clipped and subdivided 3D polygons
46** 0x7FF00 : gouraud tables used for 3D lighting
47*/
48
55#ifndef JO_VDP1_MAX_COMMANDS
56# define JO_VDP1_MAX_COMMANDS (1000)
57#endif
58
60typedef struct
61{
62 unsigned short ctrl;
63 unsigned short link;
64 unsigned short pmod;
65 unsigned short colr;
66 unsigned short srca;
67 unsigned short size;
68 short xa;
69 short ya;
70 short xb;
71 short yb;
72 short xc;
73 short yc;
74 short xd;
75 short yd;
76 unsigned short grda;
77 unsigned short reserved;
79
81# define JO_VDP1_CMD_NORMAL_SPRITE (0x0000)
82# define JO_VDP1_CMD_SCALED_SPRITE (0x0001)
83# define JO_VDP1_CMD_DISTORTED_SPRITE (0x0002)
84# define JO_VDP1_CMD_POLYGON (0x0004)
85# define JO_VDP1_CMD_POLYLINE (0x0005)
86# define JO_VDP1_CMD_LINE (0x0006)
87# define JO_VDP1_CMD_USER_CLIP (0x0008)
88# define JO_VDP1_CMD_SYSTEM_CLIP (0x0009)
89# define JO_VDP1_CMD_LOCAL_COORD (0x000A)
90# define JO_VDP1_CMD_SKIP_ASSIGN (0x5000)
91# define JO_VDP1_CMD_END (0x8000)
92
93/*
94** Drawing function (low bits of the "direction" attribute)
95*/
97# define JO_VDP1_FUNCTION_SPRITE (1)
99# define JO_VDP1_FUNCTION_TEXTURE (2)
101# define JO_VDP1_FUNCTION_POLYGON (4)
103# define JO_VDP1_FUNCTION_POLYLINE (5)
105# define JO_VDP1_FUNCTION_LINE (6)
107# define JO_VDP1_FUNCTION_USER_CLIP (8)
109# define JO_VDP1_FUNCTION_SYSTEM_CLIP (9)
111# define JO_VDP1_FUNCTION_LOCAL_COORDINATES (10)
112
114# define JO_VDP1_DIRECTION_FUNCTION_MASK (0x0f)
116# define JO_VDP1_DIRECTION_FLIP_MASK (0x30)
118# define JO_VDP1_H_FLIP (0x10)
119
120/*
121** Draw mode (CMDPMOD register layout)
122*/
124# define JO_VDP1_MSB_ON (1 << 15)
126# define JO_VDP1_HIGH_SPEED_SHRINK (1 << 12)
128# define JO_VDP1_NO_WINDOW (0)
130# define JO_VDP1_WINDOW_IN (2 << 9)
132# define JO_VDP1_WINDOW_OUT (3 << 9)
134# define JO_VDP1_MESH (1 << 8)
136# define JO_VDP1_NO_MESH (0)
138# define JO_VDP1_END_CODE_DISABLE (1 << 7)
140# define JO_VDP1_RGB_END_CODE (0x7FFF)
142# define JO_VDP1_TRANSPARENT_PIXEL_DISABLE (1 << 6)
144# define JO_VDP1_COLOR_MODE_16_BANK (0 << 3)
146# define JO_VDP1_COLOR_MODE_16_LOOKUP (1 << 3)
148# define JO_VDP1_COLOR_MODE_64_BANK (2 << 3)
150# define JO_VDP1_COLOR_MODE_128_BANK (3 << 3)
152# define JO_VDP1_COLOR_MODE_256_BANK (4 << 3)
154# define JO_VDP1_COLOR_MODE_RGB (5 << 3)
156# define JO_VDP1_COLOR_MODE_MASK (7 << 3)
158# define JO_VDP1_COLOR_CALC_REPLACE (0)
160# define JO_VDP1_COLOR_CALC_SHADOW (1)
162# define JO_VDP1_COLOR_CALC_HALF_LUMINANCE (2)
164# define JO_VDP1_COLOR_CALC_HALF_TRANSPARENT (3)
166# define JO_VDP1_COLOR_CALC_GOURAUD (4)
168# define JO_VDP1_COLOR_CALC_MASK (7)
170# define JO_VDP1_NO_GOURAUD (0)
172# define JO_VDP1_NO_PALETTE (0)
173
174/*
175** Texture size: bytes = width * height * 4 >> shift
176*/
178# define JO_VDP1_TEXTURE_SIZE_SHIFT_16_COLORS (3)
180# define JO_VDP1_TEXTURE_SIZE_SHIFT_256_COLORS (2)
182# define JO_VDP1_TEXTURE_SIZE_SHIFT_32K_COLORS (1)
183
185# define JO_VDP1_OPTION_USE_TEXTURE (1 << 2)
186
187/*
188** Direction attribute: drawing function | flip | (options << 16) | (draw mode bits << 24)
189*/
191# define JO_VDP1_SPRITE_NO_FLIP ((0) | JO_VDP1_FUNCTION_TEXTURE | (JO_VDP1_OPTION_USE_TEXTURE << 16))
193# define JO_VDP1_SPRITE_H_FLIP ((1 << 4) | JO_VDP1_FUNCTION_TEXTURE | (JO_VDP1_OPTION_USE_TEXTURE << 16))
195# define JO_VDP1_SPRITE_V_FLIP ((1 << 5) | JO_VDP1_FUNCTION_TEXTURE | (JO_VDP1_OPTION_USE_TEXTURE << 16))
197# define JO_VDP1_SPRITE_HV_FLIP ((3 << 4) | JO_VDP1_FUNCTION_TEXTURE | (JO_VDP1_OPTION_USE_TEXTURE << 16))
199# define JO_VDP1_POLYGON (JO_VDP1_FUNCTION_POLYGON | ((JO_VDP1_END_CODE_DISABLE | JO_VDP1_TRANSPARENT_PIXEL_DISABLE) << 24))
201# define JO_VDP1_POLYLINE (JO_VDP1_FUNCTION_POLYLINE | ((JO_VDP1_END_CODE_DISABLE | JO_VDP1_TRANSPARENT_PIXEL_DISABLE) << 24))
203# define JO_VDP1_LINE (JO_VDP1_FUNCTION_LINE | ((JO_VDP1_END_CODE_DISABLE | JO_VDP1_TRANSPARENT_PIXEL_DISABLE) << 24))
204
208# define JO_VDP1_DIRECTION_COMMAND(DIRECTION) ((DIRECTION) & (JO_VDP1_DIRECTION_FUNCTION_MASK | JO_VDP1_DIRECTION_FLIP_MASK))
212# define JO_VDP1_DIRECTION_OPTIONS(DIRECTION) (((DIRECTION) >> 16) & 0x1c)
216# define JO_VDP1_DIRECTION_DRAW_MODE(DIRECTION) (((DIRECTION) >> 24) & (JO_VDP1_END_CODE_DISABLE | JO_VDP1_TRANSPARENT_PIXEL_DISABLE))
217
219typedef struct
220{
221 unsigned short texture_id; // Sprite id (__jo_sprite_def index)
222 unsigned short draw_mode; // JO_VDP1_COLOR_MODE_xxx | JO_VDP1_COLOR_CALC_xxx | ...
223 unsigned short color; // RGB color or palette
224 unsigned short gouraud_table; // Gouraud table address / 8
225 unsigned short direction; // JO_VDP1_FUNCTION_xxx | flip
227
235# define JO_VDP1_SPRITE_ATTRIBUTE(TEXTURE, COLOR, GOURAUD, DRAW_MODE, DIRECTION) \
236 {(TEXTURE), (DRAW_MODE) | JO_VDP1_DIRECTION_DRAW_MODE(DIRECTION), (COLOR), (GOURAUD), JO_VDP1_DIRECTION_COMMAND(DIRECTION)}
237
239typedef struct
240{
244 jo_fixed scale_x; // Scale (negative value: perspective scale)
245 jo_fixed scale_y; // Vertical scale (only for __jo_vdp1_draw_sprite_hv())
247
249# define JO_VDP1_LIGHT_TABLE_OFFSET (0x7FF00)
252# define JO_VDP1_CLIP_GOURAUD_TABLE_OFFSET (0x7E700)
254# define JO_VDP1_CLIP_GOURAUD_TABLE_COUNT (768)
255
257typedef struct
258{
259 short left;
260 short top;
261 short right;
262 short bottom;
263 short center_x;
264 short center_y;
266 unsigned short clip_mode; // Draw mode bits of the 3D polygons: JO_VDP1_WINDOW_IN (clipped to the window), 0
268
271# define JO_VDP1_WINDOW_COUNT (3)
273
275extern int __jo_vdp1_current_window;
276
279
281extern unsigned short __jo_vdp1_display_level;
282
284void __jo_vdp1_init(void);
285
288
291
296unsigned int __jo_vdp1_frame_tag(void);
297
303bool __jo_vdp1_displayed_frame_changed(unsigned int * const tag);
304
311
317
325jo_vdp1_command *__jo_vdp1_reserve_commands(const unsigned int count, unsigned short **keys, unsigned int *reserved);
326
332unsigned short *__jo_vdp1_commit_commands_without_window(const unsigned int used);
333
337void __jo_vdp1_commit_commands(const unsigned int used);
338
340# define JO_VDP1_MAX_KEY (0xffff)
341
343extern int __jo_vdp1_depth_bias;
344
349static __jo_force_inline unsigned short __jo_vdp1_sort_key(const jo_fixed z)
350{
351 int key;
352
354 if (key < 0)
355 return (0);
356 if (key > JO_VDP1_MAX_KEY)
357 return (JO_VDP1_MAX_KEY);
358 return ((unsigned short)key);
359}
360
362extern short __jo_vdp1_system_clip_right;
363
366
374
377
379extern bool __jo_vdp1_wireframe;
380
385{
386 // Variable shift: computed once instead of for each sprite (the SH-2 can only shift by 1, 2, 8 or 16 bits)
388}
389
398
405bool __jo_vdp1_draw_sprite(const jo_vdp1_sprite_position * const pos, const jo_vdp1_sprite_attribute * const attr, const jo_angle angle);
406
413bool __jo_vdp1_draw_sprite_hv(const jo_vdp1_sprite_position * const pos, const jo_vdp1_sprite_attribute * const attr, const jo_angle angle);
414
421bool __jo_vdp1_draw_sprite_4p(const jo_pos2D_fixed * const points, const jo_fixed z, const jo_vdp1_sprite_attribute * const attr);
422
432bool __jo_vdp1_write_sprite_4p(jo_vdp1_command * const cmd, const jo_pos2D_fixed * const points, const jo_fixed z,
433 const jo_vdp1_sprite_attribute * const attr, const int window);
434
441void __jo_sprite_attributes_of(jo_vdp1_sprite_attribute *attr, const int sprite_id, const jo_sprite_attributes * const state);
442
450bool __jo_vdp1_draw_sprite_3d(const jo_vdp1_sprite_position * const pos, const jo_vdp1_sprite_attribute * const attr, const jo_angle angle);
451
462void __jo_vdp1_set_window(const short left, const short top, const short right, const short bottom,
463 const short zlimit, const short center_x, const short center_y);
464
468static __jo_force_inline void __jo_vdp1_select_window(const int window)
469{
470 __jo_vdp1_current_window = (window >= 0 && window < JO_VDP1_WINDOW_COUNT) ? window : 0;
471}
472
475
479
482
483#endif /* !__JO_VDP1_COMMAND_PIPELINE_H__ */
484
485/*
486** END OF FILE
487*/
#define JO_FIXED_TO_INT(X)
Convert jo_fixed to int.
Definition math.h:459
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39
short jo_angle
Angle.
Definition types.h:54
int jo_fixed
Fixed point Q16.16 number.
Definition types.h:48
2D position using fixed number
Definition types.h:65
Sprite attributes.
Definition types.h:271
jo_vdp1_command * __jo_vdp1_add_command(const jo_fixed z)
Allocate a command for the current frame (internal usage).
unsigned short draw_mode
Definition vdp1_command_pipeline.h:222
unsigned int __jo_vdp1_frame_tag(void)
Number of the frame being built (internal usage): the VDP2 changes that must match the image of this ...
short bottom
Definition vdp1_command_pipeline.h:262
int __jo_vdp1_frame_command_count(void)
VDP1 commands of the frame being made by the master CPU, read without the cache (internal usage: the ...
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:73
short left
Definition vdp1_command_pipeline.h:259
bool __jo_vdp1_draw_sprite_hv(const jo_vdp1_sprite_position *const pos, const jo_vdp1_sprite_attribute *const attr, const jo_angle angle)
Draw a sprite on the screen with different horizontal and vertical scale (internal usage).
unsigned short clip_mode
Definition vdp1_command_pipeline.h:266
short xd
Definition vdp1_command_pipeline.h:74
bool __jo_vdp1_write_sprite_4p(jo_vdp1_command *const cmd, const jo_pos2D_fixed *const points, const jo_fixed z, const jo_vdp1_sprite_attribute *const attr, const int window)
Write the command of a sprite using 4 points in a command table of the caller (internal usage: the sl...
void __jo_vdp1_end_frame(void)
Sort the commands of the frame and copy them into the VDP1 VRAM (internal usage).
short ya
Definition vdp1_command_pipeline.h:69
unsigned short grda
Definition vdp1_command_pipeline.h:76
jo_fixed scale_x
Definition vdp1_command_pipeline.h:244
jo_fixed x
Definition vdp1_command_pipeline.h:241
jo_fixed __jo_vdp1_near_z
Near clipping distance (internal usage, see __jo_vdp1_near_clip()).
short yb
Definition vdp1_command_pipeline.h:71
short yd
Definition vdp1_command_pipeline.h:75
bool __jo_vdp1_wireframe
Wireframe mode (internal usage, see jo_3d_set_wireframe()).
jo_fixed zlimit
Definition vdp1_command_pipeline.h:265
bool __jo_vdp1_set_attribute(jo_vdp1_command *cmd, const jo_vdp1_sprite_attribute *attr)
Fill the drawing part of a command from a sprite attribute (internal usage).
jo_vdp1_window __jo_vdp1_windows[(3)]
unsigned short gouraud_table
Definition vdp1_command_pipeline.h:224
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:384
bool __jo_vdp1_displayed_frame_changed(unsigned int *const tag)
Called during the VBLANK, after __jo_vdp1_vblank() (internal usage, a VBLANK callback): the image of ...
short xb
Definition vdp1_command_pipeline.h:70
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:68
bool __jo_vdp1_draw_sprite_3d(const jo_vdp1_sprite_position *const pos, const jo_vdp1_sprite_attribute *const attr, const jo_angle angle)
Draw a sprite in the 3D space using the current matrix (internal usage).
unsigned short ctrl
Definition vdp1_command_pipeline.h:62
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:468
short right
Definition vdp1_command_pipeline.h:261
int __jo_vdp1_current_window
Current 3D window (internal usage).
unsigned short direction
Definition vdp1_command_pipeline.h:225
unsigned short color
Definition vdp1_command_pipeline.h:223
unsigned short size
Definition vdp1_command_pipeline.h:67
short __jo_vdp1_system_clip_bottom
Bottom of the system clipping area (internal usage).
#define JO_VDP1_WINDOW_COUNT
3D windows (internal usage)
Definition vdp1_command_pipeline.h:271
unsigned short * __jo_vdp1_commit_commands_without_window(const unsigned int used)
Add the first commands reserved by __jo_vdp1_reserve_commands() to the frame, their windows written b...
unsigned short texture_id
Definition vdp1_command_pipeline.h:221
unsigned short link
Definition vdp1_command_pipeline.h:63
short center_x
Definition vdp1_command_pipeline.h:263
#define JO_VDP1_MAX_KEY
Biggest depth key.
Definition vdp1_command_pipeline.h:340
static __jo_force_inline jo_fixed __jo_vdp1_near_clip(void)
Near clipping distance (internal usage).
Definition vdp1_command_pipeline.h:394
void __jo_vdp1_commit_commands(const unsigned int used)
Add the first commands reserved by __jo_vdp1_reserve_commands() to the frame (internal usage).
void __jo_sprite_attributes_of(jo_vdp1_sprite_attribute *attr, const int sprite_id, const jo_sprite_attributes *const state)
Attribute of a sprite with some sprite attributes (internal usage: the slave CPU uses its copy of the...
jo_fixed y
Definition vdp1_command_pipeline.h:242
jo_vdp1_command * __jo_vdp1_reserve_commands(const unsigned int count, unsigned short **keys, unsigned int *reserved)
Reserve commands for the current frame (internal usage).
jo_fixed scale_y
Definition vdp1_command_pipeline.h:245
void __jo_vdp1_init(void)
Initialize the VDP1 (internal usage).
bool __jo_vdp1_draw_sprite_4p(const jo_pos2D_fixed *const points, const jo_fixed z, const jo_vdp1_sprite_attribute *const attr)
Draw a sprite or a polygon using 4 points (internal usage).
short top
Definition vdp1_command_pipeline.h:260
static __jo_force_inline unsigned short __jo_vdp1_sort_key(const jo_fixed z)
Depth key of a command (internal usage).
Definition vdp1_command_pipeline.h:349
unsigned short reserved
Definition vdp1_command_pipeline.h:77
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)....
short __jo_vdp1_system_clip_right
Right of the system clipping area (internal usage).
unsigned short pmod
Definition vdp1_command_pipeline.h:64
short xc
Definition vdp1_command_pipeline.h:72
short center_y
Definition vdp1_command_pipeline.h:264
bool __jo_vdp1_draw_sprite(const jo_vdp1_sprite_position *const pos, const jo_vdp1_sprite_attribute *const attr, const jo_angle angle)
Draw a sprite on the screen (internal usage).
unsigned short colr
Definition vdp1_command_pipeline.h:65
void __jo_vdp1_vblank(void)
Display the last complete command table (internal usage, called during VBLANK-IN).
jo_fixed z
Definition vdp1_command_pipeline.h:243
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:66
VDP1 command (32 bytes).
Definition vdp1_command_pipeline.h:61
Sprite attribute.
Definition vdp1_command_pipeline.h:220
Sprite position (internal usage).
Definition vdp1_command_pipeline.h:240
3D window (internal usage)
Definition vdp1_command_pipeline.h:258