mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
parent
4fa52dcc56
commit
b3df3a7864
169
internal/shader/shader.go
Normal file
169
internal/shader/shader.go
Normal file
@ -0,0 +1,169 @@
|
||||
// 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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
varyingStructName = "VertexOut"
|
||||
)
|
||||
|
||||
var (
|
||||
kageTagRe = regexp.MustCompile("^`" + `kage:\"(.+)\"` + "`$")
|
||||
)
|
||||
|
||||
type nameAndType struct {
|
||||
name string
|
||||
typ typ
|
||||
}
|
||||
|
||||
type Shader struct {
|
||||
// position is the field name of VertexOut that represents a vertex position (gl_Position in GLSL).
|
||||
position nameAndType
|
||||
|
||||
// varyings is a set of varying variables.
|
||||
varyings []nameAndType
|
||||
|
||||
errs []string
|
||||
}
|
||||
|
||||
type ParseError struct {
|
||||
errs []string
|
||||
}
|
||||
|
||||
func (p *ParseError) Error() string {
|
||||
return strings.Join(p.errs, "\n")
|
||||
}
|
||||
|
||||
func NewShader(src []byte) (*Shader, error) {
|
||||
f, err := parser.ParseFile(token.NewFileSet(), "", src, parser.AllErrors)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s := &Shader{}
|
||||
s.parse(f)
|
||||
|
||||
if len(s.errs) > 0 {
|
||||
return nil, &ParseError{s.errs}
|
||||
}
|
||||
|
||||
// TODO: Make a call graph and reorder the elements.
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Shader) parse(f *ast.File) {
|
||||
// TODO: Accumulate errors
|
||||
for name, obj := range f.Scope.Objects {
|
||||
switch name {
|
||||
case varyingStructName:
|
||||
s.parseVaryingStruct(obj)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (sh *Shader) parseVaryingStruct(obj *ast.Object) {
|
||||
name := obj.Name
|
||||
if obj.Kind != ast.Typ {
|
||||
sh.errs = append(sh.errs, fmt.Sprintf("%s must be a type but %s", name, obj.Kind))
|
||||
return
|
||||
}
|
||||
t := obj.Decl.(*ast.TypeSpec).Type
|
||||
s, ok := t.(*ast.StructType)
|
||||
if !ok {
|
||||
sh.errs = append(sh.errs, fmt.Sprintf("%s must be a struct but not", name))
|
||||
return
|
||||
}
|
||||
|
||||
for _, f := range s.Fields.List {
|
||||
if f.Tag != nil {
|
||||
tag := f.Tag.Value
|
||||
m := kageTagRe.FindStringSubmatch(tag)
|
||||
if m == nil {
|
||||
sh.errs = append(sh.errs, fmt.Sprintf("invalid struct tag: %s", tag))
|
||||
continue
|
||||
}
|
||||
if m[1] != "position" {
|
||||
sh.errs = append(sh.errs, fmt.Sprintf("struct tag value must be position in %s but %s", varyingStructName, m[1]))
|
||||
continue
|
||||
}
|
||||
if len(f.Names) != 1 {
|
||||
sh.errs = append(sh.errs, fmt.Sprintf("position members must be one"))
|
||||
continue
|
||||
}
|
||||
t, err := parseType(f.Type)
|
||||
if err != nil {
|
||||
sh.errs = append(sh.errs, err.Error())
|
||||
continue
|
||||
}
|
||||
if t != typVec4 {
|
||||
sh.errs = append(sh.errs, fmt.Sprintf("position must be vec4 but %s", t))
|
||||
continue
|
||||
}
|
||||
sh.position = nameAndType{
|
||||
name: f.Names[0].Name,
|
||||
typ: t,
|
||||
}
|
||||
continue
|
||||
}
|
||||
t, err := parseType(f.Type)
|
||||
if err != nil {
|
||||
sh.errs = append(sh.errs, err.Error())
|
||||
continue
|
||||
}
|
||||
if !t.numeric() {
|
||||
sh.errs = append(sh.errs, fmt.Sprintf("members in %s must be numeric but %s", varyingStructName, t))
|
||||
continue
|
||||
}
|
||||
for _, n := range f.Names {
|
||||
sh.varyings = append(sh.varyings, nameAndType{
|
||||
name: n.Name,
|
||||
typ: t,
|
||||
})
|
||||
}
|
||||
}
|
||||
sort.Slice(sh.varyings, func(a, b int) bool {
|
||||
return sh.varyings[a].name < sh.varyings[b].name
|
||||
})
|
||||
}
|
||||
|
||||
// Dump dumps the shader state in an intermediate language.
|
||||
func (s *Shader) Dump() string {
|
||||
var lines []string
|
||||
|
||||
lines = append(lines, fmt.Sprintf("var %s varying %s // position", s.position.name, s.position.typ))
|
||||
for _, v := range s.varyings {
|
||||
lines = append(lines, fmt.Sprintf("var %s varying %s", v.name, v.typ))
|
||||
}
|
||||
return strings.Join(lines, "\n") + "\n"
|
||||
}
|
||||
|
||||
func (s *Shader) GlslVertex() string {
|
||||
var lines []string
|
||||
|
||||
for _, v := range s.varyings {
|
||||
// TODO: variable names must be escaped not to conflict with keywords.
|
||||
lines = append(lines, fmt.Sprintf("varying %s %s;", v.typ.glslString(), v.name))
|
||||
}
|
||||
return strings.Join(lines, "\n") + "\n"
|
||||
}
|
53
internal/shader/shader_test.go
Normal file
53
internal/shader/shader_test.go
Normal file
@ -0,0 +1,53 @@
|
||||
// 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 (
|
||||
"testing"
|
||||
|
||||
. "github.com/hajimehoshi/ebiten/internal/shader"
|
||||
)
|
||||
|
||||
func TestGlsl(t *testing.T) {
|
||||
tests := []struct {
|
||||
In string
|
||||
Dump string
|
||||
}{
|
||||
{
|
||||
In: `package main
|
||||
|
||||
type VertexOut struct {
|
||||
Position vec4 ` + "`kage:\"position\"`" + `
|
||||
TexCoord vec2
|
||||
Color vec4
|
||||
}
|
||||
`,
|
||||
Dump: `var Position varying vec4 // position
|
||||
var Color varying vec4
|
||||
var TexCoord varying vec2
|
||||
`,
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
s, err := NewShader([]byte(tc.In))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
continue
|
||||
}
|
||||
if got, want := s.Dump(), tc.Dump; got != want {
|
||||
t.Errorf("got: %v, want: %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
125
internal/shader/type.go
Normal file
125
internal/shader/type.go
Normal file
@ -0,0 +1,125 @@
|
||||
// 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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
)
|
||||
|
||||
type typ int
|
||||
|
||||
// TODO: What about array types?
|
||||
|
||||
const (
|
||||
typBool typ = iota
|
||||
typInt
|
||||
typFloat
|
||||
typVec2
|
||||
typVec3
|
||||
typVec4
|
||||
typMat2
|
||||
typMat3
|
||||
typMat4
|
||||
typSampler2d
|
||||
)
|
||||
|
||||
func parseType(expr ast.Expr) (typ, error) {
|
||||
switch t := expr.(type) {
|
||||
case *ast.Ident:
|
||||
switch t.Name {
|
||||
case "bool":
|
||||
return typBool, nil
|
||||
case "int":
|
||||
return typInt, nil
|
||||
case "float":
|
||||
return typFloat, nil
|
||||
case "vec2":
|
||||
return typVec2, nil
|
||||
case "vec3":
|
||||
return typVec3, nil
|
||||
case "vec4":
|
||||
return typVec4, nil
|
||||
case "mat2":
|
||||
return typMat2, nil
|
||||
case "mat3":
|
||||
return typMat3, nil
|
||||
case "mat4":
|
||||
return typMat4, nil
|
||||
case "sampler2d":
|
||||
return typSampler2d, nil
|
||||
}
|
||||
// TODO: Parse array types
|
||||
}
|
||||
return 0, fmt.Errorf("invalid type: %s", expr)
|
||||
}
|
||||
|
||||
func (t typ) String() string {
|
||||
switch t {
|
||||
case typBool:
|
||||
return "bool"
|
||||
case typInt:
|
||||
return "int"
|
||||
case typFloat:
|
||||
return "float"
|
||||
case typVec2:
|
||||
return "vec2"
|
||||
case typVec3:
|
||||
return "vec3"
|
||||
case typVec4:
|
||||
return "vec4"
|
||||
case typMat2:
|
||||
return "mat2"
|
||||
case typMat3:
|
||||
return "mat3"
|
||||
case typMat4:
|
||||
return "mat4"
|
||||
case typSampler2d:
|
||||
return "sampler2d"
|
||||
default:
|
||||
return fmt.Sprintf("unknown(%d)", t)
|
||||
}
|
||||
}
|
||||
|
||||
func (t typ) numeric() bool {
|
||||
return t != typSampler2d
|
||||
}
|
||||
|
||||
func (t typ) glslString() string {
|
||||
switch t {
|
||||
case typBool:
|
||||
return "bool"
|
||||
case typInt:
|
||||
return "int"
|
||||
case typFloat:
|
||||
return "float"
|
||||
case typVec2:
|
||||
return "vec2"
|
||||
case typVec3:
|
||||
return "vec3"
|
||||
case typVec4:
|
||||
return "vec4"
|
||||
case typMat2:
|
||||
return "mat2"
|
||||
case typMat3:
|
||||
return "mat3"
|
||||
case typMat4:
|
||||
return "mat4"
|
||||
case typSampler2d:
|
||||
return "?(sampler2d)"
|
||||
default:
|
||||
return fmt.Sprintf("?(%d)", t)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user