@null3d/splats 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,48 +14,74 @@ export declare const CLOUD_RADIUS = 5;
14
14
  export declare const TOTAL = 6;
15
15
  /** The number of detail levels of a chunk: every splat, then half, a quarter and an eighth. */
16
16
  export declare const LEVELS = 4;
17
+ /** The place of the budget among a call's numbers. */
18
+ export declare const CALL_BUDGET = 0;
19
+ /** The place of the camera's first number among a call's numbers. */
20
+ export declare const CALL_EYE = 1;
21
+ /** The count of a call's numbers: the budget and the camera's world position. */
22
+ export declare const CALL_NUMBERS = 4;
17
23
  /**
18
- * One call's input, which the add-on reuses and refills so a call makes no object. `view` is
19
- * shared with the worker where the page allows a `SharedArrayBuffer`, so its counts land in the
20
- * add-on's buffer; without one, the worker answers with a copy.
24
+ * One call's input, which the add-on reuses and refills so a call makes no object: the operation;
25
+ * the selector's number, which names its table and scratch in this worker; the version, which
26
+ * changes when the chunk table changes (a new selector, or a new number of chunks); the number of
27
+ * chunks; the call's numbers, the most splats that draw and the camera's world position at
28
+ * `CALL_BUDGET` and `CALL_EYE`; and the whole buffer, which holds the header, the chunk table and
29
+ * the counts. `view` is shared with the worker where the page allows a `SharedArrayBuffer`, so its
30
+ * counts land in the add-on's buffer; without one, the worker answers with a copy.
31
+ *
32
+ * Inputs and answers are arrays, not objects. A worker receives a copy of each, and V8 gives a
33
+ * copied object a hidden class that it holds only weakly. A full garbage collection with no such
34
+ * object alive frees the class and discards the optimized code that read it, which then compiles
35
+ * again. V8 holds an array's hidden class for the life of the page. The numbers that change at each
36
+ * call sit in a `Float64Array`, since an array element that holds a fraction makes a new number
37
+ * object at each write.
21
38
  */
22
- export interface SelectInput {
23
- /** `select` chooses the counts; `drop` frees the selector's arrays. */
24
- readonly op?: 'select';
25
- /** The selector's number, which names its table and scratch in this worker. */
39
+ export type SelectInput = [
40
+ op: 'select',
41
+ id: number,
42
+ version: number,
43
+ chunks: number,
44
+ numbers: Float64Array,
45
+ view: Float32Array
46
+ ];
47
+ /**
48
+ * A call's answer: the number of splats that draw, then the chosen count of each chunk when the
49
+ * worker could not write into `view` itself.
50
+ */
51
+ export type SelectAnswer = [total: number, counts?: Float32Array];
52
+ /** The place of the operation in an input. */
53
+ export declare const INPUT_OP = 0;
54
+ /** The place of the selector's number in an input. */
55
+ export declare const INPUT_ID = 1;
56
+ /** The place of the chunk table's version in a selection's input. */
57
+ export declare const INPUT_VERSION = 2;
58
+ /** The place of the number of chunks in a selection's input. */
59
+ export declare const INPUT_CHUNKS = 3;
60
+ /** The place of the call's numbers in a selection's input. */
61
+ export declare const INPUT_NUMBERS = 4;
62
+ /** The place of the whole buffer in a selection's input. */
63
+ export declare const INPUT_VIEW = 5;
64
+ /** The place of the total that draws in an answer. */
65
+ export declare const ANSWER_TOTAL = 0;
66
+ /** The place of the chosen counts in an answer, where the page shares no memory. */
67
+ export declare const ANSWER_COUNTS = 1;
68
+ /** The parts of a selection's input, which `selectInput` puts at their named places. */
69
+ export interface SelectInputParts {
26
70
  readonly id: number;
27
- /** Changes when the chunk table changes: a new selector, or a new number of chunks. */
28
71
  readonly version: number;
29
- /** The number of chunks. */
30
72
  readonly chunks: number;
31
- /** The most splats that draw. */
32
- budget: number;
33
- /** The camera's world position. */
34
- eyeX: number;
35
- eyeY: number;
36
- eyeZ: number;
37
- /** The whole buffer: the header, the chunk table and the counts. */
73
+ readonly numbers: Float64Array;
38
74
  readonly view: Float32Array;
39
75
  }
40
- /** A call's answer: the total that draws, and the counts when the page shares no memory. */
41
- export interface SelectAnswer {
42
- /** The number of splats that draw. */
43
- readonly total: number;
44
- /** The chosen count of each chunk, when the worker could not write into `view` itself. */
45
- readonly counts?: Float32Array;
46
- }
47
- /** An input that frees the arrays of a selector, which a cloud leaves behind. */
48
- export interface DropInput {
49
- readonly op: 'drop';
50
- readonly id: number;
51
- }
76
+ /** A selection's input, made from its parts at their named places. */
77
+ export declare function selectInput(parts: SelectInputParts): SelectInput;
78
+ /** An input that frees the arrays of a selector, which a cloud leaves behind: its number. */
79
+ export type DropInput = [op: 'drop', id: number];
52
80
  /**
53
81
  * An input that only starts the module in the worker, which a cloud sends before it knows its
54
82
  * chunks, so its first selection finds the task loaded.
55
83
  */
56
- export interface StartInput {
57
- readonly op: 'start';
58
- }
84
+ export type StartInput = [op: 'start'];
59
85
  /**
60
86
  * Runs one call. The engine calls this once for each task, with the compiled WebAssembly module
61
87
  * that the task names.
@@ -25,6 +25,32 @@ export const CLOUD_RADIUS = 5;
25
25
  export const TOTAL = 6;
26
26
  /** The number of detail levels of a chunk: every splat, then half, a quarter and an eighth. */
27
27
  export const LEVELS = 4;
28
+ /** The place of the budget among a call's numbers. */
29
+ export const CALL_BUDGET = 0;
30
+ /** The place of the camera's first number among a call's numbers. */
31
+ export const CALL_EYE = 1;
32
+ /** The count of a call's numbers: the budget and the camera's world position. */
33
+ export const CALL_NUMBERS = 4;
34
+ /** The place of the operation in an input. */
35
+ export const INPUT_OP = 0;
36
+ /** The place of the selector's number in an input. */
37
+ export const INPUT_ID = 1;
38
+ /** The place of the chunk table's version in a selection's input. */
39
+ export const INPUT_VERSION = 2;
40
+ /** The place of the number of chunks in a selection's input. */
41
+ export const INPUT_CHUNKS = 3;
42
+ /** The place of the call's numbers in a selection's input. */
43
+ export const INPUT_NUMBERS = 4;
44
+ /** The place of the whole buffer in a selection's input. */
45
+ export const INPUT_VIEW = 5;
46
+ /** The place of the total that draws in an answer. */
47
+ export const ANSWER_TOTAL = 0;
48
+ /** The place of the chosen counts in an answer, where the page shares no memory. */
49
+ export const ANSWER_COUNTS = 1;
50
+ /** A selection's input, made from its parts at their named places. */
51
+ export function selectInput(parts) {
52
+ return ['select', parts.id, parts.version, parts.chunks, parts.numbers, parts.view];
53
+ }
28
54
  /** The module, which the worker instantiates at its first task and keeps. */
29
55
  let wasm;
30
56
  /**
@@ -121,23 +147,27 @@ const PARAMS = 12;
121
147
  * state's arrays behind for each chunk.
122
148
  */
123
149
  function stateFor(input) {
124
- const kept = states.get(input.id);
125
- if (kept && kept.capacity >= input.chunks) {
150
+ const id = input[INPUT_ID];
151
+ const version = input[INPUT_VERSION];
152
+ const chunks = input[INPUT_CHUNKS];
153
+ const view = input[INPUT_VIEW];
154
+ const kept = states.get(id);
155
+ if (kept && kept.capacity >= chunks) {
126
156
  // The caller names a new version when the chunk table changes, so a moved frame keeps the
127
157
  // copy that the worker already holds and copies no table.
128
- if (kept.version === input.version && kept.chunks === input.chunks)
158
+ if (kept.version === version && kept.chunks === chunks)
129
159
  return kept;
130
- kept.version = input.version;
131
- kept.chunks = input.chunks;
132
- writeBytes(kept.table, input.view.subarray(HEADER, HEADER + input.chunks * STRIDE));
160
+ kept.version = version;
161
+ kept.chunks = chunks;
162
+ writeBytes(kept.table, view.subarray(HEADER, HEADER + chunks * STRIDE));
133
163
  return kept;
134
164
  }
135
- const capacity = kept ? Math.max(input.chunks, kept.capacity * 2) : input.chunks;
165
+ const capacity = kept ? Math.max(chunks, kept.capacity * 2) : chunks;
136
166
  if (kept)
137
167
  drop(kept);
138
168
  const state = {
139
- version: input.version,
140
- chunks: input.chunks,
169
+ version,
170
+ chunks,
141
171
  capacity,
142
172
  params: alloc(PARAMS * FLOAT_BYTES),
143
173
  table: alloc(capacity * STRIDE * 4),
@@ -147,20 +177,23 @@ function stateFor(input) {
147
177
  order: alloc(capacity * 4),
148
178
  scratch: alloc(capacity * 4),
149
179
  };
150
- writeBytes(state.table, input.view.subarray(HEADER, HEADER + input.chunks * STRIDE));
151
- states.set(input.id, state);
180
+ writeBytes(state.table, view.subarray(HEADER, HEADER + chunks * STRIDE));
181
+ states.set(id, state);
152
182
  return state;
153
183
  }
154
184
  /** Chooses each chunk's count, writes it into the caller's buffer, and answers the total. */
155
185
  function select(module, state, input) {
186
+ const chunks = input[INPUT_CHUNKS];
187
+ const numbers = input[INPUT_NUMBERS];
188
+ const view = input[INPUT_VIEW];
156
189
  // The parameter block, written in place so a call makes no array.
157
190
  const params = new Float64Array(module.memory.buffer, state.params, PARAMS);
158
- params[0] = input.chunks;
159
- params[1] = input.budget;
160
- params[2] = input.eyeX;
161
- params[3] = input.eyeY;
162
- params[4] = input.eyeZ;
163
- params[5] = input.view[CLOUD_RADIUS];
191
+ params[0] = chunks;
192
+ params[1] = numbers[CALL_BUDGET];
193
+ params[2] = numbers[CALL_EYE];
194
+ params[3] = numbers[CALL_EYE + 1];
195
+ params[4] = numbers[CALL_EYE + 2];
196
+ params[5] = view[CLOUD_RADIUS];
164
197
  params[6] = state.table;
165
198
  params[7] = state.counts;
166
199
  params[8] = state.distances;
@@ -169,14 +202,14 @@ function select(module, state, input) {
169
202
  params[11] = state.scratch;
170
203
  const total = module.splat_select(state.params);
171
204
  // The counts follow the chunk table in the buffer that the sketch reads.
172
- if (shared(input.view)) {
173
- const offset = (HEADER + input.chunks * STRIDE) * 4;
174
- readBytes(state.counts, new Float32Array(input.view.buffer, offset, input.chunks));
175
- return { total };
205
+ if (shared(view)) {
206
+ const offset = (HEADER + chunks * STRIDE) * 4;
207
+ readBytes(state.counts, new Float32Array(view.buffer, offset, chunks));
208
+ return [total];
176
209
  }
177
- const copy = new Float32Array(input.chunks);
210
+ const copy = new Float32Array(chunks);
178
211
  readBytes(state.counts, copy);
179
- return { total, counts: copy };
212
+ return [total, copy];
180
213
  }
181
214
  /**
182
215
  * Runs one call. The engine calls this once for each task, with the compiled WebAssembly module
@@ -184,17 +217,18 @@ function select(module, state, input) {
184
217
  */
185
218
  export function run(input, modules) {
186
219
  const request = input;
187
- if (request.op === 'drop') {
188
- const state = states.get(request.id);
220
+ if (request[INPUT_OP] === 'drop') {
221
+ const id = request[INPUT_ID];
222
+ const state = states.get(id);
189
223
  if (state) {
190
224
  drop(state);
191
- states.delete(request.id);
225
+ states.delete(id);
192
226
  }
193
- return { output: { total: 0 } };
227
+ return { output: [0] };
194
228
  }
195
229
  const module = wasm ?? start(modules('splats'));
196
- if (request.op === 'start')
197
- return { output: { total: 0 } };
230
+ if (request[INPUT_OP] === 'start')
231
+ return { output: [0] };
198
232
  const state = stateFor(request);
199
233
  return { output: select(module, state, request) };
200
234
  }
package/lib/select.d.ts CHANGED
@@ -17,6 +17,8 @@ export declare class SplatSelector {
17
17
  private readonly view;
18
18
  /** The task's input, which keeps its shape so a call makes no object. */
19
19
  private readonly input;
20
+ /** The input's numbers, which each call writes: the budget and the camera. */
21
+ private readonly numbers;
20
22
  /** How many splats draw of each chunk, which the task writes in the shared buffer. */
21
23
  readonly counts: Float32Array;
22
24
  /** This selector's number, which a grown cloud hands to its next selector. */
package/lib/select.js CHANGED
@@ -5,7 +5,7 @@
5
5
  // the counts. The task writes the counts into the buffer, which the sketch reads straight from it,
6
6
  // so no count crosses a message on a page with shared memory and a call allocates no array.
7
7
  import { runTask } from '@null3d/engine';
8
- import { BUDGET, CHUNK_COUNT, CLOUD_RADIUS, EYE, HEADER, STRIDE, } from './select-task.js';
8
+ import { BUDGET, CALL_BUDGET, CALL_EYE, CALL_NUMBERS, CHUNK_COUNT, CLOUD_RADIUS, EYE, HEADER, STRIDE, selectInput, } from './select-task.js';
9
9
  /** The task that chooses the splats: its module, and the WebAssembly module that it calls. */
10
10
  export const SELECT_TASK = {
11
11
  name: 'splats-select',
@@ -19,7 +19,7 @@ export const SELECT_TASK = {
19
19
  ],
20
20
  };
21
21
  /** The input that starts the task's module in a worker. */
22
- const START = { op: 'start' };
22
+ const START = ['start'];
23
23
  /** The task's start while it runs, which the next selection follows. */
24
24
  let starting;
25
25
  /**
@@ -49,6 +49,8 @@ export class SplatSelector {
49
49
  view;
50
50
  /** The task's input, which keeps its shape so a call makes no object. */
51
51
  input;
52
+ /** The input's numbers, which each call writes: the budget and the camera. */
53
+ numbers = new Float64Array(CALL_NUMBERS);
52
54
  /** How many splats draw of each chunk, which the task writes in the shared buffer. */
53
55
  counts;
54
56
  /** This selector's number, which a grown cloud hands to its next selector. */
@@ -65,17 +67,13 @@ export class SplatSelector {
65
67
  : new SharedArrayBuffer(floats * 4);
66
68
  this.view = new Float32Array(buffer);
67
69
  this.id = previousId ?? ++lastId;
68
- this.input = {
69
- op: 'select',
70
+ this.input = selectInput({
70
71
  id: this.id,
71
72
  version: ++lastVersion,
72
73
  chunks: chunks.length,
73
- budget: 0,
74
- eyeX: 0,
75
- eyeY: 0,
76
- eyeZ: 0,
74
+ numbers: this.numbers,
77
75
  view: this.view,
78
- };
76
+ });
79
77
  this.view[CHUNK_COUNT] = chunks.length;
80
78
  this.view[CLOUD_RADIUS] = cloudRadiusOf(chunks);
81
79
  this.counts = this.view.subarray(HEADER + chunks.length * STRIDE, floats);
@@ -100,10 +98,10 @@ export class SplatSelector {
100
98
  this.view[EYE] = eye[0];
101
99
  this.view[EYE + 1] = eye[1];
102
100
  this.view[EYE + 2] = eye[2];
103
- this.input.budget = budget;
104
- this.input.eyeX = eye[0];
105
- this.input.eyeY = eye[1];
106
- this.input.eyeZ = eye[2];
101
+ this.numbers[CALL_BUDGET] = budget;
102
+ this.numbers[CALL_EYE] = eye[0];
103
+ this.numbers[CALL_EYE + 1] = eye[1];
104
+ this.numbers[CALL_EYE + 2] = eye[2];
107
105
  if (starting)
108
106
  return starting.then(() => runTask(SELECT_TASK, this.input));
109
107
  return runTask(SELECT_TASK, this.input);
@@ -113,7 +111,8 @@ export class SplatSelector {
113
111
  * cloud does, so a page that makes and frees clouds does not grow the module's memory.
114
112
  */
115
113
  drop() {
116
- void runTask(SELECT_TASK, { op: 'drop', id: this.id }).catch(() => undefined);
114
+ const input = ['drop', this.id];
115
+ void runTask(SELECT_TASK, input).catch(() => undefined);
117
116
  }
118
117
  }
119
118
  /**