Skip to content

Commit ef1efc7

Browse files
committed
more size options, fix another redundantly allocated texture
1 parent 3bc94b7 commit ef1efc7

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

sample/alphaToCoverage/main.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ quitIfWebGPUNotAvailable(adapter, device);
1414
//
1515

1616
const kInitConfig = {
17-
width: 8,
18-
height: 8,
17+
sizeLog2: 3,
1918
showResolvedColor: true,
2019
color1: 0x0000ff,
2120
alpha1: 127,
@@ -37,8 +36,7 @@ gui.width = 300;
3736

3837
const settings = gui.addFolder('Settings');
3938
settings.open();
40-
settings.add(config, 'width', 1, 16, 1);
41-
settings.add(config, 'height', 1, 16, 1);
39+
settings.add(config, 'sizeLog2', 0, 8, 1).name('size = 2**');
4240
settings.add(config, 'showResolvedColor', true);
4341

4442
const draw1Panel = gui.addFolder('Draw 1');
@@ -83,25 +81,36 @@ const bufInstanceColors = device.createBuffer({
8381
size: 8,
8482
});
8583

86-
let multisampleTexture, multisampleTextureView;
84+
let multisampleTexture: GPUTexture, multisampleTextureView: GPUTextureView;
85+
let resolveTexture: GPUTexture, resolveTextureView: GPUTextureView;
86+
let lastSize = 0;
8787
function resetMultisampleTexture() {
88-
if (
89-
!multisampleTexture ||
90-
multisampleTexture.width !== config.width ||
91-
multisampleTexture.height !== config.height
92-
) {
88+
const size = 2 ** config.sizeLog2;
89+
if (lastSize !== size) {
9390
if (multisampleTexture) {
9491
multisampleTexture.destroy();
9592
}
96-
console.log('recreate');
9793
multisampleTexture = device.createTexture({
9894
format: 'rgba8unorm',
9995
usage:
10096
GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
101-
size: [config.width, config.height],
97+
size: [size, size],
10298
sampleCount: 4,
10399
});
104100
multisampleTextureView = multisampleTexture.createView();
101+
102+
if (resolveTexture) {
103+
resolveTexture.destroy();
104+
}
105+
resolveTexture = device.createTexture({
106+
format: 'rgba8unorm',
107+
usage:
108+
GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
109+
size: [size, size],
110+
});
111+
resolveTextureView = resolveTexture.createView();
112+
113+
lastSize = size;
105114
}
106115
}
107116

@@ -175,13 +184,6 @@ const showMultisampleTextureBGL =
175184
function render() {
176185
applyConfig();
177186

178-
const resolveTexture = device.createTexture({
179-
format: 'rgba8unorm',
180-
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
181-
size: [config.width, config.height],
182-
});
183-
const resolveTextureView = resolveTexture.createView();
184-
185187
const showMultisampleTextureBG = device.createBindGroup({
186188
layout: showMultisampleTextureBGL,
187189
entries: [

sample/alphaToCoverage/showMultisampleTexture.wgsl

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,26 @@ fn fmain(vary: Varying) -> @location(0) vec4f {
4949
let xyInt = vec2u(xy);
5050
let xyFrac = xy % vec2f(1, 1);
5151

52-
if xyInt.x >= dim.x || xyInt.y >= dim.y {
53-
return vec4f(0, 0, 0, 1);
54-
}
52+
// Show the visualization only if the resolution is large enough to see it
53+
if (dpdx(xy.x) < kGridEdgeHalfWidth) & (dpdy(xy.y) < kGridEdgeHalfWidth) {
54+
// Check if we're close to a sample; if so, visualize the sample value
55+
for (var sampleIndex = 0; sampleIndex < kSampleCount; sampleIndex += 1) {
56+
let distanceFromSample = distance(xyFrac, kSamplePositions[sampleIndex]);
57+
if distanceFromSample < kSampleInnerRadius {
58+
// Draw a circle for the sample value
59+
let val = textureLoad(tex, xyInt, sampleIndex).rgb;
60+
return vec4f(val, 1);
61+
} else if distanceFromSample < kSampleOuterRadius {
62+
// Draw a ring around the circle
63+
return vec4f(0, 0, 0, 1);
64+
}
65+
}
5566

56-
// Check if we're close to a sample, and return it
57-
for (var sampleIndex = 0; sampleIndex < kSampleCount; sampleIndex += 1) {
58-
let distanceFromSample = distance(xyFrac, kSamplePositions[sampleIndex]);
59-
if distanceFromSample < kSampleInnerRadius {
60-
// Draw a circle for the sample value
61-
let val = textureLoad(tex, xyInt, sampleIndex).rgb;
62-
return vec4f(val, 1);
63-
} else if distanceFromSample < kSampleOuterRadius {
64-
// Draw a ring around the circle
67+
// If close to a grid edge, render the grid
68+
let distanceToGridEdge = abs((xyFrac + 0.5) % 1 - 0.5);
69+
if min(distanceToGridEdge.x, distanceToGridEdge.y) < kGridEdgeHalfWidth {
6570
return vec4f(0, 0, 0, 1);
6671
}
67-
68-
}
69-
70-
// If close to a grid edge, render the grid
71-
let distanceToGridEdge = abs((xyFrac + 0.5) % 1 - 0.5);
72-
if min(distanceToGridEdge.x, distanceToGridEdge.y) < kGridEdgeHalfWidth {
73-
return vec4f(0, 0, 0, 1);
7472
}
7573

7674
// Otherwise, show the multisample-resolved result as the background

0 commit comments

Comments
 (0)