1 /*
2  * Copyright 2020 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef GrD3DGpuDescriptorTableManager_DEFINED
9 #define GrD3DGpuDescriptorTableManager_DEFINED
10 
11 #include "src/gpu/d3d/GrD3DDescriptorHeap.h"
12 
13 class GrD3DCommandList;
14 class GrD3DDirectCommandList;
15 class GrD3DGpu;
16 
17 class GrD3DDescriptorTable : public SkRefCnt {
18 public:
GrD3DDescriptorTable(D3D12_CPU_DESCRIPTOR_HANDLE baseCPU,D3D12_GPU_DESCRIPTOR_HANDLE baseGPU,D3D12_DESCRIPTOR_HEAP_TYPE type)19     GrD3DDescriptorTable(D3D12_CPU_DESCRIPTOR_HANDLE baseCPU, D3D12_GPU_DESCRIPTOR_HANDLE baseGPU,
20                          D3D12_DESCRIPTOR_HEAP_TYPE type)
21         : fDescriptorTableCpuStart(baseCPU)
22         , fDescriptorTableGpuStart(baseGPU)
23         , fType(type) {}
24 
baseCpuDescriptorPtr()25     const D3D12_CPU_DESCRIPTOR_HANDLE* baseCpuDescriptorPtr() {
26         return &fDescriptorTableCpuStart;
27     }
28 
baseGpuDescriptor()29     const D3D12_GPU_DESCRIPTOR_HANDLE baseGpuDescriptor() {
30         return fDescriptorTableGpuStart;
31     }
32 
type()33     D3D12_DESCRIPTOR_HEAP_TYPE type() const { return fType; }
34 
35 private:
36     D3D12_CPU_DESCRIPTOR_HANDLE fDescriptorTableCpuStart;
37     D3D12_GPU_DESCRIPTOR_HANDLE fDescriptorTableGpuStart;
38     D3D12_DESCRIPTOR_HEAP_TYPE fType;
39 };
40 
41 class GrD3DDescriptorTableManager {
42 public:
43     GrD3DDescriptorTableManager(GrD3DGpu*);
44 
45     sk_sp<GrD3DDescriptorTable> createShaderViewTable(GrD3DGpu*, unsigned int count);
46     sk_sp<GrD3DDescriptorTable> createSamplerTable(GrD3DGpu*, unsigned int count);
47 
48     void prepForSubmit(GrD3DGpu* gpu);
49 
50 private:
51     class Heap : public GrRecycledResource {
52     public:
53         static sk_sp<Heap> Make(GrD3DGpu* gpu, D3D12_DESCRIPTOR_HEAP_TYPE type,
54                                 unsigned int numDescriptors);
55 
56         sk_sp<GrD3DDescriptorTable> allocateTable(unsigned int count);
canAllocate(unsigned int count)57         bool canAllocate(unsigned int count) const {
58             return (fDescriptorCount - fNextAvailable) >= count;
59         }
d3dDescriptorHeap()60         ID3D12DescriptorHeap* d3dDescriptorHeap() const { return fHeap->descriptorHeap(); }
type()61         D3D12_DESCRIPTOR_HEAP_TYPE type() const { return fType; }
descriptorCount()62         unsigned int descriptorCount() { return fDescriptorCount; }
used()63         bool used() { return fNextAvailable > 0; }
64 
reset()65         void reset() {
66             fNextAvailable = 0;
67         }
68 
69     private:
Heap(GrD3DGpu * gpu,std::unique_ptr<GrD3DDescriptorHeap> & heap,D3D12_DESCRIPTOR_HEAP_TYPE type,unsigned int descriptorCount)70         Heap(GrD3DGpu* gpu, std::unique_ptr<GrD3DDescriptorHeap>& heap,
71              D3D12_DESCRIPTOR_HEAP_TYPE type, unsigned int descriptorCount)
72             : INHERITED()
73             , fGpu(gpu)
74             , fHeap(std::move(heap))
75             , fType(type)
76             , fDescriptorCount(descriptorCount)
77             , fNextAvailable(0) {
78         }
79 
freeGPUData()80         void freeGPUData() const override {}
81         void onRecycle() const override;
82 
83 #ifdef SK_TRACE_MANAGED_RESOURCES
dumpInfo()84         void dumpInfo() const override {
85             SkDebugf("GrD3DDescriptorTable::Heap: %d (%d refs)\n", fHeap.get(), this->getRefCnt());
86         }
87 #endif
88 
89         GrD3DGpu* fGpu;
90         std::unique_ptr<GrD3DDescriptorHeap> fHeap;
91         D3D12_DESCRIPTOR_HEAP_TYPE fType;
92         unsigned int fDescriptorCount;
93         unsigned int fNextAvailable;
94 
95         using INHERITED = GrRecycledResource;
96     };
97 
98     class HeapPool {
99     public:
100         HeapPool(GrD3DGpu*, D3D12_DESCRIPTOR_HEAP_TYPE);
101 
102         sk_sp<GrD3DDescriptorTable> allocateTable(GrD3DGpu*, unsigned int count);
103         void recycle(sk_sp<Heap>);
104         sk_sp<Heap>& currentDescriptorHeap();
105         void prepForSubmit(GrD3DGpu* gpu);
106 
107     private:
108         static constexpr int kInitialHeapDescriptorCount = 256;
109 
110         std::vector<sk_sp<Heap>> fDescriptorHeaps;
111         D3D12_DESCRIPTOR_HEAP_TYPE fHeapType;
112         unsigned int fCurrentHeapDescriptorCount;
113     };
114 
115     void setHeaps(GrD3DGpu*);
116     void recycle(Heap*);
117 
118     HeapPool fShaderViewDescriptorPool;
119     HeapPool fSamplerDescriptorPool;
120 };
121 
122 #endif
123