[deno] Add some WebIDL converters

This commit is contained in:
Andy Leiserson committed 2025-12-19 15:12:03 -08:00
1 parent bdf7d9bb5d
commit 90e162c0e3
6 files changed
+155 -33

No files matched your search

+2 -2
View File
@@ -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<GPUBufferBindingLayout>,
pub sampler: Option<GPUSamplerBindingLayout>,
pub texture: Option<GPUTextureBindingLayout>,
+3 -10
View File
@@ -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()
+3 -3
View File
@@ -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<GPUBlendState>,
#[webidl(default = 0xF)]
#[options(enforce_range = true)]
pub write_mask: u32,
#[webidl(default = GPUColorWriteFlags(wgpu_types::ColorWrites::ALL))]
pub write_mask: GPUColorWriteFlags,
}
#[derive(WebIDL)]
+5 -7
View File
@@ -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<GPUDevice>,
pub usage: u32,
pub usage: GPUTextureUsageFlags,
pub format: GPUTextureFormat,
pub surface_config:
wgpu_types::SurfaceConfiguration<Vec<wgpu_types::TextureFormat>>,
@@ -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<GPUDevice>,
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,
+7 -11
View File
@@ -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<GPUTextureFormat>,
}
@@ -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<GPUTextureFormat>,
dimension: Option<GPUTextureViewDimension>,
#[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)]
+135
View File
@@ -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<Self, WebIdlError> {
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<GPUTextureUsageFlags> 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<Self, WebIdlError> {
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<GPUShaderStageFlags> 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<Self, WebIdlError> {
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<GPUColorWriteFlags> for wgpu_types::ColorWrites {
fn from(value: GPUColorWriteFlags) -> Self {
value.0
}
}