From c6c3e020a4a971a65422643285677dbd3c493dd8 Mon Sep 17 00:00:00 2001
From: faiface <faiface@ksp.sk>
Date: Thu, 24 Nov 2016 14:10:33 +0100
Subject: [PATCH] implement non-blocking call queue calls

---
 pixelgl/shader.go  |  6 +++---
 pixelgl/texture.go |  6 +++---
 pixelgl/thread.go  |  8 +++++++-
 pixelgl/vertex.go  | 19 +++++++++----------
 4 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/pixelgl/shader.go b/pixelgl/shader.go
index bd03373..1cdc766 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 b2ba8ce..e7bd12a 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 9b9ecc2..6153c20 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 e25fad7..241da40 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)
-- 
GitLab