gputex 0.4.0 → 0.6.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.
package/dist/index.js CHANGED
@@ -6,7 +6,9 @@ var TextureFormat = {
6
6
  BC7: "BC7",
7
7
  BC7_SRGB: "BC7_SRGB",
8
8
  ASTC_4x4: "ASTC_4x4",
9
- ASTC_4x4_SRGB: "ASTC_4x4_SRGB"
9
+ ASTC_4x4_SRGB: "ASTC_4x4_SRGB",
10
+ ETC2_RGB8: "ETC2_RGB8",
11
+ ETC2_RGB8_SRGB: "ETC2_RGB8_SRGB"
10
12
  };
11
13
  var WebGPUFeature = {
12
14
  BC: "texture-compression-bc",
@@ -24,8 +26,7 @@ var FORMATS_BY_FEATURE = {
24
26
  TextureFormat.BC7_SRGB
25
27
  ],
26
28
  [WebGPUFeature.ASTC]: [TextureFormat.ASTC_4x4, TextureFormat.ASTC_4x4_SRGB],
27
- // ETC2 has no encoder yet — explicit empty keeps exhaustiveness check.
28
- [WebGPUFeature.ETC2]: []
29
+ [WebGPUFeature.ETC2]: [TextureFormat.ETC2_RGB8, TextureFormat.ETC2_RGB8_SRGB]
29
30
  };
30
31
  function detectCapabilities(adapter) {
31
32
  if (!adapter || !adapter.features || typeof adapter.features.has !== "function") {
@@ -38,12 +39,27 @@ function detectCapabilities(adapter) {
38
39
  const supportedFormats = [];
39
40
  if (bc) supportedFormats.push(...FORMATS_BY_FEATURE[WebGPUFeature.BC]);
40
41
  if (astc) supportedFormats.push(...FORMATS_BY_FEATURE[WebGPUFeature.ASTC]);
42
+ if (etc2) supportedFormats.push(...FORMATS_BY_FEATURE[WebGPUFeature.ETC2]);
41
43
  return { bc, astc, etc2, supportedFormats };
42
44
  }
43
45
 
44
46
  // src/workarounds.ts
45
- function uploadSourceTexture(device, srcTex, source, width, height, flipY, useWriteTexture) {
46
- if (useWriteTexture && source instanceof ImageData) {
47
+ function needsWriteTextureWorkaround(adapter) {
48
+ const { vendor, architecture } = adapter.info ?? {};
49
+ return vendor === "img-tec" && architecture === "d-series";
50
+ }
51
+ function uploadSourceTexture(device, srcTex, source, width, height, flipY) {
52
+ if (source instanceof ImageData && !flipY) {
53
+ if (srcTex.format === "rg8unorm") {
54
+ const rgba = source.data;
55
+ const rg = new Uint8Array(width * height * 2);
56
+ for (let i = 0; i < width * height; i++) {
57
+ rg[i * 2] = rgba[i * 4];
58
+ rg[i * 2 + 1] = rgba[i * 4 + 1];
59
+ }
60
+ device.queue.writeTexture({ texture: srcTex }, rg, { bytesPerRow: width * 2 }, [width, height, 1]);
61
+ return;
62
+ }
47
63
  device.queue.writeTexture({ texture: srcTex }, source.data, { bytesPerRow: width * 4 }, [width, height, 1]);
48
64
  } else {
49
65
  device.queue.copyExternalImageToTexture({ source, flipY }, { texture: srcTex }, [
@@ -55,6 +71,7 @@ function uploadSourceTexture(device, srcTex, source, width, height, flipY, useWr
55
71
  }
56
72
 
57
73
  // src/Encoder.ts
74
+ var CHAIN_ALIGN = 256;
58
75
  var Encoder = class {
59
76
  /**
60
77
  * Subclasses set this to the WebGPU feature string the output texture
@@ -97,8 +114,14 @@ var Encoder = class {
97
114
  disableF16;
98
115
  // The single compute pipeline: built from the f16 module when the device
99
116
  // supports shader-f16 and the subclass provides an f16 source, from the
100
- // f32 module otherwise. Both implement the same algorithm.
101
- _pipeline;
117
+ // f32 module otherwise. Both implement the same algorithm. Created with
118
+ // `createComputePipelineAsync` so shader compilation overlaps whatever
119
+ // follows construction (image decode, first upload) instead of stalling
120
+ // the first dispatch; encodes await readiness.
121
+ _pipelineReady;
122
+ // Source-preparation pipeline, when the subclass declares one (ETC2's
123
+ // packed-luma + quadrant-average split). Null for direct-source encoders.
124
+ _prepPipelineReady = null;
102
125
  // -------------------------------------------------------------------- //
103
126
  // Per-encoder GPU resource cache. Creating the source texture, output/
104
127
  // staging buffers and bind group on every encode costs ~1ms of host time
@@ -112,12 +135,41 @@ var Encoder = class {
112
135
  _cachedSrcTex = null;
113
136
  _cachedSrcW = 0;
114
137
  _cachedSrcH = 0;
138
+ // Upload memoisation: the ImageBitmap whose pixels the cached source
139
+ // texture currently holds. ImageBitmaps are immutable, so encoding the
140
+ // same bitmap again (benchmark loops, quality-ladder re-encodes, format
141
+ // A/B) can skip the copyExternalImageToTexture entirely — at 4096² that
142
+ // upload is ~9 ms, dominating the whole encode. Mutable sources
143
+ // (ImageData, canvases, video) are never memoised.
144
+ _cachedSrcSource = null;
145
+ _cachedSrcFlipY = false;
115
146
  _cachedDst = null;
116
147
  _cachedStaging = null;
117
148
  _cachedParams = null;
118
149
  _lastParams = null;
119
150
  _cachedBindGroup = null;
151
+ _cachedPrepPlanes = null;
152
+ _cachedPrepBindGroup = null;
120
153
  _resourcesBusy = false;
154
+ /** Set when the active shader declares a @binding(3) sampler (the BC5
155
+ * kernels read texels through textureGather + clamp-to-edge). */
156
+ _usesSampler = false;
157
+ _sampler = null;
158
+ // Mip-chain cache — the `encodeMipChainToBytes()` counterpart of the
159
+ // single-shot cache above: per-level source textures + bind groups keyed
160
+ // on the exact level-size signature, one params buffer holding every
161
+ // level's uniforms at 256-byte offsets, grow-only output/staging buffers.
162
+ // Pays off when consecutive chains share dimensions (bulk-loading
163
+ // same-sized textures through `compressTexture()`).
164
+ _chainSig = null;
165
+ _chainTextures = [];
166
+ _chainPrepPlanes = [];
167
+ _chainPrepBindGroups = [];
168
+ _chainParams = null;
169
+ _chainBindGroups = [];
170
+ _chainDst = null;
171
+ _chainStaging = null;
172
+ _chainBusy = false;
121
173
  constructor({ device, adapter, ownsDevice = false, disableF16 = false }) {
122
174
  this.device = device;
123
175
  this.adapter = adapter;
@@ -128,28 +180,70 @@ var Encoder = class {
128
180
  _buildPipeline() {
129
181
  const device = this.device;
130
182
  const useF16 = this._useF16;
183
+ const code = useF16 ? this.wgslSourceFastF16() : this.wgslSource();
184
+ this._usesSampler = /@binding\(3\)\s+var\s+\w+\s*:\s*sampler\s*;/.test(code);
131
185
  const module = device.createShaderModule({
132
186
  label: `${this.label}-encoder${useF16 ? "-f16" : ""}`,
133
- code: useF16 ? this.wgslSourceFastF16() : this.wgslSource()
187
+ code
134
188
  });
135
- this._pipeline = device.createComputePipeline({
189
+ const constants = this.pipelineConstants();
190
+ this._pipelineReady = device.createComputePipelineAsync({
136
191
  label: `${this.label}-encoder-pipeline${useF16 ? "-f16" : ""}`,
137
192
  layout: "auto",
138
- compute: { module, entryPoint: "encode" }
193
+ compute: { module, entryPoint: "encode", ...constants ? { constants } : {} }
194
+ });
195
+ const prepCode = this.wgslPrepSource();
196
+ if (prepCode) {
197
+ const prepModule = device.createShaderModule({ label: `${this.label}-prep`, code: prepCode });
198
+ this._prepPipelineReady = device.createComputePipelineAsync({
199
+ label: `${this.label}-prep-pipeline`,
200
+ layout: "auto",
201
+ compute: { module: prepModule, entryPoint: "encode" }
202
+ });
203
+ this._prepPipelineReady.catch(() => {
204
+ });
205
+ }
206
+ this._pipelineReady.catch(() => {
139
207
  });
140
208
  }
141
209
  destroy() {
142
210
  this._cachedSrcTex?.destroy();
211
+ if (this._cachedPrepPlanes) for (const t of this._cachedPrepPlanes) t.destroy();
212
+ this._cachedPrepPlanes = null;
213
+ this._cachedPrepBindGroup = null;
143
214
  this._cachedDst?.destroy();
144
215
  this._cachedStaging?.destroy();
145
216
  this._cachedParams?.destroy();
146
217
  this._cachedSrcTex = null;
218
+ this._cachedSrcSource = null;
147
219
  this._cachedDst = null;
148
220
  this._cachedStaging = null;
149
221
  this._cachedParams = null;
150
222
  this._cachedBindGroup = null;
223
+ for (const tex of this._chainTextures) tex.destroy();
224
+ this._chainTextures = [];
225
+ for (const planes of this._chainPrepPlanes) for (const t of planes) t.destroy();
226
+ this._chainPrepPlanes = [];
227
+ this._chainPrepBindGroups = [];
228
+ this._chainParams?.destroy();
229
+ this._chainDst?.destroy();
230
+ this._chainStaging?.destroy();
231
+ this._chainParams = null;
232
+ this._chainDst = null;
233
+ this._chainStaging = null;
234
+ this._chainBindGroups = [];
235
+ this._chainSig = null;
151
236
  if (this.ownsDevice) this.device.destroy();
152
237
  }
238
+ /**
239
+ * Pipeline-creation override constants for the active shader, or
240
+ * undefined for none. Subclasses expose quality/behaviour toggles this
241
+ * way so the OFF state is dead-coded by the shader compiler instead of
242
+ * branched at runtime.
243
+ */
244
+ pipelineConstants() {
245
+ return void 0;
246
+ }
153
247
  /** WGSL `@workgroup_size` dimensions. Default 8×8×1. */
154
248
  get workgroupSize() {
155
249
  return [8, 8, 1];
@@ -158,6 +252,18 @@ var Encoder = class {
158
252
  get supportsSrgb() {
159
253
  return true;
160
254
  }
255
+ /**
256
+ * Format of the single-shot source texture the encode pass samples.
257
+ * Encoders that read only a channel subset can narrow it to cut DRAM
258
+ * traffic on the bandwidth-bound compute pass (BC5 reads rg8unorm — half
259
+ * the bytes of rgba8). Must be valid as a copyExternalImageToTexture
260
+ * destination and renderable. Chain encodes keep rgba8unorm regardless
261
+ * (their inputs are RGBA mip levels / texture views); the WGSL is
262
+ * format-agnostic (`texture_2d<f32>`), so mixing is byte-identical.
263
+ */
264
+ get srcTextureFormat() {
265
+ return "rgba8unorm";
266
+ }
161
267
  /**
162
268
  * Optional f16 WGSL variant. Used only when the device reports the
163
269
  * `shader-f16` feature; the format's f32 `wgslSource()` is the automatic
@@ -170,6 +276,53 @@ var Encoder = class {
170
276
  get _useF16() {
171
277
  return !this.disableF16 && this.wgslSourceFastF16() !== null && this.device.features.has("shader-f16");
172
278
  }
279
+ // ------------------------------------------------------------------ //
280
+ // Optional source-preparation pass (null/empty for direct encoders). //
281
+ // A subclass returning a prep shader gets a two-pass encode: the prep
282
+ // pass reads the RGBA8 source (binding 0) and writes the prepared
283
+ // planes as storage textures (plane 0 at binding 1, plane 1 at
284
+ // binding 3, params at binding 2); the encode pass then reads plane 0
285
+ // at binding 0 and plane 1 at binding 3 instead of the source.
286
+ // ------------------------------------------------------------------ //
287
+ /** WGSL for the preparation pass, or null when the encoder reads the
288
+ * RGBA8 source directly. */
289
+ wgslPrepSource() {
290
+ return null;
291
+ }
292
+ /** Formats and sizes of the prepared planes for a padded source size. */
293
+ prepPlanes(paddedWidth, paddedHeight) {
294
+ void paddedWidth;
295
+ void paddedHeight;
296
+ return [];
297
+ }
298
+ /** Workgroup counts for the prep dispatch. */
299
+ prepDispatch(blocksX, blocksY) {
300
+ return [blocksX, blocksY];
301
+ }
302
+ /** Create the prepared-plane textures for one padded source size. */
303
+ _createPrepPlanes(paddedWidth, paddedHeight) {
304
+ return this.prepPlanes(paddedWidth, paddedHeight).map(
305
+ (p, i) => this.device.createTexture({
306
+ label: `${this.label}-prep-${i}`,
307
+ size: [p.width, p.height, 1],
308
+ format: p.format,
309
+ usage: GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.TEXTURE_BINDING
310
+ })
311
+ );
312
+ }
313
+ /** Bind group for one prep dispatch. */
314
+ _createPrepBindGroup(prepPipeline, srcView, planes, params, paramsOffset = 0) {
315
+ return this.device.createBindGroup({
316
+ label: `${this.label}-prep-bg`,
317
+ layout: prepPipeline.getBindGroupLayout(0),
318
+ entries: [
319
+ { binding: 0, resource: srcView },
320
+ { binding: 1, resource: planes[0].createView() },
321
+ { binding: 2, resource: { buffer: params, offset: paramsOffset, size: 16 } },
322
+ { binding: 3, resource: planes[1].createView() }
323
+ ]
324
+ });
325
+ }
173
326
  /**
174
327
  * True if the device reports the feature the output texture needs.
175
328
  * The encoder itself only writes to a storage buffer, so this is about
@@ -202,12 +355,15 @@ var Encoder = class {
202
355
  const blocksY = paddedHeight >> 2;
203
356
  const blockCount = blocksX * blocksY;
204
357
  const outByteLen = blockCount * this.bytesPerBlock;
358
+ const pipeline = await this._pipelineReady;
359
+ const prepPipeline = this._prepPipelineReady ? await this._prepPipelineReady : null;
205
360
  const useCache = !this._resourcesBusy;
206
361
  if (useCache) this._resourcesBusy = true;
207
362
  let srcTex;
208
363
  let dstBuffer;
209
364
  let paramsBuffer;
210
365
  let staging;
366
+ let transientPrepPlanes = null;
211
367
  try {
212
368
  let srcTexIsNew = true;
213
369
  if (useCache && this._cachedSrcTex && this._cachedSrcW === paddedWidth && this._cachedSrcH === paddedHeight) {
@@ -217,7 +373,7 @@ var Encoder = class {
217
373
  srcTex = device.createTexture({
218
374
  label: `${this.label}-src`,
219
375
  size: [paddedWidth, paddedHeight, 1],
220
- format: "rgba8unorm",
376
+ format: this.srcTextureFormat,
221
377
  // RENDER_ATTACHMENT is required by copyExternalImageToTexture
222
378
  // (internally a blit) even though we never render into this texture.
223
379
  usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.RENDER_ATTACHMENT
@@ -229,7 +385,31 @@ var Encoder = class {
229
385
  this._cachedSrcH = paddedHeight;
230
386
  }
231
387
  }
232
- uploadSourceTexture(device, srcTex, source, width, height, flipY, source instanceof ImageData);
388
+ let prepPlanes = null;
389
+ let prepPlanesNew = false;
390
+ if (prepPipeline) {
391
+ if (useCache && !srcTexIsNew && this._cachedPrepPlanes) {
392
+ prepPlanes = this._cachedPrepPlanes;
393
+ } else {
394
+ prepPlanes = this._createPrepPlanes(paddedWidth, paddedHeight);
395
+ prepPlanesNew = true;
396
+ if (useCache) {
397
+ if (this._cachedPrepPlanes) for (const t of this._cachedPrepPlanes) t.destroy();
398
+ this._cachedPrepPlanes = prepPlanes;
399
+ this._cachedPrepBindGroup = null;
400
+ } else {
401
+ transientPrepPlanes = prepPlanes;
402
+ }
403
+ }
404
+ }
405
+ const uploadSkippable = !srcTexIsNew && source instanceof ImageBitmap && this._cachedSrcSource === source && this._cachedSrcFlipY === flipY;
406
+ if (!uploadSkippable) {
407
+ uploadSourceTexture(device, srcTex, source, width, height, flipY);
408
+ }
409
+ if (useCache) {
410
+ this._cachedSrcSource = source instanceof ImageBitmap ? source : null;
411
+ this._cachedSrcFlipY = flipY;
412
+ }
233
413
  let dstIsNew = true;
234
414
  if (useCache && this._cachedDst && this._cachedDst.size >= outByteLen) {
235
415
  dstBuffer = this._cachedDst;
@@ -281,63 +461,74 @@ var Encoder = class {
281
461
  });
282
462
  device.queue.writeBuffer(paramsBuffer, 0, new Uint32Array([blocksX, blocksY, width, height]));
283
463
  }
284
- const pipeline = this._pipeline;
285
- if (useCache && (srcTexIsNew || dstIsNew)) this._cachedBindGroup = null;
464
+ if (useCache && (srcTexIsNew || dstIsNew || prepPlanesNew)) this._cachedBindGroup = null;
286
465
  let bindGroup = useCache ? this._cachedBindGroup : null;
287
466
  if (!bindGroup) {
467
+ const entries2 = [
468
+ { binding: 0, resource: prepPlanes ? prepPlanes[0].createView() : srcTex.createView() },
469
+ { binding: 1, resource: { buffer: dstBuffer } },
470
+ { binding: 2, resource: { buffer: paramsBuffer } }
471
+ ];
472
+ if (prepPlanes) {
473
+ entries2.push({ binding: 3, resource: prepPlanes[1].createView() });
474
+ } else if (this._usesSampler) {
475
+ this._sampler ??= device.createSampler({
476
+ label: `${this.label}-clamp-sampler`,
477
+ addressModeU: "clamp-to-edge",
478
+ addressModeV: "clamp-to-edge"
479
+ });
480
+ entries2.push({ binding: 3, resource: this._sampler });
481
+ }
288
482
  bindGroup = device.createBindGroup({
289
483
  label: `${this.label}-bg`,
290
484
  layout: pipeline.getBindGroupLayout(0),
291
- entries: [
292
- { binding: 0, resource: srcTex.createView() },
293
- { binding: 1, resource: { buffer: dstBuffer } },
294
- { binding: 2, resource: { buffer: paramsBuffer } }
295
- ]
485
+ entries: entries2
296
486
  });
297
487
  if (useCache) this._cachedBindGroup = bindGroup;
298
488
  }
299
- const useTimestamps = withGpuTime && device.features.has("timestamp-query");
300
- const querySet = useTimestamps ? device.createQuerySet({ type: "timestamp", count: 2 }) : null;
301
- const queryBuffer = useTimestamps ? device.createBuffer({
302
- label: `${this.label}-ts-resolve`,
303
- size: 16,
304
- usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC
305
- }) : null;
489
+ let prepBindGroup = useCache && !prepPlanesNew && !srcTexIsNew ? this._cachedPrepBindGroup : null;
490
+ if (prepPipeline && prepPlanes && !prepBindGroup) {
491
+ prepBindGroup = this._createPrepBindGroup(prepPipeline, srcTex.createView(), prepPlanes, paramsBuffer);
492
+ if (useCache) this._cachedPrepBindGroup = prepBindGroup;
493
+ }
494
+ const timing = withGpuTime ? this._createTiming() : null;
306
495
  const [wgX, wgY] = this.workgroupSize;
307
496
  const t0 = performance.now();
308
497
  const enc = device.createCommandEncoder({ label: `${this.label}-encode` });
498
+ if (prepPipeline && prepBindGroup) {
499
+ const prepPass = enc.beginComputePass(
500
+ timing ? { timestampWrites: { querySet: timing.querySet, beginningOfPassWriteIndex: 0 } } : void 0
501
+ );
502
+ prepPass.setPipeline(prepPipeline);
503
+ prepPass.setBindGroup(0, prepBindGroup);
504
+ const [px, py] = this.prepDispatch(blocksX, blocksY);
505
+ prepPass.dispatchWorkgroups(Math.ceil(px / wgX), Math.ceil(py / wgY), 1);
506
+ prepPass.end();
507
+ }
309
508
  const pass = enc.beginComputePass(
310
- querySet ? { timestampWrites: { querySet, beginningOfPassWriteIndex: 0, endOfPassWriteIndex: 1 } } : void 0
509
+ timing ? {
510
+ timestampWrites: {
511
+ querySet: timing.querySet,
512
+ ...prepPipeline ? {} : { beginningOfPassWriteIndex: 0 },
513
+ endOfPassWriteIndex: 1
514
+ }
515
+ } : void 0
311
516
  );
312
517
  pass.setPipeline(pipeline);
313
518
  pass.setBindGroup(0, bindGroup);
314
519
  pass.dispatchWorkgroups(Math.ceil(blocksX / wgX), Math.ceil(blocksY / wgY), 1);
315
520
  pass.end();
316
- if (querySet && queryBuffer) enc.resolveQuerySet(querySet, 0, 2, queryBuffer, 0);
317
521
  enc.copyBufferToBuffer(dstBuffer, 0, staging, 0, outByteLen);
318
- const tsStaging = querySet && queryBuffer ? device.createBuffer({
319
- label: `${this.label}-ts-staging`,
320
- size: 16,
321
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
322
- }) : null;
323
- if (tsStaging && queryBuffer) enc.copyBufferToBuffer(queryBuffer, 0, tsStaging, 0, 16);
522
+ if (timing) {
523
+ enc.resolveQuerySet(timing.querySet, 0, 2, timing.resolve, 0);
524
+ enc.copyBufferToBuffer(timing.resolve, 0, timing.staging, 0, 16);
525
+ }
324
526
  device.queue.submit([enc.finish()]);
325
527
  await staging.mapAsync(GPUMapMode.READ, 0, outByteLen);
326
528
  const data = new Uint8Array(staging.getMappedRange(0, outByteLen).slice(0));
327
529
  staging.unmap();
328
530
  const encodeMs = performance.now() - t0;
329
- let gpuMs;
330
- if (tsStaging) {
331
- await tsStaging.mapAsync(GPUMapMode.READ);
332
- const [begin, end] = new BigUint64Array(tsStaging.getMappedRange().slice(0));
333
- tsStaging.unmap();
334
- tsStaging.destroy();
335
- if (end !== void 0 && begin !== void 0 && end > begin) {
336
- gpuMs = Number(end - begin) / 1e6;
337
- }
338
- }
339
- querySet?.destroy();
340
- queryBuffer?.destroy();
531
+ const gpuMs = timing ? await this._readTimingMs(timing) : void 0;
341
532
  return { width, height, paddedWidth, paddedHeight, data, encodeMs, gpuMs };
342
533
  } finally {
343
534
  if (useCache) {
@@ -347,13 +538,441 @@ var Encoder = class {
347
538
  dstBuffer?.destroy();
348
539
  staging?.destroy();
349
540
  paramsBuffer?.destroy();
541
+ if (transientPrepPlanes) for (const t of transientPrepPlanes) t.destroy();
542
+ }
543
+ }
544
+ }
545
+ // ------------------------------------------------------------------ //
546
+ // Whole-chain encode — every level in one submission. //
547
+ // ------------------------------------------------------------------ //
548
+ /**
549
+ * Encode a whole mip chain in ONE GPU submission. A per-level
550
+ * `encodeToBytes()` loop costs a full CPU↔GPU round trip per level (a
551
+ * 1024² chain is 11 levels → 11 `mapAsync` waits with the GPU idle in
552
+ * between); this path uploads every level, records one dispatch per level
553
+ * into a single compute pass, copies all outputs into one staging buffer
554
+ * and maps it once.
555
+ *
556
+ * `levels` are raw RGBA8 pixels in base-to-tail order; sizes don't have to
557
+ * halve level-to-level (each level is padded and clamped independently,
558
+ * exactly like `encodeToBytes`). Uploads use `writeTexture` — the direct
559
+ * raw-bytes path, which also sidesteps the broken
560
+ * `copyExternalImageToTexture` devices (see workarounds.ts). There is no
561
+ * flip option: bake any vertical flip into level 0 before generating the
562
+ * chain, as `compressTexture()` does.
563
+ */
564
+ async encodeMipChainToBytes(levels, { withGpuTime = false } = {}) {
565
+ const device = this.device;
566
+ if (levels.length === 0) {
567
+ throw new Error(`${this.label}Encoder: encodeMipChainToBytes needs at least one level`);
568
+ }
569
+ const pipeline = await this._pipelineReady;
570
+ const prepPipeline = this._prepPipelineReady ? await this._prepPipelineReady : null;
571
+ const t0 = performance.now();
572
+ levels.forEach((level, i) => {
573
+ if (!level.width || !level.height) {
574
+ throw new Error(`${this.label}Encoder: mip level ${i} has no dimensions`);
575
+ }
576
+ if (level.data.length < level.width * level.height * 4) {
577
+ throw new Error(
578
+ `${this.label}Encoder: mip level ${i} has ${level.data.length} bytes, expected ${level.width * level.height * 4}`
579
+ );
580
+ }
581
+ });
582
+ const { geoms, byteSpan } = this._chainGeometry(levels);
583
+ const sig = geoms.map((g) => `${g.width}x${g.height}`).join();
584
+ const useCache = !this._chainBusy;
585
+ if (useCache) this._chainBusy = true;
586
+ let textures;
587
+ let prepPlaneSets;
588
+ let prepBindGroups;
589
+ let params;
590
+ let bindGroups;
591
+ let dst;
592
+ let staging;
593
+ let transientLevelSet = false;
594
+ try {
595
+ let dstIsNew = true;
596
+ if (useCache && this._chainDst && this._chainDst.size >= byteSpan) {
597
+ dst = this._chainDst;
598
+ dstIsNew = false;
599
+ } else {
600
+ dst = device.createBuffer({
601
+ label: `${this.label}-chain-dst`,
602
+ size: byteSpan,
603
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
604
+ });
605
+ if (useCache) {
606
+ this._chainDst?.destroy();
607
+ this._chainDst = dst;
608
+ }
609
+ }
610
+ if (useCache && !dstIsNew && this._chainSig === sig && this._chainParams) {
611
+ textures = this._chainTextures;
612
+ params = this._chainParams;
613
+ bindGroups = this._chainBindGroups;
614
+ prepPlaneSets = this._chainPrepPlanes;
615
+ prepBindGroups = this._chainPrepBindGroups;
616
+ } else {
617
+ const texs = geoms.map(
618
+ (g, i) => device.createTexture({
619
+ label: `${this.label}-chain-src-${i}`,
620
+ size: [g.paddedWidth, g.paddedHeight, 1],
621
+ format: "rgba8unorm",
622
+ // No RENDER_ATTACHMENT: writeTexture is a plain copy, not the
623
+ // copyExternalImageToTexture blit.
624
+ usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING
625
+ })
626
+ );
627
+ textures = texs;
628
+ params = device.createBuffer({
629
+ label: `${this.label}-chain-params`,
630
+ size: geoms.length * CHAIN_ALIGN,
631
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
632
+ });
633
+ const paramsData = new Uint32Array(geoms.length * CHAIN_ALIGN / 4);
634
+ geoms.forEach((g, i) => {
635
+ paramsData.set([g.blocksX, g.blocksY, g.width, g.height], i * CHAIN_ALIGN / 4);
636
+ });
637
+ device.queue.writeBuffer(params, 0, paramsData);
638
+ if (this._usesSampler) {
639
+ this._sampler ??= device.createSampler({
640
+ label: `${this.label}-clamp-sampler`,
641
+ addressModeU: "clamp-to-edge",
642
+ addressModeV: "clamp-to-edge"
643
+ });
644
+ }
645
+ const dstBuf = dst;
646
+ const paramsBuf = params;
647
+ const planeSets = prepPipeline ? geoms.map((g) => this._createPrepPlanes(g.paddedWidth, g.paddedHeight)) : [];
648
+ prepPlaneSets = planeSets;
649
+ prepBindGroups = prepPipeline ? geoms.map(
650
+ (g, i) => this._createPrepBindGroup(prepPipeline, texs[i].createView(), planeSets[i], paramsBuf, i * CHAIN_ALIGN)
651
+ ) : [];
652
+ bindGroups = geoms.map((g, i) => {
653
+ const entries2 = [
654
+ {
655
+ binding: 0,
656
+ resource: prepPipeline ? planeSets[i][0].createView() : texs[i].createView()
657
+ },
658
+ { binding: 1, resource: { buffer: dstBuf, offset: g.dstOffset, size: g.byteLen } },
659
+ { binding: 2, resource: { buffer: paramsBuf, offset: i * CHAIN_ALIGN, size: 16 } }
660
+ ];
661
+ if (prepPipeline) {
662
+ entries2.push({ binding: 3, resource: planeSets[i][1].createView() });
663
+ } else if (this._usesSampler) {
664
+ entries2.push({ binding: 3, resource: this._sampler });
665
+ }
666
+ return device.createBindGroup({
667
+ label: `${this.label}-chain-bg-${i}`,
668
+ layout: pipeline.getBindGroupLayout(0),
669
+ entries: entries2
670
+ });
671
+ });
672
+ if (useCache) {
673
+ for (const tex of this._chainTextures) tex.destroy();
674
+ for (const planes of this._chainPrepPlanes) for (const t of planes) t.destroy();
675
+ this._chainParams?.destroy();
676
+ this._chainTextures = textures;
677
+ this._chainPrepPlanes = planeSets;
678
+ this._chainPrepBindGroups = prepBindGroups;
679
+ this._chainParams = params;
680
+ this._chainBindGroups = bindGroups;
681
+ this._chainSig = sig;
682
+ } else {
683
+ transientLevelSet = true;
684
+ }
685
+ }
686
+ if (useCache && this._chainStaging && this._chainStaging.size >= byteSpan) {
687
+ staging = this._chainStaging;
688
+ } else {
689
+ staging = device.createBuffer({
690
+ label: `${this.label}-chain-staging`,
691
+ size: byteSpan,
692
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
693
+ });
694
+ if (useCache) {
695
+ this._chainStaging?.destroy();
696
+ this._chainStaging = staging;
697
+ }
698
+ }
699
+ for (let i = 0; i < levels.length; i++) {
700
+ const level = levels[i];
701
+ device.queue.writeTexture({ texture: textures[i] }, level.data, { bytesPerRow: level.width * 4 }, [
702
+ level.width,
703
+ level.height,
704
+ 1
705
+ ]);
706
+ }
707
+ return await this._submitChainAndRead(
708
+ pipeline,
709
+ geoms,
710
+ byteSpan,
711
+ bindGroups,
712
+ dst,
713
+ staging,
714
+ withGpuTime,
715
+ t0,
716
+ prepPipeline && prepBindGroups ? { pipeline: prepPipeline, bindGroups: prepBindGroups } : null
717
+ );
718
+ } finally {
719
+ if (useCache) {
720
+ this._chainBusy = false;
721
+ } else {
722
+ dst?.destroy();
723
+ staging?.destroy();
724
+ }
725
+ if (transientLevelSet) {
726
+ if (textures) for (const tex of textures) tex.destroy();
727
+ if (prepPlaneSets) for (const planes of prepPlaneSets) for (const t of planes) t.destroy();
728
+ params?.destroy();
729
+ }
730
+ }
731
+ }
732
+ /**
733
+ * Encode every mip level of a GPU-resident texture in one submission —
734
+ * the zero-CPU-pixels counterpart of `encodeMipChainToBytes()`. Pair it
735
+ * with `generateGpuMipChain()` (gpuMipgen.ts): upload the image once,
736
+ * box-filter the chain on the GPU, then encode straight from the
737
+ * texture's mip views. Pixels never transit the CPU between the source
738
+ * image and the compressed-bytes readback.
739
+ *
740
+ * `srcTex` must be `rgba8unorm` with TEXTURE_BINDING usage; level 0's
741
+ * dimensions are taken from the texture and lower levels follow the
742
+ * standard floor-halving chain. Encoded output is identical to feeding
743
+ * the equivalent CPU chain to `encodeMipChainToBytes()`.
744
+ */
745
+ async encodeMipChainFromTexture(srcTex, { withGpuTime = false } = {}) {
746
+ const device = this.device;
747
+ const pipeline = await this._pipelineReady;
748
+ const prepPipeline = this._prepPipelineReady ? await this._prepPipelineReady : null;
749
+ const t0 = performance.now();
750
+ const dims = [];
751
+ for (let i = 0; i < srcTex.mipLevelCount; i++) {
752
+ dims.push({ width: Math.max(1, srcTex.width >> i), height: Math.max(1, srcTex.height >> i) });
753
+ }
754
+ const { geoms, byteSpan } = this._chainGeometry(dims);
755
+ const useCache = !this._chainBusy;
756
+ if (useCache) this._chainBusy = true;
757
+ let dst;
758
+ let staging;
759
+ let params;
760
+ let planeSets = null;
761
+ try {
762
+ if (useCache && this._chainDst && this._chainDst.size >= byteSpan) {
763
+ dst = this._chainDst;
764
+ } else {
765
+ dst = device.createBuffer({
766
+ label: `${this.label}-chain-dst`,
767
+ size: byteSpan,
768
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
769
+ });
770
+ if (useCache) {
771
+ this._chainDst?.destroy();
772
+ this._chainDst = dst;
773
+ this._chainSig = null;
774
+ }
775
+ }
776
+ if (useCache && this._chainStaging && this._chainStaging.size >= byteSpan) {
777
+ staging = this._chainStaging;
778
+ } else {
779
+ staging = device.createBuffer({
780
+ label: `${this.label}-chain-staging`,
781
+ size: byteSpan,
782
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
783
+ });
784
+ if (useCache) {
785
+ this._chainStaging?.destroy();
786
+ this._chainStaging = staging;
787
+ }
788
+ }
789
+ params = device.createBuffer({
790
+ label: `${this.label}-chain-params`,
791
+ size: geoms.length * CHAIN_ALIGN,
792
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
793
+ });
794
+ const paramsData = new Uint32Array(geoms.length * CHAIN_ALIGN / 4);
795
+ geoms.forEach((g, i) => {
796
+ paramsData.set([g.blocksX, g.blocksY, g.width, g.height], i * CHAIN_ALIGN / 4);
797
+ });
798
+ device.queue.writeBuffer(params, 0, paramsData);
799
+ if (this._usesSampler) {
800
+ this._sampler ??= device.createSampler({
801
+ label: `${this.label}-clamp-sampler`,
802
+ addressModeU: "clamp-to-edge",
803
+ addressModeV: "clamp-to-edge"
804
+ });
805
+ }
806
+ const dstBuf = dst;
807
+ const paramsBuf = params;
808
+ planeSets = prepPipeline ? geoms.map((g) => this._createPrepPlanes(g.paddedWidth, g.paddedHeight)) : null;
809
+ const planes = planeSets;
810
+ const prepBindGroups = prepPipeline && planes ? geoms.map(
811
+ (_, i) => this._createPrepBindGroup(
812
+ prepPipeline,
813
+ srcTex.createView({ baseMipLevel: i, mipLevelCount: 1 }),
814
+ planes[i],
815
+ paramsBuf,
816
+ i * CHAIN_ALIGN
817
+ )
818
+ ) : null;
819
+ const bindGroups = geoms.map((g, i) => {
820
+ const entries2 = [
821
+ {
822
+ binding: 0,
823
+ resource: planes ? planes[i][0].createView() : srcTex.createView({ baseMipLevel: i, mipLevelCount: 1 })
824
+ },
825
+ { binding: 1, resource: { buffer: dstBuf, offset: g.dstOffset, size: g.byteLen } },
826
+ { binding: 2, resource: { buffer: paramsBuf, offset: i * CHAIN_ALIGN, size: 16 } }
827
+ ];
828
+ if (planes) {
829
+ entries2.push({ binding: 3, resource: planes[i][1].createView() });
830
+ } else if (this._usesSampler) {
831
+ entries2.push({ binding: 3, resource: this._sampler });
832
+ }
833
+ return device.createBindGroup({
834
+ label: `${this.label}-chain-bg-${i}`,
835
+ layout: pipeline.getBindGroupLayout(0),
836
+ entries: entries2
837
+ });
838
+ });
839
+ return await this._submitChainAndRead(
840
+ pipeline,
841
+ geoms,
842
+ byteSpan,
843
+ bindGroups,
844
+ dst,
845
+ staging,
846
+ withGpuTime,
847
+ t0,
848
+ prepPipeline && prepBindGroups ? { pipeline: prepPipeline, bindGroups: prepBindGroups } : null
849
+ );
850
+ } finally {
851
+ if (useCache) {
852
+ this._chainBusy = false;
853
+ } else {
854
+ dst?.destroy();
855
+ staging?.destroy();
350
856
  }
857
+ params?.destroy();
858
+ if (planeSets) for (const planes of planeSets) for (const t of planes) t.destroy();
859
+ }
860
+ }
861
+ /** Block-grid geometry + packed output offsets for a chain of levels.
862
+ * `byteSpan` is both the dst buffer size and the readback copy size (a
863
+ * multiple of 4: byteLen is a multiple of bytesPerBlock ≥ 8, offsets are
864
+ * CHAIN_ALIGN-ed). */
865
+ _chainGeometry(dims) {
866
+ let dstCursor = 0;
867
+ const geoms = dims.map(({ width, height }) => {
868
+ const paddedWidth = width + 3 & ~3;
869
+ const paddedHeight = height + 3 & ~3;
870
+ const blocksX = paddedWidth >> 2;
871
+ const blocksY = paddedHeight >> 2;
872
+ const byteLen = blocksX * blocksY * this.bytesPerBlock;
873
+ const dstOffset = dstCursor;
874
+ dstCursor = Math.ceil((dstCursor + byteLen) / CHAIN_ALIGN) * CHAIN_ALIGN;
875
+ return { width, height, paddedWidth, paddedHeight, blocksX, blocksY, byteLen, dstOffset };
876
+ });
877
+ const last = geoms[geoms.length - 1];
878
+ return { geoms, byteSpan: last.dstOffset + last.byteLen };
879
+ }
880
+ /** Shared chain-encode tail: one compute pass with a dispatch per level,
881
+ * one submit, one staging readback sliced into per-level byte arrays. */
882
+ async _submitChainAndRead(pipeline, geoms, byteSpan, bindGroups, dst, staging, withGpuTime, t0, prep = null) {
883
+ const device = this.device;
884
+ const timing = withGpuTime ? this._createTiming() : null;
885
+ const [wgX, wgY] = this.workgroupSize;
886
+ const enc = device.createCommandEncoder({ label: `${this.label}-encode-chain` });
887
+ if (prep) {
888
+ const prepPass = enc.beginComputePass(
889
+ timing ? { timestampWrites: { querySet: timing.querySet, beginningOfPassWriteIndex: 0 } } : void 0
890
+ );
891
+ prepPass.setPipeline(prep.pipeline);
892
+ for (let i = 0; i < geoms.length; i++) {
893
+ const g = geoms[i];
894
+ prepPass.setBindGroup(0, prep.bindGroups[i]);
895
+ const [px, py] = this.prepDispatch(g.blocksX, g.blocksY);
896
+ prepPass.dispatchWorkgroups(Math.ceil(px / wgX), Math.ceil(py / wgY), 1);
897
+ }
898
+ prepPass.end();
899
+ }
900
+ const pass = enc.beginComputePass(
901
+ timing ? {
902
+ timestampWrites: {
903
+ querySet: timing.querySet,
904
+ ...prep ? {} : { beginningOfPassWriteIndex: 0 },
905
+ endOfPassWriteIndex: 1
906
+ }
907
+ } : void 0
908
+ );
909
+ pass.setPipeline(pipeline);
910
+ for (let i = 0; i < geoms.length; i++) {
911
+ const g = geoms[i];
912
+ pass.setBindGroup(0, bindGroups[i]);
913
+ pass.dispatchWorkgroups(Math.ceil(g.blocksX / wgX), Math.ceil(g.blocksY / wgY), 1);
914
+ }
915
+ pass.end();
916
+ enc.copyBufferToBuffer(dst, 0, staging, 0, byteSpan);
917
+ if (timing) {
918
+ enc.resolveQuerySet(timing.querySet, 0, 2, timing.resolve, 0);
919
+ enc.copyBufferToBuffer(timing.resolve, 0, timing.staging, 0, 16);
920
+ }
921
+ device.queue.submit([enc.finish()]);
922
+ await staging.mapAsync(GPUMapMode.READ, 0, byteSpan);
923
+ const mapped = staging.getMappedRange(0, byteSpan);
924
+ const out = geoms.map((g) => ({
925
+ width: g.width,
926
+ height: g.height,
927
+ paddedWidth: g.paddedWidth,
928
+ paddedHeight: g.paddedHeight,
929
+ data: new Uint8Array(mapped.slice(g.dstOffset, g.dstOffset + g.byteLen))
930
+ }));
931
+ staging.unmap();
932
+ const encodeMs = performance.now() - t0;
933
+ const gpuMs = timing ? await this._readTimingMs(timing) : void 0;
934
+ return { levels: out, encodeMs, gpuMs };
935
+ }
936
+ // ------------------------------------------------------------------ //
937
+ // GPU timing plumbing (timestamp queries) //
938
+ // ------------------------------------------------------------------ //
939
+ /** Create the query set + resolve/staging buffers for one timed
940
+ * submission, or null when the device lacks 'timestamp-query'. */
941
+ _createTiming() {
942
+ const device = this.device;
943
+ if (!device.features.has("timestamp-query")) return null;
944
+ return {
945
+ querySet: device.createQuerySet({ type: "timestamp", count: 2 }),
946
+ resolve: device.createBuffer({
947
+ label: `${this.label}-ts-resolve`,
948
+ size: 16,
949
+ usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC
950
+ }),
951
+ staging: device.createBuffer({
952
+ label: `${this.label}-ts-staging`,
953
+ size: 16,
954
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
955
+ })
956
+ };
957
+ }
958
+ /** Read back a timed submission's pass duration (ms) and destroy the
959
+ * timing objects. Timestamps are u64 nanoseconds. */
960
+ async _readTimingMs(timing) {
961
+ await timing.staging.mapAsync(GPUMapMode.READ);
962
+ const [begin, end] = new BigUint64Array(timing.staging.getMappedRange().slice(0));
963
+ timing.staging.unmap();
964
+ timing.staging.destroy();
965
+ timing.resolve.destroy();
966
+ timing.querySet.destroy();
967
+ if (end !== void 0 && begin !== void 0 && end > begin) {
968
+ return Number(end - begin) / 1e6;
351
969
  }
970
+ return void 0;
352
971
  }
353
972
  };
354
973
 
355
974
  // src/bc1.wgsl
356
- var bc1_default = "// BC1 (DXT1) compute shader encoder.\n//\n// Each invocation encodes one 4x4 pixel block into an 8-byte BC1 block\n// written as 2 x u32 into the destination storage buffer. This is the f32\n// fallback; bc1_fast_f16.wgsl is the same algorithm and is preferred when\n// the device reports shader-f16.\n//\n// BC1 block layout (little-endian):\n// u32[0]: color0 (low 16) | color1 (high 16) both in RGB565\n// u32[1]: 16 x 2-bit indices, pixel 0 = bits 0..1, pixel 15 = bits 30..31\n//\n// We always force the 4-color mode (color0 > color1, numeric 16-bit):\n// idx 0 -> color0\n// idx 1 -> color1\n// idx 2 -> (2*color0 + color1) / 3\n// idx 3 -> ( color0 + 2*color1) / 3\n//\n// ALGORITHM: principal-axis endpoint seed (covariance power-iteration; inset\n// bbox on degenerate blocks), inset by ~half a 565 cell along the axis, then\n// a fused pass that projects every pixel onto the decoded-endpoint line (the\n// 4 palette entries are colinear and evenly spaced, so the nearest entry is\n// the rounded projection \u2014 no 4-entry search) while accumulating the\n// least-squares refit sums, followed by up to TWO refit rounds (re-quantise,\n// reproject with indices packed on the fly, accept only on lower block\n// error).\n\nstruct Params {\n blocks_x: u32,\n blocks_y: u32,\n width: u32,\n height: u32,\n};\n\n@group(0) @binding(0) var src_tex: texture_2d<f32>;\n@group(0) @binding(1) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(2) var<uniform> params: Params;\n\nfn to565(c: vec3<f32>) -> u32 {\n // Round-to-nearest quantization into 5-6-5.\n let r = u32(clamp(floor(c.r * 31.0 + 0.5), 0.0, 31.0));\n let g = u32(clamp(floor(c.g * 63.0 + 0.5), 0.0, 63.0));\n let b = u32(clamp(floor(c.b * 31.0 + 0.5), 0.0, 31.0));\n return (r << 11u) | (g << 5u) | b;\n}\n\nfn from565(c: u32) -> vec3<f32> {\n let r = (c >> 11u) & 31u;\n let g = (c >> 5u) & 63u;\n let b = c & 31u;\n // 5/6-bit -> 8-bit: (x*527+23)>>6 (6-bit: 259/33) \u2014 round-to-nearest\n // scaling, matching bc1_ref.ts and typical hardware decoders (white ->\n // 255). Integer u32 math is exact. NOTE: this is NOT plain bit-replication\n // ((x<<3)|(x>>2)) \u2014 they differ for some codes (e.g. 5-bit 3 -> 25 vs 24).\n // Selecting indices against this palette is what makes the encoder agree\n // with what the GPU will actually sample.\n let r8 = (r * 527u + 23u) >> 6u;\n let g8 = (g * 259u + 33u) >> 6u;\n let b8 = (b * 527u + 23u) >> 6u;\n return vec3<f32>(vec3<u32>(r8, g8, b8)) / 255.0;\n}\n\n// One projection pass against the decoded endpoints of (c0,c1): the packed\n// 2-bit indices, the block's squared error, and the LSQ normal-equation sums\n// of the resulting assignment \u2014 so an accepted refit can seed the next\n// round. Levels s run 0..3 along p0\u2192p1 (palette = p0, p0+\u2153d, p0+\u2154d, p1 \u2014\n// colinear, evenly spaced, so rounding the projection IS the nearest-entry\n// search). Level \u2192 BC1 index: 0\u21920 (c0), 1\u21922 (\u2154c0+\u2153c1), 2\u21923, 3\u21921 (c1); as a\n// packed LUT: (0x78 >> 2L) & 3.\nstruct ProjStats {\n indices: u32,\n err: f32,\n sAA: f32, sBB: f32, sAB: f32,\n sAV: vec3<f32>, sBV: vec3<f32>,\n s_min: f32, s_max: f32,\n};\nfn project_stats(pix: ptr<function, array<vec3<f32>, 16>>, c0: u32, c1: u32) -> ProjStats {\n var out: ProjStats;\n out.indices = 0u;\n out.err = 0.0;\n out.sAA = 0.0; out.sBB = 0.0; out.sAB = 0.0;\n out.sAV = vec3<f32>(0.0); out.sBV = vec3<f32>(0.0);\n out.s_min = 3.0; out.s_max = 0.0;\n let p0 = from565(c0);\n let p1 = from565(c1);\n let dir = p1 - p0;\n let dd = dot(dir, dir);\n if (dd == 0.0) {\n // Unreachable for distinct 565 codes (the decode is injective); kept so\n // a degenerate call still returns a consistent error.\n out.s_min = 0.0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let e = (*pix)[k] - p0;\n out.err = out.err + dot(e, e);\n }\n return out;\n }\n let inv = 3.0 / dd;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let v = (*pix)[k];\n let s = clamp(floor(dot(v - p0, dir) * inv + 0.5), 0.0, 3.0);\n out.s_min = min(out.s_min, s); out.s_max = max(out.s_max, s);\n let b = s * (1.0 / 3.0); let a = 1.0 - b;\n out.sAA = out.sAA + a * a; out.sBB = out.sBB + b * b; out.sAB = out.sAB + a * b;\n out.sAV = out.sAV + a * v; out.sBV = out.sBV + b * v;\n let e = v - (p0 + b * dir);\n out.err = out.err + dot(e, e);\n out.indices = out.indices | (((0x78u >> (u32(s) * 2u)) & 3u) << (k * 2u));\n }\n return out;\n}\n\n// Principal colour axis via covariance power-iteration, seeded with the bbox\n// diagonal. Returns a unit axis, or vec3(0) for a degenerate (constant)\n// block. The bbox diagonal alone is sign-blind and points across\n// anti-correlated data (normal maps, hue edges) instead of along it.\nfn principal_axis(\n pixels: ptr<function, array<vec3<f32>, 16>>,\n mean: vec3<f32>,\n seed: vec3<f32>,\n) -> vec3<f32> {\n // Symmetric 3x3 covariance, stored as its three rows.\n var c0v = vec3<f32>(0.0);\n var c1v = vec3<f32>(0.0);\n var c2v = vec3<f32>(0.0);\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let d = (*pixels)[k] - mean;\n c0v = c0v + d.x * d;\n c1v = c1v + d.y * d;\n c2v = c2v + d.z * d;\n }\n var v = seed;\n var len = length(v);\n if (len < 1e-9) { return vec3<f32>(0.0); }\n v = v / len;\n for (var iter: u32 = 0u; iter < 8u; iter = iter + 1u) {\n let nv = vec3<f32>(dot(c0v, v), dot(c1v, v), dot(c2v, v));\n len = length(nv);\n if (len < 1e-12) { return vec3<f32>(0.0); }\n v = nv / len;\n }\n return v;\n}\n\n@compute @workgroup_size(8, 8, 1)\nfn encode(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) {\n return;\n }\n\n let bx = gid.x;\n let by = gid.y;\n let block_index = by * params.blocks_x + bx;\n\n let base = vec2<i32>(i32(bx) * 4, i32(by) * 4);\n let max_xy = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);\n\n var pixels: array<vec3<f32>, 16>;\n var bb_min = vec3<f32>(1.0, 1.0, 1.0);\n var bb_max = vec3<f32>(0.0, 0.0, 0.0);\n var mean = vec3<f32>(0.0);\n\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let lx = i32(i & 3u);\n let ly = i32(i >> 2u);\n // Clamp to edge for non-multiple-of-4 textures.\n let p = clamp(base + vec2<i32>(lx, ly), vec2<i32>(0, 0), max_xy);\n let c = textureLoad(src_tex, p, 0).rgb;\n pixels[i] = c;\n bb_min = min(bb_min, c);\n bb_max = max(bb_max, c);\n mean = mean + c;\n }\n mean = mean * (1.0 / 16.0);\n\n // Seed endpoints from the block's principal colour axis at the exact\n // projection extents, inset by ~half a 565 cell along the axis (stb_dxt\n // heuristic). Degenerate (near-flat) blocks keep the inset-bbox seed.\n var seed_hi: vec3<f32>;\n var seed_lo: vec3<f32>;\n let axis = principal_axis(&pixels, mean, bb_max - bb_min);\n if (dot(axis, axis) > 0.0) {\n var t_min: f32 = 1e30;\n var t_max: f32 = -1e30;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let t = dot(pixels[k] - mean, axis);\n t_min = min(t_min, t);\n t_max = max(t_max, t);\n }\n let pad = (t_max - t_min) / 16.0;\n seed_hi = clamp(mean + (t_max - pad) * axis, vec3<f32>(0.0), vec3<f32>(1.0));\n seed_lo = clamp(mean + (t_min + pad) * axis, vec3<f32>(0.0), vec3<f32>(1.0));\n } else {\n let inset = (bb_max - bb_min) / 16.0;\n seed_hi = clamp(bb_max - inset, vec3<f32>(0.0), vec3<f32>(1.0));\n seed_lo = clamp(bb_min + inset, vec3<f32>(0.0), vec3<f32>(1.0));\n }\n var c0 = to565(seed_hi);\n var c1 = to565(seed_lo);\n if (c0 == c1) {\n if (c1 > 0u) { c1 = c1 - 1u; } else { c0 = c0 + 1u; }\n } else if (c0 < c1) {\n let t = c0; c0 = c1; c1 = t;\n }\n\n // Fused seed pass, then up to TWO least-squares refit rounds, each\n // accepted only if the block's squared error actually decreases \u2014 the\n // refit minimises a continuous objective and can lose after 565\n // quantisation. Every pass re-accumulates the normal-equation sums, so an\n // accepted round seeds the next.\n var cur = project_stats(&pixels, c0, c1);\n for (var it: u32 = 0u; it < 2u; it = it + 1u) {\n // Refit only on a well-conditioned system: when every pixel lands on\n // ONE level (flat blocks \u2014 the 4-colour nudge forces c0 \u2260 c1 even\n // then) the system is rank-1 and det/numerators are pure float noise;\n // the solve would return garbage endpoints. With \u22652 levels\n // det = \u03A3_i<j (b_j \u2212 b_i)\xB2 \u2265 ~1.67, so 1e-3 is a safe guard.\n if (cur.s_min >= cur.s_max) { break; }\n let det = cur.sAA * cur.sBB - cur.sAB * cur.sAB;\n if (abs(det) <= 1e-3) { break; }\n // Clamp the refit to the block bbox (not [0,1]): on multi-cluster\n // blocks the unconstrained solve extrapolates far outside the block's\n // colours and the per-channel clamp then bends the hue \u2014 fringe pixels\n // decode to colours that exist nowhere in the block. Constraining to\n // the bbox also measures better in plain SSE (+1.6 dB on the colour\n // test card), so the accept-if-better guard below keeps more refits.\n let e0 = clamp((cur.sBB * cur.sAV - cur.sAB * cur.sBV) / det, bb_min, bb_max);\n let e1 = clamp((cur.sAA * cur.sBV - cur.sAB * cur.sAV) / det, bb_min, bb_max);\n var nc0 = to565(e0);\n var nc1 = to565(e1);\n if (nc0 == nc1) {\n if (nc1 > 0u) { nc1 = nc1 - 1u; } else { nc0 = nc0 + 1u; }\n } else if (nc0 < nc1) {\n let t = nc0; nc0 = nc1; nc1 = t;\n }\n if (nc0 == c0 && nc1 == c1) { break; }\n let nxt = project_stats(&pixels, nc0, nc1);\n if (nxt.err >= cur.err) { break; }\n c0 = nc0;\n c1 = nc1;\n cur = nxt;\n }\n\n let out = block_index * 2u;\n dst[out] = c0 | (c1 << 16u);\n dst[out + 1u] = cur.indices;\n}\n";
975
+ var bc1_default = "// BC1 (DXT1) compute shader encoder.\n//\n// Each invocation encodes one 4x4 pixel block into an 8-byte BC1 block\n// written as 2 x u32 into the destination storage buffer. This is the f32\n// fallback; bc1_fast_f16.wgsl is the same algorithm and is preferred when\n// the device reports shader-f16.\n//\n// BC1 block layout (little-endian):\n// u32[0]: color0 (low 16) | color1 (high 16) both in RGB565\n// u32[1]: 16 x 2-bit indices, pixel 0 = bits 0..1, pixel 15 = bits 30..31\n//\n// We always force the 4-color mode (color0 > color1, numeric 16-bit):\n// idx 0 -> color0\n// idx 1 -> color1\n// idx 2 -> (2*color0 + color1) / 3\n// idx 3 -> ( color0 + 2*color1) / 3\n//\n// ALGORITHM: principal-axis endpoint seed (covariance power-iteration; inset\n// bbox on degenerate blocks), inset by ~half a 565 cell along the axis, then\n// a fused pass that projects every pixel onto the decoded-endpoint line (the\n// 4 palette entries are colinear and evenly spaced, so the nearest entry is\n// the rounded projection \u2014 no 4-entry search) while accumulating the\n// least-squares refit sums, followed by up to TWO refit rounds (re-quantise,\n// reproject with indices packed on the fly, accept only on lower block\n// error).\n\nstruct Params {\n blocks_x: u32,\n blocks_y: u32,\n width: u32,\n height: u32,\n};\n\n@group(0) @binding(0) var src_tex: texture_2d<f32>;\n@group(0) @binding(1) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(2) var<uniform> params: Params;\n\nfn to565(c: vec3<f32>) -> u32 {\n // Round-to-nearest quantization into 5-6-5.\n let r = u32(clamp(floor(c.r * 31.0 + 0.5), 0.0, 31.0));\n let g = u32(clamp(floor(c.g * 63.0 + 0.5), 0.0, 63.0));\n let b = u32(clamp(floor(c.b * 31.0 + 0.5), 0.0, 31.0));\n return (r << 11u) | (g << 5u) | b;\n}\n\nfn from565(c: u32) -> vec3<f32> {\n let r = (c >> 11u) & 31u;\n let g = (c >> 5u) & 63u;\n let b = c & 31u;\n // 5/6-bit -> 8-bit: (x*527+23)>>6 (6-bit: 259/33) \u2014 round-to-nearest\n // scaling, matching bc1_ref.ts and typical hardware decoders (white ->\n // 255). Integer u32 math is exact. NOTE: this is NOT plain bit-replication\n // ((x<<3)|(x>>2)) \u2014 they differ for some codes (e.g. 5-bit 3 -> 25 vs 24).\n // Selecting indices against this palette is what makes the encoder agree\n // with what the GPU will actually sample.\n let r8 = (r * 527u + 23u) >> 6u;\n let g8 = (g * 259u + 33u) >> 6u;\n let b8 = (b * 527u + 23u) >> 6u;\n return vec3<f32>(vec3<u32>(r8, g8, b8)) / 255.0;\n}\n\n// One projection pass against the decoded endpoints of (c0,c1): the packed\n// 2-bit indices, the block's squared error, and the LSQ normal-equation sums\n// of the resulting assignment \u2014 so an accepted refit can seed the next\n// round. Levels s run 0..3 along p0\u2192p1 (palette = p0, p0+\u2153d, p0+\u2154d, p1 \u2014\n// colinear, evenly spaced, so rounding the projection IS the nearest-entry\n// search). Level \u2192 BC1 index: 0\u21920 (c0), 1\u21922 (\u2154c0+\u2153c1), 2\u21923, 3\u21921 (c1); as a\n// packed LUT: (0x78 >> 2L) & 3.\nstruct ProjStats {\n indices: u32,\n err: f32,\n sAA: f32, sBB: f32, sAB: f32,\n sAV: vec3<f32>, sBV: vec3<f32>,\n s_min: f32, s_max: f32,\n};\nfn project_stats(pix: ptr<function, array<vec3<f32>, 16>>, c0: u32, c1: u32) -> ProjStats {\n var out: ProjStats;\n out.indices = 0u;\n out.err = 0.0;\n out.sAA = 0.0; out.sBB = 0.0; out.sAB = 0.0;\n out.sAV = vec3<f32>(0.0); out.sBV = vec3<f32>(0.0);\n out.s_min = 3.0; out.s_max = 0.0;\n let p0 = from565(c0);\n let p1 = from565(c1);\n let dir = p1 - p0;\n let dd = dot(dir, dir);\n if (dd == 0.0) {\n // Unreachable for distinct 565 codes (the decode is injective); kept so\n // a degenerate call still returns a consistent error.\n out.s_min = 0.0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let e = (*pix)[k] - p0;\n out.err = out.err + dot(e, e);\n }\n return out;\n }\n let inv = 3.0 / dd;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let v = (*pix)[k];\n let s = clamp(floor(dot(v - p0, dir) * inv + 0.5), 0.0, 3.0);\n out.s_min = min(out.s_min, s); out.s_max = max(out.s_max, s);\n let b = s * (1.0 / 3.0); let a = 1.0 - b;\n out.sAA = out.sAA + a * a; out.sBB = out.sBB + b * b; out.sAB = out.sAB + a * b;\n out.sAV = out.sAV + a * v; out.sBV = out.sBV + b * v;\n let e = v - (p0 + b * dir);\n out.err = out.err + dot(e, e);\n out.indices = out.indices | (((0x78u >> (u32(s) * 2u)) & 3u) << (k * 2u));\n }\n return out;\n}\n\n// Principal colour axis via covariance power-iteration, seeded with the bbox\n// diagonal. Returns a unit axis, or vec3(0) for a degenerate (constant)\n// block. The bbox diagonal alone is sign-blind and points across\n// anti-correlated data (normal maps, hue edges) instead of along it.\nfn principal_axis(\n pixels: ptr<function, array<vec3<f32>, 16>>,\n mean: vec3<f32>,\n seed: vec3<f32>,\n) -> vec3<f32> {\n // Symmetric 3x3 covariance, stored as its three rows.\n var c0v = vec3<f32>(0.0);\n var c1v = vec3<f32>(0.0);\n var c2v = vec3<f32>(0.0);\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let d = (*pixels)[k] - mean;\n c0v = c0v + d.x * d;\n c1v = c1v + d.y * d;\n c2v = c2v + d.z * d;\n }\n var v = seed;\n var len = length(v);\n if (len < 1e-9) { return vec3<f32>(0.0); }\n v = v / len;\n for (var iter: u32 = 0u; iter < 8u; iter = iter + 1u) {\n let nv = vec3<f32>(dot(c0v, v), dot(c1v, v), dot(c2v, v));\n len = length(nv);\n if (len < 1e-12) { return vec3<f32>(0.0); }\n v = nv / len;\n }\n return v;\n}\n\n@compute @workgroup_size(8, 8, 1)\nfn encode(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) {\n return;\n }\n\n let bx = gid.x;\n let by = gid.y;\n let block_index = by * params.blocks_x + bx;\n\n let base = vec2<i32>(i32(bx) * 4, i32(by) * 4);\n let max_xy = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);\n\n var pixels: array<vec3<f32>, 16>;\n var bb_min = vec3<f32>(1.0, 1.0, 1.0);\n var bb_max = vec3<f32>(0.0, 0.0, 0.0);\n var mean = vec3<f32>(0.0);\n var gd = 0.0;\n\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let lx = i32(i & 3u);\n let ly = i32(i >> 2u);\n // Clamp to edge for non-multiple-of-4 textures.\n let p = clamp(base + vec2<i32>(lx, ly), vec2<i32>(0, 0), max_xy);\n let c = textureLoad(src_tex, p, 0).rgb;\n pixels[i] = c;\n bb_min = min(bb_min, c);\n bb_max = max(bb_max, c);\n mean = mean + c;\n gd = max(gd, max(abs(c.x - c.y), abs(c.x - c.z)));\n }\n mean = mean * (1.0 / 16.0);\n // Exactly-gray blocks free the refit from the bbox clamp (no hue to\n // protect; smooth gradients want endpoints outside the data range) \u2014\n // see bc1_fast_f16.wgsl.\n let gray = gd == 0.0;\n let lim_lo = select(bb_min, vec3<f32>(0.0), gray);\n let lim_hi = select(bb_max, vec3<f32>(1.0), gray);\n\n // Seed endpoints from the block's principal colour axis at the exact\n // projection extents, inset by ~half a 565 cell along the axis (stb_dxt\n // heuristic). Degenerate (near-flat) blocks keep the inset-bbox seed.\n var seed_hi: vec3<f32>;\n var seed_lo: vec3<f32>;\n let axis = principal_axis(&pixels, mean, bb_max - bb_min);\n if (dot(axis, axis) > 0.0) {\n var t_min: f32 = 1e30;\n var t_max: f32 = -1e30;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let t = dot(pixels[k] - mean, axis);\n t_min = min(t_min, t);\n t_max = max(t_max, t);\n }\n let pad = (t_max - t_min) / 16.0;\n seed_hi = clamp(mean + (t_max - pad) * axis, vec3<f32>(0.0), vec3<f32>(1.0));\n seed_lo = clamp(mean + (t_min + pad) * axis, vec3<f32>(0.0), vec3<f32>(1.0));\n } else {\n let inset = (bb_max - bb_min) / 16.0;\n seed_hi = clamp(bb_max - inset, vec3<f32>(0.0), vec3<f32>(1.0));\n seed_lo = clamp(bb_min + inset, vec3<f32>(0.0), vec3<f32>(1.0));\n }\n var c0 = to565(seed_hi);\n var c1 = to565(seed_lo);\n if (c0 == c1) {\n if (c1 > 0u) { c1 = c1 - 1u; } else { c0 = c0 + 1u; }\n } else if (c0 < c1) {\n let t = c0; c0 = c1; c1 = t;\n }\n\n // Fused seed pass, then up to TWO least-squares refit rounds, each\n // accepted only if the block's squared error actually decreases \u2014 the\n // refit minimises a continuous objective and can lose after 565\n // quantisation. Every pass re-accumulates the normal-equation sums, so an\n // accepted round seeds the next.\n var cur = project_stats(&pixels, c0, c1);\n for (var it: u32 = 0u; it < 2u; it = it + 1u) {\n // Refit only on a well-conditioned system: when every pixel lands on\n // ONE level (flat blocks \u2014 the 4-colour nudge forces c0 \u2260 c1 even\n // then) the system is rank-1 and det/numerators are pure float noise;\n // the solve would return garbage endpoints. With \u22652 levels\n // det = \u03A3_i<j (b_j \u2212 b_i)\xB2 \u2265 ~1.67, so 1e-3 is a safe guard.\n if (cur.s_min >= cur.s_max) { break; }\n let det = cur.sAA * cur.sBB - cur.sAB * cur.sAB;\n if (abs(det) <= 1e-3) { break; }\n // Clamp the refit to the block bbox (not [0,1]): on multi-cluster\n // blocks the unconstrained solve extrapolates far outside the block's\n // colours and the per-channel clamp then bends the hue \u2014 fringe pixels\n // decode to colours that exist nowhere in the block. Constraining to\n // the bbox also measures better in plain SSE (+1.6 dB on the colour\n // test card), so the accept-if-better guard below keeps more refits.\n let e0 = clamp((cur.sBB * cur.sAV - cur.sAB * cur.sBV) / det, lim_lo, lim_hi);\n let e1 = clamp((cur.sAA * cur.sBV - cur.sAB * cur.sAV) / det, lim_lo, lim_hi);\n var nc0 = to565(e0);\n var nc1 = to565(e1);\n if (nc0 == nc1) {\n if (nc1 > 0u) { nc1 = nc1 - 1u; } else { nc0 = nc0 + 1u; }\n } else if (nc0 < nc1) {\n let t = nc0; nc0 = nc1; nc1 = t;\n }\n if (nc0 == c0 && nc1 == c1) { break; }\n let nxt = project_stats(&pixels, nc0, nc1);\n if (nxt.err >= cur.err) { break; }\n c0 = nc0;\n c1 = nc1;\n cur = nxt;\n }\n\n let out = block_index * 2u;\n dst[out] = c0 | (c1 << 16u);\n dst[out + 1u] = cur.indices;\n}\n";
357
976
 
358
977
  // src/bc1_fast_f16.wgsl
359
978
  var bc1_fast_f16_default = `// bc1 "fast" encoder \u2014 f16 variant (requires the shader-f16 feature).
@@ -484,13 +1103,23 @@ fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
484
1103
  var mn = h3(1.0);
485
1104
  var mxv = h3(0.0);
486
1105
  var mean = h3(0.0);
1106
+ var gd = h(0.0);
487
1107
  for (var i: u32 = 0u; i < 16u; i = i + 1u) {
488
1108
  let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);
489
1109
  let px = h3(textureLoad(src_tex, p, 0).rgb);
490
1110
  pix[i] = px; mn = min(mn, px); mxv = max(mxv, px);
491
1111
  mean = mean + px;
1112
+ gd = max(gd, max(abs(px.x - px.y), abs(px.x - px.z)));
492
1113
  }
493
1114
  mean = mean * h(1.0 / 16.0);
1115
+ // Exactly-gray blocks free the refit from the bbox clamp below: a gray
1116
+ // block has no hue to bend (the clamp's whole purpose), and on smooth
1117
+ // gradients the LSQ optimum often lies OUTSIDE the data range \u2014 endpoints
1118
+ // spread wider than the block so the 1/3-2/3 interpolants land on the
1119
+ // values. Same rationale as the BC5 scalar channels (+0.32 dB there).
1120
+ let gray = gd == h(0.0);
1121
+ let lim_lo = select(mn, h3(0.0), gray);
1122
+ let lim_hi = select(mxv, h3(1.0), gray);
494
1123
 
495
1124
  // Seed endpoints from the block's principal colour axis (covariance
496
1125
  // power-iteration, seeded with the bbox diagonal \u2014 same family as the
@@ -564,14 +1193,15 @@ fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
564
1193
  if (cur.s_min >= cur.s_max) { break; }
565
1194
  let det = cur.sAA * cur.sBB - cur.sAB * cur.sAB;
566
1195
  if (abs(det) <= h(0.5)) { break; }
567
- // Clamp the refit to the block bbox (not [0,1]): on multi-cluster blocks
568
- // the unconstrained solve extrapolates far outside the block's colours
1196
+ // Clamp the refit to the block bbox (not [0,1]) \u2014 except for exactly
1197
+ // gray blocks, see the load pass: on multi-cluster blocks the
1198
+ // unconstrained solve extrapolates far outside the block's colours
569
1199
  // and the per-channel clamp then bends the hue \u2014 fringe pixels decode to
570
1200
  // colours that exist nowhere in the block. Constraining to the bbox also
571
1201
  // measures better in plain SSE (+1.6 dB on the colour test card), so the
572
1202
  // accept-if-better guard below keeps more refits.
573
- let e0 = clamp((cur.sBB * cur.sAV - cur.sAB * cur.sBV) / det, mn, mxv);
574
- let e1 = clamp((cur.sAA * cur.sBV - cur.sAB * cur.sAV) / det, mn, mxv);
1203
+ let e0 = clamp((cur.sBB * cur.sAV - cur.sAB * cur.sBV) / det, lim_lo, lim_hi);
1204
+ let e1 = clamp((cur.sAA * cur.sBV - cur.sAB * cur.sAV) / det, lim_lo, lim_hi);
575
1205
  let rq = order565(to565(e0), to565(e1));
576
1206
  if (rq.x == c0 && rq.y == c1) { break; }
577
1207
  let nxt = project_stats(&pix, rq.x, rq.y);
@@ -612,12 +1242,12 @@ var BC1Encoder = class extends Encoder {
612
1242
  };
613
1243
 
614
1244
  // src/bc5.wgsl
615
- var bc5_default = "// BC5 (RGTC2) compute shader encoder.\n//\n// Each invocation encodes one 4x4 pixel block into a 16-byte BC5 block\n// written as 4 x u32 into the destination storage buffer. This is the f32\n// fallback; bc5_fast_f16.wgsl is the same algorithm and is preferred when\n// the device reports shader-f16.\n//\n// BC5 = two BC4 blocks concatenated:\n// block bytes 0..7 : BC4 of R channel (normal.x for tangent-space normals)\n// block bytes 8..15 : BC4 of G channel (normal.y)\n//\n// Each BC4 half-block (8 bytes):\n// byte 0 : red0 (8-bit endpoint)\n// byte 1 : red1 (8-bit endpoint)\n// bytes 2..7 : 16 \xD7 3-bit indices, LSB-first, pixel 0 at bit 0\n//\n// We always produce the 6-interpolation mode (red0 > red1). See `bc4_ref.ts`\n// for the reference this encoder is validated against.\n//\n// Pipeline per channel: bbox endpoints + O(1) projection assignment per\n// texel. The 8-entry palette in 6-interp mode is COLINEAR and EVENLY spaced\n// from r0 to r1 (levels 0..7 in palette order 0,2,3,4,5,6,7,1), so the\n// nearest entry is the rounded projection onto the r0\u2192r1 axis \u2014 no 8-entry\n// search, and the 3-bit indices are packed on the fly. The LSQ refit sums\n// are accumulated in the same fused pass; the requantised refit is accepted\n// only if it lowers the block error (worth ~1.3 dB on the normal-map card).\n\nstruct Params {\n blocks_x: u32,\n blocks_y: u32,\n width: u32,\n height: u32,\n};\n\n@group(0) @binding(0) var src_tex: texture_2d<f32>;\n@group(0) @binding(1) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(2) var<uniform> params: Params;\n\nfn quantize8(v: f32) -> u32 {\n // Round-to-nearest, clamp to [0, 255]. floor(x + 0.5) is the same\n // rounding rule the CPU reference uses.\n return u32(clamp(floor(v * 255.0 + 0.5), 0.0, 255.0));\n}\n\n// Encode 16 single-channel values into an 8-byte BC4 block, packed as\n// two little-endian u32s (u32[0] = bytes 0..3, u32[1] = bytes 4..7).\n// vmin/vmax are the channel's min/max, computed in the caller's load loop \u2014\n// fusing that scan there saves a 16-value pass per channel.\nfn encode_bc4(values: ptr<function, array<f32, 16>>, vmin: f32, vmax: f32) -> vec2<u32> {\n var r0: u32 = quantize8(vmax);\n var r1: u32 = quantize8(vmin);\n // Force 6-interp mode: red0 > red1 strictly.\n if (r0 == r1) {\n if (r1 > 0u) { r1 = r1 - 1u; }\n else { r0 = r0 + 1u; }\n }\n\n // ONE fused pass: level = round(7\xB7(v \u2212 r0)/(r1 \u2212 r0)) projection\n // assignment (the 8-entry 6-interp palette is colinear and evenly\n // spaced, so the rounded projection IS the nearest-entry search), the\n // seed solution's packed indices and squared error, and the least-\n // squares normal-equation sums. The refit endpoints are re-quantised,\n // reprojected, and accepted only if the block error decreases.\n // Level \u2192 BC4 index LUT (0,2,3,4,5,6,7,1) packed as 3-bit entries in\n // 0x3F58D0. Pixel k's 3 bits start at bit 3k+16 of the (w0,w1) pair\n // (bytes 0..1 are the endpoints); k = 5 straddles the word boundary.\n let r0f = f32(r0) / 255.0;\n let dir = f32(r1) / 255.0 - r0f;\n let scale = 7.0 / dir;\n var w0 = r0 | (r1 << 8u);\n var w1 = 0u;\n var sAA = 0.0; var sBB = 0.0; var sAB = 0.0;\n var sAV = 0.0; var sBV = 0.0;\n var s_min = 7.0; var s_max = 0.0;\n var seed_err = 0.0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let vr = (*values)[k] - r0f;\n let L = clamp(floor(vr * scale + 0.5), 0.0, 7.0);\n s_min = min(s_min, L); s_max = max(s_max, L);\n let b = L * (1.0 / 7.0); let a = 1.0 - b;\n sAA = sAA + a * a; sBB = sBB + b * b; sAB = sAB + a * b;\n sAV = sAV + a * vr; sBV = sBV + b * vr;\n let e = vr - b * dir;\n seed_err = seed_err + e * e;\n let idx = (0x3F58D0u >> (u32(L) * 3u)) & 7u;\n let bit = 3u * k + 16u;\n if (bit <= 29u) {\n w0 = w0 | (idx << bit);\n } else if (bit >= 32u) {\n w1 = w1 | (idx << (bit - 32u));\n } else {\n w0 = w0 | (idx << bit);\n w1 = w1 | (idx >> (32u - bit));\n }\n }\n\n // Rank-1 guard: with every pixel on ONE level the system is singular\n // (det is float rounding noise); with \u22652 distinct levels\n // det = \u03A3_i<j (b_j \u2212 b_i)\xB2 \u2265 15/49 \u2248 0.306.\n if (s_min < s_max) {\n let det = sAA * sBB - sAB * sAB;\n if (abs(det) > 1e-3) {\n // Clamp to [0,1], NOT the block's value range: for a scalar channel,\n // endpoints beyond the data range are often genuinely optimal (they\n // centre the palette levels on the data) and there is no colour axis\n // to bend \u2014 the bbox clamp the colour formats need costs ~0.3 dB\n // here. The accept-if-better guard still protects against a refit\n // that loses after quantisation.\n let e0 = clamp(r0f + (sBB * sAV - sAB * sBV) / det, 0.0, 1.0);\n let e1 = clamp(r0f + (sAA * sBV - sAB * sAV) / det, 0.0, 1.0);\n let n0 = quantize8(e0);\n let n1 = quantize8(e1);\n // Keep 6-interp mode (r0 > r1 strictly); skip the no-op refit.\n if (n0 > n1 && !(n0 == r0 && n1 == r1)) {\n let n0f = f32(n0) / 255.0;\n let ndir = f32(n1) / 255.0 - n0f;\n let nscale = 7.0 / ndir;\n var nw0 = n0 | (n1 << 8u);\n var nw1 = 0u;\n var refit_err = 0.0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let vr = (*values)[k] - n0f;\n let L = clamp(floor(vr * nscale + 0.5), 0.0, 7.0);\n let e = vr - L * (1.0 / 7.0) * ndir;\n refit_err = refit_err + e * e;\n let idx = (0x3F58D0u >> (u32(L) * 3u)) & 7u;\n let bit = 3u * k + 16u;\n if (bit <= 29u) {\n nw0 = nw0 | (idx << bit);\n } else if (bit >= 32u) {\n nw1 = nw1 | (idx << (bit - 32u));\n } else {\n nw0 = nw0 | (idx << bit);\n nw1 = nw1 | (idx >> (32u - bit));\n }\n }\n if (refit_err < seed_err) {\n return vec2<u32>(nw0, nw1);\n }\n }\n }\n }\n return vec2<u32>(w0, w1);\n}\n\n@compute @workgroup_size(8, 8, 1)\nfn encode(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) {\n return;\n }\n\n let bx = gid.x;\n let by = gid.y;\n let block_index = by * params.blocks_x + bx;\n\n let base = vec2<i32>(i32(bx) * 4, i32(by) * 4);\n let max_xy = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);\n\n // Load 4\xD74 RG values, splitting into per-channel arrays so each can\n // be handed to encode_bc4 independently; the per-channel min/max scan is\n // fused into the same loop.\n var r_values: array<f32, 16>;\n var g_values: array<f32, 16>;\n var r_min: f32 = 1.0; var r_max: f32 = 0.0;\n var g_min: f32 = 1.0; var g_max: f32 = 0.0;\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let lx = i32(i & 3u);\n let ly = i32(i >> 2u);\n // Clamp to edge for non-multiple-of-4 input sizes.\n let p = clamp(base + vec2<i32>(lx, ly), vec2<i32>(0, 0), max_xy);\n let c = textureLoad(src_tex, p, 0);\n r_values[i] = c.r;\n g_values[i] = c.g;\n r_min = min(r_min, c.r); r_max = max(r_max, c.r);\n g_min = min(g_min, c.g); g_max = max(g_max, c.g);\n }\n\n let r_block = encode_bc4(&r_values, r_min, r_max);\n let g_block = encode_bc4(&g_values, g_min, g_max);\n\n // BC5 block = R half (bytes 0..7) || G half (bytes 8..15) = 4 u32s.\n let out = block_index * 4u;\n dst[out + 0u] = r_block.x;\n dst[out + 1u] = r_block.y;\n dst[out + 2u] = g_block.x;\n dst[out + 3u] = g_block.y;\n}\n";
1245
+ var bc5_default = "// BC5 (RGTC2) compute shader encoder.\n//\n// Each invocation encodes one 4x4 pixel block into a 16-byte BC5 block\n// written as 4 x u32 into the destination storage buffer. This is the f32\n// fallback; bc5_fast_f16.wgsl is the same algorithm and is preferred when\n// the device reports shader-f16.\n//\n// BC5 = two BC4 blocks concatenated:\n// block bytes 0..7 : BC4 of R channel (normal.x for tangent-space normals)\n// block bytes 8..15 : BC4 of G channel (normal.y)\n//\n// Each BC4 half-block (8 bytes):\n// byte 0 : red0 (8-bit endpoint)\n// byte 1 : red1 (8-bit endpoint)\n// bytes 2..7 : 16 \xD7 3-bit indices, LSB-first, pixel 0 at bit 0\n//\n// We always produce the 6-interpolation mode (red0 > red1). See `bc4_ref.ts`\n// for the reference this encoder is validated against.\n//\n// Same algorithm as bc5_fast_f16.wgsl (see that file for the full notes):\n// \u2022 both channels processed as vec2 lanes of the fused passes;\n// \u2022 pass 1 accumulates MOMENTS (\u03A3L, \u03A3L\xB2, \u03A3\u03C1, \u03A3L\u03C1) from which every LSQ\n// normal-equation sum is an O(1) per-block expression; the rank guard\n// is the exact 16\xB7\u03A3L\xB2 == (\u03A3L)\xB2 test;\n// \u2022 the closed-form refit prices the nearest rounding of the solve\n// through E(\u03B4) = err \u2212 2(\u03B40\xB7sAR + \u03B41\xB7sBR) + \u03B40\xB2sAA + 2\u03B40\u03B41\xB7sAB\n// + \u03B41\xB2sBB, accept-if-better;\n// \u2022 pass 2 packs the indices ONCE, against the FINAL endpoints \u2014 full\n// reprojection quality at parity cost;\n// \u2022 the 16 texel reads are 8 textureGather fetches (4 quads \xD7 R,G)\n// through a clamp-to-edge sampler, byte-identical to per-texel loads;\n// \u2022 3-bit indices accumulate branch-free into two 24-bit words (pixels\n// 0..7 and 8..15) recombined with constant shifts \u2014 no per-pixel\n// straddle branches.\n// Values are kept in the [0,255] f32 domain throughout.\n//\n// Level \u2192 BC4 index LUT (0,2,3,4,5,6,7,1) packed as 3-bit entries in\n// 0x3F58D0.\n\nstruct Params {\n blocks_x: u32,\n blocks_y: u32,\n width: u32,\n height: u32,\n};\n\n@group(0) @binding(0) var src_tex: texture_2d<f32>;\n@group(0) @binding(1) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(2) var<uniform> params: Params;\n@group(0) @binding(3) var smp: sampler;\n\nconst IDX_LUT: u32 = 0x3F58D0u;\n\n// Closed-form accept-if-better endpoint refinement for one channel \u2014 same\n// as the f16 module's `refine` (both run this per-block step in f32).\n// Endpoints clamp to [0,255], NOT the block's value range: for a scalar\n// channel, endpoints beyond the data range are often genuinely optimal and\n// there is no colour axis to bend.\nfn refine(sAA: f32, sBB: f32, sAB: f32, sAR: f32, sBR: f32, b0: u32, b1: u32, spread: bool) -> vec2<u32> {\n var out = vec2<u32>(b0, b1);\n let det = sAA * sBB - sAB * sAB;\n if (!spread || abs(det) <= 1e-3) { return out; }\n let b0f = f32(b0);\n let b1f = f32(b1);\n let e0 = clamp(b0f + (sBB * sAR - sAB * sBR) / det, 0.0, 255.0);\n let e1 = clamp(b1f + (sAA * sBR - sAB * sAR) / det, 0.0, 255.0);\n let q0f = floor(e0 + 0.5);\n let q1f = floor(e1 + 0.5);\n let q0 = u32(q0f);\n let q1 = u32(q1f);\n if (q0 > q1 && !(q0 == b0 && q1 == b1)) {\n let dd0 = q0f - b0f;\n let dd1 = q1f - b1f;\n let eNew = -2.0 * (dd0 * sAR + dd1 * sBR)\n + dd0 * dd0 * sAA + 2.0 * dd0 * dd1 * sAB + dd1 * dd1 * sBB;\n if (eNew < 0.0) {\n out = vec2<u32>(q0, q1);\n }\n }\n return out;\n}\n\n@compute @workgroup_size(8, 8, 1)\nfn encode(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) {\n return;\n }\n let bi = gid.y * params.blocks_x + gid.x;\n let base = vec2<i32>(i32(gid.x) * 4, i32(gid.y) * 4);\n\n // Load 4\xD74 R/G pairs (x = R, y = G throughout), min/max fused in.\n // Interior blocks read via 8 gathers normalised by the PHYSICAL (padded)\n // texture size; blocks straddling the source edge of a non-multiple-of-4\n // image fall back to per-texel loads clamped to the last real texel (the\n // padding strip is zero-initialised \u2014 see bc5_fast_f16.wgsl).\n // Gather components: w=(0,0) z=(1,0) x=(0,1) y=(1,1) within each quad.\n var v: array<vec2<f32>, 16>;\n var vmin = vec2<f32>(255.0);\n var vmax = vec2<f32>(0.0);\n if (u32(base.x) + 4u <= params.width && u32(base.y) + 4u <= params.height) {\n let inv_size = vec2<f32>(1.0, 1.0) / vec2<f32>(textureDimensions(src_tex));\n for (var q: u32 = 0u; q < 4u; q = q + 1u) {\n let qo = vec2<u32>((q & 1u) * 2u, (q >> 1u) * 2u);\n let cc = (vec2<f32>(base) + vec2<f32>(qo) + vec2<f32>(1.0, 1.0)) * inv_size;\n let r4 = textureGather(0, src_tex, smp, cc) * 255.0;\n let g4 = textureGather(1, src_tex, smp, cc) * 255.0;\n let i = qo.y * 4u + qo.x;\n let vw = vec2<f32>(r4.w, g4.w);\n let vz = vec2<f32>(r4.z, g4.z);\n let vx = vec2<f32>(r4.x, g4.x);\n let vy = vec2<f32>(r4.y, g4.y);\n v[i] = vw; v[i + 1u] = vz; v[i + 4u] = vx; v[i + 5u] = vy;\n vmin = min(min(vmin, min(vw, vz)), min(vx, vy));\n vmax = max(max(vmax, max(vw, vz)), max(vx, vy));\n }\n } else {\n let mx = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);\n let c = textureLoad(src_tex, p, 0);\n let val = vec2<f32>(c.r, c.g) * 255.0;\n v[i] = val; vmin = min(vmin, val); vmax = max(vmax, val);\n }\n }\n\n // Seed endpoints at the exact per-channel extremes (round-to-nearest, the\n // same rule the CPU reference uses). Flat blocks get nudged apart to keep\n // the 6-interp mode (r0 > r1 strictly).\n var r0 = vec2<u32>(clamp(floor(vmax + 0.5), vec2<f32>(0.0), vec2<f32>(255.0)));\n var r1 = vec2<u32>(clamp(floor(vmin + 0.5), vec2<f32>(0.0), vec2<f32>(255.0)));\n if (r0.x == r1.x) { if (r1.x > 0u) { r1.x = r1.x - 1u; } else { r0.x = r0.x + 1u; } }\n if (r0.y == r1.y) { if (r1.y > 0u) { r1.y = r1.y - 1u; } else { r0.y = r0.y + 1u; } }\n\n let r0f = vec2<f32>(r0);\n let r1f = vec2<f32>(r1);\n let dir = r1f - r0f;\n let scale = vec2<f32>(7.0) / dir;\n\n // Pass 1, both channels \u2014 MOMENTS only. t = 7(v\u2212r0)/(r1\u2212r0) \u2208 [0,7]\n // (the seed covers the data), L = round(t), \u03C1 = t \u2212 L.\n var sL = vec2<f32>(0.0); var sLL = vec2<f32>(0.0);\n var pR = vec2<f32>(0.0); var pLR = vec2<f32>(0.0);\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let t = (v[k] - r0f) * scale;\n let L = clamp(floor(t + 0.5), vec2<f32>(0.0), vec2<f32>(7.0));\n let rho = t - L;\n sL = sL + L; sLL = sLL + L * L;\n pR = pR + rho; pLR = pLR + L * rho;\n }\n\n // Per-block refit off the moments (see bc5_fast_f16.wgsl for the\n // identities).\n let sBB = sLL * (1.0 / 49.0);\n let sAB = sL * (1.0 / 7.0) - sBB;\n let sAA = vec2<f32>(16.0) - 2.0 * sL * (1.0 / 7.0) + sBB;\n let sBR = pLR * dir * (1.0 / 49.0);\n let sAR = (pR - pLR * (1.0 / 7.0)) * dir * (1.0 / 7.0);\n let spread = 16.0 * sLL != sL * sL;\n\n let fx = refine(sAA.x, sBB.x, sAB.x, sAR.x, sBR.x, r0.x, r1.x, spread.x);\n let fy = refine(sAA.y, sBB.y, sAB.y, sAR.y, sBR.y, r0.y, r1.y, spread.y);\n let n0 = vec2<u32>(fx.x, fy.x);\n let n1 = vec2<u32>(fx.y, fy.y);\n\n // Pass 2, both channels \u2014 pack the shipped indices against the FINAL\n // endpoints (rejected channels re-derive their seed assignment). iA\n // holds pixels 0..7 (3 bits each), iB pixels 8..15.\n let n0f = vec2<f32>(n0);\n let n1f = vec2<f32>(n1);\n let scale2 = vec2<f32>(7.0) / (n1f - n0f);\n var iAx = 0u; var iBx = 0u; var iAy = 0u; var iBy = 0u;\n for (var k: u32 = 0u; k < 8u; k = k + 1u) {\n let L = clamp(floor((v[k] - n0f) * scale2 + 0.5), vec2<f32>(0.0), vec2<f32>(7.0));\n iAx = iAx | (((IDX_LUT >> (u32(L.x) * 3u)) & 7u) << (k * 3u));\n iAy = iAy | (((IDX_LUT >> (u32(L.y) * 3u)) & 7u) << (k * 3u));\n }\n for (var k: u32 = 8u; k < 16u; k = k + 1u) {\n let L = clamp(floor((v[k] - n0f) * scale2 + 0.5), vec2<f32>(0.0), vec2<f32>(7.0));\n iBx = iBx | (((IDX_LUT >> (u32(L.x) * 3u)) & 7u) << ((k - 8u) * 3u));\n iBy = iBy | (((IDX_LUT >> (u32(L.y) * 3u)) & 7u) << ((k - 8u) * 3u));\n }\n\n // BC5 block = R half (bytes 0..7) || G half (bytes 8..15) = 4 u32s.\n let o = bi * 4u;\n dst[o] = n0.x | (n1.x << 8u) | (iAx << 16u);\n dst[o + 1u] = (iAx >> 16u) | (iBx << 8u);\n dst[o + 2u] = n0.y | (n1.y << 8u) | (iAy << 16u);\n dst[o + 3u] = (iAy >> 16u) | (iBy << 8u);\n}\n";
616
1246
 
617
1247
  // src/bc5_fast_f16.wgsl
618
1248
  var bc5_fast_f16_default = `// bc5 "fast" encoder \u2014 f16 variant (requires the shader-f16 feature).
619
- // Two BC4 halves (R and G) \u2014 same output family as bc5.wgsl's fast branch,
620
- // tuned for throughput:
1249
+ // Two BC4 halves (R and G) \u2014 same output family as bc5.wgsl, tuned for
1250
+ // throughput:
621
1251
  //
622
1252
  // \u2022 The 8-entry palette in 6-interpolation mode is COLINEAR and EVENLY
623
1253
  // spaced from r0 to r1 (levels 0..7 in palette order 0,2,3,4,5,6,7,1),
@@ -626,166 +1256,218 @@ var bc5_fast_f16_default = `// bc5 "fast" encoder \u2014 f16 variant (requires t
626
1256
  // \u2022 Math runs in the exact-integer [0,255] f16 domain: endpoints and pixel
627
1257
  // values are whole numbers \u2264 255 (exact in f16), so the only rounding is
628
1258
  // the single 1/(r1\u2212r0) division.
629
- // \u2022 ONE fused pass per channel: projection assignment + the least-squares
630
- // refit sums + the seed solution's packed indices and squared error. The
631
- // refit endpoints are re-quantised, reprojected, and accepted only if
632
- // the block error decreases (same accept-if-better family as the BC1
633
- // fast path) \u2014 worth ~1.3 dB on the normal-map card.
634
- // \u2022 3-bit indices are packed into the 48-bit field on the fly \u2014 no
635
- // array<u32,16> private array and no separate packing loop.
636
- //
637
- // f16 range notes: value sums accumulate v \u2212 r0 (the affine-basis shift trick
638
- // from the BC7/ASTC fast paths) scaled by 1/16, and error residuals are
639
- // scaled by 1/16 before squaring \u2014 worst-case magnitudes stay \u22724k, well
640
- // inside f16's 65504 max, with rounding a small fraction of a level. The
641
- // accept-if-better guard makes any residual f16 noise fail-safe (worst case:
642
- // the refit is rejected and the seed solution ships).
1259
+ // \u2022 BOTH channels ride the same fused passes as vec2<f16> lanes \u2014 each
1260
+ // loop computes projections and moments for R and G at once instead of
1261
+ // two scalar encode_bc4 calls.
1262
+ // \u2022 The 16 texel reads are 8 textureGather fetches (4 quads \xD7 R,G) for
1263
+ // interior blocks \u2014 byte-identical output to per-texel loads, \u22123.6%
1264
+ // GPU on 4096\xB2 (/ab, 2026-07). Blocks straddling the source edge of a
1265
+ // non-multiple-of-4 image use clamped per-texel loads instead: the
1266
+ // upload pads the texture with ZEROS, so a normalised-coordinate
1267
+ // gather there would read padding (or mis-scale against the padded
1268
+ // size) instead of replicating the last real texel.
1269
+ // \u2022 Pass 1 accumulates MOMENTS, not normal-equation sums. With b = L/7
1270
+ // and the level-space residual \u03C1 = t \u2212 L (t = 7(v\u2212r0)/(r1\u2212r0), so the
1271
+ // value-space residual is r = \u03C1\xB7dir/7), every LSQ sum is an O(1)
1272
+ // per-block function of four accumulators:
1273
+ // sBB = \u03A3L\xB2/49 sAB = \u03A3L/7 \u2212 \u03A3L\xB2/49 sAA = 16 \u2212 2\u03A3L/7 + \u03A3L\xB2/49
1274
+ // sBR = \u03A3L\u03C1\xB7dir/49 sAR = (\u03A3\u03C1 \u2212 \u03A3L\u03C1/7)\xB7dir/7
1275
+ // Per-pixel work drops from 5 product-accumulates + 3 temporaries to 4
1276
+ // cheap accumulates (\u03A3L, \u03A3L\xB2, \u03A3\u03C1, \u03A3L\u03C1), and the f16 range analysis
1277
+ // becomes trivial: \u03A3L \u2264 112 and \u03A3L\xB2 \u2264 784 are exact f16 integers,
1278
+ // |\u03C1| \u2264 ~0.5 keeps \u03A3\u03C1/\u03A3L\u03C1 tiny. The per-BLOCK refit math (including
1279
+ // the solve and E(\u03B4) pricing) runs in f32 \u2014 free at block granularity,
1280
+ // and it retires the sum-cancellation worries the old residual-sum
1281
+ // scheme was built around.
1282
+ // \u2022 The rank guard is EXACT: all pixels on one level \u27FA 16\xB7\u03A3L\xB2 == (\u03A3L)\xB2
1283
+ // (integers, so the comparison is precise in f32) \u2014 no lmin/lmax
1284
+ // tracking in the loop.
1285
+ // \u2022 The refit is accepted or rejected CLOSED-FORM, with no trial
1286
+ // projection pass: the solve is e = seed + M\u207B\xB9(sAR,sBR), and the error
1287
+ // of re-quantised endpoints ON THE CURRENT INDICES is
1288
+ // E(\u03B4) = err \u2212 2(\u03B40\xB7sAR + \u03B41\xB7sBR) + \u03B40\xB2sAA + 2\u03B40\u03B41\xB7sAB + \u03B41\xB2sBB
1289
+ // with \u03B4 = quantised endpoint \u2212 base endpoint, compared as the delta
1290
+ // form E \u2212 err < 0. Only the NEAREST rounding of the fractional solve
1291
+ // is priced: pricing all four floor/ceil combinations (the correlated
1292
+ // 2-D quadratic's integer optimum isn't always the nearest rounding)
1293
+ // measured \u22640.015 dB on every content class but ~8% GPU on smooth
1294
+ // content, where Apple's lossless texture compression collapses the
1295
+ // read cost and leaves the kernel ALU-bound (/ab 2026-07, rock 4K
1296
+ // displacement: floor 0.61\xD7 of the rgba8-noise floor).
1297
+ // \u2022 Pass 2 packs the indices ONCE, against the FINAL endpoints \u2014 full
1298
+ // reprojection quality. The previous single-pass scheme shipped the
1299
+ // SEED indices with accepted refit endpoints, giving up 0.08..0.19 dB
1300
+ // because a third reprojection loop cost +14% GPU; with the moment
1301
+ // form paying for the second loop, reprojection now measures at PARITY
1302
+ // with that scheme (/ab 2026-07, interleaved: proc 2048\xB2/4096\xB2 and the
1303
+ // rock 4K normal map, all within \xB11%, vs read floor ~2% below).
1304
+ // Measured and NOT taken: a second refit round off pass-2 moments
1305
+ // (+14%, register cliff, \u22480 gain on smooth content \u2014 round 2 only pays
1306
+ // on noise); full normal-equation sums in both passes (+49%).
1307
+ // \u2022 3-bit indices are packed BRANCH-FREE: pixels 0..7 accumulate into a
1308
+ // 24-bit word, pixels 8..15 into another, recombined with constant
1309
+ // shifts into the 48-bit field (w0 gets field bits 0..15 above the two
1310
+ // endpoint bytes, w1 gets field bits 16..47) \u2014 no per-pixel straddle
1311
+ // branches.
643
1312
  //
644
1313
  // Level \u2192 BC4 index (0\u2192r0 ... 7\u2192r1): 0,2,3,4,5,6,7,1 \u2014 packed 3-bit LUT
645
1314
  // 0x3F58D0 = sum(idx[L] << 3L).
646
1315
  //
647
1316
  // The host selects this module only when the device reports shader-f16,
648
- // falling back to bc5.wgsl otherwise. "high" never uses this.
1317
+ // falling back to bc5.wgsl otherwise.
649
1318
  enable f16;
650
1319
  alias h = f16;
1320
+ alias h2 = vec2<f16>;
651
1321
  struct Params { blocks_x: u32, blocks_y: u32, width: u32, height: u32, };
652
1322
  @group(0) @binding(0) var src_tex: texture_2d<f32>;
653
1323
  @group(0) @binding(1) var<storage, read_write> dst: array<u32>;
654
1324
  @group(0) @binding(2) var<uniform> params: Params;
655
-
656
- // Project the 16 values onto the r0\u2192r1 axis, packing the 3-bit indices on the
657
- // fly (pixel k's index starts at bit 3k of the 48-bit field, i.e. bit 3k+16
658
- // of the (w0,w1) pair; k = 5 straddles the word boundary) and accumulating
659
- // the squared error (residuals scaled by 1/16 before squaring). Returns the
660
- // packed words with the endpoint bytes already in place.
661
- struct Proj { w0: u32, w1: u32, err: h };
662
- fn project_pack(values: ptr<function, array<h, 16>>, r0: u32, r1: u32) -> Proj {
663
- let r0f = h(f32(r0));
664
- let dir = h(f32(r1)) - r0f;
665
- let scale = h(7.0) / dir;
666
- var out: Proj;
667
- out.w0 = r0 | (r1 << 8u);
668
- out.w1 = 0u;
669
- out.err = h(0.0);
670
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
671
- let vr = (*values)[k] - r0f;
672
- let L = clamp(floor(vr * scale + h(0.5)), h(0.0), h(7.0));
673
- let e = (vr - L * h(1.0 / 7.0) * dir) * h(1.0 / 16.0);
674
- out.err = out.err + e * e;
675
- let idx = (0x3F58D0u >> (u32(L) * 3u)) & 7u;
676
- let bit = 3u * k + 16u;
677
- if (bit <= 29u) {
678
- out.w0 = out.w0 | (idx << bit);
679
- } else if (bit >= 32u) {
680
- out.w1 = out.w1 | (idx << (bit - 32u));
681
- } else {
682
- out.w0 = out.w0 | (idx << bit);
683
- out.w1 = out.w1 | (idx >> (32u - bit));
1325
+ @group(0) @binding(3) var smp: sampler;
1326
+
1327
+ const IDX_LUT: u32 = 0x3F58D0u;
1328
+
1329
+ // Closed-form accept-if-better endpoint refinement for one channel (f32:
1330
+ // per-block O(1) work, so precision is free here). Takes the LSQ sums for
1331
+ // the current indices, the current integer endpoints b0 > b1, and the rank
1332
+ // guard; prices the nearest rounding of the fractional solve via E(\u03B4) and
1333
+ // returns it when it strictly improves and stays in 6-interp mode
1334
+ // (q0 > q1), or (b0,b1) unchanged. Endpoints clamp to [0,255], NOT the
1335
+ // block's value range: for a scalar channel, endpoints beyond the data
1336
+ // range are often genuinely optimal and there is no colour axis to bend
1337
+ // (the bbox clamp the colour formats need costs ~0.3 dB here).
1338
+ fn refine(sAA: f32, sBB: f32, sAB: f32, sAR: f32, sBR: f32, b0: u32, b1: u32, spread: bool) -> vec2<u32> {
1339
+ var out = vec2<u32>(b0, b1);
1340
+ let det = sAA * sBB - sAB * sAB;
1341
+ if (!spread || abs(det) <= 1e-3) { return out; }
1342
+ let b0f = f32(b0);
1343
+ let b1f = f32(b1);
1344
+ let e0 = clamp(b0f + (sBB * sAR - sAB * sBR) / det, 0.0, 255.0);
1345
+ let e1 = clamp(b1f + (sAA * sBR - sAB * sAR) / det, 0.0, 255.0);
1346
+ let q0f = floor(e0 + 0.5);
1347
+ let q1f = floor(e1 + 0.5);
1348
+ let q0 = u32(q0f);
1349
+ let q1 = u32(q1f);
1350
+ if (q0 > q1 && !(q0 == b0 && q1 == b1)) {
1351
+ let dd0 = q0f - b0f;
1352
+ let dd1 = q1f - b1f;
1353
+ let eNew = -2.0 * (dd0 * sAR + dd1 * sBR)
1354
+ + dd0 * dd0 * sAA + 2.0 * dd0 * dd1 * sAB + dd1 * dd1 * sBB;
1355
+ if (eNew < 0.0) {
1356
+ out = vec2<u32>(q0, q1);
684
1357
  }
685
1358
  }
686
1359
  return out;
687
1360
  }
688
1361
 
689
- // Encode one channel (16 values in exact-integer [0,255] f16) to a BC4 half.
690
- // vmin/vmax are the channel's min/max, computed in the caller's load loop \u2014
691
- // fusing that scan there saves a 16-value pass per channel.
692
- fn encode_bc4(values: ptr<function, array<h, 16>>, vmin: h, vmax: h) -> vec2<u32> {
693
- var r0 = u32(vmax); // values are exact integers \u2014 no rounding needed
694
- var r1 = u32(vmin);
695
- if (r0 == r1) {
696
- // Flat block: nudge to keep 6-interp mode (r0 > r1 strictly).
697
- if (r1 > 0u) { r1 = r1 - 1u; } else { r0 = r0 + 1u; }
698
- }
699
-
700
- // Fused seed pass: projection assignment (level = round(7\xB7(v \u2212 r0)/
701
- // (r1 \u2212 r0)), clamped \u2014 |v \u2212 r0| \u2264 r0 \u2212 r1 for in-block values) + packed
702
- // indices + seed error + the LSQ normal-equation sums. Value sums
703
- // accumulate (v \u2212 r0)/16: the shift keeps the accumulators proportional to
704
- // the block's span, the exact power-of-two scale keeps products \u2264 4080.
705
- let r0f = h(f32(r0));
706
- let dir = h(f32(r1)) - r0f;
707
- let scale = h(7.0) / dir;
708
- var seed: Proj;
709
- seed.w0 = r0 | (r1 << 8u);
710
- seed.w1 = 0u;
711
- seed.err = h(0.0);
712
- var sAA = h(0.0); var sBB = h(0.0); var sAB = h(0.0);
713
- var sAV = h(0.0); var sBV = h(0.0);
714
- var s_min = h(7.0); var s_max = h(0.0);
715
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
716
- let vr = (*values)[k] - r0f;
717
- let L = clamp(floor(vr * scale + h(0.5)), h(0.0), h(7.0));
718
- s_min = min(s_min, L); s_max = max(s_max, L);
719
- let b = L * h(1.0 / 7.0); let a = h(1.0) - b;
720
- let vr16 = vr * h(1.0 / 16.0);
721
- sAA = sAA + a * a; sBB = sBB + b * b; sAB = sAB + a * b;
722
- sAV = sAV + a * vr16; sBV = sBV + b * vr16;
723
- let e = vr16 - b * dir * h(1.0 / 16.0);
724
- seed.err = seed.err + e * e;
725
- let idx = (0x3F58D0u >> (u32(L) * 3u)) & 7u;
726
- let bit = 3u * k + 16u;
727
- if (bit <= 29u) {
728
- seed.w0 = seed.w0 | (idx << bit);
729
- } else if (bit >= 32u) {
730
- seed.w1 = seed.w1 | (idx << (bit - 32u));
731
- } else {
732
- // k = 5 straddles the word boundary (bits 31..33).
733
- seed.w0 = seed.w0 | (idx << bit);
734
- seed.w1 = seed.w1 | (idx >> (32u - bit));
735
- }
736
- }
737
-
738
- // LSQ refit, accepted only if the requantised endpoints lower the block
739
- // error. Rank-1 guard: with every pixel on ONE level the system is
740
- // singular and det is pure f16 rounding noise (\u22720.03); with \u22652 distinct
741
- // levels det = \u03A3_i<j (b_j \u2212 b_i)\xB2 \u2265 15/49 \u2248 0.306 \u2014 0.1 separates cleanly.
742
- if (s_min < s_max) {
743
- let det = sAA * sBB - sAB * sAB;
744
- if (abs(det) > h(0.1)) {
745
- // \xD716 undoes the accumulator scale. Clamp to [0,255], NOT the block's
746
- // value range: for a scalar channel, endpoints beyond the data range
747
- // are often genuinely optimal (they centre the palette levels on the
748
- // data) and there is no colour axis to bend \u2014 the bbox clamp the
749
- // colour formats need costs ~0.3 dB here. The accept-if-better guard
750
- // still protects against a refit that loses after quantisation.
751
- let e0 = clamp(r0f + (sBB * sAV - sAB * sBV) * h(16.0) / det, h(0.0), h(255.0));
752
- let e1 = clamp(r0f + (sAA * sBV - sAB * sAV) * h(16.0) / det, h(0.0), h(255.0));
753
- let n0 = u32(floor(e0 + h(0.5)));
754
- let n1 = u32(floor(e1 + h(0.5)));
755
- // Keep 6-interp mode (r0 > r1 strictly); skip the no-op refit.
756
- if (n0 > n1 && !(n0 == r0 && n1 == r1)) {
757
- let refit = project_pack(values, n0, n1);
758
- if (refit.err < seed.err) {
759
- return vec2<u32>(refit.w0, refit.w1);
760
- }
761
- }
762
- }
763
- }
764
- return vec2<u32>(seed.w0, seed.w1);
765
- }
766
-
767
1362
  @compute @workgroup_size(8, 8, 1)
768
1363
  fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
769
1364
  if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) { return; }
770
1365
  let bi = gid.y * params.blocks_x + gid.x;
771
1366
  let base = vec2<i32>(i32(gid.x) * 4, i32(gid.y) * 4);
772
- let mx = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);
773
- var rv: array<h, 16>;
774
- var gv: array<h, 16>;
775
- var rmin = h(255.0); var rmax = h(0.0);
776
- var gmin = h(255.0); var gmax = h(0.0);
777
- for (var i: u32 = 0u; i < 16u; i = i + 1u) {
778
- let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);
779
- let c = textureLoad(src_tex, p, 0);
780
- let r = h(c.r * 255.0);
781
- let g = h(c.g * 255.0);
782
- rv[i] = r; rmin = min(rmin, r); rmax = max(rmax, r);
783
- gv[i] = g; gmin = min(gmin, g); gmax = max(gmax, g);
784
- }
785
- let rb = encode_bc4(&rv, rmin, rmax);
786
- let gb = encode_bc4(&gv, gmin, gmax);
1367
+
1368
+ // Load 4\xD74 R/G pairs (x = R, y = G throughout), min/max fused in.
1369
+ // INTERIOR blocks \u2014 every block when the source is a multiple of 4, so
1370
+ // the branch is wavefront-uniform on benchmark-shaped content \u2014 read via
1371
+ // 8 gathers (4 quads \xD7 R,G); the gather point (base+quad+1) normalised by
1372
+ // the PHYSICAL (padded) texture size sits exactly between the quad's
1373
+ // texel centers, and interior quads never touch the zero-initialised
1374
+ // padding strip. Blocks straddling the source edge of a
1375
+ // non-multiple-of-4 image fall back to per-texel loads clamped to the
1376
+ // last real texel (gather cannot replicate an edge texel mid-quad).
1377
+ // Gather components: w=(0,0) z=(1,0) x=(0,1) y=(1,1) within each quad.
1378
+ var v: array<h2, 16>;
1379
+ var vmin = h2(255.0);
1380
+ var vmax = h2(0.0);
1381
+ if (u32(base.x) + 4u <= params.width && u32(base.y) + 4u <= params.height) {
1382
+ let inv_size = vec2<f32>(1.0, 1.0) / vec2<f32>(textureDimensions(src_tex));
1383
+ for (var q: u32 = 0u; q < 4u; q = q + 1u) {
1384
+ let qo = vec2<u32>((q & 1u) * 2u, (q >> 1u) * 2u);
1385
+ let cc = (vec2<f32>(base) + vec2<f32>(qo) + vec2<f32>(1.0, 1.0)) * inv_size;
1386
+ let r4 = textureGather(0, src_tex, smp, cc) * 255.0;
1387
+ let g4 = textureGather(1, src_tex, smp, cc) * 255.0;
1388
+ let i = qo.y * 4u + qo.x;
1389
+ let vw = h2(h(r4.w), h(g4.w));
1390
+ let vz = h2(h(r4.z), h(g4.z));
1391
+ let vx = h2(h(r4.x), h(g4.x));
1392
+ let vy = h2(h(r4.y), h(g4.y));
1393
+ v[i] = vw; v[i + 1u] = vz; v[i + 4u] = vx; v[i + 5u] = vy;
1394
+ vmin = min(min(vmin, min(vw, vz)), min(vx, vy));
1395
+ vmax = max(max(vmax, max(vw, vz)), max(vx, vy));
1396
+ }
1397
+ } else {
1398
+ let mx = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);
1399
+ for (var i: u32 = 0u; i < 16u; i = i + 1u) {
1400
+ let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);
1401
+ let c = textureLoad(src_tex, p, 0);
1402
+ let val = h2(h(c.r * 255.0), h(c.g * 255.0));
1403
+ v[i] = val; vmin = min(vmin, val); vmax = max(vmax, val);
1404
+ }
1405
+ }
1406
+
1407
+ // Seed endpoints at the exact per-channel extremes (values are exact
1408
+ // integers \u2014 no rounding needed). Flat blocks get nudged apart to keep
1409
+ // the 6-interp mode (r0 > r1 strictly).
1410
+ var r0 = vec2<u32>(vmax);
1411
+ var r1 = vec2<u32>(vmin);
1412
+ if (r0.x == r1.x) { if (r1.x > 0u) { r1.x = r1.x - 1u; } else { r0.x = r0.x + 1u; } }
1413
+ if (r0.y == r1.y) { if (r1.y > 0u) { r1.y = r1.y - 1u; } else { r0.y = r0.y + 1u; } }
1414
+
1415
+ let r0f = h2(vec2<f32>(r0));
1416
+ let r1f = h2(vec2<f32>(r1));
1417
+ let dir = r1f - r0f;
1418
+ let scale = h2(7.0) / dir;
1419
+
1420
+ // Pass 1, both channels \u2014 MOMENTS only. t = 7(v\u2212r0)/(r1\u2212r0) \u2208 [0,7]
1421
+ // (the seed covers the data), L = round(t), \u03C1 = t \u2212 L.
1422
+ var sL = h2(0.0); var sLL = h2(0.0); var pR = h2(0.0); var pLR = h2(0.0);
1423
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1424
+ let t = (v[k] - r0f) * scale;
1425
+ let L = clamp(floor(t + h2(0.5)), h2(0.0), h2(7.0));
1426
+ let rho = t - L;
1427
+ sL = sL + L; sLL = sLL + L * L;
1428
+ pR = pR + rho; pLR = pLR + L * rho;
1429
+ }
1430
+
1431
+ // Per-block refit in f32 off the moments (see header for the identities).
1432
+ let sLf = vec2<f32>(sL);
1433
+ let sLLf = vec2<f32>(sLL);
1434
+ let dirf = vec2<f32>(r1) - vec2<f32>(r0);
1435
+ let sBB = sLLf * (1.0 / 49.0);
1436
+ let sAB = sLf * (1.0 / 7.0) - sBB;
1437
+ let sAA = vec2<f32>(16.0) - 2.0 * sLf * (1.0 / 7.0) + sBB;
1438
+ let sBR = vec2<f32>(pLR) * dirf * (1.0 / 49.0);
1439
+ let sAR = (vec2<f32>(pR) - vec2<f32>(pLR) * (1.0 / 7.0)) * dirf * (1.0 / 7.0);
1440
+ let spread = 16.0 * sLLf != sLf * sLf;
1441
+
1442
+ let fx = refine(sAA.x, sBB.x, sAB.x, sAR.x, sBR.x, r0.x, r1.x, spread.x);
1443
+ let fy = refine(sAA.y, sBB.y, sAB.y, sAR.y, sBR.y, r0.y, r1.y, spread.y);
1444
+ let n0 = vec2<u32>(fx.x, fy.x);
1445
+ let n1 = vec2<u32>(fx.y, fy.y);
1446
+
1447
+ // Pass 2, both channels \u2014 pack the shipped indices against the FINAL
1448
+ // endpoints (rejected channels re-derive their seed assignment). iA
1449
+ // holds pixels 0..7 (3 bits each), iB pixels 8..15.
1450
+ let n0f = h2(vec2<f32>(n0));
1451
+ let n1f = h2(vec2<f32>(n1));
1452
+ let scale2 = h2(7.0) / (n1f - n0f);
1453
+ var iAx = 0u; var iBx = 0u; var iAy = 0u; var iBy = 0u;
1454
+ for (var k: u32 = 0u; k < 8u; k = k + 1u) {
1455
+ let L = clamp(floor((v[k] - n0f) * scale2 + h2(0.5)), h2(0.0), h2(7.0));
1456
+ iAx = iAx | (((IDX_LUT >> (u32(L.x) * 3u)) & 7u) << (k * 3u));
1457
+ iAy = iAy | (((IDX_LUT >> (u32(L.y) * 3u)) & 7u) << (k * 3u));
1458
+ }
1459
+ for (var k: u32 = 8u; k < 16u; k = k + 1u) {
1460
+ let L = clamp(floor((v[k] - n0f) * scale2 + h2(0.5)), h2(0.0), h2(7.0));
1461
+ iBx = iBx | (((IDX_LUT >> (u32(L.x) * 3u)) & 7u) << ((k - 8u) * 3u));
1462
+ iBy = iBy | (((IDX_LUT >> (u32(L.y) * 3u)) & 7u) << ((k - 8u) * 3u));
1463
+ }
1464
+
1465
+ // BC5 block = R half (bytes 0..7) || G half (bytes 8..15) = 4 u32s.
787
1466
  let o = bi * 4u;
788
- dst[o] = rb.x; dst[o + 1u] = rb.y; dst[o + 2u] = gb.x; dst[o + 3u] = gb.y;
1467
+ dst[o] = n0.x | (n1.x << 8u) | (iAx << 16u);
1468
+ dst[o + 1u] = (iAx >> 16u) | (iBx << 8u);
1469
+ dst[o + 2u] = n0.y | (n1.y << 8u) | (iAy << 16u);
1470
+ dst[o + 3u] = (iAy >> 16u) | (iBy << 8u);
789
1471
  }
790
1472
  `;
791
1473
 
@@ -802,6 +1484,14 @@ var BC5Encoder = class extends Encoder {
802
1484
  get supportsSrgb() {
803
1485
  return false;
804
1486
  }
1487
+ // BC5 stores only R and G — a two-channel source texture halves the
1488
+ // encode pass's DRAM reads. Measured on the pass (/ab 2026-07): −4% on a
1489
+ // real 4K normal map, −8..−12% on procedural 2048²/4096², and the raw
1490
+ // read floor itself drops 35%, so the win grows as the kernel's ALU
1491
+ // shrinks. Also halves source-texture memory during encodes.
1492
+ get srcTextureFormat() {
1493
+ return "rg8unorm";
1494
+ }
805
1495
  wgslSource() {
806
1496
  return bc5_default;
807
1497
  }
@@ -814,202 +1504,25 @@ var BC5Encoder = class extends Encoder {
814
1504
  };
815
1505
 
816
1506
  // src/bc7.wgsl
817
- var bc7_default = "// BC7 (BPTC) mode 6 compute shader encoder.\n//\n// One invocation per 4\xD74 block. Emits 16 bytes = 4 u32s into the storage\n// buffer at `dst[block_index * 4 .. + 3]`. This is the f32 fallback;\n// bc7_fast_f16.wgsl is the same algorithm and is preferred when the device\n// reports shader-f16.\n//\n// ALGORITHM: principal-axis seed (covariance power-iteration; bbox on\n// degenerate blocks) at the exact projection extents, quantised directly \u2014\n// no LSQ refit; with the seed on the principal axis, mode 6's 16-level\n// palette leaves the refit under 0.15 dB, unlike the 4-level BC1/ASTC\n// encoders which keep theirs \u2014 then one pass that projects each pixel onto\n// the endpoint line (the 16 palette entries are colinear, so the nearest\n// index is the rounded projection \u2014 no palette build, no 16-entry search),\n// packed on the fly into two nibble words.\n//\n// MODE 6 LAYOUT (LSB-first, bit 0 = byte 0's bit 0)\n// bits 0..6 mode field (0b0000001 \u2014 only bit 6 is 1)\n// bits 7..13 R0 (7-bit) bits 14..20 R1 bits 21..27 G0 bits 28..34 G1\n// bits 35..41 B0 bits 42..48 B1 bits 49..55 A0 bits 56..62 A1\n// bit 63 P0 bit 64 P1\n// bits 65..67 pixel 0 index (3 bits; anchor, MSB implicit 0)\n// bits 68..71 pixel 1 index (4 bits) ... bits 124..127 pixel 15 index\n//\n// Effective 8-bit endpoint channel = (7_bit_value << 1) | p_bit.\n// Palette[i] = ((64 \u2212 W4[i]) \xD7 e0_8 + W4[i] \xD7 e1_8 + 32) >> 6, integer.\n//\n// The block is assembled with straight-line constant shifts (see the layout\n// summary in bc7_fast_f16.wgsl) \u2014 a generic write_bits() helper's dynamic\n// word indexing keeps the output array out of registers.\n\nstruct Params {\n blocks_x: u32,\n blocks_y: u32,\n width: u32,\n height: u32,\n};\n\n@group(0) @binding(0) var src_tex: texture_2d<f32>;\n@group(0) @binding(1) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(2) var<uniform> params: Params;\n\nfn to8(v: vec4<f32>) -> vec4<i32> {\n return vec4<i32>(clamp(floor(v * 255.0 + 0.5), vec4<f32>(0.0), vec4<f32>(255.0)));\n}\n\nfn dist2(a: vec4<i32>, b: vec4<i32>) -> i32 {\n let d = a - b;\n let e = d * d;\n return e.x + e.y + e.z + e.w;\n}\n\n// Quantize an 8-bit ideal endpoint to (7-bit value, reconstructed 8-bit)\n// under a fixed p-bit, all four channels at once. q7 = round((ideal8 \u2212 p)/2).\nstruct QuantPair { seven: vec4<i32>, eight: vec4<i32> };\nfn quantize_endpoint(ideal8: vec4<i32>, p: u32) -> QuantPair {\n let q = vec4<i32>(clamp(\n floor((vec4<f32>(ideal8) - f32(p)) / 2.0 + 0.5),\n vec4<f32>(0.0), vec4<f32>(127.0),\n ));\n let eff = (q << vec4<u32>(1u)) | vec4<i32>(i32(p));\n return QuantPair(q, eff);\n}\n\n// Endpoint with its chosen p-bit, picked by minimum quantisation error.\nstruct Ep { seven: vec4<i32>, eight: vec4<i32>, p: u32 };\nfn pick_ep(ideal: vec4<i32>) -> Ep {\n let a = quantize_endpoint(ideal, 0u);\n let b = quantize_endpoint(ideal, 1u);\n if (dist2(b.eight, ideal) < dist2(a.eight, ideal)) { return Ep(b.seven, b.eight, 1u); }\n return Ep(a.seven, a.eight, 0u);\n}\n\n// Principal colour axis via power-iteration over precomputed, mean-corrected\n// covariance rows (the moments are accumulated for free in the pixel-load\n// loop), seeded with the bbox diagonal. Returns a unit axis, or vec4(0) for\n// a degenerate (constant) block. Same family as bc1.wgsl's principal_axis \u2014\n// the bbox diagonal alone is sign-blind and points across anti-correlated\n// data (normal maps, hue edges) instead of along it.\nfn principal_axis4(\n c0v: vec4<f32>,\n c1v: vec4<f32>,\n c2v: vec4<f32>,\n c3v: vec4<f32>,\n seed: vec4<f32>,\n) -> vec4<f32> {\n var v = seed;\n var len = length(v);\n if (len < 1e-9) { return vec4<f32>(0.0); }\n v = v / len;\n for (var iter: u32 = 0u; iter < 8u; iter = iter + 1u) {\n let nv = vec4<f32>(dot(c0v, v), dot(c1v, v), dot(c2v, v), dot(c3v, v));\n len = length(nv);\n if (len < 1e-12) { return vec4<f32>(0.0); }\n v = nv / len;\n }\n return v;\n}\n\n// ------------------------------- Entry --------------------------------- //\n\n@compute @workgroup_size(8, 8, 1)\nfn encode(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) {\n return;\n }\n\n let bx = gid.x;\n let by = gid.y;\n let block_index = by * params.blocks_x + bx;\n\n let base = vec2<i32>(i32(bx) * 4, i32(by) * 4);\n let max_xy = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);\n\n // Load 16 RGBA pixels (8-bit integer domain) and the per-channel bbox,\n // with the covariance moments FUSED in: d = px \u2212 pixel0 (first-pixel-\n // relative, so the sums scale with the block's span; d is integer-valued\n // and \u2264255, exact in f32).\n var pixels: array<vec4<i32>, 16>;\n var lo = vec4<i32>(255);\n var hi = vec4<i32>(0);\n var p0f = vec4<f32>(0.0);\n var sd = vec4<f32>(0.0);\n var c0v = vec4<f32>(0.0);\n var c1v = vec4<f32>(0.0);\n var c2v = vec4<f32>(0.0);\n var c3v = vec4<f32>(0.0);\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let lx = i32(i & 3u);\n let ly = i32(i >> 2u);\n let p = clamp(base + vec2<i32>(lx, ly), vec2<i32>(0, 0), max_xy);\n let px = to8(textureLoad(src_tex, p, 0));\n pixels[i] = px;\n lo = min(lo, px);\n hi = max(hi, px);\n if (i == 0u) { p0f = vec4<f32>(px); }\n let d = vec4<f32>(px) - p0f;\n sd = sd + d;\n c0v = c0v + d.x * d;\n c1v = c1v + d.y * d;\n c2v = c2v + d.z * d;\n c3v = c3v + d.w * d;\n }\n let mean = p0f + sd * (1.0 / 16.0);\n\n // Seed endpoints from the block's principal colour axis at the exact\n // projection extents (see header), quantise, and assign indices in one\n // projection pass.\n // Mean-correct the fused moments: C = \u03A3dd\u1D40 \u2212 (\u03A3d)(\u03A3d)\u1D40/16.\n let sd16 = sd * (1.0 / 16.0);\n let r0v = c0v - sd.x * sd16;\n let r1v = c1v - sd.y * sd16;\n let r2v = c2v - sd.z * sd16;\n let r3v = c3v - sd.w * sd16;\n var seed0 = lo;\n var seed1 = hi;\n let axis = principal_axis4(r0v, r1v, r2v, r3v, vec4<f32>(hi - lo));\n if (dot(axis, axis) > 0.0) {\n // Exact projection extents along the axis. (A Rayleigh-quotient span\n // estimate was tried in place of this pass \u2014 it saves 16 dots but\n // costs 0.1\u20130.8 dB and 4\u201310\xD7 on the worst-easy-block gate: \u03C3\n // misjudges two-cluster and outlier blocks. The pass stays.)\n var t_min: f32 = 1e30;\n var t_max: f32 = -1e30;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let t = dot(vec4<f32>(pixels[k]) - mean, axis);\n t_min = min(t_min, t);\n t_max = max(t_max, t);\n }\n seed0 = vec4<i32>(clamp(round(mean + t_min * axis), vec4<f32>(0.0), vec4<f32>(255.0)));\n seed1 = vec4<i32>(clamp(round(mean + t_max * axis), vec4<f32>(0.0), vec4<f32>(255.0)));\n }\n\n // The 16 4-bit indices, packed LSB-first into two nibble words\n // (pixel k \u2192 bits 4k..4k+3).\n var ilo: u32 = 0u;\n var ihi: u32 = 0u;\n var ep0 = pick_ep(seed0);\n var ep1 = pick_ep(seed1);\n let dir = vec4<f32>(ep1.eight - ep0.eight);\n let dd = dot(dir, dir);\n if (dd > 0.0) {\n let e0f = vec4<f32>(ep0.eight);\n let inv = 15.0 / dd;\n for (var k: u32 = 0u; k < 8u; k = k + 1u) {\n let s = clamp(floor(dot(vec4<f32>(pixels[k]) - e0f, dir) * inv + 0.5), 0.0, 15.0);\n ilo = ilo | (u32(s) << (k * 4u));\n }\n for (var k: u32 = 8u; k < 16u; k = k + 1u) {\n let s = clamp(floor(dot(vec4<f32>(pixels[k]) - e0f, dir) * inv + 0.5), 0.0, 15.0);\n ihi = ihi | (u32(s) << ((k - 8u) * 4u));\n }\n }\n var e0_7 = ep0.seven;\n var e1_7 = ep1.seven;\n var p0 = ep0.p;\n var p1 = ep1.p;\n\n // Anchor rule \u2014 pixel 0's index MSB must be 0. Swapping endpoints reflects\n // every index (i \u2192 15\u2212i), which on packed nibbles is a bitwise NOT.\n if ((ilo & 0x8u) != 0u) {\n let t7 = e0_7; e0_7 = e1_7; e1_7 = t7;\n let tp = p0; p0 = p1; p1 = tp;\n ilo = ~ilo; ihi = ~ihi;\n }\n\n // Straight-line mode-6 packing (see layout at the top of the file).\n let e0 = vec4<u32>(e0_7);\n let e1 = vec4<u32>(e1_7);\n let w0 = 0x40u | (e0.x << 7u) | (e1.x << 14u) | (e0.y << 21u) | (e1.y << 28u);\n let w1 = (e1.y >> 4u) | (e0.z << 3u) | (e1.z << 10u) | (e0.w << 17u) | (e1.w << 24u) | (p0 << 31u);\n let w2 = p1 | ((ilo & 0x7u) << 1u) | (ilo & 0xFFFFFFF0u);\n let w3 = ihi;\n\n let out = block_index * 4u;\n dst[out + 0u] = w0;\n dst[out + 1u] = w1;\n dst[out + 2u] = w2;\n dst[out + 3u] = w3;\n}\n";
1507
+ var bc7_default = "// BC7 (BPTC) mode 6 compute shader encoder.\n//\n// One invocation per 4\xD74 block. Emits 16 bytes = 4 u32s into the storage\n// buffer at `dst[block_index * 4 .. + 3]`. This is the f32 fallback;\n// bc7_fast_f16.wgsl is the same algorithm and is preferred when the device\n// reports shader-f16.\n//\n// ALGORITHM: principal-axis seed (covariance power-iteration; bbox on\n// degenerate blocks) at the exact projection extents, quantised directly \u2014\n// no LSQ refit; with the seed on the principal axis, mode 6's 16-level\n// palette leaves the refit under 0.15 dB, unlike the 4-level BC1/ASTC\n// encoders which keep theirs \u2014 then one pass that projects each pixel onto\n// the endpoint line (the 16 palette entries are colinear, so the nearest\n// index is the rounded projection \u2014 no palette build, no 16-entry search),\n// packed on the fly into two nibble words.\n//\n// A MODE 1 (2-subset) candidate was built and evaluated (2026-07) and\n// dropped: ~+1.3 dB on multi-modal content but up to ~3\xD7 the pass cost on\n// exactly that content \u2014 see bc7_fast_f16.wgsl. The CPU reference decoder\n// keeps mode 1 support (bc7_ref.ts).\n//\n// MODE 6 LAYOUT (LSB-first, bit 0 = byte 0's bit 0)\n// bits 0..6 mode field (0b0000001 \u2014 only bit 6 is 1)\n// bits 7..13 R0 (7-bit) bits 14..20 R1 bits 21..27 G0 bits 28..34 G1\n// bits 35..41 B0 bits 42..48 B1 bits 49..55 A0 bits 56..62 A1\n// bit 63 P0 bit 64 P1\n// bits 65..67 pixel 0 index (3 bits; anchor, MSB implicit 0)\n// bits 68..71 pixel 1 index (4 bits) ... bits 124..127 pixel 15 index\n//\n// Effective 8-bit endpoint channel = (7_bit_value << 1) | p_bit.\n// Palette[i] = ((64 \u2212 W4[i]) \xD7 e0_8 + W4[i] \xD7 e1_8 + 32) >> 6, integer.\n//\n// The block is assembled with straight-line constant shifts (see the layout\n// summary in bc7_fast_f16.wgsl) \u2014 a generic write_bits() helper's dynamic\n// word indexing keeps the output array out of registers.\n\nstruct Params {\n blocks_x: u32,\n blocks_y: u32,\n width: u32,\n height: u32,\n};\n\n@group(0) @binding(0) var src_tex: texture_2d<f32>;\n@group(0) @binding(1) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(2) var<uniform> params: Params;\n\nfn to8(v: vec4<f32>) -> vec4<i32> {\n return vec4<i32>(clamp(floor(v * 255.0 + 0.5), vec4<f32>(0.0), vec4<f32>(255.0)));\n}\n\nfn dist2(a: vec4<i32>, b: vec4<i32>) -> i32 {\n let d = a - b;\n let e = d * d;\n return e.x + e.y + e.z + e.w;\n}\n\n// Quantize an 8-bit ideal endpoint to (7-bit value, reconstructed 8-bit)\n// under a fixed p-bit, all four channels at once. q7 = round((ideal8 \u2212 p)/2).\nstruct QuantPair { seven: vec4<i32>, eight: vec4<i32> };\nfn quantize_endpoint(ideal8: vec4<i32>, p: u32) -> QuantPair {\n let q = vec4<i32>(clamp(\n floor((vec4<f32>(ideal8) - f32(p)) / 2.0 + 0.5),\n vec4<f32>(0.0), vec4<f32>(127.0),\n ));\n let eff = (q << vec4<u32>(1u)) | vec4<i32>(i32(p));\n return QuantPair(q, eff);\n}\n\n// Endpoint with its chosen p-bit, picked by minimum quantisation error.\nstruct Ep { seven: vec4<i32>, eight: vec4<i32>, p: u32 };\nfn pick_ep(ideal: vec4<i32>) -> Ep {\n let a = quantize_endpoint(ideal, 0u);\n let b = quantize_endpoint(ideal, 1u);\n if (dist2(b.eight, ideal) < dist2(a.eight, ideal)) { return Ep(b.seven, b.eight, 1u); }\n return Ep(a.seven, a.eight, 0u);\n}\n\n// Principal colour axis via power-iteration over precomputed, mean-corrected\n// covariance rows (the moments are accumulated for free in the pixel-load\n// loop), seeded with the bbox diagonal. Returns a unit axis, or vec4(0) for\n// a degenerate (constant) block. Same family as bc1.wgsl's principal_axis \u2014\n// the bbox diagonal alone is sign-blind and points across anti-correlated\n// data (normal maps, hue edges) instead of along it.\nfn principal_axis4(\n c0v: vec4<f32>,\n c1v: vec4<f32>,\n c2v: vec4<f32>,\n c3v: vec4<f32>,\n seed: vec4<f32>,\n) -> vec4<f32> {\n var v = seed;\n var len = length(v);\n if (len < 1e-9) { return vec4<f32>(0.0); }\n v = v / len;\n for (var iter: u32 = 0u; iter < 8u; iter = iter + 1u) {\n let nv = vec4<f32>(dot(c0v, v), dot(c1v, v), dot(c2v, v), dot(c3v, v));\n len = length(nv);\n if (len < 1e-12) { return vec4<f32>(0.0); }\n v = nv / len;\n }\n return v;\n}\n\n\n// ------------------------------- Entry --------------------------------- //\n\n@compute @workgroup_size(8, 8, 1)\nfn encode(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) {\n return;\n }\n\n let bx = gid.x;\n let by = gid.y;\n let block_index = by * params.blocks_x + bx;\n\n let base = vec2<i32>(i32(bx) * 4, i32(by) * 4);\n let max_xy = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);\n\n // Load 16 RGBA pixels (8-bit integer domain) and the per-channel bbox,\n // with the covariance moments FUSED in: d = px \u2212 pixel0 (first-pixel-\n // relative, so the sums scale with the block's span; d is integer-valued\n // and \u2264255, exact in f32).\n var pixels: array<vec4<i32>, 16>;\n var lo = vec4<i32>(255);\n var hi = vec4<i32>(0);\n var gd = 0;\n var p0f = vec4<f32>(0.0);\n var sd = vec4<f32>(0.0);\n var c0v = vec4<f32>(0.0);\n var c1v = vec4<f32>(0.0);\n var c2v = vec4<f32>(0.0);\n var c3v = vec4<f32>(0.0);\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let lx = i32(i & 3u);\n let ly = i32(i >> 2u);\n let p = clamp(base + vec2<i32>(lx, ly), vec2<i32>(0, 0), max_xy);\n let px = to8(textureLoad(src_tex, p, 0));\n pixels[i] = px;\n lo = min(lo, px);\n hi = max(hi, px);\n gd = max(gd, max(abs(px.x - px.y), abs(px.x - px.z)));\n if (i == 0u) { p0f = vec4<f32>(px); }\n let d = vec4<f32>(px) - p0f;\n sd = sd + d;\n c0v = c0v + d.x * d;\n c1v = c1v + d.y * d;\n c2v = c2v + d.z * d;\n c3v = c3v + d.w * d;\n }\n let mean = p0f + sd * (1.0 / 16.0);\n\n // Seed endpoints from the block's principal colour axis at the exact\n // projection extents (see header), quantise, and assign indices in one\n // projection pass.\n // Mean-correct the fused moments: C = \u03A3dd\u1D40 \u2212 (\u03A3d)(\u03A3d)\u1D40/16.\n let sd16 = sd * (1.0 / 16.0);\n let r0v = c0v - sd.x * sd16;\n let r1v = c1v - sd.y * sd16;\n let r2v = c2v - sd.z * sd16;\n let r3v = c3v - sd.w * sd16;\n var seed0 = lo;\n var seed1 = hi;\n // Gray + opaque blocks: the axis is analytically (1,1,1,0)/\u221A3 with\n // extents at the luma min/max \u2014 skip iteration + extents pass entirely\n // (see bc7_fast_f16.wgsl).\n let gray = lo.w == 255 && gd == 0;\n if (gray) {\n seed0 = vec4<i32>(lo.x, lo.x, lo.x, 255);\n seed1 = vec4<i32>(hi.x, hi.x, hi.x, 255);\n } else {\n let axis = principal_axis4(r0v, r1v, r2v, r3v, vec4<f32>(hi - lo));\n if (dot(axis, axis) > 0.0) {\n // Exact projection extents along the axis. (A Rayleigh-quotient span\n // estimate was tried in place of this pass \u2014 it saves 16 dots but\n // costs 0.1\u20130.8 dB and 4\u201310\xD7 on the worst-easy-block gate: \u03C3\n // misjudges two-cluster and outlier blocks. The pass stays.)\n var t_min: f32 = 1e30;\n var t_max: f32 = -1e30;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let t = dot(vec4<f32>(pixels[k]) - mean, axis);\n t_min = min(t_min, t);\n t_max = max(t_max, t);\n }\n seed0 = vec4<i32>(clamp(round(mean + t_min * axis), vec4<f32>(0.0), vec4<f32>(255.0)));\n seed1 = vec4<i32>(clamp(round(mean + t_max * axis), vec4<f32>(0.0), vec4<f32>(255.0)));\n }\n }\n\n // The 16 4-bit indices, packed LSB-first into two nibble words\n // (pixel k \u2192 bits 4k..4k+3).\n var ilo: u32 = 0u;\n var ihi: u32 = 0u;\n var ep0 = pick_ep(seed0);\n var ep1 = pick_ep(seed1);\n let dir = vec4<f32>(ep1.eight - ep0.eight);\n let dd = dot(dir, dir);\n if (dd > 0.0) {\n let e0f = vec4<f32>(ep0.eight);\n let inv = 15.0 / dd;\n for (var k: u32 = 0u; k < 8u; k = k + 1u) {\n let s = clamp(floor(dot(vec4<f32>(pixels[k]) - e0f, dir) * inv + 0.5), 0.0, 15.0);\n ilo = ilo | (u32(s) << (k * 4u));\n }\n for (var k: u32 = 8u; k < 16u; k = k + 1u) {\n let s = clamp(floor(dot(vec4<f32>(pixels[k]) - e0f, dir) * inv + 0.5), 0.0, 15.0);\n ihi = ihi | (u32(s) << ((k - 8u) * 4u));\n }\n }\n var e0_7 = ep0.seven;\n var e1_7 = ep1.seven;\n var p0 = ep0.p;\n var p1 = ep1.p;\n\n // Anchor rule \u2014 pixel 0's index MSB must be 0. Swapping endpoints reflects\n // every index (i \u2192 15\u2212i), which on packed nibbles is a bitwise NOT.\n if ((ilo & 0x8u) != 0u) {\n let t7 = e0_7; e0_7 = e1_7; e1_7 = t7;\n let tp = p0; p0 = p1; p1 = tp;\n ilo = ~ilo; ihi = ~ihi;\n }\n\n // Straight-line mode-6 packing (see layout at the top of the file).\n let e0 = vec4<u32>(e0_7);\n let e1 = vec4<u32>(e1_7);\n let w0 = 0x40u | (e0.x << 7u) | (e1.x << 14u) | (e0.y << 21u) | (e1.y << 28u);\n let w1 = (e1.y >> 4u) | (e0.z << 3u) | (e1.z << 10u) | (e0.w << 17u) | (e1.w << 24u) | (p0 << 31u);\n let w2 = p1 | ((ilo & 0x7u) << 1u) | (ilo & 0xFFFFFFF0u);\n let w3 = ihi;\n\n let out = block_index * 4u;\n dst[out + 0u] = w0;\n dst[out + 1u] = w1;\n dst[out + 2u] = w2;\n dst[out + 3u] = w3;\n}\n";
818
1508
 
819
1509
  // src/bc7_fast_f16.wgsl
820
- var bc7_fast_f16_default = `// bc7 "fast" encoder \u2014 f16 variant (requires the shader-f16 feature).
821
- // Same algorithm family as the f32 fast path in bc7.wgsl (principal-axis
822
- // seed at the exact projection extents \u2192 quantise \u2192 one projection-based
823
- // index-assignment pass), tuned for throughput:
824
- //
825
- // \u2022 All projection math in f16 ([0,1] domain). ~2\xD7 ALU throughput on
826
- // f16-capable GPUs. The projection direction is pre-scaled by 32:
827
- // a shallow block (endpoints ~1/255 apart) has dd = dot(dir,dir) \u2248 1.5e-5,
828
- // where 15/dd \u2248 10\u2076 overflows f16 (max 65504) to +inf and the products
829
- // inside the projection dot are subnormal \u2014 the indices turn to garbage
830
- // (visible as banding on smooth gradients). Scaling dir by 32 multiplies
831
- // the dots by 32 and dd by 1024; s = dot\xB7(32\xB715/dd\u2083\u2082) is the same
832
- // quantity with every intermediate in f16's normal range (worst case
833
- // inv = 480/0.0157 \u2248 3.0e4 < 65504).
834
- // \u2022 NO least-squares refit, unlike the BC1/BC5/ASTC fast paths: with the
835
- // seed already on the principal axis at the exact projection extents,
836
- // mode 6's fine 16-level palette leaves the refit \u22640.05 dB on the colour
837
- // card and \u22640.15 dB on the normal card \u2014 not worth its two extra
838
- // 16-pixel passes. The coarse 4-level formats DO need it (dropping it
839
- // there costs 0.5\u20131.3 dB).
840
- // \u2022 Indices are packed into two u32 nibble words ON THE FLY during the
841
- // projection pass \u2014 no array<u32,16> private array. The BC7 anchor
842
- // reflection (i \u2192 15\u2212i) is then just a bitwise NOT of both words.
843
- // \u2022 The 128-bit block is assembled with straight-line constant shifts
844
- // instead of a generic write_bits() helper (whose dynamic word indexing
845
- // defeats register promotion of the output array).
846
- //
847
- // The host selects this module only when the device reports shader-f16,
848
- // falling back to bc7.wgsl otherwise. "high" never uses this.
849
- //
850
- // MODE 6 BIT LAYOUT (LSB-first): see bc7.wgsl. Summary:
851
- // w0: mode(7 bits, 0x40) R0 R1 G0 G1[3:0]
852
- // w1: G1[6:4] B0 B1 A0 A1 P0
853
- // w2: P1, pixel0 index (3 bits), pixels 1..7 (4 bits each)
854
- // w3: pixels 8..15 (4 bits each)
855
- enable f16;
856
- struct Params { blocks_x: u32, blocks_y: u32, width: u32, height: u32, };
857
- @group(0) @binding(0) var src_tex: texture_2d<f32>;
858
- @group(0) @binding(1) var<storage, read_write> dst: array<u32>;
859
- @group(0) @binding(2) var<uniform> params: Params;
860
- alias h = f16;
861
- alias h4 = vec4<f16>;
862
-
863
- // Quantise an ideal endpoint (h4 in [0,1]) to 7-bit + p-bit, choosing the
864
- // p-bit with the lower quantisation error. \`eight\` is the decoded value the
865
- // hardware will interpolate with, back in [0,1].
866
- struct Ep { seven: vec4<u32>, eight: h4, p: u32 };
867
- fn pick_ep(ideal01: h4) -> Ep {
868
- let ideal = ideal01 * h(255.0);
869
- let q0 = clamp(floor(ideal * h(0.5) + h(0.5)), h4(0.0), h4(127.0)); // p=0
870
- let e0 = q0 * h(2.0);
871
- let q1 = clamp(floor((ideal - h(1.0)) * h(0.5) + h(0.5)), h4(0.0), h4(127.0)); // p=1
872
- let e1 = q1 * h(2.0) + h(1.0);
873
- let d0 = e0 - ideal; let d1 = e1 - ideal;
874
- if (dot(d1, d1) < dot(d0, d0)) { return Ep(vec4<u32>(q1), e1 * h(1.0 / 255.0), 1u); }
875
- return Ep(vec4<u32>(q0), e0 * h(1.0 / 255.0), 0u);
876
- }
877
-
878
- @compute @workgroup_size(8, 8, 1)
879
- fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
880
- if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) { return; }
881
- let bi = gid.y * params.blocks_x + gid.x;
882
- let base = vec2<i32>(i32(gid.x) * 4, i32(gid.y) * 4);
883
- let mx = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);
884
-
885
- // Load pass, with the covariance moments FUSED in (no separate 16-pixel
886
- // pass): d = (px \u2212 pixel0)\xB716, relative to the block's first pixel so the
887
- // accumulators scale with the block's span \u2014 raw \u03A3v\xB7v\u1D40 moments would
888
- // cancel catastrophically in f16 \u2014 and pre-scaled \xD716 so shallow blocks
889
- // (span ~1/255 \u2192 d\xB2 \u2248 1e-3) clear the subnormal floor while full-range
890
- // sums stay \u22644096. C = \u03A3dd\u1D40 \u2212 (\u03A3d)(\u03A3d)\u1D40/16 is the \xD7256-scaled covariance.
891
- var pix: array<h4, 16>;
892
- var lo = h4(1.0);
893
- var hi = h4(0.0);
894
- var p0v = h4(0.0);
895
- var sd = h4(0.0);
896
- var c0v = h4(0.0);
897
- var c1v = h4(0.0);
898
- var c2v = h4(0.0);
899
- var c3v = h4(0.0);
900
- for (var i: u32 = 0u; i < 16u; i = i + 1u) {
901
- let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);
902
- let px = h4(textureLoad(src_tex, p, 0));
903
- pix[i] = px; lo = min(lo, px); hi = max(hi, px);
904
- if (i == 0u) { p0v = px; }
905
- let d = (px - p0v) * h(16.0);
906
- sd = sd + d;
907
- c0v = c0v + d.x * d;
908
- c1v = c1v + d.y * d;
909
- c2v = c2v + d.z * d;
910
- c3v = c3v + d.w * d;
911
- }
912
- let mean = p0v + sd * h(1.0 / 256.0);
913
- // Mean-correction via sd4\xB7sd4\u1D40 with sd4 = \u03A3d/4: (\u03A3d)(\u03A3d)\u1D40/16 with every
914
- // product \u22644096 (a direct \u03A3d\xB7\u03A3d\u1D40 could hit 65536 and overflow f16).
915
- let sd4 = sd * h(0.25);
916
- c0v = c0v - sd4.x * sd4;
917
- c1v = c1v - sd4.y * sd4;
918
- c2v = c2v - sd4.z * sd4;
919
- c3v = c3v - sd4.w * sd4;
920
-
921
- // Seed endpoints from the block's principal colour axis (covariance
922
- // power-iteration, seeded with the bbox diagonal \u2014 same family as the BC1
923
- // 'high' path). The bbox diagonal is sign-blind: on anti-correlated
924
- // channels (normal maps, hue edges) it points across the data instead of
925
- // along it, and the LSQ refit \u2014 which fits endpoints GIVEN the projection
926
- // indices \u2014 can't recover from a wrong axis. The iteration renormalises by
927
- // the max component (a plain length() of the matvec output could overflow
928
- // f16), so only the direction survives.
929
- var seed_lo = lo;
930
- var seed_hi = hi;
931
- var axis = hi - lo;
932
- var axis_ok = true;
933
- for (var it: u32 = 0u; it < 4u; it = it + 1u) {
934
- let nv = h4(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis), dot(c3v, axis));
935
- let m = max(max(abs(nv.x), abs(nv.y)), max(abs(nv.z), abs(nv.w)));
936
- if (m < h(1e-4)) { axis_ok = false; break; }
937
- axis = nv / m;
938
- }
939
- if (axis_ok) {
940
- axis = axis / length(axis);
941
- // Exact projection extents along the axis. (A Rayleigh-quotient span
942
- // estimate was tried in place of this pass \u2014 it saves 16 dots but costs
943
- // 0.1\u20130.8 dB and 4\u201310\xD7 on the worst-easy-block gate: \u03C3 misjudges
944
- // two-cluster and outlier blocks and the quantised weight grid can't
945
- // recover. The pass stays.)
946
- var t_min = h(4.0);
947
- var t_max = h(-4.0);
948
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
949
- let t = dot(pix[k] - mean, axis);
950
- t_min = min(t_min, t);
951
- t_max = max(t_max, t);
952
- }
953
- seed_lo = clamp(mean + t_min * axis, h4(0.0), h4(1.0));
954
- seed_hi = clamp(mean + t_max * axis, h4(0.0), h4(1.0));
955
- }
956
-
957
- // Fit from the principal-axis seed (bbox on degenerate blocks), then
958
- // quantise the refit endpoints. The refit is clamped to the block bbox: on
959
- // multi-cluster blocks the unconstrained solve extrapolates far outside
960
- // the block's colours and the per-channel [0,1] clamp then bends the hue \u2014
961
- // fringe pixels decode to colours that exist nowhere in the block.
962
- // Constraining to the bbox also measures better in plain SSE (+1.3 dB on
963
- // the colour test card).
964
- // Quantise the PCA-extents seed directly \u2014 no LSQ refit (see header).
965
- var ep0 = pick_ep(seed_lo);
966
- var ep1 = pick_ep(seed_hi);
967
-
968
- // Final projection against the decoded endpoints, packing the 4-bit indices
969
- // into two nibble words as we go (pixel k \u2192 bits 4k..4k+3 of ilo/ihi).
970
- var ilo: u32 = 0u;
971
- var ihi: u32 = 0u;
972
- // Same \xD732 pre-scale as proj_fit; distinct quantised endpoints are \u22651/255
973
- // apart, i.e. dd\u2083\u2082 \u2265 0.0157, so the flat-block threshold only catches
974
- // truly identical endpoints.
975
- let dir = (ep1.eight - ep0.eight) * h(32.0);
976
- let dd = dot(dir, dir);
977
- if (dd >= h(0.008)) {
978
- let inv = h(480.0) / dd;
979
- for (var k: u32 = 0u; k < 8u; k = k + 1u) {
980
- let s = clamp(floor(dot(pix[k] - ep0.eight, dir) * inv + h(0.5)), h(0.0), h(15.0));
981
- ilo = ilo | (u32(s) << (k * 4u));
982
- }
983
- for (var k: u32 = 8u; k < 16u; k = k + 1u) {
984
- let s = clamp(floor(dot(pix[k] - ep0.eight, dir) * inv + h(0.5)), h(0.0), h(15.0));
985
- ihi = ihi | (u32(s) << ((k - 8u) * 4u));
986
- }
987
- }
988
-
989
- // Anchor rule \u2014 pixel 0's index MSB must be 0. Swapping endpoints reflects
990
- // every index (i \u2192 15\u2212i), which on packed nibbles is a bitwise NOT.
991
- if ((ilo & 0x8u) != 0u) {
992
- let t = ep0; ep0 = ep1; ep1 = t;
993
- ilo = ~ilo; ihi = ~ihi;
994
- }
995
-
996
- // Straight-line mode-6 packing (see layout above).
997
- let e0 = ep0.seven;
998
- let e1 = ep1.seven;
999
- let w0 = 0x40u | (e0.x << 7u) | (e1.x << 14u) | (e0.y << 21u) | (e1.y << 28u);
1000
- let w1 = (e1.y >> 4u) | (e0.z << 3u) | (e1.z << 10u) | (e0.w << 17u) | (e1.w << 24u) | (ep0.p << 31u);
1001
- let w2 = ep1.p | ((ilo & 0x7u) << 1u) | (ilo & 0xFFFFFFF0u);
1002
- let w3 = ihi;
1003
-
1004
- let o = bi * 4u;
1005
- dst[o] = w0; dst[o + 1u] = w1; dst[o + 2u] = w2; dst[o + 3u] = w3;
1006
- }
1007
- `;
1510
+ var bc7_fast_f16_default = "// bc7 \"fast\" encoder \u2014 f16 variant (requires the shader-f16 feature).\n// Same algorithm family as the f32 fast path in bc7.wgsl (principal-axis\n// seed at the exact projection extents \u2192 quantise \u2192 one projection-based\n// index-assignment pass), tuned for throughput:\n//\n// \u2022 All projection math in f16 ([0,1] domain). ~2\xD7 ALU throughput on\n// f16-capable GPUs. The projection direction is pre-scaled by 32:\n// a shallow block (endpoints ~1/255 apart) has dd = dot(dir,dir) \u2248 1.5e-5,\n// where 15/dd \u2248 10\u2076 overflows f16 (max 65504) to +inf and the products\n// inside the projection dot are subnormal \u2014 the indices turn to garbage\n// (visible as banding on smooth gradients). Scaling dir by 32 multiplies\n// the dots by 32 and dd by 1024; s = dot\xB7(32\xB7L/dd\u2083\u2082) is the same\n// quantity with every intermediate in f16's normal range (worst case\n// inv = 480/0.0157 \u2248 3.0e4 < 65504).\n// \u2022 TWO MODES (mode 4 OPT-IN via the enable_mode4 override constant,\n// default off and dead-coded at pipeline creation \u2014 see the constant's\n// comment for the measured cost/benefit), decided per block BEFORE\n// encoding \u2014 never encoded both:\n// mode 6 (single RGBA line, 4-bit indices) by default, mode 4 (rotation:\n// one channel split into its own scalar plane with 3-bit indices, the\n// remaining three on a 2-bit line) when the principal axis leaves a\n// large share of the block's variance unexplained \u2014 decorrelated data\n// (normal maps, channel-packed atlases) where any single 4-D line fails.\n// The decision reads the covariance already in registers (\u03BB = axis\u1D40Ca,\n// residual = trace \u2212 \u03BB) and costs no extra pass. An encode-both-and-\n// compare trial was priced at ~2\xD7 on exactly this content (see the mode\n// 1 postmortem below) \u2014 deciding first keeps it at ~1.2\xD7.\n// \u2022 The two modes SHARE the per-pixel passes (axis matvecs, projection\n// extents, the index/weight pass runs once with per-thread level count,\n// index width and packing split) so warps holding a mix of mode-4 and\n// mode-6 blocks do not execute two disjoint kernels back to back \u2014 a\n// first cut with separate per-mode passes measured 1.77\xD7 on normal maps\n// from exactly that divergence; the only mode-4-extra 16-pixel work is\n// the cheap scalar-plane pass.\n// \u2022 GRAY + opaque blocks (every texel R == G == B, A == 1) have their\n// principal axis analytically: (1,1,1,0)/\u221A3, with projection extents at\n// the luma min/max. They skip the power iteration AND the extents pass\n// (\u221234% GPU on roughness/AO/displacement content) and always take\n// mode 6 \u2014 a gray single line fits gray data exactly.\n// \u2022 NO least-squares refit, unlike the BC1/BC5/ASTC fast paths: with the\n// seed already on the principal axis at the exact projection extents,\n// mode 6's fine 16-level palette leaves the refit \u22640.05 dB on the colour\n// card, \u22640.15 dB on the normal card and +0.03 dB on the channel-packed\n// packed-materials atlas \u2014 not worth its two extra 16-pixel passes. The\n// coarse 4-level formats DO need it (dropping it there costs 0.5\u20131.3 dB).\n// \u2022 A MODE 1 (2-subset) candidate was built and evaluated (2026-07): it\n// buys ~+1.3 dB on multi-modal content but its candidate evaluation\n// costs up to ~3\xD7 the mode-6 pass on exactly that content \u2014 dropped in\n// favour of the decided (not compared) mode 4 above, which covers the\n// decorrelated-channel share of that content at a fraction of the cost.\n// The CPU reference decoder keeps mode 1 support (bc7_ref.ts).\n// \u2022 Indices are packed into two u32 words ON THE FLY during the\n// projection pass \u2014 no array<u32,16> private array. The BC7 anchor\n// reflection is then just a bitwise NOT of the packed words.\n// \u2022 The 128-bit block is assembled with straight-line constant shifts\n// instead of a generic write_bits() helper (whose dynamic word indexing\n// defeats register promotion of the output array).\n//\n// The host selects this module only when the device reports shader-f16,\n// falling back to bc7.wgsl otherwise.\n//\n// MODE 6 BIT LAYOUT (LSB-first): see bc7.wgsl. Summary:\n// w0: mode(7 bits, 0x40) R0 R1 G0 G1[3:0]\n// w1: G1[6:4] B0 B1 A0 A1 P0\n// w2: P1, pixel0 index (3 bits), pixels 1..7 (4 bits each)\n// w3: pixels 8..15 (4 bits each)\n// MODE 4 BIT LAYOUT (LSB-first): mode 0b00001, rotation @5 (channel swapped\n// with alpha), idxMode @7 (0 = colour \u2192 2-bit set, scalar \u2192 3-bit set),\n// colour endpoints 6\xD75 bits @8, alpha endpoints 2\xD76 @38, 31-bit 2-bit index\n// field @50 (pixel 0 anchored to 1 bit), 47-bit 3-bit index field @81\n// (pixel 0 anchored to 2 bits). Validated bit-exact against hardware\n// bc7-rgba-unorm sampling; decode reference in bc7_ref.ts.\nenable f16;\nstruct Params { blocks_x: u32, blocks_y: u32, width: u32, height: u32, };\n@group(0) @binding(0) var src_tex: texture_2d<f32>;\n@group(0) @binding(1) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(2) var<uniform> params: Params;\nalias h = f16;\nalias h4 = vec4<f16>;\n\n// Mode-4 gate: encode mode 4 when the principal axis leaves more than\n// MODE4_THETA of the (\xD7256-scaled) total variance unexplained and the block\n// isn't near-flat. Tuned against per-content mode histograms and PSNR.\nconst MODE4_THETA: f16 = 0.2;\nconst MODE4_FLOOR: f16 = 1.0;\nconst MODE4_CONC: f16 = 0.5;\n\n// OPT-IN adaptive mode 4, folded at pipeline creation (WebGPU override\n// constant; default OFF dead-codes the whole path \u2014 measured at exact par\n// with the mode-6-only kernel). Rationale: the quality is real (+2.5\u20132.9 dB\n// on normal maps, +1.9\u20132.4 on channel-packed atlases) but any warp holding\n// one mode-4 block executes both modes' passes, and content that benefits\n// runs 1.4\u20131.5\xD7; a \u03B8 sweep showed quality and warp-poisoning scale together\n// (no per-block middle ground without subgroup ballots). So the trade is\n// the CALLER's: BC7Encoder({ adaptiveMode4: true }).\noverride enable_mode4: bool = false;\n\n// Quantise an ideal endpoint (h4 in [0,1]) to 7-bit + p-bit, choosing the\n// p-bit with the lower quantisation error. `eight` is the decoded value the\n// hardware will interpolate with, back in [0,1].\nstruct Ep { seven: vec4<u32>, eight: h4, p: u32 };\nfn pick_ep(ideal01: h4) -> Ep {\n let ideal = ideal01 * h(255.0);\n let q0 = clamp(floor(ideal * h(0.5) + h(0.5)), h4(0.0), h4(127.0)); // p=0\n let e0 = q0 * h(2.0);\n let q1 = clamp(floor((ideal - h(1.0)) * h(0.5) + h(0.5)), h4(0.0), h4(127.0)); // p=1\n let e1 = q1 * h(2.0) + h(1.0);\n let d0 = e0 - ideal; let d1 = e1 - ideal;\n if (dot(d1, d1) < dot(d0, d0)) { return Ep(vec4<u32>(q1), e1 * h(1.0 / 255.0), 1u); }\n return Ep(vec4<u32>(q0), e0 * h(1.0 / 255.0), 0u);\n}\n\n@compute @workgroup_size(8, 8, 1)\nfn encode(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) { return; }\n let bi = gid.y * params.blocks_x + gid.x;\n let base = vec2<i32>(i32(gid.x) * 4, i32(gid.y) * 4);\n let mx = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);\n\n // Load pass, with the covariance moments FUSED in (no separate 16-pixel\n // pass): d = (px \u2212 pixel0)\xB716, relative to the block's first pixel so the\n // accumulators scale with the block's span \u2014 raw \u03A3v\xB7v\u1D40 moments would\n // cancel catastrophically in f16 \u2014 and pre-scaled \xD716 so shallow blocks\n // (span ~1/255 \u2192 d\xB2 \u2248 1e-3) clear the subnormal floor while full-range\n // sums stay \u22644096. C = \u03A3dd\u1D40 \u2212 (\u03A3d)(\u03A3d)\u1D40/16 is the \xD7256-scaled covariance.\n var pix: array<h4, 16>;\n var lo = h4(1.0);\n var hi = h4(0.0);\n var gd = h(0.0);\n var p0v = h4(0.0);\n var sd = h4(0.0);\n var c0v = h4(0.0);\n var c1v = h4(0.0);\n var c2v = h4(0.0);\n var c3v = h4(0.0);\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);\n let px = h4(textureLoad(src_tex, p, 0));\n pix[i] = px; lo = min(lo, px); hi = max(hi, px);\n gd = max(gd, max(abs(px.x - px.y), abs(px.x - px.z)));\n if (i == 0u) { p0v = px; }\n let d = (px - p0v) * h(16.0);\n sd = sd + d;\n c0v = c0v + d.x * d;\n c1v = c1v + d.y * d;\n c2v = c2v + d.z * d;\n c3v = c3v + d.w * d;\n }\n let mean = p0v + sd * h(1.0 / 256.0);\n // Mean-correction via sd4\xB7sd4\u1D40 with sd4 = \u03A3d/4: (\u03A3d)(\u03A3d)\u1D40/16 with every\n // product \u22644096 (a direct \u03A3d\xB7\u03A3d\u1D40 could hit 65536 and overflow f16).\n let sd4 = sd * h(0.25);\n c0v = c0v - sd4.x * sd4;\n c1v = c1v - sd4.y * sd4;\n c2v = c2v - sd4.z * sd4;\n c3v = c3v - sd4.w * sd4;\n\n // Seed endpoints + per-block mode decision (see header).\n var seed_lo = lo;\n var seed_hi = hi;\n var use4 = false;\n var cmask = h4(1.0);\n var ch = 0u;\n if (lo.w == h(1.0) && gd == h(0.0)) {\n // GRAY + opaque: analytic axis (1,1,1,0)/\u221A3, extents at luma min/max,\n // always mode 6 \u2014 and a fully specialised tail: gray textures are\n // warp-uniform, and routing them through the parametric shared loop\n // below (runtime index width/split) measured +28% on displacement\n // content purely from the lost constant-shift codegen.\n var ep0g = pick_ep(h4(lo.x, lo.x, lo.x, h(1.0)));\n var ep1g = pick_ep(h4(hi.x, hi.x, hi.x, h(1.0)));\n var ilo = 0u;\n var ihi = 0u;\n let dirg = (ep1g.eight - ep0g.eight) * h(32.0);\n let ddg = dot(dirg, dirg);\n if (ddg >= h(0.008)) {\n let invg = h(480.0) / ddg;\n for (var k: u32 = 0u; k < 8u; k = k + 1u) {\n let sg = clamp(floor(dot(pix[k] - ep0g.eight, dirg) * invg + h(0.5)), h(0.0), h(15.0));\n ilo = ilo | (u32(sg) << (k * 4u));\n }\n for (var k: u32 = 8u; k < 16u; k = k + 1u) {\n let sg = clamp(floor(dot(pix[k] - ep0g.eight, dirg) * invg + h(0.5)), h(0.0), h(15.0));\n ihi = ihi | (u32(sg) << ((k - 8u) * 4u));\n }\n }\n if ((ilo & 0x8u) != 0u) {\n let t = ep0g; ep0g = ep1g; ep1g = t;\n ilo = ~ilo; ihi = ~ihi;\n }\n let e0g = ep0g.seven;\n let e1g = ep1g.seven;\n let og = bi * 4u;\n dst[og] = 0x40u | (e0g.x << 7u) | (e1g.x << 14u) | (e0g.y << 21u) | (e1g.y << 28u);\n dst[og + 1u] = (e1g.y >> 4u) | (e0g.z << 3u) | (e1g.z << 10u) | (e0g.w << 17u) | (e1g.w << 24u) | (ep0g.p << 31u);\n dst[og + 2u] = ep1g.p | ((ilo & 0x7u) << 1u) | (ilo & 0xFFFFFFF0u);\n dst[og + 3u] = ihi;\n return;\n }\n {\n var axis = hi - lo;\n var axis_ok = true;\n // 8 iterations: 4 was under-converged on noisy 4-D blocks (heavily\n // downscaled photographic/channel-packed content) \u2014 going to 8 measured\n // +0.75 dB on the normal card, +0.12 colour, +0.08 packed-materials, and\n // matches the f32 fallback's iteration count. Four extra 4-dot matvecs\n // per block are noise next to the index pass.\n for (var it: u32 = 0u; it < 8u; it = it + 1u) {\n let nv = h4(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis), dot(c3v, axis));\n let m = max(max(abs(nv.x), abs(nv.y)), max(abs(nv.z), abs(nv.w)));\n if (m < h(1e-4)) { axis_ok = false; break; }\n axis = nv / m;\n }\n if (axis_ok) {\n axis = axis / length(axis);\n var axisF = axis;\n\n // Mode decision from the covariance already in registers: \u03BB is the\n // variance the mode-6 line explains, trace \u2212 \u03BB what it cannot.\n let Ca = h4(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis), dot(c3v, axis));\n let lam = dot(Ca, axis);\n let diag = h4(c0v.x, c1v.y, c2v.z, c3v.w);\n let trace = diag.x + diag.y + diag.z + diag.w;\n let resid = trace - lam;\n let rc = diag - lam * axis * axis;\n var rbest = rc.x;\n if (rc.y > rbest) { ch = 1u; rbest = rc.y; }\n if (rc.z > rbest) { ch = 2u; rbest = rc.z; }\n if (rc.w > rbest) { ch = 3u; rbest = rc.w; }\n use4 = enable_mode4 && resid > MODE4_THETA * trace && trace > MODE4_FLOOR && rbest > MODE4_CONC * resid;\n if (use4) {\n // The colour plane is the remaining three channels, handled as\n // masked 4-vectors so every vec4 pass below applies unchanged.\n // Branchless mask build \u2014 a dynamic component store spills the\n // vector to scratch on some compilers.\n cmask = h4(1.0) - h4(h(f32(u32(ch == 0u))), h(f32(u32(ch == 1u))), h(f32(u32(ch == 2u))), h(f32(u32(ch == 3u))));\n var a3 = (hi - lo) * cmask;\n var ok3 = true;\n for (var it: u32 = 0u; it < 2u; it = it + 1u) {\n let nv = h4(dot(c0v, a3), dot(c1v, a3), dot(c2v, a3), dot(c3v, a3)) * cmask;\n let m = max(max(abs(nv.x), abs(nv.y)), max(abs(nv.z), abs(nv.w)));\n if (m < h(1e-4)) { ok3 = false; break; }\n a3 = nv / m;\n }\n if (ok3) {\n axisF = a3 / length(a3);\n } else {\n use4 = false;\n cmask = h4(1.0);\n }\n }\n\n // Exact projection extents along the fit axis \u2014 ONE shared pass for\n // both modes (for mode 4 axisF[ch] = 0, so the scalar plane is\n // invisible to it). (A Rayleigh-quotient span estimate was tried in\n // place of this pass \u2014 it saves 16 dots but costs 0.1\u20130.8 dB and\n // 4\u201310\xD7 on the worst-easy-block gate.)\n var t_min = h(4.0);\n var t_max = h(-4.0);\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let t = dot(pix[k] - mean, axisF);\n t_min = min(t_min, t);\n t_max = max(t_max, t);\n }\n seed_lo = clamp(mean + t_min * axisF, h4(0.0), h4(1.0));\n seed_hi = clamp(mean + t_max * axisF, h4(0.0), h4(1.0));\n }\n }\n\n // Endpoints, per mode. d0/d1 are the DECODED values the weight pass\n // projects against.\n var ep0: Ep;\n var ep1: Ep;\n var q0c = vec4<u32>(0u);\n var q1c = vec4<u32>(0u);\n var A0 = 0u;\n var A1 = 0u;\n var iA = 0u;\n var iB = 0u;\n var d0: h4;\n var d1: h4;\n var d0a = h(0.0);\n var sca = h(0.0);\n let chs = h4(1.0) - cmask;\n ep0 = pick_ep(seed_lo);\n ep1 = pick_ep(seed_hi);\n d0 = ep0.eight;\n d1 = ep1.eight;\n if (use4) {\n // Scalar plane (3-bit index set): 6-bit endpoints at the channel's\n // exact extremes. Its projection is FUSED into the shared weight pass\n // below \u2014 a separate 16-pixel pass here measured +46% on normal maps\n // (mixed warps paid it wholesale); fused, the marginal cost is one dot\n // per pixel under a warp-uniform predicate.\n let a0q = u32(floor(dot(lo, chs) * h(63.0) + h(0.5)));\n let a1q = u32(floor(dot(hi, chs) * h(63.0) + h(0.5)));\n A0 = a0q;\n A1 = a1q;\n let d0av = h(f32((a0q << 2u) | (a0q >> 4u))) * h(1.0 / 255.0);\n let d1av = h(f32((a1q << 2u) | (a1q >> 4u))) * h(1.0 / 255.0);\n let aspan = d1av - d0av;\n if (aspan > h(0.001)) {\n d0a = d0av;\n sca = h(7.0) / aspan;\n }\n // Colour plane: 5-bit endpoints from the masked extents seed.\n q0c = vec4<u32>(clamp(floor(seed_lo * h(31.0) + h(0.5)), h4(0.0), h4(31.0)));\n q1c = vec4<u32>(clamp(floor(seed_hi * h(31.0) + h(0.5)), h4(0.0), h4(31.0)));\n d0 = h4(vec4<f32>((q0c << vec4<u32>(3u)) | (q0c >> vec4<u32>(2u)))) * h(1.0 / 255.0) * cmask;\n d1 = h4(vec4<f32>((q1c << vec4<u32>(3u)) | (q1c >> vec4<u32>(2u)))) * h(1.0 / 255.0) * cmask;\n }\n\n // Index/weight pass: per-mode SPECIALISED loops (constant level counts\n // and shifts, so each unrolls cleanly \u2014 a single parametric loop with\n // runtime width/split measured +22% on pure mode-6 photo content).\n // Mixed warps execute both loops; the mode-4 one carries the fused\n // scalar-plane projection. For mode 4 pix[ch]\xB7dir[ch] = 0, so the\n // scalar plane never perturbs the colour projection.\n var a_lo = 0u;\n var a_hi = 0u;\n // Same \xD732 pre-scale as the extents math; distinct quantised endpoints\n // are \u22651/255 apart (dd\u2083\u2082 \u2265 0.0157), so the flat-block threshold only\n // catches truly identical ones.\n let dir = (d1 - d0) * h(32.0);\n let dd = dot(dir, dir);\n let live = dd >= h(0.008);\n if (use4) {\n if (live) {\n let inv = h(96.0) / dd;\n for (var k: u32 = 0u; k < 8u; k = k + 1u) {\n let s = clamp(floor(dot(pix[k] - d0, dir) * inv + h(0.5)), h(0.0), h(3.0));\n let sv = clamp(floor((dot(pix[k], chs) - d0a) * sca + h(0.5)), h(0.0), h(7.0));\n a_lo = a_lo | (u32(s) << (k * 2u));\n iA = iA | (u32(sv) << (k * 3u));\n }\n for (var k: u32 = 8u; k < 16u; k = k + 1u) {\n let s = clamp(floor(dot(pix[k] - d0, dir) * inv + h(0.5)), h(0.0), h(3.0));\n let sv = clamp(floor((dot(pix[k], chs) - d0a) * sca + h(0.5)), h(0.0), h(7.0));\n a_lo = a_lo | (u32(s) << (k * 2u));\n iB = iB | (u32(sv) << ((k - 8u) * 3u));\n }\n }\n } else if (live) {\n let inv = h(480.0) / dd;\n for (var k: u32 = 0u; k < 8u; k = k + 1u) {\n let s = clamp(floor(dot(pix[k] - d0, dir) * inv + h(0.5)), h(0.0), h(15.0));\n a_lo = a_lo | (u32(s) << (k * 4u));\n }\n for (var k: u32 = 8u; k < 16u; k = k + 1u) {\n let s = clamp(floor(dot(pix[k] - d0, dir) * inv + h(0.5)), h(0.0), h(15.0));\n a_hi = a_hi | (u32(s) << ((k - 8u) * 4u));\n }\n }\n\n // Anchors + packing. Mode 6 packs unconditionally (one-sided branches\n // compile better than two-sided divergence); mode-4 threads overwrite.\n let o = bi * 4u;\n {\n var ilo = a_lo;\n var ihi = a_hi;\n if ((ilo & 0x8u) != 0u) {\n let t = ep0; ep0 = ep1; ep1 = t;\n ilo = ~ilo; ihi = ~ihi;\n }\n let e0 = ep0.seven;\n let e1 = ep1.seven;\n dst[o] = 0x40u | (e0.x << 7u) | (e1.x << 14u) | (e0.y << 21u) | (e1.y << 28u);\n dst[o + 1u] = (e1.y >> 4u) | (e0.z << 3u) | (e1.z << 10u) | (e0.w << 17u) | (e1.w << 24u) | (ep0.p << 31u);\n dst[o + 2u] = ep1.p | ((ilo & 0x7u) << 1u) | (ilo & 0xFFFFFFF0u);\n dst[o + 3u] = ihi;\n }\n if (use4) {\n // 3-bit anchor: pixel 0's MSB must be 0; reflect = bitwise NOT.\n if ((iA & 4u) != 0u) {\n let tA = A0;\n A0 = A1;\n A1 = tA;\n iA = ~iA & 0xFFFFFFu;\n iB = ~iB & 0xFFFFFFu;\n }\n var c2 = a_lo;\n // 2-bit anchor: pixel 0's MSB must be 0.\n if ((c2 & 2u) != 0u) {\n let tq = q0c;\n q0c = q1c;\n q1c = tq;\n c2 = ~c2;\n }\n // Rotated-space RGB: position ch carries the original alpha.\n let R0 = select(q0c.x, q0c.w, ch == 0u);\n let G0 = select(q0c.y, q0c.w, ch == 1u);\n let B0 = select(q0c.z, q0c.w, ch == 2u);\n let R1 = select(q1c.x, q1c.w, ch == 0u);\n let G1 = select(q1c.y, q1c.w, ch == 1u);\n let B1 = select(q1c.z, q1c.w, ch == 2u);\n let rot = (ch + 1u) & 3u;\n // Index fields drop the anchors' MSBs: 31 bits (2-bit set) and 47 bits\n // (3-bit set).\n let field2 = (c2 & 1u) | ((c2 >> 2u) << 1u);\n let f_lo = (iA & 3u) | ((iA >> 3u) << 2u) | (iB << 23u);\n let f_hi = iB >> 9u;\n dst[o] = 0x10u | (rot << 5u) | (R0 << 8u) | (R1 << 13u) | (G0 << 18u) | (G1 << 23u) | (B0 << 28u);\n dst[o + 1u] = (B0 >> 4u) | (B1 << 1u) | (A0 << 6u) | (A1 << 12u) | ((field2 & 0x3FFFu) << 18u);\n dst[o + 2u] = (field2 >> 14u) | (f_lo << 17u);\n dst[o + 3u] = (f_lo >> 15u) | (f_hi << 17u);\n }\n}\n";
1008
1511
 
1009
1512
  // src/BC7Encoder.ts
1010
1513
  var BC7Encoder = class extends Encoder {
1011
1514
  static requiredFeature = WebGPUFeature.BC;
1012
1515
  static textureFormats = [TextureFormat.BC7, TextureFormat.BC7_SRGB];
1516
+ adaptiveMode4;
1517
+ constructor(opts) {
1518
+ super(opts);
1519
+ this.adaptiveMode4 = opts.adaptiveMode4 === true;
1520
+ if (this.adaptiveMode4) this._buildPipeline();
1521
+ }
1522
+ pipelineConstants() {
1523
+ if (this.adaptiveMode4 && this._useF16) return { enable_mode4: 1 };
1524
+ return void 0;
1525
+ }
1013
1526
  get label() {
1014
1527
  return "bc7";
1015
1528
  }
@@ -1031,35 +1544,60 @@ var BC7Encoder = class extends Encoder {
1031
1544
  };
1032
1545
 
1033
1546
  // src/astc4x4.wgsl
1034
- var astc4x4_default = "// ASTC 4\xD74 LDR compute shader encoder.\n//\n// One invocation per 4\xD74 block. Emits 16 bytes = 4 u32s into the storage\n// buffer at `dst[block_index * 4 .. + 3]`. This is the f32 fallback;\n// astc4x4_fast_f16.wgsl is the same algorithm and is preferred when the\n// device reports shader-f16.\n//\n// ALGORITHM: principal-axis seed (covariance power-iteration; bbox on\n// degenerate blocks) at the exact projection extents \u2192 one fused pass that\n// projects each pixel onto the endpoint line (the 4 palette entries are\n// colinear, so the nearest is the rounded projection \u2014 no per-entry search)\n// while accumulating the least-squares refit sums, then a reprojection\n// against the quantised refit endpoints with the weights packed on the fly.\n// The endpoint ordering rule is applied before the weight pass, so no\n// reflection is needed.\n//\n// RESTRICTED SUBSET: single partition, no dual-plane, CEM 12 (LDR RGBA direct),\n// 4\xD74 weight grid with 2-bit weights (QUANT_4), 8-bit endpoints (QUANT_256).\n//\n// BLOCK LAYOUT (128 bits, LSB-first)\n// bits [10:0] block mode = 0x042\n// bits [12:11] partition count \u2212 1 = 0\n// bits [16:13] CEM = 12\n// bits [80:17] endpoints: R0 R1 G0 G1 B0 B1 A0 A1 (8-bit each)\n// bits [127:96] 16 \xD7 2-bit weights; weight k: bit(127\u22122k)=lsb, bit(126\u22122k)=msb\n//\n// ENDPOINT ORDERING: if sum(e0.rgb) > sum(e1.rgb) swap endpoints and reflect\n// indices (w' = 3 \u2212 w) to keep the decoder out of blue contraction.\n\nstruct Params {\n blocks_x: u32,\n blocks_y: u32,\n width: u32,\n height: u32,\n};\n\n@group(0) @binding(0) var src_tex: texture_2d<f32>;\n@group(0) @binding(1) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(2) var<uniform> params: Params;\n\nfn to8(v: vec4<f32>) -> vec4<i32> {\n return vec4<i32>(clamp(floor(v * 255.0 + 0.5), vec4<f32>(0.0), vec4<f32>(255.0)));\n}\n\n// One pass over the block: project every pixel onto the e0\u2192e1 line (4 levels,\n// QUANT_4 \u2248 thirds) and accumulate the least-squares normal-equation sums;\n// solve for the refit endpoints. Weights are not produced here \u2014 the caller\n// reprojects against the quantised refit endpoints anyway.\nstruct Fit { e0: vec4<i32>, e1: vec4<i32>, valid: bool };\nfn proj_fit(pixels: ptr<function, array<vec4<i32>, 16>>, e0: vec4<i32>, e1: vec4<i32>) -> Fit {\n var out: Fit;\n out.valid = false;\n let dir = vec4<f32>(e1 - e0);\n let dd = dot(dir, dir);\n if (dd == 0.0) { return out; }\n let e0f = vec4<f32>(e0);\n let inv = 3.0 / dd;\n var sAA: f32 = 0.0; var sBB: f32 = 0.0; var sAB: f32 = 0.0;\n var sAV: vec4<f32> = vec4<f32>(0.0); var sBV: vec4<f32> = vec4<f32>(0.0);\n var s_min = 3.0; var s_max = 0.0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let v = vec4<f32>((*pixels)[k]);\n let s = clamp(floor(dot(v - e0f, dir) * inv + 0.5), 0.0, 3.0);\n s_min = min(s_min, s); s_max = max(s_max, s);\n let b = s * (1.0 / 3.0); let a = 1.0 - b;\n sAA = sAA + a * a; sBB = sBB + b * b; sAB = sAB + a * b;\n sAV = sAV + a * v; sBV = sBV + b * v;\n }\n // Rank-1 guard: if every pixel projects to ONE level the system is\n // singular \u2014 det and the numerators are pure float rounding noise and the\n // solve returns garbage endpoints. With \u22652 levels det \u2265 15\xB7(1/3)\xB2 \u2248 1.67.\n if (s_min == s_max) { return out; }\n let det = sAA * sBB - sAB * sAB;\n if (abs(det) < 1e-3) { return out; }\n out.e0 = vec4<i32>(clamp(round((sBB * sAV - sAB * sBV) / det), vec4<f32>(0.0), vec4<f32>(255.0)));\n out.e1 = vec4<i32>(clamp(round((sAA * sBV - sAB * sAV) / det), vec4<f32>(0.0), vec4<f32>(255.0)));\n out.valid = true;\n return out;\n}\n\n// Principal colour axis via covariance power-iteration (RGBA, 8-bit integer\n// pixel domain), seeded with the bbox diagonal. Returns a unit axis, or\n// vec4(0) for a degenerate (constant) block. Used to seed the LSQ fit \u2014 the\n// bbox diagonal is sign-blind and points across anti-correlated data (normal\n// maps, hue edges) instead of along it.\nfn principal_axis4(\n pixels: ptr<function, array<vec4<i32>, 16>>,\n mean: vec4<f32>,\n seed: vec4<f32>,\n) -> vec4<f32> {\n var c0v = vec4<f32>(0.0);\n var c1v = vec4<f32>(0.0);\n var c2v = vec4<f32>(0.0);\n var c3v = vec4<f32>(0.0);\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let d = vec4<f32>((*pixels)[k]) - mean;\n c0v = c0v + d.x * d;\n c1v = c1v + d.y * d;\n c2v = c2v + d.z * d;\n c3v = c3v + d.w * d;\n }\n var v = seed;\n var len = length(v);\n if (len < 1e-9) { return vec4<f32>(0.0); }\n v = v / len;\n for (var iter: u32 = 0u; iter < 8u; iter = iter + 1u) {\n let nv = vec4<f32>(dot(c0v, v), dot(c1v, v), dot(c2v, v), dot(c3v, v));\n len = length(nv);\n if (len < 1e-12) { return vec4<f32>(0.0); }\n v = nv / len;\n }\n return v;\n}\n\n// ------------------------------- Entry ---------------------------------- //\n\n@compute @workgroup_size(8, 8, 1)\nfn encode(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) {\n return;\n }\n\n let bx = gid.x;\n let by = gid.y;\n let block_index = by * params.blocks_x + bx;\n\n let base = vec2<i32>(i32(bx) * 4, i32(by) * 4);\n let max_xy = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);\n\n var pixels: array<vec4<i32>, 16>;\n var lo = vec4<i32>(255);\n var hi = vec4<i32>(0);\n var isum = vec4<i32>(0);\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0, 0), max_xy);\n let px = to8(textureLoad(src_tex, p, 0));\n pixels[i] = px;\n lo = min(lo, px);\n hi = max(hi, px);\n isum = isum + px;\n }\n let mean = vec4<f32>(isum) * (1.0 / 16.0);\n\n // Fused LSQ fit seeded from the block's principal colour axis at the exact\n // projection extents, quantised refit endpoints, ordering applied BEFORE\n // the weight pass so no reflection is needed.\n // The refit is clamped to the block bbox: on multi-cluster blocks the\n // unconstrained solve extrapolates far outside the block's colours and the\n // per-channel [0,255] clamp then bends the hue \u2014 fringe pixels decode to\n // colours that exist nowhere in the block. Constraining to the bbox also\n // measures better in plain SSE (+1.8 dB on the colour test card).\n var seed0 = lo;\n var seed1 = hi;\n let axis = principal_axis4(&pixels, mean, vec4<f32>(hi - lo));\n if (dot(axis, axis) > 0.0) {\n var t_min: f32 = 1e30;\n var t_max: f32 = -1e30;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let t = dot(vec4<f32>(pixels[k]) - mean, axis);\n t_min = min(t_min, t);\n t_max = max(t_max, t);\n }\n seed0 = vec4<i32>(clamp(round(mean + t_min * axis), vec4<f32>(0.0), vec4<f32>(255.0)));\n seed1 = vec4<i32>(clamp(round(mean + t_max * axis), vec4<f32>(0.0), vec4<f32>(255.0)));\n }\n let r = proj_fit(&pixels, seed0, seed1);\n var e0 = lo;\n var e1 = hi;\n if (r.valid) { e0 = clamp(r.e0, lo, hi); e1 = clamp(r.e1, lo, hi); }\n if (e0.x + e0.y + e0.z > e1.x + e1.y + e1.z) {\n let tmp = e0; e0 = e1; e1 = tmp;\n }\n\n // Weight pass, packing on the fly (weight k's lsb at bit 31\u22122k, msb at\n // bit 30\u22122k).\n var w3: u32 = 0u;\n let dir = vec4<f32>(e1 - e0);\n let dd = dot(dir, dir);\n if (dd > 0.0) {\n let e0f = vec4<f32>(e0);\n let inv = 3.0 / dd;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let s = u32(clamp(floor(dot(vec4<f32>(pixels[k]) - e0f, dir) * inv + 0.5), 0.0, 3.0));\n w3 = w3 | ((s & 1u) << (31u - 2u * k)) | (((s >> 1u) & 1u) << (30u - 2u * k));\n }\n }\n\n // Straight-line packing: block mode 0x042 @0, partitions\u22121=0 @11, CEM 12\n // @13, endpoints R0 R1 G0 G1 B0 B1 A0 A1 (8 bits each) from bit 17,\n // weights in the last word.\n let E0 = vec4<u32>(e0);\n let E1 = vec4<u32>(e1);\n let w0 = 0x042u | (12u << 13u) | (E0.x << 17u) | (E1.x << 25u);\n let w1 = (E1.x >> 7u) | (E0.y << 1u) | (E1.y << 9u) | (E0.z << 17u) | (E1.z << 25u);\n let w2 = (E1.z >> 7u) | (E0.w << 1u) | (E1.w << 9u);\n\n let out = block_index * 4u;\n dst[out + 0u] = w0;\n dst[out + 1u] = w1;\n dst[out + 2u] = w2;\n dst[out + 3u] = w3;\n}\n";
1547
+ var astc4x4_default = "// ASTC 4\xD74 LDR compute shader encoder.\n//\n// One invocation per 4\xD74 block. Emits 16 bytes = 4 u32s into the storage\n// buffer at `dst[block_index * 4 .. + 3]`. This is the f32 fallback;\n// astc4x4_fast_f16.wgsl is the same algorithm and is preferred when the\n// device reports shader-f16.\n//\n// ALGORITHM: per-block class selection, then a single-line fit:\n// gray + opaque \u2192 CEM 0 (luminance), 5-bit weights, block mode 0x253 \u2014\n// scalar path: exact min/max endpoints, 32-level weight\n// assignment, no covariance / iteration / refit needed\n// opaque \u2192 CEM 8 (RGB), 3-bit weights, block mode 0x053\n// translucent \u2192 CEM 12 (RGBA), 2-bit weights, block mode 0x042\n// Colour paths: principal-axis seed (covariance power-iteration; bbox on\n// degenerate blocks) at the exact projection extents \u2192 one fused pass that\n// projects each pixel onto the endpoint line (the palette entries are\n// colinear, so the nearest is the rounded projection \u2014 no per-entry search)\n// while accumulating the least-squares refit sums, then a reprojection\n// against the quantised refit endpoints with the weights packed on the fly.\n// The endpoint ordering rule is applied before the weight pass, so no\n// reflection is needed.\n//\n// RESTRICTED SUBSET + BLOCK LAYOUT: see astc4x4_ref.ts (single partition,\n// no dual-plane, CEM 0/8/12, 8-bit endpoints, plain-bit weight ISE; block\n// mode derivations and the weight-stream bit order are documented there).\n//\n// WEIGHT PLACEMENT: stream bit q (bit j of weight k, q = nBits\xB7k + j) lives\n// at block bit 127 \u2212 q, so a stream word assembled LSB-first maps onto a\n// block word with a single reverseBits().\n//\n// ENDPOINT ORDERING: CEM 8/12 decoders branch into blue contraction when\n// sum(e0.rgb) > sum(e1.rgb); the encoder swaps endpoints up front (weights\n// are assigned after the swap, so no reflection pass). CEM 0 has no rule\n// (L0 \u2264 L1 by construction from min/max).\n\nstruct Params {\n blocks_x: u32,\n blocks_y: u32,\n width: u32,\n height: u32,\n};\n\n@group(0) @binding(0) var src_tex: texture_2d<f32>;\n@group(0) @binding(1) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(2) var<uniform> params: Params;\n\nfn to8(v: vec4<f32>) -> vec4<i32> {\n return vec4<i32>(clamp(floor(v * 255.0 + 0.5), vec4<f32>(0.0), vec4<f32>(255.0)));\n}\n\n// One pass over the block: project every pixel onto the e0\u2192e1 line\n// (lmax + 1 colinear levels, so the nearest entry is the rounded\n// projection) and accumulate the least-squares normal-equation sums;\n// solve for the refit endpoints. Weights are not produced here \u2014 the\n// caller reprojects against the quantised refit endpoints anyway.\nstruct Fit { e0: vec4<i32>, e1: vec4<i32>, valid: bool, wstream: u32 };\nfn proj_fit(pixels: ptr<function, array<vec4<i32>, 16>>, e0: vec4<i32>, e1: vec4<i32>) -> Fit {\n var out: Fit;\n out.valid = false;\n out.wstream = 0u;\n let dir = vec4<f32>(e1 - e0);\n let dd = dot(dir, dir);\n if (dd == 0.0) { return out; }\n let e0f = vec4<f32>(e0);\n let inv = 3.0 / dd;\n var sAA: f32 = 0.0; var sBB: f32 = 0.0; var sAB: f32 = 0.0;\n var sAV: vec4<f32> = vec4<f32>(0.0); var sBV: vec4<f32> = vec4<f32>(0.0);\n var s_min = 3.0; var s_max = 0.0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let v = vec4<f32>((*pixels)[k]);\n let s = clamp(floor(dot(v - e0f, dir) * inv + 0.5), 0.0, 3.0);\n out.wstream = out.wstream | (u32(s) << (2u * k));\n s_min = min(s_min, s); s_max = max(s_max, s);\n let b = s * (1.0 / 3.0); let a = 1.0 - b;\n sAA = sAA + a * a; sBB = sBB + b * b; sAB = sAB + a * b;\n sAV = sAV + a * v; sBV = sBV + b * v;\n }\n // Rank-1 guard: if every pixel projects to ONE level the system is\n // singular \u2014 det and the numerators are pure float rounding noise and the\n // solve returns garbage endpoints. With \u22652 levels det \u2265 15\xB7(1/3)\xB2 \u2248 1.67.\n if (s_min == s_max) { return out; }\n let det = sAA * sBB - sAB * sAB;\n if (abs(det) < 1e-3) { return out; }\n out.e0 = vec4<i32>(clamp(round((sBB * sAV - sAB * sBV) / det), vec4<f32>(0.0), vec4<f32>(255.0)));\n out.e1 = vec4<i32>(clamp(round((sAA * sBV - sAB * sAV) / det), vec4<f32>(0.0), vec4<f32>(255.0)));\n out.valid = true;\n return out;\n}\n\n// Principal colour axis via covariance power-iteration (RGBA, 8-bit integer\n// pixel domain), seeded with the bbox diagonal. Returns a unit axis, or\n// vec4(0) for a degenerate (constant) block. Used to seed the LSQ fit \u2014 the\n// bbox diagonal is sign-blind and points across anti-correlated data (normal\n// maps, hue edges) instead of along it.\nfn principal_axis4(\n pixels: ptr<function, array<vec4<i32>, 16>>,\n mean: vec4<f32>,\n seed: vec4<f32>,\n iters: u32,\n) -> vec4<f32> {\n var c0v = vec4<f32>(0.0);\n var c1v = vec4<f32>(0.0);\n var c2v = vec4<f32>(0.0);\n var c3v = vec4<f32>(0.0);\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let d = vec4<f32>((*pixels)[k]) - mean;\n c0v = c0v + d.x * d;\n c1v = c1v + d.y * d;\n c2v = c2v + d.z * d;\n c3v = c3v + d.w * d;\n }\n var v = seed;\n var len = length(v);\n if (len < 1e-9) { return vec4<f32>(0.0); }\n v = v / len;\n for (var iter: u32 = 0u; iter < iters; iter = iter + 1u) {\n let nv = vec4<f32>(dot(c0v, v), dot(c1v, v), dot(c2v, v), dot(c3v, v));\n len = length(nv);\n if (len < 1e-12) { return vec4<f32>(0.0); }\n v = nv / len;\n }\n return v;\n}\n\n// ------------------------------- Entry ---------------------------------- //\n\n@compute @workgroup_size(8, 8, 1)\nfn encode(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) {\n return;\n }\n\n let bx = gid.x;\n let by = gid.y;\n let block_index = by * params.blocks_x + bx;\n\n let base = vec2<i32>(i32(bx) * 4, i32(by) * 4);\n let max_xy = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);\n\n var pixels: array<vec4<i32>, 16>;\n var lo = vec4<i32>(255);\n var hi = vec4<i32>(0);\n var isum = vec4<i32>(0);\n var gd = 0; // max |R\u2212G|, |R\u2212B| over the block; 0 \u21D4 exactly grayscale\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0, 0), max_xy);\n let px = to8(textureLoad(src_tex, p, 0));\n pixels[i] = px;\n lo = min(lo, px);\n hi = max(hi, px);\n isum = isum + px;\n gd = max(gd, max(abs(px.x - px.y), abs(px.x - px.z)));\n }\n let opaque = lo.w == 255;\n\n var w0: u32; var w1: u32; var w2: u32; var w3: u32;\n\n if (opaque && gd == 0) {\n // ---------------- Luminance path: CEM 0, 5-bit weights ----------------\n // Endpoints at the exact extremes; 32 palette levels make an LSQ refit\n // unnecessary.\n let L0 = u32(lo.x);\n let L1 = u32(hi.x);\n var s0 = 0u; var s1 = 0u; var s2 = 0u;\n if (L1 > L0) {\n let sc = 64.0 / f32(hi.x - lo.x);\n // Exact nearest entry of the QUANT_32 grid: unq = 2w for w \u2264 15,\n // 2w + 2 for w \u2265 16 (4-wide gap at the middle, so uniform rounding\n // is wrong there). Best candidate of each half, keep the closer.\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let u = clamp(f32(pixels[k].x - lo.x) * sc, 0.0, 64.0);\n let wlo = clamp(floor(u * 0.5 + 0.5), 0.0, 15.0);\n let whi = clamp(floor((u - 2.0) * 0.5 + 0.5), 16.0, 31.0);\n let pick = abs(u - wlo * 2.0) <= abs(u - (whi * 2.0 + 2.0));\n let w = u32(select(whi, wlo, pick));\n // Stream bit q = 5k + j; straddles handled with constant shifts.\n let off = 5u * k;\n if (off < 28u) { s0 = s0 | (w << off); }\n else if (off == 30u) { s0 = s0 | (w << 30u); s1 = s1 | (w >> 2u); }\n else if (off < 60u) { s1 = s1 | (w << (off - 32u)); }\n else if (off == 60u) { s1 = s1 | (w << 28u); s2 = s2 | (w >> 4u); }\n else { s2 = s2 | (w << (off - 64u)); }\n }\n }\n // Mode 0x253, partitions\u22121 = 0, CEM 0, L0 @17, L1 @25 (top bit spills\n // into w1 bit 0); stream words map onto block words via reverseBits.\n w0 = 0x253u | (L0 << 17u) | (L1 << 25u);\n w1 = (L1 >> 7u) | reverseBits(s2);\n w2 = reverseBits(s1);\n w3 = reverseBits(s0);\n } else {\n // ------------- Colour paths: shared PCA seed ---------------------------\n let mean = vec4<f32>(isum) * (1.0 / 16.0);\n\n // Fused LSQ fit seeded from the block's principal colour axis at the\n // exact projection extents, quantised refit endpoints, ordering applied\n // BEFORE the weight pass so no reflection is needed.\n // The refit is clamped to the block bbox: on multi-cluster blocks the\n // unconstrained solve extrapolates far outside the block's colours and\n // the per-channel [0,255] clamp then bends the hue \u2014 fringe pixels\n // decode to colours that exist nowhere in the block. Constraining to\n // the bbox also measures better in plain SSE (+1.8 dB on the colour\n // test card).\n var seed0 = lo;\n var seed1 = hi;\n // 8 iterations for opaque blocks (the axis is the endpoint quality\n // there), 4 for translucent ones whose LSQ refit absorbs residual\n // axis error (see astc4x4_fast_f16.wgsl).\n let axis = principal_axis4(&pixels, mean, vec4<f32>(hi - lo), select(4u, 8u, opaque));\n if (dot(axis, axis) > 0.0) {\n var t_min: f32 = 1e30;\n var t_max: f32 = -1e30;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let t = dot(vec4<f32>(pixels[k]) - mean, axis);\n t_min = min(t_min, t);\n t_max = max(t_max, t);\n }\n seed0 = vec4<i32>(clamp(round(mean + t_min * axis), vec4<f32>(0.0), vec4<f32>(255.0)));\n seed1 = vec4<i32>(clamp(round(mean + t_max * axis), vec4<f32>(0.0), vec4<f32>(255.0)));\n }\n // Opaque blocks (CEM 8, 8-level weights) ship the quantised PCA\n // extents directly; only the translucent CEM 12 path refits its coarse\n // 4-level grid (see astc4x4_fast_f16.wgsl for the measured trade).\n var e0 = lo;\n var e1 = hi;\n var fitStream = 0u;\n var haveFitWeights = false;\n if (opaque) {\n // Bbox-clamped like the fit output (see astc4x4_fast_f16.wgsl).\n e0 = clamp(seed0, lo, hi);\n e1 = clamp(seed1, lo, hi);\n } else {\n let r = proj_fit(&pixels, seed0, seed1);\n if (r.valid) {\n e0 = clamp(r.e0, lo, hi);\n e1 = clamp(r.e1, lo, hi);\n fitStream = r.wstream;\n haveFitWeights = true;\n }\n }\n var swapped = false;\n if (e0.x + e0.y + e0.z > e1.x + e1.y + e1.z) {\n let tmp = e0; e0 = e1; e1 = tmp;\n swapped = true;\n }\n let E0 = vec4<u32>(e0);\n let E1 = vec4<u32>(e1);\n\n // Weight pass against the final endpoints, packing on the fly.\n let dir = vec4<f32>(e1 - e0);\n let dd = dot(dir, dir);\n let e0f = vec4<f32>(e0);\n if (opaque) {\n // CEM 8: 3-bit weights, stream bit q = 3k.\n var s0 = 0u; var s1 = 0u;\n if (dd > 0.0) {\n let inv = 7.0 / dd;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let w = u32(clamp(floor(dot(vec4<f32>(pixels[k]) - e0f, dir) * inv + 0.5), 0.0, 7.0));\n let off = 3u * k;\n if (off < 30u) { s0 = s0 | (w << off); }\n else if (off == 30u) { s0 = s0 | (w << 30u); s1 = s1 | (w >> 2u); }\n else { s1 = s1 | (w << (off - 32u)); }\n }\n }\n // Mode 0x053, CEM 8 @13, endpoints R0 R1 G0 G1 B0 B1 from bit 17.\n w0 = 0x053u | (8u << 13u) | (E0.x << 17u) | (E1.x << 25u);\n w1 = (E1.x >> 7u) | (E0.y << 1u) | (E1.y << 9u) | (E0.z << 17u) | (E1.z << 25u);\n w2 = (E1.z >> 7u) | reverseBits(s1);\n w3 = reverseBits(s0);\n } else {\n // CEM 12: 2-bit weights, stream bit q = 2k (single stream word).\n // Valid fits ship the fit-pass weights; the blue-contraction swap is\n // a full reflection w \u2192 3\u2212w = bitwise NOT of the packed stream (see\n // astc4x4_fast_f16.wgsl for the measured trade).\n var s0 = 0u;\n if (haveFitWeights) {\n s0 = select(fitStream, ~fitStream, swapped);\n } else if (dd > 0.0) {\n let inv = 3.0 / dd;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let w = u32(clamp(floor(dot(vec4<f32>(pixels[k]) - e0f, dir) * inv + 0.5), 0.0, 3.0));\n s0 = s0 | (w << (2u * k));\n }\n }\n // Mode 0x042, CEM 12 @13, endpoints R0 R1 G0 G1 B0 B1 A0 A1 from 17.\n w0 = 0x042u | (12u << 13u) | (E0.x << 17u) | (E1.x << 25u);\n w1 = (E1.x >> 7u) | (E0.y << 1u) | (E1.y << 9u) | (E0.z << 17u) | (E1.z << 25u);\n w2 = (E1.z >> 7u) | (E0.w << 1u) | (E1.w << 9u);\n w3 = reverseBits(s0);\n }\n }\n\n let out = block_index * 4u;\n dst[out + 0u] = w0;\n dst[out + 1u] = w1;\n dst[out + 2u] = w2;\n dst[out + 3u] = w3;\n}\n";
1035
1548
 
1036
1549
  // src/astc4x4_fast_f16.wgsl
1037
1550
  var astc4x4_fast_f16_default = `// astc4x4 "fast" encoder \u2014 f16 variant (requires the shader-f16 feature).
1038
- // Same algorithm family as the f32 fast path in astc4x4.wgsl (principal-axis
1039
- // seed \u2192 projection weight assignment with a fused least-squares refit \u2192
1040
- // reproject), tuned for throughput:
1551
+ // Same algorithm family as the f32 fallback in astc4x4.wgsl (per-block
1552
+ // class selection, principal-axis seed \u2192 projection weight assignment with
1553
+ // a fused least-squares refit \u2192 reproject), tuned for throughput:
1041
1554
  //
1042
- // \u2022 All projection / refit math in f16 ([0,1] domain). The projection
1043
- // direction is pre-scaled by 32 \u2014 a shallow block (endpoints ~1/255
1044
- // apart) has dd \u2248 1.5e-5, where 3/dd \u2248 2e5 overflows f16 (max 65504) to
1045
- // +inf and the projection dots go subnormal, turning weights and the LSQ
1046
- // refit to garbage (banding on smooth gradients). Scaling dir by 32 puts
1047
- // every intermediate in f16's normal range; s = dot\xB7(32\xB73/dd\u2083\u2082) is the
1048
- // same quantity (worst case inv = 96/0.0157 \u2248 6.1e3).
1049
- // \u2022 The seed pass only accumulates the LSQ sums (no weight output) \u2014 the
1050
- // final weights come from reprojecting against the refit endpoints.
1555
+ // \u2022 THREE block classes, picked per block from the loaded pixels (the
1556
+ // ASTC bit budget trades endpoint bits against weight bits, so a block
1557
+ // only pays for the channels it uses):
1558
+ // gray + opaque \u2192 CEM 0 (luminance), 5-bit weights, mode 0x253
1559
+ // opaque \u2192 CEM 8 (RGB), 3-bit weights, mode 0x053
1560
+ // translucent \u2192 CEM 12 (RGBA), 2-bit weights, mode 0x042
1561
+ // "gray" = every texel R == G == B exactly (f16 equality is exact for
1562
+ // 8-bit sources), "opaque" = every texel A == 1. The gray path is
1563
+ // scalar \u2014 no covariance, no power iteration, no 4-D fit \u2014 and runs
1564
+ // [0,255]-integer f16 math like the BC5 kernel (exact endpoints, and
1565
+ // 64/span \u2264 64 never overflows f16, unlike a [0,1]-domain 1/dd).
1566
+ // \u2022 All projection / refit math in f16 ([0,1] domain, colour paths).
1567
+ // The projection direction is pre-scaled by 32 \u2014 a shallow block
1568
+ // (endpoints ~1/255 apart) has dd \u2248 1.5e-5, where 3/dd \u2248 2e5 overflows
1569
+ // f16 (max 65504) to +inf and the projection dots go subnormal,
1570
+ // turning weights and the LSQ refit to garbage (banding on smooth
1571
+ // gradients). Scaling dir by 32 puts every intermediate in f16's
1572
+ // normal range; s = dot\xB7(32\xB7L/dd\u2083\u2082) is the same quantity (worst case
1573
+ // for L = 7 levels: inv = 224/0.0157 \u2248 1.4e4).
1574
+ // \u2022 Opaque blocks ship the quantised PCA extents directly (no LSQ fit
1575
+ // pass, 8 power iterations \u2014 see the endpoint-selection comment); only
1576
+ // the translucent CEM 12 path still refits, and its seed pass only
1577
+ // accumulates the LSQ sums \u2014 final weights always come from a
1578
+ // reprojection against the final endpoints.
1051
1579
  // \u2022 Endpoint ordering (the blue-contraction rule: sum(e0.rgb) must not
1052
1580
  // exceed sum(e1.rgb)) is applied BEFORE the final projection, so no
1053
1581
  // weight-reflection pass is needed.
1054
- // \u2022 Weights are packed into the reversed-bit-order field on the fly, and
1055
- // the 128-bit block is assembled with straight-line constant shifts
1056
- // instead of a generic write_bits() helper.
1582
+ // \u2022 Weight streams are accumulated LSB-first into u32 words and placed
1583
+ // into the block's reversed-bit-order field with reverseBits() \u2014
1584
+ // stream bit q lives at block bit 127 \u2212 q, so a whole stream word
1585
+ // maps onto a block word with a single bit reversal.
1586
+ // \u2022 The covariance moments stay in their OWN pass, deliberately: fusing
1587
+ // them into the load loop bc7-style (pixel-0 residuals, with or without
1588
+ // hoisting pixel 0 out of the loop) measured +5% GPU time on 4096\xB2
1589
+ // (/ab A/B, 2026-07, M3) \u2014 the separate loop overlaps the 16 texture
1590
+ // loads' latency better than a longer in-loop dependency chain does.
1591
+ // Per-pass cost on the same rig, for future tuning: covariance+power-
1592
+ // iteration \u2248 18%, LSQ fit pass \u2248 24%, extents pass \u2248 6% of the kernel;
1593
+ // @workgroup_size 16\xD78 measured exactly at par with 8\xD78.
1057
1594
  //
1058
- // RESTRICTED SUBSET + BLOCK LAYOUT: see astc4x4.wgsl (single partition,
1059
- // CEM 12, 8-bit endpoints, 2-bit weights).
1595
+ // RESTRICTED SUBSET + BLOCK LAYOUT: see astc4x4_ref.ts (single partition,
1596
+ // CEM 0/8/12, 8-bit endpoints, plain-bit weight ISE, block modes
1597
+ // 0x253/0x053/0x042).
1060
1598
  //
1061
1599
  // The host selects this module only when the device reports shader-f16,
1062
- // falling back to astc4x4.wgsl otherwise. "high" never uses this.
1600
+ // falling back to astc4x4.wgsl otherwise.
1063
1601
  enable f16;
1064
1602
  alias h = f16;
1065
1603
  alias h4 = vec4<f16>;
@@ -1068,10 +1606,15 @@ struct Params { blocks_x: u32, blocks_y: u32, width: u32, height: u32, };
1068
1606
  @group(0) @binding(1) var<storage, read_write> dst: array<u32>;
1069
1607
  @group(0) @binding(2) var<uniform> params: Params;
1070
1608
 
1071
- struct Fit { e0: h4, e1: h4, valid: bool };
1609
+ // Fused projection + least-squares refit against the 4-level QUANT_4
1610
+ // palette \u2014 only the translucent (CEM 12) path still refits: the 2-bit
1611
+ // weight grid is coarse enough to need it, while the opaque paths get
1612
+ // more from spending the same time elsewhere (see header).
1613
+ struct Fit { e0: h4, e1: h4, valid: bool, wstream: u32 };
1072
1614
  fn proj_fit(pix: ptr<function, array<h4, 16>>, e0: h4, e1: h4) -> Fit {
1073
1615
  var out: Fit;
1074
1616
  out.valid = false;
1617
+ out.wstream = 0u;
1075
1618
  // dir pre-scaled by 32 to keep dd and the projection dots in f16's normal
1076
1619
  // range (see header). Spans below ~0.7 of an 8-bit step (dd\u2083\u2082 < 0.008,
1077
1620
  // possible only for non-8-bit sources) are treated as flat.
@@ -1089,8 +1632,10 @@ fn proj_fit(pix: ptr<function, array<h4, 16>>, e0: h4, e1: h4) -> Fit {
1089
1632
  for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1090
1633
  let vr = (*pix)[k] - e0;
1091
1634
  let s = clamp(floor(dot(vr, dir) * inv + h(0.5)), h(0.0), h(3.0));
1635
+ out.wstream = out.wstream | (u32(s) << (2u * k));
1092
1636
  s_min = min(s_min, s); s_max = max(s_max, s);
1093
- let b = s * h(1.0 / 3.0); let a = h(1.0) - b;
1637
+ let b = s * h(1.0 / 3.0);
1638
+ let a = h(1.0) - b;
1094
1639
  sAA = sAA + a * a; sBB = sBB + b * b; sAB = sAB + a * b;
1095
1640
  sAV = sAV + a * vr; sBV = sBV + b * vr;
1096
1641
  }
@@ -1118,102 +1663,218 @@ fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
1118
1663
  let base = vec2<i32>(i32(gid.x) * 4, i32(gid.y) * 4);
1119
1664
  let mx = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);
1120
1665
 
1666
+ // Load pass. gd tracks the largest chroma deviation \u2014 0 iff the block is
1667
+ // exactly grayscale (equal 8-bit channels convert to identical f16s).
1121
1668
  var pix: array<h4, 16>;
1122
1669
  var lo = h4(1.0);
1123
1670
  var hi = h4(0.0);
1124
1671
  var mean = h4(0.0);
1672
+ var gd = h(0.0);
1125
1673
  for (var i: u32 = 0u; i < 16u; i = i + 1u) {
1126
1674
  let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);
1127
1675
  let px = h4(textureLoad(src_tex, p, 0));
1128
1676
  pix[i] = px; lo = min(lo, px); hi = max(hi, px);
1129
1677
  mean = mean + px;
1678
+ gd = max(gd, max(abs(px.x - px.y), abs(px.x - px.z)));
1130
1679
  }
1131
1680
  mean = mean * h(1.0 / 16.0);
1132
-
1133
- // Seed endpoints from the block's principal colour axis (covariance
1134
- // power-iteration, seeded with the bbox diagonal). The bbox diagonal is
1135
- // sign-blind: on anti-correlated channels (normal maps, hue edges) it
1136
- // points across the data instead of along it, and the LSQ refit \u2014 which
1137
- // fits endpoints GIVEN the projection weights \u2014 can't recover from a wrong
1138
- // axis. Deviations are pre-scaled \xD716 so covariance entries for shallow
1139
- // blocks stay in f16's normal range (span ~1/255 \u2192 d\xB2 \u2248 1e-3) while
1140
- // full-range sums stay \u22644096; the iteration renormalises by the max
1141
- // component (a plain length() of the matvec output could overflow f16), so
1142
- // only the direction survives.
1143
- var seed_lo = lo;
1144
- var seed_hi = hi;
1145
- var c0v = h4(0.0);
1146
- var c1v = h4(0.0);
1147
- var c2v = h4(0.0);
1148
- var c3v = h4(0.0);
1149
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1150
- let d = (pix[k] - mean) * h(16.0);
1151
- c0v = c0v + d.x * d;
1152
- c1v = c1v + d.y * d;
1153
- c2v = c2v + d.z * d;
1154
- c3v = c3v + d.w * d;
1155
- }
1156
- var axis = hi - lo;
1157
- var axis_ok = true;
1158
- for (var it: u32 = 0u; it < 4u; it = it + 1u) {
1159
- let nv = h4(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis), dot(c3v, axis));
1160
- let m = max(max(abs(nv.x), abs(nv.y)), max(abs(nv.z), abs(nv.w)));
1161
- if (m < h(1e-4)) { axis_ok = false; break; }
1162
- axis = nv / m;
1163
- }
1164
- if (axis_ok) {
1165
- axis = axis / length(axis);
1166
- var t_min = h(4.0);
1167
- var t_max = h(-4.0);
1168
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1169
- let t = dot(pix[k] - mean, axis);
1170
- t_min = min(t_min, t);
1171
- t_max = max(t_max, t);
1681
+ let opaque = lo.w == h(1.0);
1682
+
1683
+ var w0: u32; var w1: u32; var w2: u32; var w3: u32;
1684
+
1685
+ if (opaque && gd == h(0.0)) {
1686
+ // ---------------- Luminance path: CEM 0, 5-bit weights ----------------
1687
+ // Scalar [0,255]-integer domain. Endpoints at the exact extremes (the
1688
+ // 8-bit values round-trip f16 exactly); 32 palette levels make an LSQ
1689
+ // refit unnecessary.
1690
+ let L0 = u32(floor(lo.x * h(255.0) + h(0.5)));
1691
+ let L1 = u32(floor(hi.x * h(255.0) + h(0.5)));
1692
+ var s0 = 0u; var s1 = 0u; var s2 = 0u;
1693
+ if (L1 > L0) {
1694
+ let l0f = h(f32(L0));
1695
+ let sc = h(64.0) / h(f32(L1 - L0)); // span \u2265 1 \u2192 sc \u2264 64, no overflow
1696
+ // Exact nearest entry of the QUANT_32 grid: unq = 2w for w \u2264 15,
1697
+ // 2w + 2 for w \u2265 16 (the grid has a 4-wide gap at the middle, so
1698
+ // uniform rounding is wrong there). Evaluate the best candidate of
1699
+ // each half and keep the closer.
1700
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1701
+ let v = floor(pix[k].x * h(255.0) + h(0.5)) - l0f; // exact integer
1702
+ let u = clamp(v * sc, h(0.0), h(64.0));
1703
+ let wlo = clamp(floor(u * h(0.5) + h(0.5)), h(0.0), h(15.0));
1704
+ let whi = clamp(floor((u - h(2.0)) * h(0.5) + h(0.5)), h(16.0), h(31.0));
1705
+ let pick = abs(u - wlo * h(2.0)) <= abs(u - (whi * h(2.0) + h(2.0)));
1706
+ let w = u32(select(whi, wlo, pick));
1707
+ // Stream bit q = 5k + j; straddles handled with constant shifts.
1708
+ let off = 5u * k;
1709
+ if (off < 28u) { s0 = s0 | (w << off); }
1710
+ else if (off == 30u) { s0 = s0 | (w << 30u); s1 = s1 | (w >> 2u); }
1711
+ else if (off < 60u) { s1 = s1 | (w << (off - 32u)); }
1712
+ else if (off == 60u) { s1 = s1 | (w << 28u); s2 = s2 | (w >> 4u); }
1713
+ else { s2 = s2 | (w << (off - 64u)); }
1714
+ }
1172
1715
  }
1173
- seed_lo = clamp(mean + t_min * axis, h4(0.0), h4(1.0));
1174
- seed_hi = clamp(mean + t_max * axis, h4(0.0), h4(1.0));
1175
- }
1176
-
1177
- // The refit is clamped to the block bbox: on multi-cluster blocks the
1178
- // unconstrained solve extrapolates far outside the block's colours and the
1179
- // per-channel [0,1] clamp then bends the hue \u2014 fringe pixels decode to
1180
- // colours that exist nowhere in the block. Constraining to the bbox also
1181
- // measures better in plain SSE (+1.8 dB on the colour test card).
1182
- let r = proj_fit(&pix, seed_lo, seed_hi);
1183
- var e0 = lo;
1184
- var e1 = hi;
1185
- if (r.valid) { e0 = clamp(r.e0, lo, hi); e1 = clamp(r.e1, lo, hi); }
1186
- var E0 = q8(e0);
1187
- var E1 = q8(e1);
1188
-
1189
- // Blue-contraction ordering, applied before the weight pass so weights are
1190
- // already oriented (no reflection needed).
1191
- if (E0.x + E0.y + E0.z > E1.x + E1.y + E1.z) {
1192
- let t = E0; E0 = E1; E1 = t;
1193
- }
1194
- let d0 = h4(vec4<f32>(E0)) * h(1.0 / 255.0);
1195
- let d1 = h4(vec4<f32>(E1)) * h(1.0 / 255.0);
1196
-
1197
- // Weight pass, packing on the fly: weight k's lsb at bit 31\u22122k of the last
1198
- // word, msb at bit 30\u22122k.
1199
- var w3: u32 = 0u;
1200
- // Same \xD732 pre-scale as proj_fit; distinct 8-bit endpoints are \u22651/255
1201
- // apart (dd\u2083\u2082 \u2265 0.0157), so the threshold only catches identical ones.
1202
- let dir = (d1 - d0) * h(32.0);
1203
- let dd = dot(dir, dir);
1204
- if (dd >= h(0.008)) {
1205
- let inv = h(96.0) / dd;
1716
+ // Mode 0x253, partitions\u22121 = 0, CEM 0, L0 @17, L1 @25 (top bit spills
1717
+ // into w1 bit 0); stream word q\u2208[0,31] \u2192 block bits 127\u202696 via
1718
+ // reverseBits, q\u2208[32,63] \u2192 95\u202664, q\u2208[64,79] \u2192 63\u202648.
1719
+ w0 = 0x253u | (L0 << 17u) | (L1 << 25u);
1720
+ w1 = (L1 >> 7u) | reverseBits(s2);
1721
+ w2 = reverseBits(s1);
1722
+ w3 = reverseBits(s0);
1723
+ } else {
1724
+ // ------------- Colour paths: shared PCA seed + LSQ refit --------------
1725
+ // Seed endpoints from the block's principal colour axis (covariance
1726
+ // power-iteration, seeded with the bbox diagonal). The bbox diagonal is
1727
+ // sign-blind: on anti-correlated channels (normal maps, hue edges) it
1728
+ // points across the data instead of along it, and the LSQ refit \u2014 which
1729
+ // fits endpoints GIVEN the projection weights \u2014 can't recover from a
1730
+ // wrong axis. Deviations are pre-scaled \xD716 so covariance entries for
1731
+ // shallow blocks stay in f16's normal range (span ~1/255 \u2192 d\xB2 \u2248 1e-3)
1732
+ // while full-range sums stay \u22644096; the iteration renormalises by the
1733
+ // max component (a plain length() of the matvec output could overflow
1734
+ // f16), so only the direction survives.
1735
+ var seed_lo = lo;
1736
+ var seed_hi = hi;
1737
+ var c0v = h4(0.0);
1738
+ var c1v = h4(0.0);
1739
+ var c2v = h4(0.0);
1740
+ var c3v = h4(0.0);
1206
1741
  for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1207
- let s = u32(clamp(floor(dot(pix[k] - d0, dir) * inv + h(0.5)), h(0.0), h(3.0)));
1208
- w3 = w3 | ((s & 1u) << (31u - 2u * k)) | (((s >> 1u) & 1u) << (30u - 2u * k));
1742
+ let d = (pix[k] - mean) * h(16.0);
1743
+ c0v = c0v + d.x * d;
1744
+ c1v = c1v + d.y * d;
1745
+ c2v = c2v + d.z * d;
1746
+ c3v = c3v + d.w * d;
1747
+ }
1748
+ var axis = hi - lo;
1749
+ var axis_ok = true;
1750
+ // 4 shared iterations, then 4 more for opaque blocks only \u2014 two
1751
+ // FIXED-bound loops rather than one divergent trip count, so both
1752
+ // unroll. Opaque blocks need the converged axis (it IS the endpoint
1753
+ // quality there; 4 was under-converged on noisy 4-D content), while
1754
+ // translucent blocks' LSQ refit absorbs residual axis error \u2014 their
1755
+ // extra 4 steps measured exactly 0.000 dB on the alpha card for
1756
+ // ~5% GPU.
1757
+ for (var it: u32 = 0u; it < 4u; it = it + 1u) {
1758
+ let nv = h4(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis), dot(c3v, axis));
1759
+ let m = max(max(abs(nv.x), abs(nv.y)), max(abs(nv.z), abs(nv.w)));
1760
+ if (m < h(1e-4)) { axis_ok = false; break; }
1761
+ axis = nv / m;
1762
+ }
1763
+ if (axis_ok && opaque) {
1764
+ for (var it: u32 = 0u; it < 4u; it = it + 1u) {
1765
+ let nv = h4(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis), dot(c3v, axis));
1766
+ let m = max(max(abs(nv.x), abs(nv.y)), max(abs(nv.z), abs(nv.w)));
1767
+ if (m < h(1e-4)) { axis_ok = false; break; }
1768
+ axis = nv / m;
1769
+ }
1770
+ }
1771
+ if (axis_ok) {
1772
+ axis = axis / length(axis);
1773
+ var t_min = h(4.0);
1774
+ var t_max = h(-4.0);
1775
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1776
+ let t = dot(pix[k] - mean, axis);
1777
+ t_min = min(t_min, t);
1778
+ t_max = max(t_max, t);
1779
+ }
1780
+ seed_lo = clamp(mean + t_min * axis, h4(0.0), h4(1.0));
1781
+ seed_hi = clamp(mean + t_max * axis, h4(0.0), h4(1.0));
1209
1782
  }
1210
- }
1211
1783
 
1212
- // Straight-line packing: block mode 0x042 @0, partitions\u22121=0 @11, CEM 12
1213
- // @13, endpoints R0 R1 G0 G1 B0 B1 A0 A1 (8 bits each) from bit 17.
1214
- let w0 = 0x042u | (12u << 13u) | (E0.x << 17u) | (E1.x << 25u);
1215
- let w1 = (E1.x >> 7u) | (E0.y << 1u) | (E1.y << 9u) | (E0.z << 17u) | (E1.z << 25u);
1216
- let w2 = (E1.z >> 7u) | (E0.w << 1u) | (E1.w << 9u);
1784
+ // Endpoint selection. OPAQUE blocks (CEM 8, 8-level weights) quantise
1785
+ // the PCA-extent seed directly, BC7-style \u2014 the LSQ fit pass measured
1786
+ // +26% GPU for \u22120.06..+0.31 dB against the converged 8-step axis
1787
+ // (/ab + PSNR A/B, 2026-07): at 8 weight levels the projection is fine
1788
+ // enough that a good axis, not a refit, carries the quality.
1789
+ // TRANSLUCENT blocks (CEM 12) keep the fit: 4 levels are coarse enough
1790
+ // that dropping it costs 0.5+ dB. Its result is clamped to the block
1791
+ // bbox: on multi-cluster blocks the unconstrained solve extrapolates
1792
+ // far outside the block's colours and the per-channel [0,1] clamp then
1793
+ // bends the hue \u2014 fringe pixels decode to colours that exist nowhere
1794
+ // in the block (and the bbox constraint also measures better in plain
1795
+ // SSE, +1.8 dB on the colour test card).
1796
+ var e0 = lo;
1797
+ var e1 = hi;
1798
+ var fitStream = 0u;
1799
+ var haveFitWeights = false;
1800
+ if (opaque) {
1801
+ // Bbox-clamped like the fit output: on multi-cluster blocks the axis
1802
+ // extents overshoot the data per-channel and decode to colours that
1803
+ // exist nowhere in the block (the odd-size padding gate caught a
1804
+ // \u22123.9 dB crop without this).
1805
+ e0 = clamp(seed_lo, lo, hi);
1806
+ e1 = clamp(seed_hi, lo, hi);
1807
+ } else {
1808
+ let r = proj_fit(&pix, seed_lo, seed_hi);
1809
+ if (r.valid) {
1810
+ e0 = clamp(r.e0, lo, hi);
1811
+ e1 = clamp(r.e1, lo, hi);
1812
+ fitStream = r.wstream;
1813
+ haveFitWeights = true;
1814
+ }
1815
+ }
1816
+ var E0 = q8(e0);
1817
+ var E1 = q8(e1);
1818
+
1819
+ // Blue-contraction ordering, applied before the weight pass so weights
1820
+ // are already oriented (no reflection needed).
1821
+ var swapped = false;
1822
+ if (E0.x + E0.y + E0.z > E1.x + E1.y + E1.z) {
1823
+ let t = E0; E0 = E1; E1 = t;
1824
+ swapped = true;
1825
+ }
1826
+ let d0 = h4(vec4<f32>(E0)) * h(1.0 / 255.0);
1827
+ let d1 = h4(vec4<f32>(E1)) * h(1.0 / 255.0);
1828
+
1829
+ // Weight pass against the decoded endpoints. Same \xD732 pre-scale as
1830
+ // proj_fit; distinct 8-bit endpoints are \u22651/255 apart (dd\u2083\u2082 \u2265 0.0157),
1831
+ // so the flat threshold only catches identical ones.
1832
+ let dir = (d1 - d0) * h(32.0);
1833
+ let dd = dot(dir, dir);
1834
+ if (opaque) {
1835
+ // CEM 8: 3-bit weights, stream bit q = 3k.
1836
+ var s0 = 0u; var s1 = 0u;
1837
+ if (dd >= h(0.008)) {
1838
+ let inv = h(224.0) / dd;
1839
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1840
+ let w = u32(clamp(floor(dot(pix[k] - d0, dir) * inv + h(0.5)), h(0.0), h(7.0)));
1841
+ let off = 3u * k;
1842
+ if (off < 30u) { s0 = s0 | (w << off); }
1843
+ else if (off == 30u) { s0 = s0 | (w << 30u); s1 = s1 | (w >> 2u); }
1844
+ else { s1 = s1 | (w << (off - 32u)); }
1845
+ }
1846
+ }
1847
+ // Mode 0x053, CEM 8 @13, endpoints R0 R1 G0 G1 B0 B1 from bit 17.
1848
+ w0 = 0x053u | (8u << 13u) | (E0.x << 17u) | (E1.x << 25u);
1849
+ w1 = (E1.x >> 7u) | (E0.y << 1u) | (E1.y << 9u) | (E0.z << 17u) | (E1.z << 25u);
1850
+ w2 = (E1.z >> 7u) | reverseBits(s1);
1851
+ w3 = reverseBits(s0);
1852
+ } else {
1853
+ // CEM 12: 2-bit weights, stream bit q = 2k (single stream word).
1854
+ // Valid fits ship the FIT-PASS weights instead of reprojecting \u2014
1855
+ // worth a whole 16-pixel pass for \u22120.09 dB on the alpha card (/ab +
1856
+ // PSNR A/B, 2026-07; the pre-adaptive-CEM encoder rejected this same
1857
+ // trade when EVERY block was CEM 12 \u2014 now only translucent blocks
1858
+ // pay it). The blue-contraction swap is a full reflection w \u2192 3\u2212w,
1859
+ // i.e. bitwise NOT of the packed stream. Invalid fits (rank-1 /
1860
+ // degenerate) fall back to reprojection against the bbox endpoints.
1861
+ var s0 = 0u;
1862
+ if (haveFitWeights) {
1863
+ s0 = select(fitStream, ~fitStream, swapped);
1864
+ } else if (dd >= h(0.008)) {
1865
+ let inv = h(96.0) / dd;
1866
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1867
+ let w = u32(clamp(floor(dot(pix[k] - d0, dir) * inv + h(0.5)), h(0.0), h(3.0)));
1868
+ s0 = s0 | (w << (2u * k));
1869
+ }
1870
+ }
1871
+ // Mode 0x042, CEM 12 @13, endpoints R0 R1 G0 G1 B0 B1 A0 A1 from 17.
1872
+ w0 = 0x042u | (12u << 13u) | (E0.x << 17u) | (E1.x << 25u);
1873
+ w1 = (E1.x >> 7u) | (E0.y << 1u) | (E1.y << 9u) | (E0.z << 17u) | (E1.z << 25u);
1874
+ w2 = (E1.z >> 7u) | (E0.w << 1u) | (E1.w << 9u);
1875
+ w3 = reverseBits(s0);
1876
+ }
1877
+ }
1217
1878
 
1218
1879
  let o = bi * 4u;
1219
1880
  dst[o] = w0; dst[o + 1u] = w1; dst[o + 2u] = w2; dst[o + 3u] = w3;
@@ -1244,6 +1905,36 @@ var ASTC4x4Encoder = class extends Encoder {
1244
1905
  }
1245
1906
  };
1246
1907
 
1908
+ // src/etc2.wgsl
1909
+ var etc2_default = "// ETC2 RGB8 compute shader encoder.\n//\n// Each invocation encodes one 4x4 pixel block into an 8-byte ETC2 RGB8 block\n// written as 2 x u32 into the destination storage buffer. ETC2 blocks are\n// big-endian on the wire (byte 0 = bits 63..56), so both words are byte-\n// swapped on the way out. The f16 module (etc2_fast_f16.wgsl) is an\n// EXACT-VALUE port \u2014 byte-identical output; see its header.\n//\n// ALGORITHM \u2014 scalar-luma selection (2026-07 rewrite; the original\n// brute-force 8-table \xD7 4-modifier \xD7 vec3-with-clamp search measured\n// 6.0 ms @2048\xB2 on Apple/metal-3, this one ~0.197 ms with the DRAM read\n// floor \u2014 16 loads + store, nothing else \u2014 at ~0.15). This is the SETTLED\n// speed/quality point: the two-candidate scored search below was once\n// swapped for an O(1) hedged pick (\u22124-7% GPU) but cost \u22120.5 dB average \u2014\n// a ~10\xD7 worse dB-per-percent trade than the refit drop \u2014 and was\n// restored. A two-pass prepared-source variant (encode pass 0.115 ms) is\n// in git history: its prep pass is also DRAM-bound and cannot overlap,\n// so the per-texture total regressed. Reading the full RGBA8 source once\n// is this machine's hard floor for any single-pass encoder; the ~0.045\n// above it is the whole algorithm.\n//\n// \u2022 The ETC1 modifier is a SCALAR shift along (1,1,1), so per texel\n// err(m) = ||e||\xB2 \u2212 2mD + 3m\xB2 with D = luma(p) \u2212 luma(base), where\n// luma(x) = x.r+x.g+x.b. Selection therefore needs only |D| threshold\n// tests: the best table entry is the m with 3m nearest D (A3/B3/THR\n// below), and \u03A3||e||\xB2 per subblock is O(1) from the load loop's\n// quadrant sums (\u03A3||p||\xB2 \u2212 2\xB7base\xB7\u03A3p + 8\xB7||base||\xB2). This estimate is\n// EXACT for unclamped decode and an UPPER BOUND on the true clamped\n// error (clamping toward [0,255] can only shrink per-channel error),\n// so every est-based gate is conservative.\n// \u2022 Flip preselect, O(1): per subblock the residual after PERFECT\n// continuous luma modulation is within-variance \u2212 (luma variance)/3;\n// the flip with the smaller summed residual wins and only it is\n// searched (both-flip est search measured +23% GPU for \u22640.15 dB).\n// Exact-grayscale blocks have BOTH residuals identically zero (all\n// variance is along luma), so near-ties fall back to scoring both\n// flips \u2014 without that, roughness/AO-style content loses ~1.25 dB.\n// \u2022 Table search is pruned to two candidates \u2014 the table whose LARGE\n// magnitude covers max|D| and its lower neighbour (outlier hedge).\n// One candidate loses ~1.2-1.6 dB on photos; all eight gain \u22640.05 dB.\n// \u2022 NO base refit. The refit family (base \u2190 subblock mean \u2212 mean chosen\n// modifier) was worth ~0.2 dB on photographic colour (rock-color\n// 33.98 \u2192 33.79 without it) but even its cheapest accepted form cost\n// ~13% GPU and the exact-accept original ~30% \u2014 dropped 2026-07 as a\n// deliberate speed/quality trade; see the suite baselines.\n// \u2022 PLANAR runs unconditionally: with the right-hand sides folded into\n// the load loop the LSQ solve is O(1) (the Gram inverse of the fixed\n// sample positions is a constant, det = 25) and its residual is the\n// closed-form \u03A3||p||\xB2 \u2212 2\xB7\u03B8\xB7rhs + \u03B8\u1D40G\u03B8 evaluated with the QUANTISED,\n// clamped corners \u2014 exact up to decode's floor-rounding, and crucially\n// clamp-aware (a continuous-corner estimate mis-picks planar on steep\n// gradients). Gating planar cost \u22120.31 dB on smooth content for zero\n// measured speed.\n// \u2022 T and H modes are decoded by hardware but never emitted \u2014 their win\n// is limited to two-chroma-cluster blocks (the colour card's per-pixel\n// chroma checkers are the visible gap) and needs a clustering pass.\n//\n// Numeric notes: texel loads use round(load\xB7255) (integer-exact unorm trip);\n// every m3 in A3/B3 is divisible by 3 so m = m3/3 is exact; est values are\n// integer sums held exactly in f32 (< 2^24).\n\nstruct Params {\n blocks_x: u32,\n blocks_y: u32,\n width: u32,\n height: u32,\n};\n\n@group(0) @binding(0) var src_tex: texture_2d<f32>;\n@group(0) @binding(1) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(2) var<uniform> params: Params;\n\nconst A3 = array<f32, 8>(6.0, 15.0, 27.0, 39.0, 54.0, 72.0, 99.0, 141.0);\nconst B3 = array<f32, 8>(24.0, 51.0, 87.0, 126.0, 180.0, 240.0, 318.0, 549.0);\nconst THR = array<f32, 8>(15.0, 33.0, 57.0, 82.5, 117.0, 156.0, 208.5, 345.0);\n\n// Planar's closed-form estimate models the QUANTISED corners exactly; only\n// decode's floor-rounding (\xB1\xBD per sample) is unmodelled. This small bias\n// keeps near-ties on the predictable ETC1 side.\nconst PLANAR_FUDGE = 8.0;\n\nfn texel_of(flip: u32, sb: u32, i: u32) -> u32 {\n if (flip == 0u) {\n return (i >> 1u) * 4u + sb * 2u + (i & 1u);\n }\n return (sb * 2u + (i >> 2u)) * 4u + (i & 3u);\n}\n\nfn quant_codes(v: vec3<f32>, max_code: vec3<f32>) -> vec3<u32> {\n return vec3<u32>(clamp(floor(v * max_code * (1.0 / 255.0) + 0.5), vec3<f32>(0.0), max_code));\n}\n\nfn extend4(c: vec3<u32>) -> vec3<f32> {\n return vec3<f32>((c << vec3<u32>(4u)) | c);\n}\nfn extend5(c: vec3<u32>) -> vec3<f32> {\n return vec3<f32>((c << vec3<u32>(3u)) | (c >> vec3<u32>(2u)));\n}\n\nfn signed3(bits: u32) -> i32 {\n return select(i32(bits), i32(bits) - 8, bits > 3u);\n}\n\nfn bswap(x: u32) -> u32 {\n return ((x & 0xffu) << 24u) | ((x & 0xff00u) << 8u) | ((x >> 8u) & 0xff00u) | (x >> 24u);\n}\n\nstruct BasePair {\n codes0: vec3<u32>,\n codes1: vec3<u32>,\n ok: bool,\n};\nfn quantise_bases(avg0: vec3<f32>, avg1: vec3<f32>, diff: bool, clamp_delta: bool) -> BasePair {\n var out: BasePair;\n out.ok = true;\n if (!diff) {\n out.codes0 = quant_codes(avg0, vec3<f32>(15.0));\n out.codes1 = quant_codes(avg1, vec3<f32>(15.0));\n return out;\n }\n let q0 = vec3<i32>(quant_codes(avg0, vec3<f32>(31.0)));\n let q1 = vec3<i32>(quant_codes(avg1, vec3<f32>(31.0)));\n let d = q1 - q0;\n if (any(d < vec3<i32>(-4)) || any(d > vec3<i32>(3))) {\n if (!clamp_delta) {\n out.ok = false;\n return out;\n }\n }\n out.codes0 = vec3<u32>(q0);\n out.codes1 = vec3<u32>(q0 + clamp(d, vec3<i32>(-4), vec3<i32>(3)));\n return out;\n}\n\nstruct SearchOut {\n table: u32,\n acc: f32,\n};\nfn sb_table_score(luma: ptr<function, array<f32, 16>>, flip: u32, sb: u32, lb: f32, t: u32) -> f32 {\n let a3 = A3[t];\n let b3 = B3[t];\n let thr = THR[t];\n var acc = 0.0;\n for (var i: u32 = 0u; i < 8u; i = i + 1u) {\n let ad = abs((*luma)[texel_of(flip, sb, i)] - lb);\n let m3 = select(a3, b3, ad > thr);\n acc = acc + m3 * (m3 - 2.0 * ad);\n }\n return acc;\n}\nfn sb_search(luma: ptr<function, array<f32, 16>>, flip: u32, sb: u32, lb: f32) -> SearchOut {\n var mx = 0.0;\n for (var i: u32 = 0u; i < 8u; i = i + 1u) {\n mx = max(mx, abs((*luma)[texel_of(flip, sb, i)] - lb));\n }\n let cover = min(\n u32(mx > 24.0) + u32(mx > 51.0) + u32(mx > 87.0) + u32(mx > 126.0) +\n u32(mx > 180.0) + u32(mx > 240.0) + u32(mx > 318.0),\n 7u,\n );\n let t_lo = select(cover - 1u, 0u, cover == 0u);\n let acc_lo = sb_table_score(luma, flip, sb, lb, t_lo);\n let acc_hi = sb_table_score(luma, flip, sb, lb, cover);\n var out: SearchOut;\n let lo_wins = acc_lo <= acc_hi;\n out.table = select(cover, t_lo, lo_wins);\n out.acc = select(acc_hi, acc_lo, lo_wins);\n return out;\n}\n\n// One flip's base quantisation + table search: everything the flip contest\n// and the index derivation need.\nstruct FlipFit {\n est: f32,\n diff: bool,\n bases: BasePair,\n lb0: f32,\n lb1: f32,\n t0: u32,\n t1: u32,\n};\nfn eval_flip(\n luma: ptr<function, array<f32, 16>>,\n flip: u32,\n sum0: vec3<f32>,\n sq0: f32,\n sum1: vec3<f32>,\n sq1: f32,\n) -> FlipFit {\n let avg0 = sum0 * 0.125;\n let avg1 = sum1 * 0.125;\n let try_diff = quantise_bases(avg0, avg1, true, false);\n var out: FlipFit;\n out.diff = try_diff.ok;\n if (out.diff) {\n out.bases = try_diff;\n } else {\n out.bases = quantise_bases(avg0, avg1, false, false);\n }\n var b0: vec3<f32>;\n var b1: vec3<f32>;\n if (out.diff) {\n b0 = extend5(out.bases.codes0);\n b1 = extend5(out.bases.codes1);\n } else {\n b0 = extend4(out.bases.codes0);\n b1 = extend4(out.bases.codes1);\n }\n out.lb0 = b0.r + b0.g + b0.b;\n out.lb1 = b1.r + b1.g + b1.b;\n let s0 = sb_search(luma, flip, 0u, out.lb0);\n let s1 = sb_search(luma, flip, 1u, out.lb1);\n out.t0 = s0.table;\n out.t1 = s1.table;\n out.est = (sq0 - 2.0 * dot(b0, sum0) + 8.0 * dot(b0, b0)) +\n (sq1 - 2.0 * dot(b1, sum1) + 8.0 * dot(b1, b1)) +\n (s0.acc + s1.acc) * (1.0 / 3.0);\n return out;\n}\n\n// Wire indices for a chosen table \u2014 computed ONCE, from the final base.\nfn sb_indices(luma: ptr<function, array<f32, 16>>, flip: u32, sb: u32, lb: f32, t: u32) -> u32 {\n let thr = THR[t];\n var indices = 0u;\n for (var i: u32 = 0u; i < 8u; i = i + 1u) {\n let d = (*luma)[texel_of(flip, sb, i)] - lb;\n let large = abs(d) > thr;\n let neg = d < 0.0;\n indices = indices | ((select(0u, 1u, large) | select(0u, 2u, neg)) << (i * 2u));\n }\n return indices;\n}\n\n@compute @workgroup_size(8, 8, 1)\nfn encode(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) {\n return;\n }\n\n let block_index = gid.y * params.blocks_x + gid.x;\n let base_xy = vec2<i32>(i32(gid.x) * 4, i32(gid.y) * 4);\n let max_xy = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);\n\n var luma: array<f32, 16>;\n var qsum: array<vec3<f32>, 4>;\n var qsq: array<f32, 4>;\n var qlsq: array<f32, 4>;\n // Planar right-hand sides, folded into the load: rB = \u03A3 (x/4)\xB7p and\n // rC = \u03A3 (y/4)\xB7p accumulate unscaled; rA = \u03A3p \u2212 rB \u2212 rC afterwards.\n var sxp = vec3<f32>(0.0);\n var syp = vec3<f32>(0.0);\n\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let lx = i32(i & 3u);\n let ly = i32(i >> 2u);\n let p = clamp(base_xy + vec2<i32>(lx, ly), vec2<i32>(0, 0), max_xy);\n let c = round(textureLoad(src_tex, p, 0).rgb * 255.0);\n let l = c.r + c.g + c.b;\n luma[i] = l;\n let q = u32(lx >= 2) | (u32(ly >= 2) << 1u);\n qsum[q] = qsum[q] + c;\n qsq[q] = qsq[q] + dot(c, c);\n qlsq[q] = qlsq[q] + l * l;\n sxp = sxp + f32(lx) * c;\n syp = syp + f32(ly) * c;\n }\n\n // ----------------------------------------------- flip + base selection --\n // Flip preselect, O(1) from quadrant sums: per subblock the residual after\n // PERFECT continuous luma modulation is (\u03A3||p||\xB2 \u2212 ||\u03A3p||\xB2/8) \u2212\n // (\u03A3\u2113\xB2 \u2212 (\u03A3\u2113)\xB2/8)/3 \u2014 the within-variance minus the (1,1,1)-direction\n // component the modifier tables can absorb. The flip minimising the summed\n // residual wins and only it gets the table search \u2014 EXCEPT when the two\n // residuals are indistinguishable: for exact-grayscale blocks (r=g=b) both\n // are identically zero, so the contest falls back to scoring both flips\n // (this recovered \u22121.25 dB on roughness/AO-style content).\n let sum0a = qsum[0] + qsum[2];\n let sum1a = qsum[1] + qsum[3];\n let sq0a = qsq[0] + qsq[2];\n let sq1a = qsq[1] + qsq[3];\n let sum0b = qsum[0] + qsum[1];\n let sum1b = qsum[2] + qsum[3];\n let sq0b = qsq[0] + qsq[1];\n let sq1b = qsq[2] + qsq[3];\n let lsq0a = qlsq[0] + qlsq[2];\n let lsq1a = qlsq[1] + qlsq[3];\n let lsq0b = qlsq[0] + qlsq[1];\n let lsq1b = qlsq[2] + qlsq[3];\n let res_a = (sq0a - dot(sum0a, sum0a) * 0.125) - (lsq0a - dot(sum0a, vec3<f32>(1.0)) * dot(sum0a, vec3<f32>(1.0)) * 0.125) * (1.0 / 3.0)\n + (sq1a - dot(sum1a, sum1a) * 0.125) - (lsq1a - dot(sum1a, vec3<f32>(1.0)) * dot(sum1a, vec3<f32>(1.0)) * 0.125) * (1.0 / 3.0);\n let res_b = (sq0b - dot(sum0b, sum0b) * 0.125) - (lsq0b - dot(sum0b, vec3<f32>(1.0)) * dot(sum0b, vec3<f32>(1.0)) * 0.125) * (1.0 / 3.0)\n + (sq1b - dot(sum1b, sum1b) * 0.125) - (lsq1b - dot(sum1b, vec3<f32>(1.0)) * dot(sum1b, vec3<f32>(1.0)) * 0.125) * (1.0 / 3.0);\n\n // Single eval_flip call site (a second inlined copy measured +50% GPU):\n // attempt 0 scores the primary flip, attempt 1 runs only in the dual\n // (indistinguishable-residuals) case and scores the other flip.\n let dual = abs(res_a - res_b) < 1.0;\n let primary = select(select(0u, 1u, res_b < res_a), 0u, dual);\n var bflip = primary;\n var sel: FlipFit;\n for (var attempt = 0u; attempt < 2u; attempt = attempt + 1u) {\n if (attempt == 1u && !dual) {\n break;\n }\n let f = select(primary, 1u, attempt == 1u);\n let cand = eval_flip(\n &luma,\n f,\n select(sum0a, sum0b, f == 1u),\n select(sq0a, sq0b, f == 1u),\n select(sum1a, sum1b, f == 1u),\n select(sq1a, sq1b, f == 1u),\n );\n if (attempt == 0u || cand.est < sel.est) {\n sel = cand;\n bflip = f;\n }\n }\n let bdiff = sel.diff;\n\n let best_est = sel.est;\n let codes0 = sel.bases.codes0;\n let codes1 = sel.bases.codes1;\n let t0 = sel.t0;\n let t1 = sel.t1;\n let fit0 = sb_indices(&luma, bflip, 0u, sel.lb0, t0);\n let fit1 = sb_indices(&luma, bflip, 1u, sel.lb1, t1);\n\n // ------------------------------------------------------------ planar --\n // Always evaluated: with the rhs folded into the load loop this is O(1),\n // and gating it on the ETC1 estimate measured \u22120.31 dB on smooth content\n // for zero speed.\n let total = qsum[0] + qsum[1] + qsum[2] + qsum[3];\n let sqtotal = qsq[0] + qsq[1] + qsq[2] + qsq[3];\n let rB = sxp * 0.25;\n let rC = syp * 0.25;\n let rA = total - rB - rC;\n let po = 0.2875 * rA - 0.0125 * rB - 0.0125 * rC;\n let ph = -0.0125 * rA + 0.4875 * rB - 0.3125 * rC;\n let pv = -0.0125 * rA - 0.3125 * rB + 0.4875 * rC;\n let pmax = vec3<f32>(63.0, 127.0, 63.0);\n let qo = quant_codes(po, pmax);\n let qh = quant_codes(ph, pmax);\n let qv = quant_codes(pv, pmax);\n // Residual of the plane the hardware will ACTUALLY decode \u2014 the\n // quantised, clamped corners \u2014 via the normal-equation identity\n // \u03A3||p \u2212 f||\xB2 = \u03A3||p||\xB2 \u2212 2\xB7\u03B8\xB7rhs + \u03B8\u1D40G\u03B8 (G is the constant Gram matrix\n // of the fixed sample positions). Estimating with the CONTINUOUS corners\n // instead is blind to corner clamping and mis-picks planar on steep\n // gradients (a 1.4-normalised-SSE easy-block artifact on the colour\n // card). Only decode's floor-rounding stays unmodelled (\u2264 ~12 SSE).\n let shl = vec3<u32>(2u, 1u, 2u);\n let shr = vec3<u32>(4u, 6u, 4u);\n let eo = vec3<f32>((qo << shl) | (qo >> shr));\n let eh = vec3<f32>((qh << shl) | (qh >> shr));\n let ev = vec3<f32>((qv << shl) | (qv >> shr));\n let gram = 3.5 * (eo * eo + eh * eh + ev * ev) + 0.5 * eo * eh + 0.5 * eo * ev + 4.5 * eh * ev;\n let planar_est = sqtotal - 2.0 * (dot(eo, rA) + dot(eh, rB) + dot(ev, rC)) +\n dot(gram, vec3<f32>(1.0)) + PLANAR_FUDGE;\n\n // ------------------------------------------------------------ packing --\n var hi: u32;\n var lo: u32;\n if (best_est <= planar_est) {\n if (bdiff) {\n let d = vec3<u32>(vec3<i32>(codes1) - vec3<i32>(codes0)) & vec3<u32>(7u);\n hi = (codes0.r << 27u) | (d.r << 24u) | (codes0.g << 19u) | (d.g << 16u) | (codes0.b << 11u) | (d.b << 8u)\n | (t0 << 5u) | (t1 << 2u) | 2u | bflip;\n } else {\n hi = (codes0.r << 28u) | (codes1.r << 24u) | (codes0.g << 20u) | (codes1.g << 16u) | (codes0.b << 12u) | (codes1.b << 8u)\n | (t0 << 5u) | (t1 << 2u) | bflip;\n }\n lo = 0u;\n for (var sb: u32 = 0u; sb < 2u; sb = sb + 1u) {\n let indices = select(fit0, fit1, sb == 1u);\n for (var i: u32 = 0u; i < 8u; i = i + 1u) {\n let k = texel_of(bflip, sb, i);\n let wire = (k & 3u) * 4u + (k >> 2u);\n let idx = (indices >> (i * 2u)) & 3u;\n lo = lo | ((idx & 1u) << wire) | ((idx >> 1u) << (16u + wire));\n }\n }\n } else {\n let ro = qo.r; let go = qo.g; let bo = qo.b;\n let rh = qh.r; let gh = qh.g; let bh = qh.b;\n let rv = qv.r; let gv = qv.g; let bv = qv.b;\n let r_sum = i32(ro >> 2u) + signed3(((ro & 3u) << 1u) | (go >> 6u));\n let r_fix = select(0u, 1u, r_sum < 0);\n let g_sum = i32((go >> 2u) & 15u) + signed3(((go & 3u) << 1u) | (bo >> 5u));\n let g_fix = select(0u, 1u, g_sum < 0);\n let p = (bo >> 3u) & 3u;\n let q = (bo >> 1u) & 3u;\n let b_fix3 = select(0u, 7u, p + q >= 4u);\n let b_fix1 = select(1u, 0u, p + q >= 4u);\n hi = (r_fix << 31u) | (ro << 25u) | ((go >> 6u) << 24u) | (g_fix << 23u) | ((go & 63u) << 17u)\n | ((bo >> 5u) << 16u) | (b_fix3 << 13u) | (((bo >> 3u) & 3u) << 11u) | (b_fix1 << 10u)\n | ((bo & 7u) << 7u) | ((rh >> 1u) << 2u) | 2u | (rh & 1u);\n lo = (gh << 25u) | (bh << 19u) | (rv << 13u) | (gv << 6u) | bv;\n }\n\n let out = block_index * 2u;\n dst[out] = bswap(hi);\n dst[out + 1u] = bswap(lo);\n}\n";
1910
+
1911
+ // src/etc2_fast_f16.wgsl
1912
+ var etc2_fast_f16_default = "// ETC2 RGB8 compute shader encoder.\n//\n// Each invocation encodes one 4x4 pixel block into an 8-byte ETC2 RGB8 block\n// written as 2 x u32 into the destination storage buffer. ETC2 blocks are\n// big-endian on the wire (byte 0 = bits 63..56), so both words are byte-\n// swapped on the way out. This is that f16 module.\n//\n// EXACT-VALUE f16: unlike the other formats' f16 fast paths (which accept\n// float rounding in a [0,1] domain), every f16 value in this shader is an\n// integer that f16 represents exactly \u2014 lumas and bases (<= 765), D values\n// (|D| <= 765) and thresholds (<= 549) all sit below f16's 2048 integer-\n// exactness limit. Sums of squares, scores and estimates stay f32 (they\n// reach +-5e5..9e6, far past f16's 65504 max). The output is therefore\n// BYTE-IDENTICAL to the f32 module \u2014 verified per-block on the suite\n// textures \u2014 and the two modules share every pin and every test gate.\n//\n// What f16 buys here is register pressure (the luma array halves), not\n// arithmetic rate: on Apple/metal-3 the two modules measure identical\n// (the shader is DRAM-read-bound), but on the mobile GPUs where ETC2 is\n// actually the target format, occupancy from smaller registers is the\n// cheapest speed there is. The COLOUR accumulators deliberately stay f32\n// even though quadrant/pair sums (<= 2040) would be exact in f16: porting\n// them measured 15% SLOWER on Apple (conversion traffic outweighs the\n// register saving). Luma + the table search are the f16 surface.\n//\n// ALGORITHM \u2014 scalar-luma selection (2026-07 rewrite; the original\n// brute-force 8-table \xD7 4-modifier \xD7 vec3-with-clamp search measured\n// 6.0 ms @2048\xB2 on Apple/metal-3, this one ~0.197 ms with the DRAM read\n// floor \u2014 16 loads + store, nothing else \u2014 at ~0.15). This is the SETTLED\n// speed/quality point: the two-candidate scored search below was once\n// swapped for an O(1) hedged pick (\u22124-7% GPU) but cost \u22120.5 dB average \u2014\n// a ~10\xD7 worse dB-per-percent trade than the refit drop \u2014 and was\n// restored. A two-pass prepared-source variant (encode pass 0.115 ms) is\n// in git history: its prep pass is also DRAM-bound and cannot overlap,\n// so the per-texture total regressed. Reading the full RGBA8 source once\n// is this machine's hard floor for any single-pass encoder; the ~0.045\n// above it is the whole algorithm.\n//\n// \u2022 The ETC1 modifier is a SCALAR shift along (1,1,1), so per texel\n// err(m) = ||e||\xB2 \u2212 2mD + 3m\xB2 with D = luma(p) \u2212 luma(base), where\n// luma(x) = x.r+x.g+x.b. Selection therefore needs only |D| threshold\n// tests: the best table entry is the m with 3m nearest D (A3/B3/THR\n// below), and \u03A3||e||\xB2 per subblock is O(1) from the load loop's\n// quadrant sums (\u03A3||p||\xB2 \u2212 2\xB7base\xB7\u03A3p + 8\xB7||base||\xB2). This estimate is\n// EXACT for unclamped decode and an UPPER BOUND on the true clamped\n// error (clamping toward [0,255] can only shrink per-channel error),\n// so every est-based gate is conservative.\n// \u2022 Flip preselect, O(1): per subblock the residual after PERFECT\n// continuous luma modulation is within-variance \u2212 (luma variance)/3;\n// the flip with the smaller summed residual wins and only it is\n// searched (both-flip est search measured +23% GPU for \u22640.15 dB).\n// Exact-grayscale blocks have BOTH residuals identically zero (all\n// variance is along luma), so near-ties fall back to scoring both\n// flips \u2014 without that, roughness/AO-style content loses ~1.25 dB.\n// \u2022 Table search is pruned to two candidates \u2014 the table whose LARGE\n// magnitude covers max|D| and its lower neighbour (outlier hedge).\n// One candidate loses ~1.2-1.6 dB on photos; all eight gain \u22640.05 dB.\n// \u2022 NO base refit. The refit family (base \u2190 subblock mean \u2212 mean chosen\n// modifier) was worth ~0.2 dB on photographic colour (rock-color\n// 33.98 \u2192 33.79 without it) but even its cheapest accepted form cost\n// ~13% GPU and the exact-accept original ~30% \u2014 dropped 2026-07 as a\n// deliberate speed/quality trade; see the suite baselines.\n// \u2022 PLANAR runs unconditionally: with the right-hand sides folded into\n// the load loop the LSQ solve is O(1) (the Gram inverse of the fixed\n// sample positions is a constant, det = 25) and its residual is the\n// closed-form \u03A3||p||\xB2 \u2212 2\xB7\u03B8\xB7rhs + \u03B8\u1D40G\u03B8 evaluated with the QUANTISED,\n// clamped corners \u2014 exact up to decode's floor-rounding, and crucially\n// clamp-aware (a continuous-corner estimate mis-picks planar on steep\n// gradients). Gating planar cost \u22120.31 dB on smooth content for zero\n// measured speed.\n// \u2022 T and H modes are decoded by hardware but never emitted \u2014 their win\n// is limited to two-chroma-cluster blocks (the colour card's per-pixel\n// chroma checkers are the visible gap) and needs a clustering pass.\n//\n// Numeric notes: texel loads use round(load\xB7255) (integer-exact unorm trip);\n// every m3 in A3/B3 is divisible by 3 so m = m3/3 is exact; est values are\n// integer sums held exactly in f32 (< 2^24).\n\nenable f16;\n\nstruct Params {\n blocks_x: u32,\n blocks_y: u32,\n width: u32,\n height: u32,\n};\n\n@group(0) @binding(0) var src_tex: texture_2d<f32>;\n@group(0) @binding(1) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(2) var<uniform> params: Params;\n\nconst A3 = array<f32, 8>(6.0, 15.0, 27.0, 39.0, 54.0, 72.0, 99.0, 141.0);\nconst B3 = array<f32, 8>(24.0, 51.0, 87.0, 126.0, 180.0, 240.0, 318.0, 549.0);\nconst THR = array<f32, 8>(15.0, 33.0, 57.0, 82.5, 117.0, 156.0, 208.5, 345.0);\n\n// Planar's closed-form estimate models the QUANTISED corners exactly; only\n// decode's floor-rounding (\xB1\xBD per sample) is unmodelled. This small bias\n// keeps near-ties on the predictable ETC1 side.\nconst PLANAR_FUDGE = 8.0;\n\nfn texel_of(flip: u32, sb: u32, i: u32) -> u32 {\n if (flip == 0u) {\n return (i >> 1u) * 4u + sb * 2u + (i & 1u);\n }\n return (sb * 2u + (i >> 2u)) * 4u + (i & 3u);\n}\n\nfn quant_codes(v: vec3<f32>, max_code: vec3<f32>) -> vec3<u32> {\n return vec3<u32>(clamp(floor(v * max_code * (1.0 / 255.0) + 0.5), vec3<f32>(0.0), max_code));\n}\n\nfn extend4(c: vec3<u32>) -> vec3<f32> {\n return vec3<f32>((c << vec3<u32>(4u)) | c);\n}\nfn extend5(c: vec3<u32>) -> vec3<f32> {\n return vec3<f32>((c << vec3<u32>(3u)) | (c >> vec3<u32>(2u)));\n}\n\nfn signed3(bits: u32) -> i32 {\n return select(i32(bits), i32(bits) - 8, bits > 3u);\n}\n\nfn bswap(x: u32) -> u32 {\n return ((x & 0xffu) << 24u) | ((x & 0xff00u) << 8u) | ((x >> 8u) & 0xff00u) | (x >> 24u);\n}\n\nstruct BasePair {\n codes0: vec3<u32>,\n codes1: vec3<u32>,\n ok: bool,\n};\nfn quantise_bases(avg0: vec3<f32>, avg1: vec3<f32>, diff: bool, clamp_delta: bool) -> BasePair {\n var out: BasePair;\n out.ok = true;\n if (!diff) {\n out.codes0 = quant_codes(avg0, vec3<f32>(15.0));\n out.codes1 = quant_codes(avg1, vec3<f32>(15.0));\n return out;\n }\n let q0 = vec3<i32>(quant_codes(avg0, vec3<f32>(31.0)));\n let q1 = vec3<i32>(quant_codes(avg1, vec3<f32>(31.0)));\n let d = q1 - q0;\n if (any(d < vec3<i32>(-4)) || any(d > vec3<i32>(3))) {\n if (!clamp_delta) {\n out.ok = false;\n return out;\n }\n }\n out.codes0 = vec3<u32>(q0);\n out.codes1 = vec3<u32>(q0 + clamp(d, vec3<i32>(-4), vec3<i32>(3)));\n return out;\n}\n\nstruct SearchOut {\n table: u32,\n acc: f32,\n};\n// D-domain values (|D| <= 765, thresholds <= 549) are exact in f16; the\n// score PRODUCTS reach +-5e5 and must be f32.\nfn sb_table_score(luma: ptr<function, array<f16, 16>>, flip: u32, sb: u32, lb: f16, t: u32) -> f32 {\n let a3 = f16(A3[t]);\n let b3 = f16(B3[t]);\n let thr = f16(THR[t]);\n var acc = 0.0;\n for (var i: u32 = 0u; i < 8u; i = i + 1u) {\n let ad = abs((*luma)[texel_of(flip, sb, i)] - lb);\n let m3 = f32(select(a3, b3, ad > thr));\n acc = acc + m3 * (m3 - 2.0 * f32(ad));\n }\n return acc;\n}\nfn sb_search(luma: ptr<function, array<f16, 16>>, flip: u32, sb: u32, lb: f16) -> SearchOut {\n var mx: f16 = 0.0;\n for (var i: u32 = 0u; i < 8u; i = i + 1u) {\n mx = max(mx, abs((*luma)[texel_of(flip, sb, i)] - lb));\n }\n let mxf = f32(mx);\n let cover = min(\n u32(mxf > 24.0) + u32(mxf > 51.0) + u32(mxf > 87.0) + u32(mxf > 126.0) +\n u32(mxf > 180.0) + u32(mxf > 240.0) + u32(mxf > 318.0),\n 7u,\n );\n let t_lo = select(cover - 1u, 0u, cover == 0u);\n let acc_lo = sb_table_score(luma, flip, sb, lb, t_lo);\n let acc_hi = sb_table_score(luma, flip, sb, lb, cover);\n var out: SearchOut;\n let lo_wins = acc_lo <= acc_hi;\n out.table = select(cover, t_lo, lo_wins);\n out.acc = select(acc_hi, acc_lo, lo_wins);\n return out;\n}\n\n// One flip's base quantisation + table search: everything the flip contest\n// and the index derivation need.\nstruct FlipFit {\n est: f32,\n diff: bool,\n bases: BasePair,\n lb0: f32,\n lb1: f32,\n t0: u32,\n t1: u32,\n};\nfn eval_flip(\n luma: ptr<function, array<f16, 16>>,\n flip: u32,\n sum0: vec3<f32>,\n sq0: f32,\n sum1: vec3<f32>,\n sq1: f32,\n) -> FlipFit {\n let avg0 = sum0 * 0.125;\n let avg1 = sum1 * 0.125;\n let try_diff = quantise_bases(avg0, avg1, true, false);\n var out: FlipFit;\n out.diff = try_diff.ok;\n if (out.diff) {\n out.bases = try_diff;\n } else {\n out.bases = quantise_bases(avg0, avg1, false, false);\n }\n var b0: vec3<f32>;\n var b1: vec3<f32>;\n if (out.diff) {\n b0 = extend5(out.bases.codes0);\n b1 = extend5(out.bases.codes1);\n } else {\n b0 = extend4(out.bases.codes0);\n b1 = extend4(out.bases.codes1);\n }\n out.lb0 = b0.r + b0.g + b0.b;\n out.lb1 = b1.r + b1.g + b1.b;\n let s0 = sb_search(luma, flip, 0u, f16(out.lb0));\n let s1 = sb_search(luma, flip, 1u, f16(out.lb1));\n out.t0 = s0.table;\n out.t1 = s1.table;\n out.est = (sq0 - 2.0 * dot(b0, sum0) + 8.0 * dot(b0, b0)) +\n (sq1 - 2.0 * dot(b1, sum1) + 8.0 * dot(b1, b1)) +\n (s0.acc + s1.acc) * (1.0 / 3.0);\n return out;\n}\n\n// Wire indices for a chosen table \u2014 computed ONCE, from the final base.\nfn sb_indices(luma: ptr<function, array<f16, 16>>, flip: u32, sb: u32, lb: f16, t: u32) -> u32 {\n let thr = f16(THR[t]);\n var indices = 0u;\n for (var i: u32 = 0u; i < 8u; i = i + 1u) {\n let d = (*luma)[texel_of(flip, sb, i)] - lb;\n let large = abs(d) > thr;\n let neg = d < 0.0;\n indices = indices | ((select(0u, 1u, large) | select(0u, 2u, neg)) << (i * 2u));\n }\n return indices;\n}\n\n@compute @workgroup_size(8, 8, 1)\nfn encode(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) {\n return;\n }\n\n let block_index = gid.y * params.blocks_x + gid.x;\n let base_xy = vec2<i32>(i32(gid.x) * 4, i32(gid.y) * 4);\n let max_xy = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);\n\n // Luma lives in f16: every value is an integer <= 765, exact in f16.\n var luma: array<f16, 16>;\n var qsum: array<vec3<f32>, 4>;\n var qsq: array<f32, 4>;\n var qlsq: array<f32, 4>;\n // Planar right-hand sides, folded into the load: rB = \u03A3 (x/4)\xB7p and\n // rC = \u03A3 (y/4)\xB7p accumulate unscaled; rA = \u03A3p \u2212 rB \u2212 rC afterwards.\n var sxp = vec3<f32>(0.0);\n var syp = vec3<f32>(0.0);\n\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let lx = i32(i & 3u);\n let ly = i32(i >> 2u);\n let p = clamp(base_xy + vec2<i32>(lx, ly), vec2<i32>(0, 0), max_xy);\n let c = round(textureLoad(src_tex, p, 0).rgb * 255.0);\n let l = c.r + c.g + c.b;\n luma[i] = f16(l);\n let q = u32(lx >= 2) | (u32(ly >= 2) << 1u);\n qsum[q] = qsum[q] + c;\n qsq[q] = qsq[q] + dot(c, c);\n qlsq[q] = qlsq[q] + l * l;\n sxp = sxp + f32(lx) * c;\n syp = syp + f32(ly) * c;\n }\n\n // ----------------------------------------------- flip + base selection --\n // Flip preselect, O(1) from quadrant sums: per subblock the residual after\n // PERFECT continuous luma modulation is (\u03A3||p||\xB2 \u2212 ||\u03A3p||\xB2/8) \u2212\n // (\u03A3\u2113\xB2 \u2212 (\u03A3\u2113)\xB2/8)/3 \u2014 the within-variance minus the (1,1,1)-direction\n // component the modifier tables can absorb. The flip minimising the summed\n // residual wins and only it gets the table search \u2014 EXCEPT when the two\n // residuals are indistinguishable: for exact-grayscale blocks (r=g=b) both\n // are identically zero, so the contest falls back to scoring both flips\n // (this recovered \u22121.25 dB on roughness/AO-style content).\n let sum0a = qsum[0] + qsum[2];\n let sum1a = qsum[1] + qsum[3];\n let sq0a = qsq[0] + qsq[2];\n let sq1a = qsq[1] + qsq[3];\n let sum0b = qsum[0] + qsum[1];\n let sum1b = qsum[2] + qsum[3];\n let sq0b = qsq[0] + qsq[1];\n let sq1b = qsq[2] + qsq[3];\n let lsq0a = qlsq[0] + qlsq[2];\n let lsq1a = qlsq[1] + qlsq[3];\n let lsq0b = qlsq[0] + qlsq[1];\n let lsq1b = qlsq[2] + qlsq[3];\n let res_a = (sq0a - dot(sum0a, sum0a) * 0.125) - (lsq0a - dot(sum0a, vec3<f32>(1.0)) * dot(sum0a, vec3<f32>(1.0)) * 0.125) * (1.0 / 3.0)\n + (sq1a - dot(sum1a, sum1a) * 0.125) - (lsq1a - dot(sum1a, vec3<f32>(1.0)) * dot(sum1a, vec3<f32>(1.0)) * 0.125) * (1.0 / 3.0);\n let res_b = (sq0b - dot(sum0b, sum0b) * 0.125) - (lsq0b - dot(sum0b, vec3<f32>(1.0)) * dot(sum0b, vec3<f32>(1.0)) * 0.125) * (1.0 / 3.0)\n + (sq1b - dot(sum1b, sum1b) * 0.125) - (lsq1b - dot(sum1b, vec3<f32>(1.0)) * dot(sum1b, vec3<f32>(1.0)) * 0.125) * (1.0 / 3.0);\n\n // Single eval_flip call site (a second inlined copy measured +50% GPU):\n // attempt 0 scores the primary flip, attempt 1 runs only in the dual\n // (indistinguishable-residuals) case and scores the other flip.\n let dual = abs(res_a - res_b) < 1.0;\n let primary = select(select(0u, 1u, res_b < res_a), 0u, dual);\n var bflip = primary;\n var sel: FlipFit;\n for (var attempt = 0u; attempt < 2u; attempt = attempt + 1u) {\n if (attempt == 1u && !dual) {\n break;\n }\n let f = select(primary, 1u, attempt == 1u);\n let cand = eval_flip(\n &luma,\n f,\n select(sum0a, sum0b, f == 1u),\n select(sq0a, sq0b, f == 1u),\n select(sum1a, sum1b, f == 1u),\n select(sq1a, sq1b, f == 1u),\n );\n if (attempt == 0u || cand.est < sel.est) {\n sel = cand;\n bflip = f;\n }\n }\n let bdiff = sel.diff;\n\n let best_est = sel.est;\n let codes0 = sel.bases.codes0;\n let codes1 = sel.bases.codes1;\n let t0 = sel.t0;\n let t1 = sel.t1;\n let fit0 = sb_indices(&luma, bflip, 0u, f16(sel.lb0), t0);\n let fit1 = sb_indices(&luma, bflip, 1u, f16(sel.lb1), t1);\n\n // ------------------------------------------------------------ planar --\n // Always evaluated: with the rhs folded into the load loop this is O(1),\n // and gating it on the ETC1 estimate measured \u22120.31 dB on smooth content\n // for zero speed.\n let total = qsum[0] + qsum[1] + qsum[2] + qsum[3];\n let sqtotal = qsq[0] + qsq[1] + qsq[2] + qsq[3];\n let rB = sxp * 0.25;\n let rC = syp * 0.25;\n let rA = total - rB - rC;\n let po = 0.2875 * rA - 0.0125 * rB - 0.0125 * rC;\n let ph = -0.0125 * rA + 0.4875 * rB - 0.3125 * rC;\n let pv = -0.0125 * rA - 0.3125 * rB + 0.4875 * rC;\n let pmax = vec3<f32>(63.0, 127.0, 63.0);\n let qo = quant_codes(po, pmax);\n let qh = quant_codes(ph, pmax);\n let qv = quant_codes(pv, pmax);\n // Residual of the plane the hardware will ACTUALLY decode \u2014 the\n // quantised, clamped corners \u2014 via the normal-equation identity\n // \u03A3||p \u2212 f||\xB2 = \u03A3||p||\xB2 \u2212 2\xB7\u03B8\xB7rhs + \u03B8\u1D40G\u03B8 (G is the constant Gram matrix\n // of the fixed sample positions). Estimating with the CONTINUOUS corners\n // instead is blind to corner clamping and mis-picks planar on steep\n // gradients (a 1.4-normalised-SSE easy-block artifact on the colour\n // card). Only decode's floor-rounding stays unmodelled (\u2264 ~12 SSE).\n let shl = vec3<u32>(2u, 1u, 2u);\n let shr = vec3<u32>(4u, 6u, 4u);\n let eo = vec3<f32>((qo << shl) | (qo >> shr));\n let eh = vec3<f32>((qh << shl) | (qh >> shr));\n let ev = vec3<f32>((qv << shl) | (qv >> shr));\n let gram = 3.5 * (eo * eo + eh * eh + ev * ev) + 0.5 * eo * eh + 0.5 * eo * ev + 4.5 * eh * ev;\n let planar_est = sqtotal - 2.0 * (dot(eo, rA) + dot(eh, rB) + dot(ev, rC)) +\n dot(gram, vec3<f32>(1.0)) + PLANAR_FUDGE;\n\n // ------------------------------------------------------------ packing --\n var hi: u32;\n var lo: u32;\n if (best_est <= planar_est) {\n if (bdiff) {\n let d = vec3<u32>(vec3<i32>(codes1) - vec3<i32>(codes0)) & vec3<u32>(7u);\n hi = (codes0.r << 27u) | (d.r << 24u) | (codes0.g << 19u) | (d.g << 16u) | (codes0.b << 11u) | (d.b << 8u)\n | (t0 << 5u) | (t1 << 2u) | 2u | bflip;\n } else {\n hi = (codes0.r << 28u) | (codes1.r << 24u) | (codes0.g << 20u) | (codes1.g << 16u) | (codes0.b << 12u) | (codes1.b << 8u)\n | (t0 << 5u) | (t1 << 2u) | bflip;\n }\n lo = 0u;\n for (var sb: u32 = 0u; sb < 2u; sb = sb + 1u) {\n let indices = select(fit0, fit1, sb == 1u);\n for (var i: u32 = 0u; i < 8u; i = i + 1u) {\n let k = texel_of(bflip, sb, i);\n let wire = (k & 3u) * 4u + (k >> 2u);\n let idx = (indices >> (i * 2u)) & 3u;\n lo = lo | ((idx & 1u) << wire) | ((idx >> 1u) << (16u + wire));\n }\n }\n } else {\n let ro = qo.r; let go = qo.g; let bo = qo.b;\n let rh = qh.r; let gh = qh.g; let bh = qh.b;\n let rv = qv.r; let gv = qv.g; let bv = qv.b;\n let r_sum = i32(ro >> 2u) + signed3(((ro & 3u) << 1u) | (go >> 6u));\n let r_fix = select(0u, 1u, r_sum < 0);\n let g_sum = i32((go >> 2u) & 15u) + signed3(((go & 3u) << 1u) | (bo >> 5u));\n let g_fix = select(0u, 1u, g_sum < 0);\n let p = (bo >> 3u) & 3u;\n let q = (bo >> 1u) & 3u;\n let b_fix3 = select(0u, 7u, p + q >= 4u);\n let b_fix1 = select(1u, 0u, p + q >= 4u);\n hi = (r_fix << 31u) | (ro << 25u) | ((go >> 6u) << 24u) | (g_fix << 23u) | ((go & 63u) << 17u)\n | ((bo >> 5u) << 16u) | (b_fix3 << 13u) | (((bo >> 3u) & 3u) << 11u) | (b_fix1 << 10u)\n | ((bo & 7u) << 7u) | ((rh >> 1u) << 2u) | 2u | (rh & 1u);\n lo = (gh << 25u) | (bh << 19u) | (rv << 13u) | (gv << 6u) | bv;\n }\n\n let out = block_index * 2u;\n dst[out] = bswap(hi);\n dst[out + 1u] = bswap(lo);\n}\n";
1913
+
1914
+ // src/ETC2Encoder.ts
1915
+ var ETC2Encoder = class extends Encoder {
1916
+ static requiredFeature = WebGPUFeature.ETC2;
1917
+ static textureFormats = [TextureFormat.ETC2_RGB8, TextureFormat.ETC2_RGB8_SRGB];
1918
+ get label() {
1919
+ return "etc2";
1920
+ }
1921
+ get bytesPerBlock() {
1922
+ return 8;
1923
+ }
1924
+ get supportsSrgb() {
1925
+ return true;
1926
+ }
1927
+ wgslSource() {
1928
+ return etc2_default;
1929
+ }
1930
+ wgslSourceFastF16() {
1931
+ return etc2_fast_f16_default;
1932
+ }
1933
+ gpuTextureFormat({ colorSpace }) {
1934
+ return colorSpace === "srgb" ? "etc2-rgb8unorm-srgb" : "etc2-rgb8unorm";
1935
+ }
1936
+ };
1937
+
1247
1938
  // src/webgl/glsl/fullscreen.vert.glsl
1248
1939
  var fullscreen_vert_default = "#version 300 es\n// Fullscreen-triangle vertex shader for the WebGL block encoders.\n//\n// Draws a single oversized triangle covering the viewport from gl_VertexID\n// alone \u2014 no vertex buffers / attributes needed (drawArrays(TRIANGLES, 0, 3)).\n// The encoder sets the viewport to (blocks_x \xD7 blocks_y), so each rasterised\n// fragment corresponds to exactly one 4\xD74 output block.\n//\n// id 0 -> (-1,-1) id 1 -> ( 3,-1) id 2 -> (-1, 3)\n\nvoid main() {\n vec2 p = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));\n gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);\n}\n";
1249
1940
 
@@ -1455,7 +2146,7 @@ var WebGLBlockEncoder = class {
1455
2146
  };
1456
2147
 
1457
2148
  // src/webgl/glsl/bc1.frag.glsl
1458
- var bc1_frag_default = "#version 300 es\n// BC1 (DXT1) fragment-shader encoder \u2014 WebGL2 port of bc1.wgsl (fast path).\n//\n// One fragment per 4\xD74 block. Output is the 8-byte BC1 block as 2 \xD7 u32 in\n// outColor.rg (outColor.ba unused); the encoder reads back RGBA32UI and keeps\n// the low two words per block. Same algorithm as bc1.wgsl:\n// principal-axis endpoint seed (covariance\n// power-iteration; inset bbox on degenerate blocks), RGB565 quantisation,\n// forced 4-colour mode, full 4-entry L2 index search, then up to TWO\n// least-squares endpoint refit rounds, each accepted only when it lowers the\n// block's error. See bc1.wgsl for the full derivation.\n\nprecision highp float;\nprecision highp int;\n\nuniform sampler2D uSrc;\nuniform ivec2 uSrcSize; // original (unpadded) width, height\nuniform int uFlipY; // 1 = sample bottom-up (matches Three.js flipY)\n\nlayout(location = 0) out uvec4 outColor;\n\n// 4-colour-mode interpolation weights: pal[j] = WA[j]*c0 + WB[j]*c1.\nconst float WA[4] = float[4](1.0, 0.0, 2.0 / 3.0, 1.0 / 3.0);\nconst float WB[4] = float[4](0.0, 1.0, 1.0 / 3.0, 2.0 / 3.0);\n\nuint to565(vec3 c) {\n uint r = uint(clamp(floor(c.r * 31.0 + 0.5), 0.0, 31.0));\n uint g = uint(clamp(floor(c.g * 63.0 + 0.5), 0.0, 63.0));\n uint b = uint(clamp(floor(c.b * 31.0 + 0.5), 0.0, 31.0));\n return (r << 11) | (g << 5) | b;\n}\n\nvec3 from565(uint c) {\n float r = float((c >> 11) & 31u);\n float g = float((c >> 5) & 63u);\n float b = float(c & 31u);\n // 5/6-bit \u2192 8-bit. floor((x*527+23)/64) == (x<<3)|(x>>2): exact hardware\n // bit-replication (white \u2192 255), so index selection matches the GPU decode.\n float r8 = floor((r * 527.0 + 23.0) / 64.0);\n float g8 = floor((g * 259.0 + 33.0) / 64.0);\n float b8 = floor((b * 527.0 + 23.0) / 64.0);\n return vec3(r8, g8, b8) / 255.0;\n}\n\n// Per-invocation scratch (mirrors the WGSL function-scope arrays passed by\n// ptr; GLSL would copy array parameters by value).\nvec3 gPixels[16];\nuint gIdx[16];\n\n// Nearest-palette assignment of gPixels for the decoded palette of (c0,c1),\n// with the block's squared error and the LSQ normal-equation sums of the\n// resulting assignment accumulated in the same pass \u2014 so an accepted refit\n// can seed the next round.\nstruct Assign { float err; float sAA; float sBB; float sAB; vec3 sAV; vec3 sBV; };\nAssign assignStats(uint c0, uint c1, out uint indices[16]) {\n vec3 p0 = from565(c0);\n vec3 p1 = from565(c1);\n vec3 pal[4];\n for (int j = 0; j < 4; j++) pal[j] = WA[j] * p0 + WB[j] * p1;\n Assign r = Assign(0.0, 0.0, 0.0, 0.0, vec3(0.0), vec3(0.0));\n for (int k = 0; k < 16; k++) {\n vec3 c = gPixels[k];\n uint bestJ = 0u;\n float bestD = 1e30;\n for (int j = 0; j < 4; j++) {\n vec3 d = pal[j] - c;\n float d2 = dot(d, d);\n if (d2 < bestD) { bestD = d2; bestJ = uint(j); }\n }\n indices[k] = bestJ;\n r.err += bestD;\n float a = WA[int(bestJ)];\n float b = WB[int(bestJ)];\n r.sAA += a * a; r.sBB += b * b; r.sAB += a * b; r.sAV += a * c; r.sBV += b * c;\n }\n return r;\n}\n\nvoid main() {\n ivec2 base = ivec2(gl_FragCoord.xy) * 4;\n ivec2 maxXY = uSrcSize - ivec2(1);\n\n vec3 bbMin = vec3(1.0);\n vec3 bbMax = vec3(0.0);\n vec3 mean = vec3(0.0);\n for (int i = 0; i < 16; i++) {\n ivec2 p = clamp(base + ivec2(i & 3, i >> 2), ivec2(0), maxXY);\n int sy = (uFlipY != 0) ? (uSrcSize.y - 1 - p.y) : p.y;\n vec3 c = texelFetch(uSrc, ivec2(p.x, sy), 0).rgb;\n gPixels[i] = c;\n bbMin = min(bbMin, c);\n bbMax = max(bbMax, c);\n mean += c;\n }\n mean /= 16.0;\n\n // Seed endpoints from the block's principal colour axis (covariance\n // power-iteration, seeded with the bbox diagonal \u2014 mirrors bc1.wgsl). The\n // bbox diagonal is sign-blind: on anti-correlated channels (normal maps,\n // hue edges) it points across the data instead of along it, and the LSQ\n // refit below \u2014 which fits endpoints GIVEN the indices \u2014 can't recover.\n // Degenerate (near-flat) blocks keep the inset-bbox seed. Both seeds inset\n // by ~half an RGB565 cell (1/16) to tighten the quantised palette.\n vec3 c0v = vec3(0.0);\n vec3 c1v = vec3(0.0);\n vec3 c2v = vec3(0.0);\n for (int k = 0; k < 16; k++) {\n vec3 d = gPixels[k] - mean;\n c0v += d.x * d;\n c1v += d.y * d;\n c2v += d.z * d;\n }\n vec3 hi;\n vec3 lo;\n vec3 axis = bbMax - bbMin;\n float alen = length(axis);\n bool axisOk = alen > 1e-9;\n if (axisOk) {\n axis /= alen;\n for (int it = 0; it < 8; it++) {\n vec3 nv = vec3(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis));\n float nlen = length(nv);\n if (nlen < 1e-12) { axisOk = false; break; }\n axis = nv / nlen;\n }\n }\n if (axisOk) {\n float tMin = 1e30;\n float tMax = -1e30;\n for (int k = 0; k < 16; k++) {\n float t = dot(gPixels[k] - mean, axis);\n tMin = min(tMin, t);\n tMax = max(tMax, t);\n }\n float pad = (tMax - tMin) / 16.0;\n hi = clamp(mean + (tMax - pad) * axis, vec3(0.0), vec3(1.0));\n lo = clamp(mean + (tMin + pad) * axis, vec3(0.0), vec3(1.0));\n } else {\n vec3 inset = (bbMax - bbMin) / 16.0;\n hi = clamp(bbMax - inset, vec3(0.0), vec3(1.0));\n lo = clamp(bbMin + inset, vec3(0.0), vec3(1.0));\n }\n\n uint c0 = to565(hi);\n uint c1 = to565(lo);\n // 4-colour mode requires color0 > color1.\n if (c0 == c1) {\n if (c1 > 0u) { c1 = c1 - 1u; } else { c0 = c0 + 1u; }\n } else if (c0 < c1) {\n uint tmp = c0; c0 = c1; c1 = tmp;\n }\n\n // Seed assignment, then up to TWO least-squares refit rounds (mirroring\n // bc1.wgsl's fast path), each accepted only if the block's squared error\n // drops \u2014 the refit minimises a continuous objective and can lose after\n // 565 quantisation. Every assignment pass re-accumulates the sums, so an\n // accepted round seeds the next.\n Assign cur = assignStats(c0, c1, gIdx);\n for (int it = 0; it < 2; it++) {\n float det = cur.sAA * cur.sBB - cur.sAB * cur.sAB;\n if (abs(det) <= 1e-9) { break; }\n // Clamp the refit to the block bbox (not [0,1]): on multi-cluster blocks\n // the unconstrained LSQ solve extrapolates far outside the block's\n // colours and the per-channel clamp then bends the hue \u2014 fringe pixels\n // decode to colours that exist nowhere in the block. Constraining to the\n // bbox also measures better in plain SSE (+1.6 dB on the colour test\n // card), so the accept-if-better guard below keeps more refits.\n vec3 e0 = clamp((cur.sBB * cur.sAV - cur.sAB * cur.sBV) / det, bbMin, bbMax);\n vec3 e1 = clamp((cur.sAA * cur.sBV - cur.sAB * cur.sAV) / det, bbMin, bbMax);\n uint nc0 = to565(e0);\n uint nc1 = to565(e1);\n if (nc0 < nc1) { uint t = nc0; nc0 = nc1; nc1 = t; }\n if (nc0 == nc1 || (nc0 == c0 && nc1 == c1)) { break; }\n uint idx2[16];\n Assign nxt = assignStats(nc0, nc1, idx2);\n if (nxt.err >= cur.err) { break; }\n c0 = nc0; c1 = nc1;\n cur = nxt;\n for (int k = 0; k < 16; k++) gIdx[k] = idx2[k];\n }\n\n uint indices = 0u;\n for (int k = 0; k < 16; k++) indices |= (gIdx[k] & 3u) << (uint(k) * 2u);\n\n outColor = uvec4(c0 | (c1 << 16), indices, 0u, 0u);\n}\n";
2149
+ var bc1_frag_default = "#version 300 es\n// BC1 (DXT1) fragment-shader encoder \u2014 WebGL2 port of bc1.wgsl (fast path).\n//\n// One fragment per 4\xD74 block. Output is the 8-byte BC1 block as 2 \xD7 u32 in\n// outColor.rg (outColor.ba unused); the encoder reads back RGBA32UI and keeps\n// the low two words per block. Same algorithm as bc1.wgsl:\n// principal-axis endpoint seed (covariance\n// power-iteration; inset bbox on degenerate blocks), RGB565 quantisation,\n// forced 4-colour mode, full 4-entry L2 index search, then up to TWO\n// least-squares endpoint refit rounds, each accepted only when it lowers the\n// block's error. See bc1.wgsl for the full derivation.\n\nprecision highp float;\nprecision highp int;\n\nuniform sampler2D uSrc;\nuniform ivec2 uSrcSize; // original (unpadded) width, height\nuniform int uFlipY; // 1 = sample bottom-up (matches Three.js flipY)\n\nlayout(location = 0) out uvec4 outColor;\n\n// 4-colour-mode interpolation weights: pal[j] = WA[j]*c0 + WB[j]*c1.\nconst float WA[4] = float[4](1.0, 0.0, 2.0 / 3.0, 1.0 / 3.0);\nconst float WB[4] = float[4](0.0, 1.0, 1.0 / 3.0, 2.0 / 3.0);\n\nuint to565(vec3 c) {\n uint r = uint(clamp(floor(c.r * 31.0 + 0.5), 0.0, 31.0));\n uint g = uint(clamp(floor(c.g * 63.0 + 0.5), 0.0, 63.0));\n uint b = uint(clamp(floor(c.b * 31.0 + 0.5), 0.0, 31.0));\n return (r << 11) | (g << 5) | b;\n}\n\nvec3 from565(uint c) {\n float r = float((c >> 11) & 31u);\n float g = float((c >> 5) & 63u);\n float b = float(c & 31u);\n // 5/6-bit \u2192 8-bit. floor((x*527+23)/64) == (x<<3)|(x>>2): exact hardware\n // bit-replication (white \u2192 255), so index selection matches the GPU decode.\n float r8 = floor((r * 527.0 + 23.0) / 64.0);\n float g8 = floor((g * 259.0 + 33.0) / 64.0);\n float b8 = floor((b * 527.0 + 23.0) / 64.0);\n return vec3(r8, g8, b8) / 255.0;\n}\n\n// Per-invocation scratch (mirrors the WGSL function-scope arrays passed by\n// ptr; GLSL would copy array parameters by value).\nvec3 gPixels[16];\nuint gIdx[16];\n\n// Nearest-palette assignment of gPixels for the decoded palette of (c0,c1),\n// with the block's squared error and the LSQ normal-equation sums of the\n// resulting assignment accumulated in the same pass \u2014 so an accepted refit\n// can seed the next round.\nstruct Assign { float err; float sAA; float sBB; float sAB; vec3 sAV; vec3 sBV; };\nAssign assignStats(uint c0, uint c1, out uint indices[16]) {\n vec3 p0 = from565(c0);\n vec3 p1 = from565(c1);\n vec3 pal[4];\n for (int j = 0; j < 4; j++) pal[j] = WA[j] * p0 + WB[j] * p1;\n Assign r = Assign(0.0, 0.0, 0.0, 0.0, vec3(0.0), vec3(0.0));\n for (int k = 0; k < 16; k++) {\n vec3 c = gPixels[k];\n uint bestJ = 0u;\n float bestD = 1e30;\n for (int j = 0; j < 4; j++) {\n vec3 d = pal[j] - c;\n float d2 = dot(d, d);\n if (d2 < bestD) { bestD = d2; bestJ = uint(j); }\n }\n indices[k] = bestJ;\n r.err += bestD;\n float a = WA[int(bestJ)];\n float b = WB[int(bestJ)];\n r.sAA += a * a; r.sBB += b * b; r.sAB += a * b; r.sAV += a * c; r.sBV += b * c;\n }\n return r;\n}\n\nvoid main() {\n ivec2 base = ivec2(gl_FragCoord.xy) * 4;\n ivec2 maxXY = uSrcSize - ivec2(1);\n\n float gd = 0.0;\n vec3 bbMin = vec3(1.0);\n vec3 bbMax = vec3(0.0);\n vec3 mean = vec3(0.0);\n for (int i = 0; i < 16; i++) {\n ivec2 p = clamp(base + ivec2(i & 3, i >> 2), ivec2(0), maxXY);\n int sy = (uFlipY != 0) ? (uSrcSize.y - 1 - p.y) : p.y;\n vec3 c = texelFetch(uSrc, ivec2(p.x, sy), 0).rgb;\n gPixels[i] = c;\n bbMin = min(bbMin, c);\n gd = max(gd, max(abs(c.x - c.y), abs(c.x - c.z)));\n bbMax = max(bbMax, c);\n mean += c;\n }\n mean /= 16.0;\n\n // Seed endpoints from the block's principal colour axis (covariance\n // power-iteration, seeded with the bbox diagonal \u2014 mirrors bc1.wgsl). The\n // bbox diagonal is sign-blind: on anti-correlated channels (normal maps,\n // hue edges) it points across the data instead of along it, and the LSQ\n // refit below \u2014 which fits endpoints GIVEN the indices \u2014 can't recover.\n // Degenerate (near-flat) blocks keep the inset-bbox seed. Both seeds inset\n // by ~half an RGB565 cell (1/16) to tighten the quantised palette.\n vec3 c0v = vec3(0.0);\n vec3 c1v = vec3(0.0);\n vec3 c2v = vec3(0.0);\n for (int k = 0; k < 16; k++) {\n vec3 d = gPixels[k] - mean;\n c0v += d.x * d;\n c1v += d.y * d;\n c2v += d.z * d;\n }\n vec3 hi;\n vec3 lo;\n vec3 axis = bbMax - bbMin;\n float alen = length(axis);\n bool axisOk = alen > 1e-9;\n if (axisOk) {\n axis /= alen;\n for (int it = 0; it < 8; it++) {\n vec3 nv = vec3(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis));\n float nlen = length(nv);\n if (nlen < 1e-12) { axisOk = false; break; }\n axis = nv / nlen;\n }\n }\n if (axisOk) {\n float tMin = 1e30;\n float tMax = -1e30;\n for (int k = 0; k < 16; k++) {\n float t = dot(gPixels[k] - mean, axis);\n tMin = min(tMin, t);\n tMax = max(tMax, t);\n }\n float pad = (tMax - tMin) / 16.0;\n hi = clamp(mean + (tMax - pad) * axis, vec3(0.0), vec3(1.0));\n lo = clamp(mean + (tMin + pad) * axis, vec3(0.0), vec3(1.0));\n } else {\n vec3 inset = (bbMax - bbMin) / 16.0;\n hi = clamp(bbMax - inset, vec3(0.0), vec3(1.0));\n lo = clamp(bbMin + inset, vec3(0.0), vec3(1.0));\n }\n\n uint c0 = to565(hi);\n uint c1 = to565(lo);\n // 4-colour mode requires color0 > color1.\n if (c0 == c1) {\n if (c1 > 0u) { c1 = c1 - 1u; } else { c0 = c0 + 1u; }\n } else if (c0 < c1) {\n uint tmp = c0; c0 = c1; c1 = tmp;\n }\n\n // Seed assignment, then up to TWO least-squares refit rounds (mirroring\n // bc1.wgsl's fast path), each accepted only if the block's squared error\n // drops \u2014 the refit minimises a continuous objective and can lose after\n // 565 quantisation. Every assignment pass re-accumulates the sums, so an\n // accepted round seeds the next.\n // Exactly-gray blocks free the refit from the bbox clamp (no hue to\n // protect; smooth gradients want endpoints outside the data range) \u2014\n // see bc1_fast_f16.wgsl.\n vec3 limLo = gd == 0.0 ? vec3(0.0) : bbMin;\n vec3 limHi = gd == 0.0 ? vec3(1.0) : bbMax;\n Assign cur = assignStats(c0, c1, gIdx);\n for (int it = 0; it < 2; it++) {\n float det = cur.sAA * cur.sBB - cur.sAB * cur.sAB;\n if (abs(det) <= 1e-9) { break; }\n // Clamp the refit to the block bbox (not [0,1]): on multi-cluster blocks\n // the unconstrained LSQ solve extrapolates far outside the block's\n // colours and the per-channel clamp then bends the hue \u2014 fringe pixels\n // decode to colours that exist nowhere in the block. Constraining to the\n // bbox also measures better in plain SSE (+1.6 dB on the colour test\n // card), so the accept-if-better guard below keeps more refits.\n vec3 e0 = clamp((cur.sBB * cur.sAV - cur.sAB * cur.sBV) / det, limLo, limHi);\n vec3 e1 = clamp((cur.sAA * cur.sBV - cur.sAB * cur.sAV) / det, limLo, limHi);\n uint nc0 = to565(e0);\n uint nc1 = to565(e1);\n if (nc0 < nc1) { uint t = nc0; nc0 = nc1; nc1 = t; }\n if (nc0 == nc1 || (nc0 == c0 && nc1 == c1)) { break; }\n uint idx2[16];\n Assign nxt = assignStats(nc0, nc1, idx2);\n if (nxt.err >= cur.err) { break; }\n c0 = nc0; c1 = nc1;\n cur = nxt;\n for (int k = 0; k < 16; k++) gIdx[k] = idx2[k];\n }\n\n uint indices = 0u;\n for (int k = 0; k < 16; k++) indices |= (gIdx[k] & 3u) << (uint(k) * 2u);\n\n outColor = uvec4(c0 | (c1 << 16), indices, 0u, 0u);\n}\n";
1459
2150
 
1460
2151
  // src/webgl/BC1WebGLEncoder.ts
1461
2152
  var BC1WebGLEncoder = class extends WebGLBlockEncoder {
@@ -1474,7 +2165,7 @@ var BC1WebGLEncoder = class extends WebGLBlockEncoder {
1474
2165
  };
1475
2166
 
1476
2167
  // src/webgl/glsl/bc5.frag.glsl
1477
- var bc5_frag_default = "#version 300 es\n// BC5 (RGTC2) fragment-shader encoder \u2014 WebGL2 port of bc5.wgsl (fast path).\n//\n// One fragment per 4\xD74 block \u2192 16-byte BC5 block as 4 \xD7 u32 in outColor.\n// BC5 = two BC4 halves (R then G). This is the *fast* path only: bbox\n// endpoints + a full-L2 index assignment per channel with the least-squares\n// refit sums accumulated in the same pass, then one refit accepted only when\n// it lowers the block's error (mirrors bc5.wgsl's fast branch \u2014 worth\n// ~1.3 dB on the normal-map card). Always emits 6-interpolation mode\n// (red0 > red1). See bc5.wgsl for the full derivation.\n\nprecision highp float;\nprecision highp int;\n\nuniform sampler2D uSrc;\nuniform ivec2 uSrcSize;\nuniform int uFlipY;\n\nlayout(location = 0) out uvec4 outColor;\n\n// 6-interpolation-mode palette weights: pal[j] = W0_6[j]*r0 + W1_6[j]*r1.\nconst float W0_6[8] = float[8](1.0, 0.0, 6.0 / 7.0, 5.0 / 7.0, 4.0 / 7.0, 3.0 / 7.0, 2.0 / 7.0, 1.0 / 7.0);\nconst float W1_6[8] = float[8](0.0, 1.0, 1.0 / 7.0, 2.0 / 7.0, 3.0 / 7.0, 4.0 / 7.0, 5.0 / 7.0, 6.0 / 7.0);\n\nuint quantize8(float v) {\n return uint(clamp(floor(v * 255.0 + 0.5), 0.0, 255.0));\n}\n\n// Nearest-palette assignment with the LSQ normal-equation sums and total\n// squared error accumulated in the same pass. Sums are only consumed by the\n// caller's refit; err drives the accept-if-better test.\nstruct Assign { float err; float sAA; float sBB; float sAB; float sAV; float sBV; };\nAssign assignAll(float values[16], float pal[8], out uint indices[16]) {\n Assign r = Assign(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);\n for (int k = 0; k < 16; k++) {\n float v = values[k];\n uint bestJ = 0u;\n float bestD = 1e20;\n for (int j = 0; j < 8; j++) {\n float d = pal[j] - v;\n float d2 = d * d;\n if (d2 < bestD) { bestD = d2; bestJ = uint(j); }\n }\n indices[k] = bestJ;\n r.err += bestD;\n float a = W0_6[int(bestJ)];\n float b = W1_6[int(bestJ)];\n r.sAA += a * a; r.sBB += b * b; r.sAB += a * b; r.sAV += a * v; r.sBV += b * v;\n }\n return r;\n}\n\n// Encode 16 single-channel values into an 8-byte BC4 block (two little-endian\n// u32s). Mirrors encode_bc4() in bc5.wgsl's fast branch: bbox seed, fused\n// assignment + LSQ sums, refit accepted only if the error drops. vmin/vmax\n// are the channel's min/max, computed in the caller's load loop \u2014 fusing\n// that scan there saves a 16-value pass per channel.\nuvec2 encodeBC4(float values[16], float vmin, float vmax) {\n uint r0 = quantize8(vmax);\n uint r1 = quantize8(vmin);\n if (r0 == r1) {\n if (r1 > 0u) { r1 = r1 - 1u; } else { r0 = r0 + 1u; }\n }\n\n float pal[8];\n float r0f = float(r0) / 255.0;\n float r1f = float(r1) / 255.0;\n for (int j = 0; j < 8; j++) {\n pal[j] = W0_6[j] * r0f + W1_6[j] * r1f;\n }\n\n uint indices[16];\n Assign seed = assignAll(values, pal, indices);\n\n // One least-squares refit, accepted only if the requantised endpoints lower\n // the block error. Clamp to [0,1], NOT the block's value range: for a\n // scalar channel, endpoints beyond the data range are often genuinely\n // optimal and there is no colour axis to bend \u2014 the bbox clamp the colour\n // formats need costs ~0.3 dB here. Keep 6-interp mode (r0 > r1 strictly).\n float det = seed.sAA * seed.sBB - seed.sAB * seed.sAB;\n if (abs(det) > 1e-9) {\n float e0 = clamp((seed.sBB * seed.sAV - seed.sAB * seed.sBV) / det, 0.0, 1.0);\n float e1 = clamp((seed.sAA * seed.sBV - seed.sAB * seed.sAV) / det, 0.0, 1.0);\n uint n0 = quantize8(e0);\n uint n1 = quantize8(e1);\n if (n0 > n1 && !(n0 == r0 && n1 == r1)) {\n float pal2[8];\n float n0f = float(n0) / 255.0;\n float n1f = float(n1) / 255.0;\n for (int j = 0; j < 8; j++) {\n pal2[j] = W0_6[j] * n0f + W1_6[j] * n1f;\n }\n uint idx2[16];\n Assign refit = assignAll(values, pal2, idx2);\n if (refit.err < seed.err) {\n r0 = n0; r1 = n1;\n for (int k = 0; k < 16; k++) indices[k] = idx2[k];\n }\n }\n }\n\n // Pack the 48-bit index field (bytes 2..7) split across two u32 halves.\n uint idxLo = 0u;\n uint idxHi = 0u;\n for (int k = 0; k < 16; k++) {\n uint bit = 3u * uint(k);\n uint v = indices[k] & 7u;\n if (bit + 3u <= 32u) {\n idxLo = idxLo | (v << bit);\n } else if (bit >= 32u) {\n idxHi = idxHi | (v << (bit - 32u));\n } else {\n idxLo = idxLo | (v << bit);\n idxHi = idxHi | (v >> (32u - bit));\n }\n }\n\n uint outLo = r0 | (r1 << 8) | ((idxLo & 0xFFFFu) << 16);\n uint outHi = (idxLo >> 16) | (idxHi << 16);\n return uvec2(outLo, outHi);\n}\n\nvoid main() {\n ivec2 base = ivec2(gl_FragCoord.xy) * 4;\n ivec2 maxXY = uSrcSize - ivec2(1);\n\n float rValues[16];\n float gValues[16];\n float rMin = 1.0; float rMax = 0.0;\n float gMin = 1.0; float gMax = 0.0;\n for (int i = 0; i < 16; i++) {\n ivec2 p = clamp(base + ivec2(i & 3, i >> 2), ivec2(0), maxXY);\n int sy = (uFlipY != 0) ? (uSrcSize.y - 1 - p.y) : p.y;\n vec4 c = texelFetch(uSrc, ivec2(p.x, sy), 0);\n rValues[i] = c.r;\n gValues[i] = c.g;\n rMin = min(rMin, c.r); rMax = max(rMax, c.r);\n gMin = min(gMin, c.g); gMax = max(gMax, c.g);\n }\n\n uvec2 rBlock = encodeBC4(rValues, rMin, rMax);\n uvec2 gBlock = encodeBC4(gValues, gMin, gMax);\n outColor = uvec4(rBlock.x, rBlock.y, gBlock.x, gBlock.y);\n}\n";
2168
+ var bc5_frag_default = "#version 300 es\n// BC5 (RGTC2) fragment-shader encoder \u2014 WebGL2 port of bc5.wgsl (fast path).\n//\n// One fragment per 4\xD74 block \u2192 16-byte BC5 block as 4 \xD7 u32 in outColor.\n// BC5 = two BC4 halves (R then G). This is the *fast* path only: bbox\n// endpoints + a full-L2 index assignment per channel with the least-squares\n// refit sums accumulated in the same pass, then one refit accepted\n// CLOSED-FORM when it lowers the block's error on the seed indices \u2014 the\n// accepted endpoints ship with the seed indices, no second assignment pass\n// (mirrors bc5.wgsl; see bc5_fast_f16.wgsl for the measured trade). Always\n// emits 6-interpolation mode (red0 > red1).\n\nprecision highp float;\nprecision highp int;\n\nuniform sampler2D uSrc;\nuniform ivec2 uSrcSize;\nuniform int uFlipY;\n\nlayout(location = 0) out uvec4 outColor;\n\n// 6-interpolation-mode palette weights: pal[j] = W0_6[j]*r0 + W1_6[j]*r1.\nconst float W0_6[8] = float[8](1.0, 0.0, 6.0 / 7.0, 5.0 / 7.0, 4.0 / 7.0, 3.0 / 7.0, 2.0 / 7.0, 1.0 / 7.0);\nconst float W1_6[8] = float[8](0.0, 1.0, 1.0 / 7.0, 2.0 / 7.0, 3.0 / 7.0, 4.0 / 7.0, 5.0 / 7.0, 6.0 / 7.0);\n\nuint quantize8(float v) {\n return uint(clamp(floor(v * 255.0 + 0.5), 0.0, 255.0));\n}\n\n// Nearest-palette assignment with the LSQ normal-equation sums and total\n// squared error accumulated in the same pass. Sums are only consumed by the\n// caller's refit; err drives the accept-if-better test.\nstruct Assign { float err; float sAA; float sBB; float sAB; float sAV; float sBV; };\nAssign assignAll(float values[16], float pal[8], out uint indices[16]) {\n Assign r = Assign(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);\n for (int k = 0; k < 16; k++) {\n float v = values[k];\n uint bestJ = 0u;\n float bestD = 1e20;\n for (int j = 0; j < 8; j++) {\n float d = pal[j] - v;\n float d2 = d * d;\n if (d2 < bestD) { bestD = d2; bestJ = uint(j); }\n }\n indices[k] = bestJ;\n r.err += bestD;\n float a = W0_6[int(bestJ)];\n float b = W1_6[int(bestJ)];\n r.sAA += a * a; r.sBB += b * b; r.sAB += a * b; r.sAV += a * v; r.sBV += b * v;\n }\n return r;\n}\n\n// Encode 16 single-channel values into an 8-byte BC4 block (two little-endian\n// u32s). Mirrors encode_bc4() in bc5.wgsl's fast branch: bbox seed, fused\n// assignment + LSQ sums, refit accepted only if the error drops. vmin/vmax\n// are the channel's min/max, computed in the caller's load loop \u2014 fusing\n// that scan there saves a 16-value pass per channel.\nuvec2 encodeBC4(float values[16], float vmin, float vmax) {\n uint r0 = quantize8(vmax);\n uint r1 = quantize8(vmin);\n if (r0 == r1) {\n if (r1 > 0u) { r1 = r1 - 1u; } else { r0 = r0 + 1u; }\n }\n\n float pal[8];\n float r0f = float(r0) / 255.0;\n float r1f = float(r1) / 255.0;\n for (int j = 0; j < 8; j++) {\n pal[j] = W0_6[j] * r0f + W1_6[j] * r1f;\n }\n\n uint indices[16];\n Assign seed = assignAll(values, pal, indices);\n\n // One least-squares refit, accepted CLOSED-FORM: with the seed indices\n // kept, the block error for endpoints (e0, e1) is\n // E(e0, e1) = sAA\xB7e0\xB2 + sBB\xB7e1\xB2 + 2(sAB\xB7e0\xB7e1 \u2212 e0\xB7sAV \u2212 e1\xB7sBV) + \u03A3V\xB2\n // and \u03A3V\xB2 cancels out of the accept comparison, so no second assignment\n // pass is needed. Clamp to [0,1], NOT the block's value range: for a\n // scalar channel, endpoints beyond the data range are often genuinely\n // optimal and there is no colour axis to bend \u2014 the bbox clamp the colour\n // formats need costs ~0.3 dB here. Keep 6-interp mode (r0 > r1 strictly).\n float det = seed.sAA * seed.sBB - seed.sAB * seed.sAB;\n if (abs(det) > 1e-9) {\n float e0 = clamp((seed.sBB * seed.sAV - seed.sAB * seed.sBV) / det, 0.0, 1.0) * 255.0;\n float e1 = clamp((seed.sAA * seed.sBV - seed.sAB * seed.sAV) / det, 0.0, 1.0) * 255.0;\n // Price all four floor/ceil roundings of the fractional solve\n // closed-form (see bc5_fast_f16.wgsl); keep the best that stays in\n // 6-interp mode and beats the seed.\n float quadSeed = seed.sAA * r0f * r0f + seed.sBB * r1f * r1f\n + 2.0 * (seed.sAB * r0f * r1f - r0f * seed.sAV - r1f * seed.sBV);\n float bestQuad = quadSeed;\n uint seed0 = r0;\n uint seed1 = r1;\n for (uint m = 0u; m < 4u; m++) {\n float q0f = clamp(floor(e0) + float(m & 1u), 0.0, 255.0);\n float q1f = clamp(floor(e1) + float(m >> 1u), 0.0, 255.0);\n uint n0 = uint(q0f);\n uint n1 = uint(q1f);\n if (n0 > n1 && !(n0 == seed0 && n1 == seed1)) {\n float n0f = q0f / 255.0;\n float n1f = q1f / 255.0;\n float quadNew = seed.sAA * n0f * n0f + seed.sBB * n1f * n1f\n + 2.0 * (seed.sAB * n0f * n1f - n0f * seed.sAV - n1f * seed.sBV);\n if (quadNew < bestQuad) {\n bestQuad = quadNew;\n r0 = n0; r1 = n1;\n }\n }\n }\n }\n\n // Pack the 48-bit index field (bytes 2..7) split across two u32 halves.\n uint idxLo = 0u;\n uint idxHi = 0u;\n for (int k = 0; k < 16; k++) {\n uint bit = 3u * uint(k);\n uint v = indices[k] & 7u;\n if (bit + 3u <= 32u) {\n idxLo = idxLo | (v << bit);\n } else if (bit >= 32u) {\n idxHi = idxHi | (v << (bit - 32u));\n } else {\n idxLo = idxLo | (v << bit);\n idxHi = idxHi | (v >> (32u - bit));\n }\n }\n\n uint outLo = r0 | (r1 << 8) | ((idxLo & 0xFFFFu) << 16);\n uint outHi = (idxLo >> 16) | (idxHi << 16);\n return uvec2(outLo, outHi);\n}\n\nvoid main() {\n ivec2 base = ivec2(gl_FragCoord.xy) * 4;\n ivec2 maxXY = uSrcSize - ivec2(1);\n\n float rValues[16];\n float gValues[16];\n float rMin = 1.0; float rMax = 0.0;\n float gMin = 1.0; float gMax = 0.0;\n for (int i = 0; i < 16; i++) {\n ivec2 p = clamp(base + ivec2(i & 3, i >> 2), ivec2(0), maxXY);\n int sy = (uFlipY != 0) ? (uSrcSize.y - 1 - p.y) : p.y;\n vec4 c = texelFetch(uSrc, ivec2(p.x, sy), 0);\n rValues[i] = c.r;\n gValues[i] = c.g;\n rMin = min(rMin, c.r); rMax = max(rMax, c.r);\n gMin = min(gMin, c.g); gMax = max(gMax, c.g);\n }\n\n uvec2 rBlock = encodeBC4(rValues, rMin, rMax);\n uvec2 gBlock = encodeBC4(gValues, gMin, gMax);\n outColor = uvec4(rBlock.x, rBlock.y, gBlock.x, gBlock.y);\n}\n";
1478
2169
 
1479
2170
  // src/webgl/BC5WebGLEncoder.ts
1480
2171
  var BC5WebGLEncoder = class extends WebGLBlockEncoder {
@@ -1493,7 +2184,7 @@ var BC5WebGLEncoder = class extends WebGLBlockEncoder {
1493
2184
  };
1494
2185
 
1495
2186
  // src/webgl/glsl/bc7.frag.glsl
1496
- var bc7_frag_default = "#version 300 es\n// BC7 (BPTC) mode-6 fragment-shader encoder \u2014 WebGL2 port of bc7.wgsl (fast).\n//\n// One fragment per 4\xD74 block \u2192 16-byte block as 4 \xD7 u32 in outColor. Fast path\n// only: principal-axis seed (covariance power-iteration; bbox on degenerate\n// blocks) at the exact projection extents, quantised directly (no LSQ refit \u2014\n// mode 6's 16-level palette leaves it under 0.15 dB) \u2192 one projection-based\n// index-assignment pass (palette is colinear, so the nearest entry is found\n// by projecting onto the endpoint line \u2014 O(1) per pixel). Same algorithm as\n// bc7.wgsl; see that file for the mode-6 bit layout and rationale.\n//\n// Determinism note: the WGSL refit uses round() (half-to-even); here we use\n// floor(x + 0.5) for portability. The two differ only at exact .5 ties, a\n// sub-LSB endpoint nudge that is visually identical.\n\nprecision highp float;\nprecision highp int;\n\nuniform sampler2D uSrc;\nuniform ivec2 uSrcSize;\nuniform int uFlipY;\n\nlayout(location = 0) out uvec4 outColor;\n\n// Per-invocation scratch (mirrors the WGSL function-scope arrays passed by ptr).\nivec4 gPixels[16];\nuint gIdx[16];\n\nstruct QuantPair { ivec4 seven; ivec4 eight; };\nstruct Ep { ivec4 seven; ivec4 eight; uint p; };\nstruct Fit { ivec4 e0; ivec4 e1; bool valid; };\n\nivec4 to8(vec4 v) {\n return ivec4(clamp(floor(v * 255.0 + 0.5), vec4(0.0), vec4(255.0)));\n}\n\nint dist2(ivec4 a, ivec4 b) {\n ivec4 d = a - b;\n ivec4 e = d * d;\n return e.x + e.y + e.z + e.w;\n}\n\n// Quantize an 8-bit ideal endpoint to (7-bit value, reconstructed 8-bit) under\n// a fixed p-bit, all four channels at once.\nQuantPair quantizeEndpoint(ivec4 ideal8, uint p) {\n ivec4 q = ivec4(clamp(floor((vec4(ideal8) - float(p)) / 2.0 + 0.5), vec4(0.0), vec4(127.0)));\n // eff = (q << 1) | p. q*2 is even and p \u2208 {0,1}, so q*2 + p is identical and\n // avoids any vector-shift-by-scalar portability question.\n ivec4 eff = q * 2 + ivec4(int(p));\n return QuantPair(q, eff);\n}\n\n// Endpoint with its chosen p-bit, picked by minimum quantisation error.\nEp pickEp(ivec4 ideal) {\n QuantPair a = quantizeEndpoint(ideal, 0u);\n QuantPair b = quantizeEndpoint(ideal, 1u);\n if (dist2(b.eight, ideal) < dist2(a.eight, ideal)) {\n return Ep(b.seven, b.eight, 1u);\n }\n return Ep(a.seven, a.eight, 0u);\n}\n\n// Principal colour axis via power-iteration over precomputed, mean-corrected\n// covariance rows (the moments are accumulated for free in the pixel-load\n// loop), seeded with the bbox diagonal. Returns a unit axis, or vec4(0.0)\n// for a degenerate (constant) block. The bbox diagonal alone is sign-blind\n// and points across anti-correlated data (normal maps, hue edges) instead of\n// along it.\nvec4 principalAxis(vec4 c0v, vec4 c1v, vec4 c2v, vec4 c3v, vec4 seed) {\n vec4 v = seed;\n float len = length(v);\n if (len < 1e-9) { return vec4(0.0); }\n v /= len;\n for (int it = 0; it < 8; it++) {\n vec4 nv = vec4(dot(c0v, v), dot(c1v, v), dot(c2v, v), dot(c3v, v));\n len = length(nv);\n if (len < 1e-12) { return vec4(0.0); }\n v = nv / len;\n }\n return v;\n}\n\n// Projection index assignment over gPixels \u2192 gIdx. When `fit`, accumulate the\n// LSQ normal-equation sums in the same pass and return refitted endpoints.\nFit projAssign(ivec4 pe0, ivec4 pe1, bool fit) {\n Fit res;\n res.e0 = ivec4(0);\n res.e1 = ivec4(0);\n res.valid = false;\n ivec4 dir = pe1 - pe0;\n int dd = dir.x * dir.x + dir.y * dir.y + dir.z * dir.z + dir.w * dir.w;\n if (dd == 0) {\n for (int k = 0; k < 16; k++) { gIdx[k] = 0u; }\n return res;\n }\n float inv = 15.0 / float(dd);\n float sAA = 0.0, sBB = 0.0, sAB = 0.0;\n vec4 sAV = vec4(0.0), sBV = vec4(0.0);\n for (int k = 0; k < 16; k++) {\n ivec4 q = gPixels[k] - pe0;\n float proj = float(q.x * dir.x + q.y * dir.y + q.z * dir.z + q.w * dir.w) * inv;\n float s = clamp(floor(proj + 0.5), 0.0, 15.0);\n gIdx[k] = uint(s);\n if (fit) {\n vec4 v = vec4(gPixels[k]);\n float b = s / 15.0;\n float a = 1.0 - b;\n sAA += a * a; sBB += b * b; sAB += a * b; sAV += a * v; sBV += b * v;\n }\n }\n if (!fit) { return res; }\n float det = sAA * sBB - sAB * sAB;\n if (abs(det) < 1e-9) { return res; }\n res.e0 = ivec4(clamp(floor((sBB * sAV - sAB * sBV) / det + 0.5), vec4(0.0), vec4(255.0)));\n res.e1 = ivec4(clamp(floor((sAA * sBV - sAB * sAV) / det + 0.5), vec4(0.0), vec4(255.0)));\n res.valid = true;\n return res;\n}\n\nvoid writeBits(inout uint block[4], uint pos, uint nbits, uint value) {\n uint v = value & ((1u << nbits) - 1u);\n uint wordLo = pos / 32u;\n uint bitLo = pos % 32u;\n uint bitsInLo = min(nbits, 32u - bitLo);\n uint maskLo = ((1u << bitsInLo) - 1u) << bitLo;\n block[wordLo] = (block[wordLo] & ~maskLo) | ((v << bitLo) & maskLo);\n if (bitsInLo < nbits) {\n uint bitsInHi = nbits - bitsInLo;\n uint maskHi = (1u << bitsInHi) - 1u;\n uint valHi = v >> bitsInLo;\n block[wordLo + 1u] = (block[wordLo + 1u] & ~maskHi) | (valHi & maskHi);\n }\n}\n\nvoid main() {\n ivec2 base = ivec2(gl_FragCoord.xy) * 4;\n ivec2 maxXY = uSrcSize - ivec2(1);\n\n // Load pass with the covariance moments FUSED in: d = px \u2212 pixel0\n // (first-pixel-relative, so the sums scale with the block's span).\n ivec4 lo = ivec4(255);\n ivec4 hi = ivec4(0);\n vec4 p0f = vec4(0.0);\n vec4 sd = vec4(0.0);\n vec4 c0v = vec4(0.0);\n vec4 c1v = vec4(0.0);\n vec4 c2v = vec4(0.0);\n vec4 c3v = vec4(0.0);\n for (int i = 0; i < 16; i++) {\n ivec2 p = clamp(base + ivec2(i & 3, i >> 2), ivec2(0), maxXY);\n int sy = (uFlipY != 0) ? (uSrcSize.y - 1 - p.y) : p.y;\n ivec4 px = to8(texelFetch(uSrc, ivec2(p.x, sy), 0));\n gPixels[i] = px;\n lo = min(lo, px);\n hi = max(hi, px);\n if (i == 0) { p0f = vec4(px); }\n vec4 d = vec4(px) - p0f;\n sd += d;\n c0v += d.x * d;\n c1v += d.y * d;\n c2v += d.z * d;\n c3v += d.w * d;\n }\n vec4 mean = p0f + sd / 16.0;\n // Mean-correct the fused moments: C = \u03A3dd\u1D40 \u2212 (\u03A3d)(\u03A3d)\u1D40/16.\n vec4 sd16 = sd / 16.0;\n c0v -= sd.x * sd16;\n c1v -= sd.y * sd16;\n c2v -= sd.z * sd16;\n c3v -= sd.w * sd16;\n\n ivec4 seed0 = lo;\n ivec4 seed1 = hi;\n vec4 axis = principalAxis(c0v, c1v, c2v, c3v, vec4(hi - lo));\n if (dot(axis, axis) > 0.0) {\n // Exact projection extents along the axis. (A Rayleigh-quotient span\n // estimate was tried in place of this pass \u2014 it saves 16 dots but costs\n // 0.1\u20130.8 dB and 4\u201310\xD7 on the worst-easy-block gate: \u03C3 misjudges\n // two-cluster and outlier blocks. The pass stays.)\n float tMin = 1e30;\n float tMax = -1e30;\n for (int k = 0; k < 16; k++) {\n float t = dot(vec4(gPixels[k]) - mean, axis);\n tMin = min(tMin, t);\n tMax = max(tMax, t);\n }\n seed0 = ivec4(clamp(floor(mean + tMin * axis + 0.5), vec4(0.0), vec4(255.0)));\n seed1 = ivec4(clamp(floor(mean + tMax * axis + 0.5), vec4(0.0), vec4(255.0)));\n }\n\n // Quantise the PCA-extents seed directly and assign indices in one\n // projection pass \u2014 no LSQ refit: with the seed already on the principal\n // axis, mode 6's 16-level palette leaves the refit under 0.15 dB (the\n // coarse 4-level BC1/ASTC fast paths DO keep theirs).\n Ep ep0 = pickEp(seed0);\n Ep ep1 = pickEp(seed1);\n projAssign(ep0.eight, ep1.eight, false);\n ivec4 e0_7 = ep0.seven;\n ivec4 e1_7 = ep1.seven;\n uint p0 = ep0.p;\n uint p1 = ep1.p;\n\n // Anchor rule \u2014 pixel 0's index MSB must be 0; otherwise swap endpoints and\n // reflect every index (decoded image unchanged).\n if ((gIdx[0] & 0x8u) != 0u) {\n ivec4 t = e0_7; e0_7 = e1_7; e1_7 = t;\n uint tp = p0; p0 = p1; p1 = tp;\n for (int k = 0; k < 16; k++) { gIdx[k] = 15u - gIdx[k]; }\n }\n\n uint block[4];\n block[0] = 0u; block[1] = 0u; block[2] = 0u; block[3] = 0u;\n uint pos = 0u;\n writeBits(block, pos, 7u, 0x40u); pos += 7u;\n writeBits(block, pos, 7u, uint(e0_7.x)); pos += 7u;\n writeBits(block, pos, 7u, uint(e1_7.x)); pos += 7u;\n writeBits(block, pos, 7u, uint(e0_7.y)); pos += 7u;\n writeBits(block, pos, 7u, uint(e1_7.y)); pos += 7u;\n writeBits(block, pos, 7u, uint(e0_7.z)); pos += 7u;\n writeBits(block, pos, 7u, uint(e1_7.z)); pos += 7u;\n writeBits(block, pos, 7u, uint(e0_7.w)); pos += 7u;\n writeBits(block, pos, 7u, uint(e1_7.w)); pos += 7u;\n writeBits(block, pos, 1u, p0); pos += 1u;\n writeBits(block, pos, 1u, p1); pos += 1u;\n writeBits(block, pos, 3u, gIdx[0] & 0x7u); pos += 3u;\n for (int k = 1; k < 16; k++) {\n writeBits(block, pos, 4u, gIdx[k] & 0xFu);\n pos += 4u;\n }\n\n outColor = uvec4(block[0], block[1], block[2], block[3]);\n}\n";
2187
+ var bc7_frag_default = "#version 300 es\n// BC7 (BPTC) mode-6 fragment-shader encoder \u2014 WebGL2 port of bc7.wgsl (fast).\n//\n// One fragment per 4\xD74 block \u2192 16-byte block as 4 \xD7 u32 in outColor. Fast path\n// only: principal-axis seed (covariance power-iteration; bbox on degenerate\n// blocks) at the exact projection extents, quantised directly (no LSQ refit \u2014\n// mode 6's 16-level palette leaves it under 0.15 dB) \u2192 one projection-based\n// index-assignment pass (palette is colinear, so the nearest entry is found\n// by projecting onto the endpoint line \u2014 O(1) per pixel). Same algorithm as\n// bc7.wgsl; see that file for the mode-6 bit layout and rationale.\n//\n// Determinism note: the WGSL refit uses round() (half-to-even); here we use\n// floor(x + 0.5) for portability. The two differ only at exact .5 ties, a\n// sub-LSB endpoint nudge that is visually identical.\n\nprecision highp float;\nprecision highp int;\n\nuniform sampler2D uSrc;\nuniform ivec2 uSrcSize;\nuniform int uFlipY;\n\nlayout(location = 0) out uvec4 outColor;\n\n// Per-invocation scratch (mirrors the WGSL function-scope arrays passed by ptr).\nivec4 gPixels[16];\nuint gIdx[16];\n\nstruct QuantPair { ivec4 seven; ivec4 eight; };\nstruct Ep { ivec4 seven; ivec4 eight; uint p; };\nstruct Fit { ivec4 e0; ivec4 e1; bool valid; };\n\nivec4 to8(vec4 v) {\n return ivec4(clamp(floor(v * 255.0 + 0.5), vec4(0.0), vec4(255.0)));\n}\n\nint dist2(ivec4 a, ivec4 b) {\n ivec4 d = a - b;\n ivec4 e = d * d;\n return e.x + e.y + e.z + e.w;\n}\n\n// Quantize an 8-bit ideal endpoint to (7-bit value, reconstructed 8-bit) under\n// a fixed p-bit, all four channels at once.\nQuantPair quantizeEndpoint(ivec4 ideal8, uint p) {\n ivec4 q = ivec4(clamp(floor((vec4(ideal8) - float(p)) / 2.0 + 0.5), vec4(0.0), vec4(127.0)));\n // eff = (q << 1) | p. q*2 is even and p \u2208 {0,1}, so q*2 + p is identical and\n // avoids any vector-shift-by-scalar portability question.\n ivec4 eff = q * 2 + ivec4(int(p));\n return QuantPair(q, eff);\n}\n\n// Endpoint with its chosen p-bit, picked by minimum quantisation error.\nEp pickEp(ivec4 ideal) {\n QuantPair a = quantizeEndpoint(ideal, 0u);\n QuantPair b = quantizeEndpoint(ideal, 1u);\n if (dist2(b.eight, ideal) < dist2(a.eight, ideal)) {\n return Ep(b.seven, b.eight, 1u);\n }\n return Ep(a.seven, a.eight, 0u);\n}\n\n// Principal colour axis via power-iteration over precomputed, mean-corrected\n// covariance rows (the moments are accumulated for free in the pixel-load\n// loop), seeded with the bbox diagonal. Returns a unit axis, or vec4(0.0)\n// for a degenerate (constant) block. The bbox diagonal alone is sign-blind\n// and points across anti-correlated data (normal maps, hue edges) instead of\n// along it.\nvec4 principalAxis(vec4 c0v, vec4 c1v, vec4 c2v, vec4 c3v, vec4 seed) {\n vec4 v = seed;\n float len = length(v);\n if (len < 1e-9) { return vec4(0.0); }\n v /= len;\n for (int it = 0; it < 8; it++) {\n vec4 nv = vec4(dot(c0v, v), dot(c1v, v), dot(c2v, v), dot(c3v, v));\n len = length(nv);\n if (len < 1e-12) { return vec4(0.0); }\n v = nv / len;\n }\n return v;\n}\n\n// Projection index assignment over gPixels \u2192 gIdx. When `fit`, accumulate the\n// LSQ normal-equation sums in the same pass and return refitted endpoints.\nFit projAssign(ivec4 pe0, ivec4 pe1, bool fit) {\n Fit res;\n res.e0 = ivec4(0);\n res.e1 = ivec4(0);\n res.valid = false;\n ivec4 dir = pe1 - pe0;\n int dd = dir.x * dir.x + dir.y * dir.y + dir.z * dir.z + dir.w * dir.w;\n if (dd == 0) {\n for (int k = 0; k < 16; k++) { gIdx[k] = 0u; }\n return res;\n }\n float inv = 15.0 / float(dd);\n float sAA = 0.0, sBB = 0.0, sAB = 0.0;\n vec4 sAV = vec4(0.0), sBV = vec4(0.0);\n for (int k = 0; k < 16; k++) {\n ivec4 q = gPixels[k] - pe0;\n float proj = float(q.x * dir.x + q.y * dir.y + q.z * dir.z + q.w * dir.w) * inv;\n float s = clamp(floor(proj + 0.5), 0.0, 15.0);\n gIdx[k] = uint(s);\n if (fit) {\n vec4 v = vec4(gPixels[k]);\n float b = s / 15.0;\n float a = 1.0 - b;\n sAA += a * a; sBB += b * b; sAB += a * b; sAV += a * v; sBV += b * v;\n }\n }\n if (!fit) { return res; }\n float det = sAA * sBB - sAB * sAB;\n if (abs(det) < 1e-9) { return res; }\n res.e0 = ivec4(clamp(floor((sBB * sAV - sAB * sBV) / det + 0.5), vec4(0.0), vec4(255.0)));\n res.e1 = ivec4(clamp(floor((sAA * sBV - sAB * sAV) / det + 0.5), vec4(0.0), vec4(255.0)));\n res.valid = true;\n return res;\n}\n\nvoid writeBits(inout uint block[4], uint pos, uint nbits, uint value) {\n uint v = value & ((1u << nbits) - 1u);\n uint wordLo = pos / 32u;\n uint bitLo = pos % 32u;\n uint bitsInLo = min(nbits, 32u - bitLo);\n uint maskLo = ((1u << bitsInLo) - 1u) << bitLo;\n block[wordLo] = (block[wordLo] & ~maskLo) | ((v << bitLo) & maskLo);\n if (bitsInLo < nbits) {\n uint bitsInHi = nbits - bitsInLo;\n uint maskHi = (1u << bitsInHi) - 1u;\n uint valHi = v >> bitsInLo;\n block[wordLo + 1u] = (block[wordLo + 1u] & ~maskHi) | (valHi & maskHi);\n }\n}\n\nvoid main() {\n ivec2 base = ivec2(gl_FragCoord.xy) * 4;\n ivec2 maxXY = uSrcSize - ivec2(1);\n\n // Load pass with the covariance moments FUSED in: d = px \u2212 pixel0\n // (first-pixel-relative, so the sums scale with the block's span).\n ivec4 lo = ivec4(255);\n ivec4 hi = ivec4(0);\n vec4 p0f = vec4(0.0);\n vec4 sd = vec4(0.0);\n vec4 c0v = vec4(0.0);\n vec4 c1v = vec4(0.0);\n vec4 c2v = vec4(0.0);\n vec4 c3v = vec4(0.0);\n int gd = 0;\n for (int i = 0; i < 16; i++) {\n ivec2 p = clamp(base + ivec2(i & 3, i >> 2), ivec2(0), maxXY);\n int sy = (uFlipY != 0) ? (uSrcSize.y - 1 - p.y) : p.y;\n ivec4 px = to8(texelFetch(uSrc, ivec2(p.x, sy), 0));\n gPixels[i] = px;\n lo = min(lo, px);\n hi = max(hi, px);\n gd = max(gd, max(abs(px.x - px.y), abs(px.x - px.z)));\n if (i == 0) { p0f = vec4(px); }\n vec4 d = vec4(px) - p0f;\n sd += d;\n c0v += d.x * d;\n c1v += d.y * d;\n c2v += d.z * d;\n c3v += d.w * d;\n }\n vec4 mean = p0f + sd / 16.0;\n // Mean-correct the fused moments: C = \u03A3dd\u1D40 \u2212 (\u03A3d)(\u03A3d)\u1D40/16.\n vec4 sd16 = sd / 16.0;\n c0v -= sd.x * sd16;\n c1v -= sd.y * sd16;\n c2v -= sd.z * sd16;\n c3v -= sd.w * sd16;\n\n ivec4 seed0 = lo;\n ivec4 seed1 = hi;\n // Gray + opaque blocks: axis is analytically (1,1,1,0)/\u221A3 with extents\n // at the luma min/max \u2014 skip iteration + extents (see bc7_fast_f16.wgsl).\n if (lo.w == 255 && gd == 0) {\n seed0 = ivec4(lo.x, lo.x, lo.x, 255);\n seed1 = ivec4(hi.x, hi.x, hi.x, 255);\n } else {\n vec4 axis = principalAxis(c0v, c1v, c2v, c3v, vec4(hi - lo));\n if (dot(axis, axis) > 0.0) {\n // Exact projection extents along the axis. (A Rayleigh-quotient span\n // estimate was tried in place of this pass \u2014 it saves 16 dots but costs\n // 0.1\u20130.8 dB and 4\u201310\xD7 on the worst-easy-block gate: \u03C3 misjudges\n // two-cluster and outlier blocks. The pass stays.)\n float tMin = 1e30;\n float tMax = -1e30;\n for (int k = 0; k < 16; k++) {\n float t = dot(vec4(gPixels[k]) - mean, axis);\n tMin = min(tMin, t);\n tMax = max(tMax, t);\n }\n seed0 = ivec4(clamp(floor(mean + tMin * axis + 0.5), vec4(0.0), vec4(255.0)));\n seed1 = ivec4(clamp(floor(mean + tMax * axis + 0.5), vec4(0.0), vec4(255.0)));\n }\n }\n\n // Quantise the PCA-extents seed directly and assign indices in one\n // projection pass \u2014 no LSQ refit: with the seed already on the principal\n // axis, mode 6's 16-level palette leaves the refit under 0.15 dB (the\n // coarse 4-level BC1/ASTC fast paths DO keep theirs).\n Ep ep0 = pickEp(seed0);\n Ep ep1 = pickEp(seed1);\n projAssign(ep0.eight, ep1.eight, false);\n ivec4 e0_7 = ep0.seven;\n ivec4 e1_7 = ep1.seven;\n uint p0 = ep0.p;\n uint p1 = ep1.p;\n\n // Anchor rule \u2014 pixel 0's index MSB must be 0; otherwise swap endpoints and\n // reflect every index (decoded image unchanged).\n if ((gIdx[0] & 0x8u) != 0u) {\n ivec4 t = e0_7; e0_7 = e1_7; e1_7 = t;\n uint tp = p0; p0 = p1; p1 = tp;\n for (int k = 0; k < 16; k++) { gIdx[k] = 15u - gIdx[k]; }\n }\n\n uint block[4];\n block[0] = 0u; block[1] = 0u; block[2] = 0u; block[3] = 0u;\n uint pos = 0u;\n writeBits(block, pos, 7u, 0x40u); pos += 7u;\n writeBits(block, pos, 7u, uint(e0_7.x)); pos += 7u;\n writeBits(block, pos, 7u, uint(e1_7.x)); pos += 7u;\n writeBits(block, pos, 7u, uint(e0_7.y)); pos += 7u;\n writeBits(block, pos, 7u, uint(e1_7.y)); pos += 7u;\n writeBits(block, pos, 7u, uint(e0_7.z)); pos += 7u;\n writeBits(block, pos, 7u, uint(e1_7.z)); pos += 7u;\n writeBits(block, pos, 7u, uint(e0_7.w)); pos += 7u;\n writeBits(block, pos, 7u, uint(e1_7.w)); pos += 7u;\n writeBits(block, pos, 1u, p0); pos += 1u;\n writeBits(block, pos, 1u, p1); pos += 1u;\n writeBits(block, pos, 3u, gIdx[0] & 0x7u); pos += 3u;\n for (int k = 1; k < 16; k++) {\n writeBits(block, pos, 4u, gIdx[k] & 0xFu);\n pos += 4u;\n }\n\n outColor = uvec4(block[0], block[1], block[2], block[3]);\n}\n";
1497
2188
 
1498
2189
  // src/webgl/BC7WebGLEncoder.ts
1499
2190
  var BC7WebGLEncoder = class extends WebGLBlockEncoder {
@@ -1512,7 +2203,7 @@ var BC7WebGLEncoder = class extends WebGLBlockEncoder {
1512
2203
  };
1513
2204
 
1514
2205
  // src/webgl/glsl/astc4x4.frag.glsl
1515
- var astc4x4_frag_default = "#version 300 es\n// ASTC 4\xD74 LDR fragment-shader encoder \u2014 WebGL2 port of astc4x4.wgsl (fast).\n//\n// One fragment per 4\xD74 block \u2192 16-byte block as 4 \xD7 u32 in outColor. Restricted\n// subset: single partition, no dual-plane, CEM 12 (LDR RGBA direct), 4\xD74 weight\n// grid with 2-bit weights (QUANT_4), 8-bit endpoints (QUANT_256). Fast path:\n// principal-axis seed (covariance power-iteration; bbox on degenerate blocks)\n// \u2192 one LSQ refit fused into a projection weight assignment (4 colinear\n// levels). Mirrors astc4x4.wgsl; see that file for the 128-bit block layout.\n//\n// Determinism note: floor(x + 0.5) replaces WGSL round() for the refit endpoints\n// (sub-LSB difference at exact .5 ties only).\n\nprecision highp float;\nprecision highp int;\n\nuniform sampler2D uSrc;\nuniform ivec2 uSrcSize;\nuniform int uFlipY;\n\nlayout(location = 0) out uvec4 outColor;\n\nivec4 gPixels[16];\nuint gIdx[16];\n\nstruct Fit { ivec4 e0; ivec4 e1; bool valid; };\n\nivec4 to8(vec4 v) {\n return ivec4(clamp(floor(v * 255.0 + 0.5), vec4(0.0), vec4(255.0)));\n}\n\n// Principal colour axis of gPixels via covariance power-iteration, seeded\n// with the bbox diagonal. Returns a unit axis, or vec4(0.0) for a degenerate\n// (constant) block. The bbox diagonal alone is sign-blind and points across\n// anti-correlated data (normal maps, hue edges) instead of along it.\nvec4 principalAxis(vec4 mean, vec4 seed) {\n vec4 c0v = vec4(0.0);\n vec4 c1v = vec4(0.0);\n vec4 c2v = vec4(0.0);\n vec4 c3v = vec4(0.0);\n for (int k = 0; k < 16; k++) {\n vec4 d = vec4(gPixels[k]) - mean;\n c0v += d.x * d;\n c1v += d.y * d;\n c2v += d.z * d;\n c3v += d.w * d;\n }\n vec4 v = seed;\n float len = length(v);\n if (len < 1e-9) { return vec4(0.0); }\n v /= len;\n for (int it = 0; it < 8; it++) {\n vec4 nv = vec4(dot(c0v, v), dot(c1v, v), dot(c2v, v), dot(c3v, v));\n len = length(nv);\n if (len < 1e-12) { return vec4(0.0); }\n v = nv / len;\n }\n return v;\n}\n\n// Projection weight assignment over 4 levels (QUANT_4 \u2248 thirds), with the LSQ\n// normal-equation sums accumulated in the same pass for a fused refit.\nFit projAssign(ivec4 pe0, ivec4 pe1, bool fit) {\n Fit res;\n res.e0 = ivec4(0);\n res.e1 = ivec4(0);\n res.valid = false;\n ivec4 dir = pe1 - pe0;\n int dd = dir.x * dir.x + dir.y * dir.y + dir.z * dir.z + dir.w * dir.w;\n if (dd == 0) {\n for (int k = 0; k < 16; k++) { gIdx[k] = 0u; }\n return res;\n }\n float inv = 3.0 / float(dd);\n float sAA = 0.0, sBB = 0.0, sAB = 0.0;\n vec4 sAV = vec4(0.0), sBV = vec4(0.0);\n for (int k = 0; k < 16; k++) {\n ivec4 q = gPixels[k] - pe0;\n float proj = float(q.x * dir.x + q.y * dir.y + q.z * dir.z + q.w * dir.w) * inv;\n float s = clamp(floor(proj + 0.5), 0.0, 3.0);\n gIdx[k] = uint(s);\n if (fit) {\n vec4 v = vec4(gPixels[k]);\n float b = s / 3.0;\n float a = 1.0 - b;\n sAA += a * a; sBB += b * b; sAB += a * b; sAV += a * v; sBV += b * v;\n }\n }\n if (!fit) { return res; }\n float det = sAA * sBB - sAB * sAB;\n if (abs(det) < 1e-9) { return res; }\n res.e0 = ivec4(clamp(floor((sBB * sAV - sAB * sBV) / det + 0.5), vec4(0.0), vec4(255.0)));\n res.e1 = ivec4(clamp(floor((sAA * sBV - sAB * sAV) / det + 0.5), vec4(0.0), vec4(255.0)));\n res.valid = true;\n return res;\n}\n\nvoid writeBits(inout uint block[4], uint pos, uint nbits, uint value) {\n uint v = value & ((1u << nbits) - 1u);\n uint wordLo = pos / 32u;\n uint bitLo = pos % 32u;\n uint bitsInLo = min(nbits, 32u - bitLo);\n uint maskLo = ((1u << bitsInLo) - 1u) << bitLo;\n block[wordLo] = (block[wordLo] & ~maskLo) | ((v << bitLo) & maskLo);\n if (bitsInLo < nbits) {\n uint bitsInHi = nbits - bitsInLo;\n uint maskHi = (1u << bitsInHi) - 1u;\n uint valHi = v >> bitsInLo;\n block[wordLo + 1u] = (block[wordLo + 1u] & ~maskHi) | (valHi & maskHi);\n }\n}\n\nvoid main() {\n ivec2 base = ivec2(gl_FragCoord.xy) * 4;\n ivec2 maxXY = uSrcSize - ivec2(1);\n\n ivec4 lo = ivec4(255);\n ivec4 hi = ivec4(0);\n ivec4 isum = ivec4(0);\n for (int i = 0; i < 16; i++) {\n ivec2 p = clamp(base + ivec2(i & 3, i >> 2), ivec2(0), maxXY);\n int sy = (uFlipY != 0) ? (uSrcSize.y - 1 - p.y) : p.y;\n ivec4 px = to8(texelFetch(uSrc, ivec2(p.x, sy), 0));\n gPixels[i] = px;\n lo = min(lo, px);\n hi = max(hi, px);\n isum += px;\n }\n vec4 mean = vec4(isum) / 16.0;\n\n ivec4 e0 = lo;\n ivec4 e1 = hi;\n vec4 axis = principalAxis(mean, vec4(hi - lo));\n if (dot(axis, axis) > 0.0) {\n float tMin = 1e30;\n float tMax = -1e30;\n for (int k = 0; k < 16; k++) {\n float t = dot(vec4(gPixels[k]) - mean, axis);\n tMin = min(tMin, t);\n tMax = max(tMax, t);\n }\n e0 = ivec4(clamp(floor(mean + tMin * axis + 0.5), vec4(0.0), vec4(255.0)));\n e1 = ivec4(clamp(floor(mean + tMax * axis + 0.5), vec4(0.0), vec4(255.0)));\n }\n Fit r = projAssign(e0, e1, true);\n if (r.valid) {\n // Clamp the refit to the block bbox: on multi-cluster blocks the\n // unconstrained LSQ solve extrapolates far outside the block's colours and\n // the per-channel [0,255] clamp then bends the hue \u2014 fringe pixels decode\n // to colours that exist nowhere in the block. Constraining to the bbox\n // also measures better in plain SSE (+1.8 dB on the colour test card).\n e0 = clamp(r.e0, lo, hi);\n e1 = clamp(r.e1, lo, hi);\n projAssign(e0, e1, false);\n }\n\n // Endpoint ordering so the decoder doesn't apply blue contraction.\n if (e0.x + e0.y + e0.z > e1.x + e1.y + e1.z) {\n ivec4 t = e0; e0 = e1; e1 = t;\n for (int k = 0; k < 16; k++) { gIdx[k] = 3u - gIdx[k]; }\n }\n\n uint block[4];\n block[0] = 0u; block[1] = 0u; block[2] = 0u; block[3] = 0u;\n writeBits(block, 0u, 11u, 0x042u);\n writeBits(block, 11u, 2u, 0u);\n writeBits(block, 13u, 4u, 12u);\n writeBits(block, 17u + 0u * 8u, 8u, uint(e0.x));\n writeBits(block, 17u + 1u * 8u, 8u, uint(e1.x));\n writeBits(block, 17u + 2u * 8u, 8u, uint(e0.y));\n writeBits(block, 17u + 3u * 8u, 8u, uint(e1.y));\n writeBits(block, 17u + 4u * 8u, 8u, uint(e0.z));\n writeBits(block, 17u + 5u * 8u, 8u, uint(e1.z));\n writeBits(block, 17u + 6u * 8u, 8u, uint(e0.w));\n writeBits(block, 17u + 7u * 8u, 8u, uint(e1.w));\n\n uint w3 = 0u;\n for (int k = 0; k < 16; k++) {\n uint w = gIdx[k] & 0x3u;\n w3 = w3 | ((w & 1u) << (31u - 2u * uint(k))) | (((w >> 1u) & 1u) << (30u - 2u * uint(k)));\n }\n block[3] = w3;\n\n outColor = uvec4(block[0], block[1], block[2], block[3]);\n}\n";
2206
+ var astc4x4_frag_default = "#version 300 es\n// ASTC 4\xD74 LDR fragment-shader encoder \u2014 WebGL2 port of astc4x4.wgsl (fast).\n//\n// One fragment per 4\xD74 block \u2192 16-byte block as 4 \xD7 u32 in outColor.\n// Restricted subset: single partition, no dual-plane, 8-bit endpoints, with\n// the block class picked per block from the content (see astc4x4_ref.ts for\n// layouts and block-mode derivations):\n// gray + opaque \u2192 CEM 0 (luminance), 5-bit weights, mode 0x253\n// opaque \u2192 CEM 8 (RGB), 3-bit weights, mode 0x053\n// translucent \u2192 CEM 12 (RGBA), 2-bit weights, mode 0x042\n// Colour paths: principal-axis seed (covariance power-iteration; bbox on\n// degenerate blocks) \u2192 one LSQ refit fused into a projection weight\n// assignment. Mirrors astc4x4.wgsl.\n//\n// Determinism note: floor(x + 0.5) replaces WGSL round() for the refit endpoints\n// (sub-LSB difference at exact .5 ties only).\n\nprecision highp float;\nprecision highp int;\n\nuniform sampler2D uSrc;\nuniform ivec2 uSrcSize;\nuniform int uFlipY;\n\nlayout(location = 0) out uvec4 outColor;\n\nivec4 gPixels[16];\nuint gIdx[16];\n\nstruct Fit { ivec4 e0; ivec4 e1; bool valid; };\n\nivec4 to8(vec4 v) {\n return ivec4(clamp(floor(v * 255.0 + 0.5), vec4(0.0), vec4(255.0)));\n}\n\n// GLSL ES 3.00 has no bitfieldReverse; classic 5-step swap. Weight-stream\n// bit q lives at block bit 127 \u2212 q, so a stream word assembled LSB-first\n// maps onto a block word with one reversal (see astc4x4.wgsl).\nuint rev32(uint x) {\n uint v = x;\n v = ((v & 0x55555555u) << 1) | ((v >> 1) & 0x55555555u);\n v = ((v & 0x33333333u) << 2) | ((v >> 2) & 0x33333333u);\n v = ((v & 0x0F0F0F0Fu) << 4) | ((v >> 4) & 0x0F0F0F0Fu);\n v = ((v & 0x00FF00FFu) << 8) | ((v >> 8) & 0x00FF00FFu);\n return (v << 16) | (v >> 16);\n}\n\n// Principal colour axis of gPixels via covariance power-iteration, seeded\n// with the bbox diagonal. Returns a unit axis, or vec4(0.0) for a degenerate\n// (constant) block. The bbox diagonal alone is sign-blind and points across\n// anti-correlated data (normal maps, hue edges) instead of along it.\nvec4 principalAxis(vec4 mean, vec4 seed, int iters) {\n vec4 c0v = vec4(0.0);\n vec4 c1v = vec4(0.0);\n vec4 c2v = vec4(0.0);\n vec4 c3v = vec4(0.0);\n for (int k = 0; k < 16; k++) {\n vec4 d = vec4(gPixels[k]) - mean;\n c0v += d.x * d;\n c1v += d.y * d;\n c2v += d.z * d;\n c3v += d.w * d;\n }\n vec4 v = seed;\n float len = length(v);\n if (len < 1e-9) { return vec4(0.0); }\n v /= len;\n for (int it = 0; it < iters; it++) {\n vec4 nv = vec4(dot(c0v, v), dot(c1v, v), dot(c2v, v), dot(c3v, v));\n len = length(nv);\n if (len < 1e-12) { return vec4(0.0); }\n v = nv / len;\n }\n return v;\n}\n\n// Projection weight assignment over lmax + 1 colinear levels, with the LSQ\n// normal-equation sums accumulated in the same pass for a fused refit.\nFit projAssign(ivec4 pe0, ivec4 pe1, float lmax, bool fit) {\n Fit res;\n res.e0 = ivec4(0);\n res.e1 = ivec4(0);\n res.valid = false;\n ivec4 dir = pe1 - pe0;\n int dd = dir.x * dir.x + dir.y * dir.y + dir.z * dir.z + dir.w * dir.w;\n if (dd == 0) {\n for (int k = 0; k < 16; k++) { gIdx[k] = 0u; }\n return res;\n }\n float inv = lmax / float(dd);\n float sAA = 0.0, sBB = 0.0, sAB = 0.0;\n vec4 sAV = vec4(0.0), sBV = vec4(0.0);\n for (int k = 0; k < 16; k++) {\n ivec4 q = gPixels[k] - pe0;\n float proj = float(q.x * dir.x + q.y * dir.y + q.z * dir.z + q.w * dir.w) * inv;\n float s = clamp(floor(proj + 0.5), 0.0, lmax);\n gIdx[k] = uint(s);\n if (fit) {\n vec4 v = vec4(gPixels[k]);\n float b = s / lmax;\n float a = 1.0 - b;\n sAA += a * a; sBB += b * b; sAB += a * b; sAV += a * v; sBV += b * v;\n }\n }\n if (!fit) { return res; }\n float det = sAA * sBB - sAB * sAB;\n if (abs(det) < 1e-9) { return res; }\n res.e0 = ivec4(clamp(floor((sBB * sAV - sAB * sBV) / det + 0.5), vec4(0.0), vec4(255.0)));\n res.e1 = ivec4(clamp(floor((sAA * sBV - sAB * sAV) / det + 0.5), vec4(0.0), vec4(255.0)));\n res.valid = true;\n return res;\n}\n\nvoid main() {\n ivec2 base = ivec2(gl_FragCoord.xy) * 4;\n ivec2 maxXY = uSrcSize - ivec2(1);\n\n ivec4 lo = ivec4(255);\n ivec4 hi = ivec4(0);\n ivec4 isum = ivec4(0);\n int gd = 0; // max |R\u2212G|, |R\u2212B| over the block; 0 \u21D4 exactly grayscale\n for (int i = 0; i < 16; i++) {\n ivec2 p = clamp(base + ivec2(i & 3, i >> 2), ivec2(0), maxXY);\n int sy = (uFlipY != 0) ? (uSrcSize.y - 1 - p.y) : p.y;\n ivec4 px = to8(texelFetch(uSrc, ivec2(p.x, sy), 0));\n gPixels[i] = px;\n lo = min(lo, px);\n hi = max(hi, px);\n isum += px;\n gd = max(gd, max(abs(px.x - px.y), abs(px.x - px.z)));\n }\n bool opaque = lo.w == 255;\n\n uint w0 = 0u; uint w1 = 0u; uint w2 = 0u; uint w3 = 0u;\n\n if (opaque && gd == 0) {\n // ---------------- Luminance path: CEM 0, 5-bit weights ----------------\n // Endpoints at the exact extremes; 32 palette levels make an LSQ refit\n // unnecessary.\n uint L0 = uint(lo.x);\n uint L1 = uint(hi.x);\n uint s0 = 0u; uint s1 = 0u; uint s2 = 0u;\n if (L1 > L0) {\n float sc = 64.0 / float(hi.x - lo.x);\n // Exact nearest entry of the QUANT_32 grid: unq = 2w for w \u2264 15,\n // 2w + 2 for w \u2265 16 (4-wide gap at the middle, so uniform rounding\n // is wrong there). Best candidate of each half, keep the closer.\n for (int k = 0; k < 16; k++) {\n float u = clamp(float(gPixels[k].x - lo.x) * sc, 0.0, 64.0);\n float wlo = clamp(floor(u * 0.5 + 0.5), 0.0, 15.0);\n float whi = clamp(floor((u - 2.0) * 0.5 + 0.5), 16.0, 31.0);\n bool pick = abs(u - wlo * 2.0) <= abs(u - (whi * 2.0 + 2.0));\n uint w = uint(pick ? wlo : whi);\n // Stream bit q = 5k + j; straddles handled with constant shifts.\n uint off = 5u * uint(k);\n if (off < 28u) { s0 |= (w << off); }\n else if (off == 30u) { s0 |= (w << 30u); s1 |= (w >> 2u); }\n else if (off < 60u) { s1 |= (w << (off - 32u)); }\n else if (off == 60u) { s1 |= (w << 28u); s2 |= (w >> 4u); }\n else { s2 |= (w << (off - 64u)); }\n }\n }\n // Mode 0x253, partitions\u22121 = 0, CEM 0, L0 @17, L1 @25 (top bit spills\n // into word 1 bit 0); stream words map onto block words via rev32.\n w0 = 0x253u | (L0 << 17u) | (L1 << 25u);\n w1 = (L1 >> 7u) | rev32(s2);\n w2 = rev32(s1);\n w3 = rev32(s0);\n } else {\n // ------------- Colour paths: shared PCA seed ---------------------------\n vec4 mean = vec4(isum) / 16.0;\n float lmax = opaque ? 7.0 : 3.0;\n uint wmax = opaque ? 7u : 3u;\n\n ivec4 e0 = lo;\n ivec4 e1 = hi;\n // 8 iterations for opaque blocks, 4 for translucent (their refit\n // absorbs residual axis error \u2014 see astc4x4_fast_f16.wgsl).\n vec4 axis = principalAxis(mean, vec4(hi - lo), opaque ? 8 : 4);\n if (dot(axis, axis) > 0.0) {\n float tMin = 1e30;\n float tMax = -1e30;\n for (int k = 0; k < 16; k++) {\n float t = dot(vec4(gPixels[k]) - mean, axis);\n tMin = min(tMin, t);\n tMax = max(tMax, t);\n }\n e0 = ivec4(clamp(floor(mean + tMin * axis + 0.5), vec4(0.0), vec4(255.0)));\n e1 = ivec4(clamp(floor(mean + tMax * axis + 0.5), vec4(0.0), vec4(255.0)));\n }\n if (opaque) {\n // CEM 8 ships the quantised PCA extents directly (no LSQ fit \u2014 see\n // astc4x4_fast_f16.wgsl for the measured trade), bbox-clamped like\n // the fit output; one assignment pass fills gIdx for the packer.\n e0 = clamp(e0, lo, hi);\n e1 = clamp(e1, lo, hi);\n projAssign(e0, e1, lmax, false);\n } else {\n Fit r = projAssign(e0, e1, lmax, true);\n if (r.valid) {\n // Clamp the refit to the block bbox: on multi-cluster blocks the\n // unconstrained LSQ solve extrapolates far outside the block's\n // colours and the per-channel [0,255] clamp then bends the hue \u2014\n // fringe pixels decode to colours that exist nowhere in the block.\n // gIdx keeps the fit-pass weights (assigned against the seed line)\n // rather than reassigning against the refit endpoints \u2014 see\n // astc4x4_fast_f16.wgsl for the measured trade.\n e0 = clamp(r.e0, lo, hi);\n e1 = clamp(r.e1, lo, hi);\n }\n }\n\n // Endpoint ordering so the decoder doesn't apply blue contraction.\n if (e0.x + e0.y + e0.z > e1.x + e1.y + e1.z) {\n ivec4 t = e0; e0 = e1; e1 = t;\n for (int k = 0; k < 16; k++) { gIdx[k] = wmax - gIdx[k]; }\n }\n\n if (opaque) {\n // CEM 8: 3-bit weights, stream bit q = 3k.\n uint s0 = 0u; uint s1 = 0u;\n for (int k = 0; k < 16; k++) {\n uint w = gIdx[k];\n uint off = 3u * uint(k);\n if (off < 30u) { s0 |= (w << off); }\n else if (off == 30u) { s0 |= (w << 30u); s1 |= (w >> 2u); }\n else { s1 |= (w << (off - 32u)); }\n }\n // Mode 0x053, CEM 8 @13, endpoints R0 R1 G0 G1 B0 B1 from bit 17.\n w0 = 0x053u | (8u << 13u) | (uint(e0.x) << 17u) | (uint(e1.x) << 25u);\n w1 = (uint(e1.x) >> 7u) | (uint(e0.y) << 1u) | (uint(e1.y) << 9u)\n | (uint(e0.z) << 17u) | (uint(e1.z) << 25u);\n w2 = (uint(e1.z) >> 7u) | rev32(s1);\n w3 = rev32(s0);\n } else {\n // CEM 12: 2-bit weights, stream bit q = 2k (single stream word).\n uint s0 = 0u;\n for (int k = 0; k < 16; k++) {\n s0 |= (gIdx[k] << (2u * uint(k)));\n }\n // Mode 0x042, CEM 12 @13, endpoints R0 R1 G0 G1 B0 B1 A0 A1 from 17.\n w0 = 0x042u | (12u << 13u) | (uint(e0.x) << 17u) | (uint(e1.x) << 25u);\n w1 = (uint(e1.x) >> 7u) | (uint(e0.y) << 1u) | (uint(e1.y) << 9u)\n | (uint(e0.z) << 17u) | (uint(e1.z) << 25u);\n w2 = (uint(e1.z) >> 7u) | (uint(e0.w) << 1u) | (uint(e1.w) << 9u);\n w3 = rev32(s0);\n }\n }\n\n outColor = uvec4(w0, w1, w2, w3);\n}\n";
1516
2207
 
1517
2208
  // src/webgl/ASTC4x4WebGLEncoder.ts
1518
2209
  var ASTC4x4WebGLEncoder = class extends WebGLBlockEncoder {
@@ -1548,26 +2239,30 @@ function detectWebGLCapabilities(gl) {
1548
2239
  // src/webgl/selectWebGLFormat.ts
1549
2240
  var NONE = { format: null, encoderClass: null, astcNormalRemap: false };
1550
2241
  function selectWebGLFormat(caps, hint, options = {}) {
1551
- const { colorSpace = "srgb", preferredFormat } = options;
2242
+ const { colorSpace = "srgb", preferredFormat, quality = "high" } = options;
1552
2243
  const srgb = colorSpace === "srgb";
1553
2244
  const astc = (astcNormalRemap) => ({
1554
2245
  format: srgb ? TextureFormat.ASTC_4x4_SRGB : TextureFormat.ASTC_4x4,
1555
2246
  encoderClass: ASTC4x4WebGLEncoder,
1556
2247
  astcNormalRemap
1557
2248
  });
2249
+ const bc1 = () => ({
2250
+ format: srgb ? TextureFormat.BC1_SRGB : TextureFormat.BC1,
2251
+ encoderClass: BC1WebGLEncoder,
2252
+ astcNormalRemap: false
2253
+ });
1558
2254
  if (preferredFormat === "bc1") {
1559
2255
  if (hint !== "color") {
1560
2256
  console.warn(
1561
2257
  `[gputex] preferredFormat 'bc1' ignored for hint '${hint}' \u2014 BC1 has no real alpha channel and is unsuitable for normal maps.`
1562
2258
  );
1563
2259
  } else if (srgb ? caps.s3tcSrgb : caps.s3tc) {
1564
- return {
1565
- format: srgb ? TextureFormat.BC1_SRGB : TextureFormat.BC1,
1566
- encoderClass: BC1WebGLEncoder,
1567
- astcNormalRemap: false
1568
- };
2260
+ return bc1();
1569
2261
  }
1570
2262
  }
2263
+ if (quality === "low" && hint === "color" && (srgb ? caps.s3tcSrgb : caps.s3tc)) {
2264
+ return bc1();
2265
+ }
1571
2266
  if (hint === "normal") {
1572
2267
  if (caps.rgtc) return { format: TextureFormat.BC5, encoderClass: BC5WebGLEncoder, astcNormalRemap: false };
1573
2268
  if (caps.astc) return astc(true);
@@ -1581,35 +2276,40 @@ function selectWebGLFormat(caps, hint, options = {}) {
1581
2276
  };
1582
2277
  }
1583
2278
  if (caps.astc) return astc(false);
1584
- if (hint === "color") {
1585
- if (srgb && caps.s3tcSrgb) {
1586
- return { format: TextureFormat.BC1_SRGB, encoderClass: BC1WebGLEncoder, astcNormalRemap: false };
1587
- }
1588
- if (!srgb && caps.s3tc) {
1589
- return { format: TextureFormat.BC1, encoderClass: BC1WebGLEncoder, astcNormalRemap: false };
1590
- }
2279
+ if (hint === "color" && (srgb ? caps.s3tcSrgb : caps.s3tc)) {
2280
+ return bc1();
1591
2281
  }
1592
2282
  return NONE;
1593
2283
  }
1594
2284
 
1595
2285
  // src/selectFormat.ts
1596
2286
  function selectFormat(adapter, hint, options = {}) {
1597
- const { colorSpace = "srgb", preferredFormat } = options;
2287
+ const { colorSpace = "srgb", preferredFormat, quality = "high" } = options;
1598
2288
  const srgb = colorSpace === "srgb";
1599
2289
  const caps = detectCapabilities(adapter);
2290
+ const bc1 = {
2291
+ format: srgb ? TextureFormat.BC1_SRGB : TextureFormat.BC1,
2292
+ encoderClass: BC1Encoder,
2293
+ astcNormalRemap: false
2294
+ };
2295
+ const etc2 = {
2296
+ format: srgb ? TextureFormat.ETC2_RGB8_SRGB : TextureFormat.ETC2_RGB8,
2297
+ encoderClass: ETC2Encoder,
2298
+ astcNormalRemap: false
2299
+ };
1600
2300
  if (preferredFormat === "bc1") {
1601
2301
  if (hint !== "color") {
1602
2302
  console.warn(
1603
2303
  `[gputex] preferredFormat 'bc1' ignored for hint '${hint}' \u2014 BC1 has no real alpha channel and is unsuitable for normal maps.`
1604
2304
  );
1605
2305
  } else if (caps.bc) {
1606
- return {
1607
- format: srgb ? TextureFormat.BC1_SRGB : TextureFormat.BC1,
1608
- encoderClass: BC1Encoder,
1609
- astcNormalRemap: false
1610
- };
2306
+ return bc1;
1611
2307
  }
1612
2308
  }
2309
+ if (quality === "low" && hint === "color") {
2310
+ if (caps.bc) return bc1;
2311
+ if (caps.etc2) return etc2;
2312
+ }
1613
2313
  if (caps.bc) {
1614
2314
  if (hint === "normal") {
1615
2315
  return { format: TextureFormat.BC5, encoderClass: BC5Encoder, astcNormalRemap: false };
@@ -1628,6 +2328,9 @@ function selectFormat(adapter, hint, options = {}) {
1628
2328
  astcNormalRemap: hint === "normal"
1629
2329
  };
1630
2330
  }
2331
+ if (caps.etc2 && hint === "color") {
2332
+ return etc2;
2333
+ }
1631
2334
  return { format: null, encoderClass: null, astcNormalRemap: false };
1632
2335
  }
1633
2336
 
@@ -1653,24 +2356,28 @@ function downsample2x(src) {
1653
2356
  const dstW = Math.max(1, src.width >> 1);
1654
2357
  const dstH = Math.max(1, src.height >> 1);
1655
2358
  const dst = new Uint8ClampedArray(dstW * dstH * 4);
1656
- const sW = src.width;
1657
- const sMaxX = src.width - 1;
2359
+ const s = src.data;
2360
+ const rowBytes = src.width * 4;
2361
+ const lastColByte = (src.width - 1) * 4;
1658
2362
  const sMaxY = src.height - 1;
2363
+ let o = 0;
1659
2364
  for (let y = 0; y < dstH; y++) {
1660
- const sy0 = y * 2;
1661
- const sy1 = Math.min(sy0 + 1, sMaxY);
2365
+ const sy0 = y << 1;
2366
+ const sy1 = sy0 < sMaxY ? sy0 + 1 : sMaxY;
2367
+ const r0 = sy0 * rowBytes;
2368
+ const r1 = sy1 * rowBytes;
1662
2369
  for (let x = 0; x < dstW; x++) {
1663
- const sx0 = x * 2;
1664
- const sx1 = Math.min(sx0 + 1, sMaxX);
1665
- const i00 = (sy0 * sW + sx0) * 4;
1666
- const i10 = (sy0 * sW + sx1) * 4;
1667
- const i01 = (sy1 * sW + sx0) * 4;
1668
- const i11 = (sy1 * sW + sx1) * 4;
1669
- const o = (y * dstW + x) * 4;
1670
- dst[o] = src.data[i00] + src.data[i10] + src.data[i01] + src.data[i11] + 2 >> 2;
1671
- dst[o + 1] = src.data[i00 + 1] + src.data[i10 + 1] + src.data[i01 + 1] + src.data[i11 + 1] + 2 >> 2;
1672
- dst[o + 2] = src.data[i00 + 2] + src.data[i10 + 2] + src.data[i01 + 2] + src.data[i11 + 2] + 2 >> 2;
1673
- dst[o + 3] = src.data[i00 + 3] + src.data[i10 + 3] + src.data[i01 + 3] + src.data[i11 + 3] + 2 >> 2;
2370
+ const sx0 = x << 3;
2371
+ const sx1 = sx0 + 4 <= lastColByte ? sx0 + 4 : lastColByte;
2372
+ const i00 = r0 + sx0;
2373
+ const i10 = r0 + sx1;
2374
+ const i01 = r1 + sx0;
2375
+ const i11 = r1 + sx1;
2376
+ dst[o] = s[i00] + s[i10] + s[i01] + s[i11] + 2 >> 2;
2377
+ dst[o + 1] = s[i00 + 1] + s[i10 + 1] + s[i01 + 1] + s[i11 + 1] + 2 >> 2;
2378
+ dst[o + 2] = s[i00 + 2] + s[i10 + 2] + s[i01 + 2] + s[i11 + 2] + 2 >> 2;
2379
+ dst[o + 3] = s[i00 + 3] + s[i10 + 3] + s[i01 + 3] + s[i11 + 3] + 2 >> 2;
2380
+ o += 4;
1674
2381
  }
1675
2382
  }
1676
2383
  return { data: dst, width: dstW, height: dstH };
@@ -1697,7 +2404,110 @@ function padToBlockMultiple(level) {
1697
2404
  return { data: out, width: pw, height: ph };
1698
2405
  }
1699
2406
 
2407
+ // src/gpuMipgen.ts
2408
+ function gpuMipLevelCount(width, height) {
2409
+ return 32 - Math.clz32(Math.max(width, height));
2410
+ }
2411
+ var DOWNSAMPLE_WGSL = (
2412
+ /* wgsl */
2413
+ `
2414
+ @group(0) @binding(0) var src : texture_2d<f32>;
2415
+ @group(0) @binding(1) var dst : texture_storage_2d<rgba8unorm, write>;
2416
+
2417
+ // 2x2 box filter, clamp-to-edge fold on odd source dimensions, integer
2418
+ // round-to-nearest \u2014 bit-exact with mipgen.ts downsample2x.
2419
+ @compute @workgroup_size(8, 8, 1)
2420
+ fn downsample(@builtin(global_invocation_id) gid : vec3<u32>) {
2421
+ let dstSize = textureDimensions(dst);
2422
+ if (gid.x >= dstSize.x || gid.y >= dstSize.y) { return; }
2423
+ let srcMax = textureDimensions(src) - vec2<u32>(1u, 1u);
2424
+ let x0 = min(gid.x * 2u, srcMax.x);
2425
+ let x1 = min(gid.x * 2u + 1u, srcMax.x);
2426
+ let y0 = min(gid.y * 2u, srcMax.y);
2427
+ let y1 = min(gid.y * 2u + 1u, srcMax.y);
2428
+ // round() recovers the exact byte values (unorm->f32 conversion is only
2429
+ // correctly-rounded, so a*255 is n +/- ~1e-5 \u2014 floor() would misround
2430
+ // sums divisible by 4 without it).
2431
+ let s = round(textureLoad(src, vec2<u32>(x0, y0), 0) * 255.0) +
2432
+ round(textureLoad(src, vec2<u32>(x1, y0), 0) * 255.0) +
2433
+ round(textureLoad(src, vec2<u32>(x0, y1), 0) * 255.0) +
2434
+ round(textureLoad(src, vec2<u32>(x1, y1), 0) * 255.0);
2435
+ textureStore(dst, gid.xy, floor((s + 2.0) * 0.25) / 255.0);
2436
+ }
2437
+ `
2438
+ );
2439
+ var pipelineCache = /* @__PURE__ */ new WeakMap();
2440
+ function getDownsamplePipeline(device) {
2441
+ let pipeline = pipelineCache.get(device);
2442
+ if (!pipeline) {
2443
+ pipeline = device.createComputePipelineAsync({
2444
+ label: "gputex-mipgen-pipeline",
2445
+ layout: "auto",
2446
+ compute: {
2447
+ module: device.createShaderModule({ label: "gputex-mipgen", code: DOWNSAMPLE_WGSL }),
2448
+ entryPoint: "downsample"
2449
+ }
2450
+ });
2451
+ pipelineCache.set(device, pipeline);
2452
+ pipeline.catch(() => pipelineCache.delete(device));
2453
+ }
2454
+ return pipeline;
2455
+ }
2456
+ async function generateGpuMipChain(device, source, { flipY = false } = {}) {
2457
+ const width = source.width;
2458
+ const height = source.height;
2459
+ if (!width || !height) {
2460
+ throw new Error("generateGpuMipChain: source has no dimensions");
2461
+ }
2462
+ const mipLevelCount = gpuMipLevelCount(width, height);
2463
+ const texture = device.createTexture({
2464
+ label: "gputex-mip-chain",
2465
+ size: [width, height, 1],
2466
+ format: "rgba8unorm",
2467
+ mipLevelCount,
2468
+ // COPY_DST + RENDER_ATTACHMENT for copyExternalImageToTexture (a blit
2469
+ // internally), TEXTURE_BINDING for downsample/encoder reads,
2470
+ // STORAGE_BINDING for downsample writes (rgba8unorm write-only storage
2471
+ // is core WebGPU).
2472
+ usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING
2473
+ });
2474
+ device.queue.copyExternalImageToTexture({ source, flipY }, { texture }, [width, height, 1]);
2475
+ const pipeline = await getDownsamplePipeline(device);
2476
+ const enc = device.createCommandEncoder({ label: "gputex-mipgen" });
2477
+ const pass = enc.beginComputePass();
2478
+ pass.setPipeline(pipeline);
2479
+ for (let level = 0; level + 1 < mipLevelCount; level++) {
2480
+ const bindGroup = device.createBindGroup({
2481
+ label: `gputex-mipgen-bg-${level}`,
2482
+ layout: pipeline.getBindGroupLayout(0),
2483
+ entries: [
2484
+ { binding: 0, resource: texture.createView({ baseMipLevel: level, mipLevelCount: 1 }) },
2485
+ { binding: 1, resource: texture.createView({ baseMipLevel: level + 1, mipLevelCount: 1 }) }
2486
+ ]
2487
+ });
2488
+ pass.setBindGroup(0, bindGroup);
2489
+ const dstW = Math.max(1, width >> level + 1);
2490
+ const dstH = Math.max(1, height >> level + 1);
2491
+ pass.dispatchWorkgroups(Math.ceil(dstW / 8), Math.ceil(dstH / 8), 1);
2492
+ }
2493
+ pass.end();
2494
+ device.queue.submit([enc.finish()]);
2495
+ return texture;
2496
+ }
2497
+
1700
2498
  // src/svg.ts
2499
+ function isSvgMarkup(source) {
2500
+ return source.trimStart().startsWith("<");
2501
+ }
2502
+ function hasSvgExtension(url) {
2503
+ return /\.svg$/i.test(url.split(/[?#]/, 1)[0]);
2504
+ }
2505
+ function isSvgBlob(blob) {
2506
+ if (blob.type) {
2507
+ return blob.type.split(";", 1)[0].trim().toLowerCase() === "image/svg+xml";
2508
+ }
2509
+ return typeof File !== "undefined" && blob instanceof File && hasSvgExtension(blob.name);
2510
+ }
1701
2511
  var ROOT_TAG_RE = /<svg(?=[\s/>])[^>]*>/;
1702
2512
  function getAttr(tag, name) {
1703
2513
  const m = new RegExp(`\\s${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`).exec(tag);
@@ -1818,6 +2628,482 @@ async function rasterizeSvg(source, options = {}) {
1818
2628
  URL.revokeObjectURL(url);
1819
2629
  }
1820
2630
  }
2631
+
2632
+ // src/transcodeCache.ts
2633
+ var DEFAULT_LIMIT = 256 * 1024 * 1024;
2634
+ var maxBytes = DEFAULT_LIMIT;
2635
+ var totalBytes = 0;
2636
+ var entries = /* @__PURE__ */ new Map();
2637
+ async function sha256Hex(data) {
2638
+ const digest = await crypto.subtle.digest("SHA-256", data);
2639
+ let hex = "";
2640
+ for (const b of new Uint8Array(digest)) hex += b.toString(16).padStart(2, "0");
2641
+ return hex;
2642
+ }
2643
+ async function sourceIdentity(source, cacheKey) {
2644
+ if (cacheKey) return `k:${cacheKey}`;
2645
+ const canHash = typeof crypto !== "undefined" && !!crypto.subtle;
2646
+ if (typeof source === "string") {
2647
+ if (source.length > 1024 || /^data:/i.test(source) || /^\s*</.test(source)) {
2648
+ return canHash ? `s:${await sha256Hex(new TextEncoder().encode(source))}` : null;
2649
+ }
2650
+ return `u:${source}`;
2651
+ }
2652
+ if (typeof Blob !== "undefined" && source instanceof Blob) {
2653
+ return canHash ? `b:${await sha256Hex(await source.arrayBuffer())}` : null;
2654
+ }
2655
+ return null;
2656
+ }
2657
+ async function buildTranscodeKey(source, cacheKey, fp) {
2658
+ if (maxBytes <= 0) return null;
2659
+ const id = await sourceIdentity(source, cacheKey);
2660
+ if (!id) return null;
2661
+ const fingerprint = [
2662
+ fp.format,
2663
+ fp.colorSpace,
2664
+ fp.flipY ? "flip" : "noflip",
2665
+ fp.mipmaps ? "mips" : "nomips",
2666
+ fp.svgSize === void 0 ? "" : JSON.stringify(fp.svgSize)
2667
+ ].join("|");
2668
+ return `${fingerprint}\0${id}`;
2669
+ }
2670
+ function readTranscodeCache(key) {
2671
+ const hit = entries.get(key);
2672
+ if (!hit) return null;
2673
+ entries.delete(key);
2674
+ entries.set(key, hit);
2675
+ return hit.entry;
2676
+ }
2677
+ function writeTranscodeCache(key, entry) {
2678
+ const bytes = entry.levels.reduce((sum, l) => sum + l.data.byteLength, 0);
2679
+ if (bytes > maxBytes) return;
2680
+ const prev = entries.get(key);
2681
+ if (prev) {
2682
+ totalBytes -= prev.bytes;
2683
+ entries.delete(key);
2684
+ }
2685
+ entries.set(key, { entry, bytes });
2686
+ totalBytes += bytes;
2687
+ evictToLimit();
2688
+ }
2689
+ function evictToLimit() {
2690
+ for (const [key, value] of entries) {
2691
+ if (totalBytes <= maxBytes) break;
2692
+ entries.delete(key);
2693
+ totalBytes -= value.bytes;
2694
+ }
2695
+ }
2696
+ function setTranscodeCacheLimit(bytes) {
2697
+ maxBytes = Math.max(0, bytes);
2698
+ evictToLimit();
2699
+ }
2700
+ function clearTranscodeCache() {
2701
+ entries.clear();
2702
+ totalBytes = 0;
2703
+ }
2704
+
2705
+ // src/compressTexture.ts
2706
+ var sharedGpuPromise = null;
2707
+ var SHARED_DEVICE_FEATURES = [
2708
+ "texture-compression-bc",
2709
+ "texture-compression-astc",
2710
+ "texture-compression-etc2",
2711
+ "shader-f16",
2712
+ "timestamp-query"
2713
+ ];
2714
+ async function createSharedGpu() {
2715
+ const adapter = await navigator.gpu.requestAdapter();
2716
+ if (!adapter) return null;
2717
+ const requiredFeatures = SHARED_DEVICE_FEATURES.filter((f) => adapter.features.has(f));
2718
+ const device = await adapter.requestDevice({ requiredFeatures });
2719
+ return { adapter, device, encoders: /* @__PURE__ */ new Map() };
2720
+ }
2721
+ function getSharedGpu() {
2722
+ if (!sharedGpuPromise) {
2723
+ const p = createSharedGpu();
2724
+ sharedGpuPromise = p;
2725
+ p.then((shared) => {
2726
+ if (!shared) return;
2727
+ void shared.device.lost.then(() => {
2728
+ shared.encoders.forEach((encoder) => encoder.destroy());
2729
+ shared.encoders.clear();
2730
+ if (sharedGpuPromise === p) sharedGpuPromise = null;
2731
+ });
2732
+ }).catch(() => {
2733
+ if (sharedGpuPromise === p) sharedGpuPromise = null;
2734
+ });
2735
+ }
2736
+ return sharedGpuPromise;
2737
+ }
2738
+ function releaseSharedGpuResources() {
2739
+ const p = sharedGpuPromise;
2740
+ sharedGpuPromise = null;
2741
+ void p?.then((shared) => {
2742
+ if (!shared) return;
2743
+ shared.encoders.forEach((encoder) => encoder.destroy());
2744
+ shared.encoders.clear();
2745
+ shared.device.destroy();
2746
+ }).catch(() => {
2747
+ });
2748
+ }
2749
+ async function sourceToBitmap(source, svgSize) {
2750
+ const opts = {
2751
+ colorSpaceConversion: "none",
2752
+ premultiplyAlpha: "none"
2753
+ };
2754
+ if (typeof source === "string") {
2755
+ if (isSvgMarkup(source)) {
2756
+ return rasterizeSvg(source, { size: svgSize });
2757
+ }
2758
+ if (/^data:/i.test(source)) {
2759
+ const blob2 = dataUrlToBlob(source);
2760
+ if (isSvgBlob(blob2)) {
2761
+ return rasterizeSvg(blob2, { size: svgSize });
2762
+ }
2763
+ return createImageBitmap(blob2, opts);
2764
+ }
2765
+ const resp = await fetch(source);
2766
+ if (!resp.ok) {
2767
+ throw new Error(`compressTexture: fetch ${source} failed (${resp.status})`);
2768
+ }
2769
+ const blob = await resp.blob();
2770
+ if (isSvgBlob(blob) || !isImageMimeType(blob.type) && hasSvgExtension(source)) {
2771
+ return rasterizeSvg(blob, { size: svgSize });
2772
+ }
2773
+ return createImageBitmap(blob, opts);
2774
+ }
2775
+ if (source instanceof Blob) {
2776
+ if (isSvgBlob(source)) {
2777
+ return rasterizeSvg(source, { size: svgSize });
2778
+ }
2779
+ return createImageBitmap(source, opts);
2780
+ }
2781
+ if (source instanceof ImageBitmap) {
2782
+ return source;
2783
+ }
2784
+ if (typeof HTMLImageElement !== "undefined" && source instanceof HTMLImageElement) {
2785
+ const src = source.currentSrc || source.src;
2786
+ if (src && (hasSvgExtension(src) || /^data:image\/svg\+xml/i.test(src))) {
2787
+ const resp = await fetch(src);
2788
+ if (!resp.ok) {
2789
+ throw new Error(`compressTexture: fetch ${src} failed (${resp.status})`);
2790
+ }
2791
+ return rasterizeSvg(await resp.blob(), { size: svgSize });
2792
+ }
2793
+ }
2794
+ return createImageBitmap(source, opts);
2795
+ }
2796
+ function isImageMimeType(type) {
2797
+ return /^image\//i.test(type) && !/svg/i.test(type);
2798
+ }
2799
+ function dataUrlToBlob(url) {
2800
+ const comma = url.indexOf(",");
2801
+ if (comma < 0) {
2802
+ throw new Error("compressTexture: malformed data: URL (no comma)");
2803
+ }
2804
+ const header = url.slice(5, comma);
2805
+ const isBase64 = /;base64$/i.test(header);
2806
+ const type = header.replace(/;base64$/i, "");
2807
+ if (!isBase64) {
2808
+ return new Blob([decodeURIComponent(url.slice(comma + 1))], { type });
2809
+ }
2810
+ const payload = url.slice(comma + 1);
2811
+ const fromBase64 = Uint8Array.fromBase64;
2812
+ if (fromBase64) {
2813
+ return new Blob([fromBase64(payload)], { type });
2814
+ }
2815
+ const bin = atob(payload);
2816
+ const bytes = new Uint8Array(bin.length);
2817
+ for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
2818
+ return new Blob([bytes], { type });
2819
+ }
2820
+ function bitmapToMipLevel(bitmap, flipY) {
2821
+ const w = bitmap.width, h = bitmap.height;
2822
+ const canvas = typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(w, h) : Object.assign(document.createElement("canvas"), { width: w, height: h });
2823
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
2824
+ if (!ctx) {
2825
+ throw new Error("compressTexture: no 2D context available for mip generation");
2826
+ }
2827
+ if (flipY) {
2828
+ ctx.translate(0, h);
2829
+ ctx.scale(1, -1);
2830
+ }
2831
+ ctx.drawImage(bitmap, 0, 0);
2832
+ const imageData = ctx.getImageData(0, 0, w, h);
2833
+ return { data: imageData.data, width: w, height: h };
2834
+ }
2835
+ function mipLevelToImageData(level) {
2836
+ return new ImageData(level.data, level.width, level.height);
2837
+ }
2838
+ async function compressTextureToBytes(source, options = {}) {
2839
+ const {
2840
+ hint = "color",
2841
+ preferredFormat,
2842
+ quality = "high",
2843
+ colorSpace = "srgb",
2844
+ svgSize,
2845
+ flipY = true,
2846
+ mipmaps = false,
2847
+ cache = false,
2848
+ cacheKey,
2849
+ device: providedDevice,
2850
+ adapter: providedAdapter
2851
+ } = options;
2852
+ const t0 = performance.now();
2853
+ const gpu = await resolveWebGPU();
2854
+ const gl = gpu ? null : resolveWebGL();
2855
+ const activeFormat = gpu?.selection.format ?? gl?.selection.format ?? null;
2856
+ let transcodeKey = null;
2857
+ if (cache && activeFormat) {
2858
+ transcodeKey = await buildTranscodeKey(source, cacheKey, {
2859
+ format: activeFormat,
2860
+ colorSpace,
2861
+ flipY,
2862
+ mipmaps,
2863
+ svgSize
2864
+ });
2865
+ if (transcodeKey) {
2866
+ const hit = readTranscodeCache(transcodeKey);
2867
+ if (hit) {
2868
+ return {
2869
+ levels: hit.levels,
2870
+ fallbackBitmap: null,
2871
+ format: hit.format,
2872
+ fallbackUncompressed: false,
2873
+ backend: gpu ? "webgpu" : "webgl",
2874
+ astcNormalRemap: (gpu ?? gl).selection.astcNormalRemap,
2875
+ width: hit.width,
2876
+ height: hit.height,
2877
+ mipLevels: hit.levels.length,
2878
+ encodeMs: 0,
2879
+ decodeMs: 0,
2880
+ totalMs: performance.now() - t0,
2881
+ cacheHit: true
2882
+ };
2883
+ }
2884
+ }
2885
+ }
2886
+ const tDecode = performance.now();
2887
+ const bitmap = await sourceToBitmap(source, svgSize);
2888
+ const decodeMs = performance.now() - tDecode;
2889
+ if (gpu) return encodeViaWebGPU(gpu);
2890
+ const viaWebGL = gl ? encodeViaWebGL(gl) : null;
2891
+ if (viaWebGL) return viaWebGL;
2892
+ console.warn(
2893
+ "[compressTextureToBytes] No compressed path available (WebGPU and WebGL2 both lack a usable compressed-texture format); returning uncompressed RGBA8."
2894
+ );
2895
+ return {
2896
+ levels: null,
2897
+ fallbackBitmap: bitmap,
2898
+ format: null,
2899
+ fallbackUncompressed: true,
2900
+ backend: "none",
2901
+ astcNormalRemap: false,
2902
+ width: bitmap.width,
2903
+ height: bitmap.height,
2904
+ mipLevels: 1,
2905
+ encodeMs: 0,
2906
+ decodeMs,
2907
+ totalMs: performance.now() - t0,
2908
+ cacheHit: false
2909
+ };
2910
+ async function resolveWebGPU() {
2911
+ if (!("gpu" in navigator)) return null;
2912
+ let shared = null;
2913
+ let adapter;
2914
+ if (providedAdapter) {
2915
+ adapter = providedAdapter;
2916
+ } else if (providedDevice) {
2917
+ adapter = await navigator.gpu.requestAdapter();
2918
+ } else {
2919
+ shared = await getSharedGpu();
2920
+ adapter = shared?.adapter ?? null;
2921
+ }
2922
+ if (!adapter) return null;
2923
+ const selection = selectFormat(adapter, hint, { colorSpace, preferredFormat, quality });
2924
+ if (!selection.format || !selection.encoderClass) return null;
2925
+ return {
2926
+ adapter,
2927
+ shared,
2928
+ selection: { ...selection, format: selection.format, encoderClass: selection.encoderClass }
2929
+ };
2930
+ }
2931
+ async function encodeViaWebGPU({ adapter, shared, selection }) {
2932
+ const EncoderCtor = selection.encoderClass;
2933
+ let encoder;
2934
+ let sharedEncoder = false;
2935
+ if (providedDevice) {
2936
+ encoder = new EncoderCtor({ device: providedDevice, adapter, ownsDevice: false });
2937
+ } else if (shared) {
2938
+ sharedEncoder = true;
2939
+ let cached = shared.encoders.get(EncoderCtor);
2940
+ if (!cached) {
2941
+ cached = new EncoderCtor({ device: shared.device, adapter: shared.adapter, ownsDevice: false });
2942
+ shared.encoders.set(EncoderCtor, cached);
2943
+ }
2944
+ encoder = cached;
2945
+ } else {
2946
+ encoder = await EncoderCtor.create();
2947
+ }
2948
+ const destroyEncoder = sharedEncoder ? () => {
2949
+ } : () => encoder.destroy();
2950
+ try {
2951
+ const needsWriteTexture = needsWriteTextureWorkaround(adapter);
2952
+ if (!mipmaps) {
2953
+ let bytes;
2954
+ if (needsWriteTexture) {
2955
+ const level0 = bitmapToMipLevel(bitmap, flipY);
2956
+ const imageData = mipLevelToImageData(level0);
2957
+ bytes = await encoder.encodeToBytes(imageData);
2958
+ } else {
2959
+ bytes = await encoder.encodeToBytes(bitmap, { flipY });
2960
+ }
2961
+ if (transcodeKey) {
2962
+ writeTranscodeCache(transcodeKey, {
2963
+ format: selection.format,
2964
+ width: bytes.width,
2965
+ height: bytes.height,
2966
+ levels: [bytes]
2967
+ });
2968
+ }
2969
+ destroyEncoder();
2970
+ return {
2971
+ levels: [bytes],
2972
+ fallbackBitmap: null,
2973
+ format: selection.format,
2974
+ fallbackUncompressed: false,
2975
+ backend: "webgpu",
2976
+ astcNormalRemap: selection.astcNormalRemap,
2977
+ width: bytes.width,
2978
+ height: bytes.height,
2979
+ mipLevels: 1,
2980
+ encodeMs: bytes.encodeMs,
2981
+ decodeMs,
2982
+ totalMs: performance.now() - t0,
2983
+ cacheHit: false
2984
+ };
2985
+ }
2986
+ let chainResult;
2987
+ if (needsWriteTexture) {
2988
+ const level0 = bitmapToMipLevel(bitmap, flipY);
2989
+ chainResult = await encoder.encodeMipChainToBytes(generateMipChain(level0).map(padToBlockMultiple));
2990
+ } else {
2991
+ const chainTex = await generateGpuMipChain(encoder.device, bitmap, { flipY });
2992
+ try {
2993
+ chainResult = await encoder.encodeMipChainFromTexture(chainTex);
2994
+ } finally {
2995
+ chainTex.destroy();
2996
+ }
2997
+ }
2998
+ const { levels, encodeMs } = chainResult;
2999
+ if (transcodeKey) {
3000
+ writeTranscodeCache(transcodeKey, {
3001
+ format: selection.format,
3002
+ width: bitmap.width,
3003
+ height: bitmap.height,
3004
+ levels
3005
+ });
3006
+ }
3007
+ destroyEncoder();
3008
+ return {
3009
+ levels,
3010
+ fallbackBitmap: null,
3011
+ format: selection.format,
3012
+ fallbackUncompressed: false,
3013
+ backend: "webgpu",
3014
+ astcNormalRemap: selection.astcNormalRemap,
3015
+ width: bitmap.width,
3016
+ height: bitmap.height,
3017
+ mipLevels: levels.length,
3018
+ encodeMs,
3019
+ decodeMs,
3020
+ totalMs: performance.now() - t0,
3021
+ cacheHit: false
3022
+ };
3023
+ } catch (e) {
3024
+ destroyEncoder();
3025
+ throw e;
3026
+ }
3027
+ }
3028
+ function resolveWebGL() {
3029
+ const gl2 = getSharedWebGLContext();
3030
+ if (!gl2) return null;
3031
+ const caps = detectWebGLCapabilities(gl2);
3032
+ const selection = selectWebGLFormat(caps, hint, { colorSpace, preferredFormat, quality });
3033
+ if (!selection.format || !selection.encoderClass) return null;
3034
+ return { gl: gl2, selection: { ...selection, format: selection.format, encoderClass: selection.encoderClass } };
3035
+ }
3036
+ function encodeViaWebGL({ gl: gl2, selection }) {
3037
+ const encoder = selection.encoderClass.create(gl2);
3038
+ try {
3039
+ if (!mipmaps) {
3040
+ const bytes = encoder.encodeToBytes(bitmap, { flipY });
3041
+ if (transcodeKey) {
3042
+ writeTranscodeCache(transcodeKey, {
3043
+ format: selection.format,
3044
+ width: bytes.width,
3045
+ height: bytes.height,
3046
+ levels: [bytes]
3047
+ });
3048
+ }
3049
+ encoder.destroy();
3050
+ return {
3051
+ levels: [bytes],
3052
+ fallbackBitmap: null,
3053
+ format: selection.format,
3054
+ fallbackUncompressed: false,
3055
+ backend: "webgl",
3056
+ astcNormalRemap: selection.astcNormalRemap,
3057
+ width: bytes.width,
3058
+ height: bytes.height,
3059
+ mipLevels: 1,
3060
+ encodeMs: bytes.encodeMs,
3061
+ decodeMs,
3062
+ totalMs: performance.now() - t0,
3063
+ cacheHit: false
3064
+ };
3065
+ }
3066
+ const level0 = bitmapToMipLevel(bitmap, flipY);
3067
+ const chain = generateMipChain(level0);
3068
+ const encodedLevels = [];
3069
+ let totalEncodeMs = 0;
3070
+ for (const level of chain) {
3071
+ const padded = padToBlockMultiple(level);
3072
+ const bytes = encoder.encodeToBytes(padded);
3073
+ encodedLevels.push(bytes);
3074
+ totalEncodeMs += bytes.encodeMs;
3075
+ }
3076
+ if (transcodeKey) {
3077
+ writeTranscodeCache(transcodeKey, {
3078
+ format: selection.format,
3079
+ width: level0.width,
3080
+ height: level0.height,
3081
+ levels: encodedLevels
3082
+ });
3083
+ }
3084
+ encoder.destroy();
3085
+ return {
3086
+ levels: encodedLevels,
3087
+ fallbackBitmap: null,
3088
+ format: selection.format,
3089
+ fallbackUncompressed: false,
3090
+ backend: "webgl",
3091
+ astcNormalRemap: selection.astcNormalRemap,
3092
+ width: level0.width,
3093
+ height: level0.height,
3094
+ mipLevels: encodedLevels.length,
3095
+ encodeMs: totalEncodeMs,
3096
+ decodeMs,
3097
+ totalMs: performance.now() - t0,
3098
+ cacheHit: false
3099
+ };
3100
+ } catch (e) {
3101
+ encoder.destroy();
3102
+ console.warn("[compressTextureToBytes] WebGL fallback encode failed; returning uncompressed RGBA8.", e);
3103
+ return null;
3104
+ }
3105
+ }
3106
+ }
1821
3107
  export {
1822
3108
  ASTC4x4Encoder,
1823
3109
  ASTC4x4WebGLEncoder,
@@ -1827,18 +3113,25 @@ export {
1827
3113
  BC5WebGLEncoder,
1828
3114
  BC7Encoder,
1829
3115
  BC7WebGLEncoder,
3116
+ ETC2Encoder,
1830
3117
  Encoder,
1831
3118
  TextureFormat,
1832
3119
  WebGLBlockEncoder,
1833
3120
  WebGPUFeature,
3121
+ clearTranscodeCache,
3122
+ compressTextureToBytes,
1834
3123
  createWebGLContext,
1835
3124
  detectCapabilities,
1836
3125
  detectWebGLCapabilities,
3126
+ generateGpuMipChain,
1837
3127
  generateMipChain,
1838
3128
  getSharedWebGLContext,
3129
+ gpuMipLevelCount,
1839
3130
  isWebGLAvailable,
1840
3131
  padToBlockMultiple,
1841
3132
  rasterizeSvg,
3133
+ releaseSharedGpuResources,
1842
3134
  selectFormat,
1843
- selectWebGLFormat
3135
+ selectWebGLFormat,
3136
+ setTranscodeCacheLimit
1844
3137
  };