|
|
|
@ -72,74 +72,78 @@ function draw(state, context) {
@@ -72,74 +72,78 @@ function draw(state, context) {
|
|
|
|
|
gl.useProgram(context.programs['sdf'].main); |
|
|
|
|
|
|
|
|
|
bvh_clip(state, context); |
|
|
|
|
|
|
|
|
|
const segment_count = geometry_write_instances(state, context); |
|
|
|
|
const dynamic_segment_count = context.dynamic_segment_count; |
|
|
|
|
const dynamic_stroke_count = context.dynamic_stroke_count; |
|
|
|
|
|
|
|
|
|
// "Static" data upload
|
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_instance']); |
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, context.instance_data_points.size * 4 + context.instance_data_ids.size * 4, gl.STREAM_DRAW); |
|
|
|
|
gl.bufferSubData(gl.ARRAY_BUFFER, 0, tv_data(context.instance_data_points)); |
|
|
|
|
gl.bufferSubData(gl.ARRAY_BUFFER, context.instance_data_points.size * 4, tv_data(context.instance_data_ids)); |
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, context.textures['stroke_data']); |
|
|
|
|
// TODO: this is stable data, only upload new strokes as they arrive
|
|
|
|
|
upload_square_rgba16ui_texture(gl, context.stroke_data, config.stroke_texture_size); |
|
|
|
|
|
|
|
|
|
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_stroke_count'], state.events.length); |
|
|
|
|
gl.uniform1i(locations['u_debug_mode'], state.debug.red); |
|
|
|
|
gl.uniform1i(locations['u_stroke_data'], 0); |
|
|
|
|
gl.uniform1i(locations['u_stroke_texture_size'], config.stroke_texture_size); |
|
|
|
|
|
|
|
|
|
gl.enableVertexAttribArray(locations['a_a']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_b']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_stroke_id']); |
|
|
|
|
|
|
|
|
|
// Points (a, b) and stroke ids are stored in separate cpu buffers so that points can be reused (look at stride and offset values)
|
|
|
|
|
gl.vertexAttribPointer(locations['a_a'], 2, gl.FLOAT, false, 2 * 4, 0); |
|
|
|
|
gl.vertexAttribPointer(locations['a_b'], 2, gl.FLOAT, false, 2 * 4, 2 * 4); |
|
|
|
|
gl.vertexAttribIPointer(locations['a_stroke_id'], 1, gl.INT, 4, context.instance_data_points.size * 4); |
|
|
|
|
|
|
|
|
|
gl.vertexAttribDivisor(locations['a_a'], 1); |
|
|
|
|
gl.vertexAttribDivisor(locations['a_b'], 1); |
|
|
|
|
gl.vertexAttribDivisor(locations['a_stroke_id'], 1); |
|
|
|
|
|
|
|
|
|
// Static draw (everything already bound)
|
|
|
|
|
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, segment_count); |
|
|
|
|
|
|
|
|
|
if (segment_count > 0) { |
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_instance']); |
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, context.instance_data_points.size * 4 + context.instance_data_ids.size * 4, gl.STREAM_DRAW); |
|
|
|
|
gl.bufferSubData(gl.ARRAY_BUFFER, 0, tv_data(context.instance_data_points)); |
|
|
|
|
gl.bufferSubData(gl.ARRAY_BUFFER, context.instance_data_points.size * 4, tv_data(context.instance_data_ids)); |
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, context.textures['stroke_data']); |
|
|
|
|
upload_square_rgba16ui_texture(gl, context.stroke_data, config.stroke_texture_size); |
|
|
|
|
|
|
|
|
|
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_stroke_count'], state.events.length); |
|
|
|
|
gl.uniform1i(locations['u_debug_mode'], state.debug.red); |
|
|
|
|
gl.uniform1i(locations['u_stroke_data'], 0); |
|
|
|
|
gl.uniform1i(locations['u_stroke_texture_size'], config.stroke_texture_size); |
|
|
|
|
|
|
|
|
|
gl.enableVertexAttribArray(locations['a_a']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_b']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_stroke_id']); |
|
|
|
|
|
|
|
|
|
// Points (a, b) and stroke ids are stored in separate cpu buffers so that points can be reused (look at stride and offset values)
|
|
|
|
|
gl.vertexAttribPointer(locations['a_a'], 2, gl.FLOAT, false, 2 * 4, 0); |
|
|
|
|
gl.vertexAttribPointer(locations['a_b'], 2, gl.FLOAT, false, 2 * 4, 2 * 4); |
|
|
|
|
gl.vertexAttribIPointer(locations['a_stroke_id'], 1, gl.INT, 4, context.instance_data_points.size * 4); |
|
|
|
|
|
|
|
|
|
gl.vertexAttribDivisor(locations['a_a'], 1); |
|
|
|
|
gl.vertexAttribDivisor(locations['a_b'], 1); |
|
|
|
|
gl.vertexAttribDivisor(locations['a_stroke_id'], 1); |
|
|
|
|
|
|
|
|
|
// Static draw (everything already bound)
|
|
|
|
|
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, segment_count); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Dynamic strokes should be drawn above static strokes
|
|
|
|
|
gl.clear(gl.DEPTH_BUFFER_BIT); |
|
|
|
|
|
|
|
|
|
// Dynamic draw (strokes currently being drawn)
|
|
|
|
|
gl.uniform1i(locations['u_stroke_count'], dynamic_stroke_count); |
|
|
|
|
gl.uniform1i(locations['u_stroke_data'], 0); |
|
|
|
|
gl.uniform1i(locations['u_stroke_texture_size'], config.dynamic_stroke_texture_size); |
|
|
|
|
|
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_dynamic_instance']); |
|
|
|
|
|
|
|
|
|
// Dynamic data upload
|
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, context.dynamic_instance_points.size * 4 + context.dynamic_instance_ids.size * 4, gl.STREAM_DRAW); |
|
|
|
|
gl.bufferSubData(gl.ARRAY_BUFFER, 0, tv_data(context.dynamic_instance_points)); |
|
|
|
|
gl.bufferSubData(gl.ARRAY_BUFFER, context.dynamic_instance_points.size * 4, tv_data(context.dynamic_instance_ids)); |
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, context.textures['dynamic_stroke_data']); |
|
|
|
|
upload_square_rgba16ui_texture(gl, context.dynamic_stroke_data, config.dynamic_stroke_texture_size); |
|
|
|
|
|
|
|
|
|
gl.enableVertexAttribArray(locations['a_a']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_b']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_stroke_id']); |
|
|
|
|
|
|
|
|
|
// Points (a, b) and stroke ids are stored in separate cpu buffers so that points can be reused (look at stride and offset values)
|
|
|
|
|
gl.vertexAttribPointer(locations['a_a'], 2, gl.FLOAT, false, 2 * 4, 0); |
|
|
|
|
gl.vertexAttribPointer(locations['a_b'], 2, gl.FLOAT, false, 2 * 4, 2 * 4); |
|
|
|
|
gl.vertexAttribIPointer(locations['a_stroke_id'], 1, gl.INT, 4, context.dynamic_instance_points.size * 4); |
|
|
|
|
|
|
|
|
|
gl.vertexAttribDivisor(locations['a_a'], 1); |
|
|
|
|
gl.vertexAttribDivisor(locations['a_b'], 1); |
|
|
|
|
gl.vertexAttribDivisor(locations['a_stroke_id'], 1); |
|
|
|
|
|
|
|
|
|
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, dynamic_segment_count); |
|
|
|
|
if (dynamic_segment_count > 0) { |
|
|
|
|
gl.uniform1i(locations['u_stroke_count'], dynamic_stroke_count); |
|
|
|
|
gl.uniform1i(locations['u_stroke_data'], 0); |
|
|
|
|
gl.uniform1i(locations['u_stroke_texture_size'], config.dynamic_stroke_texture_size); |
|
|
|
|
|
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_dynamic_instance']); |
|
|
|
|
|
|
|
|
|
// Dynamic data upload
|
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, context.dynamic_instance_points.size * 4 + context.dynamic_instance_ids.size * 4, gl.STREAM_DRAW); |
|
|
|
|
gl.bufferSubData(gl.ARRAY_BUFFER, 0, tv_data(context.dynamic_instance_points)); |
|
|
|
|
gl.bufferSubData(gl.ARRAY_BUFFER, context.dynamic_instance_points.size * 4, tv_data(context.dynamic_instance_ids)); |
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, context.textures['dynamic_stroke_data']); |
|
|
|
|
upload_square_rgba16ui_texture(gl, context.dynamic_stroke_data, config.dynamic_stroke_texture_size); |
|
|
|
|
|
|
|
|
|
gl.enableVertexAttribArray(locations['a_a']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_b']); |
|
|
|
|
gl.enableVertexAttribArray(locations['a_stroke_id']); |
|
|
|
|
|
|
|
|
|
// Points (a, b) and stroke ids are stored in separate cpu buffers so that points can be reused (look at stride and offset values)
|
|
|
|
|
gl.vertexAttribPointer(locations['a_a'], 2, gl.FLOAT, false, 2 * 4, 0); |
|
|
|
|
gl.vertexAttribPointer(locations['a_b'], 2, gl.FLOAT, false, 2 * 4, 2 * 4); |
|
|
|
|
gl.vertexAttribIPointer(locations['a_stroke_id'], 1, gl.INT, 4, context.dynamic_instance_points.size * 4); |
|
|
|
|
|
|
|
|
|
gl.vertexAttribDivisor(locations['a_a'], 1); |
|
|
|
|
gl.vertexAttribDivisor(locations['a_b'], 1); |
|
|
|
|
gl.vertexAttribDivisor(locations['a_stroke_id'], 1); |
|
|
|
|
|
|
|
|
|
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, dynamic_segment_count); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
document.getElementById('debug-stats').innerHTML = ` |
|
|
|
|
<span>Segments onscreen: ${segment_count}</span> |
|
|
|
|