-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdynamicvao.js
270 lines (265 loc) · 9.5 KB
/
dynamicvao.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'use strict'
class DVBO {
constructor(gl,elementSize,nElements,dynamic=true) {
this.elementSize = elementSize;
this.nElements = nElements;
this.a = new Float32Array(nElements*elementSize);
this.created = false;
this.must_update = dynamic;
this.dynamic = dynamic;
this.vbo = gl.createBuffer(); // Awaiting first data upload
}
copy_from(old_dvbo) {
this.a.set(old_dvbo.a.subarray(0,old_dvbo.a.length));
}
subarray(index) {
return this.a.subarray(index*this.elementSize, (index+1)*this.elementSize);
}
rebase(index,arrayfloats) {
arrayfloats.a = this.subarray(index);
}
acquire(index) { // Meant to be overridden
return new ArrayFloats(this.subarray(index));
}
drawMode(gl) {
return this.dynamic ? gl.STREAM_DRAW : gl.STATIC_DRAW;
}
// Creates VBO on GPU (first data upload)
create(gl) {
gl.bindBuffer(gl.ARRAY_BUFFER, this.vbo);
gl.bufferData(gl.ARRAY_BUFFER, this.a, this.drawMode(gl));
}
// Updates VBO on GPU
update(gl) {
//console.log(this.a);
gl.bindBuffer(gl.ARRAY_BUFFER, this.vbo);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.a, 0, this.a.length);
}
// Deletes VBO on GPU
delete(gl) {
gl.deleteBuffer(this.vbo);
}
prepare(gl) { // Prepares for rendering
if (!this.created) {
this.create(gl);
this.created = true;
} else {
if (this.must_update) {
this.update(gl);
if (!this.dynamic) {
this.must_update = false;
}
}
}
}
}
class ScalarDVBO extends DVBO {
constructor(gl,nElements,dynamic=true) {
super(gl,1,nElements,dynamic);
}
acquire(index) {
return new AbstractScalar(this.subarray(index));
}
}
class VecDVBO extends DVBO {
constructor(gl,nElements,dynamic=true) {
super(gl,2,nElements,dynamic);
}
acquire(index) {
return new AbstractVec(this.subarray(index));
}
}
class MatDVBO extends DVBO {
constructor(gl,nElements,dynamic=true) {
super(gl,9,nElements,dynamic);
}
acquire(index) {
return new AbstractMat(this.subarray(index)).set(); // Set to identity matrix
}
}
/*
* A vertex array object that contains only square vertices.
*/
class SimpleVAO {
constructor(gl,program,square_vbo) {
this.square_vbo = square_vbo;
this.vertex_loc = gl.getAttribLocation(program,'vertex');
this.construct(gl);
}
construct(gl) {
this.vao = gl.ext_vao.createVertexArrayOES();
gl.ext_vao.bindVertexArrayOES(this.vao);
gl.bindBuffer(gl.ARRAY_BUFFER,this.square_vbo);
gl.vertexAttribPointer(this.vertex_loc, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(this.vertex_loc);
}
delete(gl) {
gl.ext_vao.deleteVertexArrayOES(this.vao);
}
draw(gl) {
gl.ext_vao.bindVertexArrayOES(this.vao);
gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
}
}
/*
* A pool of index elements for instanced sprite drawing.
*/
class DynamicVAO {
constructor(gl,program,square_vbo,channels,size=1) {
this.square_vbo = square_vbo;
this.vertex_loc = gl.getAttribLocation(program,'vertex');
this.channels = channels;
this.recycled_objects = [];
this.object_indices = new Map();
this.size = size;
this.head = 0;
this.refresh_requested = false;
// Find locations in program
if (this.vertex_loc < 0) {
console.error("Shader missing the absolutely essential 'vertex' location.");
}
this.locations = new Map();
this.malformed_names = new Set();
for (const name in channels) {
const channel = channels[name];
if (channel.type === "scalar" || channel.type === "vec") {
const loc = gl.getAttribLocation(program,name);
if (loc < 0) {
console.error("Could not find attribute",name);
this.malformed_names.add(name);
} else {
this.locations.set(name,loc);
}
} else if (channel.type === "mat") {
const loc_x = gl.getAttribLocation(program,name+'_x');
const loc_y = gl.getAttribLocation(program,name+'_y');
const loc_z = gl.getAttribLocation(program,name+'_z');
if (loc_x < 0 || loc_y < 0) {
console.error("Could not find attribute",name,"because we missed",loc_x<0 ? name+'_x':'',loc_y<0 ? name+'_y':'',loc_z<0 ? name+'_z':'');
this.malformed_names.add(name);
} else {
this.locations.set(name,[loc_x,loc_y,loc_z]);
}
} else {
this.malformed_names.add(name);
}
}
// Construct initial VAO
this.vao = null;
this.construct(gl,size);
}
construct(gl,size) {
this.vao = gl.ext_vao.createVertexArrayOES();
gl.ext_vao.bindVertexArrayOES(this.vao);
gl.bindBuffer(gl.ARRAY_BUFFER,this.square_vbo);
gl.vertexAttribPointer(this.vertex_loc, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(this.vertex_loc);
this.DVBOs = new Map();
for (const name in this.channels) {
if (!this.locations.has(name)) continue; // Skip names that we don't have locations for. We don't have locations for names that weren't in the shader.
const channel = this.channels[name];
const loc = this.locations.get(name);
if (channel.type === "scalar") {
const DVBO = new ScalarDVBO(gl,size,channel.dynamic);
this.DVBOs.set(name,DVBO);
gl.bindBuffer(gl.ARRAY_BUFFER,DVBO.vbo);
gl.vertexAttribPointer(loc, 1, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(loc);
gl.ext_instance.vertexAttribDivisorANGLE(loc, 1);
} else if (channel.type === "vec") {
const DVBO = new VecDVBO(gl,size,channel.dynamic);
this.DVBOs.set(name,DVBO);
gl.bindBuffer(gl.ARRAY_BUFFER,DVBO.vbo);
gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(loc);
gl.ext_instance.vertexAttribDivisorANGLE(loc, 1);
} else if (channel.type === "mat") {
const DVBO = new MatDVBO(gl,size,channel.dynamic);
this.DVBOs.set(name,DVBO);
gl.bindBuffer(gl.ARRAY_BUFFER,DVBO.vbo);
gl.vertexAttribPointer(loc[0], 3, gl.FLOAT, false, 4*9, 4*0);
gl.enableVertexAttribArray(loc[0]);
gl.ext_instance.vertexAttribDivisorANGLE(loc[0], 1);
gl.vertexAttribPointer(loc[1], 3, gl.FLOAT, false, 4*9, 4*3);
gl.enableVertexAttribArray(loc[1]);
gl.ext_instance.vertexAttribDivisorANGLE(loc[1], 1);
gl.vertexAttribPointer(loc[2], 3, gl.FLOAT, false, 4*9, 4*6);
gl.enableVertexAttribArray(loc[2]);
gl.ext_instance.vertexAttribDivisorANGLE(loc[2], 1);
}
}
this.size = size;
}
expand(gl,size) {
this.delete(gl);
const old_DVBOs = this.DVBOs;
this.construct(gl,size);
// Move over backing
for (const [object,index] of this.object_indices) {
for (const name in object) {
this.DVBOs.get(name).rebase(index,object[name]);
}
}
// Copy arrays
for (const [name,old_DVBO] of old_DVBOs) {
this.DVBOs.get(name).copy_from(old_DVBO);
}
}
delete(gl) {
for (const [name,dvbo] of this.DVBOs) {
dvbo.delete(gl);
}
gl.ext_vao.deleteVertexArrayOES(this.vao);
}
make_object(index) {
const object = {};
for (const name in this.channels) {
object[name] = this.DVBOs.get(name).acquire(index);
}
this.object_indices.set(object,index); // Saving for rebuilding
return object;
}
acquire(gl) {
if (this.recycled_objects.length > 0) {
// We can re-use a recycled object.
this.request_refresh();
return this.recycled_objects.pop();
} else {
if (this.head < this.size) {
// Create a new object
const index = this.head;
this.head += 1;
this.request_refresh();
return this.make_object(index);
} else {
// Out of space!
this.expand(gl,this.size * 2);
return this.acquire(gl);
}
}
}
relenquish(object) {
for (const name in object) {
object[name].zeroeq(); // Zero out relenquished objects
}
this.recycled_objects.push(object);
this.request_refresh();
}
// Flag all objects for data refresh, including static ones.
request_refresh() {
this.refresh_requested = true;
}
prepare(gl) {
for (const [name,dvbo] of this.DVBOs) {
if (this.refresh_requested) {
dvbo.must_update = true;
}
dvbo.prepare(gl);
}
this.refresh_requested = false;
}
draw(gl) {
gl.ext_vao.bindVertexArrayOES(this.vao);
gl.ext_instance.drawArraysInstancedANGLE(gl.TRIANGLE_STRIP,0,4,this.head);
}
}