• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // D3D12Backend.cpp: contains the definition of symbols exported by D3D12Backend.h so that they
16 // can be compiled twice: once export (shared library), once not exported (static library)
17 
18 #include "dawn_native/d3d12/D3D11on12Util.h"
19 
20 #include "common/HashUtils.h"
21 #include "common/Log.h"
22 #include "dawn_native/d3d12/DeviceD3D12.h"
23 
24 namespace dawn_native { namespace d3d12 {
25 
Flush11On12DeviceToAvoidLeaks(ComPtr<ID3D11On12Device> d3d11on12Device)26     void Flush11On12DeviceToAvoidLeaks(ComPtr<ID3D11On12Device> d3d11on12Device) {
27         if (d3d11on12Device == nullptr) {
28             return;
29         }
30 
31         ComPtr<ID3D11Device> d3d11Device;
32         if (FAILED(d3d11on12Device.As(&d3d11Device))) {
33             return;
34         }
35 
36         ComPtr<ID3D11DeviceContext> d3d11DeviceContext;
37         d3d11Device->GetImmediateContext(&d3d11DeviceContext);
38 
39         ASSERT(d3d11DeviceContext != nullptr);
40 
41         // 11on12 has a bug where D3D12 resources used only for keyed shared mutexes
42         // are not released until work is submitted to the device context and flushed.
43         // The most minimal work we can get away with is issuing a TiledResourceBarrier.
44 
45         // ID3D11DeviceContext2 is available in Win8.1 and above. This suffices for a
46         // D3D12 backend since both D3D12 and 11on12 first appeared in Windows 10.
47         ComPtr<ID3D11DeviceContext2> d3d11DeviceContext2;
48         if (FAILED(d3d11DeviceContext.As(&d3d11DeviceContext2))) {
49             return;
50         }
51 
52         d3d11DeviceContext2->TiledResourceBarrier(nullptr, nullptr);
53         d3d11DeviceContext2->Flush();
54     }
55 
D3D11on12ResourceCacheEntry(ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex,ComPtr<ID3D11On12Device> d3d11On12Device)56     D3D11on12ResourceCacheEntry::D3D11on12ResourceCacheEntry(
57         ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex,
58         ComPtr<ID3D11On12Device> d3d11On12Device)
59         : mDXGIKeyedMutex(std::move(dxgiKeyedMutex)), mD3D11on12Device(std::move(d3d11On12Device)) {
60     }
61 
D3D11on12ResourceCacheEntry(ComPtr<ID3D11On12Device> d3d11On12Device)62     D3D11on12ResourceCacheEntry::D3D11on12ResourceCacheEntry(
63         ComPtr<ID3D11On12Device> d3d11On12Device)
64         : mD3D11on12Device(std::move(d3d11On12Device)) {
65     }
66 
~D3D11on12ResourceCacheEntry()67     D3D11on12ResourceCacheEntry::~D3D11on12ResourceCacheEntry() {
68         if (mDXGIKeyedMutex == nullptr) {
69             return;
70         }
71 
72         ComPtr<ID3D11Resource> d3d11Resource;
73         if (FAILED(mDXGIKeyedMutex.As(&d3d11Resource))) {
74             return;
75         }
76 
77         ASSERT(mD3D11on12Device != nullptr);
78 
79         ID3D11Resource* d3d11ResourceRaw = d3d11Resource.Get();
80         mD3D11on12Device->ReleaseWrappedResources(&d3d11ResourceRaw, 1);
81 
82         d3d11Resource.Reset();
83         mDXGIKeyedMutex.Reset();
84 
85         Flush11On12DeviceToAvoidLeaks(std::move(mD3D11on12Device));
86     }
87 
GetDXGIKeyedMutex() const88     ComPtr<IDXGIKeyedMutex> D3D11on12ResourceCacheEntry::GetDXGIKeyedMutex() const {
89         ASSERT(mDXGIKeyedMutex != nullptr);
90         return mDXGIKeyedMutex;
91     }
92 
operator ()(const Ref<D3D11on12ResourceCacheEntry> a) const93     size_t D3D11on12ResourceCacheEntry::HashFunc::operator()(
94         const Ref<D3D11on12ResourceCacheEntry> a) const {
95         size_t hash = 0;
96         HashCombine(&hash, a->mD3D11on12Device.Get());
97         return hash;
98     }
99 
operator ()(const Ref<D3D11on12ResourceCacheEntry> a,const Ref<D3D11on12ResourceCacheEntry> b) const100     bool D3D11on12ResourceCacheEntry::EqualityFunc::operator()(
101         const Ref<D3D11on12ResourceCacheEntry> a,
102         const Ref<D3D11on12ResourceCacheEntry> b) const {
103         return a->mD3D11on12Device == b->mD3D11on12Device;
104     }
105 
106     D3D11on12ResourceCache::D3D11on12ResourceCache() = default;
107 
108     D3D11on12ResourceCache::~D3D11on12ResourceCache() = default;
109 
GetOrCreateD3D11on12Resource(WGPUDevice device,ID3D12Resource * d3d12Resource)110     Ref<D3D11on12ResourceCacheEntry> D3D11on12ResourceCache::GetOrCreateD3D11on12Resource(
111         WGPUDevice device,
112         ID3D12Resource* d3d12Resource) {
113         Device* backendDevice = reinterpret_cast<Device*>(device);
114         // The Dawn and 11on12 device share the same D3D12 command queue whereas this external image
115         // could be accessed/produced with multiple Dawn devices. To avoid cross-queue sharing
116         // restrictions, the 11 wrapped resource is forbidden to be shared between Dawn devices by
117         // using the 11on12 device as the cache key.
118         ComPtr<ID3D11On12Device> d3d11on12Device = backendDevice->GetOrCreateD3D11on12Device();
119         if (d3d11on12Device == nullptr) {
120             dawn::ErrorLog() << "Unable to create 11on12 device for external image";
121             return nullptr;
122         }
123 
124         D3D11on12ResourceCacheEntry blueprint(d3d11on12Device);
125         auto iter = mCache.find(&blueprint);
126         if (iter != mCache.end()) {
127             return *iter;
128         }
129 
130         // We use IDXGIKeyedMutexes to synchronize access between D3D11 and D3D12. D3D11/12 fences
131         // are a viable alternative but are, unfortunately, not available on all versions of Windows
132         // 10. Since D3D12 does not directly support keyed mutexes, we need to wrap the D3D12
133         // resource using 11on12 and QueryInterface the D3D11 representation for the keyed mutex.
134         ComPtr<ID3D11Texture2D> d3d11Texture;
135         D3D11_RESOURCE_FLAGS resourceFlags;
136         resourceFlags.BindFlags = 0;
137         resourceFlags.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
138         resourceFlags.CPUAccessFlags = 0;
139         resourceFlags.StructureByteStride = 0;
140         if (FAILED(d3d11on12Device->CreateWrappedResource(
141                 d3d12Resource, &resourceFlags, D3D12_RESOURCE_STATE_COMMON,
142                 D3D12_RESOURCE_STATE_COMMON, IID_PPV_ARGS(&d3d11Texture)))) {
143             return nullptr;
144         }
145 
146         ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex;
147         if (FAILED(d3d11Texture.As(&dxgiKeyedMutex))) {
148             return nullptr;
149         }
150 
151         // Keep this cache from growing unbounded.
152         // TODO(dawn:625): Consider using a replacement policy based cache.
153         if (mCache.size() > kMaxD3D11on12ResourceCacheSize) {
154             mCache.clear();
155         }
156 
157         Ref<D3D11on12ResourceCacheEntry> entry =
158             AcquireRef(new D3D11on12ResourceCacheEntry(dxgiKeyedMutex, std::move(d3d11on12Device)));
159         mCache.insert(entry);
160 
161         return entry;
162     }
163 
164 }}  // namespace dawn_native::d3d12