use dom_struct::dom_struct;
use js::context::{JSContext, NoGC};
use js::realm::CurrentRealm;
use log::warn;
use malloc_size_of_derive::MallocSizeOf;
use script_bindings::DomTypes;
use script_bindings::cell::DomRefCell;
use script_bindings::codegen::GenericBindings::WebGPUBinding::{
GPUShaderModuleDescriptor, GPUShaderModuleMethods, GPUShaderModuleWrap,
};
use script_bindings::interfaces::{
HeapTracedPromiseHelpers, PromiseHelpers, StackRootPromiseHelpers,
};
use script_bindings::reflector::{DomGlobalGeneric, Reflector, reflect_dom_object_with_wrap};
use script_bindings::routed_promise::RoutedPromiseListener;
use webgpu_traits::{ShaderCompilationInfo, WebGPU, WebGPURequest, WebGPUShaderModule};
use crate::JSTraceable;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::USVString;
use crate::gpucompilationinfo::GPUCompilationInfo;
use crate::traits::{Equivalence, WebGPUGlobalTrait, WebGPUPromise, WebGPUPromiseCallbackTrait};
#[derive(JSTraceable, MallocSizeOf)]
struct DroppableGPUShaderModule {
#[no_trace]
channel: WebGPU,
#[no_trace]
shader_module: WebGPUShaderModule,
}
impl Drop for DroppableGPUShaderModule {
fn drop(&mut self) {
if let Err(e) = self
.channel
.0
.send(WebGPURequest::DropShaderModule(self.shader_module.0))
{
warn!(
"Failed to send DropShaderModule ({:?}) ({})",
self.shader_module.0, e
);
}
}
}
#[dom_struct]
pub struct GPUShaderModule<D: DomTypes> {
reflector_: Reflector,
label: DomRefCell<USVString>,
compilation_info_promise: <D::Promise as PromiseHelpers<D>>::HeapTraced,
droppable: DroppableGPUShaderModule,
}
impl<D: Equivalence> GPUShaderModule<D> {
fn new_inherited(
channel: WebGPU,
shader_module: WebGPUShaderModule,
label: USVString,
promise: &<D::Promise as PromiseHelpers<D>>::StackRoot,
) -> Self {
Self {
reflector_: Reflector::new(),
label: DomRefCell::new(label),
compilation_info_promise: promise.to_traced(),
droppable: DroppableGPUShaderModule {
channel,
shader_module,
},
}
}
pub(crate) fn new(
cx: &mut JSContext,
global: &D::GlobalScope,
channel: WebGPU,
shader_module: WebGPUShaderModule,
label: USVString,
promise: &<D::Promise as PromiseHelpers<D>>::StackRoot,
) -> DomRoot<Self> {
reflect_dom_object_with_wrap::<D, _, _>(
cx,
Box::new(GPUShaderModule::new_inherited(
channel,
shader_module,
label,
promise,
)),
global,
GPUShaderModuleWrap::<D>,
)
}
}
impl<D> GPUShaderModule<D>
where
D: Equivalence,
<D::Promise as PromiseHelpers<D>>::StackRoot: WebGPUPromise<D>,
{
pub(crate) fn id(&self) -> WebGPUShaderModule {
self.droppable.shader_module
}
pub(crate) fn create(
cx: &mut CurrentRealm<'_>,
device: &D::GPUDevice,
descriptor: &GPUShaderModuleDescriptor,
) -> DomRoot<GPUShaderModule<D>> {
let program_id = device
.global_from_reflector()
.global_wgpu_id_hub()
.create_shader_module_id();
let promise = D::Promise::new_in_realm(cx);
let shader_module = GPUShaderModule::new(
cx,
&*device.global_from_reflector(),
device.channel(),
WebGPUShaderModule(program_id),
descriptor.parent.label.clone(),
&promise,
);
let callback = promise.callback_promise_dom_manipulation_task_source(&*shader_module);
device
.channel()
.0
.send(WebGPURequest::CreateShaderModule {
device_id: device.id().0,
program_id,
program: descriptor.code.0.clone(),
label: None,
callback,
})
.expect("Failed to create WebGPU ShaderModule");
shader_module
}
}
impl<D: DomTypes> GPUShaderModuleMethods<D> for GPUShaderModule<D> {
fn Label(&self) -> USVString {
self.label.borrow().clone()
}
fn SetLabel(&self, no_gc: &NoGC, value: USVString) {
*self.label.safe_borrow_mut(no_gc) = value;
}
fn GetCompilationInfo(&self, cx: &JSContext) -> <D::Promise as PromiseHelpers<D>>::StackRoot {
self.compilation_info_promise.root(cx)
}
}
impl<D: Equivalence> RoutedPromiseListener<D, Option<ShaderCompilationInfo>> for GPUShaderModule<D>
where
Self: DomGlobalGeneric<D>,
{
fn handle_response(
&self,
cx: &mut js::context::JSContext,
response: Option<ShaderCompilationInfo>,
promise: &<D::Promise as PromiseHelpers<D>>::StackRoot,
) {
let info = GPUCompilationInfo::<D>::from(cx, &self.global_from_reflector(), response);
promise.resolve_native(cx, &info);
}
}