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 | { |
| 21 | // 128-bit program hash key. |
| 22 | using ProgramHash = std::array<uint8_t, 20>; |
| 23 | } // namespace gl |
| 24 | |
| 25 | namespace std |
| 26 | { |
| 27 | template <> |
| 28 | struct hash<gl::ProgramHash> |
| 29 | { |
| 30 | // Simple routine to hash four ints. |
| 31 | size_t operator()(const gl::ProgramHash &programHash) const |
| 32 | { |
| 33 | unsigned int hash = 0; |
| 34 | for (uint32_t num : programHash) |
| 35 | { |
| 36 | hash *= 37; |
| 37 | hash += num; |
| 38 | } |
| 39 | return hash; |
| 40 | } |
| 41 | }; |
| 42 | } // namespace std |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 43 | |
| 44 | namespace gl |
| 45 | { |
| 46 | class Context; |
| 47 | class InfoLog; |
| 48 | class Program; |
| 49 | class ProgramState; |
| 50 | |
| 51 | class MemoryProgramCache final : angle::NonCopyable |
| 52 | { |
| 53 | public: |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame^] | 54 | MemoryProgramCache(size_t maxCacheSizeBytes); |
| 55 | ~MemoryProgramCache(); |
| 56 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 57 | // Writes a program's binary to the output memory buffer. |
| 58 | static void Serialize(const Context *context, |
| 59 | const Program *program, |
| 60 | angle::MemoryBuffer *binaryOut); |
| 61 | |
| 62 | // Loads program state according to the specified binary blob. |
| 63 | static LinkResult Deserialize(const Context *context, |
| 64 | const Program *program, |
| 65 | ProgramState *state, |
| 66 | const uint8_t *binary, |
| 67 | size_t length, |
| 68 | InfoLog &infoLog); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame^] | 69 | |
| 70 | static void ComputeHash(const Context *context, const Program *program, ProgramHash *hashOut); |
| 71 | |
| 72 | // Check if the cache contains a binary matching the specified program. |
| 73 | bool get(const ProgramHash &programHash, const angle::MemoryBuffer **programOut); |
| 74 | |
| 75 | // Evict a program from the binary cache. |
| 76 | void remove(const ProgramHash &programHash); |
| 77 | |
| 78 | // Helper method that serializes a program. |
| 79 | void putProgram(const ProgramHash &programHash, const Context *context, const Program *program); |
| 80 | |
| 81 | // Helper method that copies a user binary. |
| 82 | void putBinary(const Context *context, |
| 83 | const Program *program, |
| 84 | const uint8_t *binary, |
| 85 | size_t length); |
| 86 | |
| 87 | // Check the cache, and deserialize and load the program if found. Evict existing hash if load |
| 88 | // fails. |
| 89 | LinkResult getProgram(const Context *context, |
| 90 | const Program *program, |
| 91 | ProgramState *state, |
| 92 | ProgramHash *hashOut); |
| 93 | |
| 94 | // Empty the cache. |
| 95 | void clear(); |
| 96 | |
| 97 | private: |
| 98 | // Insert or update a binary program. Program contents are transferred. |
| 99 | void put(const ProgramHash &programHash, angle::MemoryBuffer &&binaryProgram); |
| 100 | |
| 101 | angle::SizedMRUCache<ProgramHash, angle::MemoryBuffer> mProgramBinaryCache; |
| 102 | unsigned int mIssuedWarnings; |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace gl |
| 106 | |
| 107 | #endif // LIBANGLE_MEMORY_PROGRAM_CACHE_H_ |