/
builder.go
127 lines (107 loc) · 2.79 KB
/
builder.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
// Copyright 2015 Matthew Collins
//
// 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 builder provides a simple way to create buffers to upload to the gpu
package builder
import (
"bytes"
"math"
"github.com/thinkofdeath/steven/native"
)
// Types allowed to be used in a buffer.
const (
UnsignedByte Type = 1
Byte Type = 1
UnsignedShort Type = 2
Short Type = 2
Float Type = 4
)
// Type is a type that is allowed in a buffer.
type Type int
// Buffer is a dynamically sized byte buffer
// for creating data to upload to the gpu.
type Buffer struct {
buf bytes.Buffer
elemSize int
scratch [8]byte
}
// New creates a new buffer containing the passed
// types.
func New(types ...Type) *Buffer {
elemSize := 0
for _, t := range types {
elemSize += int(t)
}
b := &Buffer{
elemSize: elemSize,
}
b.buf.Grow(elemSize * 3000)
return b
}
// UnsignedByte appends an unsigned byte to the
// buffer.
func (b *Buffer) UnsignedByte(i byte) {
b.buf.WriteByte(i)
}
// Byte appends a signed byte to the buffer.
func (b *Buffer) Byte(i int8) {
b.UnsignedByte(byte(i))
}
// UnsignedShort writes an unsigned short to the
// buffer
func (b *Buffer) UnsignedShort(i uint16) {
d := b.scratch[:2]
native.Order.PutUint16(d, i)
b.buf.Write(d)
}
// Short writes a short to the buffer.
func (b *Buffer) Short(i int16) {
b.UnsignedShort(uint16(i))
}
func (b *Buffer) UnsignedInt(i uint32) {
d := b.scratch[:4]
native.Order.PutUint32(d, i)
b.buf.Write(d)
}
// Float writes a float to the buffer
func (b *Buffer) Float(f float32) {
d := b.scratch[:4]
i := math.Float32bits(f)
native.Order.PutUint32(d, i)
b.buf.Write(d)
}
// Write appends the passed bytes to this buffer
func (b *Buffer) Write(p []byte) {
b.buf.Write(p)
}
// WriteBuffer copies the passed buffer to this buffer
func (b *Buffer) WriteBuffer(o *Buffer) {
o.buf.WriteTo(&b.buf)
}
// Count returns the number of vertices in the buffer
func (b *Buffer) Count() int {
return b.buf.Len() / b.elemSize
}
// Data returns a byte slice of the buffer
func (b *Buffer) Data() []byte {
return b.buf.Bytes()
}
// ElementSize returns the size of a single vertex in the
// buffer
func (b *Buffer) ElementSize() int {
return b.elemSize
}
// Reset resets the internal buffer ready for reuse
func (b *Buffer) Reset() {
b.buf.Reset()
}