graphicsdriver/opengl: Refactoring

This commit is contained in:
Hajime Hoshi 2020-11-21 22:59:30 +09:00
parent 859d247093
commit 9c014a281a
6 changed files with 63 additions and 56 deletions

View File

@ -81,8 +81,6 @@ func getProgramID(p program) programID {
const ( const (
vertexShader = shaderType(gl.VERTEX_SHADER) vertexShader = shaderType(gl.VERTEX_SHADER)
fragmentShader = shaderType(gl.FRAGMENT_SHADER) fragmentShader = shaderType(gl.FRAGMENT_SHADER)
arrayBuffer = bufferType(gl.ARRAY_BUFFER)
elementArrayBuffer = bufferType(gl.ELEMENT_ARRAY_BUFFER)
short = dataType(gl.SHORT) short = dataType(gl.SHORT)
float = dataType(gl.FLOAT) float = dataType(gl.FLOAT)
@ -388,29 +386,33 @@ func (c *context) disableVertexAttribArray(p program, index int) {
func (c *context) newArrayBuffer(size int) buffer { func (c *context) newArrayBuffer(size int) buffer {
var b uint32 var b uint32
gl.GenBuffers(1, &b) gl.GenBuffers(1, &b)
gl.BindBuffer(uint32(arrayBuffer), b) gl.BindBuffer(gl.ARRAY_BUFFER, b)
gl.BufferData(uint32(arrayBuffer), size, nil, gl.DYNAMIC_DRAW) gl.BufferData(gl.ARRAY_BUFFER, size, nil, gl.DYNAMIC_DRAW)
return buffer(b) return buffer(b)
} }
func (c *context) newElementArrayBuffer(size int) buffer { func (c *context) newElementArrayBuffer(size int) buffer {
var b uint32 var b uint32
gl.GenBuffers(1, &b) gl.GenBuffers(1, &b)
gl.BindBuffer(uint32(elementArrayBuffer), b) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, b)
gl.BufferData(uint32(elementArrayBuffer), size, nil, gl.DYNAMIC_DRAW) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, size, nil, gl.DYNAMIC_DRAW)
return buffer(b) return buffer(b)
} }
func (c *context) bindBuffer(bufferType bufferType, b buffer) { func (c *context) bindArrayBuffer(b buffer) {
gl.BindBuffer(uint32(bufferType), uint32(b)) 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) { 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) { 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) { func (c *context) deleteBuffer(b buffer) {

View File

@ -76,8 +76,6 @@ func getProgramID(p program) programID {
const ( const (
vertexShader = shaderType(gles.VERTEX_SHADER) vertexShader = shaderType(gles.VERTEX_SHADER)
fragmentShader = shaderType(gles.FRAGMENT_SHADER) fragmentShader = shaderType(gles.FRAGMENT_SHADER)
arrayBuffer = bufferType(gles.ARRAY_BUFFER)
elementArrayBuffer = bufferType(gles.ELEMENT_ARRAY_BUFFER)
short = dataType(gles.SHORT) short = dataType(gles.SHORT)
float = dataType(gles.FLOAT) float = dataType(gles.FLOAT)
@ -427,22 +425,27 @@ func (c *context) disableVertexAttribArray(p program, index int) {
func (c *context) newArrayBuffer(size int) buffer { func (c *context) newArrayBuffer(size int) buffer {
gl := c.gl gl := c.gl
b := gl.Call("createBuffer") b := gl.Call("createBuffer")
gl.Call("bindBuffer", int(arrayBuffer), js.Value(b)) gl.Call("bindBuffer", gles.ARRAY_BUFFER, js.Value(b))
gl.Call("bufferData", int(arrayBuffer), size, gles.DYNAMIC_DRAW) gl.Call("bufferData", gles.ARRAY_BUFFER, size, gles.DYNAMIC_DRAW)
return buffer(b) return buffer(b)
} }
func (c *context) newElementArrayBuffer(size int) buffer { func (c *context) newElementArrayBuffer(size int) buffer {
gl := c.gl gl := c.gl
b := gl.Call("createBuffer") b := gl.Call("createBuffer")
gl.Call("bindBuffer", int(elementArrayBuffer), js.Value(b)) gl.Call("bindBuffer", gles.ELEMENT_ARRAY_BUFFER, js.Value(b))
gl.Call("bufferData", int(elementArrayBuffer), size, gles.DYNAMIC_DRAW) gl.Call("bufferData", gles.ELEMENT_ARRAY_BUFFER, size, gles.DYNAMIC_DRAW)
return buffer(b) return buffer(b)
} }
func (c *context) bindBuffer(bufferType bufferType, b buffer) { func (c *context) bindArrayBuffer(b buffer) {
gl := c.gl 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) { func (c *context) arrayBufferSubData(data []float32) {
@ -450,7 +453,7 @@ func (c *context) arrayBufferSubData(data []float32) {
arr8 := jsutil.TemporaryUint8Array(len(data) * 4) arr8 := jsutil.TemporaryUint8Array(len(data) * 4)
arr := js.Global().Get("Float32Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(data)) arr := js.Global().Get("Float32Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(data))
jsutil.CopySliceToJS(arr, 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) { func (c *context) elementArrayBufferSubData(data []uint16) {
@ -458,7 +461,7 @@ func (c *context) elementArrayBufferSubData(data []uint16) {
arr8 := jsutil.TemporaryUint8Array(len(data) * 2) arr8 := jsutil.TemporaryUint8Array(len(data) * 2)
arr := js.Global().Get("Uint16Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(data)) arr := js.Global().Get("Uint16Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(data))
jsutil.CopySliceToJS(arr, 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) { func (c *context) deleteBuffer(b buffer) {

View File

@ -79,8 +79,6 @@ func getProgramID(p program) programID {
const ( const (
vertexShader = shaderType(gles.VERTEX_SHADER) vertexShader = shaderType(gles.VERTEX_SHADER)
fragmentShader = shaderType(gles.FRAGMENT_SHADER) fragmentShader = shaderType(gles.FRAGMENT_SHADER)
arrayBuffer = bufferType(gles.ARRAY_BUFFER)
elementArrayBuffer = bufferType(gles.ELEMENT_ARRAY_BUFFER)
short = dataType(gles.SHORT) short = dataType(gles.SHORT)
float = dataType(gles.FLOAT) float = dataType(gles.FLOAT)
@ -356,28 +354,32 @@ func (c *context) disableVertexAttribArray(p program, index int) {
func (c *context) newArrayBuffer(size int) buffer { func (c *context) newArrayBuffer(size int) buffer {
b := c.ctx.GenBuffers(1)[0] b := c.ctx.GenBuffers(1)[0]
c.ctx.BindBuffer(uint32(arrayBuffer), b) c.ctx.BindBuffer(gles.ARRAY_BUFFER, b)
c.ctx.BufferData(uint32(arrayBuffer), size, nil, gles.DYNAMIC_DRAW) c.ctx.BufferData(gles.ARRAY_BUFFER, size, nil, gles.DYNAMIC_DRAW)
return buffer(b) return buffer(b)
} }
func (c *context) newElementArrayBuffer(size int) buffer { func (c *context) newElementArrayBuffer(size int) buffer {
b := c.ctx.GenBuffers(1)[0] b := c.ctx.GenBuffers(1)[0]
c.ctx.BindBuffer(uint32(elementArrayBuffer), b) c.ctx.BindBuffer(gles.ELEMENT_ARRAY_BUFFER, b)
c.ctx.BufferData(uint32(elementArrayBuffer), size, nil, gles.DYNAMIC_DRAW) c.ctx.BufferData(gles.ELEMENT_ARRAY_BUFFER, size, nil, gles.DYNAMIC_DRAW)
return buffer(b) return buffer(b)
} }
func (c *context) bindBuffer(bufferType bufferType, b buffer) { func (c *context) bindArrayBuffer(b buffer) {
c.ctx.BindBuffer(uint32(bufferType), uint32(b)) 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) { 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) { 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) { func (c *context) deleteBuffer(b buffer) {

View File

@ -17,8 +17,6 @@ package gl
const ( const (
VERTEX_SHADER = 0x8B31 VERTEX_SHADER = 0x8B31
FRAGMENT_SHADER = 0x8B30 FRAGMENT_SHADER = 0x8B30
ARRAY_BUFFER = 0x8892
ELEMENT_ARRAY_BUFFER = 0x8893
SHORT = 0x1402 SHORT = 0x1402
FLOAT = 0x1406 FLOAT = 0x1406
@ -30,11 +28,13 @@ const (
ONE_MINUS_DST_ALPHA = 0x0305 ONE_MINUS_DST_ALPHA = 0x0305
DST_COLOR = 0x0306 DST_COLOR = 0x0306
ARRAY_BUFFER = 0x8892
BLEND = 0x0BE2 BLEND = 0x0BE2
CLAMP_TO_EDGE = 0x812F CLAMP_TO_EDGE = 0x812F
COLOR_ATTACHMENT0 = 0x8CE0 COLOR_ATTACHMENT0 = 0x8CE0
COMPILE_STATUS = 0x8B81 COMPILE_STATUS = 0x8B81
DYNAMIC_DRAW = 0x88E8 DYNAMIC_DRAW = 0x88E8
ELEMENT_ARRAY_BUFFER = 0x8893
FALSE = 0 FALSE = 0
FRAMEBUFFER = 0x8D40 FRAMEBUFFER = 0x8D40
FRAMEBUFFER_BINDING = 0x8CA6 FRAMEBUFFER_BINDING = 0x8CA6

View File

@ -18,8 +18,6 @@ package gles
const ( const (
VERTEX_SHADER = 0x8B31 VERTEX_SHADER = 0x8B31
FRAGMENT_SHADER = 0x8B30 FRAGMENT_SHADER = 0x8B30
ARRAY_BUFFER = 0x8892
ELEMENT_ARRAY_BUFFER = 0x8893
SHORT = 0x1402 SHORT = 0x1402
FLOAT = 0x1406 FLOAT = 0x1406
@ -31,11 +29,13 @@ const (
ONE_MINUS_DST_ALPHA = 0x0305 ONE_MINUS_DST_ALPHA = 0x0305
DST_COLOR = 0x0306 DST_COLOR = 0x0306
ARRAY_BUFFER = 0x8892
BLEND = 0x0BE2 BLEND = 0x0BE2
CLAMP_TO_EDGE = 0x812F CLAMP_TO_EDGE = 0x812F
COLOR_ATTACHMENT0 = 0x8CE0 COLOR_ATTACHMENT0 = 0x8CE0
COMPILE_STATUS = 0x8B81 COMPILE_STATUS = 0x8B81
DYNAMIC_DRAW = 0x88E8 DYNAMIC_DRAW = 0x88E8
ELEMENT_ARRAY_BUFFER = 0x8893
FALSE = 0 FALSE = 0
FRAMEBUFFER = 0x8D40 FRAMEBUFFER = 0x8D40
FRAMEBUFFER_BINDING = 0x8CA6 FRAMEBUFFER_BINDING = 0x8CA6

View File

@ -252,8 +252,8 @@ func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textu
g.context.useProgram(program) g.context.useProgram(program)
if g.state.lastProgram.equal(zeroProgram) { if g.state.lastProgram.equal(zeroProgram) {
theArrayBufferLayout.enable(&g.context, program) theArrayBufferLayout.enable(&g.context, program)
g.context.bindBuffer(arrayBuffer, g.state.arrayBuffer) g.context.bindArrayBuffer(g.state.arrayBuffer)
g.context.bindBuffer(elementArrayBuffer, g.state.elementArrayBuffer) g.context.bindElementArrayBuffer(g.state.elementArrayBuffer)
} }
g.state.lastProgram = program g.state.lastProgram = program