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-09-02 19:45:22 +02:00
|
|
|
"fmt"
|
2022-09-14 19:45:36 +02:00
|
|
|
"os"
|
2020-06-08 04:18:27 +02:00
|
|
|
"path/filepath"
|
2020-06-08 05:31:50 +02:00
|
|
|
"runtime"
|
2020-06-08 04:18:27 +02:00
|
|
|
"strings"
|
2020-05-08 17:46:01 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shader"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/glsl"
|
2022-03-25 17:31:05 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/hlsl"
|
2022-03-09 16:08:36 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/msl"
|
2020-05-08 17:46:01 +02:00
|
|
|
)
|
|
|
|
|
2022-01-11 17:41:06 +01:00
|
|
|
func glslVertexNormalize(str string) string {
|
|
|
|
p := glsl.VertexPrelude(glsl.GLSLVersionDefault)
|
|
|
|
if strings.HasPrefix(str, p) {
|
|
|
|
str = str[len(p):]
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func glslFragmentNormalize(str string) string {
|
2020-10-26 02:33:11 +01:00
|
|
|
p := glsl.FragmentPrelude(glsl.GLSLVersionDefault)
|
|
|
|
if strings.HasPrefix(str, p) {
|
|
|
|
str = str[len(p):]
|
2020-08-01 11:19:06 +02:00
|
|
|
}
|
2020-08-01 11:19:06 +02:00
|
|
|
return strings.TrimSpace(str)
|
2020-08-01 11:19:06 +02:00
|
|
|
}
|
|
|
|
|
2022-03-25 17:31:05 +01:00
|
|
|
func hlslNormalize(str string) string {
|
|
|
|
if strings.HasPrefix(str, hlsl.Prelude) {
|
|
|
|
str = str[len(hlsl.Prelude):]
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(str)
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:11:15 +02:00
|
|
|
func metalNormalize(str string) string {
|
2022-03-10 08:24:56 +01:00
|
|
|
if strings.HasPrefix(str, msl.Prelude) {
|
|
|
|
str = str[len(msl.Prelude):]
|
2020-09-09 18:11:15 +02:00
|
|
|
}
|
|
|
|
return strings.TrimSpace(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func compare(t *testing.T, title, got, want string) {
|
|
|
|
var msg string
|
|
|
|
gotlines := strings.Split(got, "\n")
|
|
|
|
wantlines := strings.Split(want, "\n")
|
|
|
|
for i := range gotlines {
|
|
|
|
if len(wantlines) <= i {
|
|
|
|
msg = fmt.Sprintf(`lines %d:
|
|
|
|
got: %s
|
|
|
|
want: (out of range)`, i+1, gotlines[i])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if gotlines[i] != wantlines[i] {
|
|
|
|
msg = fmt.Sprintf(`lines %d:
|
|
|
|
got: %s
|
|
|
|
want: %s`, i+1, gotlines[i], wantlines[i])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Errorf("%s: got: %v, want: %v\n\n%s", title, got, want, msg)
|
|
|
|
}
|
|
|
|
|
2020-06-08 04:18:27 +02:00
|
|
|
func TestCompile(t *testing.T) {
|
2020-06-08 05:31:50 +02:00
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
t.Skip("file open might not be implemented in this environment")
|
|
|
|
}
|
|
|
|
|
2022-09-14 19:45:36 +02:00
|
|
|
files, err := os.ReadDir("testdata")
|
2020-06-08 04:18:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-31 16:57:03 +02:00
|
|
|
|
2020-06-08 04:18:27 +02:00
|
|
|
type testcase struct {
|
2020-09-09 18:11:15 +02:00
|
|
|
Name string
|
|
|
|
Src []byte
|
|
|
|
VS []byte
|
|
|
|
FS []byte
|
2022-03-25 17:31:05 +01:00
|
|
|
HLSL []byte
|
2020-09-09 18:11:15 +02:00
|
|
|
Metal []byte
|
2020-05-31 16:57:03 +02:00
|
|
|
}
|
2020-06-08 04:18:27 +02:00
|
|
|
|
|
|
|
fnames := map[string]struct{}{}
|
|
|
|
for _, f := range files {
|
|
|
|
if f.IsDir() {
|
|
|
|
continue
|
2020-05-31 20:24:43 +02:00
|
|
|
}
|
2020-06-08 04:18:27 +02:00
|
|
|
fnames[f.Name()] = struct{}{}
|
2020-05-31 16:57:03 +02:00
|
|
|
}
|
2020-06-07 18:41:47 +02:00
|
|
|
|
2020-06-08 04:18:27 +02:00
|
|
|
tests := []testcase{}
|
|
|
|
for n := range fnames {
|
|
|
|
if !strings.HasSuffix(n, ".go") {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-02 15:45:44 +02:00
|
|
|
|
2022-09-14 19:45:36 +02:00
|
|
|
src, err := os.ReadFile(filepath.Join("testdata", n))
|
2020-06-08 04:18:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-03 16:56:08 +02:00
|
|
|
|
2020-06-08 04:18:27 +02:00
|
|
|
name := n[:len(n)-len(".go")]
|
|
|
|
tc := testcase{
|
|
|
|
Name: name,
|
|
|
|
Src: src,
|
|
|
|
}
|
2020-06-03 16:56:08 +02:00
|
|
|
|
2020-06-08 04:18:27 +02:00
|
|
|
vsn := name + ".expected.vs"
|
|
|
|
if _, ok := fnames[vsn]; ok {
|
2022-09-14 19:45:36 +02:00
|
|
|
vs, err := os.ReadFile(filepath.Join("testdata", vsn))
|
2020-06-08 04:18:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tc.VS = vs
|
|
|
|
}
|
2020-06-03 16:56:08 +02:00
|
|
|
|
2020-06-08 04:18:27 +02:00
|
|
|
fsn := name + ".expected.fs"
|
|
|
|
if _, ok := fnames[fsn]; ok {
|
2022-09-14 19:45:36 +02:00
|
|
|
fs, err := os.ReadFile(filepath.Join("testdata", fsn))
|
2020-06-08 04:18:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tc.FS = fs
|
|
|
|
}
|
2020-06-03 16:56:08 +02:00
|
|
|
|
2020-06-08 04:18:27 +02:00
|
|
|
if tc.VS == nil && tc.FS == nil {
|
|
|
|
t.Fatalf("no expected file for %s", name)
|
|
|
|
}
|
2020-06-03 16:56:08 +02:00
|
|
|
|
2022-03-25 17:31:05 +01:00
|
|
|
hlsln := name + ".expected.hlsl"
|
|
|
|
if _, ok := fnames[hlsln]; ok {
|
2022-09-14 19:45:36 +02:00
|
|
|
hlsl, err := os.ReadFile(filepath.Join("testdata", hlsln))
|
2022-03-25 17:31:05 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tc.HLSL = hlsl
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:11:15 +02:00
|
|
|
metaln := name + ".expected.metal"
|
|
|
|
if _, ok := fnames[metaln]; ok {
|
2022-09-14 19:45:36 +02:00
|
|
|
metal, err := os.ReadFile(filepath.Join("testdata", metaln))
|
2020-09-09 18:11:15 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tc.Metal = metal
|
|
|
|
}
|
|
|
|
|
2020-06-08 04:18:27 +02:00
|
|
|
tests = append(tests, tc)
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
2020-06-08 04:18:27 +02:00
|
|
|
|
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) {
|
2023-04-16 11:35:12 +02:00
|
|
|
s, err := shader.Compile(tc.Src, "Vertex", "Fragment", 0)
|
2020-05-31 19:23:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
2020-05-31 16:57:03 +02:00
|
|
|
}
|
2020-09-09 18:11:15 +02:00
|
|
|
|
|
|
|
// GLSL
|
2020-10-26 02:33:11 +01:00
|
|
|
vs, fs := glsl.Compile(s, glsl.GLSLVersionDefault)
|
2022-01-11 17:41:06 +01:00
|
|
|
if got, want := glslVertexNormalize(vs), glslVertexNormalize(string(tc.VS)); got != want {
|
2020-09-09 18:11:15 +02:00
|
|
|
compare(t, "GLSL Vertex", got, want)
|
2020-05-31 19:23:27 +02:00
|
|
|
}
|
2020-06-08 04:18:27 +02:00
|
|
|
if tc.FS != nil {
|
2022-01-11 17:41:06 +01:00
|
|
|
if got, want := glslFragmentNormalize(fs), glslFragmentNormalize(string(tc.FS)); got != want {
|
2020-09-09 18:11:15 +02:00
|
|
|
compare(t, "GLSL Fragment", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 17:31:05 +01:00
|
|
|
if tc.HLSL != nil {
|
2022-12-24 08:12:35 +01:00
|
|
|
vs, _, _ := hlsl.Compile(s)
|
|
|
|
if got, want := hlslNormalize(vs), hlslNormalize(string(tc.HLSL)); got != want {
|
2022-03-25 17:31:05 +01:00
|
|
|
compare(t, "HLSL", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:11:15 +02:00
|
|
|
if tc.Metal != nil {
|
2022-03-10 08:24:56 +01:00
|
|
|
m := msl.Compile(s, "Vertex", "Fragment")
|
2020-09-09 18:11:15 +02:00
|
|
|
if got, want := metalNormalize(m), metalNormalize(string(tc.Metal)); got != want {
|
|
|
|
compare(t, "Metal", got, want)
|
2020-05-31 19:23:27 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-08 14:29:01 +02:00
|
|
|
|
|
|
|
// Just check that Compile doesn't cause panic.
|
|
|
|
// TODO: Should the results be tested?
|
2022-03-10 08:24:56 +01:00
|
|
|
msl.Compile(s, "Vertex", "Fragmentp")
|
2020-05-31 19:23:27 +02:00
|
|
|
})
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
|
|
|
}
|