Greg Daniel | 02c4590 | 2020-03-09 10:58:09 -0400 | [diff] [blame] | 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 | #include "src/gpu/d3d/GrD3DResourceProvider.h" |
| 9 | |
Greg Daniel | b698ad3 | 2020-05-04 10:17:22 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrContextOptions.h" |
| 11 | #include "src/gpu/GrContextPriv.h" |
Greg Daniel | e52c978 | 2020-03-23 14:18:37 -0400 | [diff] [blame] | 12 | #include "src/gpu/d3d/GrD3DCommandList.h" |
Greg Daniel | 02c4590 | 2020-03-09 10:58:09 -0400 | [diff] [blame] | 13 | #include "src/gpu/d3d/GrD3DGpu.h" |
Greg Daniel | b698ad3 | 2020-05-04 10:17:22 -0400 | [diff] [blame] | 14 | #include "src/gpu/d3d/GrD3DPipelineState.h" |
| 15 | #include "src/gpu/d3d/GrD3DPipelineStateBuilder.h" |
Greg Daniel | 02c4590 | 2020-03-09 10:58:09 -0400 | [diff] [blame] | 16 | |
Greg Daniel | b698ad3 | 2020-05-04 10:17:22 -0400 | [diff] [blame] | 17 | GrD3DResourceProvider::GrD3DResourceProvider(GrD3DGpu* gpu) |
| 18 | : fGpu(gpu) |
Jim Van Verth | afd4113 | 2020-05-28 06:44:55 -0400 | [diff] [blame^] | 19 | , fCpuDescriptorManager(gpu) |
Greg Daniel | b698ad3 | 2020-05-04 10:17:22 -0400 | [diff] [blame] | 20 | , fPipelineStateCache(new PipelineStateCache(gpu)) { |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 21 | } |
| 22 | |
Greg Daniel | e52c978 | 2020-03-23 14:18:37 -0400 | [diff] [blame] | 23 | std::unique_ptr<GrD3DDirectCommandList> GrD3DResourceProvider::findOrCreateDirectCommandList() { |
Greg Daniel | 7a5f1fa | 2020-03-24 14:50:19 -0400 | [diff] [blame] | 24 | if (fAvailableDirectCommandLists.count()) { |
| 25 | std::unique_ptr<GrD3DDirectCommandList> list = |
| 26 | std::move(fAvailableDirectCommandLists.back()); |
| 27 | fAvailableDirectCommandLists.pop_back(); |
| 28 | return list; |
| 29 | } |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 30 | return GrD3DDirectCommandList::Make(fGpu->device()); |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 31 | } |
Greg Daniel | 7a5f1fa | 2020-03-24 14:50:19 -0400 | [diff] [blame] | 32 | |
| 33 | void GrD3DResourceProvider::recycleDirectCommandList( |
| 34 | std::unique_ptr<GrD3DDirectCommandList> commandList) { |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 35 | commandList->reset(); |
Greg Daniel | 7a5f1fa | 2020-03-24 14:50:19 -0400 | [diff] [blame] | 36 | fAvailableDirectCommandLists.push_back(std::move(commandList)); |
| 37 | } |
Greg Daniel | c31edc0 | 2020-04-23 11:03:02 -0400 | [diff] [blame] | 38 | |
| 39 | sk_sp<GrD3DRootSignature> GrD3DResourceProvider::findOrCreateRootSignature(int numTextureSamplers) { |
| 40 | for (int i = 0; i < fRootSignatures.count(); ++i) { |
| 41 | if (fRootSignatures[i]->isCompatible(numTextureSamplers)) { |
| 42 | return fRootSignatures[i]; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | auto rootSig = GrD3DRootSignature::Make(fGpu, numTextureSamplers); |
| 47 | if (!rootSig) { |
| 48 | return nullptr; |
| 49 | } |
| 50 | fRootSignatures.push_back(rootSig); |
| 51 | return rootSig; |
| 52 | } |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame] | 53 | |
| 54 | |
| 55 | D3D12_CPU_DESCRIPTOR_HANDLE GrD3DResourceProvider::createRenderTargetView( |
| 56 | ID3D12Resource* textureResource) { |
Jim Van Verth | afd4113 | 2020-05-28 06:44:55 -0400 | [diff] [blame^] | 57 | return fCpuDescriptorManager.createRenderTargetView(fGpu, textureResource); |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void GrD3DResourceProvider::recycleRenderTargetView(D3D12_CPU_DESCRIPTOR_HANDLE* rtvDescriptor) { |
Jim Van Verth | afd4113 | 2020-05-28 06:44:55 -0400 | [diff] [blame^] | 61 | fCpuDescriptorManager.recycleRenderTargetView(rtvDescriptor); |
Jim Van Verth | 7359165 | 2020-05-08 12:50:38 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | D3D12_CPU_DESCRIPTOR_HANDLE GrD3DResourceProvider::createDepthStencilView( |
| 65 | ID3D12Resource* textureResource) { |
Jim Van Verth | afd4113 | 2020-05-28 06:44:55 -0400 | [diff] [blame^] | 66 | return fCpuDescriptorManager.createDepthStencilView(fGpu, textureResource); |
Jim Van Verth | 7359165 | 2020-05-08 12:50:38 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void GrD3DResourceProvider::recycleDepthStencilView(D3D12_CPU_DESCRIPTOR_HANDLE* dsvDescriptor) { |
Jim Van Verth | afd4113 | 2020-05-28 06:44:55 -0400 | [diff] [blame^] | 70 | fCpuDescriptorManager.recycleDepthStencilView(dsvDescriptor); |
| 71 | } |
| 72 | |
| 73 | D3D12_CPU_DESCRIPTOR_HANDLE GrD3DResourceProvider::createConstantBufferView( |
| 74 | ID3D12Resource* bufferResource, size_t offset, size_t size) { |
| 75 | return fCpuDescriptorManager.createConstantBufferView(fGpu, bufferResource, offset, size); |
| 76 | } |
| 77 | |
| 78 | D3D12_CPU_DESCRIPTOR_HANDLE GrD3DResourceProvider::createShaderResourceView( |
| 79 | ID3D12Resource* resource) { |
| 80 | return fCpuDescriptorManager.createShaderResourceView(fGpu, resource); |
| 81 | } |
| 82 | |
| 83 | void GrD3DResourceProvider::recycleConstantOrShaderView(D3D12_CPU_DESCRIPTOR_HANDLE* view) { |
| 84 | fCpuDescriptorManager.recycleConstantOrShaderView(view); |
| 85 | } |
| 86 | |
| 87 | D3D12_CPU_DESCRIPTOR_HANDLE GrD3DResourceProvider::createSampler( |
| 88 | D3D12_FILTER filter, D3D12_TEXTURE_ADDRESS_MODE addressModeU, |
| 89 | D3D12_TEXTURE_ADDRESS_MODE addressModeV) { |
| 90 | return fCpuDescriptorManager.createSampler(fGpu, filter, addressModeU, addressModeV); |
| 91 | } |
| 92 | |
| 93 | void GrD3DResourceProvider::recycleSampler(D3D12_CPU_DESCRIPTOR_HANDLE* sampler) { |
| 94 | fCpuDescriptorManager.recycleSampler(sampler); |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame] | 95 | } |
Greg Daniel | b698ad3 | 2020-05-04 10:17:22 -0400 | [diff] [blame] | 96 | |
| 97 | sk_sp<GrD3DPipelineState> GrD3DResourceProvider::findOrCreateCompatiblePipelineState( |
| 98 | GrRenderTarget* rt, const GrProgramInfo& info) { |
| 99 | return fPipelineStateCache->refPipelineState(rt, info); |
| 100 | } |
| 101 | |
| 102 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
| 105 | // Display pipeline state cache usage |
| 106 | static const bool c_DisplayMtlPipelineCache{false}; |
| 107 | #endif |
| 108 | |
| 109 | struct GrD3DResourceProvider::PipelineStateCache::Entry { |
| 110 | Entry(GrD3DGpu* gpu, sk_sp<GrD3DPipelineState> pipelineState) |
| 111 | : fGpu(gpu), fPipelineState(std::move(pipelineState)) {} |
| 112 | |
| 113 | GrD3DGpu* fGpu; |
| 114 | sk_sp<GrD3DPipelineState> fPipelineState; |
| 115 | }; |
| 116 | |
| 117 | GrD3DResourceProvider::PipelineStateCache::PipelineStateCache(GrD3DGpu* gpu) |
| 118 | : fMap(gpu->getContext()->priv().options().fRuntimeProgramCacheSize) |
| 119 | , fGpu(gpu) |
| 120 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
| 121 | , fTotalRequests(0) |
| 122 | , fCacheMisses(0) |
| 123 | #endif |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | GrD3DResourceProvider::PipelineStateCache::~PipelineStateCache() { |
| 128 | // dump stats |
| 129 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
| 130 | if (c_DisplayMtlPipelineCache) { |
| 131 | SkDebugf("--- Pipeline State Cache ---\n"); |
| 132 | SkDebugf("Total requests: %d\n", fTotalRequests); |
| 133 | SkDebugf("Cache misses: %d\n", fCacheMisses); |
| 134 | SkDebugf("Cache miss %%: %f\n", |
| 135 | (fTotalRequests > 0) ? 100.f * fCacheMisses / fTotalRequests : 0.f); |
| 136 | SkDebugf("---------------------\n"); |
| 137 | } |
| 138 | #endif |
| 139 | } |
| 140 | |
| 141 | sk_sp<GrD3DPipelineState> GrD3DResourceProvider::PipelineStateCache::refPipelineState( |
| 142 | GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) { |
| 143 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
| 144 | ++fTotalRequests; |
| 145 | #endif |
| 146 | |
| 147 | const GrCaps* caps = fGpu->caps(); |
| 148 | |
| 149 | GrProgramDesc desc = caps->makeDesc(renderTarget, programInfo); |
| 150 | if (!desc.isValid()) { |
| 151 | GrCapsDebugf(fGpu->caps(), "Failed to build mtl program descriptor!\n"); |
| 152 | return nullptr; |
| 153 | } |
| 154 | |
| 155 | std::unique_ptr<Entry>* entry = fMap.find(desc); |
| 156 | if (!entry) { |
| 157 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
| 158 | ++fCacheMisses; |
| 159 | #endif |
| 160 | sk_sp<GrD3DPipelineState> pipelineState = GrD3DPipelineStateBuilder::MakePipelineState( |
| 161 | fGpu, renderTarget, desc, programInfo); |
| 162 | if (!pipelineState) { |
| 163 | return nullptr; |
| 164 | } |
| 165 | entry = fMap.insert(desc, std::unique_ptr<Entry>( |
| 166 | new Entry(fGpu, std::move(pipelineState)))); |
| 167 | return (*entry)->fPipelineState; |
| 168 | } |
| 169 | return (*entry)->fPipelineState; |
| 170 | } |