From 02db3bad530c144a77f732f661c2e2180f4a27f0 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 4 Apr 2022 01:55:51 +0900 Subject: [PATCH] internal/graphicscommand: remove the dependency on a graphics driver from compileShader Updates #2035 --- internal/graphics/vertex.go | 4 ++- internal/graphicscommand/command.go | 2 +- internal/graphicscommand/shader.go | 25 +++---------------- .../directx/graphics_windows.go | 6 +++++ .../graphicsdriver/metal/graphics_darwin.go | 7 ++++++ internal/graphicsdriver/opengl/graphics.go | 11 ++++++++ 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/internal/graphics/vertex.go b/internal/graphics/vertex.go index efd495414..64819a7a4 100644 --- a/internal/graphics/vertex.go +++ b/internal/graphics/vertex.go @@ -29,7 +29,8 @@ const ( 1 + // the texture destination region's size 1 + // the offsets array of the second and the following images 1 + // the texture source region's origin - 1 // the texture source region's size + 1 + // the texture source region's size + 1 // the projection matrix TextureDestinationSizeUniformVariableIndex = 0 TextureSourceSizesUniformVariableIndex = 1 @@ -38,6 +39,7 @@ const ( TextureSourceOffsetsUniformVariableIndex = 4 TextureSourceRegionOriginUniformVariableIndex = 5 TextureSourceRegionSizeUniformVariableIndex = 6 + ProjectionMatrixUniformVariableIndex = 7 ) const ( diff --git a/internal/graphicscommand/command.go b/internal/graphicscommand/command.go index 0bdd00fcd..c2c2016fd 100644 --- a/internal/graphicscommand/command.go +++ b/internal/graphicscommand/command.go @@ -666,7 +666,7 @@ func (c *newShaderCommand) String() string { // Exec executes a newShaderCommand. func (c *newShaderCommand) Exec(graphicsDriver graphicsdriver.Graphics, indexOffset int) error { - ir, err := compileShader(graphicsDriver, c.src) + ir, err := compileShader(c.src) if err != nil { return err } diff --git a/internal/graphicscommand/shader.go b/internal/graphicscommand/shader.go index 05ff7c61c..f0454b955 100644 --- a/internal/graphicscommand/shader.go +++ b/internal/graphicscommand/shader.go @@ -106,33 +106,16 @@ func imageSrc%[1]dAt(pos vec2) vec4 { } } -func compileShader(graphicsDriver graphicsdriver.Graphics, src []byte) (*shaderir.Program, error) { +func compileShader(src []byte) (*shaderir.Program, error) { var buf bytes.Buffer buf.Write(src) buf.WriteString(shaderSuffix) - if graphicsDriver.FramebufferYDirection() != graphicsDriver.NDCYDirection() { - buf.WriteString(` + buf.WriteString(`var __projectionMatrix mat4 + func __vertex(position vec2, texCoord vec2, color vec4) (vec4, vec2, vec4) { - return mat4( - 2/__imageDstTextureSize.x, 0, 0, 0, - 0, -2/__imageDstTextureSize.y, 0, 0, - 0, 0, 1, 0, - -1, 1, 0, 1, - ) * vec4(position, 0, 1), texCoord, color + return __projectionMatrix * vec4(position, 0, 1), texCoord, color } `) - } else { - buf.WriteString(` -func __vertex(position vec2, texCoord vec2, color vec4) (vec4, vec2, vec4) { - return mat4( - 2/__imageDstTextureSize.x, 0, 0, 0, - 0, 2/__imageDstTextureSize.y, 0, 0, - 0, 0, 1, 0, - -1, -1, 0, 1, - ) * vec4(position, 0, 1), texCoord, color -} -`) - } fs := token.NewFileSet() f, err := parser.ParseFile(fs, "", buf.Bytes(), parser.AllErrors) diff --git a/internal/graphicsdriver/directx/graphics_windows.go b/internal/graphicsdriver/directx/graphics_windows.go index ce005507a..b7189b159 100644 --- a/internal/graphicsdriver/directx/graphics_windows.go +++ b/internal/graphicsdriver/directx/graphics_windows.go @@ -1056,6 +1056,12 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcs [graphics.Sh us[graphics.TextureSourceRegionOriginUniformVariableIndex] = usorigin ussize := []float32{float32(srcRegion.Width), float32(srcRegion.Height)} us[graphics.TextureSourceRegionSizeUniformVariableIndex] = ussize + us[graphics.ProjectionMatrixUniformVariableIndex] = []float32{ + 2 / float32(dw), 0, 0, 0, + 0, -2 / float32(dh), 0, 0, + 0, 0, 1, 0, + -1, 1, 0, 1, + } for i, u := range uniforms { us[graphics.PreservedUniformVariablesNum+i] = u diff --git a/internal/graphicsdriver/metal/graphics_darwin.go b/internal/graphicsdriver/metal/graphics_darwin.go index 992e2d669..4d51ddb04 100644 --- a/internal/graphicsdriver/metal/graphics_darwin.go +++ b/internal/graphicsdriver/metal/graphics_darwin.go @@ -995,6 +995,13 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics. ussize := []float32{float32(srcRegion.Width), float32(srcRegion.Height)} uniformVars[graphics.TextureSourceRegionSizeUniformVariableIndex] = ussize + uniformVars[graphics.ProjectionMatrixUniformVariableIndex] = []float32{ + 2 / float32(dw), 0, 0, 0, + 0, -2 / float32(dh), 0, 0, + 0, 0, 1, 0, + -1, 1, 0, 1, + } + // Set the additional uniform variables. for i, v := range uniforms { const offset = graphics.PreservedUniformVariablesNum diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index f804bdfeb..76504c408 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -320,6 +320,17 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics. g.uniformVars[idx].value = size g.uniformVars[idx].typ = shader.ir.Uniforms[idx] } + { + const idx = graphics.ProjectionMatrixUniformVariableIndex + g.uniformVars[idx].name = g.uniformVariableName(idx) + g.uniformVars[idx].value = []float32{ + 2 / float32(dw), 0, 0, 0, + 0, 2 / float32(dh), 0, 0, + 0, 0, 1, 0, + -1, -1, 0, 1, + } + g.uniformVars[idx].typ = shader.ir.Uniforms[idx] + } for i, v := range uniforms { const offset = graphics.PreservedUniformVariablesNum