From 6e98e0716d75b707f1079b00aa5a1bb3150c959e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 21 Feb 2016 22:20:33 +0900 Subject: [PATCH] ui: SwapBuffer is now done on the rendering context thread --- internal/graphics/opengl/context.go | 35 +++++++++++++++++++++-------- internal/ui/ui_glfw.go | 13 ++++++----- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/internal/graphics/opengl/context.go b/internal/graphics/opengl/context.go index 8536a2d03..a3f7777f1 100644 --- a/internal/graphics/opengl/context.go +++ b/internal/graphics/opengl/context.go @@ -47,12 +47,14 @@ func GetProgramID(p Program) ProgramID { return ProgramID(p.Value) } -type context struct{} - -// TODO: These variables can be in the context struct. -var ( - gl mgl.Context +type context struct { worker mgl.Worker + funcs chan func() +} + +// TODO: This variable can be in the context struct. +var ( + gl mgl.Context ) func NewContext() *Context { @@ -68,14 +70,19 @@ func NewContext() *Context { Triangles: mgl.TRIANGLES, Lines: mgl.LINES, } - gl, worker = mgl.NewContext() + c.funcs = make(chan func()) + gl, c.worker = mgl.NewContext() return c } -func Loop() { +func (c *Context) Loop() { for { - <-worker.WorkAvailable() - worker.DoWork() + select { + case <-c.worker.WorkAvailable(): + c.worker.DoWork() + case f := <-c.funcs: + f() + } } } @@ -88,6 +95,16 @@ func (c *Context) Init() { gl.BlendFunc(mgl.ONE, mgl.ONE_MINUS_SRC_ALPHA) } +func (c *Context) RunOnContextThread(f func()) { + ch := make(chan struct{}) + c.funcs <- func() { + f() + close(ch) + } + <-ch + return +} + func (c *Context) Check() { if e := gl.GetError(); e != mgl.NO_ERROR { panic(fmt.Sprintf("check failed: %d", e)) diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 5074bc181..32a226dad 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -57,15 +57,15 @@ func Init() *opengl.Context { runtime.LockOSThread() u.window.MakeContextCurrent() glfw.SwapInterval(1) + u.context = opengl.NewContext() close(ch) - opengl.Loop() + u.context.Loop() }() currentUI = u - context := opengl.NewContext() <-ch - context.Init() + u.context.Init() - return context + return u.context } func Start(width, height, scale int, title string) (actualScale int, err error) { @@ -104,6 +104,7 @@ type userInterface struct { height int scale int actualScale int + context *opengl.Context } func (u *userInterface) start(width, height, scale int, title string) (actualScale int, err error) { @@ -146,7 +147,9 @@ func (u *userInterface) isClosed() bool { } func (u *userInterface) swapBuffers() { - u.window.SwapBuffers() + u.context.RunOnContextThread(func() { + u.window.SwapBuffers() + }) } func (u *userInterface) setScreenSize(width, height, scale int) bool {