ebiten/internal/shaderir/type.go

119 lines
2.1 KiB
Go
Raw Normal View History

2020-05-11 17:19:42 +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 shaderir
import (
"fmt"
"strings"
)
type Type struct {
2020-05-12 16:32:32 +02:00
Main BasicType
Sub []Type
Length int
2020-05-11 17:19:42 +02:00
}
func (t *Type) serialize() string {
2020-05-12 16:32:32 +02:00
switch t.Main {
2020-05-11 17:19:42 +02:00
case None:
return "none"
case Bool:
return "bool"
2020-05-15 20:10:03 +02:00
case Int:
return "int"
2020-05-11 17:19:42 +02:00
case Float:
return "float"
case Vec2:
return "vec2"
case Vec3:
return "vec3"
case Vec4:
return "vec4"
case Mat2:
return "mat2"
case Mat3:
return "mat3"
case Mat4:
return "mat4"
2020-05-16 16:33:17 +02:00
case Sampler2D:
return "sampler2D"
2020-05-11 17:19:42 +02:00
case Array:
2020-05-12 16:32:32 +02:00
return fmt.Sprintf("%s[%d]", t.Sub[0].serialize(), t.Length)
2020-05-11 17:19:42 +02:00
case Struct:
str := "struct{"
2020-05-12 16:32:32 +02:00
sub := make([]string, 0, len(t.Sub))
for _, st := range t.Sub {
2020-05-11 17:19:42 +02:00
sub = append(sub, st.serialize())
}
str += strings.Join(sub, ",")
str += "}"
return str
default:
return fmt.Sprintf("?(unknown type: %d)", t)
}
}
type BasicType int
const (
None BasicType = iota
Bool
2020-05-15 20:10:03 +02:00
Int
2020-05-11 17:19:42 +02:00
Float
Vec2
Vec3
Vec4
Mat2
Mat3
Mat4
2020-05-16 16:33:17 +02:00
Sampler2D
2020-05-11 17:19:42 +02:00
Array
Struct
)
func (t BasicType) Glsl() string {
switch t {
case None:
return "?(none)"
case Bool:
return "bool"
2020-05-15 20:10:03 +02:00
case Int:
return "Int"
2020-05-11 17:19:42 +02:00
case Float:
return "float"
case Vec2:
return "vec2"
case Vec3:
return "vec3"
case Vec4:
return "vec4"
case Mat2:
return "mat2"
case Mat3:
return "mat3"
case Mat4:
return "mat4"
2020-05-16 16:33:17 +02:00
case Sampler2D:
return "sampler2D"
2020-05-11 17:19:42 +02:00
case Array:
// First-class array is not available on GLSL ES 2.
return "?(array)"
case Struct:
return "?(struct)"
default:
return fmt.Sprintf("?(unknown type: %d)", t)
}
}