ebiten/internal/shaderir/ir_test.go

711 lines
12 KiB
Go
Raw Normal View History

2020-05-11 17:19:42 +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 shaderir_test
import (
"testing"
. "github.com/hajimehoshi/ebiten/internal/shaderir"
)
2020-05-14 20:33:07 +02:00
func block(localVars []Type, stmts ...Stmt) Block {
return Block{
LocalVars: localVars,
Stmts: stmts,
}
}
2020-05-16 12:10:12 +02:00
func exprStmt(expr Expr) Stmt {
return Stmt{
Type: ExprStmt,
Exprs: []Expr{expr},
}
}
2020-05-14 20:33:07 +02:00
func blockStmt(block Block) Stmt {
return Stmt{
Type: BlockStmt,
Blocks: []Block{block},
}
}
2020-05-16 13:16:04 +02:00
func returnStmt(expr Expr) Stmt {
return Stmt{
Type: Return,
Exprs: []Expr{expr},
}
}
2020-05-14 16:47:15 +02:00
func assignStmt(lhs Expr, rhs Expr) Stmt {
return Stmt{
Type: Assign,
Exprs: []Expr{lhs, rhs},
}
}
2020-05-14 20:12:23 +02:00
func ifStmt(cond Expr, block Block, elseBlock Block) Stmt {
2020-05-14 19:10:07 +02:00
return Stmt{
2020-05-14 20:12:23 +02:00
Type: If,
Exprs: []Expr{cond},
Blocks: []Block{block, elseBlock},
2020-05-14 19:10:07 +02:00
}
}
func forStmt(init, end int, op Op, delta int, block Block) Stmt {
2020-05-15 20:10:03 +02:00
return Stmt{
Type: For,
Blocks: []Block{block},
ForInit: init,
ForEnd: end,
ForOp: op,
2020-05-15 20:10:03 +02:00
ForDelta: delta,
}
}
2020-05-15 20:40:33 +02:00
func floatExpr(value float32) Expr {
2020-05-14 19:10:07 +02:00
return Expr{
2020-05-15 20:40:33 +02:00
Type: FloatExpr,
Float: value,
2020-05-14 19:10:07 +02:00
}
}
func uniformVariableExpr(index int) Expr {
2020-05-14 16:47:15 +02:00
return Expr{
Type: UniformVariable,
Index: index,
}
}
func localVariableExpr(index int) Expr {
return Expr{
Type: LocalVariable,
Index: index,
2020-05-14 16:47:15 +02:00
}
}
2020-05-16 17:25:31 +02:00
func builtinFuncExpr(f BuiltinFunc) Expr {
return Expr{
Type: BuiltinFuncExpr,
BuiltinFunc: f,
}
}
2020-05-16 19:24:35 +02:00
func swizzlingExpr(swizzling string) Expr {
return Expr{
2020-05-16 20:00:57 +02:00
Type: SwizzlingExpr,
Swizzling: swizzling,
2020-05-16 19:24:35 +02:00
}
}
2020-05-16 20:00:57 +02:00
func functionExpr(index int) Expr {
2020-05-16 11:09:04 +02:00
return Expr{
2020-05-16 20:00:57 +02:00
Type: FunctionExpr,
Index: index,
2020-05-16 11:09:04 +02:00
}
}
2020-05-14 16:47:15 +02:00
func binaryExpr(op Op, exprs ...Expr) Expr {
return Expr{
Type: Binary,
Op: op,
Exprs: exprs,
}
}
2020-05-16 10:22:17 +02:00
func selectionExpr(cond, a, b Expr) Expr {
return Expr{
Type: Selection,
Exprs: []Expr{cond, a, b},
}
}
2020-05-16 17:25:31 +02:00
func callExpr(callee Expr, args ...Expr) Expr {
2020-05-16 12:10:12 +02:00
return Expr{
Type: Call,
2020-05-16 17:25:31 +02:00
Exprs: append([]Expr{callee}, args...),
2020-05-16 12:10:12 +02:00
}
}
2020-05-16 11:09:04 +02:00
func fieldSelectorExpr(a, b Expr) Expr {
return Expr{
Type: FieldSelector,
Exprs: []Expr{a, b},
}
}
2020-05-11 17:19:42 +02:00
func TestOutput(t *testing.T) {
tests := []struct {
Name string
Program Program
Glsl string
}{
{
Name: "Empty",
Program: Program{},
Glsl: ``,
},
{
Name: "Uniform",
Program: Program{
2020-05-14 16:47:15 +02:00
Uniforms: []Type{
{Main: Float},
2020-05-11 17:19:42 +02:00
},
},
2020-05-12 16:32:32 +02:00
Glsl: `uniform float U0;`,
2020-05-11 17:19:42 +02:00
},
{
Name: "UniformStruct",
Program: Program{
2020-05-14 16:47:15 +02:00
Uniforms: []Type{
2020-05-11 17:19:42 +02:00
{
2020-05-14 16:47:15 +02:00
Main: Struct,
Sub: []Type{
{Main: Float},
2020-05-11 17:19:42 +02:00
},
},
},
},
Glsl: `struct S0 {
float M0;
};
2020-05-12 16:32:32 +02:00
uniform S0 U0;`,
},
{
Name: "Vars",
Program: Program{
2020-05-14 16:47:15 +02:00
Uniforms: []Type{
{Main: Float},
2020-05-12 16:32:32 +02:00
},
2020-05-14 16:47:15 +02:00
Attributes: []Type{
{Main: Vec2},
2020-05-12 16:32:32 +02:00
},
2020-05-14 16:47:15 +02:00
Varyings: []Type{
{Main: Vec3},
2020-05-12 16:32:32 +02:00
},
},
Glsl: `uniform float U0;
attribute vec2 A0;
varying vec3 V0;`,
},
{
2020-05-13 16:31:17 +02:00
Name: "Func",
2020-05-12 16:32:32 +02:00
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
2020-05-12 16:32:32 +02:00
},
},
},
Glsl: `void F0(void) {
2020-05-13 16:31:17 +02:00
}`,
},
{
Name: "FuncParams",
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
2020-05-13 16:31:17 +02:00
InParams: []Type{
{Main: Float},
{Main: Vec2},
{Main: Vec4},
},
InOutParams: []Type{
{Main: Mat2},
},
OutParams: []Type{
{Main: Mat4},
},
},
},
},
Glsl: `void F0(in float l0, in vec2 l1, in vec4 l2, inout mat2 l3, out mat4 l4) {
2020-05-16 13:16:04 +02:00
}`,
},
{
Name: "FuncReturn",
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
2020-05-16 13:16:04 +02:00
InParams: []Type{
{Main: Float},
},
Return: Type{Main: Float},
Block: block(
nil,
returnStmt(
localVariableExpr(0),
2020-05-16 13:16:04 +02:00
),
),
},
},
},
Glsl: `float F0(in float l0) {
return l0;
2020-05-13 17:46:36 +02:00
}`,
},
{
Name: "FuncLocals",
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
2020-05-13 17:46:36 +02:00
InParams: []Type{
{Main: Float},
},
InOutParams: []Type{
{Main: Float},
},
OutParams: []Type{
{Main: Float},
},
2020-05-14 20:33:07 +02:00
Block: block([]Type{
{Main: Mat4},
{Main: Mat4},
}),
2020-05-13 17:46:36 +02:00
},
},
},
Glsl: `void F0(in float l0, inout float l1, out float l2) {
mat4 l3;
mat4 l4;
}`,
},
{
Name: "FuncBlocks",
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
InParams: []Type{
{Main: Float},
},
InOutParams: []Type{
{Main: Float},
},
OutParams: []Type{
{Main: Float},
},
2020-05-14 20:33:07 +02:00
Block: block(
[]Type{
{Main: Mat4},
{Main: Mat4},
},
2020-05-14 20:33:07 +02:00
blockStmt(
block(
[]Type{
{Main: Mat4},
{Main: Mat4},
},
2020-05-14 20:33:07 +02:00
),
),
),
},
},
},
Glsl: `void F0(in float l0, inout float l1, out float l2) {
mat4 l3;
mat4 l4;
{
mat4 l5;
mat4 l6;
}
2020-05-13 18:45:33 +02:00
}`,
},
{
2020-05-16 10:22:17 +02:00
Name: "Add",
2020-05-13 18:45:33 +02:00
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
2020-05-13 18:45:33 +02:00
InParams: []Type{
{Main: Float},
{Main: Float},
},
OutParams: []Type{
{Main: Float},
},
2020-05-14 20:33:07 +02:00
Block: block(
nil,
assignStmt(
localVariableExpr(2),
2020-05-14 20:33:07 +02:00
binaryExpr(
Add,
localVariableExpr(0),
localVariableExpr(1),
2020-05-14 16:47:15 +02:00
),
2020-05-14 20:33:07 +02:00
),
),
2020-05-13 18:45:33 +02:00
},
},
},
Glsl: `void F0(in float l0, in float l1, out float l2) {
l2 = (l0) + (l1);
2020-05-14 19:10:07 +02:00
}`,
},
{
2020-05-16 10:22:17 +02:00
Name: "Selection",
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
2020-05-16 10:22:17 +02:00
InParams: []Type{
{Main: Bool},
{Main: Float},
{Main: Float},
},
OutParams: []Type{
{Main: Float},
},
Block: block(
nil,
assignStmt(
localVariableExpr(3),
2020-05-16 10:22:17 +02:00
selectionExpr(
localVariableExpr(0),
localVariableExpr(1),
localVariableExpr(2),
2020-05-16 10:22:17 +02:00
),
),
),
},
},
},
Glsl: `void F0(in bool l0, in float l1, in float l2, out float l3) {
l3 = (l0) ? (l1) : (l2);
2020-05-16 12:10:12 +02:00
}`,
},
{
Name: "Call",
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
2020-05-16 12:10:12 +02:00
InParams: []Type{
{Main: Float},
{Main: Float},
},
OutParams: []Type{
{Main: Vec2},
},
Block: block(
nil,
exprStmt(
callExpr(
2020-05-16 20:00:57 +02:00
functionExpr(1),
2020-05-16 12:10:12 +02:00
),
),
assignStmt(
localVariableExpr(2),
2020-05-16 12:10:12 +02:00
callExpr(
2020-05-16 20:00:57 +02:00
functionExpr(2),
localVariableExpr(0),
localVariableExpr(1),
2020-05-16 12:10:12 +02:00
),
),
),
},
},
},
Glsl: `void F0(in float l0, in float l1, out vec2 l2) {
2020-05-16 17:25:31 +02:00
(F1)();
l2 = (F2)(l0, l1);
}`,
},
{
Name: "BuiltinFunc",
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
2020-05-16 17:25:31 +02:00
InParams: []Type{
{Main: Float},
{Main: Float},
},
OutParams: []Type{
{Main: Float},
},
Block: block(
nil,
assignStmt(
localVariableExpr(2),
2020-05-16 17:25:31 +02:00
callExpr(
builtinFuncExpr(Min),
localVariableExpr(0),
localVariableExpr(1),
2020-05-16 17:25:31 +02:00
),
),
),
},
},
},
Glsl: `void F0(in float l0, in float l1, out float l2) {
l2 = (min)(l0, l1);
2020-05-16 11:09:04 +02:00
}`,
},
{
Name: "FieldSelector",
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
2020-05-16 11:09:04 +02:00
InParams: []Type{
{Main: Vec4},
},
OutParams: []Type{
2020-05-16 19:24:35 +02:00
{Main: Vec2},
2020-05-16 11:09:04 +02:00
},
Block: block(
nil,
assignStmt(
localVariableExpr(1),
2020-05-16 11:09:04 +02:00
fieldSelectorExpr(
localVariableExpr(0),
2020-05-16 19:24:35 +02:00
swizzlingExpr("xz"),
2020-05-16 11:09:04 +02:00
),
),
),
},
},
},
2020-05-16 19:24:35 +02:00
Glsl: `void F0(in vec4 l0, out vec2 l1) {
l1 = (l0).xz;
2020-05-16 10:22:17 +02:00
}`,
},
{
Name: "If",
2020-05-14 19:10:07 +02:00
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
2020-05-14 19:10:07 +02:00
InParams: []Type{
{Main: Float},
{Main: Float},
},
OutParams: []Type{
{Main: Float},
},
2020-05-14 20:33:07 +02:00
Block: block(
nil,
ifStmt(
binaryExpr(
2020-05-16 17:25:31 +02:00
EqualOp,
localVariableExpr(0),
2020-05-15 20:40:33 +02:00
floatExpr(0),
2020-05-14 20:33:07 +02:00
),
block(
nil,
assignStmt(
localVariableExpr(2),
localVariableExpr(0),
2020-05-14 19:10:07 +02:00
),
),
2020-05-14 20:33:07 +02:00
block(
nil,
assignStmt(
localVariableExpr(2),
localVariableExpr(1),
2020-05-14 20:33:07 +02:00
),
),
),
),
2020-05-14 19:10:07 +02:00
},
},
},
Glsl: `void F0(in float l0, in float l1, out float l2) {
if ((l0) == (0.000000000e+00)) {
l2 = l0;
} else {
l2 = l1;
}
2020-05-15 20:10:03 +02:00
}`,
},
{
2020-05-16 10:22:17 +02:00
Name: "For",
2020-05-15 20:10:03 +02:00
Program: Program{
Funcs: []Func{
{
2020-05-16 20:00:57 +02:00
Index: 0,
2020-05-15 20:10:03 +02:00
InParams: []Type{
{Main: Float},
{Main: Float},
},
OutParams: []Type{
{Main: Float},
},
Block: block(
nil,
forStmt(
0,
100,
2020-05-16 17:25:31 +02:00
LessThanOp,
2020-05-15 20:10:03 +02:00
1,
block(
nil,
assignStmt(
localVariableExpr(2),
localVariableExpr(0),
2020-05-15 20:10:03 +02:00
),
),
),
),
},
},
},
Glsl: `void F0(in float l0, in float l1, out float l2) {
for (int l3 = 0; l3 < 100; l3++) {
l2 = l0;
}
2020-05-16 15:18:58 +02:00
}`,
},
{
Name: "VertexFunc",
Program: Program{
Uniforms: []Type{
{Main: Float},
},
Attributes: []Type{
{Main: Vec4},
{Main: Float},
{Main: Vec2},
},
Varyings: []Type{
{Main: Float},
{Main: Vec2},
},
VertexFunc: VertexFunc{
Block: block(
nil,
assignStmt(
localVariableExpr(5),
localVariableExpr(0),
2020-05-16 15:18:58 +02:00
),
assignStmt(
localVariableExpr(3),
localVariableExpr(1),
2020-05-16 15:18:58 +02:00
),
assignStmt(
localVariableExpr(4),
localVariableExpr(2),
2020-05-16 15:18:58 +02:00
),
),
},
},
Glsl: `uniform float U0;
attribute vec4 A0;
attribute float A1;
attribute vec2 A2;
varying float V0;
varying vec2 V1;
2020-05-17 10:06:21 +02:00
2020-05-16 16:29:01 +02:00
#if defined(COMPILING_VERTEX_SHADER)
2020-05-16 15:18:58 +02:00
void main(void) {
gl_Position = A0;
V0 = A1;
V1 = A2;
2020-05-16 16:29:01 +02:00
}
#endif`,
2020-05-16 16:07:24 +02:00
},
{
Name: "FragmentFunc",
Program: Program{
Uniforms: []Type{
{Main: Float},
},
Attributes: []Type{
{Main: Vec4},
{Main: Float},
{Main: Vec2},
},
Varyings: []Type{
{Main: Float},
{Main: Vec2},
},
VertexFunc: VertexFunc{
Block: block(
nil,
assignStmt(
localVariableExpr(5),
localVariableExpr(0),
2020-05-16 16:07:24 +02:00
),
assignStmt(
localVariableExpr(3),
localVariableExpr(1),
2020-05-16 16:07:24 +02:00
),
assignStmt(
localVariableExpr(4),
localVariableExpr(2),
2020-05-16 16:07:24 +02:00
),
),
},
FragmentFunc: FragmentFunc{
Block: block(
[]Type{
{Main: Vec2},
{Main: Vec4},
{Main: Float},
},
assignStmt(
localVariableExpr(5),
localVariableExpr(0),
2020-05-16 16:07:24 +02:00
),
assignStmt(
localVariableExpr(3),
localVariableExpr(1),
2020-05-16 16:07:24 +02:00
),
assignStmt(
localVariableExpr(4),
localVariableExpr(2),
2020-05-16 16:07:24 +02:00
),
),
},
},
Glsl: `uniform float U0;
attribute vec4 A0;
attribute float A1;
attribute vec2 A2;
varying float V0;
varying vec2 V1;
2020-05-17 10:06:21 +02:00
2020-05-16 16:29:01 +02:00
#if defined(COMPILING_VERTEX_SHADER)
2020-05-16 16:07:24 +02:00
void main(void) {
gl_Position = A0;
V0 = A1;
V1 = A2;
}
2020-05-16 16:29:01 +02:00
#endif
2020-05-17 10:06:21 +02:00
2020-05-16 16:29:01 +02:00
#if defined(COMPILING_FRAGMENT_SHADER)
2020-05-16 16:07:24 +02:00
void main(void) {
vec2 l0;
vec4 l1;
float l2;
l2 = V0;
l0 = V1;
l1 = gl_FragCoord;
2020-05-16 16:29:01 +02:00
}
#endif`,
2020-05-11 17:19:42 +02:00
},
}
for _, tc := range tests {
got := tc.Program.Glsl()
want := tc.Glsl + "\n"
if got != want {
t.Errorf("%s: got: %s, want: %s", tc.Name, got, want)
}
}
}