Skip to main content

agpu/
types.rs

1//! Core GPU types — agpu's own type system replacing direct wgpu dependency.
2//!
3//! Users depend on `agpu` instead of `wgpu`. Every type is defined or
4//! re-exported here so that downstream crates never need `wgpu` in their
5//! `Cargo.toml`.
6
7// ── Re-exports from wgpu ────────────────────────────────────────────
8//
9// Configuration types that are pure data — no resource handles.
10// Users see these as `agpu::TextureFormat`, `agpu::BufferUsages`, etc.
11
12// Texture
13pub use wgpu::Extent3d;
14pub use wgpu::Origin3d;
15pub use wgpu::StorageTextureAccess;
16pub use wgpu::TextureAspect;
17pub use wgpu::TextureDimension;
18pub use wgpu::TextureFormat;
19pub use wgpu::TextureSampleType;
20pub use wgpu::TextureUsages;
21pub use wgpu::TextureViewDimension;
22
23// Buffer
24pub use wgpu::BufferAddress;
25pub use wgpu::BufferSize;
26pub use wgpu::BufferUsages;
27
28// Pipeline
29pub use wgpu::DepthBiasState;
30pub use wgpu::DepthStencilState;
31pub use wgpu::Face;
32pub use wgpu::FrontFace;
33pub use wgpu::IndexFormat;
34pub use wgpu::MultisampleState;
35pub use wgpu::PipelineCompilationOptions;
36pub use wgpu::PolygonMode;
37pub use wgpu::PrimitiveState;
38pub use wgpu::PrimitiveTopology;
39pub use wgpu::StencilFaceState;
40pub use wgpu::StencilOperation;
41pub use wgpu::StencilState;
42
43// Blend / Color
44pub use wgpu::BlendComponent;
45pub use wgpu::BlendFactor;
46pub use wgpu::BlendOperation;
47pub use wgpu::BlendState;
48pub use wgpu::Color;
49pub use wgpu::ColorTargetState;
50pub use wgpu::ColorWrites;
51
52// Shader
53pub use wgpu::ShaderSource;
54pub use wgpu::ShaderStages;
55
56// Bind group
57pub use wgpu::BindingType;
58pub use wgpu::BufferBindingType;
59pub use wgpu::SamplerBindingType;
60
61// Vertex
62pub use wgpu::VertexAttribute;
63pub use wgpu::VertexBufferLayout;
64pub use wgpu::VertexFormat;
65pub use wgpu::VertexStepMode;
66
67// Render pass
68pub use wgpu::LoadOp;
69pub use wgpu::Operations;
70pub use wgpu::StoreOp;
71
72// Sampler
73pub use wgpu::AddressMode;
74pub use wgpu::CompareFunction;
75pub use wgpu::FilterMode;
76
77// Surface
78pub use wgpu::PresentMode;
79pub use wgpu::SurfaceConfiguration;
80
81// Device
82pub use wgpu::Features;
83pub use wgpu::Limits;
84pub use wgpu::Maintain;
85pub use wgpu::MemoryHints;
86pub use wgpu::PowerPreference;
87
88// Backend selection
89pub use wgpu::Backends;
90
91// Misc
92pub use wgpu::CommandEncoderDescriptor;
93pub use wgpu::MapMode;
94pub use wgpu::RenderPassColorAttachment;
95pub use wgpu::RenderPassDepthStencilAttachment;
96pub use wgpu::RenderPassDescriptor;
97pub use wgpu::RenderPassTimestampWrites;
98pub use wgpu::SurfaceError;
99pub use wgpu::TextureViewDescriptor;
100
101// Raw wgpu types needed by downstream backends
102pub use wgpu::Device;
103pub use wgpu::Instance;
104pub use wgpu::InstanceDescriptor;
105pub use wgpu::Queue;
106pub use wgpu::Surface;
107pub use wgpu::TextureDescriptor;
108pub use wgpu::TextureView;
109
110// ── agpu-specific types ─────────────────────────────────────────────
111
112/// Backend preference for GPU initialisation.
113///
114/// agpu is Vulkan-first: when `VulkanPreferred` is used (the default),
115/// the engine tries the Vulkan backend before falling back to the
116/// platform default.  `OpenGLPreferred` selects GL/GLES first.
117#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
118pub enum BackendPreference {
119    /// Try Vulkan first, then fall back to the platform default.
120    VulkanPreferred,
121    /// Try OpenGL/GLES first, then fall back to the platform default.
122    OpenGLPreferred,
123    /// Use the platform default (DX12 on Windows, Metal on macOS, Vulkan on Linux).
124    #[default]
125    PlatformDefault,
126
127    /// Force a specific set of backends.
128    Specific(Backends),
129}
130
131impl BackendPreference {
132    /// Convert to the concrete `wgpu::Backends` bitflags.
133    ///
134    /// `PlatformDefault` selects the native API for the current OS:
135    /// DX12 on Windows, Metal on macOS, Vulkan elsewhere.
136    pub fn to_backends(self) -> Backends {
137        match self {
138            Self::VulkanPreferred => Backends::VULKAN,
139            Self::OpenGLPreferred => Backends::GL,
140            Self::PlatformDefault => {
141                #[cfg(target_os = "windows")]
142                {
143                    Backends::DX12
144                }
145                #[cfg(target_os = "macos")]
146                {
147                    Backends::METAL
148                }
149                #[cfg(not(any(target_os = "windows", target_os = "macos")))]
150                {
151                    Backends::VULKAN
152                }
153            }
154            Self::Specific(b) => b,
155        }
156    }
157}
158
159/// Describes how to create a GPU buffer.
160#[derive(Debug, Clone)]
161pub struct GpuBufferDescriptor {
162    pub label: Option<String>,
163    pub size: u64,
164    pub usage: BufferUsages,
165    pub mapped_at_creation: bool,
166}
167
168impl Default for GpuBufferDescriptor {
169    fn default() -> Self {
170        Self {
171            label: None,
172            size: 0,
173            usage: BufferUsages::empty(),
174            mapped_at_creation: false,
175        }
176    }
177}
178
179/// Describes how to create a GPU texture.
180#[derive(Debug, Clone)]
181pub struct GpuTextureDescriptor {
182    pub label: Option<String>,
183    pub size: Extent3d,
184    pub mip_level_count: u32,
185    pub sample_count: u32,
186    pub dimension: TextureDimension,
187    pub format: TextureFormat,
188    pub usage: TextureUsages,
189    pub view_formats: Vec<TextureFormat>,
190}
191
192impl Default for GpuTextureDescriptor {
193    fn default() -> Self {
194        Self {
195            label: None,
196            size: Extent3d {
197                width: 1,
198                height: 1,
199                depth_or_array_layers: 1,
200            },
201            mip_level_count: 1,
202            sample_count: 1,
203            dimension: TextureDimension::D2,
204            format: TextureFormat::Rgba8UnormSrgb,
205            usage: TextureUsages::empty(),
206            view_formats: Vec::new(),
207        }
208    }
209}
210
211/// Describes how to create a GPU sampler.
212#[derive(Debug, Clone)]
213pub struct GpuSamplerDescriptor {
214    pub label: Option<String>,
215    pub address_mode_u: AddressMode,
216    pub address_mode_v: AddressMode,
217    pub address_mode_w: AddressMode,
218    pub mag_filter: FilterMode,
219    pub min_filter: FilterMode,
220    pub mipmap_filter: FilterMode,
221    pub compare: Option<CompareFunction>,
222    pub anisotropy_clamp: u16,
223    pub lod_min_clamp: f32,
224    pub lod_max_clamp: f32,
225}
226
227impl Default for GpuSamplerDescriptor {
228    fn default() -> Self {
229        Self {
230            label: None,
231            address_mode_u: AddressMode::ClampToEdge,
232            address_mode_v: AddressMode::ClampToEdge,
233            address_mode_w: AddressMode::ClampToEdge,
234            mag_filter: FilterMode::Linear,
235            min_filter: FilterMode::Linear,
236            mipmap_filter: FilterMode::Nearest,
237            compare: None,
238            anisotropy_clamp: 1,
239            lod_min_clamp: 0.0,
240            lod_max_clamp: 32.0,
241        }
242    }
243}
244
245/// GPU resource kind — used by the resource tracker and ontology.
246#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
247pub enum GpuResourceKind {
248    Buffer,
249    Texture,
250    TextureView,
251    Sampler,
252    ShaderModule,
253    BindGroupLayout,
254    BindGroup,
255    PipelineLayout,
256    RenderPipeline,
257    ComputePipeline,
258    QuerySet,
259    CommandBuffer,
260}
261
262impl std::fmt::Display for GpuResourceKind {
263    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
264        write!(f, "{self:?}")
265    }
266}