Skip to content

Commit b389c3d

Browse files
authored
fix: globally cached multisampled texture (#238)
1 parent 1cf2055 commit b389c3d

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

src/WebGPU/getDevice.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default async function getDevice() {
1212

1313
const device = await adapter.requestDevice({
1414
requiredFeatures: hasBGRA8unormStorage ? ['bgra8unorm-storage'] : [],
15+
label: 'id: ' + Date.now(),
1516
})
1617
device.lost.then((info) => {
1718
console.error(`WebGPU device was lost: ${info.message}`)

src/getCanvasRenderDescriptor.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import getMultisampleTexture from './getMultisampleTexture'
2-
import getDepthTexture from './getDepthTexture'
2+
// import getDepthTexture from './getDepthTexture'
33
let multisampleTexture: GPUTexture | undefined
44

5+
export function destroyCanvasTextures() {
6+
multisampleTexture?.destroy()
7+
multisampleTexture = undefined
8+
}
9+
510
export default function getCanvasRenderDescriptor(context: GPUCanvasContext, device: GPUDevice) {
611
// here we need to render that texture into canvas
712
const canvasTexture = context.getCurrentTexture()
@@ -19,8 +24,8 @@ export default function getCanvasRenderDescriptor(context: GPUCanvasContext, dev
1924
label: 'our render to canvas renderPass',
2025
colorAttachments: [
2126
{
22-
view: multisampleTexture.createView(),
23-
resolveTarget: canvasTexture.createView(), // resolveTarget is used to resolve multisample texture into single sample texture
27+
view: multisampleTexture.createView({ label: 'multisample texture view' }), // we draw to this texture first
28+
resolveTarget: canvasTexture.createView({ label: 'canvas texture view' }), // resolveTarget is used to resolve multisample texture into single sample texture
2429
clearValue: [0, 0, 0, 1],
2530
loadOp: 'clear', // before rendering clear the texture to value "clear". Other option is "load" to load existing content of the texture into GPU so we can draw over it
2631
storeOp: 'store', // to store the result of what we draw, other option is "discard"

src/getDepthTexture.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
let depthTexture: GPUTexture | undefined
22

3+
export function destroy() {
4+
depthTexture?.destroy()
5+
depthTexture = undefined
6+
}
7+
38
export default function getDepthTexture(device: GPUDevice, width: number, height: number) {
49
if (!depthTexture || depthTexture.width !== width || depthTexture.height !== height) {
510
depthTexture?.destroy()

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
ZigAsset,
2222
} from './types'
2323
export * from './types'
24+
import { destroyCanvasTextures } from 'getCanvasRenderDescriptor'
2425

2526
export interface CreatorAPI {
2627
addImage: (url: string) => void
@@ -291,6 +292,7 @@ export default async function initCreator(
291292
stopRAF()
292293
Logic.deinitState()
293294
context.unconfigure()
295+
destroyCanvasTextures()
294296
device.destroy()
295297
},
296298
setTool: (tool) => {

0 commit comments

Comments
 (0)