d3d11: only signal non-monitored fence with shared resources usage

Bug: chromium:335553337
Change-Id: I8795606591f654629100fcabdca10706027481c1
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/186564
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Peng Huang <penghuang@chromium.org>
diff --git a/src/dawn/native/d3d11/CommandRecordingContextD3D11.cpp b/src/dawn/native/d3d11/CommandRecordingContextD3D11.cpp
index 2fc2ead..331a43f 100644
--- a/src/dawn/native/d3d11/CommandRecordingContextD3D11.cpp
+++ b/src/dawn/native/d3d11/CommandRecordingContextD3D11.cpp
@@ -142,6 +142,10 @@
     return {};
 }
 
+void ScopedCommandRecordingContext::SetNeedsFence() const {
+    Get()->mNeedsFence = true;
+}
+
 ScopedSwapStateCommandRecordingContext::ScopedSwapStateCommandRecordingContext(
     CommandRecordingContextGuard&& guard)
     : ScopedCommandRecordingContext(std::move(guard)),
@@ -275,4 +279,10 @@
     mAcquiredKeyedMutexes.clear();
 }
 
+bool CommandRecordingContext::AcquireNeedsFence() {
+    bool needsFence = mNeedsFence;
+    mNeedsFence = false;
+    return needsFence;
+}
+
 }  // namespace dawn::native::d3d11
diff --git a/src/dawn/native/d3d11/CommandRecordingContextD3D11.h b/src/dawn/native/d3d11/CommandRecordingContextD3D11.h
index 59ea471..5a932a8 100644
--- a/src/dawn/native/d3d11/CommandRecordingContextD3D11.h
+++ b/src/dawn/native/d3d11/CommandRecordingContextD3D11.h
@@ -87,6 +87,8 @@
 
     void ReleaseKeyedMutexes();
 
+    bool AcquireNeedsFence();
+
   private:
     template <typename Ctx, typename Traits>
     friend class CommandRecordingContextGuard;
@@ -110,6 +112,8 @@
 
     absl::flat_hash_set<Ref<d3d::KeyedMutex>> mAcquiredKeyedMutexes;
 
+    bool mNeedsFence = false;
+
     Ref<Device> mDevice;
 };
 
@@ -158,6 +162,8 @@
     MaybeError FlushUniformBuffer() const;
 
     MaybeError AcquireKeyedMutex(Ref<d3d::KeyedMutex> keyedMutex) const;
+
+    void SetNeedsFence() const;
 };
 
 // For using ID3D11DeviceContext directly. It swaps and resets ID3DDeviceContextState of
diff --git a/src/dawn/native/d3d11/QueueD3D11.cpp b/src/dawn/native/d3d11/QueueD3D11.cpp
index fa0ccdf..f6fc1f4 100644
--- a/src/dawn/native/d3d11/QueueD3D11.cpp
+++ b/src/dawn/native/d3d11/QueueD3D11.cpp
@@ -337,11 +337,12 @@
 
     IncrementLastSubmittedCommandSerial();
     ExecutionSerial lastSubmittedSerial = GetLastSubmittedCommandSerial();
-    // TODO(crbug.com/335553337): only signal fence when it is needed.
-    TRACE_EVENT1(GetDevice()->GetPlatform(), General, "D3D11Device::SignalFence", "serial",
-                 uint64_t(lastSubmittedSerial));
-    DAWN_TRY(CheckHRESULT(commandContext.Signal(mFence.Get(), uint64_t(lastSubmittedSerial)),
-                          "D3D11 command queue signal fence"));
+    if (commandContext->AcquireNeedsFence()) {
+        TRACE_EVENT1(GetDevice()->GetPlatform(), General, "D3D11Device::SignalFence", "serial",
+                     uint64_t(lastSubmittedSerial));
+        DAWN_TRY(CheckHRESULT(commandContext.Signal(mFence.Get(), uint64_t(lastSubmittedSerial)),
+                              "D3D11 command queue signal fence"));
+    }
 
     SystemEventReceiver receiver;
     DAWN_TRY_ASSIGN(receiver, GetSystemEventReceiver());
diff --git a/src/dawn/native/d3d11/TextureD3D11.cpp b/src/dawn/native/d3d11/TextureD3D11.cpp
index 41781d5..e050471 100644
--- a/src/dawn/native/d3d11/TextureD3D11.cpp
+++ b/src/dawn/native/d3d11/TextureD3D11.cpp
@@ -511,6 +511,7 @@
                 commandContext->Wait(ToBackend(fence.object)->GetD3DFence(), fence.signaledValue),
                 "ID3D11DeviceContext4::Wait"));
         }
+        commandContext->SetNeedsFence();
     }
     if (mKeyedMutex != nullptr) {
         DAWN_TRY(commandContext->AcquireKeyedMutex(mKeyedMutex));