Skip to content

Commit 7307951

Browse files
authored
feat: Sampler schemas (#1819)
1 parent e1187c6 commit 7307951

File tree

25 files changed

+472
-334
lines changed

25 files changed

+472
-334
lines changed

apps/typegpu-docs/src/examples/image-processing/ascii-filter/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const glyphSize = root.createUniform(d.u32, 8);
1515
const uvTransformBuffer = root
1616
.createUniform(d.mat2x2f, d.mat2x2f.identity());
1717

18-
const shaderSampler = tgpu['~unstable'].sampler({
18+
const shaderSampler = root['~unstable'].createSampler({
1919
magFilter: 'linear',
2020
minFilter: 'linear',
2121
});
@@ -78,7 +78,7 @@ const fragmentFn = tgpu['~unstable'].fragmentFn({
7878

7979
const color = std.textureSampleBaseClampToEdge(
8080
layout.$.externalTexture,
81-
shaderSampler,
81+
shaderSampler.$,
8282
blockCoord,
8383
);
8484

apps/typegpu-docs/src/examples/image-processing/blur/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const textures = [0, 1].map(() => {
5454
});
5555
const renderView = textures[1].createView(d.texture2d(d.f32));
5656

57-
const sampler = tgpu['~unstable'].sampler({
57+
const sampler = root['~unstable'].createSampler({
5858
magFilter: 'linear',
5959
minFilter: 'linear',
6060
});
@@ -93,7 +93,7 @@ const computeFn = tgpu['~unstable'].computeFn({
9393

9494
tileData.$[r][lid.x * 4 + d.u32(c)] = std.textureSampleLevel(
9595
ioLayout.$.inTexture,
96-
sampler,
96+
sampler.$,
9797
d.vec2f(d.vec2f(loadIndex).add(d.vec2f(0.5)).div(d.vec2f(dims))),
9898
0,
9999
).xyz;
@@ -146,7 +146,7 @@ const renderFragment = tgpu['~unstable'].fragmentFn({
146146
})((input) =>
147147
std.textureSample(
148148
renderView.$,
149-
sampler,
149+
sampler.$,
150150
input.uv,
151151
)
152152
);

apps/typegpu-docs/src/examples/rendering/cubemap-reflection/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ const texture = root['~unstable']
109109
await loadCubemap(texture, chosenCubemap);
110110

111111
const cubemap = texture.createView(d.textureCube(d.f32));
112-
const sampler = tgpu['~unstable'].sampler({
112+
const sampler = root['~unstable'].createSampler({
113113
magFilter: 'linear',
114114
minFilter: 'linear',
115115
});
@@ -218,7 +218,7 @@ const fragmentFn = tgpu['~unstable'].fragmentFn({
218218
);
219219
const environmentColor = std.textureSample(
220220
cubemapBinding.$,
221-
texSampler,
221+
texSampler.$,
222222
reflectionVector,
223223
);
224224

@@ -266,7 +266,7 @@ const cubeFragmentFn = tgpu['~unstable'].fragmentFn({
266266
})((input) => {
267267
return std.textureSample(
268268
cubemapBinding.$,
269-
texSampler,
269+
texSampler.$,
270270
std.normalize(input.texCoord),
271271
);
272272
});

apps/typegpu-docs/src/examples/rendering/simple-shadow/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function createShadowTextures(
6565
format: 'depth32float',
6666
}).$usage('render', 'sampled');
6767

68-
const comparisonSampler = tgpu['~unstable'].comparisonSampler({
68+
const comparisonSampler = root['~unstable'].createComparisonSampler({
6969
compare: sampleCompare,
7070
magFilter: pcf ? 'linear' : 'nearest',
7171
minFilter: pcf ? 'linear' : 'nearest',

apps/typegpu-docs/src/examples/simple/liquid-glass/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ imageTexture.write(imageBitmap);
2424
imageTexture.generateMipmaps();
2525

2626
const sampledView = imageTexture.createView();
27-
const sampler = tgpu['~unstable'].sampler({
27+
const sampler = root['~unstable'].createSampler({
2828
magFilter: 'linear',
2929
minFilter: 'linear',
3030
mipmapFilter: 'linear',
@@ -97,6 +97,7 @@ const applyTint = (color: d.v3f, tint: d.Infer<typeof TintParams>) => {
9797

9898
const sampleWithChromaticAberration = (
9999
tex: d.texture2d<d.F32>,
100+
sampler: d.sampler,
100101
uv: d.v2f,
101102
offset: number,
102103
dir: d.v2f,
@@ -137,18 +138,19 @@ const fragmentShader = tgpu['~unstable'].fragmentFn({
137138

138139
const blurSample = std.textureSampleBias(
139140
sampledView.$,
140-
sampler,
141+
sampler.$,
141142
uv,
142143
paramsUniform.$.blur,
143144
);
144145
const refractedSample = sampleWithChromaticAberration(
145146
sampledView.$,
147+
sampler.$,
146148
uv.add(dir.mul(paramsUniform.$.refractionStrength * normalizedDist)),
147149
paramsUniform.$.chromaticStrength * normalizedDist,
148150
dir,
149151
paramsUniform.$.blur * paramsUniform.$.edgeBlurMultiplier,
150152
);
151-
const normalSample = std.textureSampleLevel(sampledView.$, sampler, uv, 0);
153+
const normalSample = std.textureSampleLevel(sampledView.$, sampler.$, uv, 0);
152154

153155
const tint = TintParams({
154156
color: paramsUniform.$.tintColor,

apps/typegpu-docs/src/examples/simulation/gravity/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ context.configure({
5353

5454
// static resources (created on the example load)
5555

56-
const sampler = tgpu['~unstable'].sampler({
56+
const sampler = root['~unstable'].createSampler({
5757
magFilter: 'linear',
5858
minFilter: 'linear',
5959
});

apps/typegpu-docs/src/examples/simulation/slime-mold-3d/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ const fullScreenTriangle = tgpu['~unstable'].vertexFn({
359359
};
360360
});
361361

362-
const sampler = tgpu['~unstable'].sampler({
362+
const sampler = root['~unstable'].createSampler({
363363
magFilter: canFilter ? 'linear' : 'nearest',
364364
minFilter: canFilter ? 'linear' : 'nearest',
365365
});
@@ -433,7 +433,7 @@ const fragmentShader = tgpu['~unstable'].fragmentFn({
433433
const texCoord = pos.div(resolution);
434434

435435
const sampleValue = std
436-
.textureSampleLevel(renderLayout.$.state, sampler, texCoord, 0)
436+
.textureSampleLevel(renderLayout.$.state, sampler.$, texCoord, 0)
437437
.x;
438438

439439
const d0 = std.smoothstep(thresholdLo, thresholdHi, sampleValue);

apps/typegpu-docs/src/examples/simulation/stable-fluid/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ const newInkTex = createField('addedInk');
125125
const forceTex = createField('force');
126126
const divergenceTex = createField('divergence');
127127

128-
const linSampler = tgpu['~unstable'].sampler({
128+
const linSampler = root['~unstable'].createSampler({
129129
magFilter: 'linear',
130130
minFilter: 'linear',
131131
});

apps/typegpu-docs/src/examples/tests/texture-test/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let texture = root['~unstable'].createTexture({
3535
texture.copyFrom(sourceTex);
3636
texture.generateMipmaps();
3737

38-
const filteringSampler = tgpu['~unstable'].sampler({
38+
const filteringSampler = root['~unstable'].createSampler({
3939
magFilter: 'linear',
4040
minFilter: 'linear',
4141
});

packages/typegpu/src/core/resolve/resolveData.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isLooseData,
66
type Unstruct,
77
} from '../../data/dataTypes.ts';
8+
import { isWgslComparisonSampler, isWgslSampler } from '../../data/sampler.ts';
89
import {
910
accessModeMap,
1011
isWgslStorageTexture,
@@ -302,5 +303,9 @@ export function resolveData(ctx: ResolutionCtx, data: AnyData): string {
302303
: `${data.type}<${data.sampleType.type}>`;
303304
}
304305

306+
if (isWgslComparisonSampler(data) || isWgslSampler(data)) {
307+
return data.type;
308+
}
309+
305310
assertExhaustive(data, 'resolveData');
306311
}

0 commit comments

Comments
 (0)