Skip to content

Commit b8f27c7

Browse files
Wumpfemilk
andauthored
Expose maximum_frame_latency (#4899)
Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
1 parent 2e38187 commit b8f27c7

File tree

19 files changed

+83
-70
lines changed

19 files changed

+83
-70
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ By @wumpf in [#5044](https://github.com/gfx-rs/wgpu/pull/5044)
9898
- Added support for the float32-filterable feature. By @almarklein in [#4759](https://github.com/gfx-rs/wgpu/pull/4759)
9999
- GPU buffer memory is released during "lose the device". By @bradwerth in [#4851](https://github.com/gfx-rs/wgpu/pull/4851)
100100
- wgpu and wgpu-core features are now documented on docs.rs. By @wumpf in [#4886](https://github.com/gfx-rs/wgpu/pull/4886)
101+
- `SurfaceConfiguration` now exposes `desired_maximum_frame_latency` which was previously hard-coded to 2. By setting it to 1 you can reduce latency under the risk of making GPU & CPU work sequential. Currently, on DX12 this affects the `MaximumFrameLatency`, on all other backends except OpenGL the size of the swapchain (on OpenGL this has no effect). By @emilk & @wumpf in [#4899](https://github.com/gfx-rs/wgpu/pull/4899)
101102
- DeviceLostClosure is guaranteed to be invoked exactly once. By @bradwerth in [#4862](https://github.com/gfx-rs/wgpu/pull/4862)
102103

103104
#### OpenGL

examples/src/framework.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ impl<E: Example + wgpu::WasmNotSendSync> From<ExampleTestParams<E>>
571571
format,
572572
width: params.width,
573573
height: params.height,
574+
desired_maximum_frame_latency: 2,
574575
present_mode: wgpu::PresentMode::Fifo,
575576
alpha_mode: wgpu::CompositeAlphaMode::Auto,
576577
view_formats: vec![format],

examples/src/hello_triangle/mod.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,9 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
7272
multiview: None,
7373
});
7474

75-
let mut config = wgpu::SurfaceConfiguration {
76-
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
77-
format: swapchain_format,
78-
width: size.width,
79-
height: size.height,
80-
present_mode: wgpu::PresentMode::Fifo,
81-
alpha_mode: swapchain_capabilities.alpha_modes[0],
82-
view_formats: vec![],
83-
};
84-
75+
let mut config = surface
76+
.get_default_config(&adapter, size.width, size.height)
77+
.unwrap();
8578
surface.configure(&device, &config);
8679

8780
let window = &window;

examples/src/hello_windows/mod.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,11 @@ impl ViewportDesc {
3030

3131
fn build(self, adapter: &wgpu::Adapter, device: &wgpu::Device) -> Viewport {
3232
let size = self.window.inner_size();
33-
34-
let caps = self.surface.get_capabilities(adapter);
35-
let config = wgpu::SurfaceConfiguration {
36-
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
37-
format: caps.formats[0],
38-
width: size.width,
39-
height: size.height,
40-
present_mode: wgpu::PresentMode::Fifo,
41-
alpha_mode: caps.alpha_modes[0],
42-
view_formats: vec![],
43-
};
44-
33+
let config = self
34+
.surface
35+
.get_default_config(adapter, size.width, size.height)
36+
.unwrap();
4537
self.surface.configure(device, &config);
46-
4738
Viewport { desc: self, config }
4839
}
4940
}

examples/src/uniform_values/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,9 @@ impl WgpuContext {
192192
multiview: None,
193193
});
194194

195-
let surface_config = wgpu::SurfaceConfiguration {
196-
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
197-
format: swapchain_format,
198-
width: size.width,
199-
height: size.height,
200-
present_mode: wgpu::PresentMode::Fifo,
201-
alpha_mode: swapchain_capabilities.alpha_modes[0],
202-
view_formats: vec![],
203-
};
195+
let surface_config = surface
196+
.get_default_config(&adapter, size.width, size.height)
197+
.unwrap();
204198
surface.configure(&device, &surface_config);
205199

206200
// (5)

wgpu-core/src/device/global.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,10 +1982,12 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
19821982
}
19831983
}
19841984

1985-
let num_frames = present::DESIRED_NUM_FRAMES
1986-
.clamp(*caps.swap_chain_sizes.start(), *caps.swap_chain_sizes.end());
1985+
let maximum_frame_latency = config.desired_maximum_frame_latency.clamp(
1986+
*caps.maximum_frame_latency.start(),
1987+
*caps.maximum_frame_latency.end(),
1988+
);
19871989
let mut hal_config = hal::SurfaceConfiguration {
1988-
swap_chain_size: num_frames,
1990+
maximum_frame_latency,
19891991
present_mode: config.present_mode,
19901992
composite_alpha_mode: config.alpha_mode,
19911993
format: config.format,
@@ -2056,7 +2058,6 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
20562058
*presentation = Some(present::Presentation {
20572059
device: super::any_device::AnyDevice::new(device.clone()),
20582060
config: config.clone(),
2059-
num_frames,
20602061
acquired_texture: None,
20612062
});
20622063
}

wgpu-core/src/present.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,11 @@ use thiserror::Error;
3737
use wgt::SurfaceStatus as Status;
3838

3939
const FRAME_TIMEOUT_MS: u32 = 1000;
40-
pub const DESIRED_NUM_FRAMES: u32 = 3;
4140

4241
#[derive(Debug)]
4342
pub(crate) struct Presentation {
4443
pub(crate) device: AnyDevice,
4544
pub(crate) config: wgt::SurfaceConfiguration<Vec<wgt::TextureFormat>>,
46-
#[allow(unused)]
47-
pub(crate) num_frames: u32,
4845
pub(crate) acquired_texture: Option<TextureId>,
4946
}
5047

wgpu-hal/examples/halmark/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const BUNNY_SIZE: f32 = 0.15 * 256.0;
2323
const GRAVITY: f32 = -9.8 * 100.0;
2424
const MAX_VELOCITY: f32 = 750.0;
2525
const COMMAND_BUFFER_PER_CONTEXT: usize = 100;
26-
const DESIRED_FRAMES: u32 = 3;
26+
const DESIRED_MAX_LATENCY: u32 = 2;
2727

2828
#[repr(C)]
2929
#[derive(Clone, Copy)]
@@ -132,9 +132,9 @@ impl<A: hal::Api> Example<A> {
132132

133133
let window_size: (u32, u32) = window.inner_size().into();
134134
let surface_config = hal::SurfaceConfiguration {
135-
swap_chain_size: DESIRED_FRAMES.clamp(
136-
*surface_caps.swap_chain_sizes.start(),
137-
*surface_caps.swap_chain_sizes.end(),
135+
maximum_frame_latency: DESIRED_MAX_LATENCY.clamp(
136+
*surface_caps.maximum_frame_latency.start(),
137+
*surface_caps.maximum_frame_latency.end(),
138138
),
139139
present_mode: wgt::PresentMode::Fifo,
140140
composite_alpha_mode: wgt::CompositeAlphaMode::Opaque,

wgpu-hal/examples/ray-traced-triangle/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::{
1414
use winit::window::WindowButtons;
1515

1616
const COMMAND_BUFFER_PER_CONTEXT: usize = 100;
17-
const DESIRED_FRAMES: u32 = 3;
17+
const DESIRED_MAX_LATENCY: u32 = 2;
1818

1919
/// [D3D12_RAYTRACING_INSTANCE_DESC](https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#d3d12_raytracing_instance_desc)
2020
/// [VkAccelerationStructureInstanceKHR](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkAccelerationStructureInstanceKHR.html)
@@ -264,9 +264,9 @@ impl<A: hal::Api> Example<A> {
264264
*surface_caps.formats.first().unwrap()
265265
};
266266
let surface_config = hal::SurfaceConfiguration {
267-
swap_chain_size: DESIRED_FRAMES
268-
.max(*surface_caps.swap_chain_sizes.start())
269-
.min(*surface_caps.swap_chain_sizes.end()),
267+
maximum_frame_latency: DESIRED_MAX_LATENCY
268+
.max(*surface_caps.maximum_frame_latency.start())
269+
.min(*surface_caps.maximum_frame_latency.end()),
270270
present_mode: wgt::PresentMode::Fifo,
271271
composite_alpha_mode: wgt::CompositeAlphaMode::Opaque,
272272
format: surface_format,

wgpu-hal/src/dx12/adapter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,8 @@ impl crate::Adapter<super::Api> for super::Adapter {
626626
wgt::TextureFormat::Rgb10a2Unorm,
627627
wgt::TextureFormat::Rgba16Float,
628628
],
629-
// we currently use a flip effect which supports 2..=16 buffers
630-
swap_chain_sizes: 2..=16,
629+
// See https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgidevice1-setmaximumframelatency
630+
maximum_frame_latency: 1..=16,
631631
current_extent,
632632
usage: crate::TextureUses::COLOR_TARGET
633633
| crate::TextureUses::COPY_SRC

0 commit comments

Comments
 (0)