diff --git a/ext/webgpu/wrap/adapter.rs b/ext/webgpu/adapter.rs similarity index 98% rename from ext/webgpu/wrap/adapter.rs rename to ext/webgpu/adapter.rs index 807ad2665b..c0e531d519 100644 --- a/ext/webgpu/wrap/adapter.rs +++ b/ext/webgpu/adapter.rs @@ -12,8 +12,8 @@ use deno_core::WebIDL; use tokio::sync::Mutex; use super::device::GPUDevice; -use crate::wrap::webidl::features_to_feature_names; -use crate::wrap::webidl::GPUFeatureName; +use crate::webidl::features_to_feature_names; +use crate::webidl::GPUFeatureName; use crate::Instance; #[derive(WebIDL)] diff --git a/ext/webgpu/wrap/bind_group.rs b/ext/webgpu/bind_group.rs similarity index 95% rename from ext/webgpu/wrap/bind_group.rs rename to ext/webgpu/bind_group.rs index 001049fa60..80efba6efd 100644 --- a/ext/webgpu/wrap/bind_group.rs +++ b/ext/webgpu/bind_group.rs @@ -14,9 +14,9 @@ use deno_core::webidl::WebIdlInterfaceConverter; use deno_core::GarbageCollected; use deno_core::WebIDL; -use crate::wrap::buffer::GPUBuffer; -use crate::wrap::sampler::GPUSampler; -use crate::wrap::texture::GPUTextureView; +use crate::buffer::GPUBuffer; +use crate::sampler::GPUSampler; +use crate::texture::GPUTextureView; use crate::Instance; pub struct GPUBindGroup { diff --git a/ext/webgpu/wrap/bind_group_layout.rs b/ext/webgpu/bind_group_layout.rs similarity index 98% rename from ext/webgpu/wrap/bind_group_layout.rs rename to ext/webgpu/bind_group_layout.rs index c21a348ae9..3ebe594620 100644 --- a/ext/webgpu/wrap/bind_group_layout.rs +++ b/ext/webgpu/bind_group_layout.rs @@ -4,7 +4,7 @@ use deno_core::op2; use deno_core::GarbageCollected; use deno_core::WebIDL; -use crate::wrap::texture::GPUTextureViewDimension; +use crate::texture::GPUTextureViewDimension; use crate::Instance; pub struct GPUBindGroupLayout { diff --git a/ext/webgpu/wrap/buffer.rs b/ext/webgpu/buffer.rs similarity index 100% rename from ext/webgpu/wrap/buffer.rs rename to ext/webgpu/buffer.rs diff --git a/ext/webgpu/wrap/command_buffer.rs b/ext/webgpu/command_buffer.rs similarity index 100% rename from ext/webgpu/wrap/command_buffer.rs rename to ext/webgpu/command_buffer.rs diff --git a/ext/webgpu/wrap/command_encoder.rs b/ext/webgpu/command_encoder.rs similarity index 94% rename from ext/webgpu/wrap/command_encoder.rs rename to ext/webgpu/command_encoder.rs index fbb515323b..a468afbf9d 100644 --- a/ext/webgpu/wrap/command_encoder.rs +++ b/ext/webgpu/command_encoder.rs @@ -11,14 +11,14 @@ use deno_error::JsErrorBox; use wgpu_core::command::ImageCopyBuffer; use wgpu_core::command::PassChannel; -use crate::wrap::buffer::GPUBuffer; -use crate::wrap::command_buffer::GPUCommandBuffer; -use crate::wrap::compute_pass::GPUComputePassEncoder; -use crate::wrap::queue::GPUTexelCopyTextureInfo; -use crate::wrap::render_pass::GPULoadOp; -use crate::wrap::render_pass::GPURenderPassEncoder; -use crate::wrap::render_pass::GPUStoreOp; -use crate::wrap::webidl::GPUExtent3D; +use crate::buffer::GPUBuffer; +use crate::command_buffer::GPUCommandBuffer; +use crate::compute_pass::GPUComputePassEncoder; +use crate::queue::GPUTexelCopyTextureInfo; +use crate::render_pass::GPULoadOp; +use crate::render_pass::GPURenderPassEncoder; +use crate::render_pass::GPUStoreOp; +use crate::webidl::GPUExtent3D; use crate::Instance; pub struct GPUCommandEncoder { @@ -54,7 +54,7 @@ impl GPUCommandEncoder { #[cppgc] fn begin_render_pass( &self, - #[webidl] descriptor: crate::wrap::render_pass::GPURenderPassDescriptor, + #[webidl] descriptor: crate::render_pass::GPURenderPassDescriptor, ) -> Result { let color_attachments = Cow::Owned( descriptor @@ -149,7 +149,7 @@ impl GPUCommandEncoder { #[cppgc] fn begin_compute_pass( &self, - #[webidl] descriptor: crate::wrap::compute_pass::GPUComputePassDescriptor, + #[webidl] descriptor: crate::compute_pass::GPUComputePassDescriptor, ) -> GPUComputePassEncoder { let timestamp_writes = descriptor.timestamp_writes.map(|timestamp_writes| { @@ -348,8 +348,7 @@ impl GPUCommandEncoder { #[cppgc] fn finish( &self, - #[webidl] - descriptor: crate::wrap::command_buffer::GPUCommandBufferDescriptor, + #[webidl] descriptor: crate::command_buffer::GPUCommandBufferDescriptor, ) -> GPUCommandBuffer { let wgpu_descriptor = wgpu_types::CommandBufferDescriptor { label: Some(Cow::Owned(descriptor.label.clone())), diff --git a/ext/webgpu/wrap/compute_pass.rs b/ext/webgpu/compute_pass.rs similarity index 95% rename from ext/webgpu/wrap/compute_pass.rs rename to ext/webgpu/compute_pass.rs index 52f3b87a1a..f9c837ed33 100644 --- a/ext/webgpu/wrap/compute_pass.rs +++ b/ext/webgpu/compute_pass.rs @@ -40,7 +40,7 @@ impl GPUComputePassEncoder { fn set_pipeline( &self, - #[webidl] pipeline: Ptr, + #[webidl] pipeline: Ptr, ) { let err = self .instance @@ -74,7 +74,7 @@ impl GPUComputePassEncoder { fn dispatch_workgroups_indirect( &self, - #[webidl] indirect_buffer: Ptr, + #[webidl] indirect_buffer: Ptr, #[webidl(options(enforce_range = true))] indirect_offset: u64, ) { let err = self @@ -134,7 +134,7 @@ impl GPUComputePassEncoder { &self, scope: &mut v8::HandleScope<'a>, #[webidl(options(enforce_range = true))] index: u32, - #[webidl] bind_group: Nullable>, + #[webidl] bind_group: Nullable>, dynamic_offsets: v8::Local<'a, v8::Value>, dynamic_offsets_data_start: v8::Local<'a, v8::Value>, dynamic_offsets_data_length: v8::Local<'a, v8::Value>, @@ -225,7 +225,7 @@ pub(crate) struct GPUComputePassDescriptor { #[derive(WebIDL)] #[webidl(dictionary)] pub(crate) struct GPUComputePassTimestampWrites { - pub query_set: Ptr, + pub query_set: Ptr, #[options(enforce_range = true)] pub beginning_of_pass_write_index: Option, #[options(enforce_range = true)] diff --git a/ext/webgpu/wrap/compute_pipeline.rs b/ext/webgpu/compute_pipeline.rs similarity index 92% rename from ext/webgpu/wrap/compute_pipeline.rs rename to ext/webgpu/compute_pipeline.rs index ce17778900..c8c1d4a888 100644 --- a/ext/webgpu/wrap/compute_pipeline.rs +++ b/ext/webgpu/compute_pipeline.rs @@ -7,9 +7,9 @@ use deno_core::GarbageCollected; use deno_core::WebIDL; use indexmap::IndexMap; -use crate::wrap::bind_group_layout::GPUBindGroupLayout; -use crate::wrap::shader::GPUShaderModule; -use crate::wrap::webidl::GPUPipelineLayoutOrGPUAutoLayoutMode; +use crate::bind_group_layout::GPUBindGroupLayout; +use crate::shader::GPUShaderModule; +use crate::webidl::GPUPipelineLayoutOrGPUAutoLayoutMode; use crate::Instance; pub struct GPUComputePipeline { diff --git a/ext/webgpu/wrap/device.rs b/ext/webgpu/device.rs similarity index 98% rename from ext/webgpu/wrap/device.rs rename to ext/webgpu/device.rs index f822dd111e..e2adcbbe8d 100644 --- a/ext/webgpu/wrap/device.rs +++ b/ext/webgpu/device.rs @@ -22,14 +22,14 @@ use super::queue::GPUQueue; use super::sampler::GPUSampler; use super::shader::GPUShaderModule; use super::texture::GPUTexture; -use crate::wrap::adapter::GPUAdapterInfo; -use crate::wrap::adapter::GPUSupportedFeatures; -use crate::wrap::adapter::GPUSupportedLimits; -use crate::wrap::command_encoder::GPUCommandEncoder; -use crate::wrap::query_set::GPUQuerySet; -use crate::wrap::render_bundle::GPURenderBundleEncoder; -use crate::wrap::render_pipeline::GPURenderPipeline; -use crate::wrap::webidl::features_to_feature_names; +use crate::adapter::GPUAdapterInfo; +use crate::adapter::GPUSupportedFeatures; +use crate::adapter::GPUSupportedLimits; +use crate::command_encoder::GPUCommandEncoder; +use crate::query_set::GPUQuerySet; +use crate::render_bundle::GPURenderBundleEncoder; +use crate::render_pipeline::GPURenderPipeline; +use crate::webidl::features_to_feature_names; use crate::Instance; pub struct GPUDevice { @@ -554,7 +554,7 @@ impl GPUDevice { #[cppgc] fn create_query_set( &self, - #[webidl] descriptor: crate::wrap::query_set::GPUQuerySetDescriptor, + #[webidl] descriptor: crate::query_set::GPUQuerySetDescriptor, ) -> GPUQuerySet { let wgpu_descriptor = wgpu_core::resource::QuerySetDescriptor { label: Some(Cow::Owned(descriptor.label.clone())), diff --git a/ext/webgpu/wrap/error.rs b/ext/webgpu/error.rs similarity index 100% rename from ext/webgpu/wrap/error.rs rename to ext/webgpu/error.rs diff --git a/ext/webgpu/lib.rs b/ext/webgpu/lib.rs index 58f3d6ceac..ff37eb765c 100644 --- a/ext/webgpu/lib.rs +++ b/ext/webgpu/lib.rs @@ -2,48 +2,152 @@ #![cfg(not(target_arch = "wasm32"))] #![warn(unsafe_op_in_unsafe_fn)] +use std::cell::RefCell; +use std::rc::Rc; +use std::sync::Arc; + +use deno_core::cppgc::SameObject; +use deno_core::op2; +use deno_core::GarbageCollected; +use deno_core::OpState; pub use wgpu_core; pub use wgpu_types; +use wgpu_types::PowerPreference; + +mod adapter; +mod bind_group; +mod bind_group_layout; +mod buffer; +mod command_buffer; +mod command_encoder; +mod compute_pass; +mod compute_pipeline; +mod device; +mod error; +mod pipeline_layout; +mod query_set; +mod queue; +mod render_bundle; +mod render_pass; +mod render_pipeline; +mod sampler; +mod shader; +mod texture; +mod webidl; pub const UNSTABLE_FEATURE_NAME: &str = "webgpu"; //pub mod byow; //pub mod surface; -mod wrap; -pub type Instance = std::sync::Arc; +pub type Instance = Arc; deno_core::extension!( deno_webgpu, deps = [deno_webidl, deno_web], - ops = [wrap::create_gpu], + ops = [create_gpu], objects = [ - wrap::GPU, - wrap::adapter::GPUAdapter, - wrap::adapter::GPUAdapterInfo, - wrap::bind_group::GPUBindGroup, - wrap::bind_group_layout::GPUBindGroupLayout, - wrap::buffer::GPUBuffer, - wrap::command_buffer::GPUCommandBuffer, - wrap::command_encoder::GPUCommandEncoder, - wrap::compute_pass::GPUComputePassEncoder, - wrap::compute_pipeline::GPUComputePipeline, - wrap::device::GPUDevice, - wrap::device::GPUDeviceLostInfo, - wrap::pipeline_layout::GPUPipelineLayout, - wrap::query_set::GPUQuerySet, - wrap::queue::GPUQueue, - wrap::render_bundle::GPURenderBundle, - wrap::render_bundle::GPURenderBundleEncoder, - wrap::render_pass::GPURenderPassEncoder, - wrap::render_pipeline::GPURenderPipeline, - wrap::sampler::GPUSampler, - wrap::shader::GPUShaderModule, - wrap::adapter::GPUSupportedFeatures, - wrap::adapter::GPUSupportedLimits, - wrap::texture::GPUTexture, - wrap::texture::GPUTextureView, + GPU, + adapter::GPUAdapter, + adapter::GPUAdapterInfo, + bind_group::GPUBindGroup, + bind_group_layout::GPUBindGroupLayout, + buffer::GPUBuffer, + command_buffer::GPUCommandBuffer, + command_encoder::GPUCommandEncoder, + compute_pass::GPUComputePassEncoder, + compute_pipeline::GPUComputePipeline, + device::GPUDevice, + device::GPUDeviceLostInfo, + pipeline_layout::GPUPipelineLayout, + query_set::GPUQuerySet, + queue::GPUQueue, + render_bundle::GPURenderBundle, + render_bundle::GPURenderBundleEncoder, + render_pass::GPURenderPassEncoder, + render_pipeline::GPURenderPipeline, + sampler::GPUSampler, + shader::GPUShaderModule, + adapter::GPUSupportedFeatures, + adapter::GPUSupportedLimits, + texture::GPUTexture, + texture::GPUTextureView, ], esm = ["00_init.js"], lazy_loaded_esm = ["01_webgpu.js"], ); + +#[op2] +#[cppgc] +pub fn create_gpu() -> GPU { + GPU +} + +pub struct GPU; + +impl GarbageCollected for GPU {} + +#[op2] +impl GPU { + #[async_method] + #[cppgc] + async fn request_adapter( + &self, + state: Rc>, + #[webidl] options: adapter::GPURequestAdapterOptions, + ) -> Option { + let mut state = state.borrow_mut(); + + let backends = std::env::var("DENO_WEBGPU_BACKEND").map_or_else( + |_| wgpu_types::Backends::all(), + |s| wgpu_core::instance::parse_backends_from_comma_list(&s), + ); + let instance = if let Some(instance) = state.try_borrow::() { + instance + } else { + state.put(Arc::new(wgpu_core::global::Global::new( + "webgpu", + wgpu_types::InstanceDescriptor { + backends, + flags: wgpu_types::InstanceFlags::from_build_config(), + dx12_shader_compiler: wgpu_types::Dx12Compiler::Fxc, + gles_minor_version: wgpu_types::Gles3MinorVersion::default(), + }, + ))); + state.borrow::() + }; + + let descriptor = wgpu_core::instance::RequestAdapterOptions { + power_preference: options + .power_preference + .map(|pp| match pp { + adapter::GPUPowerPreference::LowPower => PowerPreference::LowPower, + adapter::GPUPowerPreference::HighPerformance => { + PowerPreference::HighPerformance + } + }) + .unwrap_or_default(), + force_fallback_adapter: options.force_fallback_adapter, + compatible_surface: None, // windowless + }; + let id = instance.request_adapter(&descriptor, backends, None).ok()?; + + Some(adapter::GPUAdapter { + instance: instance.clone(), + features: SameObject::new(), + limits: SameObject::new(), + info: Rc::new(SameObject::new()), + id, + }) + } + + #[string] + fn getPreferredCanvasFormat(&self) -> &'static str { + // https://github.com/mozilla/gecko-dev/blob/b75080bb8b11844d18cb5f9ac6e68a866ef8e243/dom/webgpu/Instance.h#L42-L47 + if cfg!(target_os = "android") { + texture::GPUTextureFormat::Rgba8unorm.as_str() + } else { + texture::GPUTextureFormat::Bgra8unorm.as_str() + } + } +} diff --git a/ext/webgpu/wrap/pipeline_layout.rs b/ext/webgpu/pipeline_layout.rs similarity index 100% rename from ext/webgpu/wrap/pipeline_layout.rs rename to ext/webgpu/pipeline_layout.rs diff --git a/ext/webgpu/wrap/query_set.rs b/ext/webgpu/query_set.rs similarity index 100% rename from ext/webgpu/wrap/query_set.rs rename to ext/webgpu/query_set.rs diff --git a/ext/webgpu/wrap/queue.rs b/ext/webgpu/queue.rs similarity index 93% rename from ext/webgpu/wrap/queue.rs rename to ext/webgpu/queue.rs index 8dadcdd4b6..a97affe70a 100644 --- a/ext/webgpu/wrap/queue.rs +++ b/ext/webgpu/queue.rs @@ -6,12 +6,12 @@ use deno_core::GarbageCollected; use deno_core::WebIDL; use deno_error::JsErrorBox; -use crate::wrap::buffer::GPUBuffer; -use crate::wrap::command_buffer::GPUCommandBuffer; -use crate::wrap::texture::GPUTexture; -use crate::wrap::texture::GPUTextureAspect; -use crate::wrap::webidl::GPUExtent3D; -use crate::wrap::webidl::GPUOrigin3D; +use crate::buffer::GPUBuffer; +use crate::command_buffer::GPUCommandBuffer; +use crate::texture::GPUTexture; +use crate::texture::GPUTextureAspect; +use crate::webidl::GPUExtent3D; +use crate::webidl::GPUOrigin3D; use crate::Instance; pub struct GPUQueue { diff --git a/ext/webgpu/wrap/render_bundle.rs b/ext/webgpu/render_bundle.rs similarity index 97% rename from ext/webgpu/wrap/render_bundle.rs rename to ext/webgpu/render_bundle.rs index 28ba01a0f3..4fef111736 100644 --- a/ext/webgpu/wrap/render_bundle.rs +++ b/ext/webgpu/render_bundle.rs @@ -16,8 +16,8 @@ use deno_core::GarbageCollected; use deno_core::WebIDL; use deno_error::JsErrorBox; -use crate::wrap::buffer::GPUBuffer; -use crate::wrap::texture::GPUTextureFormat; +use crate::buffer::GPUBuffer; +use crate::texture::GPUTextureFormat; use crate::Instance; pub struct GPURenderBundleEncoder { @@ -121,7 +121,7 @@ impl GPURenderBundleEncoder { &self, scope: &mut v8::HandleScope<'a>, #[webidl(options(enforce_range = true))] index: u32, - #[webidl] bind_group: Nullable>, + #[webidl] bind_group: Nullable>, dynamic_offsets: v8::Local<'a, v8::Value>, dynamic_offsets_data_start: v8::Local<'a, v8::Value>, dynamic_offsets_data_length: v8::Local<'a, v8::Value>, @@ -202,7 +202,7 @@ impl GPURenderBundleEncoder { fn set_pipeline( &self, - #[webidl] pipeline: Ptr, + #[webidl] pipeline: Ptr, ) -> Result<(), JsErrorBox> { let mut encoder = self.encoder.borrow_mut(); let encoder = encoder.as_mut().ok_or_else(|| { @@ -220,7 +220,7 @@ impl GPURenderBundleEncoder { fn set_index_buffer( &self, #[webidl] buffer: Ptr, - #[webidl] index_format: crate::wrap::render_pipeline::GPUIndexFormat, + #[webidl] index_format: crate::render_pipeline::GPUIndexFormat, #[webidl(default = 0, options(enforce_range = true))] offset: u64, #[webidl(options(enforce_range = true))] size: Option, ) -> Result<(), JsErrorBox> { diff --git a/ext/webgpu/wrap/render_pass.rs b/ext/webgpu/render_pass.rs similarity index 95% rename from ext/webgpu/wrap/render_pass.rs rename to ext/webgpu/render_pass.rs index 05f931085f..9b84ecb3ff 100644 --- a/ext/webgpu/wrap/render_pass.rs +++ b/ext/webgpu/render_pass.rs @@ -14,10 +14,10 @@ use deno_core::webidl::WebIdlError; use deno_core::GarbageCollected; use deno_core::WebIDL; -use crate::wrap::buffer::GPUBuffer; -use crate::wrap::render_bundle::GPURenderBundle; -use crate::wrap::texture::GPUTextureView; -use crate::wrap::webidl::GPUColor; +use crate::buffer::GPUBuffer; +use crate::render_bundle::GPURenderBundle; +use crate::texture::GPUTextureView; +use crate::webidl::GPUColor; use crate::Instance; pub struct GPURenderPassEncoder { @@ -201,7 +201,7 @@ impl GPURenderPassEncoder { &self, scope: &mut v8::HandleScope<'a>, #[webidl(options(enforce_range = true))] index: u32, - #[webidl] bind_group: Nullable>, + #[webidl] bind_group: Nullable>, dynamic_offsets: v8::Local<'a, v8::Value>, dynamic_offsets_data_start: v8::Local<'a, v8::Value>, dynamic_offsets_data_length: v8::Local<'a, v8::Value>, @@ -281,7 +281,7 @@ impl GPURenderPassEncoder { fn set_pipeline( &self, - #[webidl] pipeline: Ptr, + #[webidl] pipeline: Ptr, ) { let err = self .instance @@ -294,7 +294,7 @@ impl GPURenderPassEncoder { fn set_index_buffer( &self, #[webidl] buffer: Ptr, - #[webidl] index_format: crate::wrap::render_pipeline::GPUIndexFormat, + #[webidl] index_format: crate::render_pipeline::GPUIndexFormat, #[webidl(default = 0, options(enforce_range = true))] offset: u64, #[webidl(options(enforce_range = true))] size: Option, ) { @@ -419,7 +419,7 @@ pub(crate) struct GPURenderPassDescriptor { pub color_attachments: Vec>, pub depth_stencil_attachment: Option, - pub occlusion_query_set: Option>, + pub occlusion_query_set: Option>, pub timestamp_writes: Option, #[webidl(default = 50000000)] #[options(enforce_range = true)] @@ -489,7 +489,7 @@ pub(crate) struct GPURenderPassDepthStencilAttachment { #[derive(WebIDL)] #[webidl(dictionary)] pub(crate) struct GPURenderPassTimestampWrites { - pub query_set: Ptr, + pub query_set: Ptr, #[options(enforce_range = true)] pub beginning_of_pass_write_index: Option, #[options(enforce_range = true)] diff --git a/ext/webgpu/wrap/render_pipeline.rs b/ext/webgpu/render_pipeline.rs similarity index 98% rename from ext/webgpu/wrap/render_pipeline.rs rename to ext/webgpu/render_pipeline.rs index 254c7541f2..27dfd7f590 100644 --- a/ext/webgpu/wrap/render_pipeline.rs +++ b/ext/webgpu/render_pipeline.rs @@ -8,11 +8,11 @@ use deno_core::GarbageCollected; use deno_core::WebIDL; use indexmap::IndexMap; -use crate::wrap::bind_group_layout::GPUBindGroupLayout; -use crate::wrap::sampler::GPUCompareFunction; -use crate::wrap::shader::GPUShaderModule; -use crate::wrap::texture::GPUTextureFormat; -use crate::wrap::webidl::GPUPipelineLayoutOrGPUAutoLayoutMode; +use crate::bind_group_layout::GPUBindGroupLayout; +use crate::sampler::GPUCompareFunction; +use crate::shader::GPUShaderModule; +use crate::texture::GPUTextureFormat; +use crate::webidl::GPUPipelineLayoutOrGPUAutoLayoutMode; use crate::Instance; pub struct GPURenderPipeline { diff --git a/ext/webgpu/wrap/sampler.rs b/ext/webgpu/sampler.rs similarity index 100% rename from ext/webgpu/wrap/sampler.rs rename to ext/webgpu/sampler.rs diff --git a/ext/webgpu/wrap/shader.rs b/ext/webgpu/shader.rs similarity index 100% rename from ext/webgpu/wrap/shader.rs rename to ext/webgpu/shader.rs diff --git a/ext/webgpu/wrap/texture.rs b/ext/webgpu/texture.rs similarity index 100% rename from ext/webgpu/wrap/texture.rs rename to ext/webgpu/texture.rs diff --git a/ext/webgpu/wrap/webidl.rs b/ext/webgpu/webidl.rs similarity index 99% rename from ext/webgpu/wrap/webidl.rs rename to ext/webgpu/webidl.rs index 3997dd658c..d7c51f6f8b 100644 --- a/ext/webgpu/wrap/webidl.rs +++ b/ext/webgpu/webidl.rs @@ -302,7 +302,7 @@ pub(crate) enum GPUAutoLayoutMode { } pub(crate) enum GPUPipelineLayoutOrGPUAutoLayoutMode { - PipelineLayout(Ptr), + PipelineLayout(Ptr), AutoLayoutMode(GPUAutoLayoutMode), } diff --git a/ext/webgpu/wrap/mod.rs b/ext/webgpu/wrap/mod.rs deleted file mode 100644 index b995647bb0..0000000000 --- a/ext/webgpu/wrap/mod.rs +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright 2018-2025 the Deno authors. MIT license. - -use std::cell::RefCell; -use std::rc::Rc; -use std::sync::Arc; - -use deno_core::cppgc::SameObject; -use deno_core::op2; -use deno_core::GarbageCollected; -use deno_core::OpState; -use wgpu_types::PowerPreference; - -use crate::Instance; - -pub mod adapter; -pub mod bind_group; -pub mod bind_group_layout; -pub mod buffer; -pub mod command_buffer; -pub mod command_encoder; -pub mod compute_pass; -pub mod compute_pipeline; -pub mod device; -pub mod error; -pub mod pipeline_layout; -pub mod query_set; -pub mod queue; -pub mod render_bundle; -pub mod render_pass; -pub mod render_pipeline; -pub mod sampler; -pub mod shader; -pub mod texture; -pub mod webidl; - -#[op2] -#[cppgc] -pub fn create_gpu() -> GPU { - GPU -} - -pub struct GPU; - -impl GarbageCollected for GPU {} - -#[op2] -impl GPU { - #[async_method] - #[cppgc] - async fn request_adapter( - &self, - state: Rc>, - #[webidl] options: adapter::GPURequestAdapterOptions, - ) -> Option { - let mut state = state.borrow_mut(); - - let backends = std::env::var("DENO_WEBGPU_BACKEND").map_or_else( - |_| wgpu_types::Backends::all(), - |s| wgpu_core::instance::parse_backends_from_comma_list(&s), - ); - let instance = if let Some(instance) = state.try_borrow::() { - instance - } else { - state.put(Arc::new(wgpu_core::global::Global::new( - "webgpu", - wgpu_types::InstanceDescriptor { - backends, - flags: wgpu_types::InstanceFlags::from_build_config(), - dx12_shader_compiler: wgpu_types::Dx12Compiler::Fxc, - gles_minor_version: wgpu_types::Gles3MinorVersion::default(), - }, - ))); - state.borrow::() - }; - - let descriptor = wgpu_core::instance::RequestAdapterOptions { - power_preference: options - .power_preference - .map(|pp| match pp { - adapter::GPUPowerPreference::LowPower => PowerPreference::LowPower, - adapter::GPUPowerPreference::HighPerformance => { - PowerPreference::HighPerformance - } - }) - .unwrap_or_default(), - force_fallback_adapter: options.force_fallback_adapter, - compatible_surface: None, // windowless - }; - let id = instance.request_adapter(&descriptor, backends, None).ok()?; - - Some(adapter::GPUAdapter { - instance: instance.clone(), - features: SameObject::new(), - limits: SameObject::new(), - info: Rc::new(SameObject::new()), - id, - }) - } - - #[string] - fn getPreferredCanvasFormat(&self) -> &'static str { - // https://github.com/mozilla/gecko-dev/blob/b75080bb8b11844d18cb5f9ac6e68a866ef8e243/dom/webgpu/Instance.h#L42-L47 - if cfg!(target_os = "android") { - texture::GPUTextureFormat::Rgba8unorm.as_str() - } else { - texture::GPUTextureFormat::Bgra8unorm.as_str() - } - } -}