From 526a92256b67077fd1a4fb61cfe349d84b9a6363 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 21 Nov 2020 23:32:13 +0900 Subject: [PATCH] graphicsdriver/opengl: Refactoring: Remove shaderType --- internal/graphicsdriver/opengl/context.go | 2 ++ .../graphicsdriver/opengl/context_desktop.go | 15 +++++++++----- internal/graphicsdriver/opengl/context_js.go | 15 +++++++++----- .../graphicsdriver/opengl/context_mobile.go | 15 +++++++++----- internal/graphicsdriver/opengl/gl/package.go | 5 ++--- internal/graphicsdriver/opengl/gles/const.go | 5 ++--- internal/graphicsdriver/opengl/program.go | 4 ++-- internal/graphicsdriver/opengl/shader.go | 4 ++-- internal/graphicsdriver/opengl/types.go | 20 ------------------- 9 files changed, 40 insertions(+), 45 deletions(-) delete mode 100644 internal/graphicsdriver/opengl/types.go diff --git a/internal/graphicsdriver/opengl/context.go b/internal/graphicsdriver/opengl/context.go index f5186970c..a69136df3 100644 --- a/internal/graphicsdriver/opengl/context.go +++ b/internal/graphicsdriver/opengl/context.go @@ -21,6 +21,8 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/driver" ) +type operation int + func convertOperation(op driver.Operation) operation { switch op { case driver.Zero: diff --git a/internal/graphicsdriver/opengl/context_desktop.go b/internal/graphicsdriver/opengl/context_desktop.go index f4acf0e2b..1e81fa8ac 100644 --- a/internal/graphicsdriver/opengl/context_desktop.go +++ b/internal/graphicsdriver/opengl/context_desktop.go @@ -79,9 +79,6 @@ func getProgramID(p program) programID { } const ( - vertexShader = shaderType(gl.VERTEX_SHADER) - fragmentShader = shaderType(gl.FRAGMENT_SHADER) - zero = operation(gl.ZERO) one = operation(gl.ONE) srcAlpha = operation(gl.SRC_ALPHA) @@ -238,8 +235,16 @@ func (c *context) deleteFramebuffer(f framebufferNative) { gl.DeleteFramebuffersEXT(1, &ff) } -func (c *context) newShader(shaderType shaderType, source string) (shader, error) { - s := gl.CreateShader(uint32(shaderType)) +func (c *context) newVertexShader(source string) (shader, error) { + return c.newShader(gl.VERTEX_SHADER, source) +} + +func (c *context) newFragmentShader(source string) (shader, error) { + return c.newShader(gl.FRAGMENT_SHADER, source) +} + +func (c *context) newShader(shaderType uint32, source string) (shader, error) { + s := gl.CreateShader(shaderType) if s == 0 { return 0, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType) } diff --git a/internal/graphicsdriver/opengl/context_js.go b/internal/graphicsdriver/opengl/context_js.go index 54e365e2d..49c592c3c 100644 --- a/internal/graphicsdriver/opengl/context_js.go +++ b/internal/graphicsdriver/opengl/context_js.go @@ -74,9 +74,6 @@ func getProgramID(p program) programID { } const ( - vertexShader = shaderType(gles.VERTEX_SHADER) - fragmentShader = shaderType(gles.FRAGMENT_SHADER) - zero = operation(gles.ZERO) one = operation(gles.ONE) srcAlpha = operation(gles.SRC_ALPHA) @@ -278,7 +275,15 @@ func (c *context) deleteFramebuffer(f framebufferNative) { gl.Call("deleteFramebuffer", js.Value(f)) } -func (c *context) newShader(shaderType shaderType, source string) (shader, error) { +func (c *context) newVertexShader(source string) (shader, error) { + return c.newShader(gles.VERTEX_SHADER, source) +} + +func (c *context) newFragmentShader(source string) (shader, error) { + return c.newShader(gles.FRAGMENT_SHADER, source) +} + +func (c *context) newShader(shaderType int, source string) (shader, error) { gl := c.gl s := gl.Call("createShader", int(shaderType)) if jsutil.Equal(s, js.Null()) { @@ -479,7 +484,7 @@ func (c *context) maxTextureSizeImpl() int { func (c *context) getShaderPrecisionFormatPrecision() int { gl := c.gl - return gl.Call("getShaderPrecisionFormat", js.ValueOf(int(fragmentShader)), gles.HIGH_FLOAT).Get("precision").Int() + return gl.Call("getShaderPrecisionFormat", gles.FRAGMENT_SHADER, gles.HIGH_FLOAT).Get("precision").Int() } func (c *context) flush() { diff --git a/internal/graphicsdriver/opengl/context_mobile.go b/internal/graphicsdriver/opengl/context_mobile.go index 67f667add..63ec81260 100644 --- a/internal/graphicsdriver/opengl/context_mobile.go +++ b/internal/graphicsdriver/opengl/context_mobile.go @@ -77,9 +77,6 @@ func getProgramID(p program) programID { } const ( - vertexShader = shaderType(gles.VERTEX_SHADER) - fragmentShader = shaderType(gles.FRAGMENT_SHADER) - zero = operation(gles.ZERO) one = operation(gles.ONE) srcAlpha = operation(gles.SRC_ALPHA) @@ -227,8 +224,16 @@ func (c *context) deleteFramebuffer(f framebufferNative) { c.ctx.DeleteFramebuffers([]uint32{uint32(f)}) } -func (c *context) newShader(shaderType shaderType, source string) (shader, error) { - s := c.ctx.CreateShader(uint32(shaderType)) +func (c *context) newVertexShader(source string) (shader, error) { + return c.newShader(gles.VERTEX_SHADER, source) +} + +func (c *context) newFragmentShader(source string) (shader, error) { + return c.newShader(gles.FRAGMENT_SHADER, source) +} + +func (c *context) newShader(shaderType uint32, source string) (shader, error) { + s := c.ctx.CreateShader(shaderType) if s == 0 { return 0, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType) } diff --git a/internal/graphicsdriver/opengl/gl/package.go b/internal/graphicsdriver/opengl/gl/package.go index 03e80e68a..72ceac822 100644 --- a/internal/graphicsdriver/opengl/gl/package.go +++ b/internal/graphicsdriver/opengl/gl/package.go @@ -15,9 +15,6 @@ package gl const ( - VERTEX_SHADER = 0x8B31 - FRAGMENT_SHADER = 0x8B30 - ZERO = 0 ONE = 1 SRC_ALPHA = 0x0302 @@ -35,6 +32,7 @@ const ( ELEMENT_ARRAY_BUFFER = 0x8893 FALSE = 0 FLOAT = 0x1406 + FRAGMENT_SHADER = 0x8B30 FRAMEBUFFER = 0x8D40 FRAMEBUFFER_BINDING = 0x8CA6 FRAMEBUFFER_COMPLETE = 0x8CD5 @@ -61,6 +59,7 @@ const ( UNPACK_ALIGNMENT = 0x0CF5 UNSIGNED_BYTE = 0x1401 UNSIGNED_SHORT = 0x1403 + VERTEX_SHADER = 0x8B31 WRITE_ONLY = 0x88B9 ) diff --git a/internal/graphicsdriver/opengl/gles/const.go b/internal/graphicsdriver/opengl/gles/const.go index 3b3a189cf..49189f3f8 100644 --- a/internal/graphicsdriver/opengl/gles/const.go +++ b/internal/graphicsdriver/opengl/gles/const.go @@ -16,9 +16,6 @@ package gles const ( - VERTEX_SHADER = 0x8B31 - FRAGMENT_SHADER = 0x8B30 - ZERO = 0 ONE = 1 SRC_ALPHA = 0x0302 @@ -36,6 +33,7 @@ const ( ELEMENT_ARRAY_BUFFER = 0x8893 FALSE = 0 FLOAT = 0x1406 + FRAGMENT_SHADER = 0x8B30 FRAMEBUFFER = 0x8D40 FRAMEBUFFER_BINDING = 0x8CA6 FRAMEBUFFER_COMPLETE = 0x8CD5 @@ -63,5 +61,6 @@ const ( UNPACK_ALIGNMENT = 0x0CF5 UNSIGNED_BYTE = 0x1401 UNSIGNED_SHORT = 0x1403 + VERTEX_SHADER = 0x8B31 WRITE_ONLY = 0x88B9 ) diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index 75af14323..ba227d014 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -173,7 +173,7 @@ func (s *openGLState) reset(context *context) error { } } - shaderVertexModelviewNative, err := context.newShader(vertexShader, vertexShaderStr()) + shaderVertexModelviewNative, err := context.newVertexShader(vertexShaderStr()) if err != nil { panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err)) } @@ -190,7 +190,7 @@ func (s *openGLState) reset(context *context) error { driver.FilterLinear, driver.FilterScreen, } { - shaderFragmentColorMatrixNative, err := context.newShader(fragmentShader, fragmentShaderStr(c, f, a)) + shaderFragmentColorMatrixNative, err := context.newFragmentShader(fragmentShaderStr(c, f, a)) if err != nil { panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err)) } diff --git a/internal/graphicsdriver/opengl/shader.go b/internal/graphicsdriver/opengl/shader.go index 4e938bad2..0ecb82c85 100644 --- a/internal/graphicsdriver/opengl/shader.go +++ b/internal/graphicsdriver/opengl/shader.go @@ -54,13 +54,13 @@ func (s *Shader) Dispose() { func (s *Shader) compile() error { vssrc, fssrc := glsl.Compile(s.ir, glslVersion()) - vs, err := s.graphics.context.newShader(vertexShader, vssrc) + vs, err := s.graphics.context.newVertexShader(vssrc) if err != nil { return fmt.Errorf("opengl: vertex shader compile error: %v, source:\n%s", err, vssrc) } defer s.graphics.context.deleteShader(vs) - fs, err := s.graphics.context.newShader(fragmentShader, fssrc) + fs, err := s.graphics.context.newFragmentShader(fssrc) if err != nil { return fmt.Errorf("opengl: fragment shader compile error: %v, source:\n%s", err, fssrc) } diff --git a/internal/graphicsdriver/opengl/types.go b/internal/graphicsdriver/opengl/types.go deleted file mode 100644 index 518da7482..000000000 --- a/internal/graphicsdriver/opengl/types.go +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2014 Hajime Hoshi -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package opengl - -type ( - shaderType int - operation int -)