Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions packages/typegpu/src/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { LooseDecorated } from './data/dataTypes.ts';
import { bool, f32, u32 } from './data/numeric.ts';
import { vec3u, vec4f } from './data/vector.ts';
import type {
AnyWgslData,
BaseData,
Bool,
Builtin,
Expand Down Expand Up @@ -62,7 +61,7 @@ export type BuiltinSubgroupId = Decorated<U32, [Builtin<'subgroup_id'>]>;
export type BuiltinNumSubgroups = Decorated<U32, [Builtin<'num_subgroups'>]>;

function defineBuiltin<T extends Decorated | LooseDecorated>(
dataType: AnyWgslData,
dataType: BaseData,
value: T['attribs'][0] extends { params: [infer TValue] } ? TValue : never,
): T {
return attribute(dataType, {
Expand Down
46 changes: 23 additions & 23 deletions packages/typegpu/src/core/buffer/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import { isWgslData } from '../../data/wgslTypes.ts';
import type { StorageFlag } from '../../extension.ts';
import type { TgpuNamable } from '../../shared/meta.ts';
import { getName, setName } from '../../shared/meta.ts';
import type {
Infer,
InferPartial,
IsValidIndexSchema,
IsValidStorageSchema,
IsValidUniformSchema,
IsValidVertexSchema,
MemIdentity,
} from '../../shared/repr.ts';
import { $internal } from '../../shared/symbols.ts';
import type { Infer, InferPartial, MemIdentity } from '../../shared/repr.ts';
import {
$internal,
type $invalidIndexSchema,
type $invalidStorageSchema,
type $invalidUniformSchema,
type $invalidVertexSchema,
} from '../../shared/symbols.ts';
import type {
Prettify,
UnionToIntersection,
Expand Down Expand Up @@ -84,15 +82,17 @@ const usageToUsageConstructor = { uniform, mutable, readonly };
/**
* Done as an object to later Prettify it
*/
type InnerValidUsagesFor<T> = {
type InnerValidUsagesFor<T extends BaseData> = {
usage:
| (IsValidStorageSchema<T> extends true ? 'storage' : never)
| (IsValidUniformSchema<T> extends true ? 'uniform' : never)
| (IsValidVertexSchema<T> extends true ? 'vertex' : never)
| (IsValidIndexSchema<T> extends true ? 'index' : never);
| (T[typeof $invalidStorageSchema] extends string ? never : 'storage')
| (T[typeof $invalidUniformSchema] extends string ? never : 'uniform')
| (T[typeof $invalidVertexSchema] extends string ? never : 'vertex')
| (T[typeof $invalidIndexSchema] extends string ? never : 'index');
};

export type ValidUsagesFor<T> = InnerValidUsagesFor<T>['usage'];
export type ValidUsagesFor<T extends BaseData> = InnerValidUsagesFor<
T
>['usage'];

export interface TgpuBuffer<TData extends BaseData> extends TgpuNamable {
readonly [$internal]: true;
Expand Down Expand Up @@ -144,30 +144,30 @@ export function INTERNAL_createBuffer<TData extends AnyData>(
return new TgpuBufferImpl(group, typeSchema, initialOrBuffer);
}

export function isBuffer<T extends TgpuBuffer<AnyData>>(
export function isBuffer<T extends TgpuBuffer<BaseData>>(
value: T | unknown,
): value is T {
return (value as TgpuBuffer<AnyData>).resourceType === 'buffer';
return (value as T).resourceType === 'buffer';
}

export function isUsableAsVertex<T extends TgpuBuffer<AnyData>>(
export function isUsableAsVertex<T extends TgpuBuffer<BaseData>>(
buffer: T,
): buffer is T & VertexFlag {
return !!(buffer as unknown as VertexFlag).usableAsVertex;
return !!(buffer as T).usableAsVertex;
}

export function isUsableAsIndex<T extends TgpuBuffer<AnyData>>(
export function isUsableAsIndex<T extends TgpuBuffer<BaseData>>(
buffer: T,
): buffer is T & IndexFlag {
return !!(buffer as unknown as IndexFlag).usableAsIndex;
return !!(buffer as T).usableAsIndex;
}

// --------------
// Implementation
// --------------
const endianness = getSystemEndianness();

class TgpuBufferImpl<TData extends AnyData> implements TgpuBuffer<TData> {
class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
public readonly [$internal] = true;
public readonly resourceType = 'buffer';
public flags: GPUBufferUsageFlags = GPUBufferUsage.COPY_DST |
Expand Down
21 changes: 10 additions & 11 deletions packages/typegpu/src/core/buffer/bufferUsage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { AnyData } from '../../data/dataTypes.ts';
import { schemaCallWrapper } from '../../data/schemaCallWrapper.ts';
import { type ResolvedSnippet, snip } from '../../data/snippet.ts';
import {
Expand Down Expand Up @@ -80,10 +79,10 @@ export interface TgpuFixedBufferUsage<TData extends BaseData>
export interface TgpuBufferMutable<TData extends BaseData>
extends TgpuBufferUsage<TData, 'mutable'> {}

export function isUsableAsUniform<T extends TgpuBuffer<AnyData>>(
export function isUsableAsUniform<T extends TgpuBuffer<BaseData>>(
buffer: T,
): buffer is T & UniformFlag {
return !!(buffer as unknown as UniformFlag).usableAsUniform;
return !!(buffer as T).usableAsUniform;
}

// --------------
Expand All @@ -97,7 +96,7 @@ const usageToVarTemplateMap: Record<BindableBufferUsage, string> = {
};

class TgpuFixedBufferImpl<
TData extends AnyWgslData,
TData extends BaseData,
TUsage extends BindableBufferUsage,
> implements
TgpuBufferUsage<TData, TUsage>,
Expand Down Expand Up @@ -237,7 +236,7 @@ class TgpuFixedBufferImpl<
}

export class TgpuLaidOutBufferImpl<
TData extends AnyWgslData,
TData extends BaseData,
TUsage extends BindableBufferUsage,
> implements TgpuBufferUsage<TData, TUsage>, SelfResolvable {
/** Type-token, not available at runtime */
Expand Down Expand Up @@ -312,8 +311,8 @@ export class TgpuLaidOutBufferImpl<
}

const mutableUsageMap = new WeakMap<
TgpuBuffer<AnyWgslData>,
TgpuFixedBufferImpl<AnyWgslData, 'mutable'>
TgpuBuffer<BaseData>,
TgpuFixedBufferImpl<BaseData, 'mutable'>
>();

export function mutable<TData extends AnyWgslData>(
Expand All @@ -336,8 +335,8 @@ export function mutable<TData extends AnyWgslData>(
}

const readonlyUsageMap = new WeakMap<
TgpuBuffer<AnyWgslData>,
TgpuFixedBufferImpl<AnyWgslData, 'readonly'>
TgpuBuffer<BaseData>,
TgpuFixedBufferImpl<BaseData, 'readonly'>
>();

export function readonly<TData extends AnyWgslData>(
Expand All @@ -360,8 +359,8 @@ export function readonly<TData extends AnyWgslData>(
}

const uniformUsageMap = new WeakMap<
TgpuBuffer<AnyWgslData>,
TgpuFixedBufferImpl<AnyWgslData, 'uniform'>
TgpuBuffer<BaseData>,
TgpuFixedBufferImpl<BaseData, 'uniform'>
>();

export function uniform<TData extends AnyWgslData>(
Expand Down
6 changes: 3 additions & 3 deletions packages/typegpu/src/core/constant/tgpuConstant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AnyData } from '../../data/dataTypes.ts';
import { type ResolvedSnippet, snip } from '../../data/snippet.ts';
import { isNaturallyEphemeral } from '../../data/wgslTypes.ts';
import { type BaseData, isNaturallyEphemeral } from '../../data/wgslTypes.ts';
import { inCodegenMode } from '../../execMode.ts';
import type { TgpuNamable } from '../../shared/meta.ts';
import { getName, setName } from '../../shared/meta.ts';
Expand All @@ -24,7 +24,7 @@ type DeepReadonly<T> = T extends { [$internal]: unknown } ? T
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
: T;

export interface TgpuConst<TDataType extends AnyData = AnyData>
export interface TgpuConst<TDataType extends BaseData = BaseData>
extends TgpuNamable {
readonly resourceType: 'const';
readonly [$gpuValueOf]: DeepReadonly<InferGPU<TDataType>>;
Expand Down Expand Up @@ -71,7 +71,7 @@ function deepFreeze<T extends object>(object: T): T {
return Object.freeze(object);
}

class TgpuConstImpl<TDataType extends AnyData>
class TgpuConstImpl<TDataType extends BaseData>
implements TgpuConst<TDataType>, SelfResolvable {
readonly [$internal] = {};
readonly resourceType: 'const';
Expand Down
10 changes: 5 additions & 5 deletions packages/typegpu/src/core/function/dualImpl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { AnyData } from '../../data/dataTypes.ts';
import { type MapValueToSnippet, snip } from '../../data/snippet.ts';
import { setName } from '../../shared/meta.ts';
import { $gpuCallable } from '../../shared/symbols.ts';
Expand All @@ -9,8 +8,9 @@ import {
NormalState,
type ResolutionCtx,
} from '../../types.ts';
import { type BaseData, isPtr } from '../../data/wgslTypes.ts';

type MapValueToDataType<T> = { [K in keyof T]: AnyData };
type MapValueToDataType<T> = { [K in keyof T]: BaseData };
type AnyFn = (...args: never[]) => unknown;

interface DualImplOptions<T extends AnyFn> {
Expand All @@ -21,10 +21,10 @@ interface DualImplOptions<T extends AnyFn> {
args: MapValueToSnippet<Parameters<T>>,
) => string;
readonly signature:
| { argTypes: AnyData[]; returnType: AnyData }
| { argTypes: BaseData[]; returnType: BaseData }
| ((
...inArgTypes: MapValueToDataType<Parameters<T>>
) => { argTypes: AnyData[]; returnType: AnyData });
) => { argTypes: BaseData[]; returnType: BaseData });
/**
* Whether the function should skip trying to execute the "normal" implementation if
* all arguments are known at compile time.
Expand Down Expand Up @@ -64,7 +64,7 @@ export function dualImpl<T extends AnyFn>(
? options.signature(
...args.map((s) => {
// Dereference implicit pointers
if (s.dataType.type === 'ptr' && s.dataType.implicit) {
if (isPtr(s.dataType) && s.dataType.implicit) {
return s.dataType.inner;
}
return s.dataType;
Expand Down
17 changes: 11 additions & 6 deletions packages/typegpu/src/core/function/fnCore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { getAttributesString } from '../../data/attributes.ts';
import { type AnyData, undecorate } from '../../data/dataTypes.ts';
import { undecorate } from '../../data/dataTypes.ts';
import { type ResolvedSnippet, snip } from '../../data/snippet.ts';
import { isWgslData, isWgslStruct, Void } from '../../data/wgslTypes.ts';
import {
type BaseData,
isWgslData,
isWgslStruct,
Void,
} from '../../data/wgslTypes.ts';
import { MissingLinksError } from '../../errors.ts';
import { getMetaData, getName, setName } from '../../shared/meta.ts';
import type { ResolutionCtx } from '../../types.ts';
Expand All @@ -17,12 +22,12 @@ export interface FnCore {
applyExternals(newExternals: ExternalMap): void;
resolve(
ctx: ResolutionCtx,
argTypes: AnyData[],
argTypes: BaseData[],
/**
* The return type of the function. If undefined, the type should be inferred
* from the implementation (relevant for shellless functions).
*/
returnType: AnyData | undefined,
returnType: BaseData | undefined,
): ResolvedSnippet;
}

Expand All @@ -45,8 +50,8 @@ export function createFnCore(

resolve(
ctx: ResolutionCtx,
argTypes: AnyData[],
returnType: AnyData | undefined,
argTypes: BaseData[],
returnType: BaseData | undefined,
): ResolvedSnippet {
const externalMap: ExternalMap = {};

Expand Down
6 changes: 3 additions & 3 deletions packages/typegpu/src/core/function/shelllessImpl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AnyData } from '../../data/dataTypes.ts';
import type { ResolvedSnippet } from '../../data/snippet.ts';
import type { BaseData } from '../../data/wgslTypes.ts';
import { getName } from '../../shared/meta.ts';
import { $getNameForward, $internal, $resolve } from '../../shared/symbols.ts';
import type { ResolutionCtx, SelfResolvable } from '../../types.ts';
Expand All @@ -23,12 +23,12 @@ import { createFnCore } from './fnCore.ts';
*/
export interface ShelllessImpl extends SelfResolvable {
readonly resourceType: 'shellless-impl';
readonly argTypes: AnyData[];
readonly argTypes: BaseData[];
readonly [$getNameForward]: unknown;
}

export function createShelllessImpl(
argTypes: AnyData[],
argTypes: BaseData[],
implementation: (...args: never[]) => unknown,
): ShelllessImpl {
const core = createFnCore(implementation, '');
Expand Down
24 changes: 12 additions & 12 deletions packages/typegpu/src/core/function/tgpuFn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { AnyData } from '../../data/dataTypes.ts';
import type { ResolvedSnippet } from '../../data/snippet.ts';
import { schemaCallWrapper } from '../../data/schemaCallWrapper.ts';
import { Void } from '../../data/wgslTypes.ts';
import { type BaseData, Void } from '../../data/wgslTypes.ts';
import { ExecutionError } from '../../errors.ts';
import { provideInsideTgpuFn } from '../../execMode.ts';
import type { TgpuNamable } from '../../shared/meta.ts';
Expand Down Expand Up @@ -44,6 +43,7 @@ import type {
} from './fnTypes.ts';
import { stripTemplate } from './templateUtils.ts';
import { comptime } from './comptime.ts';
import type { AnyData } from '../../data/dataTypes.ts';

// ----------
// Public API
Expand All @@ -53,8 +53,8 @@ import { comptime } from './comptime.ts';
* Describes a function signature (its arguments and return type)
*/
type TgpuFnShellHeader<
Args extends AnyData[],
Return extends AnyData,
Args extends BaseData[],
Return extends BaseData,
> = {
readonly [$internal]: true;
readonly argTypes: Args;
Expand All @@ -68,8 +68,8 @@ type TgpuFnShellHeader<
* and passing the implementation (as WGSL string or JS function) as the argument.
*/
export type TgpuFnShell<
Args extends AnyData[],
Return extends AnyData,
Args extends BaseData[],
Return extends BaseData,
> =
& TgpuFnShellHeader<Args, Return>
& (<T extends (...args: InferArgs<Args>) => Infer<Return>>(
Expand All @@ -90,17 +90,17 @@ interface TgpuFnBase<ImplSchema extends AnyFn> extends TgpuNamable {
readonly resourceType: 'function';
readonly shell: TgpuFnShellHeader<
Parameters<ImplSchema>,
Extract<ReturnType<ImplSchema>, AnyData>
Extract<ReturnType<ImplSchema>, BaseData>
>;
readonly [$providing]?: Providing | undefined;

$uses(dependencyMap: Record<string, unknown>): this;
with<T>(slot: TgpuSlot<T>, value: Eventual<T>): TgpuFn<ImplSchema>;
with<T extends AnyData>(
with<T extends BaseData>(
accessor: TgpuAccessor<T>,
value: AccessorIn<NoInfer<T>>,
): TgpuFn<ImplSchema>;
with<T extends AnyData>(
with<T extends BaseData>(
accessor: TgpuMutableAccessor<T>,
value: MutableAccessorIn<NoInfer<T>>,
): TgpuFn<ImplSchema>;
Expand Down Expand Up @@ -143,7 +143,7 @@ export function fn<
return Object.assign(call, shell) as unknown as TgpuFnShell<Args, Return>;
}

export function isTgpuFn<Args extends AnyData[] | [], Return extends AnyData>(
export function isTgpuFn<Args extends BaseData[] | [], Return extends BaseData>(
value: unknown | TgpuFn<(...args: Args) => Return>,
): value is TgpuFn<(...args: Args) => Return> {
return isMarkedInternal(value) &&
Expand All @@ -161,7 +161,7 @@ function stringifyPair([slot, value]: SlotValuePair): string {
function createFn<ImplSchema extends AnyFn>(
shell: TgpuFnShellHeader<
Parameters<ImplSchema>,
Extract<ReturnType<ImplSchema>, AnyData>
Extract<ReturnType<ImplSchema>, BaseData>
>,
implementation: Implementation<ImplSchema>,
): TgpuFn<ImplSchema> {
Expand Down Expand Up @@ -227,7 +227,7 @@ function createFn<ImplSchema extends AnyFn>(
}

const castAndCopiedArgs = args.map((arg, index) =>
schemaCallWrapper(shell.argTypes[index] as unknown as AnyData, arg)
schemaCallWrapper(shell.argTypes[index] as unknown as BaseData, arg)
) as InferArgs<Parameters<ImplSchema>>;

const result = implementation(...castAndCopiedArgs);
Expand Down
Loading
Loading