ui: SwapBuffer is now done on the rendering context thread

This commit is contained in:
Hajime Hoshi 2016-02-21 22:20:33 +09:00
parent bb39766873
commit 6e98e0716d
2 changed files with 34 additions and 14 deletions

View File

@ -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))

View File

@ -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 {