1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2020 Google LLC
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "src/gpu/d3d/GrD3DAMDMemoryAllocator.h"
9cb93a386Sopenharmony_ci#include "src/gpu/d3d/GrD3DUtil.h"
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_cisk_sp<GrD3DMemoryAllocator> GrD3DAMDMemoryAllocator::Make(IDXGIAdapter* adapter,
12cb93a386Sopenharmony_ci                                                          ID3D12Device* device) {
13cb93a386Sopenharmony_ci    D3D12MA::ALLOCATOR_DESC allocatorDesc = {};
14cb93a386Sopenharmony_ci    allocatorDesc.pAdapter = adapter;
15cb93a386Sopenharmony_ci    allocatorDesc.pDevice = device;
16cb93a386Sopenharmony_ci    allocatorDesc.Flags = D3D12MA::ALLOCATOR_FLAG_SINGLETHREADED; // faster if we're single-threaded
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ci    D3D12MA::Allocator* allocator;
19cb93a386Sopenharmony_ci    HRESULT hr = D3D12MA::CreateAllocator(&allocatorDesc, &allocator);
20cb93a386Sopenharmony_ci    if (!SUCCEEDED(hr)) {
21cb93a386Sopenharmony_ci        return nullptr;
22cb93a386Sopenharmony_ci    }
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci    return sk_sp<GrD3DMemoryAllocator>(new GrD3DAMDMemoryAllocator(allocator));
25cb93a386Sopenharmony_ci}
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_cigr_cp<ID3D12Resource> GrD3DAMDMemoryAllocator::createResource(
28cb93a386Sopenharmony_ci        D3D12_HEAP_TYPE heapType, const D3D12_RESOURCE_DESC* resourceDesc,
29cb93a386Sopenharmony_ci        D3D12_RESOURCE_STATES initialResourceState, sk_sp<GrD3DAlloc>* allocation,
30cb93a386Sopenharmony_ci        const D3D12_CLEAR_VALUE* clearValue) {
31cb93a386Sopenharmony_ci    D3D12MA::ALLOCATION_DESC allocationDesc = {};
32cb93a386Sopenharmony_ci    allocationDesc.HeapType = heapType;
33cb93a386Sopenharmony_ci    // TODO: Determine flags. For now create new heaps regardless of budget,
34cb93a386Sopenharmony_ci    //       and always suballocate and use CreatePlacedResource.
35cb93a386Sopenharmony_ci    // allocationDesc.Flags = ?
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci    gr_cp<ID3D12Resource> resource;
38cb93a386Sopenharmony_ci    D3D12MA::Allocation* d3d12maAllocation;
39cb93a386Sopenharmony_ci    HRESULT hr = fAllocator->CreateResource(&allocationDesc, resourceDesc,
40cb93a386Sopenharmony_ci                                            initialResourceState, clearValue,
41cb93a386Sopenharmony_ci                                            &d3d12maAllocation, IID_PPV_ARGS(&resource));
42cb93a386Sopenharmony_ci    if (!SUCCEEDED(hr)) {
43cb93a386Sopenharmony_ci        return nullptr;
44cb93a386Sopenharmony_ci    }
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_ci    allocation->reset(new Alloc(d3d12maAllocation));
47cb93a386Sopenharmony_ci    return resource;
48cb93a386Sopenharmony_ci}
49cb93a386Sopenharmony_ci
50cb93a386Sopenharmony_cigr_cp<ID3D12Resource> GrD3DAMDMemoryAllocator::createAliasingResource(
51cb93a386Sopenharmony_ci        sk_sp<GrD3DAlloc>& allocation, uint64_t localOffset,
52cb93a386Sopenharmony_ci        const D3D12_RESOURCE_DESC* resourceDesc, D3D12_RESOURCE_STATES initialResourceState,
53cb93a386Sopenharmony_ci        const D3D12_CLEAR_VALUE* clearValue) {
54cb93a386Sopenharmony_ci    Alloc* alloc = (Alloc*)allocation.get();
55cb93a386Sopenharmony_ci    gr_cp<ID3D12Resource> resource;
56cb93a386Sopenharmony_ci    HRESULT hr = fAllocator->CreateAliasingResource(alloc->fAllocation, localOffset, resourceDesc,
57cb93a386Sopenharmony_ci                                                    initialResourceState, clearValue,
58cb93a386Sopenharmony_ci                                                    IID_PPV_ARGS(&resource));
59cb93a386Sopenharmony_ci    if (!SUCCEEDED(hr)) {
60cb93a386Sopenharmony_ci        return nullptr;
61cb93a386Sopenharmony_ci    }
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_ci    return resource;
64cb93a386Sopenharmony_ci}
65