2020-05-24 13:15:07 +02:00
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
2020-05-24 15:15:50 +02:00
|
|
|
var (
|
|
|
|
projectionMatrix = shaderir.Expr{
|
2020-05-24 13:15:07 +02:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-05-24 15:15:50 +02:00
|
|
|
vertexPosition = shaderir.Expr{
|
2020-05-24 13:15:07 +02:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-05-24 15:15:50 +02:00
|
|
|
defaultVertexFunc = shaderir.VertexFunc{
|
|
|
|
Block: shaderir.Block{
|
|
|
|
Stmts: []shaderir.Stmt{
|
|
|
|
{
|
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
{
|
|
|
|
Type: shaderir.LocalVariable,
|
2020-06-02 17:46:52 +02:00
|
|
|
Index: 5, // the varying variable
|
2020-05-24 10:14:37 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: shaderir.LocalVariable,
|
|
|
|
Index: 1, // the 2nd attribute variable
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
{
|
|
|
|
Type: shaderir.LocalVariable,
|
2020-06-02 17:46:52 +02:00
|
|
|
Index: 4, // gl_Position in GLSL
|
2020-05-24 15:15:50 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: shaderir.Binary,
|
|
|
|
Op: shaderir.Mul,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
projectionMatrix,
|
|
|
|
vertexPosition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-05-24 10:14:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func defaultProgram() shaderir.Program {
|
|
|
|
return shaderir.Program{
|
2020-05-24 15:15:50 +02:00
|
|
|
Uniforms: []shaderir.Type{
|
2020-06-04 20:00:43 +02:00
|
|
|
{Main: shaderir.Vec2}, // Viewport size. This must be the first uniform variable, and the values are set at graphicscommand driver.
|
2020-05-24 15:15:50 +02:00
|
|
|
},
|
|
|
|
Attributes: []shaderir.Type{
|
2020-05-24 10:14:37 +02:00
|
|
|
{Main: shaderir.Vec2}, // Local var (0) in the vertex shader
|
|
|
|
{Main: shaderir.Vec2}, // Local var (1) in the vertex shader
|
|
|
|
{Main: shaderir.Vec4}, // Local var (2) in the vertex shader
|
|
|
|
{Main: shaderir.Vec4}, // Local var (3) in the vertex shader
|
|
|
|
},
|
|
|
|
Varyings: []shaderir.Type{
|
|
|
|
{Main: shaderir.Vec2}, // Local var (4) in the vertex shader, (0) in the fragment shader
|
2020-05-24 15:15:50 +02:00
|
|
|
},
|
|
|
|
VertexFunc: defaultVertexFunc,
|
|
|
|
}
|
2020-05-24 10:14:37 +02:00
|
|
|
}
|
2020-05-24 15:15:50 +02:00
|
|
|
|
|
|
|
// ShaderProgramFill returns a shader intermediate representation to fill the frambuffer.
|
|
|
|
//
|
2020-05-26 15:50:33 +02:00
|
|
|
// Uniform variable's index and its value are:
|
2020-05-24 15:15:50 +02:00
|
|
|
//
|
2020-05-24 10:14:37 +02:00
|
|
|
// 0: the framebuffer size (Vec2)
|
2020-05-24 15:15:50 +02:00
|
|
|
func ShaderProgramFill(r, g, b, a byte) shaderir.Program {
|
2020-05-24 13:15:07 +02:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-05-24 10:14:37 +02:00
|
|
|
p := defaultProgram()
|
2020-05-24 15:15:50 +02:00
|
|
|
p.FragmentFunc = shaderir.FragmentFunc{
|
|
|
|
Block: shaderir.Block{
|
|
|
|
Stmts: []shaderir.Stmt{
|
|
|
|
{
|
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
{
|
|
|
|
Type: shaderir.LocalVariable,
|
2020-05-24 10:14:37 +02:00
|
|
|
Index: 2,
|
2020-05-24 13:15:07 +02:00
|
|
|
},
|
2020-05-24 15:15:50 +02:00
|
|
|
clr,
|
2020-05-24 13:15:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-05-24 15:15:50 +02:00
|
|
|
|
|
|
|
return p
|
2020-05-24 13:15:07 +02:00
|
|
|
}
|
2020-05-24 10:14:37 +02:00
|
|
|
|
|
|
|
// ShaderProgramImages returns a shader intermediate representation to render the frambuffer with the given images.
|
|
|
|
//
|
2020-05-26 15:50:33 +02:00
|
|
|
// Uniform variables's indices and their values are:
|
2020-05-24 10:14:37 +02:00
|
|
|
//
|
|
|
|
// 0: the framebuffer size (Vec2)
|
2020-05-30 10:48:56 +02:00
|
|
|
// 1: the first images (Texture2D)
|
|
|
|
// 2n: the n-th image (Texture2D)
|
2020-05-29 22:16:50 +02:00
|
|
|
// 2n+1: the n-th image's region (Vec4)
|
2020-05-24 10:14:37 +02:00
|
|
|
//
|
|
|
|
// The first image's size and region are represented in attribute variables.
|
|
|
|
//
|
|
|
|
// The size and region values are actually not used in this shader so far.
|
|
|
|
func ShaderProgramImages(imageNum int) shaderir.Program {
|
|
|
|
if imageNum <= 0 {
|
|
|
|
panic("testing: imageNum must be >= 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
p := defaultProgram()
|
|
|
|
|
|
|
|
for i := 0; i < imageNum; i++ {
|
2020-05-30 10:48:56 +02:00
|
|
|
p.Uniforms = append(p.Uniforms, shaderir.Type{Main: shaderir.Texture2D})
|
2020-05-24 10:14:37 +02:00
|
|
|
if i > 0 {
|
|
|
|
p.Uniforms = append(p.Uniforms, shaderir.Type{Main: shaderir.Vec4})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the fragment shader, local variables are:
|
|
|
|
//
|
2020-06-02 19:01:50 +02:00
|
|
|
// 0: gl_FragCoord
|
|
|
|
// 1: Varying variables (vec2)
|
2020-05-24 10:14:37 +02:00
|
|
|
// 2: gl_FragColor
|
|
|
|
// 3: Actual local variables in the main function
|
|
|
|
|
|
|
|
local := shaderir.Expr{
|
|
|
|
Type: shaderir.LocalVariable,
|
|
|
|
Index: 3,
|
|
|
|
}
|
|
|
|
fragColor := shaderir.Expr{
|
|
|
|
Type: shaderir.LocalVariable,
|
|
|
|
Index: 2,
|
|
|
|
}
|
|
|
|
texPos := shaderir.Expr{
|
|
|
|
Type: shaderir.LocalVariable,
|
2020-06-02 19:01:50 +02:00
|
|
|
Index: 1,
|
2020-05-24 10:14:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var stmts []shaderir.Stmt
|
|
|
|
for i := 0; i < imageNum; i++ {
|
|
|
|
var rhs shaderir.Expr
|
|
|
|
if i == 0 {
|
|
|
|
rhs = shaderir.Expr{
|
|
|
|
Type: shaderir.Call,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
{
|
|
|
|
Type: shaderir.BuiltinFuncExpr,
|
2020-05-30 10:48:56 +02:00
|
|
|
BuiltinFunc: shaderir.Texture2DF,
|
2020-05-24 10:14:37 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: shaderir.UniformVariable,
|
|
|
|
Index: 1,
|
|
|
|
},
|
|
|
|
texPos,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rhs = shaderir.Expr{
|
|
|
|
Type: shaderir.Binary,
|
|
|
|
Op: shaderir.Add,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
local,
|
|
|
|
{
|
|
|
|
Type: shaderir.Call,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
{
|
|
|
|
Type: shaderir.BuiltinFuncExpr,
|
2020-05-30 10:48:56 +02:00
|
|
|
BuiltinFunc: shaderir.Texture2DF,
|
2020-05-24 10:14:37 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: shaderir.UniformVariable,
|
2020-05-29 22:16:50 +02:00
|
|
|
Index: 2 * i,
|
2020-05-24 10:14:37 +02:00
|
|
|
},
|
|
|
|
texPos,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
local,
|
|
|
|
rhs,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
fragColor,
|
|
|
|
local,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
p.FragmentFunc = shaderir.FragmentFunc{
|
|
|
|
Block: shaderir.Block{
|
|
|
|
LocalVars: []shaderir.Type{
|
|
|
|
{Main: shaderir.Vec4},
|
|
|
|
},
|
|
|
|
Stmts: stmts,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|