From ef08e218c8adba794d4cd4a1949407cbb3e92b2b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 17 Nov 2018 22:29:04 +0900 Subject: [PATCH] graphicsdriver/opengl: Refactoring: Remove framebuffer.projectionMatrix --- internal/graphicsdriver/opengl/framebuffer.go | 22 +++---------- internal/graphicsdriver/opengl/image.go | 7 ---- internal/graphicsdriver/opengl/matrix.go | 32 ------------------- internal/graphicsdriver/opengl/program.go | 23 ++++++------- internal/graphicsdriver/opengl/shader.go | 9 +++++- 5 files changed, 24 insertions(+), 69 deletions(-) delete mode 100644 internal/graphicsdriver/opengl/matrix.go diff --git a/internal/graphicsdriver/opengl/framebuffer.go b/internal/graphicsdriver/opengl/framebuffer.go index d1af3f06d..b19f7becb 100644 --- a/internal/graphicsdriver/opengl/framebuffer.go +++ b/internal/graphicsdriver/opengl/framebuffer.go @@ -16,11 +16,10 @@ package opengl // framebuffer is a wrapper of OpenGL's framebuffer. type framebuffer struct { - driver *Driver - native framebufferNative - proMatrix []float32 - width int - height int + driver *Driver + native framebufferNative + width int + height int } // newFramebufferFromTexture creates a framebuffer from the given texture. @@ -45,19 +44,6 @@ func newScreenFramebuffer(context *context, width, height int) *framebuffer { } } -// projectionMatrix returns a projection matrix of the framebuffer. -// -// A projection matrix converts the coodinates on the framebuffer -// (0, 0) - (viewport width, viewport height) -// to the normalized device coodinates (-1, -1) - (1, 1) with adjustment. -func (f *framebuffer) projectionMatrix() []float32 { - if f.proMatrix != nil { - return f.proMatrix - } - f.proMatrix = orthoProjectionMatrix(0, f.width, 0, f.height) - return f.proMatrix -} - func (f *framebuffer) delete(context *context) { if f.native != context.getScreenFramebuffer() { context.deleteFramebuffer(f.native) diff --git a/internal/graphicsdriver/opengl/image.go b/internal/graphicsdriver/opengl/image.go index d01d47eb8..c1eeba5f3 100644 --- a/internal/graphicsdriver/opengl/image.go +++ b/internal/graphicsdriver/opengl/image.go @@ -63,13 +63,6 @@ func (i *Image) Pixels() ([]byte, error) { return p, nil } -func (i *Image) projectionMatrix() []float32 { - if i.framebuffer == nil { - panic("not reached") - } - return i.framebuffer.projectionMatrix() -} - func (i *Image) ensureFramebuffer() error { if i.framebuffer != nil { return nil diff --git a/internal/graphicsdriver/opengl/matrix.go b/internal/graphicsdriver/opengl/matrix.go deleted file mode 100644 index 162808cb5..000000000 --- a/internal/graphicsdriver/opengl/matrix.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2018 The Ebiten Authors -// -// 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 - -// orthoProjectionMatrix returns an orthogonal projection matrix for OpenGL. -// -// The matrix converts the coodinates (left, bottom) - (right, top) to the normalized device coodinates (-1, -1) - (1, 1). -func orthoProjectionMatrix(left, right, bottom, top int) []float32 { - e11 := 2 / float32(right-left) - e22 := 2 / float32(top-bottom) - e14 := -1 * float32(right+left) / float32(right-left) - e24 := -1 * float32(top+bottom) / float32(top-bottom) - - return []float32{ - e11, 0, 0, 0, - 0, e22, 0, 0, - 0, 0, 1, 0, - e14, e24, 0, 1, - } -} diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index 618d7c300..65a6074e0 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -124,7 +124,7 @@ type openGLState struct { programScreen program lastProgram program - lastProjectionMatrix []float32 + lastViewportSize []float32 lastColorMatrix []float32 lastColorMatrixTranslation []float32 lastSourceWidth int @@ -151,7 +151,7 @@ func (s *openGLState) reset(context *context) error { } s.lastProgram = zeroProgram - s.lastProjectionMatrix = nil + s.lastViewportSize = nil s.lastColorMatrix = nil s.lastColorMatrixTranslation = nil s.lastSourceWidth = 0 @@ -266,7 +266,10 @@ func (d *Driver) useProgram(mode graphics.CompositeMode, colorM *affine.ColorM, if err := destination.setViewport(); err != nil { return err } - proj := destination.projectionMatrix() + viewportSize := []float32{ + float32(destination.framebuffer.width), + float32(destination.framebuffer.height), + } dstW := destination.width srcW, srcH := source.width, source.height @@ -298,21 +301,19 @@ func (d *Driver) useProgram(mode graphics.CompositeMode, colorM *affine.ColorM, } d.state.lastProgram = program - d.state.lastProjectionMatrix = nil + d.state.lastViewportSize = nil d.state.lastColorMatrix = nil d.state.lastColorMatrixTranslation = nil d.state.lastSourceWidth = 0 d.state.lastSourceHeight = 0 } - if !areSameFloat32Array(d.state.lastProjectionMatrix, proj) { - d.context.uniformFloats(program, "projection_matrix", proj) - if d.state.lastProjectionMatrix == nil { - d.state.lastProjectionMatrix = make([]float32, 16) + if !areSameFloat32Array(d.state.lastViewportSize, viewportSize) { + d.context.uniformFloats(program, "viewport_size", viewportSize) + if d.state.lastViewportSize == nil { + d.state.lastViewportSize = make([]float32, 2) } - // (*framebuffer).projectionMatrix is always same for the same framebuffer. - // It's OK to hold the reference without copying. - d.state.lastProjectionMatrix = proj + d.state.lastViewportSize = viewportSize } esBody, esTranslate := colorM.UnsafeElements() diff --git a/internal/graphicsdriver/opengl/shader.go b/internal/graphicsdriver/opengl/shader.go index 9a87e3dc7..41c7d4937 100644 --- a/internal/graphicsdriver/opengl/shader.go +++ b/internal/graphicsdriver/opengl/shader.go @@ -47,7 +47,7 @@ func shaderStr(id shaderID) string { const ( shaderStrVertex = ` -uniform mat4 projection_matrix; +uniform vec2 viewport_size; attribute vec4 vertex; attribute vec4 tex_coord; attribute vec4 color_scale; @@ -66,6 +66,13 @@ void main(void) { varying_tex_coord_max = vec2(1, 1); } varying_color_scale = color_scale; + + mat4 projection_matrix = mat4( + vec4(2.0 / viewport_size.x, 0, 0, 0), + vec4(0, 2.0 / viewport_size.y, 0, 0), + vec4(0, 0, 1, 0), + vec4(-1, -1, 0, 1) + ); gl_Position = projection_matrix * vertex; } `