-
Notifications
You must be signed in to change notification settings - Fork 12
/
buffer.js
132 lines (113 loc) · 3.65 KB
/
buffer.js
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
import { checkProps } from "./utils.js";
/**
* @typedef {import("./types.js").PexResource} BufferOptions
* @property {Array|import("./types.js").TypedArray|ArrayBuffer} data
* @property {ctx.DataType} [type]
* @property {ctx.Usage} [usage=ctx.Usage.StaticDraw]
* @property {number} offset
*/
const allowedProps = [
"target", // Note: only at creation
"data",
"usage",
"type",
"offset",
];
function createBuffer(ctx, opts) {
checkProps(allowedProps, opts);
const gl = ctx.gl;
console.assert(
opts.target === gl.ARRAY_BUFFER || opts.target === gl.ELEMENT_ARRAY_BUFFER,
"Invalid buffer target",
);
const buffer = {
class: opts.target === gl.ARRAY_BUFFER ? "vertexBuffer" : "indexBuffer",
handle: gl.createBuffer(),
target: opts.target,
usage: opts.usage || gl.STATIC_DRAW,
_update: updateBuffer,
_dispose() {
gl.deleteBuffer(this.handle);
this.handle = null;
},
};
updateBuffer(ctx, buffer, opts);
return buffer;
}
function updateBuffer(ctx, buffer, opts) {
checkProps(allowedProps, opts);
const gl = ctx.gl;
let data = opts.data || opts;
let type = opts.type;
let offset = opts.offset || 0;
if (Array.isArray(data)) {
if (!type) {
if (buffer.target === gl.ARRAY_BUFFER) {
type = ctx.DataType.Float32;
} else if (buffer.target === gl.ELEMENT_ARRAY_BUFFER) {
type = ctx.DataType.Uint16;
}
}
const sourceData = data;
const elemSize = Array.isArray(sourceData[0]) ? sourceData[0].length : 1;
const size = elemSize * sourceData.length;
if (type === ctx.DataType.Float32) {
data = new Float32Array(elemSize === 1 ? sourceData : size);
} else if (type === ctx.DataType.Uint8) {
data = new Uint8Array(elemSize === 1 ? sourceData : size);
} else if (type === ctx.DataType.Uint16) {
data = new Uint16Array(elemSize === 1 ? sourceData : size);
} else if (type === ctx.DataType.Uint32) {
data = new Uint32Array(elemSize === 1 ? sourceData : size);
} else if (type === ctx.DataType.Int8) {
data = new Int8Array(elemSize === 1 ? sourceData : size);
}
if (elemSize > 1) {
for (let i = 0; i < sourceData.length; i++) {
for (let j = 0; j < elemSize; j++) {
const index = i * elemSize + j;
data[index] = sourceData[i][j];
}
}
}
} else if (data instanceof Float32Array) {
type = ctx.DataType.Float32;
} else if (data instanceof Uint8Array) {
type = ctx.DataType.Uint8;
} else if (data instanceof Uint16Array) {
type = ctx.DataType.Uint16;
} else if (data instanceof Uint32Array) {
type = ctx.DataType.Uint32;
} else if (data instanceof Int8Array) {
type = ctx.DataType.Int8;
} else if (data instanceof ArrayBuffer) {
// assuming type was provided
if (!type) {
if (buffer.target === gl.ARRAY_BUFFER) {
type = ctx.DataType.Float32;
} else if (buffer.target === gl.ELEMENT_ARRAY_BUFFER) {
type = ctx.DataType.Uint16;
}
}
} else {
throw new Error(`Unknown buffer data type: ${data.constructor}`);
}
buffer.type = type;
// TODO: is this a valid guess?
buffer.length =
data.length ??
data.byteLength / ctx.DataTypeConstructor[type].BYTES_PER_ELEMENT;
if (ctx.state.vertexArray) {
ctx.state.vertexArray = undefined;
gl.bindVertexArray(null);
}
// TODO: push state, and pop as this can modify existing VBO?
gl.bindBuffer(buffer.target, buffer.handle);
if (offset) {
gl.bufferSubData(buffer.target, offset, data);
} else {
gl.bufferData(buffer.target, data, buffer.usage);
}
buffer.info = ctx.DataTypeConstructor[type].name;
}
export default createBuffer;