Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
278 commits
Select commit Hold shift + click to select a range
5fc12f4
directional lighting
snacchus Jun 2, 2024
67366c4
point lights
snacchus Jun 3, 2024
f743b33
fixed last two vertices being corrupted
snacchus Jun 4, 2024
6282eac
fog
snacchus Jun 6, 2024
7d77edc
rename magma_fixed_function -> mgfx
snacchus Jun 8, 2024
4b4f2f7
fix normal matrix in mgdemo
snacchus Jun 8, 2024
c60a611
Emit triangles asynchronously
snacchus Jun 9, 2024
ec66304
fix some vadd ops being influenced randomly
snacchus Jun 9, 2024
b4a8155
move clip space position to end of vertex struct
snacchus Jun 10, 2024
3569601
implement clipping
snacchus Jun 12, 2024
1420dd8
add missing autosync call
snacchus Jun 12, 2024
e28093d
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Jun 16, 2024
4cd630b
expose clip factor
snacchus Jun 16, 2024
941220c
add profiling to mgdemo
snacchus Jun 16, 2024
29e003e
expose some more functions + housekeeping
snacchus Jun 16, 2024
15bcaeb
move vertex macro to mgfx + housekeeping
snacchus Jun 17, 2024
6f0829f
add README to mgdemo
snacchus Jun 17, 2024
037e045
add mghello example
snacchus Jun 17, 2024
0ea3d05
renamings + more comments in mgdemo
snacchus Jun 17, 2024
8b3c5dd
fix mg_draw_indexed
snacchus Jun 22, 2024
12ce2c9
all tests green
snacchus Jun 23, 2024
dc49375
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Jun 23, 2024
c654fa7
n64elfcompress: add missing .gitignore
snacchus Jun 29, 2024
e4861bc
model64: add support for different vertex formats
snacchus Jun 29, 2024
bb6f039
refactor buffer API
snacchus Jun 29, 2024
c3547f6
mgdemo: first attempt at integrating model64
snacchus Jun 29, 2024
1ea5f2b
mgdemo: better model64 support, improved models
snacchus Jun 30, 2024
98a5380
remove some TODOs
snacchus Jun 30, 2024
e7908eb
fix uniforms not being saved on overlay switch
snacchus Jun 30, 2024
b96550b
add TODO
snacchus Jul 1, 2024
518a444
add debug overlay to mgdemo
snacchus Jul 12, 2024
c6d5414
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Jul 12, 2024
402069b
improve overlay
snacchus Jul 12, 2024
d61bfa1
mgdemo: make object count dynamic
snacchus Jul 13, 2024
6b056fa
mgdemo: use new delta time API
snacchus Jul 13, 2024
8ea7af1
mgdemo: add camera controls
snacchus Jul 13, 2024
87280aa
remove buffer flags
snacchus Jul 14, 2024
e523415
remove some TODOs
snacchus Jul 14, 2024
eeb3606
prepare environment mapping
snacchus Jul 14, 2024
b78c948
Move clipping code to a sub-overlay
snacchus Jul 14, 2024
6b5c97b
add enviroment mapping
snacchus Jul 14, 2024
b38bcd0
mgdemo: add environment mapped materials
snacchus Jul 14, 2024
fd46119
mgdemo: fix flickering in debug overlay
snacchus Jul 14, 2024
dc4ea42
fix saturation bug in env mapping
snacchus Jul 14, 2024
a139d8b
mgdemo: fix env texture getting flipped vertically
snacchus Jul 14, 2024
732e519
mgdemo: add controls and profiler info to readme
snacchus Jul 14, 2024
382c1ca
remove matrix palette API for now
snacchus Jul 15, 2024
6ab89aa
make GL ucode compatible with rspq_profile
snacchus Jul 15, 2024
a8d7737
manage TODOs
snacchus Jul 15, 2024
ed5f572
add more TODOs
snacchus Jul 15, 2024
0fc0c61
add more TODOs
snacchus Jul 15, 2024
a2cc506
improve perf by shortening MgCmd_DrawIndices
snacchus Jul 19, 2024
719d3e3
remove TODO
snacchus Jul 19, 2024
e816650
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Jul 20, 2024
dba3d08
use RDPQ_Triangle jump labels to improve perf
snacchus Jul 20, 2024
4c42641
mgdemo: add controls to toggle animation
snacchus Jul 20, 2024
cdbe39f
prepare for flexible vertex layout
snacchus Jul 21, 2024
8f05cb0
remove leftover tracing
snacchus Jul 21, 2024
899a3fb
fix unaligned vertex sizes
snacchus Jul 21, 2024
cb686d3
remove "libdragon.h" from source files
snacchus Jul 21, 2024
2cdf37b
save shader code pointer and size directly
snacchus Jul 21, 2024
6e78cb1
customizable vertex layout: first stage
snacchus Jul 25, 2024
ff20a29
rsp: add "meta" section
snacchus Jul 28, 2024
1ba8b78
save uniform definitions in ucode meta section
snacchus Jul 28, 2024
f29d6c6
generically configurable vertex layout
snacchus Aug 13, 2024
3673904
adapt examples to vertex layout changes
snacchus Aug 13, 2024
1a0800d
validate vertex layout configuration + refactoring
snacchus Aug 13, 2024
1526d50
remove completed TODO
snacchus Aug 13, 2024
4d1cabe
update TODO
snacchus Aug 13, 2024
bbaa9d2
attempt implementing binary compatibility check
snacchus Aug 13, 2024
eccb74d
remove some unneeded constants
snacchus Aug 13, 2024
765be46
revert unintended change
snacchus Aug 13, 2024
1c29a90
housekeeping: mark internal functions static
snacchus Aug 13, 2024
bce09d4
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Aug 13, 2024
e6dc486
perspective normalization
snacchus Aug 15, 2024
647c2c7
adapt to upstream changes
snacchus Aug 15, 2024
504388f
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Aug 15, 2024
0fa5287
fix multiplication overflow when loading vertices
snacchus Aug 16, 2024
2cd3880
mgdemo: change camera to flying controls
snacchus Aug 17, 2024
62b9aca
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Aug 17, 2024
b831760
housekeeping: consistently use "mg" prefix
snacchus Aug 17, 2024
3a56c37
mkmodel: optimize meshes
snacchus Aug 20, 2024
5e4dfab
mkmodel: fix preprocessing bug
snacchus Aug 22, 2024
5f7997a
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Aug 22, 2024
64d2ada
fix missing include in dragonfs.c
snacchus Aug 23, 2024
68c544e
fix rsp header generation for new toolchain
snacchus Aug 23, 2024
ddbd76b
mkmodel: fix debug message always being printed
snacchus Aug 23, 2024
909c787
WIP ucode optimization
snacchus Aug 24, 2024
72e8542
WIP ucode optimisation
snacchus Aug 24, 2024
e553471
removed branches, converted VU to SU ops
snacchus Sep 1, 2024
a58ef34
switch SU and VU
snacchus Sep 3, 2024
479bb77
pipelining second attempt WIP
snacchus Sep 6, 2024
94f768f
WIP
snacchus Sep 7, 2024
2a8175c
WIP
snacchus Sep 11, 2024
ba12ad2
WIP
snacchus Sep 14, 2024
a8fc9b4
WIP
snacchus Sep 14, 2024
b8297ff
finish ucode optimisation
snacchus Sep 14, 2024
b807e17
fix light color
snacchus Sep 14, 2024
24efa8f
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Sep 14, 2024
f4d18ff
remove libdragon include from kernel.c
snacchus Oct 3, 2024
f70c4d8
remove resource sets from API
snacchus Oct 3, 2024
12d88fc
remove buffers from API
snacchus Oct 3, 2024
445941c
inline mg_bind_vertex_buffer
snacchus Oct 3, 2024
d1e67b6
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Oct 3, 2024
a67ffea
fix compiler error in mkmodel
snacchus Oct 3, 2024
e047e72
mg_init and mg_close can be called multiple times
snacchus Oct 3, 2024
536c441
Documentation
snacchus Oct 6, 2024
a4a9c9e
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Oct 6, 2024
59df7d0
rename some functions
snacchus Oct 7, 2024
730eed4
More documentation
snacchus Oct 7, 2024
2dd525c
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Oct 7, 2024
af01508
Fix list in docs + clarify guide
snacchus Oct 7, 2024
2838e42
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Oct 9, 2024
98c536b
Merge branch 'preview' into magma
snacchus Oct 11, 2024
384868a
Merge branch 'preview' into magma
snacchus Oct 21, 2024
c4ed3c4
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Oct 29, 2024
981538d
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Oct 30, 2024
000a538
fix magma triangles
snacchus Oct 31, 2024
d244b5d
fix clipping bug
snacchus Nov 2, 2024
5fc4b5d
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Nov 2, 2024
01e6b69
Merge branch 'preview' into magma
snacchus Nov 24, 2024
d6cf4c5
fmath: more functions, bugfixes + tests
snacchus Nov 27, 2024
c752b8a
magma: integrate with fgeom
snacchus Nov 27, 2024
d2ee1d4
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Nov 27, 2024
46158ec
Merge branch 'preview' into magma
snacchus Dec 1, 2024
c53d8d1
Merge branch 'preview' into magma
snacchus Dec 16, 2024
afd39b8
mgfx tool WIP
snacchus Dec 20, 2024
db506d5
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Dec 31, 2024
22b7ca9
mgfx: fix various bugs
snacchus Jan 1, 2025
7b9fedb
magma: add extern inlines
snacchus Jan 1, 2025
d59a8d1
mgfx_mesh: add function for submesh count
snacchus Jan 1, 2025
50ded92
install mgfx_mesh.h
snacchus Jan 1, 2025
f792949
mgdemo: switch to new mesh importer
snacchus Jan 1, 2025
c5dc03e
remove intermediate file
snacchus Jan 1, 2025
fe89c05
revert changes to model64
snacchus Jan 1, 2025
f79066a
vifx: add .gitignore
snacchus Jan 1, 2025
1bf4027
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Jan 1, 2025
811b521
port mgfx to rspl
snacchus Feb 9, 2025
857e490
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Feb 9, 2025
41738e5
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Feb 11, 2025
cd65d57
improve perf
snacchus Feb 17, 2025
54b0694
include constants from headers
snacchus Feb 19, 2025
1b8dc9f
mkmaterial: add missing gitignore
snacchus Feb 19, 2025
c837e76
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Feb 19, 2025
4684fad
split environment mapping into variant
snacchus Feb 23, 2025
351d512
fix zero lights case
snacchus Feb 24, 2025
bcb9859
adapt examples
snacchus Feb 24, 2025
93c618a
fix fog
snacchus Feb 26, 2025
d738c62
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Feb 26, 2025
3bc7a7d
increase vertex cache size
snacchus Mar 1, 2025
bfcbe5e
reorder uniforms
snacchus Mar 1, 2025
ad21a80
re-added point lights
snacchus Mar 2, 2025
f251616
improve perf
snacchus Mar 2, 2025
f1b12b6
improve perf again
snacchus Mar 2, 2025
e097d0f
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Mar 2, 2025
03e238b
revert optimization flags in tools
snacchus Mar 3, 2025
1c5e69c
Merge branch 'preview' into magma
snacchus Mar 3, 2025
40cbc1d
re-adapt gitignore to preview
snacchus Mar 3, 2025
e31d739
rename shader
snacchus Mar 3, 2025
71448d5
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Mar 14, 2025
5ead1ed
Merge branch 'preview' into magma
snacchus Apr 26, 2025
db2f327
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Jul 13, 2025
bd231ff
magma: adjust to upstream changes in Makefile
snacchus Jul 13, 2025
2591f73
magma: call rspq_init in mg_init
snacchus Jul 13, 2025
30a0030
start reimplementing GL on top of magma
snacchus Jul 13, 2025
9b0b63d
basic VBO functionality
snacchus Jul 17, 2025
d95b4d7
magma: fix negative viewport size
snacchus Jul 19, 2025
f823fea
mgfx: fix shade being black when fog is turned off
snacchus Jul 19, 2025
1011a0d
GL: matrix stack
snacchus Jul 19, 2025
189bbc6
magma: fix vertex cache overflow
snacchus Jul 20, 2025
8bd265d
add perf overlay
snacchus Jul 20, 2025
ebff386
extract near and far plane
snacchus Jul 20, 2025
851fd6d
GL: cache display list generated from index buffer
snacchus Jul 21, 2025
5aaba69
refactor
snacchus Jul 21, 2025
9ac235b
cache range of converted vertex data
snacchus Jul 26, 2025
22bd401
fix memory leak in element cache
snacchus Jul 26, 2025
3b04feb
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Jul 26, 2025
948f23a
Merge branch 'magma' into magma-GL
snacchus Jul 26, 2025
2b115e8
add missing gitignore
snacchus Jul 26, 2025
59ef380
re-convert vertex data when VBO is modified
snacchus Jul 27, 2025
004887a
small improvements and fixes
snacchus Jul 27, 2025
8ab5024
implement reference counter in buffer objects
snacchus Jul 27, 2025
70cdfb0
fix normal conversion
snacchus Jul 29, 2025
c963256
mgfx: remove unnecessary const clauses
snacchus Jul 29, 2025
7ea1353
implement lighting
snacchus Jul 29, 2025
7f7b36a
improve fog
snacchus Jul 29, 2025
23a73f8
add stuff to gldemo
snacchus Jul 29, 2025
5ca8ba0
fix objects not being deletable individually
snacchus Aug 2, 2025
ddca972
implement texture
snacchus Aug 3, 2025
e151b1b
add miscellaneous functions
snacchus Aug 3, 2025
29c8734
implement render modes
snacchus Aug 3, 2025
059306c
various improvements
snacchus Aug 5, 2025
4f0ad39
add packed normal format
snacchus Aug 5, 2025
c41df11
add missing fixed point precision functions
snacchus Aug 5, 2025
664b79f
refactor pipeline creation
snacchus Aug 6, 2025
1561b12
Merge remote-tracking branch 'upstream/preview' into magma-GL
snacchus Aug 6, 2025
71b5ff8
cache pipelines in hashtable
snacchus Aug 6, 2025
3ba69ad
Merge remote-tracking branch 'upstream/preview' into magma-GL
snacchus Aug 7, 2025
2fbf783
free pipelines
snacchus Aug 7, 2025
0521a77
implement sphere mapping
snacchus Aug 7, 2025
d779102
implement tex transform
snacchus Aug 8, 2025
68e7902
set zmode
snacchus Aug 8, 2025
b33f650
implement glBegin/glEnd
snacchus Aug 10, 2025
5d36853
Merge remote-tracking branch 'upstream/preview' into magma-GL
snacchus Aug 10, 2025
2cb5c35
add some missing functions
snacchus Aug 10, 2025
8d0ed73
implement GL_RDPQ_TEXTURING_N64
snacchus Aug 10, 2025
582cc41
correctly synchronize uploading uniforms
snacchus Aug 10, 2025
3634279
delete old GL files
snacchus Aug 10, 2025
02909da
add dummy implementation for lists
snacchus Aug 11, 2025
664087c
fix some bugs
snacchus Aug 11, 2025
1763105
refactor dirty flags
snacchus Aug 13, 2025
cec37cf
refactor enable flags and hints
snacchus Aug 17, 2025
e033010
split rendermode into multiple states
snacchus Aug 17, 2025
c37f4f7
refactor fog and scissor
snacchus Aug 17, 2025
8d8210e
refactor viewport
snacchus Aug 17, 2025
fc9e1e6
implement RDPQ material
snacchus Aug 17, 2025
be74e15
introduce state system
snacchus Aug 19, 2025
a0dec99
small refactoring
snacchus Aug 23, 2025
dc7037d
refactor state system
snacchus Aug 23, 2025
b77a2a7
minor refactoring
snacchus Aug 24, 2025
2bb7e6b
reorder state
snacchus Aug 24, 2025
b8b33a0
fix unit tests
snacchus Aug 25, 2025
7dac18d
minor changes to gldemo
snacchus Aug 25, 2025
7da0165
Merge remote-tracking branch 'upstream/preview' into magma-GL
snacchus Aug 25, 2025
97c6ec9
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Aug 25, 2025
98bfb0e
add GL/texture.h
snacchus Sep 11, 2025
cb5e2f2
reset GL state to zero on cleanup
snacchus Sep 11, 2025
b95e7b2
fix GL_FRONT_AND_BACK
snacchus Sep 11, 2025
ed00d66
magma: add back padding after vertex cache
snacchus Sep 11, 2025
9c320c6
make magma tests work with variable cache size
snacchus Sep 11, 2025
a12af62
fix misaligned read during vertex conversion
snacchus Sep 11, 2025
bf7acb2
make mgfx_mesh support multiple meshes per file
snacchus Oct 4, 2025
3d06481
rename tool mgfx to mkmesh
snacchus Oct 4, 2025
a6edd95
Merge remote-tracking branch 'upstream/preview' into magma-GL
snacchus Oct 4, 2025
f150f4c
remove old gldemo
snacchus Oct 5, 2025
90a5a13
set gldemo up for profiling
snacchus Oct 5, 2025
20b1968
use new zbuffer in mgdemo
snacchus Oct 5, 2025
b0576b6
Merge remote-tracking branch 'upstream/preview' into magma-GL
snacchus Oct 5, 2025
0cba6e2
share mesh conversion between mkmodel + mkmesh
snacchus Oct 8, 2025
0f9e94c
write missing mesh index
snacchus Oct 8, 2025
bccb2bb
Merge remote-tracking branch 'upstream/preview' into magma-GL
snacchus Oct 8, 2025
898e0a3
model64: remove textures
snacchus Oct 8, 2025
805a712
Merge remote-tracking branch 'upstream/preview' into magma-GL
snacchus Oct 11, 2025
51cc641
fix testrom
snacchus Oct 11, 2025
efc7919
model64: add material support via mkmaterial
snacchus Oct 11, 2025
3166df5
WIP parse f3d material in mkmaterial
snacchus Oct 12, 2025
d66cf14
Merge remote-tracking branch 'upstream/preview' into magma
snacchus Oct 12, 2025
1a1d9b5
Merge branch 'magma' into magma-GL
snacchus Oct 12, 2025
47e2293
cherry pick changes from magma-GL
snacchus Oct 12, 2025
feb14af
Merge branch 'magma' into magma-GL
snacchus Oct 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ include $(SOURCE_DIR)/GL/libdragon.mk
include $(SOURCE_DIR)/video/libdragon.mk
include $(SOURCE_DIR)/rspq/libdragon.mk
include $(SOURCE_DIR)/rdpq/libdragon.mk
include $(SOURCE_DIR)/magma/libdragon.mk
include $(SOURCE_DIR)/math/libdragon.mk
include $(SOURCE_DIR)/compress/libdragon.mk

# TODO: Make this generically available in n64.mk somehow
$(SOURCE_DIR)/magma/rsp_magma.h: $(BUILD_DIR)/magma/rsp_magma.o
$(N64_NM) -n $(BUILD_DIR)/magma/rsp_magma.elf \
| awk 'BEGIN {print("#ifndef __RSP_MAGMA_SYMBOLS\n#define __RSP_MAGMA_SYMBOLS") } $$2 ~ /[dDbtT]/ {printf("#define RSP_MAGMA_%s 0x%s\n", $$3, substr($$1,5,4))} END {print("#endif")}' \
> $@

$(BUILD_DIR)/magma/magma.o: $(SOURCE_DIR)/magma/rsp_magma.h

libdragon.a: $(LIBDRAGON_OBJS)

%.a:
Expand Down
2 changes: 2 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ EXAMPLES += fontgallery
EXAMPLES += gldemo
EXAMPLES += joypadtest
EXAMPLES += loadspritefromsd
EXAMPLES += mgdemo
EXAMPLES += mghello
EXAMPLES += micro-ui
EXAMPLES += mixertest
EXAMPLES += overlays
Expand Down
11 changes: 9 additions & 2 deletions examples/gldemo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ include $(N64_INST)/include/n64.mk

src = gldemo.c
assets_png = $(wildcard assets/*.png)
assets_glb = $(wildcard assets/*.glb)

assets_conv = $(addprefix filesystem/,$(notdir $(assets_png:%.png=%.sprite)))
assets_conv = $(addprefix filesystem/,$(notdir $(assets_png:%.png=%.sprite))) \
$(addprefix filesystem/,$(notdir $(assets_glb:%.glb=%.model64)))

MKSPRITE_FLAGS ?=

Expand All @@ -13,7 +15,12 @@ all: gldemo.z64
filesystem/%.sprite: assets/%.png
@mkdir -p $(dir $@)
@echo " [SPRITE] $@"
@$(N64_MKSPRITE) -f RGBA16 --compress -o "$(dir $@)" "$<"
@$(N64_MKSPRITE) -f RGBA16 -c -o "$(dir $@)" "$<"

filesystem/%.model64: assets/%.glb
@mkdir -p $(dir $@)
@echo " [MODEL] $@"
@$(N64_MKMODEL) -c 0 -o "$(dir $@)" "$<"

$(BUILD_DIR)/gldemo.dfs: $(assets_conv)
$(BUILD_DIR)/gldemo.elf: $(src:%.c=$(BUILD_DIR)/%.o)
Expand Down
Binary file added examples/gldemo/assets/scene.glb
Binary file not shown.
Binary file added examples/gldemo/assets/unit1m.i8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
330 changes: 330 additions & 0 deletions examples/gldemo/debug_overlay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
/**
* @copyright 2024 - Max Bebök (modified by snacchus)
* @license MIT
*/
#ifndef DEBUG_OVERLAY_H
#define DEBUG_OVERLAY_H

#include <libdragon.h>
#include <rspq_profile.h>

#define DEBUG_OVERLAY_FONT_ID UINT8_MAX
#define DEBUG_OVERLAY_DEFAULT_STYLE_ID 0
#define DEBUG_OVERLAY_ACCENT_STYLE_ID 1
#define DEBUG_OVERLAY_MUTED_STYLE_ID 2
#define DEBUG_OVERLAY_DARK_STYLE_ID 3
#define DEBUG_OVERLAY_TEXT_YOFFSET 10

typedef struct {
uint32_t calls;
int32_t timeUs;
uint32_t index;
color_t color;
bool isIdle;
const char *name;
} ProfileSlot;

const static color_t THEME_COLORS[10] = {
(color_t){ 0xd4, 0x3d, 0x51, 0xff},
(color_t){ 0xea, 0x7e, 0x54, 0xff},
(color_t){ 0xf7, 0xb8, 0x6f, 0xff},
(color_t){ 0xff, 0xee, 0xa1, 0xff},
(color_t){ 0xb4, 0xce, 0x85, 0xff},
(color_t){ 0x6a, 0xab, 0x75, 0xff},
(color_t){ 0x00, 0x87, 0x6c, 0xff},
(color_t){ 0x00, 0x5a, 0x5a, 0xff},
(color_t){ 0x00, 0x3e, 0x5c, 0xff},
(color_t){ 0x00, 0x2c, 0x4e, 0xff}
};

static void debug_draw_line_hori(float x, float y, float width) {
rdpq_fill_rectangle(x, y, x + width, y + 1);
}

static void debug_draw_line_vert(float x, float y, float height) {
rdpq_fill_rectangle(x, y, x + 1, y + height);
}

static void debug_draw_color_rect(float x, float y, float width, float height, color_t color) {
rdpq_set_fill_color(color);
rdpq_fill_rectangle(x, y, x + width, y + height);
}

static void draw_circle_slice(float x, float y, float radius, float startAngle, float endAngle, color_t color)
{
rdpq_set_prim_color(color);

float angleStep = 0.05f;
float x1 = x + fm_cosf(startAngle) * radius;
float y1 = y + fm_sinf(startAngle) * radius;
for(float angle = startAngle; angle < endAngle; angle += angleStep) {
float angleNext = angle + angleStep;
float x2 = x + fm_cosf(angleNext) * radius;
float y2 = y + fm_sinf(angleNext) * radius;
rdpq_triangle(&TRIFMT_FILL, (float[]){ x, y }, (float[]){ x1, y1 }, (float[]){ x2, y2 });
x1 = x2;
y1 = y2;
}
}

static void debug_profile_patch_name(ProfileSlot *slot)
{
switch (slot->index) {
case 0: slot->name = "builtins"; break;
case RSPQ_PROFILE_CSLOT_WAIT_CPU: slot->name = "CPU"; break;
case RSPQ_PROFILE_CSLOT_WAIT_RDP: slot->name = "RDP"; break;
case RSPQ_PROFILE_CSLOT_WAIT_RDP_SYNCFULL: slot->name = "SYNC_FULL"; break;
case RSPQ_PROFILE_CSLOT_WAIT_RDP_SYNCFULL_MULTI: slot->name = "multi SYNC_F"; break;
case RSPQ_PROFILE_CSLOT_OVL_SWITCH: slot->name = "Ovl switch"; break;
}
}

static int debug_profile_slot_compare(const void * a, const void * b) {
const ProfileSlot *slotA = (const ProfileSlot *)a;
const ProfileSlot *slotB = (const ProfileSlot *)b;
int timeDiff = slotB->timeUs - slotA->timeUs;
return timeDiff == 0 ? (slotB->calls - slotA->calls) : timeDiff;
}

static bool debug_profile_is_idle(uint32_t index) {
return index == RSPQ_PROFILE_CSLOT_WAIT_CPU || index == RSPQ_PROFILE_CSLOT_WAIT_RDP
|| index == RSPQ_PROFILE_CSLOT_WAIT_RDP_SYNCFULL || index == RSPQ_PROFILE_CSLOT_WAIT_RDP_SYNCFULL_MULTI;
}

static void debug_print_table_entry(ProfileSlot *slot, float posX, float *posY)
{
if(slot->calls != 0) {
rdpq_text_printf(NULL, DEBUG_OVERLAY_FONT_ID, posX, *posY+DEBUG_OVERLAY_TEXT_YOFFSET, "%-10.10s %5lu %7luu", slot->name, slot->calls, slot->timeUs);
} else {
rdpq_text_printf(NULL, DEBUG_OVERLAY_FONT_ID, posX, *posY+DEBUG_OVERLAY_TEXT_YOFFSET, "%-10.10s - %7luu", slot->name, slot->timeUs);
}
*posY += 10;
}


#define RCP_TICKS_TO_USECS(ticks) (((ticks) * 1000000ULL) / RCP_FREQUENCY)

static rspq_profile_data_t profile_data;

void debug_overlay_init()
{
rdpq_font_t *font = rdpq_font_load_builtin(FONT_BUILTIN_DEBUG_MONO);
rdpq_text_register_font(DEBUG_OVERLAY_FONT_ID, font);
rdpq_font_style(font, DEBUG_OVERLAY_ACCENT_STYLE_ID, &(rdpq_fontstyle_t) {
.color = RGBA32(0x99, 0x99, 0xEE, 0xFF)
});
rdpq_font_style(font, DEBUG_OVERLAY_MUTED_STYLE_ID, &(rdpq_fontstyle_t) {
.color = RGBA32(0xAA, 0xAA, 0xAA, 0xFF)
});
rdpq_font_style(font, DEBUG_OVERLAY_DARK_STYLE_ID, &(rdpq_fontstyle_t) {
.color = RGBA32(0x55, 0x55, 0x55, 0x99)
});
}

void debug_draw_perf_overlay(float measuredFps)
{
if(profile_data.frame_count == 0)return;

const float TABLE_POS_X = 104;
const float TABLE_POS_Y = 12;
const float FRAME_BARS_POS_Y = 178;

const float PIE_RADIUS = 33;
const float PIE_POS_BUSY[2] = {48, 48};
const float PIE_POS_WAIT[2] = {48, PIE_POS_BUSY[1] + PIE_RADIUS*2 + 7};

uint64_t totalTicks = 0;
uint32_t timeTotalBusy = 0;
uint32_t timeTotalWait = 0;

float posY = TABLE_POS_Y;
rdpq_text_print(NULL, DEBUG_OVERLAY_FONT_ID, TABLE_POS_X, posY+DEBUG_OVERLAY_TEXT_YOFFSET, "Tasks Calls Time");
posY += 12;

// Copy & convert performance data to a sortable array
const uint32_t SLOT_COUNT = RSPQ_PROFILE_SLOT_COUNT + 1;
ProfileSlot slots[SLOT_COUNT];
for(size_t i = 0; i < RSPQ_PROFILE_SLOT_COUNT; i++)
{
ProfileSlot *slot = &slots[i];
slot->index = i;
slot->isIdle = debug_profile_is_idle(i);
slot->name = profile_data.slots[i].name;

if(slot->name == NULL)continue;
debug_profile_patch_name(slot);

totalTicks += profile_data.slots[i].total_ticks;

slot->calls = (uint32_t)(profile_data.slots[i].sample_count / profile_data.frame_count);
slot->timeUs = RCP_TICKS_TO_USECS(profile_data.slots[i].total_ticks / profile_data.frame_count);

if(slot->name[0] == 'r' && slot->name[1] == 's' && slot->name[2] == 'p') {
slot->name += 4;
}

if(slot->isIdle) {
timeTotalWait += slot->timeUs;
} else {
timeTotalBusy += slot->timeUs;
}
}

// Calc. global times
uint64_t dispatchTicks = profile_data.total_ticks > totalTicks ? profile_data.total_ticks - totalTicks : 0;
uint64_t dispatchTime = RCP_TICKS_TO_USECS(dispatchTicks / profile_data.frame_count);
uint64_t rdpTimeBusy = RCP_TICKS_TO_USECS(profile_data.rdp_busy_ticks / profile_data.frame_count);

// Add dispatch time into the slots
slots[SLOT_COUNT - 1] = (ProfileSlot){
.index = SLOT_COUNT - 1,
.name = "Cmd process",
.calls = 0,
.timeUs = dispatchTime
};
timeTotalBusy += dispatchTime;

// Now sort for both the table and pie chart
qsort(slots, SLOT_COUNT, sizeof(ProfileSlot), debug_profile_slot_compare);

// Draw table (texts, busy)
uint32_t colorIndex = 0;
for(size_t i = 0; i < SLOT_COUNT; i++)
{
if(slots[i].isIdle || slots[i].name == NULL)continue;
slots[i].color = THEME_COLORS[colorIndex % 8];
colorIndex++;
debug_print_table_entry(&slots[i], TABLE_POS_X, &posY);
}

posY += 2; float endSectionOvlY = posY;
posY += 1;

// Table - Total Waits
rdpq_text_printf(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_ACCENT_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, TABLE_POS_X, posY+DEBUG_OVERLAY_TEXT_YOFFSET, "Total (busy) %7ldu", (uint32_t)timeTotalBusy);

posY += 12; float endSectionTotalBusyY = posY;
posY += 6;

colorIndex = 4;
for(size_t i = 0; i < SLOT_COUNT; i++)
{
if(!slots[i].isIdle || slots[i].name == NULL)continue;
slots[i].color = THEME_COLORS[colorIndex % 8];
colorIndex++;
debug_print_table_entry(&slots[i], TABLE_POS_X, &posY);
}

posY += 2; float endSectionCPUY = posY;
posY += 1;

// Table - Total Waits
rdpq_text_printf(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_ACCENT_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, TABLE_POS_X, posY+DEBUG_OVERLAY_TEXT_YOFFSET, "Total (waiting) %7ldu", (uint32_t)timeTotalWait);

posY += 12; float endSectionTotalWaitsY = posY;
posY += 10;

// Pie Chart
float angleOffset = -1.57079632679f;


rdpq_set_mode_standard();
rdpq_mode_combiner(RDPQ_COMBINER_FLAT);

if(profile_data.frame_count != 1)
{
for(int type=0; type<2; ++type)
{
for(size_t i = 0; i < RSPQ_PROFILE_SLOT_COUNT; i++)
{
ProfileSlot *slot = &slots[i];
if(slot->isIdle != type || slot->name == NULL)continue;
const float* pos = type == 0 ? PIE_POS_BUSY : PIE_POS_WAIT;
float refTotal = type == 0 ? timeTotalBusy : timeTotalWait;

float slotAngle = 0;
if(refTotal > 0)slotAngle = (float)slot->timeUs / (float)refTotal * 6.28318530718f;

if(slotAngle > 0.01f) {
draw_circle_slice(pos[0], pos[1], PIE_RADIUS, angleOffset, angleOffset + slotAngle, slot->color);
}
angleOffset += slotAngle;
}
}
}

// RCP performance bars
float timeScale = 1.0f / (200.0f); // 1 / (us per pixel)
posY = FRAME_BARS_POS_Y;
float barPos[2] = {48, posY+16};
const float BAR_HEIGHT = 10;
const float BAR_BORDER = 2;

float busyWidth = (float)timeTotalBusy * timeScale;
float idleWidth = (float)timeTotalWait * timeScale;
float rdpBusyWidth = (float)rdpTimeBusy * timeScale;

float posFps60 = (1000000.0f / 60.0f) * timeScale;
float posFps30 = (1000000.0f / 30.0f) * timeScale;
float posFps20 = (1000000.0f / 20.0f) * timeScale;

// bar (idle vs busy) - Text
rdpq_text_print(NULL, DEBUG_OVERLAY_FONT_ID, barPos[0]-30, posY+DEBUG_OVERLAY_TEXT_YOFFSET + 4 + (BAR_HEIGHT+BAR_BORDER), "RSP");
rdpq_text_print(NULL, DEBUG_OVERLAY_FONT_ID, barPos[0]-30, posY+DEBUG_OVERLAY_TEXT_YOFFSET + 4 + (BAR_HEIGHT+BAR_BORDER)*2, "RDP");

float fpsMarkerY = posY + 8 + (BAR_HEIGHT+BAR_BORDER)*3;
rdpq_text_print(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_MUTED_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, barPos[0]-30, fpsMarkerY+DEBUG_OVERLAY_TEXT_YOFFSET, "FPS Target:");

// FPS marker at bottom of lines
rdpq_text_printf(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_MUTED_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, barPos[0] + floorf(posFps60) - 14, fpsMarkerY+DEBUG_OVERLAY_TEXT_YOFFSET, "60");
rdpq_text_printf(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_MUTED_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, barPos[0] + floorf(posFps30) - 14, fpsMarkerY+DEBUG_OVERLAY_TEXT_YOFFSET, "30");
rdpq_text_printf(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_MUTED_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, barPos[0] + floorf(posFps20) - 14, fpsMarkerY+DEBUG_OVERLAY_TEXT_YOFFSET, "20");

rdpq_text_printf(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_ACCENT_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, barPos[0] + 120, posY+DEBUG_OVERLAY_TEXT_YOFFSET, "FPS: %.2f", measuredFps);
rdpq_text_printf(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_MUTED_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, barPos[0] + 208, posY+DEBUG_OVERLAY_TEXT_YOFFSET, "(f:%lld)", profile_data.frame_count);

// ======== FILL MODE ========

rdpq_set_mode_fill(RGBA32(0x22, 0x22, 0x22, 0xFF));

// table lines
const float LINE_POS_X = 94;
const float LINE_SIZE_X = 210;
rdpq_set_fill_color(RGBA32(44, 44, 44, 0xFF));
debug_draw_line_hori(LINE_POS_X, TABLE_POS_Y+11, LINE_SIZE_X);
debug_draw_line_hori(LINE_POS_X, endSectionOvlY, LINE_SIZE_X);
debug_draw_line_hori(LINE_POS_X+10, endSectionCPUY, LINE_SIZE_X-10);

debug_draw_line_hori(LINE_POS_X+10, endSectionTotalBusyY, LINE_SIZE_X-10);
debug_draw_line_hori(LINE_POS_X+10, endSectionTotalBusyY+2, LINE_SIZE_X-10);

debug_draw_line_hori(LINE_POS_X+10, endSectionTotalWaitsY, LINE_SIZE_X-10);
debug_draw_line_hori(LINE_POS_X+10, endSectionTotalWaitsY+2, LINE_SIZE_X-10);

// background till end of frame range
rdpq_fill_rectangle(barPos[0]-2, barPos[1]-2, barPos[0]+posFps20, barPos[1] + (BAR_HEIGHT + BAR_BORDER)*2);

// RSP busy + idle bar
debug_draw_color_rect(barPos[0], barPos[1], busyWidth, BAR_HEIGHT, RGBA32(0x44, 0x44, 0xAA, 0xFF));
debug_draw_color_rect(barPos[0] + busyWidth, barPos[1], idleWidth, BAR_HEIGHT, RGBA32(0xAA, 0xAA, 0xAA, 0xFF));

// RDP busy (purple)
debug_draw_color_rect(barPos[0], barPos[1] + BAR_HEIGHT + BAR_BORDER, rdpBusyWidth, BAR_HEIGHT, RGBA32(0xAA, 0x44, 0xAA, 0xFF));

// lines marking frame-rates
rdpq_set_fill_color(RGBA32(0xFF, 0xFF, 0xFF, 0xFF));
debug_draw_line_vert(barPos[0] + posFps60, barPos[1]-BAR_BORDER, BAR_HEIGHT + 30);
debug_draw_line_vert(barPos[0] + posFps30, barPos[1]-BAR_BORDER, BAR_HEIGHT + 30);
debug_draw_line_vert(barPos[0] + posFps20, barPos[1]-BAR_BORDER, BAR_HEIGHT + 30);

// total time on right side of bar
if((busyWidth+idleWidth) < 150) {
rdpq_text_printf(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_DARK_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, barPos[0]+198, posY+DEBUG_OVERLAY_TEXT_YOFFSET + 3 + (BAR_HEIGHT+BAR_BORDER), "%7luu", timeTotalBusy + timeTotalWait);
rdpq_text_printf(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_DARK_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, barPos[0]+198, posY+DEBUG_OVERLAY_TEXT_YOFFSET + 3 + (BAR_HEIGHT+BAR_BORDER)*2, "%7lluu", rdpTimeBusy);
} else {
rdpq_text_printf(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_DARK_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, barPos[0]+2, posY+DEBUG_OVERLAY_TEXT_YOFFSET + 3 + (BAR_HEIGHT+BAR_BORDER), "%luu", timeTotalBusy + timeTotalWait);
rdpq_text_printf(&(rdpq_textparms_t){.style_id = DEBUG_OVERLAY_DARK_STYLE_ID}, DEBUG_OVERLAY_FONT_ID, barPos[0]+2, posY+DEBUG_OVERLAY_TEXT_YOFFSET + 3 + (BAR_HEIGHT+BAR_BORDER)*2, "%lluu", rdpTimeBusy);
}
}

#endif //DEBUG_OVERLAY_H
Loading