2019-10-17 17:25:28 +00:00
|
|
|
// Copyright 2019 The Dawn Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "dawn_native/d3d12/HeapAllocatorD3D12.h"
|
2019-10-28 12:59:36 +00:00
|
|
|
#include "dawn_native/d3d12/D3D12Error.h"
|
2019-10-17 17:25:28 +00:00
|
|
|
#include "dawn_native/d3d12/DeviceD3D12.h"
|
|
|
|
#include "dawn_native/d3d12/HeapD3D12.h"
|
2020-03-31 15:31:56 +00:00
|
|
|
#include "dawn_native/d3d12/ResidencyManagerD3D12.h"
|
2019-10-17 17:25:28 +00:00
|
|
|
|
|
|
|
namespace dawn_native { namespace d3d12 {
|
|
|
|
|
|
|
|
HeapAllocator::HeapAllocator(Device* device,
|
|
|
|
D3D12_HEAP_TYPE heapType,
|
2020-04-23 21:50:32 +00:00
|
|
|
D3D12_HEAP_FLAGS heapFlags,
|
|
|
|
MemorySegment memorySegment)
|
|
|
|
: mDevice(device),
|
|
|
|
mHeapType(heapType),
|
|
|
|
mHeapFlags(heapFlags),
|
|
|
|
mMemorySegment(memorySegment) {
|
2019-10-17 17:25:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 21:28:16 +00:00
|
|
|
ResultOrError<std::unique_ptr<ResourceHeapBase>> HeapAllocator::AllocateResourceHeap(
|
|
|
|
uint64_t size) {
|
2019-10-17 17:25:28 +00:00
|
|
|
D3D12_HEAP_DESC heapDesc;
|
|
|
|
heapDesc.SizeInBytes = size;
|
|
|
|
heapDesc.Properties.Type = mHeapType;
|
|
|
|
heapDesc.Properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
|
|
|
heapDesc.Properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
|
|
|
heapDesc.Properties.CreationNodeMask = 0;
|
|
|
|
heapDesc.Properties.VisibleNodeMask = 0;
|
2019-10-22 21:01:11 +00:00
|
|
|
// It is preferred to use a size that is a multiple of the alignment.
|
|
|
|
// However, MSAA heaps are always aligned to 4MB instead of 64KB. This means
|
|
|
|
// if the heap size is too small, the VMM would fragment.
|
|
|
|
// TODO(bryan.bernhart@intel.com): Consider having MSAA vs non-MSAA heaps.
|
|
|
|
heapDesc.Alignment = D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT;
|
2019-10-17 17:25:28 +00:00
|
|
|
heapDesc.Flags = mHeapFlags;
|
|
|
|
|
2020-03-31 15:31:56 +00:00
|
|
|
// CreateHeap will implicitly make the created heap resident. We must ensure enough free
|
|
|
|
// memory exists before allocating to avoid an out-of-memory error when overcommitted.
|
2020-04-23 21:50:32 +00:00
|
|
|
DAWN_TRY(mDevice->GetResidencyManager()->EnsureCanAllocate(size, mMemorySegment));
|
2020-03-31 15:31:56 +00:00
|
|
|
|
|
|
|
ComPtr<ID3D12Heap> d3d12Heap;
|
2019-10-28 12:59:36 +00:00
|
|
|
DAWN_TRY(CheckOutOfMemoryHRESULT(
|
2020-03-31 15:31:56 +00:00
|
|
|
mDevice->GetD3D12Device()->CreateHeap(&heapDesc, IID_PPV_ARGS(&d3d12Heap)),
|
2019-10-28 12:59:36 +00:00
|
|
|
"ID3D12Device::CreateHeap"));
|
2019-10-17 17:25:28 +00:00
|
|
|
|
2020-03-31 15:31:56 +00:00
|
|
|
std::unique_ptr<ResourceHeapBase> heapBase =
|
2020-04-23 21:50:32 +00:00
|
|
|
std::make_unique<Heap>(std::move(d3d12Heap), mMemorySegment, size);
|
2020-03-31 15:31:56 +00:00
|
|
|
|
|
|
|
// Calling CreateHeap implicitly calls MakeResident on the new heap. We must track this to
|
|
|
|
// avoid calling MakeResident a second time.
|
|
|
|
mDevice->GetResidencyManager()->TrackResidentAllocation(ToBackend(heapBase.get()));
|
2020-04-11 03:22:33 +00:00
|
|
|
return std::move(heapBase);
|
2019-10-17 17:25:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 21:28:16 +00:00
|
|
|
void HeapAllocator::DeallocateResourceHeap(std::unique_ptr<ResourceHeapBase> heap) {
|
2019-10-17 17:25:28 +00:00
|
|
|
mDevice->ReferenceUntilUnused(static_cast<Heap*>(heap.get())->GetD3D12Heap());
|
|
|
|
}
|
|
|
|
|
2019-10-24 21:28:16 +00:00
|
|
|
}} // namespace dawn_native::d3d12
|