Skip to content

Commit 315a4cc

Browse files
Merge branch 'main' into feat/allow-omitting-types-in-wgsl-header
2 parents 51d04ee + 84a4d9d commit 315a4cc

File tree

6 files changed

+225
-6
lines changed

6 files changed

+225
-6
lines changed

packages/typegpu/src/data/vectorOps.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,17 @@ export const VectorOps = {
307307
vec4u: unary4u(Math.acos),
308308
} as Record<VecKind, <T extends vBase>(v: T) => T>,
309309

310+
acosh: {
311+
vec2f: unary2f(Math.acosh),
312+
vec2h: unary2h(Math.acosh),
313+
314+
vec3f: unary3f(Math.acosh),
315+
vec3h: unary3h(Math.acosh),
316+
317+
vec4f: unary4f(Math.acosh),
318+
vec4h: unary4h(Math.acosh),
319+
} as Record<VecKind, <T extends vBase>(v: T) => T>,
320+
310321
asin: {
311322
vec2f: unary2f(Math.asin),
312323
vec2h: unary2h(Math.asin),
@@ -1014,6 +1025,17 @@ export const VectorOps = {
10141025
vec4h: unary4h(Math.cos),
10151026
} as Record<VecKind, <T extends vBase>(v: T) => T>,
10161027

1028+
cosh: {
1029+
vec2f: unary2f(Math.cosh),
1030+
vec2h: unary2h(Math.cosh),
1031+
1032+
vec3f: unary3f(Math.cosh),
1033+
vec3h: unary3h(Math.cosh),
1034+
1035+
vec4f: unary4f(Math.cosh),
1036+
vec4h: unary4h(Math.cosh),
1037+
} as Record<VecKind, <T extends vBase>(v: T) => T>,
1038+
10171039
exp: {
10181040
vec2f: unary2f(Math.exp),
10191041
vec2h: unary2h(Math.exp),
@@ -1025,6 +1047,17 @@ export const VectorOps = {
10251047
vec4h: unary4h(Math.exp),
10261048
} as Record<VecKind, <T extends vBase>(v: T) => T>,
10271049

1050+
exp2: {
1051+
vec2f: unary2f((val) => 2 ** val),
1052+
vec2h: unary2h((val) => 2 ** val),
1053+
1054+
vec3f: unary3f((val) => 2 ** val),
1055+
vec3h: unary3h((val) => 2 ** val),
1056+
1057+
vec4f: unary4f((val) => 2 ** val),
1058+
vec4h: unary4h((val) => 2 ** val),
1059+
} as Record<VecKind, <T extends vBase>(v: T) => T>,
1060+
10281061
fract: {
10291062
vec2f: unary2f((value) => value - Math.floor(value)),
10301063
vec2h: unary2h((value) => value - Math.floor(value)),

packages/typegpu/src/std/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ export {
1010
// builtin functions
1111
abs,
1212
acos,
13+
acosh,
1314
asin,
1415
atan2,
1516
ceil,
1617
clamp,
1718
cos,
19+
cosh,
1820
cross,
1921
distance,
2022
dot,
2123
exp,
24+
exp2,
2225
floor,
2326
fract,
2427
length,

packages/typegpu/src/std/numeric.ts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,23 +268,39 @@ export const acos = createDualImpl(
268268
if (typeof value === 'number') {
269269
return Math.acos(value) as T;
270270
}
271-
return VectorOps.acos[(value as AnyFloatVecInstance).kind](
272-
value as never,
273-
) as T;
271+
return VectorOps.acos[value.kind](value) as T;
274272
},
275273
// GPU implementation
276274
(value) => snip(`acos(${value.value})`, value.dataType),
277275
);
278276

277+
/**
278+
* @privateRemarks
279+
* https://www.w3.org/TR/WGSL/#acosh-builtin
280+
*/
281+
export const acosh = createDualImpl(
282+
// CPU implementation
283+
<T extends AnyFloatVecInstance | number>(value: T): T => {
284+
if (typeof value === 'number') {
285+
return Math.acosh(value) as T;
286+
}
287+
return VectorOps.acosh[value.kind](value) as T;
288+
},
289+
// GPU implementation
290+
(value) => snip(`acosh(${value.value})`, value.dataType),
291+
);
292+
293+
/**
294+
* @privateRemarks
295+
* https://www.w3.org/TR/WGSL/#asin-builtin
296+
*/
279297
export const asin = createDualImpl(
280298
// CPU implementation
281299
<T extends AnyFloatVecInstance | number>(value: T): T => {
282300
if (typeof value === 'number') {
283301
return Math.asin(value) as T;
284302
}
285-
return VectorOps.asin[(value as AnyFloatVecInstance).kind](
286-
value as never,
287-
) as T;
303+
return VectorOps.asin[value.kind](value) as T;
288304
},
289305
// GPU implementation
290306
(value) => snip(`asin(${value.value})`, value.dataType),
@@ -343,6 +359,22 @@ export const cos = createDualImpl(
343359
(value) => snip(`cos(${value.value})`, value.dataType),
344360
);
345361

362+
/**
363+
* @privateRemarks
364+
* https://www.w3.org/TR/WGSL/#cosh-builtin
365+
*/
366+
export const cosh = createDualImpl(
367+
// CPU implementation
368+
<T extends AnyFloatVecInstance | number>(value: T): T => {
369+
if (typeof value === 'number') {
370+
return Math.cosh(value) as T;
371+
}
372+
return VectorOps.cosh[value.kind](value) as T;
373+
},
374+
// GPU implementation
375+
(value) => snip(`cosh(${value.value})`, value.dataType),
376+
);
377+
346378
/**
347379
* @privateRemarks
348380
* https://www.w3.org/TR/WGSL/#cross-builtin
@@ -498,6 +530,22 @@ export const exp = createDualImpl(
498530
(value) => snip(`exp(${value.value})`, value.dataType),
499531
);
500532

533+
/**
534+
* @privateRemarks
535+
* https://www.w3.org/TR/WGSL/#exp2-builtin
536+
*/
537+
export const exp2 = createDualImpl(
538+
// CPU implementation
539+
<T extends AnyFloatVecInstance | number>(value: T): T => {
540+
if (typeof value === 'number') {
541+
return (2 ** value) as T;
542+
}
543+
return VectorOps.exp2[value.kind](value) as T;
544+
},
545+
// GPU implementation
546+
(value) => snip(`exp2(${value.value})`, value.dataType),
547+
);
548+
501549
type PowOverload = {
502550
(base: number, exponent: number): number;
503551
<T extends AnyFloatVecInstance>(base: T, exponent: T): T;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { vec2f, vec3f, vec4f } from '../../../src/data/index.ts';
3+
import { acosh, isCloseTo } from '../../../src/std/index.ts';
4+
5+
describe('acosh', () => {
6+
it('computes acosh of a number', () => {
7+
expect(acosh(1)).toBeCloseTo(0);
8+
expect(acosh(Math.cosh(1))).toBeCloseTo(1);
9+
expect(acosh(Math.cosh(-1))).toBeCloseTo(1);
10+
});
11+
12+
it('computes acosh of vec2f', () => {
13+
const input = vec2f(1, Math.cosh(1));
14+
const expected = vec2f(Math.acosh(1), 1);
15+
expect(isCloseTo(acosh(input), expected)).toBe(true);
16+
});
17+
18+
it('tests acosh(cosh())', () => {
19+
const input = vec2f(1, Math.cosh(1));
20+
const expected = vec2f(Math.acosh(1), Math.acosh(Math.cosh(1)));
21+
expect(isCloseTo(acosh(input), expected)).toBe(true);
22+
});
23+
24+
it('computes acosh of vec3f', () => {
25+
const input = vec3f(1, Math.cosh(1), Math.cosh(-1));
26+
const expected = vec3f(Math.acosh(1), 1, 1);
27+
expect(isCloseTo(acosh(input), expected)).toBe(true);
28+
});
29+
30+
it('computes acosh of vec4f', () => {
31+
const input = vec4f(1, Math.cosh(1), Math.cosh(-1), Math.cosh(2));
32+
const expected = vec4f(Math.acosh(1), 1, 1, 2);
33+
expect(isCloseTo(acosh(input), expected)).toBe(true);
34+
});
35+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { vec2f, vec3f, vec4f } from '../../../src/data/index.ts';
3+
import { cosh, isCloseTo } from '../../../src/std/index.ts';
4+
5+
describe('cosh', () => {
6+
it('computes cosh of a number', () => {
7+
expect(cosh(0)).toBeCloseTo(1);
8+
expect(cosh(1)).toBeCloseTo(Math.cosh(1));
9+
expect(cosh(-1)).toBeCloseTo(Math.cosh(-1));
10+
});
11+
12+
it('computes cosh of vec2f', () => {
13+
const input = vec2f(0, 1);
14+
const expected = vec2f(Math.cosh(0), Math.cosh(1));
15+
expect(isCloseTo(cosh(input), expected)).toBe(true);
16+
});
17+
18+
it('computes cosh of vec3f', () => {
19+
const input = vec3f(0, 1, -1);
20+
const expected = vec3f(Math.cosh(0), Math.cosh(1), Math.cosh(-1));
21+
expect(isCloseTo(cosh(input), expected)).toBe(true);
22+
});
23+
24+
it('computes cosh of vec4f', () => {
25+
const input = vec4f(0, 1, -1, 2);
26+
const expected = vec4f(
27+
Math.cosh(0),
28+
Math.cosh(1),
29+
Math.cosh(-1),
30+
Math.cosh(2),
31+
);
32+
expect(isCloseTo(cosh(input), expected)).toBe(true);
33+
});
34+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { describe, expect, it } from 'vitest';
2+
import {
3+
vec2f,
4+
vec2h,
5+
vec3f,
6+
vec3h,
7+
vec4f,
8+
vec4h,
9+
} from '../../../src/data/index.ts';
10+
import { isCloseTo } from '../../../src/std/index.ts';
11+
import { exp2 } from '../../../src/std/numeric.ts';
12+
13+
describe('exp2', () => {
14+
it('computes exp2 of a number', () => {
15+
expect(exp2(0)).toBeCloseTo(1);
16+
expect(exp2(1)).toBeCloseTo(2);
17+
expect(exp2(2)).toBeCloseTo(4);
18+
expect(exp2(-1)).toBeCloseTo(0.5);
19+
expect(exp2(-2)).toBeCloseTo(0.25);
20+
});
21+
22+
it('computes exp2 of vec2f', () => {
23+
const input = vec2f(0, 1);
24+
const expected = vec2f(2 ** 0, 2 ** 1);
25+
expect(isCloseTo(exp2(input), expected)).toBe(true);
26+
});
27+
28+
it('computes exp2 of vec3f', () => {
29+
const input = vec3f(0, 1, -1);
30+
const expected = vec3f(2 ** 0, 2 ** 1, 2 ** -1);
31+
expect(isCloseTo(exp2(input), expected)).toBe(true);
32+
});
33+
34+
it('computes exp2 of vec4f', () => {
35+
const input = vec4f(0, 1, -1, 2);
36+
const expected = vec4f(2 ** 0, 2 ** 1, 2 ** -1, 2 ** 2);
37+
expect(isCloseTo(exp2(input), expected)).toBe(true);
38+
});
39+
40+
it('computes exp2 of vec2h', () => {
41+
const input = vec2h(0, 1);
42+
const expected = vec2h(2 ** 0, 2 ** 1);
43+
const result = exp2(input);
44+
expect(result.x).toBeCloseTo(expected.x);
45+
expect(result.y).toBeCloseTo(expected.y);
46+
});
47+
48+
it('computes exp2 of vec3h', () => {
49+
const input = vec3h(0, 1, -1);
50+
const expected = vec3h(2 ** 0, 2 ** 1, 2 ** -1);
51+
const result = exp2(input);
52+
expect(result.x).toBeCloseTo(expected.x);
53+
expect(result.y).toBeCloseTo(expected.y);
54+
expect(result.z).toBeCloseTo(expected.z);
55+
});
56+
57+
it('computes exp2 of vec4h', () => {
58+
const input = vec4h(0, 1, -1, 2);
59+
const expected = vec4h(2 ** 0, 2 ** 1, 2 ** -1, 2 ** 2);
60+
const result = exp2(input);
61+
expect(result.x).toBeCloseTo(expected.x);
62+
expect(result.y).toBeCloseTo(expected.y);
63+
expect(result.z).toBeCloseTo(expected.z);
64+
expect(result.w).toBeCloseTo(expected.w);
65+
});
66+
});

0 commit comments

Comments
 (0)