d3d12: Move NextSerial out of ExecuteCommandList

Reverts to the code flow before fences were implemented. NextSerial is
now the reponsibility of the caller of ExecutePendingCommandContext like
it was before. We now use GetPendingCommandSerial to store the signal
fence value instead of GetLastSubmittedCommandSerial and check that the
signal fence value was submitted in EndAccess.

Bug: dawn:576
Change-Id: I616840a0932ec17f77fcab38058773006dfae32f
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/103501
Commit-Queue: Sunny Sachanandani <sunnyps@chromium.org>
Auto-Submit: Sunny Sachanandani <sunnyps@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/dawn/native/d3d12/CommandRecordingContext.cpp b/src/dawn/native/d3d12/CommandRecordingContext.cpp
index 7c9cf0e..cd44e4a 100644
--- a/src/dawn/native/d3d12/CommandRecordingContext.cpp
+++ b/src/dawn/native/d3d12/CommandRecordingContext.cpp
@@ -123,8 +123,6 @@
         ID3D12CommandList* d3d12CommandList = GetCommandList();
         device->GetCommandQueue()->ExecuteCommandLists(1, &d3d12CommandList);
 
-        DAWN_TRY(device->NextSerial());
-
         for (Texture* texture : mSharedTextures) {
             DAWN_TRY(texture->SynchronizeImportedTextureAfterUse());
         }
diff --git a/src/dawn/native/d3d12/DeviceD3D12.cpp b/src/dawn/native/d3d12/DeviceD3D12.cpp
index 9bfb03e..9aa1627 100644
--- a/src/dawn/native/d3d12/DeviceD3D12.cpp
+++ b/src/dawn/native/d3d12/DeviceD3D12.cpp
@@ -338,8 +338,8 @@
     mUsedComObjectRefs.ClearUpTo(completedSerial);
 
     if (mPendingCommands.IsOpen()) {
-        // CommandRecordingContext::ExecuteCommandList() calls NextSerial().
         DAWN_TRY(ExecutePendingCommandContext());
+        DAWN_TRY(NextSerial());
     }
 
     DAWN_TRY(CheckDebugLayerAndGenerateErrors());
diff --git a/src/dawn/native/d3d12/DeviceD3D12.h b/src/dawn/native/d3d12/DeviceD3D12.h
index 6b56d1a..d122fd1 100644
--- a/src/dawn/native/d3d12/DeviceD3D12.h
+++ b/src/dawn/native/d3d12/DeviceD3D12.h
@@ -91,7 +91,6 @@
 
     void ReferenceUntilUnused(ComPtr<IUnknown> object);
 
-    // Execute pending CommandRecordingContext, and increment last submitted serial if needed.
     MaybeError ExecutePendingCommandContext();
 
     ResultOrError<std::unique_ptr<StagingBufferBase>> CreateStagingBuffer(size_t size) override;
diff --git a/src/dawn/native/d3d12/TextureD3D12.cpp b/src/dawn/native/d3d12/TextureD3D12.cpp
index 02588c2..f17e995 100644
--- a/src/dawn/native/d3d12/TextureD3D12.cpp
+++ b/src/dawn/native/d3d12/TextureD3D12.cpp
@@ -691,19 +691,19 @@
         // Needed to ensure that command allocator doesn't get destroyed before pending commands
         // are submitted due to calling NextSerial(). No-op if there are no pending commands.
         DAWN_TRY(ToBackend(GetDevice())->ExecutePendingCommandContext());
-
         // If there were pending commands that used this texture mSignalFenceValue will be set,
         // but if it's still not set, generate a signal fence after waiting on wait fences.
         if (!mSignalFenceValue.has_value()) {
             DAWN_TRY(SynchronizeImportedTextureBeforeUse());
-            DAWN_TRY(ToBackend(GetDevice())->NextSerial());
             DAWN_TRY(SynchronizeImportedTextureAfterUse());
-            ASSERT(mSignalFenceValue.has_value());
         }
+        DAWN_TRY(ToBackend(GetDevice())->NextSerial());
+        ASSERT(mSignalFenceValue.has_value());
     }
 
-    // Explicitly call reset() since std::move() on optional doesn't make it std::nullopt.
     ExecutionSerial ret = mSignalFenceValue.value();
+    ASSERT(ret <= GetDevice()->GetLastSubmittedCommandSerial());
+    // Explicitly call reset() since std::move() on optional doesn't make it std::nullopt.
     mSignalFenceValue.reset();
     return ret;
 }
@@ -760,8 +760,8 @@
     if (mD3D11on12Resource != nullptr) {
         DAWN_TRY(mD3D11on12Resource->ReleaseKeyedMutex());
     } else {
-        // CommandRecordingContext will call NextSerial() to increment the fence before this call.
-        mSignalFenceValue = ToBackend(GetDevice())->GetLastSubmittedCommandSerial();
+        // NextSerial() will be called after this - this is also checked in EndAccess().
+        mSignalFenceValue = ToBackend(GetDevice())->GetPendingCommandSerial();
     }
     return {};
 }