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 GrD3DResourceProvider_DEFINED 9 #define GrD3DResourceProvider_DEFINED 10 11 #include "include/gpu/ganesh/d3d/GrD3DTypes.h" 12 #include "include/private/base/SkTArray.h" 13 #include "src/core/SkChecksum.h" 14 #include "src/core/SkLRUCache.h" 15 #include "src/core/SkTHash.h" 16 #include "src/gpu/ganesh/GrProgramDesc.h" 17 #include "src/gpu/ganesh/GrRingBuffer.h" 18 #include "src/gpu/ganesh/d3d/GrD3DCommandSignature.h" 19 #include "src/gpu/ganesh/d3d/GrD3DCpuDescriptorManager.h" 20 #include "src/gpu/ganesh/d3d/GrD3DDescriptorTableManager.h" 21 #include "src/gpu/ganesh/d3d/GrD3DPipeline.h" 22 #include "src/gpu/ganesh/d3d/GrD3DRootSignature.h" 23 #include "src/gpu/ganesh/d3d/GrD3DUtil.h" 24 25 #include <memory> 26 27 class GrD3DCommandSignature; 28 class GrD3DDirectCommandList; 29 class GrD3DGpu; 30 class GrD3DPipelineState; 31 class GrD3DRenderTarget; 32 class GrSamplerState; 33 34 class GrD3DResourceProvider { 35 public: 36 GrD3DResourceProvider(GrD3DGpu*); 37 38 void destroyResources(); 39 40 std::unique_ptr<GrD3DDirectCommandList> findOrCreateDirectCommandList(); 41 42 void recycleDirectCommandList(std::unique_ptr<GrD3DDirectCommandList>); 43 44 sk_sp<GrD3DRootSignature> findOrCreateRootSignature(int numTextureSamplers, 45 int numUAVs = 0); 46 47 sk_sp<GrD3DCommandSignature> findOrCreateCommandSignature(GrD3DCommandSignature::ForIndexed, 48 unsigned int slot); 49 50 GrD3DDescriptorHeap::CPUHandle createRenderTargetView(ID3D12Resource* textureResource); 51 void recycleRenderTargetView(const GrD3DDescriptorHeap::CPUHandle&); 52 53 GrD3DDescriptorHeap::CPUHandle createDepthStencilView(ID3D12Resource* textureResource); 54 void recycleDepthStencilView(const GrD3DDescriptorHeap::CPUHandle&); 55 56 GrD3DDescriptorHeap::CPUHandle createConstantBufferView(ID3D12Resource* bufferResource, 57 size_t offset, 58 size_t size); 59 GrD3DDescriptorHeap::CPUHandle createShaderResourceView(ID3D12Resource* resource, 60 unsigned int mostDetailedMip = 0, 61 unsigned int mipLevels = -1); 62 GrD3DDescriptorHeap::CPUHandle createUnorderedAccessView(ID3D12Resource* resource, 63 unsigned int mipSlice); 64 void recycleShaderView(const GrD3DDescriptorHeap::CPUHandle&); 65 66 D3D12_CPU_DESCRIPTOR_HANDLE findOrCreateCompatibleSampler(const GrSamplerState& params); 67 68 sk_sp<GrD3DDescriptorTable> findOrCreateShaderViewTable( 69 const std::vector<D3D12_CPU_DESCRIPTOR_HANDLE>& shaderViews); 70 sk_sp<GrD3DDescriptorTable> findOrCreateSamplerTable( 71 const std::vector<D3D12_CPU_DESCRIPTOR_HANDLE>& samplers); descriptorTableMgr()72 GrD3DDescriptorTableManager* descriptorTableMgr() { 73 return &fDescriptorTableManager; 74 } 75 76 GrD3DPipelineState* findOrCreateCompatiblePipelineState(GrD3DRenderTarget*, 77 const GrProgramInfo&); 78 79 sk_sp<GrD3DPipeline> findOrCreateMipmapPipeline(); 80 81 D3D12_GPU_VIRTUAL_ADDRESS uploadConstantData(void* data, size_t size); 82 void prepForSubmit(); 83 markPipelineStateUniformsDirty()84 void markPipelineStateUniformsDirty() { fPipelineStateCache->markPipelineStateUniformsDirty(); } 85 86 #if defined(GPU_TEST_UTILS) resetShaderCacheForTesting()87 void resetShaderCacheForTesting() const { fPipelineStateCache->release(); } 88 #endif 89 90 private: 91 #ifdef SK_DEBUG 92 #define GR_PIPELINE_STATE_CACHE_STATS 93 #endif 94 95 class PipelineStateCache : public ::SkNoncopyable { 96 public: 97 PipelineStateCache(GrD3DGpu* gpu); 98 ~PipelineStateCache(); 99 100 void release(); 101 GrD3DPipelineState* refPipelineState(GrD3DRenderTarget*, const GrProgramInfo&); 102 103 void markPipelineStateUniformsDirty(); 104 105 private: 106 struct Entry; 107 108 struct DescHash { operatorDescHash109 uint32_t operator()(const GrProgramDesc& desc) const { 110 return SkChecksum::Hash32(desc.asKey(), desc.keyLength()); 111 } 112 }; 113 114 SkLRUCache<const GrProgramDesc, std::unique_ptr<Entry>, DescHash> fMap; 115 116 GrD3DGpu* fGpu; 117 118 #ifdef GR_PIPELINE_STATE_CACHE_STATS 119 int fTotalRequests; 120 int fCacheMisses; 121 #endif 122 }; 123 124 class DescriptorTableCache : public ::SkNoncopyable { 125 public: DescriptorTableCache(GrD3DGpu * gpu)126 DescriptorTableCache(GrD3DGpu* gpu) : fGpu(gpu), fMap(64) { 127 // Initialize the array we pass into CopyDescriptors for ranges. 128 // At the moment any descriptor we pass into CopyDescriptors is only itself, 129 // not the beginning of a range, so each range size is always 1. 130 for (int i = 0; i < kRangeSizesCount; ++i) { 131 fRangeSizes[i] = 1; 132 } 133 } 134 ~DescriptorTableCache() = default; 135 136 void release(); 137 typedef std::function<sk_sp<GrD3DDescriptorTable>(GrD3DGpu*, unsigned int)> CreateFunc; 138 sk_sp<GrD3DDescriptorTable> findOrCreateDescTable( 139 const std::vector<D3D12_CPU_DESCRIPTOR_HANDLE>&, CreateFunc); 140 141 private: 142 GrD3DGpu* fGpu; 143 144 typedef std::vector<D3D12_CPU_DESCRIPTOR_HANDLE> DescTableKey; 145 typedef sk_sp<GrD3DDescriptorTable> DescTableValue; 146 struct DescTableHash { operatorDescTableHash147 uint32_t operator()(DescTableKey key) const { 148 return SkChecksum::Hash32(key.data(), 149 key.size() * sizeof(D3D12_CPU_DESCRIPTOR_HANDLE)); 150 } 151 }; 152 SkLRUCache<DescTableKey, DescTableValue, DescTableHash> fMap; 153 inline static constexpr int kRangeSizesCount = 8; 154 unsigned int fRangeSizes[kRangeSizesCount]; 155 }; 156 157 GrD3DGpu* fGpu; 158 159 skia_private::STArray<4, std::unique_ptr<GrD3DDirectCommandList>> fAvailableDirectCommandLists; 160 skia_private::STArray<4, sk_sp<GrD3DRootSignature>> fRootSignatures; 161 skia_private::STArray<2, sk_sp<GrD3DCommandSignature>> fCommandSignatures; 162 163 GrD3DCpuDescriptorManager fCpuDescriptorManager; 164 GrD3DDescriptorTableManager fDescriptorTableManager; 165 166 std::unique_ptr<PipelineStateCache> fPipelineStateCache; 167 sk_sp<GrD3DPipeline> fMipmapPipeline; 168 169 skia_private::THashMap<uint32_t, D3D12_CPU_DESCRIPTOR_HANDLE> fSamplers; 170 171 DescriptorTableCache fShaderResourceDescriptorTableCache; 172 DescriptorTableCache fSamplerDescriptorTableCache; 173 }; 174 175 #endif 176