typegpu
Version:
A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.
296 lines (295 loc) • 14.4 kB
TypeScript
import type { StorageTextureFormats } from '../core/texture/textureFormats.ts';
import { $internal, $repr } from '../shared/symbols.ts';
import type { WithDefaults } from '../shared/utilityTypes.ts';
import type { F32 } from './wgslTypes.ts';
import type { BaseData, TextureSampleTypes } from './wgslTypes.ts';
export type StorageTextureDimension = '1d' | '2d' | '2d-array' | '3d';
export type WgslTextureProps = {
dimension: GPUTextureViewDimension;
sampleType: TextureSampleTypes;
multisampled: boolean;
};
export type WgslStorageTextureProps = {
dimension: StorageTextureDimension;
format: StorageTextureFormats;
access: GPUStorageTextureAccess;
};
type ResolvedTextureProps<TProps extends Partial<WgslTextureProps>> = WithDefaults<TProps, WgslTextureProps>;
type ResolvedStorageTextureProps<TProps extends Partial<WgslStorageTextureProps>> = WithDefaults<TProps, WgslStorageTextureProps>;
type SampledTextureLiteral = `texture_${'1d' | '2d' | '2d_array' | '3d' | 'cube' | 'cube_array' | 'multisampled_2d' | 'depth_multisampled_2d' | 'depth_2d' | 'depth_2d_array' | 'depth_cube' | 'depth_cube_array'}`;
type StorageTextureLiteral = `texture_storage_${'1d' | '2d' | '2d_array' | '3d'}`;
export interface WgslTexture<TProps extends Partial<WgslTextureProps> = WgslTextureProps> extends BaseData {
readonly [$repr]: unknown;
readonly type: SampledTextureLiteral;
readonly sampleType: ResolvedTextureProps<TProps>['sampleType'];
readonly dimension: ResolvedTextureProps<TProps>['dimension'];
readonly multisampled: ResolvedTextureProps<TProps>['multisampled'];
readonly bindingSampleType: [GPUTextureSampleType, ...GPUTextureSampleType[]];
}
export interface WgslStorageTexture<TProps extends Partial<WgslStorageTextureProps> = WgslStorageTextureProps> extends BaseData {
readonly [$repr]: unknown;
readonly type: StorageTextureLiteral;
readonly format: ResolvedStorageTextureProps<TProps>['format'];
readonly dimension: ResolvedStorageTextureProps<TProps>['dimension'];
readonly access: ResolvedStorageTextureProps<TProps>['access'];
}
export interface WgslExternalTexture extends BaseData {
readonly [$repr]: textureExternal;
readonly type: 'texture_external';
readonly dimension: '2d';
}
export interface WgslTexture1d<TSample extends TextureSampleTypes = TextureSampleTypes> extends WgslTexture<{
dimension: '1d';
sampleType: TSample;
multisampled: false;
}> {
readonly type: 'texture_1d';
readonly [$repr]: texture1d<TSample>;
}
export interface WgslTexture2d<TSample extends TextureSampleTypes = TextureSampleTypes> extends WgslTexture<{
dimension: '2d';
sampleType: TSample;
multisampled: false;
}> {
readonly type: 'texture_2d';
readonly [$repr]: texture2d<TSample>;
}
export interface WgslTextureMultisampled2d<TSample extends TextureSampleTypes = TextureSampleTypes> extends WgslTexture<{
dimension: '2d';
sampleType: TSample;
multisampled: true;
}> {
readonly type: 'texture_multisampled_2d';
readonly [$repr]: textureMultisampled2d<TSample>;
}
export interface WgslTexture2dArray<TSample extends TextureSampleTypes = TextureSampleTypes> extends WgslTexture<{
dimension: '2d-array';
sampleType: TSample;
multisampled: false;
}> {
readonly type: 'texture_2d_array';
readonly [$repr]: texture2dArray<TSample>;
}
export interface WgslTextureCube<TSample extends TextureSampleTypes = TextureSampleTypes> extends WgslTexture<{
dimension: 'cube';
sampleType: TSample;
multisampled: false;
}> {
readonly type: 'texture_cube';
readonly [$repr]: textureCube<TSample>;
}
export interface WgslTextureCubeArray<TSample extends TextureSampleTypes = TextureSampleTypes> extends WgslTexture<{
dimension: 'cube-array';
sampleType: TSample;
multisampled: false;
}> {
readonly type: 'texture_cube_array';
readonly [$repr]: textureCubeArray<TSample>;
}
export interface WgslTexture3d<TSample extends TextureSampleTypes = TextureSampleTypes> extends WgslTexture<{
dimension: '3d';
sampleType: TSample;
multisampled: false;
}> {
readonly type: 'texture_3d';
readonly [$repr]: texture3d<TSample>;
}
export interface WgslTextureDepth2d extends WgslTexture<{
dimension: '2d';
sampleType: F32;
multisampled: false;
}> {
readonly type: 'texture_depth_2d';
readonly [$repr]: textureDepth2d;
}
export interface WgslTextureDepthMultisampled2d extends WgslTexture<{
dimension: '2d';
sampleType: F32;
multisampled: true;
}> {
readonly type: 'texture_depth_multisampled_2d';
readonly [$repr]: textureDepthMultisampled2d;
}
export interface WgslTextureDepth2dArray extends WgslTexture<{
dimension: '2d-array';
sampleType: F32;
multisampled: false;
}> {
readonly type: 'texture_depth_2d_array';
readonly [$repr]: textureDepth2dArray;
}
export interface WgslTextureDepthCube extends WgslTexture<{
dimension: 'cube';
sampleType: F32;
multisampled: false;
}> {
readonly type: 'texture_depth_cube';
readonly [$repr]: textureDepthCube;
}
export interface WgslTextureDepthCubeArray extends WgslTexture<{
dimension: 'cube-array';
sampleType: F32;
multisampled: false;
}> {
readonly type: 'texture_depth_cube_array';
readonly [$repr]: textureDepthCubeArray;
}
export interface WgslStorageTexture1d<TFormat extends StorageTextureFormats = StorageTextureFormats, TAccess extends GPUStorageTextureAccess = GPUStorageTextureAccess> extends WgslStorageTexture<{
dimension: '1d';
format: TFormat;
access: TAccess;
}> {
readonly type: 'texture_storage_1d';
readonly [$repr]: textureStorage1d<TFormat, TAccess>;
}
export interface WgslStorageTexture2d<TFormat extends StorageTextureFormats = StorageTextureFormats, TAccess extends GPUStorageTextureAccess = GPUStorageTextureAccess> extends WgslStorageTexture<{
dimension: '2d';
format: TFormat;
access: TAccess;
}> {
readonly type: 'texture_storage_2d';
readonly [$repr]: textureStorage2d<TFormat, TAccess>;
}
export interface WgslStorageTexture2dArray<TFormat extends StorageTextureFormats = StorageTextureFormats, TAccess extends GPUStorageTextureAccess = GPUStorageTextureAccess> extends WgslStorageTexture<{
dimension: '2d-array';
format: TFormat;
access: TAccess;
}> {
readonly type: 'texture_storage_2d_array';
readonly [$repr]: textureStorage2dArray<TFormat, TAccess>;
}
export interface WgslStorageTexture3d<TFormat extends StorageTextureFormats = StorageTextureFormats, TAccess extends GPUStorageTextureAccess = GPUStorageTextureAccess> extends WgslStorageTexture<{
dimension: '3d';
format: TFormat;
access: TAccess;
}> {
readonly type: 'texture_storage_3d';
readonly [$repr]: textureStorage3d<TFormat, TAccess>;
}
type SampledTextureSchemaMap<T extends WgslTextureProps> = {
multisampled: {
'1d': never;
'2d': WgslTextureMultisampled2d<T['sampleType']>;
'2d-array': never;
'3d': never;
cube: never;
'cube-array': never;
};
sampled: {
'1d': WgslTexture1d<T['sampleType']>;
'2d': WgslTexture2d<T['sampleType']>;
'2d-array': WgslTexture2dArray<T['sampleType']>;
'3d': WgslTexture3d<T['sampleType']>;
cube: WgslTextureCube<T['sampleType']>;
'cube-array': WgslTextureCubeArray<T['sampleType']>;
};
};
type StorageTextureSchemaMap<T extends WgslStorageTextureProps> = {
'1d': WgslStorageTexture1d<T['format'], T['access']>;
'2d': WgslStorageTexture2d<T['format'], T['access']>;
'2d-array': WgslStorageTexture2dArray<T['format'], T['access']>;
'3d': WgslStorageTexture3d<T['format'], T['access']>;
};
export type TextureSchemaForDescriptor<T extends WgslTextureProps | WgslStorageTextureProps> = T extends WgslTextureProps ? T['multisampled'] extends true ? SampledTextureSchemaMap<T>['multisampled'][T['dimension']] : SampledTextureSchemaMap<T>['sampled'][T['dimension']] : T extends WgslStorageTextureProps ? StorageTextureSchemaMap<T>[T['dimension']] : never;
export declare function textureDescriptorToSchema<T extends WgslTextureProps | WgslStorageTextureProps>(desc: T): TextureSchemaForDescriptor<T>;
export declare const accessModeMap: Record<GPUStorageTextureAccess, string>;
export interface texture1d<T extends TextureSampleTypes = TextureSampleTypes> {
readonly kind: `texture_1d<${T['type']}>`;
[$internal]: T;
}
export declare function texture1d<T extends TextureSampleTypes>(sampleType: T): WgslTexture1d<T>;
export declare function texture1d(): WgslTexture1d<F32>;
export interface texture2d<T extends TextureSampleTypes = TextureSampleTypes> {
readonly kind: `texture_2d<${T['type']}>`;
[$internal]: T;
}
export declare function texture2d<T extends TextureSampleTypes>(sampleType: T): WgslTexture2d<T>;
export declare function texture2d(): WgslTexture2d<F32>;
export interface textureMultisampled2d<T extends TextureSampleTypes = TextureSampleTypes> {
readonly kind: `texture_multisampled_2d<${T['type']}>`;
[$internal]: T;
}
export declare function textureMultisampled2d<T extends TextureSampleTypes>(sampleType: T): WgslTextureMultisampled2d<T>;
export declare function textureMultisampled2d(): WgslTextureMultisampled2d<F32>;
export interface texture2dArray<T extends TextureSampleTypes = TextureSampleTypes> {
readonly kind: `texture_2d_array<${T['type']}>`;
[$internal]: T;
}
export declare function texture2dArray<T extends TextureSampleTypes>(sampleType: T): WgslTexture2dArray<T>;
export declare function texture2dArray(): WgslTexture2dArray<F32>;
export interface textureCube<T extends TextureSampleTypes = TextureSampleTypes> {
readonly kind: `texture_cube<${T['type']}>`;
[$internal]: T;
}
export declare function textureCube<T extends TextureSampleTypes>(sampleType: T): WgslTextureCube<T>;
export declare function textureCube(): WgslTextureCube<F32>;
export interface textureCubeArray<T extends TextureSampleTypes = TextureSampleTypes> {
readonly kind: `texture_cube_array<${T['type']}>`;
[$internal]: T;
}
export declare function textureCubeArray<T extends TextureSampleTypes>(sampleType: T): WgslTextureCubeArray<T>;
export declare function textureCubeArray(): WgslTextureCubeArray<F32>;
export interface texture3d<T extends TextureSampleTypes = TextureSampleTypes> {
readonly kind: `texture_3d<${T['type']}>`;
[$internal]: T;
}
export declare function texture3d<T extends TextureSampleTypes>(sampleType: T): WgslTexture3d<T>;
export declare function texture3d(): WgslTexture3d<F32>;
export interface textureStorage1d<TFormat extends StorageTextureFormats = StorageTextureFormats, TAccess extends GPUStorageTextureAccess = GPUStorageTextureAccess> {
readonly kind: `texture_storage_1d<${TFormat}, ${TAccess}>`;
[$internal]: [TFormat, TAccess];
}
export declare function textureStorage1d<TFormat extends StorageTextureFormats, TAccess extends GPUStorageTextureAccess>(format: TFormat, access: TAccess): WgslStorageTexture1d<TFormat, TAccess>;
export declare function textureStorage1d<TFormat extends StorageTextureFormats>(format: TFormat): WgslStorageTexture1d<TFormat, 'write-only'>;
export interface textureStorage2d<TFormat extends StorageTextureFormats = StorageTextureFormats, TAccess extends GPUStorageTextureAccess = GPUStorageTextureAccess> {
readonly kind: `texture_storage_2d<${TFormat}, ${TAccess}>`;
[$internal]: [TFormat, TAccess];
}
export declare function textureStorage2d<TFormat extends StorageTextureFormats, TAccess extends GPUStorageTextureAccess>(format: TFormat, access: TAccess): WgslStorageTexture2d<TFormat, TAccess>;
export declare function textureStorage2d<TFormat extends StorageTextureFormats>(format: TFormat): WgslStorageTexture2d<TFormat, 'write-only'>;
export interface textureStorage2dArray<TFormat extends StorageTextureFormats = StorageTextureFormats, TAccess extends GPUStorageTextureAccess = GPUStorageTextureAccess> {
readonly kind: `texture_storage_2d_array<${TFormat}, ${TAccess}>`;
[$internal]: [TFormat, TAccess];
}
export declare function textureStorage2dArray<TFormat extends StorageTextureFormats, TAccess extends GPUStorageTextureAccess>(format: TFormat, access: TAccess): WgslStorageTexture2dArray<TFormat, TAccess>;
export declare function textureStorage2dArray<TFormat extends StorageTextureFormats>(format: TFormat): WgslStorageTexture2dArray<TFormat, 'write-only'>;
export interface textureStorage3d<TFormat extends StorageTextureFormats = StorageTextureFormats, TAccess extends GPUStorageTextureAccess = GPUStorageTextureAccess> {
readonly kind: `texture_storage_3d<${TFormat}, ${TAccess}>`;
[$internal]: [TFormat, TAccess];
}
export declare function textureStorage3d<TFormat extends StorageTextureFormats, TAccess extends GPUStorageTextureAccess>(format: TFormat, access: TAccess): WgslStorageTexture3d<TFormat, TAccess>;
export declare function textureStorage3d<TFormat extends StorageTextureFormats>(format: TFormat): WgslStorageTexture3d<TFormat, 'write-only'>;
export interface textureDepth2d {
readonly kind: 'texture_depth_2d';
[$internal]: F32;
}
export declare function textureDepth2d(): WgslTextureDepth2d;
export interface textureDepthMultisampled2d {
readonly kind: 'texture_depth_multisampled_2d';
[$internal]: F32;
}
export declare function textureDepthMultisampled2d(): WgslTextureDepthMultisampled2d;
export interface textureDepth2dArray {
readonly kind: 'texture_depth_2d_array';
[$internal]: F32;
}
export declare function textureDepth2dArray(): WgslTextureDepth2dArray;
export interface textureDepthCube {
readonly kind: 'texture_depth_cube';
[$internal]: F32;
}
export declare function textureDepthCube(): WgslTextureDepthCube;
export interface textureDepthCubeArray {
readonly kind: 'texture_depth_cube_array';
[$internal]: F32;
}
export declare function textureDepthCubeArray(): WgslTextureDepthCubeArray;
export interface textureExternal {
readonly kind: 'texture_external';
[$internal]: true;
}
export declare function textureExternal(): WgslExternalTexture;
export declare function isWgslTexture(value: unknown): value is WgslTexture;
export declare function isWgslStorageTexture(value: unknown): value is WgslStorageTexture;
export declare function isWgslExternalTexture(value: unknown): value is WgslExternalTexture;
export {};