KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
video.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/video.h
4
5 Copyright (C) 2001 Anders Clerwall (scav)
6 Copyright (C) 2023-2024 Donald Haase
7
8*/
9
10/** \file dc/video.h
11 \brief Functions related to video output.
12 \ingroup video_display
13
14 This file deals with the video output hardware in the Dreamcast. There are
15 functions defined herein that deal with setting up the video hardware,
16 defining the resolution of the display, dealing with the framebuffer, etc.
17
18 \author Anders Clerwall
19 \author Megan Potter
20*/
21
22#ifndef __DC_VIDEO_H
23#define __DC_VIDEO_H
24
25#include <stdbool.h>
26#include <stdint.h>
27#include <kos/cdefs.h>
28__BEGIN_DECLS
29
30/** \defgroup video_display Display
31 \brief Display and framebuffer configuration
32 \ingroup video
33*/
34
35/** \defgroup video_modes Modes
36 \brief Video display modes and management
37 \ingroup video_display
38*/
39
40/** \defgroup video_cables Cables
41 \brief Cable/connector types and management for display
42 \ingroup video_modes
43*/
44
45/** \defgroup vid_ctype Types
46 \brief Type of cable connected to the DC
47 \ingroup video_cables
48
49 The vid_check_cable() function will return one of this set of values to let
50 you know what type of cable is connected to the Dreamcast. These are also
51 used in the video mode settings to limit modes to certain cable types.
52
53 @{
54*/
55#define CT_ANY -1 /**< \brief Any cable type. Used only internally. */
56#define CT_VGA 0 /**< \brief VGA Box */
57#define CT_NONE 1 /**< \brief Nothing connected */
58#define CT_RGB 2 /**< \brief RGB/SCART cable */
59#define CT_COMPOSITE 3 /**< \brief Composite cable or RF switch */
60/** @} */
61
62/** \defgroup video_modes_pixel Pixel Modes
63 \brief Pixel mode settings for the framebuffer
64 \ingroup video_modes
65*/
66
67/** \brief Pixel mode values for the framebuffer
68
69 This set of constants control the pixel mode that the framebuffer is set to.
70 These are hardware-based values and get set in bits 2 and 3 of PVR_FB_CFG_1.
71*/
72typedef enum vid_pixel_mode {
73 PM_RGB555 = 0, /**< \brief RGB555 pixel mode (15-bit) */
74 PM_RGB565 = 1, /**< \brief RGB565 pixel mode (16-bit) */
75 PM_RGB888P = 2, /**< \brief RBG888 packed pixel mode (24-bit) */
76 PM_RGB0888 = 3, /**< \brief RGB0888 pixel mode (32-bit) */
77 PM_RGB888 = 3 /**< \brief Backwards compatibility support */
79
80/** \brief Video pixel mode depths
81 \ingroup video_modes_pixel
82*/
83static const uint8_t vid_pmode_bpp[4] = {2, 2, 3, 4};
84
85/** \defgroup video_modes_display Types
86 \brief Display mode type values
87 \ingroup video_modes
88*/
89
90/** \brief Generic Display Modes
91 \ingroup video_modes_display
92 */
93typedef enum vid_display_mode_generic {
94 DM_GENERIC_FIRST = 0x1000, /**< \brief First valid generic mode */
95 DM_320x240 = 0x1000, /**< \brief 320x240 resolution */
96 DM_640x480, /**< \brief 640x480 resolution */
97 DM_256x256, /**< \brief 256x256 resolution */
98 DM_768x480, /**< \brief 768x480 resolution */
99 DM_768x576, /**< \brief 768x576 resolution */
100 DM_GENERIC_LAST = DM_768x576 /**< \brief Last valid generic mode */
102
103/** \brief Multi-buffered mode setting.
104 \ingroup video_modes_display
105
106 OR this with the generic mode to get multiple framebuffers instead of one.
107*/
108#define DM_MULTIBUFFER 0x2000
109
110/** \brief Default video mode. */
111#define DEFAULT_VID_MODE DM_640x480
112
113/** \brief Default pixel mode for video. */
114#define DEFAULT_PIXEL_MODE PM_RGB565
115
116/* ------------------------------------------------------------------------- */
117/* More specific modes (and actual indices into the mode table) */
118
119/** \brief Specific Display Modes
120 \ingroup video_modes_display
121*/
122typedef enum vid_display_mode {
123 DM_INVALID = 0, /**< \brief Invalid display mode */
124 /* Valid modes below */
125 DM_320x240_VGA = 1, /**< \brief 320x240 VGA 60Hz */
126 DM_320x240_NTSC, /**< \brief 320x240 NTSC 60Hz */
127 DM_640x480_VGA, /**< \brief 640x480 VGA 60Hz */
128 DM_640x480_NTSC_IL, /**< \brief 640x480 NTSC Interlaced 60Hz */
129 DM_640x480_PAL_IL, /**< \brief 640x480 PAL Interlaced 50Hz */
130 DM_256x256_PAL_IL, /**< \brief 256x256 PAL Interlaced 50Hz */
131 DM_768x480_NTSC_IL, /**< \brief 768x480 NTSC Interlaced 60Hz */
132 DM_768x576_PAL_IL, /**< \brief 768x576 PAL Interlaced 50Hz */
133 DM_768x480_PAL_IL, /**< \brief 768x480 PAL Interlaced 50Hz */
134 DM_320x240_PAL, /**< \brief 320x240 PAL 50Hz */
135 /* The below is only for counting.. */
136 DM_SENTINEL, /**< \brief Sentinel value, for counting */
137 DM_MODE_COUNT /**< \brief Number of modes */
139
140/** \defgroup vid_flags Flags
141 \brief vid_mode_t Field Flags
142 \ingroup video_modes
143
144 These flags indicate various things related to the modes for a vid_mode_t.
145
146 @{
147*/
148#define VID_INTERLACE 0x00000001 /**< \brief Interlaced display */
149#define VID_LINEDOUBLE 0x00000002 /**< \brief Display each scanline twice */
150#define VID_PIXELDOUBLE 0x00000004 /**< \brief Display each pixel twice */
151#define VID_PAL 0x00000008 /**< \brief 50Hz refresh rate, if not VGA */
152/** @} */
153
154/** \brief Video mode structure.
155 \ingroup video_modes
156
157 KOS maintains a list of valid video modes internally that correspond to the
158 specific display modes enumeration. Each of them is built of one of these.
159
160 \headerfile dc/video.h
161*/
162typedef struct vid_mode {
163 uint16_t generic; /**< \brief Generic mode type for vid_set_mode() */
164 uint16_t width; /**< \brief Width of the display, in pixels */
165 uint16_t height; /**< \brief Height of the display, in pixels */
166 uint32_t flags; /**< \brief Combination of one or more VID_* flags */
167
168 int16_t cable_type; /**< \brief Allowed cable type */
169 vid_pixel_mode_t pm; /**< \brief Pixel mode */
170
171 uint16_t scanlines; /**< \brief Number of scanlines */
172 uint16_t clocks; /**< \brief Clocks per scanline */
173 uint16_t bitmapx; /**< \brief Bitmap window X position */
174 uint16_t bitmapy; /**< \brief Bitmap window Y position (automatically
175 increased for PAL) */
176 uint16_t scanint1; /**< \brief First scanline interrupt position */
177 uint16_t scanint2; /**< \brief Second scanline interrupt position
178 (automatically doubled for VGA) */
179 uint16_t borderx1; /**< \brief Border X starting position */
180 uint16_t borderx2; /**< \brief Border X stop position */
181 uint16_t bordery1; /**< \brief Border Y starting position */
182 uint16_t bordery2; /**< \brief Border Y stop position */
183
184 uint16_t fb_curr; /**< \brief Current framebuffer */
185 uint16_t fb_count; /**< \brief Number of framebuffers */
186 size_t fb_size; /**< \brief Size of each framebuffer */
187} vid_mode_t;
188
189/** \brief The list of builtin video modes. Do not modify these!
190 \ingroup video_modes
191*/
193
194/** \brief The current video mode. Do not modify directly!
195 \ingroup video_modes
196*/
197extern vid_mode_t *vid_mode;
198
199/* These point to the current drawing area. If you're not using a multi-buffered
200 mode, that means they do what KOS always used to do (they'll point at the
201 start of VRAM). If you're using a multi-buffered mode, they'll point at the
202 next framebuffer to be displayed. You must use vid_flip for this to work
203 though (if you use vid_set_start, they'll point at the display base, for
204 compatibility's sake).
205*/
206
207/** \defgroup video_fb Framebuffer
208 \brief API for framebuffer management
209 \ingroup video_display
210*/
211
212/** \brief 16-bit size pointer to the current drawing area.
213 \ingroup video_fb
214*/
215extern uint16_t *vram_s;
216
217/** \brief 32-bit size pointer to the current drawing area.
218 \ingroup video_fb
219*/
220extern uint32_t *vram_l;
221
222
223/** \brief Retrieve the connected video cable type.
224 \ingroup video_cables
225
226 This function checks the video cable and reports what it finds.
227
228 \retval CT_VGA If a VGA Box or cable is connected.
229 \retval CT_NONE If nothing is connected.
230 \retval CT_RGB If a RGB/SCART cable is connected.
231 \retval CT_COMPOSITE If a composite cable or RF switch is connected.
232*/
233int8_t vid_check_cable(void);
234
235/** \brief Set the VRAM convenience pointers.
236 \ingroup video_fb
237
238 This function sets the vram_s and vram_l pointers to specified offset
239 within VRAM. In multibuffered mode it allows manual management of them.
240
241 \param base The offset within VRAM to set the base to.
242*/
244
245/** \brief Set the VRAM base of the framebuffer.
246 \ingroup video_fb
247
248 This function sets the vram_s and vram_l pointers to specified offset
249 within VRAM and sets the start position of the framebuffer to the same
250 offset.
251
252 \param base The offset within VRAM to set the base to.
253*/
255
256/** \brief Get the VRAM base of a framebuffer.
257 \ingroup video_fb
258
259 This function gets the position of the specified framebuffer within VRAM.
260 Any invalid fb value will be treated as the current framebuffer.
261
262 \param fb The number of the framebuffer or -1 for current.
263*/
265
266/** \brief Set the current framebuffer in a multibuffered setup.
267 \ingroup video_fb
268
269 This function sets the displayed framebuffer to the specified buffer.
270 Unlike vid_set_fb, this does not point the vram pointers to the next
271 framebuffer, allowing for non-linear management of the FBs.
272
273 \param fb The framebuffer to display or any other value
274 to display the next one.
275
276*/
277void vid_set_fb(int32_t fb);
278
279/** \brief Flip to a framebuffer in a multibuffered setup.
280 \ingroup video_fb
281
282 This function sets the displayed framebuffer to the specified buffer and
283 sets the vram_s and vram_l pointers to point at the next framebuffer, to
284 allow for tearing-free framebuffer-direct drawing.
285
286 \param fb The framebuffer to display or any other value
287 to display the next one.
288
289*/
290void vid_flip(int32_t fb);
291
292/** \brief Set the border color of the display.
293 \ingroup video_display
294
295 This sets the color of the border area of the display.
296
297 \note
298 On some screens, the border area may not be shown at all,
299 whereas on some displays you may see the whole thing.
300
301 \param r The red value of the color (0-255).
302 \param g The green value of the color (0-255).
303 \param b The blue value of the color (0-255).
304
305 \return Old border color value (RGB888)
306*/
307uint32_t vid_border_color(uint8_t r, uint8_t g, uint8_t b);
308
309/** \brief Clear the framebuffer.
310 \ingroup video_fb
311
312 This function sets the whole framebuffer to the specified color. Internally,
313 this uses the store queues to actually clear the display entirely.
314
315 \note
316 This operates via the vram convenience pointers. In multibuffered mode,
317 by default it will clear the framebuffer you are currently writing to
318 rather than the one being displayed.
319
320 \param r The red value of the color (0-255).
321 \param g The green value of the color (0-255).
322 \param b The blue value of the color (0-255).
323*/
324void vid_clear(uint8_t r, uint8_t g, uint8_t b);
325
326/** \brief Clear VRAM.
327 \ingroup video_fb
328
329 This function is essentially a memset() for the whole of VRAM that will
330 clear it all to 0 bytes.
331*/
332void vid_empty(void);
333
334/** \brief Get the state of video output.
335 \ingroup video_display
336
337 This function gets the state of video output as set via vid_set_enabled.
338
339 \return true if enabled, false if not.
340*/
342
343/** \brief Enable/disable the display.
344 \ingroup video_display
345
346 This function enables or disables video output
347
348 \param val true to enable video output, false to disable.
349
350 \note
351 Unlike vid_clear/vid_empty this does not modify any framebuffer.
352 Instead it merely sets registers that immediately disable output.
353*/
354void vid_set_enabled(bool val);
355
356/** \defgroup video_misc Miscellaneous
357 \brief Miscellaneous video API utilities
358 \ingroup video
359*/
360
361/** \brief Wait for VBlank.
362 \ingroup video_misc
363
364 This function busy loops until the vertical blanking period starts.
365*/
366void vid_waitvbl(void);
367
368/** \brief Set the video mode.
369 \ingroup video_modes
370
371 This function sets the current video mode to the one specified by the
372 parameters.
373
374 \param dm The display mode to use. One of the DM_* values.
375 \param pm The pixel mode to use. One of the PM_* values.
376*/
378
379/** \brief Set the video mode.
380 \ingroup video_modes
381
382 This function sets the current video mode to the mode structure passed in.
383 You can use this to add support to your program for modes that KOS doesn't
384 have support for built-in (of course, you should tell us the settings so we
385 can add them into KOS if you do this).
386
387 \param mode A filled in vid_mode_t for the mode wanted.
388*/
390
391/** \defgroup video_init Initialization
392 \brief Initialization and shutdown of the video subsystem
393 \ingroup video
394
395 @{
396*/
397
398/** \brief Initialize the video system.
399
400 This function initializes the video display, setting the mode to the
401 specified parameters, clearing vram, and setting the first framebuffer as
402 active.
403
404 \param disp_mode The display mode to use. One of the DM_* values.
405 \param pixel_mode The pixel mode to use. One of the PM_* values.
406*/
407void vid_init(int disp_mode, vid_pixel_mode_t pixel_mode);
408
409/** \brief Shut down the video system.
410
411 This function reinitializes the video system to what dcload and friends
412 expect it to be.
413*/
414void vid_shutdown(void);
415
416/** @} */
417
418/** \brief Take a screenshot.
419 \ingroup video_fb
420
421 This function takes the current framebuffer (/vram_l) and dumps it out
422 to a PPM file.
423
424 \param destfn The filename to save to.
425 \return 0 on success, <0 on failure.
426*/
427int vid_screen_shot(const char *destfn);
428
429/** \brief Create image data from the current framebuffer.
430 \ingroup video_fb
431
432 This function takes the current framebuffer (/vram_l) and converts it into
433 24bpp image data. The function allocates memory for the image data, which
434 the caller is responsible for freeing.
435
436 \param buffer A pointer to a uint8_t* that will be allocated and
437 filled with the image data.
438 \return The size of the created image data in bytes, or 0 on failure.
439*/
440size_t vid_screen_shot_data(uint8_t **buffer);
441
442/** \brief Enable or disable dithering.
443 \ingroup video_fb
444
445 This function can be used to enable or disable dithering when a 15-bit or
446 16-bit video mode is used.
447
448 \param enable Whether or not dithering should be enabled.
449*/
450void vid_set_dithering(bool enable);
451
452__END_DECLS
453
454#endif /* __DC_VIDEO_H */
455
Various common macros used throughout the codebase.
static uint32_t("Please see purupuru_effect_t for modern equivalent.") PURUPURU_EFFECT2_UINTENSITY(uint8_t x)
Definition purupuru.h:96
int8_t vid_check_cable(void)
Retrieve the connected video cable type.
uint32_t vid_border_color(uint8_t r, uint8_t g, uint8_t b)
Set the border color of the display.
bool vid_get_enabled(void)
Get the state of video output.
void vid_set_enabled(bool val)
Enable/disable the display.
void vid_set_vram(uint32_t base)
Set the VRAM convenience pointers.
uint16_t * vram_s
16-bit size pointer to the current drawing area.
int vid_screen_shot(const char *destfn)
Take a screenshot.
void vid_set_fb(int32_t fb)
Set the current framebuffer in a multibuffered setup.
uint32_t * vram_l
32-bit size pointer to the current drawing area.
void vid_clear(uint8_t r, uint8_t g, uint8_t b)
Clear the framebuffer.
size_t vid_screen_shot_data(uint8_t **buffer)
Create image data from the current framebuffer.
uint32_t vid_get_start(int32_t fb)
Get the VRAM base of a framebuffer.
void vid_set_start(uint32_t base)
Set the VRAM base of the framebuffer.
void vid_set_dithering(bool enable)
Enable or disable dithering.
void vid_empty(void)
Clear VRAM.
void vid_flip(int32_t fb)
Flip to a framebuffer in a multibuffered setup.
void vid_init(int disp_mode, vid_pixel_mode_t pixel_mode)
Initialize the video system.
void vid_shutdown(void)
Shut down the video system.
void vid_waitvbl(void)
Wait for VBlank.
vid_display_mode_generic_t
Generic Display Modes.
Definition video.h:93
vid_display_mode_t
Specific Display Modes.
Definition video.h:122
@ DM_GENERIC_LAST
Last valid generic mode.
Definition video.h:100
@ DM_GENERIC_FIRST
First valid generic mode.
Definition video.h:94
@ DM_640x480
640x480 resolution
Definition video.h:96
@ DM_768x576
768x576 resolution
Definition video.h:99
@ DM_320x240
320x240 resolution
Definition video.h:95
@ DM_768x480
768x480 resolution
Definition video.h:98
@ DM_256x256
256x256 resolution
Definition video.h:97
@ DM_640x480_NTSC_IL
640x480 NTSC Interlaced 60Hz
Definition video.h:128
@ DM_SENTINEL
Sentinel value, for counting.
Definition video.h:136
@ DM_640x480_VGA
640x480 VGA 60Hz
Definition video.h:127
@ DM_320x240_NTSC
320x240 NTSC 60Hz
Definition video.h:126
@ DM_256x256_PAL_IL
256x256 PAL Interlaced 50Hz
Definition video.h:130
@ DM_768x576_PAL_IL
768x576 PAL Interlaced 50Hz
Definition video.h:132
@ DM_320x240_PAL
320x240 PAL 50Hz
Definition video.h:134
@ DM_768x480_PAL_IL
768x480 PAL Interlaced 50Hz
Definition video.h:133
@ DM_640x480_PAL_IL
640x480 PAL Interlaced 50Hz
Definition video.h:129
@ DM_INVALID
Invalid display mode.
Definition video.h:123
@ DM_MODE_COUNT
Number of modes.
Definition video.h:137
@ DM_768x480_NTSC_IL
768x480 NTSC Interlaced 60Hz
Definition video.h:131
@ DM_320x240_VGA
320x240 VGA 60Hz
Definition video.h:125
static const uint8_t vid_pmode_bpp[4]
Video pixel mode depths.
Definition video.h:83
vid_mode_t * vid_mode
The current video mode.
void vid_set_mode(int dm, vid_pixel_mode_t pm)
Set the video mode.
void vid_set_mode_ex(vid_mode_t *mode)
Set the video mode.
vid_mode_t vid_builtin[DM_MODE_COUNT]
The list of builtin video modes.
Video mode structure.
Definition video.h:162
uint16_t scanint1
First scanline interrupt position.
Definition video.h:176
uint32_t flags
Combination of one or more VID_* flags.
Definition video.h:166
uint16_t height
Height of the display, in pixels.
Definition video.h:165
uint16_t scanlines
Number of scanlines.
Definition video.h:171
vid_pixel_mode_t pm
Pixel mode.
Definition video.h:169
uint16_t bitmapy
Bitmap window Y position (automatically increased for PAL)
Definition video.h:174
uint16_t scanint2
Second scanline interrupt position (automatically doubled for VGA)
Definition video.h:177
uint16_t clocks
Clocks per scanline.
Definition video.h:172
size_t fb_size
Size of each framebuffer.
Definition video.h:186
uint16_t fb_count
Number of framebuffers.
Definition video.h:185
uint16_t bitmapx
Bitmap window X position.
Definition video.h:173
uint16_t borderx2
Border X stop position.
Definition video.h:180
uint16_t fb_curr
Current framebuffer.
Definition video.h:184
int16_t cable_type
Allowed cable type.
Definition video.h:168
uint16_t bordery2
Border Y stop position.
Definition video.h:182
uint16_t width
Width of the display, in pixels.
Definition video.h:164
uint16_t bordery1
Border Y starting position.
Definition video.h:181
uint16_t borderx1
Border X starting position.
Definition video.h:179
vid_pixel_mode_t
Pixel mode values for the framebuffer.
Definition video.h:72
@ PM_RGB888P
RBG888 packed pixel mode (24-bit)
Definition video.h:75
@ PM_RGB565
RGB565 pixel mode (16-bit)
Definition video.h:74
@ PM_RGB555
RGB555 pixel mode (15-bit)
Definition video.h:73
@ PM_RGB0888
RGB0888 pixel mode (32-bit)
Definition video.h:76
@ PM_RGB888
Backwards compatibility support.
Definition video.h:77