|
|
|
@ -12,6 +12,9 @@ function draw(state, context) {
@@ -12,6 +12,9 @@ function draw(state, context) {
|
|
|
|
|
const width = window.innerWidth; |
|
|
|
|
const height = window.innerHeight; |
|
|
|
|
|
|
|
|
|
const frame_start = performance.now(); |
|
|
|
|
const sync = gl.fenceSync(gl.SYNC_GPU_COMMANDS_COMPLETE, 0); |
|
|
|
|
|
|
|
|
|
let locations; |
|
|
|
|
let buffers; |
|
|
|
|
|
|
|
|
@ -25,31 +28,68 @@ function draw(state, context) {
@@ -25,31 +28,68 @@ function draw(state, context) {
|
|
|
|
|
|
|
|
|
|
gl.useProgram(context.programs['sdf']); |
|
|
|
|
|
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_packed']); |
|
|
|
|
gl.uniform2f(locations['u_res'], context.canvas.width, context.canvas.height); |
|
|
|
|
gl.uniform2f(locations['u_scale'], state.canvas.zoom, state.canvas.zoom); |
|
|
|
|
gl.uniform2f(locations['u_translation'], state.canvas.offset.x, state.canvas.offset.y); |
|
|
|
|
gl.uniform1i(locations['u_debug_mode'], context.debug_mode ? 1 : 0); |
|
|
|
|
|
|
|
|
|
const static_points = context.static_serializer.offset / config.bytes_per_point; |
|
|
|
|
const dynamic_points = context.dynamic_serializer.offset / config.bytes_per_point; |
|
|
|
|
|
|
|
|
|
if (static_points > 0) { |
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_packed_static']); |
|
|
|
|
|
|
|
|
|
gl.enableVertexAttribArray(locations['a_pos']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_line']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_color']); |
|
|
|
|
|
|
|
|
|
gl.vertexAttribPointer(locations['a_pos'], 3, gl.FLOAT, false, config.bytes_per_point, 0); |
|
|
|
|
gl.vertexAttribPointer(locations['a_line'], 4, gl.FLOAT, false, config.bytes_per_point, 4 * 3); |
|
|
|
|
gl.vertexAttribPointer(locations['a_color'], 3, gl.UNSIGNED_BYTE, true, config.bytes_per_point, 4 * 3 + 4 * 4); |
|
|
|
|
|
|
|
|
|
gl.enableVertexAttribArray(locations['a_pos']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_line']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_color']); |
|
|
|
|
if (context.need_static_upload) { |
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(context.static_serializer.buffer, 0, context.static_serializer.offset), gl.STATIC_DRAW); |
|
|
|
|
context.need_static_upload = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gl.vertexAttribPointer(locations['a_pos'], 3, gl.FLOAT, false, config.bytes_per_point, 0); |
|
|
|
|
gl.vertexAttribPointer(locations['a_line'], 4, gl.FLOAT, false, config.bytes_per_point, 4 * 3); |
|
|
|
|
gl.vertexAttribPointer(locations['a_color'], 3, gl.UNSIGNED_BYTE, true, config.bytes_per_point, 4 * 3 + 4 * 4); |
|
|
|
|
gl.drawArrays(gl.TRIANGLES, 0, static_points); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (dynamic_points > 0) { |
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_packed_dynamic']); |
|
|
|
|
|
|
|
|
|
const npoints = context.static_serializer.offset / config.bytes_per_point; |
|
|
|
|
gl.enableVertexAttribArray(locations['a_pos']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_line']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_color']); |
|
|
|
|
|
|
|
|
|
// TODO: if changed
|
|
|
|
|
gl.vertexAttribPointer(locations['a_pos'], 3, gl.FLOAT, false, config.bytes_per_point, 0); |
|
|
|
|
gl.vertexAttribPointer(locations['a_line'], 4, gl.FLOAT, false, config.bytes_per_point, 4 * 3); |
|
|
|
|
gl.vertexAttribPointer(locations['a_color'], 3, gl.UNSIGNED_BYTE, true, config.bytes_per_point, 4 * 3 + 4 * 4); |
|
|
|
|
|
|
|
|
|
if (npoints > 0) { |
|
|
|
|
gl.uniform2f(locations['u_res'], context.canvas.width, context.canvas.height); |
|
|
|
|
gl.uniform2f(locations['u_scale'], state.canvas.zoom, state.canvas.zoom); |
|
|
|
|
gl.uniform2f(locations['u_translation'], state.canvas.offset.x, state.canvas.offset.y); |
|
|
|
|
gl.uniform1i(locations['u_debug_mode'], context.debug_mode ? 1 : 0); |
|
|
|
|
if (context.need_dynamic_upload) { |
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(context.dynamic_serializer.buffer, 0, context.dynamic_serializer.offset), gl.STATIC_DRAW); |
|
|
|
|
context.need_dynamic_upload = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(context.static_serializer.buffer, 0, context.static_serializer.offset), gl.STATIC_DRAW); |
|
|
|
|
gl.drawArrays(gl.TRIANGLES, 0, dynamic_points); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gl.drawArrays(gl.TRIANGLES, 0, npoints); |
|
|
|
|
const next_tick = () => { |
|
|
|
|
const wait_status = gl.clientWaitSync(sync, 0, 0); |
|
|
|
|
const frame_end = performance.now(); |
|
|
|
|
|
|
|
|
|
if (wait_status === gl.ALREADY_SIGNALED || wait_status === gl.CONDITION_SATISFIED) { |
|
|
|
|
const frametime_ms = frame_end - frame_start; |
|
|
|
|
gl.deleteSync(sync); |
|
|
|
|
console.debug(frametime_ms); |
|
|
|
|
} else { |
|
|
|
|
setTimeout(next_tick, 0); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setTimeout(next_tick, 0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Images
|
|
|
|
|
// locations = context.locations['image'];
|
|
|
|
|
// buffers = context.buffers['image'];
|
|
|
|
|