mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-13 19:43:17 +01:00
internal/shaderlister: add go:embeddedshader directive for embedded files
Updates #3157
This commit is contained in:
parent
3666920fb7
commit
3ebbbeead3
@ -14,6 +14,8 @@
|
||||
|
||||
//go:build ignore
|
||||
|
||||
//ebitengine:embeddedshader
|
||||
|
||||
//kage:unit pixels
|
||||
|
||||
// Reference: a public domain CRT effect
|
||||
|
@ -14,6 +14,8 @@
|
||||
|
||||
//go:build ignore
|
||||
|
||||
//ebitengine:embeddedshader
|
||||
|
||||
//kage:unit pixels
|
||||
|
||||
package main
|
||||
|
@ -14,6 +14,8 @@
|
||||
|
||||
//go:build ignore
|
||||
|
||||
//ebitengine:embeddedshader
|
||||
|
||||
//kage:unit pixels
|
||||
|
||||
package main
|
||||
|
@ -14,6 +14,8 @@
|
||||
|
||||
//go:build ignore
|
||||
|
||||
//ebitengine:embeddedshader
|
||||
|
||||
//kage:unit pixels
|
||||
|
||||
package main
|
||||
|
@ -14,7 +14,8 @@
|
||||
|
||||
//go:build ignore
|
||||
|
||||
// Specify the 'pixel' mode.
|
||||
//ebitengine:embeddedshader
|
||||
|
||||
//kage:unit pixels
|
||||
|
||||
package main
|
||||
|
@ -14,6 +14,8 @@
|
||||
|
||||
//go:build ignore
|
||||
|
||||
//ebitengine:embeddedshader
|
||||
|
||||
//kage:unit pixels
|
||||
|
||||
package main
|
||||
|
@ -14,6 +14,8 @@
|
||||
|
||||
//go:build ignore
|
||||
|
||||
//ebitengine:embeddedshader
|
||||
|
||||
//kage:unit pixels
|
||||
|
||||
package main
|
||||
|
@ -14,6 +14,8 @@
|
||||
|
||||
//go:build ignore
|
||||
|
||||
//ebitengine:embeddedshader
|
||||
|
||||
//kage:unit pixels
|
||||
|
||||
package main
|
||||
|
@ -94,7 +94,7 @@ func xmain() error {
|
||||
}
|
||||
|
||||
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()...)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -124,7 +124,11 @@ func xmain() error {
|
||||
}
|
||||
|
||||
origN := len(shaders)
|
||||
shaders = appendShaderSources(shaders, pkg)
|
||||
shaders, err = appendShaderSources(shaders, pkg)
|
||||
if err != nil {
|
||||
visitErr = err
|
||||
return false
|
||||
}
|
||||
|
||||
// Add source hashes.
|
||||
for i := range shaders[origN:] {
|
||||
@ -175,20 +179,26 @@ func isStandardImportPath(path string) bool {
|
||||
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 {
|
||||
for _, line := range commentGroup.List {
|
||||
if reDirective.MatchString(line.Text) {
|
||||
if reShaderDirective.MatchString(line.Text) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader {
|
||||
func appendShaderSources(shaders []Shader, pkg *packages.Package) ([]Shader, error) {
|
||||
topLevelDecls := map[ast.Decl]struct{}{}
|
||||
for _, file := range pkg.Syntax {
|
||||
for _, decl := range file.Decls {
|
||||
@ -220,7 +230,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader {
|
||||
if genDecl.Lparen != token.NoPos {
|
||||
if genDecl.Doc != nil && hasShaderDirectiveInComment(genDecl.Doc) {
|
||||
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,
|
||||
"file", pos.Filename,
|
||||
"line", pos.Line,
|
||||
@ -268,7 +278,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader {
|
||||
|
||||
if !isTopLevelDecl(genDecl) {
|
||||
pos := pkg.Fset.Position(docPos)
|
||||
slog.Warn(fmt.Sprintf("misplaced %s directive", directive),
|
||||
slog.Warn(fmt.Sprintf("misplaced %s directive", shaderDirective),
|
||||
"package", pkg.PkgPath,
|
||||
"file", pos.Filename,
|
||||
"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.
|
||||
if len(spec.Names) != 1 {
|
||||
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,
|
||||
"file", pos.Filename,
|
||||
"line", pos.Line,
|
||||
@ -293,7 +303,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader {
|
||||
c, ok := def.(*types.Const)
|
||||
if !ok {
|
||||
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,
|
||||
"file", pos.Filename,
|
||||
"line", pos.Line,
|
||||
@ -305,7 +315,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) []Shader {
|
||||
val := c.Val()
|
||||
if val.Kind() != constant.String {
|
||||
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,
|
||||
"file", pos.Filename,
|
||||
"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 {
|
||||
|
@ -19,6 +19,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -58,29 +59,47 @@ func TestRun(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
slices.SortFunc(shaders, func(s1, s2 shader) int {
|
||||
return cmp.Compare(s1.Source, s2.Source)
|
||||
type filteredShader struct {
|
||||
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)
|
||||
}
|
||||
|
||||
for i, s := range shaders {
|
||||
if s.Package == "" {
|
||||
for i, s := range filteredShaders {
|
||||
if s.shader.Package == "" {
|
||||
t.Errorf("s.Package is empty: %v", s)
|
||||
}
|
||||
if s.File == "" {
|
||||
if s.shader.File == "" {
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,11 @@
|
||||
|
||||
package shaderlistertest
|
||||
|
||||
import "github.com/hajimehoshi/ebiten/v2/internal/shaderlister/shaderlistertest2"
|
||||
import (
|
||||
"embed"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderlister/shaderlistertest2"
|
||||
)
|
||||
|
||||
//ebitengine:shader
|
||||
const _ = "shader 1"
|
||||
@ -66,3 +70,9 @@ const (
|
||||
//ebitengine:shader
|
||||
_, _ = "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
|
||||
|
21
internal/shaderlister/shaderlistertest/embed2_kage.go
Normal file
21
internal/shaderlister/shaderlistertest/embed2_kage.go
Normal 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
|
21
internal/shaderlister/shaderlistertest/embed_kage.go
Normal file
21
internal/shaderlister/shaderlistertest/embed_kage.go
Normal 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
|
21
internal/shaderlister/shaderlistertest/embed_notkage.go
Normal file
21
internal/shaderlister/shaderlistertest/embed_notkage.go
Normal 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
|
@ -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
|
@ -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
|
Loading…
Reference in New Issue
Block a user