From a5b656e44e7205b38dff45b06ece5832bc724cb2 Mon Sep 17 00:00:00 2001 From: mikolalysenko Date: Sun, 11 Sep 2016 10:55:57 -0400 Subject: [PATCH 1/2] test case for new elements constructor --- test/elements-length.js | 32 ++++++++++++++++++++++++++++++++ test/util/index.js | 1 + 2 files changed, 33 insertions(+) create mode 100644 test/elements-length.js diff --git a/test/elements-length.js b/test/elements-length.js new file mode 100644 index 00000000..f7b47647 --- /dev/null +++ b/test/elements-length.js @@ -0,0 +1,32 @@ +var tape = require('tape') +var createContext = require('./util/create-context') +var createREGL = require('../regl') + +tape('elements - length', function (t) { + var gl = createContext(5, 5) + var regl = createREGL(gl) + + var N = 5 + + var cellsBuffer = regl.elements({ + type: 'uint16', + usage: 'dynamic', + primitive: 'triangles', + count: N * 3 + }) + + t.equals(cellsBuffer._elements.buffer.byteLength, N * 3 * 2, 'count') + + var cells2Buffer = regl.elements({ + type: 'uint16', + usage: 'dynamic', + primitive: 'triangles', + length: N * 2 * 3 + }) + + t.equals(cells2Buffer._elements.vertCount, N * 3, 'count') + + regl.destroy() + createContext.destroy(gl) + t.end() +}) diff --git a/test/util/index.js b/test/util/index.js index 6a37813b..b9d0dd6d 100644 --- a/test/util/index.js +++ b/test/util/index.js @@ -60,3 +60,4 @@ require('../attribute-constants') require('../attributes-nested') require('../buffer-update') require('../elements-update') +require('../elements-length') From e407d227dd4b0911235fd5ee1b555702f5e270a3 Mon Sep 17 00:00:00 2001 From: mikolalysenko Date: Sun, 11 Sep 2016 10:56:09 -0400 Subject: [PATCH 2/2] infer length and count for elements --- lib/elements.js | 78 +++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/lib/elements.js b/lib/elements.js index 45d5bee2..5a7ee740 100644 --- a/lib/elements.js +++ b/lib/elements.js @@ -75,21 +75,29 @@ module.exports = function wrapElementsState (gl, extensions, bufferState, stats) count, byteLength, type) { - var predictedType = type - if (!type && ( - !isTypedArray(data) || - (isNDArrayLike(data) && !isTypedArray(data.data)))) { - predictedType = extensions.oes_element_index_uint - ? GL_UNSIGNED_INT - : GL_UNSIGNED_SHORT - } elements.buffer.bind() - bufferState._initBuffer( - elements.buffer, - data, - usage, - predictedType, - 3) + if (data) { + var predictedType = type + if (!type && ( + !isTypedArray(data) || + (isNDArrayLike(data) && !isTypedArray(data.data)))) { + predictedType = extensions.oes_element_index_uint + ? GL_UNSIGNED_INT + : GL_UNSIGNED_SHORT + } + bufferState._initBuffer( + elements.buffer, + data, + usage, + predictedType, + 3) + } else { + gl.bufferData(GL_ELEMENT_ARRAY_BUFFER, byteLength, usage) + elements.buffer.dtype = dtype || GL_UNSIGNED_BYTE + elements.buffer.usage = usage + elements.buffer.dimension = 3 + elements.buffer.byteLength = byteLength + } var dtype = type if (!type) { @@ -212,9 +220,6 @@ module.exports = function wrapElementsState (gl, extensions, bufferState, stats) 'invalid vertex count for elements') vertCount = options.count | 0 } - if ('length' in options) { - byteLength = options.length | 0 - } if ('type' in options) { check.parameter( options.type, @@ -222,28 +227,25 @@ module.exports = function wrapElementsState (gl, extensions, bufferState, stats) 'invalid buffer type') dtype = elementTypes[options.type] } + if ('length' in options) { + byteLength = options.length | 0 + } else { + byteLength = vertCount + if (dtype === GL_UNSIGNED_SHORT || dtype === GL_SHORT) { + byteLength *= 2 + } else if (dtype === GL_UNSIGNED_INT || dtype === GL_INT) { + byteLength *= 4 + } + } } - if (data) { - initElements( - elements, - data, - usage, - primType, - vertCount, - byteLength, - dtype) - } else { - var _buffer = elements.buffer - _buffer.bind() - gl.bufferData(GL_ELEMENT_ARRAY_BUFFER, byteLength, usage) - _buffer.dtype = dtype || GL_UNSIGNED_BYTE - _buffer.usage = usage - _buffer.dimension = 3 - _buffer.byteLength = byteLength - elements.primType = primType < 0 ? GL_TRIANGLES : primType - elements.vertCount = vertCount < 0 ? 0 : vertCount - elements.type = _buffer.dtype - } + initElements( + elements, + data, + usage, + primType, + vertCount, + byteLength, + dtype) } return reglElements