Skip to content

Commit

Permalink
Pass frame delta for fixed movement rate in benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
bhperry committed Aug 23, 2024
1 parent 922dca5 commit 9a7f813
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
12 changes: 6 additions & 6 deletions tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
7 changes: 5 additions & 2 deletions tools/benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
22 changes: 12 additions & 10 deletions tools/benchmark/imdraw_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down Expand Up @@ -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)
Expand All @@ -107,8 +111,6 @@ func (mt *movingTriangles) Step(win *opengl.Window) {
mt.imd.Draw(win)
}
}

mt.counter++
}

func tri(cell pixel.Vec) *imdraw.IMDraw {
Expand Down
28 changes: 16 additions & 12 deletions tools/benchmark/sprite_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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))
}
}
Expand Down

0 comments on commit 9a7f813

Please sign in to comment.