shader_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2020 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package shader_test
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "runtime"
  20. "strings"
  21. "testing"
  22. "github.com/hajimehoshi/ebiten/v2/internal/shader"
  23. "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
  24. "github.com/hajimehoshi/ebiten/v2/internal/shaderir/glsl"
  25. "github.com/hajimehoshi/ebiten/v2/internal/shaderir/hlsl"
  26. "github.com/hajimehoshi/ebiten/v2/internal/shaderir/msl"
  27. )
  28. func glslVertexNormalize(str string) string {
  29. p := glsl.VertexPrelude(glsl.GLSLVersionDefault)
  30. str = strings.TrimPrefix(str, p)
  31. return strings.TrimSpace(str)
  32. }
  33. func glslFragmentNormalize(str string) string {
  34. p := glsl.FragmentPrelude(glsl.GLSLVersionDefault)
  35. str = strings.TrimPrefix(str, p)
  36. return strings.TrimSpace(str)
  37. }
  38. func hlslNormalize(str string, prelude string) string {
  39. str = strings.TrimPrefix(str, prelude)
  40. return strings.TrimSpace(str)
  41. }
  42. func metalNormalize(str string) string {
  43. prelude := msl.Prelude(shaderir.Texels)
  44. str = strings.TrimPrefix(str, prelude)
  45. return strings.TrimSpace(str)
  46. }
  47. func compare(t *testing.T, title, got, want string) {
  48. var msg string
  49. gotlines := strings.Split(got, "\n")
  50. wantlines := strings.Split(want, "\n")
  51. for i := range gotlines {
  52. if len(wantlines) <= i {
  53. msg = fmt.Sprintf(`lines %d:
  54. got: %s
  55. want: (out of range)`, i+1, gotlines[i])
  56. break
  57. }
  58. if gotlines[i] != wantlines[i] {
  59. msg = fmt.Sprintf(`lines %d:
  60. got: %s
  61. want: %s`, i+1, gotlines[i], wantlines[i])
  62. break
  63. }
  64. }
  65. t.Errorf("%s: got: %v, want: %v\n\n%s", title, got, want, msg)
  66. }
  67. func TestCompile(t *testing.T) {
  68. if runtime.GOOS == "js" {
  69. t.Skip("file open might not be implemented in this environment")
  70. }
  71. files, err := os.ReadDir("testdata")
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. type testcase struct {
  76. Name string
  77. Src []byte
  78. VS []byte
  79. FS []byte
  80. HLSL []byte
  81. Metal []byte
  82. }
  83. fnames := map[string]struct{}{}
  84. for _, f := range files {
  85. if f.IsDir() {
  86. continue
  87. }
  88. fnames[f.Name()] = struct{}{}
  89. }
  90. tests := []testcase{}
  91. for n := range fnames {
  92. if !strings.HasSuffix(n, ".go") {
  93. continue
  94. }
  95. src, err := os.ReadFile(filepath.Join("testdata", n))
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. name := n[:len(n)-len(".go")]
  100. tc := testcase{
  101. Name: name,
  102. Src: src,
  103. }
  104. vsn := name + ".expected.vs"
  105. if _, ok := fnames[vsn]; ok {
  106. vs, err := os.ReadFile(filepath.Join("testdata", vsn))
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. tc.VS = vs
  111. }
  112. fsn := name + ".expected.fs"
  113. if _, ok := fnames[fsn]; ok {
  114. fs, err := os.ReadFile(filepath.Join("testdata", fsn))
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. tc.FS = fs
  119. }
  120. if tc.VS == nil && tc.FS == nil {
  121. t.Fatalf("no expected file for %s", name)
  122. }
  123. hlsln := name + ".expected.hlsl"
  124. if _, ok := fnames[hlsln]; ok {
  125. hlsl, err := os.ReadFile(filepath.Join("testdata", hlsln))
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. tc.HLSL = hlsl
  130. }
  131. metaln := name + ".expected.metal"
  132. if _, ok := fnames[metaln]; ok {
  133. metal, err := os.ReadFile(filepath.Join("testdata", metaln))
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. tc.Metal = metal
  138. }
  139. tests = append(tests, tc)
  140. }
  141. for _, tc := range tests {
  142. t.Run(tc.Name, func(t *testing.T) {
  143. s, err := shader.Compile(tc.Src, "Vertex", "Fragment", 0)
  144. if err != nil {
  145. t.Error(err)
  146. return
  147. }
  148. // GLSL
  149. vs, fs := glsl.Compile(s, glsl.GLSLVersionDefault)
  150. if got, want := glslVertexNormalize(vs), glslVertexNormalize(string(tc.VS)); got != want {
  151. compare(t, "GLSL Vertex", got, want)
  152. }
  153. if tc.FS != nil {
  154. if got, want := glslFragmentNormalize(fs), glslFragmentNormalize(string(tc.FS)); got != want {
  155. compare(t, "GLSL Fragment", got, want)
  156. }
  157. }
  158. if tc.HLSL != nil {
  159. vs, _, prelude := hlsl.Compile(s)
  160. if got, want := hlslNormalize(vs, prelude), hlslNormalize(string(tc.HLSL), prelude); got != want {
  161. compare(t, "HLSL", got, want)
  162. }
  163. }
  164. if tc.Metal != nil {
  165. m := msl.Compile(s)
  166. if got, want := metalNormalize(m), metalNormalize(string(tc.Metal)); got != want {
  167. compare(t, "Metal", got, want)
  168. }
  169. }
  170. // Just check that Compile doesn't cause panic.
  171. // TODO: Should the results be tested?
  172. msl.Compile(s)
  173. })
  174. }
  175. }