Skip to content

Commit

Permalink
Examples device selector fix (#6404)
Browse files Browse the repository at this point in the history
  • Loading branch information
slimbuck authored May 21, 2024
1 parent 84fae5a commit 9ff94f9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Examples can also contain comments which allow you to define the default configu
// @config NO_MINISTATS
// @config WEBGPU_DISABLED
// @config WEBGL_DISABLED
// @config WEBGL1_DISABLED
import * as pc from 'playcanvas';
...
```
Expand Down
57 changes: 27 additions & 30 deletions examples/iframe/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,49 +75,46 @@ export function clearImports() {
blobUrls.forEach(URL.revokeObjectURL);
}

const DEVICE_TYPES = ['webgpu', 'webgl2'];
const DEVICE_TYPES = ['webgpu', 'webgl2', 'webgl1'];

/**
* @param {{ WEBGPU_DISABLED: boolean; WEBGL_DISABLED: boolean; }} config - The configuration object.
* @param {{ WEBGPU_DISABLED: boolean; WEBGL_DISABLED: boolean; WEBGL1_DISABLED: boolean; }} config - The configuration object.
*/
function getDeviceType(config) {
if (params.deviceType && DEVICE_TYPES.includes(params.deviceType)) {
console.warn("Overwriting default deviceType from URL: ", params.deviceType);
return params.deviceType;
}

if (config.WEBGPU_DISABLED) {
return 'webgl2';
}

if (config.WEBGL_DISABLED) {
return 'webgpu';
}

if (params.deviceType) {
console.warn("Overwriting default deviceType from URL");
return params.deviceType;
}
const selectedDevice = localStorage.getItem('preferredGraphicsDevice');

const savedDevice = localStorage.getItem('preferredGraphicsDevice');
if (config.WEBGPU_ENABLED) {
let preferredDevice = 'webgpu';
if (isLinuxChrome()) {
preferredDevice = 'webgl2';
}
return savedDevice || preferredDevice;
}

switch (savedDevice) {
switch (selectedDevice) {
case 'webgpu':
console.warn('Picked WebGPU but example is not supported on WebGPU, defaulting to WebGL2');
return 'webgl2';
case 'webgl1':
if (config.WEBGPU_DISABLED) {
console.warn('Picked WebGPU but example is not supported on WebGPU, defaulting to WebGL2');
return 'webgl2';
}
break;
case 'webgl2':
return savedDevice;
default:
return 'webgl2';
if (config.WEBGL_DISABLED) {
console.warn('Picked WebGL2 but example is not supported on WebGL, defaulting to WebGPU');
return 'webgpu';
}
break;
case 'webgl1':
if (config.WEBGL_DISABLED) {
console.warn('Picked WebGL1 but example is not supported on WebGL, defaulting to WebGPU');
return 'webgpu';
}

if (config.WEBGL1_DISABLED) {
console.warn('Picked WebGL1 but example is not supported on WebGL1, defaulting to WebGL2');
return 'webgl2';
}
break;
}

return selectedDevice;
}

export let deviceType = 'webgl2';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @config WEBGL1_DISABLED
// @config DESCRIPTION <ul><li>Click to add sand<li>Shift-click to remove sand<li>Press space to reset.</ul>
import * as pc from 'playcanvas';
import { data } from '@examples/observer';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @config WEBGPU_DISABLED
// @config WEBGL1_DISABLED
import * as pc from 'playcanvas';
import files from '@examples/files';
import { deviceType, rootPath } from '@examples/utils';
Expand Down

0 comments on commit 9ff94f9

Please sign in to comment.