ultra-text 1.0.1 → 1.0.3

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.
Files changed (237) hide show
  1. package/README.md +339 -1039
  2. package/dist/assets/cpuSdf.worker-Dy4P5_TI.js +466 -0
  3. package/dist/assets/cpuSdf.worker-Dy4P5_TI.js.map +1 -0
  4. package/dist/assets/traceOutline.worker-B5uVKmR4.js +1725 -0
  5. package/dist/assets/traceOutline.worker-B5uVKmR4.js.map +1 -0
  6. package/dist/atlas/appendAtlas.js +97 -31
  7. package/dist/atlas/appendAtlas.js.map +1 -1
  8. package/dist/atlas/buildAtlas.js +4 -3
  9. package/dist/atlas/buildAtlas.js.map +1 -1
  10. package/dist/atlas/colorGlyphs.d.ts +0 -10
  11. package/dist/atlas/colorGlyphs.js +97 -155
  12. package/dist/atlas/colorGlyphs.js.map +1 -1
  13. package/dist/atlas/colorTexture.d.ts +1 -0
  14. package/dist/atlas/colorTexture.js +64 -0
  15. package/dist/atlas/colorTexture.js.map +1 -0
  16. package/dist/atlas/cpuAtlas.d.ts +4 -1
  17. package/dist/atlas/cpuAtlas.js +14 -7
  18. package/dist/atlas/cpuAtlas.js.map +1 -1
  19. package/dist/atlas/cpuSdf.d.ts +1 -1
  20. package/dist/atlas/cpuSdf.js +235 -68
  21. package/dist/atlas/cpuSdf.js.map +1 -1
  22. package/dist/atlas/cpuWorker.d.ts +11 -3
  23. package/dist/atlas/cpuWorker.js +179 -22
  24. package/dist/atlas/cpuWorker.js.map +1 -1
  25. package/dist/atlas/gpuAtlas.js +188 -287
  26. package/dist/atlas/gpuAtlas.js.map +1 -1
  27. package/dist/atlas/gpuCommon.d.ts +257 -0
  28. package/dist/atlas/gpuCommon.js +845 -0
  29. package/dist/atlas/gpuCommon.js.map +1 -0
  30. package/dist/atlas/jfaWgsl.d.ts +1 -1
  31. package/dist/atlas/jfaWgsl.js +226 -249
  32. package/dist/atlas/jfaWgsl.js.map +1 -1
  33. package/dist/atlas/rasterize.d.ts +22 -0
  34. package/dist/atlas/rasterize.js +329 -158
  35. package/dist/atlas/rasterize.js.map +1 -1
  36. package/dist/atlas/shapeRaster.js +2 -1
  37. package/dist/atlas/shapeRaster.js.map +1 -1
  38. package/dist/atlas/vanillaBounds.d.ts +64 -0
  39. package/dist/atlas/vanillaBounds.js +42 -0
  40. package/dist/atlas/vanillaBounds.js.map +1 -0
  41. package/dist/atlas/vanillaCore.d.ts +100 -0
  42. package/dist/atlas/vanillaCore.js +1065 -0
  43. package/dist/atlas/vanillaCore.js.map +1 -0
  44. package/dist/atlas/vanillaCpuAtlas.d.ts +9 -0
  45. package/dist/atlas/vanillaCpuAtlas.js +72 -0
  46. package/dist/atlas/vanillaCpuAtlas.js.map +1 -0
  47. package/dist/atlas/vanillaExtras.d.ts +8 -0
  48. package/dist/atlas/vanillaExtras.js +167 -0
  49. package/dist/atlas/vanillaExtras.js.map +1 -0
  50. package/dist/atlas/vanillaGpuHelpers.d.ts +295 -0
  51. package/dist/atlas/vanillaGpuHelpers.js +354 -0
  52. package/dist/atlas/vanillaGpuHelpers.js.map +1 -0
  53. package/dist/dom/DomSdf.js +243 -297
  54. package/dist/dom/DomSdf.js.map +1 -1
  55. package/dist/dom/DomSlug.js +94 -165
  56. package/dist/dom/DomSlug.js.map +1 -1
  57. package/dist/dom/Text.d.ts +4 -1
  58. package/dist/dom/Text.js +5 -1
  59. package/dist/dom/Text.js.map +1 -1
  60. package/dist/dom/browserShaping.js +31 -14
  61. package/dist/dom/browserShaping.js.map +1 -1
  62. package/dist/dom/clusters.js +13 -2
  63. package/dist/dom/clusters.js.map +1 -1
  64. package/dist/dom/cssEffects.d.ts +4 -0
  65. package/dist/dom/cssEffects.js +72 -0
  66. package/dist/dom/cssEffects.js.map +1 -0
  67. package/dist/dom/domAnalytic.d.ts +53 -0
  68. package/dist/dom/domAnalytic.js +282 -0
  69. package/dist/dom/domAnalytic.js.map +1 -0
  70. package/dist/dom/domExtras.d.ts +27 -0
  71. package/dist/dom/domExtras.js +137 -0
  72. package/dist/dom/domExtras.js.map +1 -0
  73. package/dist/dom/fontCanvas.d.ts +16 -0
  74. package/dist/dom/fontCanvas.js +31 -13
  75. package/dist/dom/fontCanvas.js.map +1 -1
  76. package/dist/dom/layoutSignature.js +175 -57
  77. package/dist/dom/layoutSignature.js.map +1 -1
  78. package/dist/dom/markup.d.ts +10 -0
  79. package/dist/dom/markup.js +16 -1
  80. package/dist/dom/markup.js.map +1 -1
  81. package/dist/dom/pretextLayout.js +78 -28
  82. package/dist/dom/pretextLayout.js.map +1 -1
  83. package/dist/dom/snapshot.d.ts +4 -0
  84. package/dist/dom/snapshot.js +891 -484
  85. package/dist/dom/snapshot.js.map +1 -1
  86. package/dist/dom/snapshotsEqual.d.ts +7 -0
  87. package/dist/dom/snapshotsEqual.js +68 -0
  88. package/dist/dom/snapshotsEqual.js.map +1 -0
  89. package/dist/dom/svgCapture.d.ts +8 -1
  90. package/dist/dom/svgCapture.js +105 -28
  91. package/dist/dom/svgCapture.js.map +1 -1
  92. package/dist/dom/textTransform.d.ts +13 -1
  93. package/dist/dom/textTransform.js +31 -13
  94. package/dist/dom/textTransform.js.map +1 -1
  95. package/dist/dom/tone.js +91 -44
  96. package/dist/dom/tone.js.map +1 -1
  97. package/dist/index.js +4 -3
  98. package/dist/slug/SlugMaterial.d.ts +17 -3
  99. package/dist/slug/SlugMaterial.js +98 -26
  100. package/dist/slug/SlugMaterial.js.map +1 -1
  101. package/dist/slug/cff.d.ts +5 -0
  102. package/dist/slug/cff.js +780 -0
  103. package/dist/slug/cff.js.map +1 -0
  104. package/dist/slug/coverage.d.ts +19 -15
  105. package/dist/slug/coverage.js +380 -225
  106. package/dist/slug/coverage.js.map +1 -1
  107. package/dist/slug/preprocessCore.d.ts +6 -2
  108. package/dist/slug/preprocessCore.js +199 -148
  109. package/dist/slug/preprocessCore.js.map +1 -1
  110. package/dist/slug/runtime.d.ts +1 -0
  111. package/dist/slug/runtime.js +113 -74
  112. package/dist/slug/runtime.js.map +1 -1
  113. package/dist/slug/standaloneRuntime.d.ts +3 -2
  114. package/dist/slug/standaloneRuntime.js +112 -67
  115. package/dist/slug/standaloneRuntime.js.map +1 -1
  116. package/dist/slug/truetype.d.ts +14 -6
  117. package/dist/slug/truetype.js +356 -255
  118. package/dist/slug/truetype.js.map +1 -1
  119. package/dist/slug/vanillaCore.d.ts +410 -0
  120. package/dist/slug/vanillaCore.js +1630 -0
  121. package/dist/slug/vanillaCore.js.map +1 -0
  122. package/dist/slug-vanilla.d.ts +13 -364
  123. package/dist/slug-vanilla.js +7 -1454
  124. package/dist/slug.d.ts +2 -2
  125. package/dist/slug.js +7 -7
  126. package/dist/text/GlyphTextMaterial.d.ts +6 -1
  127. package/dist/text/GlyphTextMaterial.js +16 -12
  128. package/dist/text/GlyphTextMaterial.js.map +1 -1
  129. package/dist/text/RectMaterial.js +6 -10
  130. package/dist/text/RectMaterial.js.map +1 -1
  131. package/dist/text/SdfTextMaterial.d.ts +9 -3
  132. package/dist/text/SdfTextMaterial.js +125 -38
  133. package/dist/text/SdfTextMaterial.js.map +1 -1
  134. package/dist/text/decorations.d.ts +7 -2
  135. package/dist/text/decorations.js +187 -79
  136. package/dist/text/decorations.js.map +1 -1
  137. package/dist/text/geometry.d.ts +3 -22
  138. package/dist/text/geometryUtils.d.ts +16 -1
  139. package/dist/text/geometryUtils.js +83 -8
  140. package/dist/text/geometryUtils.js.map +1 -1
  141. package/dist/text/glyphGeometry.d.ts +9 -0
  142. package/dist/text/glyphGeometry.js +434 -0
  143. package/dist/text/glyphGeometry.js.map +1 -0
  144. package/dist/text/index.d.ts +2 -1
  145. package/dist/text/rectGeometry.d.ts +13 -0
  146. package/dist/text/rectGeometry.js +101 -0
  147. package/dist/text/rectGeometry.js.map +1 -0
  148. package/dist/text/reflow.d.ts +23 -1
  149. package/dist/text/reflow.js +223 -94
  150. package/dist/text/reflow.js.map +1 -1
  151. package/dist/text/snap.d.ts +15 -0
  152. package/dist/text/snap.js +26 -0
  153. package/dist/text/snap.js.map +1 -0
  154. package/dist/text/tone.js +2 -2
  155. package/dist/text/tone.js.map +1 -1
  156. package/dist/types.d.ts +23 -7
  157. package/dist/vanilla.d.ts +13 -363
  158. package/dist/vanilla.js +7 -1474
  159. package/dist/windfoil/WindfoilCompositeMaterial.d.ts +1 -1
  160. package/dist/windfoil/WindfoilCompositeMaterial.js +10 -3
  161. package/dist/windfoil/WindfoilCompositeMaterial.js.map +1 -1
  162. package/dist/windfoil/WindfoilMaterial.d.ts +12 -5
  163. package/dist/windfoil/WindfoilMaterial.js +71 -8
  164. package/dist/windfoil/WindfoilMaterial.js.map +1 -1
  165. package/dist/windfoil/compositeGeometry.js +12 -5
  166. package/dist/windfoil/compositeGeometry.js.map +1 -1
  167. package/dist/windfoil/coverage.d.ts +10 -2
  168. package/dist/windfoil/coverage.js +147 -59
  169. package/dist/windfoil/coverage.js.map +1 -1
  170. package/dist/windfoil/geometry.d.ts +17 -2
  171. package/dist/windfoil/geometry.js +220 -57
  172. package/dist/windfoil/geometry.js.map +1 -1
  173. package/dist/windfoil/layerGrid.js +8 -8
  174. package/dist/windfoil/layerGrid.js.map +1 -1
  175. package/dist/windfoil/mixed.d.ts +1 -0
  176. package/dist/windfoil/mixed.js +57 -29
  177. package/dist/windfoil/mixed.js.map +1 -1
  178. package/dist/windfoil/preprocess.js +2 -2
  179. package/dist/windfoil/preprocessCore.d.ts +2 -0
  180. package/dist/windfoil/preprocessCore.js +69 -37
  181. package/dist/windfoil/preprocessCore.js.map +1 -1
  182. package/dist/windfoil/requests.d.ts +1 -1
  183. package/dist/windfoil/requests.js +11 -4
  184. package/dist/windfoil/requests.js.map +1 -1
  185. package/dist/windfoil/resolveOutline.d.ts +9 -0
  186. package/dist/windfoil/resolveOutline.js +20 -0
  187. package/dist/windfoil/resolveOutline.js.map +1 -0
  188. package/dist/windfoil/runtime.d.ts +4 -1
  189. package/dist/windfoil/runtime.js +69 -63
  190. package/dist/windfoil/runtime.js.map +1 -1
  191. package/dist/windfoil/shapeGeometry.js +80 -34
  192. package/dist/windfoil/shapeGeometry.js.map +1 -1
  193. package/dist/windfoil/sizeSwitch.d.ts +1 -1
  194. package/dist/windfoil/sizeSwitch.js +13 -8
  195. package/dist/windfoil/sizeSwitch.js.map +1 -1
  196. package/dist/windfoil/storage.d.ts +78 -6
  197. package/dist/windfoil/storage.js +166 -11
  198. package/dist/windfoil/storage.js.map +1 -1
  199. package/dist/windfoil/svg/curves.d.ts +20 -0
  200. package/dist/windfoil/svg/curves.js +145 -42
  201. package/dist/windfoil/svg/curves.js.map +1 -1
  202. package/dist/windfoil/svg/document.js +10 -1
  203. package/dist/windfoil/svg/document.js.map +1 -1
  204. package/dist/windfoil/svg/pathConverter.js +66 -50
  205. package/dist/windfoil/svg/pathConverter.js.map +1 -1
  206. package/dist/windfoil/svg/pathData.d.ts +9 -0
  207. package/dist/windfoil/svg/pathData.js +142 -109
  208. package/dist/windfoil/svg/pathData.js.map +1 -1
  209. package/dist/windfoil/svg/winding.js +34 -10
  210. package/dist/windfoil/svg/winding.js.map +1 -1
  211. package/dist/windfoil/trace/contours.d.ts +5 -0
  212. package/dist/windfoil/trace/contours.js +256 -62
  213. package/dist/windfoil/trace/contours.js.map +1 -1
  214. package/dist/windfoil/trace/fit.d.ts +3 -1
  215. package/dist/windfoil/trace/fit.js +915 -265
  216. package/dist/windfoil/trace/fit.js.map +1 -1
  217. package/dist/windfoil/trace/outline.js +55 -30
  218. package/dist/windfoil/trace/outline.js.map +1 -1
  219. package/dist/windfoil/trace/traceOutline.worker.d.ts +24 -0
  220. package/dist/windfoil/trace/traceWorker.d.ts +13 -0
  221. package/dist/windfoil/trace/traceWorker.js +192 -0
  222. package/dist/windfoil/trace/traceWorker.js.map +1 -0
  223. package/dist/windfoil/trace/types.d.ts +10 -0
  224. package/dist/windfoil/tracedOutlines.d.ts +2 -0
  225. package/dist/windfoil/tracedOutlines.js +393 -103
  226. package/dist/windfoil/tracedOutlines.js.map +1 -1
  227. package/package.json +48 -29
  228. package/skills/ultra-text/SKILL.md +9 -10
  229. package/skills/ultra-text/references/frameworks.md +46 -2
  230. package/skills/ultra-text/references/three-tsl.md +27 -25
  231. package/skills/ultra-text/references/vanilla-webgpu.md +5 -2
  232. package/dist/assets/cpuSdf.worker-xS57yQbP.js +0 -322
  233. package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +0 -1
  234. package/dist/slug-vanilla.js.map +0 -1
  235. package/dist/text/geometry.js +0 -348
  236. package/dist/text/geometry.js.map +0 -1
  237. package/dist/vanilla.js.map +0 -1
@@ -1,184 +1,168 @@
1
1
  import { buildJfaWgsl } from "./jfaWgsl.js";
2
+ import { checkCommonAtlasLimits, edtRadius, encodeGpuColorBleed, encodeJfaWork, getDeviceGpuPool, getJfaLayouts, openErrorScopes, peekDeferredColorBleed, putReadyGpuColorTexture, selectAtlasMode, selectWorkgroupDimension, storageBufferFits, supportsDirectStore, takeDeferredColorBleed, takeReadyGpuColorTexture } from "./gpuCommon.js";
3
+ import { encodeMipmapChain, getDeviceMipPipeline, mipCount } from "./vanillaGpuHelpers.js";
2
4
  import { ExternalTexture, LinearFilter, LinearMipmapLinearFilter, NoColorSpace, RedFormat, WebGPUBackend } from "three/webgpu";
3
5
  // src/atlas/gpuAtlas.ts
4
- var MIN_WORKGROUP_DIMENSION = 8;
5
- var CORE_WORKGROUP_DIMENSION = 16;
6
- var SEED_BYTES_PER_TEXEL = 4;
7
- var COMPUTE_STORAGE_BUFFERS = 2;
8
- var FRAGMENT_STORAGE_BUFFERS = 1;
9
- var KERNEL_NEEDS = [
10
- ["maxComputeInvocationsPerWorkgroup", MIN_WORKGROUP_DIMENSION ** 2],
11
- ["maxComputeWorkgroupSizeX", MIN_WORKGROUP_DIMENSION],
12
- ["maxComputeWorkgroupSizeY", MIN_WORKGROUP_DIMENSION],
13
- ["maxStorageBuffersPerShaderStage", COMPUTE_STORAGE_BUFFERS]
14
- ];
15
- function selectWorkgroupDimension(limits) {
16
- const inv = limits?.maxComputeInvocationsPerWorkgroup ?? 256;
17
- const wx = limits?.maxComputeWorkgroupSizeX ?? 256;
18
- const wy = limits?.maxComputeWorkgroupSizeY ?? 256;
19
- return inv >= CORE_WORKGROUP_DIMENSION ** 2 && wx >= CORE_WORKGROUP_DIMENSION && wy >= CORE_WORKGROUP_DIMENSION ? CORE_WORKGROUP_DIMENSION : MIN_WORKGROUP_DIMENSION;
20
- }
21
- function supportsDirectStore(device) {
22
- const textures = globalThis.GPUTextureUsage;
23
- return typeof textures?.STORAGE_BINDING === "number" && textures.STORAGE_BINDING > 0 && Boolean(device.features?.has("texture-formats-tier1"));
6
+ var devicePipelines = /* @__PURE__ */ new WeakMap();
7
+ function asAtlasPipelines(workgroupDim, directStore, layouts, tiledStore, tiledStoreWide, multi, sep12) {
8
+ return {
9
+ workgroupDim,
10
+ directStore,
11
+ layouts,
12
+ sepHoriz: multi?.sepHoriz ?? null,
13
+ sepVertStore: multi?.sepVertStore ?? null,
14
+ sepHorizPx12: sep12?.sepHorizPx12 ?? null,
15
+ sepVertPx12: sep12?.sepVertPx12 ?? null,
16
+ tiledStore,
17
+ tiledStoreWide,
18
+ resolve: multi?.resolve ?? null
19
+ };
24
20
  }
25
- /** A texture-to-buffer copy aligns each row to 256 bytes; the raster keeps 4 bytes a texel. */
26
- function rasterRowBytes(width) {
27
- return Math.ceil(width / 64) * 256;
21
+ function atlasModeFor(directStore, pxRange, limits, width = 0, height = 0) {
22
+ return selectAtlasMode(directStore, pxRange, limits, width, height);
28
23
  }
29
- /**
30
- * Past a device limit the passes are invalid, which WebGPU reports only asynchronously:
31
- * checking first lets `auto` fall back without GPU work and `gpu` fail with a reason.
32
- */
33
- function checkAtlasLimits(limits, width, height, maxStep, directStore) {
34
- if (maxStep > 64) throw new RangeError("buildGpuSdfAtlas: pxRange exceeds fixed-point jump range; use atlasMode \"cpu\"");
35
- if (!limits) return;
36
- for (const [name, need] of KERNEL_NEEDS) {
37
- const value = limits[name];
38
- if (value !== void 0 && value < need) throw new RangeError(`buildGpuSdfAtlas: ${name}=${value}, needs ${need}; use atlasMode "cpu"`);
39
- }
40
- if (!directStore) {
41
- const fragBuffers = limits.maxStorageBuffersInFragmentStage;
42
- if (fragBuffers !== void 0 && fragBuffers < FRAGMENT_STORAGE_BUFFERS) throw new RangeError(`buildGpuSdfAtlas: maxStorageBuffersInFragmentStage=${fragBuffers}, needs ${FRAGMENT_STORAGE_BUFFERS}; use atlasMode "cpu"`);
43
- }
44
- const largest = limits.maxTextureDimension2D;
45
- if (largest !== void 0 && Math.max(width, height) > largest) throw new RangeError(`buildGpuSdfAtlas: ${width}×${height} exceeds maxTextureDimension2D=${largest}; lower maxAtlasSize`);
46
- const bytes = Math.max(width * height * SEED_BYTES_PER_TEXEL, rasterRowBytes(width) * height);
47
- if (bytes > limits.maxStorageBufferBindingSize || bytes > limits.maxBufferSize) throw new RangeError(`buildGpuSdfAtlas: ${width}×${height} exceeds storage-buffer limits; use atlasMode "cpu" or lower maxAtlasSize`);
48
- }
49
- var ERROR_FILTERS = [
50
- "validation",
51
- "out-of-memory",
52
- "internal"
53
- ];
54
- function openErrorScopes(device) {
55
- const push = device.pushErrorScope;
56
- const pop = device.popErrorScope;
57
- if (!push || !pop) return { close: () => Promise.resolve(null) };
58
- for (const filter of ERROR_FILTERS) push.call(device, filter);
59
- return { close() {
60
- return Promise.all(ERROR_FILTERS.map(() => pop.call(device))).then((errors) => {
61
- const error = errors.find(Boolean);
62
- return error ? error.message ?? "unknown GPU error" : null;
63
- }, (error) => String(error));
64
- } };
24
+ function getDeviceCache(device, workgroupDim, directStore, needWideTiled = false) {
25
+ const existing = devicePipelines.get(device);
26
+ if (existing?.workgroupDim === workgroupDim && existing.directStore === directStore && (!needWideTiled || existing.includeWideTiled)) return existing;
27
+ const includeWideTiled = needWideTiled || !storageBufferFits(device.limits, 4096, 4096);
28
+ const cache = {
29
+ workgroupDim,
30
+ directStore,
31
+ includeWideTiled,
32
+ layouts: getJfaLayouts(device),
33
+ module: device.createShaderModule({
34
+ label: "ultra-text atlas",
35
+ code: buildJfaWgsl(workgroupDim, directStore, true, includeWideTiled)
36
+ })
37
+ };
38
+ devicePipelines.set(device, cache);
39
+ return cache;
65
40
  }
66
- function jfaSteps(pxRange) {
67
- const radius = Math.ceil(pxRange * .5) + 1;
68
- let step = 1;
69
- while (step * 2 - 1 < radius) step <<= 1;
70
- const steps = [];
71
- for (; step >= 1; step >>= 1) steps.push(step);
72
- return steps;
41
+ function cachedAtlasPipelines(device, workgroupDim, directStore, mode) {
42
+ const cache = devicePipelines.get(device);
43
+ if (cache?.workgroupDim !== workgroupDim || cache.directStore !== directStore) return void 0;
44
+ if (mode === "sep12") return cache.sep12 ? asAtlasPipelines(workgroupDim, directStore, cache.layouts, null, null, void 0, cache.sep12) : void 0;
45
+ if (mode === "tiled12") return cache.tiledStore ? asAtlasPipelines(workgroupDim, directStore, cache.layouts, cache.tiledStore, null) : void 0;
46
+ if (mode === "tiled32") return cache.tiledWide ? asAtlasPipelines(workgroupDim, directStore, cache.layouts, null, cache.tiledWide) : void 0;
47
+ return cache.multi ? asAtlasPipelines(workgroupDim, directStore, cache.layouts, null, null, cache.multi) : void 0;
73
48
  }
74
- var devicePipelines = /* @__PURE__ */ new WeakMap();
75
- var devicePipelinePromises = /* @__PURE__ */ new WeakMap();
76
49
  function supportsAsyncAtlasPipelines(device, directStore) {
77
50
  return typeof device.createComputePipelineAsync === "function" && (directStore || typeof device.createRenderPipelineAsync === "function");
78
51
  }
79
- function atlasPipelinesSync(device, workgroupDim, directStore) {
80
- const cached = devicePipelines.get(device);
81
- if (cached?.directStore === directStore && cached.workgroupDim === workgroupDim) return cached;
82
- const module = device.createShaderModule({
83
- label: "ultra-text atlas",
84
- code: buildJfaWgsl(workgroupDim, directStore)
85
- });
86
- const compute = (entryPoint) => device.createComputePipeline({
87
- layout: "auto",
52
+ function buildAtlasPipelines(device, workgroupDim, directStore, mode, async) {
53
+ const cached = cachedAtlasPipelines(device, workgroupDim, directStore, mode);
54
+ if (cached) return async ? Promise.resolve(cached) : cached;
55
+ const existing = devicePipelines.get(device);
56
+ const sameConfig = existing?.workgroupDim === workgroupDim && existing.directStore === directStore;
57
+ if (async && sameConfig) {
58
+ const inFlight = mode === "sep12" ? existing.sep12Promise : mode === "tiled12" ? existing.tiledPromise : mode === "tiled32" ? existing.tiledWidePromise : existing.multiPromise;
59
+ if (inFlight) return inFlight;
60
+ }
61
+ const cache = getDeviceCache(device, workgroupDim, directStore, mode === "tiled32");
62
+ const { module, layouts } = cache;
63
+ const computeSync = (entryPoint, layout) => device.createComputePipeline({
64
+ layout,
88
65
  compute: {
89
66
  module,
90
67
  entryPoint
91
68
  }
92
69
  });
93
- const pipelines = {
94
- workgroupDim,
95
- directStore,
96
- seed: compute("seedMain"),
97
- jump: compute("jumpMain"),
98
- jumpStore: directStore ? compute("jumpStoreMain") : null,
99
- resolve: directStore ? null : device.createRenderPipeline({
100
- layout: "auto",
101
- vertex: {
102
- module,
103
- entryPoint: "fullscreenVert"
104
- },
105
- fragment: {
106
- module,
107
- entryPoint: "resolveFrag",
108
- targets: [{ format: "r8unorm" }]
109
- }
110
- })
111
- };
112
- devicePipelines.set(device, pipelines);
113
- return pipelines;
114
- }
115
- function atlasPipelinesAsync(device, workgroupDim, directStore) {
116
- const cached = devicePipelines.get(device);
117
- if (cached?.directStore === directStore && cached.workgroupDim === workgroupDim) return Promise.resolve(cached);
118
- const inFlight = devicePipelinePromises.get(device);
119
- if (inFlight?.directStore === directStore && inFlight.workgroupDim === workgroupDim) return inFlight.promise;
120
- const module = device.createShaderModule({
121
- label: "ultra-text atlas",
122
- code: buildJfaWgsl(workgroupDim, directStore)
123
- });
124
- const computeAsync = (entryPoint) => device.createComputePipelineAsync({
125
- layout: "auto",
70
+ const computeAsync = (entryPoint, layout) => device.createComputePipelineAsync({
71
+ layout,
126
72
  compute: {
127
73
  module,
128
74
  entryPoint
129
75
  }
130
76
  });
131
- const promise = Promise.all([
132
- computeAsync("seedMain"),
133
- computeAsync("jumpMain"),
134
- directStore ? computeAsync("jumpStoreMain") : Promise.resolve(null),
135
- directStore ? Promise.resolve(null) : device.createRenderPipelineAsync({
136
- layout: "auto",
137
- vertex: {
138
- module,
139
- entryPoint: "fullscreenVert"
140
- },
141
- fragment: {
142
- module,
143
- entryPoint: "resolveFrag",
144
- targets: [{ format: "r8unorm" }]
145
- }
146
- })
147
- ]).then(([seed, jump, jumpStore, resolve]) => {
148
- const pipelines = {
149
- workgroupDim,
150
- directStore,
151
- seed,
152
- jump,
153
- jumpStore,
77
+ if (mode === "sep12") {
78
+ const hLayout = layouts ? layouts.sepHorizPipeline : "auto";
79
+ const vLayout = layouts ? layouts.sepVertStorePipeline : "auto";
80
+ const storeSep12 = (sepHorizPx12, sepVertPx12) => {
81
+ const sep12 = {
82
+ sepHorizPx12,
83
+ sepVertPx12
84
+ };
85
+ cache.sep12 = sep12;
86
+ return asAtlasPipelines(workgroupDim, directStore, layouts, null, null, void 0, sep12);
87
+ };
88
+ if (!async) return storeSep12(computeSync("sepHorizPx12", hLayout), computeSync("sepVertPx12", vLayout));
89
+ const promise = Promise.all([computeAsync("sepHorizPx12", hLayout), computeAsync("sepVertPx12", vLayout)]).then(([h, v]) => storeSep12(h, v), (error) => {
90
+ if (cache.sep12Promise === promise) cache.sep12Promise = void 0;
91
+ throw error;
92
+ });
93
+ cache.sep12Promise = promise;
94
+ return promise;
95
+ }
96
+ if (mode === "tiled12" || mode === "tiled32") {
97
+ const isWide = mode === "tiled32";
98
+ const entryPoint = isWide ? "tiledStoreWideMain" : "tiledStoreMain";
99
+ const layout = layouts ? layouts.tiledPipeline : "auto";
100
+ const store = (pipeline) => {
101
+ if (isWide) cache.tiledWide = pipeline;
102
+ else cache.tiledStore = pipeline;
103
+ return asAtlasPipelines(workgroupDim, directStore, layouts, isWide ? null : pipeline, isWide ? pipeline : null);
104
+ };
105
+ if (!async) return store(computeSync(entryPoint, layout));
106
+ const promise = computeAsync(entryPoint, layout).then((pipeline) => store(pipeline), (error) => {
107
+ if (isWide) {
108
+ if (cache.tiledWidePromise === promise) cache.tiledWidePromise = void 0;
109
+ } else if (cache.tiledPromise === promise) cache.tiledPromise = void 0;
110
+ throw error;
111
+ });
112
+ if (isWide) cache.tiledWidePromise = promise;
113
+ else cache.tiledPromise = promise;
114
+ return promise;
115
+ }
116
+ const resolveDesc = {
117
+ layout: layouts ? layouts.resolvePipeline : "auto",
118
+ vertex: {
119
+ module,
120
+ entryPoint: "fullscreenVert"
121
+ },
122
+ fragment: {
123
+ module,
124
+ entryPoint: "resolveFrag",
125
+ targets: [{ format: "r8unorm" }]
126
+ }
127
+ };
128
+ const store = (sepHoriz, sepVertStore, resolve) => {
129
+ const multi = {
130
+ sepHoriz,
131
+ sepVertStore,
154
132
  resolve
155
133
  };
156
- devicePipelines.set(device, pipelines);
157
- return pipelines;
158
- }, (error) => {
159
- devicePipelinePromises.delete(device);
134
+ cache.multi = multi;
135
+ return asAtlasPipelines(workgroupDim, directStore, layouts, null, null, multi);
136
+ };
137
+ const horizLayout = layouts ? layouts.sepHorizPipeline : "auto";
138
+ const vertLayout = directStore && layouts ? layouts.sepVertStorePipeline : "auto";
139
+ if (!async) return store(computeSync("sepHorizMain", horizLayout), directStore ? computeSync("sepVertStoreMain", vertLayout) : null, directStore ? null : device.createRenderPipeline(resolveDesc));
140
+ const promise = Promise.all([
141
+ computeAsync("sepHorizMain", horizLayout),
142
+ directStore ? computeAsync("sepVertStoreMain", vertLayout) : Promise.resolve(null),
143
+ directStore ? Promise.resolve(null) : device.createRenderPipelineAsync(resolveDesc)
144
+ ]).then(([sepHoriz, sepVertStore, resolve]) => store(sepHoriz, sepVertStore, resolve), (error) => {
145
+ if (cache.multiPromise === promise) cache.multiPromise = void 0;
160
146
  throw error;
161
147
  });
162
- devicePipelinePromises.set(device, {
163
- workgroupDim,
164
- directStore,
165
- promise
166
- });
148
+ cache.multiPromise = promise;
167
149
  return promise;
168
150
  }
169
151
  /**
170
152
  * @internal Starts the native compile of the device's atlas passes ahead of its first atlas,
171
153
  * while the caller reads the DOM.
172
154
  */
173
- function prepareAtlasPasses(renderer) {
155
+ function prepareAtlasPasses(renderer, pxRange) {
174
156
  const device = atlasDevice(renderer);
175
- if (device?.createShaderModule && !devicePipelines.has(device) && !devicePipelinePromises.has(device)) {
176
- const directStore = supportsDirectStore(device);
177
- const workgroupDim = selectWorkgroupDimension(device.limits);
178
- if (supportsAsyncAtlasPipelines(device, directStore)) atlasPipelinesAsync(device, workgroupDim, directStore).catch(() => void 0);
179
- else atlasPipelinesSync(device, workgroupDim, directStore);
180
- device.queue.submit([]);
181
- }
157
+ if (!device?.createShaderModule) return;
158
+ const directStore = supportsDirectStore(device.features);
159
+ const workgroupDim = selectWorkgroupDimension(device.limits);
160
+ const mode = atlasModeFor(directStore, pxRange);
161
+ const cached = devicePipelines.get(device);
162
+ if (cached?.workgroupDim === workgroupDim && cached.directStore === directStore && (mode === "sep12" ? cached.sep12 || cached.sep12Promise : mode === "tiled12" ? cached.tiledStore || cached.tiledPromise : mode === "tiled32" ? cached.tiledWide || cached.tiledWidePromise : cached.multi || cached.multiPromise)) return;
163
+ if (supportsAsyncAtlasPipelines(device, directStore)) buildAtlasPipelines(device, workgroupDim, directStore, mode, true).catch(() => void 0);
164
+ else buildAtlasPipelines(device, workgroupDim, directStore, mode, false);
165
+ device.queue.submit([]);
182
166
  }
183
167
  /**
184
168
  * @internal Regenerates the mips of an atlas texture from its level 0 with three's own mip
@@ -204,159 +188,46 @@ function atlasDevice(renderer) {
204
188
  * @internal Runs the atlas passes over `raster` into level 0 of a new r8unorm texture of
205
189
  * `mipLevelCount` levels, all in one command buffer.
206
190
  */
207
- async function encodeGpuSdf(device, raster, mipLevelCount, finish, onSubmitted) {
208
- const { width, height, pxRange } = raster;
191
+ async function encodeGpuSdf(device, raster, mipLevelCount, finish, onSubmitted, extraTransient) {
192
+ const { width, height, pxRange, canvas } = raster;
209
193
  const { GPUBufferUsage: buffers, GPUTextureUsage: textures } = globalThis;
210
194
  if (!device?.createCommandEncoder || !buffers || !textures) throw new Error("buildGpuSdfAtlas: device cannot run the atlas passes");
211
- const steps = jfaSteps(pxRange);
212
- const directStore = supportsDirectStore(device);
213
- checkAtlasLimits(device.limits, width, height, steps[0], directStore);
195
+ const radius = edtRadius(pxRange);
196
+ const directStore = supportsDirectStore(device.features);
197
+ const mode = atlasModeFor(directStore, pxRange, device.limits, width, height);
198
+ checkCommonAtlasLimits("buildGpuSdfAtlas", device.limits, width, height, radius, directStore, "; use atlasMode \"cpu\"", mode);
214
199
  const workgroupDim = selectWorkgroupDimension(device.limits);
215
- let prebuilt = devicePipelines.get(device);
216
- if (!(prebuilt?.directStore === directStore && prebuilt.workgroupDim === workgroupDim) && supportsAsyncAtlasPipelines(device, directStore)) try {
217
- prebuilt = await atlasPipelinesAsync(device, workgroupDim, directStore);
200
+ let prebuilt = cachedAtlasPipelines(device, workgroupDim, directStore, mode);
201
+ const cachedEntry = devicePipelines.get(device);
202
+ const inFlight = !prebuilt && cachedEntry?.workgroupDim === workgroupDim && cachedEntry.directStore === directStore ? mode === "sep12" ? cachedEntry.sep12Promise : mode === "tiled12" ? cachedEntry.tiledPromise : mode === "tiled32" ? cachedEntry.tiledWidePromise : cachedEntry.multiPromise : void 0;
203
+ if (inFlight) try {
204
+ prebuilt = await inFlight;
218
205
  } catch (error) {
219
206
  throw new Error(`buildGpuSdfAtlas: the device rejected the atlas passes: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
220
207
  }
221
- else if (!(prebuilt?.directStore === directStore && prebuilt.workgroupDim === workgroupDim)) prebuilt = void 0;
208
+ const pool = getDeviceGpuPool(device);
209
+ const retainPool = pool.beginBuild();
222
210
  const transient = [];
223
211
  let output;
212
+ let resolvedOk = false;
213
+ let buildEnded = false;
224
214
  try {
225
215
  const scopes = openErrorScopes(device);
226
216
  let reported;
227
217
  try {
228
- const pipelines = prebuilt ?? atlasPipelinesSync(device, workgroupDim, directStore);
229
- const rowBytes = rasterRowBytes(width);
230
- const buffer = (size, usage) => {
231
- const created = device.createBuffer({
232
- size,
233
- usage
234
- });
235
- transient.push(created);
236
- return created;
237
- };
238
- const upload = device.createTexture({
239
- size: [width, height],
240
- format: "rgba8unorm",
241
- usage: textures.COPY_SRC | textures.COPY_DST | textures.RENDER_ATTACHMENT
242
- });
243
- transient.push(upload);
244
- const words = buffer(rowBytes * height, buffers.STORAGE | buffers.COPY_DST);
245
- const seedBytes = width * height * SEED_BYTES_PER_TEXEL;
246
- const seeds = [buffer(seedBytes, buffers.STORAGE), buffer(seedBytes, buffers.STORAGE)];
247
- const params = buffer(steps.length * 256 + 16, buffers.UNIFORM | buffers.COPY_DST);
248
- const values = new Int32Array(steps.length * 64 + 4);
249
- const invPxRange = 1 / pxRange;
250
- values.set([
251
- width,
252
- height,
253
- rowBytes / 4
254
- ]);
255
- new Float32Array(values.buffer, 12, 1)[0] = invPxRange;
256
- steps.forEach((step, index) => {
257
- const base = (index + 1) * 64;
258
- values.set([
259
- width,
260
- height,
261
- step
262
- ], base);
263
- new Float32Array(values.buffer, base * 4 + 12, 1)[0] = invPxRange;
264
- });
265
- device.queue.writeBuffer(params, 0, values);
266
- device.queue.copyExternalImageToTexture({
267
- source: raster.canvas,
268
- flipY: false
269
- }, {
270
- texture: upload,
271
- premultipliedAlpha: false
272
- }, [width, height]);
218
+ const pipelines = prebuilt ?? buildAtlasPipelines(device, workgroupDim, directStore, mode, false);
273
219
  output = device.createTexture({
274
220
  size: [width, height],
275
221
  mipLevelCount,
276
222
  format: "r8unorm",
277
223
  usage: textures.RENDER_ATTACHMENT | textures.TEXTURE_BINDING | textures.COPY_SRC | textures.COPY_DST | (pipelines.directStore ? textures.STORAGE_BINDING ?? 0 : 0)
278
224
  });
279
- const bind = (pipeline, offset, ...storageBuffers) => device.createBindGroup({
280
- layout: pipeline.getBindGroupLayout(0),
281
- entries: [{
282
- binding: 0,
283
- resource: {
284
- buffer: params,
285
- offset,
286
- size: 16
287
- }
288
- }, ...storageBuffers.map((resource, index) => ({
289
- binding: index + 1,
290
- resource: { buffer: resource }
291
- }))]
292
- });
293
225
  const encoder = device.createCommandEncoder();
294
- encoder.copyTextureToBuffer({ texture: upload }, {
295
- buffer: words,
296
- bytesPerRow: rowBytes,
297
- rowsPerImage: height
298
- }, [width, height]);
299
- const groupsX = Math.ceil(width / pipelines.workgroupDim);
300
- const groupsY = Math.ceil(height / pipelines.workgroupDim);
301
- const flood = encoder.beginComputePass();
302
- flood.setPipeline(pipelines.seed);
303
- flood.setBindGroup(0, bind(pipelines.seed, 0, words, seeds[0]));
304
- flood.dispatchWorkgroups(groupsX, groupsY);
305
- if (pipelines.directStore && pipelines.jumpStore) {
306
- const midSteps = steps.length - 1;
307
- if (midSteps > 0) {
308
- flood.setPipeline(pipelines.jump);
309
- for (let index = 0; index < midSteps; index++) {
310
- const into = seeds[(index + 1) % 2];
311
- flood.setBindGroup(0, bind(pipelines.jump, (index + 1) * 256, seeds[index % 2], into));
312
- flood.dispatchWorkgroups(groupsX, groupsY);
313
- }
314
- }
315
- const finalView = output.createView({ mipLevelCount: 1 });
316
- flood.setPipeline(pipelines.jumpStore);
317
- flood.setBindGroup(0, device.createBindGroup({
318
- layout: pipelines.jumpStore.getBindGroupLayout(0),
319
- entries: [
320
- {
321
- binding: 0,
322
- resource: {
323
- buffer: params,
324
- offset: steps.length * 256,
325
- size: 16
326
- }
327
- },
328
- {
329
- binding: 1,
330
- resource: { buffer: seeds[midSteps % 2] }
331
- },
332
- {
333
- binding: 2,
334
- resource: finalView
335
- }
336
- ]
337
- }));
338
- flood.dispatchWorkgroups(groupsX, groupsY);
339
- flood.end();
340
- } else {
341
- flood.setPipeline(pipelines.jump);
342
- steps.forEach((_, index) => {
343
- const into = seeds[(index + 1) % 2];
344
- flood.setBindGroup(0, bind(pipelines.jump, (index + 1) * 256, seeds[index % 2], into));
345
- flood.dispatchWorkgroups(groupsX, groupsY);
346
- });
347
- flood.end();
348
- const resolve = encoder.beginRenderPass({ colorAttachments: [{
349
- view: output.createView({ mipLevelCount: 1 }),
350
- loadOp: "clear",
351
- storeOp: "store"
352
- }] });
353
- resolve.setPipeline(pipelines.resolve);
354
- resolve.setBindGroup(0, bind(pipelines.resolve, 0, seeds[steps.length % 2]));
355
- resolve.draw(3);
356
- resolve.end();
357
- }
226
+ encodeJfaWork(device, encoder, pipelines, canvas, width, height, pxRange, radius, output, transient, buffers, textures, pool);
358
227
  finish?.(encoder, output);
228
+ if (extraTransient?.length) transient.push(...extraTransient);
359
229
  device.queue.submit([encoder.finish()]);
230
+ pool.resetRing();
360
231
  } finally {
361
232
  reported = scopes.close();
362
233
  }
@@ -364,14 +235,17 @@ async function encodeGpuSdf(device, raster, mipLevelCount, finish, onSubmitted)
364
235
  const rejection = await reported;
365
236
  if (rejection !== null) {
366
237
  devicePipelines.delete(device);
367
- devicePipelinePromises.delete(device);
368
238
  throw new Error(`buildGpuSdfAtlas: the device rejected the atlas passes: ${rejection}`);
369
239
  }
240
+ resolvedOk = true;
241
+ buildEnded = true;
242
+ pool.endBuild(transient, retainPool, false);
243
+ transient.length = 0;
370
244
  const retained = output;
371
245
  output = void 0;
372
246
  return retained;
373
247
  } finally {
374
- for (const resource of transient) resource.destroy();
248
+ if (!resolvedOk && !buildEnded) pool.endBuild(transient, false, true);
375
249
  output?.destroy();
376
250
  }
377
251
  }
@@ -439,10 +313,12 @@ async function buildGpuSdfAtlas(renderer, raster, options = {}, onProvisional) {
439
313
  }
440
314
  };
441
315
  };
442
- if (onProvisional && !mipmaps) {
316
+ const device = atlasDevice(renderer);
317
+ const bleed = raster.colorBand && device ? peekDeferredColorBleed(raster.colorBand.data) : void 0;
318
+ if (onProvisional && !mipmaps && !bleed) {
443
319
  let wrapped;
444
320
  try {
445
- const gpuTexture = await encodeGpuSdf(atlasDevice(renderer), raster, levels, void 0, (candidate) => {
321
+ const gpuTexture = await encodeGpuSdf(device, raster, levels, void 0, (candidate) => {
446
322
  wrapped = wrapAtlas(candidate);
447
323
  onProvisional(wrapped.atlas);
448
324
  });
@@ -455,10 +331,35 @@ async function buildGpuSdfAtlas(renderer, raster, options = {}, onProvisional) {
455
331
  throw error;
456
332
  }
457
333
  }
458
- const { atlas } = wrapAtlas(await encodeGpuSdf(atlasDevice(renderer), raster, levels));
334
+ const extraTransient = [];
335
+ let colorGpuTex;
336
+ const encodeBleed = (encoder) => {
337
+ if (!bleed || !device) return;
338
+ const colorMips = mipCount(width, bleed.height, mipmaps);
339
+ const res = encodeGpuColorBleed(device, encoder, bleed, colorMips, getDeviceGpuPool(device));
340
+ colorGpuTex = res.texture;
341
+ extraTransient.push(...res.transient);
342
+ if (colorMips > 1) {
343
+ const mip = getDeviceMipPipeline(device, "rgba8unorm-srgb");
344
+ encodeMipmapChain(device, encoder, colorGpuTex, colorMips, mip.pipeline, mip.sampler, mip.layout);
345
+ }
346
+ };
347
+ let gpuTexture;
348
+ try {
349
+ gpuTexture = await encodeGpuSdf(device, raster, levels, bleed ? encodeBleed : void 0, void 0, extraTransient);
350
+ } catch (error) {
351
+ colorGpuTex?.destroy();
352
+ throw error;
353
+ }
354
+ if (colorGpuTex && raster.colorBand) {
355
+ takeDeferredColorBleed(raster.colorBand.data);
356
+ putReadyGpuColorTexture(raster.colorBand.data, colorGpuTex);
357
+ }
358
+ const { atlas } = wrapAtlas(gpuTexture);
459
359
  try {
460
360
  if (mipmaps) await generateAtlasMips(renderer, atlas.texture);
461
361
  } catch (error) {
362
+ if (raster.colorBand) takeReadyGpuColorTexture(raster.colorBand.data)?.destroy();
462
363
  atlas.dispose();
463
364
  throw error;
464
365
  }