Skip to content

Commit 08bb885

Browse files
author
Andrey Kuzmin
committed
Use drawArrays for sequential buffers
1 parent a0d420a commit 08bb885

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

src/Elm/Kernel/WebGL.js

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -275,38 +275,28 @@ function _WebGL_doBindAttribute(gl, attribute, mesh, attributes) {
275275
* @param {Mesh} mesh a mesh object from Elm
276276
* @return {Object} buffer - an object with the following properties
277277
* @return {Number} buffer.numIndices
278-
* @return {WebGLBuffer} buffer.indexBuffer
278+
* @return {WebGLBuffer|null} buffer.indexBuffer - optional index buffer
279279
* @return {Object} buffer.buffers - will be used to buffer attributes
280280
*/
281281
function _WebGL_doBindSetup(gl, mesh) {
282-
_WebGL_log('Created index buffer');
283-
var indexBuffer = gl.createBuffer();
284-
var indices = (mesh.a.__$indexSize === 0)
285-
? _WebGL_makeSequentialBuffer(mesh.a.__$elemSize * _WebGL_listLength(mesh.b))
286-
: _WebGL_makeIndexedBuffer(mesh.c, mesh.a.__$indexSize);
287-
288-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
289-
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
290-
291-
return {
292-
numIndices: indices.length,
293-
indexBuffer: indexBuffer,
294-
buffers: {}
295-
};
296-
}
297-
298-
/**
299-
* Create an indices array and fill it with 0..n
300-
*
301-
* @param {Number} numIndices The number of indices
302-
* @return {Uint16Array} indices
303-
*/
304-
function _WebGL_makeSequentialBuffer(numIndices) {
305-
var indices = new Uint16Array(numIndices);
306-
for (var i = 0; i < numIndices; i++) {
307-
indices[i] = i;
282+
if (mesh.a.indexSize > 0) {
283+
_WebGL_log('Created index buffer');
284+
var indexBuffer = gl.createBuffer();
285+
var indices = _WebGL_makeIndexedBuffer(mesh.c, mesh.a.indexSize);
286+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
287+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
288+
return {
289+
numIndices: indices.length,
290+
indexBuffer: indexBuffer,
291+
buffers: {}
292+
};
293+
} else {
294+
return {
295+
numIndices: mesh.a.elemSize * _WebGL_listLength(mesh.b),
296+
indexBuffer: null,
297+
buffers: {}
298+
};
308299
}
309-
return indices;
310300
}
311301

312302
/**
@@ -416,8 +406,6 @@ var _WebGL_drawGL = F2(function (model, domNode) {
416406
model.__cache.buffers.set(entity.__mesh, buffer);
417407
}
418408

419-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer.indexBuffer);
420-
421409
var numAttributes = gl.getProgramParameter(program.glProgram, gl.ACTIVE_ATTRIBUTES);
422410

423411
for (var i = 0; i < numAttributes; i++) {
@@ -450,7 +438,12 @@ var _WebGL_drawGL = F2(function (model, domNode) {
450438
return A2(__WI_enableSetting, gl, setting);
451439
}, entity.__settings);
452440

453-
gl.drawElements(entity.__mesh.a.__$mode, buffer.numIndices, gl.UNSIGNED_SHORT, 0);
441+
if (buffer.indexBuffer) {
442+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer.indexBuffer);
443+
gl.drawElements(entity.d.a.mode, buffer.numIndices, gl.UNSIGNED_SHORT, 0);
444+
} else {
445+
gl.drawArrays(entity.d.a.mode, 0, buffer.numIndices);
446+
}
454447

455448
_WebGL_listEach(function (setting) {
456449
return A2(__WI_disableSetting, gl, setting);

0 commit comments

Comments
 (0)