1- import mat4 from " utils/mat4"
2- import { pointer } from " ./pointer"
3- import { on_update_pick } from " ../logic/index.zig"
1+ import mat4 from ' utils/mat4'
2+ import { pointer } from ' ./pointer'
3+ import { on_update_pick } from ' ../logic/index.zig'
44
55const NUM_PIXELS = 1
66
77export default class PickManager {
8-
98 private pickBuffer : GPUBuffer
109 private pickTexture : GPUTexture
1110 private pickDepthTexture : GPUTexture
1211 private isPreviousPickDone = true
1312
14- constructor (
15- private device : GPUDevice ,
16- ) {
13+ constructor ( device : GPUDevice ) {
1714 this . pickBuffer = device . createBuffer ( {
1815 size : NUM_PIXELS * 4 ,
1916 usage : GPUBufferUsage . MAP_READ | GPUBufferUsage . COPY_DST ,
@@ -31,21 +28,22 @@ export default class PickManager {
3128 usage : GPUTextureUsage . RENDER_ATTACHMENT ,
3229 } )
3330 }
31+
3432 /**
3533 * Starts a picking render pass.
3634 * @param encoder The GPUCommandEncoder to use for the render pass.
3735 * @returns An object which contains render pass and a callback to end picking.
3836 */
39- startPicking ( encoder : GPUCommandEncoder ) : { pass : GPURenderPassEncoder , end : VoidFunction } {
37+ startPicking ( encoder : GPUCommandEncoder ) : { pass : GPURenderPassEncoder ; end : VoidFunction } {
4038 const descriptor : GPURenderPassDescriptor = {
4139 // describe which textures we want to raw to and how use them
42- label : " our render to canvas renderPass" ,
40+ label : ' our render to canvas renderPass' ,
4341 colorAttachments : [
4442 {
4543 view : this . pickTexture . createView ( ) ,
46- loadOp : " clear" ,
44+ loadOp : ' clear' ,
4745 clearValue : [ 0 , 0 , 0 , 1 ] ,
48- storeOp : " store" ,
46+ storeOp : ' store' ,
4947 } as const ,
5048 ] ,
5149 // depthStencilAttachment: {
@@ -68,19 +66,22 @@ export default class PickManager {
6866 pass . end ( )
6967
7068 if ( this . isPreviousPickDone ) {
71- encoder . copyTextureToBuffer ( {
69+ encoder . copyTextureToBuffer (
70+ {
7271 texture : this . pickTexture ,
73- origin : { x : 0 , y : 0 }
74- } , {
72+ origin : { x : 0 , y : 0 } ,
73+ } ,
74+ {
7575 buffer : this . pickBuffer ,
76- } , {
76+ } ,
77+ {
7778 width : NUM_PIXELS ,
7879 }
7980 )
8081 }
8182 }
8283
83- return { pass, end : endPicking }
84+ return { pass, end : endPicking }
8485 }
8586
8687 createMatrix ( canvas : HTMLCanvasElement , canvasMatrix : Float32Array ) {
@@ -104,10 +105,15 @@ export default class PickManager {
104105 async asyncPick ( ) {
105106 if ( ! this . isPreviousPickDone ) return
106107 this . isPreviousPickDone = false
107- await this . pickBuffer . mapAsync ( GPUMapMode . READ , 0 , 4 * NUM_PIXELS )
108- const [ id ] = new Uint32Array ( this . pickBuffer . getMappedRange ( 0 , 4 * NUM_PIXELS ) )
109- on_update_pick ( id )
110- this . pickBuffer . unmap ( )
108+ try {
109+ await this . pickBuffer . mapAsync ( GPUMapMode . READ , 0 , 4 * NUM_PIXELS )
110+ const [ id ] = new Uint32Array ( this . pickBuffer . getMappedRange ( 0 , 4 * NUM_PIXELS ) )
111+ on_update_pick ( id )
112+ this . pickBuffer . unmap ( )
113+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
114+ } catch ( err ) {
115+ /* ignorign errors when map fails because device was destroyed(so buffer too and was unmapped before mapAsync completed)*/
116+ }
111117 this . isPreviousPickDone = true
112118 }
113- }
119+ }
0 commit comments