From 9c014a281ade5d325c8726501ed2422649a79f15 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 21 Nov 2020 22:59:30 +0900 Subject: [PATCH] graphicsdriver/opengl: Refactoring --- .../graphicsdriver/opengl/context_desktop.go | 30 +++++++++--------- internal/graphicsdriver/opengl/context_js.go | 31 ++++++++++--------- .../graphicsdriver/opengl/context_mobile.go | 30 +++++++++--------- internal/graphicsdriver/opengl/gl/package.go | 12 +++---- internal/graphicsdriver/opengl/gles/const.go | 12 +++---- internal/graphicsdriver/opengl/program.go | 4 +-- 6 files changed, 63 insertions(+), 56 deletions(-) diff --git a/internal/graphicsdriver/opengl/context_desktop.go b/internal/graphicsdriver/opengl/context_desktop.go index 6996020d7..c901371be 100644 --- a/internal/graphicsdriver/opengl/context_desktop.go +++ b/internal/graphicsdriver/opengl/context_desktop.go @@ -79,12 +79,10 @@ func getProgramID(p program) programID { } const ( - vertexShader = shaderType(gl.VERTEX_SHADER) - fragmentShader = shaderType(gl.FRAGMENT_SHADER) - arrayBuffer = bufferType(gl.ARRAY_BUFFER) - elementArrayBuffer = bufferType(gl.ELEMENT_ARRAY_BUFFER) - short = dataType(gl.SHORT) - float = dataType(gl.FLOAT) + vertexShader = shaderType(gl.VERTEX_SHADER) + fragmentShader = shaderType(gl.FRAGMENT_SHADER) + short = dataType(gl.SHORT) + float = dataType(gl.FLOAT) zero = operation(gl.ZERO) one = operation(gl.ONE) @@ -388,29 +386,33 @@ func (c *context) disableVertexAttribArray(p program, index int) { func (c *context) newArrayBuffer(size int) buffer { var b uint32 gl.GenBuffers(1, &b) - gl.BindBuffer(uint32(arrayBuffer), b) - gl.BufferData(uint32(arrayBuffer), size, nil, gl.DYNAMIC_DRAW) + gl.BindBuffer(gl.ARRAY_BUFFER, b) + gl.BufferData(gl.ARRAY_BUFFER, size, nil, gl.DYNAMIC_DRAW) return buffer(b) } func (c *context) newElementArrayBuffer(size int) buffer { var b uint32 gl.GenBuffers(1, &b) - gl.BindBuffer(uint32(elementArrayBuffer), b) - gl.BufferData(uint32(elementArrayBuffer), size, nil, gl.DYNAMIC_DRAW) + gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, b) + gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, size, nil, gl.DYNAMIC_DRAW) return buffer(b) } -func (c *context) bindBuffer(bufferType bufferType, b buffer) { - gl.BindBuffer(uint32(bufferType), uint32(b)) +func (c *context) bindArrayBuffer(b buffer) { + gl.BindBuffer(gl.ARRAY_BUFFER, uint32(b)) +} + +func (c *context) bindElementArrayBuffer(b buffer) { + gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, uint32(b)) } func (c *context) arrayBufferSubData(data []float32) { - gl.BufferSubData(uint32(arrayBuffer), 0, len(data)*4, gl.Ptr(data)) + gl.BufferSubData(gl.ARRAY_BUFFER, 0, len(data)*4, gl.Ptr(data)) } func (c *context) elementArrayBufferSubData(data []uint16) { - gl.BufferSubData(uint32(elementArrayBuffer), 0, len(data)*2, gl.Ptr(data)) + gl.BufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, len(data)*2, gl.Ptr(data)) } func (c *context) deleteBuffer(b buffer) { diff --git a/internal/graphicsdriver/opengl/context_js.go b/internal/graphicsdriver/opengl/context_js.go index b6a437059..a0a8a376d 100644 --- a/internal/graphicsdriver/opengl/context_js.go +++ b/internal/graphicsdriver/opengl/context_js.go @@ -74,12 +74,10 @@ func getProgramID(p program) programID { } const ( - vertexShader = shaderType(gles.VERTEX_SHADER) - fragmentShader = shaderType(gles.FRAGMENT_SHADER) - arrayBuffer = bufferType(gles.ARRAY_BUFFER) - elementArrayBuffer = bufferType(gles.ELEMENT_ARRAY_BUFFER) - short = dataType(gles.SHORT) - float = dataType(gles.FLOAT) + vertexShader = shaderType(gles.VERTEX_SHADER) + fragmentShader = shaderType(gles.FRAGMENT_SHADER) + short = dataType(gles.SHORT) + float = dataType(gles.FLOAT) zero = operation(gles.ZERO) one = operation(gles.ONE) @@ -427,22 +425,27 @@ func (c *context) disableVertexAttribArray(p program, index int) { func (c *context) newArrayBuffer(size int) buffer { gl := c.gl b := gl.Call("createBuffer") - gl.Call("bindBuffer", int(arrayBuffer), js.Value(b)) - gl.Call("bufferData", int(arrayBuffer), size, gles.DYNAMIC_DRAW) + gl.Call("bindBuffer", gles.ARRAY_BUFFER, js.Value(b)) + gl.Call("bufferData", gles.ARRAY_BUFFER, size, gles.DYNAMIC_DRAW) return buffer(b) } func (c *context) newElementArrayBuffer(size int) buffer { gl := c.gl b := gl.Call("createBuffer") - gl.Call("bindBuffer", int(elementArrayBuffer), js.Value(b)) - gl.Call("bufferData", int(elementArrayBuffer), size, gles.DYNAMIC_DRAW) + gl.Call("bindBuffer", gles.ELEMENT_ARRAY_BUFFER, js.Value(b)) + gl.Call("bufferData", gles.ELEMENT_ARRAY_BUFFER, size, gles.DYNAMIC_DRAW) return buffer(b) } -func (c *context) bindBuffer(bufferType bufferType, b buffer) { +func (c *context) bindArrayBuffer(b buffer) { gl := c.gl - gl.Call("bindBuffer", int(bufferType), js.Value(b)) + gl.Call("bindBuffer", gles.ARRAY_BUFFER, js.Value(b)) +} + +func (c *context) bindElementArrayBuffer(b buffer) { + gl := c.gl + gl.Call("bindBuffer", gles.ELEMENT_ARRAY_BUFFER, js.Value(b)) } func (c *context) arrayBufferSubData(data []float32) { @@ -450,7 +453,7 @@ func (c *context) arrayBufferSubData(data []float32) { arr8 := jsutil.TemporaryUint8Array(len(data) * 4) arr := js.Global().Get("Float32Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(data)) jsutil.CopySliceToJS(arr, data) - gl.Call("bufferSubData", int(arrayBuffer), 0, arr) + gl.Call("bufferSubData", gles.ARRAY_BUFFER, 0, arr) } func (c *context) elementArrayBufferSubData(data []uint16) { @@ -458,7 +461,7 @@ func (c *context) elementArrayBufferSubData(data []uint16) { arr8 := jsutil.TemporaryUint8Array(len(data) * 2) arr := js.Global().Get("Uint16Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(data)) jsutil.CopySliceToJS(arr, data) - gl.Call("bufferSubData", int(elementArrayBuffer), 0, arr) + gl.Call("bufferSubData", gles.ELEMENT_ARRAY_BUFFER, 0, arr) } func (c *context) deleteBuffer(b buffer) { diff --git a/internal/graphicsdriver/opengl/context_mobile.go b/internal/graphicsdriver/opengl/context_mobile.go index 600c5ba4a..07e0d7634 100644 --- a/internal/graphicsdriver/opengl/context_mobile.go +++ b/internal/graphicsdriver/opengl/context_mobile.go @@ -77,12 +77,10 @@ func getProgramID(p program) programID { } const ( - vertexShader = shaderType(gles.VERTEX_SHADER) - fragmentShader = shaderType(gles.FRAGMENT_SHADER) - arrayBuffer = bufferType(gles.ARRAY_BUFFER) - elementArrayBuffer = bufferType(gles.ELEMENT_ARRAY_BUFFER) - short = dataType(gles.SHORT) - float = dataType(gles.FLOAT) + vertexShader = shaderType(gles.VERTEX_SHADER) + fragmentShader = shaderType(gles.FRAGMENT_SHADER) + short = dataType(gles.SHORT) + float = dataType(gles.FLOAT) zero = operation(gles.ZERO) one = operation(gles.ONE) @@ -356,28 +354,32 @@ func (c *context) disableVertexAttribArray(p program, index int) { func (c *context) newArrayBuffer(size int) buffer { b := c.ctx.GenBuffers(1)[0] - c.ctx.BindBuffer(uint32(arrayBuffer), b) - c.ctx.BufferData(uint32(arrayBuffer), size, nil, gles.DYNAMIC_DRAW) + c.ctx.BindBuffer(gles.ARRAY_BUFFER, b) + c.ctx.BufferData(gles.ARRAY_BUFFER, size, nil, gles.DYNAMIC_DRAW) return buffer(b) } func (c *context) newElementArrayBuffer(size int) buffer { b := c.ctx.GenBuffers(1)[0] - c.ctx.BindBuffer(uint32(elementArrayBuffer), b) - c.ctx.BufferData(uint32(elementArrayBuffer), size, nil, gles.DYNAMIC_DRAW) + c.ctx.BindBuffer(gles.ELEMENT_ARRAY_BUFFER, b) + c.ctx.BufferData(gles.ELEMENT_ARRAY_BUFFER, size, nil, gles.DYNAMIC_DRAW) return buffer(b) } -func (c *context) bindBuffer(bufferType bufferType, b buffer) { - c.ctx.BindBuffer(uint32(bufferType), uint32(b)) +func (c *context) bindArrayBuffer(b buffer) { + c.ctx.BindBuffer(gles.ARRAY_BUFFER, uint32(b)) +} + +func (c *context) bindElementArrayBuffer(b buffer) { + c.ctx.BindBuffer(gles.ELEMENT_ARRAY_BUFFER, uint32(b)) } func (c *context) arrayBufferSubData(data []float32) { - c.ctx.BufferSubData(uint32(arrayBuffer), 0, float32sToBytes(data)) + c.ctx.BufferSubData(gles.ARRAY_BUFFER, 0, float32sToBytes(data)) } func (c *context) elementArrayBufferSubData(data []uint16) { - c.ctx.BufferSubData(uint32(elementArrayBuffer), 0, uint16sToBytes(data)) + c.ctx.BufferSubData(gles.ELEMENT_ARRAY_BUFFER, 0, uint16sToBytes(data)) } func (c *context) deleteBuffer(b buffer) { diff --git a/internal/graphicsdriver/opengl/gl/package.go b/internal/graphicsdriver/opengl/gl/package.go index 512875114..31add3290 100644 --- a/internal/graphicsdriver/opengl/gl/package.go +++ b/internal/graphicsdriver/opengl/gl/package.go @@ -15,12 +15,10 @@ package gl const ( - VERTEX_SHADER = 0x8B31 - FRAGMENT_SHADER = 0x8B30 - ARRAY_BUFFER = 0x8892 - ELEMENT_ARRAY_BUFFER = 0x8893 - SHORT = 0x1402 - FLOAT = 0x1406 + VERTEX_SHADER = 0x8B31 + FRAGMENT_SHADER = 0x8B30 + SHORT = 0x1402 + FLOAT = 0x1406 ZERO = 0 ONE = 1 @@ -30,11 +28,13 @@ const ( ONE_MINUS_DST_ALPHA = 0x0305 DST_COLOR = 0x0306 + ARRAY_BUFFER = 0x8892 BLEND = 0x0BE2 CLAMP_TO_EDGE = 0x812F COLOR_ATTACHMENT0 = 0x8CE0 COMPILE_STATUS = 0x8B81 DYNAMIC_DRAW = 0x88E8 + ELEMENT_ARRAY_BUFFER = 0x8893 FALSE = 0 FRAMEBUFFER = 0x8D40 FRAMEBUFFER_BINDING = 0x8CA6 diff --git a/internal/graphicsdriver/opengl/gles/const.go b/internal/graphicsdriver/opengl/gles/const.go index 7e00ddb8c..840f65877 100644 --- a/internal/graphicsdriver/opengl/gles/const.go +++ b/internal/graphicsdriver/opengl/gles/const.go @@ -16,12 +16,10 @@ package gles const ( - VERTEX_SHADER = 0x8B31 - FRAGMENT_SHADER = 0x8B30 - ARRAY_BUFFER = 0x8892 - ELEMENT_ARRAY_BUFFER = 0x8893 - SHORT = 0x1402 - FLOAT = 0x1406 + VERTEX_SHADER = 0x8B31 + FRAGMENT_SHADER = 0x8B30 + SHORT = 0x1402 + FLOAT = 0x1406 ZERO = 0 ONE = 1 @@ -31,11 +29,13 @@ const ( ONE_MINUS_DST_ALPHA = 0x0305 DST_COLOR = 0x0306 + ARRAY_BUFFER = 0x8892 BLEND = 0x0BE2 CLAMP_TO_EDGE = 0x812F COLOR_ATTACHMENT0 = 0x8CE0 COMPILE_STATUS = 0x8B81 DYNAMIC_DRAW = 0x88E8 + ELEMENT_ARRAY_BUFFER = 0x8893 FALSE = 0 FRAMEBUFFER = 0x8D40 FRAMEBUFFER_BINDING = 0x8CA6 diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index 691efd9c7..6f4f32a6b 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -252,8 +252,8 @@ func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textu g.context.useProgram(program) if g.state.lastProgram.equal(zeroProgram) { theArrayBufferLayout.enable(&g.context, program) - g.context.bindBuffer(arrayBuffer, g.state.arrayBuffer) - g.context.bindBuffer(elementArrayBuffer, g.state.elementArrayBuffer) + g.context.bindArrayBuffer(g.state.arrayBuffer) + g.context.bindElementArrayBuffer(g.state.elementArrayBuffer) } g.state.lastProgram = program