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
//! Window configuration.
use crate::{
app::{LogicalSize, Size, WindowAttributes},
gfx::{self, PresentMode},
};
/// Configuration for a window.
///
/// Combines application-level window attributes with graphics configuration.
#[derive(Clone, Default, Debug)]
pub struct WindowConfig {
/// Window attributes from wgame_app.
pub app: WindowAttributes,
/// Graphics configuration.
pub gfx: gfx::Config,
}
impl WindowConfig {
/// Sets the window title.
pub fn title(self, title: &str) -> Self {
Self {
app: self.app.with_title(title),
..self
}
}
/// Sets the inner window size in physical pixels.
pub fn size(self, size: (u32, u32)) -> Self {
Self {
app: self.app.with_inner_size(Size::new(size.0, size.1)),
..self
}
}
/// Sets the inner window size in logical pixels, scaled by the OS.
///
/// ```
/// let config = wgame::WindowConfig::default().logical_size((800.0, 600.0));
/// ```
///
/// Also accepted by `#[wgame::window(logical_size = (800.0, 600.0))]`.
pub fn logical_size(self, size: (f64, f64)) -> Self {
Self {
app: self.app.with_inner_size(LogicalSize::new(size.0, size.1)),
..self
}
}
/// Sets whether the window is resizable.
pub fn resizable(self, resizable: bool) -> Self {
Self {
app: self.app.with_resizable(resizable),
..self
}
}
/// Sets the optional GPU features required by the application.
///
/// Unsupported features cause window graphics initialization to fail. This
/// does not change the selected native or browser backend.
pub fn required_features(mut self, features: wgpu::Features) -> Self {
self.gfx.required_features = features;
self
}
/// Sets device limits, including compute and storage-buffer capabilities.
///
/// Defaults are WebGL2-compatible and exclude compute. Use a compute-capable
/// backend with the ordinary WebGPU limits for a compute application:
///
/// ```
/// let config = wgame::WindowConfig::default()
/// .required_limits(wgpu::Limits::default())
/// .vsync(false);
/// ```
///
/// See [`gfx::Config::required_limits`] for texture-resolution handling.
pub fn required_limits(mut self, limits: wgpu::Limits) -> Self {
self.gfx.required_limits = limits;
self
}
/// Request the adapter's buffer limits instead of the configured sizes.
///
/// This opt-in permits buffers larger than WebGPU's baseline limits without
/// allocating memory. It leaves compute and other requirements unchanged:
///
/// ```
/// let config = wgame::WindowConfig::default()
/// .required_limits(wgpu::Limits::default())
/// .use_adapter_buffer_limits(true);
/// ```
///
/// See [`gfx::Config::use_adapter_buffer_limits`] for allocation constraints.
pub fn use_adapter_buffer_limits(mut self, enabled: bool) -> Self {
self.gfx.use_adapter_buffer_limits = enabled;
self
}
/// Sets whether vsync is enabled, preserving GPU device requirements.
pub fn vsync(self, vsync: bool) -> Self {
Self {
gfx: gfx::Config {
present_mode: if vsync {
PresentMode::AutoVsync
} else {
PresentMode::AutoNoVsync
},
..self.gfx
},
..self
}
}
}
#[cfg(test)]
mod tests {
use super::WindowConfig;
#[test]
fn graphics_requirements_survive_window_builders() {
let limits = wgpu::Limits {
max_compute_workgroups_per_dimension: 128,
..wgpu::Limits::default()
};
let features = wgpu::Features::TIMESTAMP_QUERY;
assert!(!WindowConfig::default().gfx.use_adapter_buffer_limits);
let config = WindowConfig::default()
.required_limits(limits.clone())
.required_features(features)
.use_adapter_buffer_limits(true)
.vsync(false)
.title("compute")
.size((640, 480))
.resizable(false);
assert_eq!(config.gfx.required_limits, limits);
assert_eq!(config.gfx.required_features, features);
assert!(config.gfx.use_adapter_buffer_limits);
assert_eq!(config.gfx.present_mode, wgpu::PresentMode::AutoNoVsync);
let config = config.vsync(true);
assert_eq!(config.gfx.required_limits, limits);
assert_eq!(config.gfx.required_features, features);
assert!(config.gfx.use_adapter_buffer_limits);
assert_eq!(config.gfx.present_mode, wgpu::PresentMode::AutoVsync);
assert!(
!config
.use_adapter_buffer_limits(false)
.gfx
.use_adapter_buffer_limits
);
}
}