blob: f7b55439220a51b13bbeb08078beb9de1f86b1f9 [file] [log] [blame]
Greg Daniel02c45902020-03-09 10:58:09 -04001/*
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 Danielb698ad32020-05-04 10:17:22 -040010#include "include/gpu/GrContextOptions.h"
11#include "src/gpu/GrContextPriv.h"
Greg Daniele52c9782020-03-23 14:18:37 -040012#include "src/gpu/d3d/GrD3DCommandList.h"
Greg Daniel02c45902020-03-09 10:58:09 -040013#include "src/gpu/d3d/GrD3DGpu.h"
Greg Danielb698ad32020-05-04 10:17:22 -040014#include "src/gpu/d3d/GrD3DPipelineState.h"
15#include "src/gpu/d3d/GrD3DPipelineStateBuilder.h"
Greg Daniel02c45902020-03-09 10:58:09 -040016
Greg Danielb698ad32020-05-04 10:17:22 -040017GrD3DResourceProvider::GrD3DResourceProvider(GrD3DGpu* gpu)
18 : fGpu(gpu)
Jim Van Verthafd41132020-05-28 06:44:55 -040019 , fCpuDescriptorManager(gpu)
Greg Danielb698ad32020-05-04 10:17:22 -040020 , fPipelineStateCache(new PipelineStateCache(gpu)) {
Greg Daniel85da3362020-03-09 15:18:35 -040021}
22
Greg Daniele52c9782020-03-23 14:18:37 -040023std::unique_ptr<GrD3DDirectCommandList> GrD3DResourceProvider::findOrCreateDirectCommandList() {
Greg Daniel7a5f1fa2020-03-24 14:50:19 -040024 if (fAvailableDirectCommandLists.count()) {
25 std::unique_ptr<GrD3DDirectCommandList> list =
26 std::move(fAvailableDirectCommandLists.back());
27 fAvailableDirectCommandLists.pop_back();
28 return list;
29 }
Greg Daniel83ed2132020-03-24 13:15:33 -040030 return GrD3DDirectCommandList::Make(fGpu->device());
Greg Daniel85da3362020-03-09 15:18:35 -040031}
Greg Daniel7a5f1fa2020-03-24 14:50:19 -040032
33void GrD3DResourceProvider::recycleDirectCommandList(
34 std::unique_ptr<GrD3DDirectCommandList> commandList) {
Jim Van Verthc632aa62020-04-17 16:58:20 -040035 commandList->reset();
Greg Daniel7a5f1fa2020-03-24 14:50:19 -040036 fAvailableDirectCommandLists.push_back(std::move(commandList));
37}
Greg Danielc31edc02020-04-23 11:03:02 -040038
39sk_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 Verth5ce94402020-05-01 14:19:13 -040053
54
55D3D12_CPU_DESCRIPTOR_HANDLE GrD3DResourceProvider::createRenderTargetView(
56 ID3D12Resource* textureResource) {
Jim Van Verthafd41132020-05-28 06:44:55 -040057 return fCpuDescriptorManager.createRenderTargetView(fGpu, textureResource);
Jim Van Verth5ce94402020-05-01 14:19:13 -040058}
59
60void GrD3DResourceProvider::recycleRenderTargetView(D3D12_CPU_DESCRIPTOR_HANDLE* rtvDescriptor) {
Jim Van Verthafd41132020-05-28 06:44:55 -040061 fCpuDescriptorManager.recycleRenderTargetView(rtvDescriptor);
Jim Van Verth73591652020-05-08 12:50:38 -040062}
63
64D3D12_CPU_DESCRIPTOR_HANDLE GrD3DResourceProvider::createDepthStencilView(
65 ID3D12Resource* textureResource) {
Jim Van Verthafd41132020-05-28 06:44:55 -040066 return fCpuDescriptorManager.createDepthStencilView(fGpu, textureResource);
Jim Van Verth73591652020-05-08 12:50:38 -040067}
68
69void GrD3DResourceProvider::recycleDepthStencilView(D3D12_CPU_DESCRIPTOR_HANDLE* dsvDescriptor) {
Jim Van Verthafd41132020-05-28 06:44:55 -040070 fCpuDescriptorManager.recycleDepthStencilView(dsvDescriptor);
71}
72
73D3D12_CPU_DESCRIPTOR_HANDLE GrD3DResourceProvider::createConstantBufferView(
74 ID3D12Resource* bufferResource, size_t offset, size_t size) {
75 return fCpuDescriptorManager.createConstantBufferView(fGpu, bufferResource, offset, size);
76}
77
78D3D12_CPU_DESCRIPTOR_HANDLE GrD3DResourceProvider::createShaderResourceView(
79 ID3D12Resource* resource) {
80 return fCpuDescriptorManager.createShaderResourceView(fGpu, resource);
81}
82
83void GrD3DResourceProvider::recycleConstantOrShaderView(D3D12_CPU_DESCRIPTOR_HANDLE* view) {
84 fCpuDescriptorManager.recycleConstantOrShaderView(view);
85}
86
87D3D12_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
93void GrD3DResourceProvider::recycleSampler(D3D12_CPU_DESCRIPTOR_HANDLE* sampler) {
94 fCpuDescriptorManager.recycleSampler(sampler);
Jim Van Verth5ce94402020-05-01 14:19:13 -040095}
Greg Danielb698ad32020-05-04 10:17:22 -040096
97sk_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
106static const bool c_DisplayMtlPipelineCache{false};
107#endif
108
109struct 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
117GrD3DResourceProvider::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
127GrD3DResourceProvider::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
141sk_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}