From 90e162c0e3dbeed025f5bfaad66fbd34ed97761b Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Tue, 9 Dec 2025 15:13:35 -0800 Subject: [PATCH] [deno] Add some WebIDL converters --- deno_webgpu/bind_group_layout.rs | 4 +- deno_webgpu/device.rs | 13 +-- deno_webgpu/render_pipeline.rs | 6 +- deno_webgpu/surface.rs | 12 ++- deno_webgpu/texture.rs | 18 ++--- deno_webgpu/webidl.rs | 135 +++++++++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 33 deletions(-) diff --git a/deno_webgpu/bind_group_layout.rs b/deno_webgpu/bind_group_layout.rs index 79b7bbe0b..fe3fe8a0f 100644 --- a/deno_webgpu/bind_group_layout.rs +++ b/deno_webgpu/bind_group_layout.rs @@ -6,6 +6,7 @@ use deno_core::WebIDL; use crate::error::GPUGenericError; use crate::texture::GPUTextureViewDimension; +use crate::webidl::GPUShaderStageFlags; use crate::Instance; pub struct GPUBindGroupLayout { @@ -63,8 +64,7 @@ pub(crate) struct GPUBindGroupLayoutDescriptor { pub(crate) struct GPUBindGroupLayoutEntry { #[options(enforce_range = true)] pub binding: u32, - #[options(enforce_range = true)] - pub visibility: u32, + pub visibility: GPUShaderStageFlags, pub buffer: Option, pub sampler: Option, pub texture: Option, diff --git a/deno_webgpu/device.rs b/deno_webgpu/device.rs index 243a9e05d..5cd8b8e1a 100644 --- a/deno_webgpu/device.rs +++ b/deno_webgpu/device.rs @@ -221,8 +221,7 @@ impl GPUDevice { sample_count: descriptor.sample_count, dimension: descriptor.dimension.clone().into(), format: descriptor.format.clone().into(), - usage: wgpu_types::TextureUsages::from_bits(descriptor.usage) - .ok_or_else(|| JsErrorBox::type_error("usage is not valid"))?, + usage: descriptor.usage.into(), view_formats: descriptor .view_formats .into_iter() @@ -340,8 +339,7 @@ impl GPUDevice { entries.push(wgpu_types::BindGroupLayoutEntry { binding: entry.binding, - visibility: wgpu_types::ShaderStages::from_bits(entry.visibility) - .ok_or_else(|| JsErrorBox::type_error("usage is not valid"))?, + visibility: entry.visibility.into(), ty, count: None, // native-only }); @@ -900,12 +898,7 @@ impl GPUDevice { operation: blend.alpha.operation.into(), }, }), - write_mask: wgpu_types::ColorWrites::from_bits( - target.write_mask, - ) - .ok_or_else(|| { - JsErrorBox::type_error("usage is not valid") - })?, + write_mask: target.write_mask.into(), }) }) .transpose() diff --git a/deno_webgpu/render_pipeline.rs b/deno_webgpu/render_pipeline.rs index 1a0fc2210..613f795b0 100644 --- a/deno_webgpu/render_pipeline.rs +++ b/deno_webgpu/render_pipeline.rs @@ -13,6 +13,7 @@ use crate::error::GPUGenericError; use crate::sampler::GPUCompareFunction; use crate::shader::GPUShaderModule; use crate::texture::GPUTextureFormat; +use crate::webidl::GPUColorWriteFlags; use crate::webidl::GPUPipelineLayoutOrGPUAutoLayoutMode; use crate::Instance; @@ -193,9 +194,8 @@ pub(crate) struct GPUFragmentState { pub(crate) struct GPUColorTargetState { pub format: GPUTextureFormat, pub blend: Option, - #[webidl(default = 0xF)] - #[options(enforce_range = true)] - pub write_mask: u32, + #[webidl(default = GPUColorWriteFlags(wgpu_types::ColorWrites::ALL))] + pub write_mask: GPUColorWriteFlags, } #[derive(WebIDL)] diff --git a/deno_webgpu/surface.rs b/deno_webgpu/surface.rs index 60df27ce0..24b4b1188 100644 --- a/deno_webgpu/surface.rs +++ b/deno_webgpu/surface.rs @@ -15,6 +15,7 @@ use crate::device::GPUDevice; use crate::error::GPUGenericError; use crate::texture::GPUTexture; use crate::texture::GPUTextureFormat; +use crate::webidl::GPUTextureUsageFlags; #[derive(Debug, thiserror::Error, deno_error::JsError)] pub enum SurfaceError { @@ -31,7 +32,7 @@ pub enum SurfaceError { pub struct Configuration { pub device: Ptr, - pub usage: u32, + pub usage: GPUTextureUsageFlags, pub format: GPUTextureFormat, pub surface_config: wgpu_types::SurfaceConfiguration>, @@ -73,11 +74,9 @@ impl GPUCanvasContext { &self, #[webidl] configuration: GPUCanvasConfiguration, ) -> Result<(), JsErrorBox> { - let usage = wgpu_types::TextureUsages::from_bits(configuration.usage) - .ok_or_else(|| JsErrorBox::type_error("usage is not valid"))?; let format = configuration.format.clone().into(); let conf = wgpu_types::SurfaceConfiguration { - usage, + usage: configuration.usage.into(), format, width: *self.width.borrow(), height: *self.height.borrow(), @@ -214,9 +213,8 @@ impl GPUCanvasContext { struct GPUCanvasConfiguration { device: Ptr, format: GPUTextureFormat, - #[webidl(default = wgpu_types::TextureUsages::RENDER_ATTACHMENT.bits())] - #[options(enforce_range = true)] - usage: u32, + #[webidl(default = GPUTextureUsageFlags(wgpu_types::TextureUsages::RENDER_ATTACHMENT))] + usage: GPUTextureUsageFlags, #[webidl(default = GPUCanvasAlphaMode::Opaque)] alpha_mode: GPUCanvasAlphaMode, diff --git a/deno_webgpu/texture.rs b/deno_webgpu/texture.rs index 8d92d7f30..6f421ee54 100644 --- a/deno_webgpu/texture.rs +++ b/deno_webgpu/texture.rs @@ -16,6 +16,7 @@ use wgpu_types::TextureFormat; use wgpu_types::TextureViewDimension; use crate::error::GPUGenericError; +use crate::webidl::GPUTextureUsageFlags; use crate::Instance; #[derive(WebIDL)] @@ -34,8 +35,7 @@ pub(crate) struct GPUTextureDescriptor { #[webidl(default = GPUTextureDimension::D2)] pub dimension: GPUTextureDimension, pub format: GPUTextureFormat, - #[options(enforce_range = true)] - pub usage: u32, + pub usage: GPUTextureUsageFlags, #[webidl(default = vec![])] pub view_formats: Vec, } @@ -54,7 +54,7 @@ pub struct GPUTexture { pub sample_count: u32, pub dimension: GPUTextureDimension, pub format: GPUTextureFormat, - pub usage: u32, + pub usage: GPUTextureUsageFlags, } impl GPUTexture { @@ -151,7 +151,7 @@ impl GPUTexture { } #[getter] fn usage(&self) -> u32 { - self.usage + self.usage.bits() } #[fast] #[undefined] @@ -168,10 +168,7 @@ impl GPUTexture { label: crate::transform_label(descriptor.label.clone()), format: descriptor.format.map(Into::into), dimension: descriptor.dimension.map(Into::into), - usage: Some( - wgpu_types::TextureUsages::from_bits(descriptor.usage) - .ok_or_else(|| JsErrorBox::type_error("usage is not valid"))?, - ), + usage: Some(descriptor.usage.into()), range: wgpu_types::ImageSubresourceRange { aspect: descriptor.aspect.into(), base_mip_level: descriptor.base_mip_level, @@ -204,9 +201,8 @@ struct GPUTextureViewDescriptor { format: Option, dimension: Option, - #[webidl(default = 0)] - #[options(enforce_range = true)] - usage: u32, + #[webidl(default = GPUTextureUsageFlags(wgpu_types::TextureUsages::empty()))] + usage: GPUTextureUsageFlags, #[webidl(default = GPUTextureAspect::All)] aspect: GPUTextureAspect, #[webidl(default = 0)] diff --git a/deno_webgpu/webidl.rs b/deno_webgpu/webidl.rs index 1aa581b47..81da9b91e 100644 --- a/deno_webgpu/webidl.rs +++ b/deno_webgpu/webidl.rs @@ -680,3 +680,138 @@ pub fn features_to_feature_names( return_features } + +#[derive(Clone, Copy)] +pub(crate) struct GPUTextureUsageFlags(pub(crate) wgpu_types::TextureUsages); + +impl<'a> WebIdlConverter<'a> for GPUTextureUsageFlags { + type Options = (); + + fn convert<'b>( + scope: &mut v8::HandleScope<'a>, + value: v8::Local<'a, v8::Value>, + prefix: Cow<'static, str>, + context: ContextFn<'b>, + _options: &Self::Options, + ) -> Result { + let flags_value = u32::convert( + scope, + value, + prefix.clone(), + context.borrowed(), + &IntOptions { + clamp: false, + enforce_range: true, + }, + )?; + + let flags = + wgpu_types::TextureUsages::from_bits(flags_value).ok_or_else(|| { + WebIdlError::other( + prefix, + context, + JsErrorBox::type_error("usage is not valid"), + ) + })?; + + Ok(GPUTextureUsageFlags(flags)) + } +} + +impl From for wgpu_types::TextureUsages { + fn from(value: GPUTextureUsageFlags) -> Self { + value.0 + } +} + +impl GPUTextureUsageFlags { + pub fn bits(&self) -> u32 { + self.0.bits() + } +} + +#[derive(Clone, Copy)] +pub(crate) struct GPUShaderStageFlags(pub(crate) wgpu_types::ShaderStages); + +impl<'a> WebIdlConverter<'a> for GPUShaderStageFlags { + type Options = (); + + fn convert<'b>( + scope: &mut v8::HandleScope<'a>, + value: v8::Local<'a, v8::Value>, + prefix: Cow<'static, str>, + context: ContextFn<'b>, + _options: &Self::Options, + ) -> Result { + let flags_value = u32::convert( + scope, + value, + prefix.clone(), + context.borrowed(), + &IntOptions { + clamp: false, + enforce_range: true, + }, + )?; + + let flags = + wgpu_types::ShaderStages::from_bits(flags_value).ok_or_else(|| { + WebIdlError::other( + prefix, + context, + JsErrorBox::type_error("shader stage is not valid"), + ) + })?; + + Ok(GPUShaderStageFlags(flags)) + } +} + +impl From for wgpu_types::ShaderStages { + fn from(value: GPUShaderStageFlags) -> Self { + value.0 + } +} + +#[derive(Clone, Copy)] +pub(crate) struct GPUColorWriteFlags(pub(crate) wgpu_types::ColorWrites); + +impl<'a> WebIdlConverter<'a> for GPUColorWriteFlags { + type Options = (); + + fn convert<'b>( + scope: &mut v8::HandleScope<'a>, + value: v8::Local<'a, v8::Value>, + prefix: Cow<'static, str>, + context: ContextFn<'b>, + _options: &Self::Options, + ) -> Result { + let flags_value = u32::convert( + scope, + value, + prefix.clone(), + context.borrowed(), + &IntOptions { + clamp: false, + enforce_range: true, + }, + )?; + + let flags = + wgpu_types::ColorWrites::from_bits(flags_value).ok_or_else(|| { + WebIdlError::other( + prefix, + context, + JsErrorBox::type_error("color write flags are not valid"), + ) + })?; + + Ok(GPUColorWriteFlags(flags)) + } +} + +impl From for wgpu_types::ColorWrites { + fn from(value: GPUColorWriteFlags) -> Self { + value.0 + } +}