From ef5d6f88f7abdca41706ff26d01d1956d41ce62f Mon Sep 17 00:00:00 2001 From: Stephen White Date: Wed, 19 Mar 2025 09:56:18 -0400 Subject: [PATCH] Fix the Cornell sample to run on desktop Compat. Desktop GPUs generally prefer BGRA for presentation, but Compatibility mode does not support bgra8unorm-storage. In that configuration, use rgba8unorm for both presentation and storage. This will be slightly slower, but will work. --- sample/cornell/main.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/sample/cornell/main.ts b/sample/cornell/main.ts index 9ad6ec16..83be8677 100644 --- a/sample/cornell/main.ts +++ b/sample/cornell/main.ts @@ -13,19 +13,22 @@ import { const canvas = document.querySelector('canvas') as HTMLCanvasElement; -const presentationFormat = navigator.gpu.getPreferredCanvasFormat(); -const features: GPUFeatureName[] = - presentationFormat === 'bgra8unorm' ? ['bgra8unorm-storage'] : []; const adapter = await navigator.gpu?.requestAdapter({ featureLevel: 'compatibility', }); quitIfAdapterNotAvailable(adapter); -for (const feature of features) { - if (!adapter.features.has(feature)) { - throw new Error( - `sample requires ${feature}, but is not supported by the adapter` - ); +const features: GPUFeatureName[] = []; +let presentationFormat = navigator.gpu.getPreferredCanvasFormat(); +if (presentationFormat == 'bgra8unorm') { + if (adapter.features.has('bgra8unorm-storage')) { + features.push('bgra8unorm-storage'); + } else { + // If the GPU prefers BGRA for presentation but the Adapter + // doesn't support bgra8unorm-storage (e.g., Compatibility + // mode), use rgba8unorm for both. This will be slower, but will + // work. + presentationFormat = 'rgba8unorm'; } } const limits: Record = {};