Skip to content

Commit 851ba3e

Browse files
KangzDawn LUCI CQ
authored andcommitted
[dawn] Add more tests of buffer mapping and device loss/destroy
Bug: 42240407, 492139412 Change-Id: Ifef5b6a0e0219c7833a364b629d03b357ed42b89 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/297055 Reviewed-by: Loko Kung <lokokung@google.com> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
1 parent b97879f commit 851ba3e

File tree

3 files changed

+83
-14
lines changed

3 files changed

+83
-14
lines changed

src/dawn/tests/end2end/DeviceLostTests.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ TEST_P(DeviceLostTest, GetMappedRange_CreateBufferMappedAtCreationAfterLoss) {
407407
ExpectObjectIsError(buffer);
408408

409409
ASSERT_NE(buffer.GetMappedRange(), nullptr);
410+
411+
// Write to the range as it should still point to valid memory.
412+
*static_cast<uint32_t*>(buffer.GetMappedRange()) = 42;
410413
}
411414

412415
// Test that device loss doesn't change the result of GetMappedRange, mappedAtCreation version.
@@ -422,6 +425,9 @@ TEST_P(DeviceLostTest, GetMappedRange_CreateBufferMappedAtCreationBeforeLoss) {
422425

423426
ASSERT_NE(buffer.GetMappedRange(), nullptr);
424427
ASSERT_EQ(buffer.GetMappedRange(), rangeBeforeLoss);
428+
429+
// Write to the range as it should still point to valid memory.
430+
*static_cast<uint32_t*>(buffer.GetMappedRange()) = 42;
425431
}
426432

427433
// Test that device loss doesn't change the result of GetMappedRange, mapping for reading version.
@@ -439,6 +445,14 @@ TEST_P(DeviceLostTest, GetMappedRange_MapAsyncReading) {
439445

440446
ASSERT_NE(buffer.GetConstMappedRange(), nullptr);
441447
ASSERT_EQ(buffer.GetConstMappedRange(), rangeBeforeLoss);
448+
449+
if (!IsNull()) {
450+
// Read from the range as it should still point to valid memory. Also check the value to
451+
// force the compiler to keep the read. The null backend doesn't do zero init so we skip
452+
// there.
453+
uint32_t zero = *static_cast<const uint32_t*>(buffer.GetConstMappedRange());
454+
ASSERT_EQ(zero, 0u);
455+
}
442456
}
443457

444458
// Test that device loss doesn't change the result of GetMappedRange, mapping for writing version.
@@ -456,6 +470,9 @@ TEST_P(DeviceLostTest, GetMappedRange_MapAsyncWriting) {
456470

457471
ASSERT_NE(buffer.GetConstMappedRange(), nullptr);
458472
ASSERT_EQ(buffer.GetConstMappedRange(), rangeBeforeLoss);
473+
474+
// Write to the range as it should still point to valid memory.
475+
*static_cast<uint32_t*>(buffer.GetMappedRange()) = 42;
459476
}
460477

461478
// TODO(dawn:929): mapasync read + resolve + loss getmappedrange != nullptr.

src/dawn/tests/unittests/validation/BufferValidationTests.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,42 @@ TEST_P(BufferMappingValidationTest, MapAsync_RetryInDestroyedCallback) {
562562
WaitForAllOperations();
563563
}
564564

565+
// Test that destroying the device cancels the mapping operation.
566+
TEST_P(BufferMappingValidationTest, DestroyDeviceWhilePending) {
567+
wgpu::Buffer buffer = CreateBuffer(4);
568+
569+
MockMapAsyncCallback mockCb;
570+
EXPECT_CALL(mockCb, Call(wgpu::MapAsyncStatus::Aborted, _)).Times(1);
571+
buffer.MapAsync(GetParam(), 0, 4, wgpu::CallbackMode::AllowProcessEvents, mockCb.Callback());
572+
573+
ExpectDeviceDestruction();
574+
device.Destroy();
575+
576+
WaitForAllOperations();
577+
ASSERT_EQ(wgpu::BufferMapState::Unmapped, buffer.GetMapState());
578+
}
579+
580+
// Test that destroying the device cancels the mapping operation.
581+
TEST_P(BufferMappingValidationTest, DestroyDeviceAfterMapping) {
582+
// TODO(https://crbug.com/42240407): Unmap buffers in the wire client when
583+
// device.Destroy() is called.
584+
DAWN_SKIP_TEST_IF(UsesWire());
585+
586+
wgpu::Buffer buffer = CreateBuffer(4);
587+
588+
MockMapAsyncCallback mockCb;
589+
EXPECT_CALL(mockCb, Call(wgpu::MapAsyncStatus::Success, _)).Times(1);
590+
buffer.MapAsync(GetParam(), 0, 4, wgpu::CallbackMode::AllowProcessEvents, mockCb.Callback());
591+
WaitForAllOperations();
592+
593+
ASSERT_EQ(wgpu::BufferMapState::Mapped, buffer.GetMapState());
594+
595+
ExpectDeviceDestruction();
596+
device.Destroy();
597+
598+
ASSERT_EQ(wgpu::BufferMapState::Unmapped, buffer.GetMapState());
599+
}
600+
565601
// Test the success case for mappedAtCreation
566602
TEST_F(BufferValidationTest, MappedAtCreationSuccess) {
567603
BufferMappedAtCreation(4, wgpu::BufferUsage::MapWrite);
@@ -596,6 +632,22 @@ TEST_F(BufferValidationTest, MappedAtCreationOOM) {
596632
}
597633
}
598634

635+
// Test that destroying the device cancels the mapping operation.
636+
TEST_F(BufferValidationTest, MappedAtCreationThenDestroyDevice) {
637+
// TODO(https://crbug.com/42240407): Unmap buffers in the wire client when
638+
// device.Destroy() is called.
639+
DAWN_SKIP_TEST_IF(UsesWire());
640+
641+
wgpu::Buffer buffer = BufferMappedAtCreation(4, wgpu::BufferUsage::CopySrc);
642+
643+
ASSERT_EQ(wgpu::BufferMapState::Mapped, buffer.GetMapState());
644+
645+
ExpectDeviceDestruction();
646+
device.Destroy();
647+
648+
ASSERT_EQ(wgpu::BufferMapState::Unmapped, buffer.GetMapState());
649+
}
650+
599651
// Test that it is valid to destroy an error buffer
600652
TEST_F(BufferValidationTest, DestroyErrorBuffer) {
601653
wgpu::BufferDescriptor desc;

src/dawn/tests/unittests/validation/CommandBufferValidationTests.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,10 @@ TEST_F(CommandBufferValidationTest, InjectedValidateErrorVariousStringTypes) {
414414
}
415415
}
416416

417+
// Test having the device destroyed before encoding.
417418
TEST_F(CommandBufferValidationTest, EncodeAfterDeviceDestroyed) {
418419
PlaceholderRenderPass placeholderRenderPass(device);
419420

420-
// Device destroyed before encoding.
421-
{
422421
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
423422
ExpectDeviceDestruction();
424423
device.Destroy();
@@ -427,19 +426,20 @@ TEST_F(CommandBufferValidationTest, EncodeAfterDeviceDestroyed) {
427426
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&placeholderRenderPass);
428427
pass.End();
429428
encoder.Finish();
430-
}
429+
}
431430

432-
// Device destroyed after encoding.
433-
{
434-
ExpectDeviceDestruction();
435-
device.Destroy();
436-
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
437-
// The encoder should not accessing any device info if device is destroyed when try
438-
// encoding.
439-
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&placeholderRenderPass);
440-
pass.End();
441-
encoder.Finish();
442-
}
431+
// Test having the device destroyed after encoding but before Finish().
432+
TEST_F(CommandBufferValidationTest, FinishAfterDeviceDestroyed) {
433+
PlaceholderRenderPass placeholderRenderPass(device);
434+
435+
ExpectDeviceDestruction();
436+
device.Destroy();
437+
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
438+
// The encoder should not accessing any device info if device is destroyed when try
439+
// encoding.
440+
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&placeholderRenderPass);
441+
pass.End();
442+
encoder.Finish();
443443
}
444444

445445
// Test that an error is produced when encoding happens after ending a pass.

0 commit comments

Comments
 (0)