Debian Codegraph @

tools/ganesh/MemoryCache.cpp

raw
1 /*
2 * Copyright 2018 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
8 #include "tools/ganesh/MemoryCache.h"
9
10 #include "src/base/SkBase64.h"
11 #include "src/core/SkMD5.h"
12 #include "src/core/SkReadBuffer.h"
13 #include "src/gpu/ganesh/GrPersistentCacheUtils.h"
14 #include "src/sksl/codegen/SkSLNativeShader.h"
15
16 #if defined(SK_VULKAN)
17 #include "src/gpu/ganesh/vk/GrVkGpu.h"
18 #endif
19
20 // Change this to 1 to log cache hits/misses/stores using SkDebugf.
21 #define LOG_MEMORY_CACHE 0
22
23 static SkString data_to_str(const SkData& data) {
24 size_t encodeLength = SkBase64::EncodedSize(data.size());
25 SkString str;
26 str.resize(encodeLength);
27 SkBase64::Encode(data.data(), data.size(), str.data());
28 static constexpr size_t kMaxLength = 60;
29 static constexpr char kTail[] = "...";
30 static const size_t kTailLen = strlen(kTail);
31 bool overlength = encodeLength > kMaxLength;
32 if (overlength) {
33 str = SkString(str.c_str(), kMaxLength - kTailLen);
34 str.append(kTail);
35 }
36 return str;
37 }
38
39 namespace sk_gpu_test {
40
41 sk_sp<SkData> MemoryCache::load(const SkData& key) {
42 auto result = fMap.find(key);
43 if (result == fMap.end()) {
44 if (LOG_MEMORY_CACHE) {
45 SkDebugf("Load Key: %s\n\tNot Found.\n\n", data_to_str(key).c_str());
46 }
47 ++fCacheMissCnt;
48 return nullptr;
49 }
50 if (LOG_MEMORY_CACHE) {
51 SkDebugf("Load Key: %s\n\tFound Data: %s\n\n", data_to_str(key).c_str(),
52 data_to_str(*result->second.fData).c_str());
53 }
54 result->second.fHitCount++;
55 return result->second.fData;
56 }
57
58 void MemoryCache::store(const SkData& key, const SkData& data, const SkString& description) {
59 if (LOG_MEMORY_CACHE) {
60 SkDebugf("Store Key: %s\n\tData: %s\n\n", data_to_str(key).c_str(),
61 data_to_str(data).c_str());
62 }
63 ++fCacheStoreCnt;
64 fMap[Key(key)] = Value(data, description);
65 }
66
67 void MemoryCache::writeShadersToDisk(const char* path, GrBackendApi api) {
68 if (GrBackendApi::kOpenGL != api && GrBackendApi::kVulkan != api) {
69 return;
70 }
71
72 for (auto it = fMap.begin(); it != fMap.end(); ++it) {
73 SkMD5 hash;
74 size_t bytesToHash = it->first.fKey->size();
75 #if defined(SK_VULKAN)
76 if (GrBackendApi::kVulkan == api) {
77 // Vulkan stores two kinds of data in the cache (shaders and pipelines). The last four
78 // bytes of the key identify which one we have. We only want to extract shaders.
79 // Additionally, we don't want to hash the tag bytes, so we get the same keys as GL,
80 // which is good for cross-checking code generation and performance.
81 GrVkGpu::PersistentCacheKeyType vkKeyType;
82 SkASSERT(bytesToHash >= sizeof(vkKeyType));
83 bytesToHash -= sizeof(vkKeyType);
84 memcpy(&vkKeyType, it->first.fKey->bytes() + bytesToHash, sizeof(vkKeyType));
85 if (vkKeyType != GrVkGpu::kShader_PersistentCacheKeyType) {
86 continue;
87 }
88 }
89 #endif
90 hash.write(it->first.fKey->bytes(), bytesToHash);
91 SkMD5::Digest digest = hash.finish();
92 SkString md5 = digest.toLowercaseHexString();
93
94 SkSL::Program::Interface interfacesIgnored[kGrShaderTypeCount];
95 const SkData* data = it->second.fData.get();
96 const SkString& description = it->second.fDescription;
97 SkReadBuffer reader(data->data(), data->size());
98 const SkFourByteTag shaderType =
99 GrPersistentCacheUtils::GetType(&reader); // Shader type tag
100 const bool isBinaryShader =
101 GrBackendApi::kVulkan == api && shaderType == SkSetFourByteTag('S', 'P', 'R', 'V');
102
103 SkSL::NativeShader shaders[kGrShaderTypeCount];
104 GrPersistentCacheUtils::UnpackCachedShaders(
105 &reader, shaders, isBinaryShader, interfacesIgnored, kGrShaderTypeCount);
106
107 // Even with the SPIR-V switches, it seems like we must use .spv, or malisc tries to
108 // run glslang on the input.
109 if (isBinaryShader) {
110 {
111 SkString filename = SkStringPrintf("%s/%s.frag.spv", path, md5.c_str());
112 SkFILEWStream file(filename.c_str());
113 file.write(shaders[kFragment_GrShaderType].fBinary.data(),
114 shaders[kFragment_GrShaderType].fBinary.size() * sizeof(uint32_t));
115 }
116 {
117 SkString filename = SkStringPrintf("%s/%s.vert.spv", path, md5.c_str());
118 SkFILEWStream file(filename.c_str());
119 file.write(shaders[kVertex_GrShaderType].fBinary.data(),
120 shaders[kVertex_GrShaderType].fBinary.size() * sizeof(uint32_t));
121 }
122 } else {
123 {
124 SkString filename = SkStringPrintf("%s/%s.frag", path, md5.c_str());
125 SkFILEWStream file(filename.c_str());
126 file.write(shaders[kFragment_GrShaderType].fText.c_str(),
127 shaders[kFragment_GrShaderType].fText.size());
128 }
129 {
130 SkString filename = SkStringPrintf("%s/%s.vert", path, md5.c_str());
131 SkFILEWStream file(filename.c_str());
132 file.write(shaders[kVertex_GrShaderType].fText.c_str(),
133 shaders[kVertex_GrShaderType].fText.size());
134 }
135 }
136
137 if (!description.isEmpty()) {
138 const char* ext = "key";
139 SkString filename = SkStringPrintf("%s/%s.%s", path, md5.c_str(), ext);
140 SkFILEWStream file(filename.c_str());
141 file.write(description.c_str(), description.size());
142 }
143 }
144 }
145
146 } // namespace sk_gpu_test