Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2017 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // MemoryProgramCache: Stores compiled and linked programs in memory so they don't |
| 7 | // always have to be re-compiled. Can be used in conjunction with the platform |
| 8 | // layer to warm up the cache from disk. |
| 9 | |
| 10 | #ifndef LIBANGLE_MEMORY_PROGRAM_CACHE_H_ |
| 11 | #define LIBANGLE_MEMORY_PROGRAM_CACHE_H_ |
| 12 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 13 | #include <array> |
| 14 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 15 | #include "common/MemoryBuffer.h" |
| 16 | #include "libANGLE/Error.h" |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 17 | #include "libANGLE/SizedMRUCache.h" |
| 18 | |
| 19 | namespace gl |
| 20 | { |
Jamie Madill | 360daee | 2017-06-29 10:36:19 -0400 | [diff] [blame] | 21 | // 160-bit SHA-1 hash key. |
Jamie Madill | c43be72 | 2017-07-13 16:22:14 -0400 | [diff] [blame] | 22 | constexpr size_t kProgramHashLength = 20; |
| 23 | using ProgramHash = std::array<uint8_t, kProgramHashLength>; |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 24 | } // namespace gl |
| 25 | |
| 26 | namespace std |
| 27 | { |
| 28 | template <> |
| 29 | struct hash<gl::ProgramHash> |
| 30 | { |
| 31 | // Simple routine to hash four ints. |
| 32 | size_t operator()(const gl::ProgramHash &programHash) const |
| 33 | { |
| 34 | unsigned int hash = 0; |
| 35 | for (uint32_t num : programHash) |
| 36 | { |
| 37 | hash *= 37; |
| 38 | hash += num; |
| 39 | } |
| 40 | return hash; |
| 41 | } |
| 42 | }; |
| 43 | } // namespace std |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 44 | |
| 45 | namespace gl |
| 46 | { |
| 47 | class Context; |
| 48 | class InfoLog; |
| 49 | class Program; |
| 50 | class ProgramState; |
| 51 | |
| 52 | class MemoryProgramCache final : angle::NonCopyable |
| 53 | { |
| 54 | public: |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 55 | MemoryProgramCache(size_t maxCacheSizeBytes); |
| 56 | ~MemoryProgramCache(); |
| 57 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 58 | // Writes a program's binary to the output memory buffer. |
| 59 | static void Serialize(const Context *context, |
| 60 | const Program *program, |
| 61 | angle::MemoryBuffer *binaryOut); |
| 62 | |
| 63 | // Loads program state according to the specified binary blob. |
| 64 | static LinkResult Deserialize(const Context *context, |
| 65 | const Program *program, |
| 66 | ProgramState *state, |
| 67 | const uint8_t *binary, |
| 68 | size_t length, |
| 69 | InfoLog &infoLog); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 70 | |
| 71 | static void ComputeHash(const Context *context, const Program *program, ProgramHash *hashOut); |
| 72 | |
| 73 | // Check if the cache contains a binary matching the specified program. |
| 74 | bool get(const ProgramHash &programHash, const angle::MemoryBuffer **programOut); |
| 75 | |
Jamie Madill | c43be72 | 2017-07-13 16:22:14 -0400 | [diff] [blame] | 76 | // For querying the contents of the cache. |
| 77 | bool getAt(size_t index, ProgramHash *hashOut, const angle::MemoryBuffer **programOut); |
| 78 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 79 | // Evict a program from the binary cache. |
| 80 | void remove(const ProgramHash &programHash); |
| 81 | |
| 82 | // Helper method that serializes a program. |
| 83 | void putProgram(const ProgramHash &programHash, const Context *context, const Program *program); |
| 84 | |
Jamie Madill | 4c19a8a | 2017-07-24 11:46:06 -0400 | [diff] [blame^] | 85 | // Same as putProgram but computes the hash. |
| 86 | void updateProgram(const Context *context, const Program *program); |
| 87 | |
Jamie Madill | c43be72 | 2017-07-13 16:22:14 -0400 | [diff] [blame] | 88 | // Store a binary directly. |
| 89 | void putBinary(const ProgramHash &programHash, const uint8_t *binary, size_t length); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 90 | |
| 91 | // Check the cache, and deserialize and load the program if found. Evict existing hash if load |
| 92 | // fails. |
| 93 | LinkResult getProgram(const Context *context, |
| 94 | const Program *program, |
| 95 | ProgramState *state, |
| 96 | ProgramHash *hashOut); |
| 97 | |
| 98 | // Empty the cache. |
| 99 | void clear(); |
| 100 | |
Jamie Madill | c43be72 | 2017-07-13 16:22:14 -0400 | [diff] [blame] | 101 | // Resize the cache. Discards current contents. |
| 102 | void resize(size_t maxCacheSizeBytes); |
| 103 | |
| 104 | // Returns the number of entries in the cache. |
| 105 | size_t entryCount() const; |
| 106 | |
| 107 | // Reduces the current cache size and returns the number of bytes freed. |
| 108 | size_t trim(size_t limit); |
| 109 | |
| 110 | // Returns the current cache size in bytes. |
| 111 | size_t size() const; |
| 112 | |
| 113 | // Returns the maximum cache size in bytes. |
| 114 | size_t maxSize() const; |
| 115 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 116 | private: |
| 117 | // Insert or update a binary program. Program contents are transferred. |
Jamie Madill | 360daee | 2017-06-29 10:36:19 -0400 | [diff] [blame] | 118 | void put(const ProgramHash &programHash, |
Jamie Madill | 360daee | 2017-06-29 10:36:19 -0400 | [diff] [blame] | 119 | angle::MemoryBuffer &&binaryProgram); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 120 | |
| 121 | angle::SizedMRUCache<ProgramHash, angle::MemoryBuffer> mProgramBinaryCache; |
| 122 | unsigned int mIssuedWarnings; |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | } // namespace gl |
| 126 | |
| 127 | #endif // LIBANGLE_MEMORY_PROGRAM_CACHE_H_ |