Skip to content

Commit c853f5c

Browse files
committed
Simplify and document
1 parent 79252cb commit c853f5c

File tree

3 files changed

+23
-46
lines changed

3 files changed

+23
-46
lines changed

packages/typegpu/src/data/ref.ts

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export interface ref<T> {
4141
// TODO: Restrict calls to this function only from within TypeGPU functions
4242
export const ref: DualFn<<T>(value: T) => ref<T>> = (() => {
4343
const gpuImpl = (value: Snippet) => {
44-
return snip(new RefOnGPU(value), UnknownData, /* origin */ 'runtime');
44+
return snip(new RefOperator(value), UnknownData, /* origin */ 'runtime');
4545
};
4646

4747
const jsImpl = <T>(value: T) => new refImpl(value);
@@ -99,66 +99,43 @@ class refImpl<T> implements ref<T> {
9999
}
100100
}
101101

102-
export class RefOnGPU {
102+
export class RefOperator implements SelfResolvable {
103103
readonly [$internal]: true;
104-
105104
readonly snippet: Snippet;
105+
106106
/**
107107
* Pointer params only exist if the ref was created from a reference (buttery-butter).
108+
*
109+
* @example
110+
* ```ts
111+
* const life = ref(42); // created from a value
112+
* const boid = ref(layout.$.boids[0]); // created from a reference
113+
* ```
108114
*/
109115
readonly ptrType: Ptr | undefined;
110116

111117
constructor(snippet: Snippet) {
112118
this[$internal] = true;
113119
this.snippet = snippet;
114-
this.ptrType = createPtrFromOrigin(
115-
snippet.origin,
116-
snippet.dataType as StorableData,
117-
);
118-
}
119-
120-
toString(): string {
121-
return `ref:${this.snippet.value}`;
122-
}
123120

124-
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
125-
invariant(
126-
!!this.ptrType,
127-
'RefOnGPU must have a pointer type when resolved',
128-
);
129-
return snip(stitch`(&${this.snippet})`, this.ptrType, this.snippet.origin);
130-
}
131-
}
132-
133-
export class RefOperator implements SelfResolvable {
134-
readonly [$internal]: true;
135-
readonly snippet: Snippet;
136-
readonly #ptrType: Ptr;
137-
138-
constructor(snippet: Snippet) {
139-
this[$internal] = true;
140-
this.snippet = snippet;
141-
142-
const ptrType = createPtrFromOrigin(
121+
this.ptrType = createPtrFromOrigin(
143122
snippet.origin,
144123
snippet.dataType as StorableData,
145124
);
146-
147-
if (!ptrType) {
148-
throw new Error(
149-
`Cannot take a reference of a value with origin ${this.snippet.origin}`,
150-
);
151-
}
152-
153-
this.#ptrType = ptrType;
154125
}
155126

156127
get [$ownSnippet](): Snippet {
157-
return snip(this, this.#ptrType, this.snippet.origin);
128+
if (!this.ptrType) {
129+
throw new Error(stitch`Cannot take a reference of ${this.snippet}`);
130+
}
131+
return snip(this, this.ptrType, this.snippet.origin);
158132
}
159133

160134
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
161-
return snip(stitch`(&${this.snippet})`, this.#ptrType, this.snippet.origin);
135+
if (!this.ptrType) {
136+
throw new Error(stitch`Cannot take a reference of ${this.snippet}`);
137+
}
138+
return snip(stitch`(&${this.snippet})`, this.ptrType, this.snippet.origin);
162139
}
163140
}
164141

packages/typegpu/src/tgsl/shellless.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
type ShelllessImpl,
44
} from '../core/function/shelllessImpl.ts';
55
import type { AnyData } from '../data/dataTypes.ts';
6-
import { RefOnGPU } from '../data/ref.ts';
6+
import { RefOperator } from '../data/ref.ts';
77
import type { Snippet } from '../data/snippet.ts';
88
import { isPtr } from '../data/wgslTypes.ts';
99
import { WgslTypeError } from '../errors.ts';
@@ -50,7 +50,7 @@ export class ShelllessRepository {
5050
}
5151

5252
const argTypes = (argSnippets ?? []).map((s, index) => {
53-
if (s.value instanceof RefOnGPU) {
53+
if (s.value instanceof RefOperator) {
5454
if (!s.value.ptrType) {
5555
throw new WgslTypeError(
5656
`d.ref() created with primitive types must be stored in a variable before use`,

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
import type { ShaderGenerator } from './shaderGenerator.ts';
4343
import type { DualFn } from '../data/dualFn.ts';
4444
import { INTERNAL_createPtr, ptrFn } from '../data/ptr.ts';
45-
import { RefOnGPU, RefOperator } from '../data/ref.ts';
45+
import { RefOperator } from '../data/ref.ts';
4646
import { constant } from '../core/constant/tgpuConstant.ts';
4747

4848
const { NodeTypeCatalog: NODE } = tinyest;
@@ -241,7 +241,7 @@ ${this.ctx.pre}}`;
241241
const lhsExpr = this.expression(lhs);
242242
const rhsExpr = this.expression(rhs);
243243

244-
if (rhsExpr.value instanceof RefOnGPU) {
244+
if (rhsExpr.value instanceof RefOperator) {
245245
throw new WgslTypeError(
246246
stitch`Cannot assign a ref to an existing variable '${lhsExpr}', define a new variable instead.`,
247247
);
@@ -780,7 +780,7 @@ ${this.ctx.pre}else ${alternate}`;
780780
);
781781
}
782782

783-
if (eq.value instanceof RefOnGPU) {
783+
if (eq.value instanceof RefOperator) {
784784
// We're assigning a newly created `d.ref()`
785785
if (eq.value.ptrType) {
786786
throw new WgslTypeError(

0 commit comments

Comments
 (0)