make Do reentrant

This commit is contained in:
faiface 2016-12-04 20:28:50 +01:00
parent 1cc26d0c2c
commit e2b361a8cd
4 changed files with 33 additions and 10 deletions

View File

@ -27,6 +27,8 @@ package pixelgl
// Also notice, that the functions are passing a Context around. This context contains the most important state variables. // Also notice, that the functions are passing a Context around. This context contains the most important state variables.
// Usually, you just pass it as you received it. If you want to pass a changed context to your child (e.g. your a shader), // Usually, you just pass it as you received it. If you want to pass a changed context to your child (e.g. your a shader),
// use ctx.With* methods. // use ctx.With* methods.
//
// If possible and makes sense, Do method should be reentrant.
type Doer interface { type Doer interface {
Do(sub func(Context)) Do(sub func(Context))
} }

View File

@ -17,6 +17,7 @@ type UniformFormat map[string]Attr
// Shader is an OpenGL shader program. // Shader is an OpenGL shader program.
type Shader struct { type Shader struct {
enabled bool
parent Doer parent Doer
program uint32 program uint32
vertexFormat VertexFormat vertexFormat VertexFormat
@ -383,6 +384,11 @@ func (s *Shader) SetUniformMat43(purpose AttrPurpose, value mgl32.Mat4x3) (ok bo
// Do stars using a shader, executes sub, and stops using it. // Do stars using a shader, executes sub, and stops using it.
func (s *Shader) Do(sub func(Context)) { func (s *Shader) Do(sub func(Context)) {
s.parent.Do(func(ctx Context) { s.parent.Do(func(ctx Context) {
if s.enabled {
sub(ctx.WithShader(s))
return
}
s.enabled = true
DoNoBlock(func() { DoNoBlock(func() {
gl.UseProgram(s.program) gl.UseProgram(s.program)
}) })
@ -390,5 +396,6 @@ func (s *Shader) Do(sub func(Context)) {
DoNoBlock(func() { DoNoBlock(func() {
gl.UseProgram(0) gl.UseProgram(0)
}) })
s.enabled = false
}) })
} }

View File

@ -7,8 +7,9 @@ import (
// Texture is an OpenGL texture. // Texture is an OpenGL texture.
type Texture struct { type Texture struct {
parent Doer enabled bool
tex uint32 parent Doer
tex uint32
} }
// NewTexture creates a new texture with the specified width and height. // NewTexture creates a new texture with the specified width and height.
@ -63,6 +64,11 @@ func (t *Texture) ID() uint32 {
// Do bind a texture, executes sub, and unbinds the texture. // Do bind a texture, executes sub, and unbinds the texture.
func (t *Texture) Do(sub func(Context)) { func (t *Texture) Do(sub func(Context)) {
t.parent.Do(func(ctx Context) { t.parent.Do(func(ctx Context) {
if t.enabled {
sub(ctx)
return
}
t.enabled = true
DoNoBlock(func() { DoNoBlock(func() {
gl.BindTexture(gl.TEXTURE_2D, t.tex) gl.BindTexture(gl.TEXTURE_2D, t.tex)
}) })
@ -70,5 +76,6 @@ func (t *Texture) Do(sub func(Context)) {
DoNoBlock(func() { DoNoBlock(func() {
gl.BindTexture(gl.TEXTURE_2D, 0) gl.BindTexture(gl.TEXTURE_2D, 0)
}) })
t.enabled = false
}) })
} }

View File

@ -69,14 +69,15 @@ const (
// VertexArray is an OpenGL vertex array object that also holds it's own vertex buffer object. // VertexArray is an OpenGL vertex array object that also holds it's own vertex buffer object.
// From the user's points of view, VertexArray is an array of vertices that can be drawn. // From the user's points of view, VertexArray is an array of vertices that can be drawn.
type VertexArray struct { type VertexArray struct {
parent Doer enabled bool
vao uint32 parent Doer
vbo uint32 vao uint32
format VertexFormat vbo uint32
stride int format VertexFormat
count int stride int
attrs map[Attr]int count int
mode VertexDrawMode attrs map[Attr]int
mode VertexDrawMode
} }
// NewVertexArray creates a new empty vertex array and wraps another Doer around it. // NewVertexArray creates a new empty vertex array and wraps another Doer around it.
@ -341,6 +342,11 @@ func (va *VertexArray) SetVertexAttributeVec4(vertex int, purpose AttrPurpose, v
// Do binds a vertex arrray and it's associated vertex buffer, executes sub, and unbinds the vertex array and it's vertex buffer. // Do binds a vertex arrray and it's associated vertex buffer, executes sub, and unbinds the vertex array and it's vertex buffer.
func (va *VertexArray) Do(sub func(Context)) { func (va *VertexArray) Do(sub func(Context)) {
va.parent.Do(func(ctx Context) { va.parent.Do(func(ctx Context) {
if va.enabled {
sub(ctx)
return
}
va.enabled = true
DoNoBlock(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)
@ -351,5 +357,6 @@ func (va *VertexArray) Do(sub func(Context)) {
gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BindBuffer(gl.ARRAY_BUFFER, 0)
gl.BindVertexArray(0) gl.BindVertexArray(0)
}) })
va.enabled = false
}) })
} }