Skip to content

Commit

Permalink
internal/shaderlister: add go:embeddedshader directive for embedded f…
Browse files Browse the repository at this point in the history
…iles

Updates #3157
  • Loading branch information
hajimehoshi committed Feb 8, 2025
1 parent 3666920 commit 3ebbbee
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 23 deletions.
2 changes: 2 additions & 0 deletions examples/flappy/crt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

//go:build ignore

//ebitengine:embeddedshader

//kage:unit pixels

// Reference: a public domain CRT effect
Expand Down
2 changes: 2 additions & 0 deletions examples/shader/chromaticaberration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

//go:build ignore

//ebitengine:embeddedshader

//kage:unit pixels

package main
Expand Down
2 changes: 2 additions & 0 deletions examples/shader/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

//go:build ignore

//ebitengine:embeddedshader

//kage:unit pixels

package main
Expand Down
2 changes: 2 additions & 0 deletions examples/shader/dissolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

//go:build ignore

//ebitengine:embeddedshader

//kage:unit pixels

package main
Expand Down
3 changes: 2 additions & 1 deletion examples/shader/lighting.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

//go:build ignore

// Specify the 'pixel' mode.
//ebitengine:embeddedshader

//kage:unit pixels

package main
Expand Down
2 changes: 2 additions & 0 deletions examples/shader/radialblur.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

//go:build ignore

//ebitengine:embeddedshader

//kage:unit pixels

package main
Expand Down
2 changes: 2 additions & 0 deletions examples/shader/texel.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

//go:build ignore

//ebitengine:embeddedshader

//kage:unit pixels

package main
Expand Down
2 changes: 2 additions & 0 deletions examples/shader/water.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

//go:build ignore

//ebitengine:embeddedshader

//kage:unit pixels

package main
Expand Down
49 changes: 37 additions & 12 deletions internal/shaderlister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:] {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
37 changes: 28 additions & 9 deletions internal/shaderlister/shaderlister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"fmt"
"os/exec"
"regexp"
"slices"
"strings"
"testing"
Expand Down Expand Up @@ -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)
}
}
Expand Down
12 changes: 11 additions & 1 deletion internal/shaderlister/shaderlistertest/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Check failure on line 75 in internal/shaderlister/shaderlistertest/def.go

View workflow job for this annotation

GitHub Actions / Test with Go 1.22.x on ubuntu-latest

invalid go:embed: multiple files for type []byte

Check failure on line 75 in internal/shaderlister/shaderlistertest/def.go

View workflow job for this annotation

GitHub Actions / Test with Go 1.23.x on ubuntu-latest

invalid go:embed: multiple files for type []byte

Check failure on line 75 in internal/shaderlister/shaderlistertest/def.go

View workflow job for this annotation

GitHub Actions / Test with Go 1.24.0-rc.2 on ubuntu-latest

invalid go:embed: multiple files for type []byte

Check failure on line 75 in internal/shaderlister/shaderlistertest/def.go

View workflow job for this annotation

GitHub Actions / Test with Go 1.22.x on macos-latest

invalid go:embed: multiple files for type []byte

Check failure on line 75 in internal/shaderlister/shaderlistertest/def.go

View workflow job for this annotation

GitHub Actions / Test with Go 1.23.x on macos-latest

invalid go:embed: multiple files for type []byte

//go:embed resource
var embed2_go embed.FS
21 changes: 21 additions & 0 deletions internal/shaderlister/shaderlistertest/embed2_kage.go
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions internal/shaderlister/shaderlistertest/embed_kage.go
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions internal/shaderlister/shaderlistertest/embed_notkage.go
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions internal/shaderlister/shaderlistertest/resource/embed_kage.go
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions internal/shaderlister/shaderlistertest/resource/embed_notkage.go
Original file line number Diff line number Diff line change
@@ -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 comments on commit 3ebbbee

Please sign in to comment.