|
|
|
function schedule_draw(state, context) {
|
|
|
|
if (!state.timers.raf) {
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
draw(state, context)
|
|
|
|
});
|
|
|
|
state.timers.raf = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function upload_if_needed(gl, buffer_kind, serializer) {
|
|
|
|
if (serializer.need_gpu_allocate) {
|
|
|
|
if (config.debug_print) console.debug('gpu allocate');
|
|
|
|
gl.bufferData(buffer_kind, serializer.size, gl.DYNAMIC_DRAW);
|
|
|
|
serializer.need_gpu_allocate = false;
|
|
|
|
serializer.gpu_upload_from = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serializer.gpu_upload_from < serializer.offset) {
|
|
|
|
if (config.debug_print) console.debug('gpu upload');
|
|
|
|
const upload_offset = serializer.gpu_upload_from;
|
|
|
|
const upload_size = serializer.offset - upload_offset;
|
|
|
|
gl.bufferSubData(buffer_kind, upload_offset, new Uint8Array(serializer.buffer, upload_offset, upload_size));
|
|
|
|
serializer.gpu_upload_from = serializer.offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function draw(state, context) {
|
|
|
|
const cpu_before = performance.now();
|
|
|
|
|
|
|
|
let lod_level = -1;
|
|
|
|
|
|
|
|
for (let i = context.lods.length - 1; i >= 0; --i) {
|
|
|
|
const level = context.lods[i];
|
|
|
|
if (state.canvas.zoom <= level.max_zoom) {
|
|
|
|
lod_level = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const lod = context.lods[lod_level];
|
|
|
|
|
|
|
|
state.timers.raf = false;
|
|
|
|
|
|
|
|
const gl = context.gl;
|
|
|
|
const width = window.innerWidth;
|
|
|
|
const height = window.innerHeight;
|
|
|
|
|
|
|
|
let query = null;
|
|
|
|
|
|
|
|
if (context.gpu_timer_ext !== null) {
|
|
|
|
query = gl.createQuery();
|
|
|
|
gl.beginQuery(context.gpu_timer_ext.TIME_ELAPSED_EXT, query);
|
|
|
|
}
|
|
|
|
|
|
|
|
gl.viewport(0, 0, context.canvas.width, context.canvas.height);
|
|
|
|
gl.clearColor(context.bgcolor.r, context.bgcolor.g, context.bgcolor.b, 1);
|
|
|
|
gl.clearDepth(0.0);
|
|
|
|
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, lod.data_buffer);
|
|
|
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, lod.index_buffer);
|
|
|
|
|
|
|
|
const quad_data = serializer_create(1024);
|
|
|
|
|
|
|
|
ser_f32(quad_data, 0);
|
|
|
|
ser_f32(quad_data, 0);
|
|
|
|
ser_f32(quad_data, 200);
|
|
|
|
ser_f32(quad_data, 100);
|
|
|
|
ser_u32(quad_data, 0);
|
|
|
|
|
|
|
|
ser_f32(quad_data, 200);
|
|
|
|
ser_f32(quad_data, 100);
|
|
|
|
ser_f32(quad_data, 255);
|
|
|
|
ser_f32(quad_data, 500);
|
|
|
|
ser_u32(quad_data, 0);
|
|
|
|
|
|
|
|
ser_f32(quad_data, 100);
|
|
|
|
ser_f32(quad_data, 300);
|
|
|
|
ser_f32(quad_data, 125);
|
|
|
|
ser_f32(quad_data, 854);
|
|
|
|
ser_u32(quad_data, 1);
|
|
|
|
|
|
|
|
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(quad_data.buffer, 0, quad_data.offset), gl.STATIC_DRAW);
|
|
|
|
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint32Array([0, 1, 2, 3, 2, 1, 4, 5, 6, 7, 6, 5, 8, 9, 10, 11, 10, 9]), gl.STATIC_DRAW);
|
|
|
|
|
|
|
|
locations = context.locations['sdf'].main;
|
|
|
|
|
|
|
|
gl.useProgram(context.programs['sdf'].main);
|
|
|
|
|
|
|
|
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'], 2);
|
|
|
|
gl.uniform1i(locations['u_debug_mode'], state.debug.red);
|
|
|
|
gl.uniform1i(locations['u_stroke_data'], 0);
|
|
|
|
|
|
|
|
gl.enableVertexAttribArray(locations['a_ab']);
|
|
|
|
gl.enableVertexAttribArray(locations['a_stroke_id']);
|
|
|
|
|
|
|
|
gl.vertexAttribPointer(locations['a_ab'], 4, gl.FLOAT, false, 5 * 4, 0);
|
|
|
|
gl.vertexAttribIPointer(locations['a_stroke_id'], 1, gl.INT, 5 * 4, 4 * 4);
|
|
|
|
|
|
|
|
gl.vertexAttribDivisor(locations['a_ab'], 1);
|
|
|
|
gl.vertexAttribDivisor(locations['a_stroke_id'], 1);
|
|
|
|
|
|
|
|
const stroke_data = serializer_create(1024);
|
|
|
|
|
|
|
|
ser_u8(stroke_data, 255);
|
|
|
|
ser_u8(stroke_data, 0);
|
|
|
|
ser_u8(stroke_data, 0);
|
|
|
|
ser_u8(stroke_data, 8);
|
|
|
|
|
|
|
|
ser_u8(stroke_data, 0);
|
|
|
|
ser_u8(stroke_data, 0);
|
|
|
|
ser_u8(stroke_data, 255);
|
|
|
|
ser_u8(stroke_data, 1);
|
|
|
|
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, context.textures['stroke_data']);
|
|
|
|
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 2, 1, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([255, 0, 0, 16, 0, 0, 255, 2]));
|
|
|
|
gl.activeTexture(gl.TEXTURE0);
|
|
|
|
|
|
|
|
gl.drawElementsInstanced(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0, 3);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
const before_clip = performance.now();
|
|
|
|
const index_count = bvh_clip(state, context, lod_level);
|
|
|
|
const after_clip = performance.now();
|
|
|
|
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, lod.data_buffer);
|
|
|
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, lod.index_buffer);
|
|
|
|
|
|
|
|
upload_if_needed(gl, gl.ARRAY_BUFFER, lod.vertices);
|
|
|
|
upload_if_needed(gl, gl.ELEMENT_ARRAY_BUFFER, lod.indices);
|
|
|
|
|
|
|
|
document.getElementById('debug-stats').innerHTML = `
|
|
|
|
<span>LOD level: ${lod_level}</span>
|
|
|
|
<span>Segments onscreen: ${index_count}</span>
|
|
|
|
<span>Canvas offset: (${state.canvas.offset.x}, ${state.canvas.offset.y})</span>
|
|
|
|
<span>Canvas zoom: ${Math.round(state.canvas.zoom * 100000) / 100000}</span>`;
|
|
|
|
|
|
|
|
if (index_count > 0) {
|
|
|
|
// DEPTH PREPASS
|
|
|
|
if (state.debug.do_prepass) {
|
|
|
|
gl.drawBuffers([gl.NONE]);
|
|
|
|
|
|
|
|
locations = context.locations['sdf'].opaque;
|
|
|
|
|
|
|
|
gl.useProgram(context.programs['sdf'].opaque);
|
|
|
|
|
|
|
|
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.stroke_count);
|
|
|
|
|
|
|
|
gl.enableVertexAttribArray(locations['a_pos']);
|
|
|
|
gl.enableVertexAttribArray(locations['a_line']);
|
|
|
|
gl.enableVertexAttribArray(locations['a_stroke_id']);
|
|
|
|
|
|
|
|
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.vertexAttribIPointer(locations['a_stroke_id'], 1, gl.INT, config.bytes_per_point, 4 * 3 + 4 * 4 + 4);
|
|
|
|
|
|
|
|
gl.drawElements(gl.TRIANGLES, index_count, gl.UNSIGNED_INT, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// MAIN PASS
|
|
|
|
gl.drawBuffers([gl.BACK]);
|
|
|
|
|
|
|
|
locations = context.locations['sdf'].main;
|
|
|
|
|
|
|
|
gl.useProgram(context.programs['sdf'].main);
|
|
|
|
|
|
|
|
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.stroke_count);
|
|
|
|
gl.uniform1i(locations['u_debug_mode'], state.debug.red);
|
|
|
|
|
|
|
|
gl.enableVertexAttribArray(locations['a_pos']);
|
|
|
|
gl.enableVertexAttribArray(locations['a_line']);
|
|
|
|
gl.enableVertexAttribArray(locations['a_color']);
|
|
|
|
gl.enableVertexAttribArray(locations['a_stroke_id']);
|
|
|
|
|
|
|
|
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.vertexAttribIPointer(locations['a_stroke_id'], 1, gl.INT, config.bytes_per_point, 4 * 3 + 4 * 4 + 4);
|
|
|
|
|
|
|
|
//index_buffer.reverse();
|
|
|
|
gl.drawElements(gl.TRIANGLES, index_count, gl.UNSIGNED_INT, 0);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
// Dynamic data (stroke previews that are currently in progress)
|
|
|
|
const dynamic_points = context.dynamic_serializer.offset / config.bytes_per_point;
|
|
|
|
|
|
|
|
if (dynamic_points > 0) {
|
|
|
|
gl.drawBuffers([gl.BACK]);
|
|
|
|
|
|
|
|
locations = context.locations['sdf'].main;
|
|
|
|
|
|
|
|
gl.useProgram(context.programs['sdf'].main);
|
|
|
|
|
|
|
|
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.stroke_count);
|
|
|
|
gl.uniform1i(locations['u_debug_mode'], state.debug.red);
|
|
|
|
|
|
|
|
gl.clear(gl.DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_packed_dynamic']);
|
|
|
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers['b_packed_dynamic_index']);
|
|
|
|
|
|
|
|
gl.enableVertexAttribArray(locations['a_pos']);
|
|
|
|
gl.enableVertexAttribArray(locations['a_line']);
|
|
|
|
gl.enableVertexAttribArray(locations['a_color']);
|
|
|
|
gl.enableVertexAttribArray(locations['a_stroke_id']);
|
|
|
|
|
|
|
|
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.vertexAttribIPointer(locations['a_stroke_id'], 1, gl.INT, config.bytes_per_point, 4 * 3 + 4 * 4 + 4);
|
|
|
|
|
|
|
|
const dynamic_indices = [];
|
|
|
|
let base = 0;
|
|
|
|
|
|
|
|
for (const player_id in state.players) {
|
|
|
|
// player has the same data as their current stroke: points, color, width
|
|
|
|
const player = state.players[player_id];
|
|
|
|
if (player.points.length > 1) {
|
|
|
|
for (let i = 0; i < player.points.length - 1; ++i) {
|
|
|
|
dynamic_indices.push(base + 0);
|
|
|
|
dynamic_indices.push(base + 1);
|
|
|
|
dynamic_indices.push(base + 2);
|
|
|
|
dynamic_indices.push(base + 3);
|
|
|
|
dynamic_indices.push(base + 2);
|
|
|
|
dynamic_indices.push(base + 1);
|
|
|
|
|
|
|
|
base += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (context.need_dynamic_upload) {
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(context.dynamic_serializer.buffer, 0, context.dynamic_serializer.offset), gl.DYNAMIC_DRAW);
|
|
|
|
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint32Array(dynamic_indices), gl.DYNAMIC_DRAW);
|
|
|
|
context.need_dynamic_upload = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl.drawElements(gl.TRIANGLES, dynamic_indices.length, gl.UNSIGNED_INT, 0);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (state.debug.draw_bvh) {
|
|
|
|
const points = new Float32Array(state.bvh.nodes.length * 6 * 2);
|
|
|
|
|
|
|
|
for (let i = 0; i < state.bvh.nodes.length; ++i) {
|
|
|
|
const box = state.bvh.nodes[i].bbox;
|
|
|
|
|
|
|
|
points[i * 12 + 0] = box.x1;
|
|
|
|
points[i * 12 + 1] = box.y1;
|
|
|
|
points[i * 12 + 2] = box.x2;
|
|
|
|
points[i * 12 + 3] = box.y1;
|
|
|
|
points[i * 12 + 4] = box.x1;
|
|
|
|
points[i * 12 + 5] = box.y2;
|
|
|
|
|
|
|
|
points[i * 12 + 6] = box.x2;
|
|
|
|
points[i * 12 + 7] = box.y2;
|
|
|
|
points[i * 12 + 8] = box.x1;
|
|
|
|
points[i * 12 + 9] = box.y2;
|
|
|
|
points[i * 12 + 10] = box.x2;
|
|
|
|
points[i * 12 + 11] = box.y1;
|
|
|
|
}
|
|
|
|
|
|
|
|
locations = context.locations['debug'];
|
|
|
|
buffers = context.buffers['debug'];
|
|
|
|
|
|
|
|
gl.useProgram(context.programs['debug']);
|
|
|
|
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.enableVertexAttribArray(locations['a_pos']);
|
|
|
|
gl.vertexAttribPointer(locations['a_pos'], 2, gl.FLOAT, false, 8, 0);
|
|
|
|
|
|
|
|
gl.clear(gl.DEPTH_BUFFER_BIT);
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, points, gl.STATIC_DRAW);
|
|
|
|
gl.drawArrays(gl.TRIANGLES, 0, points.length / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context.gpu_timer_ext) {
|
|
|
|
gl.endQuery(context.gpu_timer_ext.TIME_ELAPSED_EXT);
|
|
|
|
|
|
|
|
const next_tick = () => {
|
|
|
|
if (query) {
|
|
|
|
// At some point in the future, after returning control to the browser
|
|
|
|
const available = gl.getQueryParameter(query, gl.QUERY_RESULT_AVAILABLE);
|
|
|
|
const disjoint = gl.getParameter(context.gpu_timer_ext.GPU_DISJOINT_EXT);
|
|
|
|
|
|
|
|
if (available && !disjoint) {
|
|
|
|
// See how much time the rendering of the object took in nanoseconds.
|
|
|
|
const timeElapsed = gl.getQueryParameter(query, gl.QUERY_RESULT);
|
|
|
|
//console.debug(timeElapsed / 1000000);
|
|
|
|
document.querySelector('.debug-timings .gpu').innerHTML = 'Last GPU Frametime: ' + Math.round(timeElapsed / 10000) / 100 + 'ms';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (available || disjoint) {
|
|
|
|
// Clean up the query object.
|
|
|
|
gl.deleteQuery(query);
|
|
|
|
// Don't re-enter this polling loop.
|
|
|
|
query = null;
|
|
|
|
} else {
|
|
|
|
setTimeout(next_tick, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(next_tick, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const cpu_after = performance.now();
|
|
|
|
|
|
|
|
|
|
|
|
document.querySelector('.debug-timings .cpu').innerHTML = 'Last CPU Frametime: ' + Math.round((cpu_after - cpu_before) * 100) / 100 + 'ms';
|
|
|
|
}
|