From 95e5c565a1baee519a60d679fbec1e99ec46ccbe Mon Sep 17 00:00:00 2001 From: Dominic Prior Date: Sat, 10 Sep 2022 11:46:42 +0100 Subject: [PATCH] Make sure gl.vertexAttribDivisor exists Previously, this would have failed on WebGL1. --- src/attributes.js | 6 ++---- src/programs.js | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/attributes.js b/src/attributes.js index 7a8b2f4d..161f2fa0 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -198,8 +198,7 @@ function makeTypedArray(array, name) { * @property {boolean} [normalize] whether or not to normalize the data. Default = false * @property {number} [offset] offset into buffer in bytes. Default = 0 * @property {number} [stride] the stride in bytes per element. Default = 0 - * @property {number} [divisor] the divisor in instances. Default = 0 - * where as anything else = do call it with this value + * @property {number} [divisor] the divisor in instances. Default = 0. Requires WebGL2 or the ANGLE_instanced_arrays extension. * @property {WebGLBuffer} buffer the buffer that contains the data for this attribute * @property {number} [drawType] the draw type passed to gl.bufferData. Default = gl.STATIC_DRAW * @memberOf module:twgl @@ -221,8 +220,7 @@ function makeTypedArray(array, name) { * @property {boolean} [normalize] normalize for `vertexAttribPointer`. Default is true if type is `Int8Array` or `Uint8Array` otherwise false. * @property {number} [stride] stride for `vertexAttribPointer`. Default = 0 * @property {number} [offset] offset for `vertexAttribPointer`. Default = 0 - * @property {number} [divisor] divisor for `vertexAttribDivisor`. Default = 0 - * where as anything else = do call it with this value + * @property {number} [divisor] divisor for `vertexAttribDivisor`. Default = 0. Requires WebGL2 or the ANGLE_instanced_arrays extension. * @property {string} [attrib] name of attribute this array maps to. Defaults to same name as array prefixed by the default attribPrefix. * @property {string} [name] synonym for `attrib`. * @property {string} [attribName] synonym for `attrib`. diff --git a/src/programs.js b/src/programs.js index 9e899f63..695c3f42 100644 --- a/src/programs.js +++ b/src/programs.js @@ -395,7 +395,9 @@ function floatAttribSetter(gl, index) { gl.enableVertexAttribArray(index); gl.vertexAttribPointer( index, b.numComponents || b.size, b.type || FLOAT, b.normalize || false, b.stride || 0, b.offset || 0); - gl.vertexAttribDivisor(index, b.divisor || 0); + if (gl.vertexAttribDivisor) { + gl.vertexAttribDivisor(index, b.divisor || 0); + } } }; } @@ -414,7 +416,9 @@ function intAttribSetter(gl, index) { gl.enableVertexAttribArray(index); gl.vertexAttribIPointer( index, b.numComponents || b.size, b.type || INT, b.stride || 0, b.offset || 0); - gl.vertexAttribDivisor(index, b.divisor || 0); + if (gl.vertexAttribDivisor) { + gl.vertexAttribDivisor(index, b.divisor || 0); + } } }; } @@ -433,7 +437,9 @@ function uintAttribSetter(gl, index) { gl.enableVertexAttribArray(index); gl.vertexAttribIPointer( index, b.numComponents || b.size, b.type || UNSIGNED_INT, b.stride || 0, b.offset || 0); - gl.vertexAttribDivisor(index, b.divisor || 0); + if (gl.vertexAttribDivisor) { + gl.vertexAttribDivisor(index, b.divisor || 0); + } } }; } @@ -456,7 +462,9 @@ function matAttribSetter(gl, index, typeInfo) { gl.enableVertexAttribArray(index + i); gl.vertexAttribPointer( index + i, size, type, normalize, stride, offset + rowOffset * i); - gl.vertexAttribDivisor(index + i, b.divisor || 0); + if (gl.vertexAttribDivisor) { + gl.vertexAttribDivisor(index + i, b.divisor || 0); + } } }; }