Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add INTERNAL_createPtr
  • Loading branch information
Aleksander Katan committed Aug 8, 2025
commit 19d6ae613f25e2fc1207c0caebcdcf1b38f3f4e9
95 changes: 56 additions & 39 deletions packages/typegpu/src/data/ptr.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,93 @@
import { $internal } from '../shared/symbols.ts';
import type { AnyData } from './dataTypes.ts';
import type { Ptr } from './wgslTypes.ts';
import type { Access, AddressSpace, Ptr } from './wgslTypes.ts';

export function ptrFn<T extends AnyData>(
inner: T,
): Ptr<'function', T, 'read-write'> {
return {
[$internal]: true,
type: 'ptr',
inner,
addressSpace: 'function',
access: 'read-write',
} as Ptr<'function', T, 'read-write'>;
return INTERNAL_createPtr('function', inner, 'read-write');
}

export function ptrPrivate<T extends AnyData>(
inner: T,
): Ptr<'private', T, 'read-write'> {
return {
[$internal]: true,
type: 'ptr',
inner,
addressSpace: 'private',
access: 'read-write',
} as Ptr<'private', T, 'read-write'>;
return INTERNAL_createPtr('private', inner, 'read-write');
}

export function ptrWorkgroup<T extends AnyData>(
inner: T,
): Ptr<'workgroup', T, 'read-write'> {
return {
[$internal]: true,
type: 'ptr',
inner,
addressSpace: 'workgroup',
access: 'read-write',
} as Ptr<'workgroup', T, 'read-write'>;
return INTERNAL_createPtr('workgroup', inner, 'read-write');
}

export function ptrStorage<
T extends AnyData,
TAccess extends 'read' | 'read-write' = 'read',
>(inner: T, access: TAccess = 'read' as TAccess): Ptr<'storage', T, TAccess> {
return {
[$internal]: true,
type: 'ptr',
inner,
addressSpace: 'storage',
access,
} as Ptr<'storage', T, TAccess>;
return INTERNAL_createPtr('storage', inner, access);
}

export function ptrUniform<T extends AnyData>(
inner: T,
): Ptr<'uniform', T, 'read'> {
return {
[$internal]: true,
type: 'ptr',
inner,
addressSpace: 'uniform',
access: 'read',
} as Ptr<'uniform', T, 'read'>;
return INTERNAL_createPtr('uniform', inner, 'read');
}

export function ptrHandle<T extends AnyData>(
inner: T,
): Ptr<'handle', T, 'read'> {
return INTERNAL_createPtr('handle', inner, 'read');
}

function INTERNAL_createPtr<
TAddressSpace extends AddressSpace,
TInner extends AnyData,
TAccess extends Access,
>(
addressSpace: TAddressSpace,
inner: TInner,
access: TAccess,
): Ptr<TAddressSpace, TInner, TAccess> {
return {
[$internal]: true,
type: 'ptr',
addressSpace,
inner,
addressSpace: 'handle',
access: 'read',
} as Ptr<'handle', T, 'read'>;
access,
} as Ptr<TAddressSpace, TInner, TAccess>;

// // In the schema call, create and return a deep copy
// // by wrapping all the values in corresponding schema calls.
// const structSchema = (instanceProps?: TProps) =>
// Object.fromEntries(
// Object.entries(props).map(([key, schema]) => [
// key,
// instanceProps
// ? schemaCloneWrapper(schema, instanceProps[key])
// : schemaDefaultWrapper(schema),
// ]),
// );

// Object.setPrototypeOf(structSchema, WgslStructImpl);
// structSchema.propTypes = props;
// Object.defineProperty(structSchema, $internal, {
// value: {
// isAbstruct,
// },
// });

// return structSchema as WgslStruct<TProps>;
}

// const WgslStructImpl = {
// type: 'struct',

// $name(label: string) {
// setName(this, label);
// return this;
// },

// toString(): string {
// return `struct:${getName(this) ?? '<unnamed>'}`;
// },
// };
Loading