mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
ui: SwapBuffer is now done on the rendering context thread
This commit is contained in:
parent
bb39766873
commit
6e98e0716d
@ -47,12 +47,14 @@ func GetProgramID(p Program) ProgramID {
|
|||||||
return ProgramID(p.Value)
|
return ProgramID(p.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
type context struct{}
|
type context struct {
|
||||||
|
worker mgl.Worker
|
||||||
|
funcs chan func()
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: These variables can be in the context struct.
|
// TODO: This variable can be in the context struct.
|
||||||
var (
|
var (
|
||||||
gl mgl.Context
|
gl mgl.Context
|
||||||
worker mgl.Worker
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewContext() *Context {
|
func NewContext() *Context {
|
||||||
@ -68,14 +70,19 @@ func NewContext() *Context {
|
|||||||
Triangles: mgl.TRIANGLES,
|
Triangles: mgl.TRIANGLES,
|
||||||
Lines: mgl.LINES,
|
Lines: mgl.LINES,
|
||||||
}
|
}
|
||||||
gl, worker = mgl.NewContext()
|
c.funcs = make(chan func())
|
||||||
|
gl, c.worker = mgl.NewContext()
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func Loop() {
|
func (c *Context) Loop() {
|
||||||
for {
|
for {
|
||||||
<-worker.WorkAvailable()
|
select {
|
||||||
worker.DoWork()
|
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)
|
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() {
|
func (c *Context) Check() {
|
||||||
if e := gl.GetError(); e != mgl.NO_ERROR {
|
if e := gl.GetError(); e != mgl.NO_ERROR {
|
||||||
panic(fmt.Sprintf("check failed: %d", e))
|
panic(fmt.Sprintf("check failed: %d", e))
|
||||||
|
@ -57,15 +57,15 @@ func Init() *opengl.Context {
|
|||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
u.window.MakeContextCurrent()
|
u.window.MakeContextCurrent()
|
||||||
glfw.SwapInterval(1)
|
glfw.SwapInterval(1)
|
||||||
|
u.context = opengl.NewContext()
|
||||||
close(ch)
|
close(ch)
|
||||||
opengl.Loop()
|
u.context.Loop()
|
||||||
}()
|
}()
|
||||||
currentUI = u
|
currentUI = u
|
||||||
context := opengl.NewContext()
|
|
||||||
<-ch
|
<-ch
|
||||||
context.Init()
|
u.context.Init()
|
||||||
|
|
||||||
return context
|
return u.context
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(width, height, scale int, title string) (actualScale int, err error) {
|
func Start(width, height, scale int, title string) (actualScale int, err error) {
|
||||||
@ -104,6 +104,7 @@ type userInterface struct {
|
|||||||
height int
|
height int
|
||||||
scale int
|
scale int
|
||||||
actualScale int
|
actualScale int
|
||||||
|
context *opengl.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) start(width, height, scale int, title string) (actualScale int, err error) {
|
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() {
|
func (u *userInterface) swapBuffers() {
|
||||||
|
u.context.RunOnContextThread(func() {
|
||||||
u.window.SwapBuffers()
|
u.window.SwapBuffers()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) setScreenSize(width, height, scale int) bool {
|
func (u *userInterface) setScreenSize(width, height, scale int) bool {
|
||||||
|
Loading…
Reference in New Issue
Block a user