implement non-blocking call queue calls

This commit is contained in:
faiface 2016-11-24 14:10:33 +01:00
parent d4e242a59b
commit c6c3e020a4
4 changed files with 22 additions and 17 deletions

View File

@ -103,7 +103,7 @@ func NewShader(parent BeginEnder, vertexShader, fragmentShader string) (*Shader,
// Delete deletes a shader program. Don't use a shader after deletion. // Delete deletes a shader program. Don't use a shader after deletion.
func (s *Shader) Delete() { func (s *Shader) Delete() {
Do(func() { DoNoBlock(func() {
gl.DeleteProgram(s.program) gl.DeleteProgram(s.program)
}) })
} }
@ -111,14 +111,14 @@ func (s *Shader) Delete() {
// Begin starts using a shader program. // Begin starts using a shader program.
func (s *Shader) Begin() { func (s *Shader) Begin() {
s.parent.Begin() s.parent.Begin()
Do(func() { DoNoBlock(func() {
gl.UseProgram(s.program) gl.UseProgram(s.program)
}) })
} }
// End stops using a shader program. // End stops using a shader program.
func (s *Shader) End() { func (s *Shader) End() {
Do(func() { DoNoBlock(func() {
gl.UseProgram(0) gl.UseProgram(0)
}) })
s.parent.End() s.parent.End()

View File

@ -45,7 +45,7 @@ func NewTexture(parent BeginEnder, width, height int, pixels []uint8) (*Texture,
// Delete deletes a texture. Don't use a texture after deletion. // Delete deletes a texture. Don't use a texture after deletion.
func (t *Texture) Delete() { func (t *Texture) Delete() {
Do(func() { DoNoBlock(func() {
gl.DeleteTextures(1, &t.tex) gl.DeleteTextures(1, &t.tex)
}) })
} }
@ -53,14 +53,14 @@ func (t *Texture) Delete() {
// Begin binds a texture. // Begin binds a texture.
func (t *Texture) Begin() { func (t *Texture) Begin() {
t.parent.Begin() t.parent.Begin()
Do(func() { DoNoBlock(func() {
gl.BindTexture(gl.TEXTURE_2D, t.tex) gl.BindTexture(gl.TEXTURE_2D, t.tex)
}) })
} }
// End unbinds a texture. // End unbinds a texture.
func (t *Texture) End() { func (t *Texture) End() {
Do(func() { DoNoBlock(func() {
gl.BindTexture(gl.TEXTURE_2D, 0) gl.BindTexture(gl.TEXTURE_2D, 0)
}) })
t.parent.End() t.parent.End()

View File

@ -11,7 +11,7 @@ import (
// execute all OpenGL calls from a single dedicated thread. This file defines functions to make // execute all OpenGL calls from a single dedicated thread. This file defines functions to make
// it possible. // it possible.
var callQueue = make(chan func()) var callQueue = make(chan func(), 32)
func init() { func init() {
go func() { go func() {
@ -34,6 +34,12 @@ func Init() {
} }
} }
// DoNoBlock executes a function inside a dedicated OpenGL thread.
// DoNoBlock does not wait until the function finishes.
func DoNoBlock(f func()) {
callQueue <- f
}
// Do executes a function inside a dedicated OpenGL thread. // Do executes a function inside a dedicated OpenGL thread.
// Do blocks until the function finishes. // Do blocks until the function finishes.
// //

View File

@ -144,7 +144,7 @@ func NewVertexArray(parent BeginEnder, format VertexFormat, mode VertexDrawMode,
// Delete deletes a vertex array and it's associated vertex buffer. Don't use a vertex array after deletion. // Delete deletes a vertex array and it's associated vertex buffer. Don't use a vertex array after deletion.
func (va *VertexArray) Delete() { func (va *VertexArray) Delete() {
Do(func() { DoNoBlock(func() {
gl.DeleteVertexArrays(1, &va.vao) gl.DeleteVertexArrays(1, &va.vao)
gl.DeleteBuffers(1, &va.vbo) gl.DeleteBuffers(1, &va.vbo)
}) })
@ -159,7 +159,7 @@ func (va *VertexArray) VertexFormat() VertexFormat {
// SetDrawMode sets the draw mode of a vertex array. Subsequent calls to Draw will use this draw mode. // SetDrawMode sets the draw mode of a vertex array. Subsequent calls to Draw will use this draw mode.
func (va *VertexArray) SetDrawMode(mode VertexDrawMode) { func (va *VertexArray) SetDrawMode(mode VertexDrawMode) {
Do(func() { DoNoBlock(func() {
va.mode = mode va.mode = mode
}) })
} }
@ -181,22 +181,21 @@ func (va *VertexArray) Draw() {
// UpdateData overwrites the current vertex array data starting at the index offset. // UpdateData overwrites the current vertex array data starting at the index offset.
// //
// Offset is not a number of bytes, instead, it's an index in the array. // Offset is not a number of bytes, instead, it's an index in the array.
func (va *VertexArray) UpdateData(offset int, data []float64) error { func (va *VertexArray) UpdateData(offset int, data []float64) {
err := DoGLErr(func() { DoNoBlock(func() {
gl.BindBuffer(gl.ARRAY_BUFFER, va.vbo) gl.BindBuffer(gl.ARRAY_BUFFER, va.vbo)
gl.BufferSubData(gl.ARRAY_BUFFER, 8*offset, 8*len(data), gl.Ptr(data)) gl.BufferSubData(gl.ARRAY_BUFFER, 8*offset, 8*len(data), gl.Ptr(data))
gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BindBuffer(gl.ARRAY_BUFFER, 0)
if err := getLastGLErr(); err != nil {
panic(errors.Wrap(err, "failed to update vertex array"))
}
}) })
if err != nil {
return errors.Wrap(err, "failed to update vertex array")
}
return nil
} }
// Begin binds a vertex array and it's associated vertex buffer. // Begin binds a vertex array and it's associated vertex buffer.
func (va *VertexArray) Begin() { func (va *VertexArray) Begin() {
va.parent.Begin() va.parent.Begin()
Do(func() { DoNoBlock(func() {
gl.BindVertexArray(va.vao) gl.BindVertexArray(va.vao)
gl.BindBuffer(gl.ARRAY_BUFFER, va.vbo) gl.BindBuffer(gl.ARRAY_BUFFER, va.vbo)
}) })
@ -204,7 +203,7 @@ func (va *VertexArray) Begin() {
// End draws a vertex array and unbinds it alongside with it's associated vertex buffer. // End draws a vertex array and unbinds it alongside with it's associated vertex buffer.
func (va *VertexArray) End() { func (va *VertexArray) End() {
Do(func() { DoNoBlock(func() {
gl.DrawArrays(uint32(va.mode), 0, int32(va.count)) gl.DrawArrays(uint32(va.mode), 0, int32(va.count))
gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BindBuffer(gl.ARRAY_BUFFER, 0)
gl.BindVertexArray(0) gl.BindVertexArray(0)