From 1344d3e7d6a3236f80d89bc603a52a68f956246e Mon Sep 17 00:00:00 2001 From: felixpalmer Date: Fri, 30 Aug 2024 08:47:06 +0200 Subject: [PATCH] webgl: clip image write based on texture size (#2234) --- .../core/test/adapter/resources/texture.spec.ts | 16 ++++++++++++++++ .../webgl/src/adapter/resources/webgl-texture.ts | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/core/test/adapter/resources/texture.spec.ts b/modules/core/test/adapter/resources/texture.spec.ts index 484fa51dc6..194e3b83f8 100644 --- a/modules/core/test/adapter/resources/texture.spec.ts +++ b/modules/core/test/adapter/resources/texture.spec.ts @@ -329,6 +329,22 @@ test('Texture#copyExternalImage', async t => { `${device.info.type} Pixels were set correctly (sub image)` ); } + + // Subimage copy (smaller canvas) + canvas.width = 1; + canvas.height = 1; + ctx.fillStyle = '#00FF00'; + ctx.fillRect(0, 0, 1, 1); + + texture.copyExternalImage({image: canvas, x: 1}); + + if (device.info.type === 'webgl') { + t.deepEquals( + device.readPixelsToArrayWebGL(texture), + new Uint8Array([255, 0, 0, 255, 0, 255, 0, 255]), + `${device.info.type} Pixels were set correctly (sub image small canvas)` + ); + } } if (device.info.type !== 'webgl') { diff --git a/modules/webgl/src/adapter/resources/webgl-texture.ts b/modules/webgl/src/adapter/resources/webgl-texture.ts index 11f254f7b3..e2c36511eb 100644 --- a/modules/webgl/src/adapter/resources/webgl-texture.ts +++ b/modules/webgl/src/adapter/resources/webgl-texture.ts @@ -243,8 +243,8 @@ export class WEBGLTexture extends Texture { const {dimension, glTarget, glFormat, glInternalFormat, glType} = this; // WebGL will error if we try to copy outside the bounds of the texture - width = Math.min(width, size.width - x); - height = Math.min(height, size.height - y); + width = Math.min(width, this.width - x); + height = Math.min(height, this.height - y); if (options.sourceX || options.sourceY) { // requires copyTexSubImage2D from a framebuffer'