Skip to content

Commit

Permalink
fix: Plum Purge (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoplaza authored Dec 10, 2024
1 parent 148aff0 commit 539e7d5
Show file tree
Hide file tree
Showing 12 changed files with 9 additions and 961 deletions.
39 changes: 5 additions & 34 deletions packages/typegpu/src/core/buffer/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { Storage } from '../../extension';
import type { TgpuNamable } from '../../namable';
import type { Infer } from '../../shared/repr';
import type { UnionToIntersection } from '../../shared/utilityTypes';
import { type TgpuPlum, type Unsubscribe, isPlum } from '../../tgpuPlumTypes';
import { isGPUBuffer } from '../../types';
import type { ExperimentalTgpuRoot } from '../root/rootTypes';

Expand Down Expand Up @@ -37,7 +36,7 @@ type LiteralToUsageType<T extends 'uniform' | 'storage' | 'vertex'> =
export interface TgpuBuffer<TData extends AnyData> extends TgpuNamable {
readonly resourceType: 'buffer';
readonly dataType: TData;
readonly initial?: Infer<TData> | TgpuPlum<Infer<TData>> | undefined;
readonly initial?: Infer<TData> | undefined;
readonly label: string | undefined;

readonly buffer: GPUBuffer;
Expand All @@ -58,7 +57,7 @@ export interface TgpuBuffer<TData extends AnyData> extends TgpuNamable {
export function createBufferImpl<TData extends AnyData>(
group: ExperimentalTgpuRoot | undefined,
typeSchema: TData,
initialOrBuffer?: Infer<TData> | TgpuPlum<Infer<TData>> | GPUBuffer,
initialOrBuffer?: Infer<TData> | GPUBuffer,
): TgpuBuffer<TData> {
return new TgpuBufferImpl(group, typeSchema, initialOrBuffer);
}
Expand Down Expand Up @@ -92,10 +91,9 @@ class TgpuBufferImpl<TData extends AnyData> implements TgpuBuffer<TData> {
private _device: GPUDevice | null = null;
private _buffer: GPUBuffer | null = null;
private _destroyed = false;
private _subscription: Unsubscribe | null = null;

private _label: string | undefined;
readonly initial: Infer<TData> | TgpuPlum<Infer<TData>> | undefined;
readonly initial: Infer<TData> | undefined;

public usableAsUniform = false;
public usableAsStorage = false;
Expand All @@ -104,11 +102,7 @@ class TgpuBufferImpl<TData extends AnyData> implements TgpuBuffer<TData> {
constructor(
private readonly _group: ExperimentalTgpuRoot | undefined,
public readonly dataType: TData,
public readonly initialOrBuffer?:
| Infer<TData>
| TgpuPlum<Infer<TData>>
| GPUBuffer
| undefined,
public readonly initialOrBuffer?: Infer<TData> | GPUBuffer | undefined,
) {
if (isGPUBuffer(initialOrBuffer)) {
this._buffer = initialOrBuffer;
Expand Down Expand Up @@ -141,27 +135,7 @@ class TgpuBufferImpl<TData extends AnyData> implements TgpuBuffer<TData> {

if (this.initial) {
const writer = new BufferWriter(this._buffer.getMappedRange());

if (isPlum(this.initial)) {
const group = this._group;

if (!group) {
throw new Error(
'Create this buffer using `root.createBuffer` instead of `tgpu.createBuffer`.',
);
}

const plum = this.initial;

writeData(writer, this.dataType, group.readPlum(plum));

this._subscription = group.onPlumChange(plum, () => {
this.write(group.readPlum(plum));
});
} else {
writeData(writer, this.dataType, this.initial);
}

writeData(writer, this.dataType, this.initial);
this._buffer.unmap();
}
}
Expand Down Expand Up @@ -305,9 +279,6 @@ class TgpuBufferImpl<TData extends AnyData> implements TgpuBuffer<TData> {
return;
}
this._destroyed = true;
if (this._subscription) {
this._subscription();
}
this._buffer?.destroy();
}

Expand Down
37 changes: 1 addition & 36 deletions packages/typegpu/src/core/root/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ import {
RandomNameRegistry,
StrictNameRegistry,
} from '../../nameRegistry';
import { type PlumListener, PlumStore } from '../../plumStore';
import type { TgpuSettable } from '../../settableTrait';
import type { Infer } from '../../shared/repr';
import type { AnyVertexAttribs } from '../../shared/vertexFormat';
import type {
TgpuBindGroup,
TgpuBindGroupLayout,
} from '../../tgpuBindGroupLayout';
import { isBindGroup, isBindGroupLayout } from '../../tgpuBindGroupLayout';
import type {
ExtractPlumValue,
TgpuPlum,
Unsubscribe,
} from '../../tgpuPlumTypes';
import type { TgpuSlot } from '../../types';
import { type TgpuBuffer, createBufferImpl, isBuffer } from '../buffer/buffer';
import type { IOLayout } from '../function/fnTypes';
Expand Down Expand Up @@ -58,7 +51,6 @@ import type {
CreateTextureOptions,
CreateTextureResult,
ExperimentalTgpuRoot,
SetPlumAction,
TgpuRoot,
WithBinding,
WithCompute,
Expand Down Expand Up @@ -165,8 +157,6 @@ class TgpuRootImpl extends WithBindingImpl implements ExperimentalTgpuRoot {

private _commandEncoder: GPUCommandEncoder | null = null;

private readonly _plumStore = new PlumStore();

constructor(
public readonly device: GPUDevice,
public readonly nameRegistry: NameRegistry,
Expand All @@ -185,7 +175,7 @@ class TgpuRootImpl extends WithBindingImpl implements ExperimentalTgpuRoot {

createBuffer<TData extends AnyData>(
typeSchema: TData,
initialOrBuffer?: Infer<TData> | TgpuPlum<Infer<TData>> | GPUBuffer,
initialOrBuffer?: Infer<TData> | GPUBuffer,
): TgpuBuffer<TData> {
const buffer = createBufferImpl(this, typeSchema, initialOrBuffer).$device(
this.device,
Expand Down Expand Up @@ -301,31 +291,6 @@ class TgpuRootImpl extends WithBindingImpl implements ExperimentalTgpuRoot {
throw new Error(`Unknown resource type: ${resource}`);
}

readPlum<TPlum extends TgpuPlum>(plum: TPlum): ExtractPlumValue<TPlum> {
return this._plumStore.get(plum);
}

setPlum<TPlum extends TgpuPlum & TgpuSettable>(
plum: TPlum,
value: SetPlumAction<ExtractPlumValue<TPlum>>,
) {
type Value = ExtractPlumValue<TPlum>;

if (typeof value === 'function') {
const compute = value as (prev: Value) => Value;
this._plumStore.set(plum, compute(this._plumStore.get(plum)));
} else {
this._plumStore.set(plum, value);
}
}

onPlumChange<TValue>(
plum: TgpuPlum<TValue>,
listener: PlumListener<TValue>,
): Unsubscribe {
return this._plumStore.subscribe(plum, listener);
}

flush() {
if (!this._commandEncoder) {
return;
Expand Down
23 changes: 1 addition & 22 deletions packages/typegpu/src/core/root/rootTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@ import type { Exotic } from '../../data/exotic';
import type { Vec4f } from '../../data/wgslTypes';
import type { JitTranspiler } from '../../jitTranspiler';
import type { NameRegistry } from '../../nameRegistry';
import type { PlumListener } from '../../plumStore';
import type { TgpuSettable } from '../../settableTrait';
import type { Infer } from '../../shared/repr';
import type { Mutable, OmitProps, Prettify } from '../../shared/utilityTypes';
import type {
ExtractPlumValue,
TgpuPlum,
Unsubscribe,
} from '../../tgpuPlumTypes';
import type { Eventual, TgpuSlot } from '../../types';
import type { Unwrapper } from '../../unwrapper';
import type { TgpuBuffer } from '../buffer/buffer';
Expand All @@ -32,8 +25,6 @@ import type { LayoutToAllowedAttribs } from '../vertexLayout/vertexAttribute';
// Public API
// ----------

export type SetPlumAction<T> = T | ((prev: T) => T);

export interface WithCompute {
createPipeline(): TgpuComputePipeline;
}
Expand Down Expand Up @@ -160,7 +151,7 @@ export interface TgpuRoot extends Unwrapper {
*/
createBuffer<TData extends AnyData>(
typeSchema: TData,
initial?: Infer<Exotic<TData>> | TgpuPlum<Infer<TData>> | undefined,
initial?: Infer<Exotic<TData>> | undefined,
): TgpuBuffer<Exotic<TData>>;

/**
Expand Down Expand Up @@ -217,18 +208,6 @@ export interface ExperimentalTgpuRoot extends TgpuRoot, WithBinding {
*/
readonly commandEncoder: GPUCommandEncoder;

readPlum<TPlum extends TgpuPlum>(plum: TPlum): ExtractPlumValue<TPlum>;

setPlum<TPlum extends TgpuPlum & TgpuSettable>(
plum: TPlum,
value: SetPlumAction<ExtractPlumValue<TPlum>>,
): void;

onPlumChange<TValue>(
plum: TgpuPlum<TValue>,
listener: PlumListener<TValue>,
): Unsubscribe;

/**
* Causes all commands enqueued by pipelines to be
* submitted to the GPU.
Expand Down
2 changes: 0 additions & 2 deletions packages/typegpu/src/experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ export type { TextureProps } from '../core/texture/textureProps';
export type { Render, Sampled } from '../core/texture/usageExtension';
export type { InitOptions, InitFromDeviceOptions } from '../core/root/init';
export type { TgpuConst } from '../tgpuConstant';
export type { TgpuPlum } from '../tgpuPlumTypes';
export type { TgpuSettable } from '../settableTrait';
export type { TgpuVar } from '../tgpuVariable';
export type { TgpuSampler } from '../core/sampler/sampler';
export type { JitTranspiler } from '../jitTranspiler';
Expand Down
5 changes: 2 additions & 3 deletions packages/typegpu/src/legacyBufferApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type TgpuBuffer, createBufferImpl } from './core/buffer/buffer';
import type { AnyData } from './data/dataTypes';
import type { Infer } from './shared/repr';
import type { TgpuPlum } from './tgpuPlumTypes';

/**
* @deprecated Use the `root.createBuffer` API instead, accessible through `await tgpu.init()`
Expand All @@ -11,7 +10,7 @@ import type { TgpuPlum } from './tgpuPlumTypes';
*/
export function createBuffer<TData extends AnyData>(
typeSchema: TData,
initial?: Infer<TData> | TgpuPlum<Infer<TData>> | undefined,
initial?: Infer<TData> | undefined,
): TgpuBuffer<TData>;

/**
Expand All @@ -30,7 +29,7 @@ export function createBuffer<TData extends AnyData>(
*/
export function createBuffer<TData extends AnyData>(
typeSchema: TData,
initialOrBuffer?: Infer<TData> | TgpuPlum<Infer<TData>> | GPUBuffer,
initialOrBuffer?: Infer<TData> | GPUBuffer,
): TgpuBuffer<TData> {
return createBufferImpl(undefined, typeSchema, initialOrBuffer);
}
Loading

0 comments on commit 539e7d5

Please sign in to comment.