From 517e6027c48f6f9066d34c5a363d1488348051b9 Mon Sep 17 00:00:00 2001 From: 5hif7y <44300833+5hif7y@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:13:55 -0300 Subject: [PATCH 1/3] Show and shorten the cold compile of the pipelines a renderer starts with On a cold driver cache the renderer compiled its pipelines synchronously for minutes (4.5 to 7 on an RX 580 without hardware ray tracing) behind a blank window that did not answer, and quitting waited for driver calls that cannot be cut. With 4 cores a single pipeline thread made frames wait behind speculative variants, so materials drew as their rows in almost every frame even with a warm store. - The start compiles on a thread while the window shows how many are made, with a screen drawn without pipelines (a clear and a copy of text painted on the CPU, ui.text_paint) and the title saying the same. - A window closed meanwhile, or a quit while a pipeline is inside the driver (app_quit_compiling), writes the kept pipelines and ends the process at once: the one exception to the engine never ending it. - The first pipeline thread takes no speculative job; speculative jobs go cheapest first and no frame waits for one. - Graphics_Settings.shaders: While_Playing, or Before_Playing, where a frame waits for the pipelines that are not speculative. - Device_Stats counts the pipelines made, those read from the store and the driver time; pipeline_store_load says what it found (Read, Missing, Other_Device after a driver update, Unreadable). - profile.load and the player log give each load its pipelines, with a note on why they compiled. - project.user_folder takes the user folder of a project out of platform, so the tools find the kept pipelines without SDL. Refs #109 Co-Authored-By: Claude Opus 5.5 --- .../mirai-agent-api/references/methods.md | 10 ++ .agents/skills/mirai-rendering/SKILL.md | 11 +- editor/api_profile_load.odin | 11 ++ editor/editor.odin | 1 + engine/render/graphics.odin | 5 + host/app.odin | 16 +- host/frame.odin | 13 +- host/load_profile.odin | 52 +++++++ host/load_profile_test.odin | 27 ++++ host/pipelines_wait.odin | 145 ++++++++++++++++++ host/player.odin | 11 +- platform/paths.odin | 16 -- platform/watch_test.odin | 14 -- platform/window.odin | 8 + platform/window_sdl_test.odin | 5 + project/user.odin | 41 +++++ project/user_test.odin | 26 ++++ render/frame.odin | 1 + render/pipeline_test.odin | 28 ++++ render/pipeline_thread.odin | 100 +++++++++--- render/renderer.odin | 64 +++++--- rhi/metal_device.odin | 3 + rhi/metal_resources.odin | 4 + rhi/pipeline_store.odin | 24 +-- rhi/pipeline_store_test.odin | 13 ++ rhi/rhi.odin | 24 +++ rhi/vulkan_device.odin | 11 +- rhi/vulkan_resources.odin | 8 +- ui/font.odin | 33 ++++ ui/font_test.odin | 22 +++ 30 files changed, 645 insertions(+), 102 deletions(-) create mode 100644 host/load_profile_test.odin create mode 100644 host/pipelines_wait.odin create mode 100644 project/user.odin create mode 100644 project/user_test.odin diff --git a/.agents/skills/mirai-agent-api/references/methods.md b/.agents/skills/mirai-agent-api/references/methods.md index c2546a123..d3beb735f 100644 --- a/.agents/skills/mirai-agent-api/references/methods.md +++ b/.agents/skills/mirai-agent-api/references/methods.md @@ -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: @@ -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 diff --git a/.agents/skills/mirai-rendering/SKILL.md b/.agents/skills/mirai-rendering/SKILL.md index 82e3ea336..50a256276 100644 --- a/.agents/skills/mirai-rendering/SKILL.md +++ b/.agents/skills/mirai-rendering/SKILL.md @@ -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, diff --git a/editor/api_profile_load.odin b/editor/api_profile_load.odin index 917a93e29..e1e9b2cfe 100644 --- a/editor/api_profile_load.odin +++ b/editor/api_profile_load.odin @@ -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), @@ -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"`, @@ -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 { @@ -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 } @@ -173,5 +183,6 @@ profile_load_notes :: proc(l:host.Load_Summary,manual:bool) -> []string { } if len(l.functions)>0 {append(¬es,"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(¬es,"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(¬es,note)} return notes[:] } diff --git a/editor/editor.odin b/editor/editor.odin index 109d2489f..4634a1842 100644 --- a/editor/editor.odin +++ b/editor/editor.odin @@ -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 } diff --git a/engine/render/graphics.odin b/engine/render/graphics.odin index de9143023..5de80d3f1 100644 --- a/engine/render/graphics.odin +++ b/engine/render/graphics.odin @@ -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 { @@ -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 @@ -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 { diff --git a/host/app.odin b/host/app.odin index aad1d6114..d0cdf0952 100644 --- a/host/app.odin +++ b/host/app.odin @@ -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: /Mirai/, 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)} diff --git a/host/frame.odin b/host/frame.odin index 19b5f2472..a54121215 100644 --- a/host/frame.odin +++ b/host/frame.odin @@ -2,6 +2,7 @@ package host import "base:runtime" import "core:c" +import "core:fmt" import "core:log" import "core:math" import "core:os" @@ -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" @@ -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} @@ -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={ @@ -114,7 +115,8 @@ 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. @@ -122,7 +124,10 @@ app_gpu_create :: proc(app:^App,desc:App_Desc) -> App_Error { // 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 } diff --git a/host/load_profile.odin b/host/load_profile.odin index d501cbde0..6a0550e0b 100644 --- a/host/load_profile.odin +++ b/host/load_profile.odin @@ -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 @@ -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, @@ -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, } @@ -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 @@ -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) @@ -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). @@ -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} @@ -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 } @@ -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) } diff --git a/host/load_profile_test.odin b/host/load_profile_test.odin new file mode 100644 index 000000000..58e733cdc --- /dev/null +++ b/host/load_profile_test.odin @@ -0,0 +1,27 @@ +package host + +import "core:strings" +import "core:testing" +import "mirai:rhi" + +// A load says nothing of pipelines it did not compile, and of those it did, +// why: a first start, an update of the driver, or a start that read the kept +// pipelines and still compiled (keys that changed). +@(test) +load_pipelines_note_says_why_pipelines_compiled :: proc(t:^testing.T) { + Case :: struct {name:string,pipelines:Load_Pipelines,store:rhi.Pipeline_Store_State,words:[]string} + cases:=[]Case{ + {"all kept",{made=100,kept=100},.Read,nil}, + {"nothing made",{},.Missing,nil}, + {"keys changed",{made=100,kept=97,ns=4_000_000},.Read,{"3 pipelines compiled","although the kept pipelines were read"}}, + {"first start",{made=100},.Missing,{"100 pipelines","first start"}}, + {"driver updated",{made=100},.Other_Device,{"driver was updated"}}, + {"frames late only",{made=5,kept=5,late=12},.Read,{"12 frames drew materials without their pipelines"}}, + {"both",{made=10,kept=4,late=3},.Read,{"6 pipelines compiled","3 frames"}}, + } + for c in cases { + note:=load_pipelines_note(Load_Summary{pipelines=c.pipelines,store=c.store}) + if len(c.words)==0 {testing.expectf(t,note=="","%s: says nothing, not %q",c.name,note)} + for word in c.words {testing.expectf(t,strings.contains(note,word),"%s: %q says %q",c.name,note,word)} + } +} diff --git a/host/pipelines_wait.odin b/host/pipelines_wait.odin new file mode 100644 index 000000000..399cdcc91 --- /dev/null +++ b/host/pipelines_wait.odin @@ -0,0 +1,145 @@ +package host + +import "core:fmt" +import "core:math" +import "core:os" +import "mirai:platform" +import render "mirai:render" +import "mirai:rhi" +import "mirai:ui" + +// What the window shows while the renderer makes the pipelines it starts with, +// which takes minutes when the driver has none of them compiled: the first +// start after an install, or after an update of the game or of the driver. +// No pipeline exists yet, so the screen is a clear and a copy of text painted +// on the CPU into the image of the window; the title says the same. +PIPELINES_WAIT_TITLE :: "Compiling shaders %d/%d, %d:%02d (only needed once per install or update)" +PIPELINES_WAIT_HEADLINE :: "Compiling shaders %d/%d" +PIPELINES_WAIT_DETAIL :: "Only needed once per install or update · %d:%02d" +PIPELINES_WAIT_SHOWN_NS :: u64(1_000_000_000) // a start that compiles for less says nothing +PIPELINES_WAIT_SIZES :: [2]f32{22,15} // of the headline and the detail, in points +PIPELINES_WAIT_BAND :: f32(96) // points of the band the text takes, centered +// sRGB colors of the screen: as bytes they go to the image, which a copy does not convert. +PIPELINES_WAIT_BACKGROUND :: [3]u8{16,17,21} +PIPELINES_WAIT_HEADLINE_COLOR :: [3]u8{236,237,240} +PIPELINES_WAIT_DETAIL_COLOR :: [3]u8{150,154,163} + +@(private) +Pipelines_Wait :: struct { + app:^App, + started:u64, + title:[platform.TITLE_CAPACITY]u8, // of the window before, put back once they are made + title_length:int, + shown:bool, + font:ui.Font, // the faces of PIPELINES_WAIT_SIZES at the density of the window + font_ready:bool, +} + +@(private) +pipelines_wait_report :: proc(user:rawptr,made,total:int) { + w:=(^Pipelines_Wait)(user) + p:=w.app.platform + // The costliest are made first, and one takes minutes: the time shows that it goes on. + seconds:=int((platform.clock_ns()-w.started)/1_000_000_000) + if !w.shown && platform.clock_ns()-w.started>=PIPELINES_WAIT_SHOWN_NS { + w.title_length=copy(w.title[:],platform.window_title(p)) + w.shown=true + sizes:=PIPELINES_WAIT_SIZES + w.font_ready=ui.font_init(&w.font,{ui.FONT_INTER},{i32(sizes[0]),i32(sizes[1])},w.app.pixel_density,w.app.allocator) + fmt.eprintfln("mirai: "+PIPELINES_WAIT_TITLE,made,total,seconds/60,seconds%60) + } + if platform.window_wait(p) { + // A driver call cannot be cut, and one takes minutes: a window closed + // now ends the process at once, the user's decision over the rule that + // nothing ends it. Nothing is open yet, and the compiled pipelines are + // written first. + rhi.device_pipelines_keep(w.app.device) + fmt.eprintfln("mirai: closed while compiling shaders: the %d compiled are kept for the next start",made) + os.exit(0) + } + if !w.shown {return} + platform.window_set_title(p,fmt.tprintf(PIPELINES_WAIT_TITLE,made,total,seconds/60,seconds%60)) + pipelines_wait_draw(w,fmt.tprintf(PIPELINES_WAIT_HEADLINE,made,total),fmt.tprintf(PIPELINES_WAIT_DETAIL,seconds/60,seconds%60)) +} + +// Called last before app_destroy, once what the application keeps is written +// (its worlds, documents and sessions closed): when the renderer is inside the +// driver making a pipeline, which a quit would wait for for minutes, the kept +// pipelines are written and the process ends at once with code, as for a +// window closed while the renderer starts. Else it returns, and app_destroy +// ends the application as always. +app_quit_compiling :: proc(app:^App,code:int) { + if app==nil || !render.renderer_pipelines_making(app.renderer) {return} + rhi.device_pipelines_keep(app.device) + fmt.eprintln("mirai: quit while a shader compiles: it is left to the next start") + os.exit(code) +} + +// The window back as it was: its title, and the font of the screen gone. +@(private) +pipelines_wait_end :: proc(w:^Pipelines_Wait) { + if w.shown {platform.window_set_title(w.app.platform,string(w.title[:w.title_length]))} + if w.font_ready {ui.font_destroy(&w.font)} + w.shown,w.font_ready=false,false +} + +// One frame of the screen: the image of the window cleared to the background +// and the band of the text copied into its middle. A frame the device cannot +// begin (a minimized window) is skipped; a format other than 8-bit RGBA or +// BGRA shows the background alone. +@(private="file") +pipelines_wait_draw :: proc(w:^Pipelines_Wait,headline,detail:string) { + d:=w.app.device + cmd,began:=rhi.frame_begin(d) + if !began {return} + defer rhi.frame_end(d,cmd) + swap:=rhi.swapchain_texture(d) + if swap==0 {return} + format:=rhi.swapchain_format(d) + width,height:=rhi.swapchain_size(d) + srgb:=format==.RGBA8_Srgb || format==.BGRA8_Srgb + background:=PIPELINES_WAIT_BACKGROUND + clear:[4]f32 + for c,i in background {clear[i]=pipelines_wait_linear(c) if srgb else f32(c)/255} + clear[3]=1 + pass:=rhi.Render_Pass_Desc{color_count=1,name="compiling shaders"} + pass.colors[0]={texture=swap,load=.Clear,store=.Store,clear=clear} + rhi.cmd_begin_render_pass(cmd,&pass) + rhi.cmd_end_render_pass(cmd) + bgra:=format==.BGRA8_Unorm || format==.BGRA8_Srgb + if !w.font_ready || !bgra && format!=.RGBA8_Unorm && format!=.RGBA8_Srgb {return} + density:=w.app.pixel_density + band:=min(int(PIPELINES_WAIT_BAND*density),int(height)) + columns:=int(width) + if band<=0 || columns<=0 {return} + coverage:=make([]u8,columns*band*2,context.temp_allocator) + if coverage==nil {return} + sizes:=PIPELINES_WAIT_SIZES + lines:=[2]string{headline,detail} + baselines:=[2]f32{f32(band)*0.42,f32(band)*0.78} + for line,i in lines { + x:=(f32(columns)-ui.text_width(&w.font,line,sizes[i])*density)*0.5 + ui.text_paint(&w.font,line,sizes[i],coverage[i*columns*band:][:columns*band],columns,band,math.floor(x),math.floor(baselines[i])) + } + upload:=rhi.frame_alloc(d,u64(columns*band*4)) + if upload.ptr==nil {return} + pixels:=([^][4]u8)(upload.ptr)[:columns*band] + colors:=[2][3]u8{PIPELINES_WAIT_HEADLINE_COLOR,PIPELINES_WAIT_DETAIL_COLOR} + for &pixel,at in pixels { + rgb:=background + for color,i in colors { + a:=u32(coverage[i*columns*band+at]) + if a==0 {continue} + for c in 0..<3 {rgb[c]=u8((u32(rgb[c])*(255-a)+u32(color[c])*a+127)/255)} + } + pixel=bgra?[4]u8{rgb[2],rgb[1],rgb[0],255}:[4]u8{rgb[0],rgb[1],rgb[2],255} + } + top:=(int(height)-band)/2 + rhi.cmd_copy_buffer_to_texture(cmd,upload.buffer,upload.offset,swap,0,{0,u32(top),u32(columns),u32(band)}) +} + +@(private="file") +pipelines_wait_linear :: proc "contextless" (c:u8) -> f32 { + v:=f32(c)/255 + return v/12.92 if v<=0.04045 else math.pow((v+0.055)/1.055,2.4) +} diff --git a/host/player.odin b/host/player.odin index b25f67523..421c34a80 100644 --- a/host/player.odin +++ b/host/player.odin @@ -14,6 +14,7 @@ import "mirai:asset" import "mirai:core" import "mirai:platform" import "mirai:project" +import render "mirai:render" import "mirai:rhi" import "mirai:std/base" import "mirai:std/jobs" @@ -280,7 +281,14 @@ player_run_summary :: proc(app:^App,frames:int,seconds:f64) -> string { file:=project.settings_file(.Player,app.root,app.engine_root,context.temp_allocator,player=app.options) applied:=file!="" && os.exists(file)?fmt.tprintf(", player options %s",file):", no player options" mean:=frames>0?1000*seconds/f64(frames):0 - return fmt.tprintf("mirai: %d frames, mean frame time %.2f ms, drawn at %vx%v%s",frames,mean,int(size.x),int(size.y),applied) + pipelines:="" + if app.renderer!=nil { + s:=rhi.device_stats(app.renderer.device) + late,waited:=render.renderer_pipelines_report(app.renderer) + pipelines=fmt.tprintf("\n pipelines: %d made (%d kept), %.0f ms in the driver, longest %.1f ms, store %v; %d frames late, %.0f ms waited", + s.pipelines_made,s.pipelines_kept,f64(s.pipeline_ns)/1e6,f64(s.pipeline_longest_ns)/1e6,s.pipeline_store,late,time.duration_milliseconds(waited)) + } + return fmt.tprintf("mirai: %d frames, mean frame time %.2f ms, drawn at %vx%v%s%s",frames,mean,int(size.x),int(size.y),applied,pipelines) } // The icon of the window: a PNG, read as 8 bits per channel with alpha. @@ -484,5 +492,6 @@ player_run :: proc(info:^core.World_Info,project_root,engine_root:string,args:[] if code==PLAYER_OK {code=PLAYER_VALIDATION} } } + app_quit_compiling(app,code) return code } diff --git a/platform/paths.odin b/platform/paths.odin index 636847ca5..74673b0b4 100644 --- a/platform/paths.odin +++ b/platform/paths.odin @@ -8,22 +8,6 @@ path_executable_folder :: proc(allocator:=context.allocator) -> (path:string,ok: directory,error:=os.get_executable_directory(allocator) return directory,error==nil } -// Folder for what the application keeps for its user (settings, saves, -// caches), created when missing: //. -// organization may be empty. -path_user_data_folder :: proc(organization,application:string,allocator:=context.allocator) -> (path:string,ok:bool) { - if application=="" {return "",false} - base,error:=os.user_data_dir(context.temp_allocator) - if error!=nil {return "",false} - parts:=[]string{base,organization,application} if organization!="" else []string{base,application} - joined,join_error:=filepath.join(parts,allocator) - if join_error!=nil {return "",false} - if os.make_directory_all(joined)!=nil && !os.is_dir(joined) { - delete(joined,allocator) - return "",false - } - return joined,true -} // Folder of the system for files that need not outlive the process. path_temporary_folder :: proc(allocator:=context.allocator) -> (path:string,ok:bool) { directory,error:=os.temp_directory(allocator) diff --git a/platform/watch_test.odin b/platform/watch_test.odin index 08d2876da..0e7f5ea5a 100644 --- a/platform/watch_test.odin +++ b/platform/watch_test.odin @@ -318,20 +318,6 @@ paths_of_the_system_exist :: proc(t:^testing.T) { testing.expect(t,ok && os.is_dir(executable),executable) temporary,temporary_ok:=path_temporary_folder(context.temp_allocator) testing.expect(t,temporary_ok && os.is_dir(temporary),temporary) - name:=fmt.tprintf("platform-test-%d",os.get_pid()) - data,data_ok:=path_user_data_folder("Mirai Tests",name,context.temp_allocator) - testing.expect(t,data_ok && os.is_dir(data),data) - testing.expect(t,strings.has_suffix(data,name) && strings.contains(data,"Mirai Tests")) - // Asking again finds the same folder. - again,again_ok:=path_user_data_folder("Mirai Tests",name,context.temp_allocator) - testing.expect(t,again_ok && again==data) - testing.expect(t,os.remove(data)==nil) - testing.expect(t,os.remove(filepath.dir(data))==nil || os.is_dir(filepath.dir(data))) - plain,plain_ok:=path_user_data_folder("",name,context.temp_allocator) - testing.expect(t,plain_ok && os.is_dir(plain) && !strings.contains(plain,"Mirai Tests")) - testing.expect(t,os.remove(plain)==nil) - _,empty_ok:=path_user_data_folder("Mirai Tests","",context.temp_allocator) - testing.expect(t,!empty_ok) } PLATFORM_BENCHMARK :: #config(PLATFORM_BENCHMARK,false) diff --git a/platform/window.odin b/platform/window.odin index 3e55364a6..44274fadc 100644 --- a/platform/window.odin +++ b/platform/window.odin @@ -429,6 +429,14 @@ window_set_size :: proc(p:^Platform,size:[2]i32) { } window_title :: proc(p:^Platform) -> string {return string(p.title[:p.title_length])} +// While the application draws no frame (it waits for something long): keeps +// the window answering the window system, gathering its events, and says +// whether it was asked to close since the last frame. False without a window. +window_wait :: proc(p:^Platform) -> (close_requested:bool) { + if p.headless || p.window.handle==nil {return false} + window_pump(p,clock_ns()) + return p.input.frame.window.close_requested +} // Cut to TITLE_CAPACITY-1 bytes. The window system is told only when it changes. window_set_title :: proc(p:^Platform,title:string) { if title==window_title(p) {return} diff --git a/platform/window_sdl_test.odin b/platform/window_sdl_test.odin index cae8de943..3a1d18025 100644 --- a/platform/window_sdl_test.odin +++ b/platform/window_sdl_test.odin @@ -117,6 +117,11 @@ sdl_window_reports_its_state_and_translates_events :: proc(t:^testing.T) { tick:=input_tick_take(p) testing.expect(t,tick.keys_pressed==Keys{.W,.Enter,.Q} && tick.keys_released==Keys{.W} && tick.mouse_pressed==Mouse_Buttons{.Right}) testing.expect_value(t,tick.mouse_delta,[2]f32{10,5}) + // While no frame is drawn, window_wait gathers the events too and tells of a close. + testing.expect(t,!window_wait(p)) + event={};event.window={type=.WINDOW_CLOSE_REQUESTED} + push(t,&event) + testing.expect(t,window_wait(p)) // QUIT asks the application to close the same way the window does. event={};event.quit={type=.QUIT} push(t,&event) diff --git a/project/user.odin b/project/user.odin new file mode 100644 index 000000000..315c9dd3b --- /dev/null +++ b/project/user.odin @@ -0,0 +1,41 @@ +package project + +import "core:os" +import "core:path/filepath" +import "core:strings" +import "mirai:std/base" + +// The organization under which projects keep what they keep for their user. +USER_ORGANIZATION :: "Mirai" +// In the user folder of a project: the GPU pipelines its runs compiled, read back by the next. +PIPELINES_FILE :: "pipelines.bin" + +// Folder for what an application keeps for its user (settings, saves, +// caches), made when missing: //. +// organization may be empty. +user_data_folder :: proc(organization,application:string,allocator:=context.allocator) -> (path:string,ok:bool) { + if application=="" {return "",false} + data,error:=os.user_data_dir(context.temp_allocator) + if error!=nil {return "",false} + parts:=[]string{data,organization,application} if organization!="" else []string{data,application} + joined,join_error:=filepath.join(parts,allocator) + if join_error!=nil {return "",false} + if os.make_directory_all(joined)!=nil && !os.is_dir(joined) { + delete(joined,allocator) + return "",false + } + return joined,true +} + +// What the project at root keeps for its user, the saves and the compiled GPU +// pipelines: /Mirai/, made +// when missing. "" without a project or a folder of user data. +user_folder :: proc(root:string,allocator:=context.allocator) -> string { + if root=="" {return ""} + name:=filepath.base(root) + if manifest,error:=manifest_read(path(root,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,_:=user_data_folder(USER_ORGANIZATION,name,allocator) + return folder +} diff --git a/project/user_test.odin b/project/user_test.odin new file mode 100644 index 000000000..b921c06d3 --- /dev/null +++ b/project/user_test.odin @@ -0,0 +1,26 @@ +package project + +import "core:fmt" +import "core:os" +import "core:path/filepath" +import "core:strings" +import "core:testing" + +@(test) +user_data_folder_is_made_once_under_its_organization :: proc(t:^testing.T) { + name:=fmt.tprintf("project-test-%d",os.get_pid()) + data,data_ok:=user_data_folder("Mirai Tests",name,context.temp_allocator) + testing.expect(t,data_ok && os.is_dir(data),data) + testing.expect(t,strings.has_suffix(data,name) && strings.contains(data,"Mirai Tests")) + // Asking again finds the same folder. + again,again_ok:=user_data_folder("Mirai Tests",name,context.temp_allocator) + testing.expect(t,again_ok && again==data) + testing.expect(t,os.remove(data)==nil) + testing.expect(t,os.remove(filepath.dir(data))==nil || os.is_dir(filepath.dir(data))) + plain,plain_ok:=user_data_folder("",name,context.temp_allocator) + testing.expect(t,plain_ok && os.is_dir(plain) && !strings.contains(plain,"Mirai Tests")) + testing.expect(t,os.remove(plain)==nil) + _,empty_ok:=user_data_folder("Mirai Tests","",context.temp_allocator) + testing.expect(t,!empty_ok) + testing.expect_value(t,user_folder(""),"") +} diff --git a/render/frame.odin b/render/frame.odin index 0a00cb4f2..cf04b316f 100644 --- a/render/frame.odin +++ b/render/frame.odin @@ -399,6 +399,7 @@ renderer_frame :: proc(r:^Renderer,frame:Frame_Desc) -> bool { pipelines_install(r) if r.variants_wanted {pipeline_variants_ask(r)} if r.scene.programs_arrived {programs_ask(r)} + pipelines_before_frame(r) // What the extensions asked of this frame lasts for it alone. defer { r.pass_count=0;r.main_pipeline=0;r.gbuffer_asked={} diff --git a/render/pipeline_test.odin b/render/pipeline_test.odin index 95cef74c5..a223b567f 100644 --- a/render/pipeline_test.odin +++ b/render/pipeline_test.odin @@ -201,3 +201,31 @@ pipelines_a_frame_does_not_ask_for_make_room :: proc(t: ^testing.T) { _, error := renderer_pipeline(r, {passes = standard_pipeline_passes(.Light)[:4]}) testing.expect(t, strings.contains(error, "places are in use"), error) } + +// What a frame needs goes first, then what is asked ahead of time, then the +// speculative jobs from the cheapest; the first thread leaves speculative jobs +// to the others unless a frame needs one. +@(test) +pipeline_jobs_go_by_need_then_cheapest_speculative :: proc(t: ^testing.T) { + Case :: struct {name: string, jobs: []Pipeline_Job, first: bool, next: int} + ahead := Pipeline_Job{state = .Asked, cost = 900} + needed := Pipeline_Job{state = .Asked, urgent = true, cost = 900} + costly := Pipeline_Job{state = .Asked, speculative = true, cost = 500} + cheap := Pipeline_Job{state = .Asked, speculative = true, cost = 100} + needed_costly := costly + needed_costly.urgent = true + making := cheap + making.state = .Making + cases := []Case{ + {"nothing asked", {{}, making}, false, -1}, + {"a frame needs it", {ahead, costly, needed}, false, 2}, + {"asked ahead of time before speculative", {costly, cheap, ahead}, false, 2}, + {"the cheapest speculative", {costly, cheap, costly}, false, 1}, + {"the first thread takes no speculative", {costly, cheap}, true, -1}, + {"unless a frame needs it", {cheap, needed_costly}, true, 1}, + {"the first thread still takes what is asked ahead", {cheap, ahead}, true, 1}, + } + for c in cases { + testing.expectf(t, pipeline_job_next(c.jobs, c.first) == c.next, "%s: %d, not %d", c.name, pipeline_job_next(c.jobs, c.first), c.next) + } +} diff --git a/render/pipeline_thread.odin b/render/pipeline_thread.odin index d458a4a37..efa0e6f86 100644 --- a/render/pipeline_thread.odin +++ b/render/pipeline_thread.odin @@ -3,6 +3,7 @@ package mirai_render import "core:os" import "core:sync" import "core:thread" +import "core:time" import "mirai:rhi" // The variants of the stages (pipeline_of, pipeline_variants_ask) and, with @@ -24,6 +25,8 @@ Pipeline_Job :: struct { made: rhi.Pipeline, state: Pipeline_Job_State, urgent: bool, // a frame needs it: made before those asked ahead of time + speculative: bool, // for settings the renderer does not use now: made last, the cheapest first, and never on the first thread; no frame waits for one + cost: int, // bytes of its shaders: what orders the speculative ones } @(private) Pipeline_Job_State :: enum u8 {Free, Asked, Making, Made} @@ -38,7 +41,9 @@ Pipeline_Thread :: struct { busy: int, // jobs that are not free (atomic): a frame without one takes no lock installed: u64, // times pipelines went in place quit: bool, - threads: [PIPELINE_THREADS_MAX]^thread.Thread, // one a core but one (half the logical processors), as create_pipelines + late_frames: u64, // frames that began while a pipeline a frame asked for was not in place + waited_ns: i64, // what frames waited for pipelines with Settings.pipelines_first + threads: [PIPELINE_THREADS_MAX]^thread.Thread, // one a core but one (half the logical processors), as create_pipelines, and the first, which takes no speculative job: what a frame needs never waits behind one thread_count: int, } @@ -47,8 +52,8 @@ pipeline_thread_start :: proc(r: ^Renderer) { t, t_error := new(Pipeline_Thread, r.allocator) if t_error != nil {return} t.device = r.device - for _ in 0 ..< clamp(os.get_processor_core_count() / 2 - 1, 1, PIPELINE_THREADS_MAX) { - t.threads[t.thread_count] = thread.create_and_start_with_poly_data(t, pipeline_thread_run) + for i in 0 ..< clamp(os.get_processor_core_count() / 2, 2, PIPELINE_THREADS_MAX) { + t.threads[t.thread_count] = thread.create_and_start_with_poly_data2(t, i == 0, pipeline_thread_run) if t.threads[t.thread_count] == nil {break} t.thread_count += 1 } @@ -57,16 +62,12 @@ pipeline_thread_start :: proc(r: ^Renderer) { } @(private = "file") -pipeline_thread_run :: proc(t: ^Pipeline_Thread) { +pipeline_thread_run :: proc(t: ^Pipeline_Thread, first: bool) { for { sync.mutex_lock(&t.mutex) job: ^Pipeline_Job for job == nil && !t.quit { - for &candidate in t.jobs { - if candidate.state != .Asked {continue} - if candidate.urgent {job = &candidate; break} - if job == nil {job = &candidate} - } + if next := pipeline_job_next(t.jobs[:], first); next >= 0 {job = &t.jobs[next]} if job == nil && !t.quit {sync.cond_wait(&t.asked, &t.mutex)} } if t.quit {sync.mutex_unlock(&t.mutex); return} @@ -82,6 +83,23 @@ pipeline_thread_run :: proc(t: ^Pipeline_Thread) { } } +// The job a thread takes next, -1 for none: one a frame needs, else one asked +// ahead of time that is not speculative, else the speculative one of the +// cheapest shaders, so a short session still ends some. The first thread +// takes no speculative job unless a frame needs it. +@(private) +pipeline_job_next :: proc(jobs: []Pipeline_Job, first: bool) -> int { + next := -1 + for candidate, i in jobs { + if candidate.state != .Asked || first && candidate.speculative && !candidate.urgent {continue} + if candidate.urgent {return i} + if next < 0 {next = i; continue} + held := jobs[next] + if held.speculative && (!candidate.speculative || candidate.cost < held.cost) {next = i} + } + return next +} + // With the renderer: the threads end and what they made and nobody took goes. @(private) pipeline_thread_destroy :: proc(r: ^Renderer) { @@ -104,10 +122,11 @@ pipeline_thread_destroy :: proc(r: ^Renderer) { // made now without the threads, when `now` or, unless `early`, without // pipelines_async; else asked of the threads, and owner^ stays 0 until the // frame that finds it made. owner must stay where it is until then. early: -// asked ahead of time, behind what frames ask for (pipeline_urgent). False -// when no job is free: the caller asks again later. +// asked ahead of time, behind what frames ask for (pipeline_urgent); +// speculative: early, for settings not in use. False when no job is free: the +// caller asks again later. @(private) -pipeline_make :: proc(r: ^Renderer, owner: ^rhi.Pipeline, graphics: rhi.Graphics_Pipeline_Desc = {}, compute: rhi.Compute_Pipeline_Desc = {}, now := false, early := false) -> bool { +pipeline_make :: proc(r: ^Renderer, owner: ^rhi.Pipeline, graphics: rhi.Graphics_Pipeline_Desc = {}, compute: rhi.Compute_Pipeline_Desc = {}, now := false, early := false, speculative := false) -> bool { t := r.pipeline_thread if t == nil || now || !r.pipelines_async && !early { owner^ = rhi.pipeline_create_compute(r.device, compute) if compute.shader != nil else rhi.pipeline_create_graphics(r.device, graphics) @@ -117,9 +136,13 @@ pipeline_make :: proc(r: ^Renderer, owner: ^rhi.Pipeline, graphics: rhi.Graphics defer sync.mutex_unlock(&t.mutex) for &job in t.jobs { if job.state != .Free {continue} - job = {owner = owner, graphics = graphics, compute = compute, state = .Asked, urgent = !early} + cost := len(rhi.shader_code_bytes(compute.shader^)) if compute.shader != nil else 0 + if compute.shader == nil && graphics.vertex != nil && graphics.fragment != nil { + cost = len(rhi.shader_code_bytes(graphics.vertex^)) + len(rhi.shader_code_bytes(graphics.fragment^)) + } + job = {owner = owner, graphics = graphics, compute = compute, state = .Asked, urgent = !early, speculative = early && speculative, cost = cost} sync.atomic_add(&t.busy, 1) - sync.cond_signal(&t.asked) + sync.cond_broadcast(&t.asked) return true } return false @@ -193,6 +216,18 @@ pipeline_urgent :: proc(r: ^Renderer, owner: ^rhi.Pipeline) { sync.mutex_lock(&t.mutex) defer sync.mutex_unlock(&t.mutex) for &job in t.jobs {if job.state != .Free && job.owner == owner {job.urgent = true}} + sync.cond_broadcast(&t.asked) +} + +// Whether a thread of the renderer is inside the driver making a pipeline: +// destroying the renderer waits for it, which takes minutes for some. +renderer_pipelines_making :: proc(r: ^Renderer) -> bool { + t := r.pipeline_thread if r != nil else nil + if t == nil || sync.atomic_load(&t.busy) == 0 {return false} + sync.mutex_lock(&t.mutex) + defer sync.mutex_unlock(&t.mutex) + for job in t.jobs {if job.state == .Making {return true}} + return false } // Whether a pipeline a frame asked for is not in place: the frames draw the @@ -207,23 +242,48 @@ renderer_pipelines_pending :: proc(r: ^Renderer) -> bool { return false } +// Before a frame records: with Settings.pipelines_first it waits for the +// pipelines asked for that are not speculative instead of drawing without them; +// else a frame that begins while one a frame needs is not in place is late. +@(private) +pipelines_before_frame :: proc(r: ^Renderer) { + t := r.pipeline_thread + if t == nil || sync.atomic_load(&t.busy) == 0 {return} + if r.settings.pipelines_first { + started := time.tick_now() + renderer_pipelines_wait(r, wanted = true) + t.waited_ns += time.duration_nanoseconds(time.tick_since(started)) + } else if renderer_pipelines_pending(r) { + t.late_frames += 1 + } +} + +// What the frames of the renderer lost to pipelines being made: the frames +// late (pipelines_before_frame) and what they waited. +renderer_pipelines_report :: proc(r: ^Renderer) -> (late_frames: u64, waited: time.Duration) { + t := r.pipeline_thread if r != nil else nil + if t == nil {return} + return t.late_frames, time.Duration(t.waited_ns) +} + // Counts the times pipelines of programs went in place: what drew a program // as its rows (an image of the editor) draws again when it changes. renderer_pipelines_made :: proc(r: ^Renderer) -> u64 { return r.pipeline_thread.installed if r != nil && r.pipeline_thread != nil else 0 } -// Waits for the pipelines frames asked for (all: those asked ahead of time -// too), or for that of owner, and puts them in place: the next frame draws -// with them. What reads an image back (a capture) -// draws again after it when renderer_pipelines_pending said so. -renderer_pipelines_wait :: proc(r: ^Renderer, owner: ^rhi.Pipeline = nil, all := false) { +// Waits for the pipelines frames asked for (wanted: those that are not +// speculative, asked ahead of time or not, and not a speculative one a frame +// asked for since; all: every one), or for that of owner, and puts them in +// place: the next frame draws with them. What reads an image back +// (a capture) draws again after it when renderer_pipelines_pending said so. +renderer_pipelines_wait :: proc(r: ^Renderer, owner: ^rhi.Pipeline = nil, all := false, wanted := false) { t := r.pipeline_thread if r != nil else nil if t == nil {return} sync.mutex_lock(&t.mutex) for { waiting := false - for job in t.jobs {waiting ||= ((all || job.urgent) if owner == nil else job.owner == owner) && (job.state == .Asked || job.state == .Making)} + for job in t.jobs {waiting ||= ((all || (!job.speculative if wanted else job.urgent)) if owner == nil else job.owner == owner) && (job.state == .Asked || job.state == .Making)} if !waiting {break} sync.cond_wait(&t.made, &t.mutex) } diff --git a/render/renderer.odin b/render/renderer.odin index 4b7bfe0de..928cf9b93 100644 --- a/render/renderer.odin +++ b/render/renderer.odin @@ -29,6 +29,8 @@ import "core:math/linalg" import "core:os" import "core:reflect" import "core:sync" +import "core:thread" +import "core:time" import "mirai:asset" import "mirai:std/jobs" import mm "mirai:std/math" @@ -79,6 +81,7 @@ Settings :: struct { colored_shadows: bool, // thin RGB filters along shadow rays through blended surfaces glass_layers: u32, // nearest blended surfaces of a pixel lit once per pixel with traced reflection and refraction (1 to GLASS_LAYERS_MAX); the ones behind are drawn forward sun_radius: f32, // angular radius of the sun, radians + pipelines_first: bool, // a frame waits for the pipelines asked for, those asked ahead of time too, instead of drawing without them while they are made } // Outlines drawn after tonemapping around instances with an outline level: @@ -428,6 +431,12 @@ Renderer_Desc :: struct { // rows until its pipelines are made (pipeline_thread.odin). False: they // are made in the frame that first asks for them. pipeline_thread: bool, + // While the pipelines it starts with are made (from seconds to minutes when + // the driver has none of them compiled), called on the calling thread about + // ten times a second with how many are. nil: renderer_init makes them on + // the calling thread too. + on_pipelines: proc(user: rawptr, made, total: int), + on_pipelines_user: rawptr, } // What one frame draws besides the scene. @@ -755,7 +764,7 @@ renderer_init :: proc(r: ^Renderer, desc: Renderer_Desc) -> bool { return false } } - if !create_pipelines(r, {.Any, tracer_wanted(r, r.settings)}) || !pipelines_init(r) || !dfg_build(r) || !display_build(r, DISPLAY_SDR) {return false} + if !create_pipelines(r, {.Any, tracer_wanted(r, r.settings)}, desc.on_pipelines, desc.on_pipelines_user) || !pipelines_init(r) || !dfg_build(r) || !display_build(r, DISPLAY_SDR) {return false} hardware, made := tracer_settle(r, r.settings, retry = false) if !made {return false} r.hardware_rt = hardware @@ -1231,14 +1240,15 @@ variants_listed :: proc(r: ^Renderer, list: ^Pipeline_List, selected: bool) { pipeline_variants_ask :: proc(r: ^Renderer) { r.variants_wanted = false tracer := Pipeline_Rays.Hardware if r.hardware_rt else .Software - list: Pipeline_List - variants_listed(r, &list, true) - variants_listed(r, &list, false) - for item in list.items[:list.count] { - v := variant_of(r, item.target) - if item.rays != .Any && item.rays != tracer || v.state != .None {continue} - graphics, compute := pipeline_desc(r, item) - if pipeline_make(r, item.target, graphics, compute, early = true) {v.state = .Asked} else {r.variants_wanted = true} + for selected in ([]bool{true, false}) { + list: Pipeline_List + variants_listed(r, &list, selected) + for item in list.items[:list.count] { + v := variant_of(r, item.target) + if item.rays != .Any && item.rays != tracer || v.state != .None {continue} + graphics, compute := pipeline_desc(r, item) + if pipeline_make(r, item.target, graphics, compute, early = true, speculative = !selected) {v.state = .Asked} else {r.variants_wanted = true} + } } } @@ -1259,10 +1269,12 @@ pipeline_possible :: proc(info: ^rhi.Device_Info, item: Pipeline_Make) -> bool { // can, with the variants the settings select, the costliest first, on a thread a core (half the logical processors): // the driver compiles each one for up to seconds. A transaction that may // allocate. Once r.reload_stop is set, from any thread, none more is made. -// False when one the renderer needs is not there; those of rays traced by -// hardware are not needed (tracer_missing). +// With progress they are made on a thread of their own while the calling one +// reports how many are (Renderer_Desc.on_pipelines). False when one the +// renderer needs is not there; those of rays traced by hardware are not needed +// (tracer_missing). @(private) -create_pipelines :: proc(r: ^Renderer, make: bit_set[Pipeline_Rays]) -> bool { +create_pipelines :: proc(r: ^Renderer, make: bit_set[Pipeline_Rays], progress: proc(user: rawptr, made, total: int) = nil, progress_user: rawptr = nil) -> bool { list: Pipeline_List pipelines_list(r, &list) variants_listed(r, &list, true) @@ -1272,6 +1284,7 @@ create_pipelines :: proc(r: ^Renderer, make: bit_set[Pipeline_Rays]) -> bool { list: ^Pipeline_List, order: [len(Pipeline_List{}.items)]int, count: int, + made: int, // atomic } work := Work{r = r, list = &list} for &item, i in list.items[:list.count] { @@ -1298,15 +1311,30 @@ create_pipelines :: proc(r: ^Renderer, make: bit_set[Pipeline_Rays]) -> bool { item := &work.list.items[work.order[at]] graphics, compute := pipeline_desc(r, item^) pipeline_make(r, item.target, graphics, compute, now = true) + sync.atomic_add(&work.made, 1) } } - // A system of its own: a reload makes them on a thread that is not the owner of r.jobs. - js: jobs.Job_System - if jobs.system_init(&js, {threads = clamp(os.get_processor_core_count() / 2, 1, min(PIPELINE_THREADS_MAX, max(work.count, 1))) - 1}) { - jobs.parallel_for(&js, jobs.OWNER, work.count, 1, run, &work) - jobs.system_destroy(&js) + // A system of its own, owned by the thread that runs it: a reload makes + // them on a thread that is not the owner of r.jobs. + run_all :: proc(work: ^Work) { + js: jobs.Job_System + if jobs.system_init(&js, {threads = clamp(os.get_processor_core_count() / 2, 1, min(PIPELINE_THREADS_MAX, max(work.count, 1))) - 1}) { + jobs.parallel_for(&js, jobs.OWNER, work.count, 1, run, work) + jobs.system_destroy(&js) + } else { + run(work, 0, work.count, jobs.OWNER) + } + } + maker := thread.create_and_start_with_poly_data(&work, run_all) if progress != nil && work.count > 0 else nil + if maker == nil { + run_all(&work) } else { - run(&work, 0, work.count, jobs.OWNER) + for !thread.is_done(maker) { + progress(progress_user, sync.atomic_load(&work.made), work.count) + time.sleep(100 * time.Millisecond) + } + thread.join(maker) + thread.destroy(maker) } for item in list.items[:list.count] { if item.rays == .Hardware || item.rays not_in make || !item.required || !pipeline_possible(info, item) || item.target == nil || item.target^ != 0 {continue} diff --git a/rhi/metal_device.odin b/rhi/metal_device.odin index 11d4ee6ba..3a2a35b01 100644 --- a/rhi/metal_device.odin +++ b/rhi/metal_device.odin @@ -567,6 +567,9 @@ mtl_table_resident :: proc(d: ^Device) { } } +// Metal keeps no pipelines across runs (Device_Desc.pipeline_cache_path): nothing to write. +device_pipelines_keep :: proc(d: ^Device) {} + device_destroy :: proc(d: ^Device) { if d == nil {return} NS.scoped_autoreleasepool() diff --git a/rhi/metal_resources.odin b/rhi/metal_resources.odin index 899417505..6f34c8a37 100644 --- a/rhi/metal_resources.odin +++ b/rhi/metal_resources.odin @@ -461,8 +461,10 @@ pipeline_create_compute :: proc(d: ^Device, desc: Compute_Pipeline_Desc) -> Pipe function,p.shaders[0] = mtl_function(d, desc.shader, desc.constants, desc.name) if function == nil {return 0} defer mtl_release(function) + started := present_now_ns() state, err := MTL.Device_newComputePipelineStateWithFunction(d.device, function) if state == nil {mtl_set_ns_error(d, err, "compute pipeline %s", desc.name); return 0} + pipeline_counted(d, present_now_ns() - started, false) p.compute = state threads := p.threadgroup.width * p.threadgroup.height * p.threadgroup.depth if limit := MTL.ComputePipelineState_maxTotalThreadsPerThreadgroup(state); NS.UInteger(threads) > limit { @@ -541,8 +543,10 @@ pipeline_create_graphics :: proc(d: ^Device, desc: Graphics_Pipeline_Desc) -> Pi MTL.RenderPipelineDescriptor_setLabel(rpd, label) mtl_release(label) } + started := present_now_ns() state, err := MTL.Device_newRenderPipelineStateWithDescriptor(d.device, rpd) if state == nil {mtl_set_ns_error(d, err, "graphics pipeline %s", desc.name); return 0} + pipeline_counted(d, present_now_ns() - started, false) p.render = state // Vulkan never writes depth when the depth test is disabled; match it. diff --git a/rhi/pipeline_store.odin b/rhi/pipeline_store.odin index 198144519..068882056 100644 --- a/rhi/pipeline_store.odin +++ b/rhi/pipeline_store.odin @@ -107,13 +107,15 @@ pipeline_store_clear :: proc(s: ^Pipeline_Store) { // Reads the store of path, after removing what processes that ended left of // their writes. A file that is missing, too large, of another device or // version, or that does not read back exactly leaves the store empty: the next -// save replaces it. -pipeline_store_load :: proc(s: ^Pipeline_Store, path: string) { +// save replaces it. Says which of those it found. +pipeline_store_load :: proc(s: ^Pipeline_Store, path: string) -> Pipeline_Store_State { serial.file_remove_stale(path) s.stamp = pipeline_file_stamp(path, s.allocator) data, error := serial.file_read(path, s.allocator, limit = i64(s.limit)) - if error != .None || data == nil {return} - if !pipeline_store_decode(s, data) {delete(data, s.allocator)} + if error != .None || data == nil {return .Missing if s.stamp == {} else .Unreadable} + state := pipeline_store_decode(s, data) + if state != .Read {delete(data, s.allocator)} + return state } @(private = "file") @@ -125,19 +127,19 @@ pipeline_file_stamp :: proc(path: string, allocator: runtime.Allocator) -> [2]i6 } // Takes data as the file of the store when it is valid for its device. -pipeline_store_decode :: proc(s: ^Pipeline_Store, data: []u8) -> bool { - if len(data) < STORE_HEADER || len(data) > s.limit {return false} - if store_read_u32(data, 0) != PIPELINE_STORE_MAGIC || store_read_u32(data, 4) != PIPELINE_STORE_VERSION {return false} +pipeline_store_decode :: proc(s: ^Pipeline_Store, data: []u8) -> Pipeline_Store_State { + if len(data) < STORE_HEADER || len(data) > s.limit {return .Unreadable} + if store_read_u32(data, 0) != PIPELINE_STORE_MAGIC || store_read_u32(data, 4) != PIPELINE_STORE_VERSION {return .Unreadable} device := Pipeline_Device{vendor = store_read_u32(data, 8), device = store_read_u32(data, 12), driver = store_read_u32(data, 16)} copy(device.uuid[:], data[32:48]) - if device != s.device {return false} + if device != s.device {return .Other_Device} chunk_count, entry_count := int(store_read_u32(data, 20)), int(store_read_u32(data, 24)) - if chunk_count > (len(data) - STORE_HEADER) / CHUNK_HEADER || entry_count > (len(data) - STORE_HEADER) / ENTRY_HEADER {return false} + if chunk_count > (len(data) - STORE_HEADER) / CHUNK_HEADER || entry_count > (len(data) - STORE_HEADER) / ENTRY_HEADER {return .Unreadable} pipeline_store_clear(s) - if !pipeline_store_decode_into(s, data, chunk_count, entry_count) {pipeline_store_clear(s); return false} + if !pipeline_store_decode_into(s, data, chunk_count, entry_count) {pipeline_store_clear(s); return .Unreadable} delete(s.file, s.allocator) s.file = data - return true + return .Read } @(private = "file") diff --git a/rhi/pipeline_store_test.odin b/rhi/pipeline_store_test.odin index 6f84a1ed7..bd070e4e8 100644 --- a/rhi/pipeline_store_test.odin +++ b/rhi/pipeline_store_test.odin @@ -43,6 +43,15 @@ test_run :: proc(path: string, keys: []u64, size := 1000, limit := PIPELINE_STOR return } +// What a run that reads the store of path finds there, without changing it. +@(private = "file") +test_state :: proc(path: string, device := TEST_DEVICE) -> Pipeline_Store_State { + s: Pipeline_Store + pipeline_store_init(&s, device) + defer pipeline_store_destroy(&s) + return pipeline_store_load(&s, path) +} + @(private = "file") test_has :: proc(found: []u64, key: u64) -> bool { for k in found {if k == key {return true}} @@ -92,6 +101,7 @@ pipeline_store_drops_what_runs_no_longer_use :: proc(t: ^testing.T) { folder := base.test_folder(t, "pipeline-store") if folder == "" {return} path := fmt.tprintf("%s/pipelines.bin", folder) + testing.expect_value(t, test_state(path), Pipeline_Store_State.Missing) test_run(path, {1, 2}) for _ in 0 ..< PIPELINE_STORE_RUNS {test_run(path, {1})} last := test_run(path, {1, 2}) @@ -99,6 +109,8 @@ pipeline_store_drops_what_runs_no_longer_use :: proc(t: ^testing.T) { testing.expect(t, !test_has(last[:], 2), "a key no run used for PIPELINE_STORE_RUNS runs left") other := TEST_DEVICE other.driver += 1 + testing.expect_value(t, test_state(path), Pipeline_Store_State.Read) + testing.expect_value(t, test_state(path, other), Pipeline_Store_State.Other_Device) testing.expect_value(t, len(test_run(path, {1}, device = other)), 0) testing.expect_value(t, len(test_run(path, {1})), 0) } @@ -151,6 +163,7 @@ pipeline_store_replaces_a_file_it_cannot_read :: proc(t: ^testing.T) { } for c in cases { testing.expect_value(t, os.write_entire_file(path, c.data), nil) + testing.expectf(t, test_state(path) == .Unreadable, "%s: it is told unreadable", c.name) testing.expectf(t, len(test_run(path, {1, 2}, size = 64)) == 0, "%s: nothing is taken from it", c.name) testing.expectf(t, len(test_run(path, {1, 2}, size = 64)) == 2, "%s: the next run reads the file that replaced it", c.name) } diff --git a/rhi/rhi.odin b/rhi/rhi.odin index 9c51bbdd2..506650ebf 100644 --- a/rhi/rhi.odin +++ b/rhi/rhi.odin @@ -751,6 +751,30 @@ Device_Stats :: struct { deletion_overflows: int, // times a frame slot held more destroyed objects than its capacity oversized: int, // transients past the frame ring, each given a buffer for its frame host_memory: int, // Vulkan: resources of GPU memory placed in system memory because device memory was full + pipelines_made: int, // pipelines created since the device was, on any thread + pipelines_kept: int, // of those, read whole from the pipelines kept across runs: no compilation + pipeline_ns: i64, // what creating them took, summed over the threads + pipeline_longest_ns: i64, + pipeline_store: Pipeline_Store_State, +} + +// What the device found of the pipelines kept across runs when it was created. +// Other_Device: they were of another GPU or driver (an update of the driver), +// so every pipeline compiles again. +Pipeline_Store_State :: enum u8 {None, Read, Missing, Other_Device, Unreadable} + +// Counts a pipeline whose creation call took took nanoseconds; kept: the +// driver did not compile it. +@(private) +pipeline_counted :: proc "contextless" (d: ^Device, took: i64, kept: bool) { + sync.atomic_add(&d.stats.pipelines_made, 1) + if kept {sync.atomic_add(&d.stats.pipelines_kept, 1)} + sync.atomic_add(&d.stats.pipeline_ns, took) + for { + longest := sync.atomic_load(&d.stats.pipeline_longest_ns) + if took <= longest {break} + if _, ok := sync.atomic_compare_exchange_weak(&d.stats.pipeline_longest_ns, longest, took); ok {break} + } } @(private) diff --git a/rhi/vulkan_device.odin b/rhi/vulkan_device.odin index 11b5a725e..e15a09b51 100644 --- a/rhi/vulkan_device.odin +++ b/rhi/vulkan_device.odin @@ -896,7 +896,7 @@ vk_create_logical_device :: proc(d: ^Device, desc: Device_Desc) -> bool { if d.store_path_len > 0 { p := d.properties pipeline_store_init(&d.pipelines_kept, {vendor = p.vendorID, device = p.deviceID, driver = p.driverVersion, uuid = p.pipelineCacheUUID}, allocator = d.allocator) - pipeline_store_load(&d.pipelines_kept, desc.pipeline_cache_path) + d.stats.pipeline_store = pipeline_store_load(&d.pipelines_kept, desc.pipeline_cache_path) } return true } @@ -943,6 +943,15 @@ vk_create_frames :: proc(d: ^Device, desc: Device_Desc) -> bool { return true } +// Writes the pipelines kept across runs now, after a write another thread is +// making: what was compiled survives a process that ends without destroying +// the device. Without a path, nothing. +device_pipelines_keep :: proc(d: ^Device) { + if d == nil || d.store_path_len == 0 {return} + path := string(d.store_path[:d.store_path_len]) + for !pipeline_store_save(&d.pipelines_kept, path) && sync.atomic_load(&d.pipelines_kept.saving) {time.sleep(10 * time.Millisecond)} +} + device_destroy :: proc(d: ^Device) { if d == nil {return} // A GPU that may still run work a wait gave up on keeps its objects: the diff --git a/rhi/vulkan_resources.odin b/rhi/vulkan_resources.odin index a7444a7e5..d21553da6 100644 --- a/rhi/vulkan_resources.odin +++ b/rhi/vulkan_resources.odin @@ -684,7 +684,9 @@ pipeline_create_compute :: proc(d: ^Device, desc: Compute_Pipeline_Desc) -> Pipe cache := vk_pipeline_cache_open(d, key) started := time.tick_now() created := vk_check(d, d.dvk.CreateComputePipelines(d.handle, cache, 1, &info, nil, &p.handle), desc.name, queued = false) - vk_pipeline_cache_close(d, key, cache, feedback, created, time.tick_since(started)) + took := time.tick_since(started) + if created {pipeline_counted(d, time.duration_nanoseconds(took), feedback.flags >= {.VALID, .APPLICATION_PIPELINE_CACHE_HIT})} + vk_pipeline_cache_close(d, key, cache, feedback, created, took) if !created {vk_release_pipeline(d, &p); return 0} vk_set_name(d, .PIPELINE, u64(p.handle), desc.name) return vk_pipeline_add(d, &p) @@ -787,7 +789,9 @@ pipeline_create_graphics :: proc(d: ^Device, desc: Graphics_Pipeline_Desc) -> Pi cache := vk_pipeline_cache_open(d, key) started := time.tick_now() created := vk_check(d, d.dvk.CreateGraphicsPipelines(d.handle, cache, 1, &info, nil, &p.handle), desc.name, queued = false) - vk_pipeline_cache_close(d, key, cache, feedback, created, time.tick_since(started)) + took := time.tick_since(started) + if created {pipeline_counted(d, time.duration_nanoseconds(took), feedback.flags >= {.VALID, .APPLICATION_PIPELINE_CACHE_HIT})} + vk_pipeline_cache_close(d, key, cache, feedback, created, took) if !created {vk_release_pipeline(d, &p); return 0} vk_set_name(d, .PIPELINE, u64(p.handle), desc.name) return vk_pipeline_add(d, &p) diff --git a/ui/font.odin b/ui/font.odin index bc9bd71de..951003313 100644 --- a/ui/font.odin +++ b/ui/font.odin @@ -771,6 +771,39 @@ text_width :: proc "contextless" (font:^Font,text:string,size:f32,f:int=0) -> f3 return max(current,largest) } +// Paints one line of text into coverage (R8, width*height texels) on the CPU, +// the pen from x on the baseline y, in device pixels: each glyph at the face +// nearest size at the density of the font, the coverage of the atlas kept +// where it is the larger. For what shows text before the GPU can draw it. +text_paint :: proc(font:^Font,text:string,size:f32,coverage:[]u8,width,height:int,x,y:f32,f:int=0) { + if font==nil || !font.ready || width<=0 || height<=0 || len(coverage)1e6 || abs(y)>1e6 {return} + face:=font_face(font,f,size*font.density) + if face==nil {return} + pen:=text_pen(font,size,f) + at:=x + for character in text { + kern,advance:=text_pen_step(&pen,character) + at+=kern*font.density + if index:=font_index(font,f,character);index>=0 { + g:=face.glyphs[index] + left,top:=int(math.floor(at+g.bounds.x+0.5)),int(math.floor(y+g.bounds.y+0.5)) + u,v:=int(g.uv.x*f32(font.width)+0.5),int(g.uv.y*f32(font.height)+0.5) + for row in 0..=height || sy<0 || sy>=font.height {continue} + for column in 0..=width || sx<0 || sx>=font.width {continue} + texel:=&coverage[ty*width+tx] + texel^=max(texel^,font.pixels[sy*font.width+sx]) + } + } + } + at+=advance*font.density + } +} + Text_Wrap :: enum u8 {None,Words} // Sizes between the top of one line of text and the top of the next: the diff --git a/ui/font_test.odin b/ui/font_test.odin index 4e2835b5f..69a7c3ec2 100644 --- a/ui/font_test.odin +++ b/ui/font_test.odin @@ -556,3 +556,25 @@ text_at_a_size_without_its_face_gets_one_between_frames :: proc(t:^testing.T) { semibold:=font_face(&owner,FONT_SEMIBOLD,18) testing.expect(t,semibold!=nil && semibold.size==18,"18 next to the face of 17 has its own") } + +// Text painted on the CPU lands on the line it is given and clips to the image. +@(test) +text_paint_covers_its_line_and_clips_to_the_image :: proc(t:^testing.T) { + font:Font + if !testing.expect(t,font_init(&font,{FONT_INTER},{20},1)) {return} + defer font_destroy(&font) + width,height:=200,40 + coverage:=make([]u8,width*height);defer delete(coverage) + text_paint(&font,"Shaders",20,coverage,width,height,10,28) + inside,above:=0,0 + for y in 0..=8 && y<=34 && x>=8 && x<=10+int(text_width(&font,"Shaders",20))+2 {inside+=1} else {above+=1} + }} + testing.expect(t,inside>100,"the glyphs cover their line") + testing.expect_value(t,above,0) + for at in ([][2]f32{{-150,28},{180,28},{10,-5},{10,60},{math.nan_f32(),28},{10,math.inf_f32(1)},{1e9,1e9}}) { + text_paint(&font,"Shaders",20,coverage,width,height,at[0],at[1]) + } + text_paint(&font,"Shaders",20,coverage[:10],width,height,10,28) +} From fae704c09f0e527327a866dab22f09720e278a7a Mon Sep 17 00:00:00 2001 From: 5hif7y <44300833+5hif7y@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:13:56 -0300 Subject: [PATCH 2/3] Add mirai bench pipelines: a cold start of a project against a warm one It plays the project twice 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). Refs #109 Co-Authored-By: Claude Opus 5.5 --- .../mirai-projects/references/projects.md | 1 + tools/mirai/bench.odin | 3 +- tools/mirai/bench_pipelines.odin | 220 ++++++++++++++++++ tools/mirai/bench_pipelines_test.odin | 44 ++++ tools/mirai/main.odin | 1 + 5 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 tools/mirai/bench_pipelines.odin create mode 100644 tools/mirai/bench_pipelines_test.odin diff --git a/.agents/skills/mirai-projects/references/projects.md b/.agents/skills/mirai-projects/references/projects.md index 1fec2528a..c51f7dd09 100644 --- a/.agents/skills/mirai-projects/references/projects.md +++ b/.agents/skills/mirai-projects/references/projects.md @@ -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 -- [arguments...]` | | See who is building or measuring | `mirai locks` | diff --git a/tools/mirai/bench.odin b/tools/mirai/bench.odin index d59f3f94a..aa60e9dfd 100644 --- a/tools/mirai/bench.odin +++ b/tools/mirai/bench.odin @@ -107,7 +107,8 @@ Bench_Report :: struct { cmd_bench :: proc(root:string,args:[]string) -> int { if len(args)>0 && args[0]=="compare" {return bench_compare(root,args[1:])} - fmt.eprintfln("Usage: mirai %s",BENCH_USAGE) + if len(args)>0 && args[0]=="pipelines" {return bench_pipelines(root,args[1:])} + fmt.eprintfln("Usage: mirai %s\n mirai %s",BENCH_USAGE,BENCH_PIPELINES_USAGE) return 2 } diff --git a/tools/mirai/bench_pipelines.odin b/tools/mirai/bench_pipelines.odin new file mode 100644 index 000000000..004a77c18 --- /dev/null +++ b/tools/mirai/bench_pipelines.odin @@ -0,0 +1,220 @@ +package main + +import "core:fmt" +import "core:os" +import "core:path/filepath" +import "core:strconv" +import "core:strings" +import "mirai:project" +import "mirai:std/subprocess" + +BENCH_PIPELINES_USAGE :: "bench pipelines [project] [--scene S] [--frames N] [--release] [--out file]" + +// mirai bench pipelines: what compiling the GPU pipelines costs a start of the +// game, the first after an install or an update against the next. It plays +// the project twice with a window, a cold start and a warm one, and writes the +// table of each: the start, the pipelines the driver compiled and what the +// frames lost to them. Cold is both caches empty: the pipelines the project +// kept (set aside, and put back after) and the driver's own, which the run +// points at an empty folder through the variables each driver reads. A cold +// run is only believed when the kept pipelines were missing and the driver +// wrote into that folder; otherwise the table says the run may not have been +// cold, and why. + +BENCH_PIPELINES_FRAMES :: 300 +BENCH_PIPELINES_ASIDE :: ".bench-aside" // after the kept pipelines while they are set aside +// The variables that point each driver's cache of compiled shaders at a folder. +BENCH_PIPELINES_DRIVER_PATHS :: [?]string{"AMD_VK_PIPELINE_CACHE_PATH","__GL_SHADER_DISK_CACHE_PATH","MESA_SHADER_CACHE_DIR"} + +Bench_Pipelines_Options :: struct { + folder,scene,out:string, + frames:int, + mode:Build_Mode, +} + +// What one start of the player says about its pipelines (host/player.odin, +// host/load_profile.odin); found: the summary line was there. +Bench_Pipelines_Run :: struct { + startup_ms,resident_ms:f64, + made,kept,late:int, + driver_ms,longest_ms,waited_ms:f64, + store:string, + found:bool, +} + +@(private) +bench_pipelines_options :: proc(args:[]string) -> (o:Bench_Pipelines_Options,error:string) { + o={folder=".",frames=BENCH_PIPELINES_FRAMES} + mode,rest:=build_mode_take(args) + o.mode=mode + named:=false + for i:=0;i=len(rest) || rest[i+1]=="" {return o,fmt.tprintf("%s needs a value",rest[i])} + option,value:=rest[i],rest[i+1] + i+=1 + switch option { + case "--scene": o.scene=value + case "--out": o.out=value + case "--frames": + number,ok:=strconv.parse_int(value,10) + if !ok || number<30 || number>36000 {return o,fmt.tprintf("--frames goes from 30 to 36000, not '%s'",value)} + o.frames=number + } + } + return o,"" +} + +// The value in line between after and before, as a number; ok false when +// either is missing or it is no number. +@(private="file") +bench_pipelines_between :: proc(line,after,before:string) -> (value:f64,ok:bool) { + start:=0 + if after!="" { + at:=strings.index(line,after) + if at<0 {return 0,false} + start=at+len(after) + } + end:=strings.index(line[start:],before) + if end<0 {return 0,false} + text:=strings.trim_space(line[start:][:end]) + if space:=strings.last_index_byte(text,' ');space>=0 {text=text[space+1:]} + return strconv.parse_f64(text) +} + +// Reads what the player printed (stdout and stderr) into a run. +bench_pipelines_parse :: proc(output:string) -> (run:Bench_Pipelines_Run) { + for line in strings.split_lines(output,context.temp_allocator) { + switch { + case strings.has_prefix(line,"mirai: load Scene "): + run.resident_ms,_=bench_pipelines_between(line,"resident at "," ms") + case strings.has_prefix(line," phases: ") && strings.contains(line," Startup "): + // The phases go the costliest first: Startup is anywhere in the line. + run.startup_ms,_=bench_pipelines_between(line," Startup "," ms") + case strings.has_prefix(line," pipelines: ") && strings.contains(line," made ("): + made,_:=bench_pipelines_between(line,"pipelines: "," made") + kept,_:=bench_pipelines_between(line,"(", " kept)") + late,_:=bench_pipelines_between(line,"; "," frames late") + run.made,run.kept,run.late=int(made),int(kept),int(late) + run.driver_ms,_=bench_pipelines_between(line,"kept), "," ms in the driver") + run.longest_ms,_=bench_pipelines_between(line,"longest "," ms") + run.waited_ms,_=bench_pipelines_between(line,"frames late, "," ms waited") + if at:=strings.index(line,"store ");at>=0 { + rest:=line[at+len("store "):] + if end:=strings.index_byte(rest,';');end>=0 {run.store=strings.clone(rest[:end],context.temp_allocator)} + } + run.found=true + } + } + return +} + +// Why a cold run may not have been cold; "" when both caches were empty. +bench_pipelines_cold_doubt :: proc(cold:Bench_Pipelines_Run,driver_wrote:bool) -> string { + switch { + case !cold.found: return "the player did not print its pipelines: nothing to say of the run" + case cold.store!="Missing": return fmt.tprintf("the kept pipelines were %s, not Missing: they were not set aside, and the run read them",cold.store) + case !driver_wrote: return "the driver wrote nothing in the folder its variables named: it kept its own cache, which may have served this run, and its times are a floor" + } + return "" +} + +@(private) +bench_pipelines :: proc(root:string,args:[]string) -> int { + o,error:=bench_pipelines_options(args) + if error!="" {fmt.eprintfln("mirai bench: %s\nUsage: mirai %s",error,BENCH_PIPELINES_USAGE);return 2} + if !os.is_file(project.path(o.folder,project.PROJECT_FILE,allocator=context.temp_allocator)) { + fmt.eprintfln("mirai bench: %s is not a project (%s is missing); pass the folder of the project",o.folder,project.PROJECT_FILE) + return 2 + } + folder:=project.path_absolute(o.folder,context.temp_allocator) + if file,alive:=session_find(folder);alive { + fmt.eprintfln("mirai bench: an editor of the project is open (pid %d) and keeps the same pipelines; close it, or run mirai session stop %s",file.pid,o.folder) + return 1 + } + user:=project.user_folder(folder,context.temp_allocator) + if user=="" {fmt.eprintln("mirai bench: the project has no user folder where its pipelines are kept");return 1} + kept:=filepath.join({user,project.PIPELINES_FILE},context.temp_allocator) or_else "" + aside:=strings.concatenate({kept,BENCH_PIPELINES_ASIDE},context.temp_allocator) + if os.exists(aside) { + fmt.eprintfln("mirai bench: %s is left from a measurement that did not end: rename it back to %s, or delete it",aside,project.PIPELINES_FILE) + return 1 + } + if code,_,_:=build_project(root,.Editor,{o.folder},o.mode);code!=0 {return code} + temporary,temporary_error:=os.temp_directory(context.temp_allocator) + if temporary_error!=nil {fmt.eprintln("mirai bench: there is no temporary folder for the driver's cache");return 1} + driver:=filepath.join({temporary,fmt.tprintf("mirai-bench-pipelines-%d",os.get_pid())},context.temp_allocator) or_else "" + if os.make_directory_all(driver)!=nil {fmt.eprintfln("mirai bench: could not make %s",driver);return 1} + defer os.remove_all(driver) + + // The kept pipelines go aside for the measurement and come back after it, whatever it gave. + had:=os.exists(kept) + if had && os.rename(kept,aside)!=nil {fmt.eprintfln("mirai bench: could not set %s aside: is a game of the project open?",kept);return 1} + defer { + os.remove(kept) + if had && os.rename(aside,kept)!=nil {fmt.eprintfln("mirai bench: could not put %s back: rename %s to it",kept,aside)} + } + + env:=make([dynamic]string,context.temp_allocator) + if environ,environ_error:=os.environ(context.temp_allocator);environ_error==nil {append(&env,..environ)} + for name in BENCH_PIPELINES_DRIVER_PATHS {append(&env,fmt.tprintf("%s=%s",name,driver))} + append(&env,"AMD_VK_PIPELINE_CACHE_FILENAME=mirai-bench","__GL_SHADER_DISK_CACHE_SKIP_CLEANUP=1") + command:=make([dynamic]string,context.temp_allocator) + append(&command,project_engine_paths(root,o.folder,o.mode).player_program,"--project",folder,"--frames",fmt.tprint(o.frames),"--show-window") + if o.scene!="" {append(&command,"--scene",o.scene)} + + runs:[2]Bench_Pipelines_Run + names:=[2]string{"cold","warm"} + driver_wrote:=false + for &run,i in runs { + fmt.printfln("mirai bench: %s start of %s (%d frames)…",names[i],o.folder,o.frames) + state,stdout,stderr,run_error:=subprocess.exec({command=command[:],env=env[:],working_dir=root},context.temp_allocator) + if run_error!=nil || state.exit_code!=0 { + fmt.eprintfln("mirai bench: the %s start failed (exit %d %v):\n%s",names[i],state.exit_code,run_error,string(stderr)) + return 1 + } + run=bench_pipelines_parse(strings.concatenate({string(stdout),"\n",string(stderr)},context.temp_allocator)) + if i==0 { + if entries,read_error:=os.read_all_directory_by_path(driver,context.temp_allocator);read_error==nil {driver_wrote=len(entries)>0} + } + } + table:=bench_pipelines_table(o,runs,bench_pipelines_cold_doubt(runs[0],driver_wrote)) + fmt.print(table) + if o.out!="" && os.write_entire_file(o.out,transmute([]u8)table)!=nil { + fmt.eprintfln("mirai bench: could not write %s",o.out) + return 1 + } + return 0 +} + +// The table of the two starts, in Markdown, with what to doubt. +bench_pipelines_table :: proc(o:Bench_Pipelines_Options,runs:[2]Bench_Pipelines_Run,doubt:string) -> string { + b:=strings.builder_make(context.temp_allocator) + scene:=o.scene if o.scene!="" else "the start scene" + fmt.sbprintfln(&b,"Pipelines of %s, %s, %d frames:\n",o.folder,scene,o.frames) + fmt.sbprintln(&b,"| | Cold | Warm |") + fmt.sbprintln(&b,"| --- | ---: | ---: |") + cold,warm:=runs[0],runs[1] + fmt.sbprintfln(&b,"| Start of the renderer, ms | %.0f | %.0f |",cold.startup_ms,warm.startup_ms) + fmt.sbprintfln(&b,"| Resident, ms after the load began | %.0f | %.0f |",cold.resident_ms,warm.resident_ms) + fmt.sbprintfln(&b,"| Pipelines made | %d | %d |",cold.made,warm.made) + fmt.sbprintfln(&b,"| Compiled by the driver | %d | %d |",cold.made-cold.kept,warm.made-warm.kept) + fmt.sbprintfln(&b,"| Driver time, ms summed | %.0f | %.0f |",cold.driver_ms,warm.driver_ms) + fmt.sbprintfln(&b,"| Longest pipeline, ms | %.1f | %.1f |",cold.longest_ms,warm.longest_ms) + fmt.sbprintfln(&b,"| Frames late (materials as rows) | %d | %d |",cold.late,warm.late) + fmt.sbprintfln(&b,"| Frames waited, ms | %.0f | %.0f |",cold.waited_ms,warm.waited_ms) + fmt.sbprintfln(&b,"| Kept pipelines found | %s | %s |",cold.store,warm.store) + if doubt!="" {fmt.sbprintfln(&b,"\nThe cold start may not have been cold: %s.",doubt)} + if warm.found && warm.made>warm.kept { + fmt.sbprintfln(&b,"\nThe warm start compiled %d: pipelines the cold one did not keep (speculative ones it had not ended), or keys that change between starts.",warm.made-warm.kept) + } + return strings.to_string(b) +} diff --git a/tools/mirai/bench_pipelines_test.odin b/tools/mirai/bench_pipelines_test.odin new file mode 100644 index 000000000..a31c51848 --- /dev/null +++ b/tools/mirai/bench_pipelines_test.odin @@ -0,0 +1,44 @@ +package main + +import "core:strings" +import "core:testing" + +// Lines as the player prints them (host/player.odin, host/load_profile.odin). +@(private="file") +BENCH_PIPELINES_COLD :: `mirai: Compiling shaders 0/97, 0:01 (only needed once per install or update) +mirai: load Scene control/control.scene: ready at 653 ms, resident at 932 ms, 18 entities + phases: Startup 288424 ms Prepare 71 ms (summed) Nav_Tiles 32 ms (summed) + pipelines: 3 pipelines compiled in 723 ms of the driver, although the kept pipelines were read: their shaders changed +mirai: 299 frames, mean frame time 18.09 ms, drawn at 1920x1017, no player options + pipelines: 102 made (0 kept), 535205 ms in the driver, longest 184201.5 ms, store Missing; 3 frames late, 675 ms waited` + +@(test) +bench_pipelines_reads_what_the_player_says :: proc(t:^testing.T) { + run:=bench_pipelines_parse(BENCH_PIPELINES_COLD) + testing.expect(t,run.found) + testing.expect_value(t,run.startup_ms,288424) + testing.expect_value(t,run.resident_ms,932) + testing.expect_value(t,run.made,102) + testing.expect_value(t,run.kept,0) + testing.expect_value(t,run.driver_ms,535205) + testing.expect_value(t,run.longest_ms,184201.5) + testing.expect_value(t,run.store,"Missing") + testing.expect_value(t,run.late,3) + testing.expect_value(t,run.waited_ms,675) + testing.expect(t,!bench_pipelines_parse("mirai: 299 frames, mean frame time 18 ms").found,"no summary, nothing found") + later:=bench_pipelines_parse(" phases: Nav_Tiles 1074 ms (summed) Startup 1027 ms Prepare 53 ms (summed)") + testing.expect_value(t,later.startup_ms,1027) +} + +// A cold start is believed only with both caches empty: the kept pipelines +// missing and the driver writing its cache anew. +@(test) +bench_pipelines_doubts_a_start_that_was_not_cold :: proc(t:^testing.T) { + cold:=bench_pipelines_parse(BENCH_PIPELINES_COLD) + testing.expect_value(t,bench_pipelines_cold_doubt(cold,true),"") + testing.expect(t,strings.contains(bench_pipelines_cold_doubt(cold,false),"own cache"),"a driver that kept its cache") + read:=cold + read.store="Read" + testing.expect(t,strings.contains(bench_pipelines_cold_doubt(read,true),"not set aside"),"kept pipelines read") + testing.expect(t,bench_pipelines_cold_doubt({},true)!="","a run that printed nothing") +} diff --git a/tools/mirai/main.odin b/tools/mirai/main.odin index 6acc17b6f..1928a94a8 100644 --- a/tools/mirai/main.odin +++ b/tools/mirai/main.odin @@ -42,6 +42,7 @@ COMMANDS :: [?]Command{ {"record",RECORD_USAGE,"Records the game of the project frame after frame at a fixed rate as 16-bit PNG or EXR images and a WAV, with a contact sheet and a manifest of its frames and its validation errors",cmd_record}, {"render",RENDER_USAGE,"Measures the fixed views of a project as the user sees them (cost per pass, stability, luminance, captures) against the base build",cmd_render}, {"bench",BENCH_USAGE,"Measures the frame cost of a project against the base build in alternating pairs, alone among the checkouts, and writes the table for a report",cmd_bench}, + {"bench",BENCH_PIPELINES_USAGE,"Measures a cold start of a project against a warm one with a window: the GPU pipelines the driver compiles and what the frames lose to them, with both caches emptied and the cold start checked",cmd_bench}, {"rpc",RPC_USAGE,"Calls a method of the editor of the project or of the given port, shows fields of the response or waits for a state; describe shows the params of the methods",cmd_rpc}, {"docs",DOCS_USAGE,"Prints the game documentation of this engine: the index, a topic or one section of it",cmd_docs}, {"mcp",MCP_USAGE,"MCP server over stdio that forwards to the editor of the project or of the given port; gives the editor tools and those of the plugins in --plugins",cmd_mcp}, From 4498ba71721824cc55bd6d4efdc16c27a7748c85 Mon Sep 17 00:00:00 2001 From: 5hif7y <44300833+5hif7y@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:51:33 -0300 Subject: [PATCH 3/3] Count the Shaders row in the test of the Graphics rows Graphics_Settings.shaders is a tagged field of the machine settings, so the popup has one row more. Refs #109 Co-Authored-By: Claude Opus 5.5 --- editor/graphics_test.odin | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/editor/graphics_test.odin b/editor/graphics_test.odin index ee89b668c..6d7afe2b0 100644 --- a/editor/graphics_test.odin +++ b/editor/graphics_test.odin @@ -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}, @@ -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