@skathi-native/cli 0.1.0-beta.3 → 0.2.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.
Files changed (68) hide show
  1. package/aot-inputs.ts +21 -2
  2. package/build.ts +241 -60
  3. package/check/bundle.ts +265 -0
  4. package/check/codes.ts +19 -0
  5. package/check/command.ts +5 -3
  6. package/check/graph.ts +3 -1
  7. package/check/index.ts +54 -2
  8. package/check/program.ts +26 -6
  9. package/cli/aot-build.ts +10 -7
  10. package/cli/build-app.ts +37 -8
  11. package/cli/config.ts +2 -2
  12. package/cli/create.ts +31 -8
  13. package/cli/dev.ts +109 -19
  14. package/cli/effects.ts +162 -0
  15. package/cli/elf.ts +3 -2
  16. package/cli/help.ts +56 -147
  17. package/cli/host-fetch.ts +33 -10
  18. package/cli/host.ts +43 -33
  19. package/cli/sk.ts +437 -234
  20. package/cli/skpk.ts +2 -0
  21. package/cli/stamp.ts +2 -2
  22. package/cli/term.ts +169 -38
  23. package/cli/workload-cache.ts +258 -0
  24. package/devtools/commands.ts +25 -0
  25. package/effects/compile.ts +1081 -0
  26. package/effects/redslangc.json +23 -0
  27. package/effects/redslangc.ts +220 -0
  28. package/effects/sk.slang +882 -0
  29. package/package.json +10 -4
  30. package/prod.ts +40 -7
  31. package/svelte.ts +76 -0
  32. package/templates/icon.png +0 -0
  33. package/templates/minimal/src/App.vue +2 -2
  34. package/templates/showcase/assets/textures/golden.png +0 -0
  35. package/templates/showcase/assets/textures/hammered.png +0 -0
  36. package/templates/showcase/assets/textures/noir.png +0 -0
  37. package/templates/showcase/assets/textures/reeded.png +0 -0
  38. package/templates/showcase/assets/textures/teal-orange.png +0 -0
  39. package/templates/showcase/shaders/dissolve.slang +40 -0
  40. package/templates/showcase/shaders/fireflies.slang +58 -0
  41. package/templates/showcase/shaders/glass.slang +31 -0
  42. package/templates/showcase/shaders/grade.slang +34 -0
  43. package/templates/showcase/shaders/halftone.slang +42 -0
  44. package/templates/showcase/shaders/lens.slang +51 -0
  45. package/templates/showcase/shaders/nebula.slang +57 -0
  46. package/templates/showcase/shaders/orbit.slang +52 -0
  47. package/templates/showcase/shaders/pond.slang +76 -0
  48. package/templates/showcase/shaders/ripples.slang +71 -0
  49. package/templates/showcase/shaders/shatter.slang +116 -0
  50. package/templates/showcase/shaders/sheen.slang +23 -0
  51. package/templates/showcase/shaders/snow.slang +57 -0
  52. package/templates/showcase/shaders/spotlight.slang +52 -0
  53. package/templates/showcase/src/App.vue +11 -16
  54. package/templates/showcase/src/components/FullWindow.vue +77 -0
  55. package/templates/showcase/src/components/Page.vue +2 -0
  56. package/templates/showcase/src/fullwindow.ts +36 -0
  57. package/templates/showcase/src/icons.ts +2 -0
  58. package/templates/showcase/src/pages/GraphicsPage.vue +122 -0
  59. package/templates/showcase/src/pages/MotionPage.vue +71 -1
  60. package/templates/showcase/src/pages/ShadersPage.vue +443 -0
  61. package/templates/showcase/src/pages/StylesPage.vue +9 -1
  62. package/templates/showcase/src/router.ts +32 -1
  63. package/templates/showcase/src/styles.css +2 -2
  64. package/templates/svelte/.vscode/settings.json +3 -0
  65. package/templates/svelte/assets/hello.txt +1 -0
  66. package/templates/svelte/src/App.svelte +142 -0
  67. package/templates/svelte/src/main.ts +4 -0
  68. package/vitecompat.ts +2 -0
@@ -0,0 +1,882 @@
1
+ // sk — the engine prelude every effect imports.
2
+ //
3
+ // import sk;
4
+ //
5
+ // [SkEffect("ripple")]
6
+ // struct Ripple : IEffect
7
+ // {
8
+ // [SkDefault("1.5")] [SkRange(0.0, 8.0)] float speed;
9
+ // [SkLength] [SkDefault("24")] float wavelength;
10
+ // [SkColor] [SkDefault("#7fd4ff")] float4 tint;
11
+ //
12
+ // float4 shade(EffectInput input) { ... }
13
+ // };
14
+ //
15
+ // That is the WHOLE authoring surface. No entry point, no semantics, no
16
+ // register bindings, no vertex stage — all of that lives below, and the
17
+ // compiler appends a short wrapper that calls `sk_begin` / `sk_finish`
18
+ // around your `shade`. One effect per file, in the app's `shaders/`:
19
+ // `sk build`, `sk run` and `sk publish` compile them into the app for every
20
+ // OS, `sk dev` recompiles on save, and `sk check` reports what fails.
21
+ // The attributes are not decoration. The compiler reflects them and turns
22
+ // that reflection into the CSS schema: `[SkEffect("ripple")]` is the
23
+ // function name, the FIELD ORDER is the argument order and the
24
+ // parameter-slot order, and the per-field attributes give the CSS type, the
25
+ // default and the clamp range. Nothing is registered twice; there is no Rust
26
+ // or JS list of your effect's parameters to keep in sync.
27
+ //
28
+ // Rules the engine cannot enforce for you:
29
+ // * Never call `Sample` inside a loop or a non-uniform branch — implicit
30
+ // derivatives are undefined there. Use `SampleLevel(..., 0)`.
31
+ // * `shade()` returns a PREMULTIPLIED colour by default. Either premultiply
32
+ // yourself (`sk_premultiply`) or opt out with
33
+ // `static override bool straightAlpha() { return true; }`. Either way
34
+ // the engine draws a premultiplied result (see `sk_finish`).
35
+ // * Effects compile at `ps_5_0`; the rest of gpui stays at `ps_4_1`.
36
+ // * An effect declares no textures, samplers or buffers: it reads
37
+ // `EffectInput` and its own fields, nothing else. An image is a field
38
+ // too: `SkTexture lut;`, `url(...)` in CSS.
39
+
40
+ module sk;
41
+
42
+ // ===========================================================================
43
+ // Authoring attributes — reflected by the compiler, read into the schema
44
+ // ===========================================================================
45
+ //
46
+ // Slang user-defined attributes are declared as structs tagged
47
+ // `[__AttributeUsage]`; their arguments become the `arguments` array under
48
+ // `userAttribs` in `-reflection-json`. This is the ONLY channel by which
49
+ // information travels from a `.slang` file to the CPU side, which is why every
50
+ // piece of CSS-facing metadata is spelled as an attribute rather than as, say,
51
+ // a `static const` (not reflected) or a field initialiser (also not reflected).
52
+
53
+ /// The CSS function name: `[SkEffect("ripple")]` -> `--shading: ripple(...)`.
54
+ /// Required, exactly once, on the effect struct.
55
+ [__AttributeUsage(_AttributeTargets.Struct)]
56
+ public struct SkEffectAttribute
57
+ {
58
+ public string name;
59
+ };
60
+
61
+ /// This effect reads `input.backdrop`, so the renderer must snapshot the
62
+ /// render target and draw the quad with blending disabled. Costs a full
63
+ /// render-target copy per effect group — do not add it unless `shade` really
64
+ /// samples the backdrop. Implied by `[SkBackdropRadius]`.
65
+ [__AttributeUsage(_AttributeTargets.Struct)]
66
+ public struct SkBackdropAttribute
67
+ {
68
+ };
69
+
70
+ /// This effect shades its element's OWN painting: `input.content`,
71
+ /// `contentAt`, `contentAtPx`. The element and its whole subtree (background,
72
+ /// border, shadows, outline, children, text, video, carets, animations) paint
73
+ /// into an offscreen group the size of the window, at full opacity, and the
74
+ /// effect is drawn in their place; what it returns is all that shows of them.
75
+ ///
76
+ /// * The drawn quad is the border box, dilated by a `[SkBleed]` parameter:
77
+ /// content can be moved out to it (a box-shadow outside the box shows only
78
+ /// there). Sampling anywhere else in the window is allowed and reads
79
+ /// transparent where the element painted nothing.
80
+ /// * `opacity` applies once, to the result; the content is opaque in the
81
+ /// group. `overflow` clips of ancestors cut the result like any effect's.
82
+ /// * A transform moves the content and the quad together; a rotation or skew
83
+ /// cannot be drawn by an effect, and the element then shows unshaded.
84
+ /// * Hit testing is unchanged: it follows layout, never pixels.
85
+ /// * The group is a Backdrop Root: a `backdrop-filter` or backdrop effect
86
+ /// inside it sees the element's own painting, not the page. The element's
87
+ /// own `backdrop-filter` still reads the page. With `[SkBackdrop]` too, the
88
+ /// effect owns its quad and composites the content over the backdrop
89
+ /// itself (`sk_over(input.backdrop, input.content)`).
90
+ /// * Cost: one window-sized offscreen target per nesting depth (pooled,
91
+ /// released after 90 idle frames) and the subtree drawn into it every
92
+ /// frame the window paints.
93
+ [__AttributeUsage(_AttributeTargets.Struct)]
94
+ public struct SkContentAttribute
95
+ {
96
+ };
97
+
98
+ /// This effect keeps state from frame to frame: `input.history`,
99
+ /// `historyAt`, `historyAtPx` read what it stored the last time it stepped,
100
+ /// per element and effect. The history is two RGBA 16-bit float textures the
101
+ /// size of the drawn quad in device pixels (16 bytes a pixel), zero when the
102
+ /// element first draws, started over when that size changes and freed the
103
+ /// first frame the element is not painted.
104
+ ///
105
+ /// Each step stores what `update` returns for every texel, unclamped; an
106
+ /// effect that does not declare `override float4 update(EffectInput input)`
107
+ /// stores what `shade` returned. Both
108
+ /// `shade` and `update` read the state as the LAST step left it, so the first
109
+ /// frame reads zeros. `update` runs with `input.historyPass` set and reads
110
+ /// content and history but never the backdrop.
111
+ ///
112
+ /// * A history steps at most once per painted frame and at most about 75
113
+ /// times a second, so a simulation runs at the same speed at 60 and 144
114
+ /// Hz. It only steps while the window repaints: animate the effect
115
+ /// (`[SkAnimated]`, `[SkAnimatedFor]`, or `[SkPointer]`, which repaints
116
+ /// on pointer moves) for as long as the state should keep changing.
117
+ /// * Limits: no history on a quad longer than 4096 device pixels on a side,
118
+ /// and at most 256 MiB of histories per window; past them it reads zeros.
119
+ /// * The element needs an identity to key its state by (every Skathi element
120
+ /// has one).
121
+ [__AttributeUsage(_AttributeTargets.Struct)]
122
+ public struct SkHistoryAttribute
123
+ {
124
+ };
125
+
126
+ /// One line of author-facing documentation, echoed in CSS advisories.
127
+ [__AttributeUsage(_AttributeTargets.Struct)]
128
+ public struct SkDocAttribute
129
+ {
130
+ public string text;
131
+ };
132
+
133
+ /// The parameter's default, written the way it would be written in CSS:
134
+ /// `"1.5"`, `"0 0.5"`, `"#7fd4ff"`, `"#ffffff28"`.
135
+ ///
136
+ /// A STRING rather than floats on purpose: Slang attribute structs have a
137
+ /// fixed field list, so a numeric spelling would need one attribute per arity
138
+ /// (`SkDefault`, `SkDefault2`, `SkDefault3`, `SkDefault4`). One string covers
139
+ /// every arity AND lets a colour default be written as a hex literal.
140
+ [__AttributeUsage(_AttributeTargets.Var)]
141
+ public struct SkDefaultAttribute
142
+ {
143
+ public string value;
144
+ };
145
+
146
+ /// Inclusive clamp for a scalar parameter. Out-of-range CSS values are
147
+ /// clamped WITH an advisory, never dropped.
148
+ [__AttributeUsage(_AttributeTargets.Var)]
149
+ public struct SkRangeAttribute
150
+ {
151
+ public float lo;
152
+ public float hi;
153
+ };
154
+
155
+ /// The CSS argument is a `<length>` (`24px`), not a `<number>`. Lengths are
156
+ /// authored in LOGICAL pixels and multiplied by the device scale factor before
157
+ /// they reach the shader, so `input.px` and a length parameter are in the same
158
+ /// units on every monitor.
159
+ [__AttributeUsage(_AttributeTargets.Var)]
160
+ public struct SkLengthAttribute
161
+ {
162
+ };
163
+
164
+ /// The CSS argument is a `<color>`. Only legal on a `float4`; the four
165
+ /// channels arrive as straight-alpha RGBA in 0..1.
166
+ [__AttributeUsage(_AttributeTargets.Var)]
167
+ public struct SkColorAttribute
168
+ {
169
+ };
170
+
171
+ /// The CSS argument may be omitted. Matching is positional but type-directed,
172
+ /// so an omitted optional parameter is skipped when the argument in that
173
+ /// position does not have its type — which is how `glow(12px, 0.8)` and
174
+ /// `glow(#7fd4ff, 12px, 0.8)` both parse. An optional parameter must therefore
175
+ /// differ in CSS type from the parameter that follows it; the compiler rejects
176
+ /// the effect at compile time otherwise.
177
+ [__AttributeUsage(_AttributeTargets.Var)]
178
+ public struct SkOptionalAttribute
179
+ {
180
+ };
181
+
182
+ /// Pins this parameter to an explicit slot in the 32-float parameter block,
183
+ /// instead of letting the packer choose. Unpinned fields then fill the gaps in
184
+ /// declaration order.
185
+ ///
186
+ /// Needed only when the packing is ALREADY a published contract — `glow`'s is,
187
+ /// because CSS transitions interpolate slot by slot and its layout predates
188
+ /// reflection-driven packing. A new effect should never need this: declare the
189
+ /// fields in the order you want the CSS arguments and let the packer work.
190
+ [__AttributeUsage(_AttributeTargets.Var)]
191
+ public struct SkSlotAttribute
192
+ {
193
+ public int index;
194
+ };
195
+
196
+ /// This length parameter DILATES the drawn quad by its value, giving an outer
197
+ /// glow somewhere to land. At most one per effect.
198
+ [__AttributeUsage(_AttributeTargets.Var)]
199
+ public struct SkBleedAttribute
200
+ {
201
+ };
202
+
203
+ /// This length parameter is the radius the backdrop is blurred to before
204
+ /// `input.backdrop` is sampled. Implies `[SkBackdrop]`. At most one per
205
+ /// effect.
206
+ [__AttributeUsage(_AttributeTargets.Var)]
207
+ public struct SkBackdropRadiusAttribute
208
+ {
209
+ };
210
+
211
+ /// While this parameter is > 0 the effect is treated as animated and the
212
+ /// window keeps repainting. Put it on the parameter that actually drives
213
+ /// `input.time` (a speed), so `speed: 0` costs nothing. At most one per
214
+ /// effect; an effect with no `[SkAnimated]` parameter never repaints on its
215
+ /// own.
216
+ [__AttributeUsage(_AttributeTargets.Var)]
217
+ public struct SkAnimatedAttribute
218
+ {
219
+ };
220
+
221
+ /// Every component of this parameter is a time on the effect clock (the
222
+ /// clock `input.time` reads, which is `performance.now() / 1000` in JS). The
223
+ /// window keeps repainting until `seconds` after the latest of them, then the
224
+ /// effect costs nothing until a parameter changes: hand it the time something
225
+ /// started instead of rewriting an age every frame.
226
+ [__AttributeUsage(_AttributeTargets.Var)]
227
+ public struct SkAnimatedForAttribute
228
+ {
229
+ public float seconds;
230
+ };
231
+
232
+ /// A `float2` the host fills with the pointer's position in `input.local`
233
+ /// units: 0..1 across the element, beyond that outside it, anywhere in the
234
+ /// window. The last position stays when the pointer leaves the window. Not a
235
+ /// CSS argument. The element repaints when the pointer moves, and only
236
+ /// elements whose effect has a pointer parameter do.
237
+ [__AttributeUsage(_AttributeTargets.Var)]
238
+ public struct SkPointerAttribute
239
+ {
240
+ };
241
+
242
+ /// On an `SkTexture`: the image is data (a normal map, noise, a LUT), sampled
243
+ /// as its file stores it — no premultiplication, alpha is just a fourth
244
+ /// channel. Without it the image is colour, sampled premultiplied in the same
245
+ /// sRGB-encoded space as `input.backdrop`.
246
+ [__AttributeUsage(_AttributeTargets.Var)]
247
+ public struct SkLinearAttribute
248
+ {
249
+ };
250
+
251
+ /// A `float3` the host fills with times on the effect clock: when the pointer
252
+ /// last moved in the window, when it last entered the window and when it last
253
+ /// left it. It is in the window while `y > z`; `sk_pointer_presence` turns
254
+ /// the three into a fade. Long ago (about -1e4) until it happens. Not a CSS
255
+ /// argument; repaints like `[SkPointer]`, and with `[SkAnimatedFor]` keeps
256
+ /// repainting for a fade after each of them.
257
+ [__AttributeUsage(_AttributeTargets.Var)]
258
+ public struct SkPointerTimesAttribute
259
+ {
260
+ };
261
+
262
+ // ===========================================================================
263
+ // The engine's binding contract
264
+ // ===========================================================================
265
+ //
266
+ // These registers are gpui's, not ours to choose: t1 is the instance buffer
267
+ // for EVERY gpui pipeline, t0 is the processed backdrop (the Dual-Kawase
268
+ // pyramid's top level), t2 the untouched render-target snapshot, b0 the shared
269
+ // globals. s1 clamps — s0 wraps, which bleeds the opposite screen edge into
270
+ // anything sampling near the border. t3..t6 are an effect's `SkTexture`
271
+ // images and s2 the sampler that repeats them.
272
+ //
273
+ // They are declared HERE, in the module, rather than in a generated wrapper:
274
+ // Slang keeps explicit `register()` annotations through the HLSL and Metal
275
+ // backends (Metal: b0 buffer(0), t1 buffer(1), t0/t2 texture(0/2), s1
276
+ // sampler(1), t3..t6 texture(3..6), s2 sampler(2)), so the module is the right
277
+ // home and an effect cannot move one.
278
+ //
279
+ // `[[vk::binding(binding, group)]]` is the same contract for the WGSL target,
280
+ // matching the pipeline layout in gpui_wgpu: group 0 the globals, group 1 the
281
+ // instance buffer, group 2 the backdrop textures and their sampler, then the
282
+ // images at 3..6 and their sampler at 9.
283
+ //
284
+ // t7 is the element's content (`[SkContent]`), t8 its history (`[SkHistory]`):
285
+ // WGSL group 2 bindings 7 and 8, Metal texture(7) and texture(8). Every effect
286
+ // pipeline binds them, transparent when the effect has neither.
287
+
288
+ public struct SkBounds
289
+ {
290
+ public float2 origin;
291
+ public float2 size;
292
+ };
293
+
294
+ public struct SkCorners
295
+ {
296
+ public float top_left;
297
+ public float top_right;
298
+ public float bottom_right;
299
+ public float bottom_left;
300
+ };
301
+
302
+ /// Keep in sync with `gpui::EffectQuad` (vendor/zed/crates/gpui/src/scene.rs),
303
+ /// `struct EffectQuad` in gpui_windows/src/effects.hlsl and in
304
+ /// gpui_wgpu/src/shaders_effects.wgsl. 56 words / 224 bytes.
305
+ ///
306
+ /// The parameter block is `float[]`, not `float4`s: a WGSL storage buffer
307
+ /// aligns a `vec4` to 16 bytes, which would move it from offset 72 to 80.
308
+ public struct SkEffectQuad
309
+ {
310
+ public uint order;
311
+ public uint effect_id;
312
+ public SkBounds bounds;
313
+ public SkBounds content_mask;
314
+ /// The content mask's rounded corners; see `sk_content_mask_coverage`.
315
+ public SkCorners content_mask_radii;
316
+ public SkCorners corner_radii;
317
+ public float params[32];
318
+ public float opacity;
319
+ public float time;
320
+ public float bleed;
321
+ public float backdrop_blur_radius;
322
+ public float scale;
323
+ public uint flags;
324
+ };
325
+
326
+ [[vk::binding(0, 1)]]
327
+ public StructuredBuffer<SkEffectQuad> sk_effect_quads : register(t1);
328
+ [[vk::binding(0, 2)]]
329
+ public Texture2D<float4> sk_backdrop_input : register(t0);
330
+ [[vk::binding(2, 2)]]
331
+ public Texture2D<float4> sk_backdrop_original : register(t2);
332
+ [[vk::binding(1, 2)]]
333
+ public SamplerState sk_clamp_sampler : register(s1);
334
+ [[vk::binding(7, 2)]]
335
+ public Texture2D<float4> sk_content : register(t7);
336
+ [[vk::binding(8, 2)]]
337
+ public Texture2D<float4> sk_history : register(t8);
338
+
339
+ // The images of `SkTexture` parameters, in declaration order, and the sampler
340
+ // that repeats them.
341
+ [[vk::binding(3, 2)]]
342
+ Texture2D<float4> sk_texture0 : register(t3);
343
+ [[vk::binding(4, 2)]]
344
+ Texture2D<float4> sk_texture1 : register(t4);
345
+ [[vk::binding(5, 2)]]
346
+ Texture2D<float4> sk_texture2 : register(t5);
347
+ [[vk::binding(6, 2)]]
348
+ Texture2D<float4> sk_texture3 : register(t6);
349
+ [[vk::binding(9, 2)]]
350
+ SamplerState sk_repeat_sampler : register(s2);
351
+
352
+ /// Scalar padding rather than a `uint3`: WGSL aligns a `vec3` to 16 bytes and
353
+ /// would grow the block from 48 to 64.
354
+ [[vk::binding(0, 0)]]
355
+ public cbuffer GlobalParams : register(b0)
356
+ {
357
+ public float4 sk_gamma_ratios;
358
+ public float2 sk_viewport_size;
359
+ public float sk_grayscale_enhanced_contrast;
360
+ public float sk_subpixel_enhanced_contrast;
361
+ public uint sk_is_bgr;
362
+ public uint sk_global_pad0;
363
+ public uint sk_global_pad1;
364
+ public uint sk_global_pad2;
365
+ };
366
+
367
+ /// Bits of `SkEffectQuad::flags`, `gpui::EFFECT_FLAG_*`.
368
+ public static const uint SK_FLAG_NEEDS_BACKDROP = 1u;
369
+ public static const uint SK_FLAG_NEEDS_CONTENT = 2u;
370
+ public static const uint SK_FLAG_HISTORY = 4u;
371
+ public static const uint SK_FLAG_HISTORY_PASS = 8u;
372
+
373
+
374
+ // ===========================================================================
375
+ // The authoring contract
376
+ // ===========================================================================
377
+
378
+ /// Everything the engine hands an effect, in one struct.
379
+ public struct EffectInput
380
+ {
381
+ /// Screen-space UV of this pixel (0..1 across the whole window). This is
382
+ /// the coordinate `backdrop` was sampled at.
383
+ public float2 uv;
384
+ /// Position within the element box, normalised 0..1. Values outside 0..1
385
+ /// occur when the effect declares a `[SkBleed]` parameter.
386
+ public float2 local;
387
+ /// Position within the element box in DEVICE pixels (top-left origin).
388
+ public float2 px;
389
+ /// Element box size in device pixels.
390
+ public float2 size;
391
+ /// The backdrop behind this pixel, premultiplied: `backdropAt(uv)`. All
392
+ /// zero for effects that did not ask for it.
393
+ public float4 backdrop;
394
+ /// Signed distance to the ELEMENT's rounded rect in device pixels:
395
+ /// negative inside, 0 on the edge, positive outside.
396
+ public float sd;
397
+ /// Antialiased coverage of the element's rounded rect. 1 well inside,
398
+ /// 0 well outside.
399
+ public float coverage;
400
+ /// Seconds on the effect clock, which shares its zero with JS:
401
+ /// `input.time == performance.now() / 1000` (rAF timestamps too).
402
+ public float time;
403
+ /// Device pixels per logical pixel (the DPI scale factor).
404
+ public float scale;
405
+ /// How far outside the element's border box the drawn quad reaches, in
406
+ /// device pixels (the resolved `[SkBleed]` parameter, 0 without one).
407
+ public float bleed;
408
+ /// The element's `opacity`, already folded into the coverage the engine
409
+ /// applies after `shade` — exposed because an effect may want to fade a
410
+ /// component of its own by the same amount.
411
+ public float opacity;
412
+ /// The coverage of the content mask, the rounded clip of an `overflow`
413
+ /// ancestor with a `border-radius`: 1 inside it, and everywhere for a
414
+ /// square one. Folded into the coverage the engine applies after `shade`,
415
+ /// like `opacity`.
416
+ public float clip;
417
+ /// Whether this effect asked for the backdrop.
418
+ public bool hasBackdrop;
419
+ /// The element's own painting at this pixel, premultiplied:
420
+ /// `contentAt(uv)`. All zero without `[SkContent]`.
421
+ public float4 content;
422
+ /// Whether this effect reads its content (`[SkContent]`).
423
+ public bool hasContent;
424
+ /// This effect's state at this pixel as the last step stored it:
425
+ /// `historyAtPx(px)`. All zero without `[SkHistory]`, on the first frame,
426
+ /// and where the engine keeps no history (see `[SkHistory]`).
427
+ public float4 history;
428
+ /// Whether this quad has a history to read.
429
+ public bool hasHistory;
430
+ /// Whether this run computes the next history state (`update`) rather
431
+ /// than the element's pixels.
432
+ public bool historyPass;
433
+ /// This quad's index in the instance buffer.
434
+ public uint quad;
435
+
436
+ /// Parameter slot `slot` (0..31) — the raw packing behind the named
437
+ /// fields on your effect struct. Bypasses the schema; prefer the fields.
438
+ public float param(uint slot)
439
+ {
440
+ return sk_effect_quads[this.quad].params[slot];
441
+ }
442
+
443
+ // The backdrop. Both textures cover the whole window as it was painted
444
+ // before this effect, so any point may be sampled, not only the element's
445
+ // own area; outside the window they clamp to its edge. Neither holds the
446
+ // element's own background (a backdrop effect paints beneath it) nor
447
+ // another backdrop effect drawn in the same group (they never overlap).
448
+ // The blurred one is a lower-resolution pyramid level: blurred to
449
+ // `[SkBackdropRadius]`, or the raw snapshot when the effect has none.
450
+ // All four return zero for an effect without `[SkBackdrop]`.
451
+
452
+ /// The backdrop at a window UV (`input.uv` space), blurred to
453
+ /// `[SkBackdropRadius]` when the effect declares one.
454
+ public float4 backdropAt(float2 uv)
455
+ {
456
+ return this.hasBackdrop
457
+ ? sk_backdrop_input.SampleLevel(sk_clamp_sampler, uv, 0.0)
458
+ : float4(0.0, 0.0, 0.0, 0.0);
459
+ }
460
+
461
+ /// The backdrop at a point in this element's device pixels (`input.px`
462
+ /// space), blurred like `backdropAt`.
463
+ public float4 backdropAtPx(float2 px)
464
+ {
465
+ return this.backdropAt(this.pxToUv(px));
466
+ }
467
+
468
+ /// The untouched backdrop at a window UV, never blurred.
469
+ public float4 rawBackdropAt(float2 uv)
470
+ {
471
+ return this.hasBackdrop
472
+ ? sk_backdrop_original.SampleLevel(sk_clamp_sampler, uv, 0.0)
473
+ : float4(0.0, 0.0, 0.0, 0.0);
474
+ }
475
+
476
+ /// The untouched backdrop at a point in this element's device pixels.
477
+ public float4 rawBackdropAtPx(float2 px)
478
+ {
479
+ return this.rawBackdropAt(this.pxToUv(px));
480
+ }
481
+
482
+ /// A point in this element's device pixels as a window UV.
483
+ public float2 pxToUv(float2 px)
484
+ {
485
+ return this.uv + (px - this.px) / sk_viewport_size;
486
+ }
487
+
488
+ // The content. It covers the whole window, like the backdrop: the
489
+ // element's painting where it is, transparent elsewhere; outside the
490
+ // window it clamps to the edge. Zero without `[SkContent]`.
491
+
492
+ /// The element's painting at a window UV (`input.uv` space).
493
+ public float4 contentAt(float2 uv)
494
+ {
495
+ return this.hasContent
496
+ ? sk_content.SampleLevel(sk_clamp_sampler, uv, 0.0)
497
+ : float4(0.0, 0.0, 0.0, 0.0);
498
+ }
499
+
500
+ /// The element's painting at a point in its device pixels (`input.px`).
501
+ public float4 contentAtPx(float2 px)
502
+ {
503
+ return this.contentAt(this.pxToUv(px));
504
+ }
505
+
506
+ /// The size of the content texture in texels: the window's, in device
507
+ /// pixels. One texel is one `px` unit.
508
+ public float2 contentSize()
509
+ {
510
+ return sk_viewport_size;
511
+ }
512
+
513
+ // The history: the drawn quad (the border box dilated by `bleed`) in
514
+ // device pixels, one texel each, clamped at its edge. Zero without
515
+ // `[SkHistory]`.
516
+
517
+ /// The state at a point in this element's device pixels (`input.px`).
518
+ public float4 historyAtPx(float2 px)
519
+ {
520
+ float2 size = this.historySize();
521
+ return this.hasHistory
522
+ ? sk_history.SampleLevel(sk_clamp_sampler, (px + this.bleed) / size, 0.0)
523
+ : float4(0.0, 0.0, 0.0, 0.0);
524
+ }
525
+
526
+ /// The state at a window UV (`input.uv` space).
527
+ public float4 historyAt(float2 uv)
528
+ {
529
+ return this.historyAtPx(this.px + (uv - this.uv) * sk_viewport_size);
530
+ }
531
+
532
+ /// The size of the history in texels: the drawn quad's, in device pixels.
533
+ public float2 historySize()
534
+ {
535
+ return max(round(sk_effect_quads[this.quad].bounds.size), float2(1.0, 1.0));
536
+ }
537
+
538
+ /// Antialiased 0..1 coverage from a signed distance in device pixels.
539
+ /// One device pixel of feather, which is what the rest of gpui uses.
540
+ public float aa(float distance)
541
+ {
542
+ return saturate(0.5 - distance);
543
+ }
544
+
545
+ /// `local`, remapped so that (0,0) is the element centre and the SHORTER
546
+ /// axis spans -1..1 — the coordinate system almost every procedural
547
+ /// pattern actually wants, because it does not stretch with the element.
548
+ public float2 centered()
549
+ {
550
+ float2 c = this.local * 2.0 - 1.0;
551
+ float2 s = this.size / max(min(this.size.x, this.size.y), 1.0);
552
+ return c * s;
553
+ }
554
+ };
555
+
556
+ /// An image parameter, written `url(...)` in CSS and resolved like an
557
+ /// `<img src>`. At most four per effect. Sampled bilinearly at mip 0, so it is
558
+ /// safe in loops and branches. Until the image has loaded, or when it fails or
559
+ /// is omitted, every read returns the field's `[SkDefault]` colour
560
+ /// (transparent without one) and `size` is zero.
561
+ public struct SkTexture
562
+ {
563
+ /// The image's size in texels; (0, 0) while it is missing.
564
+ public float2 size;
565
+ uint slot;
566
+ float4 fallback;
567
+
568
+ public bool loaded()
569
+ {
570
+ return this.size.x > 0.0;
571
+ }
572
+
573
+ /// The image at a 0..1 UV, clamped to its edge texels outside it.
574
+ public float4 sample(float2 uv)
575
+ {
576
+ return this.loaded() ? sk_texture_at(this.slot, sk_clamp_sampler, uv) : this.fallback;
577
+ }
578
+
579
+ /// The image at a UV, repeating outside 0..1.
580
+ public float4 sampleRepeat(float2 uv)
581
+ {
582
+ return this.loaded() ? sk_texture_at(this.slot, sk_repeat_sampler, uv) : this.fallback;
583
+ }
584
+ };
585
+
586
+ /// Built by the generated wrapper; an effect receives its `SkTexture` fields filled in.
587
+ public SkTexture sk_texture(uint slot, float2 size, float4 fallback)
588
+ {
589
+ SkTexture t;
590
+ t.size = size;
591
+ t.slot = slot;
592
+ t.fallback = fallback;
593
+ return t;
594
+ }
595
+
596
+ float4 sk_texture_at(uint slot, SamplerState s, float2 uv)
597
+ {
598
+ float4 texel = float4(0.0, 0.0, 0.0, 0.0);
599
+ if (slot == 0u)
600
+ {
601
+ texel = sk_texture0.SampleLevel(s, uv, 0.0);
602
+ }
603
+ else if (slot == 1u)
604
+ {
605
+ texel = sk_texture1.SampleLevel(s, uv, 0.0);
606
+ }
607
+ else if (slot == 2u)
608
+ {
609
+ texel = sk_texture2.SampleLevel(s, uv, 0.0);
610
+ }
611
+ else if (slot == 3u)
612
+ {
613
+ texel = sk_texture3.SampleLevel(s, uv, 0.0);
614
+ }
615
+ return texel;
616
+ }
617
+
618
+ /// The one interface an effect implements.
619
+ public interface IEffect
620
+ {
621
+ /// Returns the colour for this pixel — PREMULTIPLIED, unless the effect
622
+ /// overrides `straightAlpha`.
623
+ float4 shade(EffectInput input);
624
+
625
+ /// Opt into authoring in STRAIGHT alpha:
626
+ ///
627
+ /// static override bool straightAlpha() { return true; }
628
+ ///
629
+ /// The engine then premultiplies whatever `shade` returns. Slang resolves
630
+ /// this at specialisation time, so the branch is folded away — it costs
631
+ /// nothing at runtime and removes the single most common way to get an
632
+ /// effect wrong.
633
+ static bool straightAlpha() { return false; }
634
+
635
+ /// With `[SkHistory]`: the state to store for this texel, read back next
636
+ /// step through `input.history`. Raw: not premultiplied, clamped or
637
+ /// covered, kept at 16-bit float precision. Defaults to `shade`.
638
+ float4 update(EffectInput input) { return this.shade(input); }
639
+ };
640
+
641
+ // ===========================================================================
642
+ // Engine composition — generic over the effect type
643
+ // ===========================================================================
644
+ //
645
+ // `sk_begin` and `sk_finish` are what a generated wrapper calls; between them
646
+ // it does nothing but unpack the parameter slots into the effect struct's
647
+ // named fields (which is why the compiler needs reflection at all). Because
648
+ // `sk_finish` is generic over `T : IEffect`, Slang specialises the whole
649
+ // composition at compile time: there is no indirect call, no vtable, and the
650
+ // engine-owned coverage/premultiply/backdrop-restore logic lives in this
651
+ // module instead of being pasted into every effect as templated text.
652
+
653
+ float sk_pick_corner_radius(float2 center_to_point, SkCorners radii)
654
+ {
655
+ // Branchless on purpose: the obvious nested-if spelling makes Slang emit a
656
+ // temporary that fxc then reports as `warning X4000: use of potentially
657
+ // uninitialized variable`, which would surface in every effect author's
658
+ // build log for no reason.
659
+ float top = center_to_point.x < 0.0 ? radii.top_left : radii.top_right;
660
+ float bottom = center_to_point.x < 0.0 ? radii.bottom_left : radii.bottom_right;
661
+ return center_to_point.y < 0.0 ? top : bottom;
662
+ }
663
+
664
+ /// Signed distance to a rounded rect. Negative inside, 0 on the edge.
665
+ public float sk_quad_sdf(float2 point, SkBounds bounds, SkCorners radii)
666
+ {
667
+ float2 half_size = bounds.size / 2.0;
668
+ float2 center_to_point = point - (bounds.origin + half_size);
669
+ float radius = sk_pick_corner_radius(center_to_point, radii);
670
+ float2 corner_center_to_point = abs(center_to_point) - half_size + radius;
671
+ if (radius == 0.0)
672
+ {
673
+ return max(corner_center_to_point.x, corner_center_to_point.y);
674
+ }
675
+ return length(max(float2(0.0, 0.0), corner_center_to_point))
676
+ + min(0.0, max(corner_center_to_point.x, corner_center_to_point.y))
677
+ - radius;
678
+ }
679
+
680
+ /// The coverage of the quad's content mask, the rounded clip an `overflow`
681
+ /// ancestor with a `border-radius` pushes. All-zero radii are the plain rect
682
+ /// the clip distances already cut, so they return 1 before any SDF math.
683
+ /// Mirrors `content_mask_coverage` in gpui_windows/src/shaders.hlsl.
684
+ public float sk_content_mask_coverage(float2 position, SkBounds mask, SkCorners radii)
685
+ {
686
+ if (radii.top_left == 0.0 && radii.top_right == 0.0 &&
687
+ radii.bottom_right == 0.0 && radii.bottom_left == 0.0)
688
+ {
689
+ return 1.0;
690
+ }
691
+ return saturate(0.5 - sk_quad_sdf(position, mask, radii));
692
+ }
693
+
694
+ /// The DirectComposition swap chain is premultiplied; keep rgb <= a so the
695
+ /// compositor never sees an out-of-gamut pixel. Applied by `sk_finish`, never
696
+ /// by effect code, precisely so an effect cannot skip it.
697
+ public float4 sk_clamp_premultiplied(float4 color)
698
+ {
699
+ // Written as one construction rather than two component writes: the
700
+ // mutating spelling makes Slang emit an indexed store (`_S[int(3)] = …`)
701
+ // that fxc then reports as `warning X4000: use of potentially
702
+ // uninitialized variable` in every effect that links this function.
703
+ float a = saturate(color.a);
704
+ return float4(clamp(color.rgb, 0.0, float3(a, a, a)), a);
705
+ }
706
+
707
+ /// Builds the `EffectInput` for this pixel. Called by the generated wrapper.
708
+ public EffectInput sk_begin(uint id, float4 frag_position)
709
+ {
710
+ SkEffectQuad q = sk_effect_quads[id];
711
+ bool history_pass = (q.flags & SK_FLAG_HISTORY_PASS) != 0u;
712
+ // A history pass draws into the history texture, whose texel (0,0) is
713
+ // the drawn quad's top-left device pixel.
714
+ float2 position = history_pass ? q.bounds.origin + frag_position.xy : frag_position.xy;
715
+ // SV_Position is the pixel centre, so this lands on a texel centre of the
716
+ // full-resolution snapshot.
717
+ float2 uv = position / sk_viewport_size;
718
+
719
+ // `bounds`/`corner_radii` describe the DRAW quad, which is the element's
720
+ // border box dilated by `bleed`. Dilating a rounded rect by b is a
721
+ // Minkowski sum with a disc of radius b, so its SDF is exactly the
722
+ // element's minus b — recover one from the other with a single add
723
+ // instead of carrying a second bounds pair through the instance buffer.
724
+ float sd_draw = sk_quad_sdf(position, q.bounds, q.corner_radii);
725
+ float sd_element = sd_draw + q.bleed;
726
+
727
+ float2 element_origin = q.bounds.origin + q.bleed;
728
+ float2 element_size = max(q.bounds.size - 2.0 * q.bleed, float2(1.0, 1.0));
729
+
730
+ bool has_backdrop = (q.flags & SK_FLAG_NEEDS_BACKDROP) != 0u;
731
+
732
+ EffectInput i;
733
+ i.uv = uv;
734
+ i.px = position - element_origin;
735
+ i.size = element_size;
736
+ i.local = i.px / element_size;
737
+ i.hasBackdrop = has_backdrop;
738
+ i.hasContent = (q.flags & SK_FLAG_NEEDS_CONTENT) != 0u;
739
+ i.hasHistory = (q.flags & SK_FLAG_HISTORY) != 0u;
740
+ i.historyPass = history_pass;
741
+ i.quad = id;
742
+ i.bleed = q.bleed;
743
+ i.backdrop = i.backdropAt(uv);
744
+ i.content = i.contentAt(uv);
745
+ i.history = i.historyAtPx(i.px);
746
+ i.sd = sd_element;
747
+ i.coverage = saturate(0.5 - sd_element);
748
+ i.time = q.time;
749
+ i.scale = q.scale;
750
+ i.opacity = q.opacity;
751
+ i.clip = sk_content_mask_coverage(position, q.content_mask, q.content_mask_radii);
752
+ return i;
753
+ }
754
+
755
+ /// Runs the effect and applies everything the engine owns: the straight-alpha
756
+ /// convention, the premultiplied clamp, the drawn quad's antialiased coverage,
757
+ /// the element opacity, and — for a backdrop effect, which draws with blending
758
+ /// disabled and therefore owns every pixel of its quad — restoring the
759
+ /// untouched snapshot outside that coverage.
760
+ ///
761
+ /// The result is PREMULTIPLIED whatever `straightAlpha` says. Every renderer
762
+ /// blends a non-backdrop effect as premultiplied source-over (One,
763
+ /// OneMinusSrcAlpha); blending it by source alpha would apply alpha twice.
764
+ public float4 sk_finish<T : IEffect>(T material, EffectInput i)
765
+ {
766
+ if (i.historyPass)
767
+ {
768
+ return material.update(i);
769
+ }
770
+ float4 shaded = material.shade(i);
771
+ if (T.straightAlpha())
772
+ {
773
+ shaded = float4(shaded.rgb * shaded.a, shaded.a);
774
+ }
775
+ shaded = sk_clamp_premultiplied(shaded);
776
+
777
+ float sd_draw = i.sd - i.bleed;
778
+ float coverage = saturate(0.5 - sd_draw) * saturate(i.opacity) * i.clip;
779
+
780
+ // Single exit on purpose — an early `return` inside the branch makes fxc
781
+ // lose track of `shaded` and warn about it.
782
+ float4 result;
783
+ if (i.hasBackdrop)
784
+ {
785
+ float4 original =
786
+ sk_backdrop_original.SampleLevel(sk_clamp_sampler, i.uv, 0.0);
787
+ result = lerp(original, shaded, coverage);
788
+ }
789
+ else
790
+ {
791
+ result = shaded * coverage;
792
+ }
793
+ return sk_clamp_premultiplied(result);
794
+ }
795
+
796
+ // ===========================================================================
797
+ // Helpers
798
+ // ===========================================================================
799
+
800
+ /// 0..1: how present the pointer is in the window, from `[SkPointerTimes]`
801
+ /// and `input.time`, fading in over `seconds` after it enters and out over
802
+ /// `seconds` after it leaves. Pair the field with `[SkAnimatedFor(seconds)]`.
803
+ public float sk_pointer_presence(float3 times, float time, float seconds)
804
+ {
805
+ float fade = max(seconds, 1e-4);
806
+ float entered = saturate((time - times.y) / fade);
807
+ float atLeave = saturate((times.z - times.y) / fade);
808
+ float leaving = atLeave * (1.0 - saturate((time - times.z) / fade));
809
+ return times.y > times.z ? entered : leaving;
810
+ }
811
+
812
+ /// Straight alpha -> premultiplied.
813
+ public float4 sk_premultiply(float4 color)
814
+ {
815
+ return float4(color.rgb * color.a, color.a);
816
+ }
817
+
818
+ /// Premultiplied `above` over premultiplied `below`.
819
+ public float4 sk_over(float4 below, float4 above)
820
+ {
821
+ return above + below * (1.0 - above.a);
822
+ }
823
+
824
+ /// Deterministic hash of a 2D point -> 0..1. Cheap, no texture needed.
825
+ public float sk_hash12(float2 p)
826
+ {
827
+ float3 p3 = frac(float3(p.x, p.y, p.x) * 0.1031);
828
+ p3 += dot(p3, float3(p3.y + 33.33, p3.z + 33.33, p3.x + 33.33));
829
+ return frac((p3.x + p3.y) * p3.z);
830
+ }
831
+
832
+ /// Deterministic hash of a 2D point -> two independent 0..1 values.
833
+ public float2 sk_hash22(float2 p)
834
+ {
835
+ float3 p3 = frac(float3(p.x, p.y, p.x) * float3(0.1031, 0.1030, 0.0973));
836
+ p3 += dot(p3, float3(p3.y + 33.33, p3.z + 33.33, p3.x + 33.33));
837
+ return frac((p3.xx + p3.yz) * p3.zy);
838
+ }
839
+
840
+ /// Value noise on a unit lattice, smoothstep-interpolated.
841
+ public float sk_value_noise(float2 p)
842
+ {
843
+ float2 i = floor(p);
844
+ float2 f = frac(p);
845
+ float2 u = f * f * (3.0 - 2.0 * f);
846
+ float a = sk_hash12(i);
847
+ float b = sk_hash12(i + float2(1.0, 0.0));
848
+ float c = sk_hash12(i + float2(0.0, 1.0));
849
+ float d = sk_hash12(i + float2(1.0, 1.0));
850
+ return lerp(lerp(a, b, u.x), lerp(c, d, u.x), u.y);
851
+ }
852
+
853
+ /// Three octaves of value noise. Unrolled: no `Sample` is involved, but a
854
+ /// fixed trip count keeps the generated HLSL flat.
855
+ public float sk_fbm(float2 p)
856
+ {
857
+ float sum = sk_value_noise(p) * 0.5;
858
+ sum += sk_value_noise(p * 2.03) * 0.25;
859
+ sum += sk_value_noise(p * 4.01) * 0.125;
860
+ return sum / 0.875;
861
+ }
862
+
863
+ /// Rotates a 2D point by `radians` about the origin.
864
+ public float2 sk_rotate(float2 p, float radians)
865
+ {
866
+ float s = sin(radians);
867
+ float c = cos(radians);
868
+ return float2(p.x * c - p.y * s, p.x * s + p.y * c);
869
+ }
870
+
871
+ /// Relative luminance of a linear-ish RGB triple.
872
+ public float sk_luma(float3 rgb)
873
+ {
874
+ return dot(rgb, float3(0.2126, 0.7152, 0.0722));
875
+ }
876
+
877
+ /// Inigo Quilez's cosine palette: `a + b * cos(2pi * (c * t + d))`. Four
878
+ /// float3 knobs that sweep a whole family of smooth, well-behaved gradients.
879
+ public float3 sk_palette(float t, float3 a, float3 b, float3 c, float3 d)
880
+ {
881
+ return a + b * cos(6.28318530718 * (c * t + d));
882
+ }