ultra-text 1.0.0 → 1.0.1
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/README.md +168 -85
- package/THIRD_PARTY_NOTICES.md +3 -0
- package/dist/assets/{cpuSdf.worker-Du9o8q4p.js → cpuSdf.worker-xS57yQbP.js} +13 -11
- package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +1 -0
- package/dist/atlas/appendAtlas.js +2 -1
- package/dist/atlas/appendAtlas.js.map +1 -1
- package/dist/atlas/buildAtlas.js +3 -14
- package/dist/atlas/buildAtlas.js.map +1 -1
- package/dist/atlas/cpuSdf.js +12 -10
- package/dist/atlas/cpuSdf.js.map +1 -1
- package/dist/atlas/cpuWorker.js +1 -1
- package/dist/atlas/cpuWorker.js.map +1 -1
- package/dist/atlas/gpuAtlas.d.ts +7 -6
- package/dist/atlas/gpuAtlas.js +280 -145
- package/dist/atlas/gpuAtlas.js.map +1 -1
- package/dist/atlas/jfaWgsl.js +201 -73
- package/dist/atlas/jfaWgsl.js.map +1 -1
- package/dist/atlas/rasterize.d.ts +2 -0
- package/dist/atlas/rasterize.js +2 -87
- package/dist/atlas/rasterize.js.map +1 -1
- package/dist/dom/DomSdf.js +66 -15
- package/dist/dom/DomSdf.js.map +1 -1
- package/dist/dom/DomSlug.d.ts +26 -0
- package/dist/dom/DomSlug.js +640 -0
- package/dist/dom/DomSlug.js.map +1 -0
- package/dist/dom/SlugText.d.ts +18 -0
- package/dist/dom/SlugText.js +94 -0
- package/dist/dom/SlugText.js.map +1 -0
- package/dist/dom/Text.js +4 -67
- package/dist/dom/Text.js.map +1 -1
- package/dist/dom/fontCanvas.d.ts +2 -0
- package/dist/dom/fontCanvas.js +101 -0
- package/dist/dom/fontCanvas.js.map +1 -0
- package/dist/dom/layoutSignature.js +29 -2
- package/dist/dom/layoutSignature.js.map +1 -1
- package/dist/dom/snapshot.js +251 -96
- package/dist/dom/snapshot.js.map +1 -1
- package/dist/dom/textHost.d.ts +11 -0
- package/dist/dom/textHost.js +68 -0
- package/dist/dom/textHost.js.map +1 -0
- package/dist/slug/SlugMaterial.d.ts +3 -2
- package/dist/slug/SlugMaterial.js +28 -2
- package/dist/slug/SlugMaterial.js.map +1 -1
- package/dist/slug/coverage.d.ts +1 -1
- package/dist/slug/coverage.js +1 -1
- package/dist/slug/coverage.js.map +1 -1
- package/dist/slug/preprocessCore.d.ts +1 -2
- package/dist/slug/preprocessCore.js +39 -16
- package/dist/slug/preprocessCore.js.map +1 -1
- package/dist/slug/runtime.js +6 -4
- package/dist/slug/runtime.js.map +1 -1
- package/dist/slug/standaloneRuntime.d.ts +39 -0
- package/dist/slug/standaloneRuntime.js +140 -0
- package/dist/slug/standaloneRuntime.js.map +1 -0
- package/dist/slug/truetype.d.ts +22 -0
- package/dist/slug/truetype.js +494 -0
- package/dist/slug/truetype.js.map +1 -0
- package/dist/slug-vanilla.d.ts +364 -0
- package/dist/slug-vanilla.js +1461 -0
- package/dist/slug-vanilla.js.map +1 -0
- package/dist/slug.d.ts +21 -0
- package/dist/slug.js +19 -0
- package/dist/text/GlyphTextMaterial.d.ts +11 -11
- package/dist/text/GlyphTextMaterial.js +98 -29
- package/dist/text/GlyphTextMaterial.js.map +1 -1
- package/dist/text/SdfTextMaterial.d.ts +5 -4
- package/dist/text/SdfTextMaterial.js +22 -4
- package/dist/text/SdfTextMaterial.js.map +1 -1
- package/dist/types.d.ts +63 -14
- package/dist/vanilla.d.ts +3 -0
- package/dist/vanilla.js +132 -59
- package/dist/vanilla.js.map +1 -1
- package/dist/windfoil/WindfoilMaterial.d.ts +2 -1
- package/dist/windfoil/WindfoilMaterial.js +8 -1
- package/dist/windfoil/WindfoilMaterial.js.map +1 -1
- package/dist/windfoil/abortable.d.ts +2 -0
- package/dist/windfoil/abortable.js +26 -0
- package/dist/windfoil/abortable.js.map +1 -0
- package/dist/windfoil/color.js +2 -1
- package/dist/windfoil/color.js.map +1 -1
- package/dist/windfoil/coverage.d.ts +3 -20
- package/dist/windfoil/coverage.js +7 -20
- package/dist/windfoil/coverage.js.map +1 -1
- package/dist/windfoil/emoji.js +1 -1
- package/dist/windfoil/emoji.js.map +1 -1
- package/dist/windfoil/emojiMetrics.js +1 -1
- package/dist/windfoil/emojiMetrics.js.map +1 -1
- package/dist/windfoil/geometry.js +100 -33
- package/dist/windfoil/geometry.js.map +1 -1
- package/dist/windfoil/layerGrid.js +14 -10
- package/dist/windfoil/layerGrid.js.map +1 -1
- package/dist/windfoil/layerNormalization.d.ts +1 -1
- package/dist/windfoil/layerNormalization.js +1 -1
- package/dist/windfoil/layerNormalization.js.map +1 -1
- package/dist/windfoil/outlineValidation.d.ts +86 -0
- package/dist/windfoil/outlineValidation.js +65 -0
- package/dist/windfoil/outlineValidation.js.map +1 -0
- package/dist/windfoil/preprocess.js +3 -2
- package/dist/windfoil/preprocess.js.map +1 -1
- package/dist/windfoil/preprocessCore.d.ts +4 -85
- package/dist/windfoil/preprocessCore.js +210 -206
- package/dist/windfoil/preprocessCore.js.map +1 -1
- package/dist/windfoil/requests.js +1 -1
- package/dist/windfoil/requests.js.map +1 -1
- package/dist/windfoil/runtime.d.ts +2 -1
- package/dist/windfoil/runtime.js +3 -2
- package/dist/windfoil/runtime.js.map +1 -1
- package/dist/windfoil/shapeResources.js +2 -1
- package/dist/windfoil/shapeResources.js.map +1 -1
- package/dist/windfoil/shapes.js +1 -1
- package/dist/windfoil/skirt.d.ts +22 -0
- package/dist/windfoil/skirt.js +20 -0
- package/dist/windfoil/skirt.js.map +1 -0
- package/dist/windfoil/storage.d.ts +2 -2
- package/dist/windfoil/storage.js +1 -22
- package/dist/windfoil/storage.js.map +1 -1
- package/dist/windfoil/trace/contours.d.ts +0 -5
- package/dist/windfoil/trace/contours.js +81 -114
- package/dist/windfoil/trace/contours.js.map +1 -1
- package/dist/windfoil/trace/fit.d.ts +0 -6
- package/dist/windfoil/trace/fit.js +304 -308
- package/dist/windfoil/trace/fit.js.map +1 -1
- package/dist/windfoil/trace/outline.js +10 -10
- package/dist/windfoil/trace/outline.js.map +1 -1
- package/dist/windfoil/trace/types.d.ts +5 -3
- package/dist/windfoil/tracedOutlines.d.ts +37 -1
- package/dist/windfoil/tracedOutlines.js +172 -33
- package/dist/windfoil/tracedOutlines.js.map +1 -1
- package/package.json +57 -8
- package/skills/ultra-text/SKILL.md +114 -0
- package/skills/ultra-text/references/frameworks.md +43 -0
- package/skills/ultra-text/references/three-tsl.md +78 -0
- package/skills/ultra-text/references/vanilla-webgpu.md +95 -0
- package/dist/assets/cpuSdf.worker-Du9o8q4p.js.map +0 -1
package/dist/atlas/gpuAtlas.js
CHANGED
|
@@ -1,14 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { buildJfaWgsl } from "./jfaWgsl.js";
|
|
2
2
|
import { ExternalTexture, LinearFilter, LinearMipmapLinearFilter, NoColorSpace, RedFormat, WebGPUBackend } from "three/webgpu";
|
|
3
3
|
// src/atlas/gpuAtlas.ts
|
|
4
|
-
var
|
|
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;
|
|
5
9
|
var KERNEL_NEEDS = [
|
|
6
|
-
["maxComputeInvocationsPerWorkgroup",
|
|
7
|
-
["maxComputeWorkgroupSizeX",
|
|
8
|
-
["maxComputeWorkgroupSizeY",
|
|
9
|
-
["maxStorageBuffersPerShaderStage",
|
|
10
|
-
["maxStorageBuffersInFragmentStage", 1]
|
|
10
|
+
["maxComputeInvocationsPerWorkgroup", MIN_WORKGROUP_DIMENSION ** 2],
|
|
11
|
+
["maxComputeWorkgroupSizeX", MIN_WORKGROUP_DIMENSION],
|
|
12
|
+
["maxComputeWorkgroupSizeY", MIN_WORKGROUP_DIMENSION],
|
|
13
|
+
["maxStorageBuffersPerShaderStage", COMPUTE_STORAGE_BUFFERS]
|
|
11
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"));
|
|
24
|
+
}
|
|
12
25
|
/** A texture-to-buffer copy aligns each row to 256 bytes; the raster keeps 4 bytes a texel. */
|
|
13
26
|
function rasterRowBytes(width) {
|
|
14
27
|
return Math.ceil(width / 64) * 256;
|
|
@@ -17,14 +30,20 @@ function rasterRowBytes(width) {
|
|
|
17
30
|
* Past a device limit the passes are invalid, which WebGPU reports only asynchronously:
|
|
18
31
|
* checking first lets `auto` fall back without GPU work and `gpu` fail with a reason.
|
|
19
32
|
*/
|
|
20
|
-
function checkAtlasLimits(limits, width, height) {
|
|
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;
|
|
21
36
|
for (const [name, need] of KERNEL_NEEDS) {
|
|
22
37
|
const value = limits[name];
|
|
23
38
|
if (value !== void 0 && value < need) throw new RangeError(`buildGpuSdfAtlas: ${name}=${value}, needs ${need}; use atlasMode "cpu"`);
|
|
24
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
|
+
}
|
|
25
44
|
const largest = limits.maxTextureDimension2D;
|
|
26
45
|
if (largest !== void 0 && Math.max(width, height) > largest) throw new RangeError(`buildGpuSdfAtlas: ${width}×${height} exceeds maxTextureDimension2D=${largest}; lower maxAtlasSize`);
|
|
27
|
-
const bytes = Math.max(width * height *
|
|
46
|
+
const bytes = Math.max(width * height * SEED_BYTES_PER_TEXEL, rasterRowBytes(width) * height);
|
|
28
47
|
if (bytes > limits.maxStorageBufferBindingSize || bytes > limits.maxBufferSize) throw new RangeError(`buildGpuSdfAtlas: ${width}×${height} exceeds storage-buffer limits; use atlasMode "cpu" or lower maxAtlasSize`);
|
|
29
48
|
}
|
|
30
49
|
var ERROR_FILTERS = [
|
|
@@ -32,12 +51,6 @@ var ERROR_FILTERS = [
|
|
|
32
51
|
"out-of-memory",
|
|
33
52
|
"internal"
|
|
34
53
|
];
|
|
35
|
-
/**
|
|
36
|
-
* Captures what the device reports about the calls made until `close()`. Without a scope an
|
|
37
|
-
* invalid pass would surface only as a console warning, and the atlas would come back blank
|
|
38
|
-
* or wrong while `auto` saw nothing to fall back from. `close()` pops synchronously, so
|
|
39
|
-
* concurrent builds cannot interleave scopes, and never rejects.
|
|
40
|
-
*/
|
|
41
54
|
function openErrorScopes(device) {
|
|
42
55
|
const push = device.pushErrorScope;
|
|
43
56
|
const pop = device.popErrorScope;
|
|
@@ -50,19 +63,6 @@ function openErrorScopes(device) {
|
|
|
50
63
|
}, (error) => String(error));
|
|
51
64
|
} };
|
|
52
65
|
}
|
|
53
|
-
/**
|
|
54
|
-
* The jump-flood ladder only has to reach as far as the encoding can represent.
|
|
55
|
-
*
|
|
56
|
-
* A byte stores `0.5 - signedDistance / pxRange`, so everything beyond
|
|
57
|
-
* `pxRange / 2` pixels from an edge saturates to 0 or 1 no matter what distance
|
|
58
|
-
* the field holds there. A ladder `S, S/2, … 1` propagates at most `2S - 1`
|
|
59
|
-
* pixels per axis, so the smallest sufficient first step is `(radius + 1) / 2`
|
|
60
|
-
* rounded up to a power of two.
|
|
61
|
-
*
|
|
62
|
-
* Sizing the ladder from `pxRange` instead of the atlas extent is what keeps this
|
|
63
|
-
* at three or four passes rather than one per octave of the atlas: at 2048² and
|
|
64
|
-
* the default padding that is 4 passes instead of 12.
|
|
65
|
-
*/
|
|
66
66
|
function jfaSteps(pxRange) {
|
|
67
67
|
const radius = Math.ceil(pxRange * .5) + 1;
|
|
68
68
|
let step = 1;
|
|
@@ -71,59 +71,112 @@ function jfaSteps(pxRange) {
|
|
|
71
71
|
for (; step >= 1; step >>= 1) steps.push(step);
|
|
72
72
|
return steps;
|
|
73
73
|
}
|
|
74
|
-
/**
|
|
75
|
-
* One shader module and pipeline set per device, made on its first atlas and kept for its
|
|
76
|
-
* lifetime: a few native objects, while the buffers of a build live only through its
|
|
77
|
-
* submission. Writing the passes in WGSL spares every page three's node graph build of
|
|
78
|
-
* three passes on its first atlas, and every later build and patch the rebuild of their
|
|
79
|
-
* graphs (about 15 and 3 ms on an M3 Max).
|
|
80
|
-
*/
|
|
81
74
|
var devicePipelines = /* @__PURE__ */ new WeakMap();
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
75
|
+
var devicePipelinePromises = /* @__PURE__ */ new WeakMap();
|
|
76
|
+
function supportsAsyncAtlasPipelines(device, directStore) {
|
|
77
|
+
return typeof device.createComputePipelineAsync === "function" && (directStore || typeof device.createRenderPipelineAsync === "function");
|
|
78
|
+
}
|
|
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",
|
|
88
|
+
compute: {
|
|
89
|
+
module,
|
|
90
|
+
entryPoint
|
|
91
|
+
}
|
|
92
|
+
});
|
|
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({
|
|
90
100
|
layout: "auto",
|
|
91
|
-
|
|
101
|
+
vertex: {
|
|
92
102
|
module,
|
|
93
|
-
entryPoint
|
|
103
|
+
entryPoint: "fullscreenVert"
|
|
104
|
+
},
|
|
105
|
+
fragment: {
|
|
106
|
+
module,
|
|
107
|
+
entryPoint: "resolveFrag",
|
|
108
|
+
targets: [{ format: "r8unorm" }]
|
|
94
109
|
}
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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",
|
|
126
|
+
compute: {
|
|
127
|
+
module,
|
|
128
|
+
entryPoint
|
|
129
|
+
}
|
|
130
|
+
});
|
|
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,
|
|
154
|
+
resolve
|
|
111
155
|
};
|
|
112
156
|
devicePipelines.set(device, pipelines);
|
|
113
|
-
|
|
114
|
-
|
|
157
|
+
return pipelines;
|
|
158
|
+
}, (error) => {
|
|
159
|
+
devicePipelinePromises.delete(device);
|
|
160
|
+
throw error;
|
|
161
|
+
});
|
|
162
|
+
devicePipelinePromises.set(device, {
|
|
163
|
+
workgroupDim,
|
|
164
|
+
directStore,
|
|
165
|
+
promise
|
|
166
|
+
});
|
|
167
|
+
return promise;
|
|
115
168
|
}
|
|
116
169
|
/**
|
|
117
170
|
* @internal Starts the native compile of the device's atlas passes ahead of its first atlas,
|
|
118
|
-
* while the caller reads the DOM
|
|
119
|
-
* with a warm shader cache), and the atlas then waits for its own work only. Making them
|
|
120
|
-
* cannot throw; a device that rejects them fails the first atlas's submission instead, in its
|
|
121
|
-
* error scopes, and that build makes them again.
|
|
171
|
+
* while the caller reads the DOM.
|
|
122
172
|
*/
|
|
123
173
|
function prepareAtlasPasses(renderer) {
|
|
124
174
|
const device = atlasDevice(renderer);
|
|
125
|
-
if (device?.createShaderModule && !devicePipelines.has(device)) {
|
|
126
|
-
|
|
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);
|
|
127
180
|
device.queue.submit([]);
|
|
128
181
|
}
|
|
129
182
|
}
|
|
@@ -149,27 +202,30 @@ function atlasDevice(renderer) {
|
|
|
149
202
|
}
|
|
150
203
|
/**
|
|
151
204
|
* @internal Runs the atlas passes over `raster` into level 0 of a new r8unorm texture of
|
|
152
|
-
* `mipLevelCount` levels, all in one command buffer
|
|
153
|
-
* of 256-byte rows, the seeds are found and flooded in two ping-pong buffers, and the resolve
|
|
154
|
-
* pass encodes the distances. `finish` may add commands that use the texture before it is
|
|
155
|
-
* submitted, as a patch does. Resolves with the texture once the device has accepted every
|
|
156
|
-
* call; rejects, holding nothing, when the device's limits cannot hold the passes, lacks
|
|
157
|
-
* these calls or reports an error for them. The buffers and the upload live only through the
|
|
158
|
-
* submission: destroying them waits for the submitted work.
|
|
205
|
+
* `mipLevelCount` levels, all in one command buffer.
|
|
159
206
|
*/
|
|
160
|
-
async function encodeGpuSdf(device, raster, mipLevelCount, finish) {
|
|
207
|
+
async function encodeGpuSdf(device, raster, mipLevelCount, finish, onSubmitted) {
|
|
161
208
|
const { width, height, pxRange } = raster;
|
|
162
209
|
const { GPUBufferUsage: buffers, GPUTextureUsage: textures } = globalThis;
|
|
163
210
|
if (!device?.createCommandEncoder || !buffers || !textures) throw new Error("buildGpuSdfAtlas: device cannot run the atlas passes");
|
|
164
|
-
|
|
211
|
+
const steps = jfaSteps(pxRange);
|
|
212
|
+
const directStore = supportsDirectStore(device);
|
|
213
|
+
checkAtlasLimits(device.limits, width, height, steps[0], directStore);
|
|
214
|
+
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);
|
|
218
|
+
} catch (error) {
|
|
219
|
+
throw new Error(`buildGpuSdfAtlas: the device rejected the atlas passes: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
220
|
+
}
|
|
221
|
+
else if (!(prebuilt?.directStore === directStore && prebuilt.workgroupDim === workgroupDim)) prebuilt = void 0;
|
|
165
222
|
const transient = [];
|
|
166
223
|
let output;
|
|
167
224
|
try {
|
|
168
225
|
const scopes = openErrorScopes(device);
|
|
169
226
|
let reported;
|
|
170
227
|
try {
|
|
171
|
-
const pipelines =
|
|
172
|
-
const steps = jfaSteps(pxRange);
|
|
228
|
+
const pipelines = prebuilt ?? atlasPipelinesSync(device, workgroupDim, directStore);
|
|
173
229
|
const rowBytes = rasterRowBytes(width);
|
|
174
230
|
const buffer = (size, usage) => {
|
|
175
231
|
const created = device.createBuffer({
|
|
@@ -186,20 +242,26 @@ async function encodeGpuSdf(device, raster, mipLevelCount, finish) {
|
|
|
186
242
|
});
|
|
187
243
|
transient.push(upload);
|
|
188
244
|
const words = buffer(rowBytes * height, buffers.STORAGE | buffers.COPY_DST);
|
|
189
|
-
const
|
|
245
|
+
const seedBytes = width * height * SEED_BYTES_PER_TEXEL;
|
|
246
|
+
const seeds = [buffer(seedBytes, buffers.STORAGE), buffer(seedBytes, buffers.STORAGE)];
|
|
190
247
|
const params = buffer(steps.length * 256 + 16, buffers.UNIFORM | buffers.COPY_DST);
|
|
191
248
|
const values = new Int32Array(steps.length * 64 + 4);
|
|
249
|
+
const invPxRange = 1 / pxRange;
|
|
192
250
|
values.set([
|
|
193
251
|
width,
|
|
194
252
|
height,
|
|
195
253
|
rowBytes / 4
|
|
196
254
|
]);
|
|
197
|
-
new Float32Array(values.buffer, 12, 1)[0] =
|
|
198
|
-
steps.forEach((step, index) =>
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
+
});
|
|
203
265
|
device.queue.writeBuffer(params, 0, values);
|
|
204
266
|
device.queue.copyExternalImageToTexture({
|
|
205
267
|
source: raster.canvas,
|
|
@@ -212,7 +274,7 @@ async function encodeGpuSdf(device, raster, mipLevelCount, finish) {
|
|
|
212
274
|
size: [width, height],
|
|
213
275
|
mipLevelCount,
|
|
214
276
|
format: "r8unorm",
|
|
215
|
-
usage: textures.RENDER_ATTACHMENT | textures.TEXTURE_BINDING | textures.COPY_SRC | textures.COPY_DST
|
|
277
|
+
usage: textures.RENDER_ATTACHMENT | textures.TEXTURE_BINDING | textures.COPY_SRC | textures.COPY_DST | (pipelines.directStore ? textures.STORAGE_BINDING ?? 0 : 0)
|
|
216
278
|
});
|
|
217
279
|
const bind = (pipeline, offset, ...storageBuffers) => device.createBindGroup({
|
|
218
280
|
layout: pipeline.getBindGroupLayout(0),
|
|
@@ -234,36 +296,75 @@ async function encodeGpuSdf(device, raster, mipLevelCount, finish) {
|
|
|
234
296
|
bytesPerRow: rowBytes,
|
|
235
297
|
rowsPerImage: height
|
|
236
298
|
}, [width, height]);
|
|
237
|
-
const groupsX = Math.ceil(width /
|
|
238
|
-
const groupsY = Math.ceil(height /
|
|
299
|
+
const groupsX = Math.ceil(width / pipelines.workgroupDim);
|
|
300
|
+
const groupsY = Math.ceil(height / pipelines.workgroupDim);
|
|
239
301
|
const flood = encoder.beginComputePass();
|
|
240
302
|
flood.setPipeline(pipelines.seed);
|
|
241
303
|
flood.setBindGroup(0, bind(pipelines.seed, 0, words, seeds[0]));
|
|
242
304
|
flood.dispatchWorkgroups(groupsX, groupsY);
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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
|
+
}));
|
|
247
338
|
flood.dispatchWorkgroups(groupsX, groupsY);
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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
|
+
}
|
|
259
358
|
finish?.(encoder, output);
|
|
260
359
|
device.queue.submit([encoder.finish()]);
|
|
261
360
|
} finally {
|
|
262
361
|
reported = scopes.close();
|
|
263
362
|
}
|
|
363
|
+
onSubmitted?.(output);
|
|
264
364
|
const rejection = await reported;
|
|
265
365
|
if (rejection !== null) {
|
|
266
366
|
devicePipelines.delete(device);
|
|
367
|
+
devicePipelinePromises.delete(device);
|
|
267
368
|
throw new Error(`buildGpuSdfAtlas: the device rejected the atlas passes: ${rejection}`);
|
|
268
369
|
}
|
|
269
370
|
const retained = output;
|
|
@@ -275,59 +376,93 @@ async function encodeGpuSdf(device, raster, mipLevelCount, finish) {
|
|
|
275
376
|
}
|
|
276
377
|
}
|
|
277
378
|
/**
|
|
278
|
-
* Generates a compact R8 runtime SDF on the renderer's WebGPU device: boundary seeding and
|
|
279
|
-
*
|
|
280
|
-
* texture is
|
|
281
|
-
*
|
|
282
|
-
* the passes, the backend lacks the calls, or the device reports an error for them.
|
|
379
|
+
* Generates a compact R8 runtime SDF on the renderer's WebGPU device: boundary seeding and
|
|
380
|
+
* 4-byte relative fixed-point jump flooding in compute passes, writing directly into `r8unorm`
|
|
381
|
+
* when `'texture-formats-tier1'` is active on the device or resolving via a fullscreen render
|
|
382
|
+
* pass on Core WebGPU 1.0 devices.
|
|
283
383
|
*/
|
|
284
|
-
async function buildGpuSdfAtlas(renderer, raster, options = {}) {
|
|
384
|
+
async function buildGpuSdfAtlas(renderer, raster, options = {}, onProvisional) {
|
|
285
385
|
await renderer.init();
|
|
286
386
|
if (!(renderer.backend instanceof WebGPUBackend)) throw new Error("buildGpuSdfAtlas: requires the WebGPU backend; use mode \"cpu\"");
|
|
287
387
|
const { width, height } = raster;
|
|
288
388
|
const mipmaps = options.mipmaps === true;
|
|
289
389
|
const levels = mipmaps ? Math.floor(Math.log2(Math.max(width, height))) + 1 : 1;
|
|
290
|
-
const
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
390
|
+
const { shapeTop } = raster;
|
|
391
|
+
const maxAnisotropy = mipmaps ? renderer.getMaxAnisotropy() : 1;
|
|
392
|
+
const wrapAtlas = (gpuTexture) => {
|
|
393
|
+
const texture = new ExternalTexture(gpuTexture);
|
|
394
|
+
let disposed = false;
|
|
395
|
+
let gpuDestroyed = false;
|
|
396
|
+
const dispose = () => {
|
|
397
|
+
if (disposed) return;
|
|
398
|
+
disposed = true;
|
|
399
|
+
try {
|
|
400
|
+
texture.dispose();
|
|
401
|
+
} finally {
|
|
402
|
+
if (!gpuDestroyed) {
|
|
403
|
+
gpuDestroyed = true;
|
|
404
|
+
gpuTexture.destroy();
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
};
|
|
296
408
|
try {
|
|
297
|
-
texture.
|
|
298
|
-
|
|
299
|
-
|
|
409
|
+
texture.image = {
|
|
410
|
+
width,
|
|
411
|
+
height
|
|
412
|
+
};
|
|
413
|
+
texture.name = "ultra-text SDF atlas";
|
|
414
|
+
texture.format = RedFormat;
|
|
415
|
+
texture.colorSpace = NoColorSpace;
|
|
416
|
+
texture.generateMipmaps = mipmaps;
|
|
417
|
+
texture.minFilter = mipmaps ? LinearMipmapLinearFilter : LinearFilter;
|
|
418
|
+
texture.magFilter = LinearFilter;
|
|
419
|
+
texture.anisotropy = mipmaps ? Math.min(Math.max(1, options.anisotropy ?? 1), maxAnisotropy) : 1;
|
|
420
|
+
if (shapeTop !== void 0) texture.userData["ultraShapeTop"] = shapeTop;
|
|
421
|
+
} catch (error) {
|
|
422
|
+
dispose();
|
|
423
|
+
throw error;
|
|
300
424
|
}
|
|
425
|
+
return {
|
|
426
|
+
atlas: {
|
|
427
|
+
...shapeTop === void 0 ? {} : { shapeTop },
|
|
428
|
+
texture,
|
|
429
|
+
width,
|
|
430
|
+
height,
|
|
431
|
+
pxRange: raster.pxRange,
|
|
432
|
+
bakeSize: raster.bakeSize,
|
|
433
|
+
glyphs: raster.glyphs,
|
|
434
|
+
mode: "gpu",
|
|
435
|
+
dispose
|
|
436
|
+
},
|
|
437
|
+
markGpuDestroyed: () => {
|
|
438
|
+
gpuDestroyed = true;
|
|
439
|
+
}
|
|
440
|
+
};
|
|
301
441
|
};
|
|
442
|
+
if (onProvisional && !mipmaps) {
|
|
443
|
+
let wrapped;
|
|
444
|
+
try {
|
|
445
|
+
const gpuTexture = await encodeGpuSdf(atlasDevice(renderer), raster, levels, void 0, (candidate) => {
|
|
446
|
+
wrapped = wrapAtlas(candidate);
|
|
447
|
+
onProvisional(wrapped.atlas);
|
|
448
|
+
});
|
|
449
|
+
return wrapped ? wrapped.atlas : wrapAtlas(gpuTexture).atlas;
|
|
450
|
+
} catch (error) {
|
|
451
|
+
if (wrapped) {
|
|
452
|
+
wrapped.markGpuDestroyed();
|
|
453
|
+
wrapped.atlas.dispose();
|
|
454
|
+
}
|
|
455
|
+
throw error;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
const { atlas } = wrapAtlas(await encodeGpuSdf(atlasDevice(renderer), raster, levels));
|
|
302
459
|
try {
|
|
303
|
-
|
|
304
|
-
width,
|
|
305
|
-
height
|
|
306
|
-
};
|
|
307
|
-
texture.name = "ultra-text SDF atlas";
|
|
308
|
-
texture.format = RedFormat;
|
|
309
|
-
texture.colorSpace = NoColorSpace;
|
|
310
|
-
texture.generateMipmaps = mipmaps;
|
|
311
|
-
texture.minFilter = mipmaps ? LinearMipmapLinearFilter : LinearFilter;
|
|
312
|
-
texture.magFilter = LinearFilter;
|
|
313
|
-
texture.anisotropy = mipmaps ? Math.min(Math.max(1, options.anisotropy ?? 1), renderer.getMaxAnisotropy()) : 1;
|
|
314
|
-
if (mipmaps) await generateAtlasMips(renderer, texture);
|
|
460
|
+
if (mipmaps) await generateAtlasMips(renderer, atlas.texture);
|
|
315
461
|
} catch (error) {
|
|
316
|
-
dispose();
|
|
462
|
+
atlas.dispose();
|
|
317
463
|
throw error;
|
|
318
464
|
}
|
|
319
|
-
|
|
320
|
-
return {
|
|
321
|
-
...shapeTop === void 0 ? {} : { shapeTop },
|
|
322
|
-
texture,
|
|
323
|
-
width,
|
|
324
|
-
height,
|
|
325
|
-
pxRange: raster.pxRange,
|
|
326
|
-
bakeSize: raster.bakeSize,
|
|
327
|
-
glyphs: raster.glyphs,
|
|
328
|
-
mode: "gpu",
|
|
329
|
-
dispose
|
|
330
|
-
};
|
|
465
|
+
return atlas;
|
|
331
466
|
}
|
|
332
467
|
export { atlasDevice, buildGpuSdfAtlas, encodeGpuSdf, generateAtlasMips, prepareAtlasPasses };
|
|
333
468
|
|