From bea4fb9e2de9bc8b58a8467763dfff520d74a7d8 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Thu, 20 Feb 2025 11:09:42 -0800 Subject: [PATCH] wgpu-hal: Adjust imports to eventually allow `no_std`. We need `core::error::Error` (Rust 1.81) to make it a reality. --- wgpu-hal/src/auxil/dxgi/conv.rs | 2 +- wgpu-hal/src/auxil/dxgi/exception.rs | 6 +++- wgpu-hal/src/auxil/dxgi/factory.rs | 2 +- wgpu-hal/src/auxil/dxgi/mod.rs | 2 ++ wgpu-hal/src/auxil/renderdoc.rs | 2 +- wgpu-hal/src/dx12/adapter.rs | 2 ++ wgpu-hal/src/dx12/command.rs | 14 ++++---- wgpu-hal/src/dx12/descriptor.rs | 2 +- wgpu-hal/src/dx12/device.rs | 2 ++ wgpu-hal/src/dx12/instance.rs | 2 +- wgpu-hal/src/dx12/mod.rs | 4 ++- wgpu-hal/src/dx12/sampler.rs | 2 ++ wgpu-hal/src/dx12/shader_compilation.rs | 4 +-- wgpu-hal/src/dynamic/adapter.rs | 2 ++ wgpu-hal/src/dynamic/command.rs | 5 +-- wgpu-hal/src/dynamic/device.rs | 2 ++ wgpu-hal/src/dynamic/instance.rs | 2 ++ wgpu-hal/src/dynamic/mod.rs | 46 ++++++++++++++----------- wgpu-hal/src/dynamic/queue.rs | 2 ++ wgpu-hal/src/dynamic/surface.rs | 7 ++-- wgpu-hal/src/gles/adapter.rs | 10 ++++-- wgpu-hal/src/gles/command.rs | 15 ++++---- wgpu-hal/src/gles/device.rs | 31 ++++++++--------- wgpu-hal/src/gles/egl.rs | 9 +++-- wgpu-hal/src/gles/emscripten.rs | 8 ++--- wgpu-hal/src/gles/fence.rs | 3 +- wgpu-hal/src/gles/mod.rs | 23 +++++++------ wgpu-hal/src/gles/queue.rs | 11 +++--- wgpu-hal/src/gles/web.rs | 4 ++- wgpu-hal/src/gles/wgl.rs | 5 +++ wgpu-hal/src/lib.rs | 31 +++++++++++------ wgpu-hal/src/metal/command.rs | 7 +++- wgpu-hal/src/metal/device.rs | 11 +++--- wgpu-hal/src/metal/mod.rs | 5 +++ wgpu-hal/src/metal/surface.rs | 1 + wgpu-hal/src/noop/buffer.rs | 6 ++-- wgpu-hal/src/noop/command.rs | 1 + wgpu-hal/src/noop/mod.rs | 13 ++++--- wgpu-hal/src/vulkan/adapter.rs | 4 +-- wgpu-hal/src/vulkan/conv.rs | 2 ++ wgpu-hal/src/vulkan/device.rs | 21 +++++------ wgpu-hal/src/vulkan/instance.rs | 4 +++ wgpu-hal/src/vulkan/mod.rs | 4 +++ 43 files changed, 213 insertions(+), 128 deletions(-) diff --git a/wgpu-hal/src/auxil/dxgi/conv.rs b/wgpu-hal/src/auxil/dxgi/conv.rs index 13e6199b8b..22c752a896 100644 --- a/wgpu-hal/src/auxil/dxgi/conv.rs +++ b/wgpu-hal/src/auxil/dxgi/conv.rs @@ -1,4 +1,4 @@ -use std::{ffi::OsString, os::windows::ffi::OsStringExt}; +use std::{ffi::OsString, os::windows::ffi::OsStringExt, string::String}; use windows::Win32::Graphics::Dxgi; diff --git a/wgpu-hal/src/auxil/dxgi/exception.rs b/wgpu-hal/src/auxil/dxgi/exception.rs index 15311e3953..3700a24977 100644 --- a/wgpu-hal/src/auxil/dxgi/exception.rs +++ b/wgpu-hal/src/auxil/dxgi/exception.rs @@ -1,4 +1,8 @@ -use std::{borrow::Cow, slice}; +use std::{ + borrow::Cow, + slice, + string::{String, ToString as _}, +}; use parking_lot::Mutex; use windows::Win32::{Foundation, System::Diagnostics::Debug}; diff --git a/wgpu-hal/src/auxil/dxgi/factory.rs b/wgpu-hal/src/auxil/dxgi/factory.rs index 37ab52bf42..23bb291cc3 100644 --- a/wgpu-hal/src/auxil/dxgi/factory.rs +++ b/wgpu-hal/src/auxil/dxgi/factory.rs @@ -1,4 +1,4 @@ -use std::ops::Deref; +use std::{ops::Deref, string::String, vec::Vec}; use windows::{core::Interface as _, Win32::Graphics::Dxgi}; diff --git a/wgpu-hal/src/auxil/dxgi/mod.rs b/wgpu-hal/src/auxil/dxgi/mod.rs index 559969633c..9b945923b8 100644 --- a/wgpu-hal/src/auxil/dxgi/mod.rs +++ b/wgpu-hal/src/auxil/dxgi/mod.rs @@ -1,3 +1,5 @@ +#![allow(clippy::std_instead_of_alloc, clippy::std_instead_of_core)] + pub mod conv; pub mod exception; pub mod factory; diff --git a/wgpu-hal/src/auxil/renderdoc.rs b/wgpu-hal/src/auxil/renderdoc.rs index 3879bb9545..14a5832dc0 100644 --- a/wgpu-hal/src/auxil/renderdoc.rs +++ b/wgpu-hal/src/auxil/renderdoc.rs @@ -1,6 +1,6 @@ //! RenderDoc integration - -use std::{ffi, os, ptr}; +use std::{ffi, os, ptr, string::String}; /// The dynamically loaded RenderDoc API function table #[repr(C)] diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 370b842ade..bc5c943d88 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -1,8 +1,10 @@ use std::{ mem::{size_of, size_of_val}, ptr, + string::String, sync::Arc, thread, + vec::Vec, }; use parking_lot::Mutex; diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 033f932b4f..474500d145 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1,15 +1,17 @@ +use std::{mem, ops::Range, vec::Vec}; + +use windows::Win32::{ + Foundation, + Graphics::{Direct3D12, Dxgi}, +}; +use windows_core::Interface; + use super::conv; use crate::{ auxil::{self, dxgi::result::HResult as _}, dx12::borrow_interface_temporarily, AccelerationStructureEntries, }; -use std::{mem, ops::Range}; -use windows::Win32::{ - Foundation, - Graphics::{Direct3D12, Dxgi}, -}; -use windows_core::Interface; fn make_box(origin: &wgt::Origin3d, size: &crate::CopyExtent) -> Direct3D12::D3D12_BOX { Direct3D12::D3D12_BOX { diff --git a/wgpu-hal/src/dx12/descriptor.rs b/wgpu-hal/src/dx12/descriptor.rs index d191d87765..ae42b18379 100644 --- a/wgpu-hal/src/dx12/descriptor.rs +++ b/wgpu-hal/src/dx12/descriptor.rs @@ -1,4 +1,4 @@ -use std::fmt; +use std::{fmt, vec::Vec}; use bit_set::BitSet; use parking_lot::Mutex; diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 9cbb6cd14e..a5fbe283db 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -4,8 +4,10 @@ use std::{ mem::{self, size_of, size_of_val}, num::NonZeroU32, ptr, slice, + string::{String, ToString as _}, sync::Arc, time::{Duration, Instant}, + vec::Vec, }; use parking_lot::Mutex; diff --git a/wgpu-hal/src/dx12/instance.rs b/wgpu-hal/src/dx12/instance.rs index 68d988da37..69f1ac7864 100644 --- a/wgpu-hal/src/dx12/instance.rs +++ b/wgpu-hal/src/dx12/instance.rs @@ -1,4 +1,4 @@ -use std::{mem::size_of_val, sync::Arc}; +use std::{mem::size_of_val, string::String, sync::Arc, vec::Vec}; use parking_lot::RwLock; use windows::{ diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index d5d6843c39..99e29e87c9 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -72,6 +72,8 @@ Otherwise, we pass a range corresponding only to the current bind group. !*/ +#![allow(clippy::std_instead_of_alloc, clippy::std_instead_of_core)] + mod adapter; mod command; mod conv; @@ -84,7 +86,7 @@ mod suballocation; mod types; mod view; -use std::{ffi, fmt, mem, num::NonZeroU32, ops::Deref, sync::Arc}; +use std::{borrow::ToOwned as _, ffi, fmt, mem, num::NonZeroU32, ops::Deref, sync::Arc, vec::Vec}; use arrayvec::ArrayVec; use parking_lot::{Mutex, RwLock}; diff --git a/wgpu-hal/src/dx12/sampler.rs b/wgpu-hal/src/dx12/sampler.rs index f49555391f..b4570fe1bd 100644 --- a/wgpu-hal/src/dx12/sampler.rs +++ b/wgpu-hal/src/dx12/sampler.rs @@ -2,6 +2,8 @@ //! //! Nearly identical to the Vulkan sampler cache, with added descriptor heap management. +use std::vec::Vec; + use hashbrown::{hash_map::Entry, HashMap}; use ordered_float::OrderedFloat; diff --git a/wgpu-hal/src/dx12/shader_compilation.rs b/wgpu-hal/src/dx12/shader_compilation.rs index 957bd8c3c4..f9f3a6b740 100644 --- a/wgpu-hal/src/dx12/shader_compilation.rs +++ b/wgpu-hal/src/dx12/shader_compilation.rs @@ -1,6 +1,6 @@ +use std::{ffi::CStr, path::PathBuf, string::String, vec::Vec}; + use crate::auxil::dxgi::result::HResult; -use std::ffi::CStr; -use std::path::PathBuf; use thiserror::Error; use windows::{ core::{Interface, PCSTR, PCWSTR}, diff --git a/wgpu-hal/src/dynamic/adapter.rs b/wgpu-hal/src/dynamic/adapter.rs index aebe8ec775..e97aca1ce3 100644 --- a/wgpu-hal/src/dynamic/adapter.rs +++ b/wgpu-hal/src/dynamic/adapter.rs @@ -1,3 +1,5 @@ +use alloc::boxed::Box; + use crate::{ Adapter, Api, DeviceError, OpenDevice, SurfaceCapabilities, TextureFormatCapabilities, }; diff --git a/wgpu-hal/src/dynamic/command.rs b/wgpu-hal/src/dynamic/command.rs index cc6dd81fe0..7b79866ad1 100644 --- a/wgpu-hal/src/dynamic/command.rs +++ b/wgpu-hal/src/dynamic/command.rs @@ -1,4 +1,5 @@ -use std::ops::Range; +use alloc::{boxed::Box, vec::Vec}; +use core::ops::Range; use crate::{ AccelerationStructureBarrier, Api, Attachment, BufferBarrier, BufferBinding, BufferCopy, @@ -13,7 +14,7 @@ use super::{ DynTexture, DynTextureView, }; -pub trait DynCommandEncoder: DynResource + std::fmt::Debug { +pub trait DynCommandEncoder: DynResource + core::fmt::Debug { unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>; unsafe fn discard_encoding(&mut self); diff --git a/wgpu-hal/src/dynamic/device.rs b/wgpu-hal/src/dynamic/device.rs index 9366270bcf..f0519609cc 100644 --- a/wgpu-hal/src/dynamic/device.rs +++ b/wgpu-hal/src/dynamic/device.rs @@ -1,3 +1,5 @@ +use alloc::{borrow::ToOwned as _, boxed::Box, vec::Vec}; + use crate::{ AccelerationStructureBuildSizes, AccelerationStructureDescriptor, Api, BindGroupDescriptor, BindGroupLayoutDescriptor, BufferDescriptor, BufferMapping, CommandEncoderDescriptor, diff --git a/wgpu-hal/src/dynamic/instance.rs b/wgpu-hal/src/dynamic/instance.rs index d5817c1513..5c209eddd1 100644 --- a/wgpu-hal/src/dynamic/instance.rs +++ b/wgpu-hal/src/dynamic/instance.rs @@ -1,3 +1,5 @@ +use alloc::{boxed::Box, vec::Vec}; + use crate::{Api, Capabilities, ExposedAdapter, Instance, InstanceError}; use super::{DynAdapter, DynResource, DynResourceExt as _, DynSurface}; diff --git a/wgpu-hal/src/dynamic/mod.rs b/wgpu-hal/src/dynamic/mod.rs index 5509d7cce6..a8dbae94ee 100644 --- a/wgpu-hal/src/dynamic/mod.rs +++ b/wgpu-hal/src/dynamic/mod.rs @@ -12,7 +12,11 @@ pub use instance::{DynExposedAdapter, DynInstance}; pub use queue::DynQueue; pub use surface::{DynAcquiredSurfaceTexture, DynSurface}; -use std::any::Any; +use alloc::boxed::Box; +use core::{ + any::{Any, TypeId}, + fmt, +}; use wgt::WasmNotSendSync; @@ -33,11 +37,11 @@ macro_rules! impl_dyn_resource { ($($type:ty),*) => { $( impl crate::DynResource for $type { - fn as_any(&self) -> &dyn ::std::any::Any { + fn as_any(&self) -> &dyn ::core::any::Any { self } - fn as_any_mut(&mut self) -> &mut dyn ::std::any::Any { + fn as_any_mut(&mut self) -> &mut dyn ::core::any::Any { self } } @@ -80,9 +84,9 @@ impl DynResourceExt for R { unsafe fn unbox(self: Box) -> T { debug_assert!( - ::type_id(self.as_ref()) == std::any::TypeId::of::(), + ::type_id(self.as_ref()) == TypeId::of::(), "Resource doesn't have the expected type, expected {:?}, got {:?}", - std::any::TypeId::of::(), + TypeId::of::(), ::type_id(self.as_ref()) ); @@ -100,25 +104,25 @@ impl DynResourceExt for R { } } -pub trait DynAccelerationStructure: DynResource + std::fmt::Debug {} -pub trait DynBindGroup: DynResource + std::fmt::Debug {} -pub trait DynBindGroupLayout: DynResource + std::fmt::Debug {} -pub trait DynBuffer: DynResource + std::fmt::Debug {} -pub trait DynCommandBuffer: DynResource + std::fmt::Debug {} -pub trait DynComputePipeline: DynResource + std::fmt::Debug {} -pub trait DynFence: DynResource + std::fmt::Debug {} -pub trait DynPipelineCache: DynResource + std::fmt::Debug {} -pub trait DynPipelineLayout: DynResource + std::fmt::Debug {} -pub trait DynQuerySet: DynResource + std::fmt::Debug {} -pub trait DynRenderPipeline: DynResource + std::fmt::Debug {} -pub trait DynSampler: DynResource + std::fmt::Debug {} -pub trait DynShaderModule: DynResource + std::fmt::Debug {} +pub trait DynAccelerationStructure: DynResource + fmt::Debug {} +pub trait DynBindGroup: DynResource + fmt::Debug {} +pub trait DynBindGroupLayout: DynResource + fmt::Debug {} +pub trait DynBuffer: DynResource + fmt::Debug {} +pub trait DynCommandBuffer: DynResource + fmt::Debug {} +pub trait DynComputePipeline: DynResource + fmt::Debug {} +pub trait DynFence: DynResource + fmt::Debug {} +pub trait DynPipelineCache: DynResource + fmt::Debug {} +pub trait DynPipelineLayout: DynResource + fmt::Debug {} +pub trait DynQuerySet: DynResource + fmt::Debug {} +pub trait DynRenderPipeline: DynResource + fmt::Debug {} +pub trait DynSampler: DynResource + fmt::Debug {} +pub trait DynShaderModule: DynResource + fmt::Debug {} pub trait DynSurfaceTexture: - DynResource + std::borrow::Borrow + std::fmt::Debug + DynResource + core::borrow::Borrow + fmt::Debug { } -pub trait DynTexture: DynResource + std::fmt::Debug {} -pub trait DynTextureView: DynResource + std::fmt::Debug {} +pub trait DynTexture: DynResource + fmt::Debug {} +pub trait DynTextureView: DynResource + fmt::Debug {} impl<'a> BufferBinding<'a, dyn DynBuffer> { pub fn expect_downcast(self) -> BufferBinding<'a, B> { diff --git a/wgpu-hal/src/dynamic/queue.rs b/wgpu-hal/src/dynamic/queue.rs index 14d7e5a969..2af6d1f9c7 100644 --- a/wgpu-hal/src/dynamic/queue.rs +++ b/wgpu-hal/src/dynamic/queue.rs @@ -1,3 +1,5 @@ +use alloc::{boxed::Box, vec::Vec}; + use crate::{ DeviceError, DynCommandBuffer, DynFence, DynResource, DynSurface, DynSurfaceTexture, FenceValue, Queue, SurfaceError, diff --git a/wgpu-hal/src/dynamic/surface.rs b/wgpu-hal/src/dynamic/surface.rs index d6c3dad623..97ac74e6fc 100644 --- a/wgpu-hal/src/dynamic/surface.rs +++ b/wgpu-hal/src/dynamic/surface.rs @@ -1,3 +1,6 @@ +use alloc::boxed::Box; +use core::time::Duration; + use crate::{ DynDevice, DynFence, DynResource, DynSurfaceTexture, Surface, SurfaceConfiguration, SurfaceError, @@ -25,7 +28,7 @@ pub trait DynSurface: DynResource { unsafe fn acquire_texture( &self, - timeout: Option, + timeout: Option, fence: &dyn DynFence, ) -> Result, SurfaceError>; @@ -49,7 +52,7 @@ impl DynSurface for S { unsafe fn acquire_texture( &self, - timeout: Option, + timeout: Option, fence: &dyn DynFence, ) -> Result, SurfaceError> { let fence = fence.expect_downcast_ref(); diff --git a/wgpu-hal/src/gles/adapter.rs b/wgpu-hal/src/gles/adapter.rs index 0ab7f4507a..248e7f7a5d 100644 --- a/wgpu-hal/src/gles/adapter.rs +++ b/wgpu-hal/src/gles/adapter.rs @@ -1,6 +1,8 @@ +use alloc::{borrow::ToOwned as _, format, string::String, sync::Arc, vec, vec::Vec}; +use core::sync::atomic::AtomicU8; + use glow::HasContext; use parking_lot::Mutex; -use std::sync::{atomic::AtomicU8, Arc}; use wgt::AstcChannel; use crate::auxil::db; @@ -79,7 +81,7 @@ impl super::Adapter { /// resulting in an `Err`. pub(super) fn parse_full_version(src: &str) -> Result<(u8, u8), crate::InstanceError> { let (version, _vendor_info) = match src.find(' ') { - Some(i) => (&src[..i], src[i + 1..].to_string()), + Some(i) => (&src[..i], src[i + 1..].to_owned()), None => (src, String::new()), }; @@ -1247,7 +1249,9 @@ impl super::AdapterShared { let buffer_mapping = unsafe { gl.map_buffer_range(target, offset, length as _, glow::MAP_READ_BIT) }; - unsafe { std::ptr::copy_nonoverlapping(buffer_mapping, dst_data.as_mut_ptr(), length) }; + unsafe { + core::ptr::copy_nonoverlapping(buffer_mapping, dst_data.as_mut_ptr(), length) + }; unsafe { gl.unmap_buffer(target) }; } diff --git a/wgpu-hal/src/gles/command.rs b/wgpu-hal/src/gles/command.rs index 6f047682db..2bff6278f1 100644 --- a/wgpu-hal/src/gles/command.rs +++ b/wgpu-hal/src/gles/command.rs @@ -1,10 +1,14 @@ -use super::{conv, Command as C}; -use arrayvec::ArrayVec; -use std::{ +use alloc::string::String; +use core::{ mem::{self, size_of, size_of_val}, ops::Range, + slice, }; +use arrayvec::ArrayVec; + +use super::{conv, Command as C}; + #[derive(Clone, Copy, Debug, Default)] struct TextureSlotDesc { tex_target: super::BindTarget, @@ -84,8 +88,7 @@ impl super::CommandBuffer { } fn add_push_constant_data(&mut self, data: &[u32]) -> Range { - let data_raw = - unsafe { std::slice::from_raw_parts(data.as_ptr().cast(), size_of_val(data)) }; + let data_raw = unsafe { slice::from_raw_parts(data.as_ptr().cast(), size_of_val(data)) }; let start = self.data_bytes.len(); assert!(start < u32::MAX as usize); self.data_bytes.extend_from_slice(data_raw); @@ -262,7 +265,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> { self.state = State::default(); - self.cmd_buffer.label = label.map(str::to_string); + self.cmd_buffer.label = label.map(String::from); Ok(()) } unsafe fn discard_encoding(&mut self) { diff --git a/wgpu-hal/src/gles/device.rs b/wgpu-hal/src/gles/device.rs index a9d6209afb..56060de656 100644 --- a/wgpu-hal/src/gles/device.rs +++ b/wgpu-hal/src/gles/device.rs @@ -1,17 +1,16 @@ -use super::{conv, PrivateCapabilities}; -use crate::auxil::map_naga_stage; +use alloc::{ + borrow::ToOwned, format, string::String, string::ToString as _, sync::Arc, vec, vec::Vec, +}; +use core::{cmp::max, convert::TryInto, num::NonZeroU32, ptr, sync::atomic::Ordering}; +use std::sync::Mutex; + +use arrayvec::ArrayVec; use glow::HasContext; use naga::FastHashMap; -use std::{ - cmp::max, - convert::TryInto, - ptr, - sync::{Arc, Mutex}, -}; +use super::{conv, PrivateCapabilities}; +use crate::auxil::map_naga_stage; use crate::TlasInstance; -use arrayvec::ArrayVec; -use std::sync::atomic::Ordering; type ShaderStage<'a> = ( naga::ShaderStage, @@ -24,7 +23,7 @@ struct CompilationContext<'a> { sampler_map: &'a mut super::SamplerBindMap, name_binding_map: &'a mut NameBindingMap, push_constant_items: &'a mut Vec, - multiview: Option, + multiview: Option, } impl CompilationContext<'_> { @@ -117,7 +116,7 @@ impl super::Device { #[cfg(any(native, Emscripten))] pub unsafe fn texture_from_raw( &self, - name: std::num::NonZeroU32, + name: NonZeroU32, desc: &crate::TextureDescriptor, drop_callback: Option, ) -> super::Texture { @@ -144,7 +143,7 @@ impl super::Device { #[cfg(any(native, Emscripten))] pub unsafe fn texture_from_raw_renderbuffer( &self, - name: std::num::NonZeroU32, + name: NonZeroU32, desc: &crate::TextureDescriptor, drop_callback: Option, ) -> super::Texture { @@ -212,7 +211,7 @@ impl super::Device { use naga::back::glsl; let pipeline_options = glsl::PipelineOptions { shader_stage: naga_stage, - entry_point: stage.entry_point.to_string(), + entry_point: stage.entry_point.to_owned(), multiview: context.multiview, }; @@ -300,7 +299,7 @@ impl super::Device { shaders: ArrayVec, { crate::MAX_CONCURRENT_SHADER_STAGES }>, layout: &super::PipelineLayout, #[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>, - multiview: Option, + multiview: Option, ) -> Result, crate::PipelineError> { let mut program_stages = ArrayVec::new(); let mut group_to_binding_to_slot = Vec::with_capacity(layout.group_infos.len()); @@ -349,7 +348,7 @@ impl super::Device { shaders: ArrayVec, { crate::MAX_CONCURRENT_SHADER_STAGES }>, layout: &super::PipelineLayout, #[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>, - multiview: Option, + multiview: Option, glsl_version: naga::back::glsl::Version, private_caps: PrivateCapabilities, ) -> Result, crate::PipelineError> { diff --git a/wgpu-hal/src/gles/egl.rs b/wgpu-hal/src/gles/egl.rs index 46ff37fbc1..c88abb28c1 100644 --- a/wgpu-hal/src/gles/egl.rs +++ b/wgpu-hal/src/gles/egl.rs @@ -1,10 +1,15 @@ +#![allow(clippy::std_instead_of_alloc, clippy::std_instead_of_core)] + +use std::{ + ffi, mem::ManuallyDrop, os::raw, ptr, rc::Rc, string::String, sync::Arc, time::Duration, + vec::Vec, +}; + use glow::HasContext; use hashbrown::HashMap; use once_cell::sync::Lazy; use parking_lot::{MappedMutexGuard, Mutex, MutexGuard, RwLock}; -use std::{ffi, mem::ManuallyDrop, os::raw, ptr, rc::Rc, sync::Arc, time::Duration}; - /// The amount of time to wait while trying to obtain a lock to the adapter context const CONTEXT_LOCK_TIMEOUT_SECS: u64 = 1; diff --git a/wgpu-hal/src/gles/emscripten.rs b/wgpu-hal/src/gles/emscripten.rs index 8a341d54d4..88cc0becc6 100644 --- a/wgpu-hal/src/gles/emscripten.rs +++ b/wgpu-hal/src/gles/emscripten.rs @@ -1,10 +1,10 @@ extern "C" { /// returns 1 if success. 0 if failure. extension name must be null terminated fn emscripten_webgl_enable_extension( - context: std::ffi::c_int, - extension: *const std::ffi::c_char, - ) -> std::ffi::c_int; - fn emscripten_webgl_get_current_context() -> std::ffi::c_int; + context: core::ffi::c_int, + extension: *const core::ffi::c_char, + ) -> core::ffi::c_int; + fn emscripten_webgl_get_current_context() -> core::ffi::c_int; } /// Webgl requires extensions to be enabled before using them. /// This function can be used to enable webgl extension on emscripten target. diff --git a/wgpu-hal/src/gles/fence.rs b/wgpu-hal/src/gles/fence.rs index 8622ae3752..b29a632429 100644 --- a/wgpu-hal/src/gles/fence.rs +++ b/wgpu-hal/src/gles/fence.rs @@ -1,4 +1,5 @@ -use std::sync::atomic::Ordering; +use alloc::vec::Vec; +use core::sync::atomic::Ordering; use glow::HasContext; diff --git a/wgpu-hal/src/gles/mod.rs b/wgpu-hal/src/gles/mod.rs index c462d7b2ec..756838d776 100644 --- a/wgpu-hal/src/gles/mod.rs +++ b/wgpu-hal/src/gles/mod.rs @@ -98,8 +98,6 @@ mod device; mod fence; mod queue; -use crate::{CopyExtent, TextureDescriptor}; - pub use fence::Fence; #[cfg(not(any(windows, webgl)))] @@ -117,14 +115,19 @@ use self::wgl::AdapterContext; #[cfg(windows)] use self::wgl::{Instance, Surface}; -use arrayvec::ArrayVec; - -use glow::HasContext; - -use naga::FastHashMap; +use alloc::{boxed::Box, string::String, string::ToString as _, sync::Arc, vec::Vec}; +use core::{ + fmt, + ops::Range, + sync::atomic::{AtomicU32, AtomicU8}, +}; use parking_lot::Mutex; -use std::sync::atomic::{AtomicU32, AtomicU8}; -use std::{fmt, ops::Range, sync::Arc}; + +use arrayvec::ArrayVec; +use glow::HasContext; +use naga::FastHashMap; + +use crate::{CopyExtent, TextureDescriptor}; #[derive(Clone, Debug)] pub struct Api; @@ -403,7 +406,7 @@ pub struct Texture { impl crate::DynTexture for Texture {} impl crate::DynSurfaceTexture for Texture {} -impl std::borrow::Borrow for Texture { +impl core::borrow::Borrow for Texture { fn borrow(&self) -> &dyn crate::DynTexture { self } diff --git a/wgpu-hal/src/gles/queue.rs b/wgpu-hal/src/gles/queue.rs index 50126a76c1..73681b7b70 100644 --- a/wgpu-hal/src/gles/queue.rs +++ b/wgpu-hal/src/gles/queue.rs @@ -1,16 +1,13 @@ use super::{conv::is_layered_target, Command as C, PrivateCapabilities}; +use alloc::sync::Arc; use arrayvec::ArrayVec; +use core::{mem::size_of, slice, sync::atomic::Ordering}; use glow::HasContext; -use std::{ - mem::size_of, - slice, - sync::{atomic::Ordering, Arc}, -}; const DEBUG_ID: u32 = 0; -fn extract_marker<'a>(data: &'a [u8], range: &std::ops::Range) -> &'a str { - std::str::from_utf8(&data[range.start as usize..range.end as usize]).unwrap() +fn extract_marker<'a>(data: &'a [u8], range: &core::ops::Range) -> &'a str { + core::str::from_utf8(&data[range.start as usize..range.end as usize]).unwrap() } fn get_2d_target(target: u32, array_layer: u32) -> u32 { diff --git a/wgpu-hal/src/gles/web.rs b/wgpu-hal/src/gles/web.rs index be02db8619..485686e91e 100644 --- a/wgpu-hal/src/gles/web.rs +++ b/wgpu-hal/src/gles/web.rs @@ -1,3 +1,5 @@ +use alloc::{format, string::String, vec::Vec}; + use glow::HasContext; use parking_lot::{Mutex, RwLock}; use wasm_bindgen::{JsCast, JsValue}; @@ -420,7 +422,7 @@ impl crate::Surface for Surface { unsafe fn acquire_texture( &self, - _timeout_ms: Option, //TODO + _timeout_ms: Option, //TODO _fence: &super::Fence, ) -> Result>, crate::SurfaceError> { let swapchain = self.swapchain.read(); diff --git a/wgpu-hal/src/gles/wgl.rs b/wgpu-hal/src/gles/wgl.rs index 41759f3817..0e7acc1884 100644 --- a/wgpu-hal/src/gles/wgl.rs +++ b/wgpu-hal/src/gles/wgl.rs @@ -1,14 +1,19 @@ +#![allow(clippy::std_instead_of_alloc, clippy::std_instead_of_core)] + use std::{ + borrow::ToOwned as _, ffi::{c_void, CStr, CString}, mem::{self, size_of, size_of_val, ManuallyDrop}, os::raw::c_int, ptr, + string::String, sync::{ mpsc::{sync_channel, SyncSender}, Arc, }, thread, time::Duration, + vec::Vec, }; use glow::HasContext; diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 249a002dd5..3bfc5dab3a 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -202,6 +202,7 @@ //! //! [wiki-debug]: https://github.com/gfx-rs/wgpu/wiki/Debugging-wgpu-Applications +#![no_std] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] #![allow( // this happens on the GL backend, where it is both thread safe and non-thread safe in the same code. @@ -226,7 +227,10 @@ clippy::pattern_type_mismatch, )] #![warn( + clippy::alloc_instead_of_core, clippy::ptr_as_ptr, + clippy::std_instead_of_alloc, + clippy::std_instead_of_core, trivial_casts, trivial_numeric_casts, unsafe_op_in_unsafe_fn, @@ -234,7 +238,12 @@ unused_qualifications )] +extern crate alloc; extern crate wgpu_types as wgt; +// TODO(https://github.com/gfx-rs/wgpu/issues/6826): disable std except on noop and gles-WebGL. +// Requires Rust 1.81 for core::error::Error. +#[macro_use] +extern crate std; /// DirectX12 API internals. #[cfg(dx12)] @@ -276,14 +285,17 @@ pub use dynamic::{ DynShaderModule, DynSurface, DynSurfaceTexture, DynTexture, DynTextureView, }; -use std::{ - borrow::{Borrow, Cow}, +#[allow(unused)] +use alloc::boxed::Box; +use alloc::{borrow::Cow, string::String, sync::Arc, vec::Vec}; +use core::{ + borrow::Borrow, fmt, num::NonZeroU32, ops::{Range, RangeInclusive}, ptr::NonNull, - sync::Arc, }; +use std::error::Error; // TODO(https://github.com/gfx-rs/wgpu/issues/6826): use core::error after MSRV bump use bitflags::bitflags; use parking_lot::Mutex; @@ -305,7 +317,7 @@ pub const QUERY_SIZE: wgt::BufferAddress = 8; pub type Label<'a> = Option<&'a str>; pub type MemoryRange = Range; pub type FenceValue = u64; -pub type AtomicFenceValue = std::sync::atomic::AtomicU64; +pub type AtomicFenceValue = core::sync::atomic::AtomicU64; /// A callback to signal that wgpu is no longer using a resource. #[cfg(any(gles, vulkan))] @@ -417,7 +429,7 @@ pub struct InstanceError { /// Underlying error value, if any is available. #[source] - source: Option>, + source: Option>, } impl InstanceError { @@ -429,10 +441,7 @@ impl InstanceError { } } #[allow(dead_code)] // may be unused on some platforms - pub(crate) fn with_source( - message: String, - source: impl std::error::Error + Send + Sync + 'static, - ) -> Self { + pub(crate) fn with_source(message: String, source: impl Error + Send + Sync + 'static) -> Self { Self { message, source: Some(Arc::new(source)), @@ -585,13 +594,13 @@ pub trait Surface: WasmNotSendSync { /// /// [`texture`]: AcquiredSurfaceTexture::texture /// [`SurfaceTexture`]: Api::SurfaceTexture - /// [`borrow`]: std::borrow::Borrow::borrow + /// [`borrow`]: alloc::borrow::Borrow::borrow /// [`Texture`]: Api::Texture /// [`Fence`]: Api::Fence /// [`self.discard_texture`]: Surface::discard_texture unsafe fn acquire_texture( &self, - timeout: Option, + timeout: Option, fence: &::Fence, ) -> Result>, SurfaceError>; diff --git a/wgpu-hal/src/metal/command.rs b/wgpu-hal/src/metal/command.rs index a836765b18..65e3fafe73 100644 --- a/wgpu-hal/src/metal/command.rs +++ b/wgpu-hal/src/metal/command.rs @@ -1,6 +1,11 @@ use super::{conv, AsNative, TimestampQuerySupport}; use crate::CommandEncoder as _; -use std::{borrow::Cow, mem::size_of, ops::Range}; +use std::{ + borrow::{Cow, ToOwned as _}, + mem::size_of, + ops::Range, + vec::Vec, +}; // has to match `Temp::binding_sizes` const WORD_SIZE: usize = 4; diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 94b08bda05..885772909e 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -1,9 +1,8 @@ +use alloc::{borrow::ToOwned as _, sync::Arc, vec::Vec}; +use core::{ptr::NonNull, sync::atomic}; +use std::{thread, time}; + use parking_lot::Mutex; -use std::{ - ptr::NonNull, - sync::{atomic, Arc}, - thread, time, -}; use super::conv; use crate::auxil::map_naga_stage; @@ -157,7 +156,7 @@ impl super::Device { spirv_cross_compatibility: false, fake_missing_bindings: false, per_entry_point_map: naga::back::msl::EntryPointResourceMap::from([( - stage.entry_point.to_string(), + stage.entry_point.to_owned(), ep_resources.clone(), )]), bounds_check_policies: naga::proc::BoundsCheckPolicies { diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index 22410a0884..d97d67576b 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -13,6 +13,8 @@ end of the VS buffer table. !*/ +#![allow(clippy::std_instead_of_alloc, clippy::std_instead_of_core)] + // `MTLFeatureSet` is superseded by `MTLGpuFamily`. // However, `MTLGpuFamily` is only supported starting MacOS 10.15, whereas our minimum target is MacOS 10.13, // See https://github.com/gpuweb/gpuweb/issues/1069 for minimum spec. @@ -27,10 +29,13 @@ mod surface; mod time; use std::{ + borrow::ToOwned as _, fmt, iter, ops, ptr::NonNull, + string::String, sync::{atomic, Arc}, thread, + vec::Vec, }; use arrayvec::ArrayVec; diff --git a/wgpu-hal/src/metal/surface.rs b/wgpu-hal/src/metal/surface.rs index a8bc91b7bf..29dbc19b0c 100644 --- a/wgpu-hal/src/metal/surface.rs +++ b/wgpu-hal/src/metal/surface.rs @@ -1,5 +1,6 @@ #![allow(clippy::let_unit_value)] // `let () =` being used to constrain result type +use std::borrow::ToOwned as _; use std::mem::ManuallyDrop; use std::ptr::NonNull; use std::thread; diff --git a/wgpu-hal/src/noop/buffer.rs b/wgpu-hal/src/noop/buffer.rs index ab3b1a5e62..6e9572ef8f 100644 --- a/wgpu-hal/src/noop/buffer.rs +++ b/wgpu-hal/src/noop/buffer.rs @@ -1,7 +1,5 @@ -use core::cell::UnsafeCell; -use core::ops::Range; -use core::ptr; -use std::sync::Arc; +use alloc::{sync::Arc, vec::Vec}; +use core::{cell::UnsafeCell, ops::Range, ptr}; #[derive(Clone, Debug)] pub struct Buffer { diff --git a/wgpu-hal/src/noop/command.rs b/wgpu-hal/src/noop/command.rs index 9163e49a47..0008341098 100644 --- a/wgpu-hal/src/noop/command.rs +++ b/wgpu-hal/src/noop/command.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use core::mem; use core::ops::Range; diff --git a/wgpu-hal/src/noop/mod.rs b/wgpu-hal/src/noop/mod.rs index 8ee434a04f..2e1f8f665c 100644 --- a/wgpu-hal/src/noop/mod.rs +++ b/wgpu-hal/src/noop/mod.rs @@ -1,8 +1,13 @@ #![allow(unused_variables)] +use alloc::{string::String, vec, vec::Vec}; +use core::{ + ptr, + sync::atomic::{AtomicU64, Ordering}, + time::Duration, +}; + use crate::TlasInstance; -use core::ptr; -use core::sync::atomic::{AtomicU64, Ordering}; mod buffer; pub use buffer::Buffer; @@ -71,7 +76,7 @@ impl crate::DynSurfaceTexture for Resource {} impl crate::DynTexture for Resource {} impl crate::DynTextureView for Resource {} -impl std::borrow::Borrow for Resource { +impl core::borrow::Borrow for Resource { fn borrow(&self) -> &dyn crate::DynTexture { self } @@ -202,7 +207,7 @@ impl crate::Surface for Context { unsafe fn acquire_texture( &self, - timeout: Option, + timeout: Option, fence: &Fence, ) -> Result>, crate::SurfaceError> { Ok(None) diff --git a/wgpu-hal/src/vulkan/adapter.rs b/wgpu-hal/src/vulkan/adapter.rs index 8b41be69fc..23b7cdd94b 100644 --- a/wgpu-hal/src/vulkan/adapter.rs +++ b/wgpu-hal/src/vulkan/adapter.rs @@ -1,9 +1,9 @@ -use super::conv; +use std::{borrow::ToOwned as _, collections::BTreeMap, ffi::CStr, sync::Arc, vec::Vec}; use ash::{amd, ext, google, khr, vk}; use parking_lot::Mutex; -use std::{collections::BTreeMap, ffi::CStr, sync::Arc}; +use super::conv; fn depth_stencil_required_flags() -> vk::FormatFeatureFlags { vk::FormatFeatureFlags::SAMPLED_IMAGE | vk::FormatFeatureFlags::DEPTH_STENCIL_ATTACHMENT diff --git a/wgpu-hal/src/vulkan/conv.rs b/wgpu-hal/src/vulkan/conv.rs index 24cc3f4564..204d20adae 100644 --- a/wgpu-hal/src/vulkan/conv.rs +++ b/wgpu-hal/src/vulkan/conv.rs @@ -1,3 +1,5 @@ +use std::vec::Vec; + use ash::vk; impl super::PrivateCapabilities { diff --git a/wgpu-hal/src/vulkan/device.rs b/wgpu-hal/src/vulkan/device.rs index e999ecc741..fe36b3578e 100644 --- a/wgpu-hal/src/vulkan/device.rs +++ b/wgpu-hal/src/vulkan/device.rs @@ -1,21 +1,22 @@ -use super::{conv, RawTlasInstance}; - -use arrayvec::ArrayVec; -use ash::{khr, vk}; -use hashbrown::hash_map::Entry; -use parking_lot::Mutex; - -use crate::TlasInstance; use std::{ - borrow::Cow, + borrow::{Cow, ToOwned as _}, collections::BTreeMap, ffi::{CStr, CString}, mem::{self, size_of, MaybeUninit}, num::NonZeroU32, ptr, slice, sync::Arc, + vec::Vec, }; +use arrayvec::ArrayVec; +use ash::{khr, vk}; +use hashbrown::hash_map::Entry; +use parking_lot::Mutex; + +use super::{conv, RawTlasInstance}; +use crate::TlasInstance; + impl super::DeviceShared { /// Set the name of `object` to `name`. /// @@ -903,7 +904,7 @@ impl super::Device { runtime_checks, } => { let pipeline_options = naga::back::spv::PipelineOptions { - entry_point: stage.entry_point.to_string(), + entry_point: stage.entry_point.to_owned(), shader_stage: naga_stage, }; let needs_temp_options = !runtime_checks.bounds_checks diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index d6ca55800d..a67781dba7 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -1,9 +1,13 @@ use std::{ + borrow::ToOwned as _, + boxed::Box, ffi::{c_void, CStr, CString}, slice, str::FromStr, + string::{String, ToString as _}, sync::Arc, thread, + vec::Vec, }; use arrayvec::ArrayVec; diff --git a/wgpu-hal/src/vulkan/mod.rs b/wgpu-hal/src/vulkan/mod.rs index 8a3c68b6ec..f052d1c25a 100644 --- a/wgpu-hal/src/vulkan/mod.rs +++ b/wgpu-hal/src/vulkan/mod.rs @@ -24,6 +24,8 @@ Otherwise, we manage a pool of `VkFence` objects behind each `hal::Fence`. !*/ +#![allow(clippy::std_instead_of_alloc, clippy::std_instead_of_core)] + mod adapter; mod command; mod conv; @@ -33,11 +35,13 @@ mod sampler; use std::{ borrow::Borrow, + boxed::Box, ffi::{CStr, CString}, fmt, mem, num::NonZeroU32, ops::DerefMut, sync::Arc, + vec::Vec, }; use arrayvec::ArrayVec;