Skip to content

Commit

Permalink
Add a powerPreference option
Browse files Browse the repository at this point in the history
Not sure how to add this here's an attempt.

You can pass `powerPreference=low-power` or
`power-preference=high-performance` in the query string.
  • Loading branch information
greggman committed Dec 15, 2022
1 parent e583fa4 commit fcba4cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/common/runtime/standalone.ts
Expand Up @@ -9,6 +9,7 @@ import { parseQuery } from '../internal/query/parseQuery.js';
import { TestQueryLevel } from '../internal/query/query.js';
import { TestTreeNode, TestSubtree, TestTreeLeaf, TestTree } from '../internal/tree.js';
import { assert, ErrorWithExtra } from '../util/util.js';
import { setDefaultRequestAdapterOptions } from '../util/navigator_gpu.js';

import { optionEnabled } from './helper/options.js';
import { TestWorker } from './helper/test_worker.js';
Expand All @@ -33,6 +34,11 @@ const worker = optionEnabled('worker') ? new TestWorker(debug) : undefined;
const autoCloseOnPass = document.getElementById('autoCloseOnPass') as HTMLInputElement;
const resultsVis = document.getElementById('resultsVis')!;

const powerPreference = new URLSearchParams(window.location.search).get('powerPreference');
if (powerPreference) {
setDefaultRequestAdapterOptions({ powerPreference: powerPreference as GPUPowerPreference });
}

dataCache.setStore({
load: async (path: string) => {
const response = await fetch(`data/${path}`);
Expand Down
27 changes: 27 additions & 0 deletions src/common/util/navigator_gpu.ts
Expand Up @@ -32,6 +32,15 @@ export function setGPUProvider(provider: GPUProvider) {

let impl: GPU | undefined = undefined;

let defaultRequestAdapterOptions: GPURequestAdapterOptions | undefined;

export function setDefaultRequestAdapterOptions(options: GPURequestAdapterOptions) {
if (impl) {
throw new Error('must call setDefaultRequestAdapterOptions before getGPU');
}
defaultRequestAdapterOptions = { ...options };
}

/**
* Finds and returns the `navigator.gpu` object (or equivalent, for non-browser implementations).
* Throws an exception if not found.
Expand All @@ -43,5 +52,23 @@ export function getGPU(): GPU {

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 || {}) });
void promise.then(async adapter => {
if (adapter) {
const info = await adapter.requestAdapterInfo();
// eslint-disable-next-line no-console
console.log(info);
}
});
return promise;
};
}

return impl;
}

0 comments on commit fcba4cf

Please sign in to comment.