From 155c63ec762a2b5789baca65a3fdab758924a7dd Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 24 May 2020 20:15:07 +0900 Subject: [PATCH] graphicscommand: Move the shader program to testing package --- internal/graphicscommand/image_test.go | 214 +---------------------- internal/testing/shader.go | 230 +++++++++++++++++++++++++ 2 files changed, 234 insertions(+), 210 deletions(-) create mode 100644 internal/testing/shader.go diff --git a/internal/graphicscommand/image_test.go b/internal/graphicscommand/image_test.go index cbdbadeca..f30225a15 100644 --- a/internal/graphicscommand/image_test.go +++ b/internal/graphicscommand/image_test.go @@ -21,12 +21,11 @@ import ( "github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/graphics" . "github.com/hajimehoshi/ebiten/internal/graphicscommand" - "github.com/hajimehoshi/ebiten/internal/shaderir" - t "github.com/hajimehoshi/ebiten/internal/testing" + etesting "github.com/hajimehoshi/ebiten/internal/testing" ) func TestMain(m *testing.M) { - t.MainWithRunLoop(m) + etesting.MainWithRunLoop(m) } func quadVertices(w, h float32) []float32 { @@ -92,213 +91,8 @@ func TestShader(t *testing.T) { is := graphics.QuadIndices() dst.DrawTriangles(clr, vs, is, nil, driver.CompositeModeClear, driver.FilterNearest, driver.AddressClampToZero, nil, nil) - mat := shaderir.Expr{ - Type: shaderir.Call, - Exprs: []shaderir.Expr{ - { - Type: shaderir.BuiltinFuncExpr, - BuiltinFunc: shaderir.Mat4F, - }, - { - Type: shaderir.Binary, - Op: shaderir.Div, - Exprs: []shaderir.Expr{ - { - Type: shaderir.FloatExpr, - Float: 2, - }, - { - Type: shaderir.FieldSelector, - Exprs: []shaderir.Expr{ - { - Type: shaderir.UniformVariable, - Index: 0, - }, - { - Type: shaderir.SwizzlingExpr, - Swizzling: "x", - }, - }, - }, - }, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.Binary, - Op: shaderir.Div, - Exprs: []shaderir.Expr{ - { - Type: shaderir.FloatExpr, - Float: 2, - }, - { - Type: shaderir.FieldSelector, - Exprs: []shaderir.Expr{ - { - Type: shaderir.UniformVariable, - Index: 0, - }, - { - Type: shaderir.SwizzlingExpr, - Swizzling: "y", - }, - }, - }, - }, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 1, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: -1, - }, - { - Type: shaderir.FloatExpr, - Float: -1, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 1, - }, - }, - } - pos := shaderir.Expr{ - Type: shaderir.Call, - Exprs: []shaderir.Expr{ - { - Type: shaderir.BuiltinFuncExpr, - BuiltinFunc: shaderir.Vec4F, - }, - { - Type: shaderir.LocalVariable, - Index: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 1, - }, - }, - } - red := shaderir.Expr{ - Type: shaderir.Call, - Exprs: []shaderir.Expr{ - { - Type: shaderir.BuiltinFuncExpr, - BuiltinFunc: shaderir.Vec4F, - }, - { - Type: shaderir.FloatExpr, - Float: 1, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 0, - }, - { - Type: shaderir.FloatExpr, - Float: 1, - }, - }, - } - s := NewShader(&shaderir.Program{ - Uniforms: []shaderir.Type{ - {Main: shaderir.Vec2}, - }, - Attributes: []shaderir.Type{ - {Main: shaderir.Vec2}, - {Main: shaderir.Vec2}, - {Main: shaderir.Vec4}, - {Main: shaderir.Vec4}, - }, - VertexFunc: shaderir.VertexFunc{ - Block: shaderir.Block{ - Stmts: []shaderir.Stmt{ - { - Type: shaderir.Assign, - Exprs: []shaderir.Expr{ - { - Type: shaderir.LocalVariable, - Index: 4, - }, - { - Type: shaderir.Binary, - Op: shaderir.Mul, - Exprs: []shaderir.Expr{ - mat, - pos, - }, - }, - }, - }, - }, - }, - }, - FragmentFunc: shaderir.FragmentFunc{ - Block: shaderir.Block{ - Stmts: []shaderir.Stmt{ - { - Type: shaderir.Assign, - Exprs: []shaderir.Expr{ - { - Type: shaderir.LocalVariable, - Index: 1, - }, - red, - }, - }, - }, - }, - }, - }) + ir := etesting.ShaderProgramFill(0xff, 0, 0, 0xff) + s := NewShader(&ir) us := map[int]interface{}{ 0: []float32{w, h}, } diff --git a/internal/testing/shader.go b/internal/testing/shader.go new file mode 100644 index 000000000..ab5ce03fe --- /dev/null +++ b/internal/testing/shader.go @@ -0,0 +1,230 @@ +// Copyright 2020 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 testing + +import ( + "github.com/hajimehoshi/ebiten/internal/shaderir" +) + +func ShaderProgramFill(r, g, b, a byte) shaderir.Program { + mat := shaderir.Expr{ + Type: shaderir.Call, + Exprs: []shaderir.Expr{ + { + Type: shaderir.BuiltinFuncExpr, + BuiltinFunc: shaderir.Mat4F, + }, + { + Type: shaderir.Binary, + Op: shaderir.Div, + Exprs: []shaderir.Expr{ + { + Type: shaderir.FloatExpr, + Float: 2, + }, + { + Type: shaderir.FieldSelector, + Exprs: []shaderir.Expr{ + { + Type: shaderir.UniformVariable, + Index: 0, + }, + { + Type: shaderir.SwizzlingExpr, + Swizzling: "x", + }, + }, + }, + }, + }, + { + Type: shaderir.FloatExpr, + Float: 0, + }, + { + Type: shaderir.FloatExpr, + Float: 0, + }, + { + Type: shaderir.FloatExpr, + Float: 0, + }, + { + Type: shaderir.FloatExpr, + Float: 0, + }, + { + Type: shaderir.Binary, + Op: shaderir.Div, + Exprs: []shaderir.Expr{ + { + Type: shaderir.FloatExpr, + Float: 2, + }, + { + Type: shaderir.FieldSelector, + Exprs: []shaderir.Expr{ + { + Type: shaderir.UniformVariable, + Index: 0, + }, + { + Type: shaderir.SwizzlingExpr, + Swizzling: "y", + }, + }, + }, + }, + }, + { + Type: shaderir.FloatExpr, + Float: 0, + }, + { + Type: shaderir.FloatExpr, + Float: 0, + }, + { + Type: shaderir.FloatExpr, + Float: 0, + }, + { + Type: shaderir.FloatExpr, + Float: 0, + }, + { + Type: shaderir.FloatExpr, + Float: 1, + }, + { + Type: shaderir.FloatExpr, + Float: 0, + }, + { + Type: shaderir.FloatExpr, + Float: -1, + }, + { + Type: shaderir.FloatExpr, + Float: -1, + }, + { + Type: shaderir.FloatExpr, + Float: 0, + }, + { + Type: shaderir.FloatExpr, + Float: 1, + }, + }, + } + pos := shaderir.Expr{ + Type: shaderir.Call, + Exprs: []shaderir.Expr{ + { + Type: shaderir.BuiltinFuncExpr, + BuiltinFunc: shaderir.Vec4F, + }, + { + Type: shaderir.LocalVariable, + Index: 0, + }, + { + Type: shaderir.FloatExpr, + Float: 0, + }, + { + Type: shaderir.FloatExpr, + Float: 1, + }, + }, + } + clr := shaderir.Expr{ + Type: shaderir.Call, + Exprs: []shaderir.Expr{ + { + Type: shaderir.BuiltinFuncExpr, + BuiltinFunc: shaderir.Vec4F, + }, + { + Type: shaderir.FloatExpr, + Float: float32(r) / 0xff, + }, + { + Type: shaderir.FloatExpr, + Float: float32(g) / 0xff, + }, + { + Type: shaderir.FloatExpr, + Float: float32(b) / 0xff, + }, + { + Type: shaderir.FloatExpr, + Float: float32(a) / 0xff, + }, + }, + } + + return shaderir.Program{ + Uniforms: []shaderir.Type{ + {Main: shaderir.Vec2}, + }, + Attributes: []shaderir.Type{ + {Main: shaderir.Vec2}, + {Main: shaderir.Vec2}, + {Main: shaderir.Vec4}, + {Main: shaderir.Vec4}, + }, + VertexFunc: shaderir.VertexFunc{ + Block: shaderir.Block{ + Stmts: []shaderir.Stmt{ + { + Type: shaderir.Assign, + Exprs: []shaderir.Expr{ + { + Type: shaderir.LocalVariable, + Index: 4, + }, + { + Type: shaderir.Binary, + Op: shaderir.Mul, + Exprs: []shaderir.Expr{ + mat, + pos, + }, + }, + }, + }, + }, + }, + }, + FragmentFunc: shaderir.FragmentFunc{ + Block: shaderir.Block{ + Stmts: []shaderir.Stmt{ + { + Type: shaderir.Assign, + Exprs: []shaderir.Expr{ + { + Type: shaderir.LocalVariable, + Index: 1, + }, + clr, + }, + }, + }, + }, + }, + } +}