1 // Copyright 2019 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 #include "dawn_native/d3d12/HeapAllocatorD3D12.h" 16 #include "dawn_native/d3d12/D3D12Error.h" 17 #include "dawn_native/d3d12/DeviceD3D12.h" 18 #include "dawn_native/d3d12/HeapD3D12.h" 19 #include "dawn_native/d3d12/ResidencyManagerD3D12.h" 20 21 namespace dawn_native { namespace d3d12 { 22 HeapAllocator(Device * device,D3D12_HEAP_TYPE heapType,D3D12_HEAP_FLAGS heapFlags,MemorySegment memorySegment)23 HeapAllocator::HeapAllocator(Device* device, 24 D3D12_HEAP_TYPE heapType, 25 D3D12_HEAP_FLAGS heapFlags, 26 MemorySegment memorySegment) 27 : mDevice(device), 28 mHeapType(heapType), 29 mHeapFlags(heapFlags), 30 mMemorySegment(memorySegment) { 31 } 32 AllocateResourceHeap(uint64_t size)33 ResultOrError<std::unique_ptr<ResourceHeapBase>> HeapAllocator::AllocateResourceHeap( 34 uint64_t size) { 35 D3D12_HEAP_DESC heapDesc; 36 heapDesc.SizeInBytes = size; 37 heapDesc.Properties.Type = mHeapType; 38 heapDesc.Properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; 39 heapDesc.Properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; 40 heapDesc.Properties.CreationNodeMask = 0; 41 heapDesc.Properties.VisibleNodeMask = 0; 42 // It is preferred to use a size that is a multiple of the alignment. 43 // However, MSAA heaps are always aligned to 4MB instead of 64KB. This means 44 // if the heap size is too small, the VMM would fragment. 45 // TODO(crbug.com/dawn/849): Consider having MSAA vs non-MSAA heaps. 46 heapDesc.Alignment = D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT; 47 heapDesc.Flags = mHeapFlags; 48 49 // CreateHeap will implicitly make the created heap resident. We must ensure enough free 50 // memory exists before allocating to avoid an out-of-memory error when overcommitted. 51 DAWN_TRY(mDevice->GetResidencyManager()->EnsureCanAllocate(size, mMemorySegment)); 52 53 ComPtr<ID3D12Heap> d3d12Heap; 54 DAWN_TRY(CheckOutOfMemoryHRESULT( 55 mDevice->GetD3D12Device()->CreateHeap(&heapDesc, IID_PPV_ARGS(&d3d12Heap)), 56 "ID3D12Device::CreateHeap")); 57 58 std::unique_ptr<ResourceHeapBase> heapBase = 59 std::make_unique<Heap>(std::move(d3d12Heap), mMemorySegment, size); 60 61 // Calling CreateHeap implicitly calls MakeResident on the new heap. We must track this to 62 // avoid calling MakeResident a second time. 63 mDevice->GetResidencyManager()->TrackResidentAllocation(ToBackend(heapBase.get())); 64 return std::move(heapBase); 65 } 66 DeallocateResourceHeap(std::unique_ptr<ResourceHeapBase> heap)67 void HeapAllocator::DeallocateResourceHeap(std::unique_ptr<ResourceHeapBase> heap) { 68 mDevice->ReferenceUntilUnused(static_cast<Heap*>(heap.get())->GetD3D12Heap()); 69 } 70 71 }} // namespace dawn_native::d3d12 72