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 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 9 | #include "GrProcessor.h" |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 10 | #include "GrRenderTargetPriv.h" // TODO: remove once refPipelineState gets passed stencil settings. |
| 11 | #include "GrStencilSettings.h" |
| 12 | #include "GrVkGpu.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 13 | #include "GrVkPipelineState.h" |
| 14 | #include "GrVkPipelineStateBuilder.h" |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 15 | #include "GrVkResourceProvider.h" |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 16 | #include "SkOpts.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 17 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 18 | #include "glsl/GrGLSLProgramDataManager.h" |
| 19 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 20 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
halcanary | 4e44efe | 2016-08-04 10:47:16 -0700 | [diff] [blame] | 21 | // Display pipeline state cache usage |
| 22 | static const bool c_DisplayVkPipelineCache{false}; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 23 | #endif |
| 24 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 25 | struct GrVkResourceProvider::PipelineStateCache::Entry { |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 26 | Entry(GrVkGpu* gpu, GrVkPipelineState* pipelineState) |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 27 | : fGpu(gpu) |
| 28 | , fPipelineState(pipelineState) {} |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 29 | |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 30 | ~Entry() { |
| 31 | if (fPipelineState) { |
| 32 | fPipelineState->freeGPUResources(fGpu); |
| 33 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 36 | GrVkGpu* fGpu; |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 37 | std::unique_ptr<GrVkPipelineState> fPipelineState; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | GrVkResourceProvider::PipelineStateCache::PipelineStateCache(GrVkGpu* gpu) |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 41 | : fMap(kMaxEntries) |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 42 | , fGpu(gpu) |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 43 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 44 | , fTotalRequests(0) |
| 45 | , fCacheMisses(0) |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 46 | #endif |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 47 | {} |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 48 | |
| 49 | GrVkResourceProvider::PipelineStateCache::~PipelineStateCache() { |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 50 | SkASSERT(0 == fMap.count()); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 51 | // dump stats |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 52 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
| 53 | if (c_DisplayVkPipelineCache) { |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 54 | SkDebugf("--- Pipeline State Cache ---\n"); |
| 55 | SkDebugf("Total requests: %d\n", fTotalRequests); |
| 56 | SkDebugf("Cache misses: %d\n", fCacheMisses); |
| 57 | SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ? |
| 58 | 100.f * fCacheMisses / fTotalRequests : |
| 59 | 0.f); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 60 | SkDebugf("---------------------\n"); |
| 61 | } |
| 62 | #endif |
| 63 | } |
| 64 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 65 | void GrVkResourceProvider::PipelineStateCache::abandon() { |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 66 | fMap.foreach([](std::unique_ptr<Entry>* e) { |
| 67 | (*e)->fPipelineState->abandonGPUResources(); |
| 68 | (*e)->fPipelineState = nullptr; |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 69 | }); |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 70 | fMap.reset(); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void GrVkResourceProvider::PipelineStateCache::release() { |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 74 | fMap.reset(); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 77 | GrVkPipelineState* GrVkResourceProvider::PipelineStateCache::refPipelineState( |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 78 | GrRenderTarget* renderTarget, |
| 79 | GrSurfaceOrigin origin, |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 80 | const GrPrimitiveProcessor& primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 81 | const GrTextureProxy* const primProcProxies[], |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 82 | const GrPipeline& pipeline, |
| 83 | GrPrimitiveType primitiveType, |
Greg Daniel | 99b88e0 | 2018-10-03 15:31:20 -0400 | [diff] [blame] | 84 | VkRenderPass compatibleRenderPass) { |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 85 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 86 | ++fTotalRequests; |
| 87 | #endif |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 88 | GrStencilSettings stencil; |
| 89 | if (pipeline.isStencilEnabled()) { |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 90 | // TODO: attach stencil and create settings during render target flush. |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 91 | SkASSERT(renderTarget->renderTargetPriv().getStencilAttachment()); |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 92 | stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(), |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 93 | renderTarget->renderTargetPriv().numStencilBits()); |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 94 | } |
| 95 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 96 | // Get GrVkProgramDesc |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 97 | GrVkPipelineStateBuilder::Desc desc; |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 98 | if (!GrVkPipelineStateBuilder::Desc::Build(&desc, renderTarget, primProc, pipeline, stencil, |
| 99 | primitiveType, fGpu)) { |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 100 | GrCapsDebugf(fGpu->caps(), "Failed to build vk program descriptor!\n"); |
egdaniel | dd97b85 | 2016-04-28 09:30:39 -0700 | [diff] [blame] | 101 | return nullptr; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 102 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 103 | |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 104 | std::unique_ptr<Entry>* entry = fMap.find(desc); |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 105 | if (!entry) { |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 106 | // Didn't find an origin-independent version, check with the specific origin |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 107 | desc.setSurfaceOriginKey(GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(origin)); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 108 | entry = fMap.find(desc); |
| 109 | } |
| 110 | if (!entry) { |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 111 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 112 | ++fCacheMisses; |
| 113 | #endif |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 114 | GrVkPipelineState* pipelineState(GrVkPipelineStateBuilder::CreatePipelineState( |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 115 | fGpu, renderTarget, origin, primProc, primProcProxies, pipeline, stencil, |
| 116 | primitiveType, &desc, compatibleRenderPass)); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 117 | if (nullptr == pipelineState) { |
| 118 | return nullptr; |
| 119 | } |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 120 | entry = fMap.insert(desc, std::unique_ptr<Entry>(new Entry(fGpu, pipelineState))); |
| 121 | return (*entry)->fPipelineState.get(); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 122 | } |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 123 | return (*entry)->fPipelineState.get(); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 124 | } |