@threenative/three-native 0.0.0-stage → 0.3.5
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.
- package/LICENSE +21 -0
- package/README.md +24 -2
- package/package.json +42 -3
- package/src/addons/buffer-geometry-utils.ts +14 -0
- package/src/addons/data-utils.ts +63 -0
- package/src/addons/decal-geometry.ts +25 -0
- package/src/addons/decal.ts +218 -0
- package/src/addons/gltf-loader-web.ts +323 -0
- package/src/addons/hdr-loader.ts +71 -0
- package/src/addons/hdr.ts +139 -0
- package/src/addons/lut-cube-loader-web.ts +17 -0
- package/src/addons/lut-cube-loader.ts +149 -0
- package/src/addons/merge-geometries.ts +374 -0
- package/src/addons/mesh-bvh-webgpu.ts +13 -0
- package/src/addons/mesh-bvh.ts +50 -0
- package/src/addons/post-effects-web.ts +22 -0
- package/src/addons/post-effects.ts +173 -0
- package/src/audio.ts +564 -0
- package/src/blend-mode.ts +33 -0
- package/src/browser-backend.ts +2156 -0
- package/src/browser-entry.ts +305 -0
- package/src/browser-math.ts +448 -0
- package/src/browser-renderer.ts +527 -0
- package/src/browser-tsl.ts +650 -0
- package/src/catalog.ts +497 -0
- package/src/fixture-format.ts +721 -0
- package/src/fixture-native.ts +426 -0
- package/src/fixture-protocol.ts +315 -0
- package/src/math-utils.ts +65 -0
- package/src/object-surface.ts +250 -0
- package/src/pass-node.ts +286 -0
- package/src/property-binding.ts +93 -0
- package/src/quad-mesh.ts +54 -0
- package/src/reflector.ts +89 -0
- package/src/refused.ts +27 -0
- package/src/render-target.ts +107 -0
- package/src/rtt.ts +158 -0
- package/src/shadow-map.ts +21 -0
- package/src/storage.ts +206 -0
- package/src/texture-sources.ts +252 -0
- package/src/tsl-extras.ts +51 -0
- package/src/tsl-struct.ts +20 -0
- package/src/tsl-uniforms.ts +234 -0
- package/src/update-ranges.ts +109 -0
- package/src/wgsl-fn.ts +55 -0
|
@@ -0,0 +1,721 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The compatibility fixture format, its goldens, and the reader that fails closed.
|
|
3
|
+
*
|
|
4
|
+
* One fixture is a script plus a non-empty observation set: `ops` build state, `observe` reads
|
|
5
|
+
* it, and the tolerance decides when the native engine's answer matches. The same fixture is
|
|
6
|
+
* executed twice — once in Node against the pinned `three` to produce a golden
|
|
7
|
+
* (`tests/compatibility/run-reference.ts`), once against a native driver that speaks the line
|
|
8
|
+
* protocol (`tests/compatibility/run-native.ts`).
|
|
9
|
+
*
|
|
10
|
+
* A golden records every number as its 16-hex-digit IEEE-754 binary64 bit string, because JSON
|
|
11
|
+
* cannot hold `-0`, `NaN` or a NaN payload and those are exactly the values a compatibility
|
|
12
|
+
* fixture exists to compare. The decimal next to it is for humans and is never compared.
|
|
13
|
+
*
|
|
14
|
+
* pnpm --filter @threenative/three-native test:reference
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
18
|
+
import path from "node:path";
|
|
19
|
+
|
|
20
|
+
/** The package root. Derived from this file so a spec and a package script agree. */
|
|
21
|
+
export const PACKAGE_ROOT = path.join(import.meta.dirname, "..");
|
|
22
|
+
export const REPO_ROOT = path.join(PACKAGE_ROOT, "..", "..");
|
|
23
|
+
export const FIXTURES_DIR = path.join(PACKAGE_ROOT, "tests", "compatibility", "fixtures");
|
|
24
|
+
export const GOLDENS_DIR = path.join(PACKAGE_ROOT, "tests", "compatibility", "goldens");
|
|
25
|
+
|
|
26
|
+
/** Kinds a golden can carry. Anything else is a typo, and a typo must not read as a match. */
|
|
27
|
+
export const OBSERVATION_KINDS = [
|
|
28
|
+
"number",
|
|
29
|
+
"numbers",
|
|
30
|
+
"boolean",
|
|
31
|
+
"string",
|
|
32
|
+
"json",
|
|
33
|
+
"pixels",
|
|
34
|
+
] as const;
|
|
35
|
+
export type ObservationKind = (typeof OBSERVATION_KINDS)[number];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The tone mappings a render fixture may name, and the three constant each one is.
|
|
39
|
+
*
|
|
40
|
+
* The constant names are shipped to the browser instead of being written twice, so a new mapping
|
|
41
|
+
* cannot be spelled in one half of the runner and missing from the other.
|
|
42
|
+
*/
|
|
43
|
+
export const TONE_MAPPING_CONSTANTS = {
|
|
44
|
+
none: "NoToneMapping",
|
|
45
|
+
linear: "LinearToneMapping",
|
|
46
|
+
reinhard: "ReinhardToneMapping",
|
|
47
|
+
cineon: "CineonToneMapping",
|
|
48
|
+
aces: "ACESFilmicToneMapping",
|
|
49
|
+
agx: "AgXToneMapping",
|
|
50
|
+
neutral: "NeutralToneMapping",
|
|
51
|
+
} as const;
|
|
52
|
+
export type ToneMappingName = keyof typeof TONE_MAPPING_CONSTANTS;
|
|
53
|
+
export const TONE_MAPPINGS = Object.keys(TONE_MAPPING_CONSTANTS) as readonly ToneMappingName[];
|
|
54
|
+
|
|
55
|
+
/** What the renderer writes into the canvas. `linear` leaves the working space untouched. */
|
|
56
|
+
export const OUTPUT_COLOR_SPACES = ["srgb", "linear"] as const;
|
|
57
|
+
export type OutputColorSpace = (typeof OUTPUT_COLOR_SPACES)[number];
|
|
58
|
+
|
|
59
|
+
/** How close two renders must be for the differential runner to call them the same frame. */
|
|
60
|
+
export interface IPixelsMetric {
|
|
61
|
+
readonly maxPixelMismatchRatio: number;
|
|
62
|
+
readonly maxPerceptualDeltaE: number;
|
|
63
|
+
/**
|
|
64
|
+
* Channel levels a pixel may differ by and still count as matching (default 0). Only for a
|
|
65
|
+
* frame that accumulates float state over many frames, where exact 8-bit agreement is not a
|
|
66
|
+
* reachable target; `levelsReason` says why.
|
|
67
|
+
*/
|
|
68
|
+
readonly levels?: number;
|
|
69
|
+
readonly levelsReason?: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The one frame a render fixture renders, and the settings that frame is captured under. */
|
|
73
|
+
export interface IFixtureRender {
|
|
74
|
+
readonly scene: string;
|
|
75
|
+
readonly camera: string;
|
|
76
|
+
readonly width: number;
|
|
77
|
+
readonly height: number;
|
|
78
|
+
readonly toneMapping: ToneMappingName;
|
|
79
|
+
readonly toneMappingExposure: number;
|
|
80
|
+
readonly outputColorSpace: OutputColorSpace;
|
|
81
|
+
/** three's `renderer.shadowMap.enabled` (PCFShadowMap); absent is off. */
|
|
82
|
+
readonly shadowMap?: boolean;
|
|
83
|
+
/** three's `renderer.shadowMap.type` while shadows are on; absent is PCFShadowMap. */
|
|
84
|
+
readonly shadowMapType?: "PCFShadowMap" | "PCFSoftShadowMap";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** What `navigator.gpu` reported about the adapter that drew the golden frame. */
|
|
88
|
+
export interface IAdapterInfo {
|
|
89
|
+
readonly architecture: string;
|
|
90
|
+
readonly description: string;
|
|
91
|
+
readonly device: string;
|
|
92
|
+
readonly vendor: string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The capture itself: the PNG next to the JSON golden, its hash, and the adapter that drew it. */
|
|
96
|
+
export interface IRenderGolden {
|
|
97
|
+
readonly png: string;
|
|
98
|
+
readonly pngSha256: string;
|
|
99
|
+
readonly width: number;
|
|
100
|
+
readonly height: number;
|
|
101
|
+
readonly adapter: IAdapterInfo;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** JSON has no NaN, Infinity or -0, so those numbers travel under a name. */
|
|
105
|
+
export type FixtureNumberName = "NaN" | "Infinity" | "-Infinity" | "-0";
|
|
106
|
+
export const FIXTURE_NUMBER_NAMES: readonly FixtureNumberName[] = [
|
|
107
|
+
"NaN",
|
|
108
|
+
"Infinity",
|
|
109
|
+
"-Infinity",
|
|
110
|
+
"-0",
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
export interface IFixtureRef {
|
|
114
|
+
readonly ref: string;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface IFixtureNumber {
|
|
118
|
+
readonly num: FixtureNumberName;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The typed arrays a fixture may build: a geometry attribute's own storage. */
|
|
122
|
+
export const FIXTURE_ARRAY_TYPES = [
|
|
123
|
+
"Float32Array",
|
|
124
|
+
"Uint8Array",
|
|
125
|
+
"Uint16Array",
|
|
126
|
+
"Uint32Array",
|
|
127
|
+
] as const;
|
|
128
|
+
export type FixtureArrayType = (typeof FIXTURE_ARRAY_TYPES)[number];
|
|
129
|
+
|
|
130
|
+
/** `new <type>(array)`: a typed array of finite numbers, for a BufferAttribute. */
|
|
131
|
+
export interface IFixtureArray {
|
|
132
|
+
readonly array: readonly number[];
|
|
133
|
+
readonly type: FixtureArrayType;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** A plain array of bound objects, as `new Skeleton([bone0, bone1])` takes. */
|
|
137
|
+
export interface IFixtureRefs {
|
|
138
|
+
readonly refs: readonly string[];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A plain options object of scalars, as `new ExtrudeGeometry(shape, { depth: 2 })` takes. */
|
|
142
|
+
export interface IFixtureRecord {
|
|
143
|
+
readonly record: Readonly<Record<string, number | string | boolean | null>>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Every argument an op or observation carries. */
|
|
147
|
+
export type FixtureArg =
|
|
148
|
+
| number
|
|
149
|
+
| string
|
|
150
|
+
| boolean
|
|
151
|
+
| null
|
|
152
|
+
| IFixtureRef
|
|
153
|
+
| IFixtureNumber
|
|
154
|
+
| IFixtureArray
|
|
155
|
+
| IFixtureRefs
|
|
156
|
+
| IFixtureRecord;
|
|
157
|
+
|
|
158
|
+
/** `abs` is an absolute difference; `ulps` is a distance in representable doubles. At least one. */
|
|
159
|
+
export interface IFixtureTolerance {
|
|
160
|
+
readonly abs?: number;
|
|
161
|
+
readonly ulps?: number;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface INewOp {
|
|
165
|
+
readonly op: "new";
|
|
166
|
+
readonly id: string;
|
|
167
|
+
readonly class: string;
|
|
168
|
+
readonly args: readonly FixtureArg[];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface ICallOp {
|
|
172
|
+
readonly op: "call";
|
|
173
|
+
readonly id: string;
|
|
174
|
+
readonly method: string;
|
|
175
|
+
readonly args: readonly FixtureArg[];
|
|
176
|
+
/** The id the return value is bound to. Absent means the value is discarded. */
|
|
177
|
+
readonly result?: string;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export interface ISetOp {
|
|
181
|
+
readonly op: "set";
|
|
182
|
+
readonly id: string;
|
|
183
|
+
readonly path: string;
|
|
184
|
+
readonly value: FixtureArg;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Loads a repository glTF/GLB (a path from the repository root) and binds `id` to its default
|
|
189
|
+
* scene: three's GLTFLoader in the references, the native loader in the native driver.
|
|
190
|
+
*/
|
|
191
|
+
export interface IGltfOp {
|
|
192
|
+
readonly op: "gltf";
|
|
193
|
+
readonly id: string;
|
|
194
|
+
readonly file: string;
|
|
195
|
+
/** Ids bound to the file's animations, in order (`gltf.animations[i]`). */
|
|
196
|
+
readonly clips?: readonly string[];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Applies a named TSL program to a bound material (and the GPU work it needs before the frame):
|
|
201
|
+
* `render/tsl-programs.js` in the reference, the C++ registry `tsl_programs.h` in the native driver,
|
|
202
|
+
* authored once each with upstream TSL and the native TSL builder. Render fixtures only.
|
|
203
|
+
*/
|
|
204
|
+
export interface ITslOp {
|
|
205
|
+
readonly op: "tsl";
|
|
206
|
+
readonly id: string;
|
|
207
|
+
readonly program: string;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export type FixtureOp = INewOp | ICallOp | ISetOp | IGltfOp | ITslOp;
|
|
211
|
+
|
|
212
|
+
/** A TSL program name: lower-case words joined by hyphens. */
|
|
213
|
+
export const TSL_PROGRAM = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/u;
|
|
214
|
+
|
|
215
|
+
/** A repository-relative glTF path: no parent steps, no absolute root. */
|
|
216
|
+
export const GLTF_FILE = /^(?!\/)(?!.*(?:^|\/)\.\.(?:\/|$))[\w./-]+\.(?:glb|gltf)$/u;
|
|
217
|
+
|
|
218
|
+
/** Reads `path` or calls `method`, never both: one observation, one source. */
|
|
219
|
+
export interface IObservation {
|
|
220
|
+
readonly id: string;
|
|
221
|
+
readonly kind: ObservationKind;
|
|
222
|
+
readonly path?: string;
|
|
223
|
+
readonly method?: string;
|
|
224
|
+
/** Required by `pixels`, forbidden on every other kind: the bounds the differential applies. */
|
|
225
|
+
readonly metric?: IPixelsMetric;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export interface IFixture {
|
|
229
|
+
readonly name: string;
|
|
230
|
+
readonly adaptedFrom: string;
|
|
231
|
+
readonly tolerance: IFixtureTolerance;
|
|
232
|
+
readonly ops: readonly FixtureOp[];
|
|
233
|
+
readonly observe: readonly IObservation[];
|
|
234
|
+
/** Present when this fixture renders a frame. Its scene and camera must name bound ids. */
|
|
235
|
+
readonly render?: IFixtureRender;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** One recorded observation. `value` is exactly what a driver prints on the wire. */
|
|
239
|
+
export interface IGoldenObservation {
|
|
240
|
+
readonly index: number;
|
|
241
|
+
readonly id: string;
|
|
242
|
+
readonly kind: ObservationKind;
|
|
243
|
+
readonly path?: string;
|
|
244
|
+
readonly method?: string;
|
|
245
|
+
readonly value: string;
|
|
246
|
+
/** Human-readable form of `value`. Never compared. */
|
|
247
|
+
readonly decimal?: string;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export interface IFixtureGolden {
|
|
251
|
+
readonly name: string;
|
|
252
|
+
readonly threeVersion: string;
|
|
253
|
+
readonly adaptedFrom: string;
|
|
254
|
+
/** Non-null when the reference could not execute this fixture; `observations` is then empty. */
|
|
255
|
+
readonly blocked: string | null;
|
|
256
|
+
readonly observations: readonly IGoldenObservation[];
|
|
257
|
+
/** Present exactly when an observation is `pixels`: where the frame is and who drew it. */
|
|
258
|
+
readonly render?: IRenderGolden;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Protocol tokens share one alphabet: no spaces, no separators, no escaping. */
|
|
262
|
+
const TOKEN = /^[A-Za-z_$][\w$]*$/u;
|
|
263
|
+
/** A fixture name may also carry hyphens, because the corpus is named like a test file. */
|
|
264
|
+
const NAME = /^[A-Za-z_$][\w$-]*$/u;
|
|
265
|
+
const PATH_SEGMENT = /^(?:[A-Za-z_$][\w$]*|\d+)$/u;
|
|
266
|
+
const HEX_BITS = /^[0-9a-f]{16}$/u;
|
|
267
|
+
const SHA256 = /^[0-9a-f]{64}$/u;
|
|
268
|
+
|
|
269
|
+
function record(value: unknown): Record<string, unknown> {
|
|
270
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
271
|
+
? (value as Record<string, unknown>)
|
|
272
|
+
: {};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function isPath(value: string): boolean {
|
|
276
|
+
return value.split(".").every((segment) => PATH_SEGMENT.test(segment));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function nonNegative(value: unknown): value is number {
|
|
280
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const BUFFER = new DataView(new ArrayBuffer(8));
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* The binary64 bits of `value` as 16 hex digits: the only lossless form a golden can hold.
|
|
287
|
+
*
|
|
288
|
+
* A NaN is recorded as the canonical quiet NaN. Hardware raises several NaN bit patterns for the
|
|
289
|
+
* same invalid operation — `0 * Infinity` alone yields both signs on x86 — so the payload is not
|
|
290
|
+
* part of the claim, and a golden carrying one would fail `--repeat` for reasons that have nothing
|
|
291
|
+
* to do with compatibility. `-0` keeps its sign, which is the sign that carries meaning.
|
|
292
|
+
*/
|
|
293
|
+
export function numberBits(value: number): string {
|
|
294
|
+
if (Number.isNaN(value)) return "7ff8000000000000";
|
|
295
|
+
BUFFER.setFloat64(0, value, false);
|
|
296
|
+
return BUFFER.getBigUint64(0, false).toString(16).padStart(16, "0");
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** The inverse of {@link numberBits}. Throws on anything that is not 16 hex digits. */
|
|
300
|
+
export function bitsNumber(bits: string): number {
|
|
301
|
+
if (!HEX_BITS.test(bits))
|
|
302
|
+
throw new Error(`TN_FIXTURE_BITS_INVALID: ${bits} is not 16 lowercase hex digits`);
|
|
303
|
+
BUFFER.setBigUint64(0, BigInt(`0x${bits}`), false);
|
|
304
|
+
return BUFFER.getFloat64(0, false);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** A decimal form a human can read. `-0` needs `Object.is`, and `String` already says NaN. */
|
|
308
|
+
export function decimalOf(value: number): string {
|
|
309
|
+
return Object.is(value, -0) ? "-0" : String(value);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Key-sorted JSON, so two drivers that build the same object compare equal regardless of order. */
|
|
313
|
+
export function canonicalJson(value: unknown): string {
|
|
314
|
+
return JSON.stringify(value, (_key, item: unknown) => {
|
|
315
|
+
if (item === null || typeof item !== "object" || Array.isArray(item)) return item;
|
|
316
|
+
const entries = Object.entries(item as Record<string, unknown>);
|
|
317
|
+
return Object.fromEntries(
|
|
318
|
+
entries.sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)),
|
|
319
|
+
);
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Every rule the format states, as messages. Fails closed: an empty array is the only pass, so a
|
|
325
|
+
* caller cannot forget to read the result.
|
|
326
|
+
*/
|
|
327
|
+
export function fixtureErrors(value: unknown, expectedName?: string): readonly string[] {
|
|
328
|
+
const errors: string[] = [];
|
|
329
|
+
const root = record(value);
|
|
330
|
+
const name = root.name;
|
|
331
|
+
if (typeof name !== "string" || !NAME.test(name))
|
|
332
|
+
errors.push("$.name: required, and must match /^[A-Za-z_$][\\w$-]*$/");
|
|
333
|
+
if (expectedName !== undefined && name !== expectedName)
|
|
334
|
+
errors.push(`$.name: ${JSON.stringify(name)} does not match its file name ${expectedName}`);
|
|
335
|
+
if (typeof root.adaptedFrom !== "string" || root.adaptedFrom.trim() === "")
|
|
336
|
+
errors.push("$.adaptedFrom: required, and must name the upstream test or `original`");
|
|
337
|
+
|
|
338
|
+
// A render block names a frame and the settings it is captured under. Anything less than the
|
|
339
|
+
// whole block is a capture nobody can reproduce, so each field is required rather than assumed.
|
|
340
|
+
let render: Record<string, unknown> | null = null;
|
|
341
|
+
if (root.render !== undefined) {
|
|
342
|
+
render = record(root.render);
|
|
343
|
+
if (root.render === null || typeof root.render !== "object" || Array.isArray(root.render))
|
|
344
|
+
errors.push("$.render: must be an object naming the scene, camera, size and tone mapping");
|
|
345
|
+
else {
|
|
346
|
+
if (typeof render.scene !== "string" || !TOKEN.test(render.scene))
|
|
347
|
+
errors.push("$.render.scene: required, and must match /^[A-Za-z_$][\\w$]*$/");
|
|
348
|
+
if (typeof render.camera !== "string" || !TOKEN.test(render.camera))
|
|
349
|
+
errors.push("$.render.camera: required, and must match /^[A-Za-z_$][\\w$]*$/");
|
|
350
|
+
for (const key of ["width", "height"] as const) {
|
|
351
|
+
if (!Number.isInteger(render[key]) || (render[key] as number) < 1)
|
|
352
|
+
errors.push(`$.render.${key}: required, and must be a positive integer`);
|
|
353
|
+
}
|
|
354
|
+
if (!TONE_MAPPINGS.includes(render.toneMapping as ToneMappingName))
|
|
355
|
+
errors.push(`$.render.toneMapping: must be one of ${TONE_MAPPINGS.join(", ")}`);
|
|
356
|
+
if (
|
|
357
|
+
render.toneMappingExposure !== undefined &&
|
|
358
|
+
!(
|
|
359
|
+
typeof render.toneMappingExposure === "number" &&
|
|
360
|
+
Number.isFinite(render.toneMappingExposure) &&
|
|
361
|
+
render.toneMappingExposure > 0
|
|
362
|
+
)
|
|
363
|
+
)
|
|
364
|
+
errors.push("$.render.toneMappingExposure: must be a positive finite number when present");
|
|
365
|
+
if (
|
|
366
|
+
render.outputColorSpace !== undefined &&
|
|
367
|
+
!OUTPUT_COLOR_SPACES.includes(render.outputColorSpace as OutputColorSpace)
|
|
368
|
+
)
|
|
369
|
+
errors.push(`$.render.outputColorSpace: must be one of ${OUTPUT_COLOR_SPACES.join(", ")}`);
|
|
370
|
+
if (render.shadowMap !== undefined && typeof render.shadowMap !== "boolean")
|
|
371
|
+
errors.push("$.render.shadowMap: must be a boolean when present");
|
|
372
|
+
if (
|
|
373
|
+
render.shadowMapType !== undefined &&
|
|
374
|
+
(render.shadowMap !== true ||
|
|
375
|
+
!["PCFShadowMap", "PCFSoftShadowMap"].includes(render.shadowMapType as string))
|
|
376
|
+
)
|
|
377
|
+
errors.push(
|
|
378
|
+
"$.render.shadowMapType: PCFShadowMap or PCFSoftShadowMap, and only with shadowMap true",
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const tolerance = record(root.tolerance);
|
|
384
|
+
if (root.tolerance === undefined) errors.push("$.tolerance: required");
|
|
385
|
+
else if (tolerance.abs === undefined && tolerance.ulps === undefined)
|
|
386
|
+
errors.push("$.tolerance: needs at least one of abs or ulps");
|
|
387
|
+
for (const key of ["abs", "ulps"] as const) {
|
|
388
|
+
if (tolerance[key] !== undefined && !nonNegative(tolerance[key]))
|
|
389
|
+
errors.push(`$.tolerance.${key}: must be a non-negative finite number`);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (!Array.isArray(root.ops) || root.ops.length === 0)
|
|
393
|
+
errors.push("$.ops: required, and a fixture with no operations has nothing to observe");
|
|
394
|
+
if (!Array.isArray(root.observe) || root.observe.length === 0)
|
|
395
|
+
errors.push("$.observe: required, and a fixture with no observations asserts nothing");
|
|
396
|
+
|
|
397
|
+
// Ids are resolved as operations are read, so a reference can only name something that exists.
|
|
398
|
+
const defined = new Set<string>();
|
|
399
|
+
const bind = (id: unknown, path: string): void => {
|
|
400
|
+
if (typeof id !== "string" || !TOKEN.test(id)) {
|
|
401
|
+
errors.push(`${path}: required, and must match /^[A-Za-z_$][\\w$]*$/`);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
if (defined.has(id)) {
|
|
405
|
+
errors.push(`${path}: id ${id} is already bound`);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
defined.add(id);
|
|
409
|
+
};
|
|
410
|
+
const argument = (arg: unknown, path: string): void => {
|
|
411
|
+
if (arg === null || typeof arg === "boolean" || typeof arg === "string") return;
|
|
412
|
+
if (typeof arg === "number") {
|
|
413
|
+
if (!Number.isFinite(arg))
|
|
414
|
+
errors.push(`${path}: write ${String(arg)} as { "num": "${String(arg)}" }`);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
const node = record(arg);
|
|
418
|
+
const keys = Object.keys(node);
|
|
419
|
+
if (keys.length === 1 && "ref" in node) {
|
|
420
|
+
if (typeof node.ref !== "string" || !TOKEN.test(node.ref))
|
|
421
|
+
errors.push(`${path}.ref: required, and must match /^[A-Za-z_$][\\w$]*$/`);
|
|
422
|
+
else if (!defined.has(node.ref)) errors.push(`${path}.ref: ${node.ref} is not a bound id`);
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
if (keys.length === 1 && "num" in node) {
|
|
426
|
+
if (!FIXTURE_NUMBER_NAMES.includes(node.num as FixtureNumberName))
|
|
427
|
+
errors.push(`${path}.num: must be one of ${FIXTURE_NUMBER_NAMES.join(", ")}`);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (keys.length === 2 && "array" in node && "type" in node) {
|
|
431
|
+
if (!FIXTURE_ARRAY_TYPES.includes(node.type as FixtureArrayType))
|
|
432
|
+
errors.push(`${path}.type: must be one of ${FIXTURE_ARRAY_TYPES.join(", ")}`);
|
|
433
|
+
if (
|
|
434
|
+
!Array.isArray(node.array) ||
|
|
435
|
+
!node.array.every((value) => typeof value === "number" && Number.isFinite(value))
|
|
436
|
+
)
|
|
437
|
+
errors.push(`${path}.array: must be an array of finite numbers`);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (keys.length === 1 && "refs" in node) {
|
|
441
|
+
if (!Array.isArray(node.refs)) {
|
|
442
|
+
errors.push(`${path}.refs: must be an array of bound ids`);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
for (const [index, id] of node.refs.entries()) {
|
|
446
|
+
if (typeof id !== "string" || !TOKEN.test(id) || !defined.has(id))
|
|
447
|
+
errors.push(`${path}.refs[${index}]: ${String(id)} is not a bound id`);
|
|
448
|
+
}
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
if (keys.length === 1 && "record" in node) {
|
|
452
|
+
const fields = record(node.record);
|
|
453
|
+
if (node.record === null || typeof node.record !== "object" || Array.isArray(node.record))
|
|
454
|
+
errors.push(`${path}.record: must be an object of scalars`);
|
|
455
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
456
|
+
if (!TOKEN.test(key))
|
|
457
|
+
errors.push(`${path}.record.${key}: keys must match /^[A-Za-z_$][\\w$]*$/`);
|
|
458
|
+
const scalar =
|
|
459
|
+
value === null ||
|
|
460
|
+
["boolean", "string"].includes(typeof value) ||
|
|
461
|
+
(typeof value === "number" && Number.isFinite(value));
|
|
462
|
+
if (!scalar)
|
|
463
|
+
errors.push(`${path}.record.${key}: must be a finite number, string, boolean or null`);
|
|
464
|
+
}
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
errors.push(
|
|
468
|
+
`${path}: must be a number, string, boolean, null, { "ref": id }, { "num": name }, { "array": [...], "type": name }, { "refs": [...] } or { "record": {...} }`,
|
|
469
|
+
);
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
for (const [index, op] of (Array.isArray(root.ops) ? root.ops : []).entries()) {
|
|
473
|
+
const at = `$.ops[${index}]`;
|
|
474
|
+
const node = record(op);
|
|
475
|
+
const args = node.args === undefined ? [] : node.args;
|
|
476
|
+
if (node.op === "gltf") {
|
|
477
|
+
if (typeof node.file !== "string" || !GLTF_FILE.test(node.file))
|
|
478
|
+
errors.push(`${at}.file: required, a repository-relative .glb or .gltf path`);
|
|
479
|
+
bind(node.id, `${at}.id`);
|
|
480
|
+
if (node.clips !== undefined) {
|
|
481
|
+
if (!Array.isArray(node.clips)) errors.push(`${at}.clips: must be an array of ids`);
|
|
482
|
+
else node.clips.forEach((clip, i) => bind(clip, `${at}.clips[${i}]`));
|
|
483
|
+
}
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
if (node.op === "tsl") {
|
|
487
|
+
if (typeof node.program !== "string" || !TSL_PROGRAM.test(node.program))
|
|
488
|
+
errors.push(`${at}.program: required, a TSL program name`);
|
|
489
|
+
if (typeof node.id !== "string" || !defined.has(node.id))
|
|
490
|
+
errors.push(`${at}.id: must name an id bound earlier`);
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
if (node.op !== "new" && node.op !== "call" && node.op !== "set") {
|
|
494
|
+
errors.push(`${at}.op: must be new, call, set, gltf or tsl`);
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
if (!Array.isArray(args)) errors.push(`${at}.args: must be an array`);
|
|
498
|
+
else args.forEach((arg, position) => argument(arg, `${at}.args[${position}]`));
|
|
499
|
+
|
|
500
|
+
if (node.op === "new") {
|
|
501
|
+
if (typeof node.class !== "string" || !TOKEN.test(node.class))
|
|
502
|
+
errors.push(`${at}.class: required, and must match /^[A-Za-z_$][\\w$]*$/`);
|
|
503
|
+
bind(node.id, `${at}.id`);
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
if (typeof node.id !== "string" || !defined.has(node.id)) {
|
|
507
|
+
errors.push(`${at}.id: ${JSON.stringify(node.id)} is not a bound id`);
|
|
508
|
+
}
|
|
509
|
+
if (node.op === "call" && (typeof node.method !== "string" || !TOKEN.test(node.method)))
|
|
510
|
+
errors.push(`${at}.method: required, and must match /^[A-Za-z_$][\\w$]*$/`);
|
|
511
|
+
if (node.op === "call" && node.result !== undefined) bind(node.result, `${at}.result`);
|
|
512
|
+
if (node.op === "set") {
|
|
513
|
+
if (typeof node.path !== "string" || !isPath(node.path))
|
|
514
|
+
errors.push(`${at}.path: required, and must be dotted identifiers or indices`);
|
|
515
|
+
argument(node.value, `${at}.value`);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
let pixels = 0;
|
|
520
|
+
for (const [index, observation] of (Array.isArray(root.observe) ? root.observe : []).entries()) {
|
|
521
|
+
const at = `$.observe[${index}]`;
|
|
522
|
+
const node = record(observation);
|
|
523
|
+
if (typeof node.id !== "string" || !defined.has(node.id))
|
|
524
|
+
errors.push(`${at}.id: ${JSON.stringify(node.id)} is not a bound id`);
|
|
525
|
+
if (!OBSERVATION_KINDS.includes(node.kind as ObservationKind))
|
|
526
|
+
errors.push(`${at}.kind: must be one of ${OBSERVATION_KINDS.join(", ")}`);
|
|
527
|
+
const hasPath = node.path !== undefined;
|
|
528
|
+
const hasMethod = node.method !== undefined;
|
|
529
|
+
// Neither is legal: an id bound to a returned value (a `toArray()` array) is read directly.
|
|
530
|
+
if (hasPath && hasMethod) errors.push(`${at}: path and method are mutually exclusive`);
|
|
531
|
+
if (hasPath && node.path !== undefined && (typeof node.path !== "string" || !isPath(node.path)))
|
|
532
|
+
errors.push(`${at}.path: required, and must be dotted identifiers or indices`);
|
|
533
|
+
if (
|
|
534
|
+
hasMethod &&
|
|
535
|
+
node.method !== undefined &&
|
|
536
|
+
(typeof node.method !== "string" || !TOKEN.test(node.method))
|
|
537
|
+
)
|
|
538
|
+
errors.push(`${at}.method: required, and must match /^[A-Za-z_$][\\w$]*$/`);
|
|
539
|
+
|
|
540
|
+
// `pixels` reads the whole captured frame, not a property of one object: it names the render's
|
|
541
|
+
// own scene, carries no path, and states the bounds the differential applies to the frame.
|
|
542
|
+
if (node.kind === "pixels") {
|
|
543
|
+
pixels += 1;
|
|
544
|
+
if (render === null) errors.push(`${at}: a pixels observation needs a $.render block`);
|
|
545
|
+
else if (node.id !== render.scene)
|
|
546
|
+
errors.push(
|
|
547
|
+
`${at}.id: a pixels observation names the rendered scene ${JSON.stringify(render.scene)}`,
|
|
548
|
+
);
|
|
549
|
+
if (hasPath || hasMethod)
|
|
550
|
+
errors.push(`${at}: pixels is the whole frame, so path and method do not apply`);
|
|
551
|
+
const metric = record(node.metric);
|
|
552
|
+
if (node.metric === undefined) errors.push(`${at}.metric: required for pixels`);
|
|
553
|
+
for (const key of ["maxPixelMismatchRatio", "maxPerceptualDeltaE"] as const) {
|
|
554
|
+
if (!nonNegative(metric[key]) || (metric[key] as number) > 1)
|
|
555
|
+
errors.push(`${at}.metric.${key}: must be a ratio between 0 and 1`);
|
|
556
|
+
}
|
|
557
|
+
if (metric.levels !== undefined) {
|
|
558
|
+
if (
|
|
559
|
+
!Number.isInteger(metric.levels) ||
|
|
560
|
+
(metric.levels as number) < 0 ||
|
|
561
|
+
(metric.levels as number) > 4
|
|
562
|
+
)
|
|
563
|
+
errors.push(`${at}.metric.levels: must be an integer between 0 and 4`);
|
|
564
|
+
else if (
|
|
565
|
+
(metric.levels as number) > 0 &&
|
|
566
|
+
(typeof metric.levelsReason !== "string" || metric.levelsReason.trim() === "")
|
|
567
|
+
)
|
|
568
|
+
errors.push(`${at}.metric.levelsReason: required when levels is above 0`);
|
|
569
|
+
}
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
if (node.metric !== undefined)
|
|
573
|
+
errors.push(`${at}.metric: only a pixels observation carries a metric`);
|
|
574
|
+
}
|
|
575
|
+
// A captured frame nothing observes is a frame nobody compared.
|
|
576
|
+
if (render !== null && pixels === 0)
|
|
577
|
+
errors.push("$.render: a render block needs a pixels observation naming its scene");
|
|
578
|
+
return errors;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/** Parses a fixture or throws. One error message names every disagreement. */
|
|
582
|
+
export function parseFixture(value: unknown, expectedName?: string): IFixture {
|
|
583
|
+
const errors = fixtureErrors(value, expectedName);
|
|
584
|
+
if (errors.length > 0)
|
|
585
|
+
throw new Error(`TN_FIXTURE_INVALID:\n${errors.map((line) => ` ${line}`).join("\n")}`);
|
|
586
|
+
return value as IFixture;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/** The reasons a golden cannot be trusted. A blocked golden is the only one with no observations. */
|
|
590
|
+
export function goldenErrors(value: unknown, expectedName?: string): readonly string[] {
|
|
591
|
+
const errors: string[] = [];
|
|
592
|
+
const root = record(value);
|
|
593
|
+
if (typeof root.name !== "string" || !NAME.test(root.name)) errors.push("$.name: required");
|
|
594
|
+
else if (expectedName !== undefined && root.name !== expectedName)
|
|
595
|
+
errors.push(`$.name: ${root.name} does not match its file name ${expectedName}`);
|
|
596
|
+
if (typeof root.threeVersion !== "string" || root.threeVersion === "")
|
|
597
|
+
errors.push("$.threeVersion: required, and must record the reference version");
|
|
598
|
+
if (root.blocked !== null && typeof root.blocked !== "string")
|
|
599
|
+
errors.push("$.blocked: must be null or a reason");
|
|
600
|
+
const observations = root.observations;
|
|
601
|
+
let pixels = false;
|
|
602
|
+
if (!Array.isArray(observations)) errors.push("$.observations: must be an array");
|
|
603
|
+
else {
|
|
604
|
+
if (root.blocked === null && observations.length === 0)
|
|
605
|
+
errors.push("$.observations: a golden that ran asserts nothing");
|
|
606
|
+
observations.forEach((entry, index) => {
|
|
607
|
+
const node = record(entry);
|
|
608
|
+
if (!Number.isInteger(node.index) || node.index !== index)
|
|
609
|
+
errors.push(`$.observations[${index}].index: must equal ${index}`);
|
|
610
|
+
if (!OBSERVATION_KINDS.includes(node.kind as ObservationKind))
|
|
611
|
+
errors.push(
|
|
612
|
+
`$.observations[${index}].kind: must be one of ${OBSERVATION_KINDS.join(", ")}`,
|
|
613
|
+
);
|
|
614
|
+
if (typeof node.id !== "string" || !TOKEN.test(node.id))
|
|
615
|
+
errors.push(`$.observations[${index}].id: required`);
|
|
616
|
+
if (typeof node.value !== "string" || node.value === "")
|
|
617
|
+
errors.push(`$.observations[${index}].value: required`);
|
|
618
|
+
if (node.kind === "pixels") pixels = true;
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
// A pixels observation is a claim about a file on disk: without the file's hash and the adapter
|
|
622
|
+
// that drew it, the claim is a number nobody can check.
|
|
623
|
+
const render = record(root.render);
|
|
624
|
+
if (pixels) {
|
|
625
|
+
if (root.render === undefined) errors.push("$.render: required when an observation is pixels");
|
|
626
|
+
else {
|
|
627
|
+
if (typeof render.png !== "string" || render.png === "")
|
|
628
|
+
errors.push("$.render.png: required, and must name the capture file");
|
|
629
|
+
if (typeof render.pngSha256 !== "string" || !SHA256.test(render.pngSha256))
|
|
630
|
+
errors.push("$.render.pngSha256: required, and must be 64 lowercase hex digits");
|
|
631
|
+
for (const key of ["width", "height"] as const)
|
|
632
|
+
if (!Number.isInteger(render[key]) || (render[key] as number) < 1)
|
|
633
|
+
errors.push(`$.render.${key}: required, and must be a positive integer`);
|
|
634
|
+
if (record(render.adapter).vendor === undefined)
|
|
635
|
+
errors.push("$.render.adapter: required, and must record what navigator.gpu reported");
|
|
636
|
+
}
|
|
637
|
+
} else if (root.render !== undefined)
|
|
638
|
+
errors.push("$.render: only a golden with a pixels observation carries a render block");
|
|
639
|
+
return errors;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
export function parseGolden(value: unknown, expectedName?: string): IFixtureGolden {
|
|
643
|
+
const errors = goldenErrors(value, expectedName);
|
|
644
|
+
if (errors.length > 0)
|
|
645
|
+
throw new Error(`TN_GOLDEN_INVALID:\n${errors.map((line) => ` ${line}`).join("\n")}`);
|
|
646
|
+
return value as IFixtureGolden;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* The reference version goldens are keyed by: the workspace catalog pin.
|
|
651
|
+
*
|
|
652
|
+
* `three` is not hoisted, so each package that declares `three: catalog:` owns its own link to the
|
|
653
|
+
* same store copy. This reads the pin the way `run-conformance.mjs` does, so a golden can never be
|
|
654
|
+
* recorded against a version the workspace does not name.
|
|
655
|
+
*/
|
|
656
|
+
export function pinnedThreeVersion(root: string = REPO_ROOT): string {
|
|
657
|
+
const workspace = readFileSync(path.join(root, "pnpm-workspace.yaml"), "utf8");
|
|
658
|
+
const version = workspace.match(/^\s*three:\s*['"]?([^\s'"]+)['"]?\s*$/mu)?.[1];
|
|
659
|
+
if (version === undefined)
|
|
660
|
+
throw new Error(
|
|
661
|
+
`TN_FIXTURE_THREE_UNPINNED: ${path.join(root, "pnpm-workspace.yaml")} has no three pin`,
|
|
662
|
+
);
|
|
663
|
+
return version;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
export function goldenPath(name: string, version: string): string {
|
|
667
|
+
return path.join(GOLDENS_DIR, version, `${name}.json`);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
export function readGolden(name: string, version: string): IFixtureGolden | null {
|
|
671
|
+
const file = goldenPath(name, version);
|
|
672
|
+
if (!existsSync(file)) return null;
|
|
673
|
+
return parseGolden(JSON.parse(readFileSync(file, "utf8")), name);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
export function writeGolden(golden: IFixtureGolden, version: string): string {
|
|
677
|
+
const file = goldenPath(golden.name, version);
|
|
678
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
679
|
+
writeFileSync(file, `${JSON.stringify(golden, null, 2)}\n`);
|
|
680
|
+
return file;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/** The captured frame, beside the JSON golden that names its hash. Never inside `artifacts/`. */
|
|
684
|
+
export function renderPngPath(name: string, version: string): string {
|
|
685
|
+
return path.join(GOLDENS_DIR, version, `${name}.png`);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
export function writeRenderPng(name: string, version: string, png: Uint8Array): string {
|
|
689
|
+
const file = renderPngPath(name, version);
|
|
690
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
691
|
+
writeFileSync(file, png);
|
|
692
|
+
return file;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* Every fixture in the corpus, sorted by name, each parsed against its own file name.
|
|
697
|
+
*
|
|
698
|
+
* The sort is on the bare name, not on the file name: `"a-b.json"` sorts before `"a.json"` because
|
|
699
|
+
* `-` is below `.`, and the conformance parent orders its rows by bare name too.
|
|
700
|
+
*/
|
|
701
|
+
export function loadFixtures(directory: string = FIXTURES_DIR): readonly IFixture[] {
|
|
702
|
+
const names = readdirSync(directory)
|
|
703
|
+
.filter((file) => file.endsWith(".json"))
|
|
704
|
+
.map((file) => path.basename(file, ".json"))
|
|
705
|
+
.sort();
|
|
706
|
+
if (names.length === 0) throw new Error(`TN_FIXTURE_EMPTY: ${directory} holds no fixtures`);
|
|
707
|
+
return names.map((name) =>
|
|
708
|
+
parseFixture(JSON.parse(readFileSync(path.join(directory, `${name}.json`), "utf8")), name),
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/** The distance between two doubles in representable steps. Used by a `ulps` tolerance. */
|
|
713
|
+
export function ulpDistance(left: number, right: number): number {
|
|
714
|
+
const ordered = (value: number): bigint => {
|
|
715
|
+
BUFFER.setFloat64(0, value, false);
|
|
716
|
+
const bits = BUFFER.getBigUint64(0, false);
|
|
717
|
+
return bits >= 0x8000000000000000n ? bits - 0x8000000000000000n : -(bits & 0x7fffffffffffffffn);
|
|
718
|
+
};
|
|
719
|
+
const distance = ordered(left) - ordered(right);
|
|
720
|
+
return Number(distance < 0n ? -distance : distance);
|
|
721
|
+
}
|