Skip to content

Commit dc8aafb

Browse files
christurnbullgreggman
authored andcommitted
The 5th parameter in gl.bufferData "length" was incorrectly being used as the size of the buffer in bytes.
This pull req ensures the size is calculated from either the length of the ArrayBufferView or the size number passed in parameter 2.
1 parent aaed7e2 commit dc8aafb

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

src/augment-api.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,7 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
387387
throw new Error(`no buffer bound to ${target}`);
388388
}
389389
let newSize = 0;
390-
if (length !== undefined) {
391-
newSize = length;
392-
} else if (isBufferSource(src)) {
390+
if (isBufferSource(src)) {
393391
newSize = src.byteLength;
394392
} else if (isNumber(src)) {
395393
newSize = src;

test/tests/buffer-tests.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,25 @@ describe('buffer tests', () => {
4646

4747
gl.bindBuffer(gl.ARRAY_BUFFER, buf1);
4848
const data1 = new Float32Array(25);
49-
const size1 = 15;
50-
gl.bufferData(gl.ARRAY_BUFFER, data1, gl.STATIC_DRAW, 0, size1);
49+
const size1 = data1.length * data1.BYTES_PER_ELEMENT;
50+
const length1 = data1.length;
51+
gl.bufferData(gl.ARRAY_BUFFER, data1, gl.STATIC_DRAW, 0, length1);
5152
tracker.addMemory(size1);
5253

5354
const data1a = new Uint16Array(37);
54-
const size1a = 30;
55-
gl.bufferData(gl.ARRAY_BUFFER, data1a, gl.STATIC_DRAW, 0, size1a);
55+
const size1a = data1a.length * data1a.BYTES_PER_ELEMENT;
56+
const length1a = data1a.length;
57+
gl.bufferData(gl.ARRAY_BUFFER, data1a, gl.STATIC_DRAW, 0, length1a);
5658
tracker.addMemory(size1a - size1);
5759

5860
const buf2 = gl.createBuffer();
5961
tracker.addObjects(1);
6062

6163
gl.bindBuffer(gl.ARRAY_BUFFER, buf2);
6264
const data2 = new Float32Array(55);
63-
const size2 = 41;
64-
gl.bufferData(gl.ARRAY_BUFFER, data2, gl.STATIC_DRAW, 0, size2);
65+
const size2 = data2.length * data2.BYTES_PER_ELEMENT;
66+
const length2 = data2.length;
67+
gl.bufferData(gl.ARRAY_BUFFER, data2, gl.STATIC_DRAW, 0, length2);
6568
tracker.addMemory(size2);
6669

6770
gl.deleteBuffer(buf1);

webgl-memory.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,9 +737,7 @@
737737
throw new Error(`no buffer bound to ${target}`);
738738
}
739739
let newSize = 0;
740-
if (length !== undefined) {
741-
newSize = length;
742-
} else if (isBufferSource(src)) {
740+
if (isBufferSource(src)) {
743741
newSize = src.byteLength;
744742
} else if (isNumber(src)) {
745743
newSize = src;

0 commit comments

Comments
 (0)