Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ const renderBackground = (

let highlights = d.f32();

const highlightWidth = 1;
const highlightWidth = d.f32(1);
const highlightHeight = 0.2;
let offsetX = d.f32();
let offsetZ = d.f32(0.05);
Expand Down
22 changes: 1 addition & 21 deletions packages/typegpu/src/core/resolve/stitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,12 @@ type ValueOrArray<T> = T | T[];
export function stitch(
strings: TemplateStringsArray,
...snippets: ValueOrArray<Snippet | string | number | undefined>[]
) {
return internalStitch(strings, snippets, false);
}

/**
* "The reverse of snipping"
* Injects resolved snippets into a template string, ensuring
* the generated code represents it's type exactly.
*/
export function stitchWithExactTypes(
strings: TemplateStringsArray,
...snippets: ValueOrArray<Snippet | string | number | undefined>[]
) {
return internalStitch(strings, snippets, true);
}

function internalStitch(
strings: TemplateStringsArray,
snippets: ValueOrArray<Snippet | string | number | undefined>[],
exact: boolean,
) {
const ctx = getResolutionCtx() as ResolutionCtx;

function resolveSnippet(maybeSnippet: Snippet | string | number) {
return isSnippet(maybeSnippet)
? ctx.resolve(maybeSnippet.value, maybeSnippet.dataType, exact).value
? ctx.resolve(maybeSnippet.value, maybeSnippet.dataType).value
: maybeSnippet;
}

Expand Down
1 change: 1 addition & 0 deletions packages/typegpu/src/data/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ function makeVecSchema<TValue, S extends number | boolean>(
returnType: schema as AnyData,
}),
normalImpl: cpuConstruct,
ignoreImplicitCastWarning: true,
codegenImpl: (...args) => stitch`${type}(${args})`,
});

Expand Down
22 changes: 7 additions & 15 deletions packages/typegpu/src/resolutionCtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,6 @@ export class ResolutionCtxImpl implements ResolutionCtx {
resolve(
item: unknown,
schema?: AnyData | UnknownData | undefined,
exact = false,
): ResolvedSnippet {
if (isTgpuFn(item) || hasTinyestMetadata(item)) {
if (
Expand All @@ -719,7 +718,7 @@ export class ResolutionCtxImpl implements ResolutionCtx {
if (isProviding(item)) {
return this.withSlots(
item[$providing].pairs,
() => this.resolve(item[$providing].inner, schema, exact),
() => this.resolve(item[$providing].inner, schema),
);
}

Expand All @@ -743,41 +742,34 @@ export class ResolutionCtxImpl implements ResolutionCtx {

// This is a value that comes from the outside, maybe we can coerce it
if (typeof item === 'number') {
const reinterpretedType = exact
? schema
: numericLiteralToSnippet(item).dataType;
const realSchema = schema ?? numericLiteralToSnippet(item).dataType;
invariant(
reinterpretedType,
'Schema has to be defined for resolving numbers',
);
invariant(
realSchema.type !== 'unknown',
'Schema has to be known for resolving numbers',
);

if (reinterpretedType.type === 'abstractInt') {
if (realSchema.type === 'abstractInt') {
return snip(`${item}`, realSchema);
}
if (reinterpretedType.type === 'u32') {
if (realSchema.type === 'u32') {
return snip(`${item}u`, realSchema);
}
if (reinterpretedType.type === 'i32') {
if (realSchema.type === 'i32') {
return snip(`${item}i`, realSchema);
}

const exp = item.toExponential();
const decimal =
reinterpretedType.type === 'abstractFloat' && Number.isInteger(item)
realSchema.type === 'abstractFloat' && Number.isInteger(item)
? `${item}.`
: `${item}`;

// Just picking the shorter one
const base = exp.length < decimal.length ? exp : decimal;
if (reinterpretedType.type === 'f32') {
if (realSchema.type === 'f32') {
return snip(`${base}f`, realSchema);
}
if (reinterpretedType.type === 'f16') {
if (realSchema.type === 'f16') {
return snip(`${base}h`, realSchema);
}
return snip(base, realSchema);
Expand Down
6 changes: 5 additions & 1 deletion packages/typegpu/src/std/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
type v4b,
} from '../data/wgslTypes.ts';
import { $internal } from '../shared/symbols.ts';
import { unify } from '../tgsl/conversion.ts';
import { sub } from './operators.ts';

function correspondingBooleanVectorSchema(dataType: AnyData) {
Expand Down Expand Up @@ -333,7 +334,10 @@ function cpuSelect<T extends number | boolean | AnyVecInstance>(
*/
export const select = dualImpl({
name: 'select',
signature: (...argTypes) => ({ argTypes, returnType: argTypes[0] }),
signature: (f, t, cond) => {
const [uf, ut] = unify([f, t]) ?? [f, t] as const;
return ({ argTypes: [uf, ut, cond], returnType: uf });
},
normalImpl: cpuSelect,
codegenImpl: (f, t, cond) => stitch`select(${f}, ${t}, ${cond})`,
});
4 changes: 2 additions & 2 deletions packages/typegpu/src/std/operators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dualImpl } from '../core/function/dualImpl.ts';
import { stitch, stitchWithExactTypes } from '../core/resolve/stitch.ts';
import { stitch } from '../core/resolve/stitch.ts';
import { abstractFloat, f16, f32 } from '../data/numeric.ts';
import { vecTypeToConstructor } from '../data/vector.ts';
import { VectorOps } from '../data/vectorOps.ts';
Expand Down Expand Up @@ -192,7 +192,7 @@ export const div = dualImpl({
});
},
normalImpl: cpuDiv,
codegenImpl: (lhs, rhs) => stitchWithExactTypes`(${lhs} / ${rhs})`,
codegenImpl: (lhs, rhs) => stitch`(${lhs} / ${rhs})`,
ignoreImplicitCastWarning: true,
});

Expand Down
8 changes: 4 additions & 4 deletions packages/typegpu/src/tgsl/wgslGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as tinyest from 'tinyest';
import { stitch, stitchWithExactTypes } from '../core/resolve/stitch.ts';
import { stitch } from '../core/resolve/stitch.ts';
import { arrayOf } from '../data/array.ts';
import {
type AnyData,
Expand Down Expand Up @@ -254,7 +254,7 @@ ${this.ctx.pre}}`;
// Post-Update Expression
const [_, op, arg] = expression;
const argExpr = this.expression(arg);
const argStr = this.ctx.resolve(argExpr.value).value;
const argStr = this.ctx.resolve(argExpr.value, argExpr.dataType).value;

return snip(`${argStr}${op}`, argExpr.dataType);
}
Expand All @@ -263,7 +263,7 @@ ${this.ctx.pre}}`;
// Unary Expression
const [_, op, arg] = expression;
const argExpr = this.expression(arg);
const argStr = this.ctx.resolve(argExpr.value).value;
const argStr = this.ctx.resolve(argExpr.value, argExpr.dataType).value;

const type = operatorToType(argExpr.dataType, op);
return snip(`${op}${argStr}`, type);
Expand Down Expand Up @@ -735,7 +735,7 @@ ${this.ctx.pre}else ${alternate}`;
rawId,
concretize(eq.dataType as wgsl.AnyWgslData),
);
return stitchWithExactTypes`${this.ctx.pre}var ${snippet
return stitch`${this.ctx.pre}var ${snippet
.value as string} = ${eq};`;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/typegpu/tests/accessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('tgpu.accessor', () => {
.with(multiplierAccess, 2);

expect(asWgsl(getColor)).toMatchInlineSnapshot(
`"fn getColor() -> vec3f{ return vec3f(1, 0, 0) * 2; }"`,
`"fn getColor() -> vec3f{ return vec3f(1, 0, 0) * 2f; }"`,
);
});

Expand Down
16 changes: 8 additions & 8 deletions packages/typegpu/tests/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('array', () => {

expect(asWgsl(testFn)).toMatchInlineSnapshot(`
"fn testFn() {
var myArray = array<u32, 1>(10);
var myArray = array<u32, 1>(10u);
var myClone = myArray;
return;
}"
Expand All @@ -215,7 +215,7 @@ describe('array', () => {

expect(asWgsl(testFn)).toMatchInlineSnapshot(`
"fn testFn() {
var myArrays = array<array<i32, 1>, 1>(array<i32, 1>(10));
var myArrays = array<array<i32, 1>, 1>(array<i32, 1>(10i));
var myClone = myArrays[0];
return;
}"
Expand Down Expand Up @@ -278,7 +278,7 @@ describe('array', () => {

expect(asWgsl(foo)).toMatchInlineSnapshot(`
"fn foo() {
var result = array<f32, 4>(1, 2, 3, 4);
var result = array<f32, 4>(1f, 2f, 3f, 4f);
}"
`);
});
Expand All @@ -290,7 +290,7 @@ describe('array', () => {

expect(asWgsl(foo)).toMatchInlineSnapshot(`
"fn foo() {
var result = array<f32, 4>(4, 3, 2, 1);
var result = array<f32, 4>(4f, 3f, 2f, 1f);
}"
`);
});
Expand All @@ -304,7 +304,7 @@ describe('array', () => {

expect(asWgsl(foo)).toMatchInlineSnapshot(`
"fn foo() {
var result = array<f32, 4>(4, 3, 2, 1);
var result = array<f32, 4>(4f, 3f, 2f, 1f);
}"
`);
});
Expand All @@ -326,7 +326,7 @@ describe('array', () => {

expect(asWgsl(foo)).toMatchInlineSnapshot(`
"fn foo() {
var result = array<f32, 8>(0, 1, 2, 3, 4, 5, 6, 7);
var result = array<f32, 8>(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f);
}"
`);
});
Expand Down Expand Up @@ -356,7 +356,7 @@ describe('array.length', () => {
var acc = 1f;
for (var i = 0u; (i < arrayLength(&values)); i++) {
values[i] = acc;
acc *= 2;
acc *= 2f;
}
}"
`);
Expand Down Expand Up @@ -385,7 +385,7 @@ describe('array.length', () => {
var acc = 1f;
for (var i = 0; (i < 128); i++) {
values[i] = acc;
acc *= 2;
acc *= 2f;
}
}"
`);
Expand Down
2 changes: 1 addition & 1 deletion packages/typegpu/tests/computePipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ describe('TgpuComputePipeline', () => {
a[0] = f16(_arg_0.gid.x);
}
{
a[1] = 1;
a[1] = 1h;
}

}"
Expand Down
2 changes: 1 addition & 1 deletion packages/typegpu/tests/constant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('tgpu.const', () => {
const fn1 = tgpu.fn([], d.u32)`() { return x; }`.$uses({ x });

expect(asWgsl(fn1)).toMatchInlineSnapshot(`
"const x: u32 = 2;
"const x: u32 = 2u;

fn fn1() -> u32{ return x; }"
`);
Expand Down
2 changes: 1 addition & 1 deletion packages/typegpu/tests/declare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ struct Output {

fn main() -> f32 {
;
return 2;
return 2f;
}"
`);
});
Expand Down
14 changes: 7 additions & 7 deletions packages/typegpu/tests/derived.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ describe('TgpuDerived', () => {

expect(asWgsl(main)).toMatchInlineSnapshot(`
"fn getDouble() -> f32 {
return 4;
return 4f;
}

fn getDouble_1() -> f32 {
return 8;
return 8f;
}

fn main() {
Expand Down Expand Up @@ -102,9 +102,9 @@ describe('TgpuDerived', () => {
}

fn main() {
fill(array<f32, 1>(1));
fill_1(array<f32, 2>(1, 2));
fill_2(array<f32, 3>(1, 2, 3));
fill(array<f32, 1>(1f));
fill_1(array<f32, 2>(1f, 2f));
fill_2(array<f32, 3>(1f, 2f, 3f));
}"
`);
});
Expand Down Expand Up @@ -180,11 +180,11 @@ describe('TgpuDerived', () => {

expect(asWgsl(main)).toMatchInlineSnapshot(`
"fn innerFn() -> f32 {
return 1;
return 1f;
}

fn innerFn_1() -> f32 {
return 2;
return 2f;
}

fn main() {
Expand Down
Loading