KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
bruces_balls.c File Reference

Example of pushing polygons with SH4ZAM. More...

#include <kos.h>
#include <sh4zam/shz_sh4zam.h>
#include <stdlib.h>

Functions

static void cache_polygon_header (pvr_poly_hdr_t *poly_header)
 
static void submit_polygon_header (const pvr_poly_hdr_t *poly_header)
 Submits the polygon header for the ball vertex strip to the PowerVR GPU.
 
static void setup_projection_view (shz_mat4x4_t *mat)
 Sets up and caches our projection view matrix for use later.
 
static void apply_model_matrix (shz_vec3_t pos, shz_vec2_t rot, const shz_mat4x4_t *proj_view)
 
static void render_sphere (float radius, uint32_t base_color)
 Renders a single one of Bruce's balls.
 
static void init_balls (Ball *balls)
 
static bool check_exit (void)
 Polls to see whether the start button of the fist controller was pressed.
 
static void update_frame_stats (void)
 Calculates and intermittently reports frame statistics.
 
int main (int argc, const char *argv[])
 Program entry point.
 

Variables

constexpr unsigned BALL_COUNT = 93
 Number of balls to render (play with me!)
 
constexpr unsigned SPHERE_STACKS = 20
 Number of Z stacks which comprise the ball's geometry.
 
constexpr unsigned SPHERE_SLICES = 20
 Number of X/Y slices which comprise each stack of the ball's geometry.
 
constexpr unsigned TRIANGLES_PER_BALL = SPHERE_STACKS * SPHERE_SLICES * 2
 Total number of triangles which get rendered per ball.
 
constexpr unsigned TRIANGLES_TOTAL = TRIANGLES_PER_BALL * BALL_COUNT
 Total number of triangles rendered per scene, including all balls.
 

Detailed Description

Example of pushing polygons with SH4ZAM.

Welcome to Bruce's Balls!

The following example illustrates the use of SH4ZAM to accelerate rendering and vertex processing. It also serves as a performance benchmark at the high-end, capable of pushing 4.5 million polygons per second with the proper build flags.

The program simply renders a series of high-polygon balls and reports back the rendering performance in terms of polygons per second. It does so by using KOS's "Direct Rendering" PVR API, which submits geometry to the PowerVR GPU via filling and flushing the two store queues to the TA in alternation. Ths is the fastest rendering path.

Note
This example is written in C23.
Author
2025 BruceLeet
2025, 2026 Falco Girgis

Function Documentation

◆ apply_model_matrix()

static void apply_model_matrix ( shz_vec3_t pos,
shz_vec2_t rot,
const shz_mat4x4_t * proj_view )
static

Ensures the final transform for a given ball occupies XMTRX.

This is the matrix which each position vector of a ball gets transformed against.

References pos.

Referenced by main().

◆ cache_polygon_header()

static void cache_polygon_header ( pvr_poly_hdr_t * poly_header)
static

Precomputes and caches the polygon header used for drawing balls.

For maximum performance, do not waste time dynamically compiling polygon contexts into headers each frame. Do it once and cache the result!

Compile the polygon context into a polygon header for use later.

References cxt, PVR_LIST_OP_POLY, pvr_poly_compile(), and pvr_poly_cxt_col().

Referenced by main().

◆ check_exit()

static bool check_exit ( void )
static

Polls to see whether the start button of the fist controller was pressed.

References maple_dev_status(), maple_enum_type(), MAPLE_FUNC_CONTROLLER, and cont_state_t::start.

Referenced by main().

◆ init_balls()

static void init_balls ( Ball * balls)
static

Initializes Bruce's balls.

Precalculates the balls' attributes, aligning them to a grid and givign them intial orientation and velocity values.

References BALL_COUNT, and y.

Referenced by main().

◆ main()

◆ render_sphere()

static void render_sphere ( float radius,
uint32_t base_color )
static

◆ setup_projection_view()

static void setup_projection_view ( shz_mat4x4_t * mat)
static

Sets up and caches our projection view matrix for use later.

Referenced by main().

◆ submit_polygon_header()

static void submit_polygon_header ( const pvr_poly_hdr_t * poly_header)
static

Submits the polygon header for the ball vertex strip to the PowerVR GPU.

References pvr_dr_target.

Referenced by main().

◆ update_frame_stats()

static void update_frame_stats ( void )
static

Calculates and intermittently reports frame statistics.

Structure containing statistics we're measuring.

< Timestamp from the last time this routine was called.

< The number of rames that have run within the last second.

< The last calculated framerate.

< Static instance of statistics, which persists between frames.

References printf, stats(), timer_ns_gettime64(), and TRIANGLES_TOTAL.

Referenced by main().

Variable Documentation

◆ BALL_COUNT

unsigned BALL_COUNT = 93
constexpr

Number of balls to render (play with me!)

Referenced by init_balls(), and main().

◆ SPHERE_SLICES

unsigned SPHERE_SLICES = 20
constexpr

Number of X/Y slices which comprise each stack of the ball's geometry.

Referenced by render_sphere().

◆ SPHERE_STACKS

unsigned SPHERE_STACKS = 20
constexpr

Number of Z stacks which comprise the ball's geometry.

Referenced by render_sphere().

◆ TRIANGLES_PER_BALL

unsigned TRIANGLES_PER_BALL = SPHERE_STACKS * SPHERE_SLICES * 2
constexpr

Total number of triangles which get rendered per ball.

◆ TRIANGLES_TOTAL

unsigned TRIANGLES_TOTAL = TRIANGLES_PER_BALL * BALL_COUNT
constexpr

Total number of triangles rendered per scene, including all balls.

Referenced by main(), and update_frame_stats().