/
type.go
188 lines (175 loc) · 3.56 KB
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// 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 {
Main BasicType
Sub []Type
Length int
}
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
}
func (t *Type) String() string {
switch t.Main {
case None:
return "none"
case Bool:
return "bool"
case Int:
return "int"
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:
return fmt.Sprintf("%s[%d]", t.Sub[0].String(), t.Length)
case Struct:
str := "struct{"
sub := make([]string, 0, len(t.Sub))
for _, st := range t.Sub {
sub = append(sub, st.String())
}
str += strings.Join(sub, ",")
str += "}"
return str
default:
return fmt.Sprintf("?(unknown type: %d)", t)
}
}
func (t *Type) FloatNum() int {
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:
return t.Length * t.Sub[0].FloatNum()
default: // TODO: Parse a struct correctly
return -1
}
}
type BasicType int
const (
None BasicType = iota
Bool
Int
Float
Vec2
Vec3
Vec4
Mat2
Mat3
Mat4
Array
Struct
)
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]
case idx == nv+1:
return Type{Main: Vec4}
default:
return localVariableType(p, topBlock, block, idx-(nv+2))
}
default:
return localVariableType(p, topBlock, block, idx)
}
}