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