@@ -424,6 +424,136 @@ void main() { gl_FragColor = texture2D(tex, uv); }`,
424424 }
425425 } ) ;
426426
427+ test ( 'copyExternalImageToTexture matches getImageData for many redrawn 2d canvases' , async ( ) => {
428+ const device = await getDevice ( ) ;
429+ ok ( device , 'no webgpu device' ) ;
430+
431+ // Like PixiJS text: many odd sized canvases, redrawn and uploaded back to back with no sync in between.
432+ const draw = ( ctx : any , i : number , round : number , width : number , height : number ) => {
433+ ctx . clearRect ( 0 , 0 , width , height ) ;
434+ ctx . fillStyle = `rgb(${ ( i * 20 ) % 256 } , ${ round * 90 } , 160)` ;
435+ ctx . fillRect ( 2 , 2 , width / 2 , height - 4 ) ;
436+ ctx . fillStyle = 'white' ;
437+ ctx . font = '14px sans-serif' ;
438+ ctx . fillText ( `T${ i } r${ round } ` , 4 , height - 6 ) ;
439+ } ;
440+ const sources = Array . from ( { length : 12 } , ( _ , i ) => make2D ( 40 + i * 7 , 20 + i * 3 ) ) ;
441+ const copies : { texture : any ; readback : any ; bytesPerRow : number ; bgra : boolean ; width : number ; height : number ; i : number ; round : number } [ ] = [ ] ;
442+ for ( let round = 0 ; round < 5 ; round ++ ) {
443+ // Frames go by between rounds, as in a render loop.
444+ await new Promise ( ( resolve ) => requestAnimationFrame ( resolve ) ) ;
445+ for ( let i = 0 ; i < sources . length ; i ++ ) {
446+ const { canvas, ctx } = sources [ i ] ;
447+ // PixiJS resizes a text's canvas whenever its text changes.
448+ canvas . width = 40 + i * 7 + round * 5 ;
449+ canvas . height = 20 + i * 3 + ( round % 2 ) ;
450+ const { width, height } = canvas ;
451+ draw ( ctx , i , round , width , height ) ;
452+ const bgra = i % 2 === 1 ;
453+ const texture = device . createTexture ( {
454+ size : [ width , height , 1 ] ,
455+ format : bgra ? 'bgra8unorm' : 'rgba8unorm' ,
456+ usage : GPUTextureUsage . TEXTURE_BINDING | GPUTextureUsage . COPY_DST | GPUTextureUsage . COPY_SRC | GPUTextureUsage . RENDER_ATTACHMENT ,
457+ } ) ;
458+ device . queue . copyExternalImageToTexture ( { source : canvas } , { texture, premultipliedAlpha : true } , [ width , height , 1 ] ) ;
459+ const bytesPerRow = Math . ceil ( ( width * 4 ) / 256 ) * 256 ;
460+ const readback = device . createBuffer ( { size : bytesPerRow * height , usage : GPUBufferUsage . COPY_DST | GPUBufferUsage . MAP_READ } ) ;
461+ copies . push ( { texture, readback, bytesPerRow, bgra, width, height, i, round } ) ;
462+ }
463+ }
464+ const encoder = device . createCommandEncoder ( ) ;
465+ for ( const { texture, readback, bytesPerRow, width, height } of copies ) {
466+ encoder . copyTextureToBuffer ( { texture } , { buffer : readback , bytesPerRow } , { width, height, depthOrArrayLayers : 1 } ) ;
467+ }
468+ device . queue . submit ( [ encoder . finish ( ) ] ) ;
469+
470+ for ( const { texture, readback, bytesPerRow, bgra, width, height, i, round } of copies ) {
471+ await readback . mapAsync ( GPUMapMode . READ ) ;
472+ const pixels = new Uint8Array ( readback . getMappedRange ( ) . slice ( 0 ) ) ;
473+ readback . unmap ( ) ;
474+ readback . destroy ( ) ;
475+ texture . destroy ( ) ;
476+ const reference = make2D ( width , height ) . ctx ;
477+ draw ( reference , i , round , width , height ) ;
478+ const expected = reference . getImageData ( 0 , 0 , width , height ) . data ;
479+ const label = `canvas ${ i } , round ${ round } ` ;
480+
481+ let bad = 0 ;
482+ let first = '' ;
483+ for ( let y = 0 ; y < height ; y ++ ) {
484+ for ( let x = 0 ; x < width ; x ++ ) {
485+ const e = ( y * width + x ) * 4 ;
486+ const a = expected [ e + 3 ] ;
487+ const want = [ Math . round ( ( expected [ e ] * a ) / 255 ) , Math . round ( ( expected [ e + 1 ] * a ) / 255 ) , Math . round ( ( expected [ e + 2 ] * a ) / 255 ) , a ] ;
488+ const o = y * bytesPerRow + x * 4 ;
489+ const got = bgra ? [ pixels [ o + 2 ] , pixels [ o + 1 ] , pixels [ o ] , pixels [ o + 3 ] ] : pixels . subarray ( o , o + 4 ) ;
490+ if ( ! pixelNear ( got , want , 12 ) ) {
491+ if ( ! bad ) {
492+ first = `at ${ x } ,${ y } expected ${ describePixel ( want ) } , got ${ describePixel ( got ) } ` ;
493+ }
494+ bad ++ ;
495+ }
496+ }
497+ }
498+ ok ( bad === 0 , `${ label } : ${ bad } of ${ width * height } pixels differ, first ${ first } ` ) ;
499+ }
500+ } ) ;
501+
502+ test ( 'copyExternalImageToTexture waits for each 2d canvas, not a later one' , async ( ) => {
503+ const device = await getDevice ( ) ;
504+ ok ( device , 'no webgpu device' ) ;
505+
506+ // A slow canvas copied first and a quick one after: the first copy must not finish early.
507+ const slow = make2D ( 1024 , 1024 ) ;
508+ const quick = make2D ( 16 , 16 ) ;
509+ const pending : { texture : any ; readback : any ; expected : number [ ] ; label : string } [ ] = [ ] ;
510+ for ( let round = 0 ; round < 6 ; round ++ ) {
511+ const { ctx } = slow ;
512+ ctx . clearRect ( 0 , 0 , 1024 , 1024 ) ;
513+ ctx . shadowColor = 'rgba(0, 0, 0, 0.5)' ;
514+ ctx . shadowBlur = 40 ;
515+ for ( let j = 0 ; j < 60 ; j ++ ) {
516+ ctx . fillStyle = `rgb(${ j * 4 } , 80, 200)` ;
517+ ctx . beginPath ( ) ;
518+ ctx . arc ( ( j * 97 ) % 1024 , ( j * 61 ) % 1024 , 120 , 0 , Math . PI * 2 ) ;
519+ ctx . fill ( ) ;
520+ }
521+ ctx . shadowBlur = 0 ;
522+ const expected = [ round * 40 , 255 - round * 40 , 60 , 255 ] ;
523+ ctx . fillStyle = `rgb(${ expected [ 0 ] } , ${ expected [ 1 ] } , ${ expected [ 2 ] } )` ;
524+ ctx . fillRect ( 480 , 480 , 64 , 64 ) ;
525+ const texture = device . createTexture ( {
526+ size : [ 1024 , 1024 , 1 ] ,
527+ format : 'rgba8unorm' ,
528+ usage : GPUTextureUsage . TEXTURE_BINDING | GPUTextureUsage . COPY_DST | GPUTextureUsage . COPY_SRC | GPUTextureUsage . RENDER_ATTACHMENT ,
529+ } ) ;
530+ device . queue . copyExternalImageToTexture ( { source : slow . canvas } , { texture, premultipliedAlpha : true } , [ 1024 , 1024 , 1 ] ) ;
531+
532+ quick . ctx . fillStyle = 'red' ;
533+ quick . ctx . fillRect ( 0 , 0 , 16 , 16 ) ;
534+ const other = device . createTexture ( {
535+ size : [ 16 , 16 , 1 ] ,
536+ format : 'rgba8unorm' ,
537+ usage : GPUTextureUsage . TEXTURE_BINDING | GPUTextureUsage . COPY_DST | GPUTextureUsage . RENDER_ATTACHMENT ,
538+ } ) ;
539+ device . queue . copyExternalImageToTexture ( { source : quick . canvas } , { texture : other , premultipliedAlpha : true } , [ 16 , 16 , 1 ] ) ;
540+
541+ const readback = device . createBuffer ( { size : 256 , usage : GPUBufferUsage . COPY_DST | GPUBufferUsage . MAP_READ } ) ;
542+ const encoder = device . createCommandEncoder ( ) ;
543+ encoder . copyTextureToBuffer ( { texture, origin : { x : 512 , y : 512 } } , { buffer : readback , bytesPerRow : 256 } , { width : 1 , height : 1 , depthOrArrayLayers : 1 } ) ;
544+ device . queue . submit ( [ encoder . finish ( ) ] ) ;
545+ pending . push ( { texture, readback, expected, label : `round ${ round } ` } ) ;
546+ }
547+ for ( const { texture, readback, expected, label } of pending ) {
548+ await readback . mapAsync ( GPUMapMode . READ ) ;
549+ const pixel = new Uint8Array ( readback . getMappedRange ( ) . slice ( 0 , 4 ) ) ;
550+ readback . unmap ( ) ;
551+ readback . destroy ( ) ;
552+ texture . destroy ( ) ;
553+ ok ( pixelNear ( pixel , expected ) , `${ label } : expected ${ describePixel ( expected ) } , got ${ describePixel ( pixel ) } ` ) ;
554+ }
555+ } ) ;
556+
427557 test ( 'copyExternalImageToTexture accepts a webgl canvas' , async ( ) => {
428558 const device = await getDevice ( ) ;
429559 ok ( device , 'no webgpu device' ) ;
0 commit comments