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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type typ int
|
|
|
|
|
|
|
|
// TODO: What about array types?
|
|
|
|
|
|
|
|
const (
|
2020-05-09 16:38:52 +02:00
|
|
|
typNone typ = iota
|
2020-05-08 17:46:01 +02:00
|
|
|
typFloat
|
|
|
|
typVec2
|
|
|
|
typVec3
|
|
|
|
typVec4
|
|
|
|
typMat2
|
|
|
|
typMat3
|
|
|
|
typMat4
|
|
|
|
typSampler2d
|
|
|
|
)
|
|
|
|
|
2020-05-09 19:01:28 +02:00
|
|
|
func 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-09 19:01:28 +02:00
|
|
|
return typFloat
|
2020-05-08 17:46:01 +02:00
|
|
|
case "vec2":
|
2020-05-09 19:01:28 +02:00
|
|
|
return typVec2
|
2020-05-08 17:46:01 +02:00
|
|
|
case "vec3":
|
2020-05-09 19:01:28 +02:00
|
|
|
return typVec3
|
2020-05-08 17:46:01 +02:00
|
|
|
case "vec4":
|
2020-05-09 19:01:28 +02:00
|
|
|
return typVec4
|
2020-05-08 17:46:01 +02:00
|
|
|
case "mat2":
|
2020-05-09 19:01:28 +02:00
|
|
|
return typMat2
|
2020-05-08 17:46:01 +02:00
|
|
|
case "mat3":
|
2020-05-09 19:01:28 +02:00
|
|
|
return typMat3
|
2020-05-08 17:46:01 +02:00
|
|
|
case "mat4":
|
2020-05-09 19:01:28 +02:00
|
|
|
return typMat4
|
2020-05-08 17:46:01 +02:00
|
|
|
case "sampler2d":
|
2020-05-09 19:01:28 +02:00
|
|
|
return typSampler2d
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
|
|
|
// TODO: Parse array types
|
|
|
|
}
|
2020-05-09 19:01:28 +02:00
|
|
|
return typNone
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t typ) String() string {
|
|
|
|
switch t {
|
2020-05-09 16:38:52 +02:00
|
|
|
case typNone:
|
|
|
|
return "(none)"
|
2020-05-08 17:46:01 +02:00
|
|
|
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 {
|
2020-05-09 16:38:52 +02:00
|
|
|
return t != typNone && t != typSampler2d
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t typ) glslString() string {
|
|
|
|
switch t {
|
2020-05-09 16:38:52 +02:00
|
|
|
case typNone:
|
|
|
|
return "?(none)"
|
2020-05-08 17:46:01 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|