Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions .agents/skills/mirai-agent-api/references/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ Reading a load:
`Prepare` and `Nav_Tiles` are summed over the job workers and can exceed the wall time.
- **A section** counts what its own load did until it joined, and also adds it to the scene or Play
that was loading then.
- **Pipelines.** `pipelines.compiled` with `store` `Read` means pipelines the kept ones did not
have: a material program seen for the first time, or keys a change of shaders, fixed state or
constants made new. A second start of the same build that still compiles them points at the
keys; `late_frames` counts the frames that drew materials as their rows meanwhile.

Reading a capture:

Expand Down Expand Up @@ -344,6 +348,12 @@ Reading a capture:
report of the task with the median and the spread of each build and the difference per pair with
its range, warns if another engine program was open or the GPU had another load, and leaves the
JSON in `--out`.
- **Compiling pipelines.** `mirai bench pipelines [project]` plays a cold start and then a warm
one with a window and prints their table: the start of the renderer, the pipelines the driver
compiled and the frames that lost them. Cold empties both caches: the kept pipelines go aside
and come back after, and the driver's own is pointed at an empty folder by its variables. The
table says when the cold start may not have been cold (the kept pipelines were read, or the
driver kept its own cache); no editor of the project may be open.

## History

Expand Down
1 change: 1 addition & 0 deletions .agents/skills/mirai-projects/references/projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ by its absolute path, which hands it to Git Bash ([launcher](#tool-and-machine))
| Capture without UI | `mirai capture [project] --out x.png [--reference r.png] [--repeat]` |
| Validate demo image and cost | `mirai render check [project] [--views a,b] [--quality] [--release]` |
| Measure the cost against main | `mirai bench compare [project] --base main` |
| Measure compiling pipelines | `mirai bench pipelines [project] [--scene S] [--frames N]` |
| Compare and crop images | `mirai image diff\|mottle\|stats\|montage\|crop\|zoom\|pick ...` |
| Measure only between measurements | `mirai measure -- <command> [arguments...]` |
| See who is building or measuring | `mirai locks` |
Expand Down
11 changes: 10 additions & 1 deletion .agents/skills/mirai-rendering/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,16 @@ description: Modify, diagnose and measure Mirai rendering - the `render` rendere
- No engine stage evaluates a Surface program except its own (`//! per surface program`): raster
uses the program's pipeline (`program_graphics`) and rays ask for it through the material points
(`points.slang`); the traversal evaluates only the alpha of programs with coverage. A frame does
not wait for a pipeline to be created: it draws the program as its row until it has it.
not wait for a pipeline to be created: it draws the program as its row until it has it, unless
`Graphics_Settings.shaders` is `Before_Playing`, where it waits for those that are not
speculative. A speculative pipeline (a variant of settings not in use) is made last, the
cheapest first, never on the first pipeline thread, and no frame waits for one, even after a
setting selects it: what a frame needs never waits behind one, and a short session still ends
some.
- With a window, the pipelines the renderer starts with compile on a thread while the window title
counts them. A window closed meanwhile, or a player or editor that quits while a pipeline is
inside the driver (`app_quit_compiling`), ends the process at once once the kept pipelines are
written: the user decided it, the one exception to the engine never ending the process.
- The passes read the G-buffer only with `include/gbuffer.slang`. A material's layers are prepared
only with `shading_layers`, and direct and indirect light compose them the same.
- A scene's image reaches the display only through the display LUT (`display_encode`); previews,
Expand Down
11 changes: 11 additions & 0 deletions editor/api_profile_load.odin
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "core:fmt"
import "core:strings"
import "mirai:editor/api"
import "mirai:host"
import "mirai:rhi"
import "mirai:std/base"

// profile.load: where the seconds of the last loads went (host.Load_Profile),
Expand Down Expand Up @@ -46,6 +47,13 @@ Api_Profile_Load_Wait :: struct {
until_ms:f32 `mirai:"label=Since it began: the end of the last frame it was busy"`,
frames:int `mirai:"label=Frames it was busy"`,
}
Api_Profile_Load_Pipelines :: struct {
made:int `mirai:"label=Pipelines the device created while it loaded; the first scene of a start counts the start of the renderer"`,
compiled:int `mirai:"label=Of them, those the driver compiled: not read from the pipelines kept across runs"`,
driver_ms:f32 `mirai:"label=What the driver took for them, summed over the threads"`,
late_frames:int `mirai:"label=Frames that drew materials without their pipelines (as their rows) while they compiled"`,
store:rhi.Pipeline_Store_State `mirai:"label=What the device found of the kept pipelines: Missing on a first start, Other_Device after an update of the driver"`,
}
Api_Profile_Load :: struct {
kind:host.Load_Kind,
state:host.Load_State `mirai:"label=Loading: not resident yet, ask again; Interrupted: another load began first"`,
Expand All @@ -62,6 +70,7 @@ Api_Profile_Load :: struct {
slowest:[]Api_Profile_Load_Asset `mirai:"label=The slowest assets, the slowest first"`,
functions:[]Api_Profile_Load_Function `mirai:"label=The loop functions that cost the most while it loaded; none for a section"`,
waits:[]Api_Profile_Load_Wait `mirai:"label=What it waited for before it was resident, the one that ended last first"`,
pipelines:Api_Profile_Load_Pipelines,
notes:[]string `mirai:"label=How to read it and what to look at first"`,
}
Api_Profile_Loads :: struct {
Expand Down Expand Up @@ -117,6 +126,7 @@ profile_load_out :: proc(s:^Session,l:host.Load_Summary,manual:bool) -> (out:Api
for f,i in l.functions {out.functions[i]={strings.clone(f.name),f32(f.ms)}}
out.waits=make([]Api_Profile_Load_Wait,len(l.waits))
for w,i in l.waits {out.waits[i]={w.busy,f32(w.until_ms),w.frames}}
out.pipelines={l.pipelines.made,l.pipelines.made-l.pipelines.kept,f32(host.load_ms(l.pipelines.ns)),int(l.pipelines.late),l.store}
out.notes=profile_load_notes(l,manual)
return
}
Expand Down Expand Up @@ -173,5 +183,6 @@ profile_load_notes :: proc(l:host.Load_Summary,manual:bool) -> []string {
}
if len(l.functions)>0 {append(&notes,"functions are the loop functions of the frames while it loaded: the physics, collision and navigation phases are part of physics_step and navigation_build")}
if l.kind==.Section {append(&notes,"A section counts what its own load did until it joined the world; it also counts in the scene or Play that was loading then")}
if note:=host.load_pipelines_note(l);note!="" {append(&notes,note)}
return notes[:]
}
1 change: 1 addition & 0 deletions editor/editor.odin
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ close :: proc(s:^Session) -> int {
session_destroy(s)
host.app_world_free(app,edit_world)
host.app_editor_free(app,editor_world)
host.app_quit_compiling(app,code)
host.app_destroy(app)
return code
}
Expand Down
3 changes: 2 additions & 1 deletion editor/graphics_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ graphics_rows_are_the_tagged_fields_of_the_settings_of_the_machine :: proc(t:^te
s:=Session{info=&info}
defer free_all(context.temp_allocator)
rows:=graphics_rows_of(&s)
if !testing.expect_value(t,len(rows),15) {return}
if !testing.expect_value(t,len(rows),16) {return}
Case :: struct {label:string, change:proc(g:^erender.Graphics_Settings), choice:string, rate:bool}
cases:=[?]Case{
{"Resolution",proc(g:^erender.Graphics_Settings) {g.resolution=1440},"1440",false},
Expand All @@ -30,6 +30,7 @@ graphics_rows_are_the_tagged_fields_of_the_settings_of_the_machine :: proc(t:^te
{"Frame limit",proc(g:^erender.Graphics_Settings) {g.frame_limit=0},"Off",true},
{"Frame generation",proc(g:^erender.Graphics_Settings) {g.frame_generation=false},"Off",true},
{"Show FPS",proc(g:^erender.Graphics_Settings) {g.show_fps=true},"On",true},
{"Shaders",proc(g:^erender.Graphics_Settings) {g.shaders=.Before_Playing},"Before playing",true},
}
for c in cases {
g:=erender.GRAPHICS_SETTINGS_DEFAULT
Expand Down
5 changes: 5 additions & 0 deletions engine/render/graphics.odin
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Textures :: enum u8 {Low,Medium,High,Ultra}
// How far meshes keep their detail (GRAPHICS_DETAIL_PIXELS), and how far and dense a
// plugin draws the scattered detail of a scene (foliage).
Detail :: enum u8 {Low,Medium,High,Ultra}
// When the driver compiles the pipelines of what is drawn: while the frames
// draw without them, or before, the frames waiting for every one.
Shader_Compilation :: enum u8 {While_Playing,Before_Playing}

@(storage,shared,setting="machine")
Graphics_Settings :: struct {
Expand All @@ -41,6 +44,7 @@ Graphics_Settings :: struct {
frame_limit:u32 `mirai:"section=Frames,graphics=Frame limit,choices=30|60|120|144|240|Off:0"`, // frames per second; 0: none
vsync:bool `mirai:"graphics=VSync"`,
frame_generation:bool `mirai:"graphics=Frame generation"`, // a technique that generates frames (FSR 3.1) shows one between each two, with the memory of its passes and half a frame more of latency; frame_limit and vsync count the frames shown
shaders:Shader_Compilation `mirai:"graphics=Shaders"`,
show_fps:bool `mirai:"graphics=Show FPS"`, // the frames per second in the editor and the game
texture_memory:u32 `mirai:"min=0,max=65536,unit=MB,section=Memory,advanced"`, // GPU memory of textures, past which they load without their largest levels; 0: a share of the device's (GRAPHICS_TEXTURE_POOL_SHARE)
geometry_memory:u32 `mirai:"min=0,max=4096,unit=MB,advanced"`, // GPU memory of the pages of streamed meshes, taken when the first one loads: a later change waits until the renderer starts again; 0: that of gpu.DEFAULT_SETTINGS
Expand Down Expand Up @@ -144,6 +148,7 @@ graphics_render_settings :: proc(settings:gpu.Settings,quality:Graphics_Settings
s.reflection_light_budget=min(s.light_budget,GRAPHICS_REFLECTION_LIGHTS)
s.colored_shadows=quality.glass_shadows
s.glass_layers=clamp(quality.glass_layers,1,gpu.GLASS_LAYERS_MAX)
s.pipelines_first=quality.shaders==.Before_Playing
s.texture_max_size=sizes[quality.textures]
s.lod_error_pixels=pixels[quality.detail]
switch {
Expand Down
16 changes: 2 additions & 14 deletions host/app.odin
Original file line number Diff line number Diff line change
Expand Up @@ -611,17 +611,5 @@ app_world_ref :: proc(app:^App,data:rawptr) -> core.World_Ref {return core.world
// Seconds of real time since the application was created.
app_seconds :: proc(app:^App) -> f64 {return f64(platform.clock_ns()-app.start_ns)/1e9}

USER_ORGANIZATION :: "Mirai"

// What the project keeps for its user, the saves and the compiled GPU
// pipelines: <user data>/Mirai/<name of the project>, made when missing. ""
// without a project or a folder of user data.
app_user_folder :: proc(root:string,allocator:=context.allocator) -> string {
if root=="" {return ""}
name:=filepath.base(root)
if manifest,error:=project.manifest_read(project.path(root,project.PROJECT_FILE,allocator=context.temp_allocator));error.code==.None {
if declared:=base.text_get(manifest.name[:]);declared!="" {name=strings.clone(declared,context.temp_allocator)}
}
folder,_:=platform.path_user_data_folder(USER_ORGANIZATION,name,allocator)
return folder
}
// What the project keeps for its user (project.user_folder).
app_user_folder :: proc(root:string,allocator:=context.allocator) -> string {return project.user_folder(root,allocator)}
13 changes: 9 additions & 4 deletions host/frame.odin
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package host

import "base:runtime"
import "core:c"
import "core:fmt"
import "core:log"
import "core:math"
import "core:os"
Expand All @@ -11,6 +12,7 @@ import "core:time"
import audio "mirai:audio"
import "mirai:core"
import "mirai:platform"
import "mirai:project"
import render "mirai:render"
import "mirai:rhi"
import "mirai:std/base"
Expand All @@ -36,7 +38,6 @@ FRAME_INSTANCE_RESERVE :: 256
FRAME_UI_TEXTURE_CAPACITY :: 32
FRAME_EFFECT_VIEW_CAPACITY :: 16
TEXTURE_VIEW_CAPACITY :: 8
PIPELINE_CACHE_FILE :: "pipelines.bin"

CAMERA_DEFAULT :: render.Camera{position={0,2,6},forward={0,-1,-6},up={0,1,0},fov_y=60,near=0.1}

Expand Down Expand Up @@ -82,7 +83,7 @@ app_gpu_create :: proc(app:^App,desc:App_Desc) -> App_Error {
// Compiled once, the pipelines of a project are read back from its user
// folder: building them all again takes the GPU driver many seconds.
pipelines:string
if folder:=app_user_folder(app.root,context.temp_allocator);folder!="" {pipelines,_=filepath.join({folder,PIPELINE_CACHE_FILE},context.temp_allocator)}
if folder:=app_user_folder(app.root,context.temp_allocator);folder!="" {pipelines,_=filepath.join({folder,project.PIPELINES_FILE},context.temp_allocator)}
surface:rhi.Surface_Desc
if desc.window {
surface={
Expand Down Expand Up @@ -114,15 +115,19 @@ app_gpu_create :: proc(app:^App,desc:App_Desc) -> App_Error {
free(renderer,app.allocator)
return .Out_Of_Memory
}
if !render.renderer_init(renderer,{
wait:=Pipelines_Wait{app=app,started=platform.clock_ns()}
initialized:=render.renderer_init(renderer,{
device=device,width=u32(app.drawable.x),height=u32(app.drawable.y),
settings=desc.render_settings,allocator=app.allocator,shaders=shaders,
// The pages of streamed meshes decode in the jobs of the app, from this thread, its owner.
jobs=&app.jobs if jobs.worker_count(&app.jobs)>0 else nil,
// A window or a surface shows each frame to someone, who would see one
// wait for the driver; without them, what a frame draws does not depend on that time.
pipeline_thread=desc.window || desc.shown,
}) {
on_pipelines=pipelines_wait_report if desc.window else nil,on_pipelines_user=&wait,
})
pipelines_wait_end(&wait)
if !initialized {
log.errorf("renderer: %s",renderer.error)
return .Renderer_Failed
}
Expand Down
52 changes: 52 additions & 0 deletions host/load_profile.odin
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "core:strings"
import "core:time"
import "mirai:core"
import render "mirai:render"
import "mirai:rhi"
import "mirai:std/base"

// What the last loads of the application took, for whoever asks where their
Expand Down Expand Up @@ -122,6 +123,14 @@ Load_Wait :: struct {
frames:int,
}

// The pipelines the device created and what the frames lost to them: counts
// since the device was made, and of a load the difference over it.
Load_Pipelines :: struct {
made,kept:int, // kept: read from the pipelines kept across runs, not compiled
ns:i64, // what the driver took for them, summed over the threads
late:u64, // frames that began while a pipeline a frame asked for was not in place
}

Load_Function :: struct {
name:[LOAD_FUNCTION_BYTES]u8,
length:int,
Expand All @@ -147,6 +156,8 @@ Load_Record :: struct {
entities:int, // of the world once ready
busy:Load_Busy_Set, // as the last frame left it
waits:[Load_Busy]Load_Wait,
pipelines_from:Load_Pipelines, // as the load opened; zero for the first, which counts the start of the renderer
pipelines:Load_Pipelines, // during the load, once it ended
functions:[LOAD_FUNCTIONS]Load_Function, // once it ended: the costliest first
function_count:int,
}
Expand Down Expand Up @@ -180,6 +191,8 @@ load_open :: proc(app:^App,kind:Load_Kind,name:string) -> u64 {
if kind==.Scene && p.startup_ns>0 {
r.phases[.Startup]={p.startup_ns,1}
p.startup_ns=0
} else {
r.pipelines_from=load_pipelines_now(app)
}
p.current,p.loading=kind,true
p.children,p.loaded=0,false
Expand Down Expand Up @@ -426,6 +439,7 @@ load_close :: proc(app:^App,r:^Load_Record,state:Load_State) {
p:=&app.load_profile
r.state=state
r.ended_ns=time.duration_nanoseconds(time.tick_since(r.started))
r.pipelines=load_pipelines_since(app,r.pipelines_from)
if p.loading && &p.records[p.current]==r {
p.loading=false
load_steps_add(app)
Expand Down Expand Up @@ -512,6 +526,39 @@ Load_Assets_Summary :: struct {kind:Load_Asset,using totals:Load_Assets,ms:f64}
Load_Function_Summary :: struct {name:string,ms:f64}
Load_Wait_Summary :: struct {busy:Load_Busy,until_ms:f64,frames:int}

@(private)
load_pipelines_now :: proc(app:^App) -> Load_Pipelines {
if app.device==nil {return {}}
s:=rhi.device_stats(app.device)
late,_:=render.renderer_pipelines_report(app.renderer)
return {s.pipelines_made,s.pipelines_kept,s.pipeline_ns,late}
}

@(private)
load_pipelines_since :: proc(app:^App,from:Load_Pipelines) -> Load_Pipelines {
now:=load_pipelines_now(app)
return {now.made-from.made,now.kept-from.kept,now.ns-from.ns,now.late-from.late}
}

// What a load says of its pipelines; "" when it compiled none and no frame was
// late. One whose start read the kept pipelines and still compiled some had
// their shaders, fixed state or constants change, or never kept them: the next
// start of the same build compiles none. In the temporary allocator.
load_pipelines_note :: proc(s:Load_Summary) -> string {
compiled:=s.pipelines.made-s.pipelines.kept
late:=fmt.tprintf("%d frames drew materials without their pipelines while they compiled",s.pipelines.late) if s.pipelines.late>0 else ""
if compiled<=0 {return late}
why:string
switch s.store {
case .Read: why="although the kept pipelines were read: their shaders, fixed state or constants changed since, or they were never kept, and the next start compiles none"
case .Other_Device: why="because the kept pipelines were of another GPU or driver (the driver was updated): every one compiles once again"
case .Missing: why="because no pipeline was kept yet: the first start compiles every one"
case .Unreadable: why="because the file of the kept pipelines could not be read: it is written again"
case .None: why="as this device keeps none across runs"
}
return fmt.tprintf("%d pipelines compiled in %.0f ms of the driver, %s%s%s",compiled,load_ms(s.pipelines.ns),why,"; " if late!="" else "",late)
}

// A record as whoever answers about it reads it: phases, kinds of asset and
// waits that took time, the costliest first, and the loop functions that
// ran meanwhile (live while it loads).
Expand All @@ -529,6 +576,8 @@ Load_Summary :: struct {
slowest:[]Load_Asset_Time,
functions:[]Load_Function_Summary,
waits:[]Load_Wait_Summary,
pipelines:Load_Pipelines, // during the load (live while it loads)
store:rhi.Pipeline_Store_State, // what the device found of the kept pipelines when it was made
}

load_ms :: proc "contextless" (ns:i64) -> f64 {return f64(ns)/1e6}
Expand Down Expand Up @@ -579,6 +628,8 @@ load_summary :: proc(app:^App,kind:Load_Kind,top:int,allocator:=context.allocato
}
slice.sort_by(waits[:],proc(a,b:Load_Wait_Summary) -> bool {return a.until_ms>b.until_ms})
out.waits=waits[:]
out.pipelines=r.pipelines if r.state!=.Loading else load_pipelines_since(app,r.pipelines_from)
if app.device!=nil {out.store=rhi.device_stats(app.device).pipeline_store}
return out,true
}

Expand Down Expand Up @@ -627,6 +678,7 @@ load_profile_text :: proc(app:^App,top:int,allocator:=context.allocator) -> stri
for w in s.waits {fmt.sbprintf(&b," %v until %.0f ms",w.busy,w.until_ms)}
strings.write_byte(&b,'\n')
}
if note:=load_pipelines_note(s);note!="" {fmt.sbprintf(&b," pipelines: %s\n",note)}
}
return strings.to_string(b)
}
Expand Down
Loading
Loading