Skip to content

Commit 4ccdd96

Browse files
committed
Working on umiform refs
1 parent 2ed5955 commit 4ccdd96

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

apps/typegpu-docs/src/examples/simulation/gravity/compute.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ export const computeCollisionsShader = tgpu['~unstable'].computeFn({
3737
otherId++
3838
) {
3939
const other = computeLayout.$.inState[otherId];
40-
// no collision occurs...
4140
if (
4241
otherId === currentId || // ...with itself
4342
other.destroyed === 1 || // ...when other is destroyed
4443
current.collisionBehavior === none || // ...when current behavior is none
4544
other.collisionBehavior === none || // ...when other behavior is none
4645
std.distance(current.position, other.position) >=
47-
radiusOf(current) + radiusOf(other) // ...when other is too far away
46+
radiusOf(d.ref(current)) + radiusOf(d.ref(other)) // ...when other is too far away
4847
) {
48+
// no collision occurs...
4949
continue;
5050
}
5151

@@ -58,8 +58,9 @@ export const computeCollisionsShader = tgpu['~unstable'].computeFn({
5858
// push the smaller object outside
5959
if (isSmaller(currentId, otherId)) {
6060
const dir = std.normalize(current.position.sub(other.position));
61-
current.position = other.position
62-
.add(dir.mul(radiusOf(current) + radiusOf(other)));
61+
current.position = other.position.add(
62+
dir.mul(radiusOf(current) + radiusOf(other)),
63+
);
6364
}
6465

6566
// bounce with tiny damping

apps/typegpu-docs/src/examples/simulation/gravity/helpers.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import { OBJLoader } from '@loaders.gl/obj';
33
import type { TgpuRoot } from 'typegpu';
44
import * as d from 'typegpu/data';
55
import { sphereTextureNames } from './enums.ts';
6-
import {
7-
type CelestialBody,
8-
renderVertexLayout,
9-
SkyBoxVertex,
10-
} from './schemas.ts';
6+
import { CelestialBody, renderVertexLayout, SkyBoxVertex } from './schemas.ts';
117

128
function vert(
139
position: [number, number, number],
@@ -166,7 +162,7 @@ export async function loadSphereTextures(root: TgpuRoot) {
166162
return texture;
167163
}
168164

169-
export const radiusOf = (body: CelestialBody): number => {
165+
export const radiusOf = (body: d.ref<CelestialBody>): number => {
170166
'use gpu';
171-
return (((body.mass * 0.75) / Math.PI) ** 0.333) * body.radiusMultiplier;
167+
return (((body.$.mass * 0.75) / Math.PI) ** 0.333) * body.$.radiusMultiplier;
172168
};

packages/typegpu/tests/ref.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,20 @@ describe('ref', () => {
4343
- fn*:hello: Cannot assign a ref to an existing variable '(&foo)', define a new variable instead.]
4444
`);
4545
});
46+
47+
it('fails when creating a ref with a reference', () => {
48+
const hello = () => {
49+
'use gpu';
50+
const position = d.vec3f(1, 2, 3);
51+
const foo = d.ref(position);
52+
};
53+
54+
expect(() => asWgsl(hello)).toThrowErrorMatchingInlineSnapshot(`
55+
[Error: Resolution of the following tree failed:
56+
- <root>
57+
- fn*:hello
58+
- fn*:hello
59+
- fn:ref: Can't create refs from references. Copy the value first by wrapping it in its schema.]
60+
`);
61+
});
4662
});

packages/typegpu/tests/tgsl/shellless.test.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,11 @@ describe('shellless', () => {
166166
pos.$.z += vel.z;
167167
};
168168

169-
const main = tgpu.fn([])(() => {
169+
const main = () => {
170+
'use gpu';
170171
const pos = d.ref(d.vec3f(0, 0, 0));
171172
advance(pos, d.vec3f(1, 2, 3));
172-
});
173+
};
173174

174175
expect(asWgsl(main)).toMatchInlineSnapshot(`
175176
"fn advance(pos: ptr<function, vec3f>, vel: vec3f) {
@@ -185,6 +186,36 @@ describe('shellless', () => {
185186
`);
186187
});
187188

189+
it('generates uniform pointer params when passing a fixed uniform directly to a function', ({ root }) => {
190+
const posUniform = root.createUniform(d.vec3f);
191+
192+
const sumComponents = (vec: d.ref<d.v3f>) => {
193+
'use gpu';
194+
return vec.$.x + vec.$.y + vec.$.z;
195+
};
196+
197+
const main = () => {
198+
'use gpu';
199+
sumComponents(posUniform);
200+
sumComponents(d.ref(posUniform.$.zyx));
201+
// sumComponents(&posUniform);
202+
};
203+
204+
expect(asWgsl(main)).toMatchInlineSnapshot(`
205+
"fn advance(pos: ptr<function, vec3f>, vel: vec3f) {
206+
(*pos).x += vel.x;
207+
(*pos).y += vel.y;
208+
(*pos).z += vel.z;
209+
}
210+
211+
fn main() {
212+
var pos = vec3f();
213+
var vel = vec3f(1, 2, 3);
214+
advance((&pos), (&vel));
215+
}"
216+
`);
217+
});
218+
188219
it('resolves when accepting no arguments', () => {
189220
const main = () => {
190221
'use gpu';

0 commit comments

Comments
 (0)