Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/utils/SkBase64.h" |
| 9 | #include "src/core/SkMD5.h" |
| 10 | #include "src/gpu/GrPersistentCacheUtils.h" |
| 11 | #include "tools/gpu/MemoryCache.h" |
Brian Osman | be2062c | 2019-04-15 09:26:13 -0400 | [diff] [blame] | 12 | |
| 13 | #if defined(SK_VULKAN) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/vk/GrVkGpu.h" |
Brian Osman | be2062c | 2019-04-15 09:26:13 -0400 | [diff] [blame] | 15 | #endif |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 16 | |
| 17 | // Change this to 1 to log cache hits/misses/stores using SkDebugf. |
| 18 | #define LOG_MEMORY_CACHE 0 |
| 19 | |
| 20 | static SkString data_to_str(const SkData& data) { |
| 21 | size_t encodeLength = SkBase64::Encode(data.data(), data.size(), nullptr); |
| 22 | SkString str; |
| 23 | str.resize(encodeLength); |
| 24 | SkBase64::Encode(data.data(), data.size(), str.writable_str()); |
| 25 | static constexpr size_t kMaxLength = 60; |
| 26 | static constexpr char kTail[] = "..."; |
| 27 | static const size_t kTailLen = strlen(kTail); |
| 28 | bool overlength = encodeLength > kMaxLength; |
| 29 | if (overlength) { |
| 30 | str = SkString(str.c_str(), kMaxLength - kTailLen); |
| 31 | str.append(kTail); |
| 32 | } |
| 33 | return str; |
| 34 | } |
| 35 | |
| 36 | namespace sk_gpu_test { |
| 37 | |
| 38 | sk_sp<SkData> MemoryCache::load(const SkData& key) { |
| 39 | auto result = fMap.find(key); |
| 40 | if (result == fMap.end()) { |
| 41 | if (LOG_MEMORY_CACHE) { |
| 42 | SkDebugf("Load Key: %s\n\tNot Found.\n\n", data_to_str(key).c_str()); |
| 43 | } |
| 44 | ++fCacheMissCnt; |
| 45 | return nullptr; |
| 46 | } |
| 47 | if (LOG_MEMORY_CACHE) { |
| 48 | SkDebugf("Load Key: %s\n\tFound Data: %s\n\n", data_to_str(key).c_str(), |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 49 | data_to_str(*result->second.fData).c_str()); |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 50 | } |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 51 | result->second.fHitCount++; |
| 52 | return result->second.fData; |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void MemoryCache::store(const SkData& key, const SkData& data) { |
| 56 | if (LOG_MEMORY_CACHE) { |
| 57 | SkDebugf("Store Key: %s\n\tData: %s\n\n", data_to_str(key).c_str(), |
| 58 | data_to_str(data).c_str()); |
| 59 | } |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 60 | fMap[Key(key)] = Value(data); |
| 61 | } |
| 62 | |
| 63 | void MemoryCache::writeShadersToDisk(const char* path, GrBackendApi api) { |
Brian Osman | be2062c | 2019-04-15 09:26:13 -0400 | [diff] [blame] | 64 | if (GrBackendApi::kOpenGL != api && GrBackendApi::kVulkan != api) { |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 65 | return; |
| 66 | } |
| 67 | |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 68 | for (auto it = fMap.begin(); it != fMap.end(); ++it) { |
| 69 | SkMD5 hash; |
Brian Osman | be2062c | 2019-04-15 09:26:13 -0400 | [diff] [blame] | 70 | size_t bytesToHash = it->first.fKey->size(); |
| 71 | #if defined(SK_VULKAN) |
| 72 | if (GrBackendApi::kVulkan == api) { |
| 73 | // Vulkan stores two kinds of data in the cache (shaders and pipelines). The last four |
| 74 | // bytes of the key identify which one we have. We only want to extract shaders. |
| 75 | // Additionally, we don't want to hash the tag bytes, so we get the same keys as GL, |
| 76 | // which is good for cross-checking code generation and performance. |
| 77 | GrVkGpu::PersistentCacheKeyType vkKeyType; |
| 78 | SkASSERT(bytesToHash >= sizeof(vkKeyType)); |
| 79 | bytesToHash -= sizeof(vkKeyType); |
| 80 | memcpy(&vkKeyType, it->first.fKey->bytes() + bytesToHash, sizeof(vkKeyType)); |
| 81 | if (vkKeyType != GrVkGpu::kShader_PersistentCacheKeyType) { |
| 82 | continue; |
| 83 | } |
| 84 | } |
| 85 | #endif |
| 86 | hash.write(it->first.fKey->bytes(), bytesToHash); |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 87 | SkMD5::Digest digest = hash.finish(); |
| 88 | SkString md5; |
| 89 | for (int i = 0; i < 16; ++i) { |
| 90 | md5.appendf("%02x", digest.data[i]); |
| 91 | } |
| 92 | |
Brian Osman | be2062c | 2019-04-15 09:26:13 -0400 | [diff] [blame] | 93 | SkSL::Program::Inputs inputsIgnored[kGrShaderTypeCount]; |
| 94 | SkSL::String shaders[kGrShaderTypeCount]; |
| 95 | const SkData* data = it->second.fData.get(); |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 96 | // Even with the SPIR-V switches, it seems like we must use .spv, or malisc tries to |
| 97 | // run glslang on the input. |
| 98 | const char* ext = GrBackendApi::kOpenGL == api ? "frag" : "spv"; |
| 99 | GrPersistentCacheUtils::UnpackCachedShaders(data, shaders, |
| 100 | inputsIgnored, kGrShaderTypeCount); |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 101 | |
Brian Osman | be2062c | 2019-04-15 09:26:13 -0400 | [diff] [blame] | 102 | SkString filename = SkStringPrintf("%s/%s.%s", path, md5.c_str(), ext); |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 103 | SkFILEWStream file(filename.c_str()); |
Brian Osman | be2062c | 2019-04-15 09:26:13 -0400 | [diff] [blame] | 104 | file.write(shaders[kFragment_GrShaderType].c_str(), shaders[kFragment_GrShaderType].size()); |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 105 | } |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } // namespace sk_gpu_test |