Skip to content

Commit 88ceba4

Browse files
committed
Fix linux build & wayland
1 parent cd5a01e commit 88ceba4

File tree

10 files changed

+67
-36
lines changed

10 files changed

+67
-36
lines changed

examples/lines.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn config() -> GlassConfig {
2323
..Default::default()
2424
},
2525
features: Features::POLYGON_MODE_LINE,
26-
..DeviceConfig::performance()
26+
..DeviceConfig::default()
2727
},
2828
window_configs: vec![WindowConfig {
2929
width: WIDTH,

examples/multiple_windows/main.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use glass::{
33
};
44
use wgpu::{Color, CommandBuffer, StoreOp};
55
use winit::{
6-
event::{DeviceEvent, DeviceId, ElementState},
6+
event::{ElementState, WindowEvent},
77
event_loop::ActiveEventLoop,
88
keyboard::{KeyCode, PhysicalKey},
99
window::WindowId,
@@ -47,18 +47,26 @@ impl GlassApp for MultiWindowApp {
4747
);
4848
}
4949

50-
fn device_input(
50+
fn window_input(
5151
&mut self,
5252
context: &mut GlassContext,
5353
event_loop: &ActiveEventLoop,
54-
_device_id: DeviceId,
55-
event: &DeviceEvent,
54+
_window_id: WindowId,
55+
event: &WindowEvent,
5656
) {
57-
if let DeviceEvent::Key(input) = event {
58-
if input.physical_key == PhysicalKey::Code(KeyCode::Space)
59-
&& input.state == ElementState::Pressed
57+
// If you want to only match first window
58+
// if _window_id != self.window_ids[0] {
59+
// return;
60+
// }
61+
if let WindowEvent::KeyboardInput {
62+
event, ..
63+
} = event
64+
{
65+
println!("Key: {:?}", event);
66+
if event.physical_key == PhysicalKey::Code(KeyCode::Space)
67+
&& event.state == ElementState::Pressed
6068
{
61-
// Create window
69+
// Create window - this will work when your window has focus
6270
self.window_ids.push(
6371
context
6472
.create_window(event_loop, WindowConfig {
@@ -69,7 +77,6 @@ impl GlassApp for MultiWindowApp {
6977
})
7078
.unwrap(),
7179
);
72-
println!("Window ids: {:#?}", self.window_ids);
7380
}
7481
}
7582
}

examples/quad/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use glass::{
22
device_context::DeviceConfig,
33
pipelines::QuadPipeline,
44
texture::Texture,
5+
utils::default_texture_format,
56
window::{GlassWindow, WindowConfig},
67
Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
78
};
8-
use wgpu::{BindGroup, CommandBuffer, Limits, StoreOp, TextureFormat, TextureUsages};
9+
use wgpu::{BindGroup, CommandBuffer, Limits, StoreOp, TextureUsages};
910
use winit::event_loop::ActiveEventLoop;
1011

1112
const WIDTH: u32 = 1920;
@@ -146,7 +147,7 @@ fn create_tree_texture(app: &GlassContext) -> Texture {
146147
app.queue(),
147148
diffuse_bytes,
148149
"tree.png",
149-
TextureFormat::Rgba8UnormSrgb,
150+
default_texture_format(),
150151
TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
151152
)
152153
.unwrap()

examples/sand/grid.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use glam::{IVec2, Vec2};
2-
use glass::{pipelines::QuadPipeline, texture::Texture};
2+
use glass::{pipelines::QuadPipeline, texture::Texture, utils::default_texture_format};
33
use image::RgbaImage;
44
use wgpu::{
55
BindGroup, Device, Extent3d, Origin3d, Queue, Sampler, TexelCopyBufferLayout,
6-
TexelCopyTextureInfo, TextureAspect, TextureFormat, TextureUsages,
6+
TexelCopyTextureInfo, TextureAspect, TextureUsages,
77
};
88

99
use crate::sand::{Sand, SandType};
@@ -37,7 +37,7 @@ impl Grid {
3737
depth_or_array_layers: 1,
3838
},
3939
1,
40-
TextureFormat::Rgba8UnormSrgb,
40+
default_texture_format(),
4141
TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
4242
);
4343
let grid_bind_group = quad.create_bind_group(device, &texture.views[0], sampler);

examples/sand/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ fn config() -> GlassConfig {
236236
max_push_constant_size: 128,
237237
..Default::default()
238238
},
239-
..DeviceConfig::performance()
239+
..DeviceConfig::default()
240240
},
241241
window_configs: vec![WindowConfig {
242242
width: CANVAS_SIZE * CANVAS_SCALE,
243243
height: CANVAS_SIZE * CANVAS_SCALE,
244-
present_mode: PresentMode::Immediate,
244+
present_mode: PresentMode::AutoNoVsync,
245245
exit_on_esc: true,
246246
..WindowConfig::default()
247247
}],

examples/shader_with_includes/main.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ use std::{borrow::Cow, path::PathBuf};
22

33
use glass::{
44
utils::{ShaderModule, WatchedShaderModule},
5+
window::GlassWindow,
56
Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
67
};
78
use wgpu::{
89
CommandBuffer, MultisampleState, PipelineLayoutDescriptor, PrimitiveState, RenderPipeline,
9-
RenderPipelineDescriptor, ShaderModuleDescriptor, StoreOp, TextureFormat,
10+
RenderPipelineDescriptor, ShaderModuleDescriptor, StoreOp,
1011
};
1112
use winit::event_loop::ActiveEventLoop;
1213

13-
const WIDTH: u32 = 1920;
14-
const HEIGHT: u32 = 1080;
15-
1614
fn main() -> Result<(), GlassError> {
17-
Glass::run(GlassConfig::performance(WIDTH, HEIGHT), |_| {
18-
Box::new(TriangleApp::default())
19-
})
15+
Glass::run(GlassConfig::default(), |_| Box::new(TriangleApp::default()))
2016
}
2117

2218
#[derive(Default)]
@@ -135,7 +131,7 @@ fn create_triangle_pipeline(context: &GlassContext, shader_module: ShaderModule)
135131
module: &shader,
136132
entry_point: Some("fs_main"),
137133
compilation_options: Default::default(),
138-
targets: &[Some(TextureFormat::Bgra8UnormSrgb.into())],
134+
targets: &[Some(GlassWindow::default_surface_format().into())],
139135
}),
140136
primitive: PrimitiveState::default(),
141137
depth_stencil: None,

examples/triangle/main.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
use std::borrow::Cow;
22

3-
use glass::{Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData};
3+
use glass::{
4+
window::GlassWindow, Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
5+
};
46
use wgpu::{
57
CommandBuffer, MultisampleState, PipelineLayoutDescriptor, PrimitiveState, RenderPipeline,
6-
RenderPipelineDescriptor, ShaderModuleDescriptor, StoreOp, TextureFormat,
8+
RenderPipelineDescriptor, ShaderModuleDescriptor, StoreOp,
79
};
810
use winit::event_loop::ActiveEventLoop;
911

10-
const WIDTH: u32 = 1920;
11-
const HEIGHT: u32 = 1080;
12-
1312
fn main() -> Result<(), GlassError> {
14-
Glass::run(GlassConfig::performance(WIDTH, HEIGHT), |_| {
15-
Box::new(TriangleApp::default())
16-
})
13+
Glass::run(GlassConfig::default(), |_| Box::new(TriangleApp::default()))
1714
}
1815

1916
#[derive(Default)]
@@ -89,7 +86,7 @@ fn create_triangle_pipeline(context: &GlassContext) -> RenderPipeline {
8986
module: &shader,
9087
entry_point: Some("fs_main"),
9188
compilation_options: Default::default(),
92-
targets: &[Some(TextureFormat::Bgra8UnormSrgb.into())],
89+
targets: &[Some(GlassWindow::default_surface_format().into())],
9390
}),
9491
primitive: PrimitiveState::default(),
9592
depth_stencil: None,

src/glass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl ApplicationHandler for Glass {
8787
for window_config in config.window_configs.iter() {
8888
winit_windows.push((
8989
window_config.clone(),
90-
GlassContext::create_winit_window(event_loop, &window_config).unwrap(),
90+
GlassContext::create_winit_window(event_loop, window_config).unwrap(),
9191
))
9292
}
9393
for (window_config, window) in winit_windows {

src/utils.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,27 @@ use flume::{unbounded, Receiver, Sender};
1111
use log::{error, trace};
1212
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
1313
use path_clean::PathClean;
14-
use wgpu::naga::Module;
14+
use wgpu::{naga::Module, TextureFormat};
1515

1616
pub fn wait_async<F: Future>(fut: F) -> F::Output {
1717
pollster::block_on(fut)
1818
}
1919

20+
/// Return default [`TextureFormat`](wgpu::TextureFormat)s
21+
pub fn default_texture_format() -> TextureFormat {
22+
#[cfg(target_os = "linux")]
23+
{
24+
if std::env::var("WAYLAND_DISPLAY").is_ok()
25+
&& std::env::var("WAYLAND_DISPLAY").unwrap() != ""
26+
{
27+
return TextureFormat::Rgba8Unorm;
28+
}
29+
}
30+
31+
// Default to sRGB for all other cases (X11, Windows, macOS, etc.)
32+
TextureFormat::Rgba8UnormSrgb
33+
}
34+
2035
#[derive(Debug, PartialEq)]
2136
pub enum ShaderError {
2237
FileReadError(String),

src/window.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ impl GlassWindow {
203203

204204
/// Return default [`TextureFormat`](wgpu::TextureFormat)s
205205
pub fn default_surface_format() -> TextureFormat {
206+
#[cfg(target_os = "linux")]
207+
{
208+
let is_wayland = std::env::var("XDG_SESSION_TYPE")
209+
.map(|s| s == "wayland")
210+
.unwrap_or_else(|_| {
211+
std::env::var("WAYLAND_DISPLAY")
212+
.map(|s| !s.is_empty())
213+
.unwrap_or(false)
214+
});
215+
216+
if is_wayland {
217+
return TextureFormat::Bgra8Unorm;
218+
}
219+
}
220+
206221
TextureFormat::Bgra8UnormSrgb
207222
}
208223

0 commit comments

Comments
 (0)