blob: faf0b6f029b85073d6b6e6f5b6a16b1ed9731e49 [file] [log] [blame]
Jamie Madill4f86d052017-06-05 12:59:26 -04001//
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 Madill32447362017-06-28 14:53:52 -040013#include <array>
14
Jamie Madill4f86d052017-06-05 12:59:26 -040015#include "common/MemoryBuffer.h"
16#include "libANGLE/Error.h"
Jamie Madill32447362017-06-28 14:53:52 -040017#include "libANGLE/SizedMRUCache.h"
18
19namespace gl
20{
21// 128-bit program hash key.
22using ProgramHash = std::array<uint8_t, 20>;
23} // namespace gl
24
25namespace std
26{
27template <>
28struct 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 Madill4f86d052017-06-05 12:59:26 -040043
44namespace gl
45{
46class Context;
47class InfoLog;
48class Program;
49class ProgramState;
50
51class MemoryProgramCache final : angle::NonCopyable
52{
53 public:
Jamie Madill32447362017-06-28 14:53:52 -040054 MemoryProgramCache(size_t maxCacheSizeBytes);
55 ~MemoryProgramCache();
56
Jamie Madill4f86d052017-06-05 12:59:26 -040057 // 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 Madill32447362017-06-28 14:53:52 -040069
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 Madill4f86d052017-06-05 12:59:26 -0400103};
104
105} // namespace gl
106
107#endif // LIBANGLE_MEMORY_PROGRAM_CACHE_H_