@skathi-native/cli 0.1.0-beta.4 → 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 (46) 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/graph.ts +3 -1
  5. package/check/index.ts +10 -1
  6. package/check/program.ts +26 -6
  7. package/cli/create.ts +29 -7
  8. package/devtools/commands.ts +25 -0
  9. package/effects/compile.ts +160 -31
  10. package/effects/sk.slang +353 -29
  11. package/package.json +7 -4
  12. package/prod.ts +40 -7
  13. package/svelte.ts +76 -0
  14. package/templates/icon.png +0 -0
  15. package/templates/showcase/assets/textures/golden.png +0 -0
  16. package/templates/showcase/assets/textures/hammered.png +0 -0
  17. package/templates/showcase/assets/textures/noir.png +0 -0
  18. package/templates/showcase/assets/textures/reeded.png +0 -0
  19. package/templates/showcase/assets/textures/teal-orange.png +0 -0
  20. package/templates/showcase/shaders/dissolve.slang +40 -0
  21. package/templates/showcase/shaders/fireflies.slang +58 -0
  22. package/templates/showcase/shaders/glass.slang +31 -0
  23. package/templates/showcase/shaders/grade.slang +34 -0
  24. package/templates/showcase/shaders/halftone.slang +42 -0
  25. package/templates/showcase/shaders/lens.slang +51 -0
  26. package/templates/showcase/shaders/nebula.slang +57 -0
  27. package/templates/showcase/shaders/orbit.slang +52 -0
  28. package/templates/showcase/shaders/pond.slang +76 -0
  29. package/templates/showcase/shaders/ripples.slang +71 -0
  30. package/templates/showcase/shaders/shatter.slang +116 -0
  31. package/templates/showcase/shaders/snow.slang +57 -0
  32. package/templates/showcase/shaders/spotlight.slang +52 -0
  33. package/templates/showcase/src/App.vue +11 -16
  34. package/templates/showcase/src/components/FullWindow.vue +77 -0
  35. package/templates/showcase/src/components/Page.vue +2 -0
  36. package/templates/showcase/src/fullwindow.ts +36 -0
  37. package/templates/showcase/src/icons.ts +2 -0
  38. package/templates/showcase/src/pages/GraphicsPage.vue +122 -0
  39. package/templates/showcase/src/pages/MotionPage.vue +71 -1
  40. package/templates/showcase/src/pages/ShadersPage.vue +443 -0
  41. package/templates/showcase/src/router.ts +32 -1
  42. package/templates/svelte/.vscode/settings.json +3 -0
  43. package/templates/svelte/assets/hello.txt +1 -0
  44. package/templates/svelte/src/App.svelte +142 -0
  45. package/templates/svelte/src/main.ts +4 -0
  46. package/vitecompat.ts +2 -0
@@ -20,7 +20,7 @@ import { PIN, ShaderCompilerError } from "./redslangc";
20
20
 
21
21
  /// Bumped when the prelude or generated wrapper changes codegen or schema shape; rides in every
22
22
  /// header and effects.json. Rust refuses an effects.json with an unknown schema.
23
- export const SCHEMA = 7;
23
+ export const SCHEMA = 10;
24
24
 
25
25
  export const PRELUDE = join(import.meta.dir, "sk.slang");
26
26
 
@@ -28,8 +28,8 @@ export const PRELUDE = join(import.meta.dir, "sk.slang");
28
28
  /// `gpui_windows` and `gpui_wgpu`, and the ids in `crates/sk-effects/generated/effects.json`. It is not alphabetical.
29
29
  export const BUILTIN_EFFECTS = ["frost", "noise", "glow"];
30
30
 
31
- /// Eight floats per instance (`EffectQuad::params0` + `params1`); raising it widens every effect quad.
32
- const PARAM_SLOTS = 8;
31
+ /// Floats per instance (`EffectQuad::params`); raising it widens every effect quad.
32
+ const PARAM_SLOTS = 32;
33
33
 
34
34
  const ENTRY = "effect_fragment";
35
35
 
@@ -73,8 +73,10 @@ const TARGETS: Target[] = [
73
73
  },
74
74
  ];
75
75
 
76
- // Group 0 the globals, 1 the instance buffer, 2 the backdrop, its sampler and the snapshot.
77
- const WGSL_BINDINGS = new Set(["0:0", "1:0", "2:0", "2:1", "2:2"]);
76
+ // Group 0 the globals, 1 the instance buffer, 2 the backdrop, its sampler and the snapshot, the
77
+ // `SkTexture` images (3..6), the element's content (7) and history (8), and the images' repeating
78
+ // sampler (9).
79
+ const WGSL_BINDINGS = new Set(["0:0", "1:0", "2:0", "2:1", "2:2", "2:3", "2:4", "2:5", "2:6", "2:7", "2:8", "2:9"]);
78
80
 
79
81
  /// The renderer builds one pipeline layout for every effect; a resource outside it would fail at
80
82
  /// pipeline creation on the user's machine, so refuse it here.
@@ -90,14 +92,21 @@ function checkWgslBindings(code: string, file: string) {
90
92
  }
91
93
  }
92
94
 
93
- /// The prelude's resources on Metal, where Slang keeps the HLSL registers (b0, t1, t0, t2, s1). One
95
+ /// The prelude's resources on Metal, where Slang keeps the HLSL registers (b0, t1, t0, t2, t3..t8, s1, s2). One
94
96
  /// argument table for every effect, so the renderer binds these and nothing else.
95
97
  const METAL_BINDINGS: Array<[at: string, name: string, what: string]> = [
96
98
  ["buffer(0)", "GlobalParams", "constant, the 48-byte globals"],
97
- ["buffer(1)", "sk_effect_quads", "device EffectQuad[], 128 bytes each"],
99
+ ["buffer(1)", "sk_effect_quads", "device EffectQuad[], 224 bytes each"],
98
100
  ["texture(0)", "sk_backdrop_input", "the blurred backdrop"],
99
101
  ["texture(2)", "sk_backdrop_original", "the render-target snapshot"],
102
+ ["texture(7)", "sk_content", "the element's content"],
103
+ ["texture(8)", "sk_history", "the effect's history"],
100
104
  ["sampler(1)", "sk_clamp_sampler", "clamp to edge"],
105
+ ["texture(3)", "sk_texture0", "the first SkTexture"],
106
+ ["texture(4)", "sk_texture1", "the second SkTexture"],
107
+ ["texture(5)", "sk_texture2", "the third SkTexture"],
108
+ ["texture(6)", "sk_texture3", "the fourth SkTexture"],
109
+ ["sampler(2)", "sk_repeat_sampler", "repeat"],
101
110
  ];
102
111
 
103
112
  /// Refuses Metal output whose entry point or arguments are not the ones the renderer provides.
@@ -126,26 +135,47 @@ const METAL_NOTE =
126
135
  METAL_BINDINGS.map(([at, name, what]) => `// [[${at}]]`.padEnd(20) + `${name.padEnd(22)}${what}\n`).join("") +
127
136
  `// The VERTEX half is the Metal renderer's own, shared by every effect.\n`;
128
137
 
129
- type ParamKind = "number" | "length" | "color";
138
+ type ParamKind = "number" | "length" | "color" | "texture";
130
139
 
131
140
  type ParamSchema = {
132
141
  name: string;
133
142
  kind: ParamKind;
143
+ /// A texture's first slot holds its width and the next its height, which the host writes.
134
144
  slot: number;
135
145
  required: boolean;
146
+ /// A texture's is what it reads as while missing, straight RGBA.
136
147
  default: [number, number, number, number];
137
148
  range: [number, number] | null;
149
+ /// A texture's binding, 0..3 (t3..t6), in declaration order.
150
+ texture?: number;
151
+ /// A texture sampled as stored, without premultiplying ([SkLinear]).
152
+ linear?: boolean;
138
153
  };
139
154
 
155
+ /// `SkTexture` fields per effect: the bindings the renderers provide.
156
+ const TEXTURE_SLOTS = 4;
157
+
140
158
  export type EffectSchema = {
141
159
  name: string;
142
160
  struct: string;
143
161
  doc: string | null;
144
162
  backdrop: boolean;
163
+ /// `[SkContent]`: drawn in place of its element's own painting, which it reads.
164
+ content: boolean;
165
+ /// `[SkHistory]`: keeps state per element from frame to frame.
166
+ history: boolean;
145
167
  params: ParamSchema[];
146
168
  backdropRadiusParam: number | null;
147
169
  bleedParam: number | null;
148
170
  animatedParam: number | null;
171
+ /// `[SkPointer]`'s first slot: the host writes the pointer there; not a CSS argument.
172
+ pointerParam: number | null;
173
+ /// `[SkPointerTimes]`'s first slot, host-filled like `pointerParam`.
174
+ pointerTimesParam: number | null;
175
+ /// Every slot of an `[SkAnimatedFor]` field: times on the effect clock.
176
+ clockSlots: number[];
177
+ /// The longest `[SkAnimatedFor]` window, in seconds.
178
+ clockWindow: number | null;
149
179
  signature: string;
150
180
  };
151
181
 
@@ -177,25 +207,24 @@ float4 sk_reflect_probe(float4 sk_position : SV_Position) : SV_Target
177
207
  `;
178
208
  }
179
209
 
180
- function slotExpr(slot: number): string {
181
- const bank = slot < 4 ? "params0" : "params1";
182
- return `sk_in.${bank}.${"xyzw"[slot % 4]}`;
183
- }
210
+ const slotExpr = (slot: number): string => `sk_in.param(${slot}u)`;
184
211
 
185
- function loadExpr(param: ParamSchema, components: number): string {
186
- if (components === 1) return slotExpr(param.slot);
212
+ function loadExpr(field: Field): string {
213
+ if (field.texture) {
214
+ const { index, fallback } = field.texture;
215
+ return (
216
+ `sk_texture(${index}u, float2(${slotExpr(field.slot)}, ${slotExpr(field.slot + 1)}), ` +
217
+ `float4(${fallback.join(", ")}))`
218
+ );
219
+ }
220
+ if (field.components === 1) return slotExpr(field.slot);
187
221
  const parts: string[] = [];
188
- for (let c = 0; c < components; c += 1) parts.push(slotExpr(param.slot + c));
189
- return `float${components}(${parts.join(", ")})`;
222
+ for (let c = 0; c < field.components; c += 1) parts.push(slotExpr(field.slot + c));
223
+ return `float${field.components}(${parts.join(", ")})`;
190
224
  }
191
225
 
192
226
  function realWrapper(effect: EffectSchema, fields: Field[]): string {
193
- const assignments = fields
194
- .map((field) => {
195
- const param = effect.params[field.firstParam];
196
- return ` sk_material.${field.name} = ${loadExpr(param!, field.components)};`;
197
- })
198
- .join("\n");
227
+ const assignments = fields.map((field) => ` sk_material.${field.name} = ${loadExpr(field)};`).join("\n");
199
228
  return `${WRAPPER_BANNER}
200
229
  // Kept for the record: reflection reads this declaration, every backend
201
230
  // strips it (nothing references it), and the renderer never binds it.
@@ -256,6 +285,7 @@ const readText = (file: string): string => readFileSync(file, "utf8").replace(/\
256
285
 
257
286
  interface ReflType {
258
287
  kind?: string;
288
+ name?: string;
259
289
  scalarType?: string;
260
290
  elementType?: ReflType;
261
291
  elementCount?: number | string;
@@ -366,8 +396,9 @@ type Attribute = { name: string; arguments: unknown[] };
366
396
  type Field = {
367
397
  name: string;
368
398
  components: number;
369
- attributes: Attribute[];
370
- firstParam: number;
399
+ slot: number;
400
+ /// An `SkTexture`: its binding and the colour it reads as while missing, as `sample` returns it.
401
+ texture?: { index: number; fallback: [number, number, number, number] };
371
402
  };
372
403
 
373
404
  function attr(attrs: Attribute[], name: string): Attribute | undefined {
@@ -436,7 +467,10 @@ function allocate(used: boolean[], components: number): number {
436
467
  return -1;
437
468
  }
438
469
 
470
+ const isTexture = (type: ReflType | undefined): boolean => type?.kind === "struct" && type.name === "SkTexture";
471
+
439
472
  function fieldComponents(type: ReflType | undefined, where: string): number {
473
+ if (isTexture(type)) return 2;
440
474
  if (type?.kind === "scalar") {
441
475
  if (type.scalarType !== "float32") {
442
476
  throw new SchemaError(
@@ -455,14 +489,25 @@ function fieldComponents(type: ReflType | undefined, where: string): number {
455
489
  return n;
456
490
  }
457
491
  throw new SchemaError(
458
- `${where}: unsupported parameter type \`${type?.kind ?? "?"}\` — a parameter must be ` +
459
- "`float`, `float2`, `float3` or `float4` (matrices, arrays, textures and " +
460
- "samplers cannot ride in the 8-float parameter block)",
492
+ `${where}: unsupported parameter type \`${type?.name ?? type?.kind ?? "?"}\` — a parameter must be ` +
493
+ "`float`, `float2`, `float3`, `float4` or `SkTexture` (an image: matrices, arrays, raw " +
494
+ "textures and samplers cannot ride in the 32-float parameter block)",
461
495
  );
462
496
  }
463
497
 
464
498
  const COMPONENT_SUFFIX = ["x", "y", "z", "w"];
465
499
 
500
+ const HOST_FILLED_REFUSES = [
501
+ "SkDefault",
502
+ "SkRange",
503
+ "SkLength",
504
+ "SkColor",
505
+ "SkOptional",
506
+ "SkAnimated",
507
+ "SkBleed",
508
+ "SkBackdropRadius",
509
+ ];
510
+
466
511
  function buildSchema(
467
512
  reflection: Reflection | null,
468
513
  structName: string,
@@ -502,6 +547,11 @@ function buildSchema(
502
547
  let backdropRadiusParam: number | null = null;
503
548
  let bleedParam: number | null = null;
504
549
  let animatedParam: number | null = null;
550
+ let pointerParam: number | null = null;
551
+ let pointerTimesParam: number | null = null;
552
+ const clockSlots: number[] = [];
553
+ let clockWindow: number | null = null;
554
+ let textures = 0;
505
555
  let backdrop = attr(structAttrs, "SkBackdrop") !== undefined;
506
556
  // Taken float slots: `[SkSlot]` runs are reserved first, then fields pack in declaration order.
507
557
  const used: boolean[] = new Array(PARAM_SLOTS).fill(false);
@@ -541,7 +591,7 @@ function buildSchema(
541
591
  if (isColor && isLength) {
542
592
  throw new SchemaError(`${where}: [SkColor] and [SkLength] are mutually exclusive`);
543
593
  }
544
- if (optional && components > 1 && !isColor) {
594
+ if (optional && components > 1 && !isColor && !isTexture(field.type)) {
545
595
  // A bare vector expands to one CSS argument per component and optional parameters are
546
596
  // skipped one argument at a time, so half a `float2` could be filled in: refuse.
547
597
  throw new SchemaError(
@@ -557,13 +607,82 @@ function buildSchema(
557
607
  if (slot < 0) {
558
608
  throw new SchemaError(
559
609
  `${where}: no room left in the parameter block. An effect quad carries ` +
560
- `${PARAM_SLOTS} floats (EffectQuad::params0 + params1) and a <color> costs 4; ` +
610
+ `${PARAM_SLOTS} floats (EffectQuad::params) and a <color> costs 4; ` +
561
611
  "drop or merge a parameter.",
562
612
  );
563
613
  }
564
614
  for (let c = 0; c < components; c += 1) used[slot + c] = true;
565
615
  }
566
616
 
617
+ const linear = attr(attrs, "SkLinear") !== undefined;
618
+ if (isTexture(field.type)) {
619
+ const refused = [
620
+ ...HOST_FILLED_REFUSES.filter((a) => a !== "SkDefault" && a !== "SkOptional"),
621
+ "SkAnimatedFor",
622
+ "SkPointer",
623
+ "SkPointerTimes",
624
+ ].find((a) => attr(attrs, a) !== undefined);
625
+ if (refused) throw new SchemaError(`${where}: [${refused}] cannot go on an SkTexture`);
626
+ if (textures === TEXTURE_SLOTS) {
627
+ throw new SchemaError(`${where}: an effect samples at most ${TEXTURE_SLOTS} SkTexture images`);
628
+ }
629
+ const fallback = defaultAttr
630
+ ? parseDefault(String(defaultAttr.arguments?.[0] ?? ""), 4, where)
631
+ : ([0, 0, 0, 0] as [number, number, number, number]);
632
+ const [r, g, b, a] = fallback;
633
+ fields.push({
634
+ name: fieldName,
635
+ components,
636
+ slot,
637
+ texture: { index: textures, fallback: linear ? fallback : [r * a, g * a, b * a, a] },
638
+ });
639
+ params.push({
640
+ name: fieldName,
641
+ kind: "texture",
642
+ slot,
643
+ required: !optional,
644
+ default: fallback,
645
+ range: null,
646
+ texture: textures,
647
+ linear,
648
+ });
649
+ textures += 1;
650
+ continue;
651
+ }
652
+ if (linear) throw new SchemaError(`${where}: [SkLinear] is only legal on an SkTexture`);
653
+ fields.push({ name: fieldName, components, slot });
654
+
655
+ const animatedFor = attr(attrs, "SkAnimatedFor");
656
+ if (animatedFor) {
657
+ const seconds = Number(animatedFor.arguments?.[0]);
658
+ if (!Number.isFinite(seconds) || seconds <= 0) {
659
+ throw new SchemaError(
660
+ `${where}: [SkAnimatedFor(${animatedFor.arguments?.[0]})] needs a positive number of seconds`,
661
+ );
662
+ }
663
+ clockWindow = Math.max(clockWindow ?? 0, seconds);
664
+ for (let c = 0; c < components; c += 1) clockSlots.push(slot + c);
665
+ }
666
+
667
+ const host = (["SkPointer", "SkPointerTimes"] as const).filter((a) => attr(attrs, a) !== undefined);
668
+ if (host.length > 1) throw new SchemaError(`${where}: [SkPointer] and [SkPointerTimes] are mutually exclusive`);
669
+ if (host[0]) {
670
+ const kind = host[0];
671
+ const wanted = kind === "SkPointer" ? 2 : 3;
672
+ if (components !== wanted) throw new SchemaError(`${where}: [${kind}] is only legal on a float${wanted}`);
673
+ const css = HOST_FILLED_REFUSES.find((a) => attr(attrs, a) !== undefined);
674
+ if (css) {
675
+ throw new SchemaError(
676
+ `${where}: [${css}] cannot go on a [${kind}] parameter — the host fills it, it is not a CSS argument`,
677
+ );
678
+ }
679
+ const current = kind === "SkPointer" ? pointerParam : pointerTimesParam;
680
+ if (current !== null) throw new SchemaError(`${where}: [${kind}] appears more than once in \`${structName}\``);
681
+ if (kind === "SkPointer") pointerParam = slot;
682
+ else pointerTimesParam = slot;
683
+ continue;
684
+ }
685
+
567
686
  const defaults = defaultAttr
568
687
  ? parseDefault(String(defaultAttr.arguments?.[0] ?? ""), components, where)
569
688
  : ([0, 0, 0, 0] as [number, number, number, number]);
@@ -571,7 +690,6 @@ function buildSchema(
571
690
  ? ([Number(rangeAttr.arguments?.[0]), Number(rangeAttr.arguments?.[1])] as [number, number])
572
691
  : null;
573
692
 
574
- const firstParam = params.length;
575
693
  if (isColor) {
576
694
  params.push({ name: fieldName, kind: "color", slot, required: !optional, default: defaults, range: null });
577
695
  } else {
@@ -586,7 +704,6 @@ function buildSchema(
586
704
  });
587
705
  }
588
706
  }
589
- fields.push({ name: fieldName, components, attributes: attrs, firstParam });
590
707
 
591
708
  const marker = (attrName: string, current: number | null, requires: string) => {
592
709
  if (attr(attrs, attrName) === undefined) return current;
@@ -634,10 +751,16 @@ function buildSchema(
634
751
  struct: structName,
635
752
  doc: docAttr ? String(docAttr.arguments?.[0] ?? "") : null,
636
753
  backdrop,
754
+ content: attr(structAttrs, "SkContent") !== undefined,
755
+ history: attr(structAttrs, "SkHistory") !== undefined,
637
756
  params,
638
757
  backdropRadiusParam,
639
758
  bleedParam,
640
759
  animatedParam,
760
+ pointerParam,
761
+ pointerTimesParam,
762
+ clockSlots,
763
+ clockWindow,
641
764
  signature,
642
765
  },
643
766
  fields,
@@ -862,6 +985,8 @@ export function effectFiles(
862
985
  struct: c.effect.struct,
863
986
  doc: c.effect.doc,
864
987
  backdrop: c.effect.backdrop,
988
+ content: c.effect.content,
989
+ history: c.effect.history,
865
990
  ...Object.fromEntries(c.outputs.map((o) => [o.target, `${stemOf(c.file)}.${o.target}`])),
866
991
  source: sourceOf(c.file),
867
992
  sourceSha256: c.sourceSha256,
@@ -870,6 +995,10 @@ export function effectFiles(
870
995
  backdropRadiusParam: c.effect.backdropRadiusParam,
871
996
  bleedParam: c.effect.bleedParam,
872
997
  animatedParam: c.effect.animatedParam,
998
+ pointerParam: c.effect.pointerParam,
999
+ pointerTimesParam: c.effect.pointerTimesParam,
1000
+ clockSlots: c.effect.clockSlots,
1001
+ clockWindow: c.effect.clockWindow,
873
1002
  params: c.effect.params,
874
1003
  bindings: c.reflection,
875
1004
  })),