Skip to content

Commit 044f971

Browse files
authored
Merge pull request #21 from elm-explorations/use-draw-arrays
Use drawArrays for sequential buffers
2 parents 111c663 + 08bb885 commit 044f971

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
@@ -277,38 +277,28 @@ function _WebGL_doBindAttribute(gl, attribute, mesh, attributes) {
277277
* @param {Mesh} mesh a mesh object from Elm
278278
* @return {Object} buffer - an object with the following properties
279279
* @return {Number} buffer.numIndices
280-
* @return {WebGLBuffer} buffer.indexBuffer
280+
* @return {WebGLBuffer|null} buffer.indexBuffer - optional index buffer
281281
* @return {Object} buffer.buffers - will be used to buffer attributes
282282
*/
283283
function _WebGL_doBindSetup(gl, mesh) {
284-
_WebGL_log('Created index buffer');
285-
var indexBuffer = gl.createBuffer();
286-
var indices = (mesh.a.__$indexSize === 0)
287-
? _WebGL_makeSequentialBuffer(mesh.a.__$elemSize * _WebGL_listLength(mesh.b))
288-
: _WebGL_makeIndexedBuffer(mesh.c, mesh.a.__$indexSize);
289-
290-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
291-
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
292-
293-
return {
294-
numIndices: indices.length,
295-
indexBuffer: indexBuffer,
296-
buffers: {}
297-
};
298-
}
299-
300-
/**
301-
* Create an indices array and fill it with 0..n
302-
*
303-
* @param {Number} numIndices The number of indices
304-
* @return {Uint16Array} indices
305-
*/
306-
function _WebGL_makeSequentialBuffer(numIndices) {
307-
var indices = new Uint16Array(numIndices);
308-
for (var i = 0; i < numIndices; i++) {
309-
indices[i] = i;
284+
if (mesh.a.indexSize > 0) {
285+
_WebGL_log('Created index buffer');
286+
var indexBuffer = gl.createBuffer();
287+
var indices = _WebGL_makeIndexedBuffer(mesh.c, mesh.a.indexSize);
288+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
289+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
290+
return {
291+
numIndices: indices.length,
292+
indexBuffer: indexBuffer,
293+
buffers: {}
294+
};
295+
} else {
296+
return {
297+
numIndices: mesh.a.elemSize * _WebGL_listLength(mesh.b),
298+
indexBuffer: null,
299+
buffers: {}
300+
};
310301
}
311-
return indices;
312302
}
313303

314304
/**
@@ -418,8 +408,6 @@ var _WebGL_drawGL = F2(function (model, domNode) {
418408
model.__cache.buffers.set(entity.__mesh, buffer);
419409
}
420410

421-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer.indexBuffer);
422-
423411
var numAttributes = gl.getProgramParameter(program.glProgram, gl.ACTIVE_ATTRIBUTES);
424412

425413
for (var i = 0; i < numAttributes; i++) {
@@ -450,7 +438,12 @@ var _WebGL_drawGL = F2(function (model, domNode) {
450438
}
451439
_WebGL_listEach(__WI_enableSetting(gl), 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(__WI_disableSetting(model.__cache), entity.__settings);
456449

0 commit comments

Comments
 (0)