Skip to content

Allow disabling waiting for latency waitable object #7400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from

Conversation

marcpabst
Copy link
Contributor

@marcpabst marcpabst commented Mar 23, 2025

This PR allows you to skip the frame latency waitable mechanism used for the DX12 backend by adding a new option to Dx12BackendOptions. The options now also carry over to the DX12 Surface, and a waitable object is only obtained when use_latency_waitable_object == true. The swap chain still gets created with the DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT flag, because I don't think there are any downsides and it lets users still obtain a waitable object themselves.

Not sure if this is the best way to do this or the right place for this setting, so let me know what you think. Happy to also change the docs.

@marcpabst marcpabst requested review from crowlKats and a team as code owners March 23, 2025 14:59
@cwfitzgerald cwfitzgerald self-assigned this Mar 23, 2025
Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I missed something, seems to be missing the part where None doesn't make a swapchain with waitable object support.

Code looks pretty darn good otherwise :)

Comment on lines 555 to 558
pub unsafe fn wait_for_frame_latency_object(
&self,
timeout: Option<std::time::Duration>,
) -> Result<bool, crate::SurfaceError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels weird being pub

Copy link
Contributor Author

@marcpabst marcpabst Apr 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea was that you don't need to expose the actual waitable handle this way. So wgpu can still handle the configuration of the swap chain and creation of the waitable object but we can abstract away the actual unsafe syscalls.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for this particular use case (user demands a certain usage of the waitable object) - it's reasonable to expose the raw handle so they can do whatever they want to do with it (maybe wait for multiple of them at once or something similarly funky.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's fine! I'll expose the waitable object and make the wait method private.

@marcpabst
Copy link
Contributor Author

Unless I missed something, seems to be missing the part where None doesn't make a swapchain with waitable object support.

Code looks pretty darn good otherwise :)

Yes, I thought that's the best way to deal with this. There's no documented downside to setting this flag, but with my (very limited) DX12 experience, that's not to say there are no side effects at all. So it might be better to at least give users an option to disable it.

@cwfitzgerald
Copy link
Member

Oh yes, very true, I wouldn't expect there to be any side effects for d3d12.

pub unsafe fn waitable_handle(&self) -> Option<Foundation::HANDLE> {
let swapchain = self.swap_chain.read();
if let Some(swap_chain) = swapchain.as_ref() {
return swap_chain.waitable.clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implies that Handles are refcounted, but they are Copy aliases around *const c_void - we should return it by copy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so just drop the clone()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah

}

unsafe impl Send for Surface {}
unsafe impl Sync for Surface {}

impl Surface {
/// Returns the waitable handle of the swapchain, if it exists.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also define the lifetime of the handle

Copy link
Contributor Author

@marcpabst marcpabst Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define the lifetime in a SAFETEY comment or as an actual Rust lifetime?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as a safety comment, saying it's valid until the surface is reconfigured or unconfigured or whatever

@cwfitzgerald
Copy link
Member

Looks good, some small nits, then g2g

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry hit the wrong button 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants