blob: 592605ea27d8808c2ef799ed682cbc4c1102196c [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comf93e7172011-03-31 21:26:24 +00006 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/gl/GrGLGpu.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
Brian Osman172bb442019-09-06 10:16:02 -040010#include "include/gpu/GrContextOptions.h"
11#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrProcessor.h"
13#include "src/gpu/GrProgramDesc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/gl/builders/GrGLProgramBuilder.h"
15#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000016
bsalomon861e1032014-12-16 07:33:49 -080017struct GrGLGpu::ProgramCache::Entry {
Brian Osmaned58e002019-09-06 14:42:43 -040018 Entry(sk_sp<GrGLProgram> program)
19 : fProgram(std::move(program)) {}
20
21 Entry(const GrGLPrecompiledProgram& precompiledProgram)
22 : fPrecompiledProgram(precompiledProgram) {}
halcanary9d524f22016-03-29 09:03:52 -070023
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050024 sk_sp<GrGLProgram> fProgram;
Brian Osmaned58e002019-09-06 14:42:43 -040025 GrGLPrecompiledProgram fPrecompiledProgram;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000026};
junov@google.comf93e7172011-03-31 21:26:24 +000027
bsalomon861e1032014-12-16 07:33:49 -080028GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu)
Brian Osman172bb442019-09-06 10:16:02 -040029 : fMap(gpu->getContext()->priv().options().fRuntimeProgramCacheSize)
Brian Osmane9d9a322019-09-03 13:59:36 -040030 , fGpu(gpu) {}
jvanverth@google.com94878772013-03-12 16:00:54 +000031
Brian Osmane9d9a322019-09-03 13:59:36 -040032GrGLGpu::ProgramCache::~ProgramCache() {}
junov@google.comf93e7172011-03-31 21:26:24 +000033
egdaniel22281c12016-03-23 13:49:40 -070034void GrGLGpu::ProgramCache::abandon() {
Brian Osman0b8bb882019-04-12 11:47:19 -040035 fMap.foreach([](std::unique_ptr<Entry>* e) {
Brian Osmaned58e002019-09-06 14:42:43 -040036 if ((*e)->fProgram) {
37 (*e)->fProgram->abandon();
38 }
Brian Osman0b8bb882019-04-12 11:47:19 -040039 });
40
41 this->reset();
42}
43
44void GrGLGpu::ProgramCache::reset() {
Robert Phillips752e08b2018-06-22 09:48:38 -040045 fMap.reset();
joshualitt8fd844f2015-12-02 13:36:47 -080046}
47
Greg Daniel7a82edf2018-12-04 10:54:34 -050048GrGLProgram* GrGLGpu::ProgramCache::refProgram(GrGLGpu* gpu,
Robert Phillipsd0fe8752019-01-31 14:13:59 -050049 GrRenderTarget* renderTarget,
Robert Phillips901aff02019-10-08 12:32:56 -040050 const GrProgramInfo& programInfo,
Robert Phillips4face832019-10-10 11:58:11 -040051 GrPrimitiveType primitiveType) {
Robert Phillips2579de42019-10-09 09:51:59 -040052 // TODO: can this be unified between GL, Vk and Mtl?
egdaniel0e1853c2016-03-17 11:35:45 -070053 // Get GrGLProgramDesc
egdaniel5d8f69f2016-09-07 07:24:12 -070054 GrProgramDesc desc;
Robert Phillipsf272bea2019-10-17 08:56:16 -040055
Robert Phillips4face832019-10-10 11:58:11 -040056 if (!GrProgramDesc::Build(&desc, renderTarget, programInfo, primitiveType, gpu)) {
egdaniel0e1853c2016-03-17 11:35:45 -070057 GrCapsDebugf(gpu->caps(), "Failed to gl program descriptor!\n");
58 return nullptr;
59 }
Brian Osmane11dfd32019-07-23 10:29:41 -040060
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050061 std::unique_ptr<Entry>* entry = fMap.find(desc);
Brian Osmaned58e002019-09-06 14:42:43 -040062 if (entry && !(*entry)->fProgram) {
63 // We've pre-compiled the GL program, but don't have the GrGLProgram scaffolding
64 const GrGLPrecompiledProgram* precompiledProgram = &((*entry)->fPrecompiledProgram);
65 SkASSERT(precompiledProgram->fProgramID != 0);
Robert Phillips901aff02019-10-08 12:32:56 -040066 GrGLProgram* program = GrGLProgramBuilder::CreateProgram(renderTarget, programInfo,
67 &desc, fGpu,
Brian Osmaned58e002019-09-06 14:42:43 -040068 precompiledProgram);
69 if (nullptr == program) {
70 // Should we purge the program ID from the cache at this point?
71 SkDEBUGFAIL("Couldn't create program from precompiled program");
72 return nullptr;
73 }
74 (*entry)->fProgram.reset(program);
75 } else if (!entry) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000076 // We have a cache miss
Robert Phillips901aff02019-10-08 12:32:56 -040077 GrGLProgram* program = GrGLProgramBuilder::CreateProgram(renderTarget, programInfo,
78 &desc, fGpu);
halcanary96fcdcc2015-08-27 07:41:13 -070079 if (nullptr == program) {
80 return nullptr;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000081 }
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050082 entry = fMap.insert(desc, std::unique_ptr<Entry>(new Entry(sk_sp<GrGLProgram>(program))));
junov@google.comf93e7172011-03-31 21:26:24 +000083 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000084
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050085 return SkRef((*entry)->fProgram.get());
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000086}
Brian Osmaned58e002019-09-06 14:42:43 -040087
88bool GrGLGpu::ProgramCache::precompileShader(const SkData& key, const SkData& data) {
89 GrProgramDesc desc;
90 if (!GrProgramDesc::BuildFromData(&desc, key.data(), key.size())) {
91 return false;
92 }
93
94 std::unique_ptr<Entry>* entry = fMap.find(desc);
95 if (entry) {
96 // We've already seen/compiled this shader
97 return true;
98 }
99
100 GrGLPrecompiledProgram precompiledProgram;
101 if (!GrGLProgramBuilder::PrecompileProgram(&precompiledProgram, fGpu, data)) {
102 return false;
103 }
104
105 fMap.insert(desc, std::unique_ptr<Entry>(new Entry(precompiledProgram)));
106 return true;
107}