Add SharedTextureMemoryD3dSwapchainBeginState

This serves as a replacement of isSwapChainTexture in
ExternalImageDXGIBeginAccessDescriptor.

Bug: dawn:1745
Bug: chromium:335003893
Change-Id: I923761c5fb460e7f6ccc511982d54d3884df68ba
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/184500
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Jie A Chen <jie.a.chen@intel.com>
diff --git a/src/dawn/dawn.json b/src/dawn/dawn.json
index e891705..fa93285 100644
--- a/src/dawn/dawn.json
+++ b/src/dawn/dawn.json
@@ -2062,6 +2062,15 @@
             {"name": "new layout", "type": "int32_t"}
         ]
     },
+    "shared texture memory D3D swapchain begin state": {
+        "category": "structure",
+        "chained": "in",
+        "chain roots": ["shared texture memory begin access descriptor"],
+        "tags": ["dawn", "native"],
+        "members": [
+            {"name": "is swapchain", "type": "bool", "default": "false"}
+        ]
+    },
     "shared fence": {
         "category": "object",
         "tags": ["dawn", "native"],
@@ -3723,19 +3732,20 @@
             {"value": 1201, "name": "shared texture memory initialized end state", "tags": ["dawn", "native"]},
             {"value": 1202, "name": "shared texture memory vk image layout begin state", "tags": ["dawn", "native"]},
             {"value": 1203, "name": "shared texture memory vk image layout end state", "tags": ["dawn", "native"]},
-            {"value": 1204, "name": "shared fence vk semaphore opaque FD descriptor", "tags": ["dawn", "native"]},
-            {"value": 1205, "name": "shared fence vk semaphore opaque FD export info", "tags": ["dawn", "native"]},
-            {"value": 1206, "name": "shared fence vk semaphore sync FD descriptor", "tags": ["dawn", "native"]},
-            {"value": 1207, "name": "shared fence vk semaphore sync FD export info", "tags": ["dawn", "native"]},
-            {"value": 1208, "name": "shared fence vk semaphore zircon handle descriptor", "tags": ["dawn", "native"]},
-            {"value": 1209, "name": "shared fence vk semaphore zircon handle export info", "tags": ["dawn", "native"]},
-            {"value": 1210, "name": "shared fence DXGI shared handle descriptor", "tags": ["dawn", "native"]},
-            {"value": 1211, "name": "shared fence DXGI shared handle export info", "tags": ["dawn", "native"]},
-            {"value": 1212, "name": "shared fence MTL shared event descriptor", "tags": ["dawn", "native"]},
-            {"value": 1213, "name": "shared fence MTL shared event export info", "tags": ["dawn", "native"]},
-            {"value": 1214, "name": "shared buffer memory D3D12 resource descriptor", "tags": ["dawn", "native"]},
-            {"value": 1215, "name": "static sampler binding layout", "tags": ["dawn"]},
-            {"value": 1216, "name": "sampler y cb cr vulkan descriptor", "tags": ["dawn", "native"]}
+            {"value": 1204, "name": "shared texture memory D3D swapchain begin state", "tags": ["dawn", "native"]},
+            {"value": 1205, "name": "shared fence vk semaphore opaque FD descriptor", "tags": ["dawn", "native"]},
+            {"value": 1206, "name": "shared fence vk semaphore opaque FD export info", "tags": ["dawn", "native"]},
+            {"value": 1207, "name": "shared fence vk semaphore sync FD descriptor", "tags": ["dawn", "native"]},
+            {"value": 1208, "name": "shared fence vk semaphore sync FD export info", "tags": ["dawn", "native"]},
+            {"value": 1209, "name": "shared fence vk semaphore zircon handle descriptor", "tags": ["dawn", "native"]},
+            {"value": 1210, "name": "shared fence vk semaphore zircon handle export info", "tags": ["dawn", "native"]},
+            {"value": 1211, "name": "shared fence DXGI shared handle descriptor", "tags": ["dawn", "native"]},
+            {"value": 1212, "name": "shared fence DXGI shared handle export info", "tags": ["dawn", "native"]},
+            {"value": 1213, "name": "shared fence MTL shared event descriptor", "tags": ["dawn", "native"]},
+            {"value": 1214, "name": "shared fence MTL shared event export info", "tags": ["dawn", "native"]},
+            {"value": 1215, "name": "shared buffer memory D3D12 resource descriptor", "tags": ["dawn", "native"]},
+            {"value": 1216, "name": "static sampler binding layout", "tags": ["dawn"]},
+            {"value": 1217, "name": "sampler y cb cr vulkan descriptor", "tags": ["dawn", "native"]}
         ]
     },
     "texture": {
diff --git a/src/dawn/native/d3d/SharedTextureMemoryD3D.cpp b/src/dawn/native/d3d/SharedTextureMemoryD3D.cpp
index 16e49df..88dc163 100644
--- a/src/dawn/native/d3d/SharedTextureMemoryD3D.cpp
+++ b/src/dawn/native/d3d/SharedTextureMemoryD3D.cpp
@@ -46,7 +46,7 @@
 MaybeError SharedTextureMemory::BeginAccessImpl(
     TextureBase* texture,
     const UnpackedPtr<BeginAccessDescriptor>& descriptor) {
-    DAWN_TRY(descriptor.ValidateSubset<>());
+    DAWN_TRY(descriptor.ValidateSubset<SharedTextureMemoryD3DSwapchainBeginState>());
     for (size_t i = 0; i < descriptor->fenceCount; ++i) {
         SharedFenceBase* fence = descriptor->fences[i];
 
diff --git a/src/dawn/native/d3d12/SharedTextureMemoryD3D12.cpp b/src/dawn/native/d3d12/SharedTextureMemoryD3D12.cpp
index 511f3d253..4e16917 100644
--- a/src/dawn/native/d3d12/SharedTextureMemoryD3D12.cpp
+++ b/src/dawn/native/d3d12/SharedTextureMemoryD3D12.cpp
@@ -129,6 +129,11 @@
     TextureBase* texture,
     const UnpackedPtr<BeginAccessDescriptor>& descriptor) {
     DAWN_TRY(d3d::SharedTextureMemory::BeginAccessImpl(texture, descriptor));
+
+    if (auto* beginState = descriptor.Get<SharedTextureMemoryD3DSwapchainBeginState>()) {
+        ToBackend(texture)->SetIsSwapchainTexture(beginState->isSwapchain);
+    }
+
     // Reset state to COMMON. BeginAccess contains a list of fences to wait on after
     // which the texture's usage will complete on the GPU.
     // All textures created from SharedTextureMemory must have
diff --git a/src/dawn/native/d3d12/TextureD3D12.cpp b/src/dawn/native/d3d12/TextureD3D12.cpp
index 8267cd8..77810be 100644
--- a/src/dawn/native/d3d12/TextureD3D12.cpp
+++ b/src/dawn/native/d3d12/TextureD3D12.cpp
@@ -477,6 +477,10 @@
     }
 }
 
+void Texture::SetIsSwapchainTexture(bool isSwapChainTexture) {
+    mSwapChainTexture = isSwapChainTexture;
+}
+
 void Texture::TrackUsageAndTransitionNow(CommandRecordingContext* commandContext,
                                          wgpu::TextureUsage usage,
                                          const SubresourceRange& range) {
diff --git a/src/dawn/native/d3d12/TextureD3D12.h b/src/dawn/native/d3d12/TextureD3D12.h
index aecb359..0a017a2 100644
--- a/src/dawn/native/d3d12/TextureD3D12.h
+++ b/src/dawn/native/d3d12/TextureD3D12.h
@@ -102,6 +102,8 @@
 
     void NotifySwapChainPresentToPIX();
 
+    void SetIsSwapchainTexture(bool isSwapChainTexture);
+
     void TrackUsageAndGetResourceBarrierForPass(CommandRecordingContext* commandContext,
                                                 std::vector<D3D12_RESOURCE_BARRIER>* barrier,
                                                 const TextureSubresourceSyncInfo& textureSyncInfos);