From 13a94d3446243de4335e36c7900c65aa235b3bb1 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 3 Jan 2015 15:52:02 +0900 Subject: [PATCH] Avoid type switch --- image.go | 10 +++---- .../graphics/internal/shader/drawtexture.go | 2 +- internal/graphics/internal/shader/program.go | 10 +++---- internal/opengl/context.go | 28 ++++++++--------- internal/opengl/context_js.go | 30 +++++++++++-------- 5 files changed, 41 insertions(+), 39 deletions(-) diff --git a/image.go b/image.go index 3bb77b1bb..e29b77a37 100644 --- a/image.go +++ b/image.go @@ -68,12 +68,12 @@ func (i *innerImage) drawImage(c *opengl.Context, img *innerImage, options *Draw return i.framebuffer.DrawTexture(c, img.texture, quads, &options.GeoM, &options.ColorM) } -func u(x float64, width int) float32 { - return float32(x) / float32(internal.NextPowerOf2Int(width)) +func u(x float32, width int) float32 { + return x / float32(internal.NextPowerOf2Int(width)) } -func v(y float64, height int) float32 { - return float32(y) / float32(internal.NextPowerOf2Int(height)) +func v(y float32, height int) float32 { + return y / float32(internal.NextPowerOf2Int(height)) } type textureQuads struct { @@ -94,7 +94,7 @@ func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 float32) { func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 float32) { src := t.parts[i].Src w, h := t.width, t.height - return u(float64(src.Min.X), w), v(float64(src.Min.Y), h), u(float64(src.Max.X), w), v(float64(src.Max.Y), h) + return u(float32(src.Min.X), w), v(float32(src.Min.Y), h), u(float32(src.Max.X), w), v(float32(src.Max.Y), h) } // Image represents an image. diff --git a/internal/graphics/internal/shader/drawtexture.go b/internal/graphics/internal/shader/drawtexture.go index 71b786a4b..024baa750 100644 --- a/internal/graphics/internal/shader/drawtexture.go +++ b/internal/graphics/internal/shader/drawtexture.go @@ -73,7 +73,7 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4 c.VertexAttribPointer(program, "vertex", stride, uintptr(float32Size*0)) c.VertexAttribPointer(program, "tex_coord", stride, uintptr(float32Size*2)) - vertices := []float32{} + vertices := make([]float32, 0, quads.Len()) for i := 0; i < quads.Len(); i++ { x0, y0, x1, y1 := quads.Vertex(i) u0, v0, u1, v1 := quads.Texture(i) diff --git a/internal/graphics/internal/shader/program.go b/internal/graphics/internal/shader/program.go index f181610a6..d176995be 100644 --- a/internal/graphics/internal/shader/program.go +++ b/internal/graphics/internal/shader/program.go @@ -73,7 +73,7 @@ func useProgramColorMatrix(c *opengl.Context, projectionMatrix []float32, geo Ma // TODO: Check the performance. program := programColorMatrix - c.Uniform(program, "projection_matrix", projectionMatrix) + c.UniformFloats(program, "projection_matrix", projectionMatrix) ma := float32(geo.Element(0, 0)) mb := float32(geo.Element(0, 1)) @@ -87,8 +87,8 @@ func useProgramColorMatrix(c *opengl.Context, projectionMatrix []float32, geo Ma 0, 0, 1, 0, tx, ty, 0, 1, } - c.Uniform(program, "modelview_matrix", glModelviewMatrix) - c.Uniform(program, "texture", 0) + c.UniformFloats(program, "modelview_matrix", glModelviewMatrix) + c.UniformInt(program, "texture", 0) e := [4][5]float32{} for i := 0; i < 4; i++ { @@ -103,11 +103,11 @@ func useProgramColorMatrix(c *opengl.Context, projectionMatrix []float32, geo Ma e[0][2], e[1][2], e[2][2], e[3][2], e[0][3], e[1][3], e[2][3], e[3][3], } - c.Uniform(program, "color_matrix", glColorMatrix) + c.UniformFloats(program, "color_matrix", glColorMatrix) glColorMatrixTranslation := []float32{ e[0][4], e[1][4], e[2][4], e[3][4], } - c.Uniform(program, "color_matrix_translation", glColorMatrixTranslation) + c.UniformFloats(program, "color_matrix_translation", glColorMatrixTranslation) return program } diff --git a/internal/opengl/context.go b/internal/opengl/context.go index 8630ea71c..16b9bb66d 100644 --- a/internal/opengl/context.go +++ b/internal/opengl/context.go @@ -174,22 +174,20 @@ func (c *Context) UseProgram(p Program) { gl.Program(p).Use() } -func (c *Context) Uniform(p Program, location string, v interface{}) { +func (c *Context) UniformInt(p Program, location string, v int) { l := gl.Program(p).GetUniformLocation(location) - switch v := v.(type) { - case int: - l.Uniform1i(v) - case []float32: - switch len(v) { - case 4: - l.Uniform4fv(1, v) - case 16: - v2 := [16]float32{} - copy(v2[:], v) - l.UniformMatrix4fv(false, v2) - default: - panic("not reach") - } + l.Uniform1i(v) +} + +func (c *Context) UniformFloats(p Program, location string, v []float32) { + l := gl.Program(p).GetUniformLocation(location) + switch len(v) { + case 4: + l.Uniform4fv(1, v) + case 16: + v2 := [16]float32{} + copy(v2[:], v) + l.UniformMatrix4fv(false, v2) default: panic("not reach") } diff --git a/internal/opengl/context_js.go b/internal/opengl/context_js.go index 9864a84dd..6d090b240 100644 --- a/internal/opengl/context_js.go +++ b/internal/opengl/context_js.go @@ -196,7 +196,7 @@ func (c *Context) UseProgram(p Program) { gl.UseProgram(p) } -func (c *Context) Uniform(p Program, location string, v interface{}) { +func (c *Context) UniformInt(p Program, location string, v int) { gl := c.gl key := locationCacheKey{p, location} l, ok := uniformLocationCache[key] @@ -204,18 +204,22 @@ func (c *Context) Uniform(p Program, location string, v interface{}) { l = gl.GetUniformLocation(p, location) uniformLocationCache[key] = l } - switch v := v.(type) { - case int: - gl.Uniform1i(l, v) - case []float32: - switch len(v) { - case 4: - gl.Call("uniform4fv", l, v) - case 16: - gl.UniformMatrix4fv(l, false, v) - default: - panic("not reach") - } + gl.Uniform1i(l, v) +} + +func (c *Context) UniformFloats(p Program, location string, v []float32) { + gl := c.gl + key := locationCacheKey{p, location} + l, ok := uniformLocationCache[key] + if !ok { + l = gl.GetUniformLocation(p, location) + uniformLocationCache[key] = l + } + switch len(v) { + case 4: + gl.Call("uniform4fv", l, v) + case 16: + gl.UniformMatrix4fv(l, false, v) default: panic("not reach") }