antimatter15 commited on
Commit
e7bc5f9
1 Parent(s): d85a1ef

4x faster sorting

Browse files
Files changed (1) hide show
  1. main.js +84 -37
main.js CHANGED
@@ -296,34 +296,57 @@ function createWorker(self) {
296
  // RGBA - colors (uint8)
297
  // IJKL - quaternion/rot (uint8)
298
  const rowLength = 3 * 4 + 3 * 4 + 4 + 4;
 
 
299
 
300
  const runSort = (viewProj) => {
301
  if (!buffer) return;
302
 
303
- // console.time("sort");
 
304
  const f_buffer = new Float32Array(buffer);
305
  const u_buffer = new Uint8Array(buffer);
306
 
307
- const depthList = new Float32Array(vertexCount);
308
- const depthIndex = new Uint32Array(vertexCount);
309
- for (let j = 0; j < vertexCount; j++) {
310
- // For some reason dividing by wumbo actually causes
311
- // problems, so this is the unnormalized camera space homo
312
- depthList[j] =
313
- viewProj[2] * f_buffer[8 * j + 0] +
314
- viewProj[6] * f_buffer[8 * j + 1] +
315
- viewProj[10] * f_buffer[8 * j + 2];
316
- depthIndex[j] = j;
317
- }
318
- depthIndex.sort((a, b) => depthList[a] - depthList[b]);
319
-
320
  const quat = new Float32Array(4 * vertexCount);
321
  const scale = new Float32Array(3 * vertexCount);
322
  const center = new Float32Array(3 * vertexCount);
323
  const color = new Float32Array(4 * vertexCount);
324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
  for (let j = 0; j < vertexCount; j++) {
326
- const i = depthIndex[j];
327
 
328
  quat[4 * j + 0] = (u_buffer[32 * i + 28 + 0] - 128) / 128;
329
  quat[4 * j + 1] = (u_buffer[32 * i + 28 + 1] - 128) / 128;
@@ -344,7 +367,7 @@ function createWorker(self) {
344
  scale[3 * j + 2] = f_buffer[8 * i + 3 + 2];
345
  }
346
 
347
- self.postMessage({ quat, center, color, scale }, [
348
  quat.buffer,
349
  center.buffer,
350
  color.buffer,
@@ -792,6 +815,9 @@ async function main() {
792
  gl.vertexAttribPointer(a_scale, 3, gl.FLOAT, false, 0, 0);
793
  ext.vertexAttribDivisorANGLE(a_scale, 1); // Use the extension here
794
 
 
 
 
795
  worker.onmessage = (e) => {
796
  if (e.data.buffer) {
797
  splatData = new Uint8Array(e.data.buffer);
@@ -804,7 +830,10 @@ async function main() {
804
  document.body.appendChild(link);
805
  link.click();
806
  } else {
807
- let { quat, scale, center, color } = e.data;
 
 
 
808
  vertexCount = quat.length / 4;
809
 
810
  gl.bindBuffer(gl.ARRAY_BUFFER, centerBuffer);
@@ -843,9 +872,9 @@ async function main() {
843
  window.addEventListener("keyup", (e) => {
844
  activeKeys = activeKeys.filter((k) => k !== e.key);
845
  });
846
- window.addEventListener('blur', () => {
847
- activeKeys = []
848
- })
849
 
850
  window.addEventListener(
851
  "wheel",
@@ -877,14 +906,13 @@ async function main() {
877
  0,
878
  (-10 * (e.deltaY * scale)) / innerHeight,
879
  );
880
- inv[13] = preY
881
  } else {
882
  let d = 4;
883
  inv = translate4(inv, 0, 0, d);
884
  inv = rotate4(inv, -(e.deltaX * scale) / innerWidth, 0, 1, 0);
885
  inv = rotate4(inv, (e.deltaY * scale) / innerHeight, 1, 0, 0);
886
  inv = translate4(inv, 0, 0, -d);
887
-
888
  }
889
 
890
  viewMatrix = invert4(inv);
@@ -912,10 +940,9 @@ async function main() {
912
  e.preventDefault();
913
  if (down == 1) {
914
  let inv = invert4(viewMatrix);
915
- let dx = 5 * (e.clientX - startX) / innerWidth;
916
- let dy = 5 * (e.clientY - startY) / innerHeight;
917
  let d = 4;
918
-
919
 
920
  inv = translate4(inv, 0, 0, d);
921
  inv = rotate4(inv, dx, 0, 1, 0);
@@ -938,7 +965,7 @@ async function main() {
938
  0,
939
  (10 * (e.clientY - startY)) / innerHeight,
940
  );
941
- inv[13] = preY
942
  viewMatrix = invert4(inv);
943
 
944
  startX = e.clientX;
@@ -1028,7 +1055,7 @@ async function main() {
1028
 
1029
  let preY = inv[13];
1030
  inv = translate4(inv, 0, 0, 3 * (1 - dscale));
1031
- inv[13] = preY
1032
 
1033
  viewMatrix = invert4(inv);
1034
 
@@ -1060,16 +1087,16 @@ async function main() {
1060
 
1061
  const frame = (now) => {
1062
  let inv = invert4(viewMatrix);
1063
-
1064
- if (activeKeys.includes("ArrowUp")){
1065
  let preY = inv[13];
1066
  inv = translate4(inv, 0, 0, 0.1);
1067
- inv[13] = preY
1068
  }
1069
- if (activeKeys.includes("ArrowDown")){
1070
  let preY = inv[13];
1071
  inv = translate4(inv, 0, 0, -0.1);
1072
- inv[13] = preY
1073
  }
1074
  if (activeKeys.includes("ArrowLeft"))
1075
  inv = translate4(inv, -0.03, 0, 0);
@@ -1084,14 +1111,34 @@ async function main() {
1084
  if (activeKeys.includes("w")) inv = rotate4(inv, 0.005, 1, 0, 0);
1085
  if (activeKeys.includes("s")) inv = rotate4(inv, -0.005, 1, 0, 0);
1086
 
1087
- if(['j', 'k', 'l', 'i'].some(k => activeKeys.includes(k))) {
1088
  let d = 4;
1089
  inv = translate4(inv, 0, 0, d);
1090
- inv = rotate4(inv, activeKeys.includes('j') ? -0.05: activeKeys.includes('l') ? 0.05 : 0, 0, 1, 0);
1091
- inv = rotate4(inv, activeKeys.includes('i') ? 0.05 : activeKeys.includes('k') ? -0.05 : 0, 1, 0, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1092
  inv = translate4(inv, 0, 0, -d);
1093
  }
1094
-
1095
  // inv[13] = preY;
1096
  viewMatrix = invert4(inv);
1097
 
@@ -1129,7 +1176,7 @@ async function main() {
1129
  } else {
1130
  gl.clear(gl.COLOR_BUFFER_BIT);
1131
  document.getElementById("spinner").style.display = "";
1132
- start = Date.now() + 2000
1133
  }
1134
  const progress = (100 * vertexCount) / (splatData.length / rowLength);
1135
  if (progress < 100) {
 
296
  // RGBA - colors (uint8)
297
  // IJKL - quaternion/rot (uint8)
298
  const rowLength = 3 * 4 + 3 * 4 + 4 + 4;
299
+ let depthMix = new BigInt64Array();
300
+ let lastProj = [];
301
 
302
  const runSort = (viewProj) => {
303
  if (!buffer) return;
304
 
305
+
306
+
307
  const f_buffer = new Float32Array(buffer);
308
  const u_buffer = new Uint8Array(buffer);
309
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310
  const quat = new Float32Array(4 * vertexCount);
311
  const scale = new Float32Array(3 * vertexCount);
312
  const center = new Float32Array(3 * vertexCount);
313
  const color = new Float32Array(4 * vertexCount);
314
 
315
+ if (depthMix.length !== vertexCount) {
316
+ depthMix = new BigInt64Array(vertexCount);
317
+ const indexMix = new Uint32Array(depthMix.buffer);
318
+ for (let j = 0; j < vertexCount; j++) {
319
+ indexMix[2 * j] = j;
320
+ }
321
+ } else {
322
+ let dot =
323
+ lastProj[2] * viewProj[2] +
324
+ lastProj[6] * viewProj[6] +
325
+ lastProj[10] * viewProj[10];
326
+ if (Math.abs(dot - 1) < 0.01) {
327
+ return;
328
+ }
329
+ }
330
+ // console.time("sort");
331
+
332
+ const floatMix = new Float32Array(depthMix.buffer);
333
+ const indexMix = new Uint32Array(depthMix.buffer);
334
+
335
+ for (let j = 0; j < vertexCount; j++) {
336
+ let i = indexMix[2 * j];
337
+ floatMix[2 * j + 1] =
338
+ 10000 +
339
+ viewProj[2] * f_buffer[8 * i + 0] +
340
+ viewProj[6] * f_buffer[8 * i + 1] +
341
+ viewProj[10] * f_buffer[8 * i + 2];
342
+ }
343
+
344
+ lastProj = viewProj;
345
+
346
+ depthMix.sort();
347
+
348
  for (let j = 0; j < vertexCount; j++) {
349
+ const i = indexMix[2 * j];
350
 
351
  quat[4 * j + 0] = (u_buffer[32 * i + 28 + 0] - 128) / 128;
352
  quat[4 * j + 1] = (u_buffer[32 * i + 28 + 1] - 128) / 128;
 
367
  scale[3 * j + 2] = f_buffer[8 * i + 3 + 2];
368
  }
369
 
370
+ self.postMessage({ quat, center, color, scale, viewProj }, [
371
  quat.buffer,
372
  center.buffer,
373
  color.buffer,
 
815
  gl.vertexAttribPointer(a_scale, 3, gl.FLOAT, false, 0, 0);
816
  ext.vertexAttribDivisorANGLE(a_scale, 1); // Use the extension here
817
 
818
+ let lastProj = []
819
+ let lastData
820
+
821
  worker.onmessage = (e) => {
822
  if (e.data.buffer) {
823
  splatData = new Uint8Array(e.data.buffer);
 
830
  document.body.appendChild(link);
831
  link.click();
832
  } else {
833
+ let { quat, scale, center, color, viewProj } = e.data;
834
+ lastData = e.data;
835
+
836
+ lastProj = viewProj
837
  vertexCount = quat.length / 4;
838
 
839
  gl.bindBuffer(gl.ARRAY_BUFFER, centerBuffer);
 
872
  window.addEventListener("keyup", (e) => {
873
  activeKeys = activeKeys.filter((k) => k !== e.key);
874
  });
875
+ window.addEventListener("blur", () => {
876
+ activeKeys = [];
877
+ });
878
 
879
  window.addEventListener(
880
  "wheel",
 
906
  0,
907
  (-10 * (e.deltaY * scale)) / innerHeight,
908
  );
909
+ inv[13] = preY;
910
  } else {
911
  let d = 4;
912
  inv = translate4(inv, 0, 0, d);
913
  inv = rotate4(inv, -(e.deltaX * scale) / innerWidth, 0, 1, 0);
914
  inv = rotate4(inv, (e.deltaY * scale) / innerHeight, 1, 0, 0);
915
  inv = translate4(inv, 0, 0, -d);
 
916
  }
917
 
918
  viewMatrix = invert4(inv);
 
940
  e.preventDefault();
941
  if (down == 1) {
942
  let inv = invert4(viewMatrix);
943
+ let dx = (5 * (e.clientX - startX)) / innerWidth;
944
+ let dy = (5 * (e.clientY - startY)) / innerHeight;
945
  let d = 4;
 
946
 
947
  inv = translate4(inv, 0, 0, d);
948
  inv = rotate4(inv, dx, 0, 1, 0);
 
965
  0,
966
  (10 * (e.clientY - startY)) / innerHeight,
967
  );
968
+ inv[13] = preY;
969
  viewMatrix = invert4(inv);
970
 
971
  startX = e.clientX;
 
1055
 
1056
  let preY = inv[13];
1057
  inv = translate4(inv, 0, 0, 3 * (1 - dscale));
1058
+ inv[13] = preY;
1059
 
1060
  viewMatrix = invert4(inv);
1061
 
 
1087
 
1088
  const frame = (now) => {
1089
  let inv = invert4(viewMatrix);
1090
+
1091
+ if (activeKeys.includes("ArrowUp")) {
1092
  let preY = inv[13];
1093
  inv = translate4(inv, 0, 0, 0.1);
1094
+ inv[13] = preY;
1095
  }
1096
+ if (activeKeys.includes("ArrowDown")) {
1097
  let preY = inv[13];
1098
  inv = translate4(inv, 0, 0, -0.1);
1099
+ inv[13] = preY;
1100
  }
1101
  if (activeKeys.includes("ArrowLeft"))
1102
  inv = translate4(inv, -0.03, 0, 0);
 
1111
  if (activeKeys.includes("w")) inv = rotate4(inv, 0.005, 1, 0, 0);
1112
  if (activeKeys.includes("s")) inv = rotate4(inv, -0.005, 1, 0, 0);
1113
 
1114
+ if (["j", "k", "l", "i"].some((k) => activeKeys.includes(k))) {
1115
  let d = 4;
1116
  inv = translate4(inv, 0, 0, d);
1117
+ inv = rotate4(
1118
+ inv,
1119
+ activeKeys.includes("j")
1120
+ ? -0.05
1121
+ : activeKeys.includes("l")
1122
+ ? 0.05
1123
+ : 0,
1124
+ 0,
1125
+ 1,
1126
+ 0,
1127
+ );
1128
+ inv = rotate4(
1129
+ inv,
1130
+ activeKeys.includes("i")
1131
+ ? 0.05
1132
+ : activeKeys.includes("k")
1133
+ ? -0.05
1134
+ : 0,
1135
+ 1,
1136
+ 0,
1137
+ 0,
1138
+ );
1139
  inv = translate4(inv, 0, 0, -d);
1140
  }
1141
+
1142
  // inv[13] = preY;
1143
  viewMatrix = invert4(inv);
1144
 
 
1176
  } else {
1177
  gl.clear(gl.COLOR_BUFFER_BIT);
1178
  document.getElementById("spinner").style.display = "";
1179
+ start = Date.now() + 2000;
1180
  }
1181
  const progress = (100 * vertexCount) / (splatData.length / rowLength);
1182
  if (progress < 100) {