opengl: Bug fixed: there were some errors shadowed and not used

This commit is contained in:
Hajime Hoshi 2016-05-14 03:24:01 +09:00
parent 88fc32c070
commit a04ce352f8
2 changed files with 105 additions and 87 deletions

View File

@ -81,30 +81,29 @@ func (c *Context) Loop() {
} }
} }
func (c *Context) RunOnContextThread(f func()) { func (c *Context) RunOnContextThread(f func() error) error {
ch := make(chan struct{}) ch := make(chan struct{})
var err error
c.funcs <- func() { c.funcs <- func() {
f() err = f()
close(ch) close(ch)
} }
<-ch <-ch
return return err
} }
func (c *Context) Init() error { func (c *Context) Init() error {
var err error if err := c.RunOnContextThread(func() error {
c.RunOnContextThread(func() {
// This initialization must be done after Loop is called. // This initialization must be done after Loop is called.
// This is why Init is separated from NewContext. // This is why Init is separated from NewContext.
if err := gl.Init(); err != nil { if err := gl.Init(); err != nil {
err = fmt.Errorf("opengl: initializing error %v", err) return fmt.Errorf("opengl: initializing error %v", err)
return
} }
// Textures' pixel formats are alpha premultiplied. // Textures' pixel formats are alpha premultiplied.
gl.Enable(gl.BLEND) gl.Enable(gl.BLEND)
}) return nil
if err != nil { }); err != nil {
return err return err
} }
c.BlendFunc(CompositeModeSourceOver) c.BlendFunc(CompositeModeSourceOver)
@ -112,24 +111,25 @@ func (c *Context) Init() error {
} }
func (c *Context) BlendFunc(mode CompositeMode) { func (c *Context) BlendFunc(mode CompositeMode) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
if c.lastCompositeMode == mode { if c.lastCompositeMode == mode {
return return nil
} }
c.lastCompositeMode = mode c.lastCompositeMode = mode
s, d := c.operations(mode) s, d := c.operations(mode)
gl.BlendFunc(uint32(s), uint32(d)) gl.BlendFunc(uint32(s), uint32(d))
return nil
}) })
} }
func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (texture Texture, err error) { func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (Texture, error) {
c.RunOnContextThread(func() { var texture Texture
if err := c.RunOnContextThread(func() error {
var t uint32 var t uint32
gl.GenTextures(1, &t) gl.GenTextures(1, &t)
// TOOD: Use gl.IsTexture // TOOD: Use gl.IsTexture
if t <= 0 { if t <= 0 {
err = errors.New("opengl: creating texture failed") return errors.New("opengl: creating texture failed")
return
} }
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
gl.BindTexture(gl.TEXTURE_2D, t) gl.BindTexture(gl.TEXTURE_2D, t)
@ -144,60 +144,68 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p)) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))
texture = Texture(t) texture = Texture(t)
return return nil
}) }); err != nil {
return return 0, err
}
return texture, nil
} }
func (c *Context) FramebufferPixels(f Framebuffer, width, height int) (pixels []uint8, err error) { func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, error) {
c.RunOnContextThread(func() { var pixels []uint8
if err := c.RunOnContextThread(func() error {
gl.Flush() gl.Flush()
gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(f)) gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(f))
pixels = make([]uint8, 4*width*height) pixels = make([]uint8, 4*width*height)
gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels)) gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels))
if e := gl.GetError(); e != gl.NO_ERROR { if e := gl.GetError(); e != gl.NO_ERROR {
pixels = nil pixels = nil
err = fmt.Errorf("opengl: glReadPixels: %d", e) return fmt.Errorf("opengl: glReadPixels: %d", e)
return
} }
return return nil
}) }); err != nil {
return return nil, err
}
return pixels, nil
} }
func (c *Context) BindTexture(t Texture) { func (c *Context) BindTexture(t Texture) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
gl.BindTexture(gl.TEXTURE_2D, uint32(t)) gl.BindTexture(gl.TEXTURE_2D, uint32(t))
return nil
}) })
} }
func (c *Context) DeleteTexture(t Texture) { func (c *Context) DeleteTexture(t Texture) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
tt := uint32(t) tt := uint32(t)
gl.DeleteTextures(1, &tt) gl.DeleteTextures(1, &tt)
return nil
}) })
} }
func (c *Context) TexSubImage2D(p []uint8, width, height int) { func (c *Context) TexSubImage2D(p []uint8, width, height int) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p)) gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))
return nil
}) })
} }
func (c *Context) BindZeroFramebuffer() { func (c *Context) BindZeroFramebuffer() {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(ZeroFramebuffer)) gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(ZeroFramebuffer))
return nil
}) })
} }
func (c *Context) NewFramebuffer(texture Texture) (framebuffer Framebuffer, err error) { func (c *Context) NewFramebuffer(texture Texture) (Framebuffer, error) {
c.RunOnContextThread(func() { var framebuffer Framebuffer
if err := c.RunOnContextThread(func() error {
var f uint32 var f uint32
gl.GenFramebuffers(1, &f) gl.GenFramebuffers(1, &f)
// TODO: Use gl.IsFramebuffer // TODO: Use gl.IsFramebuffer
if f <= 0 { if f <= 0 {
err = errors.New("opengl: creating framebuffer failed: gl.IsFramebuffer returns false") return errors.New("opengl: creating framebuffer failed: gl.IsFramebuffer returns false")
return
} }
gl.BindFramebuffer(gl.FRAMEBUFFER, f) gl.BindFramebuffer(gl.FRAMEBUFFER, f)
@ -205,62 +213,58 @@ func (c *Context) NewFramebuffer(texture Texture) (framebuffer Framebuffer, err
s := gl.CheckFramebufferStatus(gl.FRAMEBUFFER) s := gl.CheckFramebufferStatus(gl.FRAMEBUFFER)
if s != gl.FRAMEBUFFER_COMPLETE { if s != gl.FRAMEBUFFER_COMPLETE {
if s != 0 { if s != 0 {
err = fmt.Errorf("opengl: creating framebuffer failed: %v", s) return fmt.Errorf("opengl: creating framebuffer failed: %v", s)
return
} }
if e := gl.GetError(); e != gl.NO_ERROR { if e := gl.GetError(); e != gl.NO_ERROR {
err = fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e) return fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e)
return
} }
err = fmt.Errorf("opengl: creating framebuffer failed: unknown error") return fmt.Errorf("opengl: creating framebuffer failed: unknown error")
return
} }
framebuffer = Framebuffer(f) framebuffer = Framebuffer(f)
return return nil
}) }); err != nil {
return return 0, err
}
return framebuffer, nil
} }
func (c *Context) SetViewport(f Framebuffer, width, height int) (err error) { func (c *Context) SetViewport(f Framebuffer, width, height int) error {
c.RunOnContextThread(func() { return c.RunOnContextThread(func() error {
gl.Flush() gl.Flush()
gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(f)) gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(f))
if st := gl.CheckFramebufferStatus(gl.FRAMEBUFFER); st != gl.FRAMEBUFFER_COMPLETE { if st := gl.CheckFramebufferStatus(gl.FRAMEBUFFER); st != gl.FRAMEBUFFER_COMPLETE {
if e := gl.GetError(); e != 0 { if e := gl.GetError(); e != 0 {
err = fmt.Errorf("opengl: glBindFramebuffer failed: %d", e) return fmt.Errorf("opengl: glBindFramebuffer failed: %d", e)
return
} }
err = errors.New("opengl: glBindFramebuffer failed: the context is different?") return errors.New("opengl: glBindFramebuffer failed: the context is different?")
return
} }
gl.Viewport(0, 0, int32(width), int32(height)) gl.Viewport(0, 0, int32(width), int32(height))
return return nil
}) })
return
} }
func (c *Context) FillFramebuffer(r, g, b, a float64) error { func (c *Context) FillFramebuffer(r, g, b, a float64) error {
c.RunOnContextThread(func() { return c.RunOnContextThread(func() error {
gl.ClearColor(float32(r), float32(g), float32(b), float32(a)) gl.ClearColor(float32(r), float32(g), float32(b), float32(a))
gl.Clear(gl.COLOR_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT)
return return nil
}) })
return nil
} }
func (c *Context) DeleteFramebuffer(f Framebuffer) { func (c *Context) DeleteFramebuffer(f Framebuffer) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
ff := uint32(f) ff := uint32(f)
gl.DeleteFramebuffers(1, &ff) gl.DeleteFramebuffers(1, &ff)
return nil
}) })
} }
func (c *Context) NewShader(shaderType ShaderType, source string) (shader Shader, err error) { func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
c.RunOnContextThread(func() { var shader Shader
if err := c.RunOnContextThread(func() error {
s := gl.CreateShader(uint32(shaderType)) s := gl.CreateShader(uint32(shaderType))
if s == 0 { if s == 0 {
err = errors.New("opengl: glCreateShader failed") return errors.New("opengl: glCreateShader failed")
return
} }
cSources, free := gl.Strs(source + "\x00") cSources, free := gl.Strs(source + "\x00")
gl.ShaderSource(uint32(s), 1, cSources, nil) gl.ShaderSource(uint32(s), 1, cSources, nil)
@ -276,18 +280,20 @@ func (c *Context) NewShader(shaderType ShaderType, source string) (shader Shader
log = make([]uint8, int(v)) log = make([]uint8, int(v))
gl.GetShaderInfoLog(uint32(s), v, nil, (*uint8)(gl.Ptr(log))) gl.GetShaderInfoLog(uint32(s), v, nil, (*uint8)(gl.Ptr(log)))
} }
err = fmt.Errorf("opengl: shader compile failed: %s", log) return fmt.Errorf("opengl: shader compile failed: %s", log)
return
} }
shader = Shader(s) shader = Shader(s)
return return nil
}) }); err != nil {
return return 0, err
}
return shader, nil
} }
func (c *Context) DeleteShader(s Shader) { func (c *Context) DeleteShader(s Shader) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
gl.DeleteShader(uint32(s)) gl.DeleteShader(uint32(s))
return nil
}) })
} }
@ -295,12 +301,12 @@ func (c *Context) GlslHighpSupported() bool {
return false return false
} }
func (c *Context) NewProgram(shaders []Shader) (program Program, err error) { func (c *Context) NewProgram(shaders []Shader) (Program, error) {
c.RunOnContextThread(func() { var program Program
if err := c.RunOnContextThread(func() error {
p := gl.CreateProgram() p := gl.CreateProgram()
if p == 0 { if p == 0 {
err = errors.New("opengl: glCreateProgram failed") return errors.New("opengl: glCreateProgram failed")
return
} }
for _, shader := range shaders { for _, shader := range shaders {
@ -310,18 +316,20 @@ func (c *Context) NewProgram(shaders []Shader) (program Program, err error) {
var v int32 var v int32
gl.GetProgramiv(p, gl.LINK_STATUS, &v) gl.GetProgramiv(p, gl.LINK_STATUS, &v)
if v == gl.FALSE { if v == gl.FALSE {
err = errors.New("opengl: program error") return errors.New("opengl: program error")
return
} }
program = Program(p) program = Program(p)
return return nil
}) }); err != nil {
return return 0, err
}
return program, nil
} }
func (c *Context) UseProgram(p Program) { func (c *Context) UseProgram(p Program) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
gl.UseProgram(uint32(p)) gl.UseProgram(uint32(p))
return nil
}) })
} }
@ -334,14 +342,15 @@ func (c *Context) getUniformLocation(p Program, location string) uniformLocation
} }
func (c *Context) UniformInt(p Program, location string, v int) { func (c *Context) UniformInt(p Program, location string, v int) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location)) l := int32(c.locationCache.GetUniformLocation(c, p, location))
gl.Uniform1i(l, int32(v)) gl.Uniform1i(l, int32(v))
return nil
}) })
} }
func (c *Context) UniformFloats(p Program, location string, v []float32) { func (c *Context) UniformFloats(p Program, location string, v []float32) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location)) l := int32(c.locationCache.GetUniformLocation(c, p, location))
switch len(v) { switch len(v) {
case 4: case 4:
@ -351,6 +360,7 @@ func (c *Context) UniformFloats(p Program, location string, v []float32) {
default: default:
panic("not reach") panic("not reach")
} }
return nil
}) })
} }
@ -363,28 +373,32 @@ func (c *Context) getAttribLocation(p Program, location string) attribLocation {
} }
func (c *Context) VertexAttribPointer(p Program, location string, normalize bool, stride int, size int, v int) { func (c *Context) VertexAttribPointer(p Program, location string, normalize bool, stride int, size int, v int) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.VertexAttribPointer(uint32(l), int32(size), gl.SHORT, normalize, int32(stride), gl.PtrOffset(v)) gl.VertexAttribPointer(uint32(l), int32(size), gl.SHORT, normalize, int32(stride), gl.PtrOffset(v))
return nil
}) })
} }
func (c *Context) EnableVertexAttribArray(p Program, location string) { func (c *Context) EnableVertexAttribArray(p Program, location string) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.EnableVertexAttribArray(uint32(l)) gl.EnableVertexAttribArray(uint32(l))
return nil
}) })
} }
func (c *Context) DisableVertexAttribArray(p Program, location string) { func (c *Context) DisableVertexAttribArray(p Program, location string) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.DisableVertexAttribArray(uint32(l)) gl.DisableVertexAttribArray(uint32(l))
return nil
}) })
} }
func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) (buffer Buffer) { func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer {
c.RunOnContextThread(func() { var buffer Buffer
c.RunOnContextThread(func() error {
var b uint32 var b uint32
gl.GenBuffers(1, &b) gl.GenBuffers(1, &b)
gl.BindBuffer(uint32(bufferType), b) gl.BindBuffer(uint32(bufferType), b)
@ -397,25 +411,28 @@ func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage Bu
panic("not reach") panic("not reach")
} }
buffer = Buffer(b) buffer = Buffer(b)
return return nil
}) })
return return buffer
} }
func (c *Context) BindElementArrayBuffer(b Buffer) { func (c *Context) BindElementArrayBuffer(b Buffer) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, uint32(b)) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, uint32(b))
return nil
}) })
} }
func (c *Context) BufferSubData(bufferType BufferType, data []int16) { func (c *Context) BufferSubData(bufferType BufferType, data []int16) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
gl.BufferSubData(uint32(bufferType), 0, 2*len(data), gl.Ptr(data)) gl.BufferSubData(uint32(bufferType), 0, 2*len(data), gl.Ptr(data))
return nil
}) })
} }
func (c *Context) DrawElements(mode Mode, len int) { func (c *Context) DrawElements(mode Mode, len int) {
c.RunOnContextThread(func() { c.RunOnContextThread(func() error {
gl.DrawElements(uint32(mode), int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(0)) gl.DrawElements(uint32(mode), int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(0))
return nil
}) })
} }

View File

@ -239,8 +239,9 @@ func (u *UserInterface) SwapBuffers() {
func (u *UserInterface) swapBuffers() { func (u *UserInterface) swapBuffers() {
// The bound framebuffer must be the default one (0) before swapping buffers. // The bound framebuffer must be the default one (0) before swapping buffers.
u.context.BindZeroFramebuffer() u.context.BindZeroFramebuffer()
u.context.RunOnContextThread(func() { u.context.RunOnContextThread(func() error {
u.window.SwapBuffers() u.window.SwapBuffers()
return nil
}) })
} }