• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrD3DCpuDescriptorManager_DEFINED
9 #define GrD3DCpuDescriptorManager_DEFINED
10 
11 #include "src/gpu/d3d/GrD3DDescriptorHeap.h"
12 #include <vector>
13 
14 class GrD3DGpu;
15 
16 class GrD3DCpuDescriptorManager {
17 public:
18     GrD3DCpuDescriptorManager(GrD3DGpu*);
19 
20     GrD3DDescriptorHeap::CPUHandle createRenderTargetView(GrD3DGpu*,
21                                                           ID3D12Resource* textureResource);
22     void recycleRenderTargetView(const GrD3DDescriptorHeap::CPUHandle&);
23 
24     GrD3DDescriptorHeap::CPUHandle createDepthStencilView(GrD3DGpu*,
25                                                           ID3D12Resource* textureResource);
26     void recycleDepthStencilView(const GrD3DDescriptorHeap::CPUHandle&);
27 
28     GrD3DDescriptorHeap::CPUHandle createConstantBufferView(GrD3DGpu*,
29                                                             ID3D12Resource* bufferResource,
30                                                             size_t offset,
31                                                             size_t size);
32     GrD3DDescriptorHeap::CPUHandle createShaderResourceView(GrD3DGpu*,
33                                                             ID3D12Resource* resource,
34                                                             unsigned int mostDetailedMip,
35                                                             unsigned int mipLevels);
36     GrD3DDescriptorHeap::CPUHandle createUnorderedAccessView(GrD3DGpu*,
37                                                              ID3D12Resource* resource,
38                                                              unsigned int mipSlice);
39     void recycleShaderView(const GrD3DDescriptorHeap::CPUHandle&);
40 
41     GrD3DDescriptorHeap::CPUHandle createSampler(GrD3DGpu*,
42                                                  D3D12_FILTER filter,
43                                                  float maxLOD,
44                                                  D3D12_TEXTURE_ADDRESS_MODE addressModeU,
45                                                  D3D12_TEXTURE_ADDRESS_MODE addressModeV);
46     void recycleSampler(const GrD3DDescriptorHeap::CPUHandle&);
47 
48 private:
49     class Heap {
50     public:
51         static std::unique_ptr<Heap> Make(GrD3DGpu* gpu, D3D12_DESCRIPTOR_HEAP_TYPE type,
52                                           unsigned int numDescriptors);
53 
54         GrD3DDescriptorHeap::CPUHandle allocateCPUHandle();
55         void freeCPUHandle(const GrD3DDescriptorHeap::CPUHandle&);
ownsHandle(const GrD3DDescriptorHeap::CPUHandle & handle)56         bool ownsHandle(const GrD3DDescriptorHeap::CPUHandle& handle) {
57             return handle.fHeapID == fHeap->uniqueID();
58         }
59 
canAllocate()60         bool canAllocate() { return fFreeCount > 0; }
61 
62     private:
Heap(std::unique_ptr<GrD3DDescriptorHeap> & heap,unsigned int numDescriptors)63         Heap(std::unique_ptr<GrD3DDescriptorHeap>& heap, unsigned int numDescriptors)
64             : fHeap(std::move(heap))
65             , fFreeBlocks(numDescriptors)
66             , fFreeCount(numDescriptors) {
67             for (unsigned int i = 0; i < numDescriptors; ++i) {
68                 fFreeBlocks.set(i);
69             }
70         }
71 
72         std::unique_ptr<GrD3DDescriptorHeap> fHeap;
73         SkBitSet fFreeBlocks;
74         unsigned int fFreeCount;
75     };
76 
77     class HeapPool {
78     public:
79         HeapPool(GrD3DGpu*, D3D12_DESCRIPTOR_HEAP_TYPE);
80 
81         GrD3DDescriptorHeap::CPUHandle allocateHandle(GrD3DGpu*);
82         void releaseHandle(const GrD3DDescriptorHeap::CPUHandle&);
83 
84     private:
85         std::vector<std::unique_ptr<Heap>> fDescriptorHeaps;
86         int fMaxAvailableDescriptors;
87         D3D12_DESCRIPTOR_HEAP_TYPE fHeapType;
88     };
89 
90     HeapPool fRTVDescriptorPool;
91     HeapPool fDSVDescriptorPool;
92     HeapPool fShaderViewDescriptorPool;
93     HeapPool fSamplerDescriptorPool;
94 };
95 
96 #endif
97