gputex 0.3.4 → 0.5.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,23 @@ 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 uploadSourceTexture(device, srcTex, source, width, height, flipY) {
48
+ if (source instanceof ImageData && !flipY) {
49
+ if (srcTex.format === "rg8unorm") {
50
+ const rgba = source.data;
51
+ const rg = new Uint8Array(width * height * 2);
52
+ for (let i = 0; i < width * height; i++) {
53
+ rg[i * 2] = rgba[i * 4];
54
+ rg[i * 2 + 1] = rgba[i * 4 + 1];
55
+ }
56
+ device.queue.writeTexture({ texture: srcTex }, rg, { bytesPerRow: width * 2 }, [width, height, 1]);
57
+ return;
58
+ }
47
59
  device.queue.writeTexture({ texture: srcTex }, source.data, { bytesPerRow: width * 4 }, [width, height, 1]);
48
60
  } else {
49
61
  device.queue.copyExternalImageToTexture({ source, flipY }, { texture: srcTex }, [
@@ -55,6 +67,7 @@ function uploadSourceTexture(device, srcTex, source, width, height, flipY, useWr
55
67
  }
56
68
 
57
69
  // src/Encoder.ts
70
+ var CHAIN_ALIGN = 256;
58
71
  var Encoder = class {
59
72
  /**
60
73
  * Subclasses set this to the WebGPU feature string the output texture
@@ -95,20 +108,16 @@ var Encoder = class {
95
108
  adapter;
96
109
  ownsDevice;
97
110
  disableF16;
98
- // f32 module — created lazily by `_ensureModule()`: when the f16 fast
99
- // module exists it serves the default path, so parsing/validating the
100
- // (larger, dual-quality) f32 source is deferred until 'high' or the
101
- // forced-f32 fallback is actually requested. Halves encoder construction
102
- // cost on f16 hardware.
103
- _module = null;
104
- // f16 'fast' module — built only when the device supports shader-f16 and the
105
- // subclass provides an f16 source. null otherwise (falls back to _module).
106
- _moduleF16 = null;
107
- _pipelineF16 = null;
108
- // Default pipeline (fast). Kept as a field for back-compat; the per-quality
109
- // cache below holds the specialised pipelines for encoders that support it.
110
- _pipeline;
111
- _pipelineCache = /* @__PURE__ */ new Map();
111
+ // The single compute pipeline: built from the f16 module when the device
112
+ // supports shader-f16 and the subclass provides an f16 source, from the
113
+ // f32 module otherwise. Both implement the same algorithm. Created with
114
+ // `createComputePipelineAsync` so shader compilation overlaps whatever
115
+ // follows construction (image decode, first upload) instead of stalling
116
+ // the first dispatch; encodes await readiness.
117
+ _pipelineReady;
118
+ // Source-preparation pipeline, when the subclass declares one (ETC2's
119
+ // packed-luma + quadrant-average split). Null for direct-source encoders.
120
+ _prepPipelineReady = null;
112
121
  // -------------------------------------------------------------------- //
113
122
  // Per-encoder GPU resource cache. Creating the source texture, output/
114
123
  // staging buffers and bind group on every encode costs ~1ms of host time
@@ -118,17 +127,45 @@ var Encoder = class {
118
127
  // these; concurrent encodes on the same encoder see `_resourcesBusy` and
119
128
  // fall back to transient resources, keeping the API contract unchanged.
120
129
  // Buffers are grow-only, the texture is recreated on size change, and the
121
- // bind group is cached per pipeline (with `layout: 'auto'` each pipeline
122
- // has its own layout) until any bound resource is recreated.
130
+ // bind group is kept until any bound resource is recreated.
123
131
  _cachedSrcTex = null;
124
132
  _cachedSrcW = 0;
125
133
  _cachedSrcH = 0;
134
+ // Upload memoisation: the ImageBitmap whose pixels the cached source
135
+ // texture currently holds. ImageBitmaps are immutable, so encoding the
136
+ // same bitmap again (benchmark loops, quality-ladder re-encodes, format
137
+ // A/B) can skip the copyExternalImageToTexture entirely — at 4096² that
138
+ // upload is ~9 ms, dominating the whole encode. Mutable sources
139
+ // (ImageData, canvases, video) are never memoised.
140
+ _cachedSrcSource = null;
141
+ _cachedSrcFlipY = false;
126
142
  _cachedDst = null;
127
143
  _cachedStaging = null;
128
144
  _cachedParams = null;
129
145
  _lastParams = null;
130
- _bindGroupCache = /* @__PURE__ */ new Map();
146
+ _cachedBindGroup = null;
147
+ _cachedPrepPlanes = null;
148
+ _cachedPrepBindGroup = null;
131
149
  _resourcesBusy = false;
150
+ /** Set when the active shader declares a @binding(3) sampler (the BC5
151
+ * kernels read texels through textureGather + clamp-to-edge). */
152
+ _usesSampler = false;
153
+ _sampler = null;
154
+ // Mip-chain cache — the `encodeMipChainToBytes()` counterpart of the
155
+ // single-shot cache above: per-level source textures + bind groups keyed
156
+ // on the exact level-size signature, one params buffer holding every
157
+ // level's uniforms at 256-byte offsets, grow-only output/staging buffers.
158
+ // Pays off when consecutive chains share dimensions (bulk-loading
159
+ // same-sized textures through `compressTexture()`).
160
+ _chainSig = null;
161
+ _chainTextures = [];
162
+ _chainPrepPlanes = [];
163
+ _chainPrepBindGroups = [];
164
+ _chainParams = null;
165
+ _chainBindGroups = [];
166
+ _chainDst = null;
167
+ _chainStaging = null;
168
+ _chainBusy = false;
132
169
  constructor({ device, adapter, ownsDevice = false, disableF16 = false }) {
133
170
  this.device = device;
134
171
  this.adapter = adapter;
@@ -138,77 +175,71 @@ var Encoder = class {
138
175
  }
139
176
  _buildPipeline() {
140
177
  const device = this.device;
141
- if (this._useF16) {
142
- this._moduleF16 = device.createShaderModule({
143
- label: `${this.label}-encoder-f16`,
144
- code: this.wgslSourceFastF16()
145
- });
146
- } else {
147
- this._ensureModule();
148
- }
149
- if (this.supportsQuality) {
150
- this._pipeline = this._getPipeline("fast");
151
- } else {
152
- this._pipeline = device.createComputePipeline({
153
- label: `${this.label}-encoder-pipeline`,
178
+ const useF16 = this._useF16;
179
+ const code = useF16 ? this.wgslSourceFastF16() : this.wgslSource();
180
+ this._usesSampler = /@binding\(3\)\s+var\s+\w+\s*:\s*sampler\s*;/.test(code);
181
+ const module = device.createShaderModule({
182
+ label: `${this.label}-encoder${useF16 ? "-f16" : ""}`,
183
+ code
184
+ });
185
+ const constants = this.pipelineConstants();
186
+ this._pipelineReady = device.createComputePipelineAsync({
187
+ label: `${this.label}-encoder-pipeline${useF16 ? "-f16" : ""}`,
188
+ layout: "auto",
189
+ compute: { module, entryPoint: "encode", ...constants ? { constants } : {} }
190
+ });
191
+ const prepCode = this.wgslPrepSource();
192
+ if (prepCode) {
193
+ const prepModule = device.createShaderModule({ label: `${this.label}-prep`, code: prepCode });
194
+ this._prepPipelineReady = device.createComputePipelineAsync({
195
+ label: `${this.label}-prep-pipeline`,
154
196
  layout: "auto",
155
- compute: { module: this._ensureModule(), entryPoint: "encode" }
197
+ compute: { module: prepModule, entryPoint: "encode" }
156
198
  });
157
- }
158
- }
159
- /** The f32 module, parsed on first use (see `_module`). */
160
- _ensureModule() {
161
- if (!this._module) {
162
- this._module = this.device.createShaderModule({
163
- label: `${this.label}-encoder`,
164
- code: this.wgslSource()
199
+ this._prepPipelineReady.catch(() => {
165
200
  });
166
201
  }
167
- return this._module;
168
- }
169
- /**
170
- * Pipeline for a given quality level. Encoders that don't declare a
171
- * `QUALITY_HIGH` override (`supportsQuality === false`, e.g. BC1) ignore the
172
- * argument and reuse the single pipeline. Specialised pipelines are cached.
173
- */
174
- _getPipeline(quality) {
175
- if (!this.supportsQuality) return this._pipeline;
176
- if (quality === "fast" && this._moduleF16) {
177
- if (!this._pipelineF16) {
178
- this._pipelineF16 = this.device.createComputePipeline({
179
- label: `${this.label}-encoder-pipeline-fast-f16`,
180
- layout: "auto",
181
- compute: { module: this._moduleF16, entryPoint: "encode" }
182
- });
183
- }
184
- return this._pipelineF16;
185
- }
186
- const cached = this._pipelineCache.get(quality);
187
- if (cached) return cached;
188
- const pipeline = this.device.createComputePipeline({
189
- label: `${this.label}-encoder-pipeline-${quality}`,
190
- layout: "auto",
191
- compute: {
192
- module: this._ensureModule(),
193
- entryPoint: "encode",
194
- constants: { QUALITY_HIGH: quality === "high" ? 1 : 0 }
195
- }
202
+ this._pipelineReady.catch(() => {
196
203
  });
197
- this._pipelineCache.set(quality, pipeline);
198
- return pipeline;
199
204
  }
200
205
  destroy() {
201
206
  this._cachedSrcTex?.destroy();
207
+ if (this._cachedPrepPlanes) for (const t of this._cachedPrepPlanes) t.destroy();
208
+ this._cachedPrepPlanes = null;
209
+ this._cachedPrepBindGroup = null;
202
210
  this._cachedDst?.destroy();
203
211
  this._cachedStaging?.destroy();
204
212
  this._cachedParams?.destroy();
205
213
  this._cachedSrcTex = null;
214
+ this._cachedSrcSource = null;
206
215
  this._cachedDst = null;
207
216
  this._cachedStaging = null;
208
217
  this._cachedParams = null;
209
- this._bindGroupCache.clear();
218
+ this._cachedBindGroup = null;
219
+ for (const tex of this._chainTextures) tex.destroy();
220
+ this._chainTextures = [];
221
+ for (const planes of this._chainPrepPlanes) for (const t of planes) t.destroy();
222
+ this._chainPrepPlanes = [];
223
+ this._chainPrepBindGroups = [];
224
+ this._chainParams?.destroy();
225
+ this._chainDst?.destroy();
226
+ this._chainStaging?.destroy();
227
+ this._chainParams = null;
228
+ this._chainDst = null;
229
+ this._chainStaging = null;
230
+ this._chainBindGroups = [];
231
+ this._chainSig = null;
210
232
  if (this.ownsDevice) this.device.destroy();
211
233
  }
234
+ /**
235
+ * Pipeline-creation override constants for the active shader, or
236
+ * undefined for none. Subclasses expose quality/behaviour toggles this
237
+ * way so the OFF state is dead-coded by the shader compiler instead of
238
+ * branched at runtime.
239
+ */
240
+ pipelineConstants() {
241
+ return void 0;
242
+ }
212
243
  /** WGSL `@workgroup_size` dimensions. Default 8×8×1. */
213
244
  get workgroupSize() {
214
245
  return [8, 8, 1];
@@ -218,25 +249,76 @@ var Encoder = class {
218
249
  return true;
219
250
  }
220
251
  /**
221
- * Whether the shader declares a `QUALITY_HIGH` pipeline-overridable constant
222
- * (i.e. has distinct fast/high search paths). Default false (e.g. a stub or a
223
- * format with a single path); BC1/BC5/BC7/ASTC override it to true.
252
+ * Format of the single-shot source texture the encode pass samples.
253
+ * Encoders that read only a channel subset can narrow it to cut DRAM
254
+ * traffic on the bandwidth-bound compute pass (BC5 reads rg8unorm — half
255
+ * the bytes of rgba8). Must be valid as a copyExternalImageToTexture
256
+ * destination and renderable. Chain encodes keep rgba8unorm regardless
257
+ * (their inputs are RGBA mip levels / texture views); the WGSL is
258
+ * format-agnostic (`texture_2d<f32>`), so mixing is byte-identical.
224
259
  */
225
- get supportsQuality() {
226
- return false;
260
+ get srcTextureFormat() {
261
+ return "rgba8unorm";
227
262
  }
228
263
  /**
229
- * Optional f16 WGSL for the 'fast' path. Used only when the device reports the
230
- * `shader-f16` feature; the format's f32 `wgslSource()` is the fallback and
231
- * `'high'` always uses it. Returns null when there's no f16 variant.
264
+ * Optional f16 WGSL variant. Used only when the device reports the
265
+ * `shader-f16` feature; the format's f32 `wgslSource()` is the automatic
266
+ * fallback. Returns null when there's no f16 variant.
232
267
  */
233
268
  wgslSourceFastF16() {
234
269
  return null;
235
270
  }
236
- /** Whether the f16 fast path is both available and supported on this device. */
271
+ /** Whether the f16 shader is both available and supported on this device. */
237
272
  get _useF16() {
238
273
  return !this.disableF16 && this.wgslSourceFastF16() !== null && this.device.features.has("shader-f16");
239
274
  }
275
+ // ------------------------------------------------------------------ //
276
+ // Optional source-preparation pass (null/empty for direct encoders). //
277
+ // A subclass returning a prep shader gets a two-pass encode: the prep
278
+ // pass reads the RGBA8 source (binding 0) and writes the prepared
279
+ // planes as storage textures (plane 0 at binding 1, plane 1 at
280
+ // binding 3, params at binding 2); the encode pass then reads plane 0
281
+ // at binding 0 and plane 1 at binding 3 instead of the source.
282
+ // ------------------------------------------------------------------ //
283
+ /** WGSL for the preparation pass, or null when the encoder reads the
284
+ * RGBA8 source directly. */
285
+ wgslPrepSource() {
286
+ return null;
287
+ }
288
+ /** Formats and sizes of the prepared planes for a padded source size. */
289
+ prepPlanes(paddedWidth, paddedHeight) {
290
+ void paddedWidth;
291
+ void paddedHeight;
292
+ return [];
293
+ }
294
+ /** Workgroup counts for the prep dispatch. */
295
+ prepDispatch(blocksX, blocksY) {
296
+ return [blocksX, blocksY];
297
+ }
298
+ /** Create the prepared-plane textures for one padded source size. */
299
+ _createPrepPlanes(paddedWidth, paddedHeight) {
300
+ return this.prepPlanes(paddedWidth, paddedHeight).map(
301
+ (p, i) => this.device.createTexture({
302
+ label: `${this.label}-prep-${i}`,
303
+ size: [p.width, p.height, 1],
304
+ format: p.format,
305
+ usage: GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.TEXTURE_BINDING
306
+ })
307
+ );
308
+ }
309
+ /** Bind group for one prep dispatch. */
310
+ _createPrepBindGroup(prepPipeline, srcView, planes, params, paramsOffset = 0) {
311
+ return this.device.createBindGroup({
312
+ label: `${this.label}-prep-bg`,
313
+ layout: prepPipeline.getBindGroupLayout(0),
314
+ entries: [
315
+ { binding: 0, resource: srcView },
316
+ { binding: 1, resource: planes[0].createView() },
317
+ { binding: 2, resource: { buffer: params, offset: paramsOffset, size: 16 } },
318
+ { binding: 3, resource: planes[1].createView() }
319
+ ]
320
+ });
321
+ }
240
322
  /**
241
323
  * True if the device reports the feature the output texture needs.
242
324
  * The encoder itself only writes to a storage buffer, so this is about
@@ -256,11 +338,7 @@ var Encoder = class {
256
338
  * bytes into a `CompressedTexture`; callers targeting another engine feed
257
339
  * `data` into that engine's compressed-texture upload directly.
258
340
  */
259
- async encodeToBytes(source, {
260
- flipY = false,
261
- quality = "fast",
262
- withGpuTime = false
263
- } = {}) {
341
+ async encodeToBytes(source, { flipY = false, withGpuTime = false } = {}) {
264
342
  const device = this.device;
265
343
  const width = source.width;
266
344
  const height = source.height;
@@ -273,12 +351,15 @@ var Encoder = class {
273
351
  const blocksY = paddedHeight >> 2;
274
352
  const blockCount = blocksX * blocksY;
275
353
  const outByteLen = blockCount * this.bytesPerBlock;
354
+ const pipeline = await this._pipelineReady;
355
+ const prepPipeline = this._prepPipelineReady ? await this._prepPipelineReady : null;
276
356
  const useCache = !this._resourcesBusy;
277
357
  if (useCache) this._resourcesBusy = true;
278
358
  let srcTex;
279
359
  let dstBuffer;
280
360
  let paramsBuffer;
281
361
  let staging;
362
+ let transientPrepPlanes = null;
282
363
  try {
283
364
  let srcTexIsNew = true;
284
365
  if (useCache && this._cachedSrcTex && this._cachedSrcW === paddedWidth && this._cachedSrcH === paddedHeight) {
@@ -288,7 +369,7 @@ var Encoder = class {
288
369
  srcTex = device.createTexture({
289
370
  label: `${this.label}-src`,
290
371
  size: [paddedWidth, paddedHeight, 1],
291
- format: "rgba8unorm",
372
+ format: this.srcTextureFormat,
292
373
  // RENDER_ATTACHMENT is required by copyExternalImageToTexture
293
374
  // (internally a blit) even though we never render into this texture.
294
375
  usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.RENDER_ATTACHMENT
@@ -300,7 +381,31 @@ var Encoder = class {
300
381
  this._cachedSrcH = paddedHeight;
301
382
  }
302
383
  }
303
- uploadSourceTexture(device, srcTex, source, width, height, flipY, source instanceof ImageData);
384
+ let prepPlanes = null;
385
+ let prepPlanesNew = false;
386
+ if (prepPipeline) {
387
+ if (useCache && !srcTexIsNew && this._cachedPrepPlanes) {
388
+ prepPlanes = this._cachedPrepPlanes;
389
+ } else {
390
+ prepPlanes = this._createPrepPlanes(paddedWidth, paddedHeight);
391
+ prepPlanesNew = true;
392
+ if (useCache) {
393
+ if (this._cachedPrepPlanes) for (const t of this._cachedPrepPlanes) t.destroy();
394
+ this._cachedPrepPlanes = prepPlanes;
395
+ this._cachedPrepBindGroup = null;
396
+ } else {
397
+ transientPrepPlanes = prepPlanes;
398
+ }
399
+ }
400
+ }
401
+ const uploadSkippable = !srcTexIsNew && source instanceof ImageBitmap && this._cachedSrcSource === source && this._cachedSrcFlipY === flipY;
402
+ if (!uploadSkippable) {
403
+ uploadSourceTexture(device, srcTex, source, width, height, flipY);
404
+ }
405
+ if (useCache) {
406
+ this._cachedSrcSource = source instanceof ImageBitmap ? source : null;
407
+ this._cachedSrcFlipY = flipY;
408
+ }
304
409
  let dstIsNew = true;
305
410
  if (useCache && this._cachedDst && this._cachedDst.size >= outByteLen) {
306
411
  dstBuffer = this._cachedDst;
@@ -352,63 +457,74 @@ var Encoder = class {
352
457
  });
353
458
  device.queue.writeBuffer(paramsBuffer, 0, new Uint32Array([blocksX, blocksY, width, height]));
354
459
  }
355
- const pipeline = this._getPipeline(quality);
356
- if (useCache && (srcTexIsNew || dstIsNew)) this._bindGroupCache.clear();
357
- let bindGroup = useCache ? this._bindGroupCache.get(pipeline) : void 0;
460
+ if (useCache && (srcTexIsNew || dstIsNew || prepPlanesNew)) this._cachedBindGroup = null;
461
+ let bindGroup = useCache ? this._cachedBindGroup : null;
358
462
  if (!bindGroup) {
463
+ const entries = [
464
+ { binding: 0, resource: prepPlanes ? prepPlanes[0].createView() : srcTex.createView() },
465
+ { binding: 1, resource: { buffer: dstBuffer } },
466
+ { binding: 2, resource: { buffer: paramsBuffer } }
467
+ ];
468
+ if (prepPlanes) {
469
+ entries.push({ binding: 3, resource: prepPlanes[1].createView() });
470
+ } else if (this._usesSampler) {
471
+ this._sampler ??= device.createSampler({
472
+ label: `${this.label}-clamp-sampler`,
473
+ addressModeU: "clamp-to-edge",
474
+ addressModeV: "clamp-to-edge"
475
+ });
476
+ entries.push({ binding: 3, resource: this._sampler });
477
+ }
359
478
  bindGroup = device.createBindGroup({
360
479
  label: `${this.label}-bg`,
361
480
  layout: pipeline.getBindGroupLayout(0),
362
- entries: [
363
- { binding: 0, resource: srcTex.createView() },
364
- { binding: 1, resource: { buffer: dstBuffer } },
365
- { binding: 2, resource: { buffer: paramsBuffer } }
366
- ]
481
+ entries
367
482
  });
368
- if (useCache) this._bindGroupCache.set(pipeline, bindGroup);
483
+ if (useCache) this._cachedBindGroup = bindGroup;
369
484
  }
370
- const useTimestamps = withGpuTime && device.features.has("timestamp-query");
371
- const querySet = useTimestamps ? device.createQuerySet({ type: "timestamp", count: 2 }) : null;
372
- const queryBuffer = useTimestamps ? device.createBuffer({
373
- label: `${this.label}-ts-resolve`,
374
- size: 16,
375
- usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC
376
- }) : null;
485
+ let prepBindGroup = useCache && !prepPlanesNew && !srcTexIsNew ? this._cachedPrepBindGroup : null;
486
+ if (prepPipeline && prepPlanes && !prepBindGroup) {
487
+ prepBindGroup = this._createPrepBindGroup(prepPipeline, srcTex.createView(), prepPlanes, paramsBuffer);
488
+ if (useCache) this._cachedPrepBindGroup = prepBindGroup;
489
+ }
490
+ const timing = withGpuTime ? this._createTiming() : null;
377
491
  const [wgX, wgY] = this.workgroupSize;
378
492
  const t0 = performance.now();
379
493
  const enc = device.createCommandEncoder({ label: `${this.label}-encode` });
494
+ if (prepPipeline && prepBindGroup) {
495
+ const prepPass = enc.beginComputePass(
496
+ timing ? { timestampWrites: { querySet: timing.querySet, beginningOfPassWriteIndex: 0 } } : void 0
497
+ );
498
+ prepPass.setPipeline(prepPipeline);
499
+ prepPass.setBindGroup(0, prepBindGroup);
500
+ const [px, py] = this.prepDispatch(blocksX, blocksY);
501
+ prepPass.dispatchWorkgroups(Math.ceil(px / wgX), Math.ceil(py / wgY), 1);
502
+ prepPass.end();
503
+ }
380
504
  const pass = enc.beginComputePass(
381
- querySet ? { timestampWrites: { querySet, beginningOfPassWriteIndex: 0, endOfPassWriteIndex: 1 } } : void 0
505
+ timing ? {
506
+ timestampWrites: {
507
+ querySet: timing.querySet,
508
+ ...prepPipeline ? {} : { beginningOfPassWriteIndex: 0 },
509
+ endOfPassWriteIndex: 1
510
+ }
511
+ } : void 0
382
512
  );
383
513
  pass.setPipeline(pipeline);
384
514
  pass.setBindGroup(0, bindGroup);
385
515
  pass.dispatchWorkgroups(Math.ceil(blocksX / wgX), Math.ceil(blocksY / wgY), 1);
386
516
  pass.end();
387
- if (querySet && queryBuffer) enc.resolveQuerySet(querySet, 0, 2, queryBuffer, 0);
388
517
  enc.copyBufferToBuffer(dstBuffer, 0, staging, 0, outByteLen);
389
- const tsStaging = querySet && queryBuffer ? device.createBuffer({
390
- label: `${this.label}-ts-staging`,
391
- size: 16,
392
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
393
- }) : null;
394
- if (tsStaging && queryBuffer) enc.copyBufferToBuffer(queryBuffer, 0, tsStaging, 0, 16);
518
+ if (timing) {
519
+ enc.resolveQuerySet(timing.querySet, 0, 2, timing.resolve, 0);
520
+ enc.copyBufferToBuffer(timing.resolve, 0, timing.staging, 0, 16);
521
+ }
395
522
  device.queue.submit([enc.finish()]);
396
523
  await staging.mapAsync(GPUMapMode.READ, 0, outByteLen);
397
524
  const data = new Uint8Array(staging.getMappedRange(0, outByteLen).slice(0));
398
525
  staging.unmap();
399
526
  const encodeMs = performance.now() - t0;
400
- let gpuMs;
401
- if (tsStaging) {
402
- await tsStaging.mapAsync(GPUMapMode.READ);
403
- const [begin, end] = new BigUint64Array(tsStaging.getMappedRange().slice(0));
404
- tsStaging.unmap();
405
- tsStaging.destroy();
406
- if (end !== void 0 && begin !== void 0 && end > begin) {
407
- gpuMs = Number(end - begin) / 1e6;
408
- }
409
- }
410
- querySet?.destroy();
411
- queryBuffer?.destroy();
527
+ const gpuMs = timing ? await this._readTimingMs(timing) : void 0;
412
528
  return { width, height, paddedWidth, paddedHeight, data, encodeMs, gpuMs };
413
529
  } finally {
414
530
  if (useCache) {
@@ -418,13 +534,441 @@ var Encoder = class {
418
534
  dstBuffer?.destroy();
419
535
  staging?.destroy();
420
536
  paramsBuffer?.destroy();
537
+ if (transientPrepPlanes) for (const t of transientPrepPlanes) t.destroy();
538
+ }
539
+ }
540
+ }
541
+ // ------------------------------------------------------------------ //
542
+ // Whole-chain encode — every level in one submission. //
543
+ // ------------------------------------------------------------------ //
544
+ /**
545
+ * Encode a whole mip chain in ONE GPU submission. A per-level
546
+ * `encodeToBytes()` loop costs a full CPU↔GPU round trip per level (a
547
+ * 1024² chain is 11 levels → 11 `mapAsync` waits with the GPU idle in
548
+ * between); this path uploads every level, records one dispatch per level
549
+ * into a single compute pass, copies all outputs into one staging buffer
550
+ * and maps it once.
551
+ *
552
+ * `levels` are raw RGBA8 pixels in base-to-tail order; sizes don't have to
553
+ * halve level-to-level (each level is padded and clamped independently,
554
+ * exactly like `encodeToBytes`). Uploads use `writeTexture` — the direct
555
+ * raw-bytes path, which also sidesteps the broken
556
+ * `copyExternalImageToTexture` devices (see workarounds.ts). There is no
557
+ * flip option: bake any vertical flip into level 0 before generating the
558
+ * chain, as `compressTexture()` does.
559
+ */
560
+ async encodeMipChainToBytes(levels, { withGpuTime = false } = {}) {
561
+ const device = this.device;
562
+ if (levels.length === 0) {
563
+ throw new Error(`${this.label}Encoder: encodeMipChainToBytes needs at least one level`);
564
+ }
565
+ const pipeline = await this._pipelineReady;
566
+ const prepPipeline = this._prepPipelineReady ? await this._prepPipelineReady : null;
567
+ const t0 = performance.now();
568
+ levels.forEach((level, i) => {
569
+ if (!level.width || !level.height) {
570
+ throw new Error(`${this.label}Encoder: mip level ${i} has no dimensions`);
571
+ }
572
+ if (level.data.length < level.width * level.height * 4) {
573
+ throw new Error(
574
+ `${this.label}Encoder: mip level ${i} has ${level.data.length} bytes, expected ${level.width * level.height * 4}`
575
+ );
576
+ }
577
+ });
578
+ const { geoms, byteSpan } = this._chainGeometry(levels);
579
+ const sig = geoms.map((g) => `${g.width}x${g.height}`).join();
580
+ const useCache = !this._chainBusy;
581
+ if (useCache) this._chainBusy = true;
582
+ let textures;
583
+ let prepPlaneSets;
584
+ let prepBindGroups;
585
+ let params;
586
+ let bindGroups;
587
+ let dst;
588
+ let staging;
589
+ let transientLevelSet = false;
590
+ try {
591
+ let dstIsNew = true;
592
+ if (useCache && this._chainDst && this._chainDst.size >= byteSpan) {
593
+ dst = this._chainDst;
594
+ dstIsNew = false;
595
+ } else {
596
+ dst = device.createBuffer({
597
+ label: `${this.label}-chain-dst`,
598
+ size: byteSpan,
599
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
600
+ });
601
+ if (useCache) {
602
+ this._chainDst?.destroy();
603
+ this._chainDst = dst;
604
+ }
605
+ }
606
+ if (useCache && !dstIsNew && this._chainSig === sig && this._chainParams) {
607
+ textures = this._chainTextures;
608
+ params = this._chainParams;
609
+ bindGroups = this._chainBindGroups;
610
+ prepPlaneSets = this._chainPrepPlanes;
611
+ prepBindGroups = this._chainPrepBindGroups;
612
+ } else {
613
+ const texs = geoms.map(
614
+ (g, i) => device.createTexture({
615
+ label: `${this.label}-chain-src-${i}`,
616
+ size: [g.paddedWidth, g.paddedHeight, 1],
617
+ format: "rgba8unorm",
618
+ // No RENDER_ATTACHMENT: writeTexture is a plain copy, not the
619
+ // copyExternalImageToTexture blit.
620
+ usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING
621
+ })
622
+ );
623
+ textures = texs;
624
+ params = device.createBuffer({
625
+ label: `${this.label}-chain-params`,
626
+ size: geoms.length * CHAIN_ALIGN,
627
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
628
+ });
629
+ const paramsData = new Uint32Array(geoms.length * CHAIN_ALIGN / 4);
630
+ geoms.forEach((g, i) => {
631
+ paramsData.set([g.blocksX, g.blocksY, g.width, g.height], i * CHAIN_ALIGN / 4);
632
+ });
633
+ device.queue.writeBuffer(params, 0, paramsData);
634
+ if (this._usesSampler) {
635
+ this._sampler ??= device.createSampler({
636
+ label: `${this.label}-clamp-sampler`,
637
+ addressModeU: "clamp-to-edge",
638
+ addressModeV: "clamp-to-edge"
639
+ });
640
+ }
641
+ const dstBuf = dst;
642
+ const paramsBuf = params;
643
+ const planeSets = prepPipeline ? geoms.map((g) => this._createPrepPlanes(g.paddedWidth, g.paddedHeight)) : [];
644
+ prepPlaneSets = planeSets;
645
+ prepBindGroups = prepPipeline ? geoms.map(
646
+ (g, i) => this._createPrepBindGroup(prepPipeline, texs[i].createView(), planeSets[i], paramsBuf, i * CHAIN_ALIGN)
647
+ ) : [];
648
+ bindGroups = geoms.map((g, i) => {
649
+ const entries = [
650
+ {
651
+ binding: 0,
652
+ resource: prepPipeline ? planeSets[i][0].createView() : texs[i].createView()
653
+ },
654
+ { binding: 1, resource: { buffer: dstBuf, offset: g.dstOffset, size: g.byteLen } },
655
+ { binding: 2, resource: { buffer: paramsBuf, offset: i * CHAIN_ALIGN, size: 16 } }
656
+ ];
657
+ if (prepPipeline) {
658
+ entries.push({ binding: 3, resource: planeSets[i][1].createView() });
659
+ } else if (this._usesSampler) {
660
+ entries.push({ binding: 3, resource: this._sampler });
661
+ }
662
+ return device.createBindGroup({
663
+ label: `${this.label}-chain-bg-${i}`,
664
+ layout: pipeline.getBindGroupLayout(0),
665
+ entries
666
+ });
667
+ });
668
+ if (useCache) {
669
+ for (const tex of this._chainTextures) tex.destroy();
670
+ for (const planes of this._chainPrepPlanes) for (const t of planes) t.destroy();
671
+ this._chainParams?.destroy();
672
+ this._chainTextures = textures;
673
+ this._chainPrepPlanes = planeSets;
674
+ this._chainPrepBindGroups = prepBindGroups;
675
+ this._chainParams = params;
676
+ this._chainBindGroups = bindGroups;
677
+ this._chainSig = sig;
678
+ } else {
679
+ transientLevelSet = true;
680
+ }
681
+ }
682
+ if (useCache && this._chainStaging && this._chainStaging.size >= byteSpan) {
683
+ staging = this._chainStaging;
684
+ } else {
685
+ staging = device.createBuffer({
686
+ label: `${this.label}-chain-staging`,
687
+ size: byteSpan,
688
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
689
+ });
690
+ if (useCache) {
691
+ this._chainStaging?.destroy();
692
+ this._chainStaging = staging;
693
+ }
694
+ }
695
+ for (let i = 0; i < levels.length; i++) {
696
+ const level = levels[i];
697
+ device.queue.writeTexture({ texture: textures[i] }, level.data, { bytesPerRow: level.width * 4 }, [
698
+ level.width,
699
+ level.height,
700
+ 1
701
+ ]);
702
+ }
703
+ return await this._submitChainAndRead(
704
+ pipeline,
705
+ geoms,
706
+ byteSpan,
707
+ bindGroups,
708
+ dst,
709
+ staging,
710
+ withGpuTime,
711
+ t0,
712
+ prepPipeline && prepBindGroups ? { pipeline: prepPipeline, bindGroups: prepBindGroups } : null
713
+ );
714
+ } finally {
715
+ if (useCache) {
716
+ this._chainBusy = false;
717
+ } else {
718
+ dst?.destroy();
719
+ staging?.destroy();
720
+ }
721
+ if (transientLevelSet) {
722
+ if (textures) for (const tex of textures) tex.destroy();
723
+ if (prepPlaneSets) for (const planes of prepPlaneSets) for (const t of planes) t.destroy();
724
+ params?.destroy();
725
+ }
726
+ }
727
+ }
728
+ /**
729
+ * Encode every mip level of a GPU-resident texture in one submission —
730
+ * the zero-CPU-pixels counterpart of `encodeMipChainToBytes()`. Pair it
731
+ * with `generateGpuMipChain()` (gpuMipgen.ts): upload the image once,
732
+ * box-filter the chain on the GPU, then encode straight from the
733
+ * texture's mip views. Pixels never transit the CPU between the source
734
+ * image and the compressed-bytes readback.
735
+ *
736
+ * `srcTex` must be `rgba8unorm` with TEXTURE_BINDING usage; level 0's
737
+ * dimensions are taken from the texture and lower levels follow the
738
+ * standard floor-halving chain. Encoded output is identical to feeding
739
+ * the equivalent CPU chain to `encodeMipChainToBytes()`.
740
+ */
741
+ async encodeMipChainFromTexture(srcTex, { withGpuTime = false } = {}) {
742
+ const device = this.device;
743
+ const pipeline = await this._pipelineReady;
744
+ const prepPipeline = this._prepPipelineReady ? await this._prepPipelineReady : null;
745
+ const t0 = performance.now();
746
+ const dims = [];
747
+ for (let i = 0; i < srcTex.mipLevelCount; i++) {
748
+ dims.push({ width: Math.max(1, srcTex.width >> i), height: Math.max(1, srcTex.height >> i) });
749
+ }
750
+ const { geoms, byteSpan } = this._chainGeometry(dims);
751
+ const useCache = !this._chainBusy;
752
+ if (useCache) this._chainBusy = true;
753
+ let dst;
754
+ let staging;
755
+ let params;
756
+ let planeSets = null;
757
+ try {
758
+ if (useCache && this._chainDst && this._chainDst.size >= byteSpan) {
759
+ dst = this._chainDst;
760
+ } else {
761
+ dst = device.createBuffer({
762
+ label: `${this.label}-chain-dst`,
763
+ size: byteSpan,
764
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
765
+ });
766
+ if (useCache) {
767
+ this._chainDst?.destroy();
768
+ this._chainDst = dst;
769
+ this._chainSig = null;
770
+ }
771
+ }
772
+ if (useCache && this._chainStaging && this._chainStaging.size >= byteSpan) {
773
+ staging = this._chainStaging;
774
+ } else {
775
+ staging = device.createBuffer({
776
+ label: `${this.label}-chain-staging`,
777
+ size: byteSpan,
778
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
779
+ });
780
+ if (useCache) {
781
+ this._chainStaging?.destroy();
782
+ this._chainStaging = staging;
783
+ }
784
+ }
785
+ params = device.createBuffer({
786
+ label: `${this.label}-chain-params`,
787
+ size: geoms.length * CHAIN_ALIGN,
788
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
789
+ });
790
+ const paramsData = new Uint32Array(geoms.length * CHAIN_ALIGN / 4);
791
+ geoms.forEach((g, i) => {
792
+ paramsData.set([g.blocksX, g.blocksY, g.width, g.height], i * CHAIN_ALIGN / 4);
793
+ });
794
+ device.queue.writeBuffer(params, 0, paramsData);
795
+ if (this._usesSampler) {
796
+ this._sampler ??= device.createSampler({
797
+ label: `${this.label}-clamp-sampler`,
798
+ addressModeU: "clamp-to-edge",
799
+ addressModeV: "clamp-to-edge"
800
+ });
801
+ }
802
+ const dstBuf = dst;
803
+ const paramsBuf = params;
804
+ planeSets = prepPipeline ? geoms.map((g) => this._createPrepPlanes(g.paddedWidth, g.paddedHeight)) : null;
805
+ const planes = planeSets;
806
+ const prepBindGroups = prepPipeline && planes ? geoms.map(
807
+ (_, i) => this._createPrepBindGroup(
808
+ prepPipeline,
809
+ srcTex.createView({ baseMipLevel: i, mipLevelCount: 1 }),
810
+ planes[i],
811
+ paramsBuf,
812
+ i * CHAIN_ALIGN
813
+ )
814
+ ) : null;
815
+ const bindGroups = geoms.map((g, i) => {
816
+ const entries = [
817
+ {
818
+ binding: 0,
819
+ resource: planes ? planes[i][0].createView() : srcTex.createView({ baseMipLevel: i, mipLevelCount: 1 })
820
+ },
821
+ { binding: 1, resource: { buffer: dstBuf, offset: g.dstOffset, size: g.byteLen } },
822
+ { binding: 2, resource: { buffer: paramsBuf, offset: i * CHAIN_ALIGN, size: 16 } }
823
+ ];
824
+ if (planes) {
825
+ entries.push({ binding: 3, resource: planes[i][1].createView() });
826
+ } else if (this._usesSampler) {
827
+ entries.push({ binding: 3, resource: this._sampler });
828
+ }
829
+ return device.createBindGroup({
830
+ label: `${this.label}-chain-bg-${i}`,
831
+ layout: pipeline.getBindGroupLayout(0),
832
+ entries
833
+ });
834
+ });
835
+ return await this._submitChainAndRead(
836
+ pipeline,
837
+ geoms,
838
+ byteSpan,
839
+ bindGroups,
840
+ dst,
841
+ staging,
842
+ withGpuTime,
843
+ t0,
844
+ prepPipeline && prepBindGroups ? { pipeline: prepPipeline, bindGroups: prepBindGroups } : null
845
+ );
846
+ } finally {
847
+ if (useCache) {
848
+ this._chainBusy = false;
849
+ } else {
850
+ dst?.destroy();
851
+ staging?.destroy();
852
+ }
853
+ params?.destroy();
854
+ if (planeSets) for (const planes of planeSets) for (const t of planes) t.destroy();
855
+ }
856
+ }
857
+ /** Block-grid geometry + packed output offsets for a chain of levels.
858
+ * `byteSpan` is both the dst buffer size and the readback copy size (a
859
+ * multiple of 4: byteLen is a multiple of bytesPerBlock ≥ 8, offsets are
860
+ * CHAIN_ALIGN-ed). */
861
+ _chainGeometry(dims) {
862
+ let dstCursor = 0;
863
+ const geoms = dims.map(({ width, height }) => {
864
+ const paddedWidth = width + 3 & ~3;
865
+ const paddedHeight = height + 3 & ~3;
866
+ const blocksX = paddedWidth >> 2;
867
+ const blocksY = paddedHeight >> 2;
868
+ const byteLen = blocksX * blocksY * this.bytesPerBlock;
869
+ const dstOffset = dstCursor;
870
+ dstCursor = Math.ceil((dstCursor + byteLen) / CHAIN_ALIGN) * CHAIN_ALIGN;
871
+ return { width, height, paddedWidth, paddedHeight, blocksX, blocksY, byteLen, dstOffset };
872
+ });
873
+ const last = geoms[geoms.length - 1];
874
+ return { geoms, byteSpan: last.dstOffset + last.byteLen };
875
+ }
876
+ /** Shared chain-encode tail: one compute pass with a dispatch per level,
877
+ * one submit, one staging readback sliced into per-level byte arrays. */
878
+ async _submitChainAndRead(pipeline, geoms, byteSpan, bindGroups, dst, staging, withGpuTime, t0, prep = null) {
879
+ const device = this.device;
880
+ const timing = withGpuTime ? this._createTiming() : null;
881
+ const [wgX, wgY] = this.workgroupSize;
882
+ const enc = device.createCommandEncoder({ label: `${this.label}-encode-chain` });
883
+ if (prep) {
884
+ const prepPass = enc.beginComputePass(
885
+ timing ? { timestampWrites: { querySet: timing.querySet, beginningOfPassWriteIndex: 0 } } : void 0
886
+ );
887
+ prepPass.setPipeline(prep.pipeline);
888
+ for (let i = 0; i < geoms.length; i++) {
889
+ const g = geoms[i];
890
+ prepPass.setBindGroup(0, prep.bindGroups[i]);
891
+ const [px, py] = this.prepDispatch(g.blocksX, g.blocksY);
892
+ prepPass.dispatchWorkgroups(Math.ceil(px / wgX), Math.ceil(py / wgY), 1);
421
893
  }
894
+ prepPass.end();
895
+ }
896
+ const pass = enc.beginComputePass(
897
+ timing ? {
898
+ timestampWrites: {
899
+ querySet: timing.querySet,
900
+ ...prep ? {} : { beginningOfPassWriteIndex: 0 },
901
+ endOfPassWriteIndex: 1
902
+ }
903
+ } : void 0
904
+ );
905
+ pass.setPipeline(pipeline);
906
+ for (let i = 0; i < geoms.length; i++) {
907
+ const g = geoms[i];
908
+ pass.setBindGroup(0, bindGroups[i]);
909
+ pass.dispatchWorkgroups(Math.ceil(g.blocksX / wgX), Math.ceil(g.blocksY / wgY), 1);
910
+ }
911
+ pass.end();
912
+ enc.copyBufferToBuffer(dst, 0, staging, 0, byteSpan);
913
+ if (timing) {
914
+ enc.resolveQuerySet(timing.querySet, 0, 2, timing.resolve, 0);
915
+ enc.copyBufferToBuffer(timing.resolve, 0, timing.staging, 0, 16);
916
+ }
917
+ device.queue.submit([enc.finish()]);
918
+ await staging.mapAsync(GPUMapMode.READ, 0, byteSpan);
919
+ const mapped = staging.getMappedRange(0, byteSpan);
920
+ const out = geoms.map((g) => ({
921
+ width: g.width,
922
+ height: g.height,
923
+ paddedWidth: g.paddedWidth,
924
+ paddedHeight: g.paddedHeight,
925
+ data: new Uint8Array(mapped.slice(g.dstOffset, g.dstOffset + g.byteLen))
926
+ }));
927
+ staging.unmap();
928
+ const encodeMs = performance.now() - t0;
929
+ const gpuMs = timing ? await this._readTimingMs(timing) : void 0;
930
+ return { levels: out, encodeMs, gpuMs };
931
+ }
932
+ // ------------------------------------------------------------------ //
933
+ // GPU timing plumbing (timestamp queries) //
934
+ // ------------------------------------------------------------------ //
935
+ /** Create the query set + resolve/staging buffers for one timed
936
+ * submission, or null when the device lacks 'timestamp-query'. */
937
+ _createTiming() {
938
+ const device = this.device;
939
+ if (!device.features.has("timestamp-query")) return null;
940
+ return {
941
+ querySet: device.createQuerySet({ type: "timestamp", count: 2 }),
942
+ resolve: device.createBuffer({
943
+ label: `${this.label}-ts-resolve`,
944
+ size: 16,
945
+ usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC
946
+ }),
947
+ staging: device.createBuffer({
948
+ label: `${this.label}-ts-staging`,
949
+ size: 16,
950
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
951
+ })
952
+ };
953
+ }
954
+ /** Read back a timed submission's pass duration (ms) and destroy the
955
+ * timing objects. Timestamps are u64 nanoseconds. */
956
+ async _readTimingMs(timing) {
957
+ await timing.staging.mapAsync(GPUMapMode.READ);
958
+ const [begin, end] = new BigUint64Array(timing.staging.getMappedRange().slice(0));
959
+ timing.staging.unmap();
960
+ timing.staging.destroy();
961
+ timing.resolve.destroy();
962
+ timing.querySet.destroy();
963
+ if (end !== void 0 && begin !== void 0 && end > begin) {
964
+ return Number(end - begin) / 1e6;
422
965
  }
966
+ return void 0;
423
967
  }
424
968
  };
425
969
 
426
970
  // src/bc1.wgsl
427
- 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.\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// QUALITY LEVELS (pipeline-overridable constant `QUALITY_HIGH`)\n// fast (0, default): principal-axis endpoint seed (the high path's\n// covariance power-iteration; inset bbox on degenerate blocks), inset by\n// ~half a 565 cell along the axis, then ONE fused pass that projects\n// every pixel onto the decoded-endpoint line (the 4 palette entries are\n// colinear and evenly spaced, so the nearest entry is the rounded\n// projection \u2014 no 4-entry search) while accumulating the least-squares\n// refit sums; the refit endpoints are re-quantised and a final projection\n// pass assigns the indices, packed on the fly.\n// high (1): endpoints are seeded from the block's principal colour axis\n// (covariance power-iteration) as well as the bbox diagonal, each refined by\n// several least-squares passes with full 4-entry searches; the lower-error\n// family wins. Mirrors bc1_ref.ts.\n\n// 0 = fast (default), 1 = high-quality. Set via pipeline constants.\noverride QUALITY_HIGH: u32 = 0u;\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// 4-color-mode interpolation weights: palette[j] = wa(j)*c0 + wb(j)*c1.\nfn wa(j: u32) -> f32 {\n switch j {\n case 0u: { return 1.0; }\n case 1u: { return 0.0; }\n case 2u: { return 2.0 / 3.0; }\n default: { return 1.0 / 3.0; } // case 3u\n }\n}\nfn wb(j: u32) -> f32 {\n switch j {\n case 0u: { return 0.0; }\n case 1u: { return 1.0; }\n case 2u: { return 1.0 / 3.0; }\n default: { return 2.0 / 3.0; } // case 3u\n }\n}\n\nfn build_palette(c0: u32, c1: u32, pal: ptr<function, array<vec3<f32>, 4>>) {\n let p0 = from565(c0);\n let p1 = from565(c1);\n for (var j: u32 = 0u; j < 4u; j = j + 1u) {\n (*pal)[j] = wa(j) * p0 + wb(j) * p1;\n }\n}\n\n// Assign each of the 16 pixels its nearest palette entry (full 4-entry L2),\n// writing indices into `out_idx` and returning the total squared error.\nfn assign_indices(\n pixels: ptr<function, array<vec3<f32>, 16>>,\n pal: ptr<function, array<vec3<f32>, 4>>,\n out_idx: ptr<function, array<u32, 16>>,\n) -> f32 {\n var err: f32 = 0.0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let c = (*pixels)[k];\n var best_j: u32 = 0u;\n var best_d: f32 = 1e30;\n for (var j: u32 = 0u; j < 4u; j = j + 1u) {\n let d = (*pal)[j] - c;\n let d2 = dot(d, d);\n if (d2 < best_d) {\n best_d = d2;\n best_j = j;\n }\n }\n (*out_idx)[k] = best_j;\n err = err + best_d;\n }\n return err;\n}\n\n// One least-squares refit pass: solve the 2x2 normal equations for the endpoint\n// colours that minimise \u03A3\u2016wa\xB7e0 + wb\xB7e1 \u2212 c\u2016\xB2 under the current indices. The\n// three channels share the scalar sums, so it's one 2x2 solve with vec3 RHS.\nstruct RefitResult { e0: vec3<f32>, e1: vec3<f32>, valid: bool };\nfn refit(\n pixels: ptr<function, array<vec3<f32>, 16>>,\n indices: ptr<function, array<u32, 16>>,\n) -> RefitResult {\n var sAA: f32 = 0.0; var sBB: f32 = 0.0; var sAB: f32 = 0.0;\n var sAV: vec3<f32> = vec3<f32>(0.0);\n var sBV: vec3<f32> = vec3<f32>(0.0);\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let a = wa((*indices)[k]);\n let b = wb((*indices)[k]);\n let v = (*pixels)[k];\n sAA = sAA + a * a;\n sBB = sBB + b * b;\n sAB = sAB + a * b;\n sAV = sAV + a * v;\n sBV = sBV + b * v;\n }\n var out: RefitResult;\n let det = sAA * sBB - sAB * sAB;\n if (abs(det) < 1e-9) {\n out.valid = false;\n return out;\n }\n out.e0 = clamp((sBB * sAV - sAB * sBV) / det, vec3<f32>(0.0), vec3<f32>(1.0));\n out.e1 = clamp((sAA * sBV - sAB * sAV) / det, vec3<f32>(0.0), vec3<f32>(1.0));\n out.valid = true;\n return out;\n}\n\n// Candidate solution tracked across endpoint seeds / refit passes.\nstruct Best { c0: u32, c1: u32, indices: array<u32, 16>, err: f32 };\n\n// Quantize (hi, lo) to 565, force 4-color mode, assign indices, then refine with\n// up to `max_refits` least-squares passes. Commits to `*best` only on strict\n// improvement.\nfn fit_from_endpoints(\n pixels: ptr<function, array<vec3<f32>, 16>>,\n hi: vec3<f32>,\n lo: vec3<f32>,\n max_refits: u32,\n best: ptr<function, Best>,\n) {\n var c0 = to565(hi);\n var c1 = to565(lo);\n // 4-color 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 let t = c0; c0 = c1; c1 = t;\n }\n\n var pal: array<vec3<f32>, 4>;\n var idx: array<u32, 16>;\n build_palette(c0, c1, &pal);\n var err = assign_indices(pixels, &pal, &idx);\n if (err < (*best).err) {\n (*best).c0 = c0; (*best).c1 = c1; (*best).indices = idx; (*best).err = err;\n }\n\n for (var rp: u32 = 0u; rp < max_refits; rp = rp + 1u) {\n let r = refit(pixels, &idx);\n if (!r.valid) { break; }\n var nc0 = to565(r.e0);\n var nc1 = to565(r.e1);\n // A refit that flips/equalises the endpoints would change decode mode;\n // keep 4-color mode, and stop once it stops moving.\n if (nc0 < nc1) { let t = nc0; nc0 = nc1; nc1 = t; }\n if (nc0 == nc1) { break; }\n if (nc0 == c0 && nc1 == c1) { break; }\n build_palette(nc0, nc1, &pal);\n let nerr = assign_indices(pixels, &pal, &idx);\n c0 = nc0; c1 = nc1; err = nerr;\n if (nerr < (*best).err) {\n (*best).c0 = nc0; (*best).c1 = nc1; (*best).indices = idx; (*best).err = nerr;\n }\n }\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) block.\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 // Inset the bounding box by ~half an RGB565 cell (1/16) so the quantized\n // 4-color palette covers the real data range more tightly (stb_dxt heuristic).\n let inset = (bb_max - bb_min) / 16.0;\n let bbox_hi = clamp(bb_max - inset, vec3<f32>(0.0), vec3<f32>(1.0));\n let bbox_lo = clamp(bb_min + inset, vec3<f32>(0.0), vec3<f32>(1.0));\n\n if (QUALITY_HIGH == 0u) {\n // -------- fast: PCA seed + projection + fused LSQ refit + reprojection\n // Seed endpoints from the block's principal colour axis (same\n // power-iteration as the high path). The bbox diagonal is sign-blind: on\n // anti-correlated channels (normal maps, hue edges) it points across the\n // data instead of along it, and the LSQ refit \u2014 which fits endpoints\n // GIVEN the projection indices \u2014 can't recover from a wrong axis.\n // Degenerate (near-flat) blocks keep the inset-bbox seed.\n var seed_hi = bbox_hi;\n var seed_lo = bbox_lo;\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 // Inset along the axis by ~half a 565 cell (stb_dxt heuristic,\n // matching the bbox inset).\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 }\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 let p0 = from565(c0);\n let p1 = from565(c1);\n\n // Fused pass: projection assignment + LSQ sums + the seed solution's\n // packed indices and squared error. Level \u2192 BC1 index: 0\u21920 (c0), 1\u21922,\n // 2\u21923, 3\u21921 (c1); as a packed LUT: (0x78 >> 2L) & 3.\n var idx_bits: u32 = 0u;\n let dir = p1 - p0;\n let dd = dot(dir, dir);\n if (dd > 0.0) {\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 = vec3<f32>(0.0); var sBV = vec3<f32>(0.0);\n var s_min = 3.0; var s_max = 0.0;\n var seed_err: f32 = 0.0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let v = pixels[k];\n let s = clamp(floor(dot(v - p0, 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 let e = v - (p0 + b * dir);\n seed_err = seed_err + dot(e, e);\n idx_bits = idx_bits | (((0x78u >> (u32(s) * 2u)) & 3u) << (k * 2u));\n }\n let det = sAA * sBB - sAB * sAB;\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 (s_min < s_max && abs(det) > 1e-3) {\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((sBB * sAV - sAB * sBV) / det, bb_min, bb_max);\n let e1 = clamp((sAA * sBV - sAB * 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 let np0 = from565(nc0);\n let np1 = from565(nc1);\n let ndir = np1 - np0;\n let ndd = dot(ndir, ndir);\n if (ndd > 0.0 && !(nc0 == c0 && nc1 == c1)) {\n // Reproject against the refit endpoints and accept them only if\n // the block error actually decreases (the refit minimises a\n // continuous objective; after 565 quantisation it can lose).\n let ninv = 3.0 / ndd;\n var refit_err: f32 = 0.0;\n var nidx_bits: u32 = 0u;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let v = pixels[k];\n let s = clamp(floor(dot(v - np0, ndir) * ninv + 0.5), 0.0, 3.0);\n let e = v - (np0 + s * (1.0 / 3.0) * ndir);\n refit_err = refit_err + dot(e, e);\n nidx_bits = nidx_bits | (((0x78u >> (u32(s) * 2u)) & 3u) << (k * 2u));\n }\n if (refit_err < seed_err) {\n c0 = nc0; c1 = nc1;\n idx_bits = nidx_bits;\n }\n }\n }\n }\n\n let out = block_index * 2u;\n dst[out] = c0 | (c1 << 16u);\n dst[out + 1u] = idx_bits;\n return;\n }\n\n // ------------------------------ high --------------------------------- //\n var best: Best;\n best.err = 1e30;\n\n // Seed from the principal colour axis: project all texels onto it, take the\n // extreme projections as endpoints, inset along the axis. Then also try the\n // bbox seed and keep whichever family yields the lower error.\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 let pca_hi = clamp(mean + (t_max - pad) * axis, vec3<f32>(0.0), vec3<f32>(1.0));\n let pca_lo = clamp(mean + (t_min + pad) * axis, vec3<f32>(0.0), vec3<f32>(1.0));\n fit_from_endpoints(&pixels, pca_hi, pca_lo, 3u, &best);\n }\n fit_from_endpoints(&pixels, bbox_hi, bbox_lo, 3u, &best);\n\n var indices: u32 = 0u;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n indices = indices | ((best.indices[k] & 3u) << (k * 2u));\n }\n\n let out = block_index * 2u;\n dst[out] = best.c0 | (best.c1 << 16u);\n dst[out + 1u] = indices;\n}\n";
971
+ 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";
428
972
 
429
973
  // src/bc1_fast_f16.wgsl
430
974
  var bc1_fast_f16_default = `// bc1 "fast" encoder \u2014 f16 variant (requires the shader-f16 feature).
@@ -440,11 +984,13 @@ var bc1_fast_f16_default = `// bc1 "fast" encoder \u2014 f16 variant (requires t
440
984
  // 3. ONE fused pass: project every pixel onto the decoded-endpoint line
441
985
  // (the 4 palette entries are colinear and evenly spaced, so the nearest
442
986
  // entry is the rounded projection \u2014 no 4-entry search) while
443
- // accumulating the least-squares refit sums, the seed solution's packed
444
- // indices and its squared error
445
- // 4. re-quantise the refit endpoints, reproject (indices packed on the
446
- // fly), and accept the refit only if the block error decreases \u2014
447
- // flat/single-level blocks skip this pass entirely
987
+ // accumulating the least-squares refit sums, the packed indices and the
988
+ // squared error
989
+ // 4. up to TWO refit rounds (mirroring the high path's iterated refits):
990
+ // re-quantise the refit endpoints, reproject (indices packed on the
991
+ // fly, sums re-accumulated to seed the next round), and accept each
992
+ // round only if the block error decreases \u2014 flat/single-level blocks
993
+ // skip these passes entirely
448
994
  //
449
995
  // vs the pre-projection fast branch (build palette + full 4-entry search \xD7 3
450
996
  // passes + refit sums pass) this does roughly half the ALU per block. The
@@ -492,6 +1038,56 @@ fn order565(a: u32, b: u32) -> vec2<u32> {
492
1038
  return vec2<u32>(c0, c1);
493
1039
  }
494
1040
 
1041
+ // One projection pass against the decoded endpoints of (c0,c1): the packed
1042
+ // 2-bit indices, the block's squared error, and the LSQ normal-equation sums
1043
+ // of the resulting assignment \u2014 so an accepted refit can seed the next
1044
+ // round. Levels s run 0..3 along p0\u2192p1 (palette = p0, p0+\u2153d, p0+\u2154d, p1 \u2014
1045
+ // colinear, evenly spaced, so rounding the projection IS the nearest-entry
1046
+ // search). Level \u2192 BC1 index: 0\u21920 (c0), 1\u21922 (\u2154c0+\u2153c1), 2\u21923, 3\u21921 (c1); as a
1047
+ // packed LUT: (0x78 >> 2L) & 3.
1048
+ struct Proj {
1049
+ indices: u32,
1050
+ err: h,
1051
+ sAA: h, sBB: h, sAB: h,
1052
+ sAV: h3, sBV: h3,
1053
+ s_min: h, s_max: h,
1054
+ };
1055
+ fn project_stats(pix: ptr<function, array<h3, 16>>, c0: u32, c1: u32) -> Proj {
1056
+ var out: Proj;
1057
+ out.indices = 0u;
1058
+ out.err = h(0.0);
1059
+ out.sAA = h(0.0); out.sBB = h(0.0); out.sAB = h(0.0);
1060
+ out.sAV = h3(0.0); out.sBV = h3(0.0);
1061
+ out.s_min = h(3.0); out.s_max = h(0.0);
1062
+ let p0 = from565(c0);
1063
+ let p1 = from565(c1);
1064
+ let dir = p1 - p0;
1065
+ let dd = dot(dir, dir);
1066
+ if (dd == h(0.0)) {
1067
+ // Unreachable for distinct 565 codes (the decode is injective); kept so
1068
+ // a degenerate call still returns a consistent error.
1069
+ out.s_min = h(0.0);
1070
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1071
+ let e = (*pix)[k] - p0;
1072
+ out.err = out.err + dot(e, e);
1073
+ }
1074
+ return out;
1075
+ }
1076
+ let inv = h(3.0) / dd;
1077
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1078
+ let v = (*pix)[k];
1079
+ let s = clamp(floor(dot(v - p0, dir) * inv + h(0.5)), h(0.0), h(3.0));
1080
+ out.s_min = min(out.s_min, s); out.s_max = max(out.s_max, s);
1081
+ let b = s * h(1.0 / 3.0); let a = h(1.0) - b;
1082
+ out.sAA = out.sAA + a * a; out.sBB = out.sBB + b * b; out.sAB = out.sAB + a * b;
1083
+ out.sAV = out.sAV + a * v; out.sBV = out.sBV + b * v;
1084
+ let e = v - (p0 + b * dir);
1085
+ out.err = out.err + dot(e, e);
1086
+ out.indices = out.indices | (((0x78u >> (u32(s) * 2u)) & 3u) << (k * 2u));
1087
+ }
1088
+ return out;
1089
+ }
1090
+
495
1091
  @compute @workgroup_size(8, 8, 1)
496
1092
  fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
497
1093
  if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) { return; }
@@ -503,13 +1099,23 @@ fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
503
1099
  var mn = h3(1.0);
504
1100
  var mxv = h3(0.0);
505
1101
  var mean = h3(0.0);
1102
+ var gd = h(0.0);
506
1103
  for (var i: u32 = 0u; i < 16u; i = i + 1u) {
507
1104
  let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);
508
1105
  let px = h3(textureLoad(src_tex, p, 0).rgb);
509
1106
  pix[i] = px; mn = min(mn, px); mxv = max(mxv, px);
510
1107
  mean = mean + px;
1108
+ gd = max(gd, max(abs(px.x - px.y), abs(px.x - px.z)));
511
1109
  }
512
1110
  mean = mean * h(1.0 / 16.0);
1111
+ // Exactly-gray blocks free the refit from the bbox clamp below: a gray
1112
+ // block has no hue to bend (the clamp's whole purpose), and on smooth
1113
+ // gradients the LSQ optimum often lies OUTSIDE the data range \u2014 endpoints
1114
+ // spread wider than the block so the 1/3-2/3 interpolants land on the
1115
+ // values. Same rationale as the BC5 scalar channels (+0.32 dB there).
1116
+ let gray = gd == h(0.0);
1117
+ let lim_lo = select(mn, h3(0.0), gray);
1118
+ let lim_hi = select(mxv, h3(1.0), gray);
513
1119
 
514
1120
  // Seed endpoints from the block's principal colour axis (covariance
515
1121
  // power-iteration, seeded with the bbox diagonal \u2014 same family as the
@@ -564,35 +1170,15 @@ fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
564
1170
  let seed = order565(to565(seed_hi), to565(seed_lo));
565
1171
  var c0 = seed.x;
566
1172
  var c1 = seed.y;
567
- let p0 = from565(c0);
568
- let p1 = from565(c1);
569
1173
 
570
- // Fused pass: projection assignment + LSQ normal-equation sums + the seed
571
- // solution's packed indices and squared error. Levels s run 0..3 along
572
- // p0\u2192p1 (palette = p0, p0+\u2153d, p0+\u2154d, p1 \u2014 colinear, evenly spaced, so
573
- // rounding the projection IS the nearest-entry search). Level \u2192 BC1 index:
574
- // 0\u21920 (c0), 1\u21922 (\u2154c0+\u2153c1), 2\u21923, 3\u21921 (c1); as a packed LUT: (0x78 >> 2L) & 3.
575
- var indices: u32 = 0u;
576
- let dir = p1 - p0;
577
- let dd = dot(dir, dir);
578
- if (dd > h(0.0)) {
579
- let inv = h(3.0) / dd;
580
- var sAA = h(0.0); var sBB = h(0.0); var sAB = h(0.0);
581
- var sAV = h3(0.0); var sBV = h3(0.0);
582
- var s_min = h(3.0); var s_max = h(0.0);
583
- var seed_err = h(0.0);
584
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
585
- let v = pix[k];
586
- let s = clamp(floor(dot(v - p0, dir) * inv + h(0.5)), h(0.0), h(3.0));
587
- s_min = min(s_min, s); s_max = max(s_max, s);
588
- let b = s * h(1.0 / 3.0); let a = h(1.0) - b;
589
- sAA = sAA + a * a; sBB = sBB + b * b; sAB = sAB + a * b;
590
- sAV = sAV + a * v; sBV = sBV + b * v;
591
- let e = v - (p0 + b * dir);
592
- seed_err = seed_err + dot(e, e);
593
- indices = indices | (((0x78u >> (u32(s) * 2u)) & 3u) << (k * 2u));
594
- }
595
- let det = sAA * sBB - sAB * sAB;
1174
+ // Fused seed pass, then up to TWO least-squares refit rounds (mirroring
1175
+ // the high path's iterated refits, at projection cost), each accepted only
1176
+ // if the block's squared error actually decreases \u2014 the refit minimises a
1177
+ // continuous objective and can lose after 565 quantisation. Every pass
1178
+ // re-accumulates the normal-equation sums, so an accepted round seeds the
1179
+ // next.
1180
+ var cur = project_stats(&pix, c0, c1);
1181
+ for (var it: u32 = 0u; it < 2u; it = it + 1u) {
596
1182
  // Refit only on a well-conditioned system. When every pixel lands on ONE
597
1183
  // level (flat / near-flat blocks \u2014 note the 4-colour-mode nudge forces
598
1184
  // c0 \u2260 c1 even for perfectly flat blocks) the system is rank-1: det is 0
@@ -600,45 +1186,30 @@ fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
600
1186
  // noise, so the solve returns garbage endpoints. With \u22652 distinct levels
601
1187
  // det = \u03A3_i<j (b_j \u2212 b_i)\xB2 \u2265 15\xB7(1/3)\xB2 \u2248 1.67, far above the ~0.05 f16
602
1188
  // noise floor \u2014 0.5 separates the two regimes cleanly.
603
- if (s_min < s_max && abs(det) > h(0.5)) {
604
- // Clamp the refit to the block bbox (not [0,1]): on multi-cluster
605
- // blocks the unconstrained solve extrapolates far outside the block's
606
- // colours and the per-channel clamp then bends the hue \u2014 fringe pixels
607
- // decode to colours that exist nowhere in the block. Constraining to
608
- // the bbox also measures better in plain SSE (+1.6 dB on the colour
609
- // test card), so the accept-if-better guard below keeps more refits.
610
- let e0 = clamp((sBB * sAV - sAB * sBV) / det, mn, mxv);
611
- let e1 = clamp((sAA * sBV - sAB * sAV) / det, mn, mxv);
612
- let refit = order565(to565(e0), to565(e1));
613
- let np0 = from565(refit.x);
614
- let np1 = from565(refit.y);
615
- let ndir = np1 - np0;
616
- let ndd = dot(ndir, ndir);
617
- if (ndd > h(0.0) && !(refit.x == c0 && refit.y == c1)) {
618
- // Reproject against the refit endpoints and accept them only if the
619
- // block's squared error actually decreases (the refit minimises a
620
- // continuous objective; after 565 quantisation it can lose).
621
- let ninv = h(3.0) / ndd;
622
- var refit_err = h(0.0);
623
- var nindices: u32 = 0u;
624
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
625
- let v = pix[k];
626
- let s = clamp(floor(dot(v - np0, ndir) * ninv + h(0.5)), h(0.0), h(3.0));
627
- let e = v - (np0 + s * h(1.0 / 3.0) * ndir);
628
- refit_err = refit_err + dot(e, e);
629
- nindices = nindices | (((0x78u >> (u32(s) * 2u)) & 3u) << (k * 2u));
630
- }
631
- if (refit_err < seed_err) {
632
- c0 = refit.x; c1 = refit.y;
633
- indices = nindices;
634
- }
635
- }
636
- }
1189
+ if (cur.s_min >= cur.s_max) { break; }
1190
+ let det = cur.sAA * cur.sBB - cur.sAB * cur.sAB;
1191
+ if (abs(det) <= h(0.5)) { break; }
1192
+ // Clamp the refit to the block bbox (not [0,1]) \u2014 except for exactly
1193
+ // gray blocks, see the load pass: on multi-cluster blocks the
1194
+ // unconstrained solve extrapolates far outside the block's colours
1195
+ // and the per-channel clamp then bends the hue \u2014 fringe pixels decode to
1196
+ // colours that exist nowhere in the block. Constraining to the bbox also
1197
+ // measures better in plain SSE (+1.6 dB on the colour test card), so the
1198
+ // accept-if-better guard below keeps more refits.
1199
+ let e0 = clamp((cur.sBB * cur.sAV - cur.sAB * cur.sBV) / det, lim_lo, lim_hi);
1200
+ let e1 = clamp((cur.sAA * cur.sBV - cur.sAB * cur.sAV) / det, lim_lo, lim_hi);
1201
+ let rq = order565(to565(e0), to565(e1));
1202
+ if (rq.x == c0 && rq.y == c1) { break; }
1203
+ let nxt = project_stats(&pix, rq.x, rq.y);
1204
+ if (nxt.err >= cur.err) { break; }
1205
+ c0 = rq.x;
1206
+ c1 = rq.y;
1207
+ cur = nxt;
637
1208
  }
638
1209
 
639
1210
  let o = bi * 2u;
640
1211
  dst[o] = c0 | (c1 << 16u);
641
- dst[o + 1u] = indices;
1212
+ dst[o + 1u] = cur.indices;
642
1213
  }
643
1214
  `;
644
1215
 
@@ -655,9 +1226,6 @@ var BC1Encoder = class extends Encoder {
655
1226
  get supportsSrgb() {
656
1227
  return true;
657
1228
  }
658
- get supportsQuality() {
659
- return true;
660
- }
661
1229
  wgslSource() {
662
1230
  return bc1_default;
663
1231
  }
@@ -670,12 +1238,12 @@ var BC1Encoder = class extends Encoder {
670
1238
  };
671
1239
 
672
1240
  // src/bc5.wgsl
673
- 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.\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\n// `bc4_ref.js` for the reasoning and the CPU reference this shader is\n// ported from \u2014 the algorithm and edge cases mirror it line-for-line.\n//\n// Pipeline per channel:\n// 1. Load 16 single-channel values, find min/max \u2192 initial endpoints.\n// 2. Quantize to 8-bit. Nudge apart if equal (forces 6-interp mode).\n// 3. Build palette, assign each texel its nearest entry (full L2).\n// 4. One-pass least-squares refinement: solve the 2\xD72 normal equations\n// for the (r0, r1) that minimizes \u03A3(palette[i_k] \u2212 v_k)\xB2. Accept\n// only if quantized endpoints still satisfy r0 > r1 AND total\n// squared error decreased.\n// 5. Pack 2 endpoint bytes + 48 bits of indices into the 8-byte block.\n//\n// The candidate endpoints/indices/error are tracked in place \u2014 the refit\n// overwrites them only when accepted \u2014 so no 16-entry index array is ever\n// copied across a function return.\n//\n// QUALITY LEVELS (pipeline-overridable constant `QUALITY_HIGH`)\n// fast (0, default): bbox endpoints + O(1) projection assignment per texel.\n// The 8-entry palette in 6-interp mode is colinear and EVENLY spaced from\n// r0 to r1 (levels 0..7 in palette order 0,2,3,4,5,6,7,1), so the nearest\n// 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\n// accepted only if it lowers the block error (worth ~1.3 dB on the\n// normal-map card).\n// high (1): full nearest search + refit, matches bc4_ref/bc5_ref.\n\n// 0 = fast (default), 1 = exhaustive/high-quality. Set via pipeline constants.\noverride QUALITY_HIGH: u32 = 0u;\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\n// 6-interpolation-mode palette weights. palette[j] = W0_6[j]*r0 + W1_6[j]*r1.\n// Expressed as a switch so we don't rely on module-scope const arrays.\nfn w0_6(j: u32) -> f32 {\n switch j {\n case 0u: { return 1.0; }\n case 1u: { return 0.0; }\n case 2u: { return 6.0 / 7.0; }\n case 3u: { return 5.0 / 7.0; }\n case 4u: { return 4.0 / 7.0; }\n case 5u: { return 3.0 / 7.0; }\n case 6u: { return 2.0 / 7.0; }\n default: { return 1.0 / 7.0; } // case 7u\n }\n}\n\nfn w1_6(j: u32) -> f32 {\n switch j {\n case 0u: { return 0.0; }\n case 1u: { return 1.0; }\n case 2u: { return 1.0 / 7.0; }\n case 3u: { return 2.0 / 7.0; }\n case 4u: { return 3.0 / 7.0; }\n case 5u: { return 4.0 / 7.0; }\n case 6u: { return 5.0 / 7.0; }\n default: { return 6.0 / 7.0; } // case 7u\n }\n}\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// Build the 8-entry palette for endpoints (r0f, r1f) in normalised space.\nfn build_pal(r0f: f32, r1f: f32, pal: ptr<function, array<f32, 8>>) {\n for (var j: u32 = 0u; j < 8u; j = j + 1u) {\n (*pal)[j] = w0_6(j) * r0f + w1_6(j) * r1f;\n }\n}\n\n// Assign each of the 16 values its nearest palette entry (full 8-entry L2),\n// writing indices into `out_idx` and returning the total squared error.\nfn assign_all(\n values: ptr<function, array<f32, 16>>,\n pal: ptr<function, array<f32, 8>>,\n out_idx: ptr<function, array<u32, 16>>,\n) -> f32 {\n var err: f32 = 0.0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let v = (*values)[k];\n var best_j: u32 = 0u;\n var best_d: f32 = 1e20;\n for (var j: u32 = 0u; j < 8u; j = j + 1u) {\n let d = (*pal)[j] - v;\n let d2 = d * d;\n if (d2 < best_d) {\n best_d = d2;\n best_j = j;\n }\n }\n (*out_idx)[k] = best_j;\n err = err + best_d;\n }\n return err;\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 // ---------------- 1. Initial endpoints: bbox of input ----------------\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 if (QUALITY_HIGH == 0u) {\n // -------- fast: fused projection + LSQ refit, accept-if-better -------\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 \u2014 worth\n // ~1.3 dB on the normal-map card over the refit-free seed.\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 the refit to the block's value range (a strict-SSE win vs\n // clamping to [0,1], same as the other formats' fast paths).\n let e0 = clamp(r0f + (sBB * sAV - sAB * sBV) / det, vmin, vmax);\n let e1 = clamp(r0f + (sAA * sBV - sAB * sAV) / det, vmin, vmax);\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 // ---------------- 2. Initial palette + indices + error --------------\n var pal: array<f32, 8>;\n build_pal(f32(r0) / 255.0, f32(r1) / 255.0, &pal);\n var indices: array<u32, 16>;\n var err = assign_all(values, &pal, &indices);\n\n // ---------------- 3. Refinement: least-squares on (r0, r1) ----------\n // High-quality only \u2014 the refit is the bulk of the per-channel cost and the\n // branch is resolved at pipeline-compile time, so the fast path skips all of\n // it (the sums loop included), not just the acceptance test.\n if (QUALITY_HIGH != 0u) {\n // Normal equations for palette[j] = a_j * r0 + b_j * r1:\n // [\u03A3AA \u03A3AB] [r0] [\u03A3AV]\n // [\u03A3AB \u03A3BB] [r1] = [\u03A3BV]\n var sAA: f32 = 0.0; var sBB: f32 = 0.0; var sAB: f32 = 0.0;\n var sAV: f32 = 0.0; var sBV: f32 = 0.0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let a = w0_6(indices[k]);\n let b = w1_6(indices[k]);\n let v = (*values)[k];\n sAA = sAA + a * a;\n sBB = sBB + b * b;\n sAB = sAB + a * b;\n sAV = sAV + a * v;\n sBV = sBV + b * v;\n }\n let det = sAA * sBB - sAB * sAB;\n // Degenerate system \u2192 skip refinement.\n if (abs(det) > 1e-9) {\n let new_r0 = clamp((sBB * sAV - sAB * sBV) / det, 0.0, 1.0);\n let new_r1 = clamp((sAA * sBV - sAB * sAV) / det, 0.0, 1.0);\n let qR0 = quantize8(new_r0);\n let qR1 = quantize8(new_r1);\n // Only accept refinements that stay in 6-interp mode. A refinement\n // that flips or equalizes the endpoints would change decode mode.\n if (qR0 > qR1) {\n build_pal(f32(qR0) / 255.0, f32(qR1) / 255.0, &pal);\n var idx2: array<u32, 16>;\n let err2 = assign_all(values, &pal, &idx2);\n if (err2 < err) {\n r0 = qR0;\n r1 = qR1;\n indices = idx2;\n err = err2;\n }\n }\n }\n }\n\n // ---------------- 4. Pack 48-bit index field + 2 endpoint bytes -----\n // The 48-bit index field spans block bytes 2..7. Split into idx_lo\n // (low 32 bits of the field) and idx_hi (high 16 bits). An index at\n // bit position 3k straddles the 32-bit boundary iff 3k < 32 < 3k+3\n // (only k = 10, 11 straddle: bits 30..32 and 33..35; actually k=10\n // is bits 30..32, k=11 is 33..35 \u2014 so k=10 straddles). We handle\n // straddles by writing to both halves.\n var idx_lo: u32 = 0u;\n var idx_hi: u32 = 0u;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let bit = 3u * k;\n let v = indices[k] & 7u;\n if (bit + 3u <= 32u) {\n idx_lo = idx_lo | (v << bit);\n } else if (bit >= 32u) {\n idx_hi = idx_hi | (v << (bit - 32u));\n } else {\n // Straddle: low part into idx_lo's top, high part into idx_hi's bottom.\n idx_lo = idx_lo | (v << bit);\n idx_hi = idx_hi | (v >> (32u - bit));\n }\n }\n\n // Final u32s, both little-endian:\n // u32[0] bytes = red0, red1, idx_lo[7:0], idx_lo[15:8]\n // u32[1] bytes = idx_lo[23:16], idx_lo[31:24], idx_hi[7:0], idx_hi[15:8]\n let out_lo = r0 | (r1 << 8u) | ((idx_lo & 0xFFFFu) << 16u);\n let out_hi = (idx_lo >> 16u) | (idx_hi << 16u);\n\n return vec2<u32>(out_lo, out_hi);\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";
1241
+ 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";
674
1242
 
675
1243
  // src/bc5_fast_f16.wgsl
676
1244
  var bc5_fast_f16_default = `// bc5 "fast" encoder \u2014 f16 variant (requires the shader-f16 feature).
677
- // Two BC4 halves (R and G) \u2014 same output family as bc5.wgsl's fast branch,
678
- // tuned for throughput:
1245
+ // Two BC4 halves (R and G) \u2014 same output family as bc5.wgsl, tuned for
1246
+ // throughput:
679
1247
  //
680
1248
  // \u2022 The 8-entry palette in 6-interpolation mode is COLINEAR and EVENLY
681
1249
  // spaced from r0 to r1 (levels 0..7 in palette order 0,2,3,4,5,6,7,1),
@@ -684,138 +1252,107 @@ var bc5_fast_f16_default = `// bc5 "fast" encoder \u2014 f16 variant (requires t
684
1252
  // \u2022 Math runs in the exact-integer [0,255] f16 domain: endpoints and pixel
685
1253
  // values are whole numbers \u2264 255 (exact in f16), so the only rounding is
686
1254
  // the single 1/(r1\u2212r0) division.
687
- // \u2022 ONE fused pass per channel: projection assignment + the least-squares
688
- // refit sums + the seed solution's packed indices and squared error. The
689
- // refit endpoints are re-quantised, reprojected, and accepted only if
690
- // the block error decreases (same accept-if-better family as the BC1
691
- // fast path) \u2014 worth ~1.3 dB on the normal-map card.
692
- // \u2022 3-bit indices are packed into the 48-bit field on the fly \u2014 no
693
- // array<u32,16> private array and no separate packing loop.
694
- //
695
- // f16 range notes: value sums accumulate v \u2212 r0 (the affine-basis shift trick
696
- // from the BC7/ASTC fast paths) scaled by 1/16, and error residuals are
697
- // scaled by 1/16 before squaring \u2014 worst-case magnitudes stay \u22724k, well
698
- // inside f16's 65504 max, with rounding a small fraction of a level. The
699
- // accept-if-better guard makes any residual f16 noise fail-safe (worst case:
700
- // the refit is rejected and the seed solution ships).
1255
+ // \u2022 BOTH channels ride the same fused passes as vec2<f16> lanes \u2014 each
1256
+ // loop computes projections and moments for R and G at once instead of
1257
+ // two scalar encode_bc4 calls.
1258
+ // \u2022 The 16 texel reads are 8 textureGather fetches (4 quads \xD7 R,G) for
1259
+ // interior blocks \u2014 byte-identical output to per-texel loads, \u22123.6%
1260
+ // GPU on 4096\xB2 (/ab, 2026-07). Blocks straddling the source edge of a
1261
+ // non-multiple-of-4 image use clamped per-texel loads instead: the
1262
+ // upload pads the texture with ZEROS, so a normalised-coordinate
1263
+ // gather there would read padding (or mis-scale against the padded
1264
+ // size) instead of replicating the last real texel.
1265
+ // \u2022 Pass 1 accumulates MOMENTS, not normal-equation sums. With b = L/7
1266
+ // and the level-space residual \u03C1 = t \u2212 L (t = 7(v\u2212r0)/(r1\u2212r0), so the
1267
+ // value-space residual is r = \u03C1\xB7dir/7), every LSQ sum is an O(1)
1268
+ // per-block function of four accumulators:
1269
+ // sBB = \u03A3L\xB2/49 sAB = \u03A3L/7 \u2212 \u03A3L\xB2/49 sAA = 16 \u2212 2\u03A3L/7 + \u03A3L\xB2/49
1270
+ // sBR = \u03A3L\u03C1\xB7dir/49 sAR = (\u03A3\u03C1 \u2212 \u03A3L\u03C1/7)\xB7dir/7
1271
+ // Per-pixel work drops from 5 product-accumulates + 3 temporaries to 4
1272
+ // cheap accumulates (\u03A3L, \u03A3L\xB2, \u03A3\u03C1, \u03A3L\u03C1), and the f16 range analysis
1273
+ // becomes trivial: \u03A3L \u2264 112 and \u03A3L\xB2 \u2264 784 are exact f16 integers,
1274
+ // |\u03C1| \u2264 ~0.5 keeps \u03A3\u03C1/\u03A3L\u03C1 tiny. The per-BLOCK refit math (including
1275
+ // the solve and E(\u03B4) pricing) runs in f32 \u2014 free at block granularity,
1276
+ // and it retires the sum-cancellation worries the old residual-sum
1277
+ // scheme was built around.
1278
+ // \u2022 The rank guard is EXACT: all pixels on one level \u27FA 16\xB7\u03A3L\xB2 == (\u03A3L)\xB2
1279
+ // (integers, so the comparison is precise in f32) \u2014 no lmin/lmax
1280
+ // tracking in the loop.
1281
+ // \u2022 The refit is accepted or rejected CLOSED-FORM, with no trial
1282
+ // projection pass: the solve is e = seed + M\u207B\xB9(sAR,sBR), and the error
1283
+ // of re-quantised endpoints ON THE CURRENT INDICES is
1284
+ // E(\u03B4) = err \u2212 2(\u03B40\xB7sAR + \u03B41\xB7sBR) + \u03B40\xB2sAA + 2\u03B40\u03B41\xB7sAB + \u03B41\xB2sBB
1285
+ // with \u03B4 = quantised endpoint \u2212 base endpoint, compared as the delta
1286
+ // form E \u2212 err < 0. Only the NEAREST rounding of the fractional solve
1287
+ // is priced: pricing all four floor/ceil combinations (the correlated
1288
+ // 2-D quadratic's integer optimum isn't always the nearest rounding)
1289
+ // measured \u22640.015 dB on every content class but ~8% GPU on smooth
1290
+ // content, where Apple's lossless texture compression collapses the
1291
+ // read cost and leaves the kernel ALU-bound (/ab 2026-07, rock 4K
1292
+ // displacement: floor 0.61\xD7 of the rgba8-noise floor).
1293
+ // \u2022 Pass 2 packs the indices ONCE, against the FINAL endpoints \u2014 full
1294
+ // reprojection quality. The previous single-pass scheme shipped the
1295
+ // SEED indices with accepted refit endpoints, giving up 0.08..0.19 dB
1296
+ // because a third reprojection loop cost +14% GPU; with the moment
1297
+ // form paying for the second loop, reprojection now measures at PARITY
1298
+ // with that scheme (/ab 2026-07, interleaved: proc 2048\xB2/4096\xB2 and the
1299
+ // rock 4K normal map, all within \xB11%, vs read floor ~2% below).
1300
+ // Measured and NOT taken: a second refit round off pass-2 moments
1301
+ // (+14%, register cliff, \u22480 gain on smooth content \u2014 round 2 only pays
1302
+ // on noise); full normal-equation sums in both passes (+49%).
1303
+ // \u2022 3-bit indices are packed BRANCH-FREE: pixels 0..7 accumulate into a
1304
+ // 24-bit word, pixels 8..15 into another, recombined with constant
1305
+ // shifts into the 48-bit field (w0 gets field bits 0..15 above the two
1306
+ // endpoint bytes, w1 gets field bits 16..47) \u2014 no per-pixel straddle
1307
+ // branches.
701
1308
  //
702
1309
  // Level \u2192 BC4 index (0\u2192r0 ... 7\u2192r1): 0,2,3,4,5,6,7,1 \u2014 packed 3-bit LUT
703
1310
  // 0x3F58D0 = sum(idx[L] << 3L).
704
1311
  //
705
1312
  // The host selects this module only when the device reports shader-f16,
706
- // falling back to bc5.wgsl otherwise. "high" never uses this.
1313
+ // falling back to bc5.wgsl otherwise.
707
1314
  enable f16;
708
1315
  alias h = f16;
1316
+ alias h2 = vec2<f16>;
709
1317
  struct Params { blocks_x: u32, blocks_y: u32, width: u32, height: u32, };
710
1318
  @group(0) @binding(0) var src_tex: texture_2d<f32>;
711
1319
  @group(0) @binding(1) var<storage, read_write> dst: array<u32>;
712
1320
  @group(0) @binding(2) var<uniform> params: Params;
1321
+ @group(0) @binding(3) var smp: sampler;
713
1322
 
714
- // Project the 16 values onto the r0\u2192r1 axis, packing the 3-bit indices on the
715
- // fly (pixel k's index starts at bit 3k of the 48-bit field, i.e. bit 3k+16
716
- // of the (w0,w1) pair; k = 5 straddles the word boundary) and accumulating
717
- // the squared error (residuals scaled by 1/16 before squaring). Returns the
718
- // packed words with the endpoint bytes already in place.
719
- struct Proj { w0: u32, w1: u32, err: h };
720
- fn project_pack(values: ptr<function, array<h, 16>>, r0: u32, r1: u32) -> Proj {
721
- let r0f = h(f32(r0));
722
- let dir = h(f32(r1)) - r0f;
723
- let scale = h(7.0) / dir;
724
- var out: Proj;
725
- out.w0 = r0 | (r1 << 8u);
726
- out.w1 = 0u;
727
- out.err = h(0.0);
728
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
729
- let vr = (*values)[k] - r0f;
730
- let L = clamp(floor(vr * scale + h(0.5)), h(0.0), h(7.0));
731
- let e = (vr - L * h(1.0 / 7.0) * dir) * h(1.0 / 16.0);
732
- out.err = out.err + e * e;
733
- let idx = (0x3F58D0u >> (u32(L) * 3u)) & 7u;
734
- let bit = 3u * k + 16u;
735
- if (bit <= 29u) {
736
- out.w0 = out.w0 | (idx << bit);
737
- } else if (bit >= 32u) {
738
- out.w1 = out.w1 | (idx << (bit - 32u));
739
- } else {
740
- out.w0 = out.w0 | (idx << bit);
741
- out.w1 = out.w1 | (idx >> (32u - bit));
742
- }
743
- }
744
- return out;
745
- }
746
-
747
- // Encode one channel (16 values in exact-integer [0,255] f16) to a BC4 half.
748
- // vmin/vmax are the channel's min/max, computed in the caller's load loop \u2014
749
- // fusing that scan there saves a 16-value pass per channel.
750
- fn encode_bc4(values: ptr<function, array<h, 16>>, vmin: h, vmax: h) -> vec2<u32> {
751
- var r0 = u32(vmax); // values are exact integers \u2014 no rounding needed
752
- var r1 = u32(vmin);
753
- if (r0 == r1) {
754
- // Flat block: nudge to keep 6-interp mode (r0 > r1 strictly).
755
- if (r1 > 0u) { r1 = r1 - 1u; } else { r0 = r0 + 1u; }
756
- }
757
-
758
- // Fused seed pass: projection assignment (level = round(7\xB7(v \u2212 r0)/
759
- // (r1 \u2212 r0)), clamped \u2014 |v \u2212 r0| \u2264 r0 \u2212 r1 for in-block values) + packed
760
- // indices + seed error + the LSQ normal-equation sums. Value sums
761
- // accumulate (v \u2212 r0)/16: the shift keeps the accumulators proportional to
762
- // the block's span, the exact power-of-two scale keeps products \u2264 4080.
763
- let r0f = h(f32(r0));
764
- let dir = h(f32(r1)) - r0f;
765
- let scale = h(7.0) / dir;
766
- var seed: Proj;
767
- seed.w0 = r0 | (r1 << 8u);
768
- seed.w1 = 0u;
769
- seed.err = h(0.0);
770
- var sAA = h(0.0); var sBB = h(0.0); var sAB = h(0.0);
771
- var sAV = h(0.0); var sBV = h(0.0);
772
- var s_min = h(7.0); var s_max = h(0.0);
773
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
774
- let vr = (*values)[k] - r0f;
775
- let L = clamp(floor(vr * scale + h(0.5)), h(0.0), h(7.0));
776
- s_min = min(s_min, L); s_max = max(s_max, L);
777
- let b = L * h(1.0 / 7.0); let a = h(1.0) - b;
778
- let vr16 = vr * h(1.0 / 16.0);
779
- sAA = sAA + a * a; sBB = sBB + b * b; sAB = sAB + a * b;
780
- sAV = sAV + a * vr16; sBV = sBV + b * vr16;
781
- let e = vr16 - b * dir * h(1.0 / 16.0);
782
- seed.err = seed.err + e * e;
783
- let idx = (0x3F58D0u >> (u32(L) * 3u)) & 7u;
784
- let bit = 3u * k + 16u;
785
- if (bit <= 29u) {
786
- seed.w0 = seed.w0 | (idx << bit);
787
- } else if (bit >= 32u) {
788
- seed.w1 = seed.w1 | (idx << (bit - 32u));
789
- } else {
790
- // k = 5 straddles the word boundary (bits 31..33).
791
- seed.w0 = seed.w0 | (idx << bit);
792
- seed.w1 = seed.w1 | (idx >> (32u - bit));
793
- }
794
- }
1323
+ const IDX_LUT: u32 = 0x3F58D0u;
795
1324
 
796
- // LSQ refit, accepted only if the requantised endpoints lower the block
797
- // error. Rank-1 guard: with every pixel on ONE level the system is
798
- // singular and det is pure f16 rounding noise (\u22720.03); with \u22652 distinct
799
- // levels det = \u03A3_i<j (b_j \u2212 b_i)\xB2 \u2265 15/49 \u2248 0.306 \u2014 0.1 separates cleanly.
800
- if (s_min < s_max) {
801
- let det = sAA * sBB - sAB * sAB;
802
- if (abs(det) > h(0.1)) {
803
- // \xD716 undoes the accumulator scale; clamp to the block's value range
804
- // (a strict-SSE win vs clamping to [0,255], same as the other formats).
805
- let e0 = clamp(r0f + (sBB * sAV - sAB * sBV) * h(16.0) / det, vmin, vmax);
806
- let e1 = clamp(r0f + (sAA * sBV - sAB * sAV) * h(16.0) / det, vmin, vmax);
807
- let n0 = u32(floor(e0 + h(0.5)));
808
- let n1 = u32(floor(e1 + h(0.5)));
809
- // Keep 6-interp mode (r0 > r1 strictly); skip the no-op refit.
810
- if (n0 > n1 && !(n0 == r0 && n1 == r1)) {
811
- let refit = project_pack(values, n0, n1);
812
- if (refit.err < seed.err) {
813
- return vec2<u32>(refit.w0, refit.w1);
814
- }
815
- }
1325
+ // Closed-form accept-if-better endpoint refinement for one channel (f32:
1326
+ // per-block O(1) work, so precision is free here). Takes the LSQ sums for
1327
+ // the current indices, the current integer endpoints b0 > b1, and the rank
1328
+ // guard; prices the nearest rounding of the fractional solve via E(\u03B4) and
1329
+ // returns it when it strictly improves and stays in 6-interp mode
1330
+ // (q0 > q1), or (b0,b1) unchanged. Endpoints clamp to [0,255], NOT the
1331
+ // block's value range: for a scalar channel, endpoints beyond the data
1332
+ // range are often genuinely optimal and there is no colour axis to bend
1333
+ // (the bbox clamp the colour formats need costs ~0.3 dB here).
1334
+ fn refine(sAA: f32, sBB: f32, sAB: f32, sAR: f32, sBR: f32, b0: u32, b1: u32, spread: bool) -> vec2<u32> {
1335
+ var out = vec2<u32>(b0, b1);
1336
+ let det = sAA * sBB - sAB * sAB;
1337
+ if (!spread || abs(det) <= 1e-3) { return out; }
1338
+ let b0f = f32(b0);
1339
+ let b1f = f32(b1);
1340
+ let e0 = clamp(b0f + (sBB * sAR - sAB * sBR) / det, 0.0, 255.0);
1341
+ let e1 = clamp(b1f + (sAA * sBR - sAB * sAR) / det, 0.0, 255.0);
1342
+ let q0f = floor(e0 + 0.5);
1343
+ let q1f = floor(e1 + 0.5);
1344
+ let q0 = u32(q0f);
1345
+ let q1 = u32(q1f);
1346
+ if (q0 > q1 && !(q0 == b0 && q1 == b1)) {
1347
+ let dd0 = q0f - b0f;
1348
+ let dd1 = q1f - b1f;
1349
+ let eNew = -2.0 * (dd0 * sAR + dd1 * sBR)
1350
+ + dd0 * dd0 * sAA + 2.0 * dd0 * dd1 * sAB + dd1 * dd1 * sBB;
1351
+ if (eNew < 0.0) {
1352
+ out = vec2<u32>(q0, q1);
816
1353
  }
817
1354
  }
818
- return vec2<u32>(seed.w0, seed.w1);
1355
+ return out;
819
1356
  }
820
1357
 
821
1358
  @compute @workgroup_size(8, 8, 1)
@@ -823,23 +1360,110 @@ fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
823
1360
  if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) { return; }
824
1361
  let bi = gid.y * params.blocks_x + gid.x;
825
1362
  let base = vec2<i32>(i32(gid.x) * 4, i32(gid.y) * 4);
826
- let mx = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);
827
- var rv: array<h, 16>;
828
- var gv: array<h, 16>;
829
- var rmin = h(255.0); var rmax = h(0.0);
830
- var gmin = h(255.0); var gmax = h(0.0);
831
- for (var i: u32 = 0u; i < 16u; i = i + 1u) {
832
- let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);
833
- let c = textureLoad(src_tex, p, 0);
834
- let r = h(c.r * 255.0);
835
- let g = h(c.g * 255.0);
836
- rv[i] = r; rmin = min(rmin, r); rmax = max(rmax, r);
837
- gv[i] = g; gmin = min(gmin, g); gmax = max(gmax, g);
838
- }
839
- let rb = encode_bc4(&rv, rmin, rmax);
840
- let gb = encode_bc4(&gv, gmin, gmax);
1363
+
1364
+ // Load 4\xD74 R/G pairs (x = R, y = G throughout), min/max fused in.
1365
+ // INTERIOR blocks \u2014 every block when the source is a multiple of 4, so
1366
+ // the branch is wavefront-uniform on benchmark-shaped content \u2014 read via
1367
+ // 8 gathers (4 quads \xD7 R,G); the gather point (base+quad+1) normalised by
1368
+ // the PHYSICAL (padded) texture size sits exactly between the quad's
1369
+ // texel centers, and interior quads never touch the zero-initialised
1370
+ // padding strip. Blocks straddling the source edge of a
1371
+ // non-multiple-of-4 image fall back to per-texel loads clamped to the
1372
+ // last real texel (gather cannot replicate an edge texel mid-quad).
1373
+ // Gather components: w=(0,0) z=(1,0) x=(0,1) y=(1,1) within each quad.
1374
+ var v: array<h2, 16>;
1375
+ var vmin = h2(255.0);
1376
+ var vmax = h2(0.0);
1377
+ if (u32(base.x) + 4u <= params.width && u32(base.y) + 4u <= params.height) {
1378
+ let inv_size = vec2<f32>(1.0, 1.0) / vec2<f32>(textureDimensions(src_tex));
1379
+ for (var q: u32 = 0u; q < 4u; q = q + 1u) {
1380
+ let qo = vec2<u32>((q & 1u) * 2u, (q >> 1u) * 2u);
1381
+ let cc = (vec2<f32>(base) + vec2<f32>(qo) + vec2<f32>(1.0, 1.0)) * inv_size;
1382
+ let r4 = textureGather(0, src_tex, smp, cc) * 255.0;
1383
+ let g4 = textureGather(1, src_tex, smp, cc) * 255.0;
1384
+ let i = qo.y * 4u + qo.x;
1385
+ let vw = h2(h(r4.w), h(g4.w));
1386
+ let vz = h2(h(r4.z), h(g4.z));
1387
+ let vx = h2(h(r4.x), h(g4.x));
1388
+ let vy = h2(h(r4.y), h(g4.y));
1389
+ v[i] = vw; v[i + 1u] = vz; v[i + 4u] = vx; v[i + 5u] = vy;
1390
+ vmin = min(min(vmin, min(vw, vz)), min(vx, vy));
1391
+ vmax = max(max(vmax, max(vw, vz)), max(vx, vy));
1392
+ }
1393
+ } else {
1394
+ let mx = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);
1395
+ for (var i: u32 = 0u; i < 16u; i = i + 1u) {
1396
+ let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);
1397
+ let c = textureLoad(src_tex, p, 0);
1398
+ let val = h2(h(c.r * 255.0), h(c.g * 255.0));
1399
+ v[i] = val; vmin = min(vmin, val); vmax = max(vmax, val);
1400
+ }
1401
+ }
1402
+
1403
+ // Seed endpoints at the exact per-channel extremes (values are exact
1404
+ // integers \u2014 no rounding needed). Flat blocks get nudged apart to keep
1405
+ // the 6-interp mode (r0 > r1 strictly).
1406
+ var r0 = vec2<u32>(vmax);
1407
+ var r1 = vec2<u32>(vmin);
1408
+ if (r0.x == r1.x) { if (r1.x > 0u) { r1.x = r1.x - 1u; } else { r0.x = r0.x + 1u; } }
1409
+ if (r0.y == r1.y) { if (r1.y > 0u) { r1.y = r1.y - 1u; } else { r0.y = r0.y + 1u; } }
1410
+
1411
+ let r0f = h2(vec2<f32>(r0));
1412
+ let r1f = h2(vec2<f32>(r1));
1413
+ let dir = r1f - r0f;
1414
+ let scale = h2(7.0) / dir;
1415
+
1416
+ // Pass 1, both channels \u2014 MOMENTS only. t = 7(v\u2212r0)/(r1\u2212r0) \u2208 [0,7]
1417
+ // (the seed covers the data), L = round(t), \u03C1 = t \u2212 L.
1418
+ var sL = h2(0.0); var sLL = h2(0.0); var pR = h2(0.0); var pLR = h2(0.0);
1419
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1420
+ let t = (v[k] - r0f) * scale;
1421
+ let L = clamp(floor(t + h2(0.5)), h2(0.0), h2(7.0));
1422
+ let rho = t - L;
1423
+ sL = sL + L; sLL = sLL + L * L;
1424
+ pR = pR + rho; pLR = pLR + L * rho;
1425
+ }
1426
+
1427
+ // Per-block refit in f32 off the moments (see header for the identities).
1428
+ let sLf = vec2<f32>(sL);
1429
+ let sLLf = vec2<f32>(sLL);
1430
+ let dirf = vec2<f32>(r1) - vec2<f32>(r0);
1431
+ let sBB = sLLf * (1.0 / 49.0);
1432
+ let sAB = sLf * (1.0 / 7.0) - sBB;
1433
+ let sAA = vec2<f32>(16.0) - 2.0 * sLf * (1.0 / 7.0) + sBB;
1434
+ let sBR = vec2<f32>(pLR) * dirf * (1.0 / 49.0);
1435
+ let sAR = (vec2<f32>(pR) - vec2<f32>(pLR) * (1.0 / 7.0)) * dirf * (1.0 / 7.0);
1436
+ let spread = 16.0 * sLLf != sLf * sLf;
1437
+
1438
+ let fx = refine(sAA.x, sBB.x, sAB.x, sAR.x, sBR.x, r0.x, r1.x, spread.x);
1439
+ let fy = refine(sAA.y, sBB.y, sAB.y, sAR.y, sBR.y, r0.y, r1.y, spread.y);
1440
+ let n0 = vec2<u32>(fx.x, fy.x);
1441
+ let n1 = vec2<u32>(fx.y, fy.y);
1442
+
1443
+ // Pass 2, both channels \u2014 pack the shipped indices against the FINAL
1444
+ // endpoints (rejected channels re-derive their seed assignment). iA
1445
+ // holds pixels 0..7 (3 bits each), iB pixels 8..15.
1446
+ let n0f = h2(vec2<f32>(n0));
1447
+ let n1f = h2(vec2<f32>(n1));
1448
+ let scale2 = h2(7.0) / (n1f - n0f);
1449
+ var iAx = 0u; var iBx = 0u; var iAy = 0u; var iBy = 0u;
1450
+ for (var k: u32 = 0u; k < 8u; k = k + 1u) {
1451
+ let L = clamp(floor((v[k] - n0f) * scale2 + h2(0.5)), h2(0.0), h2(7.0));
1452
+ iAx = iAx | (((IDX_LUT >> (u32(L.x) * 3u)) & 7u) << (k * 3u));
1453
+ iAy = iAy | (((IDX_LUT >> (u32(L.y) * 3u)) & 7u) << (k * 3u));
1454
+ }
1455
+ for (var k: u32 = 8u; k < 16u; k = k + 1u) {
1456
+ let L = clamp(floor((v[k] - n0f) * scale2 + h2(0.5)), h2(0.0), h2(7.0));
1457
+ iBx = iBx | (((IDX_LUT >> (u32(L.x) * 3u)) & 7u) << ((k - 8u) * 3u));
1458
+ iBy = iBy | (((IDX_LUT >> (u32(L.y) * 3u)) & 7u) << ((k - 8u) * 3u));
1459
+ }
1460
+
1461
+ // BC5 block = R half (bytes 0..7) || G half (bytes 8..15) = 4 u32s.
841
1462
  let o = bi * 4u;
842
- dst[o] = rb.x; dst[o + 1u] = rb.y; dst[o + 2u] = gb.x; dst[o + 3u] = gb.y;
1463
+ dst[o] = n0.x | (n1.x << 8u) | (iAx << 16u);
1464
+ dst[o + 1u] = (iAx >> 16u) | (iBx << 8u);
1465
+ dst[o + 2u] = n0.y | (n1.y << 8u) | (iAy << 16u);
1466
+ dst[o + 3u] = (iAy >> 16u) | (iBy << 8u);
843
1467
  }
844
1468
  `;
845
1469
 
@@ -856,8 +1480,13 @@ var BC5Encoder = class extends Encoder {
856
1480
  get supportsSrgb() {
857
1481
  return false;
858
1482
  }
859
- get supportsQuality() {
860
- return true;
1483
+ // BC5 stores only R and G — a two-channel source texture halves the
1484
+ // encode pass's DRAM reads. Measured on the pass (/ab 2026-07): −4% on a
1485
+ // real 4K normal map, −8..−12% on procedural 2048²/4096², and the raw
1486
+ // read floor itself drops 35%, so the win grows as the kernel's ALU
1487
+ // shrinks. Also halves source-texture memory during encodes.
1488
+ get srcTextureFormat() {
1489
+ return "rg8unorm";
861
1490
  }
862
1491
  wgslSource() {
863
1492
  return bc5_default;
@@ -871,202 +1500,25 @@ var BC5Encoder = class extends Encoder {
871
1500
  };
872
1501
 
873
1502
  // src/bc7.wgsl
874
- 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]`.\n//\n// QUALITY LEVELS (pipeline-overridable constant `QUALITY_HIGH`)\n// fast (0, default): principal-axis seed (covariance power-iteration; bbox\n// on degenerate blocks) at the exact projection extents, quantised\n// directly \u2014 no LSQ refit; with the seed on the principal axis, mode 6's\n// 16-level palette leaves the refit under 0.15 dB, unlike the 4-level\n// BC1/ASTC fast paths which keep theirs \u2014 then one pass that projects\n// each pixel onto the endpoint line (the 16 palette entries are\n// colinear, so the nearest index is the rounded projection \u2014 no palette\n// build, no 16-entry search), packed on the fly into two nibble words.\n// high (1): farthest-pair seed, exhaustive p-bit search over all four\n// (p0,p1) \u2208 {0,1}\xB2 combos, full 16-entry nearest search, one LSQ refit \u2014\n// matches bc7_ref.ts up to FP tie-breaks.\n//\n// The fast/high branch is selected at pipeline-compile time, so the driver\n// eliminates the unused code entirely.\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\n// 0 = fast (default), 1 = exhaustive/high-quality. Set via pipeline constants.\noverride QUALITY_HIGH: u32 = 0u;\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\n// Mode 6 interpolation weights (\xD7 1/64), fixed by the spec (`W4` in bc7_ref.ts).\nfn w4(i: u32) -> u32 {\n switch i {\n case 0u: { return 0u; }\n case 1u: { return 4u; }\n case 2u: { return 9u; }\n case 3u: { return 13u; }\n case 4u: { return 17u; }\n case 5u: { return 21u; }\n case 6u: { return 26u; }\n case 7u: { return 30u; }\n case 8u: { return 34u; }\n case 9u: { return 38u; }\n case 10u: { return 43u; }\n case 11u: { return 47u; }\n case 12u: { return 51u; }\n case 13u: { return 55u; }\n case 14u: { return 60u; }\n default: { return 64u; } // case 15u\n }\n}\n\nfn interp4(e0: vec4<i32>, e1: vec4<i32>, w: i32) -> vec4<i32> {\n return ((64 - w) * e0 + w * e1 + vec4<i32>(32)) >> vec4<u32>(6u);\n}\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) under\n// a fixed p-bit, all four channels at once. q7 = round((ideal8 \u2212 p)/2); used by\n// both paths.\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// ============================ FAST PATH ================================ //\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;\n// used by the fast path to seed the LSQ fit \u2014 the bbox diagonal is\n// sign-blind and points across anti-correlated data (normal maps, hue edges)\n// 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// ============================ HIGH PATH ================================ //\n\nstruct Pair { a: vec4<i32>, b: vec4<i32> };\nfn farthest_pair(pixels: ptr<function, array<vec4<i32>, 16>>) -> Pair {\n var best_d: i32 = 0;\n var pa = (*pixels)[0];\n var pb = (*pixels)[1];\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let xi = (*pixels)[i];\n for (var j: u32 = i + 1u; j < 16u; j = j + 1u) {\n let d = dist2(xi, (*pixels)[j]);\n if (d > best_d) { best_d = d; pa = xi; pb = (*pixels)[j]; }\n }\n }\n return Pair(pa, pb);\n}\n\nfn build_palette_6(e0: vec4<i32>, e1: vec4<i32>, pal: ptr<function, array<vec4<i32>, 16>>) {\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n (*pal)[i] = interp4(e0, e1, i32(w4(i)));\n }\n}\n\nfn assign_all(\n pixels: ptr<function, array<vec4<i32>, 16>>,\n pal: ptr<function, array<vec4<i32>, 16>>,\n out_idx: ptr<function, array<u32, 16>>,\n) -> i32 {\n var err: i32 = 0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let px = (*pixels)[k];\n var best_i: u32 = 0u;\n var best_d: i32 = 2147483647;\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let d = dist2(px, (*pal)[i]);\n if (d < best_d) { best_d = d; best_i = i; }\n }\n (*out_idx)[k] = best_i;\n err = err + best_d;\n }\n return err;\n}\n\nstruct BestMode6 {\n e0_7: vec4<i32>, e1_7: vec4<i32>,\n p0: u32, p1: u32,\n indices: array<u32, 16>,\n err: i32,\n};\n\n// Exhaustive p-bit search (high path); commits to `*best` only on improvement.\nfn try_pbit_combos(\n pixels: ptr<function, array<vec4<i32>, 16>>,\n ideal0: vec4<i32>,\n ideal1: vec4<i32>,\n best: ptr<function, BestMode6>,\n) {\n var local_best = (*best).err;\n var pal: array<vec4<i32>, 16>;\n var tmp: array<u32, 16>;\n for (var p0: u32 = 0u; p0 < 2u; p0 = p0 + 1u) {\n let q0 = quantize_endpoint(ideal0, p0);\n for (var p1: u32 = 0u; p1 < 2u; p1 = p1 + 1u) {\n let q1 = quantize_endpoint(ideal1, p1);\n build_palette_6(q0.eight, q1.eight, &pal);\n let err = assign_all(pixels, &pal, &tmp);\n if (err < local_best) {\n local_best = err;\n (*best).e0_7 = q0.seven;\n (*best).e1_7 = q1.seven;\n (*best).p0 = p0;\n (*best).p1 = p1;\n (*best).indices = tmp;\n (*best).err = err;\n }\n }\n }\n}\n\n// Exact-weight LSQ refit (high path); matches bc7_ref.ts `refitEndpointsMode6`.\nstruct RefitResult { e0: vec4<i32>, e1: vec4<i32>, valid: bool };\nfn refit_endpoints(\n pixels: ptr<function, array<vec4<i32>, 16>>,\n indices: ptr<function, array<u32, 16>>,\n) -> RefitResult {\n var sAA: f32 = 0.0;\n var sBB: f32 = 0.0;\n var sAB: f32 = 0.0;\n var sAV: vec4<f32> = vec4<f32>(0.0);\n var sBV: vec4<f32> = vec4<f32>(0.0);\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let i = (*indices)[k];\n let a = f32(64u - w4(i)) / 64.0;\n let b = f32(w4(i)) / 64.0;\n let v = vec4<f32>((*pixels)[k]);\n sAA = sAA + a * a;\n sBB = sBB + b * b;\n sAB = sAB + a * b;\n sAV = sAV + a * v;\n sBV = sBV + b * v;\n }\n let det = sAA * sBB - sAB * sAB;\n var out: RefitResult;\n if (abs(det) < 1e-9) {\n out.valid = false;\n return out;\n }\n let e0f = (sBB * sAV - sAB * sBV) / det;\n let e1f = (sAA * sBV - sAB * sAV) / det;\n out.e0 = vec4<i32>(clamp(round(e0f), vec4<f32>(0.0), vec4<f32>(255.0)));\n out.e1 = vec4<i32>(clamp(round(e1f), vec4<f32>(0.0), vec4<f32>(255.0)));\n out.valid = true;\n return out;\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). Only the fast branch consumes the moment\n // accumulators \u2014 the QUALITY_HIGH pipeline dead-code-eliminates them.\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 // Both branches produce: 7-bit endpoints + p-bits, and the 16 4-bit indices\n // packed LSB-first into two nibble words (pixel k \u2192 bits 4k..4k+3).\n var e0_7: vec4<i32>;\n var e1_7: vec4<i32>;\n var p0: u32;\n var p1: u32;\n var ilo: u32 = 0u;\n var ihi: u32 = 0u;\n\n if (QUALITY_HIGH != 0u) {\n let fp = farthest_pair(&pixels);\n var best: BestMode6;\n best.err = 2147483647;\n try_pbit_combos(&pixels, fp.a, fp.b, &best);\n let refit = refit_endpoints(&pixels, &best.indices);\n if (refit.valid) {\n try_pbit_combos(&pixels, refit.e0, refit.e1, &best);\n }\n e0_7 = best.e0_7; e1_7 = best.e1_7; p0 = best.p0; p1 = best.p1;\n for (var k: u32 = 0u; k < 8u; k = k + 1u) {\n ilo = ilo | (best.indices[k] << (k * 4u));\n }\n for (var k: u32 = 8u; k < 16u; k = k + 1u) {\n ihi = ihi | (best.indices[k] << ((k - 8u) * 4u));\n }\n } else {\n // Seed endpoints from the block's principal colour axis (covariance\n // power-iteration; bbox on degenerate blocks) at the exact projection\n // extents, quantise, and assign indices in one projection pass. No LSQ\n // refit: with the seed already on the principal axis, mode 6's 16-level\n // palette leaves the refit \u22640.05 dB on the colour card and \u22640.15 dB on\n // the normal card \u2014 not worth its two extra 16-pixel passes (the coarse\n // 4-level BC1/ASTC fast paths DO keep theirs).\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 let ep0 = pick_ep(seed0);\n let 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 e0_7 = ep0.seven; e1_7 = ep1.seven; p0 = ep0.p; p1 = ep1.p;\n }\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";
1503
+ 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";
875
1504
 
876
1505
  // src/bc7_fast_f16.wgsl
877
- var bc7_fast_f16_default = `// bc7 "fast" encoder \u2014 f16 variant (requires the shader-f16 feature).
878
- // Same algorithm family as the f32 fast path in bc7.wgsl (principal-axis
879
- // seed at the exact projection extents \u2192 quantise \u2192 one projection-based
880
- // index-assignment pass), tuned for throughput:
881
- //
882
- // \u2022 All projection math in f16 ([0,1] domain). ~2\xD7 ALU throughput on
883
- // f16-capable GPUs. The projection direction is pre-scaled by 32:
884
- // a shallow block (endpoints ~1/255 apart) has dd = dot(dir,dir) \u2248 1.5e-5,
885
- // where 15/dd \u2248 10\u2076 overflows f16 (max 65504) to +inf and the products
886
- // inside the projection dot are subnormal \u2014 the indices turn to garbage
887
- // (visible as banding on smooth gradients). Scaling dir by 32 multiplies
888
- // the dots by 32 and dd by 1024; s = dot\xB7(32\xB715/dd\u2083\u2082) is the same
889
- // quantity with every intermediate in f16's normal range (worst case
890
- // inv = 480/0.0157 \u2248 3.0e4 < 65504).
891
- // \u2022 NO least-squares refit, unlike the BC1/BC5/ASTC fast paths: with the
892
- // seed already on the principal axis at the exact projection extents,
893
- // mode 6's fine 16-level palette leaves the refit \u22640.05 dB on the colour
894
- // card and \u22640.15 dB on the normal card \u2014 not worth its two extra
895
- // 16-pixel passes. The coarse 4-level formats DO need it (dropping it
896
- // there costs 0.5\u20131.3 dB).
897
- // \u2022 Indices are packed into two u32 nibble words ON THE FLY during the
898
- // projection pass \u2014 no array<u32,16> private array. The BC7 anchor
899
- // reflection (i \u2192 15\u2212i) is then just a bitwise NOT of both words.
900
- // \u2022 The 128-bit block is assembled with straight-line constant shifts
901
- // instead of a generic write_bits() helper (whose dynamic word indexing
902
- // defeats register promotion of the output array).
903
- //
904
- // The host selects this module only when the device reports shader-f16,
905
- // falling back to bc7.wgsl otherwise. "high" never uses this.
906
- //
907
- // MODE 6 BIT LAYOUT (LSB-first): see bc7.wgsl. Summary:
908
- // w0: mode(7 bits, 0x40) R0 R1 G0 G1[3:0]
909
- // w1: G1[6:4] B0 B1 A0 A1 P0
910
- // w2: P1, pixel0 index (3 bits), pixels 1..7 (4 bits each)
911
- // w3: pixels 8..15 (4 bits each)
912
- enable f16;
913
- struct Params { blocks_x: u32, blocks_y: u32, width: u32, height: u32, };
914
- @group(0) @binding(0) var src_tex: texture_2d<f32>;
915
- @group(0) @binding(1) var<storage, read_write> dst: array<u32>;
916
- @group(0) @binding(2) var<uniform> params: Params;
917
- alias h = f16;
918
- alias h4 = vec4<f16>;
919
-
920
- // Quantise an ideal endpoint (h4 in [0,1]) to 7-bit + p-bit, choosing the
921
- // p-bit with the lower quantisation error. \`eight\` is the decoded value the
922
- // hardware will interpolate with, back in [0,1].
923
- struct Ep { seven: vec4<u32>, eight: h4, p: u32 };
924
- fn pick_ep(ideal01: h4) -> Ep {
925
- let ideal = ideal01 * h(255.0);
926
- let q0 = clamp(floor(ideal * h(0.5) + h(0.5)), h4(0.0), h4(127.0)); // p=0
927
- let e0 = q0 * h(2.0);
928
- let q1 = clamp(floor((ideal - h(1.0)) * h(0.5) + h(0.5)), h4(0.0), h4(127.0)); // p=1
929
- let e1 = q1 * h(2.0) + h(1.0);
930
- let d0 = e0 - ideal; let d1 = e1 - ideal;
931
- if (dot(d1, d1) < dot(d0, d0)) { return Ep(vec4<u32>(q1), e1 * h(1.0 / 255.0), 1u); }
932
- return Ep(vec4<u32>(q0), e0 * h(1.0 / 255.0), 0u);
933
- }
934
-
935
- @compute @workgroup_size(8, 8, 1)
936
- fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
937
- if (gid.x >= params.blocks_x || gid.y >= params.blocks_y) { return; }
938
- let bi = gid.y * params.blocks_x + gid.x;
939
- let base = vec2<i32>(i32(gid.x) * 4, i32(gid.y) * 4);
940
- let mx = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);
941
-
942
- // Load pass, with the covariance moments FUSED in (no separate 16-pixel
943
- // pass): d = (px \u2212 pixel0)\xB716, relative to the block's first pixel so the
944
- // accumulators scale with the block's span \u2014 raw \u03A3v\xB7v\u1D40 moments would
945
- // cancel catastrophically in f16 \u2014 and pre-scaled \xD716 so shallow blocks
946
- // (span ~1/255 \u2192 d\xB2 \u2248 1e-3) clear the subnormal floor while full-range
947
- // sums stay \u22644096. C = \u03A3dd\u1D40 \u2212 (\u03A3d)(\u03A3d)\u1D40/16 is the \xD7256-scaled covariance.
948
- var pix: array<h4, 16>;
949
- var lo = h4(1.0);
950
- var hi = h4(0.0);
951
- var p0v = h4(0.0);
952
- var sd = h4(0.0);
953
- var c0v = h4(0.0);
954
- var c1v = h4(0.0);
955
- var c2v = h4(0.0);
956
- var c3v = h4(0.0);
957
- for (var i: u32 = 0u; i < 16u; i = i + 1u) {
958
- let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);
959
- let px = h4(textureLoad(src_tex, p, 0));
960
- pix[i] = px; lo = min(lo, px); hi = max(hi, px);
961
- if (i == 0u) { p0v = px; }
962
- let d = (px - p0v) * h(16.0);
963
- sd = sd + d;
964
- c0v = c0v + d.x * d;
965
- c1v = c1v + d.y * d;
966
- c2v = c2v + d.z * d;
967
- c3v = c3v + d.w * d;
968
- }
969
- let mean = p0v + sd * h(1.0 / 256.0);
970
- // Mean-correction via sd4\xB7sd4\u1D40 with sd4 = \u03A3d/4: (\u03A3d)(\u03A3d)\u1D40/16 with every
971
- // product \u22644096 (a direct \u03A3d\xB7\u03A3d\u1D40 could hit 65536 and overflow f16).
972
- let sd4 = sd * h(0.25);
973
- c0v = c0v - sd4.x * sd4;
974
- c1v = c1v - sd4.y * sd4;
975
- c2v = c2v - sd4.z * sd4;
976
- c3v = c3v - sd4.w * sd4;
977
-
978
- // Seed endpoints from the block's principal colour axis (covariance
979
- // power-iteration, seeded with the bbox diagonal \u2014 same family as the BC1
980
- // 'high' path). The bbox diagonal is sign-blind: on anti-correlated
981
- // channels (normal maps, hue edges) it points across the data instead of
982
- // along it, and the LSQ refit \u2014 which fits endpoints GIVEN the projection
983
- // indices \u2014 can't recover from a wrong axis. The iteration renormalises by
984
- // the max component (a plain length() of the matvec output could overflow
985
- // f16), so only the direction survives.
986
- var seed_lo = lo;
987
- var seed_hi = hi;
988
- var axis = hi - lo;
989
- var axis_ok = true;
990
- for (var it: u32 = 0u; it < 4u; it = it + 1u) {
991
- let nv = h4(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis), dot(c3v, axis));
992
- let m = max(max(abs(nv.x), abs(nv.y)), max(abs(nv.z), abs(nv.w)));
993
- if (m < h(1e-4)) { axis_ok = false; break; }
994
- axis = nv / m;
995
- }
996
- if (axis_ok) {
997
- axis = axis / length(axis);
998
- // Exact projection extents along the axis. (A Rayleigh-quotient span
999
- // estimate was tried in place of this pass \u2014 it saves 16 dots but costs
1000
- // 0.1\u20130.8 dB and 4\u201310\xD7 on the worst-easy-block gate: \u03C3 misjudges
1001
- // two-cluster and outlier blocks and the quantised weight grid can't
1002
- // recover. The pass stays.)
1003
- var t_min = h(4.0);
1004
- var t_max = h(-4.0);
1005
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1006
- let t = dot(pix[k] - mean, axis);
1007
- t_min = min(t_min, t);
1008
- t_max = max(t_max, t);
1009
- }
1010
- seed_lo = clamp(mean + t_min * axis, h4(0.0), h4(1.0));
1011
- seed_hi = clamp(mean + t_max * axis, h4(0.0), h4(1.0));
1012
- }
1013
-
1014
- // Fit from the principal-axis seed (bbox on degenerate blocks), then
1015
- // quantise the refit endpoints. The refit is clamped to the block bbox: on
1016
- // multi-cluster blocks the unconstrained solve extrapolates far outside
1017
- // the block's colours and the per-channel [0,1] clamp then bends the hue \u2014
1018
- // fringe pixels decode to colours that exist nowhere in the block.
1019
- // Constraining to the bbox also measures better in plain SSE (+1.3 dB on
1020
- // the colour test card).
1021
- // Quantise the PCA-extents seed directly \u2014 no LSQ refit (see header).
1022
- var ep0 = pick_ep(seed_lo);
1023
- var ep1 = pick_ep(seed_hi);
1024
-
1025
- // Final projection against the decoded endpoints, packing the 4-bit indices
1026
- // into two nibble words as we go (pixel k \u2192 bits 4k..4k+3 of ilo/ihi).
1027
- var ilo: u32 = 0u;
1028
- var ihi: u32 = 0u;
1029
- // Same \xD732 pre-scale as proj_fit; distinct quantised endpoints are \u22651/255
1030
- // apart, i.e. dd\u2083\u2082 \u2265 0.0157, so the flat-block threshold only catches
1031
- // truly identical endpoints.
1032
- let dir = (ep1.eight - ep0.eight) * h(32.0);
1033
- let dd = dot(dir, dir);
1034
- if (dd >= h(0.008)) {
1035
- let inv = h(480.0) / dd;
1036
- for (var k: u32 = 0u; k < 8u; k = k + 1u) {
1037
- let s = clamp(floor(dot(pix[k] - ep0.eight, dir) * inv + h(0.5)), h(0.0), h(15.0));
1038
- ilo = ilo | (u32(s) << (k * 4u));
1039
- }
1040
- for (var k: u32 = 8u; k < 16u; k = k + 1u) {
1041
- let s = clamp(floor(dot(pix[k] - ep0.eight, dir) * inv + h(0.5)), h(0.0), h(15.0));
1042
- ihi = ihi | (u32(s) << ((k - 8u) * 4u));
1043
- }
1044
- }
1045
-
1046
- // Anchor rule \u2014 pixel 0's index MSB must be 0. Swapping endpoints reflects
1047
- // every index (i \u2192 15\u2212i), which on packed nibbles is a bitwise NOT.
1048
- if ((ilo & 0x8u) != 0u) {
1049
- let t = ep0; ep0 = ep1; ep1 = t;
1050
- ilo = ~ilo; ihi = ~ihi;
1051
- }
1052
-
1053
- // Straight-line mode-6 packing (see layout above).
1054
- let e0 = ep0.seven;
1055
- let e1 = ep1.seven;
1056
- let w0 = 0x40u | (e0.x << 7u) | (e1.x << 14u) | (e0.y << 21u) | (e1.y << 28u);
1057
- let w1 = (e1.y >> 4u) | (e0.z << 3u) | (e1.z << 10u) | (e0.w << 17u) | (e1.w << 24u) | (ep0.p << 31u);
1058
- let w2 = ep1.p | ((ilo & 0x7u) << 1u) | (ilo & 0xFFFFFFF0u);
1059
- let w3 = ihi;
1060
-
1061
- let o = bi * 4u;
1062
- dst[o] = w0; dst[o + 1u] = w1; dst[o + 2u] = w2; dst[o + 3u] = w3;
1063
- }
1064
- `;
1506
+ 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";
1065
1507
 
1066
1508
  // src/BC7Encoder.ts
1067
1509
  var BC7Encoder = class extends Encoder {
1068
1510
  static requiredFeature = WebGPUFeature.BC;
1069
1511
  static textureFormats = [TextureFormat.BC7, TextureFormat.BC7_SRGB];
1512
+ adaptiveMode4;
1513
+ constructor(opts) {
1514
+ super(opts);
1515
+ this.adaptiveMode4 = opts.adaptiveMode4 === true;
1516
+ if (this.adaptiveMode4) this._buildPipeline();
1517
+ }
1518
+ pipelineConstants() {
1519
+ if (this.adaptiveMode4 && this._useF16) return { enable_mode4: 1 };
1520
+ return void 0;
1521
+ }
1070
1522
  get label() {
1071
1523
  return "bc7";
1072
1524
  }
@@ -1076,9 +1528,6 @@ var BC7Encoder = class extends Encoder {
1076
1528
  get supportsSrgb() {
1077
1529
  return true;
1078
1530
  }
1079
- get supportsQuality() {
1080
- return true;
1081
- }
1082
1531
  wgslSource() {
1083
1532
  return bc7_default;
1084
1533
  }
@@ -1091,35 +1540,60 @@ var BC7Encoder = class extends Encoder {
1091
1540
  };
1092
1541
 
1093
1542
  // src/astc4x4.wgsl
1094
- 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]`.\n//\n// QUALITY LEVELS (pipeline-overridable constant `QUALITY_HIGH`)\n// fast (0, default): principal-axis seed (covariance power-iteration; bbox\n// on degenerate blocks) \u2192 one fused pass that projects each pixel onto\n// the endpoint line (the 4 palette entries are colinear, so the nearest\n// is the rounded projection \u2014 no per-entry search) while accumulating the\n// least-squares refit sums, then a reprojection against the quantised\n// refit endpoints with the weights packed on the fly. The endpoint\n// ordering rule is applied before the weight pass, so no reflection is\n// needed.\n// high (1): O(N\xB2) farthest-pair seed, full 4-entry nearest search, one LSQ\n// refit \u2014 matches astc4x4_ref.ts up to FP tie-breaks.\n// The fast branch is selected at pipeline-compile time; the driver eliminates\n// the unused (high) code.\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\n// 0 = fast (default), 1 = exhaustive/high-quality. Set via pipeline constants.\noverride QUALITY_HIGH: u32 = 0u;\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 weight_unq(i: u32) -> i32 {\n switch i {\n case 0u: { return 0; }\n case 1u: { return 21; }\n case 2u: { return 43; }\n default: { return 64; } // case 3u\n }\n}\n\nfn interp4(e0: vec4<i32>, e1: vec4<i32>, w: i32) -> vec4<i32> {\n return ((64 - w) * e0 + w * e1 + vec4<i32>(32)) >> vec4<u32>(6u);\n}\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// ============================ FAST PATH ================================ //\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 by the fast path to seed\n// the LSQ fit \u2014 the bbox diagonal is sign-blind and points across\n// anti-correlated data (normal 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// ============================ HIGH PATH ================================ //\n\nstruct Pair { a: vec4<i32>, b: vec4<i32> };\nfn farthest_pair(pixels: ptr<function, array<vec4<i32>, 16>>) -> Pair {\n var best_d: i32 = 0;\n var pa = (*pixels)[0];\n var pb = (*pixels)[1];\n for (var i: u32 = 0u; i < 16u; i = i + 1u) {\n let xi = (*pixels)[i];\n for (var j: u32 = i + 1u; j < 16u; j = j + 1u) {\n let d = dist2(xi, (*pixels)[j]);\n if (d > best_d) { best_d = d; pa = xi; pb = (*pixels)[j]; }\n }\n }\n return Pair(pa, pb);\n}\n\nfn build_palette(e0: vec4<i32>, e1: vec4<i32>, pal: ptr<function, array<vec4<i32>, 4>>) {\n for (var i: u32 = 0u; i < 4u; i = i + 1u) {\n (*pal)[i] = interp4(e0, e1, weight_unq(i));\n }\n}\n\nfn assign_all(\n pixels: ptr<function, array<vec4<i32>, 16>>,\n pal: ptr<function, array<vec4<i32>, 4>>,\n out_idx: ptr<function, array<u32, 16>>,\n) -> i32 {\n var err: i32 = 0;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let px = (*pixels)[k];\n var best_i: u32 = 0u;\n var best_d: i32 = 2147483647;\n for (var i: u32 = 0u; i < 4u; i = i + 1u) {\n let d = dist2(px, (*pal)[i]);\n if (d < best_d) { best_d = d; best_i = i; }\n }\n (*out_idx)[k] = best_i;\n err = err + best_d;\n }\n return err;\n}\n\nstruct RefitResult { e0: vec4<i32>, e1: vec4<i32>, valid: bool };\nfn refit_endpoints(\n pixels: ptr<function, array<vec4<i32>, 16>>,\n indices: ptr<function, array<u32, 16>>,\n) -> RefitResult {\n var sAA: f32 = 0.0;\n var sBB: f32 = 0.0;\n var sAB: f32 = 0.0;\n var sAV: vec4<f32> = vec4<f32>(0.0);\n var sBV: vec4<f32> = vec4<f32>(0.0);\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let unq = weight_unq((*indices)[k]);\n let a = f32(64 - unq) / 64.0;\n let b = f32(unq) / 64.0;\n let v = vec4<f32>((*pixels)[k]);\n sAA = sAA + a * a;\n sBB = sBB + b * b;\n sAB = sAB + a * b;\n sAV = sAV + a * v;\n sBV = sBV + b * v;\n }\n let det = sAA * sBB - sAB * sAB;\n var out: RefitResult;\n if (abs(det) < 1e-9) {\n out.valid = false;\n return out;\n }\n let e0f = (sBB * sAV - sAB * sBV) / det;\n let e1f = (sAA * sBV - sAB * sAV) / det;\n out.e0 = vec4<i32>(clamp(round(e0f), vec4<f32>(0.0), vec4<f32>(255.0)));\n out.e1 = vec4<i32>(clamp(round(e1f), vec4<f32>(0.0), vec4<f32>(255.0)));\n out.valid = true;\n return out;\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 // Both branches produce the final endpoints (already ordered so the\n // decoder doesn't apply blue contraction) and the packed weight word\n // (weight k's lsb at bit 31\u22122k, msb at bit 30\u22122k).\n var e0: vec4<i32>;\n var e1: vec4<i32>;\n var w3: u32 = 0u;\n\n if (QUALITY_HIGH != 0u) {\n let fp = farthest_pair(&pixels);\n e0 = fp.a;\n e1 = fp.b;\n var indices: array<u32, 16>;\n var pal: array<vec4<i32>, 4>;\n build_palette(e0, e1, &pal);\n var err = assign_all(&pixels, &pal, &indices);\n let refit = refit_endpoints(&pixels, &indices);\n if (refit.valid) {\n build_palette(refit.e0, refit.e1, &pal);\n var idx2: array<u32, 16>;\n let err2 = assign_all(&pixels, &pal, &idx2);\n if (err2 < err) {\n e0 = refit.e0;\n e1 = refit.e1;\n indices = idx2;\n err = err2;\n }\n }\n // Endpoint ordering, reflecting the assigned weights.\n if (e0.x + e0.y + e0.z > e1.x + e1.y + e1.z) {\n let tmp = e0; e0 = e1; e1 = tmp;\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n indices[k] = 3u - indices[k];\n }\n }\n for (var k: u32 = 0u; k < 16u; k = k + 1u) {\n let w = indices[k] & 0x3u;\n w3 = w3 | ((w & 1u) << (31u - 2u * k)) | (((w >> 1u) & 1u) << (30u - 2u * k));\n }\n } else {\n // Fused LSQ fit seeded from the block's principal colour axis\n // (covariance power-iteration; bbox on degenerate blocks), quantised\n // refit endpoints, ordering applied BEFORE the weight pass so no\n // 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 e0 = lo;\n 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 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\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";
1543
+ 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";
1095
1544
 
1096
1545
  // src/astc4x4_fast_f16.wgsl
1097
1546
  var astc4x4_fast_f16_default = `// astc4x4 "fast" encoder \u2014 f16 variant (requires the shader-f16 feature).
1098
- // Same algorithm family as the f32 fast path in astc4x4.wgsl (principal-axis
1099
- // seed \u2192 projection weight assignment with a fused least-squares refit \u2192
1100
- // reproject), tuned for throughput:
1547
+ // Same algorithm family as the f32 fallback in astc4x4.wgsl (per-block
1548
+ // class selection, principal-axis seed \u2192 projection weight assignment with
1549
+ // a fused least-squares refit \u2192 reproject), tuned for throughput:
1101
1550
  //
1102
- // \u2022 All projection / refit math in f16 ([0,1] domain). The projection
1103
- // direction is pre-scaled by 32 \u2014 a shallow block (endpoints ~1/255
1104
- // apart) has dd \u2248 1.5e-5, where 3/dd \u2248 2e5 overflows f16 (max 65504) to
1105
- // +inf and the projection dots go subnormal, turning weights and the LSQ
1106
- // refit to garbage (banding on smooth gradients). Scaling dir by 32 puts
1107
- // every intermediate in f16's normal range; s = dot\xB7(32\xB73/dd\u2083\u2082) is the
1108
- // same quantity (worst case inv = 96/0.0157 \u2248 6.1e3).
1109
- // \u2022 The seed pass only accumulates the LSQ sums (no weight output) \u2014 the
1110
- // final weights come from reprojecting against the refit endpoints.
1551
+ // \u2022 THREE block classes, picked per block from the loaded pixels (the
1552
+ // ASTC bit budget trades endpoint bits against weight bits, so a block
1553
+ // only pays for the channels it uses):
1554
+ // gray + opaque \u2192 CEM 0 (luminance), 5-bit weights, mode 0x253
1555
+ // opaque \u2192 CEM 8 (RGB), 3-bit weights, mode 0x053
1556
+ // translucent \u2192 CEM 12 (RGBA), 2-bit weights, mode 0x042
1557
+ // "gray" = every texel R == G == B exactly (f16 equality is exact for
1558
+ // 8-bit sources), "opaque" = every texel A == 1. The gray path is
1559
+ // scalar \u2014 no covariance, no power iteration, no 4-D fit \u2014 and runs
1560
+ // [0,255]-integer f16 math like the BC5 kernel (exact endpoints, and
1561
+ // 64/span \u2264 64 never overflows f16, unlike a [0,1]-domain 1/dd).
1562
+ // \u2022 All projection / refit math in f16 ([0,1] domain, colour paths).
1563
+ // The projection direction is pre-scaled by 32 \u2014 a shallow block
1564
+ // (endpoints ~1/255 apart) has dd \u2248 1.5e-5, where 3/dd \u2248 2e5 overflows
1565
+ // f16 (max 65504) to +inf and the projection dots go subnormal,
1566
+ // turning weights and the LSQ refit to garbage (banding on smooth
1567
+ // gradients). Scaling dir by 32 puts every intermediate in f16's
1568
+ // normal range; s = dot\xB7(32\xB7L/dd\u2083\u2082) is the same quantity (worst case
1569
+ // for L = 7 levels: inv = 224/0.0157 \u2248 1.4e4).
1570
+ // \u2022 Opaque blocks ship the quantised PCA extents directly (no LSQ fit
1571
+ // pass, 8 power iterations \u2014 see the endpoint-selection comment); only
1572
+ // the translucent CEM 12 path still refits, and its seed pass only
1573
+ // accumulates the LSQ sums \u2014 final weights always come from a
1574
+ // reprojection against the final endpoints.
1111
1575
  // \u2022 Endpoint ordering (the blue-contraction rule: sum(e0.rgb) must not
1112
1576
  // exceed sum(e1.rgb)) is applied BEFORE the final projection, so no
1113
1577
  // weight-reflection pass is needed.
1114
- // \u2022 Weights are packed into the reversed-bit-order field on the fly, and
1115
- // the 128-bit block is assembled with straight-line constant shifts
1116
- // instead of a generic write_bits() helper.
1578
+ // \u2022 Weight streams are accumulated LSB-first into u32 words and placed
1579
+ // into the block's reversed-bit-order field with reverseBits() \u2014
1580
+ // stream bit q lives at block bit 127 \u2212 q, so a whole stream word
1581
+ // maps onto a block word with a single bit reversal.
1582
+ // \u2022 The covariance moments stay in their OWN pass, deliberately: fusing
1583
+ // them into the load loop bc7-style (pixel-0 residuals, with or without
1584
+ // hoisting pixel 0 out of the loop) measured +5% GPU time on 4096\xB2
1585
+ // (/ab A/B, 2026-07, M3) \u2014 the separate loop overlaps the 16 texture
1586
+ // loads' latency better than a longer in-loop dependency chain does.
1587
+ // Per-pass cost on the same rig, for future tuning: covariance+power-
1588
+ // iteration \u2248 18%, LSQ fit pass \u2248 24%, extents pass \u2248 6% of the kernel;
1589
+ // @workgroup_size 16\xD78 measured exactly at par with 8\xD78.
1117
1590
  //
1118
- // RESTRICTED SUBSET + BLOCK LAYOUT: see astc4x4.wgsl (single partition,
1119
- // CEM 12, 8-bit endpoints, 2-bit weights).
1591
+ // RESTRICTED SUBSET + BLOCK LAYOUT: see astc4x4_ref.ts (single partition,
1592
+ // CEM 0/8/12, 8-bit endpoints, plain-bit weight ISE, block modes
1593
+ // 0x253/0x053/0x042).
1120
1594
  //
1121
1595
  // The host selects this module only when the device reports shader-f16,
1122
- // falling back to astc4x4.wgsl otherwise. "high" never uses this.
1596
+ // falling back to astc4x4.wgsl otherwise.
1123
1597
  enable f16;
1124
1598
  alias h = f16;
1125
1599
  alias h4 = vec4<f16>;
@@ -1128,10 +1602,15 @@ struct Params { blocks_x: u32, blocks_y: u32, width: u32, height: u32, };
1128
1602
  @group(0) @binding(1) var<storage, read_write> dst: array<u32>;
1129
1603
  @group(0) @binding(2) var<uniform> params: Params;
1130
1604
 
1131
- struct Fit { e0: h4, e1: h4, valid: bool };
1605
+ // Fused projection + least-squares refit against the 4-level QUANT_4
1606
+ // palette \u2014 only the translucent (CEM 12) path still refits: the 2-bit
1607
+ // weight grid is coarse enough to need it, while the opaque paths get
1608
+ // more from spending the same time elsewhere (see header).
1609
+ struct Fit { e0: h4, e1: h4, valid: bool, wstream: u32 };
1132
1610
  fn proj_fit(pix: ptr<function, array<h4, 16>>, e0: h4, e1: h4) -> Fit {
1133
1611
  var out: Fit;
1134
1612
  out.valid = false;
1613
+ out.wstream = 0u;
1135
1614
  // dir pre-scaled by 32 to keep dd and the projection dots in f16's normal
1136
1615
  // range (see header). Spans below ~0.7 of an 8-bit step (dd\u2083\u2082 < 0.008,
1137
1616
  // possible only for non-8-bit sources) are treated as flat.
@@ -1149,8 +1628,10 @@ fn proj_fit(pix: ptr<function, array<h4, 16>>, e0: h4, e1: h4) -> Fit {
1149
1628
  for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1150
1629
  let vr = (*pix)[k] - e0;
1151
1630
  let s = clamp(floor(dot(vr, dir) * inv + h(0.5)), h(0.0), h(3.0));
1631
+ out.wstream = out.wstream | (u32(s) << (2u * k));
1152
1632
  s_min = min(s_min, s); s_max = max(s_max, s);
1153
- let b = s * h(1.0 / 3.0); let a = h(1.0) - b;
1633
+ let b = s * h(1.0 / 3.0);
1634
+ let a = h(1.0) - b;
1154
1635
  sAA = sAA + a * a; sBB = sBB + b * b; sAB = sAB + a * b;
1155
1636
  sAV = sAV + a * vr; sBV = sBV + b * vr;
1156
1637
  }
@@ -1178,102 +1659,218 @@ fn encode(@builtin(global_invocation_id) gid: vec3<u32>) {
1178
1659
  let base = vec2<i32>(i32(gid.x) * 4, i32(gid.y) * 4);
1179
1660
  let mx = vec2<i32>(i32(params.width) - 1, i32(params.height) - 1);
1180
1661
 
1662
+ // Load pass. gd tracks the largest chroma deviation \u2014 0 iff the block is
1663
+ // exactly grayscale (equal 8-bit channels convert to identical f16s).
1181
1664
  var pix: array<h4, 16>;
1182
1665
  var lo = h4(1.0);
1183
1666
  var hi = h4(0.0);
1184
1667
  var mean = h4(0.0);
1668
+ var gd = h(0.0);
1185
1669
  for (var i: u32 = 0u; i < 16u; i = i + 1u) {
1186
1670
  let p = clamp(base + vec2<i32>(i32(i & 3u), i32(i >> 2u)), vec2<i32>(0), mx);
1187
1671
  let px = h4(textureLoad(src_tex, p, 0));
1188
1672
  pix[i] = px; lo = min(lo, px); hi = max(hi, px);
1189
1673
  mean = mean + px;
1674
+ gd = max(gd, max(abs(px.x - px.y), abs(px.x - px.z)));
1190
1675
  }
1191
1676
  mean = mean * h(1.0 / 16.0);
1677
+ let opaque = lo.w == h(1.0);
1192
1678
 
1193
- // Seed endpoints from the block's principal colour axis (covariance
1194
- // power-iteration, seeded with the bbox diagonal). The bbox diagonal is
1195
- // sign-blind: on anti-correlated channels (normal maps, hue edges) it
1196
- // points across the data instead of along it, and the LSQ refit \u2014 which
1197
- // fits endpoints GIVEN the projection weights \u2014 can't recover from a wrong
1198
- // axis. Deviations are pre-scaled \xD716 so covariance entries for shallow
1199
- // blocks stay in f16's normal range (span ~1/255 \u2192 d\xB2 \u2248 1e-3) while
1200
- // full-range sums stay \u22644096; the iteration renormalises by the max
1201
- // component (a plain length() of the matvec output could overflow f16), so
1202
- // only the direction survives.
1203
- var seed_lo = lo;
1204
- var seed_hi = hi;
1205
- var c0v = h4(0.0);
1206
- var c1v = h4(0.0);
1207
- var c2v = h4(0.0);
1208
- var c3v = h4(0.0);
1209
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1210
- let d = (pix[k] - mean) * h(16.0);
1211
- c0v = c0v + d.x * d;
1212
- c1v = c1v + d.y * d;
1213
- c2v = c2v + d.z * d;
1214
- c3v = c3v + d.w * d;
1215
- }
1216
- var axis = hi - lo;
1217
- var axis_ok = true;
1218
- for (var it: u32 = 0u; it < 4u; it = it + 1u) {
1219
- let nv = h4(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis), dot(c3v, axis));
1220
- let m = max(max(abs(nv.x), abs(nv.y)), max(abs(nv.z), abs(nv.w)));
1221
- if (m < h(1e-4)) { axis_ok = false; break; }
1222
- axis = nv / m;
1223
- }
1224
- if (axis_ok) {
1225
- axis = axis / length(axis);
1226
- var t_min = h(4.0);
1227
- var t_max = h(-4.0);
1228
- for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1229
- let t = dot(pix[k] - mean, axis);
1230
- t_min = min(t_min, t);
1231
- t_max = max(t_max, t);
1679
+ var w0: u32; var w1: u32; var w2: u32; var w3: u32;
1680
+
1681
+ if (opaque && gd == h(0.0)) {
1682
+ // ---------------- Luminance path: CEM 0, 5-bit weights ----------------
1683
+ // Scalar [0,255]-integer domain. Endpoints at the exact extremes (the
1684
+ // 8-bit values round-trip f16 exactly); 32 palette levels make an LSQ
1685
+ // refit unnecessary.
1686
+ let L0 = u32(floor(lo.x * h(255.0) + h(0.5)));
1687
+ let L1 = u32(floor(hi.x * h(255.0) + h(0.5)));
1688
+ var s0 = 0u; var s1 = 0u; var s2 = 0u;
1689
+ if (L1 > L0) {
1690
+ let l0f = h(f32(L0));
1691
+ let sc = h(64.0) / h(f32(L1 - L0)); // span \u2265 1 \u2192 sc \u2264 64, no overflow
1692
+ // Exact nearest entry of the QUANT_32 grid: unq = 2w for w \u2264 15,
1693
+ // 2w + 2 for w \u2265 16 (the grid has a 4-wide gap at the middle, so
1694
+ // uniform rounding is wrong there). Evaluate the best candidate of
1695
+ // each half and keep the closer.
1696
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1697
+ let v = floor(pix[k].x * h(255.0) + h(0.5)) - l0f; // exact integer
1698
+ let u = clamp(v * sc, h(0.0), h(64.0));
1699
+ let wlo = clamp(floor(u * h(0.5) + h(0.5)), h(0.0), h(15.0));
1700
+ let whi = clamp(floor((u - h(2.0)) * h(0.5) + h(0.5)), h(16.0), h(31.0));
1701
+ let pick = abs(u - wlo * h(2.0)) <= abs(u - (whi * h(2.0) + h(2.0)));
1702
+ let w = u32(select(whi, wlo, pick));
1703
+ // Stream bit q = 5k + j; straddles handled with constant shifts.
1704
+ let off = 5u * k;
1705
+ if (off < 28u) { s0 = s0 | (w << off); }
1706
+ else if (off == 30u) { s0 = s0 | (w << 30u); s1 = s1 | (w >> 2u); }
1707
+ else if (off < 60u) { s1 = s1 | (w << (off - 32u)); }
1708
+ else if (off == 60u) { s1 = s1 | (w << 28u); s2 = s2 | (w >> 4u); }
1709
+ else { s2 = s2 | (w << (off - 64u)); }
1710
+ }
1232
1711
  }
1233
- seed_lo = clamp(mean + t_min * axis, h4(0.0), h4(1.0));
1234
- seed_hi = clamp(mean + t_max * axis, h4(0.0), h4(1.0));
1235
- }
1236
-
1237
- // The refit is clamped to the block bbox: on multi-cluster blocks the
1238
- // unconstrained solve extrapolates far outside the block's colours and the
1239
- // per-channel [0,1] clamp then bends the hue \u2014 fringe pixels decode to
1240
- // colours that exist nowhere in the block. Constraining to the bbox also
1241
- // measures better in plain SSE (+1.8 dB on the colour test card).
1242
- let r = proj_fit(&pix, seed_lo, seed_hi);
1243
- var e0 = lo;
1244
- var e1 = hi;
1245
- if (r.valid) { e0 = clamp(r.e0, lo, hi); e1 = clamp(r.e1, lo, hi); }
1246
- var E0 = q8(e0);
1247
- var E1 = q8(e1);
1248
-
1249
- // Blue-contraction ordering, applied before the weight pass so weights are
1250
- // already oriented (no reflection needed).
1251
- if (E0.x + E0.y + E0.z > E1.x + E1.y + E1.z) {
1252
- let t = E0; E0 = E1; E1 = t;
1253
- }
1254
- let d0 = h4(vec4<f32>(E0)) * h(1.0 / 255.0);
1255
- let d1 = h4(vec4<f32>(E1)) * h(1.0 / 255.0);
1256
-
1257
- // Weight pass, packing on the fly: weight k's lsb at bit 31\u22122k of the last
1258
- // word, msb at bit 30\u22122k.
1259
- var w3: u32 = 0u;
1260
- // Same \xD732 pre-scale as proj_fit; distinct 8-bit endpoints are \u22651/255
1261
- // apart (dd\u2083\u2082 \u2265 0.0157), so the threshold only catches identical ones.
1262
- let dir = (d1 - d0) * h(32.0);
1263
- let dd = dot(dir, dir);
1264
- if (dd >= h(0.008)) {
1265
- let inv = h(96.0) / dd;
1712
+ // Mode 0x253, partitions\u22121 = 0, CEM 0, L0 @17, L1 @25 (top bit spills
1713
+ // into w1 bit 0); stream word q\u2208[0,31] \u2192 block bits 127\u202696 via
1714
+ // reverseBits, q\u2208[32,63] \u2192 95\u202664, q\u2208[64,79] \u2192 63\u202648.
1715
+ w0 = 0x253u | (L0 << 17u) | (L1 << 25u);
1716
+ w1 = (L1 >> 7u) | reverseBits(s2);
1717
+ w2 = reverseBits(s1);
1718
+ w3 = reverseBits(s0);
1719
+ } else {
1720
+ // ------------- Colour paths: shared PCA seed + LSQ refit --------------
1721
+ // Seed endpoints from the block's principal colour axis (covariance
1722
+ // power-iteration, seeded with the bbox diagonal). The bbox diagonal is
1723
+ // sign-blind: on anti-correlated channels (normal maps, hue edges) it
1724
+ // points across the data instead of along it, and the LSQ refit \u2014 which
1725
+ // fits endpoints GIVEN the projection weights \u2014 can't recover from a
1726
+ // wrong axis. Deviations are pre-scaled \xD716 so covariance entries for
1727
+ // shallow blocks stay in f16's normal range (span ~1/255 \u2192 d\xB2 \u2248 1e-3)
1728
+ // while full-range sums stay \u22644096; the iteration renormalises by the
1729
+ // max component (a plain length() of the matvec output could overflow
1730
+ // f16), so only the direction survives.
1731
+ var seed_lo = lo;
1732
+ var seed_hi = hi;
1733
+ var c0v = h4(0.0);
1734
+ var c1v = h4(0.0);
1735
+ var c2v = h4(0.0);
1736
+ var c3v = h4(0.0);
1266
1737
  for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1267
- let s = u32(clamp(floor(dot(pix[k] - d0, dir) * inv + h(0.5)), h(0.0), h(3.0)));
1268
- w3 = w3 | ((s & 1u) << (31u - 2u * k)) | (((s >> 1u) & 1u) << (30u - 2u * k));
1738
+ let d = (pix[k] - mean) * h(16.0);
1739
+ c0v = c0v + d.x * d;
1740
+ c1v = c1v + d.y * d;
1741
+ c2v = c2v + d.z * d;
1742
+ c3v = c3v + d.w * d;
1269
1743
  }
1270
- }
1744
+ var axis = hi - lo;
1745
+ var axis_ok = true;
1746
+ // 4 shared iterations, then 4 more for opaque blocks only \u2014 two
1747
+ // FIXED-bound loops rather than one divergent trip count, so both
1748
+ // unroll. Opaque blocks need the converged axis (it IS the endpoint
1749
+ // quality there; 4 was under-converged on noisy 4-D content), while
1750
+ // translucent blocks' LSQ refit absorbs residual axis error \u2014 their
1751
+ // extra 4 steps measured exactly 0.000 dB on the alpha card for
1752
+ // ~5% GPU.
1753
+ for (var it: u32 = 0u; it < 4u; it = it + 1u) {
1754
+ let nv = h4(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis), dot(c3v, axis));
1755
+ let m = max(max(abs(nv.x), abs(nv.y)), max(abs(nv.z), abs(nv.w)));
1756
+ if (m < h(1e-4)) { axis_ok = false; break; }
1757
+ axis = nv / m;
1758
+ }
1759
+ if (axis_ok && opaque) {
1760
+ for (var it: u32 = 0u; it < 4u; it = it + 1u) {
1761
+ let nv = h4(dot(c0v, axis), dot(c1v, axis), dot(c2v, axis), dot(c3v, axis));
1762
+ let m = max(max(abs(nv.x), abs(nv.y)), max(abs(nv.z), abs(nv.w)));
1763
+ if (m < h(1e-4)) { axis_ok = false; break; }
1764
+ axis = nv / m;
1765
+ }
1766
+ }
1767
+ if (axis_ok) {
1768
+ axis = axis / length(axis);
1769
+ var t_min = h(4.0);
1770
+ var t_max = h(-4.0);
1771
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1772
+ let t = dot(pix[k] - mean, axis);
1773
+ t_min = min(t_min, t);
1774
+ t_max = max(t_max, t);
1775
+ }
1776
+ seed_lo = clamp(mean + t_min * axis, h4(0.0), h4(1.0));
1777
+ seed_hi = clamp(mean + t_max * axis, h4(0.0), h4(1.0));
1778
+ }
1779
+
1780
+ // Endpoint selection. OPAQUE blocks (CEM 8, 8-level weights) quantise
1781
+ // the PCA-extent seed directly, BC7-style \u2014 the LSQ fit pass measured
1782
+ // +26% GPU for \u22120.06..+0.31 dB against the converged 8-step axis
1783
+ // (/ab + PSNR A/B, 2026-07): at 8 weight levels the projection is fine
1784
+ // enough that a good axis, not a refit, carries the quality.
1785
+ // TRANSLUCENT blocks (CEM 12) keep the fit: 4 levels are coarse enough
1786
+ // that dropping it costs 0.5+ dB. Its result is clamped to the block
1787
+ // bbox: on multi-cluster blocks the unconstrained solve extrapolates
1788
+ // far outside the block's colours and the per-channel [0,1] clamp then
1789
+ // bends the hue \u2014 fringe pixels decode to colours that exist nowhere
1790
+ // in the block (and the bbox constraint also measures better in plain
1791
+ // SSE, +1.8 dB on the colour test card).
1792
+ var e0 = lo;
1793
+ var e1 = hi;
1794
+ var fitStream = 0u;
1795
+ var haveFitWeights = false;
1796
+ if (opaque) {
1797
+ // Bbox-clamped like the fit output: on multi-cluster blocks the axis
1798
+ // extents overshoot the data per-channel and decode to colours that
1799
+ // exist nowhere in the block (the odd-size padding gate caught a
1800
+ // \u22123.9 dB crop without this).
1801
+ e0 = clamp(seed_lo, lo, hi);
1802
+ e1 = clamp(seed_hi, lo, hi);
1803
+ } else {
1804
+ let r = proj_fit(&pix, seed_lo, seed_hi);
1805
+ if (r.valid) {
1806
+ e0 = clamp(r.e0, lo, hi);
1807
+ e1 = clamp(r.e1, lo, hi);
1808
+ fitStream = r.wstream;
1809
+ haveFitWeights = true;
1810
+ }
1811
+ }
1812
+ var E0 = q8(e0);
1813
+ var E1 = q8(e1);
1814
+
1815
+ // Blue-contraction ordering, applied before the weight pass so weights
1816
+ // are already oriented (no reflection needed).
1817
+ var swapped = false;
1818
+ if (E0.x + E0.y + E0.z > E1.x + E1.y + E1.z) {
1819
+ let t = E0; E0 = E1; E1 = t;
1820
+ swapped = true;
1821
+ }
1822
+ let d0 = h4(vec4<f32>(E0)) * h(1.0 / 255.0);
1823
+ let d1 = h4(vec4<f32>(E1)) * h(1.0 / 255.0);
1271
1824
 
1272
- // Straight-line packing: block mode 0x042 @0, partitions\u22121=0 @11, CEM 12
1273
- // @13, endpoints R0 R1 G0 G1 B0 B1 A0 A1 (8 bits each) from bit 17.
1274
- let w0 = 0x042u | (12u << 13u) | (E0.x << 17u) | (E1.x << 25u);
1275
- let w1 = (E1.x >> 7u) | (E0.y << 1u) | (E1.y << 9u) | (E0.z << 17u) | (E1.z << 25u);
1276
- let w2 = (E1.z >> 7u) | (E0.w << 1u) | (E1.w << 9u);
1825
+ // Weight pass against the decoded endpoints. Same \xD732 pre-scale as
1826
+ // proj_fit; distinct 8-bit endpoints are \u22651/255 apart (dd\u2083\u2082 \u2265 0.0157),
1827
+ // so the flat threshold only catches identical ones.
1828
+ let dir = (d1 - d0) * h(32.0);
1829
+ let dd = dot(dir, dir);
1830
+ if (opaque) {
1831
+ // CEM 8: 3-bit weights, stream bit q = 3k.
1832
+ var s0 = 0u; var s1 = 0u;
1833
+ if (dd >= h(0.008)) {
1834
+ let inv = h(224.0) / dd;
1835
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1836
+ let w = u32(clamp(floor(dot(pix[k] - d0, dir) * inv + h(0.5)), h(0.0), h(7.0)));
1837
+ let off = 3u * k;
1838
+ if (off < 30u) { s0 = s0 | (w << off); }
1839
+ else if (off == 30u) { s0 = s0 | (w << 30u); s1 = s1 | (w >> 2u); }
1840
+ else { s1 = s1 | (w << (off - 32u)); }
1841
+ }
1842
+ }
1843
+ // Mode 0x053, CEM 8 @13, endpoints R0 R1 G0 G1 B0 B1 from bit 17.
1844
+ w0 = 0x053u | (8u << 13u) | (E0.x << 17u) | (E1.x << 25u);
1845
+ w1 = (E1.x >> 7u) | (E0.y << 1u) | (E1.y << 9u) | (E0.z << 17u) | (E1.z << 25u);
1846
+ w2 = (E1.z >> 7u) | reverseBits(s1);
1847
+ w3 = reverseBits(s0);
1848
+ } else {
1849
+ // CEM 12: 2-bit weights, stream bit q = 2k (single stream word).
1850
+ // Valid fits ship the FIT-PASS weights instead of reprojecting \u2014
1851
+ // worth a whole 16-pixel pass for \u22120.09 dB on the alpha card (/ab +
1852
+ // PSNR A/B, 2026-07; the pre-adaptive-CEM encoder rejected this same
1853
+ // trade when EVERY block was CEM 12 \u2014 now only translucent blocks
1854
+ // pay it). The blue-contraction swap is a full reflection w \u2192 3\u2212w,
1855
+ // i.e. bitwise NOT of the packed stream. Invalid fits (rank-1 /
1856
+ // degenerate) fall back to reprojection against the bbox endpoints.
1857
+ var s0 = 0u;
1858
+ if (haveFitWeights) {
1859
+ s0 = select(fitStream, ~fitStream, swapped);
1860
+ } else if (dd >= h(0.008)) {
1861
+ let inv = h(96.0) / dd;
1862
+ for (var k: u32 = 0u; k < 16u; k = k + 1u) {
1863
+ let w = u32(clamp(floor(dot(pix[k] - d0, dir) * inv + h(0.5)), h(0.0), h(3.0)));
1864
+ s0 = s0 | (w << (2u * k));
1865
+ }
1866
+ }
1867
+ // Mode 0x042, CEM 12 @13, endpoints R0 R1 G0 G1 B0 B1 A0 A1 from 17.
1868
+ w0 = 0x042u | (12u << 13u) | (E0.x << 17u) | (E1.x << 25u);
1869
+ w1 = (E1.x >> 7u) | (E0.y << 1u) | (E1.y << 9u) | (E0.z << 17u) | (E1.z << 25u);
1870
+ w2 = (E1.z >> 7u) | (E0.w << 1u) | (E1.w << 9u);
1871
+ w3 = reverseBits(s0);
1872
+ }
1873
+ }
1277
1874
 
1278
1875
  let o = bi * 4u;
1279
1876
  dst[o] = w0; dst[o + 1u] = w1; dst[o + 2u] = w2; dst[o + 3u] = w3;
@@ -1293,9 +1890,6 @@ var ASTC4x4Encoder = class extends Encoder {
1293
1890
  get supportsSrgb() {
1294
1891
  return true;
1295
1892
  }
1296
- get supportsQuality() {
1297
- return true;
1298
- }
1299
1893
  wgslSource() {
1300
1894
  return astc4x4_default;
1301
1895
  }
@@ -1307,6 +1901,36 @@ var ASTC4x4Encoder = class extends Encoder {
1307
1901
  }
1308
1902
  };
1309
1903
 
1904
+ // src/etc2.wgsl
1905
+ 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";
1906
+
1907
+ // src/etc2_fast_f16.wgsl
1908
+ 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";
1909
+
1910
+ // src/ETC2Encoder.ts
1911
+ var ETC2Encoder = class extends Encoder {
1912
+ static requiredFeature = WebGPUFeature.ETC2;
1913
+ static textureFormats = [TextureFormat.ETC2_RGB8, TextureFormat.ETC2_RGB8_SRGB];
1914
+ get label() {
1915
+ return "etc2";
1916
+ }
1917
+ get bytesPerBlock() {
1918
+ return 8;
1919
+ }
1920
+ get supportsSrgb() {
1921
+ return true;
1922
+ }
1923
+ wgslSource() {
1924
+ return etc2_default;
1925
+ }
1926
+ wgslSourceFastF16() {
1927
+ return etc2_fast_f16_default;
1928
+ }
1929
+ gpuTextureFormat({ colorSpace }) {
1930
+ return colorSpace === "srgb" ? "etc2-rgb8unorm-srgb" : "etc2-rgb8unorm";
1931
+ }
1932
+ };
1933
+
1310
1934
  // src/webgl/glsl/fullscreen.vert.glsl
1311
1935
  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";
1312
1936
 
@@ -1518,7 +2142,7 @@ var WebGLBlockEncoder = class {
1518
2142
  };
1519
2143
 
1520
2144
  // src/webgl/glsl/bc1.frag.glsl
1521
- 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. This is the *fast* path only (the WGSL\n// `QUALITY_HIGH == 0` branch): 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 a single\n// least-squares endpoint refit accepted only when it lowers the block's error.\n// 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\nvoid main() {\n ivec2 base = ivec2(gl_FragCoord.xy) * 4;\n ivec2 maxXY = uSrcSize - ivec2(1);\n\n vec3 pixels[16];\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 pixels[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 = pixels[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(pixels[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 // Build the palette in decoded space, assign each pixel its nearest entry.\n vec3 pal[4];\n vec3 p0 = from565(c0);\n vec3 p1 = from565(c1);\n for (int j = 0; j < 4; j++) pal[j] = WA[j] * p0 + WB[j] * p1;\n\n uint idx[16];\n float err = 0.0;\n for (int k = 0; k < 16; k++) {\n vec3 c = pixels[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 idx[k] = bestJ;\n err += bestD;\n }\n\n // One least-squares refit: re-solve the endpoints for the current indices,\n // re-quantise, re-assign; keep it only if the squared error drops.\n float sAA = 0.0, sBB = 0.0, sAB = 0.0;\n vec3 sAV = vec3(0.0), sBV = vec3(0.0);\n for (int k = 0; k < 16; k++) {\n float a = WA[int(idx[k])];\n float b = WB[int(idx[k])];\n vec3 v = pixels[k];\n sAA += a * a; sBB += b * b; sAB += a * b; sAV += a * v; sBV += b * v;\n }\n float det = sAA * sBB - sAB * sAB;\n if (abs(det) > 1e-9) {\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 colours\n // and the per-channel 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.6 dB on the colour test card), so the\n // accept-if-better guard below keeps more refits.\n vec3 e0 = clamp((sBB * sAV - sAB * sBV) / det, bbMin, bbMax);\n vec3 e1 = clamp((sAA * sBV - sAB * 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)) {\n vec3 q0 = from565(nc0);\n vec3 q1 = from565(nc1);\n vec3 pal2[4];\n for (int j = 0; j < 4; j++) pal2[j] = WA[j] * q0 + WB[j] * q1;\n uint idx2[16];\n float nerr = 0.0;\n for (int k = 0; k < 16; k++) {\n vec3 c = pixels[k];\n uint bestJ = 0u;\n float bestD = 1e30;\n for (int j = 0; j < 4; j++) {\n vec3 d = pal2[j] - c;\n float d2 = dot(d, d);\n if (d2 < bestD) { bestD = d2; bestJ = uint(j); }\n }\n idx2[k] = bestJ;\n nerr += bestD;\n }\n if (nerr < err) {\n c0 = nc0; c1 = nc1;\n for (int k = 0; k < 16; k++) idx[k] = idx2[k];\n }\n }\n }\n\n uint indices = 0u;\n for (int k = 0; k < 16; k++) indices |= (idx[k] & 3u) << (uint(k) * 2u);\n\n outColor = uvec4(c0 | (c1 << 16), indices, 0u, 0u);\n}\n";
2145
+ 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";
1522
2146
 
1523
2147
  // src/webgl/BC1WebGLEncoder.ts
1524
2148
  var BC1WebGLEncoder = class extends WebGLBlockEncoder {
@@ -1537,7 +2161,7 @@ var BC1WebGLEncoder = class extends WebGLBlockEncoder {
1537
2161
  };
1538
2162
 
1539
2163
  // src/webgl/glsl/bc5.frag.glsl
1540
- 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 the block's value range (a strict-SSE win vs\n // clamping to [0,1], same as the other formats' fast paths); keep 6-interp\n // 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, vmin, vmax);\n float e1 = clamp((seed.sAA * seed.sBV - seed.sAB * seed.sAV) / det, vmin, vmax);\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";
2164
+ 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";
1541
2165
 
1542
2166
  // src/webgl/BC5WebGLEncoder.ts
1543
2167
  var BC5WebGLEncoder = class extends WebGLBlockEncoder {
@@ -1556,7 +2180,7 @@ var BC5WebGLEncoder = class extends WebGLBlockEncoder {
1556
2180
  };
1557
2181
 
1558
2182
  // src/webgl/glsl/bc7.frag.glsl
1559
- 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). Mirrors the\n// `QUALITY_HIGH == 0` branch of bc7.wgsl; see that file for the mode-6 bit\n// 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";
2183
+ 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";
1560
2184
 
1561
2185
  // src/webgl/BC7WebGLEncoder.ts
1562
2186
  var BC7WebGLEncoder = class extends WebGLBlockEncoder {
@@ -1575,7 +2199,7 @@ var BC7WebGLEncoder = class extends WebGLBlockEncoder {
1575
2199
  };
1576
2200
 
1577
2201
  // src/webgl/glsl/astc4x4.frag.glsl
1578
- 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 the `QUALITY_HIGH == 0` branch of astc4x4.wgsl; see that file\n// 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";
2202
+ 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";
1579
2203
 
1580
2204
  // src/webgl/ASTC4x4WebGLEncoder.ts
1581
2205
  var ASTC4x4WebGLEncoder = class extends WebGLBlockEncoder {
@@ -1611,26 +2235,30 @@ function detectWebGLCapabilities(gl) {
1611
2235
  // src/webgl/selectWebGLFormat.ts
1612
2236
  var NONE = { format: null, encoderClass: null, astcNormalRemap: false };
1613
2237
  function selectWebGLFormat(caps, hint, options = {}) {
1614
- const { colorSpace = "srgb", preferredFormat } = options;
2238
+ const { colorSpace = "srgb", preferredFormat, quality = "high" } = options;
1615
2239
  const srgb = colorSpace === "srgb";
1616
2240
  const astc = (astcNormalRemap) => ({
1617
2241
  format: srgb ? TextureFormat.ASTC_4x4_SRGB : TextureFormat.ASTC_4x4,
1618
2242
  encoderClass: ASTC4x4WebGLEncoder,
1619
2243
  astcNormalRemap
1620
2244
  });
2245
+ const bc1 = () => ({
2246
+ format: srgb ? TextureFormat.BC1_SRGB : TextureFormat.BC1,
2247
+ encoderClass: BC1WebGLEncoder,
2248
+ astcNormalRemap: false
2249
+ });
1621
2250
  if (preferredFormat === "bc1") {
1622
2251
  if (hint !== "color") {
1623
2252
  console.warn(
1624
2253
  `[gputex] preferredFormat 'bc1' ignored for hint '${hint}' \u2014 BC1 has no real alpha channel and is unsuitable for normal maps.`
1625
2254
  );
1626
2255
  } else if (srgb ? caps.s3tcSrgb : caps.s3tc) {
1627
- return {
1628
- format: srgb ? TextureFormat.BC1_SRGB : TextureFormat.BC1,
1629
- encoderClass: BC1WebGLEncoder,
1630
- astcNormalRemap: false
1631
- };
2256
+ return bc1();
1632
2257
  }
1633
2258
  }
2259
+ if (quality === "low" && hint === "color" && (srgb ? caps.s3tcSrgb : caps.s3tc)) {
2260
+ return bc1();
2261
+ }
1634
2262
  if (hint === "normal") {
1635
2263
  if (caps.rgtc) return { format: TextureFormat.BC5, encoderClass: BC5WebGLEncoder, astcNormalRemap: false };
1636
2264
  if (caps.astc) return astc(true);
@@ -1644,35 +2272,40 @@ function selectWebGLFormat(caps, hint, options = {}) {
1644
2272
  };
1645
2273
  }
1646
2274
  if (caps.astc) return astc(false);
1647
- if (hint === "color") {
1648
- if (srgb && caps.s3tcSrgb) {
1649
- return { format: TextureFormat.BC1_SRGB, encoderClass: BC1WebGLEncoder, astcNormalRemap: false };
1650
- }
1651
- if (!srgb && caps.s3tc) {
1652
- return { format: TextureFormat.BC1, encoderClass: BC1WebGLEncoder, astcNormalRemap: false };
1653
- }
2275
+ if (hint === "color" && (srgb ? caps.s3tcSrgb : caps.s3tc)) {
2276
+ return bc1();
1654
2277
  }
1655
2278
  return NONE;
1656
2279
  }
1657
2280
 
1658
2281
  // src/selectFormat.ts
1659
2282
  function selectFormat(adapter, hint, options = {}) {
1660
- const { colorSpace = "srgb", preferredFormat } = options;
2283
+ const { colorSpace = "srgb", preferredFormat, quality = "high" } = options;
1661
2284
  const srgb = colorSpace === "srgb";
1662
2285
  const caps = detectCapabilities(adapter);
2286
+ const bc1 = {
2287
+ format: srgb ? TextureFormat.BC1_SRGB : TextureFormat.BC1,
2288
+ encoderClass: BC1Encoder,
2289
+ astcNormalRemap: false
2290
+ };
2291
+ const etc2 = {
2292
+ format: srgb ? TextureFormat.ETC2_RGB8_SRGB : TextureFormat.ETC2_RGB8,
2293
+ encoderClass: ETC2Encoder,
2294
+ astcNormalRemap: false
2295
+ };
1663
2296
  if (preferredFormat === "bc1") {
1664
2297
  if (hint !== "color") {
1665
2298
  console.warn(
1666
2299
  `[gputex] preferredFormat 'bc1' ignored for hint '${hint}' \u2014 BC1 has no real alpha channel and is unsuitable for normal maps.`
1667
2300
  );
1668
2301
  } else if (caps.bc) {
1669
- return {
1670
- format: srgb ? TextureFormat.BC1_SRGB : TextureFormat.BC1,
1671
- encoderClass: BC1Encoder,
1672
- astcNormalRemap: false
1673
- };
2302
+ return bc1;
1674
2303
  }
1675
2304
  }
2305
+ if (quality === "low" && hint === "color") {
2306
+ if (caps.bc) return bc1;
2307
+ if (caps.etc2) return etc2;
2308
+ }
1676
2309
  if (caps.bc) {
1677
2310
  if (hint === "normal") {
1678
2311
  return { format: TextureFormat.BC5, encoderClass: BC5Encoder, astcNormalRemap: false };
@@ -1691,6 +2324,9 @@ function selectFormat(adapter, hint, options = {}) {
1691
2324
  astcNormalRemap: hint === "normal"
1692
2325
  };
1693
2326
  }
2327
+ if (caps.etc2 && hint === "color") {
2328
+ return etc2;
2329
+ }
1694
2330
  return { format: null, encoderClass: null, astcNormalRemap: false };
1695
2331
  }
1696
2332
 
@@ -1716,24 +2352,28 @@ function downsample2x(src) {
1716
2352
  const dstW = Math.max(1, src.width >> 1);
1717
2353
  const dstH = Math.max(1, src.height >> 1);
1718
2354
  const dst = new Uint8ClampedArray(dstW * dstH * 4);
1719
- const sW = src.width;
1720
- const sMaxX = src.width - 1;
2355
+ const s = src.data;
2356
+ const rowBytes = src.width * 4;
2357
+ const lastColByte = (src.width - 1) * 4;
1721
2358
  const sMaxY = src.height - 1;
2359
+ let o = 0;
1722
2360
  for (let y = 0; y < dstH; y++) {
1723
- const sy0 = y * 2;
1724
- const sy1 = Math.min(sy0 + 1, sMaxY);
2361
+ const sy0 = y << 1;
2362
+ const sy1 = sy0 < sMaxY ? sy0 + 1 : sMaxY;
2363
+ const r0 = sy0 * rowBytes;
2364
+ const r1 = sy1 * rowBytes;
1725
2365
  for (let x = 0; x < dstW; x++) {
1726
- const sx0 = x * 2;
1727
- const sx1 = Math.min(sx0 + 1, sMaxX);
1728
- const i00 = (sy0 * sW + sx0) * 4;
1729
- const i10 = (sy0 * sW + sx1) * 4;
1730
- const i01 = (sy1 * sW + sx0) * 4;
1731
- const i11 = (sy1 * sW + sx1) * 4;
1732
- const o = (y * dstW + x) * 4;
1733
- dst[o] = src.data[i00] + src.data[i10] + src.data[i01] + src.data[i11] + 2 >> 2;
1734
- dst[o + 1] = src.data[i00 + 1] + src.data[i10 + 1] + src.data[i01 + 1] + src.data[i11 + 1] + 2 >> 2;
1735
- dst[o + 2] = src.data[i00 + 2] + src.data[i10 + 2] + src.data[i01 + 2] + src.data[i11 + 2] + 2 >> 2;
1736
- dst[o + 3] = src.data[i00 + 3] + src.data[i10 + 3] + src.data[i01 + 3] + src.data[i11 + 3] + 2 >> 2;
2366
+ const sx0 = x << 3;
2367
+ const sx1 = sx0 + 4 <= lastColByte ? sx0 + 4 : lastColByte;
2368
+ const i00 = r0 + sx0;
2369
+ const i10 = r0 + sx1;
2370
+ const i01 = r1 + sx0;
2371
+ const i11 = r1 + sx1;
2372
+ dst[o] = s[i00] + s[i10] + s[i01] + s[i11] + 2 >> 2;
2373
+ dst[o + 1] = s[i00 + 1] + s[i10 + 1] + s[i01 + 1] + s[i11 + 1] + 2 >> 2;
2374
+ dst[o + 2] = s[i00 + 2] + s[i10 + 2] + s[i01 + 2] + s[i11 + 2] + 2 >> 2;
2375
+ dst[o + 3] = s[i00 + 3] + s[i10 + 3] + s[i01 + 3] + s[i11 + 3] + 2 >> 2;
2376
+ o += 4;
1737
2377
  }
1738
2378
  }
1739
2379
  return { data: dst, width: dstW, height: dstH };
@@ -1760,6 +2400,97 @@ function padToBlockMultiple(level) {
1760
2400
  return { data: out, width: pw, height: ph };
1761
2401
  }
1762
2402
 
2403
+ // src/gpuMipgen.ts
2404
+ function gpuMipLevelCount(width, height) {
2405
+ return 32 - Math.clz32(Math.max(width, height));
2406
+ }
2407
+ var DOWNSAMPLE_WGSL = (
2408
+ /* wgsl */
2409
+ `
2410
+ @group(0) @binding(0) var src : texture_2d<f32>;
2411
+ @group(0) @binding(1) var dst : texture_storage_2d<rgba8unorm, write>;
2412
+
2413
+ // 2x2 box filter, clamp-to-edge fold on odd source dimensions, integer
2414
+ // round-to-nearest \u2014 bit-exact with mipgen.ts downsample2x.
2415
+ @compute @workgroup_size(8, 8, 1)
2416
+ fn downsample(@builtin(global_invocation_id) gid : vec3<u32>) {
2417
+ let dstSize = textureDimensions(dst);
2418
+ if (gid.x >= dstSize.x || gid.y >= dstSize.y) { return; }
2419
+ let srcMax = textureDimensions(src) - vec2<u32>(1u, 1u);
2420
+ let x0 = min(gid.x * 2u, srcMax.x);
2421
+ let x1 = min(gid.x * 2u + 1u, srcMax.x);
2422
+ let y0 = min(gid.y * 2u, srcMax.y);
2423
+ let y1 = min(gid.y * 2u + 1u, srcMax.y);
2424
+ // round() recovers the exact byte values (unorm->f32 conversion is only
2425
+ // correctly-rounded, so a*255 is n +/- ~1e-5 \u2014 floor() would misround
2426
+ // sums divisible by 4 without it).
2427
+ let s = round(textureLoad(src, vec2<u32>(x0, y0), 0) * 255.0) +
2428
+ round(textureLoad(src, vec2<u32>(x1, y0), 0) * 255.0) +
2429
+ round(textureLoad(src, vec2<u32>(x0, y1), 0) * 255.0) +
2430
+ round(textureLoad(src, vec2<u32>(x1, y1), 0) * 255.0);
2431
+ textureStore(dst, gid.xy, floor((s + 2.0) * 0.25) / 255.0);
2432
+ }
2433
+ `
2434
+ );
2435
+ var pipelineCache = /* @__PURE__ */ new WeakMap();
2436
+ function getDownsamplePipeline(device) {
2437
+ let pipeline = pipelineCache.get(device);
2438
+ if (!pipeline) {
2439
+ pipeline = device.createComputePipelineAsync({
2440
+ label: "gputex-mipgen-pipeline",
2441
+ layout: "auto",
2442
+ compute: {
2443
+ module: device.createShaderModule({ label: "gputex-mipgen", code: DOWNSAMPLE_WGSL }),
2444
+ entryPoint: "downsample"
2445
+ }
2446
+ });
2447
+ pipelineCache.set(device, pipeline);
2448
+ pipeline.catch(() => pipelineCache.delete(device));
2449
+ }
2450
+ return pipeline;
2451
+ }
2452
+ async function generateGpuMipChain(device, source, { flipY = false } = {}) {
2453
+ const width = source.width;
2454
+ const height = source.height;
2455
+ if (!width || !height) {
2456
+ throw new Error("generateGpuMipChain: source has no dimensions");
2457
+ }
2458
+ const mipLevelCount = gpuMipLevelCount(width, height);
2459
+ const texture = device.createTexture({
2460
+ label: "gputex-mip-chain",
2461
+ size: [width, height, 1],
2462
+ format: "rgba8unorm",
2463
+ mipLevelCount,
2464
+ // COPY_DST + RENDER_ATTACHMENT for copyExternalImageToTexture (a blit
2465
+ // internally), TEXTURE_BINDING for downsample/encoder reads,
2466
+ // STORAGE_BINDING for downsample writes (rgba8unorm write-only storage
2467
+ // is core WebGPU).
2468
+ usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING
2469
+ });
2470
+ device.queue.copyExternalImageToTexture({ source, flipY }, { texture }, [width, height, 1]);
2471
+ const pipeline = await getDownsamplePipeline(device);
2472
+ const enc = device.createCommandEncoder({ label: "gputex-mipgen" });
2473
+ const pass = enc.beginComputePass();
2474
+ pass.setPipeline(pipeline);
2475
+ for (let level = 0; level + 1 < mipLevelCount; level++) {
2476
+ const bindGroup = device.createBindGroup({
2477
+ label: `gputex-mipgen-bg-${level}`,
2478
+ layout: pipeline.getBindGroupLayout(0),
2479
+ entries: [
2480
+ { binding: 0, resource: texture.createView({ baseMipLevel: level, mipLevelCount: 1 }) },
2481
+ { binding: 1, resource: texture.createView({ baseMipLevel: level + 1, mipLevelCount: 1 }) }
2482
+ ]
2483
+ });
2484
+ pass.setBindGroup(0, bindGroup);
2485
+ const dstW = Math.max(1, width >> level + 1);
2486
+ const dstH = Math.max(1, height >> level + 1);
2487
+ pass.dispatchWorkgroups(Math.ceil(dstW / 8), Math.ceil(dstH / 8), 1);
2488
+ }
2489
+ pass.end();
2490
+ device.queue.submit([enc.finish()]);
2491
+ return texture;
2492
+ }
2493
+
1763
2494
  // src/svg.ts
1764
2495
  var ROOT_TAG_RE = /<svg(?=[\s/>])[^>]*>/;
1765
2496
  function getAttr(tag, name) {
@@ -1890,6 +2621,7 @@ export {
1890
2621
  BC5WebGLEncoder,
1891
2622
  BC7Encoder,
1892
2623
  BC7WebGLEncoder,
2624
+ ETC2Encoder,
1893
2625
  Encoder,
1894
2626
  TextureFormat,
1895
2627
  WebGLBlockEncoder,
@@ -1897,8 +2629,10 @@ export {
1897
2629
  createWebGLContext,
1898
2630
  detectCapabilities,
1899
2631
  detectWebGLCapabilities,
2632
+ generateGpuMipChain,
1900
2633
  generateMipChain,
1901
2634
  getSharedWebGLContext,
2635
+ gpuMipLevelCount,
1902
2636
  isWebGLAvailable,
1903
2637
  padToBlockMultiple,
1904
2638
  rasterizeSvg,