Skip to content

Commit 63fad7e

Browse files
committed
make VertexArrayBuffer more generic
1 parent 3939850 commit 63fad7e

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/video/webgl/buffer/vertex.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77

88
export default class VertexArrayBuffer {
99

10-
constructor(vertex_size, vertex_per_quad) {
10+
constructor(vertex_size, vertex_per_obj) {
1111
// the size of one vertex in float
1212
this.vertexSize = vertex_size;
13-
// size of a quad in vertex
14-
this.quadSize = vertex_per_quad;
13+
// size of an object in vertex
14+
this.objSize = vertex_per_obj;
1515
// the maximum number of vertices the vertex array buffer can hold
16-
this.maxVertex = 256;
16+
this.maxVertex = 256; // (note: this seems to be the sweet spot performance-wise when using batching)
1717
// the current number of vertices added to the vertex array buffer
1818
this.vertexCount = 0;
1919

2020
// the actual vertex data buffer
21-
this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.quadSize);
21+
this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.objSize);
2222
// Float32 and Uint32 view of the vertex data array buffer
2323
this.bufferF32 = new Float32Array(this.buffer);
2424
this.bufferU32 = new Uint32Array(this.buffer);
@@ -37,22 +37,26 @@
3737
* return true if full
3838
* @ignore
3939
*/
40-
isFull(vertex = this.quadSize) {
40+
isFull(vertex = this.objSize) {
4141
return (this.vertexCount + vertex >= this.maxVertex);
4242
}
4343

4444
/**
4545
* resize the vertex buffer, retaining its original contents
4646
* @ignore
4747
*/
48-
resize() {
49-
// double the vertex size
50-
this.maxVertex <<= 1;
48+
resize(vertexCount) {
49+
50+
while (vertexCount > this.maxVertex) {
51+
// double the vertex size
52+
this.maxVertex <<= 1;
53+
}
54+
5155
// save a reference to the previous data
5256
var data = this.bufferF32;
5357

5458
// recreate ArrayBuffer and views
55-
this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.quadSize);
59+
this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.objSize);
5660
this.bufferF32 = new Float32Array(this.buffer);
5761
this.bufferU32 = new Uint32Array(this.buffer);
5862

@@ -70,19 +74,19 @@
7074
var offset = this.vertexCount * this.vertexSize;
7175

7276
if (this.vertexCount >= this.maxVertex) {
73-
this.resize();
77+
this.resize(this.vertexCount);
7478
}
7579

76-
this.bufferF32[offset + 0] = x;
77-
this.bufferF32[offset + 1] = y;
80+
this.bufferF32[offset] = x;
81+
this.bufferF32[++offset] = y;
7882

7983
if (typeof u !== "undefined") {
80-
this.bufferF32[offset + 2] = u;
81-
this.bufferF32[offset + 3] = v;
84+
this.bufferF32[++offset] = u;
85+
this.bufferF32[++offset] = v;
8286
}
8387

8488
if (typeof tint !== "undefined") {
85-
this.bufferU32[offset + 4] = tint;
89+
this.bufferU32[++offset] = tint;
8690
}
8791

8892
this.vertexCount++;

0 commit comments

Comments
 (0)