Skip to content
Merged
Show file tree
Hide file tree
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
Change pointer inner type to StorableData
  • Loading branch information
Aleksander Katan committed Aug 11, 2025
commit 924a4d1bf4257aa0bb77ce4178bd7e715d1b61ea
1 change: 1 addition & 0 deletions packages/typegpu/src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type {
Mat4x4f,
Ptr,
Size,
StorableData,
U16,
U32,
v2b,
Expand Down
52 changes: 8 additions & 44 deletions packages/typegpu/src/data/ptr.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
import { $internal } from '../shared/symbols.ts';
import type { AnyData } from './dataTypes.ts';
import type { Access, AddressSpace, Ptr } from './wgslTypes.ts';
import type { Access, AddressSpace, Ptr, StorableData } from './wgslTypes.ts';

export function ptrFn<T extends AnyData>(
export function ptrFn<T extends StorableData>(
inner: T,
): Ptr<'function', T, 'read-write'> {
return INTERNAL_createPtr('function', inner, 'read-write');
}

export function ptrPrivate<T extends AnyData>(
export function ptrPrivate<T extends StorableData>(
inner: T,
): Ptr<'private', T, 'read-write'> {
return INTERNAL_createPtr('private', inner, 'read-write');
}

export function ptrWorkgroup<T extends AnyData>(
export function ptrWorkgroup<T extends StorableData>(
inner: T,
): Ptr<'workgroup', T, 'read-write'> {
return INTERNAL_createPtr('workgroup', inner, 'read-write');
}

export function ptrStorage<
T extends AnyData,
T extends StorableData,
TAccess extends 'read' | 'read-write' = 'read',
>(inner: T, access: TAccess = 'read' as TAccess): Ptr<'storage', T, TAccess> {
return INTERNAL_createPtr('storage', inner, access);
}

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

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

function INTERNAL_createPtr<
TAddressSpace extends AddressSpace,
TInner extends AnyData,
TInner extends StorableData,
TAccess extends Access,
>(
addressSpace: TAddressSpace,
Expand All @@ -55,39 +54,4 @@ function INTERNAL_createPtr<
inner,
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>'}`;
// },
// };
8 changes: 6 additions & 2 deletions packages/typegpu/src/data/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import type { AnyData } from './index.ts';
import { type AnyData, isPtr } from './index.ts';
import { formatToWGSLType } from './vertexFormatData.ts';

/**
* A wrapper for `schema(item)` or `schema()` call.
* A wrapper for `schema(item)` or `schema()` call on JS side.
* If the schema is a pointer, returns the value pointed to without copying.
* If the schema is a TgpuVertexFormatData, it instead calls the corresponding constructible schema.
* Throws an error if the schema is not callable.
*/
export function schemaCallWrapper<T>(schema: AnyData, item?: T): T {
const maybeType = (schema as { type: string })?.type;

try {
if (item !== undefined && isPtr(schema)) {
return item;
}
// TgpuVertexFormatData are not callable
const callSchema = (maybeType in formatToWGSLType
? formatToWGSLType[maybeType as keyof typeof formatToWGSLType]
Expand Down
34 changes: 33 additions & 1 deletion packages/typegpu/src/data/wgslTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ export type Access = 'read' | 'write' | 'read-write';

export interface Ptr<
TAddr extends AddressSpace = AddressSpace,
TInner extends BaseData = BaseData, // can also be sampler or texture (╯'□')╯︵ ┻━┻
TInner extends StorableData = StorableData, // can also be sampler or texture (╯'□')╯︵ ┻━┻
TAccess extends Access = Access,
> extends BaseData {
readonly type: 'ptr';
Expand Down Expand Up @@ -1521,6 +1521,38 @@ export type ScalarData =
| AbstractInt
| AbstractFloat;

export type VecData =
| Vec2f
| Vec2h
| Vec2i
| Vec2u
| Vec2b
| Vec3f
| Vec3h
| Vec3i
| Vec3u
| Vec3b
| Vec4f
| Vec4h
| Vec4i
| Vec4u
| Vec4b;

export type MatData =
| Mat2x2f
| Mat3x3f
| Mat4x4f;

export type StorableData =
| ScalarData
| VecData
| MatData
| Atomic
| WgslArray
| WgslStruct;
// | Texture
// | Sampler

export type AnyWgslData =
| Bool
| F32
Expand Down
8 changes: 4 additions & 4 deletions packages/typegpu/src/std/array.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { snip } from '../data/snippet.ts';
import { createDualImpl } from '../core/function/dualImpl.ts';
import { abstractInt, u32 } from '../data/numeric.ts';
import { ptrFn } from '../data/ptr.ts';
import type { AnyWgslData } from '../data/wgslTypes.ts';
import { snip } from '../data/snippet.ts';
import type { ScalarData } from '../data/wgslTypes.ts';
import { isPtr, isWgslArray } from '../data/wgslTypes.ts';
import { createDualImpl } from '../core/function/dualImpl.ts';

export const arrayLength = createDualImpl(
// CPU implementation
Expand All @@ -19,5 +19,5 @@ export const arrayLength = createDualImpl(
return snip(`arrayLength(${a.value})`, u32);
},
'arrayLength',
(a) => [ptrFn(a.dataType as AnyWgslData)],
(a) => [ptrFn(a.dataType as ScalarData)],
);