Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions sample/particles/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GUI } from 'dat.gui';

import particleWGSL from './particle.wgsl';
import probabilityMapWGSL from './probabilityMap.wgsl';
import { quitIfWebGPUNotAvailableOrMissingFeatures } from '../util';
import { fail, quitIfWebGPUNotAvailableOrMissingFeatures } from '../util';

const numParticles = 50000;
const particlePositionOffset = 0;
Expand All @@ -28,14 +28,48 @@ const context = canvas.getContext('webgpu');
const devicePixelRatio = window.devicePixelRatio;
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;
const presentationFormat = 'rgba16float';
const hdrPresentationFormat: GPUTextureFormat = 'rgba16float';
let presentationFormat: GPUTextureFormat = hdrPresentationFormat;
let hasHDRCanvasFormat = true;

function configureContext() {
try {
context.configure({
device,
format: presentationFormat,
toneMapping: { mode: simulationParams.toneMappingMode },
toneMapping: { mode: 'standard' },
});
} catch (error) {
hasHDRCanvasFormat = false;
presentationFormat = navigator.gpu.getPreferredCanvasFormat();

try {
context.configure({
device,
format: presentationFormat,
});
} catch (fallbackError) {
fail(
`Unable to configure the WebGPU canvas. The HDR format '${hdrPresentationFormat}' is not supported by this browser, and configuring the preferred canvas format '${presentationFormat}' also failed.\n\n${fallbackError}`
);
}

console.warn(
`Canvas texture format '${hdrPresentationFormat}' is not supported by this browser. Falling back to '${presentationFormat}'.`,
error
);
}

function configureContext() {
const configuration: GPUCanvasConfiguration = {
device,
format: presentationFormat,
};

if (hasHDRCanvasFormat) {
configuration.toneMapping = { mode: simulationParams.toneMappingMode };
}

context.configure(configuration);
hdrFolder.name = getHdrFolderName();
}

Expand Down Expand Up @@ -336,6 +370,9 @@ function getHdrFolderName() {
if (!hdrMediaQuery.matches) {
return "HDR settings ⚠️ Display isn't compatible";
}
if (!hasHDRCanvasFormat) {
return "HDR settings ⚠️ Browser doesn't support HDR canvas";
}
if (!('getConfiguration' in GPUCanvasContext.prototype)) {
return 'HDR settings';
}
Expand Down
2 changes: 1 addition & 1 deletion sample/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export function quitIfWebGPUNotAvailableOrMissingFeatures(
}

/** Fail by showing a console error, and dialog box if possible. */
const fail = (() => {
export const fail = (() => {
type ErrorOutput = { show(msg: string): void };

function createErrorOutput() {
Expand Down