graphics: Bug fix: textuer should be stored at OpenGL layer

This commit is contained in:
Hajime Hoshi 2016-07-09 19:15:53 +09:00
parent 193c03ea58
commit 275fc66f06
5 changed files with 34 additions and 16 deletions

View File

@ -38,12 +38,24 @@ type Context struct {
locationCache *locationCache locationCache *locationCache
screenFramebuffer Framebuffer // This might not be the default frame buffer '0' (e.g. iOS). screenFramebuffer Framebuffer // This might not be the default frame buffer '0' (e.g. iOS).
lastFramebuffer Framebuffer lastFramebuffer Framebuffer
lastTexture Texture
lastViewportWidth int lastViewportWidth int
lastViewportHeight int lastViewportHeight int
lastCompositeMode CompositeMode lastCompositeMode CompositeMode
context context
} }
func (c *Context) BindTexture(t Texture) error {
if c.lastTexture == t {
return nil
}
if err := c.bindTextureImpl(t); err != nil {
return err
}
c.lastTexture = t
return nil
}
func (c *Context) bindFramebuffer(f Framebuffer) error { func (c *Context) bindFramebuffer(f Framebuffer) error {
if c.lastFramebuffer == f { if c.lastFramebuffer == f {
return nil return nil

View File

@ -150,8 +150,15 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (
return errors.New("opengl: creating texture failed") return errors.New("opengl: creating texture failed")
} }
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
gl.BindTexture(gl.TEXTURE_2D, t) texture = Texture(t)
return nil
}); err != nil {
return 0, err
}
if err := c.BindTexture(texture); err != nil {
return 0, err
}
if err := c.RunOnContextThread(func() error {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int32(filter)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int32(filter))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int32(filter)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int32(filter))
@ -160,8 +167,6 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (
p = pixels p = pixels
} }
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)
return nil return nil
}); err != nil { }); err != nil {
return 0, err return 0, err
@ -202,11 +207,12 @@ func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8,
return pixels, nil return pixels, nil
} }
func (c *Context) BindTexture(t Texture) { func (c *Context) bindTextureImpl(t Texture) error {
c.RunOnContextThread(func() error { c.RunOnContextThread(func() error {
gl.BindTexture(gl.TEXTURE_2D, uint32(t)) gl.BindTexture(gl.TEXTURE_2D, uint32(t))
return nil return nil
}) })
return nil
} }
func (c *Context) DeleteTexture(t Texture) { func (c *Context) DeleteTexture(t Texture) {

View File

@ -144,7 +144,9 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (
return Texture{nil}, errors.New("opengl: glGenTexture failed") return Texture{nil}, errors.New("opengl: glGenTexture failed")
} }
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
gl.BindTexture(gl.TEXTURE_2D, t) if err := c.BindTexture(Texture{t}); err != nil {
return Texture{}, err
}
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int(filter)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int(filter))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int(filter)) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int(filter))
@ -182,9 +184,10 @@ func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8,
return pixels.Interface().([]uint8), nil return pixels.Interface().([]uint8), nil
} }
func (c *Context) BindTexture(t Texture) { func (c *Context) bindTextureImpl(t Texture) error {
gl := c.gl gl := c.gl
gl.BindTexture(gl.TEXTURE_2D, t.Object) gl.BindTexture(gl.TEXTURE_2D, t.Object)
return nil
} }
func (c *Context) DeleteTexture(t Texture) { func (c *Context) DeleteTexture(t Texture) {

View File

@ -122,7 +122,9 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (
return Texture{}, errors.New("opengl: creating texture failed") return Texture{}, errors.New("opengl: creating texture failed")
} }
gl.PixelStorei(mgl.UNPACK_ALIGNMENT, 4) gl.PixelStorei(mgl.UNPACK_ALIGNMENT, 4)
gl.BindTexture(mgl.TEXTURE_2D, t) if err := c.BindTexture(Texture(t)); err != nil {
return Texture{}, err
}
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MAG_FILTER, int(filter)) gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MAG_FILTER, int(filter))
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MIN_FILTER, int(filter)) gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MIN_FILTER, int(filter))
@ -156,9 +158,10 @@ func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8,
return pixels, nil return pixels, nil
} }
func (c *Context) BindTexture(t Texture) { func (c *Context) bindTextureImpl(t Texture) error {
gl := c.gl gl := c.gl
gl.BindTexture(mgl.TEXTURE_2D, mgl.Texture(t)) gl.BindTexture(mgl.TEXTURE_2D, mgl.Texture(t))
return nil
} }
func (c *Context) DeleteTexture(t Texture) { func (c *Context) DeleteTexture(t Texture) {

View File

@ -30,7 +30,6 @@ type openGLState struct {
lastModelviewMatrix []float32 lastModelviewMatrix []float32
lastColorMatrix []float32 lastColorMatrix []float32
lastColorMatrixTranslation []float32 lastColorMatrixTranslation []float32
lastTexture opengl.Texture
} }
var ( var (
@ -63,7 +62,6 @@ func (s *openGLState) reset(context *opengl.Context) error {
s.lastModelviewMatrix = nil s.lastModelviewMatrix = nil
s.lastColorMatrix = nil s.lastColorMatrix = nil
s.lastColorMatrixTranslation = nil s.lastColorMatrixTranslation = nil
s.lastTexture = zeroTexture
if s.arrayBuffer != zeroBuffer { if s.arrayBuffer != zeroBuffer {
context.DeleteBuffer(s.arrayBuffer) context.DeleteBuffer(s.arrayBuffer)
@ -152,7 +150,6 @@ func (p *programContext) begin() {
p.state.lastModelviewMatrix = nil p.state.lastModelviewMatrix = nil
p.state.lastColorMatrix = nil p.state.lastColorMatrix = nil
p.state.lastColorMatrixTranslation = nil p.state.lastColorMatrixTranslation = nil
p.state.lastTexture = zeroTexture
c.BindElementArrayBuffer(p.state.indexBufferQuads) c.BindElementArrayBuffer(p.state.indexBufferQuads)
c.UniformInt(p.program, "texture", 0) c.UniformInt(p.program, "texture", 0)
} }
@ -218,10 +215,7 @@ func (p *programContext) begin() {
// We don't have to call gl.ActiveTexture here: GL_TEXTURE0 is the default active texture // We don't have to call gl.ActiveTexture here: GL_TEXTURE0 is the default active texture
// See also: https://www.opengl.org/sdk/docs/man2/xhtml/glActiveTexture.xml // See also: https://www.opengl.org/sdk/docs/man2/xhtml/glActiveTexture.xml
if p.state.lastTexture != p.texture {
c.BindTexture(p.texture) c.BindTexture(p.texture)
p.state.lastTexture = p.texture
}
} }
func (p *programContext) end() { func (p *programContext) end() {