2020-05-08 17:46:01 +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 shader_test
|
|
|
|
|
|
|
|
import (
|
2020-06-06 17:52:43 +02:00
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2020-05-08 17:46:01 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/hajimehoshi/ebiten/internal/shader"
|
|
|
|
)
|
|
|
|
|
2020-05-09 10:19:05 +02:00
|
|
|
func TestDump(t *testing.T) {
|
2020-05-08 17:46:01 +02:00
|
|
|
tests := []struct {
|
2020-05-10 12:48:24 +02:00
|
|
|
Name string
|
2020-05-10 13:41:43 +02:00
|
|
|
Src string
|
2020-05-30 10:03:43 +02:00
|
|
|
VS string
|
|
|
|
FS string
|
2020-05-08 17:46:01 +02:00
|
|
|
}{
|
2020-05-30 18:56:07 +02:00
|
|
|
{
|
|
|
|
Name: "uniforms",
|
|
|
|
Src: `package main
|
|
|
|
|
|
|
|
var (
|
|
|
|
Foo vec2
|
|
|
|
Boo vec4
|
|
|
|
)`,
|
|
|
|
VS: `uniform vec2 U0;
|
|
|
|
uniform vec4 U1;`,
|
|
|
|
},
|
2020-05-31 09:20:36 +02:00
|
|
|
{
|
|
|
|
Name: "func",
|
|
|
|
Src: `package main
|
|
|
|
|
|
|
|
func Foo(foo vec2) vec4 {
|
|
|
|
}`,
|
|
|
|
VS: `void F0(in vec2 l0, out vec4 l1) {
|
2020-05-31 11:01:12 +02:00
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "func body",
|
|
|
|
Src: `package main
|
|
|
|
|
|
|
|
func Foo(foo vec2) vec4 {
|
2020-05-31 12:20:53 +02:00
|
|
|
return vec4(foo, 0, 1)
|
2020-05-31 11:01:12 +02:00
|
|
|
}`,
|
|
|
|
VS: `void F0(in vec2 l0, out vec4 l1) {
|
2020-06-02 14:45:33 +02:00
|
|
|
l1 = vec4(l0, 0.0, 1.0);
|
2020-05-31 11:01:12 +02:00
|
|
|
return;
|
2020-06-07 09:21:02 +02:00
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "var init",
|
|
|
|
Src: `package main
|
|
|
|
|
|
|
|
func Foo(foo vec2) vec4 {
|
|
|
|
var ret vec4 = vec4(foo, 0, 1)
|
|
|
|
return ret
|
|
|
|
}`,
|
|
|
|
VS: `void F0(in vec2 l0, out vec4 l1) {
|
|
|
|
vec4 l2 = vec4(0.0);
|
|
|
|
l2 = vec4(l0, 0.0, 1.0);
|
|
|
|
l1 = l2;
|
|
|
|
return;
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "var init 2",
|
|
|
|
Src: `package main
|
|
|
|
|
|
|
|
func Foo(foo vec2) vec4 {
|
|
|
|
var bar1 vec4 = vec4(foo, 0, 1)
|
|
|
|
bar1.x = bar1.x
|
|
|
|
var bar2 vec4 = bar1
|
|
|
|
return bar2
|
|
|
|
}`,
|
|
|
|
VS: `void F0(in vec2 l0, out vec4 l1) {
|
|
|
|
vec4 l2 = vec4(0.0);
|
|
|
|
vec4 l3 = vec4(0.0);
|
|
|
|
l2 = vec4(l0, 0.0, 1.0);
|
|
|
|
(l2).x = (l2).x;
|
|
|
|
l3 = l2;
|
|
|
|
l1 = l3;
|
|
|
|
return;
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "var multiple init",
|
|
|
|
Src: `package main
|
|
|
|
|
|
|
|
func Foo(foo vec2) vec4 {
|
|
|
|
var bar1, bar2 vec2 = foo, foo
|
|
|
|
return vec4(bar1, bar2)
|
|
|
|
}`,
|
|
|
|
VS: `void F0(in vec2 l0, out vec4 l1) {
|
|
|
|
vec2 l2 = vec2(0.0);
|
|
|
|
vec2 l3 = vec2(0.0);
|
|
|
|
l2 = l0;
|
|
|
|
l3 = l0;
|
|
|
|
l1 = vec4(l2, l3);
|
|
|
|
return;
|
2020-05-31 12:20:53 +02:00
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
2020-05-31 19:23:27 +02:00
|
|
|
Name: "multiple out params",
|
2020-05-31 12:20:53 +02:00
|
|
|
Src: `package main
|
|
|
|
|
|
|
|
func Foo(foo vec4) (float, float, float, float) {
|
|
|
|
return foo.x, foo.y, foo.z, foo.w
|
|
|
|
}`,
|
|
|
|
VS: `void F0(in vec4 l0, out float l1, out float l2, out float l3, out float l4) {
|
|
|
|
l1 = (l0).x;
|
|
|
|
l2 = (l0).y;
|
|
|
|
l3 = (l0).z;
|
|
|
|
l4 = (l0).w;
|
|
|
|
return;
|
|
|
|
}`,
|
2020-05-31 16:57:03 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "blocks",
|
|
|
|
Src: `package main
|
|
|
|
|
|
|
|
func Foo(foo vec2) vec4 {
|
|
|
|
var r vec4
|
|
|
|
{
|
|
|
|
r.x = foo.x
|
|
|
|
var foo vec3
|
|
|
|
{
|
|
|
|
r.y = foo.y
|
|
|
|
var foo vec4
|
|
|
|
r.z = foo.z
|
|
|
|
}
|
2020-05-31 20:24:43 +02:00
|
|
|
{
|
|
|
|
r.y = foo.y
|
|
|
|
var foo vec4
|
|
|
|
r.z = foo.z
|
|
|
|
}
|
2020-05-31 16:57:03 +02:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}`,
|
|
|
|
VS: `void F0(in vec2 l0, out vec4 l1) {
|
|
|
|
vec4 l2 = vec4(0.0);
|
|
|
|
{
|
|
|
|
vec3 l3 = vec3(0.0);
|
|
|
|
(l2).x = (l0).x;
|
|
|
|
{
|
|
|
|
vec4 l4 = vec4(0.0);
|
|
|
|
(l2).y = (l3).y;
|
|
|
|
(l2).z = (l4).z;
|
|
|
|
}
|
2020-05-31 20:24:43 +02:00
|
|
|
{
|
|
|
|
vec4 l4 = vec4(0.0);
|
|
|
|
(l2).y = (l3).y;
|
|
|
|
(l2).z = (l4).z;
|
|
|
|
}
|
2020-05-31 16:57:03 +02:00
|
|
|
}
|
|
|
|
l1 = l2;
|
2020-05-31 12:20:53 +02:00
|
|
|
return;
|
2020-05-31 19:23:27 +02:00
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "define",
|
|
|
|
Src: `package main
|
|
|
|
|
|
|
|
func Foo(foo vec2) vec4 {
|
|
|
|
r := vec4(foo, 0, 1)
|
|
|
|
return r
|
|
|
|
}`,
|
|
|
|
VS: `void F0(in vec2 l0, out vec4 l1) {
|
|
|
|
vec4 l2 = vec4(0.0);
|
2020-06-02 14:45:33 +02:00
|
|
|
l2 = vec4(l0, 0.0, 1.0);
|
2020-05-31 19:23:27 +02:00
|
|
|
l1 = l2;
|
|
|
|
return;
|
2020-06-02 15:45:44 +02:00
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "vertex",
|
|
|
|
Src: `package main
|
|
|
|
|
|
|
|
func Vertex(position vec2, texCoord vec2, color vec4) (position vec4, texCoord vec2, color vec4) {
|
|
|
|
projectionMatrix := mat4(
|
|
|
|
2 / ScreenSize.x, 0, 0, 0,
|
|
|
|
0, 2 / ScreenSize.y, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
-1, -1, 0, 1,
|
|
|
|
)
|
|
|
|
return projectionMatrix * vec4(position, 0, 1), texCoord, color
|
2020-06-03 15:30:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var ScreenSize vec2`,
|
2020-06-02 15:45:44 +02:00
|
|
|
VS: `uniform vec2 U0;
|
|
|
|
attribute vec2 A0;
|
|
|
|
attribute vec2 A1;
|
|
|
|
attribute vec4 A2;
|
|
|
|
varying vec2 V0;
|
|
|
|
varying vec4 V1;
|
|
|
|
|
|
|
|
void main(void) {
|
|
|
|
mat4 l0 = mat4(0.0);
|
|
|
|
l0 = mat4((2.0) / ((U0).x), 0.0, 0.0, 0.0, 0.0, (2.0) / ((U0).y), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -(1.0), -(1.0), 0.0, 1.0);
|
|
|
|
gl_Position = (l0) * (vec4(A0, 0.0, 1.0));
|
|
|
|
V0 = A1;
|
|
|
|
V1 = A2;
|
|
|
|
return;
|
2020-05-31 09:20:36 +02:00
|
|
|
}`,
|
2020-06-03 15:30:34 +02:00
|
|
|
FS: `uniform vec2 U0;
|
|
|
|
varying vec2 V0;
|
|
|
|
varying vec4 V1;`,
|
2020-05-31 09:20:36 +02:00
|
|
|
},
|
2020-06-03 16:56:08 +02:00
|
|
|
{
|
|
|
|
Name: "vertex and fragment",
|
|
|
|
Src: `package main
|
|
|
|
|
|
|
|
func Vertex(position vec2, texCoord vec2, color vec4) (position vec4, texCoord vec2, color vec4) {
|
|
|
|
projectionMatrix := mat4(
|
|
|
|
2 / ScreenSize.x, 0, 0, 0,
|
|
|
|
0, 2 / ScreenSize.y, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
-1, -1, 0, 1,
|
|
|
|
)
|
|
|
|
return projectionMatrix * vec4(position, 0, 1), texCoord, color
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(1, 0, 0, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ScreenSize vec2`,
|
|
|
|
VS: `uniform vec2 U0;
|
|
|
|
attribute vec2 A0;
|
|
|
|
attribute vec2 A1;
|
|
|
|
attribute vec4 A2;
|
|
|
|
varying vec2 V0;
|
|
|
|
varying vec4 V1;
|
|
|
|
|
|
|
|
void main(void) {
|
|
|
|
mat4 l0 = mat4(0.0);
|
|
|
|
l0 = mat4((2.0) / ((U0).x), 0.0, 0.0, 0.0, 0.0, (2.0) / ((U0).y), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -(1.0), -(1.0), 0.0, 1.0);
|
|
|
|
gl_Position = (l0) * (vec4(A0, 0.0, 1.0));
|
|
|
|
V0 = A1;
|
|
|
|
V1 = A2;
|
|
|
|
return;
|
|
|
|
}`,
|
|
|
|
FS: `uniform vec2 U0;
|
|
|
|
varying vec2 V0;
|
|
|
|
varying vec4 V1;
|
|
|
|
|
|
|
|
void main(void) {
|
|
|
|
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
|
|
|
return;
|
|
|
|
}`,
|
|
|
|
},
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
|
|
|
for _, tc := range tests {
|
2020-05-31 19:23:27 +02:00
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
2020-06-06 17:52:43 +02:00
|
|
|
fset := token.NewFileSet()
|
|
|
|
f, err := parser.ParseFile(fset, "", []byte(tc.Src), parser.AllErrors)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := Compile(fset, f, "Vertex", "Fragment")
|
2020-05-31 19:23:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
2020-05-31 16:57:03 +02:00
|
|
|
}
|
2020-05-31 19:23:27 +02:00
|
|
|
vs, fs := s.Glsl()
|
|
|
|
if got, want := vs, tc.VS+"\n"; got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
if tc.FS != "" {
|
|
|
|
if got, want := fs, tc.FS+"\n"; got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
|
|
|
}
|