github.com/cybriq/giocore@v0.0.7-0.20210703034601-cfb9cb5f3900/gpu/internal/rendertest/bench_test.go_ (about)

     1  package rendertest
     2  
     3  import (
     4  	"image"
     5  	"image/color"
     6  	"math"
     7  	"testing"
     8  
     9  	"github.com/cybriq/giocore/f32"
    10  	"github.com/cybriq/giocore/font/gofont"
    11  	"github.com/cybriq/giocore/gpu/headless"
    12  	"github.com/cybriq/giocore/layout"
    13  	"github.com/cybriq/giocore/op"
    14  	"github.com/cybriq/giocore/op/clip"
    15  	"github.com/cybriq/giocore/op/paint"
    16  	"github.com/cybriq/giocore/widget/material"
    17  )
    18  
    19  // use some global variables for benchmarking so as to not pollute
    20  // the reported allocs with allocations that we do not want to count.
    21  var (
    22  	c1, c2, c3    = make(chan op.CallOp), make(chan op.CallOp), make(chan op.CallOp)
    23  	op1, op2, op3 op.Ops
    24  )
    25  
    26  func setupBenchmark(b *testing.B) (layout.Context, *headless.Window, *material.Theme) {
    27  	sz := image.Point{X: 1024, Y: 1200}
    28  	w := newWindow(b, sz.X, sz.Y)
    29  	ops := new(op.Ops)
    30  	gtx := layout.Context{
    31  		Ops:         ops,
    32  		Constraints: layout.Exact(sz),
    33  	}
    34  	th := material.NewTheme(gofont.Collection())
    35  	return gtx, w, th
    36  }
    37  
    38  func resetOps(gtx layout.Context) {
    39  	gtx.Ops.Reset()
    40  	op1.Reset()
    41  	op2.Reset()
    42  	op3.Reset()
    43  }
    44  
    45  func finishBenchmark(b *testing.B, w *headless.Window) {
    46  	b.StopTimer()
    47  	if *dumpImages {
    48  		img, err := w.Screenshot()
    49  		w.Release()
    50  		if err != nil {
    51  			b.Error(err)
    52  		}
    53  		if err := saveImage(b.Name()+".png", img); err != nil {
    54  			b.Error(err)
    55  		}
    56  	}
    57  }
    58  
    59  func BenchmarkDrawUICached(b *testing.B) {
    60  	// As BenchmarkDraw but the same op.Ops every time that is not reset - this
    61  	// should thus allow for maximal cache usage.
    62  	gtx, w, th := setupBenchmark(b)
    63  	drawCore(gtx, th)
    64  	w.Frame(gtx.Ops)
    65  	b.ResetTimer()
    66  	for i := 0; i < b.N; i++ {
    67  		w.Frame(gtx.Ops)
    68  	}
    69  	finishBenchmark(b, w)
    70  }
    71  
    72  func BenchmarkDrawUI(b *testing.B) {
    73  	// BenchmarkDraw is intended as a reasonable overall benchmark for
    74  	// the drawing performance of the full drawing pipeline, in each iteration
    75  	// resetting the ops and drawing, similar to how a typical UI would function.
    76  	// This will allow font caching across frames.
    77  	gtx, w, th := setupBenchmark(b)
    78  	drawCore(gtx, th)
    79  	w.Frame(gtx.Ops)
    80  	b.ReportAllocs()
    81  	b.ResetTimer()
    82  	for i := 0; i < b.N; i++ {
    83  		resetOps(gtx)
    84  
    85  		p := op.Save(gtx.Ops)
    86  		off := float32(math.Mod(float64(i)/10, 10))
    87  		op.Offset(f32.Pt(off, off)).Add(gtx.Ops)
    88  
    89  		drawCore(gtx, th)
    90  
    91  		p.Load()
    92  		w.Frame(gtx.Ops)
    93  	}
    94  	finishBenchmark(b, w)
    95  }
    96  
    97  func BenchmarkDrawUITransformed(b *testing.B) {
    98  	// Like BenchmarkDraw UI but transformed at every frame
    99  	gtx, w, th := setupBenchmark(b)
   100  	drawCore(gtx, th)
   101  	w.Frame(gtx.Ops)
   102  	b.ReportAllocs()
   103  	b.ResetTimer()
   104  	for i := 0; i < b.N; i++ {
   105  		resetOps(gtx)
   106  
   107  		p := op.Save(gtx.Ops)
   108  		angle := float32(math.Mod(float64(i)/1000, 0.05))
   109  		a := f32.Affine2D{}.Shear(f32.Point{}, angle, angle).Rotate(f32.Point{}, angle)
   110  		op.Affine(a).Add(gtx.Ops)
   111  
   112  		drawCore(gtx, th)
   113  
   114  		p.Load()
   115  		w.Frame(gtx.Ops)
   116  	}
   117  	finishBenchmark(b, w)
   118  }
   119  
   120  func Benchmark1000Circles(b *testing.B) {
   121  	// Benchmark1000Shapes draws 1000 individual shapes such that no caching between
   122  	// shapes will be possible and resets buffers on each operation to prevent caching
   123  	// between frames.
   124  	gtx, w, _ := setupBenchmark(b)
   125  	draw1000Circles(gtx)
   126  	w.Frame(gtx.Ops)
   127  	b.ReportAllocs()
   128  	b.ResetTimer()
   129  	for i := 0; i < b.N; i++ {
   130  		resetOps(gtx)
   131  		draw1000Circles(gtx)
   132  		w.Frame(gtx.Ops)
   133  	}
   134  	finishBenchmark(b, w)
   135  }
   136  
   137  func Benchmark1000CirclesInstanced(b *testing.B) {
   138  	// Like Benchmark1000Circles but will record them and thus allow for caching between
   139  	// them.
   140  	gtx, w, _ := setupBenchmark(b)
   141  	draw1000CirclesInstanced(gtx)
   142  	w.Frame(gtx.Ops)
   143  	b.ReportAllocs()
   144  	b.ResetTimer()
   145  	for i := 0; i < b.N; i++ {
   146  		resetOps(gtx)
   147  		draw1000CirclesInstanced(gtx)
   148  		w.Frame(gtx.Ops)
   149  	}
   150  	finishBenchmark(b, w)
   151  }
   152  
   153  func draw1000Circles(gtx layout.Context) {
   154  	ops := gtx.Ops
   155  	for x := 0; x < 100; x++ {
   156  		p := op.Save(ops)
   157  		op.Offset(f32.Pt(float32(x*10), 0)).Add(ops)
   158  		for y := 0; y < 10; y++ {
   159  			paint.FillShape(ops,
   160  				color.NRGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120},
   161  				clip.RRect{Rect: f32.Rect(0, 0, 10, 10), NE: 5, SE: 5, SW: 5, NW: 5}.Op(ops),
   162  			)
   163  			op.Offset(f32.Pt(0, float32(100))).Add(ops)
   164  		}
   165  		p.Load()
   166  	}
   167  }
   168  
   169  func draw1000CirclesInstanced(gtx layout.Context) {
   170  	ops := gtx.Ops
   171  
   172  	r := op.Record(ops)
   173  	clip.RRect{Rect: f32.Rect(0, 0, 10, 10), NE: 5, SE: 5, SW: 5, NW: 5}.Add(ops)
   174  	paint.PaintOp{}.Add(ops)
   175  	c := r.Stop()
   176  
   177  	for x := 0; x < 100; x++ {
   178  		p := op.Save(ops)
   179  		op.Offset(f32.Pt(float32(x*10), 0)).Add(ops)
   180  		for y := 0; y < 10; y++ {
   181  			pi := op.Save(ops)
   182  			paint.ColorOp{Color: color.NRGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120}}.Add(ops)
   183  			c.Add(ops)
   184  			pi.Load()
   185  			op.Offset(f32.Pt(0, float32(100))).Add(ops)
   186  		}
   187  		p.Load()
   188  	}
   189  }
   190  
   191  func drawCore(gtx layout.Context, th *material.Theme) {
   192  	c1 := drawIndividualShapes(gtx, th)
   193  	c2 := drawShapeInstances(gtx, th)
   194  	c3 := drawText(gtx, th)
   195  
   196  	(<-c1).Add(gtx.Ops)
   197  	(<-c2).Add(gtx.Ops)
   198  	(<-c3).Add(gtx.Ops)
   199  }
   200  
   201  func drawIndividualShapes(gtx layout.Context, th *material.Theme) chan op.CallOp {
   202  	// draw 81 rounded rectangles of different solid colors - each one individually
   203  	go func() {
   204  		ops := &op1
   205  		c := op.Record(ops)
   206  		for x := 0; x < 9; x++ {
   207  			p := op.Save(ops)
   208  			op.Offset(f32.Pt(float32(x*50), 0)).Add(ops)
   209  			for y := 0; y < 9; y++ {
   210  				paint.FillShape(ops,
   211  					color.NRGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120},
   212  					clip.RRect{Rect: f32.Rect(0, 0, 25, 25), NE: 10, SE: 10, SW: 10, NW: 10}.Op(ops),
   213  				)
   214  				op.Offset(f32.Pt(0, float32(50))).Add(ops)
   215  			}
   216  			p.Load()
   217  		}
   218  		c1 <- c.Stop()
   219  	}()
   220  	return c1
   221  }
   222  
   223  func drawShapeInstances(gtx layout.Context, th *material.Theme) chan op.CallOp {
   224  	// draw 400 textured circle instances, each with individual transform
   225  	go func() {
   226  		ops := &op2
   227  		co := op.Record(ops)
   228  
   229  		r := op.Record(ops)
   230  		clip.RRect{Rect: f32.Rect(0, 0, 25, 25), NE: 10, SE: 10, SW: 10, NW: 10}.Add(ops)
   231  		paint.PaintOp{}.Add(ops)
   232  		c := r.Stop()
   233  
   234  		squares.Add(ops)
   235  		rad := float32(0)
   236  		for x := 0; x < 20; x++ {
   237  			for y := 0; y < 20; y++ {
   238  				p := op.Save(ops)
   239  				op.Offset(f32.Pt(float32(x*50+25), float32(y*50+25))).Add(ops)
   240  				c.Add(ops)
   241  				p.Load()
   242  				rad += math.Pi * 2 / 400
   243  			}
   244  		}
   245  		c2 <- co.Stop()
   246  	}()
   247  	return c2
   248  }
   249  
   250  func drawText(gtx layout.Context, th *material.Theme) chan op.CallOp {
   251  	// draw 40 lines of text with different transforms.
   252  	go func() {
   253  		ops := &op3
   254  		c := op.Record(ops)
   255  
   256  		txt := material.H6(th, "")
   257  		for x := 0; x < 40; x++ {
   258  			txt.Text = textRows[x]
   259  			p := op.Save(ops)
   260  			op.Offset(f32.Pt(float32(0), float32(24*x))).Add(ops)
   261  			gtx.Ops = ops
   262  			txt.Layout(gtx)
   263  			p.Load()
   264  		}
   265  		c3 <- c.Stop()
   266  	}()
   267  	return c3
   268  }
   269  
   270  var textRows = []string{
   271  	"1. I learned from my grandfather, Verus, to use good manners, and to",
   272  	"put restraint on anger. 2. In the famous memory of my father I had a",
   273  	"pattern of modesty and manliness. 3. Of my mother I learned to be",
   274  	"pious and generous; to keep myself not only from evil deeds, but even",
   275  	"from evil thoughts; and to live with a simplicity which is far from",
   276  	"customary among the rich. 4. I owe it to my great-grandfather that I",
   277  	"did not attend public lectures and discussions, but had good and able",
   278  	"teachers at home; and I owe him also the knowledge that for things of",
   279  	"this nature a man should count no expense too great.",
   280  	"5. My tutor taught me not to favour either green or blue at the",
   281  	"chariot races, nor, in the contests of gladiators, to be a supporter",
   282  	"either of light or heavy armed. He taught me also to endure labour;",
   283  	"not to need many things; to serve myself without troubling others; not",
   284  	"to intermeddle in the affairs of others, and not easily to listen to",
   285  	"slanders against them.",
   286  	"6. Of Diognetus I had the lesson not to busy myself about vain things;",
   287  	"not to credit the great professions of such as pretend to work",
   288  	"wonders, or of sorcerers about their charms, and their expelling of",
   289  	"Demons and the like; not to keep quails (for fighting or divination),",
   290  	"nor to run after such things; to suffer freedom of speech in others,",
   291  	"and to apply myself heartily to philosophy. Him also I must thank for",
   292  	"my hearing first Bacchius, then Tandasis and Marcianus; that I wrote",
   293  	"dialogues in my youth, and took a liking to the philosopher's pallet",
   294  	"and skins, and to the other things which, by the Grecian discipline,",
   295  	"belong to that profession.",
   296  	"7. To Rusticus I owe my first apprehensions that my nature needed",
   297  	"reform and cure; and that I did not fall into the ambition of the",
   298  	"common Sophists, either by composing speculative writings or by",
   299  	"declaiming harangues of exhortation in public; further, that I never",
   300  	"strove to be admired by ostentation of great patience in an ascetic",
   301  	"life, or by display of activity and application; that I gave over the",
   302  	"study of rhetoric, poetry, and the graces of language; and that I did",
   303  	"not pace my house in my senatorial robes, or practise any similar",
   304  	"affectation. I observed also the simplicity of style in his letters,",
   305  	"particularly in that which he wrote to my mother from Sinuessa. I",
   306  	"learned from him to be easily appeased, and to be readily reconciled",
   307  	"with those who had displeased me or given cause of offence, so soon as",
   308  	"they inclined to make their peace; to read with care; not to rest",
   309  	"satisfied with a slight and superficial knowledge; nor quickly to",
   310  	"assent to great talkers. I have him to thank that I met with the",
   311  }