Skip to content

Commit a28d796

Browse files
authored
fix: Enable erasableSyntaxOnly and verbatimModuleSyntax TypeScript options (#2332)
1 parent b051294 commit a28d796

35 files changed

+437
-317
lines changed

packages/typegpu-gl/src/tgpuRootWebGL.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Compute operations, storage buffers, textures, etc. throw WebGLFallbackUnsupportedError.
77
*/
88

9-
import tgpu, { d, ShaderGenerator, TgpuFragmentFn, TgpuVertexFn } from 'typegpu';
9+
import tgpu, { d, ShaderGenerator, type TgpuFragmentFn, type TgpuVertexFn } from 'typegpu';
1010
import glslGenerator, { translateWgslTypeToGlsl } from './glslGenerator.ts';
1111

1212
// ----------

packages/typegpu-sort/src/scan/prefixScan.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@ export class PrefixScanComputer {
2727
#scanPipeline?: TgpuComputePipeline;
2828
#reducePipeline?: TgpuComputePipeline;
2929
#opPipeline?: TgpuComputePipeline;
30+
#root: TgpuRoot;
31+
#operation: BinaryOp['operation'];
32+
#identityElement: BinaryOp['identityElement'];
3033

3134
constructor(
32-
private root: TgpuRoot,
33-
private operation: BinaryOp['operation'],
34-
private identityElement: BinaryOp['identityElement'],
35-
) {}
35+
root: TgpuRoot,
36+
operation: BinaryOp['operation'],
37+
identityElement: BinaryOp['identityElement'],
38+
) {
39+
this.#root = root;
40+
this.#operation = operation;
41+
this.#identityElement = identityElement;
42+
}
3643

3744
private getScanPipeline(onlyGreatestElement: boolean): TgpuComputePipeline {
3845
const cached = onlyGreatestElement ? this.#reducePipeline : this.#scanPipeline;
@@ -41,9 +48,9 @@ export class PrefixScanComputer {
4148
return cached;
4249
}
4350

44-
const pipeline = this.root
45-
.with(operatorSlot, this.operation)
46-
.with(identitySlot, this.identityElement)
51+
const pipeline = this.#root
52+
.with(operatorSlot, this.#operation)
53+
.with(identitySlot, this.#identityElement)
4754
.with(onlyGreatestElementSlot, onlyGreatestElement)
4855
.createComputePipeline({
4956
compute: computeBlock,
@@ -59,14 +66,14 @@ export class PrefixScanComputer {
5966
}
6067

6168
private get opPipeline(): TgpuComputePipeline {
62-
this.#opPipeline ??= this.root.with(operatorSlot, this.operation).createComputePipeline({
69+
this.#opPipeline ??= this.#root.with(operatorSlot, this.#operation).createComputePipeline({
6370
compute: uniformOp,
6471
});
6572
return this.#opPipeline;
6673
}
6774

6875
private getScratchBuffer(size: number): TgpuBuffer<d.WgslArray<d.F32>> & StorageFlag {
69-
return this.root.createBuffer(d.arrayOf(d.f32, size)).$usage('storage');
76+
return this.#root.createBuffer(d.arrayOf(d.f32, size)).$usage('storage');
7077
}
7178

7279
private recursiveScan(
@@ -82,7 +89,7 @@ export class PrefixScanComputer {
8289
// Base case: single workgroup
8390
if (numWorkgroups === 1) {
8491
const finalSums = this.getScratchBuffer(1);
85-
const bg = this.root.createBindGroup(scanLayout, {
92+
const bg = this.#root.createBindGroup(scanLayout, {
8693
input: buffer,
8794
sums: finalSums,
8895
});
@@ -102,7 +109,7 @@ export class PrefixScanComputer {
102109
// Recursive case:
103110
let sumsBuffer = this.getScratchBuffer(numWorkgroups);
104111

105-
const scanBg = this.root.createBindGroup(scanLayout, {
112+
const scanBg = this.#root.createBindGroup(scanLayout, {
106113
input: buffer,
107114
sums: sumsBuffer,
108115
});
@@ -128,7 +135,7 @@ export class PrefixScanComputer {
128135
return sumsBuffer;
129136
}
130137

131-
const opBg = this.root.createBindGroup(uniformOpLayout, {
138+
const opBg = this.#root.createBindGroup(uniformOpLayout, {
132139
input: buffer,
133140
sums: sumsBuffer,
134141
});

packages/typegpu/src/core/buffer/buffer.ts

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,19 @@ export function isUsableAsIndex<T extends TgpuBuffer<BaseData>>(
189189
const endianness = getSystemEndianness();
190190

191191
class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
192-
public readonly [$internal] = true;
193-
public readonly resourceType = 'buffer';
194-
public flags: GPUBufferUsageFlags = GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC;
192+
readonly [$internal] = true;
193+
readonly resourceType = 'buffer';
194+
flags: GPUBufferUsageFlags = GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC;
195+
readonly dataType: TData;
195196

196197
readonly #device: GPUDevice;
197-
private _buffer: GPUBuffer | null = null;
198-
private _ownBuffer: boolean;
199-
private _destroyed = false;
200-
private _hostBuffer: ArrayBuffer | undefined;
201-
private _mappedRange: ArrayBuffer | undefined;
202-
private _initialCallback: BufferInitCallback<TData> | undefined;
198+
#buffer: GPUBuffer | null = null;
199+
#ownBuffer: boolean;
200+
#destroyed = false;
201+
#hostBuffer: ArrayBuffer | undefined;
202+
#mappedRange: ArrayBuffer | undefined;
203+
#initialCallback: BufferInitCallback<TData> | undefined;
204+
readonly #disallowedUsages: UsageLiteral[] | undefined;
203205

204206
readonly initial: InferInput<TData> | undefined;
205207

@@ -211,52 +213,54 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
211213

212214
constructor(
213215
root: ExperimentalTgpuRoot,
214-
public readonly dataType: TData,
216+
dataType: TData,
215217
initialOrBuffer?: BufferInitialData<TData> | GPUBuffer,
216-
private readonly _disallowedUsages?: UsageLiteral[],
218+
disallowedUsages?: UsageLiteral[],
217219
) {
220+
this.dataType = dataType;
221+
this.#disallowedUsages = disallowedUsages;
218222
this.#device = root.device;
219223
if (isGPUBuffer(initialOrBuffer)) {
220-
this._ownBuffer = false;
221-
this._buffer = initialOrBuffer;
224+
this.#ownBuffer = false;
225+
this.#buffer = initialOrBuffer;
222226
} else {
223-
this._ownBuffer = true;
227+
this.#ownBuffer = true;
224228
if (typeof initialOrBuffer === 'function') {
225-
this._initialCallback = initialOrBuffer as BufferInitCallback<TData>;
229+
this.#initialCallback = initialOrBuffer as BufferInitCallback<TData>;
226230
} else {
227231
this.initial = initialOrBuffer;
228232
}
229233
}
230234
}
231235

232236
get buffer() {
233-
if (this._destroyed) {
237+
if (this.#destroyed) {
234238
throw new Error('This buffer has been destroyed');
235239
}
236240

237-
if (!this._buffer) {
238-
this._buffer = this.#device.createBuffer({
241+
if (!this.#buffer) {
242+
this.#buffer = this.#device.createBuffer({
239243
size: sizeOf(this.dataType),
240244
usage: this.flags,
241-
mappedAtCreation: !!this.initial || !!this._initialCallback,
245+
mappedAtCreation: !!this.initial || !!this.#initialCallback,
242246
label: getName(this) ?? '<unnamed>',
243247
});
244248

245-
if (this.initial || this._initialCallback) {
246-
if (this._initialCallback) {
247-
this._initialCallback(this);
249+
if (this.initial || this.#initialCallback) {
250+
if (this.#initialCallback) {
251+
this.#initialCallback(this);
248252
} else if (this.initial) {
249253
this.#writeToTarget(this.#getMappedRange(), this.initial);
250254
}
251255
this.#unmapBuffer();
252256
}
253257
}
254258

255-
return this._buffer;
259+
return this.#buffer;
256260
}
257261

258262
get destroyed() {
259-
return this._destroyed;
263+
return this.#destroyed;
260264
}
261265

262266
get arrayBuffer(): ArrayBuffer {
@@ -265,35 +269,35 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
265269
return this.#getMappedRange();
266270
}
267271

268-
if (!this._hostBuffer) {
269-
this._hostBuffer = new ArrayBuffer(sizeOf(this.dataType));
272+
if (!this.#hostBuffer) {
273+
this.#hostBuffer = new ArrayBuffer(sizeOf(this.dataType));
270274
}
271275

272-
return this._hostBuffer;
276+
return this.#hostBuffer;
273277
}
274278

275279
#getMappedRange(): ArrayBuffer {
276-
if (!this._buffer || this._buffer.mapState !== 'mapped') {
280+
if (!this.#buffer || this.#buffer.mapState !== 'mapped') {
277281
throw new Error('Buffer is not mapped.');
278282
}
279283

280-
this._mappedRange ??= this._buffer.getMappedRange();
281-
return this._mappedRange;
284+
this.#mappedRange ??= this.#buffer.getMappedRange();
285+
return this.#mappedRange;
282286
}
283287

284288
#unmapBuffer(): void {
285-
if (!this._buffer || this._buffer.mapState !== 'mapped') {
289+
if (!this.#buffer || this.#buffer.mapState !== 'mapped') {
286290
return;
287291
}
288292

289-
this._mappedRange = undefined;
290-
this._buffer.unmap();
293+
this.#mappedRange = undefined;
294+
this.#buffer.unmap();
291295
}
292296

293297
$name(label: string) {
294298
setName(this, label);
295-
if (this._buffer) {
296-
this._buffer.label = label;
299+
if (this.#buffer) {
300+
this.#buffer.label = label;
297301
}
298302
return this;
299303
}
@@ -302,7 +306,7 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
302306
...usages: T
303307
): this & UnionToIntersection<LiteralToUsageType<T[number]>> {
304308
for (const usage of usages) {
305-
if (this._disallowedUsages?.includes(usage)) {
309+
if (this.#disallowedUsages?.includes(usage)) {
306310
throw new Error(`Buffer of type ${this.dataType.type} cannot be used as ${usage}`);
307311
}
308312

@@ -321,7 +325,7 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
321325
}
322326

323327
$addFlags(flags: GPUBufferUsageFlags) {
324-
if (!this._ownBuffer) {
328+
if (!this.#ownBuffer) {
325329
throw new Error('Cannot add flags to a buffer that is not managed by TypeGPU.');
326330
}
327331

@@ -426,16 +430,16 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
426430
return;
427431
}
428432

429-
if (!this._hostBuffer) {
430-
this._hostBuffer = new ArrayBuffer(bufferSize);
433+
if (!this.#hostBuffer) {
434+
this.#hostBuffer = new ArrayBuffer(bufferSize);
431435
}
432436

433-
// If the caller already wrote directly into _hostBuffer via
437+
// If the caller already wrote directly into #hostBuffer via
434438
// arrayBuffer, skip the redundant copy, the data is already in place.
435-
if (!(data instanceof ArrayBuffer && data === this._hostBuffer)) {
436-
this.#writeToTarget(this._hostBuffer, data, options);
439+
if (!(data instanceof ArrayBuffer && data === this.#hostBuffer)) {
440+
this.#writeToTarget(this.#hostBuffer, data, options);
437441
}
438-
this.#device.queue.writeBuffer(gpuBuffer, startOffset, this._hostBuffer, startOffset, size);
442+
this.#device.queue.writeBuffer(gpuBuffer, startOffset, this.#hostBuffer, startOffset, size);
439443
}
440444

441445
public writePartial(data: InferPartial<TData>): void {
@@ -527,13 +531,13 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
527531
}
528532

529533
destroy() {
530-
if (this._destroyed) {
534+
if (this.#destroyed) {
531535
return;
532536
}
533-
this._destroyed = true;
534-
this._mappedRange = undefined;
535-
if (this._ownBuffer) {
536-
this._buffer?.destroy();
537+
this.#destroyed = true;
538+
this.#mappedRange = undefined;
539+
if (this.#ownBuffer) {
540+
this.#buffer?.destroy();
537541
}
538542
}
539543

packages/typegpu/src/core/buffer/bufferShorthand.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,18 @@ export class TgpuBufferShorthandImpl<
8686
> implements SelfResolvable {
8787
readonly [$internal] = true;
8888
readonly [$getNameForward]: object;
89+
readonly resourceType: TType;
90+
readonly buffer: TgpuBuffer<TData> &
91+
(TType extends 'mutable' | 'readonly' ? StorageFlag : UniformFlag);
92+
8993
readonly #usage: TgpuBufferUsage<TData, TType>;
9094

9195
constructor(
92-
public readonly resourceType: TType,
93-
public readonly buffer: TgpuBuffer<TData> &
94-
(TType extends 'mutable' | 'readonly' ? StorageFlag : UniformFlag),
96+
resourceType: TType,
97+
buffer: TgpuBuffer<TData> & (TType extends 'mutable' | 'readonly' ? StorageFlag : UniformFlag),
9598
) {
99+
this.resourceType = resourceType;
100+
this.buffer = buffer;
96101
this[$getNameForward] = buffer;
97102
// oxlint-disable-next-line typescript/no-explicit-any -- too complex a type
98103
this.#usage = (this.buffer as any).as(this.resourceType);

packages/typegpu/src/core/buffer/bufferUsage.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,16 @@ class TgpuFixedBufferImpl<TData extends BaseData, TUsage extends BindableBufferU
9797
{
9898
/** Type-token, not available at runtime */
9999
declare readonly [$repr]: Infer<TData>;
100+
100101
readonly resourceType = 'buffer-usage' as const;
102+
readonly usage: TUsage;
103+
readonly buffer: TgpuBuffer<TData>;
101104
readonly [$internal]: { readonly dataType: TData };
102105
readonly [$getNameForward]: TgpuBuffer<TData>;
103106

104-
constructor(
105-
public readonly usage: TUsage,
106-
public readonly buffer: TgpuBuffer<TData>,
107-
) {
107+
constructor(usage: TUsage, buffer: TgpuBuffer<TData>) {
108+
this.usage = usage;
109+
this.buffer = buffer;
108110
this[$internal] = { dataType: buffer.dataType };
109111
this[$getNameForward] = buffer;
110112
}
@@ -222,16 +224,18 @@ export class TgpuLaidOutBufferImpl<TData extends BaseData, TUsage extends Bindab
222224
{
223225
/** Type-token, not available at runtime */
224226
declare readonly [$repr]: Infer<TData>;
227+
225228
readonly [$internal]: { readonly dataType: TData };
226229
readonly resourceType = 'buffer-usage' as const;
230+
readonly usage: TUsage;
231+
readonly dataType: TData;
232+
227233
readonly #membership: LayoutMembership;
228234

229-
constructor(
230-
public readonly usage: TUsage,
231-
public readonly dataType: TData,
232-
membership: LayoutMembership,
233-
) {
235+
constructor(usage: TUsage, dataType: TData, membership: LayoutMembership) {
234236
this[$internal] = { dataType };
237+
this.usage = usage;
238+
this.dataType = dataType;
235239
this.#membership = membership;
236240
setName(this, membership.key);
237241
}

packages/typegpu/src/core/constant/tgpuConstant.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ function deepFreeze<T extends object>(object: T): T {
9494
class TgpuConstImpl<TDataType extends BaseData> implements TgpuConst<TDataType>, SelfResolvable {
9595
readonly [$internal] = {};
9696
readonly resourceType: 'const';
97+
readonly dataType: TDataType;
98+
9799
readonly #value: DeepReadonly<InferGPU<TDataType>>;
98100

99-
constructor(
100-
public readonly dataType: TDataType,
101-
value: InferGPU<TDataType>,
102-
) {
101+
constructor(dataType: TDataType, value: InferGPU<TDataType>) {
103102
this.resourceType = 'const';
103+
this.dataType = dataType;
104104
this.#value =
105105
value && typeof value === 'object'
106106
? (deepFreeze(value) as DeepReadonly<InferGPU<TDataType>>)

0 commit comments

Comments
 (0)