Skip to content

Commit 95e5c56

Browse files
dominicpriorgreggman
authored andcommitted
Make sure gl.vertexAttribDivisor exists
Previously, this would have failed on WebGL1.
1 parent 3d94eaf commit 95e5c56

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/attributes.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ function makeTypedArray(array, name) {
198198
* @property {boolean} [normalize] whether or not to normalize the data. Default = false
199199
* @property {number} [offset] offset into buffer in bytes. Default = 0
200200
* @property {number} [stride] the stride in bytes per element. Default = 0
201-
* @property {number} [divisor] the divisor in instances. Default = 0
202-
* where as anything else = do call it with this value
201+
* @property {number} [divisor] the divisor in instances. Default = 0. Requires WebGL2 or the ANGLE_instanced_arrays extension.
203202
* @property {WebGLBuffer} buffer the buffer that contains the data for this attribute
204203
* @property {number} [drawType] the draw type passed to gl.bufferData. Default = gl.STATIC_DRAW
205204
* @memberOf module:twgl
@@ -221,8 +220,7 @@ function makeTypedArray(array, name) {
221220
* @property {boolean} [normalize] normalize for `vertexAttribPointer`. Default is true if type is `Int8Array` or `Uint8Array` otherwise false.
222221
* @property {number} [stride] stride for `vertexAttribPointer`. Default = 0
223222
* @property {number} [offset] offset for `vertexAttribPointer`. Default = 0
224-
* @property {number} [divisor] divisor for `vertexAttribDivisor`. Default = 0
225-
* where as anything else = do call it with this value
223+
* @property {number} [divisor] divisor for `vertexAttribDivisor`. Default = 0. Requires WebGL2 or the ANGLE_instanced_arrays extension.
226224
* @property {string} [attrib] name of attribute this array maps to. Defaults to same name as array prefixed by the default attribPrefix.
227225
* @property {string} [name] synonym for `attrib`.
228226
* @property {string} [attribName] synonym for `attrib`.

src/programs.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,9 @@ function floatAttribSetter(gl, index) {
395395
gl.enableVertexAttribArray(index);
396396
gl.vertexAttribPointer(
397397
index, b.numComponents || b.size, b.type || FLOAT, b.normalize || false, b.stride || 0, b.offset || 0);
398-
gl.vertexAttribDivisor(index, b.divisor || 0);
398+
if (gl.vertexAttribDivisor) {
399+
gl.vertexAttribDivisor(index, b.divisor || 0);
400+
}
399401
}
400402
};
401403
}
@@ -414,7 +416,9 @@ function intAttribSetter(gl, index) {
414416
gl.enableVertexAttribArray(index);
415417
gl.vertexAttribIPointer(
416418
index, b.numComponents || b.size, b.type || INT, b.stride || 0, b.offset || 0);
417-
gl.vertexAttribDivisor(index, b.divisor || 0);
419+
if (gl.vertexAttribDivisor) {
420+
gl.vertexAttribDivisor(index, b.divisor || 0);
421+
}
418422
}
419423
};
420424
}
@@ -433,7 +437,9 @@ function uintAttribSetter(gl, index) {
433437
gl.enableVertexAttribArray(index);
434438
gl.vertexAttribIPointer(
435439
index, b.numComponents || b.size, b.type || UNSIGNED_INT, b.stride || 0, b.offset || 0);
436-
gl.vertexAttribDivisor(index, b.divisor || 0);
440+
if (gl.vertexAttribDivisor) {
441+
gl.vertexAttribDivisor(index, b.divisor || 0);
442+
}
437443
}
438444
};
439445
}
@@ -456,7 +462,9 @@ function matAttribSetter(gl, index, typeInfo) {
456462
gl.enableVertexAttribArray(index + i);
457463
gl.vertexAttribPointer(
458464
index + i, size, type, normalize, stride, offset + rowOffset * i);
459-
gl.vertexAttribDivisor(index + i, b.divisor || 0);
465+
if (gl.vertexAttribDivisor) {
466+
gl.vertexAttribDivisor(index + i, b.divisor || 0);
467+
}
460468
}
461469
};
462470
}

0 commit comments

Comments
 (0)