use dom_struct::dom_struct;
use js::jsapi::{HandleObject, Heap, JSObject};
use js::realm::CurrentRealm;
use jstraceable_derive::JSTraceable;
use log::warn;
use malloc_size_of_derive::MallocSizeOf;
use script_bindings::callback::CallbackContainer;
use script_bindings::codegen::GenericBindings::EventHandlerBinding::EventHandlerNonNull;
use script_bindings::codegen::GenericBindings::WebGPUBinding::{
GPUAdapterMethods, GPUAdapterWrap, GPUDeviceDescriptor, GPUDeviceLostReason,
};
use script_bindings::interfaces::{GlobalScopeHelpers, PromiseHelpers};
use script_bindings::like::Setlike;
use script_bindings::reflector::{DomGlobalGeneric, Reflector, reflect_dom_object_with_wrap};
use script_bindings::routed_promise::RoutedPromiseListener;
use script_bindings::{DomTypes, cformat};
use webgpu_traits::{
AdapterInfo, DeviceDescriptor, DeviceType, ExperimentalFeatures, Features, Limits, MemoryHints,
RequestDeviceError, Trace, WebGPU, WebGPUAdapter, WebGPUDeviceResponse, WebGPURequest,
};
use crate::dom::bindings::error::Error;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::gpuadapterinfo::GPUAdapterInfo;
use crate::gpudevice::GPUDevice;
use crate::gpusupportedfeatures::{GPUSupportedFeatures, gpu_to_wgt_feature};
use crate::gpusupportedlimits::{GPUSupportedLimits, set_limit};
use crate::traits::{Equivalence, WebGPUGlobalTrait, WebGPUPromise, WebGPUPromiseCallbackTrait};
#[derive(JSTraceable, MallocSizeOf)]
struct DroppableGPUAdapter {
#[no_trace]
channel: WebGPU,
#[no_trace]
adapter: WebGPUAdapter,
}
impl Drop for DroppableGPUAdapter {
fn drop(&mut self) {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::DropAdapter(self.adapter.0))
{
warn!(
"Failed to send WebGPURequest::DropAdapter({:?}) ({})",
self.adapter.0, e
);
};
}
}
#[dom_struct]
pub struct GPUAdapter<D: DomTypes> {
reflector_: Reflector,
name: DOMString,
#[ignore_malloc_size_of = "mozjs"]
extensions: Heap<*mut JSObject>,
features: Dom<GPUSupportedFeatures<D>>,
limits: Dom<GPUSupportedLimits<D>>,
info: Dom<GPUAdapterInfo<D>>,
droppable: DroppableGPUAdapter,
}
impl<D> GPUAdapter<D>
where
D: Equivalence,
{
fn new_inherited(
channel: WebGPU,
name: DOMString,
features: &GPUSupportedFeatures<D>,
limits: &GPUSupportedLimits<D>,
info: &GPUAdapterInfo<D>,
adapter: WebGPUAdapter,
) -> Self {
Self {
reflector_: Reflector::new(),
name,
extensions: Heap::default(),
features: Dom::from_ref(features),
limits: Dom::from_ref(limits),
info: Dom::from_ref(info),
droppable: DroppableGPUAdapter { channel, adapter },
}
}
#[allow(clippy::too_many_arguments)]
pub fn new(
cx: &mut js::context::JSContext,
global: &D::GlobalScope,
channel: WebGPU,
name: DOMString,
extensions: HandleObject,
features: Features,
limits: Limits,
info: AdapterInfo,
adapter: WebGPUAdapter,
) -> DomRoot<Self> {
let features = GPUSupportedFeatures::Constructor(cx, global, None, features).unwrap();
let limits = GPUSupportedLimits::new(cx, global, limits);
let info = GPUAdapter::create_adapter_info(cx, global, info, &features);
let dom_root = reflect_dom_object_with_wrap::<D, _, _>(
cx,
Box::new(GPUAdapter::new_inherited(
channel, name, &features, &limits, &info, adapter,
)),
global,
GPUAdapterWrap::<D>,
);
dom_root.extensions.set(*extensions);
dom_root
}
fn create_adapter_info(
cx: &mut js::context::JSContext,
global: &D::GlobalScope,
info: AdapterInfo,
features: &GPUSupportedFeatures<D>,
) -> DomRoot<GPUAdapterInfo<D>> {
let vendor = if info.vendor != 0 {
info.vendor.to_string().into()
} else {
DOMString::new()
};
let architecture = DOMString::new();
let device = if info.device != 0 {
info.device.to_string().into()
} else {
DOMString::new()
};
let description = info.name.clone().into();
let (subgroup_min_size, subgroup_max_size) =
if features.has(cx, DOMString::from_static("subgroups")) {
(info.subgroup_min_size, info.subgroup_max_size)
} else {
(4, 128)
};
let is_fallback_adapter = info.device_type == DeviceType::Cpu;
GPUAdapterInfo::new(
cx,
global,
vendor,
architecture,
device,
description,
subgroup_min_size,
subgroup_max_size,
is_fallback_adapter,
)
}
pub fn channel(&self) -> WebGPU {
self.droppable.channel.clone()
}
pub fn info(&self) -> DomRoot<GPUAdapterInfo<D>> {
DomRoot::from_ref(&self.info)
}
}
impl<D> GPUAdapterMethods<D> for GPUAdapter<D>
where
D: Equivalence,
<D::Promise as PromiseHelpers<D>>::StackRoot: WebGPUPromise<D>,
{
fn RequestDevice(
&self,
cx: &mut CurrentRealm<'_>,
descriptor: &GPUDeviceDescriptor,
) -> <D::Promise as PromiseHelpers<D>>::StackRoot {
let promise = D::Promise::new_in_realm(cx);
let callback = promise.callback_promise_dom_manipulation_task_source(self);
let mut required_features = Features::empty();
for &ext in descriptor.requiredFeatures.iter() {
if let Some(feature) = gpu_to_wgt_feature(ext) {
required_features.insert(feature);
} else {
promise.reject_error(
cx,
Error::Type(cformat!("{} is not supported feature", ext.as_str())),
);
return promise;
}
}
let mut required_limits = Limits::default();
if let Some(limits) = &descriptor.requiredLimits {
for (limit, value) in (*limits).iter() {
if !set_limit(&mut required_limits, &limit.str(), *value) {
warn!("Unknown GPUDevice limit: {limit}");
promise.reject_error(
cx,
Error::Operation(Some(format!("Unknown GPUDevice limit: {limit}"))),
);
return promise;
}
}
}
let desc = DeviceDescriptor {
required_features,
required_limits,
label: Some(descriptor.parent.label.to_string()),
memory_hints: MemoryHints::MemoryUsage,
trace: Trace::Off,
experimental_features: ExperimentalFeatures::disabled(),
};
let device_id = self
.global_from_reflector()
.global_wgpu_id_hub()
.create_device_id();
let queue_id = self
.global_from_reflector()
.global_wgpu_id_hub()
.create_queue_id();
let pipeline_id = self.global_from_reflector().pipeline_id();
if self
.droppable
.channel
.0
.send(WebGPURequest::RequestDevice {
sender: callback,
adapter_id: self.droppable.adapter,
descriptor: desc,
device_id,
queue_id,
pipeline_id,
})
.is_err()
{
promise.reject_error(
cx,
Error::Operation(Some("Could not Request GPU Device".to_string())),
);
}
promise
}
fn Features(&self) -> DomRoot<GPUSupportedFeatures<D>> {
DomRoot::from_ref(&self.features)
}
fn Limits(&self) -> DomRoot<GPUSupportedLimits<D>> {
DomRoot::from_ref(&self.limits)
}
fn Info(&self) -> DomRoot<GPUAdapterInfo<D>> {
DomRoot::from_ref(&self.info)
}
}
impl<D: Equivalence> RoutedPromiseListener<D, WebGPUDeviceResponse> for GPUAdapter<D>
where
Self: DomGlobalGeneric<D>,
EventHandlerNonNull<D>: CallbackContainer,
{
fn handle_response(
&self,
cx: &mut js::context::JSContext,
response: WebGPUDeviceResponse,
promise: &<D::Promise as PromiseHelpers<D>>::StackRoot,
) {
match response {
(device_id, queue_id, Ok(descriptor)) => {
let device = GPUDevice::<D>::new(
cx,
&self.global_from_reflector(),
self.channel(),
self,
HandleObject::null(),
descriptor.required_features,
descriptor.required_limits,
device_id,
queue_id,
descriptor.label.unwrap_or_default(),
);
self.global_from_reflector().add_webgpu_device(&device);
promise.resolve_native(cx, &device);
},
(_, _, Err(RequestDeviceError::UnsupportedFeature(f))) => promise.reject_error(
cx,
Error::Type(cformat!("Unsupported features were requested: {}", f)),
),
(_, _, Err(RequestDeviceError::LimitsExceeded(l))) => {
warn!("{}", l);
promise.reject_error(
cx,
Error::Operation(Some("WebGPU Device Limit exceeded".to_string())),
)
},
(device_id, queue_id, Err(RequestDeviceError::Other(e))) => {
let device = GPUDevice::<D>::new(
cx,
&self.global_from_reflector(),
self.channel(),
self,
HandleObject::null(),
Features::default(),
Limits::default(),
device_id,
queue_id,
String::new(),
);
device.lose(GPUDeviceLostReason::Unknown, e);
promise.resolve_native(cx, &device);
},
}
}
}