From 9a7f813ebce4641732636e22fa7acecc87a4705c Mon Sep 17 00:00:00 2001 From: Ben Perry Date: Fri, 23 Aug 2024 12:27:09 -0500 Subject: [PATCH] Pass frame delta for fixed movement rate in benchmarks --- tools/README.md | 12 ++++++------ tools/benchmark/benchmark.go | 7 +++++-- tools/benchmark/imdraw_bench.go | 22 ++++++++++++---------- tools/benchmark/sprite_bench.go | 28 ++++++++++++++++------------ 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/tools/README.md b/tools/README.md index 2b83a2a..d2a23dd 100644 --- a/tools/README.md +++ b/tools/README.md @@ -51,12 +51,12 @@ Information about the machines used to record benchmark stats | Machine | Pixel | Benchmark | Duration | Frames | FPS Avg | FPS Min | FPS Max | FPS Stdev | |--------------------|--------|------------------------------|----------|--------|---------|---------|---------|-----------| -| bhperry-wsl | v2.2.0 | imdraw-moving | 30.01s | 2232 | 74.37 | 60 | 78 | 3.45 | -| bhperry-wsl | v2.2.0 | imdraw-static | 30.02s | 2334 | 77.75 | 73 | 80 | 1.2 | -| bhperry-wsl | v2.2.0 | sprite-moving | 30.03s | 1452 | 48.35 | 45 | 50 | 1.05 | -| bhperry-wsl | v2.2.0 | sprite-moving-batched | 30.01s | 4004 | 133.42 | 127 | 139 | 2.45 | -| bhperry-wsl | v2.2.0 | sprite-static | 30.02s | 1534 | 51.1 | 48 | 52 | 0.91 | -| bhperry-wsl | v2.2.0 | sprite-static-batched | 30s | 5293 | 176.43 | 163 | 179 | 2.99 | +| bhperry-wsl | v2.2.0 | imdraw-moving | 30s | 2214 | 73.79 | 68 | 76 | 1.77 | +| bhperry-wsl | v2.2.0 | imdraw-static | 30s | 2355 | 78.5 | 72 | 81 | 1.89 | +| bhperry-wsl | v2.2.0 | sprite-moving | 30.03s | 1451 | 48.32 | 45 | 50 | 1.25 | +| bhperry-wsl | v2.2.0 | sprite-moving-batched | 30.01s | 4085 | 136.12 | 127 | 142 | 3.17 | +| bhperry-wsl | v2.2.0 | sprite-static | 30.01s | 1518 | 50.59 | 47 | 52 | 1.45 | +| bhperry-wsl | v2.2.0 | sprite-static-batched | 30.01s | 5318 | 177.2 | 159 | 182 | 6.01 | | bhperry-win10 | v2.2.0 | imdraw-moving | 30.03s | 1425 | 47.45 | 21 | 49 | 4.96 | | bhperry-win10 | v2.2.0 | imdraw-static | 30s | 1533 | 51.1 | 50 | 52 | 0.55 | | bhperry-win10 | v2.2.0 | sprite-moving | 30.02s | 1145 | 38.15 | 37 | 39 | 0.46 | diff --git a/tools/benchmark/benchmark.go b/tools/benchmark/benchmark.go index 9f562f4..d00c4c1 100644 --- a/tools/benchmark/benchmark.go +++ b/tools/benchmark/benchmark.go @@ -64,9 +64,12 @@ func (c Config) Run() (*Stats, error) { second := time.NewTicker(time.Second) done := time.NewTicker(duration) start := time.Now() + last := start loop: for frame = 0; !win.Closed(); frame++ { - benchmark.Step(win) + now := time.Now() + benchmark.Step(win, now.Sub(last).Seconds()) + last = now win.Update() select { @@ -90,7 +93,7 @@ loop: // Benchmark provides hooks into the stages of a window's lifecycle type Benchmark interface { - Step(win *opengl.Window) + Step(win *opengl.Window, delta float64) } // Registry is a collection of benchmark configs diff --git a/tools/benchmark/imdraw_bench.go b/tools/benchmark/imdraw_bench.go index 301a55e..0c7eeea 100644 --- a/tools/benchmark/imdraw_bench.go +++ b/tools/benchmark/imdraw_bench.go @@ -51,7 +51,7 @@ type staticTriangles struct { cell pixel.Vec } -func (st *staticTriangles) Step(win *opengl.Window) { +func (st *staticTriangles) Step(win *opengl.Window, delta float64) { win.Clear(backgroundColor) for i := 0; i < st.cols; i++ { @@ -82,23 +82,27 @@ type movingTriangles struct { imd *imdraw.IMDraw rows, cols int cell pixel.Vec - counter int + yOffset float64 } -func (mt *movingTriangles) Step(win *opengl.Window) { +func (mt *movingTriangles) Step(win *opengl.Window, delta float64) { win.Clear(backgroundColor) + mt.yOffset += mt.cell.Y * delta * 3 + if mt.yOffset >= mt.cell.Y { + mt.yOffset = 0 + } + for i := 0; i < mt.cols; i++ { - yOffset := -mt.cell.Y - delta := float64(mt.counter % int(mt.cell.Y)) + columnOffset := -mt.cell.Y if i%2 == 0 { - yOffset += delta + columnOffset += mt.yOffset } else { - yOffset -= delta + columnOffset -= mt.yOffset } for j := 0; j < mt.rows+2; j++ { - pos := pixel.V(float64(i)*mt.cell.X, (float64(j)*mt.cell.Y)+yOffset) + pos := pixel.V(float64(i)*mt.cell.X, (float64(j)*mt.cell.Y)+columnOffset) matrix := pixel.IM.Moved(pos) if i%2 == 1 { matrix = matrix.Rotated(pos.Add(pixel.V(mt.cell.X/2, mt.cell.Y/2)), math.Pi) @@ -107,8 +111,6 @@ func (mt *movingTriangles) Step(win *opengl.Window) { mt.imd.Draw(win) } } - - mt.counter++ } func tri(cell pixel.Vec) *imdraw.IMDraw { diff --git a/tools/benchmark/sprite_bench.go b/tools/benchmark/sprite_bench.go index bcc3819..14eccfc 100644 --- a/tools/benchmark/sprite_bench.go +++ b/tools/benchmark/sprite_bench.go @@ -89,7 +89,7 @@ type spriteStatic struct { batch *pixel.Batch } -func (ss *spriteStatic) Step(win *opengl.Window) { +func (ss *spriteStatic) Step(win *opengl.Window, delta float64) { win.Clear(backgroundColor) var target pixel.Target if ss.batch != nil { @@ -134,13 +134,13 @@ func newSpriteMovingBatched(win *opengl.Window) (Benchmark, error) { type spriteMoving struct { sprite *pixel.Sprite + batch *pixel.Batch rows, cols int cell pixel.Vec - counter int - batch *pixel.Batch + yOffset float64 } -func (sm *spriteMoving) Step(win *opengl.Window) { +func (sm *spriteMoving) Step(win *opengl.Window, delta float64) { win.Clear(backgroundColor) var target pixel.Target if sm.batch != nil { @@ -149,11 +149,16 @@ func (sm *spriteMoving) Step(win *opengl.Window) { } else { target = win } - spriteGridMoving(sm.sprite, target, sm.rows, sm.cols, sm.cell, sm.counter) + + sm.yOffset += sm.cell.Y * delta * 3 + if sm.yOffset >= sm.cell.Y { + sm.yOffset = 0 + } + + spriteGridMoving(sm.sprite, target, sm.rows, sm.cols, sm.cell, sm.yOffset) if sm.batch != nil { sm.batch.Draw(win) } - sm.counter += 1 } func spriteGrid(sprite *pixel.Sprite, target pixel.Target, rows, cols int, cell pixel.Vec) { @@ -170,23 +175,22 @@ func spriteGrid(sprite *pixel.Sprite, target pixel.Target, rows, cols int, cell } } -func spriteGridMoving(sprite *pixel.Sprite, target pixel.Target, rows, cols int, cell pixel.Vec, counter int) { +func spriteGridMoving(sprite *pixel.Sprite, target pixel.Target, rows, cols int, cell pixel.Vec, yOffset float64) { spriteBounds := sprite.Frame().Bounds() spriteWidth := spriteBounds.W() spriteHeight := spriteBounds.H() matrix := pixel.IM.ScaledXY(pixel.ZV, pixel.V(cell.X/spriteWidth, cell.Y/spriteHeight)) offset := pixel.V(cell.X/2, cell.Y/2) for i := 0; i < cols; i++ { - yOffset := -cell.Y - delta := float64(counter % int(cell.Y)) + columnOffset := -cell.Y if i%2 == 0 { - yOffset += delta + columnOffset += yOffset } else { - yOffset -= delta + columnOffset -= yOffset } for j := 0; j < rows+2; j++ { - pos := pixel.V(float64(i)*cell.X, (float64(j)*cell.Y)+yOffset).Add(offset) + pos := pixel.V(float64(i)*cell.X, (float64(j)*cell.Y)+columnOffset).Add(offset) sprite.Draw(target, matrix.Moved(pos)) } }