From c6cf8e5184b9810a5b1837dcc6d55d1803a3dc07 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 19 Feb 2018 01:45:03 +0900 Subject: [PATCH] opengl: Remove struct usages to avoid copying (program) --- internal/opengl/context_desktop.go | 2 +- internal/opengl/context_js.go | 24 +++++++++++------------- internal/opengl/context_mobile.go | 2 +- internal/opengl/locationcache.go | 4 ++-- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/internal/opengl/context_desktop.go b/internal/opengl/context_desktop.go index 6ef8c17b3..b47fab9e2 100644 --- a/internal/opengl/context_desktop.go +++ b/internal/opengl/context_desktop.go @@ -54,7 +54,7 @@ const ( invalidFramebuffer = (1 << 32) - 1 ) -func (p Program) id() programID { +func getProgramID(p Program) programID { return programID(p) } diff --git a/internal/opengl/context_js.go b/internal/opengl/context_js.go index 74f5d0208..ba248a473 100644 --- a/internal/opengl/context_js.go +++ b/internal/opengl/context_js.go @@ -41,9 +41,7 @@ type Shader struct { *js.Object } -type Program struct { - *js.Object -} +type Program interface{} type Buffer struct { *js.Object @@ -68,8 +66,8 @@ var ( invalidFramebuffer = Framebuffer{} ) -func (p Program) id() programID { - return programID(p.Get("__ebiten_programId").Int()) +func getProgramID(p Program) programID { + return programID(p.(*js.Object).Get("__ebiten_programId").Int()) } func init() { @@ -301,7 +299,7 @@ func (c *Context) NewProgram(shaders []Shader) (Program, error) { gl := c.gl p := gl.CreateProgram() if p == nil { - return Program{nil}, errors.New("opengl: glCreateProgram failed") + return nil, errors.New("opengl: glCreateProgram failed") } p.Set("__ebiten_programId", c.lastProgramID) c.lastProgramID++ @@ -311,27 +309,27 @@ func (c *Context) NewProgram(shaders []Shader) (Program, error) { } gl.LinkProgram(p) if !gl.GetProgramParameterb(p, gl.LINK_STATUS) { - return Program{nil}, errors.New("opengl: program error") + return nil, errors.New("opengl: program error") } - return Program{p}, nil + return Program(p), nil } func (c *Context) UseProgram(p Program) { gl := c.gl - gl.UseProgram(p.Object) + gl.UseProgram(p.(*js.Object)) } func (c *Context) DeleteProgram(p Program) { gl := c.gl - if !gl.IsProgram(p.Object) { + if !gl.IsProgram(p.(*js.Object)) { return } - gl.DeleteProgram(p.Object) + gl.DeleteProgram(p.(*js.Object)) } func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation { gl := c.gl - return uniformLocation(gl.GetUniformLocation(p.Object, location)) + return uniformLocation(gl.GetUniformLocation(p.(*js.Object), location)) } func (c *Context) UniformInt(p Program, location string, v int) { @@ -357,7 +355,7 @@ func (c *Context) UniformFloats(p Program, location string, v []float32) { func (c *Context) getAttribLocationImpl(p Program, location string) attribLocation { gl := c.gl - return attribLocation(gl.GetAttribLocation(p.Object, location)) + return attribLocation(gl.GetAttribLocation(p.(*js.Object), location)) } func (c *Context) VertexAttribPointer(p Program, location string, size int, dataType DataType, stride int, offset int) { diff --git a/internal/opengl/context_mobile.go b/internal/opengl/context_mobile.go index 9cfde1d5d..8165ea54d 100644 --- a/internal/opengl/context_mobile.go +++ b/internal/opengl/context_mobile.go @@ -53,7 +53,7 @@ var ( invalidFramebuffer = Framebuffer(mgl.Framebuffer{(1 << 32) - 1}) ) -func (p Program) id() programID { +func getProgramID(p Program) programID { return programID(p.Value) } diff --git a/internal/opengl/locationcache.go b/internal/opengl/locationcache.go index 25a81546f..3c39c8f3f 100644 --- a/internal/opengl/locationcache.go +++ b/internal/opengl/locationcache.go @@ -29,7 +29,7 @@ func newLocationCache() *locationCache { } func (c *locationCache) GetUniformLocation(context *Context, p Program, location string) uniformLocation { - id := p.id() + id := getProgramID(p) if _, ok := c.uniformLocationCache[id]; !ok { c.uniformLocationCache[id] = map[string]uniformLocation{} } @@ -42,7 +42,7 @@ func (c *locationCache) GetUniformLocation(context *Context, p Program, location } func (c *locationCache) GetAttribLocation(context *Context, p Program, location string) attribLocation { - id := p.id() + id := getProgramID(p) if _, ok := c.attribLocationCache[id]; !ok { c.attribLocationCache[id] = map[string]attribLocation{} }