From f6573e7bb925817cad44f2f620e8879db4165fc9 Mon Sep 17 00:00:00 2001 From: "A.Olokhtonov" Date: Thu, 28 Mar 2024 14:19:49 +0300 Subject: [PATCH] Correcty complute total point count. Only call glClear once workers have finished LOD. Only allow next draw() call after we finished rendering frame --- client/lod_worker.js | 24 ++++++++++++++---------- client/wasm/lod.c | 7 ++++++- client/wasm/lod.wasm | Bin 2844 -> 2858 bytes client/webgl_draw.js | 18 ++++++++++-------- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/client/lod_worker.js b/client/lod_worker.js index 2f382f8..311f9f0 100644 --- a/client/lod_worker.js +++ b/client/lod_worker.js @@ -15,16 +15,20 @@ async function init(tid, memory, heap_base) { } function work(indices_base, indices_count, zoom, offsets) { - exports.do_lod( - indices_base, indices_count, zoom, - offsets['coords_from'], - offsets['line_threshold'], - offsets['xs'], - offsets['ys'], - offsets['pressures'], - offsets['result_buffers'] + thread_id * 4, - offsets['result_counts'] + thread_id * 4, - ); + try { + exports.do_lod( + indices_base, indices_count, zoom, + offsets['coords_from'], + offsets['line_threshold'], + offsets['xs'], + offsets['ys'], + offsets['pressures'], + offsets['result_buffers'] + thread_id * 4, + offsets['result_counts'] + thread_id * 4, + ); + } catch (e) { + console.error('WASM:', e); + } postMessage({ 'type': 'lod_done' }); } diff --git a/client/wasm/lod.c b/client/wasm/lod.c index fa7dc81..949f5a1 100644 --- a/client/wasm/lod.c +++ b/client/wasm/lod.c @@ -211,7 +211,12 @@ do_lod(int *clipped_indices, int clipped_count, float zoom, int first_stroke = clipped_indices[0]; int last_stroke = clipped_indices[clipped_count - 1]; - int total_points = stroke_coords_from[last_stroke + 1] - stroke_coords_from[first_stroke]; + int total_points = 0; + + for (int i = 0; i < clipped_count; ++i) { + int stroke_index = clipped_indices[i]; + total_points += stroke_coords_from[stroke_index + 1] - stroke_coords_from[stroke_index]; + } int *segments_from = alloc_dynamic((clipped_count + 1) * 4); int *segments = alloc_dynamic(total_points * 4); // TODO: this is a very conservative estimate, we can lower memory usage if we get this tighter diff --git a/client/wasm/lod.wasm b/client/wasm/lod.wasm index 1a066e784cb0ba3e8c47760d5a2ee5e6ea2b7c1f..c2827dad5c1162e7b4b8a2a78642e0b0fab0eb6e 100755 GIT binary patch literal 2858 zcmZuz-EtJi74GiY-|3lMt$slg;&g*;V37bJ1jcrX!9ZM2Di_XWVuqD=5Sm?Sv1S#U zB(B|yRLWKH1p#uO7f3Gb7sy4PAWz^6CohqVlJE4au*&gn&2;za)92?qKRu$8?MWeo zH04J^Jdz`R%o@ zM$fefK)1jB8B)(!HM-?+I_QGamo_yF-$Bzo_ZzKh%6Hjz5K+@ph-*kGF?| zgG>lNj)(m$&USyDcv2UOPJef2-&4&A`|kekkeP(6WXUkjo(fMtK1h-{8+L}f+oBY> zRrlF`XAjGA0P%zE7XcwSg+9y3=f-9g+RbVV)POAdCD_#nv+`@>As zgE;PHgZN2jzuQl|671}DhMhRs?|Nm3gzoJgPJZfDz*(%G?Cw1As$m@O^aop=exb33 zlTJUMtYfplz1Z||cG%hKC&f}PoOF4@__*KM@q+39-w~=NmH(HP?+K~-xhK>e{sZqw zMDpkZB^pXdrK54}uDB^U5Rp1sP;N3h5=txvQZrTF9UVz5wXO&f=LPZsha$15-1K*o z=@L_kUl)|HBE>dVoEWKIL9SrorO|IcIa*OT;<$?4RR6x=nNjU?{hMjOW88YfRwXp#XG`G}J_Dm|8z7rI=Hgl8>=xLee1NU=Yck2?AMm0@N_7 z{;&b&G#Ek%bGF%~i4V&Y`^+4bS>PF}@UZf}5aLIq{)BP#{wuqh&zPGqYk|xISauq= z!Ypb6fJm4IL^q?bf6{peik${M=%;3Zc;*dLbBdN|IW4rBMVg-2F_W4lNb>PaLC;j7 zr|vXRjh3J&sA?#lf?_+zVAxVL${aTZ!kmGD9G^0kI3{OOyMiryHoc^TQ8Yud zsU@ID6>_v&U?NgHgfx>+f zYN?rJn^V(3*hv~cP+GV@tQ54s#`p{dPc*Q>)Jy}pB8PlBT|^Fwo9vTIb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i-bfnP9nZBIau0LlZqg)c@&>uPwdqGhM77i`EYw`Gc9H zi&HDo0u}ZJ2j3S?$B8sRiEQeYyfcT`_i|*j!;}@umR91EyfV)50i9hEh;$ND2?mOJ z-4ed+FYr=Gq_7tA#;8L}Q7UoyzV?f(Qg>G}QQ7hSs)Mh_$WFM;{QcC9I9}Q-QlBeH zp@cnDhRgx(4F6EWKNW7E>gTryKbIE@F);YG+&bFAEm{9fVGB2NYxuk3&un3vzU|?g zW^4G4-bN2Q5Nn55Mm5)jgJ>d*&>Orl*0en`6LsQOUBgZd*x*->4~Q*%>oLWc@b^>9 U%Y#@K@|GI+qN4u%yRoP0e|SMg%K!iX diff --git a/client/webgl_draw.js b/client/webgl_draw.js index 546799c..28247cc 100644 --- a/client/webgl_draw.js +++ b/client/webgl_draw.js @@ -25,6 +25,8 @@ function upload_if_needed(gl, buffer_kind, serializer) { } function upload_square_rgba16ui_texture(gl, serializer, texture_size) { + // TODO: only subupload what's needed + const bpp = 2 * 4; const data_size = serializer.offset; const data_pixels = data_size / bpp; // data_size % bpp is expected to always be zero here @@ -76,19 +78,11 @@ function draw_html(state) { async function draw(state, context) { const cpu_before = performance.now(); - 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); - } - locations = context.locations['sdf'].main; buffers = context.buffers['sdf']; @@ -98,6 +92,13 @@ async function draw(state, context) { const dynamic_segment_count = context.dynamic_segment_count; const dynamic_stroke_count = context.dynamic_stroke_count; + let query = null; + + if (context.gpu_timer_ext !== null) { + query = gl.createQuery(); + gl.beginQuery(context.gpu_timer_ext.TIME_ELAPSED_EXT, query); + } + // Only clear once we have the data, this might not always be on the same frame? gl.viewport(0, 0, context.canvas.width, context.canvas.height); gl.clearColor(context.bgcolor.r, context.bgcolor.g, context.bgcolor.b, 1); @@ -229,6 +230,7 @@ async function draw(state, context) { const cpu_after = performance.now(); + state.timers.raf = false; document.querySelector('.debug-timings .cpu').innerHTML = 'Last CPU Frametime: ' + Math.round((cpu_after - cpu_before) * 100) / 100 + 'ms';