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
|
|
|
}
|
|
|
|
|
2020-06-10 17:29:01 +02:00
|
|
|
func (t *Type) Equal(rhs *Type) bool {
|
|
|
|
if t.Main != rhs.Main {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if t.Length != rhs.Length {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(t.Sub) != len(rhs.Sub) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, s := range t.Sub {
|
|
|
|
if !s.Equal(&rhs.Sub[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-20 11:10:23 +02:00
|
|
|
func (t *Type) String() 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"
|
|
|
|
case Array:
|
2020-06-20 11:10:23 +02:00
|
|
|
return fmt.Sprintf("%s[%d]", t.Sub[0].String(), 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-06-20 11:10:23 +02:00
|
|
|
sub = append(sub, st.String())
|
2020-05-11 17:19:42 +02:00
|
|
|
}
|
|
|
|
str += strings.Join(sub, ",")
|
|
|
|
str += "}"
|
|
|
|
return str
|
|
|
|
default:
|
2021-11-01 16:05:03 +01:00
|
|
|
return fmt.Sprintf("?(unknown type: %d)", t.Main)
|
2020-05-11 17:19:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:52:15 +02:00
|
|
|
func (t *Type) FloatCount() int {
|
2020-07-30 04:27:36 +02:00
|
|
|
switch t.Main {
|
|
|
|
case Float:
|
|
|
|
return 1
|
|
|
|
case Vec2:
|
|
|
|
return 2
|
|
|
|
case Vec3:
|
|
|
|
return 3
|
|
|
|
case Vec4:
|
|
|
|
return 4
|
|
|
|
case Mat2:
|
|
|
|
return 4
|
|
|
|
case Mat3:
|
|
|
|
return 9
|
|
|
|
case Mat4:
|
|
|
|
return 16
|
|
|
|
case Array:
|
2022-07-12 18:52:15 +02:00
|
|
|
return t.Length * t.Sub[0].FloatCount()
|
2020-07-30 04:27:36 +02:00
|
|
|
default: // TODO: Parse a struct correctly
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 19:26:33 +02:00
|
|
|
func (t *Type) IsVector() bool {
|
|
|
|
switch t.Main {
|
|
|
|
case Vec2, Vec3, Vec4:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-13 11:17:44 +01:00
|
|
|
func (t *Type) IsMatrix() bool {
|
|
|
|
switch t.Main {
|
|
|
|
case Mat2, Mat3, Mat4:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:19:42 +02:00
|
|
|
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
|
|
|
|
Array
|
|
|
|
Struct
|
|
|
|
)
|
2020-09-09 18:38:54 +02:00
|
|
|
|
|
|
|
func descendantLocalVars(block, target *Block) ([]Type, bool) {
|
|
|
|
if block == target {
|
|
|
|
return block.LocalVars, true
|
|
|
|
}
|
|
|
|
|
|
|
|
var ts []Type
|
|
|
|
for _, s := range block.Stmts {
|
|
|
|
for _, b := range s.Blocks {
|
|
|
|
if ts2, found := descendantLocalVars(b, target); found {
|
|
|
|
n := b.LocalVarIndexOffset - block.LocalVarIndexOffset
|
|
|
|
ts = append(ts, block.LocalVars[:n]...)
|
|
|
|
ts = append(ts, ts2...)
|
|
|
|
return ts, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func localVariableType(p *Program, topBlock, block *Block, absidx int) Type {
|
|
|
|
// TODO: Rename this function (truly-local variable?)
|
|
|
|
var ts []Type
|
|
|
|
for _, f := range p.Funcs {
|
|
|
|
if f.Block == topBlock {
|
|
|
|
ts = append(f.InParams, f.OutParams...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ts2, _ := descendantLocalVars(topBlock, block)
|
|
|
|
ts = append(ts, ts2...)
|
|
|
|
return ts[absidx]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Program) LocalVariableType(topBlock, block *Block, idx int) Type {
|
|
|
|
switch topBlock {
|
|
|
|
case p.VertexFunc.Block:
|
|
|
|
na := len(p.Attributes)
|
|
|
|
nv := len(p.Varyings)
|
|
|
|
switch {
|
|
|
|
case idx < na:
|
|
|
|
return p.Attributes[idx]
|
|
|
|
case idx == na:
|
|
|
|
return Type{Main: Vec4}
|
|
|
|
case idx < na+nv+1:
|
|
|
|
return p.Varyings[idx-na-1]
|
|
|
|
default:
|
|
|
|
return localVariableType(p, topBlock, block, idx-(na+nv+1))
|
|
|
|
}
|
|
|
|
case p.FragmentFunc.Block:
|
|
|
|
nv := len(p.Varyings)
|
|
|
|
switch {
|
|
|
|
case idx == 0:
|
|
|
|
return Type{Main: Vec4}
|
|
|
|
case idx < nv+1:
|
|
|
|
return p.Varyings[idx-1]
|
|
|
|
default:
|
2022-08-16 18:01:23 +02:00
|
|
|
return localVariableType(p, topBlock, block, idx-(nv+1))
|
2020-09-09 18:38:54 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return localVariableType(p, topBlock, block, idx)
|
|
|
|
}
|
|
|
|
}
|