|
159 | 159 | canvas.width = window.innerWidth; |
160 | 160 | document.body.appendChild(canvas); |
161 | 161 |
|
162 | | - const gl = canvas.getContext("webgl"); |
| 162 | + const gl = canvas.getContext("webgl2"); |
163 | 163 | gl.enable(gl.SCISSOR_TEST); |
164 | 164 | gl.enable(gl.BLEND); |
165 | 165 | gl.blendEquation(gl.FUNC_ADD); |
|
187 | 187 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); |
188 | 188 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); |
189 | 189 |
|
| 190 | + // Since the indexes are 16 bit, we cannot have more than 65536 vertices |
| 191 | + // at the same time. |
190 | 192 | const MAX_VERTICES = 65535; |
191 | 193 |
|
192 | 194 | const indexArray = new Uint16Array ( MAX_VERTICES ); |
| 195 | + // 20 bytes per vertex. We cannot have more veritices than the index array |
193 | 196 | const vboArray = new ArrayBuffer( MAX_VERTICES * 20 ); |
194 | 197 |
|
195 | 198 | const vbo = gl.createBuffer(); |
|
269 | 272 | projMatrix |
270 | 273 | ); |
271 | 274 |
|
272 | | - function updateBuffersRaw(data) { |
| 275 | + ws.onmessage = e => { |
| 276 | + const data = e.data; |
273 | 277 | const dv = new DataView(data); |
274 | 278 | const all_cmds = [] |
275 | 279 |
|
|
280 | 284 | let cmd_offset = 12 + vtx_length*20 + idx_length*2; |
281 | 285 | for (let i = 0; i < cmd_length; ++i) { |
282 | 286 | 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), |
284 | 296 | 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 | + ], |
290 | 302 | }); |
291 | | - cmd_offset += 20; |
| 303 | + cmd_offset += 52; |
292 | 304 | } |
293 | 305 |
|
294 | | - const localVbo = new DataView(data, 12, vtx_length * 20); |
295 | | - const localIndex = new DataView(data, 12 + vtx_length*20, idx_length*2) |
296 | 306 |
|
297 | 307 | gl.bindBuffer(gl.ARRAY_BUFFER, vbo); |
298 | | - gl.bufferSubData(gl.ARRAY_BUFFER, 0, localVbo); |
299 | | - |
300 | 308 | 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) |
309 | 311 |
|
310 | | - let curr_base = 0; |
| 312 | + // We need to rebind buffers every time, because webgl does not support |
| 313 | + // glDrawElementsBaseVertex |
311 | 314 | 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 | + |
312 | 323 | 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); |
315 | 325 | } |
316 | 326 | } |
317 | 327 |
|
|
0 commit comments