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 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 22 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
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) { |
| 34 | fPipelineState->freeGPUResources(fGpu); |
| 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) |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 44 | , fGpu(gpu) |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 45 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 46 | , fTotalRequests(0) |
| 47 | , fCacheMisses(0) |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 48 | #endif |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 49 | {} |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 50 | |
| 51 | GrVkResourceProvider::PipelineStateCache::~PipelineStateCache() { |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 52 | SkASSERT(0 == fMap.count()); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 53 | // dump stats |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 54 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
| 55 | if (c_DisplayVkPipelineCache) { |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 56 | SkDebugf("--- Pipeline State Cache ---\n"); |
| 57 | SkDebugf("Total requests: %d\n", fTotalRequests); |
| 58 | SkDebugf("Cache misses: %d\n", fCacheMisses); |
| 59 | SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ? |
| 60 | 100.f * fCacheMisses / fTotalRequests : |
| 61 | 0.f); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 62 | SkDebugf("---------------------\n"); |
| 63 | } |
| 64 | #endif |
| 65 | } |
| 66 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 67 | void GrVkResourceProvider::PipelineStateCache::release() { |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 68 | fMap.reset(); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Stephen White | b185785 | 2020-02-07 15:33:23 +0000 | [diff] [blame^] | 71 | GrVkPipelineState* GrVkResourceProvider::PipelineStateCache::findOrCreatePipelineState( |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 72 | GrRenderTarget* renderTarget, |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 73 | const GrProgramInfo& programInfo, |
Greg Daniel | 99b88e0 | 2018-10-03 15:31:20 -0400 | [diff] [blame] | 74 | VkRenderPass compatibleRenderPass) { |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 75 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 76 | ++fTotalRequests; |
| 77 | #endif |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 78 | |
| 79 | #ifdef SK_DEBUG |
Robert Phillips | 6c2aa7a | 2019-10-17 19:06:39 +0000 | [diff] [blame] | 80 | if (programInfo.pipeline().isStencilEnabled()) { |
Robert Phillips | 6c2aa7a | 2019-10-17 19:06:39 +0000 | [diff] [blame] | 81 | SkASSERT(renderTarget->renderTargetPriv().getStencilAttachment()); |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 82 | SkASSERT(renderTarget->renderTargetPriv().numStencilBits() == 8); |
Robert Phillips | 6c2aa7a | 2019-10-17 19:06:39 +0000 | [diff] [blame] | 83 | } |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 84 | #endif |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 85 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 86 | GrProgramDesc desc = fGpu->caps()->makeDesc(renderTarget, programInfo); |
| 87 | if (!desc.isValid()) { |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 88 | GrCapsDebugf(fGpu->caps(), "Failed to build vk program descriptor!\n"); |
egdaniel | dd97b85 | 2016-04-28 09:30:39 -0700 | [diff] [blame] | 89 | return nullptr; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 90 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 91 | |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 92 | std::unique_ptr<Entry>* entry = fMap.find(desc); |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 93 | if (!entry) { |
| 94 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 95 | ++fCacheMisses; |
| 96 | #endif |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 97 | GrVkPipelineState* pipelineState(GrVkPipelineStateBuilder::CreatePipelineState( |
Stephen White | b185785 | 2020-02-07 15:33:23 +0000 | [diff] [blame^] | 98 | fGpu, renderTarget, desc, programInfo, compatibleRenderPass)); |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 99 | if (!pipelineState) { |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 100 | return nullptr; |
| 101 | } |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 102 | entry = fMap.insert(desc, std::unique_ptr<Entry>(new Entry(fGpu, pipelineState))); |
| 103 | return (*entry)->fPipelineState.get(); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 104 | } |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 105 | return (*entry)->fPipelineState.get(); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 106 | } |