diff --git a/pixelgl/shader.go b/pixelgl/shader.go index bd033738cb360c4c9e371f99694275529ce2a2d6..1cdc7667b87bbb735113e88199a74bd004416753 100644 --- a/pixelgl/shader.go +++ b/pixelgl/shader.go @@ -103,7 +103,7 @@ func NewShader(parent BeginEnder, vertexShader, fragmentShader string) (*Shader, // Delete deletes a shader program. Don't use a shader after deletion. func (s *Shader) Delete() { - Do(func() { + DoNoBlock(func() { gl.DeleteProgram(s.program) }) } @@ -111,14 +111,14 @@ func (s *Shader) Delete() { // Begin starts using a shader program. func (s *Shader) Begin() { s.parent.Begin() - Do(func() { + DoNoBlock(func() { gl.UseProgram(s.program) }) } // End stops using a shader program. func (s *Shader) End() { - Do(func() { + DoNoBlock(func() { gl.UseProgram(0) }) s.parent.End() diff --git a/pixelgl/texture.go b/pixelgl/texture.go index b2ba8ce09f6375922d4694c4ca3f4f99819a790e..e7bd12a764220ac8cee0813ea22cd1b87d0371ac 100644 --- a/pixelgl/texture.go +++ b/pixelgl/texture.go @@ -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. func (t *Texture) Delete() { - Do(func() { + DoNoBlock(func() { gl.DeleteTextures(1, &t.tex) }) } @@ -53,14 +53,14 @@ func (t *Texture) Delete() { // Begin binds a texture. func (t *Texture) Begin() { t.parent.Begin() - Do(func() { + DoNoBlock(func() { gl.BindTexture(gl.TEXTURE_2D, t.tex) }) } // End unbinds a texture. func (t *Texture) End() { - Do(func() { + DoNoBlock(func() { gl.BindTexture(gl.TEXTURE_2D, 0) }) t.parent.End() diff --git a/pixelgl/thread.go b/pixelgl/thread.go index 9b9ecc254868985c52fa5153b31ff17af37cbcdb..6153c20d80a189436065785882d5630e1d1f8112 100644 --- a/pixelgl/thread.go +++ b/pixelgl/thread.go @@ -11,7 +11,7 @@ import ( // execute all OpenGL calls from a single dedicated thread. This file defines functions to make // it possible. -var callQueue = make(chan func()) +var callQueue = make(chan func(), 32) func init() { 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 blocks until the function finishes. // diff --git a/pixelgl/vertex.go b/pixelgl/vertex.go index e25fad74e01b7048a9992b7a23c2ec5bf46302fd..241da40917ed8c27f76e8d8dae5708d2c1efb2f7 100644 --- a/pixelgl/vertex.go +++ b/pixelgl/vertex.go @@ -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. func (va *VertexArray) Delete() { - Do(func() { + DoNoBlock(func() { gl.DeleteVertexArrays(1, &va.vao) 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. func (va *VertexArray) SetDrawMode(mode VertexDrawMode) { - Do(func() { + DoNoBlock(func() { va.mode = mode }) } @@ -181,22 +181,21 @@ func (va *VertexArray) Draw() { // 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. -func (va *VertexArray) UpdateData(offset int, data []float64) error { - err := DoGLErr(func() { +func (va *VertexArray) UpdateData(offset int, data []float64) { + DoNoBlock(func() { gl.BindBuffer(gl.ARRAY_BUFFER, va.vbo) gl.BufferSubData(gl.ARRAY_BUFFER, 8*offset, 8*len(data), gl.Ptr(data)) 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. func (va *VertexArray) Begin() { va.parent.Begin() - Do(func() { + DoNoBlock(func() { gl.BindVertexArray(va.vao) 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. func (va *VertexArray) End() { - Do(func() { + DoNoBlock(func() { gl.DrawArrays(uint32(va.mode), 0, int32(va.count)) gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BindVertexArray(0)