• 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 #ifndef DAWNNATIVE_D3D11ON12UTIL_H_
16 #define DAWNNATIVE_D3D11ON12UTIL_H_
17 
18 #include "common/RefCounted.h"
19 #include "dawn_native/d3d12/d3d12_platform.h"
20 
21 #include <dawn_native/DawnNative.h>
22 #include <memory>
23 #include <unordered_set>
24 
25 struct ID3D11On12Device;
26 struct IDXGIKeyedMutex;
27 
28 namespace dawn_native { namespace d3d12 {
29 
30     // Wraps 11 wrapped resources in a cache.
31     class D3D11on12ResourceCacheEntry : public RefCounted {
32       public:
33         D3D11on12ResourceCacheEntry(ComPtr<ID3D11On12Device> d3d11on12Device);
34         D3D11on12ResourceCacheEntry(ComPtr<IDXGIKeyedMutex> d3d11on12Resource,
35                                     ComPtr<ID3D11On12Device> d3d11on12Device);
36         ~D3D11on12ResourceCacheEntry();
37 
38         ComPtr<IDXGIKeyedMutex> GetDXGIKeyedMutex() const;
39 
40         // Functors necessary for the
41         // unordered_set<D3D11on12ResourceCacheEntry&>-based cache.
42         struct HashFunc {
43             size_t operator()(const Ref<D3D11on12ResourceCacheEntry> a) const;
44         };
45 
46         struct EqualityFunc {
47             bool operator()(const Ref<D3D11on12ResourceCacheEntry> a,
48                             const Ref<D3D11on12ResourceCacheEntry> b) const;
49         };
50 
51       private:
52         ComPtr<IDXGIKeyedMutex> mDXGIKeyedMutex;
53         ComPtr<ID3D11On12Device> mD3D11on12Device;
54     };
55 
56     // |D3D11on12ResourceCache| maintains a cache of 11 wrapped resources.
57     // Each entry represents a 11 resource that is exclusively accessed by Dawn device.
58     // Since each Dawn device creates and stores a 11on12 device, the 11on12 device
59     // is used as the key for the cache entry which ensures only the same 11 wrapped
60     // resource is re-used and also fully released.
61     //
62     // The cache is primarily needed to avoid repeatedly calling CreateWrappedResource
63     // and special release code per ProduceTexture(device).
64     class D3D11on12ResourceCache {
65       public:
66         D3D11on12ResourceCache();
67         ~D3D11on12ResourceCache();
68 
69         Ref<D3D11on12ResourceCacheEntry> GetOrCreateD3D11on12Resource(
70             WGPUDevice device,
71             ID3D12Resource* d3d12Resource);
72 
73       private:
74         // TODO(dawn:625): Figure out a large enough cache size.
75         static constexpr uint64_t kMaxD3D11on12ResourceCacheSize = 5;
76 
77         // 11on12 resource cache entries are refcounted to ensure if the ExternalImage outlives the
78         // Dawn texture (or vice-versa), we always fully release the 11 wrapped resource without
79         // waiting until Dawn device to shutdown.
80         using Cache = std::unordered_set<Ref<D3D11on12ResourceCacheEntry>,
81                                          D3D11on12ResourceCacheEntry::HashFunc,
82                                          D3D11on12ResourceCacheEntry::EqualityFunc>;
83 
84         Cache mCache;
85     };
86 
87 }}  // namespace dawn_native::d3d12
88 
89 #endif  // DAWNNATIVE_D3D11ON12UTIL_H_
90