From b58c15815b7deb8ffa775b49ee54f306b4e8de85 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Sat, 14 Mar 2020 23:06:56 -0230 Subject: [PATCH] Move more shared types into wgpu-types --- Cargo.lock | 1 + wgpu-core/Cargo.toml | 1 + wgpu-core/src/command/bind.rs | 3 +- wgpu-core/src/command/compute.rs | 6 +- wgpu-core/src/command/mod.rs | 9 +- wgpu-core/src/command/render.rs | 60 +++--------- wgpu-core/src/command/transfer.rs | 3 +- wgpu-core/src/conv.rs | 41 +++++--- wgpu-core/src/device/mod.rs | 12 +-- wgpu-core/src/lib.rs | 50 ---------- wgpu-core/src/resource.rs | 23 +---- wgpu-core/src/swap_chain.rs | 92 +++++------------- wgpu-core/src/track/texture.rs | 3 +- wgpu-native/src/device.rs | 4 +- wgpu-remote/src/server.rs | 2 +- wgpu-types/Cargo.toml | 1 + wgpu-types/src/lib.rs | 151 ++++++++++++++++++++++++++++++ 17 files changed, 237 insertions(+), 225 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 951f34b347..17dd30ef4f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -742,6 +742,7 @@ name = "wgpu-types" version = "0.1.0" dependencies = [ "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "peek-poke 0.2.0 (git+https://github.com/kvark/peek-poke?rev=969bd7fe2be1a83f87916dc8b388c63cfd457075)", "serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index 5bc785efe2..54c685d3f0 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -46,6 +46,7 @@ optional = true path = "../wgpu-types" package = "wgpu-types" version = "0.1" +features = ["peek-poke"] [target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies] gfx-backend-metal = { version = "0.4" } diff --git a/wgpu-core/src/command/bind.rs b/wgpu-core/src/command/bind.rs index 48ab6cc706..41c8444e80 100644 --- a/wgpu-core/src/command/bind.rs +++ b/wgpu-core/src/command/bind.rs @@ -6,12 +6,11 @@ use crate::{ binding_model::BindGroup, hub::GfxBackend, id::{BindGroupId, BindGroupLayoutId, PipelineLayoutId}, - DynamicOffset, Stored, }; use smallvec::{smallvec, SmallVec}; - +use wgt::DynamicOffset; use std::slice; pub const DEFAULT_BIND_GROUPS: usize = 4; diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index b1fa2618b6..610294e158 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -11,10 +11,9 @@ use crate::{ device::{all_buffer_stages, BIND_BUFFER_ALIGNMENT}, hub::{GfxBackend, Global, Token}, id, - DynamicOffset, }; -use wgt::{BufferAddress, BufferUsage}; +use wgt::{BufferAddress, BufferUsage, DynamicOffset}; use hal::command::CommandBuffer as _; use peek_poke::{Peek, PeekCopy, Poke}; @@ -221,10 +220,9 @@ pub mod compute_ffi { }; use crate::{ id, - DynamicOffset, RawString, }; - use wgt::BufferAddress; +use wgt::{BufferAddress, DynamicOffset}; use std::{convert::TryInto, slice}; /// # Safety diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 6f9a17744e..2f83aef961 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -35,6 +35,7 @@ use std::{ slice, thread::ThreadId, }; +use wgt::RenderPassColorAttachmentDescriptorBase; #[derive(Clone, Copy, Debug, peek_poke::PeekCopy, peek_poke::Poke)] @@ -194,14 +195,6 @@ impl CommandBuffer { } } -#[repr(C)] -#[derive(Clone, Debug, Default)] -pub struct CommandEncoderDescriptor { - // MSVC doesn't allow zero-sized structs - // We can remove this when we actually have a field - pub todo: u32, -} - #[repr(C)] #[derive(Clone, Debug, Default)] pub struct CommandBufferDescriptor { diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 301f192747..3c90151687 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -21,14 +21,23 @@ use crate::{ hub::{GfxBackend, Global, Token}, id, pipeline::PipelineFlags, - resource::{TextureUsage, TextureViewInner}, + resource::TextureViewInner, track::TrackerSet, - Color, - DynamicOffset, Stored, }; -use wgt::{BufferAddress, BufferUsage, IndexFormat, InputStepMode}; +use wgt::{ + BufferAddress, + BufferUsage, + Color, + DynamicOffset, + IndexFormat, + InputStepMode, + LoadOp, + RenderPassColorAttachmentDescriptorBase, + RenderPassDepthStencilAttachmentDescriptorBase, + TextureUsage, +}; use arrayvec::ArrayVec; use hal::command::CommandBuffer as _; use peek_poke::{Peek, PeekCopy, Poke}; @@ -43,45 +52,8 @@ use std::{ slice, }; - -#[repr(C)] -#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PeekCopy, Poke)] -pub enum LoadOp { - Clear = 0, - Load = 1, -} - -#[repr(C)] -#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PeekCopy, Poke)] -pub enum StoreOp { - Clear = 0, - Store = 1, -} - -#[repr(C)] -#[derive(Debug, PeekCopy, Poke)] -pub struct RenderPassColorAttachmentDescriptorBase { - pub attachment: T, - pub resolve_target: R, - pub load_op: LoadOp, - pub store_op: StoreOp, - pub clear_color: Color, -} - -#[repr(C)] -#[derive(Clone, Debug, PeekCopy, Poke)] -pub struct RenderPassDepthStencilAttachmentDescriptorBase { - pub attachment: T, - pub depth_load_op: LoadOp, - pub depth_store_op: StoreOp, - pub clear_depth: f32, - pub stencil_load_op: LoadOp, - pub stencil_store_op: StoreOp, - pub clear_stencil: u32, -} - //Note: this could look better if `cbindgen` wasn't confused by &T used in place of -// a generic paramter, it's not able to mange +// a generic parameter, it's not able to manage pub type OptionRef<'a, T> = Option<&'a T>; pub type RenderPassColorAttachmentDescriptor<'a> = RenderPassColorAttachmentDescriptorBase>; @@ -1156,11 +1128,9 @@ pub mod render_ffi { }; use crate::{ id, - Color, - DynamicOffset, RawString, }; - use wgt::BufferAddress; + use wgt::{BufferAddress, Color, DynamicOffset}; use std::{convert::TryInto, slice}; /// # Safety diff --git a/wgpu-core/src/command/transfer.rs b/wgpu-core/src/command/transfer.rs index e259d4b935..58fe147368 100644 --- a/wgpu-core/src/command/transfer.rs +++ b/wgpu-core/src/command/transfer.rs @@ -7,12 +7,11 @@ use crate::{ device::{all_buffer_stages, all_image_stages}, hub::{GfxBackend, Global, Token}, id::{BufferId, CommandEncoderId, TextureId}, - resource::TextureUsage, Extent3d, Origin3d, }; -use wgt::{BufferAddress, BufferUsage}; +use wgt::{BufferAddress, BufferUsage, TextureUsage}; use hal::command::CommandBuffer as _; use std::iter; diff --git a/wgpu-core/src/conv.rs b/wgpu-core/src/conv.rs index a6d469cfb6..1380a3f685 100644 --- a/wgpu-core/src/conv.rs +++ b/wgpu-core/src/conv.rs @@ -2,8 +2,25 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use crate::{binding_model, command, resource, Color, Extent3d, Features, Origin3d}; -use wgt::{BlendDescriptor, BlendFactor, ColorStateDescriptor, ColorWrite, CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, IndexFormat, PrimitiveTopology, StencilOperation, StencilStateFaceDescriptor, TextureFormat, RasterizationStateDescriptor, VertexFormat}; +use crate::{binding_model, resource, Extent3d, Features, Origin3d}; +use wgt::{ + BlendDescriptor, + BlendFactor, + Color, + ColorStateDescriptor, + ColorWrite, + CompareFunction, + CullMode, + DepthStencilStateDescriptor, + FrontFace, + IndexFormat, + PrimitiveTopology, + StencilOperation, + StencilStateFaceDescriptor, + TextureFormat, + RasterizationStateDescriptor, + VertexFormat, +}; pub fn map_buffer_usage( usage: wgt::BufferUsage, @@ -47,10 +64,10 @@ pub fn map_buffer_usage( } pub fn map_texture_usage( - usage: resource::TextureUsage, + usage: wgt::TextureUsage, aspects: hal::format::Aspects, ) -> hal::image::Usage { - use crate::resource::TextureUsage as W; + use wgt::TextureUsage as W; use hal::image::Usage as U; let mut value = U::empty(); @@ -524,10 +541,10 @@ pub fn map_buffer_state(usage: wgt::BufferUsage) -> hal::buffer::State { } pub fn map_texture_state( - usage: resource::TextureUsage, + usage: wgt::TextureUsage, aspects: hal::format::Aspects, ) -> hal::image::State { - use crate::resource::TextureUsage as W; + use wgt::TextureUsage as W; use hal::image::{Access as A, Layout as L}; let is_color = aspects.contains(hal::format::Aspects::COLOR); @@ -567,17 +584,17 @@ pub fn map_texture_state( } pub fn map_load_store_ops( - load: command::LoadOp, - store: command::StoreOp, + load: wgt::LoadOp, + store: wgt::StoreOp, ) -> hal::pass::AttachmentOps { hal::pass::AttachmentOps { load: match load { - command::LoadOp::Clear => hal::pass::AttachmentLoadOp::Clear, - command::LoadOp::Load => hal::pass::AttachmentLoadOp::Load, + wgt::LoadOp::Clear => hal::pass::AttachmentLoadOp::Clear, + wgt::LoadOp::Load => hal::pass::AttachmentLoadOp::Load, }, store: match store { - command::StoreOp::Clear => hal::pass::AttachmentStoreOp::DontCare, //TODO! - command::StoreOp::Store => hal::pass::AttachmentStoreOp::Store, + wgt::StoreOp::Clear => hal::pass::AttachmentStoreOp::DontCare, //TODO! + wgt::StoreOp::Store => hal::pass::AttachmentStoreOp::Store, }, } } diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index c773e60fc4..120678789e 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -367,7 +367,7 @@ impl Device { match desc.format { TextureFormat::Depth24Plus | TextureFormat::Depth24PlusStencil8 => { assert!(!desc.usage.intersects( - resource::TextureUsage::COPY_SRC | resource::TextureUsage::COPY_DST + wgt::TextureUsage::COPY_SRC | wgt::TextureUsage::COPY_DST )); } _ => {} @@ -1098,12 +1098,12 @@ impl> Global { binding_model::BindingResource::TextureView(id) => { let (usage, image_layout) = match decl.ty { binding_model::BindingType::SampledTexture => ( - resource::TextureUsage::SAMPLED, + wgt::TextureUsage::SAMPLED, hal::image::Layout::ShaderReadOnlyOptimal, ), binding_model::BindingType::ReadonlyStorageTexture | binding_model::BindingType::WriteonlyStorageTexture => { - (resource::TextureUsage::STORAGE, hal::image::Layout::General) + (wgt::TextureUsage::STORAGE, hal::image::Layout::General) } _ => panic!("Mismatched texture binding for {:?}", decl), }; @@ -1235,7 +1235,7 @@ impl> Global { pub fn device_create_command_encoder( &self, device_id: id::DeviceId, - _desc: &command::CommandEncoderDescriptor, + _desc: &wgt::CommandEncoderDescriptor, id_in: F::Input, ) -> id::CommandEncoderId { let hub = B::hub(self); @@ -1848,7 +1848,7 @@ impl> Global { &self, device_id: id::DeviceId, surface_id: id::SurfaceId, - desc: &swap_chain::SwapChainDescriptor, + desc: &wgt::SwapChainDescriptor, ) -> id::SwapChainId { log::info!("creating swap chain {:?}", desc); let hub = B::hub(self); @@ -1872,7 +1872,7 @@ impl> Global { let num_frames = swap_chain::DESIRED_NUM_FRAMES .max(*caps.image_count.start()) .min(*caps.image_count.end()); - let mut config = desc.to_hal(num_frames, device.features); + let mut config = swap_chain::swap_chain_descriptor_to_hal(&desc, num_frames, device.features); if let Some(formats) = formats { assert!( formats.contains(&config.format), diff --git a/wgpu-core/src/lib.rs b/wgpu-core/src/lib.rs index 08aa7e7567..2cd1dcfdf1 100644 --- a/wgpu-core/src/lib.rs +++ b/wgpu-core/src/lib.rs @@ -31,7 +31,6 @@ pub mod swap_chain; pub mod track; pub use hal::pso::read_spirv; -use peek_poke::{PeekCopy, Poke}; use std::{ os::raw::c_char, @@ -43,7 +42,6 @@ type SubmissionIndex = usize; type Index = u32; type Epoch = u32; -pub type DynamicOffset = u32; pub type RawString = *const c_char; //TODO: make it private. Currently used for swapchain creation impl. @@ -110,54 +108,6 @@ struct Stored { ref_count: RefCount, } -#[repr(C)] -#[derive(Clone, Copy, Debug, PeekCopy, Poke)] -pub struct Color { - pub r: f64, - pub g: f64, - pub b: f64, - pub a: f64, -} - -impl Color { - pub const TRANSPARENT: Self = Color { - r: 0.0, - g: 0.0, - b: 0.0, - a: 0.0, - }; - pub const BLACK: Self = Color { - r: 0.0, - g: 0.0, - b: 0.0, - a: 1.0, - }; - pub const WHITE: Self = Color { - r: 1.0, - g: 1.0, - b: 1.0, - a: 1.0, - }; - pub const RED: Self = Color { - r: 1.0, - g: 0.0, - b: 0.0, - a: 1.0, - }; - pub const GREEN: Self = Color { - r: 0.0, - g: 1.0, - b: 0.0, - a: 1.0, - }; - pub const BLUE: Self = Color { - r: 0.0, - g: 0.0, - b: 1.0, - a: 1.0, - }; -} - #[repr(C)] #[derive(Clone, Copy, Debug)] pub struct Origin3d { diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 4db4b293b2..4a923e8bf9 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -11,7 +11,7 @@ use crate::{ Stored, }; -use wgt::{BufferAddress, BufferUsage, CompareFunction, TextureFormat}; +use wgt::{BufferAddress, BufferUsage, CompareFunction, TextureFormat, TextureUsage}; use hal; use rendy_memory::MemoryBlock; @@ -101,27 +101,6 @@ pub enum TextureDimension { D3, } -bitflags::bitflags! { - #[repr(transparent)] - pub struct TextureUsage: u32 { - const COPY_SRC = 1; - const COPY_DST = 2; - const SAMPLED = 4; - const STORAGE = 8; - const OUTPUT_ATTACHMENT = 16; - const NONE = 0; - /// The combination of all read-only usages. - const READ_ALL = Self::COPY_SRC.bits | Self::SAMPLED.bits; - /// The combination of all write-only and read-write usages. - const WRITE_ALL = Self::COPY_DST.bits | Self::STORAGE.bits | Self::OUTPUT_ATTACHMENT.bits; - /// The combination of all usages that the are guaranteed to be be ordered by the hardware. - /// If a usage is not ordered, then even if it doesn't change between draw calls, there - /// still need to be pipeline barriers inserted for synchronization. - const ORDERED = Self::READ_ALL.bits | Self::OUTPUT_ATTACHMENT.bits; - const UNINITIALIZED = 0xFFFF; - } -} - #[repr(C)] #[derive(Debug)] pub struct TextureDescriptor { diff --git a/wgpu-core/src/swap_chain.rs b/wgpu-core/src/swap_chain.rs index 67adbccb28..b60a33c492 100644 --- a/wgpu-core/src/swap_chain.rs +++ b/wgpu-core/src/swap_chain.rs @@ -37,13 +37,12 @@ use crate::{ hub::{GfxBackend, Global, IdentityFilter, Token}, id::{DeviceId, SwapChainId, TextureViewId}, resource, - Extent3d, Features, LifeGuard, Stored, }; -use wgt::TextureFormat; +use wgt::SwapChainDescriptor; use hal::{self, device::Device as _, queue::CommandQueue as _, window::PresentationSurface as _}; @@ -61,73 +60,26 @@ pub struct SwapChain { pub(crate) acquired_framebuffers: Vec, } -#[repr(C)] -#[derive(Copy, Clone, Debug)] -pub enum PresentMode { - /// The presentation engine does **not** wait for a vertical blanking period and - /// the request is presented immediately. This is a low-latency presentation mode, - /// but visible tearing may be observed. Will fallback to `Fifo` if unavailable on the - /// selected platform and backend. Not optimal for mobile. - Immediate = 0, - /// The presentation engine waits for the next vertical blanking period to update - /// the current image, but frames may be submitted without delay. This is a low-latency - /// presentation mode and visible tearing will **not** be observed. Will fallback to `Fifo` - /// if unavailable on the selected platform and backend. Not optimal for mobile. - Mailbox = 1, - /// The presentation engine waits for the next vertical blanking period to update - /// the current image. The framerate will be capped at the display refresh rate, - /// corresponding to the `VSync`. Tearing cannot be observed. Optimal for mobile. - Fifo = 2, -} - -#[repr(C)] -#[derive(Clone, Debug)] -pub struct SwapChainDescriptor { - pub usage: resource::TextureUsage, - pub format: TextureFormat, - pub width: u32, - pub height: u32, - pub present_mode: PresentMode, -} - -impl SwapChainDescriptor { - pub(crate) fn to_hal( - &self, - num_frames: u32, - features: Features, - ) -> hal::window::SwapchainConfig { - let mut config = hal::window::SwapchainConfig::new( - self.width, - self.height, - conv::map_texture_format(self.format, features), - num_frames, - ); - //TODO: check for supported - config.image_usage = conv::map_texture_usage(self.usage, hal::format::Aspects::COLOR); - config.composite_alpha_mode = hal::window::CompositeAlphaMode::OPAQUE; - config.present_mode = match self.present_mode { - PresentMode::Immediate => hal::window::PresentMode::IMMEDIATE, - PresentMode::Mailbox => hal::window::PresentMode::MAILBOX, - PresentMode::Fifo => hal::window::PresentMode::FIFO, - }; - config - } - - pub fn to_texture_desc(&self) -> resource::TextureDescriptor { - resource::TextureDescriptor { - size: Extent3d { - width: self.width, - height: self.height, - depth: 1, - }, - mip_level_count: 1, - array_layer_count: 1, - sample_count: 1, - dimension: resource::TextureDimension::D2, - format: self.format, - usage: self.usage, - } - } +pub(crate) fn swap_chain_descriptor_to_hal( + desc: &SwapChainDescriptor, + num_frames: u32, + features: Features, +) -> hal::window::SwapchainConfig { + let mut config = hal::window::SwapchainConfig::new( + desc.width, + desc.height, + conv::map_texture_format(desc.format, features), + num_frames, + ); + //TODO: check for supported + config.image_usage = conv::map_texture_usage(desc.usage, hal::format::Aspects::COLOR); + config.composite_alpha_mode = hal::window::CompositeAlphaMode::OPAQUE; + config.present_mode = match desc.present_mode { + wgt::PresentMode::Immediate => hal::window::PresentMode::IMMEDIATE, + wgt::PresentMode::Mailbox => hal::window::PresentMode::MAILBOX, + wgt::PresentMode::Fifo => hal::window::PresentMode::FIFO, + }; + config } #[repr(C)] @@ -166,7 +118,7 @@ impl> Global { } Err(e) => { log::warn!("acquire_image() failed ({:?}), reconfiguring swapchain", e); - let desc = sc.desc.to_hal(sc.num_frames, device.features); + let desc = swap_chain_descriptor_to_hal(&sc.desc, sc.num_frames, device.features); unsafe { suf.configure_swapchain(&device.raw, desc).unwrap(); suf.acquire_image(FRAME_TIMEOUT_MS * 1_000_000).unwrap() diff --git a/wgpu-core/src/track/texture.rs b/wgpu-core/src/track/texture.rs index 7452f4de71..cdebee55fd 100644 --- a/wgpu-core/src/track/texture.rs +++ b/wgpu-core/src/track/texture.rs @@ -3,8 +3,9 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use super::{range::RangedStates, PendingTransition, ResourceState, Unit}; -use crate::{device::MAX_MIP_LEVELS, id::TextureId, resource::TextureUsage}; +use crate::{device::MAX_MIP_LEVELS, id::TextureId}; +use wgt::TextureUsage; use arrayvec::ArrayVec; use std::{iter, ops::Range}; diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 6ec723854e..975ff314b7 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -301,7 +301,7 @@ pub extern "C" fn wgpu_device_create_shader_module( #[no_mangle] pub extern "C" fn wgpu_device_create_command_encoder( device_id: id::DeviceId, - desc: Option<&core::command::CommandEncoderDescriptor>, + desc: Option<&wgt::CommandEncoderDescriptor>, ) -> id::CommandEncoderId { let desc = &desc.cloned().unwrap_or_default(); gfx_select!(device_id => GLOBAL.device_create_command_encoder(device_id, desc, PhantomData)) @@ -357,7 +357,7 @@ pub extern "C" fn wgpu_device_create_compute_pipeline( pub extern "C" fn wgpu_device_create_swap_chain( device_id: id::DeviceId, surface_id: id::SurfaceId, - desc: &core::swap_chain::SwapChainDescriptor, + desc: &wgt::SwapChainDescriptor, ) -> id::SwapChainId { gfx_select!(device_id => GLOBAL.device_create_swap_chain(device_id, surface_id, desc)) } diff --git a/wgpu-remote/src/server.rs b/wgpu-remote/src/server.rs index c3aa6665f2..e47eade19b 100644 --- a/wgpu-remote/src/server.rs +++ b/wgpu-remote/src/server.rs @@ -142,7 +142,7 @@ pub extern "C" fn wgpu_server_buffer_destroy(global: &Global, self_id: id::Buffe pub extern "C" fn wgpu_server_device_create_encoder( global: &Global, self_id: id::DeviceId, - desc: &core::command::CommandEncoderDescriptor, + desc: &wgt::CommandEncoderDescriptor, new_id: id::CommandEncoderId, ) { gfx_select!(self_id => global.device_create_command_encoder(self_id, &desc, new_id)); diff --git a/wgpu-types/Cargo.toml b/wgpu-types/Cargo.toml index d1cf46d954..068fb1ade9 100644 --- a/wgpu-types/Cargo.toml +++ b/wgpu-types/Cargo.toml @@ -17,3 +17,4 @@ license = "MPL-2.0" [dependencies] bitflags = "1.0" serde = { version = "1.0", features = ["serde_derive"], optional = true } +peek-poke = { git = "https://github.com/kvark/peek-poke", rev = "969bd7fe2be1a83f87916dc8b388c63cfd457075", optional = true } diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index d451ca3554..a7c21e47dd 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -5,6 +5,8 @@ use std::{io, slice}; #[cfg(feature = "serde")] use serde::{Serialize, Deserialize}; +#[cfg(feature = "peek-poke")] +use peek_poke::{PeekCopy, Poke}; #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq)] @@ -540,3 +542,152 @@ pub struct BufferDescriptor { pub size: BufferAddress, pub usage: BufferUsage, } + +#[repr(C)] +#[derive(Clone, Debug, Default)] +pub struct CommandEncoderDescriptor { + // MSVC doesn't allow zero-sized structs + // We can remove this when we actually have a field + pub todo: u32, +} + +pub type DynamicOffset = u32; + +#[repr(C)] +#[derive(Copy, Clone, Debug)] +pub enum PresentMode { + /// The presentation engine does **not** wait for a vertical blanking period and + /// the request is presented immediately. This is a low-latency presentation mode, + /// but visible tearing may be observed. Will fallback to `Fifo` if unavailable on the + /// selected platform and backend. Not optimal for mobile. + Immediate = 0, + /// The presentation engine waits for the next vertical blanking period to update + /// the current image, but frames may be submitted without delay. This is a low-latency + /// presentation mode and visible tearing will **not** be observed. Will fallback to `Fifo` + /// if unavailable on the selected platform and backend. Not optimal for mobile. + Mailbox = 1, + /// The presentation engine waits for the next vertical blanking period to update + /// the current image. The framerate will be capped at the display refresh rate, + /// corresponding to the `VSync`. Tearing cannot be observed. Optimal for mobile. + Fifo = 2, +} + +bitflags::bitflags! { + #[repr(transparent)] + pub struct TextureUsage: u32 { + const COPY_SRC = 1; + const COPY_DST = 2; + const SAMPLED = 4; + const STORAGE = 8; + const OUTPUT_ATTACHMENT = 16; + const NONE = 0; + /// The combination of all read-only usages. + const READ_ALL = Self::COPY_SRC.bits | Self::SAMPLED.bits; + /// The combination of all write-only and read-write usages. + const WRITE_ALL = Self::COPY_DST.bits | Self::STORAGE.bits | Self::OUTPUT_ATTACHMENT.bits; + /// The combination of all usages that the are guaranteed to be be ordered by the hardware. + /// If a usage is not ordered, then even if it doesn't change between draw calls, there + /// still need to be pipeline barriers inserted for synchronization. + const ORDERED = Self::READ_ALL.bits | Self::OUTPUT_ATTACHMENT.bits; + const UNINITIALIZED = 0xFFFF; + } +} + +#[repr(C)] +#[derive(Clone, Debug)] +pub struct SwapChainDescriptor { + pub usage: TextureUsage, + pub format: TextureFormat, + pub width: u32, + pub height: u32, + pub present_mode: PresentMode, +} + +#[repr(C)] +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +#[cfg_attr(feature = "peek-poke", derive(PeekCopy, Poke))] +pub enum LoadOp { + Clear = 0, + Load = 1, +} + +#[repr(C)] +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +#[cfg_attr(feature = "peek-poke", derive(PeekCopy, Poke))] +pub enum StoreOp { + Clear = 0, + Store = 1, +} + +#[repr(C)] +#[derive(Debug)] +#[cfg_attr(feature = "peek-poke", derive(PeekCopy, Poke))] +pub struct RenderPassColorAttachmentDescriptorBase { + pub attachment: T, + pub resolve_target: R, + pub load_op: LoadOp, + pub store_op: StoreOp, + pub clear_color: Color, +} + +#[repr(C)] +#[derive(Clone, Debug)] +#[cfg_attr(feature = "peek-poke", derive(PeekCopy, Poke))] +pub struct RenderPassDepthStencilAttachmentDescriptorBase { + pub attachment: T, + pub depth_load_op: LoadOp, + pub depth_store_op: StoreOp, + pub clear_depth: f32, + pub stencil_load_op: LoadOp, + pub stencil_store_op: StoreOp, + pub clear_stencil: u32, +} + +#[repr(C)] +#[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "peek-poke", derive(PeekCopy, Poke))] +pub struct Color { + pub r: f64, + pub g: f64, + pub b: f64, + pub a: f64, +} + +impl Color { + pub const TRANSPARENT: Self = Color { + r: 0.0, + g: 0.0, + b: 0.0, + a: 0.0, + }; + pub const BLACK: Self = Color { + r: 0.0, + g: 0.0, + b: 0.0, + a: 1.0, + }; + pub const WHITE: Self = Color { + r: 1.0, + g: 1.0, + b: 1.0, + a: 1.0, + }; + pub const RED: Self = Color { + r: 1.0, + g: 0.0, + b: 0.0, + a: 1.0, + }; + pub const GREEN: Self = Color { + r: 0.0, + g: 1.0, + b: 0.0, + a: 1.0, + }; + pub const BLUE: Self = Color { + r: 0.0, + g: 0.0, + b: 1.0, + a: 1.0, + }; +}