@threenative/three-native 0.3.12 → 0.3.13

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.
@@ -4,9 +4,11 @@
4
4
  * surface and every draw; this class turns three's calls into host calls and reports what the
5
5
  * engine did. What it does not implement throws its diagnostic rather than doing nothing.
6
6
  */
7
+ import { packedRef } from "./browser-abi-types.js";
7
8
  import { TSL_NODE, type TnAbiModule, engineRef } from "./browser-backend.js";
8
9
  import { type ITslRuntime, defineTsl, isTslNode } from "./browser-tsl.js";
9
10
  import { debugTelemetry } from "./debug-telemetry.js";
11
+ import { WEB_FRAME_FIELD } from "./generated/web-frame-fields.js";
10
12
  import {
11
13
  CPU_STAGE_ENTRIES,
12
14
  type CpuStageName,
@@ -18,9 +20,15 @@ import {
18
20
  } from "./gpu-stages.js";
19
21
  import type { PassDraw } from "./pass-node.js";
20
22
  import { frameAttachmentName } from "./pass-node.js";
21
- import { defineRenderTargets, targetTexelBytes } from "./render-target.js";
23
+ import {
24
+ type IPositionLike,
25
+ type IRegionLike,
26
+ defineRenderTargets,
27
+ targetTexelBytes,
28
+ textureCopyArguments,
29
+ } from "./render-target.js";
22
30
  import { ShadowMap } from "./shadow-map.js";
23
- import { syncVirtualShadowLods } from "./shadow-node.js";
31
+ import { reportVirtualShadowFrame, syncVirtualShadowLods } from "./shadow-node.js";
24
32
  import { defineSphereReflectionCapture } from "./sphere-reflection-capture.js";
25
33
  import { readStorageBytes } from "./storage.js";
26
34
  import type { INodeFrame } from "./temp-node.js";
@@ -50,6 +58,7 @@ type HostCall =
50
58
  | "_tnw_web_copy_texture"
51
59
  | "_tnw_web_read_mrt"
52
60
  | "_tnw_web_device_limit"
61
+ | "_tnw_web_device_feature"
53
62
  | "_tnw_web_threads";
54
63
 
55
64
  export type WebHostModule = Record<HostCall, (...args: number[]) => number> & {
@@ -66,7 +75,16 @@ export function isWebHostModule(module: TnAbiModule): module is WebEngineModule
66
75
  * The tnw_* contract this facade speaks (web_host.cpp `kWebHostAbi`). Bump both on every tnw_*
67
76
  * signature change: web-host-abi.spec.ts pins the signatures per number, and a mismatch fails init.
68
77
  */
69
- export const WEB_HOST_ABI = 11;
78
+ export const WEB_HOST_ABI = 13;
79
+
80
+ /**
81
+ * The device features three reads by name, as `tnw_web_device_feature`'s argument (web_host.cpp): every
82
+ * other name answers false, as a WebGPU GPUSupportedFeatures without it does.
83
+ */
84
+ const WEB_DEVICE_FEATURES: Readonly<Record<string, number>> = {
85
+ "core-features-and-limits": 0,
86
+ "indirect-first-instance": 1,
87
+ };
70
88
 
71
89
  /** The canvas key the host's surface looks up; Emscripten resolves it before any DOM query. */
72
90
  const CANVAS_TARGET = "!threenative-canvas";
@@ -82,7 +100,12 @@ const FAILED = 2;
82
100
  * `tnw_web_frame` field of the first named stage's segment (web_host.cpp): 19 + 4 shadow, scene, other and output
83
101
  * passes. `GPU_STAGES` counts on from there, one segment per stage.
84
102
  */
85
- const GPU_STAGE_FIELD = 23;
103
+ const GPU_STAGE_FIELD: number = WEB_FRAME_FIELD.gpuSegmentGather;
104
+
105
+ /** One number of the frame snapshot (`tnw_web_frame` 89), read in place at the block's index plus the field id. */
106
+ function frameField(heap: Float64Array, block: number, field: number): number {
107
+ return heap[block + field] ?? Number.NaN;
108
+ }
86
109
 
87
110
  interface IAdapterInfo {
88
111
  readonly vendor: string;
@@ -91,20 +114,6 @@ interface IAdapterInfo {
91
114
  readonly device: string;
92
115
  }
93
116
 
94
- /** three's Box2 and Box3 as a texture copy reads them: a region by its corners. */
95
- interface IRegionLike {
96
- readonly isBox3?: boolean;
97
- readonly min: { readonly x: number; readonly y: number; readonly z?: number };
98
- readonly max: { readonly x: number; readonly y: number; readonly z?: number };
99
- }
100
-
101
- /** A Vector2 or Vector3: the destination origin of a texture copy, z the array layer. */
102
- interface IPositionLike {
103
- readonly x: number;
104
- readonly y: number;
105
- readonly z?: number;
106
- }
107
-
108
117
  interface IColorLike {
109
118
  readonly r: number;
110
119
  readonly g: number;
@@ -147,7 +156,8 @@ function markBaked(environment: IBakedEnvironment): void {
147
156
  export function defineWebRenderer(
148
157
  module: WebEngineModule,
149
158
  Color: new (...args: unknown[]) => IColorLike,
150
- beforeRender: (frame?: INodeFrame) => void = () => {},
159
+ /** The frame hook: a draw passes its frame; a dispatch passes none, with its kernels' engine node ids. */
160
+ beforeRender: (frame?: INodeFrame, kernels?: readonly number[]) => void = () => {},
151
161
  tsl?: ITslRuntime,
152
162
  /** Draws the offscreen PassNodes into their targets (pass-node.ts); given the drawing buffer and the target draw. */
153
163
  drawSecondaryPasses: (
@@ -178,17 +188,11 @@ export function defineWebRenderer(
178
188
  const ref = typeof object === "object" && object !== null ? engineRef(object) : undefined;
179
189
  if (ref === undefined)
180
190
  throw new TypeError(`TN_WASM_RENDER: the ${what} is not an engine object.`);
181
- const [type, context, slot, generation] = ref.key.split(":").map(Number) as [
182
- number,
183
- number,
184
- number,
185
- number,
186
- ];
191
+ const { w0, w1, w2 } = packedRef(ref);
187
192
  const view = new DataView(module.HEAPU8.buffer);
188
- view.setUint16(handles + offset, type, true);
189
- view.setUint16(handles + offset + 2, context, true);
190
- view.setUint32(handles + offset + 4, slot, true);
191
- view.setUint32(handles + offset + 8, generation, true);
193
+ view.setUint32(handles + offset, w0, true);
194
+ view.setUint32(handles + offset + 4, w1, true);
195
+ view.setUint32(handles + offset + 8, w2, true);
192
196
  };
193
197
 
194
198
  const Renderer = class WebGPURenderer {
@@ -197,7 +201,7 @@ export function defineWebRenderer(
197
201
  readonly supportsRetainedTargetSubrect = false;
198
202
  renderScale = 1;
199
203
  get temporalUpscale(): boolean {
200
- return module._tnw_web_frame(34) === 1;
204
+ return module._tnw_web_frame(WEB_FRAME_FIELD.temporalUpscale) === 1;
201
205
  }
202
206
  #stageMeters = true;
203
207
  /** Address of the host's stage-total array (`tnw_web_frame` 78), 0 until `init` asks. */
@@ -208,7 +212,7 @@ export function defineWebRenderer(
208
212
  */
209
213
  setStageMeters(enabled: boolean): void {
210
214
  this.#stageMeters = enabled;
211
- module._tnw_web_frame(enabled ? -3 : -4);
215
+ module._tnw_web_frame(enabled ? WEB_FRAME_FIELD.cpuMetersOn : WEB_FRAME_FIELD.cpuMetersOff);
212
216
  }
213
217
  setRenderScale(scale: number): void {
214
218
  if (!Number.isFinite(scale) || scale < 0.25 || scale > 1)
@@ -275,6 +279,10 @@ export function defineWebRenderer(
275
279
  // The GPU milliseconds of the sample `render.timestamp` reports, by named stage (gpu-stages.ts). A stage that
276
280
  // drew no pass is absent; core's `stageGpuReading` hands it to the GI and shadow budgets.
277
281
  gpuStages: {} as Record<string, number>,
282
+ // The same sample split by pass (frame_gpu_timer.h segments): the scene pass, every shadow map, and the rest
283
+ // (other passes, output, gather). `frame` is the sample's source frame, -1 until one resolves; core's
284
+ // `gpuFrameSample` hands the split to the frame budget's `gpuMain`, `gpuShadow` and `gpuOther`.
285
+ gpuSplit: { frame: -1, main: 0, shadow: 0, other: 0 },
278
286
  // The last frame's shadow passes, the spot and point light maps apart from the sun's (PRD-650):
279
287
  // lamp maps redrawn and their draws, every shadow draw (the sun's are the rest), and the GPU
280
288
  // milliseconds of each part in the sample `render.timestamp` reports, -1 until one resolves.
@@ -352,6 +360,11 @@ export function defineWebRenderer(
352
360
  trackTimestamp: boolean;
353
361
  getTimestampFrames(type: string): number[];
354
362
  pipelineCompiles(): number;
363
+ /**
364
+ * three's GPUSupportedFeatures.has, which core reads to gate its GPU world scene
365
+ * (world-gpu-scene.ts `gpuSceneUnsupported`): the device's own answer, asked of the host.
366
+ */
367
+ hasFeature(name: string): boolean;
355
368
  /** Resolves as three's GPUDevice.lost does once the host loses its device; core reports it (TN_DEVICE_LOST). */
356
369
  readonly device: {
357
370
  readonly lost: Promise<{ reason: string; message: string }>;
@@ -418,7 +431,11 @@ export function defineWebRenderer(
418
431
  },
419
432
  trackTimestamp: Boolean(parameters.trackTimestamp ?? true),
420
433
  getTimestampFrames: timestampFrames,
421
- pipelineCompiles: () => module._tnw_web_frame(2),
434
+ pipelineCompiles: () => module._tnw_web_frame(WEB_FRAME_FIELD.pipelineCompiles),
435
+ hasFeature(name: string): boolean {
436
+ const feature = WEB_DEVICE_FEATURES[name];
437
+ return feature !== undefined && module._tnw_web_device_feature(feature) === 1;
438
+ },
422
439
  device: {
423
440
  lost: new Promise((resolve) => {
424
441
  this.#deviceLost = resolve;
@@ -476,8 +493,9 @@ export function defineWebRenderer(
476
493
  };
477
494
  // Honest, like the engine: a device without timestamp-query times no frame, so no caller
478
495
  // may be told timestamps are enabled. An explicit opt-out stays off.
479
- if (module._tnw_web_frame(10) === 0) this.backend.trackTimestamp = false;
480
- this.#stageBase = module._tnw_web_frame(78);
496
+ if (module._tnw_web_frame(WEB_FRAME_FIELD.gpuTimerAvailable) === 0)
497
+ this.backend.trackTimestamp = false;
498
+ this.#stageBase = module._tnw_web_frame(WEB_FRAME_FIELD.stageTotalsAddress);
481
499
  if (this.#stageBase > 0) {
482
500
  this.info.engine.cpuMs = Object.fromEntries(
483
501
  CPU_STAGE_ENTRIES.map(([name]) => [name, 0]),
@@ -581,6 +599,20 @@ export function defineWebRenderer(
581
599
  }
582
600
  }
583
601
 
602
+ /**
603
+ * three's `hasFeature`, which KTX2Loader.detectSupport reads (PRD-867): the block formats the engine
604
+ * uploads on this device. BC7 answers from the device; ETC2 answers false until the engine uploads
605
+ * three's opaque `RGB_ETC2_Format`, which KTX2Loader picks there, so a KTX2 falls back to RGBA instead.
606
+ */
607
+ hasFeature(name: string): boolean {
608
+ if (this.#adapter === undefined)
609
+ throw new Error("TN_WASM_RENDERER: hasFeature() before init() finished.");
610
+ return (
611
+ name === "texture-compression-bc" &&
612
+ module._tnw_web_frame(WEB_FRAME_FIELD.textureCompressionBc) === 1
613
+ );
614
+ }
615
+
584
616
  render(scene: unknown, camera: unknown): void {
585
617
  if (this.#adapter === undefined)
586
618
  throw new Error("TN_WASM_RENDERER: render() before init() finished.");
@@ -593,7 +625,7 @@ export function defineWebRenderer(
593
625
  // texture already taken, so refusing it saves no frame and presents the world without it.
594
626
  // Measured on Lantern Vale: every cover over a drawn frame was refused, so the loading screen
595
627
  // flickered black for the whole environment bake and then showed the unfinished world.
596
- if (!this.#frameOpen && module._tnw_web_frame(11) === 1) {
628
+ if (!this.#frameOpen && module._tnw_web_frame(WEB_FRAME_FIELD.gpuBusy) === 1) {
597
629
  // A lost device leaves its frames on the GPU for good, so the gate declines every later frame
598
630
  // and no draw ever returns a status: name the loss here, where the frame poll just ran.
599
631
  if (this.#noticeLost()) return;
@@ -623,7 +655,11 @@ export function defineWebRenderer(
623
655
  const renderFrame = this.info.frame;
624
656
  if (debug !== undefined) {
625
657
  // Observation only: native captures this request with each submitted sample.
626
- module._tnw_web_frame(this.backend.trackTimestamp ? -1 : -2);
658
+ module._tnw_web_frame(
659
+ this.backend.trackTimestamp
660
+ ? WEB_FRAME_FIELD.debugTimestampOn
661
+ : WEB_FRAME_FIELD.debugTimestampOff,
662
+ );
627
663
  debug?.add(
628
664
  "gpuTimestampRequests",
629
665
  this.backend.trackTimestamp ? 1 : 0,
@@ -641,32 +677,42 @@ export function defineWebRenderer(
641
677
  ),
642
678
  );
643
679
  this.info.frame += 1;
644
- if (this.#bakingEnvironment !== undefined && module._tnw_web_frame(12) === 0) {
680
+ // PRD-878: one host call copies every number of the frame into a block, read in place below.
681
+ const block = module._tnw_web_frame(WEB_FRAME_FIELD.frameSnapshot) / 8;
682
+ const heap = module.HEAPF64; // after the call: a grown memory replaces the view
683
+ if (!this.#overlay)
684
+ reportVirtualShadowFrame(
685
+ frameField(heap, block, WEB_FRAME_FIELD.virtualShadowPendingLevels),
686
+ );
687
+ if (
688
+ this.#bakingEnvironment !== undefined &&
689
+ frameField(heap, block, WEB_FRAME_FIELD.environmentPending) === 0
690
+ ) {
645
691
  // An overlay draw has no environment and says nothing; another sky ends the watch.
646
692
  const drawnEnvironment = (scene as { environment?: unknown }).environment;
647
693
  if (drawnEnvironment === this.#bakingEnvironment) markBaked(this.#bakingEnvironment);
648
694
  if (drawnEnvironment != null) this.#bakingEnvironment = undefined;
649
695
  }
650
- this.info.render.frameCalls = module._tnw_web_frame(0);
696
+ this.info.render.frameCalls = frameField(heap, block, WEB_FRAME_FIELD.draws);
651
697
  this.info.render.calls += this.info.render.frameCalls;
652
698
  this.info.render.drawCalls += this.info.render.frameCalls;
653
- const lastTriangles = module._tnw_web_frame(1);
699
+ const lastTriangles = frameField(heap, block, WEB_FRAME_FIELD.triangles);
654
700
  this.info.render.triangles += lastTriangles;
655
701
  const shadows = this.info.engine.shadows;
656
- shadows.lampRedraws = module._tnw_web_frame(25);
657
- shadows.lampDraws = module._tnw_web_frame(26);
658
- shadows.shadowDraws = module._tnw_web_frame(27);
659
- shadows.lampMapMin = module._tnw_web_frame(28);
660
- shadows.lampMapMax = module._tnw_web_frame(29);
661
- shadows.keptMaps = module._tnw_web_frame(40);
662
- this.info.engine.uniformBytes = module._tnw_web_frame(41);
702
+ shadows.lampRedraws = frameField(heap, block, WEB_FRAME_FIELD.lampRedraws);
703
+ shadows.lampDraws = frameField(heap, block, WEB_FRAME_FIELD.lampShadowDraws);
704
+ shadows.shadowDraws = frameField(heap, block, WEB_FRAME_FIELD.shadowDraws);
705
+ shadows.lampMapMin = frameField(heap, block, WEB_FRAME_FIELD.lampMapMin);
706
+ shadows.lampMapMax = frameField(heap, block, WEB_FRAME_FIELD.lampMapMax);
707
+ shadows.keptMaps = frameField(heap, block, WEB_FRAME_FIELD.keptShadowMaps);
708
+ this.info.engine.uniformBytes = frameField(heap, block, WEB_FRAME_FIELD.uniformBytes);
663
709
  const split = this.info.engine.frameSplit;
664
710
  // Field 0 answers for the renderer that drew last and field 27 for the world's: an overlay has no shadow pass of its own.
665
711
  split.shadowDraws = this.#overlay ? 0 : shadows.shadowDraws;
666
- split.shadowTriangles = module._tnw_web_frame(85);
712
+ split.shadowTriangles = frameField(heap, block, WEB_FRAME_FIELD.shadowTriangles);
667
713
  split.mainDraws = this.info.render.frameCalls - split.shadowDraws;
668
714
  split.mainTriangles = lastTriangles - split.shadowTriangles;
669
- const gpuSamples = module._tnw_web_frame(8);
715
+ const gpuSamples = frameField(heap, block, WEB_FRAME_FIELD.gpuSamples);
670
716
  if (gpuSamples > this.#gpuSamples) {
671
717
  debug?.add(
672
718
  "gpuTimestampSamples",
@@ -675,51 +721,77 @@ export function defineWebRenderer(
675
721
  "native timestamp readback",
676
722
  );
677
723
  this.#gpuSamples = gpuSamples;
678
- const gpuMs = module._tnw_web_frame(7);
724
+ const gpuMs = frameField(heap, block, WEB_FRAME_FIELD.gpuMs);
679
725
  // The engine reports which submitted frame this sample belongs to; the readback can land
680
726
  // many frames later, so the arrival frame is never the sample's age.
681
727
  if (Number.isFinite(gpuMs) && gpuMs > 0) {
682
728
  this.info.render.timestamp = gpuMs;
683
- this.#timestampFrames = [module._tnw_web_frame(9)];
729
+ this.#timestampFrames = [frameField(heap, block, WEB_FRAME_FIELD.gpuSourceFrame)];
684
730
  const stages: Record<string, number> = {};
685
731
  GPU_STAGES.forEach((name, index) => {
686
- const stageMs = module._tnw_web_frame(GPU_STAGE_FIELD + index);
732
+ const stageMs = frameField(heap, block, GPU_STAGE_FIELD + index);
687
733
  if (stageMs > 0) stages[name] = stageMs;
688
734
  });
689
- const shadowMs = module._tnw_web_frame(SHADOW_SEGMENT_FIELD);
735
+ const shadowMs = frameField(heap, block, SHADOW_SEGMENT_FIELD);
690
736
  if (shadowMs > 0) stages[SHADOW_STAGE] = shadowMs;
691
737
  this.info.engine.gpuStages = stages;
692
- this.info.engine.shadows.sunGpuMs = module._tnw_web_frame(19);
693
- this.info.engine.shadows.lampGpuMs = module._tnw_web_frame(24);
738
+ this.info.engine.shadows.sunGpuMs = frameField(
739
+ heap,
740
+ block,
741
+ WEB_FRAME_FIELD.gpuSegmentShadow,
742
+ );
743
+ this.info.engine.shadows.lampGpuMs = frameField(
744
+ heap,
745
+ block,
746
+ WEB_FRAME_FIELD.gpuSegmentLampShadow,
747
+ );
748
+ const segment = (field: number): number => Math.max(0, frameField(heap, block, field));
749
+ const split = this.info.engine.gpuSplit;
750
+ split.frame = this.#timestampFrames[0] ?? -1;
751
+ split.main = segment(WEB_FRAME_FIELD.gpuSegmentScene);
752
+ split.shadow =
753
+ segment(WEB_FRAME_FIELD.gpuSegmentShadow) +
754
+ segment(WEB_FRAME_FIELD.gpuSegmentLampShadow);
755
+ split.other =
756
+ segment(WEB_FRAME_FIELD.gpuSegmentOther) +
757
+ segment(WEB_FRAME_FIELD.gpuSegmentOutput) +
758
+ segment(WEB_FRAME_FIELD.gpuSegmentGather);
694
759
  // Opt-in, as `__tnPassLog`: a probe that sets `__tnGpuSegments` to an array gets each resolved sample's
695
760
  // split (frame_gpu_timer.h segments: sun shadow, scene, other passes, output, gather, lamp shadow).
696
761
  (globalThis as { __tnGpuSegments?: number[][] }).__tnGpuSegments?.push([
697
762
  gpuMs,
698
- ...[19, 20, 21, 22, 23, 24].map((field) => module._tnw_web_frame(field)),
763
+ ...[
764
+ WEB_FRAME_FIELD.gpuSegmentShadow,
765
+ WEB_FRAME_FIELD.gpuSegmentScene,
766
+ WEB_FRAME_FIELD.gpuSegmentOther,
767
+ WEB_FRAME_FIELD.gpuSegmentOutput,
768
+ WEB_FRAME_FIELD.gpuSegmentGather,
769
+ WEB_FRAME_FIELD.gpuSegmentLampShadow,
770
+ ].map((field) => frameField(heap, block, field)),
699
771
  ]);
700
772
  }
701
773
  }
702
774
  const engine = this.info.engine;
703
- engine.compiles = module._tnw_web_frame(2);
704
- engine.textLookups = module._tnw_web_frame(3);
705
- engine.bindGroups = module._tnw_web_frame(4);
706
- engine.graphKeys = module._tnw_web_frame(5);
707
- engine.programs = module._tnw_web_frame(6);
775
+ engine.compiles = frameField(heap, block, WEB_FRAME_FIELD.pipelineCompiles);
776
+ engine.textLookups = frameField(heap, block, WEB_FRAME_FIELD.pipelineLookups);
777
+ engine.bindGroups = frameField(heap, block, WEB_FRAME_FIELD.bindGroupsCreated);
778
+ engine.graphKeys = frameField(heap, block, WEB_FRAME_FIELD.graphKeyBuilds);
779
+ engine.programs = frameField(heap, block, WEB_FRAME_FIELD.programs);
708
780
  const temporal = engine.temporal;
709
- temporal.active = module._tnw_web_frame(13) === 1;
710
- temporal.frame = module._tnw_web_frame(14);
711
- temporal.historyValid = module._tnw_web_frame(15) === 1;
712
- temporal.jitter[0] = module._tnw_web_frame(16);
713
- temporal.jitter[1] = module._tnw_web_frame(17);
714
- temporal.rejected = module._tnw_web_frame(30);
715
- temporal.clamped = module._tnw_web_frame(31);
716
- temporal.pixels = module._tnw_web_frame(32);
717
- temporal.countedFrame = module._tnw_web_frame(33);
718
- temporal.inputWidth = module._tnw_web_frame(35);
719
- temporal.inputHeight = module._tnw_web_frame(36);
720
- temporal.jitterLength = module._tnw_web_frame(37);
721
- temporal.outputWidth = module._tnw_web_frame(38);
722
- temporal.outputHeight = module._tnw_web_frame(39);
781
+ temporal.active = frameField(heap, block, WEB_FRAME_FIELD.temporalActive) === 1;
782
+ temporal.frame = frameField(heap, block, WEB_FRAME_FIELD.temporalFrame);
783
+ temporal.historyValid = frameField(heap, block, WEB_FRAME_FIELD.temporalHistoryValid) === 1;
784
+ temporal.jitter[0] = frameField(heap, block, WEB_FRAME_FIELD.temporalJitterX);
785
+ temporal.jitter[1] = frameField(heap, block, WEB_FRAME_FIELD.temporalJitterY);
786
+ temporal.rejected = frameField(heap, block, WEB_FRAME_FIELD.temporalRejected);
787
+ temporal.clamped = frameField(heap, block, WEB_FRAME_FIELD.temporalClamped);
788
+ temporal.pixels = frameField(heap, block, WEB_FRAME_FIELD.temporalPixels);
789
+ temporal.countedFrame = frameField(heap, block, WEB_FRAME_FIELD.temporalCountedFrame);
790
+ temporal.inputWidth = frameField(heap, block, WEB_FRAME_FIELD.inputWidth);
791
+ temporal.inputHeight = frameField(heap, block, WEB_FRAME_FIELD.inputHeight);
792
+ temporal.jitterLength = frameField(heap, block, WEB_FRAME_FIELD.temporalJitterLength);
793
+ temporal.outputWidth = frameField(heap, block, WEB_FRAME_FIELD.width);
794
+ temporal.outputHeight = frameField(heap, block, WEB_FRAME_FIELD.height);
723
795
  if (debug !== undefined && temporal.active) {
724
796
  // The native renderer counts the active/history/rejected/clamped pixel totals under these
725
797
  // exact names (foundation/waste_counters.h, renderer.cpp); a JS series beside them would be
@@ -728,12 +800,11 @@ export function defineWebRenderer(
728
800
  debug.add("temporalInputPixels", temporal.inputWidth * temporal.inputHeight, "temporal");
729
801
  debug.add("temporalOutputPixels", temporal.outputWidth * temporal.outputHeight, "temporal");
730
802
  }
731
- engine.samples = module._tnw_web_frame(18);
803
+ engine.samples = frameField(heap, block, WEB_FRAME_FIELD.sampleCount);
732
804
  const cpu = engine.cpuMs;
733
805
  const work = engine.work;
734
806
  if (this.#stageMeters && cpu !== undefined && work !== undefined) {
735
807
  // In place, from the array the host fills at the end of every render: the ABI census above stays as it was.
736
- const heap = module.HEAPF64;
737
808
  const first = this.#stageBase / 8;
738
809
  for (let index = 0; index < CPU_STAGE_ENTRIES.length; index += 1)
739
810
  cpu[(CPU_STAGE_ENTRIES[index] as readonly [CpuStageName, number])[0]] =
@@ -743,19 +814,21 @@ export function defineWebRenderer(
743
814
  heap[first + CPU_STAGE_ENTRIES.length + index] ?? 0;
744
815
  }
745
816
  const passes = engine.passes;
746
- passes.mainRecorded = module._tnw_web_frame(60);
747
- passes.recordCause = module._tnw_web_frame(61);
748
- passes.auxRecorded = module._tnw_web_frame(62);
749
- passes.recordMs = module._tnw_web_frame(63);
750
- passes.shadowEncodeMs = module._tnw_web_frame(64);
751
- passes.shadowRecorded = module._tnw_web_frame(65);
817
+ passes.mainRecorded = frameField(heap, block, WEB_FRAME_FIELD.mainRecorded);
818
+ passes.recordCause = frameField(heap, block, WEB_FRAME_FIELD.recordCause);
819
+ passes.auxRecorded = frameField(heap, block, WEB_FRAME_FIELD.auxRecorded);
820
+ passes.recordMs = frameField(heap, block, WEB_FRAME_FIELD.recordMs);
821
+ passes.shadowEncodeMs = frameField(heap, block, WEB_FRAME_FIELD.shadowEncodeMs);
822
+ passes.shadowRecorded = frameField(heap, block, WEB_FRAME_FIELD.shadowRecorded);
752
823
  // The startup's own report, read only while the cover is up so a held frame sees the one it just made. After
753
824
  // the release nothing reads it, so a steady frame pays no host call for it and the last values stand.
754
825
  if (this.#presentationHeld) {
755
- engine.startup.worldSubmissions = module._tnw_web_frame(71);
756
- engine.startup.shadowInputsReady = module._tnw_web_frame(72) === 1;
826
+ engine.startup.worldSubmissions = frameField(heap, block, WEB_FRAME_FIELD.worldSubmissions);
827
+ engine.startup.shadowInputsReady =
828
+ frameField(heap, block, WEB_FRAME_FIELD.shadowInputsReady) === 1;
757
829
  } else if (!engine.startup.worldPresented) {
758
- engine.startup.worldPresented = module._tnw_web_frame(86) === 1;
830
+ engine.startup.worldPresented =
831
+ module._tnw_web_frame(WEB_FRAME_FIELD.firstWorldFrameDrawn) === 1;
759
832
  }
760
833
  // Opt-in, as `__tnCallCounts`: a probe that sets `__tnPassLog` to an array gets every frame's counters.
761
834
  (globalThis as { __tnPassLog?: unknown[] }).__tnPassLog?.push({ ...passes });
@@ -798,7 +871,13 @@ export function defineWebRenderer(
798
871
  * render() while a render target is set (render-target.ts): draws into the target. `deferred` (an offscreen PassNode)
799
872
  * plans the draw and has the engine record it in the frame that samples the target.
800
873
  */
801
- [DRAW_TARGET](target: unknown, root: unknown, camera: unknown, deferred = false): void {
874
+ [DRAW_TARGET](
875
+ target: unknown,
876
+ root: unknown,
877
+ camera: unknown,
878
+ deferred = false,
879
+ layer = 0,
880
+ ): void {
802
881
  if (this.#adapter === undefined)
803
882
  throw new Error("TN_WASM_RENDERER: render() before init() finished.");
804
883
  if (this.#lost) return;
@@ -814,7 +893,8 @@ export function defineWebRenderer(
814
893
  handles,
815
894
  handles + 12,
816
895
  ...this.#clear,
817
- (deferred ? 1 : 0) | (this.autoClear ? 0 : 2),
896
+ // Bits 8 and up: the layer of an array target (tnw_web_render_target).
897
+ (deferred ? 1 : 0) | (this.autoClear ? 0 : 2) | (layer << 8),
818
898
  ),
819
899
  );
820
900
  }
@@ -1001,14 +1081,15 @@ export function defineWebRenderer(
1001
1081
  throw new Error(
1002
1082
  "TN_COMPUTE_DISPATCH_SIZE: renderer.compute(nodes, dispatchSize) is not available on the Wasm engine; give the node its count.",
1003
1083
  );
1004
- beforeRender();
1005
- for (const node of Array.isArray(computeNodes) ? computeNodes : [computeNodes]) {
1084
+ const kernels = (Array.isArray(computeNodes) ? computeNodes : [computeNodes]).map((node) => {
1006
1085
  if (!isTslNode(node))
1007
1086
  throw new TypeError(
1008
1087
  "TN_COMPUTE_NODE: renderer.compute takes the node Fn(() => { ... })().compute(count) makes",
1009
1088
  );
1010
- tsl.compute(node[TSL_NODE]);
1011
- }
1089
+ return node[TSL_NODE];
1090
+ });
1091
+ beforeRender(undefined, kernels);
1092
+ for (const kernel of kernels) tsl.compute(kernel);
1012
1093
  }
1013
1094
 
1014
1095
  async computeAsync(computeNodes: unknown, dispatchSize: unknown = null): Promise<void> {
@@ -1075,25 +1156,11 @@ export function defineWebRenderer(
1075
1156
  throw new Error("TN_WASM_RENDERER: copyTextureToTexture() before init() finished.");
1076
1157
  writeHandle(0, source, "source texture");
1077
1158
  writeHandle(12, destination, "destination texture");
1078
- const { min, max } = srcRegion ?? {};
1079
- // A Box2 has no z: its depth is one layer, a Box3's its own.
1080
- const size =
1081
- min && max ? [max.x - min.x, max.y - min.y, (max.z ?? 1) - (min.z ?? 0)] : [-1, -1, 1];
1082
1159
  check(
1083
1160
  module._tnw_web_copy_texture(
1084
1161
  handles,
1085
1162
  handles + 12,
1086
- min?.x ?? 0,
1087
- min?.y ?? 0,
1088
- min?.z ?? 0,
1089
- size[0] as number,
1090
- size[1] as number,
1091
- srcRegion?.isBox3 ? (size[2] as number) : 1,
1092
- dstPosition?.x ?? 0,
1093
- dstPosition?.y ?? 0,
1094
- dstPosition?.z ?? 0,
1095
- srcLevel,
1096
- dstLevel,
1163
+ ...textureCopyArguments(srcRegion, dstPosition, srcLevel, dstLevel),
1097
1164
  ),
1098
1165
  );
1099
1166
  }
@@ -1208,12 +1275,12 @@ export function defineWebRenderer(
1208
1275
  ),
1209
1276
  });
1210
1277
  defineRenderTargets(Renderer.prototype, (renderer) => ({
1211
- draw: (target, root, camera) =>
1212
- (renderer as { [DRAW_TARGET](t: unknown, r: unknown, c: unknown): void })[DRAW_TARGET](
1213
- target,
1214
- root,
1215
- camera,
1216
- ),
1278
+ draw: (target, root, camera, layer) =>
1279
+ (
1280
+ renderer as {
1281
+ [DRAW_TARGET](t: unknown, r: unknown, c: unknown, d: boolean, l: number): void;
1282
+ }
1283
+ )[DRAW_TARGET](target, root, camera, false, layer),
1217
1284
  clear: (target, color, depth) =>
1218
1285
  (renderer as { [CLEAR_TARGET](t: unknown, c: boolean, d: boolean): void })[CLEAR_TARGET](
1219
1286
  target,