Skip to content

Commit

Permalink
Make sure gl.vertexAttribDivisor exists
Browse files Browse the repository at this point in the history
Previously, this would have failed on WebGL1.
  • Loading branch information
Dominic Prior authored and greggman committed Sep 11, 2022
1 parent 3d94eaf commit 95e5c56
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`.
Expand Down
16 changes: 12 additions & 4 deletions src/programs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
}
Expand All @@ -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);
}
}
};
}
Expand All @@ -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);
}
}
};
}
Expand All @@ -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);
}
}
};
}
Expand Down

0 comments on commit 95e5c56

Please sign in to comment.