Skip to content

Commit 39045ba

Browse files
authored
Merge branch 'main' into feat/byeflush
2 parents 4ba5660 + 29c0185 commit 39045ba

File tree

16 files changed

+1410
-929
lines changed

16 files changed

+1410
-929
lines changed

packages/typegpu/src/data/numberOps.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ export const divInteger = (lhs: number, rhs: number) => {
1919
}
2020
return Math.trunc(lhs / rhs);
2121
};
22+
23+
export function bitcastU32toF32Impl(n: number): number {
24+
const dataView = new DataView(new ArrayBuffer(4));
25+
dataView.setUint32(0, n, true);
26+
return dataView.getFloat32(0, true);
27+
}
28+
export function bitcastU32toI32Impl(n: number): number {
29+
const dataView = new DataView(new ArrayBuffer(4));
30+
dataView.setUint32(0, n, true);
31+
return dataView.getInt32(0, true);
32+
}

packages/typegpu/src/data/vector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { stitch } from '../core/resolve/stitch.ts';
21
import { dualImpl } from '../core/function/dualImpl.ts';
2+
import { stitch } from '../core/resolve/stitch.ts';
33
import { $repr } from '../shared/symbols.ts';
4+
import { type AnyData, undecorate } from './dataTypes.ts';
45
import { bool, f16, f32, i32, u32 } from './numeric.ts';
56
import {
67
Vec2bImpl,
@@ -44,7 +45,6 @@ import type {
4445
Vec4u,
4546
} from './wgslTypes.ts';
4647
import { isVec } from './wgslTypes.ts';
47-
import { type AnyData, undecorate } from './dataTypes.ts';
4848

4949
// ----------
5050
// Public API

packages/typegpu/src/data/vectorOps.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { $internal } from '../shared/symbols.ts';
22
import { mat2x2f, mat3x3f, mat4x4f } from './matrix.ts';
3-
import { clamp, divInteger, smoothstepScalar } from './numberOps.ts';
3+
import {
4+
bitcastU32toF32Impl,
5+
bitcastU32toI32Impl,
6+
clamp,
7+
divInteger,
8+
smoothstepScalar,
9+
} from './numberOps.ts';
410
import * as vectorConstructors from './vector.ts';
511
import type * as wgsl from './wgslTypes.ts';
612
import type { VecKind } from './wgslTypes.ts';
@@ -1253,4 +1259,54 @@ export const VectorOps = {
12531259
vec4f: unary4f(Math.tanh),
12541260
vec4h: unary4h(Math.tanh),
12551261
} as Record<VecKind, <T extends vBase>(v: T) => T>,
1262+
1263+
bitcastU32toF32: {
1264+
vec2u: (n: wgsl.v2u) =>
1265+
vec2f(bitcastU32toF32Impl(n.x), bitcastU32toF32Impl(n.y)),
1266+
vec3u: (n: wgsl.v3u) =>
1267+
vec3f(
1268+
bitcastU32toF32Impl(n.x),
1269+
bitcastU32toF32Impl(n.y),
1270+
bitcastU32toF32Impl(n.z),
1271+
),
1272+
vec4u: (n: wgsl.v4u) =>
1273+
vec4f(
1274+
bitcastU32toF32Impl(n.x),
1275+
bitcastU32toF32Impl(n.y),
1276+
bitcastU32toF32Impl(n.z),
1277+
bitcastU32toF32Impl(n.w),
1278+
),
1279+
} as Record<
1280+
VecKind,
1281+
<T extends wgsl.AnyUnsignedVecInstance>(
1282+
v: T,
1283+
) => T extends wgsl.v2u ? wgsl.v2f
1284+
: T extends wgsl.v3u ? wgsl.v3f
1285+
: wgsl.v4f
1286+
>,
1287+
1288+
bitcastU32toI32: {
1289+
vec2u: (n: wgsl.v2u) =>
1290+
vec2i(bitcastU32toI32Impl(n.x), bitcastU32toI32Impl(n.y)),
1291+
vec3u: (n: wgsl.v3u) =>
1292+
vec3i(
1293+
bitcastU32toI32Impl(n.x),
1294+
bitcastU32toI32Impl(n.y),
1295+
bitcastU32toI32Impl(n.z),
1296+
),
1297+
vec4u: (n: wgsl.v4u) =>
1298+
vec4i(
1299+
bitcastU32toI32Impl(n.x),
1300+
bitcastU32toI32Impl(n.y),
1301+
bitcastU32toI32Impl(n.z),
1302+
bitcastU32toI32Impl(n.w),
1303+
),
1304+
} as Record<
1305+
VecKind,
1306+
<T extends wgsl.AnyUnsignedVecInstance>(
1307+
v: T,
1308+
) => T extends wgsl.v2u ? wgsl.v2i
1309+
: T extends wgsl.v3u ? wgsl.v3i
1310+
: wgsl.v4i
1311+
>,
12561312
};

packages/typegpu/src/data/wgslTypes.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,8 @@ export type AnyFloat16VecInstance = v2h | v3h | v4h;
655655

656656
export type AnyFloatVecInstance = v2f | v2h | v3f | v3h | v4f | v4h;
657657

658+
export type AnyUnsignedVecInstance = v2u | v3u | v4u;
659+
658660
export type AnyIntegerVecInstance = v2i | v2u | v3i | v3u | v4i | v4u;
659661

660662
export type AnyBooleanVecInstance = v2b | v3b | v4b;
@@ -1844,3 +1846,17 @@ export function isNumericSchema(
18441846
type === 'u32')
18451847
);
18461848
}
1849+
1850+
export function isHalfPrecisionSchema(
1851+
schema: unknown,
1852+
): schema is F16 | Vec2h | Vec3h | Vec4h {
1853+
const type = (schema as BaseData)?.type;
1854+
1855+
return (
1856+
!!(schema as BaseData)?.[$internal] &&
1857+
(type === 'f16' ||
1858+
type === 'vec2h' ||
1859+
type === 'vec3h' ||
1860+
type === 'vec4h')
1861+
);
1862+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { dualImpl } from '../core/function/dualImpl.ts';
2+
import { stitch } from '../core/resolve/stitch.ts';
3+
import { bitcastU32toF32Impl, bitcastU32toI32Impl } from '../data/numberOps.ts';
4+
import { f32, i32, u32 } from '../data/numeric.ts';
5+
import { isVec } from '../data/wgslTypes.ts';
6+
import { vec2f, vec2i, vec3f, vec3i, vec4f, vec4i } from '../data/vector.ts';
7+
import { VectorOps } from '../data/vectorOps.ts';
8+
import type {
9+
v2f,
10+
v2i,
11+
v2u,
12+
v3f,
13+
v3i,
14+
v3u,
15+
v4f,
16+
v4i,
17+
v4u,
18+
} from '../data/wgslTypes.ts';
19+
import { unify } from '../tgsl/conversion.ts';
20+
21+
export type BitcastU32toF32Overload =
22+
& ((value: number) => number)
23+
& ((value: v2u) => v2f)
24+
& ((value: v3u) => v3f)
25+
& ((value: v4u) => v4f);
26+
27+
export const bitcastU32toF32 = dualImpl({
28+
name: 'bitcastU32toF32',
29+
normalImpl: ((value) => {
30+
if (typeof value === 'number') {
31+
return bitcastU32toF32Impl(value);
32+
}
33+
return VectorOps.bitcastU32toF32[value.kind](value);
34+
}) as BitcastU32toF32Overload,
35+
codegenImpl: (n) => stitch`bitcast<f32>(${n})`,
36+
signature: (...arg) => {
37+
const uargs = unify(arg, [u32]) ?? arg;
38+
return {
39+
argTypes: uargs,
40+
returnType: isVec(uargs[0])
41+
? uargs[0].type === 'vec2u'
42+
? vec2f
43+
: uargs[0].type === 'vec3u'
44+
? vec3f
45+
: vec4f
46+
: f32,
47+
};
48+
},
49+
});
50+
51+
export type BitcastU32toI32Overload =
52+
& ((value: number) => number)
53+
& ((value: v2u) => v2i)
54+
& ((value: v3u) => v3i)
55+
& ((value: v4u) => v4i);
56+
57+
export const bitcastU32toI32 = dualImpl({
58+
name: 'bitcastU32toI32',
59+
normalImpl: ((value) => {
60+
if (typeof value === 'number') {
61+
return bitcastU32toI32Impl(value);
62+
}
63+
return VectorOps.bitcastU32toI32[value.kind](value);
64+
}) as BitcastU32toI32Overload,
65+
codegenImpl: (n) => stitch`bitcast<i32>(${n})`,
66+
signature: (...arg) => {
67+
const uargs = unify(arg, [u32]) ?? arg;
68+
return {
69+
argTypes: uargs,
70+
returnType: isVec(uargs[0])
71+
? uargs[0].type === 'vec2u'
72+
? vec2i
73+
: uargs[0].type === 'vec3u'
74+
? vec3i
75+
: vec4i
76+
: i32,
77+
};
78+
},
79+
});

packages/typegpu/src/std/boolean.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { dualImpl } from '../core/function/dualImpl.ts';
12
import { stitch } from '../core/resolve/stitch.ts';
2-
import { isSnippetNumeric, snip } from '../data/snippet.ts';
3+
import type { AnyData } from '../data/dataTypes.ts';
34
import { bool, f32 } from '../data/numeric.ts';
5+
import { isSnippetNumeric, snip } from '../data/snippet.ts';
46
import { vec2b, vec3b, vec4b } from '../data/vector.ts';
57
import { VectorOps } from '../data/vectorOps.ts';
68
import {
@@ -16,10 +18,8 @@ import {
1618
type v3b,
1719
type v4b,
1820
} from '../data/wgslTypes.ts';
19-
import { dualImpl } from '../core/function/dualImpl.ts';
20-
import { sub } from './operators.ts';
21-
import type { AnyData } from '../data/dataTypes.ts';
2221
import { $internal } from '../shared/symbols.ts';
22+
import { sub } from './operators.ts';
2323

2424
function correspondingBooleanVectorSchema(dataType: AnyData) {
2525
if (dataType.type.includes('2')) {
@@ -270,7 +270,7 @@ export const isCloseTo = dualImpl({
270270
lhs: T,
271271
rhs: T,
272272
precision = 0.01,
273-
) => {
273+
): boolean => {
274274
if (typeof lhs === 'number' && typeof rhs === 'number') {
275275
return Math.abs(lhs - rhs) < precision;
276276
}
@@ -296,16 +296,31 @@ export const isCloseTo = dualImpl({
296296
},
297297
});
298298

299-
export type SelectOverload = {
300-
<T extends number | boolean | AnyVecInstance>(f: T, t: T, cond: boolean): T;
301-
<T extends AnyVecInstance>(
302-
f: T,
303-
t: T,
304-
cond: T extends AnyVec2Instance ? v2b
299+
function cpuSelect(f: boolean, t: boolean, cond: boolean): boolean;
300+
function cpuSelect(f: number, t: number, cond: boolean): number;
301+
function cpuSelect<T extends AnyVecInstance>(
302+
f: T,
303+
t: T,
304+
cond:
305+
| boolean
306+
| (T extends AnyVec2Instance ? v2b
305307
: T extends AnyVec3Instance ? v3b
306-
: v4b,
307-
): T;
308-
};
308+
: v4b),
309+
): T;
310+
function cpuSelect<T extends number | boolean | AnyVecInstance>(
311+
f: T,
312+
t: T,
313+
cond: AnyBooleanVecInstance | boolean,
314+
) {
315+
if (typeof cond === 'boolean') {
316+
return cond ? t : f;
317+
}
318+
return VectorOps.select[(f as AnyVecInstance).kind](
319+
f as AnyVecInstance,
320+
t as AnyVecInstance,
321+
cond,
322+
);
323+
}
309324

310325
/**
311326
* Returns `t` if `cond` is `true`, and `f` otherwise.
@@ -316,22 +331,9 @@ export type SelectOverload = {
316331
* select(vec2i(1, 2), vec2i(3, 4), true) // returns vec2i(3, 4)
317332
* select(vec2i(1, 2), vec2i(3, 4), vec2b(false, true)) // returns vec2i(1, 4)
318333
*/
319-
export const select: SelectOverload = dualImpl({
334+
export const select = dualImpl({
320335
name: 'select',
321336
signature: (...argTypes) => ({ argTypes, returnType: argTypes[0] }),
322-
normalImpl<T extends number | boolean | AnyVecInstance>(
323-
f: T,
324-
t: T,
325-
cond: AnyBooleanVecInstance | boolean,
326-
) {
327-
if (typeof cond === 'boolean') {
328-
return cond ? t : f;
329-
}
330-
return VectorOps.select[(f as AnyVecInstance).kind](
331-
f as AnyVecInstance,
332-
t as AnyVecInstance,
333-
cond,
334-
);
335-
},
337+
normalImpl: cpuSelect,
336338
codegenImpl: (f, t, cond) => stitch`select(${f}, ${t}, ${cond})`,
337339
});

0 commit comments

Comments
 (0)