-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffer.go
144 lines (120 loc) · 2.37 KB
/
buffer.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
package gfx
import "github.com/axe/axe-go/pkg/data"
type DataType int
const (
Float DataType = iota
Double
Byte
UByte
Short
UShort
Int
UInt
)
func (d DataType) Bytes() int {
switch d {
case Double:
return 8
case Float, Int, UInt:
return 4
case Short, UShort:
return 2
case Byte, UByte:
return 1
}
return 0
}
func (d DataType) Write(out *data.Bytes, v float32) {
switch d {
case Double:
out.PutFloat64(float64(v))
case Float:
out.PutFloat32(v)
case Int:
out.PutInt32(int32(v))
case UInt:
out.PutUint32(uint32(v))
case Short:
out.PutInt16(int16(v))
case UShort:
out.PutUint16(uint16(v))
case Byte:
out.PutInt8(int8(v))
case UByte:
out.PutUint8(uint8(v))
}
}
type AttributeType int
const (
TypeVertex AttributeType = iota
TypeColor
TypeNormal
TypeTexture
TypeGeneric
)
type Frequency int
const (
Never Frequency = iota
Sometimes
Always
)
type BufferAttribute struct {
Name string
Data DataType
Size int
Type AttributeType
Offset int
Location int
}
func (ba *BufferAttribute) SetName(name string) {
ba.Name = name
}
type BufferFormat struct {
Attributes []BufferAttribute
ByType [5]*BufferAttribute
Stride int
Frequency Frequency
Draw, Read bool
}
func (f *BufferFormat) Add(data DataType, size int, attr AttributeType) *BufferAttribute {
i := len(f.Attributes)
f.Attributes = append(f.Attributes, BufferAttribute{
Data: data,
Size: size,
Type: attr,
Offset: f.Stride,
})
f.Stride += data.Bytes() * size
a := &f.Attributes[i]
f.ByType[a.Type] = a
return a
}
func (f *BufferFormat) Get(attr AttributeType) *BufferAttribute {
return f.ByType[attr]
}
func (f *BufferFormat) AddVertex(data DataType, size int) *BufferAttribute {
return f.Add(data, size, TypeVertex)
}
func (f *BufferFormat) AddColor(data DataType, size int) *BufferAttribute {
return f.Add(data, size, TypeColor)
}
func (f *BufferFormat) AddNormal() *BufferAttribute {
return f.Add(Float, 3, TypeNormal)
}
func (f *BufferFormat) AddTexture() *BufferAttribute {
return f.Add(Float, 2, TypeTexture)
}
func (f *BufferFormat) AddGeneric(data DataType, size int) *BufferAttribute {
return f.Add(data, size, TypeGeneric)
}
func (f *BufferFormat) Create(capacity int) *Buffer {
return &Buffer{
Format: f,
Data: data.NewBytes(capacity),
}
}
type Buffer struct {
Format *BufferFormat
Data *data.Bytes
Metadata any
}