sprites: Speed up: use float64 as often as possible

This commit is contained in:
Hajime Hoshi 2015-01-14 23:58:34 +09:00
parent 3200ae58a4
commit 25cf2dab22
4 changed files with 30 additions and 30 deletions

View File

@ -108,9 +108,9 @@ func (l rectVertexQuads) Len() int {
return l.Rects.Len() return l.Rects.Len()
} }
func (l rectVertexQuads) Vertex(i int) (x0, y0, x1, y1 float32) { func (l rectVertexQuads) Vertex(i int) (x0, y0, x1, y1 float64) {
ix0, iy0, ix1, iy1 := l.Rects.Points(i) ix0, iy0, ix1, iy1 := l.Rects.Points(i)
return float32(ix0), float32(iy0), float32(ix1), float32(iy1) return float64(ix0), float64(iy0), float64(ix1), float64(iy1)
} }
// DrawRects draws rectangles on the image. // DrawRects draws rectangles on the image.

View File

@ -66,12 +66,12 @@ func (w *wholeImage) Src(i int) (x0, y0, x1, y1 int) {
return 0, 0, w.width, w.height return 0, 0, w.width, w.height
} }
func u(x float32, width int) float32 { func u(x float64, width int) float64 {
return x / float32(internal.NextPowerOf2Int(width)) return x / float64(internal.NextPowerOf2Int(width))
} }
func v(y float32, height int) float32 { func v(y float64, height int) float64 {
return y / float32(internal.NextPowerOf2Int(height)) return y / float64(internal.NextPowerOf2Int(height))
} }
type textureQuads struct { type textureQuads struct {
@ -84,13 +84,13 @@ func (t *textureQuads) Len() int {
return t.parts.Len() return t.parts.Len()
} }
func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 float32) { func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 float64) {
ix0, iy0, ix1, iy1 := t.parts.Dst(i) ix0, iy0, ix1, iy1 := t.parts.Dst(i)
return float32(ix0), float32(iy0), float32(ix1), float32(iy1) return float64(ix0), float64(iy0), float64(ix1), float64(iy1)
} }
func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 float32) { func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 float64) {
x0, y0, x1, y1 := t.parts.Src(i) x0, y0, x1, y1 := t.parts.Src(i)
w, h := t.width, t.height w, h := t.width, t.height
return u(float32(x0), w), v(float32(y0), h), u(float32(x1), w), v(float32(y1), h) return u(float64(x0), w), v(float64(y0), h), u(float64(x1), w), v(float64(y1), h)
} }

View File

@ -94,8 +94,8 @@ type Matrix interface {
type TextureQuads interface { type TextureQuads interface {
Len() int Len() int
Vertex(i int) (x0, y0, x1, y1 float32) Vertex(i int) (x0, y0, x1, y1 float64)
Texture(i int) (u0, v0, u1, v1 float32) Texture(i int) (u0, v0, u1, v1 float64)
} }
func (f *Framebuffer) Fill(c *opengl.Context, r, g, b, a float64) error { func (f *Framebuffer) Fill(c *opengl.Context, r, g, b, a float64) error {
@ -115,7 +115,7 @@ func (f *Framebuffer) DrawTexture(c *opengl.Context, t *Texture, quads TextureQu
type VertexQuads interface { type VertexQuads interface {
Len() int Len() int
Vertex(i int) (x0, y0, x1, y1 float32) Vertex(i int) (x0, y0, x1, y1 float64)
} }
func (f *Framebuffer) DrawRects(c *opengl.Context, r, g, b, a float64, quads VertexQuads) error { func (f *Framebuffer) DrawRects(c *opengl.Context, r, g, b, a float64, quads VertexQuads) error {

View File

@ -35,19 +35,19 @@ type Matrix interface {
type TextureQuads interface { type TextureQuads interface {
Len() int Len() int
Vertex(i int) (x0, y0, x1, y1 float32) Vertex(i int) (x0, y0, x1, y1 float64)
Texture(i int) (u0, v0, u1, v1 float32) Texture(i int) (u0, v0, u1, v1 float64)
} }
// TODO: better name?
const stride = 4 * 4
var initialized = false var initialized = false
func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4][4]float64, quads TextureQuads, geo Matrix, color Matrix) error { func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4][4]float64, quads TextureQuads, geo Matrix, color Matrix) error {
// TODO: WebGL doesn't seem to have Check gl.MAX_ELEMENTS_VERTICES or gl.MAX_ELEMENTS_INDICES so far. // TODO: WebGL doesn't seem to have Check gl.MAX_ELEMENTS_VERTICES or gl.MAX_ELEMENTS_INDICES so far.
// Let's use them to compare to len(quads) in the future. // Let's use them to compare to len(quads) in the future.
// TODO: Unify stride or other consts
const stride = 4 * 4
if !initialized { if !initialized {
if err := initialize(c); err != nil { if err := initialize(c); err != nil {
return err return err
@ -73,10 +73,10 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4
continue continue
} }
vertices = append(vertices, vertices = append(vertices,
x0, y0, u0, v0, float32(x0), float32(y0), float32(u0), float32(v0),
x1, y0, u1, v0, float32(x1), float32(y0), float32(u1), float32(v0),
x0, y1, u0, v1, float32(x0), float32(y1), float32(u0), float32(v1),
x1, y1, u1, v1, float32(x1), float32(y1), float32(u1), float32(v1),
) )
} }
if len(vertices) == 0 { if len(vertices) == 0 {
@ -89,7 +89,7 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4
type VertexQuads interface { type VertexQuads interface {
Len() int Len() int
Vertex(i int) (x0, y0, x1, y1 float32) Vertex(i int) (x0, y0, x1, y1 float64)
} }
func max(a, b float32) float32 { func max(a, b float32) float32 {
@ -99,9 +99,9 @@ func max(a, b float32) float32 {
return a return a
} }
func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, r, g, b, a float64, quads VertexQuads) error { var vertices = make([]float32, 0, stride*quadsMaxNum)
const stride = 4 * 4
func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, r, g, b, a float64, quads VertexQuads) error {
if !initialized { if !initialized {
if err := initialize(c); err != nil { if err := initialize(c); err != nil {
return err return err
@ -116,17 +116,17 @@ func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, r, g, b, a fl
f := useProgramRect(c, glMatrix(projectionMatrix), r, g, b, a) f := useProgramRect(c, glMatrix(projectionMatrix), r, g, b, a)
defer f.FinishProgram() defer f.FinishProgram()
vertices := make([]float32, 0, stride*quads.Len()) vertices := vertices[0:0]
for i := 0; i < quads.Len(); i++ { for i := 0; i < quads.Len(); i++ {
x0, y0, x1, y1 := quads.Vertex(i) x0, y0, x1, y1 := quads.Vertex(i)
if x0 == x1 || y0 == y1 { if x0 == x1 || y0 == y1 {
continue continue
} }
vertices = append(vertices, vertices = append(vertices,
x0, y0, 0, 0, float32(x0), float32(y0), 0, 0,
x1, y0, 1, 0, float32(x1), float32(y0), 1, 0,
x0, y1, 0, 1, float32(x0), float32(y1), 0, 1,
x1, y1, 1, 1, float32(x1), float32(y1), 1, 1,
) )
} }
if len(vertices) == 0 { if len(vertices) == 0 {