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