@holtjs/stage 0.13.0 → 0.14.0

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.
@@ -3128,11 +3128,11 @@ async function requestWebGpuDevice(canvas, options = {}) {
3128
3128
  return null;
3129
3129
  }
3130
3130
  }
3131
- const MAX_VERTS = 65536;
3132
- const MAX_INDICES = 98304;
3131
+ const INITIAL_VERTS = 65536;
3132
+ const INITIAL_INDICES = 98304;
3133
3133
  const VERT_FLOATS = 6;
3134
3134
  const VERT_STRIDE = 24;
3135
- const MAX_SDF_PANELS = 16384;
3135
+ const INITIAL_SDF_PANELS = 16384;
3136
3136
  const SDF_FLOATS = 16;
3137
3137
  const SDF_STRIDE = 64;
3138
3138
  const WGSL_BATCH_SRC = `
@@ -3405,18 +3405,27 @@ var RendererWebGpu = class extends EventEmitter {
3405
3405
  sdfQuadIbo;
3406
3406
  samplerLinear;
3407
3407
  samplerNearest;
3408
+ samplerLinearRepeat;
3409
+ samplerNearestRepeat;
3408
3410
  batchBgl;
3409
3411
  sdfBgl;
3410
3412
  sdfBindGroup;
3413
+ batchMod;
3414
+ batchLayout;
3415
+ sdfMod;
3416
+ sdfLayout;
3411
3417
  batchPipelines = /* @__PURE__ */ new Map();
3412
3418
  sdfPipelines = /* @__PURE__ */ new Map();
3413
3419
  texCache = /* @__PURE__ */ new WeakMap();
3414
3420
  dummyEntry;
3415
- vertRaw = new ArrayBuffer(MAX_VERTS * VERT_STRIDE);
3421
+ vertCap = INITIAL_VERTS;
3422
+ indexCap = INITIAL_INDICES;
3423
+ vertRaw = new ArrayBuffer(INITIAL_VERTS * VERT_STRIDE);
3416
3424
  f32 = new Float32Array(this.vertRaw);
3417
3425
  u32 = new Uint32Array(this.vertRaw);
3418
- indices = new Uint16Array(MAX_INDICES);
3419
- sdfRaw = new ArrayBuffer(MAX_SDF_PANELS * SDF_STRIDE);
3426
+ indices = new Uint32Array(INITIAL_INDICES);
3427
+ sdfCap = INITIAL_SDF_PANELS;
3428
+ sdfRaw = new ArrayBuffer(INITIAL_SDF_PANELS * SDF_STRIDE);
3420
3429
  sdfF32 = new Float32Array(this.sdfRaw);
3421
3430
  sdfU32 = new Uint32Array(this.sdfRaw);
3422
3431
  vertCount = 0;
@@ -3424,6 +3433,8 @@ var RendererWebGpu = class extends EventEmitter {
3424
3433
  cmdStartIndex = 0;
3425
3434
  sdfCount = 0;
3426
3435
  sdfCmdStart = 0;
3436
+ curTargetW = 1;
3437
+ curTargetH = 1;
3427
3438
  curBlend = BlendModes.NORMAL;
3428
3439
  curBindGroup = null;
3429
3440
  cmds = [];
@@ -3515,6 +3526,18 @@ var RendererWebGpu = class extends EventEmitter {
3515
3526
  magFilter: "nearest",
3516
3527
  minFilter: "nearest"
3517
3528
  });
3529
+ this.samplerLinearRepeat = dev.createSampler({
3530
+ magFilter: "linear",
3531
+ minFilter: "linear",
3532
+ addressModeU: "repeat",
3533
+ addressModeV: "repeat"
3534
+ });
3535
+ this.samplerNearestRepeat = dev.createSampler({
3536
+ magFilter: "nearest",
3537
+ minFilter: "nearest",
3538
+ addressModeU: "repeat",
3539
+ addressModeV: "repeat"
3540
+ });
3518
3541
  this.batchBgl = dev.createBindGroupLayout({ entries: [
3519
3542
  {
3520
3543
  binding: 0,
@@ -3544,101 +3567,18 @@ var RendererWebGpu = class extends EventEmitter {
3544
3567
  resource: { buffer: this.globalsBuf }
3545
3568
  }]
3546
3569
  });
3547
- const batchMod = dev.createShaderModule({ code: WGSL_BATCH_SRC });
3548
- const batchLayout = dev.createPipelineLayout({ bindGroupLayouts: [this.batchBgl] });
3549
- const sdfMod = dev.createShaderModule({ code: WGSL_SDF_SRC });
3550
- const sdfLayout = dev.createPipelineLayout({ bindGroupLayouts: [this.sdfBgl] });
3570
+ this.batchMod = dev.createShaderModule({ code: WGSL_BATCH_SRC });
3571
+ this.batchLayout = dev.createPipelineLayout({ bindGroupLayouts: [this.batchBgl] });
3572
+ this.sdfMod = dev.createShaderModule({ code: WGSL_SDF_SRC });
3573
+ this.sdfLayout = dev.createPipelineLayout({ bindGroupLayouts: [this.sdfBgl] });
3551
3574
  for (const mode of [
3552
3575
  BlendModes.NORMAL,
3553
3576
  BlendModes.ADD,
3554
3577
  BlendModes.MULTIPLY,
3555
3578
  BlendModes.SCREEN
3556
3579
  ]) {
3557
- this.batchPipelines.set(mode, dev.createRenderPipeline({
3558
- layout: batchLayout,
3559
- vertex: {
3560
- module: batchMod,
3561
- entryPoint: "vsMain",
3562
- buffers: [{
3563
- arrayStride: VERT_STRIDE,
3564
- attributes: [
3565
- {
3566
- shaderLocation: 0,
3567
- offset: 0,
3568
- format: "float32x2"
3569
- },
3570
- {
3571
- shaderLocation: 1,
3572
- offset: 8,
3573
- format: "float32x2"
3574
- },
3575
- {
3576
- shaderLocation: 2,
3577
- offset: 16,
3578
- format: "uint32"
3579
- },
3580
- {
3581
- shaderLocation: 3,
3582
- offset: 20,
3583
- format: "float32"
3584
- }
3585
- ]
3586
- }]
3587
- },
3588
- fragment: {
3589
- module: batchMod,
3590
- entryPoint: "fsMain",
3591
- targets: [blendTargetState(format, mode)]
3592
- },
3593
- primitive: { topology: "triangle-list" }
3594
- }));
3595
- this.sdfPipelines.set(mode, dev.createRenderPipeline({
3596
- layout: sdfLayout,
3597
- vertex: {
3598
- module: sdfMod,
3599
- entryPoint: "vsSdf",
3600
- buffers: [{
3601
- arrayStride: 8,
3602
- stepMode: "vertex",
3603
- attributes: [{
3604
- shaderLocation: 0,
3605
- offset: 0,
3606
- format: "float32x2"
3607
- }]
3608
- }, {
3609
- arrayStride: SDF_STRIDE,
3610
- stepMode: "instance",
3611
- attributes: [
3612
- {
3613
- shaderLocation: 1,
3614
- offset: 0,
3615
- format: "float32x4"
3616
- },
3617
- {
3618
- shaderLocation: 2,
3619
- offset: 16,
3620
- format: "float32x4"
3621
- },
3622
- {
3623
- shaderLocation: 3,
3624
- offset: 32,
3625
- format: "float32x4"
3626
- },
3627
- {
3628
- shaderLocation: 4,
3629
- offset: 48,
3630
- format: "float32x4"
3631
- }
3632
- ]
3633
- }]
3634
- },
3635
- fragment: {
3636
- module: sdfMod,
3637
- entryPoint: "fsSdf",
3638
- targets: [blendTargetState(format, mode)]
3639
- },
3640
- primitive: { topology: "triangle-list" }
3641
- }));
3580
+ this.getBatchPipeline(format, mode);
3581
+ this.getSdfPipeline(format, mode);
3642
3582
  }
3643
3583
  const dummyTex = dev.createTexture({
3644
3584
  size: [
@@ -3673,6 +3613,159 @@ var RendererWebGpu = class extends EventEmitter {
3673
3613
  bindGroupNearest: this.makeBatchBindGroup(dummyView, this.samplerNearest)
3674
3614
  };
3675
3615
  }
3616
+ getBatchPipeline(targetFormat, mode) {
3617
+ const key = `${targetFormat}:${mode}`;
3618
+ let p = this.batchPipelines.get(key);
3619
+ if (p) return p;
3620
+ p = this.device.createRenderPipeline({
3621
+ layout: this.batchLayout,
3622
+ vertex: {
3623
+ module: this.batchMod,
3624
+ entryPoint: "vsMain",
3625
+ buffers: [{
3626
+ arrayStride: VERT_STRIDE,
3627
+ attributes: [
3628
+ {
3629
+ shaderLocation: 0,
3630
+ offset: 0,
3631
+ format: "float32x2"
3632
+ },
3633
+ {
3634
+ shaderLocation: 1,
3635
+ offset: 8,
3636
+ format: "float32x2"
3637
+ },
3638
+ {
3639
+ shaderLocation: 2,
3640
+ offset: 16,
3641
+ format: "uint32"
3642
+ },
3643
+ {
3644
+ shaderLocation: 3,
3645
+ offset: 20,
3646
+ format: "float32"
3647
+ }
3648
+ ]
3649
+ }]
3650
+ },
3651
+ fragment: {
3652
+ module: this.batchMod,
3653
+ entryPoint: "fsMain",
3654
+ targets: [blendTargetState(targetFormat, mode)]
3655
+ },
3656
+ primitive: { topology: "triangle-list" }
3657
+ });
3658
+ this.batchPipelines.set(key, p);
3659
+ return p;
3660
+ }
3661
+ getSdfPipeline(targetFormat, mode) {
3662
+ const key = `${targetFormat}:${mode}`;
3663
+ let p = this.sdfPipelines.get(key);
3664
+ if (p) return p;
3665
+ p = this.device.createRenderPipeline({
3666
+ layout: this.sdfLayout,
3667
+ vertex: {
3668
+ module: this.sdfMod,
3669
+ entryPoint: "vsSdf",
3670
+ buffers: [{
3671
+ arrayStride: 8,
3672
+ stepMode: "vertex",
3673
+ attributes: [{
3674
+ shaderLocation: 0,
3675
+ offset: 0,
3676
+ format: "float32x2"
3677
+ }]
3678
+ }, {
3679
+ arrayStride: SDF_STRIDE,
3680
+ stepMode: "instance",
3681
+ attributes: [
3682
+ {
3683
+ shaderLocation: 1,
3684
+ offset: 0,
3685
+ format: "float32x4"
3686
+ },
3687
+ {
3688
+ shaderLocation: 2,
3689
+ offset: 16,
3690
+ format: "float32x4"
3691
+ },
3692
+ {
3693
+ shaderLocation: 3,
3694
+ offset: 32,
3695
+ format: "float32x4"
3696
+ },
3697
+ {
3698
+ shaderLocation: 4,
3699
+ offset: 48,
3700
+ format: "float32x4"
3701
+ }
3702
+ ]
3703
+ }]
3704
+ },
3705
+ fragment: {
3706
+ module: this.sdfMod,
3707
+ entryPoint: "fsSdf",
3708
+ targets: [blendTargetState(targetFormat, mode)]
3709
+ },
3710
+ primitive: { topology: "triangle-list" }
3711
+ });
3712
+ this.sdfPipelines.set(key, p);
3713
+ return p;
3714
+ }
3715
+ ensureBatchCapacity(extraVerts, extraIndices) {
3716
+ if (this.vertCount + extraVerts > this.vertCap) {
3717
+ let nextCap = this.vertCap;
3718
+ while (nextCap < this.vertCount + extraVerts) nextCap *= 2;
3719
+ const nextRaw = new ArrayBuffer(nextCap * VERT_STRIDE);
3720
+ const nextF32 = new Float32Array(nextRaw);
3721
+ nextF32.set(this.f32.subarray(0, this.vertCount * VERT_FLOATS));
3722
+ this.vertCap = nextCap;
3723
+ this.vertRaw = nextRaw;
3724
+ this.f32 = nextF32;
3725
+ this.u32 = new Uint32Array(nextRaw);
3726
+ try {
3727
+ this.vbo?.destroy?.();
3728
+ } catch {}
3729
+ this.vbo = this.device.createBuffer({
3730
+ size: nextRaw.byteLength,
3731
+ usage: 40
3732
+ });
3733
+ }
3734
+ if (this.indexCount + extraIndices > this.indexCap) {
3735
+ let nextCap = this.indexCap;
3736
+ while (nextCap < this.indexCount + extraIndices) nextCap *= 2;
3737
+ const nextInd = new Uint32Array(nextCap);
3738
+ nextInd.set(this.indices.subarray(0, this.indexCount));
3739
+ this.indexCap = nextCap;
3740
+ this.indices = nextInd;
3741
+ try {
3742
+ this.ibo?.destroy?.();
3743
+ } catch {}
3744
+ this.ibo = this.device.createBuffer({
3745
+ size: nextInd.byteLength,
3746
+ usage: 24
3747
+ });
3748
+ }
3749
+ }
3750
+ ensureSdfCapacity(extraInstances) {
3751
+ if (this.sdfCount + extraInstances <= this.sdfCap) return;
3752
+ let nextCap = this.sdfCap;
3753
+ while (nextCap < this.sdfCount + extraInstances) nextCap *= 2;
3754
+ const nextRaw = new ArrayBuffer(nextCap * SDF_STRIDE);
3755
+ const nextF32 = new Float32Array(nextRaw);
3756
+ nextF32.set(this.sdfF32.subarray(0, this.sdfCount * SDF_FLOATS));
3757
+ this.sdfCap = nextCap;
3758
+ this.sdfRaw = nextRaw;
3759
+ this.sdfF32 = nextF32;
3760
+ this.sdfU32 = new Uint32Array(nextRaw);
3761
+ try {
3762
+ this.sdfInstVbo?.destroy?.();
3763
+ } catch {}
3764
+ this.sdfInstVbo = this.device.createBuffer({
3765
+ size: nextRaw.byteLength,
3766
+ usage: 40
3767
+ });
3768
+ }
3676
3769
  makeBatchBindGroup(view, sampler) {
3677
3770
  return this.device.createBindGroup({
3678
3771
  layout: this.batchBgl,
@@ -3836,10 +3929,19 @@ var RendererWebGpu = class extends EventEmitter {
3836
3929
  if (dirty && entry && !isRenderTarget) this.extractAndUploadTexture(entry.tex, image, w, h, premultiply);
3837
3930
  return nearest ? entry.bindGroupNearest : entry.bindGroupLinear;
3838
3931
  }
3839
- ensureSourceBindGroup(src) {
3840
- const bg = this.ensureGpuTexture(src, src.image, src.width, src.height, src.filterMode === FilterMode.NEAREST, src.dirty, src.premultiplyAlpha, src.isRenderTarget);
3932
+ ensureSourceBindGroup(src, repeat = false) {
3933
+ const nearest = src.filterMode === FilterMode.NEAREST;
3934
+ const bg = this.ensureGpuTexture(src, src.image, src.width, src.height, nearest, src.dirty, src.premultiplyAlpha, src.isRenderTarget);
3841
3935
  src.dirty = false;
3842
- return bg;
3936
+ if (!repeat || !bg) return bg;
3937
+ const entry = this.texCache.get(src);
3938
+ if (!entry) return bg;
3939
+ if (nearest) {
3940
+ if (!entry.bindGroupNearestRepeat) entry.bindGroupNearestRepeat = this.makeBatchBindGroup(entry.view, this.samplerNearestRepeat);
3941
+ return entry.bindGroupNearestRepeat;
3942
+ }
3943
+ if (!entry.bindGroupLinearRepeat) entry.bindGroupLinearRepeat = this.makeBatchBindGroup(entry.view, this.samplerLinearRepeat);
3944
+ return entry.bindGroupLinearRepeat;
3843
3945
  }
3844
3946
  currentScissor() {
3845
3947
  return this.scissorStack.length > 0 ? { ...this.scissorStack[this.scissorStack.length - 1] } : null;
@@ -3880,36 +3982,45 @@ var RendererWebGpu = class extends EventEmitter {
3880
3982
  this.curBindGroup = bindGroup;
3881
3983
  }
3882
3984
  pushQuad(m, x0, y0, x1, y1, u0, v0, u1, v1, abgr, mode, blend, bindGroup) {
3883
- if (this.vertCount + 4 > MAX_VERTS || this.indexCount + 6 > MAX_INDICES) return;
3985
+ const a = m.a, b = m.b, c = m.c, d = m.d, tx = m.tx, ty = m.ty;
3986
+ const sx0 = a * x0 + c * y0 + tx, sy0 = b * x0 + d * y0 + ty;
3987
+ const sx1 = a * x1 + c * y0 + tx, sy1 = b * x1 + d * y0 + ty;
3988
+ const sx2 = a * x1 + c * y1 + tx, sy2 = b * x1 + d * y1 + ty;
3989
+ const sx3 = a * x0 + c * y1 + tx, sy3 = b * x0 + d * y1 + ty;
3990
+ const tw = this.curTargetW, th = this.curTargetH;
3991
+ if (sx0 < 0 && sx1 < 0 && sx2 < 0 && sx3 < 0 || sx0 > tw && sx1 > tw && sx2 > tw && sx3 > tw || sy0 < 0 && sy1 < 0 && sy2 < 0 && sy3 < 0 || sy0 > th && sy1 > th && sy2 > th && sy3 > th) {
3992
+ this.lastStats.culled++;
3993
+ return;
3994
+ }
3995
+ this.ensureBatchCapacity(4, 6);
3884
3996
  this.bindBatch(blend, bindGroup);
3885
3997
  const bv = this.vertCount;
3886
3998
  let fi = bv * VERT_FLOATS;
3887
3999
  const f32 = this.f32;
3888
4000
  const u32 = this.u32;
3889
- const a = m.a, b = m.b, c = m.c, d = m.d, tx = m.tx, ty = m.ty;
3890
- f32[fi] = a * x0 + c * y0 + tx;
3891
- f32[fi + 1] = b * x0 + d * y0 + ty;
4001
+ f32[fi] = sx0;
4002
+ f32[fi + 1] = sy0;
3892
4003
  f32[fi + 2] = u0;
3893
4004
  f32[fi + 3] = v0;
3894
4005
  u32[fi + 4] = abgr;
3895
4006
  f32[fi + 5] = mode;
3896
4007
  fi += VERT_FLOATS;
3897
- f32[fi] = a * x1 + c * y0 + tx;
3898
- f32[fi + 1] = b * x1 + d * y0 + ty;
4008
+ f32[fi] = sx1;
4009
+ f32[fi + 1] = sy1;
3899
4010
  f32[fi + 2] = u1;
3900
4011
  f32[fi + 3] = v0;
3901
4012
  u32[fi + 4] = abgr;
3902
4013
  f32[fi + 5] = mode;
3903
4014
  fi += VERT_FLOATS;
3904
- f32[fi] = a * x1 + c * y1 + tx;
3905
- f32[fi + 1] = b * x1 + d * y1 + ty;
4015
+ f32[fi] = sx2;
4016
+ f32[fi + 1] = sy2;
3906
4017
  f32[fi + 2] = u1;
3907
4018
  f32[fi + 3] = v1;
3908
4019
  u32[fi + 4] = abgr;
3909
4020
  f32[fi + 5] = mode;
3910
4021
  fi += VERT_FLOATS;
3911
- f32[fi] = a * x0 + c * y1 + tx;
3912
- f32[fi + 1] = b * x0 + d * y1 + ty;
4022
+ f32[fi] = sx3;
4023
+ f32[fi + 1] = sy3;
3913
4024
  f32[fi + 2] = u0;
3914
4025
  f32[fi + 3] = v1;
3915
4026
  u32[fi + 4] = abgr;
@@ -3926,29 +4037,37 @@ var RendererWebGpu = class extends EventEmitter {
3926
4037
  this.indexCount = ii + 6;
3927
4038
  }
3928
4039
  pushTri(m, x0, y0, x1, y1, x2, y2, abgr, blend) {
3929
- if (this.vertCount + 3 > MAX_VERTS || this.indexCount + 3 > MAX_INDICES) return;
4040
+ const a = m.a, b = m.b, c = m.c, d = m.d, tx = m.tx, ty = m.ty;
4041
+ const sx0 = a * x0 + c * y0 + tx, sy0 = b * x0 + d * y0 + ty;
4042
+ const sx1 = a * x1 + c * y1 + tx, sy1 = b * x1 + d * y1 + ty;
4043
+ const sx2 = a * x2 + c * y2 + tx, sy2 = b * x2 + d * y2 + ty;
4044
+ const tw = this.curTargetW, th = this.curTargetH;
4045
+ if (sx0 < 0 && sx1 < 0 && sx2 < 0 || sx0 > tw && sx1 > tw && sx2 > tw || sy0 < 0 && sy1 < 0 && sy2 < 0 || sy0 > th && sy1 > th && sy2 > th) {
4046
+ this.lastStats.culled++;
4047
+ return;
4048
+ }
4049
+ this.ensureBatchCapacity(3, 3);
3930
4050
  this.bindBatch(blend, this.dummyEntry.bindGroupLinear);
3931
4051
  const bv = this.vertCount;
3932
4052
  let fi = bv * VERT_FLOATS;
3933
4053
  const f32 = this.f32;
3934
4054
  const u32 = this.u32;
3935
- const a = m.a, b = m.b, c = m.c, d = m.d, tx = m.tx, ty = m.ty;
3936
- f32[fi] = a * x0 + c * y0 + tx;
3937
- f32[fi + 1] = b * x0 + d * y0 + ty;
4055
+ f32[fi] = sx0;
4056
+ f32[fi + 1] = sy0;
3938
4057
  f32[fi + 2] = 0;
3939
4058
  f32[fi + 3] = 0;
3940
4059
  u32[fi + 4] = abgr;
3941
4060
  f32[fi + 5] = 2;
3942
4061
  fi += VERT_FLOATS;
3943
- f32[fi] = a * x1 + c * y1 + tx;
3944
- f32[fi + 1] = b * x1 + d * y1 + ty;
4062
+ f32[fi] = sx1;
4063
+ f32[fi + 1] = sy1;
3945
4064
  f32[fi + 2] = 0;
3946
4065
  f32[fi + 3] = 0;
3947
4066
  u32[fi + 4] = abgr;
3948
4067
  f32[fi + 5] = 2;
3949
4068
  fi += VERT_FLOATS;
3950
- f32[fi] = a * x2 + c * y2 + tx;
3951
- f32[fi + 1] = b * x2 + d * y2 + ty;
4069
+ f32[fi] = sx2;
4070
+ f32[fi + 1] = sy2;
3952
4071
  f32[fi + 2] = 0;
3953
4072
  f32[fi + 3] = 0;
3954
4073
  u32[fi + 4] = abgr;
@@ -3961,12 +4080,21 @@ var RendererWebGpu = class extends EventEmitter {
3961
4080
  this.indexCount = ii + 3;
3962
4081
  }
3963
4082
  pushSdfInstance(m, rx, ry, w, h, rTL, rTR, rBR, rBL, borderW, pixelStep, fillAbgr, borderAbgr, blend) {
3964
- if (this.sdfCount >= MAX_SDF_PANELS || w <= 0 || h <= 0) return;
4083
+ if (w <= 0 || h <= 0) return;
4084
+ const tx = m.a * rx + m.c * ry + m.tx;
4085
+ const ty = m.b * rx + m.d * ry + m.ty;
4086
+ const sx1 = tx + m.a * w, sy1 = ty + m.b * w;
4087
+ const sx2 = sx1 + m.c * h, sy2 = sy1 + m.d * h;
4088
+ const sx3 = tx + m.c * h, sy3 = ty + m.d * h;
4089
+ const tw = this.curTargetW, th = this.curTargetH;
4090
+ if (tx < 0 && sx1 < 0 && sx2 < 0 && sx3 < 0 || tx > tw && sx1 > tw && sx2 > tw && sx3 > tw || ty < 0 && sy1 < 0 && sy2 < 0 && sy3 < 0 || ty > th && sy1 > th && sy2 > th && sy3 > th) {
4091
+ this.lastStats.culled++;
4092
+ return;
4093
+ }
4094
+ this.ensureSdfCapacity(1);
3965
4095
  if (this.indexCount > this.cmdStartIndex) this.flushPendingBatch();
3966
4096
  if (this.curBlend !== blend && this.sdfCount > this.sdfCmdStart) this.flushPendingSdf();
3967
4097
  this.curBlend = blend;
3968
- const tx = m.a * rx + m.c * ry + m.tx;
3969
- const ty = m.b * rx + m.d * ry + m.ty;
3970
4098
  const maxR = Math.min(w, h) * .5;
3971
4099
  const base = this.sdfCount * SDF_FLOATS;
3972
4100
  const f = this.sdfF32;
@@ -4230,27 +4358,86 @@ var RendererWebGpu = class extends EventEmitter {
4230
4358
  const txt = obj;
4231
4359
  const cv = txt.ensureCanvas();
4232
4360
  if (!cv || cv.width <= 0 || cv.height <= 0) break;
4233
- const prevRev = txt._wgpuRev ?? -1;
4234
- const curRev = txt.canvasDirty ? -2 : txt._wgpuSeq ?? 0;
4235
- const dirty = prevRev !== curRev || txt.glDirty;
4361
+ const dirty = Boolean(txt.glDirty);
4236
4362
  const bg = this.ensureGpuTexture(txt, cv, cv.width, cv.height, false, dirty);
4237
4363
  txt.glDirty = false;
4238
- txt._wgpuRev = curRev;
4239
4364
  if (!bg) break;
4240
4365
  const ox = txt.displayOriginX, oy = txt.displayOriginY;
4241
4366
  const rgb = txt.isTinted ? txt.tint : WHITE_RGB;
4242
- this.pushQuad(m, -ox, -oy, txt.width - ox, txt.height - oy, 0, 0, 1, 1, packColorAbgr(rgb, effAlpha), 0, txt.blendMode, bg);
4367
+ if (txt.isCropped && txt.width > 0 && txt.height > 0) {
4368
+ const cx0 = Math.max(0, Math.min(txt.width, txt.cropX));
4369
+ const cy0 = Math.max(0, Math.min(txt.height, txt.cropY));
4370
+ const cx1 = Math.max(cx0, Math.min(txt.width, txt.cropX + txt.cropW));
4371
+ const cy1 = Math.max(cy0, Math.min(txt.height, txt.cropY + txt.cropH));
4372
+ if (cx1 > cx0 && cy1 > cy0) this.pushQuad(m, cx0 - ox, cy0 - oy, cx1 - ox, cy1 - oy, cx0 / txt.width, cy0 / txt.height, cx1 / txt.width, cy1 / txt.height, packColorAbgr(rgb, effAlpha), 0, txt.blendMode, bg);
4373
+ } else this.pushQuad(m, -ox, -oy, txt.width - ox, txt.height - oy, 0, 0, 1, 1, packColorAbgr(rgb, effAlpha), 0, txt.blendMode, bg);
4243
4374
  break;
4244
4375
  }
4245
- case "NineSlice":
4246
- case "TileSprite": {
4376
+ case "NineSlice": {
4247
4377
  const ns = obj;
4248
4378
  const fr = ns.frame;
4249
- if (!fr) break;
4379
+ if (!fr || ns.width <= 0 || ns.height <= 0) break;
4250
4380
  const bg = this.ensureSourceBindGroup(fr.source);
4251
4381
  if (!bg) break;
4252
4382
  const ox = ns.displayOriginX, oy = ns.displayOriginY;
4253
- this.pushQuad(m, -ox, -oy, ns.width - ox, ns.height - oy, fr.u0, fr.v0, fr.u1, fr.v1, packColorAbgr(ns.isTinted ? ns.tint : WHITE_RGB, effAlpha), 0, ns.blendMode, bg);
4383
+ const abgr = packColorAbgr(ns.isTinted ? ns.tint : WHITE_RGB, effAlpha);
4384
+ const sw = fr.cutWidth, sh = fr.cutHeight;
4385
+ const lw = Math.min(ns.leftWidth, ns.width * .5, sw);
4386
+ const rw = Math.min(ns.rightWidth, ns.width * .5, sw - lw);
4387
+ const th = Math.min(ns.topHeight, ns.height * .5, sh);
4388
+ const bh = Math.min(ns.bottomHeight, ns.height * .5, sh - th);
4389
+ const xs = [
4390
+ -ox,
4391
+ lw - ox,
4392
+ ns.width - rw - ox,
4393
+ ns.width - ox
4394
+ ];
4395
+ const ys = [
4396
+ -oy,
4397
+ th - oy,
4398
+ ns.height - bh - oy,
4399
+ ns.height - oy
4400
+ ];
4401
+ const du = fr.u1 - fr.u0, dv = fr.v1 - fr.v0;
4402
+ const us = [
4403
+ fr.u0,
4404
+ fr.u0 + lw / sw * du,
4405
+ fr.u1 - rw / sw * du,
4406
+ fr.u1
4407
+ ];
4408
+ const vs = [
4409
+ fr.v0,
4410
+ fr.v0 + th / sh * dv,
4411
+ fr.v1 - bh / sh * dv,
4412
+ fr.v1
4413
+ ];
4414
+ for (let ry = 0; ry < 3; ry++) {
4415
+ if (ys[ry + 1] <= ys[ry]) continue;
4416
+ for (let rx = 0; rx < 3; rx++) {
4417
+ if (xs[rx + 1] <= xs[rx]) continue;
4418
+ this.pushQuad(m, xs[rx], ys[ry], xs[rx + 1], ys[ry + 1], us[rx], vs[ry], us[rx + 1], vs[ry + 1], abgr, 0, ns.blendMode, bg);
4419
+ }
4420
+ }
4421
+ break;
4422
+ }
4423
+ case "TileSprite": {
4424
+ const ts = obj;
4425
+ const fr = ts.frame;
4426
+ if (!fr || ts.width <= 0 || ts.height <= 0) break;
4427
+ const isFullTex = fr.cutX === 0 && fr.cutY === 0 && fr.cutWidth === fr.source.width && fr.cutHeight === fr.source.height;
4428
+ const bg = this.ensureSourceBindGroup(fr.source, isFullTex);
4429
+ if (!bg) break;
4430
+ const ox = ts.displayOriginX, oy = ts.displayOriginY;
4431
+ const abgr = packColorAbgr(ts.isTinted ? ts.tint : WHITE_RGB, effAlpha);
4432
+ if (isFullTex) {
4433
+ const sx = Math.max(1e-4, ts.tileScaleX);
4434
+ const sy = Math.max(1e-4, ts.tileScaleY);
4435
+ const u0 = ts.tilePositionX / fr.width;
4436
+ const v0 = ts.tilePositionY / fr.height;
4437
+ const u1 = u0 + ts.width / (fr.width * sx);
4438
+ const v1 = v0 + ts.height / (fr.height * sy);
4439
+ this.pushQuad(m, -ox, -oy, ts.width - ox, ts.height - oy, u0, v0, u1, v1, abgr, 0, ts.blendMode, bg);
4440
+ } else this.pushQuad(m, -ox, -oy, ts.width - ox, ts.height - oy, fr.u0, fr.v0, fr.u1, fr.v1, abgr, 0, ts.blendMode, bg);
4254
4441
  break;
4255
4442
  }
4256
4443
  case "Graphics":
@@ -4359,8 +4546,8 @@ var RendererWebGpu = class extends EventEmitter {
4359
4546
  const sy1 = cm.b * wx1 + cm.d * wy1 + cm.ty;
4360
4547
  const l = Math.max(0, Math.floor(Math.min(sx0, sx1)));
4361
4548
  const t = Math.max(0, Math.floor(Math.min(sy0, sy1)));
4362
- const r = Math.min(this.width, Math.ceil(Math.max(sx0, sx1)));
4363
- const b = Math.min(this.height, Math.ceil(Math.max(sy0, sy1)));
4549
+ const r = Math.min(this.curTargetW, Math.ceil(Math.max(sx0, sx1)));
4550
+ const b = Math.min(this.curTargetH, Math.ceil(Math.max(sy0, sy1)));
4364
4551
  this.scissorStack.push({
4365
4552
  x: l,
4366
4553
  y: t,
@@ -4385,31 +4572,53 @@ var RendererWebGpu = class extends EventEmitter {
4385
4572
  }
4386
4573
  }
4387
4574
  }
4388
- render(displayList, camera) {
4575
+ executePass(targetView, targetFormat, targetW, targetH, displayList, camera, recordMainStats) {
4576
+ const savedState = this.cmds.length > 0 || this.vertCount > 0 || this.sdfCount > 0 ? {
4577
+ vertCount: this.vertCount,
4578
+ indexCount: this.indexCount,
4579
+ cmdStartIndex: this.cmdStartIndex,
4580
+ sdfCount: this.sdfCount,
4581
+ sdfCmdStart: this.sdfCmdStart,
4582
+ curTargetW: this.curTargetW,
4583
+ curTargetH: this.curTargetH,
4584
+ curBlend: this.curBlend,
4585
+ curBindGroup: this.curBindGroup,
4586
+ cmds: this.cmds,
4587
+ scissorStack: this.scissorStack,
4588
+ vertSnap: this.vertCount > 0 ? this.f32.slice(0, this.vertCount * VERT_FLOATS) : null,
4589
+ idxSnap: this.indexCount > 0 ? this.indices.slice(0, this.indexCount) : null,
4590
+ sdfSnap: this.sdfCount > 0 ? this.sdfF32.slice(0, this.sdfCount * SDF_FLOATS) : null
4591
+ } : null;
4592
+ if (savedState) {
4593
+ this.cmds = [];
4594
+ this.scissorStack = [];
4595
+ }
4389
4596
  camera.syncMetrics();
4390
4597
  this.vertCount = 0;
4391
4598
  this.indexCount = 0;
4392
4599
  this.cmdStartIndex = 0;
4393
4600
  this.sdfCount = 0;
4394
4601
  this.sdfCmdStart = 0;
4602
+ this.curTargetW = targetW;
4603
+ this.curTargetH = targetH;
4395
4604
  this.cmds.length = 0;
4396
4605
  this.scissorStack.length = 0;
4397
4606
  this.curBlend = BlendModes.NORMAL;
4398
4607
  this.curBindGroup = null;
4608
+ if (recordMainStats) this.lastStats.culled = 0;
4399
4609
  const list = displayList.sorted();
4400
4610
  const camAlpha = camera.alpha;
4401
4611
  for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, null, camAlpha, 0);
4402
4612
  this.flushAllPending();
4403
4613
  this.device.queue.writeBuffer(this.globalsBuf, 0, new Float32Array([
4404
- this.width,
4405
- this.height,
4614
+ targetW,
4615
+ targetH,
4406
4616
  0,
4407
4617
  0
4408
4618
  ]));
4409
4619
  if (this.vertCount > 0) {
4410
4620
  this.device.queue.writeBuffer(this.vbo, 0, this.f32.subarray(0, this.vertCount * VERT_FLOATS));
4411
- const idxWriteCount = this.indexCount + 1 & -2;
4412
- this.device.queue.writeBuffer(this.ibo, 0, this.indices.subarray(0, idxWriteCount));
4621
+ this.device.queue.writeBuffer(this.ibo, 0, this.indices.subarray(0, this.indexCount));
4413
4622
  }
4414
4623
  if (this.sdfCount > 0) this.device.queue.writeBuffer(this.sdfInstVbo, 0, this.sdfF32.subarray(0, this.sdfCount * SDF_FLOATS));
4415
4624
  const bg = camera.backgroundColor;
@@ -4424,10 +4633,9 @@ var RendererWebGpu = class extends EventEmitter {
4424
4633
  b: bg.b * bg.a,
4425
4634
  a: bg.a
4426
4635
  };
4427
- const view = this.gpuContext ? this.gpuContext.getCurrentTexture().createView() : this.ensureOffscreenTarget();
4428
4636
  const encoder = this.device.createCommandEncoder();
4429
4637
  const pass = encoder.beginRenderPass({ colorAttachments: [{
4430
- view,
4638
+ view: targetView,
4431
4639
  clearValue: clearColor,
4432
4640
  loadOp: "clear",
4433
4641
  storeOp: "store"
@@ -4438,16 +4646,16 @@ var RendererWebGpu = class extends EventEmitter {
4438
4646
  if (cmd.scissor) {
4439
4647
  if (cmd.scissor.w <= 0 || cmd.scissor.h <= 0) continue;
4440
4648
  pass.setScissorRect(cmd.scissor.x, cmd.scissor.y, cmd.scissor.w, cmd.scissor.h);
4441
- } else pass.setScissorRect(0, 0, this.width, this.height);
4649
+ } else pass.setScissorRect(0, 0, targetW, targetH);
4442
4650
  if (cmd.kind === "batch") {
4443
- pass.setPipeline(this.batchPipelines.get(cmd.blend) ?? this.batchPipelines.get(BlendModes.NORMAL));
4651
+ pass.setPipeline(this.getBatchPipeline(targetFormat, cmd.blend));
4444
4652
  pass.setBindGroup(0, cmd.bindGroup);
4445
4653
  pass.setVertexBuffer(0, this.vbo);
4446
- pass.setIndexBuffer(this.ibo, "uint16");
4654
+ pass.setIndexBuffer(this.ibo, "uint32");
4447
4655
  pass.drawIndexed(cmd.indexCount, 1, cmd.firstIndex, 0, 0);
4448
4656
  } else {
4449
4657
  sdfDraws++;
4450
- pass.setPipeline(this.sdfPipelines.get(cmd.blend) ?? this.sdfPipelines.get(BlendModes.NORMAL));
4658
+ pass.setPipeline(this.getSdfPipeline(targetFormat, cmd.blend));
4451
4659
  pass.setBindGroup(0, this.sdfBindGroup);
4452
4660
  pass.setVertexBuffer(0, this.sdfCornerVbo);
4453
4661
  pass.setVertexBuffer(1, this.sdfInstVbo);
@@ -4457,12 +4665,36 @@ var RendererWebGpu = class extends EventEmitter {
4457
4665
  }
4458
4666
  pass.end();
4459
4667
  this.device.queue.submit([encoder.finish()]);
4460
- this.lastStats.drawCalls = this.cmds.length;
4461
- this.lastStats.flushes = this.cmds.length;
4462
- this.lastStats.tris = (this.indexCount / 3 | 0) + this.sdfCount * 2;
4463
- this.lastStats.quads = (this.vertCount >> 2) + this.sdfCount;
4464
- this.lastStats.sdfPanels = this.sdfCount;
4465
- this.lastStats.sdfDraws = sdfDraws;
4668
+ if (recordMainStats) {
4669
+ this.lastStats.drawCalls = this.cmds.length;
4670
+ this.lastStats.flushes = this.cmds.length;
4671
+ this.lastStats.tris = (this.indexCount / 3 | 0) + this.sdfCount * 2;
4672
+ this.lastStats.quads = (this.vertCount >> 2) + this.sdfCount;
4673
+ this.lastStats.sdfPanels = this.sdfCount;
4674
+ this.lastStats.sdfDraws = sdfDraws;
4675
+ }
4676
+ if (savedState) {
4677
+ this.ensureBatchCapacity(savedState.vertCount, savedState.indexCount);
4678
+ this.ensureSdfCapacity(savedState.sdfCount);
4679
+ if (savedState.vertSnap) this.f32.set(savedState.vertSnap, 0);
4680
+ if (savedState.idxSnap) this.indices.set(savedState.idxSnap, 0);
4681
+ if (savedState.sdfSnap) this.sdfF32.set(savedState.sdfSnap, 0);
4682
+ this.vertCount = savedState.vertCount;
4683
+ this.indexCount = savedState.indexCount;
4684
+ this.cmdStartIndex = savedState.cmdStartIndex;
4685
+ this.sdfCount = savedState.sdfCount;
4686
+ this.sdfCmdStart = savedState.sdfCmdStart;
4687
+ this.curTargetW = savedState.curTargetW;
4688
+ this.curTargetH = savedState.curTargetH;
4689
+ this.curBlend = savedState.curBlend;
4690
+ this.curBindGroup = savedState.curBindGroup;
4691
+ this.cmds = savedState.cmds;
4692
+ this.scissorStack = savedState.scissorStack;
4693
+ }
4694
+ }
4695
+ render(displayList, camera) {
4696
+ const view = this.gpuContext ? this.gpuContext.getCurrentTexture().createView() : this.ensureOffscreenTarget();
4697
+ this.executePass(view, this.format, this.width, this.height, displayList, camera, true);
4466
4698
  }
4467
4699
  encodeCopyToStaging() {
4468
4700
  if (!this.offscreenTex) this.ensureOffscreenTarget();
@@ -4555,50 +4787,109 @@ var RendererWebGpu = class extends EventEmitter {
4555
4787
  buf.unmap();
4556
4788
  }
4557
4789
  }
4558
- /** Create an offscreen render target backed by a registered Stage `Texture` (`key`) for WebGPU passes (e.g. `Lighting2DSystem`). */
4790
+ /** Create an offscreen GPU render target backed by a registered Stage `Texture` (`key`) for WebGPU passes (e.g. `Lighting2DSystem`). */
4559
4791
  createRenderTarget(key, width, height, textures, filterMode = FilterMode.LINEAR) {
4560
4792
  const w = Math.max(1, Math.round(width));
4561
4793
  const h = Math.max(1, Math.round(height));
4562
- const doc = this.canvas?.ownerDocument ?? (typeof document !== "undefined" ? document : void 0);
4563
- if (!doc) throw new Error("Stage: ownerDocument unavailable for WebGPU RenderTarget");
4564
- const rtCanvas = doc.createElement("canvas");
4565
- rtCanvas.width = w;
4566
- rtCanvas.height = h;
4567
- const tex = textures.addCanvas(key, rtCanvas);
4794
+ const tex = textures.addImage(key, {
4795
+ width: w,
4796
+ height: h
4797
+ }, {
4798
+ filterMode,
4799
+ isRenderTarget: true
4800
+ });
4568
4801
  if (!tex) throw new Error("Stage: failed to register WebGPU RenderTarget texture");
4569
- tex.setFilter(filterMode);
4802
+ const src = tex.source[0];
4803
+ this.ensureSourceBindGroup(src);
4804
+ const gpuEntry = this.texCache.get(src);
4570
4805
  const rtCamera = new Camera(w, h, true);
4571
- const rtRenderer = new Renderer2D(rtCanvas, w, h);
4806
+ let lazy2D = null;
4807
+ let lazyCanvas = null;
4808
+ let lastUsed2D = false;
4572
4809
  return {
4573
4810
  key,
4574
4811
  width: w,
4575
4812
  height: h,
4576
4813
  texture: tex,
4577
4814
  camera: rtCamera,
4578
- _rtRenderer: rtRenderer,
4815
+ _gpuEntry: gpuEntry,
4816
+ _markGpuRendered: () => {
4817
+ lastUsed2D = false;
4818
+ },
4819
+ _render2DCallback: (cb, cam) => {
4820
+ if (!lazy2D || !lazyCanvas) {
4821
+ const doc = this.canvas?.ownerDocument ?? (typeof document !== "undefined" ? document : void 0);
4822
+ if (!doc) throw new Error("Stage: ownerDocument unavailable for 2D callback RenderTarget");
4823
+ lazyCanvas = doc.createElement("canvas");
4824
+ lazyCanvas.width = w;
4825
+ lazyCanvas.height = h;
4826
+ lazy2D = new Renderer2D(lazyCanvas, w, h);
4827
+ }
4828
+ cam.syncMetrics();
4829
+ cb(lazy2D, cam);
4830
+ this.extractAndUploadTexture(gpuEntry.tex, lazyCanvas, w, h, true);
4831
+ lastUsed2D = true;
4832
+ },
4579
4833
  readPixels: (out) => {
4580
4834
  const byteLen = w * h * 4;
4581
4835
  const dst = out && out.length >= byteLen ? out : new Uint8Array(byteLen);
4582
- const img = rtRenderer.ctx.getImageData(0, 0, w, h);
4583
- dst.set(img.data.subarray(0, byteLen));
4836
+ if (lastUsed2D && lazy2D) {
4837
+ const img = lazy2D.ctx.getImageData(0, 0, w, h);
4838
+ dst.set(img.data.subarray(0, byteLen));
4839
+ return dst;
4840
+ }
4841
+ if (typeof this.device._readBufferSync === "function") {
4842
+ const bytesPerRow = w * 4 + 255 & -256;
4843
+ const totalBuf = bytesPerRow * h;
4844
+ const tmpBuf = this.device.createBuffer({
4845
+ size: totalBuf,
4846
+ usage: 9
4847
+ });
4848
+ const enc = this.device.createCommandEncoder();
4849
+ enc.copyTextureToBuffer({ texture: gpuEntry.tex }, {
4850
+ buffer: tmpBuf,
4851
+ offset: 0,
4852
+ bytesPerRow,
4853
+ rowsPerImage: h
4854
+ }, [
4855
+ w,
4856
+ h,
4857
+ 1
4858
+ ]);
4859
+ this.device.queue.submit([enc.finish()]);
4860
+ if (bytesPerRow === w * 4) this.device._readBufferSync(tmpBuf, dst, byteLen);
4861
+ else {
4862
+ const raw = new Uint8Array(totalBuf);
4863
+ this.device._readBufferSync(tmpBuf, raw, totalBuf);
4864
+ for (let y = 0; y < h; y++) dst.set(raw.subarray(y * bytesPerRow, y * bytesPerRow + w * 4), y * w * 4);
4865
+ }
4866
+ try {
4867
+ tmpBuf.destroy?.();
4868
+ } catch {}
4869
+ return dst;
4870
+ }
4584
4871
  return dst;
4585
4872
  },
4586
4873
  destroy: () => {
4587
- rtRenderer.destroy();
4874
+ lazy2D?.destroy();
4875
+ try {
4876
+ gpuEntry.tex?.destroy?.();
4877
+ } catch {}
4588
4878
  textures.remove(key);
4589
4879
  }
4590
4880
  };
4591
4881
  }
4592
- /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` and mark its texture dirty for WebGPU upload (`camera` defaults to `target.camera`). */
4882
+ /** Render a `DisplayList` natively on the GPU into an offscreen `StageRenderTarget` (zero CPU Canvas2D copy!). */
4593
4883
  renderToTarget(target, content, camera = target.camera) {
4594
- const sub = target._rtRenderer;
4595
- if (!sub) return;
4884
+ const ext = target;
4596
4885
  if (typeof content === "function") {
4597
- camera.syncMetrics();
4598
- content(sub, camera);
4599
- } else sub.render(content, camera);
4600
- const src = target.texture.source[0];
4601
- if (src) src.dirty = true;
4886
+ ext._render2DCallback?.(content, camera);
4887
+ return;
4888
+ }
4889
+ const entry = ext._gpuEntry ?? this.texCache.get(target.texture.source[0]);
4890
+ if (!entry) return;
4891
+ ext._markGpuRendered?.();
4892
+ this.executePass(entry.view, "rgba8unorm", target.width, target.height, content, camera, false);
4602
4893
  }
4603
4894
  destroy() {
4604
4895
  this.removeAllListeners();