/
gltypes.go
170 lines (149 loc) · 3.75 KB
/
gltypes.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
package gli
import "fmt"
//go:generate stringer -type=ShaderType
type ShaderType uint32
const (
ShaderTypeVertex ShaderType = iota + 1
ShaderTypeGeometry
ShaderTypeFragment
)
type ShaderId uint32
type ProgramId uint32
//go:generate stringer -type=BaseType
type BaseType uint32
const (
BaseTypeFloat BaseType = iota + 1
BaseTypeInt
BaseTypeUnsignedInt
BaseTypeBool
)
//go:generate stringer -type=ResourceType
type ResourceType uint32
const (
ResourceTypeUniform ResourceType = iota + 1
ResourceTypeAttribute
)
//go:generate stringer -type=BufferUsage
type BufferUsage uint32
const (
BufferUsageStreamDraw BufferUsage = iota + 1
BufferUsageStreamRead
BufferUsageStreamCopy
BufferUsageStaticDraw
BufferUsageStaticRead
BufferUsageStaticCopy
BufferUsageDynamicDraw
BufferUsageDynamicRead
BufferUsageDynamicCopy
)
//go:generate stringer -type=BufferTarget
type BufferTarget uint32
const (
BufferTargetArray BufferTarget = iota + 1
BufferTargetCopyRead
BufferTargetCopyWrite
BufferTargetElementArray
BufferTargetPixelPack
BufferTargetPixelUnpack
BufferTargetTexture
BufferTargetTransformFeedback
BufferTargetUniform
)
//go:generate stringer -type=SamplerType
type SamplerType uint32
const (
SamplerType1d SamplerType = iota + 1
SamplerType2d
SamplerType3d
SamplerTypeCude
SamplerType2dRect
SamplerType1dArray
SamplerType2dArray
SamplerTypeCubeArray
SamplerTypeBuffer
SamplerType2dMS
SamplerType2dMSArray
SamplerType1dShadow
SamplerType2dShadow
SamplerTypeCubeShadow
SamplerType2dRectShadow
SamplerType1dArrayShadow
SamplerType2dArrayShadow
SamplerTypeCubeArrayShadow
)
type DataType struct {
Base BaseType
Sampler SamplerType
Cols byte
Matrix bool
Size uint
}
func (dt DataType) GoString() string {
return fmt.Sprintf("gli.DataType{Base: %s, Sampler: %s, Cols: %d, Matrix: %t, Size: %d}", dt.Base.String(), dt.Sampler.String(), dt.Cols, dt.Matrix, dt.Size)
}
func (dt DataType) IsValid() error {
if dt.Base > BaseTypeBool {
return fmt.Errorf("Invalid DataType %#v: Unknown BaseType", dt)
}
if dt.Base == 0 {
return fmt.Errorf("Invalid DataType %#v: No base type", dt)
}
if dt.Sampler > 0 {
return dt.isValidSampler()
}
if dt.Matrix {
return dt.isValidMatrix()
}
if dt.Cols > 0 {
return dt.isValidVector()
}
return nil
}
func (dt DataType) isValidSampler() error {
if dt.Sampler > SamplerTypeCubeArrayShadow {
return fmt.Errorf("Invalid DataType %#v: Unknown SamplerType", dt)
}
if dt.Matrix {
return fmt.Errorf("Invalid DataType %#v: Sampler types not allowed to be matrices", dt)
}
if dt.Cols > 0 {
return fmt.Errorf("Invalid DataType %#v: Sampler types not allowed to have columns or rows", dt)
}
if dt.Sampler > SamplerType1dShadow {
if dt.Base != BaseTypeFloat {
return fmt.Errorf("Invalid DataType %#v: Shadow samplers can only have BaseType Float", dt)
}
}
if dt.Base != BaseTypeFloat && dt.Base != BaseTypeInt && dt.Base != BaseTypeUnsignedInt {
return fmt.Errorf("Invalid DataTyp %#v: Only float, int and uint samplers are allowed", dt)
}
return nil
}
func (dt DataType) isValidMatrix() error {
if dt.Cols > 4 {
return fmt.Errorf("Invalid DataType %#v: Matrix dimensions too large", dt)
}
if dt.Cols <= 1 {
return fmt.Errorf("Invalid DataType %#v: Matrix dimensions too small", dt)
}
if dt.Base != BaseTypeFloat {
return fmt.Errorf("Invalid DataType %#v: Only Float or Double matrices are allowed", dt)
}
return nil
}
func (dt DataType) isValidVector() error {
if dt.Cols > 4 {
return fmt.Errorf("Invalid DataType %#v: Vector dimensions too large", dt)
}
if dt.Cols == 1 {
return fmt.Errorf("Invalid DataType %#v: Vector dimensions too small", dt)
}
return nil
}
func (dt DataType) Location() uint {
base := uint(1)
if dt.Matrix {
base = uint(dt.Cols)
}
return base * uint(dt.Size)
}