From 8dba7b7722718644d6121d774c5bee4603f31273 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 21 Nov 2020 23:14:43 +0900 Subject: [PATCH] graphicsdriver/opengl: Refactoring: Remove dataType --- .../graphicsdriver/opengl/context_desktop.go | 6 ++---- internal/graphicsdriver/opengl/context_js.go | 6 ++---- .../graphicsdriver/opengl/context_mobile.go | 6 ++---- internal/graphicsdriver/opengl/gl/package.go | 4 ++-- internal/graphicsdriver/opengl/gles/const.go | 4 ++-- internal/graphicsdriver/opengl/program.go | 10 ++++++---- internal/graphicsdriver/opengl/types.go | 17 ----------------- 7 files changed, 16 insertions(+), 37 deletions(-) diff --git a/internal/graphicsdriver/opengl/context_desktop.go b/internal/graphicsdriver/opengl/context_desktop.go index c901371be..f4acf0e2b 100644 --- a/internal/graphicsdriver/opengl/context_desktop.go +++ b/internal/graphicsdriver/opengl/context_desktop.go @@ -81,8 +81,6 @@ func getProgramID(p program) programID { const ( 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) @@ -371,8 +369,8 @@ func (c *context) uniformFloats(p program, location string, v []float32, typ sha return true } -func (c *context) vertexAttribPointer(p program, index int, size int, dataType dataType, stride int, offset int) { - gl.VertexAttribPointer(uint32(index), int32(size), uint32(dataType), false, int32(stride), uintptr(offset)) +func (c *context) vertexAttribPointer(p program, index int, size int, stride int, offset int) { + gl.VertexAttribPointer(uint32(index), int32(size), gl.FLOAT, false, int32(stride), uintptr(offset)) } func (c *context) enableVertexAttribArray(p program, index int) { diff --git a/internal/graphicsdriver/opengl/context_js.go b/internal/graphicsdriver/opengl/context_js.go index a0a8a376d..54e365e2d 100644 --- a/internal/graphicsdriver/opengl/context_js.go +++ b/internal/graphicsdriver/opengl/context_js.go @@ -76,8 +76,6 @@ func getProgramID(p program) programID { const ( 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) @@ -407,9 +405,9 @@ func (c *context) uniformFloats(p program, location string, v []float32, typ sha return true } -func (c *context) vertexAttribPointer(p program, index int, size int, dataType dataType, stride int, offset int) { +func (c *context) vertexAttribPointer(p program, index int, size int, stride int, offset int) { gl := c.gl - gl.Call("vertexAttribPointer", index, size, int(dataType), false, stride, offset) + gl.Call("vertexAttribPointer", index, size, gles.FLOAT, false, stride, offset) } func (c *context) enableVertexAttribArray(p program, index int) { diff --git a/internal/graphicsdriver/opengl/context_mobile.go b/internal/graphicsdriver/opengl/context_mobile.go index 07e0d7634..67f667add 100644 --- a/internal/graphicsdriver/opengl/context_mobile.go +++ b/internal/graphicsdriver/opengl/context_mobile.go @@ -79,8 +79,6 @@ func getProgramID(p program) programID { const ( 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) @@ -340,8 +338,8 @@ func (c *context) uniformFloats(p program, location string, v []float32, typ sha return true } -func (c *context) vertexAttribPointer(p program, index int, size int, dataType dataType, stride int, offset int) { - c.ctx.VertexAttribPointer(uint32(index), int32(size), uint32(dataType), false, int32(stride), offset) +func (c *context) vertexAttribPointer(p program, index int, size int, stride int, offset int) { + c.ctx.VertexAttribPointer(uint32(index), int32(size), gles.FLOAT, false, int32(stride), offset) } func (c *context) enableVertexAttribArray(p program, index int) { diff --git a/internal/graphicsdriver/opengl/gl/package.go b/internal/graphicsdriver/opengl/gl/package.go index 31add3290..03e80e68a 100644 --- a/internal/graphicsdriver/opengl/gl/package.go +++ b/internal/graphicsdriver/opengl/gl/package.go @@ -17,8 +17,6 @@ package gl const ( VERTEX_SHADER = 0x8B31 FRAGMENT_SHADER = 0x8B30 - SHORT = 0x1402 - FLOAT = 0x1406 ZERO = 0 ONE = 1 @@ -36,6 +34,7 @@ const ( DYNAMIC_DRAW = 0x88E8 ELEMENT_ARRAY_BUFFER = 0x8893 FALSE = 0 + FLOAT = 0x1406 FRAMEBUFFER = 0x8D40 FRAMEBUFFER_BINDING = 0x8CA6 FRAMEBUFFER_COMPLETE = 0x8CD5 @@ -48,6 +47,7 @@ const ( PIXEL_UNPACK_BUFFER = 0x88EC READ_WRITE = 0x88BA RGBA = 0x1908 + SHORT = 0x1402 STREAM_DRAW = 0x88E0 TEXTURE0 = 0x84C0 TEXTURE_2D = 0x0DE1 diff --git a/internal/graphicsdriver/opengl/gles/const.go b/internal/graphicsdriver/opengl/gles/const.go index 840f65877..3b3a189cf 100644 --- a/internal/graphicsdriver/opengl/gles/const.go +++ b/internal/graphicsdriver/opengl/gles/const.go @@ -18,8 +18,6 @@ package gles const ( VERTEX_SHADER = 0x8B31 FRAGMENT_SHADER = 0x8B30 - SHORT = 0x1402 - FLOAT = 0x1406 ZERO = 0 ONE = 1 @@ -37,6 +35,7 @@ const ( DYNAMIC_DRAW = 0x88E8 ELEMENT_ARRAY_BUFFER = 0x8893 FALSE = 0 + FLOAT = 0x1406 FRAMEBUFFER = 0x8D40 FRAMEBUFFER_BINDING = 0x8CA6 FRAMEBUFFER_COMPLETE = 0x8CD5 @@ -51,6 +50,7 @@ const ( READ_WRITE = 0x88BA RGBA = 0x1908 SCISSOR_TEST = 0x0C11 + SHORT = 0x1402 STREAM_DRAW = 0x88E0 TEXTURE0 = 0x84C0 TEXTURE_2D = 0x0DE1 diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index 6f4f32a6b..75af14323 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -23,6 +23,8 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/web" ) +const floatSizeInBytes = 4 + // arrayBufferLayoutPart is a part of an array buffer layout. type arrayBufferLayoutPart struct { // TODO: This struct should belong to a program and know it. @@ -54,7 +56,7 @@ func (a *arrayBufferLayout) totalBytes() int { } t := 0 for _, p := range a.parts { - t += float.SizeInBytes() * p.num + t += floatSizeInBytes * p.num } a.total = t return a.total @@ -73,8 +75,8 @@ func (a *arrayBufferLayout) enable(context *context, program program) { total := a.totalBytes() offset := 0 for i, p := range a.parts { - context.vertexAttribPointer(program, i, p.num, float, total, offset) - offset += float.SizeInBytes() * p.num + context.vertexAttribPointer(program, i, p.num, total, offset) + offset += floatSizeInBytes * p.num } } @@ -106,7 +108,7 @@ var theArrayBufferLayout = arrayBufferLayout{ } func init() { - vertexFloatNum := theArrayBufferLayout.totalBytes() / float.SizeInBytes() + vertexFloatNum := theArrayBufferLayout.totalBytes() / floatSizeInBytes if graphics.VertexFloatNum != vertexFloatNum { panic(fmt.Sprintf("vertex float num must be %d but %d", graphics.VertexFloatNum, vertexFloatNum)) } diff --git a/internal/graphicsdriver/opengl/types.go b/internal/graphicsdriver/opengl/types.go index a47458053..518da7482 100644 --- a/internal/graphicsdriver/opengl/types.go +++ b/internal/graphicsdriver/opengl/types.go @@ -14,24 +14,7 @@ package opengl -import ( - "fmt" -) - type ( shaderType int operation int ) - -type dataType int - -func (d dataType) SizeInBytes() int { - switch d { - case short: - return 2 - case float: - return 4 - default: - panic(fmt.Sprintf("opengl: invalid data type: %d", d)) - } -}