graphics: Refactoring: Replace QuadVertexNum with QuadVertexSizeInBytes func

This commit is contained in:
Hajime Hoshi 2016-10-23 03:12:11 +09:00
parent 2c844ec70c
commit bc8a8fbae8
3 changed files with 18 additions and 17 deletions

View File

@ -84,7 +84,7 @@ type textureQuads struct {
} }
func (t *textureQuads) vertices() []uint8 { func (t *textureQuads) vertices() []uint8 {
const size = graphics.QuadVertexNum size := graphics.QuadVertexSizeInBytes()
l := t.parts.Len() l := t.parts.Len()
vertices := make([]uint8, l*size) vertices := make([]uint8, l*size)
p := t.parts p := t.parts

View File

@ -108,7 +108,7 @@ 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. // 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. // Let's use them to compare to len(quads) in the future.
if maxQuads < len(vertices)/QuadVertexNum { if maxQuads < len(vertices)/QuadVertexSizeInBytes() {
return fmt.Errorf("len(quads) must be equal to or less than %d", maxQuads) return fmt.Errorf("len(quads) must be equal to or less than %d", maxQuads)
} }
numc := len(g) numc := len(g)
@ -118,7 +118,7 @@ func (q *commandQueue) Flush(context *opengl.Context) error {
return err return err
} }
if c, ok := c.(*drawImageCommand); ok { if c, ok := c.(*drawImageCommand); ok {
indexOffsetInBytes += 6 * len(c.vertices) / QuadVertexNum * 2 indexOffsetInBytes += 6 * len(c.vertices) / QuadVertexSizeInBytes() * 2
} }
} }
if 0 < numc { if 0 < numc {
@ -161,9 +161,9 @@ type drawImageCommand struct {
mode opengl.CompositeMode mode opengl.CompositeMode
} }
const ( func QuadVertexSizeInBytes() int {
QuadVertexNum = 32 // 4 * 2 [vertices] * 2 [tex_coords] * 2[bytes] return 4 * theArrayBufferLayout.total()
) }
func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int) error { func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int) error {
if err := c.dst.framebuffer.setAsViewport(context); err != nil { if err := c.dst.framebuffer.setAsViewport(context); err != nil {
@ -199,13 +199,13 @@ func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int)
func (c *drawImageCommand) split(quadsNum int) [2]*drawImageCommand { func (c *drawImageCommand) split(quadsNum int) [2]*drawImageCommand {
c1 := *c c1 := *c
c2 := *c c2 := *c
c1.vertices = c.vertices[:quadsNum*QuadVertexNum] c1.vertices = c.vertices[:quadsNum*QuadVertexSizeInBytes()]
c2.vertices = c.vertices[quadsNum*QuadVertexNum:] c2.vertices = c.vertices[quadsNum*QuadVertexSizeInBytes():]
return [2]*drawImageCommand{&c1, &c2} return [2]*drawImageCommand{&c1, &c2}
} }
func (c *drawImageCommand) quadsNum() int { func (c *drawImageCommand) quadsNum() int {
return len(c.vertices) / QuadVertexNum return len(c.vertices) / QuadVertexSizeInBytes()
} }
type replacePixelsCommand struct { type replacePixelsCommand struct {

View File

@ -32,22 +32,23 @@ type arrayBufferLayout struct {
parts []arrayBufferLayoutPart parts []arrayBufferLayoutPart
} }
func (a *arrayBufferLayout) newArrayBuffer(c *opengl.Context) opengl.Buffer { func (a *arrayBufferLayout) total() int {
total := 0 t := 0
for _, p := range a.parts { for _, p := range a.parts {
total += p.dataType.SizeInBytes() * p.num t += p.dataType.SizeInBytes() * p.num
} }
return c.NewBuffer(opengl.ArrayBuffer, total*4*maxQuads, opengl.DynamicDraw) return t
}
func (a *arrayBufferLayout) newArrayBuffer(c *opengl.Context) opengl.Buffer {
return c.NewBuffer(opengl.ArrayBuffer, a.total()*4*maxQuads, opengl.DynamicDraw)
} }
func (a *arrayBufferLayout) enable(c *opengl.Context, program opengl.Program) { func (a *arrayBufferLayout) enable(c *opengl.Context, program opengl.Program) {
for _, p := range a.parts { for _, p := range a.parts {
c.EnableVertexAttribArray(program, p.name) c.EnableVertexAttribArray(program, p.name)
} }
total := 0 total := a.total()
for _, p := range a.parts {
total += p.dataType.SizeInBytes() * p.num
}
offset := 0 offset := 0
for _, p := range a.parts { for _, p := range a.parts {
c.VertexAttribPointer(program, p.name, p.num, p.dataType, p.normalize, total, offset) c.VertexAttribPointer(program, p.name, p.num, p.dataType, p.normalize, total, offset)