Skip to content

Commit b6edd6d

Browse files
committed
Fix GUI when more than 2**16 vertices
1 parent 70979c5 commit b6edd6d

1 file changed

Lines changed: 34 additions & 24 deletions

File tree

remote/remote.html

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
canvas.width = window.innerWidth;
160160
document.body.appendChild(canvas);
161161

162-
const gl = canvas.getContext("webgl");
162+
const gl = canvas.getContext("webgl2");
163163
gl.enable(gl.SCISSOR_TEST);
164164
gl.enable(gl.BLEND);
165165
gl.blendEquation(gl.FUNC_ADD);
@@ -187,9 +187,12 @@
187187
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
188188
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
189189

190+
// Since the indexes are 16 bit, we cannot have more than 65536 vertices
191+
// at the same time.
190192
const MAX_VERTICES = 65535;
191193

192194
const indexArray = new Uint16Array ( MAX_VERTICES );
195+
// 20 bytes per vertex. We cannot have more veritices than the index array
193196
const vboArray = new ArrayBuffer( MAX_VERTICES * 20 );
194197

195198
const vbo = gl.createBuffer();
@@ -269,7 +272,8 @@
269272
projMatrix
270273
);
271274

272-
function updateBuffersRaw(data) {
275+
ws.onmessage = e => {
276+
const data = e.data;
273277
const dv = new DataView(data);
274278
const all_cmds = []
275279

@@ -280,38 +284,44 @@
280284
let cmd_offset = 12 + vtx_length*20 + idx_length*2;
281285
for (let i = 0; i < cmd_length; ++i) {
282286
all_cmds.push({
283-
cnt: dv.getInt32(cmd_offset, true),
287+
list_id: dv.getInt32(cmd_offset, true),
288+
cmd_id: dv.getInt32(cmd_offset + 4, true),
289+
cnt: dv.getInt32(cmd_offset + 8, true),
290+
vtxOff: dv.getInt32(cmd_offset + 12, true),
291+
idxOff: dv.getInt32(cmd_offset + 16, true),
292+
vtxCount: dv.getInt32(cmd_offset + 20, true),
293+
idxCount: dv.getInt32(cmd_offset + 24, true),
294+
vtxBase: dv.getInt32(cmd_offset + 28, true),
295+
idxBase: dv.getInt32(cmd_offset + 32, true),
284296
clp: [
285-
dv.getFloat32(cmd_offset + 4, true),
286-
dv.getFloat32(cmd_offset + 8, true),
287-
dv.getFloat32(cmd_offset + 12, true),
288-
dv.getFloat32(cmd_offset + 16, true)
289-
]
297+
dv.getFloat32(cmd_offset + 36, true),
298+
dv.getFloat32(cmd_offset + 40, true),
299+
dv.getFloat32(cmd_offset + 44, true),
300+
dv.getFloat32(cmd_offset + 48, true)
301+
],
290302
});
291-
cmd_offset += 20;
303+
cmd_offset += 52;
292304
}
293305

294-
const localVbo = new DataView(data, 12, vtx_length * 20);
295-
const localIndex = new DataView(data, 12 + vtx_length*20, idx_length*2)
296306

297307
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
298-
gl.bufferSubData(gl.ARRAY_BUFFER, 0, localVbo);
299-
300308
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
301-
gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, localIndex);
302-
303-
return all_cmds;
304-
}
305-
306-
307-
ws.onmessage = e => {
308-
const all_cmds = updateBuffersRaw(e.data);
309+
const localVbo = new DataView(data, 12, vtx_length*20);
310+
const localIndex = new DataView(data, 12 + vtx_length*20, idx_length*2)
309311

310-
let curr_base = 0;
312+
// We need to rebind buffers every time, because webgl does not support
313+
// glDrawElementsBaseVertex
311314
for (const cmd of all_cmds) {
315+
// vtxOff is reset for each command list, while vtxBase is
316+
// keeps track of the base of the vertices for a given command list
317+
// we probaly overcopy the vertices, but there is no way to know
318+
// how many of them are actually used.
319+
gl.bufferSubData(gl.ARRAY_BUFFER, 0, localVbo, cmd.vtxOff*20 + cmd.vtxBase*20, cmd.vtxCount*20 - cmd.vtxOff*20);
320+
// We know that we want to render cnt elements, so we can simply copy those.
321+
gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, localIndex, cmd.idxOff*2 + cmd.idxBase*2, cmd.cnt*2);
322+
312323
gl.scissor(cmd.clp[0], window.innerHeight - cmd.clp[3], cmd.clp[2] - cmd.clp[0], cmd.clp[3] - cmd.clp[1]);
313-
gl.drawElements(gl.TRIANGLES, cmd.cnt, gl.UNSIGNED_SHORT, curr_base*2);
314-
curr_base += cmd.cnt;
324+
gl.drawElements(gl.TRIANGLES, cmd.cnt, gl.UNSIGNED_SHORT, 0);
315325
}
316326
}
317327

0 commit comments

Comments
 (0)