use wasm_bindgen::prelude::*;
use crate::backend::{BackendSelection, BackendType, SelectionStrategy};
use crate::gpu::GpuBackend;
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GpuBackendWasm {
Vulkan = 0,
Metal = 1,
Dx12 = 2,
BrowserWebGpu = 3,
Gl = 4,
None = 5,
}
impl From<GpuBackend> for GpuBackendWasm {
#[allow(clippy::match_same_arms)]
fn from(backend: GpuBackend) -> Self {
match backend {
GpuBackend::Vulkan => Self::Vulkan,
GpuBackend::Metal => Self::Metal,
GpuBackend::Dx12 => Self::Dx12,
GpuBackend::Dx11 => Self::Gl, GpuBackend::OpenGl => Self::Gl,
GpuBackend::BrowserWebGpu => Self::BrowserWebGpu,
GpuBackend::None => Self::None,
}
}
}
impl From<GpuBackendWasm> for GpuBackend {
fn from(wasm: GpuBackendWasm) -> Self {
match wasm {
GpuBackendWasm::Vulkan => Self::Vulkan,
GpuBackendWasm::Metal => Self::Metal,
GpuBackendWasm::Dx12 => Self::Dx12,
GpuBackendWasm::BrowserWebGpu => Self::BrowserWebGpu,
GpuBackendWasm::Gl => Self::OpenGl,
GpuBackendWasm::None => Self::None,
}
}
}
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackendTypeWasm {
Simd = 0,
Gpu = 1,
Cpu = 2,
Auto = 3,
}
impl From<BackendType> for BackendTypeWasm {
fn from(backend: BackendType) -> Self {
match backend {
BackendType::Simd => Self::Simd,
BackendType::Gpu => Self::Gpu,
BackendType::Cpu => Self::Cpu,
BackendType::Auto => Self::Auto,
}
}
}
impl From<BackendTypeWasm> for BackendType {
fn from(wasm: BackendTypeWasm) -> Self {
match wasm {
BackendTypeWasm::Simd => Self::Simd,
BackendTypeWasm::Gpu => Self::Gpu,
BackendTypeWasm::Cpu => Self::Cpu,
BackendTypeWasm::Auto => Self::Auto,
}
}
}
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SelectionStrategyWasm {
PreferGpu = 0,
PreferSimd = 1,
Automatic = 2,
Threshold = 3,
}
impl From<SelectionStrategy> for SelectionStrategyWasm {
fn from(strategy: SelectionStrategy) -> Self {
match strategy {
SelectionStrategy::PreferGpu => Self::PreferGpu,
SelectionStrategy::PreferSimd => Self::PreferSimd,
SelectionStrategy::Automatic => Self::Automatic,
SelectionStrategy::Threshold { .. } => Self::Threshold,
}
}
}
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct BackendSelectionWasm {
backend: BackendTypeWasm,
reason: String,
}
#[wasm_bindgen]
impl BackendSelectionWasm {
#[wasm_bindgen(getter)]
pub fn backend(&self) -> BackendTypeWasm {
self.backend
}
#[wasm_bindgen(getter)]
pub fn reason(&self) -> String {
self.reason.clone()
}
#[wasm_bindgen(js_name = backendName)]
pub fn backend_name(&self) -> String {
match self.backend {
BackendTypeWasm::Simd => "SIMD".to_string(),
BackendTypeWasm::Gpu => "GPU".to_string(),
BackendTypeWasm::Cpu => "CPU".to_string(),
BackendTypeWasm::Auto => "Auto".to_string(),
}
}
#[wasm_bindgen(js_name = isGpu)]
pub fn is_gpu(&self) -> bool {
matches!(self.backend, BackendTypeWasm::Gpu)
}
#[wasm_bindgen(js_name = isSimd)]
pub fn is_simd(&self) -> bool {
matches!(self.backend, BackendTypeWasm::Simd)
}
}
impl From<BackendSelection> for BackendSelectionWasm {
fn from(selection: BackendSelection) -> Self {
Self {
backend: selection.backend.into(),
reason: selection.reason,
}
}
}