1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use crate::{
app::App,
ecs::plugin::Plugin,
rendering::{
backend::{Backend, ColorTarget, FrameOperations, Pass},
errors::AcquireError,
sync::InitSender,
window::{GPUSurfaceHandle, WindowConfig},
},
wgpu::{
compute_pass::{CommandEncoder, ComputePass},
render_pass::RenderPass,
texture_view::TextureView,
window::WinitWindow,
},
};
/// The `wgpu`-backed [`Backend`] implementation. Inserted as a resource
/// once [`init`](Self::init) finishes (see the [`Backend`] trait docs for
/// how that's driven); everything in [`super`] that uploads to the GPU
/// (`Res<WGPUBackend>` in an [`Asset::upload`](crate::assets::upload::Asset::upload)
/// impl) reads `device`/`queue` directly off this.
///
/// `device`/`queue` are `pub` because some operations genuinely need them
/// (submitting command encoders, `queue.write_buffer`, resource types
/// [`wgpu::prelude`](super::prelude) doesn't cover) — but for building a
/// buffer, bind group layout, or bind group, reach for
/// [`wgpu::prelude`](super::prelude) first rather than hand-writing a
/// `wgpu::BufferDescriptor`/`BindGroupLayoutDescriptor`/`BindGroupDescriptor`
/// against `device` directly.
pub struct WGPUBackend {
pub device: wgpu::Device,
pub queue: wgpu::Queue,
pub surface: wgpu::Surface<'static>,
pub config: wgpu::SurfaceConfiguration,
}
impl WGPUBackend {
async fn init_async(
handle: impl GPUSurfaceHandle,
width: u32,
height: u32,
sender: InitSender<Self>,
) {
let backends = if cfg!(target_arch = "wasm32") {
wgpu::Backends::BROWSER_WEBGPU
} else {
wgpu::Backends::PRIMARY
};
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
display: None,
backends,
flags: wgpu::InstanceFlags::default(),
memory_budget_thresholds: wgpu::MemoryBudgetThresholds::default(),
backend_options: wgpu::BackendOptions::default(),
});
let surface = instance.create_surface(handle).unwrap();
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
force_fallback_adapter: false,
compatible_surface: Some(&surface),
})
.await
.unwrap();
let (required_features, required_limits) = if cfg!(target_arch = "wasm32") {
(wgpu::Features::empty(), wgpu::Limits::defaults())
} else {
(
wgpu::Features::ADDRESS_MODE_CLAMP_TO_BORDER,
wgpu::Limits::default(),
)
};
let (device, queue) = adapter
.request_device(&wgpu::DeviceDescriptor {
label: None,
required_features,
required_limits,
..Default::default()
})
.await
.unwrap();
let caps = surface.get_capabilities(&adapter);
let format = caps
.formats
.iter()
.copied()
.find(|f| f.is_srgb())
.unwrap_or(caps.formats[0]);
// Prefer Fifo (vsync) explicitly rather than trusting caps.present_modes[0] —
// its ordering isn't guaranteed to put Fifo first, and an uncapped mode
// (Immediate/Mailbox) here would tear and burn GPU cycles for no benefit.
let present_mode = caps
.present_modes
.iter()
.copied()
.find(|m| *m == wgpu::PresentMode::Fifo)
.unwrap_or(caps.present_modes[0]);
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format,
present_mode,
alpha_mode: caps.alpha_modes[0],
width,
height,
desired_maximum_frame_latency: 2,
view_formats: vec![],
};
surface.configure(&device, &config);
sender.send(WGPUBackend {
device,
queue,
surface,
config,
});
}
}
pub struct WGPUFrame {
encoder: wgpu::CommandEncoder,
view: wgpu::TextureView,
surface_texture: wgpu::SurfaceTexture,
}
impl FrameOperations for WGPUFrame {
type Context<'a> = RenderPass<'a>;
type Attachment = TextureView;
type DepthAttachment = TextureView;
fn begin(&mut self, pass: Pass<'_, Self>) -> Self::Context<'_> {
let color_attachments: Vec<_> = pass
.colors
.iter()
.map(|target| {
let (view, clear) = match target {
ColorTarget::Default { clear } => (&self.view, clear),
ColorTarget::Custom { attachment, clear } => (attachment.raw(), clear),
};
Some(wgpu::RenderPassColorAttachment {
view,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: clear
.map(|[r, g, b, a]| {
wgpu::LoadOp::Clear(wgpu::Color {
r: r as f64,
g: g as f64,
b: b as f64,
a: a as f64,
})
})
.unwrap_or(wgpu::LoadOp::Load),
store: wgpu::StoreOp::Store,
},
})
})
.collect();
let depth_stencil_attachment =
pass.depth
.as_ref()
.map(|d| wgpu::RenderPassDepthStencilAttachment {
view: d.attachment.raw(),
depth_ops: Some(wgpu::Operations {
load: d
.clear
.map(wgpu::LoadOp::Clear)
.unwrap_or(wgpu::LoadOp::Load),
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
});
let raw = self.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &color_attachments,
depth_stencil_attachment,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
RenderPass::new(raw)
}
}
impl WGPUFrame {
/// Begin a compute pass on this frame's command encoder.
pub fn compute_pass(&mut self, label: Option<&str>) -> ComputePass<'_> {
let raw = self.encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label,
timestamp_writes: None,
});
ComputePass::new(raw)
}
}
impl WGPUBackend {
/// Starts a command encoder for standalone GPU work not tied to an
/// acquired frame — a compute dispatch outside `SystemStage::Render`,
/// say. Begin a [`ComputePass`] on it via
/// [`CommandEncoder::compute_pass`], then hand it to [`submit`](Self::submit)
/// when done. Render passes don't need this —
/// [`ActiveFrame::begin_pass`](crate::rendering::active_frame::ActiveFrame::begin_pass)
/// manages its own frame-tied encoder internally.
pub fn create_command_encoder(&self, label: Option<&str>) -> CommandEncoder {
CommandEncoder::new(self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label }))
}
/// Finishes and submits `encoder`'s recorded commands to the queue.
pub fn submit(&self, encoder: CommandEncoder) {
self.queue.submit(std::iter::once(encoder.into_raw().finish()));
}
}
impl Backend for WGPUBackend {
type Frame = WGPUFrame;
/// Native blocks the calling thread on [`init_async`](Self::init_async)
/// via `pollster::block_on` (fine here — this only runs once, during
/// [`App::build`](crate::app::App::build), and there's no other work
/// competing for the thread yet). Web can't block its single thread, so
/// it hands `init_async` to `wasm_bindgen_futures::spawn_local` instead
/// and returns immediately — [`GraphicsPlugin`](crate::rendering::graphics_plugin::GraphicsPlugin)
/// polls the resulting `sender`/[`InitReceiver`](crate::rendering::sync::InitReceiver)
/// pair every tick either way, so callers don't need to know which path
/// ran. A second [`Backend`] implementation should follow the same
/// split if it also needs to run on both targets.
fn init(handle: impl GPUSurfaceHandle, width: u32, height: u32, sender: InitSender<Self>) {
#[cfg(not(target_arch = "wasm32"))]
{
pollster::block_on(Self::init_async(handle, width, height, sender));
}
#[cfg(target_arch = "wasm32")]
{
wasm_bindgen_futures::spawn_local(Self::init_async(handle, width, height, sender));
}
}
fn resize(&mut self, width: u32, height: u32) {
if width == 0 || height == 0 {
return; // minimized — don't reconfigure to a degenerate size
}
self.config.width = width;
self.config.height = height;
self.surface.configure(&self.device, &self.config);
}
fn acquire(&mut self) -> Result<Self::Frame, AcquireError> {
let surface_texture = match self.surface.get_current_texture() {
wgpu::CurrentSurfaceTexture::Success(texture) => texture,
wgpu::CurrentSurfaceTexture::Suboptimal(texture) => texture,
wgpu::CurrentSurfaceTexture::Timeout | wgpu::CurrentSurfaceTexture::Outdated => {
return Err(AcquireError::Transient);
}
other => {
return Err(AcquireError::Fatal(format!(
"unexpected surface state: {other:?}"
)));
}
};
let view = surface_texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
Ok(WGPUFrame {
encoder,
view,
surface_texture,
})
}
fn present(&mut self, frame: Self::Frame) {
self.queue.submit(std::iter::once(frame.encoder.finish()));
frame.surface_texture.present();
}
}
pub struct WGPUPlugin {
config: WindowConfig,
}
impl WGPUPlugin {
pub fn new(config: WindowConfig) -> Self {
Self { config }
}
}
impl Plugin for WGPUPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(crate::prelude::WindowPlugin::<WinitWindow>::new(
WindowConfig {
title: self.config.title.clone(),
width: self.config.width,
height: self.config.height,
},
))
.add_plugin(crate::prelude::GraphicsPlugin::<WGPUBackend, WinitWindow>::new())
.add_plugin(crate::prelude::RenderPlugin::<WGPUBackend>::new())
.add_plugin(crate::wgpu::textures::TexturePlugin)
.add_plugin(crate::wgpu::texture_array::TextureArrayPlugin)
.add_plugin(crate::wgpu::cubemap::CubemapPlugin)
.add_plugin(crate::wgpu::mesh::MeshPlugin::new())
.add_plugin(crate::wgpu::material::MaterialPlugin::new())
.add_plugin(crate::wgpu::instance::MaterialInstancePlugin::new())
.add_plugin(crate::wgpu::compute::ComputePlugin::new())
.add_plugin(crate::wgpu::instance::ComputeInstancePlugin::new())
.add_plugin(crate::prelude::LazyResourcePlugin::<
WGPUBackend,
crate::wgpu::samplers::GlobalSamplers,
>::new());
}
}