Rewrite From/TryFrom conversions on generated types to avoid future orphan rule violations (#34554)

* script: Add traits to allow converting between types that are not defined in the script crate.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Rewrite all From/TryFrom implementations on generated WebIDL types to use new Convert/TryConvert traits.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2024-12-11 00:52:48 -05:00 committed by GitHub
parent e10e989abb
commit e0cbab2fbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
40 changed files with 439 additions and 380 deletions

View file

@ -8,6 +8,7 @@ use dom_struct::dom_struct;
use webgpu::wgc::binding_model::BindGroupDescriptor;
use webgpu::{WebGPU, WebGPUBindGroup, WebGPUDevice, WebGPURequest};
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUBindGroupDescriptor, GPUBindGroupMethods,
@ -81,11 +82,11 @@ impl GPUBindGroup {
let entries = descriptor
.entries
.iter()
.map(|bind| bind.into())
.map(|bind| bind.convert())
.collect::<Vec<_>>();
let desc = BindGroupDescriptor {
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
layout: descriptor.layout.id().0,
entries: Cow::Owned(entries),
};

View file

@ -8,6 +8,7 @@ use dom_struct::dom_struct;
use webgpu::wgc::binding_model::BindGroupLayoutDescriptor;
use webgpu::{WebGPU, WebGPUBindGroupLayout, WebGPURequest};
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUBindGroupLayoutDescriptor, GPUBindGroupLayoutMethods,
@ -80,7 +81,7 @@ impl GPUBindGroupLayout {
let desc = match entries {
Ok(entries) => Some(BindGroupLayoutDescriptor {
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
entries: Cow::Owned(entries),
}),
Err(error) => {

View file

@ -12,6 +12,7 @@ use js::typedarray::ArrayBuffer;
use webgpu::wgc::device::HostMap;
use webgpu::{wgt, Mapping, WebGPU, WebGPUBuffer, WebGPURequest, WebGPUResponse};
use crate::conversions::Convert;
use crate::dom::bindings::buffer_source::DataBlock;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
@ -136,7 +137,7 @@ impl GPUBuffer {
descriptor: &GPUBufferDescriptor,
) -> Fallible<DomRoot<GPUBuffer>> {
let desc = wgt::BufferDescriptor {
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
size: descriptor.size as wgt::BufferAddress,
usage: wgt::BufferUsages::from_bits_retain(descriptor.usage),
mapped_at_creation: descriptor.mappedAtCreation,

View file

@ -20,6 +20,7 @@ use webrender_api::ImageKey;
use super::gpuconvert::convert_texture_descriptor;
use super::gputexture::GPUTexture;
use crate::conversions::Convert;
use crate::dom::bindings::codegen::Bindings::GPUCanvasContextBinding::GPUCanvasContextMethods;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::GPUTexture_Binding::GPUTextureMethods;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
@ -196,7 +197,7 @@ impl GPUCanvasContext {
drawing_buffer.config = Some(ContextConfiguration {
device_id: configuration.device.id().0,
queue_id: configuration.device.queue_id().0,
format: configuration.format.into(),
format: configuration.format.convert(),
is_opaque: matches!(configuration.alphaMode, GPUCanvasAlphaMode::Opaque),
});
} else {

View file

@ -9,6 +9,7 @@ use webgpu::{
WebGPURenderPass, WebGPURequest,
};
use crate::conversions::{Convert, TryConvert};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUCommandBufferDescriptor, GPUCommandEncoderDescriptor, GPUCommandEncoderMethods,
@ -93,7 +94,7 @@ impl GPUCommandEncoder {
device_id: device.id().0,
command_encoder_id,
desc: wgt::CommandEncoderDescriptor {
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
},
})
.expect("Failed to create WebGPU command encoder");
@ -131,7 +132,7 @@ impl GPUCommandEncoderMethods<crate::DomTypeHolder> for GPUCommandEncoder {
if let Err(e) = self.channel.0.send(WebGPURequest::BeginComputePass {
command_encoder_id: self.id().0,
compute_pass_id,
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
device_id: self.device.id().0,
}) {
warn!("Failed to send WebGPURequest::BeginComputePass {e:?}");
@ -179,7 +180,7 @@ impl GPUCommandEncoderMethods<crate::DomTypeHolder> for GPUCommandEncoder {
clear_value: color
.clearValue
.as_ref()
.map(|color| (color).try_into())
.map(|color| (color).try_convert())
.transpose()?
.unwrap_or_default(),
read_only: false,
@ -196,7 +197,7 @@ impl GPUCommandEncoderMethods<crate::DomTypeHolder> for GPUCommandEncoder {
if let Err(e) = self.channel.0.send(WebGPURequest::BeginRenderPass {
command_encoder_id: self.id().0,
render_pass_id,
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
depth_stencil_attachment,
color_attachments,
device_id: self.device.id().0,
@ -246,9 +247,9 @@ impl GPUCommandEncoderMethods<crate::DomTypeHolder> for GPUCommandEncoder {
.0
.send(WebGPURequest::CopyBufferToTexture {
command_encoder_id: self.encoder.0,
source: source.into(),
destination: destination.try_into()?,
copy_size: (&copy_size).try_into()?,
source: source.convert(),
destination: destination.try_convert()?,
copy_size: (&copy_size).try_convert()?,
})
.expect("Failed to send CopyBufferToTexture");
@ -266,9 +267,9 @@ impl GPUCommandEncoderMethods<crate::DomTypeHolder> for GPUCommandEncoder {
.0
.send(WebGPURequest::CopyTextureToBuffer {
command_encoder_id: self.encoder.0,
source: source.try_into()?,
destination: destination.into(),
copy_size: (&copy_size).try_into()?,
source: source.try_convert()?,
destination: destination.convert(),
copy_size: (&copy_size).try_convert()?,
})
.expect("Failed to send CopyTextureToBuffer");
@ -286,9 +287,9 @@ impl GPUCommandEncoderMethods<crate::DomTypeHolder> for GPUCommandEncoder {
.0
.send(WebGPURequest::CopyTextureToTexture {
command_encoder_id: self.encoder.0,
source: source.try_into()?,
destination: destination.try_into()?,
copy_size: (&copy_size).try_into()?,
source: source.try_convert()?,
destination: destination.try_convert()?,
copy_size: (&copy_size).try_convert()?,
})
.expect("Failed to send CopyTextureToTexture");
@ -303,7 +304,7 @@ impl GPUCommandEncoderMethods<crate::DomTypeHolder> for GPUCommandEncoder {
command_encoder_id: self.encoder.0,
device_id: self.device.id().0,
desc: wgt::CommandBufferDescriptor {
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
},
})
.expect("Failed to send Finish");

View file

@ -7,6 +7,7 @@ use ipc_channel::ipc::IpcSender;
use webgpu::wgc::pipeline::ComputePipelineDescriptor;
use webgpu::{WebGPU, WebGPUBindGroupLayout, WebGPUComputePipeline, WebGPURequest, WebGPUResponse};
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUComputePipelineDescriptor, GPUComputePipelineMethods,
@ -79,9 +80,9 @@ impl GPUComputePipeline {
let pipeline_layout = device.get_pipeline_layout_data(&descriptor.parent.layout);
let desc = ComputePipelineDescriptor {
label: (&descriptor.parent.parent).into(),
label: (&descriptor.parent.parent).convert(),
layout: pipeline_layout.explicit(),
stage: (&descriptor.compute).into(),
stage: (&descriptor.compute).convert(),
cache: None,
};

View file

@ -11,6 +11,7 @@ use webgpu::wgc::pipeline::ProgrammableStageDescriptor;
use webgpu::wgc::resource::TextureDescriptor;
use webgpu::wgt::{self, AstcBlock, AstcChannel};
use crate::conversions::{Convert, TryConvert};
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUAddressMode, GPUBindGroupEntry, GPUBindGroupLayoutEntry, GPUBindingResource,
GPUBlendComponent, GPUBlendFactor, GPUBlendOperation, GPUBufferBindingType, GPUColor,
@ -24,9 +25,9 @@ use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::types::GPUDevice;
impl From<GPUTextureFormat> for wgt::TextureFormat {
fn from(format: GPUTextureFormat) -> Self {
match format {
impl Convert<wgt::TextureFormat> for GPUTextureFormat {
fn convert(self) -> wgt::TextureFormat {
match self {
GPUTextureFormat::R8unorm => wgt::TextureFormat::R8Unorm,
GPUTextureFormat::R8snorm => wgt::TextureFormat::R8Snorm,
GPUTextureFormat::R8uint => wgt::TextureFormat::R8Uint,
@ -210,11 +211,11 @@ impl From<GPUTextureFormat> for wgt::TextureFormat {
}
}
impl TryFrom<&GPUExtent3D> for wgt::Extent3d {
impl TryConvert<wgt::Extent3d> for &GPUExtent3D {
type Error = Error;
fn try_from(size: &GPUExtent3D) -> Result<Self, Self::Error> {
match *size {
fn try_convert(self) -> Result<wgt::Extent3d, Self::Error> {
match *self {
GPUExtent3D::GPUExtent3DDict(ref dict) => Ok(wgt::Extent3d {
width: dict.width,
height: dict.height,
@ -238,19 +239,19 @@ impl TryFrom<&GPUExtent3D> for wgt::Extent3d {
}
}
impl From<&GPUImageDataLayout> for wgt::ImageDataLayout {
fn from(data_layout: &GPUImageDataLayout) -> Self {
impl Convert<wgt::ImageDataLayout> for &GPUImageDataLayout {
fn convert(self) -> wgt::ImageDataLayout {
wgt::ImageDataLayout {
offset: data_layout.offset as wgt::BufferAddress,
bytes_per_row: data_layout.bytesPerRow,
rows_per_image: data_layout.rowsPerImage,
offset: self.offset as wgt::BufferAddress,
bytes_per_row: self.bytesPerRow,
rows_per_image: self.rowsPerImage,
}
}
}
impl From<GPUVertexFormat> for wgt::VertexFormat {
fn from(format: GPUVertexFormat) -> Self {
match format {
impl Convert<wgt::VertexFormat> for GPUVertexFormat {
fn convert(self) -> wgt::VertexFormat {
match self {
GPUVertexFormat::Uint8x2 => wgt::VertexFormat::Uint8x2,
GPUVertexFormat::Uint8x4 => wgt::VertexFormat::Uint8x4,
GPUVertexFormat::Sint8x2 => wgt::VertexFormat::Sint8x2,
@ -285,34 +286,34 @@ impl From<GPUVertexFormat> for wgt::VertexFormat {
}
}
impl From<&GPUPrimitiveState> for wgt::PrimitiveState {
fn from(primitive_state: &GPUPrimitiveState) -> Self {
impl Convert<wgt::PrimitiveState> for &GPUPrimitiveState {
fn convert(self) -> wgt::PrimitiveState {
wgt::PrimitiveState {
topology: wgt::PrimitiveTopology::from(&primitive_state.topology),
strip_index_format: primitive_state.stripIndexFormat.map(|index_format| {
match index_format {
topology: self.topology.convert(),
strip_index_format: self
.stripIndexFormat
.map(|index_format| match index_format {
GPUIndexFormat::Uint16 => wgt::IndexFormat::Uint16,
GPUIndexFormat::Uint32 => wgt::IndexFormat::Uint32,
}
}),
front_face: match primitive_state.frontFace {
}),
front_face: match self.frontFace {
GPUFrontFace::Ccw => wgt::FrontFace::Ccw,
GPUFrontFace::Cw => wgt::FrontFace::Cw,
},
cull_mode: match primitive_state.cullMode {
cull_mode: match self.cullMode {
GPUCullMode::None => None,
GPUCullMode::Front => Some(wgt::Face::Front),
GPUCullMode::Back => Some(wgt::Face::Back),
},
unclipped_depth: primitive_state.clampDepth,
unclipped_depth: self.clampDepth,
..Default::default()
}
}
}
impl From<&GPUPrimitiveTopology> for wgt::PrimitiveTopology {
fn from(primitive_topology: &GPUPrimitiveTopology) -> Self {
match primitive_topology {
impl Convert<wgt::PrimitiveTopology> for &GPUPrimitiveTopology {
fn convert(self) -> wgt::PrimitiveTopology {
match self {
GPUPrimitiveTopology::Point_list => wgt::PrimitiveTopology::PointList,
GPUPrimitiveTopology::Line_list => wgt::PrimitiveTopology::LineList,
GPUPrimitiveTopology::Line_strip => wgt::PrimitiveTopology::LineStrip,
@ -322,9 +323,9 @@ impl From<&GPUPrimitiveTopology> for wgt::PrimitiveTopology {
}
}
impl From<GPUAddressMode> for wgt::AddressMode {
fn from(address_mode: GPUAddressMode) -> Self {
match address_mode {
impl Convert<wgt::AddressMode> for GPUAddressMode {
fn convert(self) -> wgt::AddressMode {
match self {
GPUAddressMode::Clamp_to_edge => wgt::AddressMode::ClampToEdge,
GPUAddressMode::Repeat => wgt::AddressMode::Repeat,
GPUAddressMode::Mirror_repeat => wgt::AddressMode::MirrorRepeat,
@ -332,18 +333,18 @@ impl From<GPUAddressMode> for wgt::AddressMode {
}
}
impl From<GPUFilterMode> for wgt::FilterMode {
fn from(filter_mode: GPUFilterMode) -> Self {
match filter_mode {
impl Convert<wgt::FilterMode> for GPUFilterMode {
fn convert(self) -> wgt::FilterMode {
match self {
GPUFilterMode::Nearest => wgt::FilterMode::Nearest,
GPUFilterMode::Linear => wgt::FilterMode::Linear,
}
}
}
impl From<GPUTextureViewDimension> for wgt::TextureViewDimension {
fn from(view_dimension: GPUTextureViewDimension) -> Self {
match view_dimension {
impl Convert<wgt::TextureViewDimension> for GPUTextureViewDimension {
fn convert(self) -> wgt::TextureViewDimension {
match self {
GPUTextureViewDimension::_1d => wgt::TextureViewDimension::D1,
GPUTextureViewDimension::_2d => wgt::TextureViewDimension::D2,
GPUTextureViewDimension::_2d_array => wgt::TextureViewDimension::D2Array,
@ -354,9 +355,9 @@ impl From<GPUTextureViewDimension> for wgt::TextureViewDimension {
}
}
impl From<GPUCompareFunction> for wgt::CompareFunction {
fn from(compare: GPUCompareFunction) -> Self {
match compare {
impl Convert<wgt::CompareFunction> for GPUCompareFunction {
fn convert(self) -> wgt::CompareFunction {
match self {
GPUCompareFunction::Never => wgt::CompareFunction::Never,
GPUCompareFunction::Less => wgt::CompareFunction::Less,
GPUCompareFunction::Equal => wgt::CompareFunction::Equal,
@ -369,9 +370,9 @@ impl From<GPUCompareFunction> for wgt::CompareFunction {
}
}
impl From<&GPUBlendFactor> for wgt::BlendFactor {
fn from(factor: &GPUBlendFactor) -> Self {
match factor {
impl Convert<wgt::BlendFactor> for &GPUBlendFactor {
fn convert(self) -> wgt::BlendFactor {
match self {
GPUBlendFactor::Zero => wgt::BlendFactor::Zero,
GPUBlendFactor::One => wgt::BlendFactor::One,
GPUBlendFactor::Src => wgt::BlendFactor::Src,
@ -389,12 +390,12 @@ impl From<&GPUBlendFactor> for wgt::BlendFactor {
}
}
impl From<&GPUBlendComponent> for wgt::BlendComponent {
fn from(blend_component: &GPUBlendComponent) -> Self {
impl Convert<wgt::BlendComponent> for &GPUBlendComponent {
fn convert(self) -> wgt::BlendComponent {
wgt::BlendComponent {
src_factor: wgt::BlendFactor::from(&blend_component.srcFactor),
dst_factor: wgt::BlendFactor::from(&blend_component.dstFactor),
operation: match blend_component.operation {
src_factor: self.srcFactor.convert(),
dst_factor: self.dstFactor.convert(),
operation: match self.operation {
GPUBlendOperation::Add => wgt::BlendOperation::Add,
GPUBlendOperation::Subtract => wgt::BlendOperation::Subtract,
GPUBlendOperation::Reverse_subtract => wgt::BlendOperation::ReverseSubtract,
@ -421,9 +422,9 @@ pub fn convert_store_op(op: Option<GPUStoreOp>) -> wgpu_com::StoreOp {
}
}
impl From<GPUStencilOperation> for wgt::StencilOperation {
fn from(operation: GPUStencilOperation) -> Self {
match operation {
impl Convert<wgt::StencilOperation> for GPUStencilOperation {
fn convert(self) -> wgt::StencilOperation {
match self {
GPUStencilOperation::Keep => wgt::StencilOperation::Keep,
GPUStencilOperation::Zero => wgt::StencilOperation::Zero,
GPUStencilOperation::Replace => wgt::StencilOperation::Replace,
@ -436,20 +437,20 @@ impl From<GPUStencilOperation> for wgt::StencilOperation {
}
}
impl From<&GPUImageCopyBuffer> for wgpu_com::ImageCopyBuffer {
fn from(ic_buffer: &GPUImageCopyBuffer) -> Self {
impl Convert<wgpu_com::ImageCopyBuffer> for &GPUImageCopyBuffer {
fn convert(self) -> wgpu_com::ImageCopyBuffer {
wgpu_com::ImageCopyBuffer {
buffer: ic_buffer.buffer.id().0,
layout: wgt::ImageDataLayout::from(&ic_buffer.parent),
buffer: self.buffer.id().0,
layout: self.parent.convert(),
}
}
}
impl TryFrom<&GPUOrigin3D> for wgt::Origin3d {
impl TryConvert<wgt::Origin3d> for &GPUOrigin3D {
type Error = Error;
fn try_from(origin: &GPUOrigin3D) -> Result<Self, Self::Error> {
match origin {
fn try_convert(self) -> Result<wgt::Origin3d, Self::Error> {
match self {
GPUOrigin3D::RangeEnforcedUnsignedLongSequence(v) => {
// https://gpuweb.github.io/gpuweb/#abstract-opdef-validate-gpuorigin3d-shape
if v.len() > 3 {
@ -473,20 +474,20 @@ impl TryFrom<&GPUOrigin3D> for wgt::Origin3d {
}
}
impl TryFrom<&GPUImageCopyTexture> for wgpu_com::ImageCopyTexture {
impl TryConvert<wgpu_com::ImageCopyTexture> for &GPUImageCopyTexture {
type Error = Error;
fn try_from(ic_texture: &GPUImageCopyTexture) -> Result<Self, Self::Error> {
fn try_convert(self) -> Result<wgpu_com::ImageCopyTexture, Self::Error> {
Ok(wgpu_com::ImageCopyTexture {
texture: ic_texture.texture.id().0,
mip_level: ic_texture.mipLevel,
origin: ic_texture
texture: self.texture.id().0,
mip_level: self.mipLevel,
origin: self
.origin
.as_ref()
.map(wgt::Origin3d::try_from)
.map(TryConvert::<wgt::Origin3d>::try_convert)
.transpose()?
.unwrap_or_default(),
aspect: match ic_texture.aspect {
aspect: match self.aspect {
GPUTextureAspect::All => wgt::TextureAspect::All,
GPUTextureAspect::Stencil_only => wgt::TextureAspect::StencilOnly,
GPUTextureAspect::Depth_only => wgt::TextureAspect::DepthOnly,
@ -495,12 +496,12 @@ impl TryFrom<&GPUImageCopyTexture> for wgpu_com::ImageCopyTexture {
}
}
impl<'a> From<&GPUObjectDescriptorBase> for Option<Cow<'a, str>> {
fn from(val: &GPUObjectDescriptorBase) -> Self {
if val.label.is_empty() {
impl<'a> Convert<Option<Cow<'a, str>>> for &GPUObjectDescriptorBase {
fn convert(self) -> Option<Cow<'a, str>> {
if self.label.is_empty() {
None
} else {
Some(Cow::Owned(val.label.to_string()))
Some(Cow::Owned(self.label.to_string()))
}
}
}
@ -541,7 +542,7 @@ pub fn convert_bind_group_layout_entry(
GPUStorageTextureAccess::Read_write => wgt::StorageTextureAccess::ReadWrite,
},
format: device.validate_texture_format_required_features(&storage.format)?,
view_dimension: storage.viewDimension.into(),
view_dimension: storage.viewDimension.convert(),
})
} else if let Some(texture) = &bgle.texture {
Some(wgt::BindingType::Texture {
@ -554,7 +555,7 @@ pub fn convert_bind_group_layout_entry(
GPUTextureSampleType::Sint => wgt::TextureSampleType::Sint,
GPUTextureSampleType::Uint => wgt::TextureSampleType::Uint,
},
view_dimension: texture.viewDimension.into(),
view_dimension: texture.viewDimension.convert(),
multisampled: texture.multisampled,
})
} else {
@ -584,13 +585,13 @@ pub fn convert_texture_descriptor(
descriptor: &GPUTextureDescriptor,
device: &GPUDevice,
) -> Fallible<(TextureDescriptor<'static>, wgt::Extent3d)> {
let size = (&descriptor.size).try_into()?;
let size = (&descriptor.size).try_convert()?;
let desc = TextureDescriptor {
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
size,
mip_level_count: descriptor.mipLevelCount,
sample_count: descriptor.sampleCount,
dimension: descriptor.dimension.into(),
dimension: descriptor.dimension.convert(),
format: device.validate_texture_format_required_features(&descriptor.format)?,
usage: wgt::TextureUsages::from_bits_retain(descriptor.usage),
view_formats: descriptor
@ -602,11 +603,11 @@ pub fn convert_texture_descriptor(
Ok((desc, size))
}
impl TryFrom<&GPUColor> for wgt::Color {
impl TryConvert<wgt::Color> for &GPUColor {
type Error = Error;
fn try_from(color: &GPUColor) -> Result<Self, Self::Error> {
match color {
fn try_convert(self) -> Result<wgt::Color, Self::Error> {
match self {
GPUColor::DoubleSequence(s) => {
// https://gpuweb.github.io/gpuweb/#abstract-opdef-validate-gpucolor-shape
if s.len() != 4 {
@ -630,17 +631,16 @@ impl TryFrom<&GPUColor> for wgt::Color {
}
}
impl<'a> From<&GPUProgrammableStage> for ProgrammableStageDescriptor<'a> {
fn from(stage: &GPUProgrammableStage) -> Self {
Self {
module: stage.module.id().0,
entry_point: stage
impl<'a> Convert<ProgrammableStageDescriptor<'a>> for &GPUProgrammableStage {
fn convert(self) -> ProgrammableStageDescriptor<'a> {
ProgrammableStageDescriptor {
module: self.module.id().0,
entry_point: self
.entryPoint
.as_ref()
.map(|ep| Cow::Owned(ep.to_string())),
constants: Cow::Owned(
stage
.constants
self.constants
.as_ref()
.map(|records| records.iter().map(|(k, v)| (k.0.clone(), **v)).collect())
.unwrap_or_default(),
@ -650,11 +650,11 @@ impl<'a> From<&GPUProgrammableStage> for ProgrammableStageDescriptor<'a> {
}
}
impl From<&GPUBindGroupEntry> for BindGroupEntry<'_> {
fn from(entry: &GPUBindGroupEntry) -> Self {
Self {
binding: entry.binding,
resource: match entry.resource {
impl<'a> Convert<BindGroupEntry<'a>> for &GPUBindGroupEntry {
fn convert(self) -> BindGroupEntry<'a> {
BindGroupEntry {
binding: self.binding,
resource: match self.resource {
GPUBindingResource::GPUSampler(ref s) => BindingResource::Sampler(s.id().0),
GPUBindingResource::GPUTextureView(ref t) => BindingResource::TextureView(t.id().0),
GPUBindingResource::GPUBufferBinding(ref b) => {
@ -669,9 +669,9 @@ impl From<&GPUBindGroupEntry> for BindGroupEntry<'_> {
}
}
impl From<GPUTextureDimension> for wgt::TextureDimension {
fn from(dimension: GPUTextureDimension) -> Self {
match dimension {
impl Convert<wgt::TextureDimension> for GPUTextureDimension {
fn convert(self) -> wgt::TextureDimension {
match self {
GPUTextureDimension::_1d => wgt::TextureDimension::D1,
GPUTextureDimension::_2d => wgt::TextureDimension::D2,
GPUTextureDimension::_3d => wgt::TextureDimension::D3,

View file

@ -23,6 +23,7 @@ use super::gpu::AsyncWGPUListener;
use super::gpudevicelostinfo::GPUDeviceLostInfo;
use super::gpupipelineerror::GPUPipelineError;
use super::gpusupportedlimits::GPUSupportedLimits;
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::EventBinding::EventInit;
use crate::dom::bindings::codegen::Bindings::EventTargetBinding::EventTargetMethods;
@ -215,7 +216,7 @@ impl GPUDevice {
&self,
format: &GPUTextureFormat,
) -> Fallible<TextureFormat> {
let texture_format: TextureFormat = (*format).into();
let texture_format: TextureFormat = (*format).convert();
if self
.features
.wgpu_features()
@ -258,11 +259,11 @@ impl GPUDevice {
let pipeline_layout = self.get_pipeline_layout_data(&descriptor.parent.layout);
let desc = wgpu_pipe::RenderPipelineDescriptor {
label: (&descriptor.parent.parent).into(),
label: (&descriptor.parent.parent).convert(),
layout: pipeline_layout.explicit(),
cache: None,
vertex: wgpu_pipe::VertexState {
stage: (&descriptor.vertex.parent).into(),
stage: (&descriptor.vertex.parent).convert(),
buffers: Cow::Owned(
descriptor
.vertex
@ -279,7 +280,7 @@ impl GPUDevice {
.attributes
.iter()
.map(|att| wgt::VertexAttribute {
format: att.format.into(),
format: att.format.convert(),
offset: att.offset,
shader_location: att.shaderLocation,
})
@ -294,7 +295,7 @@ impl GPUDevice {
.as_ref()
.map(|stage| -> Fallible<wgpu_pipe::FragmentState> {
Ok(wgpu_pipe::FragmentState {
stage: (&stage.parent).into(),
stage: (&stage.parent).convert(),
targets: Cow::Owned(
stage
.targets
@ -309,8 +310,8 @@ impl GPUDevice {
),
blend: state.blend.as_ref().map(|blend| {
wgt::BlendState {
color: (&blend.color).into(),
alpha: (&blend.alpha).into(),
color: (&blend.color).convert(),
alpha: (&blend.alpha).convert(),
}
}),
})
@ -321,7 +322,7 @@ impl GPUDevice {
})
})
.transpose()?,
primitive: (&descriptor.primitive).into(),
primitive: (&descriptor.primitive).convert(),
depth_stencil: descriptor
.depthStencil
.as_ref()
@ -330,20 +331,20 @@ impl GPUDevice {
.map(|format| wgt::DepthStencilState {
format,
depth_write_enabled: dss_desc.depthWriteEnabled,
depth_compare: dss_desc.depthCompare.into(),
depth_compare: dss_desc.depthCompare.convert(),
stencil: wgt::StencilState {
front: wgt::StencilFaceState {
compare: dss_desc.stencilFront.compare.into(),
compare: dss_desc.stencilFront.compare.convert(),
fail_op: dss_desc.stencilFront.failOp.into(),
depth_fail_op: dss_desc.stencilFront.depthFailOp.into(),
pass_op: dss_desc.stencilFront.passOp.into(),
fail_op: dss_desc.stencilFront.failOp.convert(),
depth_fail_op: dss_desc.stencilFront.depthFailOp.convert(),
pass_op: dss_desc.stencilFront.passOp.convert(),
},
back: wgt::StencilFaceState {
compare: dss_desc.stencilBack.compare.into(),
fail_op: dss_desc.stencilBack.failOp.into(),
depth_fail_op: dss_desc.stencilBack.depthFailOp.into(),
pass_op: dss_desc.stencilBack.passOp.into(),
compare: dss_desc.stencilBack.compare.convert(),
fail_op: dss_desc.stencilBack.failOp.convert(),
depth_fail_op: dss_desc.stencilBack.depthFailOp.convert(),
pass_op: dss_desc.stencilBack.passOp.convert(),
},
read_mask: dss_desc.stencilReadMask,
write_mask: dss_desc.stencilWriteMask,

View file

@ -6,6 +6,7 @@ use dom_struct::dom_struct;
use js::rust::HandleObject;
use webgpu::{Error, ErrorFilter};
use crate::conversions::Convert;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{GPUErrorFilter, GPUErrorMethods};
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, Reflector};
use crate::dom::bindings::root::DomRoot;
@ -79,9 +80,9 @@ impl GPUErrorMethods<crate::DomTypeHolder> for GPUError {
}
}
impl From<ErrorFilter> for GPUErrorFilter {
fn from(filter: ErrorFilter) -> Self {
match filter {
impl Convert<GPUErrorFilter> for ErrorFilter {
fn convert(self) -> GPUErrorFilter {
match self {
ErrorFilter::Validation => GPUErrorFilter::Validation,
ErrorFilter::OutOfMemory => GPUErrorFilter::Out_of_memory,
ErrorFilter::Internal => GPUErrorFilter::Internal,

View file

@ -8,6 +8,7 @@ use dom_struct::dom_struct;
use webgpu::wgc::binding_model::PipelineLayoutDescriptor;
use webgpu::{WebGPU, WebGPUBindGroupLayout, WebGPUPipelineLayout, WebGPURequest};
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUPipelineLayoutDescriptor, GPUPipelineLayoutMethods,
@ -87,7 +88,7 @@ impl GPUPipelineLayout {
.collect::<Vec<_>>();
let desc = PipelineLayoutDescriptor {
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
bind_group_layouts: Cow::Owned(bgls.iter().map(|l| l.0).collect::<Vec<_>>()),
push_constant_ranges: Cow::Owned(vec![]),
};

View file

@ -9,6 +9,7 @@ use ipc_channel::ipc::IpcSharedMemory;
use webgpu::{wgt, WebGPU, WebGPUQueue, WebGPURequest, WebGPUResponse};
use super::gpu::{response_async, AsyncWGPUListener};
use crate::conversions::{Convert, TryConvert};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUExtent3D, GPUImageCopyTexture, GPUImageDataLayout, GPUQueueMethods, GPUSize64,
@ -162,9 +163,9 @@ impl GPUQueueMethods<crate::DomTypeHolder> for GPUQueue {
return Err(Error::Operation);
}
let texture_cv = destination.try_into()?;
let texture_layout = data_layout.into();
let write_size = (&size).try_into()?;
let texture_cv = destination.try_convert()?;
let texture_layout = data_layout.convert();
let write_size = (&size).try_convert()?;
let final_data = IpcSharedMemory::from_bytes(&bytes);
if let Err(e) = self.channel.0.send(WebGPURequest::WriteTexture {

View file

@ -10,6 +10,7 @@ use webgpu::wgc::command::{
};
use webgpu::{wgt, WebGPU, WebGPURenderBundle, WebGPURequest};
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUIndexFormat, GPURenderBundleDescriptor, GPURenderBundleEncoderDescriptor,
@ -81,7 +82,7 @@ impl GPURenderBundleEncoder {
descriptor: &GPURenderBundleEncoderDescriptor,
) -> Fallible<DomRoot<GPURenderBundleEncoder>> {
let desc = RenderBundleEncoderDescriptor {
label: (&descriptor.parent.parent).into(),
label: (&descriptor.parent.parent).convert(),
color_formats: Cow::Owned(
descriptor
.parent
@ -252,7 +253,7 @@ impl GPURenderBundleEncoderMethods<crate::DomTypeHolder> for GPURenderBundleEnco
/// <https://gpuweb.github.io/gpuweb/#dom-gpurenderbundleencoder-finish>
fn Finish(&self, descriptor: &GPURenderBundleDescriptor) -> DomRoot<GPURenderBundle> {
let desc = wgt::RenderBundleDescriptor {
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
};
let encoder = self.render_bundle_encoder.borrow_mut().take().unwrap();
let render_bundle_id = self.global().wgpu_id_hub().create_render_bundle_id();

View file

@ -5,6 +5,7 @@
use dom_struct::dom_struct;
use webgpu::{wgt, RenderCommand, WebGPU, WebGPURenderPass, WebGPURequest};
use crate::conversions::TryConvert;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUColor, GPUIndexFormat, GPURenderPassEncoderMethods,
@ -130,7 +131,7 @@ impl GPURenderPassEncoderMethods<crate::DomTypeHolder> for GPURenderPassEncoder
/// <https://gpuweb.github.io/gpuweb/#dom-gpurenderpassencoder-setblendcolor>
fn SetBlendConstant(&self, color: GPUColor) -> Fallible<()> {
self.send_render_command(RenderCommand::SetBlendConstant((&color).try_into()?));
self.send_render_command(RenderCommand::SetBlendConstant((&color).try_convert()?));
Ok(())
}

View file

@ -6,6 +6,7 @@ use dom_struct::dom_struct;
use webgpu::wgc::resource::SamplerDescriptor;
use webgpu::{WebGPU, WebGPUDevice, WebGPURequest, WebGPUSampler};
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUSamplerDescriptor, GPUSamplerMethods,
@ -79,18 +80,18 @@ impl GPUSampler {
let sampler_id = device.global().wgpu_id_hub().create_sampler_id();
let compare_enable = descriptor.compare.is_some();
let desc = SamplerDescriptor {
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
address_modes: [
descriptor.addressModeU.into(),
descriptor.addressModeV.into(),
descriptor.addressModeW.into(),
descriptor.addressModeU.convert(),
descriptor.addressModeV.convert(),
descriptor.addressModeW.convert(),
],
mag_filter: descriptor.magFilter.into(),
min_filter: descriptor.minFilter.into(),
mipmap_filter: descriptor.mipmapFilter.into(),
mag_filter: descriptor.magFilter.convert(),
min_filter: descriptor.minFilter.convert(),
mipmap_filter: descriptor.mipmapFilter.convert(),
lod_min_clamp: *descriptor.lodMinClamp,
lod_max_clamp: *descriptor.lodMaxClamp,
compare: descriptor.compare.map(Into::into),
compare: descriptor.compare.map(Convert::convert),
anisotropy_clamp: 1,
border_color: None,
};

View file

@ -9,6 +9,7 @@ use webgpu::wgc::resource;
use webgpu::{wgt, WebGPU, WebGPURequest, WebGPUTexture, WebGPUTextureView};
use super::gpuconvert::convert_texture_descriptor;
use crate::conversions::Convert;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUTextureAspect, GPUTextureDescriptor, GPUTextureDimension, GPUTextureFormat,
@ -180,12 +181,12 @@ impl GPUTextureMethods<crate::DomTypeHolder> for GPUTexture {
!matches!(descriptor.arrayLayerCount, Some(0))
{
Some(resource::TextureViewDescriptor {
label: (&descriptor.parent).into(),
label: (&descriptor.parent).convert(),
format: descriptor
.format
.map(|f| self.device.validate_texture_format_required_features(&f))
.transpose()?,
dimension: descriptor.dimension.map(|dimension| dimension.into()),
dimension: descriptor.dimension.map(|dimension| dimension.convert()),
range: wgt::ImageSubresourceRange {
aspect: match descriptor.aspect {
GPUTextureAspect::All => wgt::TextureAspect::All,