internal/shaderlister: add go:embeddedshader directive for embedded files

Updates #3157
This commit is contained in:
Hajime Hoshi 2025-02-08 17:40:31 +09:00
parent 3666920fb7
commit 3ebbbeead3
16 changed files with 197 additions and 23 deletions

View File

@ -14,6 +14,8 @@
//go:build ignore //go:build ignore
//ebitengine:embeddedshader
//kage:unit pixels //kage:unit pixels
// Reference: a public domain CRT effect // Reference: a public domain CRT effect

View File

@ -14,6 +14,8 @@
//go:build ignore //go:build ignore
//ebitengine:embeddedshader
//kage:unit pixels //kage:unit pixels
package main package main

View File

@ -14,6 +14,8 @@
//go:build ignore //go:build ignore
//ebitengine:embeddedshader
//kage:unit pixels //kage:unit pixels
package main package main

View File

@ -14,6 +14,8 @@
//go:build ignore //go:build ignore
//ebitengine:embeddedshader
//kage:unit pixels //kage:unit pixels
package main package main

View File

@ -14,7 +14,8 @@
//go:build ignore //go:build ignore
// Specify the 'pixel' mode. //ebitengine:embeddedshader
//kage:unit pixels //kage:unit pixels
package main package main

View File

@ -14,6 +14,8 @@
//go:build ignore //go:build ignore
//ebitengine:embeddedshader
//kage:unit pixels //kage:unit pixels
package main package main

View File

@ -14,6 +14,8 @@
//go:build ignore //go:build ignore
//ebitengine:embeddedshader
//kage:unit pixels //kage:unit pixels
package main package main

View File

@ -14,6 +14,8 @@
//go:build ignore //go:build ignore
//ebitengine:embeddedshader
//kage:unit pixels //kage:unit pixels
package main package main

View File

@ -94,7 +94,7 @@ func xmain() error {
} }
pkgs, err := packages.Load(&packages.Config{ pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo, Mode: packages.NeedName | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedEmbedFiles,
}, flag.Args()...) }, flag.Args()...)
if err != nil { if err != nil {
return err return err
@ -124,7 +124,11 @@ func xmain() error {
} }
origN := len(shaders) origN := len(shaders)
shaders = appendShaderSources(shaders, pkg) shaders, err = appendShaderSources(shaders, pkg)
if err != nil {
visitErr = err
return false
}
// Add source hashes. // Add source hashes.
for i := range shaders[origN:] { for i := range shaders[origN:] {
@ -175,20 +179,26 @@ func isStandardImportPath(path string) bool {
return !strings.Contains(head, ".") return !strings.Contains(head, ".")
} }
const directive = "ebitengine:shader" const (
shaderDirective = "ebitengine:shader"
embeddedShaderDirective = "ebitengine:embeddedshader"
)
var reDirective = regexp.MustCompile(`(?m)^\s*//` + regexp.QuoteMeta(directive)) var (
reShaderDirective = regexp.MustCompile(`(?m)^\s*//` + regexp.QuoteMeta(shaderDirective))
reEmbeddedShaderDirective = regexp.MustCompile(`(?m)^\s*//` + regexp.QuoteMeta(embeddedShaderDirective))
)
func hasShaderDirectiveInComment(commentGroup *ast.CommentGroup) bool { func hasShaderDirectiveInComment(commentGroup *ast.CommentGroup) bool {
for _, line := range commentGroup.List { for _, line := range commentGroup.List {
if reDirective.MatchString(line.Text) { if reShaderDirective.MatchString(line.Text) {
return true return true
} }
} }
return false return false
} }
func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader { func appendShaderSources(shaders []Shader, pkg *packages.Package) ([]Shader, error) {
topLevelDecls := map[ast.Decl]struct{}{} topLevelDecls := map[ast.Decl]struct{}{}
for _, file := range pkg.Syntax { for _, file := range pkg.Syntax {
for _, decl := range file.Decls { for _, decl := range file.Decls {
@ -220,7 +230,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader {
if genDecl.Lparen != token.NoPos { if genDecl.Lparen != token.NoPos {
if genDecl.Doc != nil && hasShaderDirectiveInComment(genDecl.Doc) { if genDecl.Doc != nil && hasShaderDirectiveInComment(genDecl.Doc) {
pos := pkg.Fset.Position(genDecl.Doc.Pos()) pos := pkg.Fset.Position(genDecl.Doc.Pos())
slog.Warn(fmt.Sprintf("misplaced %s directive", directive), slog.Warn(fmt.Sprintf("misplaced %s directive", shaderDirective),
"package", pkg.PkgPath, "package", pkg.PkgPath,
"file", pos.Filename, "file", pos.Filename,
"line", pos.Line, "line", pos.Line,
@ -268,7 +278,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader {
if !isTopLevelDecl(genDecl) { if !isTopLevelDecl(genDecl) {
pos := pkg.Fset.Position(docPos) pos := pkg.Fset.Position(docPos)
slog.Warn(fmt.Sprintf("misplaced %s directive", directive), slog.Warn(fmt.Sprintf("misplaced %s directive", shaderDirective),
"package", pkg.PkgPath, "package", pkg.PkgPath,
"file", pos.Filename, "file", pos.Filename,
"line", pos.Line, "line", pos.Line,
@ -279,7 +289,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader {
// Avoid multiple names like `const a, b = "foo", "bar"` to avoid confusions. // Avoid multiple names like `const a, b = "foo", "bar"` to avoid confusions.
if len(spec.Names) != 1 { if len(spec.Names) != 1 {
pos := pkg.Fset.Position(docPos) pos := pkg.Fset.Position(docPos)
slog.Warn(fmt.Sprintf("%s cannot apply to multiple declarations", directive), slog.Warn(fmt.Sprintf("%s cannot apply to multiple declarations", shaderDirective),
"package", pkg.PkgPath, "package", pkg.PkgPath,
"file", pos.Filename, "file", pos.Filename,
"line", pos.Line, "line", pos.Line,
@ -293,7 +303,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader {
c, ok := def.(*types.Const) c, ok := def.(*types.Const)
if !ok { if !ok {
pos := pkg.Fset.Position(docPos) pos := pkg.Fset.Position(docPos)
slog.Warn(fmt.Sprintf("%s cannot apply to %s", directive, objectTypeString(def)), slog.Warn(fmt.Sprintf("%s cannot apply to %s", shaderDirective, objectTypeString(def)),
"package", pkg.PkgPath, "package", pkg.PkgPath,
"file", pos.Filename, "file", pos.Filename,
"line", pos.Line, "line", pos.Line,
@ -305,7 +315,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader {
val := c.Val() val := c.Val()
if val.Kind() != constant.String { if val.Kind() != constant.String {
pos := pkg.Fset.Position(docPos) pos := pkg.Fset.Position(docPos)
slog.Warn(fmt.Sprintf("%s cannot apply to const type of %s", directive, val.Kind()), slog.Warn(fmt.Sprintf("%s cannot apply to const type of %s", shaderDirective, val.Kind()),
"package", pkg.PkgPath, "package", pkg.PkgPath,
"file", pos.Filename, "file", pos.Filename,
"line", pos.Line, "line", pos.Line,
@ -325,7 +335,22 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader {
} }
}) })
return shaders for _, file := range pkg.EmbedFiles {
content, err := os.ReadFile(file)
if err != nil {
return nil, err
}
if !reEmbeddedShaderDirective.Match(content) {
continue
}
shaders = append(shaders, Shader{
Package: pkg.PkgPath,
File: file,
Source: string(content),
})
}
return shaders, nil
} }
func objectTypeString(obj types.Object) string { func objectTypeString(obj types.Object) string {

View File

@ -19,6 +19,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os/exec" "os/exec"
"regexp"
"slices" "slices"
"strings" "strings"
"testing" "testing"
@ -58,29 +59,47 @@ func TestRun(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
slices.SortFunc(shaders, func(s1, s2 shader) int { type filteredShader struct {
return cmp.Compare(s1.Source, s2.Source) shader shader
filteredSource string
}
re := regexp.MustCompile(`shader \d+`)
var filteredShaders []filteredShader
for _, s := range shaders {
m := re.FindAllString(s.Source, 1)
if len(m) != 1 {
t.Fatalf("invalid source: %q", s.Source)
}
filteredShaders = append(filteredShaders, filteredShader{
shader: s,
filteredSource: m[0],
})
}
slices.SortFunc(filteredShaders, func(s1, s2 filteredShader) int {
return cmp.Compare(s1.filteredSource, s2.filteredSource)
}) })
if got, want := len(shaders), 6; got != want { if got, want := len(filteredShaders), 9; got != want {
t.Fatalf("len(shaders): got: %d, want: %d", got, want) t.Fatalf("len(shaders): got: %d, want: %d", got, want)
} }
for i, s := range shaders { for i, s := range filteredShaders {
if s.Package == "" { if s.shader.Package == "" {
t.Errorf("s.Package is empty: %v", s) t.Errorf("s.Package is empty: %v", s)
} }
if s.File == "" { if s.shader.File == "" {
t.Errorf("s.File is empty: %v", s) t.Errorf("s.File is empty: %v", s)
} }
hash, err := graphics.CalcSourceHash([]byte(s.Source)) hash, err := graphics.CalcSourceHash([]byte(s.shader.Source))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if got, want := s.SourceHash, hash.String(); got != want { if got, want := s.shader.SourceHash, hash.String(); got != want {
t.Errorf("s.SourceHash: got: %q, want: %q", got, want) t.Errorf("s.SourceHash: got: %q, want: %q", got, want)
} }
if got, want := s.Source, fmt.Sprintf("shader %d", i+1); got != want { if got, want := s.filteredSource, fmt.Sprintf("shader %d", i+1); got != want {
t.Errorf("s.Source: got: %q, want: %q", got, want) t.Errorf("s.Source: got: %q, want: %q", got, want)
} }
} }

View File

@ -14,7 +14,11 @@
package shaderlistertest package shaderlistertest
import "github.com/hajimehoshi/ebiten/v2/internal/shaderlister/shaderlistertest2" import (
"embed"
"github.com/hajimehoshi/ebiten/v2/internal/shaderlister/shaderlistertest2"
)
//ebitengine:shader //ebitengine:shader
const _ = "shader 1" const _ = "shader 1"
@ -66,3 +70,9 @@ const (
//ebitengine:shader //ebitengine:shader
_, _ = "ignored", "ignored again" // multiple consts are ignored to avoid confusion. _, _ = "ignored", "ignored again" // multiple consts are ignored to avoid confusion.
) )
//go:embed *kage.go
var embed_go []byte
//go:embed resource
var embed2_go embed.FS

View File

@ -0,0 +1,21 @@
// Copyright 2025 The Ebitengine 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.
//go:build ignore
//ebitengine:embeddedshader
package main
// shader 8

View File

@ -0,0 +1,21 @@
// Copyright 2025 The Ebitengine 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.
//go:build ignore
//ebitengine:embeddedshader
package main
// shader 7

View File

@ -0,0 +1,21 @@
// Copyright 2025 The Ebitengine 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.
//go:build ignore
// This file lacks the directive `ebitengine:embeddedshader`, so this is not considered as a shader file by shaderlister.
package main
// shader 0

View File

@ -0,0 +1,21 @@
// Copyright 2025 The Ebitengine 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.
//go:build ignore
//ebitengine:embeddedshader
package main
// shader 9

View File

@ -0,0 +1,21 @@
// Copyright 2025 The Ebitengine 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.
//go:build ignore
// This file lacks the directive `ebitengine:embeddedshader`, so this is not considered as a shader file by shaderlister.
package main
// shader 0