Skip to content
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

[wgpu-core] simplify callbacks #6624

Merged
merged 8 commits into from
Nov 26, 2024
Merged

[wgpu-core] simplify callbacks #6624

merged 8 commits into from
Nov 26, 2024

Conversation

teoxoy
Copy link
Member

@teoxoy teoxoy commented Nov 26, 2024

Follow-up to #6588.

At a high-level:

  • it removes C variants of closures as users of wgpu-core already use the rust variants (incuding wgpu-native).
  • removes the device_unregister_device_lost_closure as users of the API that have cleanup work to do can just implement Drop for state passed into the closure

@teoxoy teoxoy requested review from crowlKats and a team as code owners November 26, 2024 14:53
@teoxoy teoxoy force-pushed the device-lost-cb branch 4 times, most recently from 871cd15 to 9d67652 Compare November 26, 2024 15:21
Comment on lines -628 to -677
#[gpu_test]
static DEVICE_DROP_THEN_LOST: GpuTestConfiguration = GpuTestConfiguration::new()
.parameters(TestParameters::default().expect_fail(FailureCase::webgl2()))
.run_sync(|ctx| {
// This test checks that when the device is dropped (such as in a GC),
// the provided DeviceLostClosure is called with reason DeviceLostReason::Dropped.
// Fails on webgl because webgl doesn't implement drop.
static WAS_CALLED: std::sync::atomic::AtomicBool = AtomicBool::new(false);

// Set a LoseDeviceCallback on the device.
let callback = Box::new(|reason, message| {
WAS_CALLED.store(true, std::sync::atomic::Ordering::SeqCst);
assert_eq!(reason, wgt::DeviceLostReason::Dropped);
assert_eq!(message, "Device dropped.");
});
ctx.device.set_device_lost_callback(callback);

drop(ctx);

assert!(
WAS_CALLED.load(std::sync::atomic::Ordering::SeqCst),
"Device lost callback should have been called."
);
});

#[gpu_test]
static DEVICE_LOST_REPLACED_CALLBACK: GpuTestConfiguration = GpuTestConfiguration::new()
.parameters(TestParameters::default())
.run_sync(|ctx| {
// This test checks that a device_lost_callback is called when it is
// replaced by another callback.
static WAS_CALLED: AtomicBool = AtomicBool::new(false);

// Set a LoseDeviceCallback on the device.
let callback = Box::new(|reason, _m| {
WAS_CALLED.store(true, std::sync::atomic::Ordering::SeqCst);
assert_eq!(reason, wgt::DeviceLostReason::ReplacedCallback);
});
ctx.device.set_device_lost_callback(callback);

// Replace the callback.
let replacement_callback = Box::new(move |_r, _m| {});
ctx.device.set_device_lost_callback(replacement_callback);

assert!(
WAS_CALLED.load(std::sync::atomic::Ordering::SeqCst),
"Device lost callback should have been called."
);
});

Copy link
Member

Choose a reason for hiding this comment

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

suggestion: I actually thought it might be valuable to take these tests and make them assert the opposite of what they originally did; that is, we guarantee that these are not called. in between.

Happy to push a fixup! if you agree.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we can guarantee that though. Technically device loss could happen at any time.

Copy link
Member

Choose a reason for hiding this comment

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

I mean, technically yes, our API needs to handle that at any given time, but we do control the testing environment fairly well, and we're not really exercising any Device functionality in these tests. Are you worried about intermittent test failures still happening, despite all that?

I think if we completely drop the ctx before the check, that makes the test interesting enough. I really can only see such intermittencies happening often in the face of other problems that would make other tests fail, anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's fair but I'm not sure it's worth testing that something doesn't happen if the code that was triggering the behavior is gone.

@ErichDonGubler ErichDonGubler added type: enhancement New feature or request area: api Issues related to API surface area: ecosystem Help the connected projects grow and prosper area: correctness We're behaving incorrectly labels Nov 26, 2024
Copy link
Member

@ErichDonGubler ErichDonGubler left a comment

Choose a reason for hiding this comment

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

LGTM overall. I have some questions, but I'm sure we'll converge on them quickly.

Copy link
Member

@ErichDonGubler ErichDonGubler left a comment

Choose a reason for hiding this comment

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

:shipit:

@teoxoy teoxoy merged commit e2eeba7 into gfx-rs:trunk Nov 26, 2024
25 checks passed
@teoxoy teoxoy deleted the device-lost-cb branch November 26, 2024 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: api Issues related to API surface area: correctness We're behaving incorrectly area: ecosystem Help the connected projects grow and prosper type: enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants