junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 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. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 8 | #include "GrGLGpu.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 10 | #include "builders/GrGLProgramBuilder.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "GrProcessor.h" |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 12 | #include "GrProgramDesc.h" |
egdaniel | 8a4c103 | 2014-09-16 07:18:54 -0700 | [diff] [blame] | 13 | #include "GrGLPathRendering.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 15 | #include "glsl/GrGLSLProgramDataManager.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 16 | #include "SkTSearch.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 17 | |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 18 | #ifdef PROGRAM_CACHE_STATS |
halcanary | 4e44efe | 2016-08-04 10:47:16 -0700 | [diff] [blame] | 19 | // Display program cache usage |
| 20 | static const bool c_DisplayCache{false}; |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 21 | #endif |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 22 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 23 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 24 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 25 | struct GrGLGpu::ProgramCache::Entry { |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 26 | Entry(sk_sp<GrGLProgram> program) |
| 27 | : fProgram(std::move(program)) {} |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 28 | |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 29 | sk_sp<GrGLProgram> fProgram; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 30 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 31 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 32 | GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu) |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 33 | : fMap(kMaxEntries) |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 34 | , fGpu(gpu) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 35 | #ifdef PROGRAM_CACHE_STATS |
| 36 | , fTotalRequests(0) |
| 37 | , fCacheMisses(0) |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 38 | , fHashMisses(0) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 39 | #endif |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 40 | {} |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 41 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 42 | GrGLGpu::ProgramCache::~ProgramCache() { |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 43 | // dump stats |
| 44 | #ifdef PROGRAM_CACHE_STATS |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 45 | if (c_DisplayCache) { |
| 46 | SkDebugf("--- Program Cache ---\n"); |
| 47 | SkDebugf("Total requests: %d\n", fTotalRequests); |
| 48 | SkDebugf("Cache misses: %d\n", fCacheMisses); |
| 49 | SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ? |
| 50 | 100.f * fCacheMisses / fTotalRequests : |
| 51 | 0.f); |
| 52 | int cacheHits = fTotalRequests - fCacheMisses; |
| 53 | SkDebugf("Hash miss %%: %f\n", (cacheHits > 0) ? 100.f * fHashMisses / cacheHits : 0.f); |
| 54 | SkDebugf("---------------------\n"); |
| 55 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 56 | #endif |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 57 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 58 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 59 | void GrGLGpu::ProgramCache::abandon() { |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 60 | #ifdef PROGRAM_CACHE_STATS |
| 61 | fTotalRequests = 0; |
| 62 | fCacheMisses = 0; |
| 63 | fHashMisses = 0; |
| 64 | #endif |
| 65 | } |
| 66 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 67 | GrGLProgram* GrGLGpu::ProgramCache::refProgram(const GrGLGpu* gpu, |
| 68 | const GrPipeline& pipeline, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 69 | const GrPrimitiveProcessor& primProc, |
| 70 | bool isPoints) { |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 71 | #ifdef PROGRAM_CACHE_STATS |
| 72 | ++fTotalRequests; |
| 73 | #endif |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 74 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 75 | // Get GrGLProgramDesc |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 76 | GrProgramDesc desc; |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 77 | if (!GrProgramDesc::Build(&desc, primProc, isPoints, pipeline, *gpu->caps()->shaderCaps())) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 78 | GrCapsDebugf(gpu->caps(), "Failed to gl program descriptor!\n"); |
| 79 | return nullptr; |
| 80 | } |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 81 | desc.finalize(); |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 82 | std::unique_ptr<Entry>* entry = fMap.find(desc); |
| 83 | if (!entry) { |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 84 | // Didn't find an origin-independent version, check with the specific origin |
| 85 | GrSurfaceOrigin origin = pipeline.getRenderTarget()->origin(); |
| 86 | desc.setSurfaceOriginKey(GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(origin)); |
| 87 | desc.finalize(); |
| 88 | entry = fMap.find(desc); |
| 89 | } |
| 90 | if (!entry) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 91 | // We have a cache miss |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 92 | #ifdef PROGRAM_CACHE_STATS |
| 93 | ++fCacheMisses; |
| 94 | #endif |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 95 | GrGLProgram* program = GrGLProgramBuilder::CreateProgram(pipeline, primProc, &desc, fGpu); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 96 | if (nullptr == program) { |
| 97 | return nullptr; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 98 | } |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 99 | entry = fMap.insert(desc, std::unique_ptr<Entry>(new Entry(sk_sp<GrGLProgram>(program)))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 100 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 101 | |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 102 | return SkRef((*entry)->fProgram.get()); |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 103 | } |