graphicsdriver/opengl: Refactoring

This commit is contained in:
Hajime Hoshi 2020-11-21 22:43:05 +09:00
parent ddc8115b51
commit 859d247093
6 changed files with 30 additions and 38 deletions

View File

@ -83,7 +83,6 @@ const (
fragmentShader = shaderType(gl.FRAGMENT_SHADER) fragmentShader = shaderType(gl.FRAGMENT_SHADER)
arrayBuffer = bufferType(gl.ARRAY_BUFFER) arrayBuffer = bufferType(gl.ARRAY_BUFFER)
elementArrayBuffer = bufferType(gl.ELEMENT_ARRAY_BUFFER) elementArrayBuffer = bufferType(gl.ELEMENT_ARRAY_BUFFER)
dynamicDraw = bufferUsage(gl.DYNAMIC_DRAW)
short = dataType(gl.SHORT) short = dataType(gl.SHORT)
float = dataType(gl.FLOAT) float = dataType(gl.FLOAT)
@ -390,7 +389,7 @@ 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(uint32(arrayBuffer), b)
gl.BufferData(uint32(arrayBuffer), size, nil, uint32(dynamicDraw)) gl.BufferData(uint32(arrayBuffer), size, nil, gl.DYNAMIC_DRAW)
return buffer(b) return buffer(b)
} }
@ -398,7 +397,7 @@ 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(uint32(elementArrayBuffer), b)
gl.BufferData(uint32(elementArrayBuffer), size, nil, uint32(dynamicDraw)) gl.BufferData(uint32(elementArrayBuffer), size, nil, gl.DYNAMIC_DRAW)
return buffer(b) return buffer(b)
} }

View File

@ -78,9 +78,6 @@ const (
fragmentShader = shaderType(gles.FRAGMENT_SHADER) fragmentShader = shaderType(gles.FRAGMENT_SHADER)
arrayBuffer = bufferType(gles.ARRAY_BUFFER) arrayBuffer = bufferType(gles.ARRAY_BUFFER)
elementArrayBuffer = bufferType(gles.ELEMENT_ARRAY_BUFFER) elementArrayBuffer = bufferType(gles.ELEMENT_ARRAY_BUFFER)
dynamicDraw = bufferUsage(gles.DYNAMIC_DRAW)
streamDraw = bufferUsage(gles.STREAM_DRAW)
pixelUnpackBuffer = bufferType(gles.PIXEL_UNPACK_BUFFER)
short = dataType(gles.SHORT) short = dataType(gles.SHORT)
float = dataType(gles.FLOAT) float = dataType(gles.FLOAT)
@ -431,7 +428,7 @@ 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", int(arrayBuffer), js.Value(b))
gl.Call("bufferData", int(arrayBuffer), size, int(dynamicDraw)) gl.Call("bufferData", int(arrayBuffer), size, gles.DYNAMIC_DRAW)
return buffer(b) return buffer(b)
} }
@ -439,7 +436,7 @@ 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", int(elementArrayBuffer), js.Value(b))
gl.Call("bufferData", int(elementArrayBuffer), size, int(dynamicDraw)) gl.Call("bufferData", int(elementArrayBuffer), size, gles.DYNAMIC_DRAW)
return buffer(b) return buffer(b)
} }
@ -513,16 +510,16 @@ func (c *context) texSubImage2D(t textureNative, width, height int, args []*driv
func (c *context) newPixelBufferObject(width, height int) buffer { func (c *context) newPixelBufferObject(width, height int) buffer {
gl := c.gl gl := c.gl
b := gl.Call("createBuffer") b := gl.Call("createBuffer")
gl.Call("bindBuffer", int(pixelUnpackBuffer), js.Value(b)) gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, js.Value(b))
gl.Call("bufferData", int(pixelUnpackBuffer), 4*width*height, int(streamDraw)) gl.Call("bufferData", gles.PIXEL_UNPACK_BUFFER, 4*width*height, gles.STREAM_DRAW)
gl.Call("bindBuffer", int(pixelUnpackBuffer), nil) gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, nil)
return buffer(b) return buffer(b)
} }
func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, height int, args []*driver.ReplacePixelsArgs) { func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, height int, args []*driver.ReplacePixelsArgs) {
c.bindTexture(t) c.bindTexture(t)
gl := c.gl gl := c.gl
gl.Call("bindBuffer", int(pixelUnpackBuffer), js.Value(buffer)) gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, js.Value(buffer))
stride := 4 * width stride := 4 * width
for _, a := range args { for _, a := range args {
@ -530,7 +527,7 @@ func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, he
jsutil.CopySliceToJS(arr, a.Pixels) jsutil.CopySliceToJS(arr, a.Pixels)
offset := 4 * (a.Y*width + a.X) offset := 4 * (a.Y*width + a.X)
for j := 0; j < a.Height; j++ { for j := 0; j < a.Height; j++ {
gl.Call("bufferSubData", int(pixelUnpackBuffer), offset+stride*j, arr, 4*a.Width*j, 4*a.Width) gl.Call("bufferSubData", gles.PIXEL_UNPACK_BUFFER, offset+stride*j, arr, 4*a.Width*j, 4*a.Width)
} }
} }
@ -538,14 +535,14 @@ func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, he
// GLsizei width, GLsizei height, // GLsizei width, GLsizei height,
// GLenum format, GLenum type, GLintptr offset); // GLenum format, GLenum type, GLintptr offset);
gl.Call("texSubImage2D", gles.TEXTURE_2D, 0, 0, 0, width, height, gles.RGBA, gles.UNSIGNED_BYTE, 0) gl.Call("texSubImage2D", gles.TEXTURE_2D, 0, 0, 0, width, height, gles.RGBA, gles.UNSIGNED_BYTE, 0)
gl.Call("bindBuffer", int(pixelUnpackBuffer), nil) gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, nil)
} }
func (c *context) getBufferSubData(buffer buffer, width, height int) []byte { func (c *context) getBufferSubData(buffer buffer, width, height int) []byte {
gl := c.gl gl := c.gl
gl.Call("bindBuffer", int(pixelUnpackBuffer), buffer) gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, buffer)
arr := jsutil.TemporaryUint8Array(4 * width * height) arr := jsutil.TemporaryUint8Array(4 * width * height)
gl.Call("getBufferSubData", int(pixelUnpackBuffer), 0, arr) gl.Call("getBufferSubData", gles.PIXEL_UNPACK_BUFFER, 0, arr)
gl.Call("bindBuffer", int(pixelUnpackBuffer), 0) gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, 0)
return jsutil.Uint8ArrayToSlice(arr) return jsutil.Uint8ArrayToSlice(arr)
} }

View File

@ -81,7 +81,6 @@ const (
fragmentShader = shaderType(gles.FRAGMENT_SHADER) fragmentShader = shaderType(gles.FRAGMENT_SHADER)
arrayBuffer = bufferType(gles.ARRAY_BUFFER) arrayBuffer = bufferType(gles.ARRAY_BUFFER)
elementArrayBuffer = bufferType(gles.ELEMENT_ARRAY_BUFFER) elementArrayBuffer = bufferType(gles.ELEMENT_ARRAY_BUFFER)
dynamicDraw = bufferUsage(gles.DYNAMIC_DRAW)
short = dataType(gles.SHORT) short = dataType(gles.SHORT)
float = dataType(gles.FLOAT) float = dataType(gles.FLOAT)
@ -358,14 +357,14 @@ 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(uint32(arrayBuffer), b)
c.ctx.BufferData(uint32(arrayBuffer), size, nil, uint32(dynamicDraw)) c.ctx.BufferData(uint32(arrayBuffer), 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(uint32(elementArrayBuffer), b)
c.ctx.BufferData(uint32(elementArrayBuffer), size, nil, uint32(dynamicDraw)) c.ctx.BufferData(uint32(elementArrayBuffer), size, nil, gles.DYNAMIC_DRAW)
return buffer(b) return buffer(b)
} }

View File

@ -19,10 +19,6 @@ const (
FRAGMENT_SHADER = 0x8B30 FRAGMENT_SHADER = 0x8B30
ARRAY_BUFFER = 0x8892 ARRAY_BUFFER = 0x8892
ELEMENT_ARRAY_BUFFER = 0x8893 ELEMENT_ARRAY_BUFFER = 0x8893
DYNAMIC_DRAW = 0x88E8
STREAM_DRAW = 0x88E0
PIXEL_PACK_BUFFER = 0x88EB
PIXEL_UNPACK_BUFFER = 0x88EC
SHORT = 0x1402 SHORT = 0x1402
FLOAT = 0x1406 FLOAT = 0x1406
@ -34,13 +30,12 @@ const (
ONE_MINUS_DST_ALPHA = 0x0305 ONE_MINUS_DST_ALPHA = 0x0305
DST_COLOR = 0x0306 DST_COLOR = 0x0306
FALSE = 0
TRUE = 1
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
FALSE = 0
FRAMEBUFFER = 0x8D40 FRAMEBUFFER = 0x8D40
FRAMEBUFFER_BINDING = 0x8CA6 FRAMEBUFFER_BINDING = 0x8CA6
FRAMEBUFFER_COMPLETE = 0x8CD5 FRAMEBUFFER_COMPLETE = 0x8CD5
@ -49,8 +44,11 @@ const (
MAX_TEXTURE_SIZE = 0x0D33 MAX_TEXTURE_SIZE = 0x0D33
NEAREST = 0x2600 NEAREST = 0x2600
NO_ERROR = 0 NO_ERROR = 0
PIXEL_PACK_BUFFER = 0x88EB
PIXEL_UNPACK_BUFFER = 0x88EC
READ_WRITE = 0x88BA READ_WRITE = 0x88BA
RGBA = 0x1908 RGBA = 0x1908
STREAM_DRAW = 0x88E0
TEXTURE0 = 0x84C0 TEXTURE0 = 0x84C0
TEXTURE_2D = 0x0DE1 TEXTURE_2D = 0x0DE1
TEXTURE_MAG_FILTER = 0x2800 TEXTURE_MAG_FILTER = 0x2800
@ -58,6 +56,7 @@ const (
TEXTURE_WRAP_S = 0x2802 TEXTURE_WRAP_S = 0x2802
TEXTURE_WRAP_T = 0x2803 TEXTURE_WRAP_T = 0x2803
TRIANGLES = 0x0004 TRIANGLES = 0x0004
TRUE = 1
SCISSOR_TEST = 0x0C11 SCISSOR_TEST = 0x0C11
UNPACK_ALIGNMENT = 0x0CF5 UNPACK_ALIGNMENT = 0x0CF5
UNSIGNED_BYTE = 0x1401 UNSIGNED_BYTE = 0x1401

View File

@ -20,10 +20,6 @@ const (
FRAGMENT_SHADER = 0x8B30 FRAGMENT_SHADER = 0x8B30
ARRAY_BUFFER = 0x8892 ARRAY_BUFFER = 0x8892
ELEMENT_ARRAY_BUFFER = 0x8893 ELEMENT_ARRAY_BUFFER = 0x8893
DYNAMIC_DRAW = 0x88E8
STREAM_DRAW = 0x88E0
PIXEL_PACK_BUFFER = 0x88EB
PIXEL_UNPACK_BUFFER = 0x88EC
SHORT = 0x1402 SHORT = 0x1402
FLOAT = 0x1406 FLOAT = 0x1406
@ -35,13 +31,12 @@ const (
ONE_MINUS_DST_ALPHA = 0x0305 ONE_MINUS_DST_ALPHA = 0x0305
DST_COLOR = 0x0306 DST_COLOR = 0x0306
FALSE = 0
TRUE = 1
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
FALSE = 0
FRAMEBUFFER = 0x8D40 FRAMEBUFFER = 0x8D40
FRAMEBUFFER_BINDING = 0x8CA6 FRAMEBUFFER_BINDING = 0x8CA6
FRAMEBUFFER_COMPLETE = 0x8CD5 FRAMEBUFFER_COMPLETE = 0x8CD5
@ -51,9 +46,12 @@ const (
MAX_TEXTURE_SIZE = 0x0D33 MAX_TEXTURE_SIZE = 0x0D33
NEAREST = 0x2600 NEAREST = 0x2600
NO_ERROR = 0 NO_ERROR = 0
PIXEL_PACK_BUFFER = 0x88EB
PIXEL_UNPACK_BUFFER = 0x88EC
READ_WRITE = 0x88BA READ_WRITE = 0x88BA
RGBA = 0x1908 RGBA = 0x1908
SCISSOR_TEST = 0x0C11 SCISSOR_TEST = 0x0C11
STREAM_DRAW = 0x88E0
TEXTURE0 = 0x84C0 TEXTURE0 = 0x84C0
TEXTURE_2D = 0x0DE1 TEXTURE_2D = 0x0DE1
TEXTURE_MAG_FILTER = 0x2800 TEXTURE_MAG_FILTER = 0x2800
@ -61,6 +59,7 @@ const (
TEXTURE_WRAP_S = 0x2802 TEXTURE_WRAP_S = 0x2802
TEXTURE_WRAP_T = 0x2803 TEXTURE_WRAP_T = 0x2803
TRIANGLES = 0x0004 TRIANGLES = 0x0004
TRUE = 1
UNPACK_ALIGNMENT = 0x0CF5 UNPACK_ALIGNMENT = 0x0CF5
UNSIGNED_BYTE = 0x1401 UNSIGNED_BYTE = 0x1401
UNSIGNED_SHORT = 0x1403 UNSIGNED_SHORT = 0x1403

View File

@ -19,10 +19,9 @@ import (
) )
type ( type (
shaderType int shaderType int
bufferType int bufferType int
bufferUsage int operation int
operation int
) )
type dataType int type dataType int