2020-05-08 17:46:01 +02:00
|
|
|
// 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"
|
2020-05-10 17:13:08 +02:00
|
|
|
"strings"
|
2020-05-08 17:46:01 +02:00
|
|
|
)
|
|
|
|
|
2020-05-10 16:19:39 +02:00
|
|
|
type basicType int
|
2020-05-08 17:46:01 +02:00
|
|
|
|
|
|
|
// TODO: What about array types?
|
|
|
|
|
|
|
|
const (
|
2020-05-10 16:19:39 +02:00
|
|
|
basicTypeNone basicType = iota
|
|
|
|
basicTypeFloat
|
|
|
|
basicTypeVec2
|
|
|
|
basicTypeVec3
|
|
|
|
basicTypeVec4
|
|
|
|
basicTypeMat2
|
|
|
|
basicTypeMat3
|
|
|
|
basicTypeMat4
|
|
|
|
basicTypeSampler2d
|
2020-05-10 17:13:08 +02:00
|
|
|
basicTypeStruct
|
2020-05-08 17:46:01 +02:00
|
|
|
)
|
|
|
|
|
2020-05-10 17:13:08 +02:00
|
|
|
type structMember struct {
|
|
|
|
name string
|
|
|
|
typ typ
|
|
|
|
tag string
|
|
|
|
}
|
|
|
|
|
|
|
|
type typ struct {
|
|
|
|
basic basicType
|
|
|
|
name string
|
|
|
|
structMembers []structMember
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *typ) isNone() bool {
|
|
|
|
return t.basic == basicTypeNone
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *Shader) parseType(expr ast.Expr) typ {
|
2020-05-08 17:46:01 +02:00
|
|
|
switch t := expr.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
switch t.Name {
|
|
|
|
case "float":
|
2020-05-10 17:13:08 +02:00
|
|
|
return typ{
|
|
|
|
basic: basicTypeFloat,
|
|
|
|
}
|
2020-05-08 17:46:01 +02:00
|
|
|
case "vec2":
|
2020-05-10 17:13:08 +02:00
|
|
|
return typ{
|
|
|
|
basic: basicTypeVec2,
|
|
|
|
}
|
2020-05-08 17:46:01 +02:00
|
|
|
case "vec3":
|
2020-05-10 17:13:08 +02:00
|
|
|
return typ{
|
|
|
|
basic: basicTypeVec3,
|
|
|
|
}
|
2020-05-08 17:46:01 +02:00
|
|
|
case "vec4":
|
2020-05-10 17:13:08 +02:00
|
|
|
return typ{
|
|
|
|
basic: basicTypeVec4,
|
|
|
|
}
|
2020-05-08 17:46:01 +02:00
|
|
|
case "mat2":
|
2020-05-10 17:13:08 +02:00
|
|
|
return typ{
|
|
|
|
basic: basicTypeMat2,
|
|
|
|
}
|
2020-05-08 17:46:01 +02:00
|
|
|
case "mat3":
|
2020-05-10 17:13:08 +02:00
|
|
|
return typ{
|
|
|
|
basic: basicTypeMat3,
|
|
|
|
}
|
2020-05-08 17:46:01 +02:00
|
|
|
case "mat4":
|
2020-05-10 17:13:08 +02:00
|
|
|
return typ{
|
|
|
|
basic: basicTypeMat4,
|
|
|
|
}
|
2020-05-08 17:46:01 +02:00
|
|
|
case "sampler2d":
|
2020-05-10 17:13:08 +02:00
|
|
|
return typ{
|
|
|
|
basic: basicTypeSampler2d,
|
|
|
|
}
|
2020-05-10 16:56:56 +02:00
|
|
|
default:
|
2020-05-10 17:13:08 +02:00
|
|
|
sh.addError(t.Pos(), fmt.Sprintf("unexpected type: %s", t.Name))
|
|
|
|
return typ{}
|
|
|
|
}
|
|
|
|
case *ast.StructType:
|
|
|
|
str := typ{
|
|
|
|
basic: basicTypeStruct,
|
|
|
|
}
|
|
|
|
for _, f := range t.Fields.List {
|
|
|
|
typ := sh.parseType(f.Type)
|
|
|
|
var tag string
|
|
|
|
if f.Tag != nil {
|
|
|
|
tag = f.Tag.Value
|
|
|
|
}
|
|
|
|
for _, n := range f.Names {
|
|
|
|
str.structMembers = append(str.structMembers, structMember{
|
|
|
|
name: n.Name,
|
|
|
|
typ: typ,
|
|
|
|
tag: tag,
|
|
|
|
})
|
|
|
|
}
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
2020-05-10 17:13:08 +02:00
|
|
|
return str
|
|
|
|
default:
|
|
|
|
sh.addError(t.Pos(), fmt.Sprintf("unepxected type: %v", t))
|
|
|
|
return typ{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t typ) dump(indent int) []string {
|
|
|
|
idt := strings.Repeat("\t", indent)
|
|
|
|
|
|
|
|
switch t.basic {
|
|
|
|
case basicTypeStruct:
|
|
|
|
ls := []string{
|
|
|
|
fmt.Sprintf("%sstruct {", idt),
|
|
|
|
}
|
|
|
|
for _, m := range t.structMembers {
|
|
|
|
ls = append(ls, fmt.Sprintf("%s\t%s %s", idt, m.name, m.typ))
|
|
|
|
}
|
|
|
|
ls = append(ls, fmt.Sprintf("%s}", idt))
|
|
|
|
return ls
|
|
|
|
default:
|
|
|
|
return []string{t.basic.String()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t typ) String() string {
|
|
|
|
if t.name != "" {
|
|
|
|
return t.name
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
2020-05-10 17:13:08 +02:00
|
|
|
return t.basic.String()
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 16:19:39 +02:00
|
|
|
func (t basicType) String() string {
|
2020-05-08 17:46:01 +02:00
|
|
|
switch t {
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeNone:
|
2020-05-09 16:38:52 +02:00
|
|
|
return "(none)"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeFloat:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "float"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeVec2:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "vec2"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeVec3:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "vec3"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeVec4:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "vec4"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeMat2:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "mat2"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeMat3:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "mat3"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeMat4:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "mat4"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeSampler2d:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "sampler2d"
|
2020-05-10 17:13:08 +02:00
|
|
|
case basicTypeStruct:
|
|
|
|
return "(struct)"
|
2020-05-08 17:46:01 +02:00
|
|
|
default:
|
|
|
|
return fmt.Sprintf("unknown(%d)", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 16:19:39 +02:00
|
|
|
func (t basicType) numeric() bool {
|
|
|
|
return t != basicTypeNone && t != basicTypeSampler2d
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 16:19:39 +02:00
|
|
|
func (t basicType) glslString() string {
|
2020-05-08 17:46:01 +02:00
|
|
|
switch t {
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeNone:
|
2020-05-09 16:38:52 +02:00
|
|
|
return "?(none)"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeFloat:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "float"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeVec2:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "vec2"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeVec3:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "vec3"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeVec4:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "vec4"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeMat2:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "mat2"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeMat3:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "mat3"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeMat4:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "mat4"
|
2020-05-10 16:19:39 +02:00
|
|
|
case basicTypeSampler2d:
|
2020-05-08 17:46:01 +02:00
|
|
|
return "?(sampler2d)"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("?(%d)", t)
|
|
|
|
}
|
|
|
|
}
|