blob: c1ca16096a6f0a166c5b6328ff69930927d20b48 [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
jvanverth39edf762014-12-22 11:44:19 -08008#include "GrGLGpu.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
joshualitt47bb3822014-10-07 16:43:25 -070010#include "builders/GrGLProgramBuilder.h"
joshualittb0a8a372014-09-23 09:50:21 -070011#include "GrProcessor.h"
egdaniel5d8f69f2016-09-07 07:24:12 -070012#include "GrProgramDesc.h"
egdaniel8a4c1032014-09-16 07:18:54 -070013#include "GrGLPathRendering.h"
egdaniel64c47282015-11-13 06:54:19 -080014#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070015#include "glsl/GrGLSLProgramDataManager.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000016#include "SkTSearch.h"
junov@google.comf93e7172011-03-31 21:26:24 +000017
jvanverth@google.coma2f4b152013-09-16 20:00:46 +000018#ifdef PROGRAM_CACHE_STATS
halcanary4e44efe2016-08-04 10:47:16 -070019// Display program cache usage
20static const bool c_DisplayCache{false};
jvanverth@google.coma2f4b152013-09-16 20:00:46 +000021#endif
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000022
egdaniel018fb622015-10-28 07:26:40 -070023typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000024
bsalomon861e1032014-12-16 07:33:49 -080025struct GrGLGpu::ProgramCache::Entry {
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050026 Entry(sk_sp<GrGLProgram> program)
27 : fProgram(std::move(program)) {}
halcanary9d524f22016-03-29 09:03:52 -070028
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050029 sk_sp<GrGLProgram> fProgram;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000030};
junov@google.comf93e7172011-03-31 21:26:24 +000031
bsalomon861e1032014-12-16 07:33:49 -080032GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu)
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050033 : fMap(kMaxEntries)
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000034 , fGpu(gpu)
jvanverth@google.com94878772013-03-12 16:00:54 +000035#ifdef PROGRAM_CACHE_STATS
36 , fTotalRequests(0)
37 , fCacheMisses(0)
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000038 , fHashMisses(0)
jvanverth@google.com94878772013-03-12 16:00:54 +000039#endif
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050040{}
jvanverth@google.com94878772013-03-12 16:00:54 +000041
bsalomon861e1032014-12-16 07:33:49 -080042GrGLGpu::ProgramCache::~ProgramCache() {
jvanverth@google.com94878772013-03-12 16:00:54 +000043 // dump stats
44#ifdef PROGRAM_CACHE_STATS
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000045 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.com94878772013-03-12 16:00:54 +000056#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000057}
junov@google.comf93e7172011-03-31 21:26:24 +000058
egdaniel22281c12016-03-23 13:49:40 -070059void GrGLGpu::ProgramCache::abandon() {
joshualitt8fd844f2015-12-02 13:36:47 -080060#ifdef PROGRAM_CACHE_STATS
61 fTotalRequests = 0;
62 fCacheMisses = 0;
63 fHashMisses = 0;
64#endif
65}
66
egdaniel0e1853c2016-03-17 11:35:45 -070067GrGLProgram* GrGLGpu::ProgramCache::refProgram(const GrGLGpu* gpu,
68 const GrPipeline& pipeline,
bsalomon2eda5b32016-09-21 10:53:24 -070069 const GrPrimitiveProcessor& primProc,
70 bool isPoints) {
jvanverth@google.com94878772013-03-12 16:00:54 +000071#ifdef PROGRAM_CACHE_STATS
72 ++fTotalRequests;
73#endif
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000074
egdaniel0e1853c2016-03-17 11:35:45 -070075 // Get GrGLProgramDesc
egdaniel5d8f69f2016-09-07 07:24:12 -070076 GrProgramDesc desc;
Brian Salomon1edc5b92016-11-29 13:43:46 -050077 if (!GrProgramDesc::Build(&desc, primProc, isPoints, pipeline, *gpu->caps()->shaderCaps())) {
egdaniel0e1853c2016-03-17 11:35:45 -070078 GrCapsDebugf(gpu->caps(), "Failed to gl program descriptor!\n");
79 return nullptr;
80 }
egdaniel5d8f69f2016-09-07 07:24:12 -070081 desc.finalize();
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050082 std::unique_ptr<Entry>* entry = fMap.find(desc);
83 if (!entry) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000084 // We have a cache miss
jvanverth@google.com94878772013-03-12 16:00:54 +000085#ifdef PROGRAM_CACHE_STATS
86 ++fCacheMisses;
87#endif
Ethan Nicholascae3a4c2017-02-02 10:43:58 -050088 GrGLProgram* program = GrGLProgramBuilder::CreateProgram(pipeline, primProc, desc, fGpu);
halcanary96fcdcc2015-08-27 07:41:13 -070089 if (nullptr == program) {
90 return nullptr;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000091 }
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050092 entry = fMap.insert(desc, std::unique_ptr<Entry>(new Entry(sk_sp<GrGLProgram>(program))));
junov@google.comf93e7172011-03-31 21:26:24 +000093 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000094
Ethan Nicholas1b9924f2016-12-15 15:28:42 -050095 return SkRef((*entry)->fProgram.get());
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000096}