diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /dom/webgpu/tests/cts/checkout/src/common/util/navigator_gpu.ts | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/webgpu/tests/cts/checkout/src/common/util/navigator_gpu.ts')
-rw-r--r-- | dom/webgpu/tests/cts/checkout/src/common/util/navigator_gpu.ts | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/dom/webgpu/tests/cts/checkout/src/common/util/navigator_gpu.ts b/dom/webgpu/tests/cts/checkout/src/common/util/navigator_gpu.ts new file mode 100644 index 0000000000..4110a0edb5 --- /dev/null +++ b/dom/webgpu/tests/cts/checkout/src/common/util/navigator_gpu.ts @@ -0,0 +1,86 @@ +/// <reference types="@webgpu/types" /> + +import { TestCaseRecorder } from '../framework/fixture.js'; + +import { ErrorWithExtra, assert, objectEquals } from './util.js'; + +/** + * Finds and returns the `navigator.gpu` object (or equivalent, for non-browser implementations). + * Throws an exception if not found. + */ +function defaultGPUProvider(): GPU { + assert( + typeof navigator !== 'undefined' && navigator.gpu !== undefined, + 'No WebGPU implementation found' + ); + return navigator.gpu; +} + +/** + * GPUProvider is a function that creates and returns a new GPU instance. + * May throw an exception if a GPU cannot be created. + */ +export type GPUProvider = () => GPU; + +let gpuProvider: GPUProvider = defaultGPUProvider; + +/** + * Sets the function to create and return a new GPU instance. + */ +export function setGPUProvider(provider: GPUProvider) { + assert(impl === undefined, 'setGPUProvider() should not be after getGPU()'); + gpuProvider = provider; +} + +let impl: GPU | undefined = undefined; + +let defaultRequestAdapterOptions: GPURequestAdapterOptions | undefined; + +export function setDefaultRequestAdapterOptions(options: GPURequestAdapterOptions) { + // It's okay to call this if you don't change the options + if (objectEquals(options, defaultRequestAdapterOptions)) { + return; + } + if (impl) { + throw new Error('must call setDefaultRequestAdapterOptions before getGPU'); + } + defaultRequestAdapterOptions = { ...options }; +} + +export function getDefaultRequestAdapterOptions() { + return defaultRequestAdapterOptions; +} + +/** + * Finds and returns the `navigator.gpu` object (or equivalent, for non-browser implementations). + * Throws an exception if not found. + */ +export function getGPU(recorder: TestCaseRecorder | null): GPU { + if (impl) { + return impl; + } + + impl = gpuProvider(); + + if (defaultRequestAdapterOptions) { + // eslint-disable-next-line @typescript-eslint/unbound-method + const oldFn = impl.requestAdapter; + impl.requestAdapter = function ( + options?: GPURequestAdapterOptions + ): Promise<GPUAdapter | null> { + const promise = oldFn.call(this, { ...defaultRequestAdapterOptions, ...options }); + if (recorder) { + void promise.then(async adapter => { + if (adapter) { + const info = await adapter.requestAdapterInfo(); + const infoString = `Adapter: ${info.vendor} / ${info.architecture} / ${info.device}`; + recorder.debug(new ErrorWithExtra(infoString, () => ({ adapterInfo: info }))); + } + }); + } + return promise; + }; + } + + return impl; +} |