egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 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 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 8 | |
Brian Osman | 172bb44 | 2019-09-06 10:16:02 -0400 | [diff] [blame] | 9 | #include "include/gpu/GrContextOptions.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/core/SkOpts.h" |
Brian Osman | 172bb44 | 2019-09-06 10:16:02 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrProcessor.h" |
| 13 | #include "src/gpu/GrRenderTargetPriv.h" |
| 14 | #include "src/gpu/GrStencilSettings.h" |
| 15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 16 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 17 | #include "src/gpu/vk/GrVkGpu.h" |
| 18 | #include "src/gpu/vk/GrVkPipelineState.h" |
| 19 | #include "src/gpu/vk/GrVkPipelineStateBuilder.h" |
| 20 | #include "src/gpu/vk/GrVkResourceProvider.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 21 | |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame^] | 22 | #ifdef SK_DEBUG |
halcanary | 4e44efe | 2016-08-04 10:47:16 -0700 | [diff] [blame] | 23 | // Display pipeline state cache usage |
| 24 | static const bool c_DisplayVkPipelineCache{false}; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 25 | #endif |
| 26 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 27 | struct GrVkResourceProvider::PipelineStateCache::Entry { |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 28 | Entry(GrVkGpu* gpu, GrVkPipelineState* pipelineState) |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 29 | : fGpu(gpu) |
| 30 | , fPipelineState(pipelineState) {} |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 31 | |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 32 | ~Entry() { |
| 33 | if (fPipelineState) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 34 | fPipelineState->freeGPUResources(); |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 35 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 38 | GrVkGpu* fGpu; |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 39 | std::unique_ptr<GrVkPipelineState> fPipelineState; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | GrVkResourceProvider::PipelineStateCache::PipelineStateCache(GrVkGpu* gpu) |
Brian Osman | 172bb44 | 2019-09-06 10:16:02 -0400 | [diff] [blame] | 43 | : fMap(gpu->getContext()->priv().options().fRuntimeProgramCacheSize) |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame^] | 44 | , fGpu(gpu) { |
| 45 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 46 | |
| 47 | GrVkResourceProvider::PipelineStateCache::~PipelineStateCache() { |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 48 | SkASSERT(0 == fMap.count()); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 49 | // dump stats |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame^] | 50 | #ifdef SK_DEBUG |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 51 | if (c_DisplayVkPipelineCache) { |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame^] | 52 | using CacheResult = GrGpu::Stats::ProgramCacheResult; |
| 53 | |
| 54 | int misses = fGpu->stats()->numInlineProgramCacheResult(CacheResult::kMiss) + |
| 55 | fGpu->stats()->numPreProgramCacheResult(CacheResult::kMiss); |
| 56 | |
| 57 | int total = misses + fGpu->stats()->numInlineProgramCacheResult(CacheResult::kHit) + |
| 58 | fGpu->stats()->numPreProgramCacheResult(CacheResult::kHit); |
| 59 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 60 | SkDebugf("--- Pipeline State Cache ---\n"); |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame^] | 61 | SkDebugf("Total requests: %d\n", total); |
| 62 | SkDebugf("Cache misses: %d\n", misses); |
| 63 | SkDebugf("Cache miss %%: %f\n", (total > 0) ? 100.f * misses / total : 0.0f); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 64 | } |
| 65 | #endif |
| 66 | } |
| 67 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 68 | void GrVkResourceProvider::PipelineStateCache::release() { |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 69 | fMap.reset(); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Stephen White | b185785 | 2020-02-07 15:33:23 +0000 | [diff] [blame] | 72 | GrVkPipelineState* GrVkResourceProvider::PipelineStateCache::findOrCreatePipelineState( |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 73 | GrRenderTarget* renderTarget, |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 74 | const GrProgramInfo& programInfo, |
Greg Daniel | 99b88e0 | 2018-10-03 15:31:20 -0400 | [diff] [blame] | 75 | VkRenderPass compatibleRenderPass) { |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 76 | #ifdef SK_DEBUG |
Robert Phillips | 6c2aa7a | 2019-10-17 19:06:39 +0000 | [diff] [blame] | 77 | if (programInfo.pipeline().isStencilEnabled()) { |
Robert Phillips | 6c2aa7a | 2019-10-17 19:06:39 +0000 | [diff] [blame] | 78 | SkASSERT(renderTarget->renderTargetPriv().getStencilAttachment()); |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 79 | SkASSERT(renderTarget->renderTargetPriv().numStencilBits() == 8); |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame^] | 80 | SkASSERT(renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() == |
| 81 | programInfo.numStencilSamples()); |
Robert Phillips | 6c2aa7a | 2019-10-17 19:06:39 +0000 | [diff] [blame] | 82 | } |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 83 | #endif |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 84 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 85 | GrProgramDesc desc = fGpu->caps()->makeDesc(renderTarget, programInfo); |
| 86 | if (!desc.isValid()) { |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 87 | GrCapsDebugf(fGpu->caps(), "Failed to build vk program descriptor!\n"); |
egdaniel | dd97b85 | 2016-04-28 09:30:39 -0700 | [diff] [blame] | 88 | return nullptr; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 89 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 90 | |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame^] | 91 | GrGpu::Stats::ProgramCacheResult stat; |
| 92 | auto tmp = this->findOrCreatePipelineState(renderTarget, desc, programInfo, |
| 93 | compatibleRenderPass, &stat); |
| 94 | if (!tmp) { |
| 95 | fGpu->stats()->incNumInlineCompilationFailures(); |
| 96 | } else { |
| 97 | fGpu->stats()->incNumInlineProgramCacheResult(stat); |
| 98 | } |
| 99 | |
| 100 | return tmp; |
Robert Phillips | 979b223 | 2020-02-20 10:47:29 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame^] | 103 | GrVkPipelineState* GrVkResourceProvider::PipelineStateCache::findOrCreatePipelineState( |
Robert Phillips | 979b223 | 2020-02-20 10:47:29 -0500 | [diff] [blame] | 104 | GrRenderTarget* renderTarget, |
| 105 | const GrProgramDesc& desc, |
| 106 | const GrProgramInfo& programInfo, |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame^] | 107 | VkRenderPass compatibleRenderPass, |
| 108 | GrGpu::Stats::ProgramCacheResult* stat) { |
| 109 | if (stat) { |
| 110 | *stat = GrGpu::Stats::ProgramCacheResult::kHit; |
| 111 | } |
| 112 | |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 113 | std::unique_ptr<Entry>* entry = fMap.find(desc); |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 114 | if (!entry) { |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame^] | 115 | if (stat) { |
| 116 | *stat = GrGpu::Stats::ProgramCacheResult::kMiss; |
| 117 | } |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 118 | GrVkPipelineState* pipelineState(GrVkPipelineStateBuilder::CreatePipelineState( |
Stephen White | b185785 | 2020-02-07 15:33:23 +0000 | [diff] [blame] | 119 | fGpu, renderTarget, desc, programInfo, compatibleRenderPass)); |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 120 | if (!pipelineState) { |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 121 | return nullptr; |
| 122 | } |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 123 | entry = fMap.insert(desc, std::unique_ptr<Entry>(new Entry(fGpu, pipelineState))); |
| 124 | return (*entry)->fPipelineState.get(); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 125 | } |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 126 | return (*entry)->fPipelineState.get(); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 127 | } |