From d3ebb6d7a3e5b4dfee15e0e41b2a3a55ec6a75af Mon Sep 17 00:00:00 2001 From: perryuwang Date: Tue, 16 Apr 2024 21:34:14 +0800 Subject: [PATCH 1/3] Add reftests: canvas display after device lost --- .../canvas_display_after_device_lost.html.ts | 53 +++++++++++++++++++ ...anvas_display_after_device_lost.https.html | 16 ++++++ .../canvas_display_after_device_lost-ref.html | 26 +++++++++ 3 files changed, 95 insertions(+) create mode 100644 src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts create mode 100644 src/webgpu/web_platform/reftests/canvas_display_after_device_lost.https.html create mode 100644 src/webgpu/web_platform/reftests/ref/canvas_display_after_device_lost-ref.html diff --git a/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts b/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts new file mode 100644 index 000000000000..332d53ae8f84 --- /dev/null +++ b/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts @@ -0,0 +1,53 @@ +import { runRefTest } from './gpu_ref_test.js'; + +runRefTest(t => { + const device = t.device; + const presentationFormat = navigator.gpu.getPreferredCanvasFormat(); + let deviceLost = false; + + function draw(canvasId: string, alphaMode: GPUCanvasAlphaMode, abortAfterDeviceLost: boolean) { + if (deviceLost && abortAfterDeviceLost) { + return; + } + + const canvas = document.getElementById(canvasId) as HTMLCanvasElement; + const ctx = canvas.getContext('webgpu') as unknown as GPUCanvasContext; + ctx.configure({ + device, + format: presentationFormat, + alphaMode, + }); + + const colorAttachment = ctx.getCurrentTexture(); + const colorAttachmentView = colorAttachment.createView(); + + const encoder = device.createCommandEncoder(); + const pass = encoder.beginRenderPass({ + colorAttachments: [ + { + view: colorAttachmentView, + clearValue: { r: 0.4, g: 1.0, b: 0.0, a: 1.0 }, + loadOp: 'clear', + storeOp: 'store', + }, + ], + }); + pass.end(); + device.queue.submit([encoder.finish()]); + } + + function drawAll() { + draw('cvs0', 'opaque', true); + draw('cvs1', 'opaque', false); + draw('cvs2', 'premultiplied', true); + draw('cvs3', 'premultiplied', false); + + if (!deviceLost) { + device.destroy(); + deviceLost = true; + } + } + + drawAll(); + requestAnimationFrame(drawAll); +}); diff --git a/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.https.html b/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.https.html new file mode 100644 index 000000000000..03ff43580a0e --- /dev/null +++ b/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.https.html @@ -0,0 +1,16 @@ + + + WebGPU canvas_display_after_device_lost + + + + + + + + + + + diff --git a/src/webgpu/web_platform/reftests/ref/canvas_display_after_device_lost-ref.html b/src/webgpu/web_platform/reftests/ref/canvas_display_after_device_lost-ref.html new file mode 100644 index 000000000000..fbbbafb6fd05 --- /dev/null +++ b/src/webgpu/web_platform/reftests/ref/canvas_display_after_device_lost-ref.html @@ -0,0 +1,26 @@ + + + WebGPU canvas_display_after_device_lost (ref) + + + + + + + + + From 7e01b6bbd0c36d9e5ee7d224d879670f62eb16c2 Mon Sep 17 00:00:00 2001 From: perryuwang Date: Wed, 17 Apr 2024 15:24:32 +0800 Subject: [PATCH 2/3] timeout --- .../canvas_display_after_device_lost.html.ts | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts b/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts index 332d53ae8f84..c1e16b700c6b 100644 --- a/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts +++ b/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts @@ -1,7 +1,17 @@ -import { runRefTest } from './gpu_ref_test.js'; +import { assert } from '../../../common/util/util.js'; +import { timeout } from '../../../common/util/timeout.js'; +import { takeScreenshotDelayed } from '../../../common/util/wpt_reftest_wait.js'; -runRefTest(t => { - const device = t.device; +void (async () => { + assert( + typeof navigator !== 'undefined' && navigator.gpu !== undefined, + 'No WebGPU implementation found' + ); + + const adapter = await navigator.gpu.requestAdapter(); + assert(adapter !== null); + const device = await adapter.requestDevice(); + assert(device !== null); const presentationFormat = navigator.gpu.getPreferredCanvasFormat(); let deviceLost = false; @@ -45,9 +55,11 @@ runRefTest(t => { if (!deviceLost) { device.destroy(); deviceLost = true; + timeout(drawAll, 100); + } else { + takeScreenshotDelayed(50); } } drawAll(); - requestAnimationFrame(drawAll); -}); +})(); From 8b15e097a85e47560cc6033413b2457cefd13b72 Mon Sep 17 00:00:00 2001 From: perryuwang Date: Wed, 17 Apr 2024 15:32:46 +0800 Subject: [PATCH 3/3] fix lint --- .../reftests/canvas_display_after_device_lost.html.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts b/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts index c1e16b700c6b..834d4ffc17e8 100644 --- a/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts +++ b/src/webgpu/web_platform/reftests/canvas_display_after_device_lost.html.ts @@ -1,5 +1,5 @@ -import { assert } from '../../../common/util/util.js'; import { timeout } from '../../../common/util/timeout.js'; +import { assert } from '../../../common/util/util.js'; import { takeScreenshotDelayed } from '../../../common/util/wpt_reftest_wait.js'; void (async () => {