Skip to content

Commit dbcd394

Browse files
committed
Merge branch 'main' into feat/ref-value
2 parents 4ccdd96 + 4757576 commit dbcd394

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1059
-1258
lines changed

apps/typegpu-docs/src/examples/rendering/jelly-slider/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ const renderBackground = (
618618

619619
let highlights = d.f32();
620620

621-
const highlightWidth = 1;
621+
const highlightWidth = d.f32(1);
622622
const highlightHeight = 0.2;
623623
let offsetX = d.f32();
624624
let offsetZ = d.f32(0.05);

packages/tgpu-gen/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"wgsl_reflect": "git://github.com/mhawryluk/wgsl_reflect.git#85994fdc8d8a3abbb4f79baf3891e54eed0c1c63"
1919
},
2020
"peerDependencies": {
21-
"typegpu": "workspace:^0.8.0"
21+
"typegpu": "workspace:^0.8.1"
2222
},
2323
"files": ["README.md", "LICENSE", "package.json", "**/*.mjs"],
2424
"devDependencies": {

packages/typegpu/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "typegpu",
33
"private": true,
4-
"version": "0.8.0",
4+
"version": "0.8.1",
55
"description": "A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.",
66
"license": "MIT",
77
"type": "module",

packages/typegpu/src/core/pipeline/renderPipeline.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { isBuiltin } from '../../data/attributes.ts';
88
import { type Disarray, getCustomLocation } from '../../data/dataTypes.ts';
99
import { sizeOf } from '../../data/sizeOf.ts';
1010
import { type ResolvedSnippet, snip } from '../../data/snippet.ts';
11+
import type { WgslTexture } from '../../data/texture.ts';
1112
import {
1213
type AnyWgslData,
1314
isWgslData,
@@ -46,7 +47,12 @@ import type { TgpuVertexFn } from '../function/tgpuVertexFn.ts';
4647
import { namespace } from '../resolve/namespace.ts';
4748
import type { ExperimentalTgpuRoot } from '../root/rootTypes.ts';
4849
import type { TgpuSlot } from '../slot/slotTypes.ts';
49-
import { isTexture, type TgpuTexture } from '../texture/texture.ts';
50+
import {
51+
isTexture,
52+
isTextureView,
53+
type TgpuTexture,
54+
type TgpuTextureView,
55+
} from '../texture/texture.ts';
5056
import type { RenderFlag } from '../texture/usageExtension.ts';
5157
import { connectAttributesToShader } from '../vertexLayout/connectAttributesToShader.ts';
5258
import {
@@ -155,7 +161,10 @@ export interface ColorAttachment {
155161
* A {@link GPUTextureView} describing the texture subresource that will be output to for this
156162
* color attachment.
157163
*/
158-
view: (TgpuTexture & RenderFlag) | GPUTextureView;
164+
view:
165+
| (TgpuTexture & RenderFlag)
166+
| GPUTextureView
167+
| TgpuTextureView<WgslTexture>;
159168
/**
160169
* Indicates the depth slice index of {@link GPUTextureViewDimension#"3d"} {@link GPURenderPassColorAttachment#view}
161170
* that will be output to for this color attachment.
@@ -506,6 +515,13 @@ class TgpuRenderPipelineImpl implements TgpuRenderPipeline {
506515
};
507516
}
508517

518+
if (isTextureView(attachment.view)) {
519+
return {
520+
...attachment,
521+
view: branch.unwrap(attachment.view),
522+
};
523+
}
524+
509525
return attachment;
510526
}) as GPURenderPassColorAttachment[]
511527
: [null];

packages/typegpu/src/core/resolve/stitch.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,12 @@ type ValueOrArray<T> = T | T[];
1111
export function stitch(
1212
strings: TemplateStringsArray,
1313
...snippets: ValueOrArray<Snippet | string | number | undefined>[]
14-
) {
15-
return internalStitch(strings, snippets, false);
16-
}
17-
18-
/**
19-
* "The reverse of snipping"
20-
* Injects resolved snippets into a template string, ensuring
21-
* the generated code represents it's type exactly.
22-
*/
23-
export function stitchWithExactTypes(
24-
strings: TemplateStringsArray,
25-
...snippets: ValueOrArray<Snippet | string | number | undefined>[]
26-
) {
27-
return internalStitch(strings, snippets, true);
28-
}
29-
30-
function internalStitch(
31-
strings: TemplateStringsArray,
32-
snippets: ValueOrArray<Snippet | string | number | undefined>[],
33-
exact: boolean,
3414
) {
3515
const ctx = getResolutionCtx() as ResolutionCtx;
3616

3717
function resolveSnippet(maybeSnippet: Snippet | string | number) {
3818
return isSnippet(maybeSnippet)
39-
? ctx.resolve(maybeSnippet.value, maybeSnippet.dataType, exact).value
19+
? ctx.resolve(maybeSnippet.value, maybeSnippet.dataType).value
4020
: maybeSnippet;
4121
}
4222

packages/typegpu/src/data/vector.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ function makeVecSchema<TValue, S extends number | boolean>(
317317
returnType: schema as AnyData,
318318
}),
319319
normalImpl: cpuConstruct,
320+
ignoreImplicitCastWarning: true,
320321
codegenImpl: (...args) => {
321322
if (args.length === 1 && args[0]?.dataType === schema) {
322323
// Already typed as the schema

packages/typegpu/src/resolutionCtx.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,6 @@ export class ResolutionCtxImpl implements ResolutionCtx {
703703
resolve(
704704
item: unknown,
705705
schema?: AnyData | UnknownData | undefined,
706-
exact = false,
707706
): ResolvedSnippet {
708707
if (isTgpuFn(item) || hasTinyestMetadata(item)) {
709708
if (
@@ -720,7 +719,7 @@ export class ResolutionCtxImpl implements ResolutionCtx {
720719
if (isProviding(item)) {
721720
return this.withSlots(
722721
item[$providing].pairs,
723-
() => this.resolve(item[$providing].inner, schema, exact),
722+
() => this.resolve(item[$providing].inner, schema),
724723
);
725724
}
726725

@@ -745,41 +744,34 @@ export class ResolutionCtxImpl implements ResolutionCtx {
745744

746745
// This is a value that comes from the outside, maybe we can coerce it
747746
if (typeof item === 'number') {
748-
const reinterpretedType = exact
749-
? schema
750-
: numericLiteralToSnippet(item).dataType;
751747
const realSchema = schema ?? numericLiteralToSnippet(item).dataType;
752-
invariant(
753-
reinterpretedType,
754-
'Schema has to be defined for resolving numbers',
755-
);
756748
invariant(
757749
realSchema.type !== 'unknown',
758750
'Schema has to be known for resolving numbers',
759751
);
760752

761-
if (reinterpretedType.type === 'abstractInt') {
753+
if (realSchema.type === 'abstractInt') {
762754
return snip(`${item}`, realSchema, /* ref */ 'constant');
763755
}
764-
if (reinterpretedType.type === 'u32') {
756+
if (realSchema.type === 'u32') {
765757
return snip(`${item}u`, realSchema, /* ref */ 'constant');
766758
}
767-
if (reinterpretedType.type === 'i32') {
759+
if (realSchema.type === 'i32') {
768760
return snip(`${item}i`, realSchema, /* ref */ 'constant');
769761
}
770762

771763
const exp = item.toExponential();
772764
const decimal =
773-
reinterpretedType.type === 'abstractFloat' && Number.isInteger(item)
765+
realSchema.type === 'abstractFloat' && Number.isInteger(item)
774766
? `${item}.`
775767
: `${item}`;
776768

777769
// Just picking the shorter one
778770
const base = exp.length < decimal.length ? exp : decimal;
779-
if (reinterpretedType.type === 'f32') {
771+
if (realSchema.type === 'f32') {
780772
return snip(`${base}f`, realSchema, /* ref */ 'constant');
781773
}
782-
if (reinterpretedType.type === 'f16') {
774+
if (realSchema.type === 'f16') {
783775
return snip(`${base}h`, realSchema, /* ref */ 'constant');
784776
}
785777
return snip(base, realSchema, /* ref */ 'constant');

packages/typegpu/src/std/boolean.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ export const select = dualImpl({
340340
name: 'select',
341341
signature: (...args) => {
342342
const [f, t, cond] = toStorables(args);
343-
const [uf, ut] = unify<[AnyData, AnyData]>([f, t]) ?? [f, t];
343+
const [uf, ut] = unify([f, t]) ?? [f, t] as const;
344344
return ({ argTypes: [uf, ut, cond], returnType: uf });
345345
},
346346
normalImpl: cpuSelect,

packages/typegpu/src/std/operators.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dualImpl } from '../core/function/dualImpl.ts';
2-
import { stitch, stitchWithExactTypes } from '../core/resolve/stitch.ts';
2+
import { stitch } from '../core/resolve/stitch.ts';
33
import { toStorable, toStorables } from '../data/dataTypes.ts';
44
import { abstractFloat, f16, f32 } from '../data/numeric.ts';
55
import { vecTypeToConstructor } from '../data/vector.ts';
@@ -196,7 +196,7 @@ export const div = dualImpl({
196196
});
197197
},
198198
normalImpl: cpuDiv,
199-
codegenImpl: (lhs, rhs) => stitchWithExactTypes`(${lhs} / ${rhs})`,
199+
codegenImpl: (lhs, rhs) => stitch`(${lhs} / ${rhs})`,
200200
ignoreImplicitCastWarning: true,
201201
});
202202

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as tinyest from 'tinyest';
2-
import { stitch, stitchWithExactTypes } from '../core/resolve/stitch.ts';
2+
import { stitch } from '../core/resolve/stitch.ts';
33
import { arrayOf } from '../data/array.ts';
44
import {
55
type AnyData,
@@ -43,6 +43,7 @@ import type { ShaderGenerator } from './shaderGenerator.ts';
4343
import type { DualFn } from '../data/dualFn.ts';
4444
import { INTERNAL_createPtr, ptrFn } from '../data/ptr.ts';
4545
import { RefOnGPU, RefOperator } from '../data/ref.ts';
46+
import { constant } from '../core/constant/tgpuConstant.ts';
4647

4748
const { NodeTypeCatalog: NODE } = tinyest;
4849

@@ -312,7 +313,7 @@ ${this.ctx.pre}}`;
312313
// Post-Update Expression
313314
const [_, op, arg] = expression;
314315
const argExpr = this.expression(arg);
315-
const argStr = this.ctx.resolve(argExpr.value).value;
316+
const argStr = this.ctx.resolve(argExpr.value, argExpr.dataType).value;
316317

317318
// Result of an operation, so not a reference to anything
318319
return snip(`${argStr}${op}`, argExpr.dataType, /* ref */ 'runtime');
@@ -329,7 +330,7 @@ ${this.ctx.pre}}`;
329330
return codegen(argExpr);
330331
}
331332

332-
const argStr = this.ctx.resolve(argExpr.value).value;
333+
const argStr = this.ctx.resolve(argExpr.value, argExpr.dataType).value;
333334

334335
const type = operatorToType(argExpr.dataType, op);
335336
// Result of an operation, so not a reference to anything
@@ -374,7 +375,7 @@ ${this.ctx.pre}}`;
374375
this.ctx.resolve(property.value, property.dataType).value;
375376

376377
throw new Error(
377-
`Cannot index value ${targetStr} with index ${propertyStr}`,
378+
`Unable to index value ${targetStr} of unknown type with index ${propertyStr}. If the value is an array, to address this, consider one of the following approaches: (1) declare the array using 'tgpu.const', (2) store the array in a buffer, or (3) define the array within the GPU function scope.`,
378379
);
379380
}
380381

@@ -431,6 +432,12 @@ ${this.ctx.pre}}`;
431432
);
432433
}
433434

435+
if (callee.value === constant) {
436+
throw new Error(
437+
'Constants cannot be defined within TypeGPU function scope. To address this, move the constant definition outside the function scope.',
438+
);
439+
}
440+
434441
if (callee.value instanceof InfixDispatch) {
435442
// Infix operator dispatch.
436443
if (!argNodes[0]) {
@@ -780,7 +787,7 @@ ${this.ctx.pre}else ${alternate}`;
780787
rawId,
781788
concretize(refSnippet.dataType as AnyData) as wgsl.StorableData,
782789
);
783-
return stitchWithExactTypes`${this.ctx.pre}var ${varName} = ${
790+
return stitch`${this.ctx.pre}var ${varName} = ${
784791
tryConvertSnippet(
785792
refSnippet,
786793
refSnippet.dataType as wgsl.AnyWgslData,
@@ -845,7 +852,7 @@ ${this.ctx.pre}else ${alternate}`;
845852
concretize(dataType),
846853
eq.origin,
847854
);
848-
return stitchWithExactTypes`${this.ctx.pre}${varType} ${snippet
855+
return stitch`${this.ctx.pre}${varType} ${snippet
849856
.value as string} = ${tryConvertSnippet(eq, dataType, false)};`;
850857
}
851858

0 commit comments

Comments
 (0)