• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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_D3D12_RESIDENCYMANAGERD3D12_H_
16 #define DAWNNATIVE_D3D12_RESIDENCYMANAGERD3D12_H_
17 
18 #include "common/LinkedList.h"
19 #include "dawn_native/D3D12Backend.h"
20 #include "dawn_native/Error.h"
21 #include "dawn_native/dawn_platform.h"
22 
23 #include "dawn_native/d3d12/d3d12_platform.h"
24 
25 namespace dawn_native { namespace d3d12 {
26 
27     class Device;
28     class Heap;
29     class Pageable;
30 
31     class ResidencyManager {
32       public:
33         ResidencyManager(Device* device);
34 
35         MaybeError LockAllocation(Pageable* pageable);
36         void UnlockAllocation(Pageable* pageable);
37 
38         MaybeError EnsureCanAllocate(uint64_t allocationSize, MemorySegment memorySegment);
39         MaybeError EnsureHeapsAreResident(Heap** heaps, size_t heapCount);
40 
41         uint64_t SetExternalMemoryReservation(MemorySegment segment,
42                                               uint64_t requestedReservationSize);
43 
44         void TrackResidentAllocation(Pageable* pageable);
45 
46         void RestrictBudgetForTesting(uint64_t artificialBudgetCap);
47 
48       private:
49         struct MemorySegmentInfo {
50             const DXGI_MEMORY_SEGMENT_GROUP dxgiSegment;
51             LinkedList<Pageable> lruCache = {};
52             uint64_t budget = 0;
53             uint64_t usage = 0;
54             uint64_t externalReservation = 0;
55             uint64_t externalRequest = 0;
56         };
57 
58         struct VideoMemoryInfo {
59             MemorySegmentInfo local = {DXGI_MEMORY_SEGMENT_GROUP_LOCAL};
60             MemorySegmentInfo nonLocal = {DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL};
61         };
62 
63         MemorySegmentInfo* GetMemorySegmentInfo(MemorySegment memorySegment);
64         ResultOrError<uint64_t> EnsureCanMakeResident(uint64_t allocationSize,
65                                                       MemorySegmentInfo* memorySegment);
66         ResultOrError<Pageable*> RemoveSingleEntryFromLRU(MemorySegmentInfo* memorySegment);
67         MaybeError MakeAllocationsResident(MemorySegmentInfo* segment,
68                                            uint64_t sizeToMakeResident,
69                                            uint64_t numberOfObjectsToMakeResident,
70                                            ID3D12Pageable** allocations);
71         void UpdateVideoMemoryInfo();
72         void UpdateMemorySegmentInfo(MemorySegmentInfo* segmentInfo);
73 
74         Device* mDevice;
75         bool mResidencyManagementEnabled = false;
76         bool mRestrictBudgetForTesting = false;
77         VideoMemoryInfo mVideoMemoryInfo = {};
78     };
79 
80 }}  // namespace dawn_native::d3d12
81 
82 #endif  // DAWNNATIVE_D3D12_RESIDENCYMANAGERD3D12_H_
83