Skip to content

Commit

Permalink
Update print_environment to show featureLevel and defaultDevice info
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x committed Jan 7, 2025
1 parent 10b66cd commit 7d39682
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/common/runtime/helper/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const kCTSOptionsInfo: OptionsInfos<CTSOptions> = {
],
},
debug: { description: 'show more info' },
compatibility: { description: 'run in compatibility mode' },
compatibility: { description: 'request adapters with featureLevel: "compatibility"' },
forceFallbackAdapter: { description: 'pass forceFallbackAdapter: true to requestAdapter' },
unrollConstEvalLoops: { description: 'unroll const eval loops in WGSL' },
powerPreference: {
Expand Down
46 changes: 31 additions & 15 deletions src/webgpu/print_environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ function consoleLogIfNotWPT(x: unknown) {

g.test('info')
.desc(
`Test which prints what global scope (e.g. worker type) it's running in.
Typically, tests will check for the presence of the feature they need (like HTMLCanvasElement)
and skip if it's not available.
`Test which prints what global scope (e.g. worker type) it's running in, and info about
the adapter and device that it gets.
Note, other tests should not check the global scope type to detect features; instead, they should
check for the presence of the feature they need (like HTMLCanvasElement) and skip if not available.
Run this test under various configurations to see different results
(Window, worker scopes, Node, etc.)
Expand All @@ -36,8 +38,19 @@ in the logs. On non-WPT runtimes, it will also print to the console with console
WPT disallows console.log and doesn't support logs on passing tests, so this does nothing on WPT.`
)
.fn(t => {
const isCompatibilityMode = (t.adapter as unknown as { isCompatibilityMode?: boolean })
.isCompatibilityMode;
// `t.device` will be the default device, because no additional capabilities were requested.
const defaultDeviceProperties = Object.fromEntries(
(function* () {
const device = t.device as unknown as Record<string, unknown>;
for (const key in device) {
// Skip things that we don't want to JSON.stringify.
if (['lost', 'queue', 'onuncapturederror', 'label'].includes(key)) {
continue;
}
yield [key, device[key]];
}
})()
);

const info = JSON.stringify(
{
Expand All @@ -46,16 +59,14 @@ WPT disallows console.log and doesn't support logs on passing tests, so this doe
globalTestConfig,
baseResourcePath: getResourcePath(''),
defaultRequestAdapterOptions: getDefaultRequestAdapterOptions(),
adapter: {
isFallbackAdapter: t.adapter.isFallbackAdapter,
isCompatibilityMode,
info: t.adapter.info,
features: Array.from(t.adapter.features),
limits: t.adapter.limits,
},
// Print all of the properties of the adapter and defaultDeviceProperties. JSON.stringify
// will skip methods (e.g. adapter.requestDevice), because they're not stringifiable.
adapter: t.adapter,
defaultDevice: defaultDeviceProperties,
},
// Flatten objects with prototype chains into plain objects, using `for..in`. (Otherwise,
// properties from the prototype chain will be ignored and things will print as `{}`.)
// - Expand iterable things into arrays.
// - Flatten objects with prototype chains into plain objects, using `for..in`. (Otherwise,
// properties from the prototype chain will be ignored and things will print as `{}`.)
(_key, value) => {
if (value === undefined || value === null) return null;
if (typeof value !== 'object') return value;
Expand All @@ -65,7 +76,12 @@ WPT disallows console.log and doesn't support logs on passing tests, so this doe
return Object.fromEntries(
(function* () {
for (const key in valueObj) {
yield [key, valueObj[key]];
const value = valueObj[key];
if (value instanceof Object && Symbol.iterator in value) {
yield [key, Array.from(value as Iterable<unknown>)];
} else {
yield [key, value];
}
}
})()
);
Expand Down

0 comments on commit 7d39682

Please sign in to comment.