Skip to content

Commit 858fcb0

Browse files
committed
Add a feature test for WebGPU
1 parent 92e9aca commit 858fcb0

3 files changed

Lines changed: 38 additions & 10 deletions

File tree

assets/main.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,34 @@
1414
//
1515
// You should have received a copy of the GNU General Public License
1616
// along with Luminol. If not, see <http://www.gnu.org/licenses/>.
17-
import wasm_bindgen, { luminol_main_start } from '/luminol.js';
1817

19-
await wasm_bindgen();
20-
luminol_main_start();
18+
// Check if the user's browser supports WebGPU
19+
console.log('Checking for WebGPU support…');
20+
let gpu = false;
21+
try {
22+
let adapter = await navigator.gpu?.requestAdapter();
23+
gpu = typeof GPUAdapter === 'function' && adapter instanceof GPUAdapter;
24+
} catch (e) {}
25+
if (gpu) {
26+
console.log('WebGPU is supported. Using WebGPU backend if available.');
27+
} else {
28+
console.log('No support detected. Using WebGL backend if available.');
29+
}
30+
31+
// If WebGPU is supported, always use luminol.js
32+
// If WebGPU is not supported, use luminol_webgl.js if it's available or fallback to luminol.js
33+
let fallback = false;
34+
let luminol;
35+
if (gpu) {
36+
luminol = await import('/luminol.js');
37+
} else {
38+
try {
39+
luminol = await import('/luminol_webgl.js');
40+
fallback = true;
41+
} catch (e) {
42+
luminol = await import ('/luminol.js');
43+
}
44+
}
45+
46+
await luminol.default(fallback ? '/luminol_webgl_bg.wasm' : '/luminol_bg.wasm');
47+
luminol.luminol_main_start(fallback);

assets/worker.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
//
1515
// You should have received a copy of the GNU General Public License
1616
// along with Luminol. If not, see <http://www.gnu.org/licenses/>.
17-
import wasm_bindgen, { luminol_worker_start } from '/luminol.js';
1817

1918
onmessage = async function (e) {
20-
if (e.data[0] === 'init') {
21-
await wasm_bindgen(undefined, e.data[1]);
22-
await luminol_worker_start(e.data[2]);
23-
}
19+
const [fallback, memory, canvas] = e.data;
20+
21+
const luminol = await import(fallback ? '/luminol_webgl.js' : '/luminol.js');
22+
23+
await luminol.default(fallback ? '/luminol_webgl_bg.wasm' : '/luminol_bg.wasm', memory);
24+
await luminol.luminol_worker_start(canvas);
2425
};

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static WORKER_DATA: Lazy<AtomicRefCell<Option<WorkerData>>> =
146146

147147
#[cfg(target_arch = "wasm32")]
148148
#[wasm_bindgen]
149-
pub fn luminol_main_start() {
149+
pub fn luminol_main_start(fallback: bool) {
150150
let (panic_tx, mut panic_rx) = mpsc::unbounded_channel::<()>();
151151

152152
wasm_bindgen_futures::spawn_local(async move {
@@ -225,7 +225,7 @@ pub fn luminol_main_start() {
225225
.expect("failed to spawn web worker");
226226

227227
let message = js_sys::Array::new();
228-
message.push(&JsValue::from("init"));
228+
message.push(&JsValue::from(fallback));
229229
message.push(&wasm_bindgen::memory());
230230
message.push(&offscreen_canvas);
231231
let transfer = js_sys::Array::new();

0 commit comments

Comments
 (0)