|
| 1 | +// Copyright (c) 1993-1996 id Software, Inc. |
| 2 | +// Copyright (c) 2026 the go-doom/engine authors. |
| 3 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | + |
| 5 | +package gore |
| 6 | + |
| 7 | +import ( |
| 8 | + "image" |
| 9 | + "os" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// benchFrontend is a headless, zero-cost DoomFrontend used by the |
| 15 | +// performance-parity timedemo benchmark. It records every rendered |
| 16 | +// frame (DrawFrame) and the wall-clock window over which they arrived |
| 17 | +// so we can derive ms/frame and FPS for comparison against |
| 18 | +// chocolate-doom's `-timedemo demo1` (which reports the same |
| 19 | +// gametics/realtics/FPS figure). It never copies the framebuffer or |
| 20 | +// touches I/O so the measured time is dominated by the engine's |
| 21 | +// software renderer (r_RenderPlayerView -> r_DrawColumn / r_DrawSpan), |
| 22 | +// matching the C engine's headless dummy-video path. |
| 23 | +type benchFrontend struct { |
| 24 | + frames int64 |
| 25 | + firstAt time.Time |
| 26 | + lastAt time.Time |
| 27 | + stopAfter int64 // Stop() after this many rendered frames |
| 28 | + checksum uint64 |
| 29 | + firstTic int32 // gametic at first counted frame |
| 30 | + lastTic int32 // gametic at last counted frame |
| 31 | + ticsSeen map[int32]struct{} |
| 32 | +} |
| 33 | + |
| 34 | +func (b *benchFrontend) DrawFrame(img *image.RGBA) { |
| 35 | + // Count ONLY frames where the engine ran the 3D world renderer |
| 36 | + // (r_RenderPlayerView): gamestate gs_LEVEL during demo playback. |
| 37 | + // The title/credit pages (gs_DEMOSCREEN) are a flat patch blit and |
| 38 | + // must not dilute the frame-time, just as chocolate-doom's |
| 39 | + // `-timedemo demo1` times only the demo's gameplay frames. |
| 40 | + if gamestate != gs_LEVEL || demoplayback == 0 || gametic == 0 { |
| 41 | + return |
| 42 | + } |
| 43 | + // Only count the FIRST redraw of each distinct gametic: at full |
| 44 | + // speed the loop may redraw an unchanged world several times per |
| 45 | + // advanced demo tic; those duplicate cache-hot redraws would |
| 46 | + // understate per-demo-frame cost. chocolate-doom renders exactly |
| 47 | + // once per gametic, so we mirror that by timing only fresh tics. |
| 48 | + if b.ticsSeen == nil { |
| 49 | + b.ticsSeen = make(map[int32]struct{}, 16384) |
| 50 | + } |
| 51 | + if _, seen := b.ticsSeen[gametic]; seen { |
| 52 | + return |
| 53 | + } |
| 54 | + now := time.Now() |
| 55 | + if b.frames == 0 { |
| 56 | + b.firstAt = now |
| 57 | + b.firstTic = gametic |
| 58 | + } |
| 59 | + b.frames++ |
| 60 | + b.lastAt = now |
| 61 | + b.lastTic = gametic |
| 62 | + b.ticsSeen[gametic] = struct{}{} |
| 63 | + // Touch a handful of pixels so the renderer's output cannot be |
| 64 | + // dead-code-eliminated, without paying a full-frame copy. |
| 65 | + if len(img.Pix) >= 256 { |
| 66 | + for i := 0; i < 256; i += 16 { |
| 67 | + b.checksum += uint64(img.Pix[i]) |
| 68 | + } |
| 69 | + } |
| 70 | + if b.stopAfter > 0 && b.frames >= b.stopAfter { |
| 71 | + Stop() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (b *benchFrontend) SetTitle(string) {} |
| 76 | +func (b *benchFrontend) CacheSound(string, []byte) {} |
| 77 | +func (b *benchFrontend) PlaySound(string, int, int, int) {} |
| 78 | +func (b *benchFrontend) GetEvent(ev *DoomEvent) bool { return false } |
| 79 | + |
| 80 | +// BenchmarkTimedemoDemo1 runs the built-in DEMO1 playback at full speed |
| 81 | +// (no frame-rate cap) headless, and reports the wall-clock frame time of |
| 82 | +// the software renderer. This is the full-engine parity benchmark vs |
| 83 | +// chocolate-doom 3.1.1 `-timedemo demo1` on the same host. |
| 84 | +// |
| 85 | +// It is NOT a normal go-benchmark loop: the engine keeps global state |
| 86 | +// across a Run() and cannot be re-entered, so we run the demo exactly |
| 87 | +// once for a fixed frame budget (b.N is ignored; invoke with |
| 88 | +// -benchtime=1x). The reported custom metrics are the headline numbers: |
| 89 | +// |
| 90 | +// ms/frame -- wall ms per rendered software frame |
| 91 | +// fps -- rendered software frames per wall second |
| 92 | +// |
| 93 | +// Skips cleanly if the shareware/Freedoom IWAD is not present (CI |
| 94 | +// without the WAD), so it never breaks the test gate. |
| 95 | +func BenchmarkTimedemoDemo1(b *testing.B) { |
| 96 | + wad := findBenchWAD() |
| 97 | + if wad == "" { |
| 98 | + b.Skip("no IWAD found (set DOOM_BENCH_WAD or place doom1.wad); skipping timedemo benchmark") |
| 99 | + } |
| 100 | + |
| 101 | + // Full speed: never sleep, advance the fake tic clock once per frame |
| 102 | + // so the demo plays as fast as the renderer can produce frames -- |
| 103 | + // the same intent as chocolate-doom's -timedemo (uncapped). |
| 104 | + dg_run_full_speed = true |
| 105 | + |
| 106 | + fe := &benchFrontend{stopAfter: 5026} |
| 107 | + |
| 108 | + // Safety net: if the demo somehow never reaches the frame budget, |
| 109 | + // bound the whole run so the benchmark can never hang. |
| 110 | + go func() { |
| 111 | + time.Sleep(60 * time.Second) |
| 112 | + Stop() |
| 113 | + }() |
| 114 | + |
| 115 | + start := time.Now() |
| 116 | + Run(fe, []string{"-iwad", wad, "-nosound", "-nomusic"}) |
| 117 | + wall := fe.lastAt.Sub(fe.firstAt) |
| 118 | + if fe.frames < 2 || wall <= 0 { |
| 119 | + b.Fatalf("timedemo produced too few frames: frames=%d wall=%v", fe.frames, wall) |
| 120 | + } |
| 121 | + |
| 122 | + distinctTics := int64(len(fe.ticsSeen)) |
| 123 | + msPerFrame := float64(wall.Nanoseconds()) / 1e6 / float64(fe.frames) |
| 124 | + fps := float64(fe.frames) / wall.Seconds() |
| 125 | + // Per-distinct-gametic timing: directly comparable to chocolate-doom |
| 126 | + // -timedemo, which times distinct demo gametics (one rendered view |
| 127 | + // per advanced tic), not raw redraw count. |
| 128 | + msPerTic := float64(wall.Nanoseconds()) / 1e6 / float64(distinctTics) |
| 129 | + ticFPS := float64(distinctTics) / wall.Seconds() |
| 130 | + b.ReportMetric(msPerTic, "ms/demoframe") |
| 131 | + b.ReportMetric(ticFPS, "demofps") |
| 132 | + b.ReportMetric(msPerFrame, "ms/redraw") |
| 133 | + b.ReportMetric(fps, "redrawfps") |
| 134 | + b.Logf("rendered %d redraws over %d distinct gametics (gametic %d..%d) in %v (startup excluded: %v)", |
| 135 | + fe.frames, distinctTics, fe.firstTic, fe.lastTic, wall, time.Since(start)-wall) |
| 136 | + b.Logf(" per distinct demo gametic: %.4f ms (%.1f demo-fps); per redraw: %.4f ms (%.1f fps); checksum=%d", |
| 137 | + msPerTic, ticFPS, msPerFrame, fps, fe.checksum) |
| 138 | +} |
| 139 | + |
| 140 | +// findBenchWAD locates an IWAD for the benchmark: explicit env override |
| 141 | +// first, then the repo's embedwad copy, then the cwd. |
| 142 | +func findBenchWAD() string { |
| 143 | + if p := os.Getenv("DOOM_BENCH_WAD"); p != "" { |
| 144 | + if _, err := os.Stat(p); err == nil { |
| 145 | + return p |
| 146 | + } |
| 147 | + } |
| 148 | + for _, c := range []string{"embedwad/doom1.wad", "doom1.wad", "freedoom1.wad", "embedwad/freedoom1.wad"} { |
| 149 | + if _, err := os.Stat(c); err == nil { |
| 150 | + return c |
| 151 | + } |
| 152 | + } |
| 153 | + return "" |
| 154 | +} |
0 commit comments