1 /*
2 * Copyright 2016 Google Inc.
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 "include/gpu/ganesh/GrContextOptions.h"
9 #include "include/gpu/ganesh/GrDirectContext.h"
10 #include "include/private/base/SkAssert.h"
11 #include "include/private/base/SkDebug.h"
12 #include "include/private/gpu/ganesh/GrTypesPriv.h"
13 #include "include/private/gpu/vk/SkiaVulkan.h"
14 #include "src/core/SkLRUCache.h"
15 #include "src/gpu/ganesh/GrAttachment.h"
16 #include "src/gpu/ganesh/GrCaps.h"
17 #include "src/gpu/ganesh/GrDirectContextPriv.h"
18 #include "src/gpu/ganesh/GrProgramDesc.h"
19 #include "src/gpu/ganesh/GrProgramInfo.h"
20 #include "src/gpu/ganesh/GrRenderTarget.h"
21 #include "src/gpu/ganesh/vk/GrVkGpu.h"
22 #include "src/gpu/ganesh/vk/GrVkPipelineState.h"
23 #include "src/gpu/ganesh/vk/GrVkPipelineStateBuilder.h"
24 #include "src/gpu/ganesh/vk/GrVkResourceProvider.h"
25
26 #include <memory>
27
28 #ifdef SK_DEBUG
29 // Display pipeline state cache usage
30 static const bool c_DisplayVkPipelineCache{false};
31 #endif
32
33 struct GrVkResourceProvider::PipelineStateCache::Entry {
EntryGrVkResourceProvider::PipelineStateCache::Entry34 Entry(GrVkGpu* gpu, GrVkPipelineState* pipelineState)
35 : fGpu(gpu)
36 , fPipelineState(pipelineState) {}
37
~EntryGrVkResourceProvider::PipelineStateCache::Entry38 ~Entry() {
39 if (fPipelineState) {
40 fPipelineState->freeGPUResources(fGpu);
41 }
42 }
43
44 GrVkGpu* fGpu;
45 std::unique_ptr<GrVkPipelineState> fPipelineState;
46 };
47
PipelineStateCache(GrVkGpu * gpu)48 GrVkResourceProvider::PipelineStateCache::PipelineStateCache(GrVkGpu* gpu)
49 : fMap(gpu->getContext()->priv().options().fRuntimeProgramCacheSize)
50 , fGpu(gpu) {
51 }
52
~PipelineStateCache()53 GrVkResourceProvider::PipelineStateCache::~PipelineStateCache() {
54 SkASSERT(0 == fMap.count());
55 // dump stats
56 #ifdef SK_DEBUG
57 if (c_DisplayVkPipelineCache) {
58 using CacheResult = Stats::ProgramCacheResult;
59
60 int misses = fStats.numInlineProgramCacheResult(CacheResult::kMiss) +
61 fStats.numPreProgramCacheResult(CacheResult::kMiss);
62
63 int total = misses + fStats.numInlineProgramCacheResult(CacheResult::kHit) +
64 fStats.numPreProgramCacheResult(CacheResult::kHit);
65
66 SkDebugf("--- Pipeline State Cache ---\n");
67 SkDebugf("Total requests: %d\n", total);
68 SkDebugf("Cache misses: %d\n", misses);
69 SkDebugf("Cache miss %%: %f\n", (total > 0) ? 100.f * misses / total : 0.0f);
70 }
71 #endif
72 }
73
release()74 void GrVkResourceProvider::PipelineStateCache::release() {
75 fMap.reset();
76 }
77
findOrCreatePipelineState(GrRenderTarget * renderTarget,const GrProgramInfo & programInfo,VkRenderPass compatibleRenderPass,bool overrideSubpassForResolveLoad)78 GrVkPipelineState* GrVkResourceProvider::PipelineStateCache::findOrCreatePipelineState(
79 GrRenderTarget* renderTarget,
80 const GrProgramInfo& programInfo,
81 VkRenderPass compatibleRenderPass,
82 bool overrideSubpassForResolveLoad) {
83 #ifdef SK_DEBUG
84 if (programInfo.isStencilEnabled()) {
85 SkASSERT(renderTarget->getStencilAttachment(programInfo.numSamples() > 1));
86 SkASSERT(renderTarget->numStencilBits(programInfo.numSamples() > 1) == 8);
87 SkASSERT(renderTarget->getStencilAttachment(programInfo.numSamples() > 1)->numSamples() ==
88 programInfo.numSamples());
89 }
90 #endif
91
92 auto flags = overrideSubpassForResolveLoad
93 ? GrCaps::ProgramDescOverrideFlags::kVulkanHasResolveLoadSubpass
94 : GrCaps::ProgramDescOverrideFlags::kNone;
95
96 GrProgramDesc desc = fGpu->caps()->makeDesc(renderTarget, programInfo, flags);
97 if (!desc.isValid()) {
98 GrCapsDebugf(fGpu->caps(), "Failed to build vk program descriptor!\n");
99 return nullptr;
100 }
101
102 Stats::ProgramCacheResult stat;
103 auto tmp = this->findOrCreatePipelineStateImpl(desc, programInfo, compatibleRenderPass,
104 overrideSubpassForResolveLoad, &stat);
105 if (!tmp) {
106 fStats.incNumInlineCompilationFailures();
107 } else {
108 fStats.incNumInlineProgramCacheResult(stat);
109 }
110
111 return tmp;
112 }
113
findOrCreatePipelineStateImpl(const GrProgramDesc & desc,const GrProgramInfo & programInfo,VkRenderPass compatibleRenderPass,bool overrideSubpassForResolveLoad,Stats::ProgramCacheResult * stat)114 GrVkPipelineState* GrVkResourceProvider::PipelineStateCache::findOrCreatePipelineStateImpl(
115 const GrProgramDesc& desc,
116 const GrProgramInfo& programInfo,
117 VkRenderPass compatibleRenderPass,
118 bool overrideSubpassForResolveLoad,
119 Stats::ProgramCacheResult* stat) {
120 if (stat) {
121 *stat = Stats::ProgramCacheResult::kHit;
122 }
123
124 std::unique_ptr<Entry>* entry = fMap.find(desc);
125 if (!entry) {
126 if (stat) {
127 *stat = Stats::ProgramCacheResult::kMiss;
128 }
129 GrVkPipelineState* pipelineState(GrVkPipelineStateBuilder::CreatePipelineState(
130 fGpu, desc, programInfo, compatibleRenderPass, overrideSubpassForResolveLoad));
131 if (!pipelineState) {
132 return nullptr;
133 }
134 entry = fMap.insert(desc, std::make_unique<Entry>(fGpu, pipelineState));
135 return (*entry)->fPipelineState.get();
136 }
137 return (*entry)->fPipelineState.get();
138 }
139