Skip to content

Commit d3e6395

Browse files
committed
chore: Shaderbits
1 parent e1cd420 commit d3e6395

3 files changed

Lines changed: 73 additions & 4 deletions

File tree

packages/typegpu/src/data/snippet.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { undecorate } from './dataTypes.ts';
22
import type { UnknownData } from './dataTypes.ts';
33
import { DEV } from '../shared/env.ts';
44
import { type BaseData, isNumericSchema } from './wgslTypes.ts';
5+
import { reduceIfCompound, type TypeBit } from '../shaderbit/typeBits.ts';
56

67
export type Origin =
78
| 'uniform'
@@ -88,10 +89,10 @@ export type MapValueToSnippet<T> = { [K in keyof T]: Snippet };
8889

8990
class SnippetImpl implements Snippet {
9091
readonly value: unknown;
91-
readonly dataType: BaseData | UnknownData;
92+
readonly dataType: TypeBit | UnknownData;
9293
readonly origin: Origin;
9394

94-
constructor(value: unknown, dataType: BaseData | UnknownData, origin: Origin) {
95+
constructor(value: unknown, dataType: TypeBit | UnknownData, origin: Origin) {
9596
this.value = value;
9697
this.dataType = dataType;
9798
this.origin = origin;
@@ -121,7 +122,7 @@ export function snip(
121122
return new SnippetImpl(
122123
value,
123124
// We don't care about attributes in snippet land, so we discard that information.
124-
undecorate(dataType as BaseData),
125+
reduceIfCompound(dataType as BaseData),
125126
origin,
126127
);
127128
}

packages/typegpu/src/data/wgslTypes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ import type { WgslExternalTexture, WgslStorageTexture, WgslTexture } from './tex
3939
import type { WgslComparisonSampler, WgslSampler } from './sampler.ts';
4040
import type { _ref as ref } from './ref.ts';
4141
import type { DualFn } from '../types.ts';
42+
import type { CompoundBit, TypeBit } from '../shaderbit/typeBits.ts';
4243

4344
type DecoratedLocation<T extends BaseData> = Decorated<T, Location[]>;
4445

45-
export interface BaseData {
46+
export interface BaseData extends CompoundBit<TypeBit> {
4647
readonly [$internal]: Record<string, unknown>;
4748
readonly type: string;
4849
readonly [$repr]: unknown;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
export interface CompoundBit<T> {
2+
'~shaderbit:wgsl': {
3+
reduce(): T;
4+
};
5+
}
6+
7+
export interface AttributeBit {
8+
name: string;
9+
params: (string | number)[][];
10+
}
11+
12+
export interface StructPropertyBit<
13+
TType extends TypeBit = TypeBit,
14+
TAttribs extends AttributeBit[] = AttributeBit[],
15+
> {
16+
type: TType;
17+
attribs: TAttribs;
18+
}
19+
20+
export interface StructBit {
21+
kind: 'struct';
22+
fields: Record<string, StructPropertyBit>;
23+
}
24+
25+
export interface ArrayBit {
26+
kind: 'array';
27+
elem: TypeBit;
28+
count: number;
29+
}
30+
31+
export type TypeBit =
32+
| 'bool'
33+
| 'f32'
34+
| 'f16'
35+
| 'i32'
36+
| 'u32'
37+
| 'vec2f'
38+
| 'vec2h'
39+
| 'vec2i'
40+
| 'vec2u'
41+
| 'vec2b'
42+
| 'vec3f'
43+
| 'vec3h'
44+
| 'vec3i'
45+
| 'vec3u'
46+
| 'vec3b'
47+
| 'vec4f'
48+
| 'vec4h'
49+
| 'vec4i'
50+
| 'vec4u'
51+
| 'vec4b'
52+
| 'mat2x2f'
53+
| 'mat3x3f'
54+
| 'mat4x4f'
55+
| StructBit
56+
| ArrayBit;
57+
58+
export function isCompoundBit(bit: unknown): bit is CompoundBit<unknown> {
59+
return !!(bit as CompoundBit<unknown>)['~shaderbit:wgsl'];
60+
}
61+
62+
export function reduceIfCompound<T>(bit: CompoundBit<T> | T): T {
63+
if (isCompoundBit(bit)) {
64+
return bit['~shaderbit:wgsl'].reduce();
65+
}
66+
return bit as T;
67+
}

0 commit comments

Comments
 (0)