From 2d5b062c3cad0f7e34f9bbd4bfddf343891b8a5d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 1 Sep 2017 01:29:56 +0900 Subject: [PATCH] opengl: Refactoring: Replace NewBuffer with new funcs Replace NewBuffer with NewArrayBuffer and NewElementArrayBuffer --- internal/graphics/program.go | 4 ++-- internal/opengl/context_desktop.go | 27 ++++++++++++++++----------- internal/opengl/context_js.go | 14 +++++++++++--- internal/opengl/context_mobile.go | 21 +++++++++++---------- 4 files changed, 40 insertions(+), 26 deletions(-) diff --git a/internal/graphics/program.go b/internal/graphics/program.go index 9a3a6cd56..997197764 100644 --- a/internal/graphics/program.go +++ b/internal/graphics/program.go @@ -47,7 +47,7 @@ func (a *arrayBufferLayout) totalBytes() int { } func (a *arrayBufferLayout) newArrayBuffer() opengl.Buffer { - return opengl.GetContext().NewBuffer(opengl.ArrayBuffer, a.totalBytes()*4*maxQuads, opengl.DynamicDraw) + return opengl.GetContext().NewArrayBuffer(a.totalBytes() * 4 * maxQuads) } func (a *arrayBufferLayout) enable(program opengl.Program) { @@ -169,7 +169,7 @@ func (s *openGLState) reset() error { indices[6*i+4] = 4*i + 2 indices[6*i+5] = 4*i + 3 } - s.indexBufferQuads = opengl.GetContext().NewBuffer(opengl.ElementArrayBuffer, indices, opengl.StaticDraw) + s.indexBufferQuads = opengl.GetContext().NewElementArrayBuffer(indices) return nil } diff --git a/internal/opengl/context_desktop.go b/internal/opengl/context_desktop.go index 1360d1243..501489e53 100644 --- a/internal/opengl/context_desktop.go +++ b/internal/opengl/context_desktop.go @@ -460,21 +460,26 @@ func (c *Context) DisableVertexAttribArray(p Program, location string) { }) } -func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer { +func (c *Context) NewArrayBuffer(size int) Buffer { var buffer Buffer _ = c.runOnContextThread(func() error { var b uint32 gl.GenBuffers(1, &b) - gl.BindBuffer(uint32(bufferType), b) - switch v := v.(type) { - case int: - gl.BufferData(uint32(bufferType), v, nil, uint32(bufferUsage)) - case []uint16: - // TODO: What about the endianness? - gl.BufferData(uint32(bufferType), 2*len(v), gl.Ptr(v), uint32(bufferUsage)) - default: - panic("not reach") - } + gl.BindBuffer(uint32(ArrayBuffer), b) + gl.BufferData(uint32(ArrayBuffer), size, nil, uint32(DynamicDraw)) + buffer = Buffer(b) + return nil + }) + return buffer +} + +func (c *Context) NewElementArrayBuffer(indices []uint16) Buffer { + var buffer Buffer + _ = c.runOnContextThread(func() error { + var b uint32 + gl.GenBuffers(1, &b) + gl.BindBuffer(uint32(ElementArrayBuffer), b) + gl.BufferData(uint32(ElementArrayBuffer), 2*len(indices), gl.Ptr(indices), uint32(StaticDraw)) buffer = Buffer(b) return nil }) diff --git a/internal/opengl/context_js.go b/internal/opengl/context_js.go index 90072e2d4..573831225 100644 --- a/internal/opengl/context_js.go +++ b/internal/opengl/context_js.go @@ -389,11 +389,19 @@ func (c *Context) DisableVertexAttribArray(p Program, location string) { gl.DisableVertexAttribArray(int(l)) } -func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer { +func (c *Context) NewArrayBuffer(size int) Buffer { gl := c.gl b := gl.CreateBuffer() - gl.BindBuffer(int(bufferType), b) - gl.BufferData(int(bufferType), v, int(bufferUsage)) + gl.BindBuffer(int(ArrayBuffer), b) + gl.BufferData(int(ArrayBuffer), size, int(DynamicDraw)) + return Buffer{b} +} + +func (c *Context) NewElementArrayBuffer(indices []uint16) Buffer { + gl := c.gl + b := gl.CreateBuffer() + gl.BindBuffer(int(ElementArrayBuffer), b) + gl.BufferData(int(ElementArrayBuffer), indices, int(StaticDraw)) return Buffer{b} } diff --git a/internal/opengl/context_mobile.go b/internal/opengl/context_mobile.go index b933c4ce2..cbb2a471d 100644 --- a/internal/opengl/context_mobile.go +++ b/internal/opengl/context_mobile.go @@ -365,18 +365,19 @@ func uint16ToBytes(v []uint16) []uint8 { return b } -func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer { +func (c *Context) NewArrayBuffer(size int) Buffer { gl := c.gl b := gl.CreateBuffer() - gl.BindBuffer(mgl.Enum(bufferType), b) - switch v := v.(type) { - case int: - gl.BufferInit(mgl.Enum(bufferType), v, mgl.Enum(bufferUsage)) - case []uint16: - gl.BufferData(mgl.Enum(bufferType), uint16ToBytes(v), mgl.Enum(bufferUsage)) - default: - panic("not reach") - } + gl.BindBuffer(mgl.Enum(ArrayBuffer), b) + gl.BufferInit(mgl.Enum(ArrayBuffer), size, mgl.Enum(DynamicDraw)) + return Buffer(b) +} + +func (c *Context) NewElementArrayBuffer(indices []uint16) Buffer { + gl := c.gl + b := gl.CreateBuffer() + gl.BindBuffer(mgl.Enum(ElementArrayBuffer), b) + gl.BufferData(mgl.Enum(ElementArrayBuffer), uint16ToBytes(indices), mgl.Enum(StaticDraw)) return Buffer(b) }