graphics: Expose graphics.QuadVertexNum

This commit is contained in:
Hajime Hoshi 2016-10-23 01:52:55 +09:00
parent 95f231d58a
commit e72ccee61b
2 changed files with 74 additions and 73 deletions

View File

@ -84,8 +84,9 @@ type textureQuads struct {
}
func (t *textureQuads) vertices() []uint8 {
const size = graphics.QuadVertexNum
l := t.parts.Len()
vertices := make([]uint8, l*32)
vertices := make([]uint8, l*size)
p := t.parts
w, h := t.width, t.height
width2p := int(graphics.NextPowerOf2Int32(int32(w)))
@ -104,73 +105,73 @@ func (t *textureQuads) vertices() []uint8 {
u0, v0, u1, v1 := u(sx0, width2p), v(sy0, height2p), u(sx1, width2p), v(sy1, height2p)
// Use direct assign here. `append` function might be slow on browsers.
if endian.IsLittle() {
vertices[32*n] = uint8(x0)
vertices[32*n+1] = uint8(x0 >> 8)
vertices[32*n+2] = uint8(y0)
vertices[32*n+3] = uint8(y0 >> 8)
vertices[32*n+4] = uint8(u0)
vertices[32*n+5] = uint8(u0 >> 8)
vertices[32*n+6] = uint8(v0)
vertices[32*n+7] = uint8(v0 >> 8)
vertices[32*n+8] = uint8(x1)
vertices[32*n+9] = uint8(x1 >> 8)
vertices[32*n+10] = uint8(y0)
vertices[32*n+11] = uint8(y0 >> 8)
vertices[32*n+12] = uint8(u1)
vertices[32*n+13] = uint8(u1 >> 8)
vertices[32*n+14] = uint8(v0)
vertices[32*n+15] = uint8(v0 >> 8)
vertices[32*n+16] = uint8(x0)
vertices[32*n+17] = uint8(x0 >> 8)
vertices[32*n+18] = uint8(y1)
vertices[32*n+19] = uint8(y1 >> 8)
vertices[32*n+20] = uint8(u0)
vertices[32*n+21] = uint8(u0 >> 8)
vertices[32*n+22] = uint8(v1)
vertices[32*n+23] = uint8(v1 >> 8)
vertices[32*n+24] = uint8(x1)
vertices[32*n+25] = uint8(x1 >> 8)
vertices[32*n+26] = uint8(y1)
vertices[32*n+27] = uint8(y1 >> 8)
vertices[32*n+28] = uint8(u1)
vertices[32*n+29] = uint8(u1 >> 8)
vertices[32*n+30] = uint8(v1)
vertices[32*n+31] = uint8(v1 >> 8)
vertices[size*n] = uint8(x0)
vertices[size*n+1] = uint8(x0 >> 8)
vertices[size*n+2] = uint8(y0)
vertices[size*n+3] = uint8(y0 >> 8)
vertices[size*n+4] = uint8(u0)
vertices[size*n+5] = uint8(u0 >> 8)
vertices[size*n+6] = uint8(v0)
vertices[size*n+7] = uint8(v0 >> 8)
vertices[size*n+8] = uint8(x1)
vertices[size*n+9] = uint8(x1 >> 8)
vertices[size*n+10] = uint8(y0)
vertices[size*n+11] = uint8(y0 >> 8)
vertices[size*n+12] = uint8(u1)
vertices[size*n+13] = uint8(u1 >> 8)
vertices[size*n+14] = uint8(v0)
vertices[size*n+15] = uint8(v0 >> 8)
vertices[size*n+16] = uint8(x0)
vertices[size*n+17] = uint8(x0 >> 8)
vertices[size*n+18] = uint8(y1)
vertices[size*n+19] = uint8(y1 >> 8)
vertices[size*n+20] = uint8(u0)
vertices[size*n+21] = uint8(u0 >> 8)
vertices[size*n+22] = uint8(v1)
vertices[size*n+23] = uint8(v1 >> 8)
vertices[size*n+24] = uint8(x1)
vertices[size*n+25] = uint8(x1 >> 8)
vertices[size*n+26] = uint8(y1)
vertices[size*n+27] = uint8(y1 >> 8)
vertices[size*n+28] = uint8(u1)
vertices[size*n+29] = uint8(u1 >> 8)
vertices[size*n+30] = uint8(v1)
vertices[size*n+31] = uint8(v1 >> 8)
} else {
vertices[32*n] = uint8(x0 >> 8)
vertices[32*n+1] = uint8(x0)
vertices[32*n+2] = uint8(y0 >> 8)
vertices[32*n+3] = uint8(y0)
vertices[32*n+4] = uint8(u0 >> 8)
vertices[32*n+5] = uint8(u0)
vertices[32*n+6] = uint8(v0 >> 8)
vertices[32*n+7] = uint8(v0)
vertices[32*n+8] = uint8(x1 >> 8)
vertices[32*n+9] = uint8(x1)
vertices[32*n+10] = uint8(y0 >> 8)
vertices[32*n+11] = uint8(y0)
vertices[32*n+12] = uint8(u1 >> 8)
vertices[32*n+13] = uint8(u1)
vertices[32*n+14] = uint8(v0 >> 8)
vertices[32*n+15] = uint8(v0)
vertices[32*n+16] = uint8(x0 >> 8)
vertices[32*n+17] = uint8(x0)
vertices[32*n+18] = uint8(y1 >> 8)
vertices[32*n+19] = uint8(y1)
vertices[32*n+20] = uint8(u0 >> 8)
vertices[32*n+21] = uint8(u0)
vertices[32*n+22] = uint8(v1 >> 8)
vertices[32*n+23] = uint8(v1)
vertices[32*n+24] = uint8(x1 >> 8)
vertices[32*n+25] = uint8(x1)
vertices[32*n+26] = uint8(y1 >> 8)
vertices[32*n+27] = uint8(y1)
vertices[32*n+28] = uint8(u1 >> 8)
vertices[32*n+29] = uint8(u1)
vertices[32*n+30] = uint8(v1 >> 8)
vertices[32*n+31] = uint8(v1)
vertices[size*n] = uint8(x0 >> 8)
vertices[size*n+1] = uint8(x0)
vertices[size*n+2] = uint8(y0 >> 8)
vertices[size*n+3] = uint8(y0)
vertices[size*n+4] = uint8(u0 >> 8)
vertices[size*n+5] = uint8(u0)
vertices[size*n+6] = uint8(v0 >> 8)
vertices[size*n+7] = uint8(v0)
vertices[size*n+8] = uint8(x1 >> 8)
vertices[size*n+9] = uint8(x1)
vertices[size*n+10] = uint8(y0 >> 8)
vertices[size*n+11] = uint8(y0)
vertices[size*n+12] = uint8(u1 >> 8)
vertices[size*n+13] = uint8(u1)
vertices[size*n+14] = uint8(v0 >> 8)
vertices[size*n+15] = uint8(v0)
vertices[size*n+16] = uint8(x0 >> 8)
vertices[size*n+17] = uint8(x0)
vertices[size*n+18] = uint8(y1 >> 8)
vertices[size*n+19] = uint8(y1)
vertices[size*n+20] = uint8(u0 >> 8)
vertices[size*n+21] = uint8(u0)
vertices[size*n+22] = uint8(v1 >> 8)
vertices[size*n+23] = uint8(v1)
vertices[size*n+24] = uint8(x1 >> 8)
vertices[size*n+25] = uint8(x1)
vertices[size*n+26] = uint8(y1 >> 8)
vertices[size*n+27] = uint8(y1)
vertices[size*n+28] = uint8(u1 >> 8)
vertices[size*n+29] = uint8(u1)
vertices[size*n+30] = uint8(v1 >> 8)
vertices[size*n+31] = uint8(v1)
}
n++
}
return vertices[:n*32]
return vertices[:n*size]
}

View File

@ -108,8 +108,8 @@ func (q *commandQueue) Flush(context *opengl.Context) error {
}
// NOTE: 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.
if maxQuads < len(vertices)/quadVertexNum {
return errors.New(fmt.Sprintf("len(quads) must be equal to or less than %d", maxQuads))
if maxQuads < len(vertices)/QuadVertexNum {
return fmt.Errorf("len(quads) must be equal to or less than %d", maxQuads)
}
numc := len(g)
indexOffsetInBytes := 0
@ -118,7 +118,7 @@ func (q *commandQueue) Flush(context *opengl.Context) error {
return err
}
if c, ok := c.(*drawImageCommand); ok {
indexOffsetInBytes += 6 * len(c.vertices) / quadVertexNum * 2
indexOffsetInBytes += 6 * len(c.vertices) / QuadVertexNum * 2
}
}
if 0 < numc {
@ -162,7 +162,7 @@ type drawImageCommand struct {
}
const (
quadVertexNum = 32 // 4 * 2 [vertices] * 2 [tex_coords] * 2[bytes]
QuadVertexNum = 32 // 4 * 2 [vertices] * 2 [tex_coords] * 2[bytes]
)
func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int) error {
@ -199,13 +199,13 @@ func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int)
func (c *drawImageCommand) split(quadsNum int) [2]*drawImageCommand {
c1 := *c
c2 := *c
c1.vertices = c.vertices[:quadsNum*quadVertexNum]
c2.vertices = c.vertices[quadsNum*quadVertexNum:]
c1.vertices = c.vertices[:quadsNum*QuadVertexNum]
c2.vertices = c.vertices[quadsNum*QuadVertexNum:]
return [2]*drawImageCommand{&c1, &c2}
}
func (c *drawImageCommand) quadsNum() int {
return len(c.vertices) / quadVertexNum
return len(c.vertices) / QuadVertexNum
}
type replacePixelsCommand struct {