Skip to content

Commit

Permalink
content: add bench render example
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Jan 23, 2021
1 parent 8f8749f commit c9ff257
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
8 changes: 8 additions & 0 deletions content/assets/bench-render/image/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module x

go 1.16

require (
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20201108214237-06ea97f0c265
)
4 changes: 4 additions & 0 deletions content/assets/bench-render/image/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw=
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20201108214237-06ea97f0c265 h1:BcbKYUZo/TKPsiSh7LymK3p+TNAJJW3OfGO/21sBbiA=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20201108214237-06ea97f0c265/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
125 changes: 125 additions & 0 deletions content/assets/bench-render/image/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright (c) 2021 The golang.design Initiative Authors.
// All rights reserved.
//
// The code below is produced by Changkun Ou <[email protected]>.

package main

import (
"flag"
"fmt"
"image"
"image/draw"
_ "image/jpeg"
_ "image/png"
"os"
"runtime"
"runtime/trace"
"time"
"unsafe"

"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)

func init() {
runtime.LockOSThread()
}

func main() {
run := flag.Bool("run", false, "start test")
traceF := flag.String("trace", "trace.out", "trace file, default: trace.out")
traceT := flag.String("d", "10s", "trace duration, default: 10s")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `usage: go run main.go -run [-trace FILENAME -d DURATION]
options:
`)
flag.PrintDefaults()
}
flag.Parse()
if !*run {
flag.Usage()
os.Exit(2)
}

d, err := time.ParseDuration(*traceT)
if err != nil {
flag.Usage()
os.Exit(2)
}

err = glfw.Init()
if err != nil {
panic(err)
}

w, err := glfw.CreateWindow(800, 600, "image", nil, nil)
if err != nil {
panic(err)
}
glfw.WindowHint(glfw.DoubleBuffer, glfw.False)
w.MakeContextCurrent()

err = gl.Init()
if err != nil {
panic(err)
}

f, err := os.Open("../ou.png")
if err != nil {
panic(err)
}
img, _, err := image.Decode(f)
if err != nil {
panic(err)
}

go func() {
f, _ := os.Create(*traceF)
defer f.Close()
trace.Start(f)
defer trace.Stop()
time.Sleep(d)
w.SetShouldClose(true)
}()

total := time.Duration(0)
count := 0
for !w.ShouldClose() {
t := time.Now()
flush(w, img, img.Bounds())
total += time.Now().Sub(t)
count++

glfw.WaitEventsTimeout(1.0 / 30)
}
timePerFlush := total / time.Duration(count)
fmt.Printf("%v, fps: %v\n", timePerFlush, int(time.Second/timePerFlush))
}

func flush(w *glfw.Window, img image.Image, canvas image.Rectangle) {
bounds := img.Bounds()
canvas = canvas.Intersect(bounds)
if canvas.Empty() {
return
}

tmp := image.NewRGBA(canvas)
draw.Draw(tmp, canvas, img, canvas.Min, draw.Src)
gl.DrawBuffer(gl.FRONT)
gl.Viewport(
int32(bounds.Min.X), int32(bounds.Min.Y),
int32(bounds.Dx()), int32(bounds.Dy()),
)
gl.RasterPos2d(
-1+2*float64(canvas.Min.X)/float64(bounds.Dx()),
+1-2*float64(canvas.Min.Y)/float64(bounds.Dy()),
)
gl.PixelZoom(1, -1)
gl.DrawPixels(
int32(canvas.Dx()), int32(canvas.Dy()),
gl.RGBA, gl.UNSIGNED_BYTE,
unsafe.Pointer(&tmp.Pix[0]),
)
gl.Flush()
}
Binary file added content/assets/bench-render/ou.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c9ff257

Please sign in to comment.