From 6de9e6696ed46be6632c2f1a8b29979098713f38 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 29 Oct 2016 01:07:19 +0900 Subject: [PATCH] graphics: Cache total bytes --- internal/graphics/command.go | 2 +- internal/graphics/program.go | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/graphics/command.go b/internal/graphics/command.go index 3926e2152..a73d8fd73 100644 --- a/internal/graphics/command.go +++ b/internal/graphics/command.go @@ -199,7 +199,7 @@ type drawImageCommand struct { } func QuadVertexSizeInBytes() int { - return 4 * theArrayBufferLayout.total() + return 4 * theArrayBufferLayout.totalBytes() } func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int) error { diff --git a/internal/graphics/program.go b/internal/graphics/program.go index 0d6f72f96..5e3a2719d 100644 --- a/internal/graphics/program.go +++ b/internal/graphics/program.go @@ -30,25 +30,30 @@ type arrayBufferLayoutPart struct { type arrayBufferLayout struct { parts []arrayBufferLayoutPart + total int } -func (a *arrayBufferLayout) total() int { +func (a *arrayBufferLayout) totalBytes() int { + if a.total != 0 { + return a.total + } t := 0 for _, p := range a.parts { t += p.dataType.SizeInBytes() * p.num } - return t + a.total = t + return a.total } func (a *arrayBufferLayout) newArrayBuffer(c *opengl.Context) opengl.Buffer { - return c.NewBuffer(opengl.ArrayBuffer, a.total()*4*maxQuads, opengl.DynamicDraw) + return c.NewBuffer(opengl.ArrayBuffer, a.totalBytes()*4*maxQuads, opengl.DynamicDraw) } func (a *arrayBufferLayout) enable(c *opengl.Context, program opengl.Program) { for _, p := range a.parts { c.EnableVertexAttribArray(program, p.name) } - total := a.total() + total := a.totalBytes() offset := 0 for _, p := range a.parts { c.VertexAttribPointer(program, p.name, p.num, p.dataType, p.normalize, total, offset)