opengl: Remove struct usages to avoid copying (texture)

This commit is contained in:
Hajime Hoshi 2018-02-19 02:49:00 +09:00
parent f85c846596
commit 2db1753503
5 changed files with 21 additions and 44 deletions

View File

@ -214,8 +214,7 @@ func (c *fillCommand) Exec(indexOffsetInBytes int) error {
opengl.GetContext().Flush() opengl.GetContext().Flush()
// Mysterious, but binding texture is needed after filling // Mysterious, but binding texture is needed after filling
// on some mechines like Photon 2 (#492). // on some mechines like Photon 2 (#492).
var t opengl.Texture opengl.GetContext().BindTexture(opengl.InvalidTexture)
opengl.GetContext().BindTexture(t)
return nil return nil
} }

View File

@ -75,7 +75,7 @@ func GetContext() *Context {
} }
func (c *Context) BindTexture(t Texture) { func (c *Context) BindTexture(t Texture) {
if c.lastTexture.equals(t) { if c.lastTexture == t {
return return
} }
c.bindTextureImpl(t) c.bindTextureImpl(t)

View File

@ -34,13 +34,7 @@ type (
Buffer uint32 Buffer uint32
) )
func (t Texture) equals(other Texture) bool { var InvalidTexture Texture
return t == other
}
func (f Framebuffer) equals(other Framebuffer) bool {
return f == other
}
type ( type (
uniformLocation int32 uniformLocation int32

View File

@ -29,30 +29,20 @@ import (
// Note that `type Texture *js.Object` doesn't work. // Note that `type Texture *js.Object` doesn't work.
// There is no way to get the internal object in that case. // There is no way to get the internal object in that case.
type Texture struct {
*js.Object
}
type ( type (
Framebuffer interface{} Texture interface{}
Shader interface{} Framebuffer interface{}
Program interface{} Shader interface{}
Buffer interface{} Program interface{}
Buffer interface{}
uniformLocation interface{}
) )
func (t Texture) equals(other Texture) bool {
return t.Object == other.Object
}
type uniformLocation interface{}
type attribLocation int type attribLocation int
type programID int type programID int
var ( var InvalidTexture = Texture((*js.Object)(nil))
invalidTexture = Texture{}
)
func getProgramID(p Program) programID { func getProgramID(p Program) programID {
return programID(p.(*js.Object).Get("__ebiten_programId").Int()) return programID(p.(*js.Object).Get("__ebiten_programId").Int())
@ -118,7 +108,7 @@ func Init() error {
func (c *Context) Reset() error { func (c *Context) Reset() error {
c.locationCache = newLocationCache() c.locationCache = newLocationCache()
c.lastTexture = invalidTexture c.lastTexture = nil
c.lastFramebuffer = nil c.lastFramebuffer = nil
c.lastViewportWidth = 0 c.lastViewportWidth = 0
c.lastViewportHeight = 0 c.lastViewportHeight = 0
@ -145,10 +135,10 @@ func (c *Context) NewTexture(width, height int, pixels []uint8) (Texture, error)
gl := c.gl gl := c.gl
t := gl.CreateTexture() t := gl.CreateTexture()
if t == nil { if t == nil {
return Texture{nil}, errors.New("opengl: glGenTexture failed") return nil, errors.New("opengl: glGenTexture failed")
} }
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
c.BindTexture(Texture{t}) c.BindTexture(t)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
@ -164,7 +154,7 @@ func (c *Context) NewTexture(width, height int, pixels []uint8) (Texture, error)
} }
gl.Call("texImage2D", gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, p) gl.Call("texImage2D", gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, p)
return Texture{t}, nil return t, nil
} }
func (c *Context) bindFramebufferImpl(f Framebuffer) { func (c *Context) bindFramebufferImpl(f Framebuffer) {
@ -187,23 +177,23 @@ func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8,
func (c *Context) bindTextureImpl(t Texture) { func (c *Context) bindTextureImpl(t Texture) {
gl := c.gl gl := c.gl
gl.BindTexture(gl.TEXTURE_2D, t.Object) gl.BindTexture(gl.TEXTURE_2D, t.(*js.Object))
} }
func (c *Context) DeleteTexture(t Texture) { func (c *Context) DeleteTexture(t Texture) {
gl := c.gl gl := c.gl
if !gl.IsTexture(t.Object) { if !gl.IsTexture(t.(*js.Object)) {
return return
} }
if c.lastTexture == t { if c.lastTexture == t {
c.lastTexture = invalidTexture c.lastTexture = nil
} }
gl.DeleteTexture(t.Object) gl.DeleteTexture(t.(*js.Object))
} }
func (c *Context) IsTexture(t Texture) bool { func (c *Context) IsTexture(t Texture) bool {
gl := c.gl gl := c.gl
b := gl.IsTexture(t.Object) b := gl.IsTexture(t.(*js.Object))
return b return b
} }
@ -220,7 +210,7 @@ func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) {
f := gl.CreateFramebuffer() f := gl.CreateFramebuffer()
c.bindFramebuffer(f) c.bindFramebuffer(f)
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, t.Object, 0) gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, t.(*js.Object), 0)
s := gl.CheckFramebufferStatus(gl.FRAMEBUFFER) s := gl.CheckFramebufferStatus(gl.FRAMEBUFFER)
if s != gl.FRAMEBUFFER_COMPLETE { if s != gl.FRAMEBUFFER_COMPLETE {
return nil, errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s)) return nil, errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s))

View File

@ -33,13 +33,7 @@ type (
Buffer mgl.Buffer Buffer mgl.Buffer
) )
func (t Texture) equals(other Texture) bool { var InvalidTexture Texture
return t == other
}
func (f Framebuffer) equals(other Framebuffer) bool {
return f == other
}
type ( type (
uniformLocation mgl.Uniform uniformLocation mgl.Uniform