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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/gl/GrGLGpu.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
Brian Osman | 172bb44 | 2019-09-06 10:16:02 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrContextOptions.h" |
| 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/GrProgramDesc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/gl/builders/GrGLProgramBuilder.h" |
| 15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 16 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 17 | struct GrGLGpu::ProgramCache::Entry { |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 18 | Entry(sk_sp<GrGLProgram> program) |
| 19 | : fProgram(std::move(program)) {} |
| 20 | |
| 21 | Entry(const GrGLPrecompiledProgram& precompiledProgram) |
| 22 | : fPrecompiledProgram(precompiledProgram) {} |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 23 | |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 24 | sk_sp<GrGLProgram> fProgram; |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 25 | GrGLPrecompiledProgram fPrecompiledProgram; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 26 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 27 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 28 | GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu) |
Brian Osman | 172bb44 | 2019-09-06 10:16:02 -0400 | [diff] [blame] | 29 | : fMap(gpu->getContext()->priv().options().fRuntimeProgramCacheSize) |
Brian Osman | e9d9a32 | 2019-09-03 13:59:36 -0400 | [diff] [blame] | 30 | , fGpu(gpu) {} |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 31 | |
Brian Osman | e9d9a32 | 2019-09-03 13:59:36 -0400 | [diff] [blame] | 32 | GrGLGpu::ProgramCache::~ProgramCache() {} |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 33 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 34 | void GrGLGpu::ProgramCache::abandon() { |
Brian Osman | 0b8bb88 | 2019-04-12 11:47:19 -0400 | [diff] [blame] | 35 | fMap.foreach([](std::unique_ptr<Entry>* e) { |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 36 | if ((*e)->fProgram) { |
| 37 | (*e)->fProgram->abandon(); |
| 38 | } |
Brian Osman | 0b8bb88 | 2019-04-12 11:47:19 -0400 | [diff] [blame] | 39 | }); |
| 40 | |
| 41 | this->reset(); |
| 42 | } |
| 43 | |
| 44 | void GrGLGpu::ProgramCache::reset() { |
Robert Phillips | 752e08b | 2018-06-22 09:48:38 -0400 | [diff] [blame] | 45 | fMap.reset(); |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 48 | GrGLProgram* GrGLGpu::ProgramCache::refProgram(GrGLGpu* gpu, |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 49 | GrRenderTarget* renderTarget, |
| 50 | GrSurfaceOrigin origin, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 51 | const GrPrimitiveProcessor& primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 52 | const GrTextureProxy* const primProcProxies[], |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 53 | const GrPipeline& pipeline, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 54 | bool isPoints) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 55 | // Get GrGLProgramDesc |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 56 | GrProgramDesc desc; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 57 | if (!GrProgramDesc::Build(&desc, renderTarget, primProc, isPoints, pipeline, gpu)) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 58 | GrCapsDebugf(gpu->caps(), "Failed to gl program descriptor!\n"); |
| 59 | return nullptr; |
| 60 | } |
Brian Osman | e11dfd3 | 2019-07-23 10:29:41 -0400 | [diff] [blame] | 61 | // If we knew the shader won't depend on origin, we could skip this (and use the same program |
| 62 | // for both origins). Instrumenting all fragment processors would be difficult and error prone. |
| 63 | desc.setSurfaceOriginKey(GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(origin)); |
| 64 | |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 65 | std::unique_ptr<Entry>* entry = fMap.find(desc); |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 66 | if (entry && !(*entry)->fProgram) { |
| 67 | // We've pre-compiled the GL program, but don't have the GrGLProgram scaffolding |
| 68 | const GrGLPrecompiledProgram* precompiledProgram = &((*entry)->fPrecompiledProgram); |
| 69 | SkASSERT(precompiledProgram->fProgramID != 0); |
| 70 | GrGLProgram* program = GrGLProgramBuilder::CreateProgram(renderTarget, origin, |
| 71 | primProc, primProcProxies, |
| 72 | pipeline, &desc, fGpu, |
| 73 | precompiledProgram); |
| 74 | if (nullptr == program) { |
| 75 | // Should we purge the program ID from the cache at this point? |
| 76 | SkDEBUGFAIL("Couldn't create program from precompiled program"); |
| 77 | return nullptr; |
| 78 | } |
| 79 | (*entry)->fProgram.reset(program); |
| 80 | } else if (!entry) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 81 | // We have a cache miss |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 82 | GrGLProgram* program = GrGLProgramBuilder::CreateProgram(renderTarget, origin, |
| 83 | primProc, primProcProxies, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 84 | pipeline, &desc, fGpu); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 85 | if (nullptr == program) { |
| 86 | return nullptr; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 87 | } |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 88 | 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] | 89 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 90 | |
Ethan Nicholas | 1b9924f | 2016-12-15 15:28:42 -0500 | [diff] [blame] | 91 | return SkRef((*entry)->fProgram.get()); |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 92 | } |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 93 | |
| 94 | bool GrGLGpu::ProgramCache::precompileShader(const SkData& key, const SkData& data) { |
| 95 | GrProgramDesc desc; |
| 96 | if (!GrProgramDesc::BuildFromData(&desc, key.data(), key.size())) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | std::unique_ptr<Entry>* entry = fMap.find(desc); |
| 101 | if (entry) { |
| 102 | // We've already seen/compiled this shader |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | GrGLPrecompiledProgram precompiledProgram; |
| 107 | if (!GrGLProgramBuilder::PrecompileProgram(&precompiledProgram, fGpu, data)) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | fMap.insert(desc, std::unique_ptr<Entry>(new Entry(precompiledProgram))); |
| 112 | return true; |
| 113 | } |