diff --git a/tools/README.md b/tools/README.md index 2039523..b294062 100644 --- a/tools/README.md +++ b/tools/README.md @@ -64,13 +64,17 @@ go run main.go bench run --all | Machine | Pixel | Benchmark | Duration | Frames | FPS Avg | FPS Min | FPS Max | FPS Stdev | |--------------------|--------|------------------------------|----------|--------|---------|---------|---------|-----------| | bhperry-wsl | v2.2.1 | imdraw-moving | 30s | 2214 | 73.79 | 68 | 76 | 1.77 | +| bhperry-wsl | v2.2.1 | imdraw-moving-batched | 30s | 5658 | 188.57 | 166 | 195 | 5.86 | | bhperry-wsl | v2.2.1 | imdraw-static | 30s | 2355 | 78.5 | 72 | 81 | 1.89 | +| bhperry-wsl | v2.2.1 | imdraw-static-batched | 30.01s | 6171 | 205.64 | 168 | 212 | 9.62 | | bhperry-wsl | v2.2.1 | sprite-moving | 30.03s | 1451 | 48.32 | 45 | 50 | 1.25 | | bhperry-wsl | v2.2.1 | sprite-moving-batched | 30.01s | 4085 | 136.12 | 127 | 142 | 3.17 | | bhperry-wsl | v2.2.1 | sprite-static | 30.01s | 1518 | 50.59 | 47 | 52 | 1.45 | | bhperry-wsl | v2.2.1 | sprite-static-batched | 30.01s | 5318 | 177.2 | 159 | 182 | 6.01 | | bhperry-win10 | v2.2.1 | imdraw-moving | 30.03s | 1430 | 47.61 | 22 | 50 | 5.85 | +| bhperry-win10 | v2.2.1 | imdraw-moving-batched | 30s | 52017 | 1733.9 | 1635 | 1915 | 43.92 | | bhperry-win10 | v2.2.1 | imdraw-static | 30.02s | 1569 | 52.27 | 51 | 53 | 0.64 | +| bhperry-win10 | v2.2.1 | imdraw-static-batched | 30.01s | 1517 | 50.55 | 21 | 53 | 6.62 | | bhperry-win10 | v2.2.1 | sprite-moving | 30.03s | 1148 | 38.23 | 35 | 39 | 0.9 | | bhperry-win10 | v2.2.1 | sprite-moving-batched | 30s | 39085 | 1302.79 | 1205 | 1329 | 23.93 | | bhperry-win10 | v2.2.1 | sprite-static | 30.04s | 1218 | 40.54 | 38 | 42 | 0.88 | diff --git a/tools/benchmark/imdraw_bench.go b/tools/benchmark/imdraw_bench.go index 0c7eeea..c2b6387 100644 --- a/tools/benchmark/imdraw_bench.go +++ b/tools/benchmark/imdraw_bench.go @@ -21,12 +21,24 @@ func init() { New: newStaticTriangles, Duration: 30 * time.Second, }, + Config{ + Name: "imdraw-static-batched", + Description: "Stationary RGB triangles in a grid with batched draw", + New: newStaticTrianglesBatched, + Duration: 30 * time.Second, + }, Config{ Name: "imdraw-moving", Description: "Columns of RGB triangles moving in opposite directions", New: newMovingTriangles, Duration: 30 * time.Second, }, + Config{ + Name: "imdraw-moving-batched", + Description: "Columns of RGB triangles moving in opposite directions with batched draw", + New: newMovingTrianglesBatched, + Duration: 30 * time.Second, + }, ) } @@ -45,8 +57,20 @@ func newStaticTriangles(win *opengl.Window) (Benchmark, error) { return benchmark, nil } +func newStaticTrianglesBatched(win *opengl.Window) (Benchmark, error) { + benchmark, err := newStaticTriangles(win) + if err != nil { + return nil, err + } + st := benchmark.(*staticTriangles) + st.target = pixel.NewBatch(&pixel.TrianglesData{}, nil) + return st, nil +} + type staticTriangles struct { imd *imdraw.IMDraw + batch *pixel.Batch + target pixel.BasicTarget rows, cols int cell pixel.Vec } @@ -54,13 +78,25 @@ type staticTriangles struct { func (st *staticTriangles) Step(win *opengl.Window, delta float64) { win.Clear(backgroundColor) + var target pixel.BasicTarget + if st.batch != nil { + st.batch.Clear() + target = st.batch + } else { + target = win + } + for i := 0; i < st.cols; i++ { for j := 0; j < st.rows; j++ { pos := pixel.V(float64(i)*st.cell.X, float64(j)*st.cell.Y) - win.SetMatrix(pixel.IM.Moved(pos)) - st.imd.Draw(win) + target.SetMatrix(pixel.IM.Moved(pos)) + st.imd.Draw(target) } } + + if st.batch != nil { + st.batch.Draw(win) + } } func newMovingTriangles(win *opengl.Window) (Benchmark, error) { @@ -78,8 +114,20 @@ func newMovingTriangles(win *opengl.Window) (Benchmark, error) { return benchmark, nil } +func newMovingTrianglesBatched(win *opengl.Window) (Benchmark, error) { + benchmark, err := newMovingTriangles(win) + if err != nil { + return nil, err + } + + mt := benchmark.(*movingTriangles) + mt.batch = pixel.NewBatch(&pixel.TrianglesData{}, nil) + return mt, nil +} + type movingTriangles struct { imd *imdraw.IMDraw + batch *pixel.Batch rows, cols int cell pixel.Vec yOffset float64 @@ -88,6 +136,14 @@ type movingTriangles struct { func (mt *movingTriangles) Step(win *opengl.Window, delta float64) { win.Clear(backgroundColor) + var target pixel.BasicTarget + if mt.batch != nil { + mt.batch.Clear() + target = mt.batch + } else { + target = win + } + mt.yOffset += mt.cell.Y * delta * 3 if mt.yOffset >= mt.cell.Y { mt.yOffset = 0 @@ -107,10 +163,14 @@ func (mt *movingTriangles) Step(win *opengl.Window, delta float64) { if i%2 == 1 { matrix = matrix.Rotated(pos.Add(pixel.V(mt.cell.X/2, mt.cell.Y/2)), math.Pi) } - win.SetMatrix(matrix) - mt.imd.Draw(win) + target.SetMatrix(matrix) + mt.imd.Draw(target) } } + + if mt.batch != nil { + mt.batch.Draw(win) + } } func tri(cell pixel.Vec) *imdraw.IMDraw {