typegpu 0.12.8 → 0.12.10

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 (119) hide show
  1. package/common/attachAutoResizer.d.ts +33 -0
  2. package/common/attachAutoResizer.js +83 -0
  3. package/common/index.d.ts +1 -0
  4. package/common/index.js +1 -0
  5. package/common/writeSoA.js +4 -3
  6. package/core/commandEncoder/commandEncoder.d.ts +1 -1
  7. package/core/declare/tgpuDeclare.d.ts +10 -7
  8. package/core/declare/tgpuDeclare.js +25 -10
  9. package/core/function/comptime.d.ts +2 -0
  10. package/core/function/comptime.js +29 -24
  11. package/core/function/createCallableSchema.js +32 -37
  12. package/core/function/dualImpl.js +45 -44
  13. package/core/function/tgpuComputeFn.d.ts +0 -3
  14. package/core/function/tgpuFn.d.ts +1 -0
  15. package/core/function/tgpuFn.js +14 -4
  16. package/core/function/tgpuFragmentFn.d.ts +1 -1
  17. package/core/function/tgpuVertexFn.d.ts +1 -1
  18. package/core/pipeline/computePipeline.js +5 -2
  19. package/core/pipeline/drawState.js +1 -1
  20. package/core/pipeline/renderPipeline.js +5 -2
  21. package/core/pipeline/webgpuLimitations.js +11 -3
  22. package/core/resolve/externals.js +4 -0
  23. package/core/resolve/namespace.d.ts +4 -0
  24. package/core/resolve/namespace.js +1 -2
  25. package/core/resolve/stitch.js +13 -8
  26. package/core/root/init.d.ts +3 -3
  27. package/core/root/init.js +12 -13
  28. package/core/root/rootTypes.d.ts +44 -37
  29. package/core/slot/accessor.js +4 -4
  30. package/core/texture/texture.d.ts +3 -3
  31. package/core/texture/texture.js +60 -44
  32. package/core/texture/textureFormats.d.ts +4 -3
  33. package/core/texture/textureFormats.js +17 -6
  34. package/core/texture/textureWrite.d.ts +9 -0
  35. package/core/texture/textureWrite.js +21 -0
  36. package/core/unroll/tgpuUnroll.js +2 -4
  37. package/core/vertexLayout/vertexAttribute.d.ts +1 -4
  38. package/core/vertexLayout/vertexLayout.js +11 -30
  39. package/data/alignmentOf.d.ts +1 -1
  40. package/data/alignmentOf.js +5 -12
  41. package/data/array.js +34 -2
  42. package/data/attributes.d.ts +16 -3
  43. package/data/attributes.js +11 -2
  44. package/data/compiledIO.js +3 -3
  45. package/data/dataIO.js +77 -111
  46. package/data/dataTypes.d.ts +0 -2
  47. package/data/dataTypes.js +3 -3
  48. package/data/disarray.js +2 -0
  49. package/data/index.d.ts +4 -3
  50. package/data/index.js +3 -2
  51. package/data/offsetUtils.js +23 -13
  52. package/data/offsets.js +1 -2
  53. package/data/partialIO.js +2 -3
  54. package/data/ref.js +35 -37
  55. package/data/schemaCallWrapper.js +2 -2
  56. package/data/schemaMemoryLayout.d.ts +6 -1
  57. package/data/schemaMemoryLayout.js +23 -14
  58. package/data/schemasEqual.d.ts +37 -0
  59. package/data/schemasEqual.js +122 -0
  60. package/data/sizeOf.d.ts +1 -1
  61. package/data/sizeOf.js +2 -3
  62. package/data/snippet.d.ts +1 -1
  63. package/data/struct.js +27 -1
  64. package/data/unstruct.js +2 -0
  65. package/data/vectorImpl.js +45 -21
  66. package/data/vertexFormatData.js +7 -9
  67. package/data/wgslTypes.d.ts +28 -13
  68. package/data/wgslTypes.js +33 -39
  69. package/extension.d.ts +0 -4
  70. package/package.json +2 -2
  71. package/resolutionCtx.d.ts +1 -1
  72. package/resolutionCtx.js +26 -7
  73. package/serial/restore.d.ts +4 -3
  74. package/serial/restore.js +2 -1
  75. package/shared/meta.js +1 -1
  76. package/shared/symbols.d.ts +5 -0
  77. package/shared/symbols.js +6 -1
  78. package/shared/utilityTypes.d.ts +0 -6
  79. package/std/bitcast.d.ts +1 -1
  80. package/std/comptime.d.ts +67 -0
  81. package/std/comptime.js +88 -0
  82. package/std/environment.js +2 -4
  83. package/std/index.d.ts +2 -0
  84. package/std/index.js +2 -0
  85. package/std/matrix.js +5 -5
  86. package/std/numeric.d.ts +2 -1
  87. package/std/numeric.js +47 -10
  88. package/std/typeOf.d.ts +31 -0
  89. package/std/typeOf.js +54 -0
  90. package/tgpu.d.ts +1 -1
  91. package/tgpu.js +1 -1
  92. package/tgpuBindGroupLayout.d.ts +6 -3
  93. package/tgpuBindGroupLayout.js +15 -2
  94. package/tgpuLogger.d.ts +3 -0
  95. package/tgpuLogger.js +3 -0
  96. package/tgpuUnstableNamespace.d.ts +1 -0
  97. package/tgpuUnstableNamespace.js +5 -0
  98. package/tgsl/accessIndex.js +10 -7
  99. package/tgsl/accessProp.js +5 -57
  100. package/tgsl/consoleLog/serializers.d.ts +1 -8
  101. package/tgsl/consoleLog/serializers.js +53 -37
  102. package/tgsl/consoleLog/types.d.ts +1 -1
  103. package/tgsl/conversion.js +21 -0
  104. package/tgsl/forOfUtils.js +1 -1
  105. package/tgsl/generationHelpers.d.ts +0 -1
  106. package/tgsl/generationHelpers.js +1 -4
  107. package/tgsl/shaderGenerator.d.ts +5 -0
  108. package/tgsl/shellless.js +3 -20
  109. package/tgsl/swizzle.d.ts +24 -0
  110. package/tgsl/swizzle.js +112 -0
  111. package/tgsl/wgslGenerator.d.ts +13 -3
  112. package/tgsl/wgslGenerator.js +150 -152
  113. package/types.d.ts +10 -10
  114. package/types.js +1 -1
  115. package/unwrapper.d.ts +2 -2
  116. package/data/deepEqual.d.ts +0 -21
  117. package/data/deepEqual.js +0 -93
  118. package/taskQueue.d.ts +0 -6
  119. package/taskQueue.js +0 -30
@@ -102,7 +102,28 @@ function getImplicitConversionRank(src, dest) {
102
102
  }
103
103
  return INFINITE_RANK;
104
104
  }
105
+ /**
106
+ * Conversion ranks only depend on immutable facts about the two schemas (their type tags,
107
+ * identity, inner types and pointer flags), so they're computed once per pair. Callers must
108
+ * treat the returned info as read-only.
109
+ */
110
+ const autoRankCache = new WeakMap();
111
+ const implicitRankCache = new WeakMap();
105
112
  function getConversionRank(src, dest, allowImplicit) {
113
+ const cache = allowImplicit ? implicitRankCache : autoRankCache;
114
+ let byDest = cache.get(src);
115
+ if (!byDest) {
116
+ byDest = new WeakMap();
117
+ cache.set(src, byDest);
118
+ }
119
+ let rank = byDest.get(dest);
120
+ if (!rank) {
121
+ rank = computeConversionRank(src, dest, allowImplicit);
122
+ byDest.set(dest, rank);
123
+ }
124
+ return rank;
125
+ }
126
+ function computeConversionRank(src, dest, allowImplicit) {
106
127
  const autoRank = getAutoConversionRank(src, dest);
107
128
  if (autoRank.rank < Number.POSITIVE_INFINITY) {
108
129
  return autoRank;
@@ -72,7 +72,7 @@ You can wrap iterable with \`tgpu.unroll(...)\`. If iterable is known at comptim
72
72
  ...defaults,
73
73
  end: dataType.elementCount > 0
74
74
  ? snip(dataType.elementCount, u32, 'constant', false)
75
- : arrayLength[$gpuCallable].call(ctx, [iterableSnippet]),
75
+ : arrayLength[$gpuCallable](ctx, [iterableSnippet]),
76
76
  };
77
77
  }
78
78
  if (wgsl.isVec(dataType)) {
@@ -5,7 +5,6 @@ import { $comptimeValueOf, $internal, $resolve } from '../shared/symbols.ts';
5
5
  export declare function numericLiteralToSnippet(value: number): Snippet;
6
6
  export declare function concretize<T extends BaseData>(type: T): T | F32 | I32;
7
7
  export declare function concretizeSnippet(snippet: Snippet): Snippet;
8
- export declare function concretizeSnippets(args: Snippet[]): Snippet[];
9
8
  export declare function coerceToSnippet(value: unknown): Snippet;
10
9
  /**
11
10
  * Intermediate representation for WGSL array expressions.
@@ -1,7 +1,7 @@
1
1
  import { UnknownData } from "../data/dataTypes.js";
2
2
  import { abstractFloat, abstractInt, bool, f32, i32 } from "../data/numeric.js";
3
3
  import { isRef } from "../data/ref.js";
4
- import { isStoredInMemory, isSnippet, snip, withDataType } from "../data/snippet.js";
4
+ import { isSnippet, isStoredInMemory, snip, withDataType } from "../data/snippet.js";
5
5
  import { isMatInstance, isNaturallyEphemeral, isVecInstance, WORKAROUND_getSchema, } from "../data/wgslTypes.js";
6
6
  import { asComptime, getOwnSnippet, } from "../types.js";
7
7
  import { WgslTypeError } from "../errors.js";
@@ -34,9 +34,6 @@ export function concretize(type) {
34
34
  export function concretizeSnippet(snippet) {
35
35
  return withDataType(concretize(snippet.dataType), snippet);
36
36
  }
37
- export function concretizeSnippets(args) {
38
- return args.map(concretizeSnippet);
39
- }
40
37
  export function coerceToSnippet(value) {
41
38
  if (isSnippet(value)) {
42
39
  // Already a snippet
@@ -3,6 +3,7 @@ import type { BaseData } from '../data/wgslTypes.ts';
3
3
  import type { ResolvedSnippet, Snippet } from '../data/snippet.ts';
4
4
  import type { VariableScope } from '../core/variable/tgpuVariable.ts';
5
5
  import type { BindableBufferUsage, FunctionArgument, ResolutionCtx, ShaderStage } from '../types.ts';
6
+ import type { AnyFn } from '../core/function/fnTypes.ts';
6
7
  export interface FunctionDefinitionOptions {
7
8
  readonly functionType: 'normal' | ShaderStage;
8
9
  readonly name: string;
@@ -78,6 +79,10 @@ export interface ShaderGenerator {
78
79
  functionDefinition(options: FunctionDefinitionOptions): string;
79
80
  typeInstantiation(schema: BaseData, args: readonly Snippet[]): ResolvedSnippet;
80
81
  numericLiteral(value: number, schema: BaseData): ResolvedSnippet;
82
+ /**
83
+ * Can be used to call arbitrary helper functions during shader generation, e.g. for polifylls.
84
+ */
85
+ call(callee: AnyFn, args: readonly Snippet[]): Snippet;
81
86
  emitTypeAnnotation(schema: BaseData): string;
82
87
  emitCall(name: string, templateParams: readonly Snippet[], args: readonly Snippet[]): string;
83
88
  emitBinaryOp(lhs: Snippet, op: BinaryOperator, rhs: Snippet): string;
package/tgsl/shellless.js CHANGED
@@ -1,27 +1,10 @@
1
1
  import { createShelllessImpl } from "../core/function/shelllessImpl.js";
2
2
  import { UnknownData } from "../data/dataTypes.js";
3
- import { isPtr, isWgslArray, isWgslStruct } from "../data/wgslTypes.js";
3
+ import { schemasEqualNominally } from "../data/schemasEqual.js";
4
+ import { isPtr } from "../data/wgslTypes.js";
4
5
  import { getFunctionMetadata, getName } from "../shared/meta.js";
5
6
  import { concretize } from "./generationHelpers.js";
6
7
  import { stringifySnippet } from "./stringifySnippet.js";
7
- function shallowEqualSchemas(a, b) {
8
- if (a.type !== b.type)
9
- return false;
10
- if (isPtr(a) && isPtr(b)) {
11
- return (a.access === b.access &&
12
- a.addressSpace === b.addressSpace &&
13
- a.implicit === b.implicit &&
14
- shallowEqualSchemas(a.inner, b.inner));
15
- }
16
- if (isWgslArray(a) && isWgslArray(b)) {
17
- return a.elementCount === b.elementCount && shallowEqualSchemas(a.elementType, b.elementType);
18
- }
19
- if (isWgslStruct(a) && isWgslStruct(b)) {
20
- // Only structs with the same identity are considered equal
21
- return a === b;
22
- }
23
- return true;
24
- }
25
8
  export class ShelllessRepository {
26
9
  cache = new Map();
27
10
  get(fn, argSnippets) {
@@ -58,7 +41,7 @@ export class ShelllessRepository {
58
41
  let cache = this.cache.get(fn);
59
42
  if (cache) {
60
43
  const variant = cache.find((v) => v.argTypes.length === argTypes.length &&
61
- v.argTypes.every((t, i) => shallowEqualSchemas(t, argTypes[i])));
44
+ v.argTypes.every((t, i) => schemasEqualNominally(t, argTypes[i])));
62
45
  if (variant) {
63
46
  return variant;
64
47
  }
@@ -0,0 +1,24 @@
1
+ import type { Origin, ResolvedSnippet, Snippet } from '../data/snippet.ts';
2
+ import { type BaseData } from '../data/wgslTypes.ts';
3
+ import { $comptimeValueOf, $internal, $resolve } from '../shared/symbols.ts';
4
+ import { type ComptimeValue, type ResolutionCtx, type SelfResolvable, type WithComptimeValue } from '../types.ts';
5
+ /**
6
+ * Intermediate representation for vector swizzle access.
7
+ * Vector swizzles create a new vector unless they are assigned to.
8
+ */
9
+ export declare class Swizzle implements SelfResolvable, WithComptimeValue {
10
+ readonly [$internal] = true;
11
+ readonly base: Snippet;
12
+ readonly components: string;
13
+ readonly dataType: BaseData;
14
+ /**
15
+ * This origin only applies when swizzle is NOT assigned to.
16
+ * When assigned to, {@link base}'s origin should be checked instead.
17
+ */
18
+ readonly origin: Origin;
19
+ constructor(base: Snippet, components: string | number);
20
+ toString(): string;
21
+ [$comptimeValueOf](): ComptimeValue;
22
+ [$resolve](ctx: ResolutionCtx): ResolvedSnippet;
23
+ }
24
+ export declare function isValidSwizzle(prop: string): boolean;
@@ -0,0 +1,112 @@
1
+ import { stitch } from "../core/resolve/stitch.js";
2
+ import { bool, f16, f32, i32, u32 } from "../data/numeric.js";
3
+ import { snip } from "../data/snippet.js";
4
+ import { vec2b, vec2f, vec2h, vec2i, vec2u, vec3b, vec3f, vec3h, vec3i, vec3u, vec4b, vec4f, vec4h, vec4i, vec4u, } from "../data/vector.js";
5
+ import { isVec } from "../data/wgslTypes.js";
6
+ import { invariant } from "../errors.js";
7
+ import { $comptimeValueOf, $internal, $resolve } from "../shared/symbols.js";
8
+ import { asComptime, } from "../types.js";
9
+ /**
10
+ * Intermediate representation for vector swizzle access.
11
+ * Vector swizzles create a new vector unless they are assigned to.
12
+ */
13
+ export class Swizzle {
14
+ [$internal] = true;
15
+ base;
16
+ components;
17
+ dataType;
18
+ /**
19
+ * This origin only applies when swizzle is NOT assigned to.
20
+ * When assigned to, {@link base}'s origin should be checked instead.
21
+ */
22
+ origin;
23
+ constructor(base, components) {
24
+ this.base = base;
25
+ this.components = sanitizeComponents(components);
26
+ this.dataType = getDataType(base, this.components.length);
27
+ this.origin =
28
+ this.components.length === 1 // special case, as `vec.x` can have its address taken, unlike `vec.xy`
29
+ ? base.origin
30
+ : base.origin === 'constant' || base.origin === 'constant-immutable-def'
31
+ ? 'constant'
32
+ : 'runtime';
33
+ }
34
+ toString() {
35
+ return 'swizzle';
36
+ }
37
+ [$comptimeValueOf]() {
38
+ const baseComptime = asComptime(this.base);
39
+ if (baseComptime) {
40
+ return {
41
+ comptime: true,
42
+ value: baseComptime.value[this.components],
43
+ };
44
+ }
45
+ return { comptime: false };
46
+ }
47
+ [$resolve](ctx) {
48
+ const comptime = this[$comptimeValueOf]();
49
+ if (comptime.comptime) {
50
+ return ctx.resolveSnippet(snip(comptime.value, this.dataType, this.origin, this.base.possibleSideEffects));
51
+ }
52
+ return ctx.resolveSnippet(snip(stitch `${this.base}.${this.components}`, this.dataType, this.origin, this.base.possibleSideEffects));
53
+ }
54
+ }
55
+ function sanitizeComponents(components) {
56
+ if (typeof components === 'string') {
57
+ if (!isValidSwizzle(components)) {
58
+ throw new Error(`The swizzle '${components}' is invalid.`);
59
+ }
60
+ return components;
61
+ }
62
+ else {
63
+ const component = 'xyzw'[components];
64
+ if (!component) {
65
+ throw new Error(`Cannot index vector with '${components}'.`);
66
+ }
67
+ return component;
68
+ }
69
+ }
70
+ const swizzleLenToType = {
71
+ f: {
72
+ 1: f32,
73
+ 2: vec2f,
74
+ 3: vec3f,
75
+ 4: vec4f,
76
+ },
77
+ h: {
78
+ 1: f16,
79
+ 2: vec2h,
80
+ 3: vec3h,
81
+ 4: vec4h,
82
+ },
83
+ i: {
84
+ 1: i32,
85
+ 2: vec2i,
86
+ 3: vec3i,
87
+ 4: vec4i,
88
+ },
89
+ u: {
90
+ 1: u32,
91
+ 2: vec2u,
92
+ 3: vec3u,
93
+ 4: vec4u,
94
+ },
95
+ b: {
96
+ 1: bool,
97
+ 2: vec2b,
98
+ 3: vec3b,
99
+ 4: vec4b,
100
+ },
101
+ };
102
+ function getDataType(base, length) {
103
+ const baseDataType = base.dataType;
104
+ invariant(isVec(baseDataType));
105
+ const swizzleTypeChar = baseDataType.type.includes('bool')
106
+ ? 'b'
107
+ : baseDataType.type[4];
108
+ return swizzleLenToType[swizzleTypeChar][length];
109
+ }
110
+ export function isValidSwizzle(prop) {
111
+ return prop.length >= 1 && prop.length <= 4 && /^[xyzw]+$|^[rgba]+$/.test(prop);
112
+ }
@@ -22,10 +22,19 @@ export declare class WgslGenerator implements ShaderGenerator {
22
22
  protected _emitSwitchStatement(discriminantExpr: Snippet, groupedCaseExprs: [tests: Snippet[], consequent: ResolvedStatement[]][]): string;
23
23
  protected _callShellless(callee: AnyFn, args: readonly Snippet[]): ResolvedSnippet | undefined;
24
24
  /**
25
- * A wrapper for `generateExpression` that updates `ctx.expectedType`
26
- * and tries to convert the result when it does not match the expected type.
25
+ * A wrapper for `_expression` that updates `ctx.expectedType` while generating.
26
+ * The result is NOT converted to the expected type.
27
27
  */
28
- protected _typedExpression(expression: tinyest.Expression, expectedType: wgsl.BaseData | wgsl.BaseData[]): Snippet;
28
+ protected _expressionWithTypeSuggestion(expression: tinyest.Expression, expectedType: wgsl.BaseData | wgsl.BaseData[]): Snippet;
29
+ /**
30
+ * Like `_expressionWithTypeSuggestion`, but also tries to convert the result
31
+ * when it does not match the expected type.
32
+ */
33
+ protected _expressionWithTypeConversion(expression: tinyest.Expression, expectedType: wgsl.BaseData | wgsl.BaseData[]): Snippet;
34
+ /**
35
+ * @param calleeSnippet The snippet the callee was produced from, used for more descriptive errors.
36
+ */
37
+ call(_callee: AnyFn, args: readonly Snippet[], calleeSnippet?: Snippet): Snippet;
29
38
  protected _expression(expression: tinyest.Expression): Snippet;
30
39
  private _expressionImpl;
31
40
  declareGlobalConst(options: ConstantDefinitionOptions): ResolvedSnippet;
@@ -70,4 +79,5 @@ export declare class WgslGenerator implements ShaderGenerator {
70
79
  * tryMarkModified('this.buffer.$') // `this` is not marked, since there is no placeholder for it
71
80
  */
72
81
  private tryMarkModified;
82
+ private validateSnippetMutation;
73
83
  }