Skip to content

Commit 9963c55

Browse files
committed
helloTriangleMSAA: avoid creating a texture every frame
1 parent f2e5ae7 commit 9963c55

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

sample/helloTriangleMSAA/main.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,42 @@ const pipeline = device.createRenderPipeline({
5555
},
5656
});
5757

58-
function frame() {
59-
let usage = GPUTextureUsage.RENDER_ATTACHMENT;
60-
if (settings.transientAttachment) {
61-
usage |= GPUTextureUsage.TRANSIENT_ATTACHMENT;
62-
}
63-
64-
const texture = device.createTexture({
65-
size: [canvas.width, canvas.height],
66-
sampleCount,
67-
format: presentationFormat,
68-
usage,
69-
});
70-
const view = texture.createView();
58+
const getMSAATempTextureView = (() => {
59+
let texture: GPUTexture | undefined;
60+
let view: GPUTextureView | undefined;
61+
return () => {
62+
let usage = GPUTextureUsage.RENDER_ATTACHMENT;
63+
if (settings.transientAttachment) {
64+
usage |= GPUTextureUsage.TRANSIENT_ATTACHMENT;
65+
}
66+
67+
if (texture?.usage !== usage) {
68+
console.log(`Reallocating with usage ${usage}`);
69+
70+
if (texture) {
71+
texture.destroy();
72+
}
73+
74+
texture = device.createTexture({
75+
size: [canvas.width, canvas.height],
76+
sampleCount,
77+
format: presentationFormat,
78+
usage,
79+
});
80+
view = texture.createView();
81+
}
82+
83+
return view!;
84+
};
85+
})();
7186

87+
function frame() {
7288
const commandEncoder = device.createCommandEncoder();
7389

7490
const renderPassDescriptor: GPURenderPassDescriptor = {
7591
colorAttachments: [
7692
{
77-
view,
93+
view: getMSAATempTextureView(),
7894
resolveTarget: context.getCurrentTexture().createView(),
7995
clearValue: [0, 0, 0, 0], // Clear to transparent
8096
loadOp: 'clear',

0 commit comments

Comments
 (0)