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 | #include "libANGLE/MemoryProgramCache.h" |
| 11 | |
| 12 | #include <GLSLANG/ShaderVars.h> |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 13 | #include <anglebase/sha1.h> |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 14 | |
Jamie Madill | f00f7ff | 2017-08-31 14:39:15 -0400 | [diff] [blame] | 15 | #include "common/utilities.h" |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 16 | #include "common/version.h" |
| 17 | #include "libANGLE/BinaryStream.h" |
| 18 | #include "libANGLE/Context.h" |
| 19 | #include "libANGLE/Uniform.h" |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 20 | #include "libANGLE/histogram_macros.h" |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 21 | #include "libANGLE/renderer/ProgramImpl.h" |
Jamie Madill | 360daee | 2017-06-29 10:36:19 -0400 | [diff] [blame] | 22 | #include "platform/Platform.h" |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 23 | |
| 24 | namespace gl |
| 25 | { |
| 26 | |
| 27 | namespace |
| 28 | { |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 29 | enum CacheResult |
| 30 | { |
| 31 | kCacheMiss, |
| 32 | kCacheHitMemory, |
| 33 | kCacheHitDisk, |
| 34 | kCacheResultMax, |
| 35 | }; |
| 36 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 37 | constexpr unsigned int kWarningLimit = 3; |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 38 | |
| 39 | void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var) |
| 40 | { |
| 41 | stream->writeInt(var.type); |
| 42 | stream->writeInt(var.precision); |
| 43 | stream->writeString(var.name); |
| 44 | stream->writeString(var.mappedName); |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 45 | stream->writeIntVector(var.arraySizes); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 46 | stream->writeInt(var.staticUse); |
Olli Etuaho | 107c724 | 2018-03-20 15:45:35 +0200 | [diff] [blame] | 47 | stream->writeInt(var.active); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 48 | stream->writeString(var.structName); |
| 49 | ASSERT(var.fields.empty()); |
| 50 | } |
| 51 | |
| 52 | void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var) |
| 53 | { |
| 54 | var->type = stream->readInt<GLenum>(); |
| 55 | var->precision = stream->readInt<GLenum>(); |
| 56 | var->name = stream->readString(); |
| 57 | var->mappedName = stream->readString(); |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 58 | stream->readIntVector<unsigned int>(&var->arraySizes); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 59 | var->staticUse = stream->readBool(); |
Olli Etuaho | 107c724 | 2018-03-20 15:45:35 +0200 | [diff] [blame] | 60 | var->active = stream->readBool(); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 61 | var->structName = stream->readString(); |
| 62 | } |
| 63 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 64 | void WriteShaderVariableBuffer(BinaryOutputStream *stream, const ShaderVariableBuffer &var) |
| 65 | { |
| 66 | stream->writeInt(var.binding); |
| 67 | stream->writeInt(var.dataSize); |
| 68 | |
Jiawei Shao | 3dd8d291 | 2018-03-30 09:39:09 +0800 | [diff] [blame] | 69 | for (ShaderType shaderType : AllShaderTypes()) |
| 70 | { |
| 71 | stream->writeInt(var.isActive(shaderType)); |
| 72 | } |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 73 | |
| 74 | stream->writeInt(var.memberIndexes.size()); |
| 75 | for (unsigned int memberCounterIndex : var.memberIndexes) |
| 76 | { |
| 77 | stream->writeInt(memberCounterIndex); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void LoadShaderVariableBuffer(BinaryInputStream *stream, ShaderVariableBuffer *var) |
| 82 | { |
| 83 | var->binding = stream->readInt<int>(); |
| 84 | var->dataSize = stream->readInt<unsigned int>(); |
Jiawei Shao | 3dd8d291 | 2018-03-30 09:39:09 +0800 | [diff] [blame] | 85 | |
| 86 | for (ShaderType shaderType : AllShaderTypes()) |
| 87 | { |
| 88 | var->setActive(shaderType, stream->readBool()); |
| 89 | } |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 90 | |
| 91 | unsigned int numMembers = stream->readInt<unsigned int>(); |
| 92 | for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++) |
| 93 | { |
| 94 | var->memberIndexes.push_back(stream->readInt<unsigned int>()); |
| 95 | } |
| 96 | } |
| 97 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 98 | void WriteBufferVariable(BinaryOutputStream *stream, const BufferVariable &var) |
| 99 | { |
| 100 | WriteShaderVar(stream, var); |
| 101 | |
| 102 | stream->writeInt(var.bufferIndex); |
| 103 | stream->writeInt(var.blockInfo.offset); |
| 104 | stream->writeInt(var.blockInfo.arrayStride); |
| 105 | stream->writeInt(var.blockInfo.matrixStride); |
| 106 | stream->writeInt(var.blockInfo.isRowMajorMatrix); |
| 107 | stream->writeInt(var.blockInfo.topLevelArrayStride); |
| 108 | stream->writeInt(var.topLevelArraySize); |
Jiawei Shao | 3dd8d291 | 2018-03-30 09:39:09 +0800 | [diff] [blame] | 109 | |
| 110 | for (ShaderType shaderType : AllShaderTypes()) |
| 111 | { |
| 112 | stream->writeInt(var.isActive(shaderType)); |
| 113 | } |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void LoadBufferVariable(BinaryInputStream *stream, BufferVariable *var) |
| 117 | { |
| 118 | LoadShaderVar(stream, var); |
| 119 | |
| 120 | var->bufferIndex = stream->readInt<int>(); |
| 121 | var->blockInfo.offset = stream->readInt<int>(); |
| 122 | var->blockInfo.arrayStride = stream->readInt<int>(); |
| 123 | var->blockInfo.matrixStride = stream->readInt<int>(); |
| 124 | var->blockInfo.isRowMajorMatrix = stream->readBool(); |
| 125 | var->blockInfo.topLevelArrayStride = stream->readInt<int>(); |
| 126 | var->topLevelArraySize = stream->readInt<int>(); |
Jiawei Shao | 3dd8d291 | 2018-03-30 09:39:09 +0800 | [diff] [blame] | 127 | |
| 128 | for (ShaderType shaderType : AllShaderTypes()) |
| 129 | { |
| 130 | var->setActive(shaderType, stream->readBool()); |
| 131 | } |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 134 | void WriteInterfaceBlock(BinaryOutputStream *stream, const InterfaceBlock &block) |
| 135 | { |
| 136 | stream->writeString(block.name); |
| 137 | stream->writeString(block.mappedName); |
| 138 | stream->writeInt(block.isArray); |
| 139 | stream->writeInt(block.arrayElement); |
| 140 | |
| 141 | WriteShaderVariableBuffer(stream, block); |
| 142 | } |
| 143 | |
| 144 | void LoadInterfaceBlock(BinaryInputStream *stream, InterfaceBlock *block) |
| 145 | { |
| 146 | block->name = stream->readString(); |
| 147 | block->mappedName = stream->readString(); |
| 148 | block->isArray = stream->readBool(); |
| 149 | block->arrayElement = stream->readInt<unsigned int>(); |
| 150 | |
| 151 | LoadShaderVariableBuffer(stream, block); |
| 152 | } |
| 153 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 154 | class HashStream final : angle::NonCopyable |
| 155 | { |
| 156 | public: |
| 157 | std::string str() { return mStringStream.str(); } |
| 158 | |
| 159 | template <typename T> |
| 160 | HashStream &operator<<(T value) |
| 161 | { |
| 162 | mStringStream << value << kSeparator; |
| 163 | return *this; |
| 164 | } |
| 165 | |
| 166 | private: |
| 167 | static constexpr char kSeparator = ':'; |
| 168 | std::ostringstream mStringStream; |
| 169 | }; |
| 170 | |
| 171 | HashStream &operator<<(HashStream &stream, const Shader *shader) |
| 172 | { |
| 173 | if (shader) |
| 174 | { |
| 175 | stream << shader->getSourceString().c_str() << shader->getSourceString().length() |
| 176 | << shader->getCompilerResourcesString().c_str(); |
| 177 | } |
| 178 | return stream; |
| 179 | } |
| 180 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 181 | HashStream &operator<<(HashStream &stream, const ProgramBindings &bindings) |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 182 | { |
| 183 | for (const auto &binding : bindings) |
| 184 | { |
| 185 | stream << binding.first << binding.second; |
| 186 | } |
| 187 | return stream; |
| 188 | } |
| 189 | |
| 190 | HashStream &operator<<(HashStream &stream, const std::vector<std::string> &strings) |
| 191 | { |
| 192 | for (const auto &str : strings) |
| 193 | { |
| 194 | stream << str; |
| 195 | } |
| 196 | return stream; |
| 197 | } |
| 198 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 199 | } // anonymous namespace |
| 200 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 201 | MemoryProgramCache::MemoryProgramCache(size_t maxCacheSizeBytes) |
| 202 | : mProgramBinaryCache(maxCacheSizeBytes), mIssuedWarnings(0) |
| 203 | { |
| 204 | } |
| 205 | |
| 206 | MemoryProgramCache::~MemoryProgramCache() |
| 207 | { |
| 208 | } |
| 209 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 210 | // static |
| 211 | LinkResult MemoryProgramCache::Deserialize(const Context *context, |
| 212 | const Program *program, |
| 213 | ProgramState *state, |
| 214 | const uint8_t *binary, |
| 215 | size_t length, |
| 216 | InfoLog &infoLog) |
| 217 | { |
| 218 | BinaryInputStream stream(binary, length); |
| 219 | |
| 220 | unsigned char commitString[ANGLE_COMMIT_HASH_SIZE]; |
| 221 | stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE); |
| 222 | if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != |
| 223 | 0) |
| 224 | { |
| 225 | infoLog << "Invalid program binary version."; |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | int majorVersion = stream.readInt<int>(); |
| 230 | int minorVersion = stream.readInt<int>(); |
| 231 | if (majorVersion != context->getClientMajorVersion() || |
| 232 | minorVersion != context->getClientMinorVersion()) |
| 233 | { |
| 234 | infoLog << "Cannot load program binaries across different ES context versions."; |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | state->mComputeShaderLocalSize[0] = stream.readInt<int>(); |
| 239 | state->mComputeShaderLocalSize[1] = stream.readInt<int>(); |
| 240 | state->mComputeShaderLocalSize[2] = stream.readInt<int>(); |
| 241 | |
Jiawei Shao | 4ed05da | 2018-02-02 14:26:15 +0800 | [diff] [blame] | 242 | state->mGeometryShaderInputPrimitiveType = stream.readInt<GLenum>(); |
| 243 | state->mGeometryShaderOutputPrimitiveType = stream.readInt<GLenum>(); |
| 244 | state->mGeometryShaderInvocations = stream.readInt<int>(); |
| 245 | state->mGeometryShaderMaxVertices = stream.readInt<int>(); |
| 246 | |
Martin Radev | 7cf6166 | 2017-07-26 17:10:53 +0300 | [diff] [blame] | 247 | state->mNumViews = stream.readInt<int>(); |
| 248 | |
Brandon Jones | c405ae7 | 2017-12-06 14:15:03 -0800 | [diff] [blame] | 249 | static_assert(MAX_VERTEX_ATTRIBS * 2 <= sizeof(uint32_t) * 8, |
| 250 | "All bits of mAttributesTypeMask types and mask fit into 32 bits each"); |
| 251 | state->mAttributesTypeMask.from_ulong(stream.readInt<uint32_t>()); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 252 | state->mAttributesMask = stream.readInt<gl::AttributesMask>(); |
Brandon Jones | c405ae7 | 2017-12-06 14:15:03 -0800 | [diff] [blame] | 253 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 254 | static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8, |
| 255 | "Too many vertex attribs for mask"); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 256 | state->mActiveAttribLocationsMask = stream.readInt<gl::AttributesMask>(); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 257 | |
| 258 | unsigned int attribCount = stream.readInt<unsigned int>(); |
| 259 | ASSERT(state->mAttributes.empty()); |
| 260 | for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex) |
| 261 | { |
| 262 | sh::Attribute attrib; |
| 263 | LoadShaderVar(&stream, &attrib); |
| 264 | attrib.location = stream.readInt<int>(); |
| 265 | state->mAttributes.push_back(attrib); |
| 266 | } |
| 267 | |
| 268 | unsigned int uniformCount = stream.readInt<unsigned int>(); |
| 269 | ASSERT(state->mUniforms.empty()); |
| 270 | for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex) |
| 271 | { |
| 272 | LinkedUniform uniform; |
| 273 | LoadShaderVar(&stream, &uniform); |
| 274 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 275 | uniform.bufferIndex = stream.readInt<int>(); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 276 | uniform.blockInfo.offset = stream.readInt<int>(); |
| 277 | uniform.blockInfo.arrayStride = stream.readInt<int>(); |
| 278 | uniform.blockInfo.matrixStride = stream.readInt<int>(); |
| 279 | uniform.blockInfo.isRowMajorMatrix = stream.readBool(); |
| 280 | |
Jamie Madill | f00f7ff | 2017-08-31 14:39:15 -0400 | [diff] [blame] | 281 | uniform.typeInfo = &GetUniformTypeInfo(uniform.type); |
| 282 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 283 | state->mUniforms.push_back(uniform); |
| 284 | } |
| 285 | |
| 286 | const unsigned int uniformIndexCount = stream.readInt<unsigned int>(); |
| 287 | ASSERT(state->mUniformLocations.empty()); |
| 288 | for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; |
| 289 | uniformIndexIndex++) |
| 290 | { |
| 291 | VariableLocation variable; |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 292 | stream.readInt(&variable.arrayIndex); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 293 | stream.readInt(&variable.index); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 294 | stream.readBool(&variable.ignored); |
| 295 | |
| 296 | state->mUniformLocations.push_back(variable); |
| 297 | } |
| 298 | |
| 299 | unsigned int uniformBlockCount = stream.readInt<unsigned int>(); |
| 300 | ASSERT(state->mUniformBlocks.empty()); |
| 301 | for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; |
| 302 | ++uniformBlockIndex) |
| 303 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 304 | InterfaceBlock uniformBlock; |
| 305 | LoadInterfaceBlock(&stream, &uniformBlock); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 306 | state->mUniformBlocks.push_back(uniformBlock); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 307 | |
jchen10 | 7a20b97 | 2017-06-13 14:25:26 +0800 | [diff] [blame] | 308 | state->mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlock.binding != 0); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 309 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 310 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 311 | unsigned int bufferVariableCount = stream.readInt<unsigned int>(); |
| 312 | ASSERT(state->mBufferVariables.empty()); |
| 313 | for (unsigned int index = 0; index < bufferVariableCount; ++index) |
| 314 | { |
| 315 | BufferVariable bufferVariable; |
| 316 | LoadBufferVariable(&stream, &bufferVariable); |
| 317 | state->mBufferVariables.push_back(bufferVariable); |
| 318 | } |
| 319 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 320 | unsigned int shaderStorageBlockCount = stream.readInt<unsigned int>(); |
| 321 | ASSERT(state->mShaderStorageBlocks.empty()); |
| 322 | for (unsigned int shaderStorageBlockIndex = 0; |
| 323 | shaderStorageBlockIndex < shaderStorageBlockCount; ++shaderStorageBlockIndex) |
| 324 | { |
| 325 | InterfaceBlock shaderStorageBlock; |
| 326 | LoadInterfaceBlock(&stream, &shaderStorageBlock); |
| 327 | state->mShaderStorageBlocks.push_back(shaderStorageBlock); |
| 328 | } |
| 329 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 330 | unsigned int atomicCounterBufferCount = stream.readInt<unsigned int>(); |
| 331 | ASSERT(state->mAtomicCounterBuffers.empty()); |
| 332 | for (unsigned int bufferIndex = 0; bufferIndex < atomicCounterBufferCount; ++bufferIndex) |
| 333 | { |
| 334 | AtomicCounterBuffer atomicCounterBuffer; |
| 335 | LoadShaderVariableBuffer(&stream, &atomicCounterBuffer); |
| 336 | |
| 337 | state->mAtomicCounterBuffers.push_back(atomicCounterBuffer); |
| 338 | } |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 339 | |
| 340 | unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>(); |
Jamie Madill | ffe00c0 | 2017-06-27 16:26:55 -0400 | [diff] [blame] | 341 | |
| 342 | // Reject programs that use transform feedback varyings if the hardware cannot support them. |
| 343 | if (transformFeedbackVaryingCount > 0 && |
| 344 | context->getWorkarounds().disableProgramCachingForTransformFeedback) |
| 345 | { |
| 346 | infoLog << "Current driver does not support transform feedback in binary programs."; |
| 347 | return false; |
| 348 | } |
| 349 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 350 | ASSERT(state->mLinkedTransformFeedbackVaryings.empty()); |
| 351 | for (unsigned int transformFeedbackVaryingIndex = 0; |
| 352 | transformFeedbackVaryingIndex < transformFeedbackVaryingCount; |
| 353 | ++transformFeedbackVaryingIndex) |
| 354 | { |
| 355 | sh::Varying varying; |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 356 | stream.readIntVector<unsigned int>(&varying.arraySizes); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 357 | stream.readInt(&varying.type); |
| 358 | stream.readString(&varying.name); |
| 359 | |
| 360 | GLuint arrayIndex = stream.readInt<GLuint>(); |
| 361 | |
| 362 | state->mLinkedTransformFeedbackVaryings.emplace_back(varying, arrayIndex); |
| 363 | } |
| 364 | |
| 365 | stream.readInt(&state->mTransformFeedbackBufferMode); |
| 366 | |
| 367 | unsigned int outputCount = stream.readInt<unsigned int>(); |
| 368 | ASSERT(state->mOutputVariables.empty()); |
| 369 | for (unsigned int outputIndex = 0; outputIndex < outputCount; ++outputIndex) |
| 370 | { |
| 371 | sh::OutputVariable output; |
| 372 | LoadShaderVar(&stream, &output); |
| 373 | output.location = stream.readInt<int>(); |
| 374 | state->mOutputVariables.push_back(output); |
| 375 | } |
| 376 | |
| 377 | unsigned int outputVarCount = stream.readInt<unsigned int>(); |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 378 | ASSERT(state->mOutputLocations.empty()); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 379 | for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex) |
| 380 | { |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 381 | VariableLocation locationData; |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 382 | stream.readInt(&locationData.arrayIndex); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 383 | stream.readInt(&locationData.index); |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 384 | stream.readBool(&locationData.ignored); |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 385 | state->mOutputLocations.push_back(locationData); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | unsigned int outputTypeCount = stream.readInt<unsigned int>(); |
| 389 | for (unsigned int outputIndex = 0; outputIndex < outputTypeCount; ++outputIndex) |
| 390 | { |
| 391 | state->mOutputVariableTypes.push_back(stream.readInt<GLenum>()); |
| 392 | } |
Brandon Jones | 76746f9 | 2017-11-22 11:44:41 -0800 | [diff] [blame] | 393 | |
Brandon Jones | c405ae7 | 2017-12-06 14:15:03 -0800 | [diff] [blame] | 394 | static_assert(IMPLEMENTATION_MAX_DRAW_BUFFERS * 2 <= 8 * sizeof(uint32_t), |
| 395 | "All bits of mDrawBufferTypeMask and mActiveOutputVariables types and mask fit " |
| 396 | "into 32 bits each"); |
| 397 | state->mDrawBufferTypeMask.from_ulong(stream.readInt<uint32_t>()); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 398 | state->mActiveOutputVariables = stream.readInt<gl::DrawBufferMask>(); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 399 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 400 | unsigned int samplerRangeLow = stream.readInt<unsigned int>(); |
| 401 | unsigned int samplerRangeHigh = stream.readInt<unsigned int>(); |
| 402 | state->mSamplerUniformRange = RangeUI(samplerRangeLow, samplerRangeHigh); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 403 | unsigned int samplerCount = stream.readInt<unsigned int>(); |
| 404 | for (unsigned int samplerIndex = 0; samplerIndex < samplerCount; ++samplerIndex) |
| 405 | { |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 406 | TextureType textureType = stream.readEnum<TextureType>(); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 407 | size_t bindingCount = stream.readInt<size_t>(); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 408 | bool unreferenced = stream.readBool(); |
| 409 | state->mSamplerBindings.emplace_back( |
| 410 | SamplerBinding(textureType, bindingCount, unreferenced)); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 411 | } |
| 412 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 413 | unsigned int imageRangeLow = stream.readInt<unsigned int>(); |
| 414 | unsigned int imageRangeHigh = stream.readInt<unsigned int>(); |
| 415 | state->mImageUniformRange = RangeUI(imageRangeLow, imageRangeHigh); |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 416 | unsigned int imageBindingCount = stream.readInt<unsigned int>(); |
| 417 | for (unsigned int imageIndex = 0; imageIndex < imageBindingCount; ++imageIndex) |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 418 | { |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 419 | unsigned int elementCount = stream.readInt<unsigned int>(); |
| 420 | ImageBinding imageBinding(elementCount); |
| 421 | for (unsigned int i = 0; i < elementCount; ++i) |
| 422 | { |
| 423 | imageBinding.boundImageUnits[i] = stream.readInt<unsigned int>(); |
| 424 | } |
| 425 | state->mImageBindings.emplace_back(imageBinding); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 426 | } |
| 427 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 428 | unsigned int atomicCounterRangeLow = stream.readInt<unsigned int>(); |
| 429 | unsigned int atomicCounterRangeHigh = stream.readInt<unsigned int>(); |
| 430 | state->mAtomicCounterUniformRange = RangeUI(atomicCounterRangeLow, atomicCounterRangeHigh); |
| 431 | |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 432 | static_assert(static_cast<unsigned long>(ShaderType::EnumCount) <= sizeof(unsigned long) * 8, |
| 433 | "Too many shader types"); |
Jiawei Shao | 3dd8d291 | 2018-03-30 09:39:09 +0800 | [diff] [blame] | 434 | state->mLinkedShaderStages = stream.readInt<gl::ShaderBitSet>(); |
Yunchao He | 85072e8 | 2017-11-14 15:43:28 +0800 | [diff] [blame] | 435 | |
James Darpinian | 30b604d | 2018-03-12 17:26:57 -0700 | [diff] [blame] | 436 | state->updateTransformFeedbackStrides(); |
| 437 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 438 | return program->getImplementation()->load(context, infoLog, &stream); |
| 439 | } |
| 440 | |
| 441 | // static |
| 442 | void MemoryProgramCache::Serialize(const Context *context, |
| 443 | const gl::Program *program, |
| 444 | angle::MemoryBuffer *binaryOut) |
| 445 | { |
| 446 | BinaryOutputStream stream; |
| 447 | |
| 448 | stream.writeBytes(reinterpret_cast<const unsigned char *>(ANGLE_COMMIT_HASH), |
| 449 | ANGLE_COMMIT_HASH_SIZE); |
| 450 | |
| 451 | // nullptr context is supported when computing binary length. |
| 452 | if (context) |
| 453 | { |
| 454 | stream.writeInt(context->getClientVersion().major); |
| 455 | stream.writeInt(context->getClientVersion().minor); |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | stream.writeInt(2); |
| 460 | stream.writeInt(0); |
| 461 | } |
| 462 | |
| 463 | const auto &state = program->getState(); |
| 464 | |
| 465 | const auto &computeLocalSize = state.getComputeShaderLocalSize(); |
| 466 | |
| 467 | stream.writeInt(computeLocalSize[0]); |
| 468 | stream.writeInt(computeLocalSize[1]); |
| 469 | stream.writeInt(computeLocalSize[2]); |
| 470 | |
Jiawei Shao | 4ed05da | 2018-02-02 14:26:15 +0800 | [diff] [blame] | 471 | ASSERT(state.mGeometryShaderInvocations >= 1 && state.mGeometryShaderMaxVertices >= 0); |
| 472 | stream.writeInt(state.mGeometryShaderInputPrimitiveType); |
| 473 | stream.writeInt(state.mGeometryShaderOutputPrimitiveType); |
| 474 | stream.writeInt(state.mGeometryShaderInvocations); |
| 475 | stream.writeInt(state.mGeometryShaderMaxVertices); |
| 476 | |
Martin Radev | 7cf6166 | 2017-07-26 17:10:53 +0300 | [diff] [blame] | 477 | stream.writeInt(state.mNumViews); |
| 478 | |
Brandon Jones | c405ae7 | 2017-12-06 14:15:03 -0800 | [diff] [blame] | 479 | static_assert(MAX_VERTEX_ATTRIBS * 2 <= sizeof(uint32_t) * 8, |
| 480 | "All bits of mAttributesTypeMask types and mask fit into 32 bits each"); |
| 481 | stream.writeInt(static_cast<int>(state.mAttributesTypeMask.to_ulong())); |
| 482 | stream.writeInt(static_cast<int>(state.mAttributesMask.to_ulong())); |
| 483 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 484 | stream.writeInt(state.getActiveAttribLocationsMask().to_ulong()); |
| 485 | |
| 486 | stream.writeInt(state.getAttributes().size()); |
| 487 | for (const sh::Attribute &attrib : state.getAttributes()) |
| 488 | { |
| 489 | WriteShaderVar(&stream, attrib); |
| 490 | stream.writeInt(attrib.location); |
| 491 | } |
| 492 | |
| 493 | stream.writeInt(state.getUniforms().size()); |
| 494 | for (const LinkedUniform &uniform : state.getUniforms()) |
| 495 | { |
| 496 | WriteShaderVar(&stream, uniform); |
| 497 | |
| 498 | // FIXME: referenced |
| 499 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 500 | stream.writeInt(uniform.bufferIndex); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 501 | stream.writeInt(uniform.blockInfo.offset); |
| 502 | stream.writeInt(uniform.blockInfo.arrayStride); |
| 503 | stream.writeInt(uniform.blockInfo.matrixStride); |
| 504 | stream.writeInt(uniform.blockInfo.isRowMajorMatrix); |
| 505 | } |
| 506 | |
| 507 | stream.writeInt(state.getUniformLocations().size()); |
| 508 | for (const auto &variable : state.getUniformLocations()) |
| 509 | { |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 510 | stream.writeInt(variable.arrayIndex); |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 511 | stream.writeIntOrNegOne(variable.index); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 512 | stream.writeInt(variable.ignored); |
| 513 | } |
| 514 | |
| 515 | stream.writeInt(state.getUniformBlocks().size()); |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 516 | for (const InterfaceBlock &uniformBlock : state.getUniformBlocks()) |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 517 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 518 | WriteInterfaceBlock(&stream, uniformBlock); |
| 519 | } |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 520 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 521 | stream.writeInt(state.getBufferVariables().size()); |
| 522 | for (const BufferVariable &bufferVariable : state.getBufferVariables()) |
| 523 | { |
| 524 | WriteBufferVariable(&stream, bufferVariable); |
| 525 | } |
| 526 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 527 | stream.writeInt(state.getShaderStorageBlocks().size()); |
| 528 | for (const InterfaceBlock &shaderStorageBlock : state.getShaderStorageBlocks()) |
| 529 | { |
| 530 | WriteInterfaceBlock(&stream, shaderStorageBlock); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 531 | } |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 532 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 533 | stream.writeInt(state.mAtomicCounterBuffers.size()); |
| 534 | for (const auto &atomicCounterBuffer : state.mAtomicCounterBuffers) |
| 535 | { |
| 536 | WriteShaderVariableBuffer(&stream, atomicCounterBuffer); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 537 | } |
| 538 | |
Jamie Madill | ffe00c0 | 2017-06-27 16:26:55 -0400 | [diff] [blame] | 539 | // Warn the app layer if saving a binary with unsupported transform feedback. |
| 540 | if (!state.getLinkedTransformFeedbackVaryings().empty() && |
| 541 | context->getWorkarounds().disableProgramCachingForTransformFeedback) |
| 542 | { |
| 543 | WARN() << "Saving program binary with transform feedback, which is not supported on this " |
| 544 | "driver."; |
| 545 | } |
| 546 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 547 | stream.writeInt(state.getLinkedTransformFeedbackVaryings().size()); |
| 548 | for (const auto &var : state.getLinkedTransformFeedbackVaryings()) |
| 549 | { |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 550 | stream.writeIntVector(var.arraySizes); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 551 | stream.writeInt(var.type); |
| 552 | stream.writeString(var.name); |
| 553 | |
| 554 | stream.writeIntOrNegOne(var.arrayIndex); |
| 555 | } |
| 556 | |
| 557 | stream.writeInt(state.getTransformFeedbackBufferMode()); |
| 558 | |
| 559 | stream.writeInt(state.getOutputVariables().size()); |
| 560 | for (const sh::OutputVariable &output : state.getOutputVariables()) |
| 561 | { |
| 562 | WriteShaderVar(&stream, output); |
| 563 | stream.writeInt(output.location); |
| 564 | } |
| 565 | |
| 566 | stream.writeInt(state.getOutputLocations().size()); |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 567 | for (const auto &outputVar : state.getOutputLocations()) |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 568 | { |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 569 | stream.writeInt(outputVar.arrayIndex); |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 570 | stream.writeIntOrNegOne(outputVar.index); |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 571 | stream.writeInt(outputVar.ignored); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | stream.writeInt(state.mOutputVariableTypes.size()); |
| 575 | for (const auto &outputVariableType : state.mOutputVariableTypes) |
| 576 | { |
| 577 | stream.writeInt(outputVariableType); |
| 578 | } |
| 579 | |
Brandon Jones | c405ae7 | 2017-12-06 14:15:03 -0800 | [diff] [blame] | 580 | static_assert( |
| 581 | IMPLEMENTATION_MAX_DRAW_BUFFERS * 2 <= 8 * sizeof(uint32_t), |
| 582 | "All bits of mDrawBufferTypeMask and mActiveOutputVariables can be contained in 32 bits"); |
| 583 | stream.writeInt(static_cast<int>(state.mDrawBufferTypeMask.to_ulong())); |
| 584 | stream.writeInt(static_cast<int>(state.mActiveOutputVariables.to_ulong())); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 585 | |
| 586 | stream.writeInt(state.getSamplerUniformRange().low()); |
| 587 | stream.writeInt(state.getSamplerUniformRange().high()); |
| 588 | |
| 589 | stream.writeInt(state.getSamplerBindings().size()); |
| 590 | for (const auto &samplerBinding : state.getSamplerBindings()) |
| 591 | { |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 592 | stream.writeEnum(samplerBinding.textureType); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 593 | stream.writeInt(samplerBinding.boundTextureUnits.size()); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 594 | stream.writeInt(samplerBinding.unreferenced); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 595 | } |
| 596 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 597 | stream.writeInt(state.getImageUniformRange().low()); |
| 598 | stream.writeInt(state.getImageUniformRange().high()); |
| 599 | |
| 600 | stream.writeInt(state.getImageBindings().size()); |
| 601 | for (const auto &imageBinding : state.getImageBindings()) |
| 602 | { |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 603 | stream.writeInt(imageBinding.boundImageUnits.size()); |
| 604 | for (size_t i = 0; i < imageBinding.boundImageUnits.size(); ++i) |
| 605 | { |
| 606 | stream.writeInt(imageBinding.boundImageUnits[i]); |
| 607 | } |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 608 | } |
| 609 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 610 | stream.writeInt(state.getAtomicCounterUniformRange().low()); |
| 611 | stream.writeInt(state.getAtomicCounterUniformRange().high()); |
| 612 | |
Yunchao He | 85072e8 | 2017-11-14 15:43:28 +0800 | [diff] [blame] | 613 | stream.writeInt(state.getLinkedShaderStages().to_ulong()); |
| 614 | |
Jamie Madill | 27a6063 | 2017-06-30 15:12:01 -0400 | [diff] [blame] | 615 | program->getImplementation()->save(context, &stream); |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 616 | |
| 617 | ASSERT(binaryOut); |
| 618 | binaryOut->resize(stream.length()); |
| 619 | memcpy(binaryOut->data(), stream.data(), stream.length()); |
| 620 | } |
| 621 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 622 | // static |
| 623 | void MemoryProgramCache::ComputeHash(const Context *context, |
| 624 | const Program *program, |
| 625 | ProgramHash *hashOut) |
| 626 | { |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 627 | // Compute the program hash. Start with the shader hashes and resource strings. |
| 628 | HashStream hashStream; |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 629 | for (ShaderType shaderType : AllShaderTypes()) |
| 630 | { |
| 631 | hashStream << program->getAttachedShader(shaderType); |
| 632 | } |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 633 | |
| 634 | // Add some ANGLE metadata and Context properties, such as version and back-end. |
| 635 | hashStream << ANGLE_COMMIT_HASH << context->getClientMajorVersion() |
| 636 | << context->getClientMinorVersion() << context->getString(GL_RENDERER); |
| 637 | |
| 638 | // Hash pre-link program properties. |
| 639 | hashStream << program->getAttributeBindings() << program->getUniformLocationBindings() |
| 640 | << program->getFragmentInputBindings() |
| 641 | << program->getState().getTransformFeedbackVaryingNames() |
| 642 | << program->getState().getTransformFeedbackBufferMode(); |
| 643 | |
| 644 | // Call the secure SHA hashing function. |
| 645 | const std::string &programKey = hashStream.str(); |
| 646 | angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(programKey.c_str()), |
| 647 | programKey.length(), hashOut->data()); |
| 648 | } |
| 649 | |
| 650 | LinkResult MemoryProgramCache::getProgram(const Context *context, |
| 651 | const Program *program, |
| 652 | ProgramState *state, |
| 653 | ProgramHash *hashOut) |
| 654 | { |
| 655 | ComputeHash(context, program, hashOut); |
| 656 | const angle::MemoryBuffer *binaryProgram = nullptr; |
| 657 | LinkResult result(false); |
| 658 | if (get(*hashOut, &binaryProgram)) |
| 659 | { |
| 660 | InfoLog infoLog; |
| 661 | ANGLE_TRY_RESULT(Deserialize(context, program, state, binaryProgram->data(), |
| 662 | binaryProgram->size(), infoLog), |
| 663 | result); |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 664 | ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", result.getResult()); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 665 | if (!result.getResult()) |
| 666 | { |
| 667 | // Cache load failed, evict. |
| 668 | if (mIssuedWarnings++ < kWarningLimit) |
| 669 | { |
| 670 | WARN() << "Failed to load binary from cache: " << infoLog.str(); |
| 671 | |
| 672 | if (mIssuedWarnings == kWarningLimit) |
| 673 | { |
| 674 | WARN() << "Reaching warning limit for cache load failures, silencing " |
| 675 | "subsequent warnings."; |
| 676 | } |
| 677 | } |
| 678 | remove(*hashOut); |
| 679 | } |
| 680 | } |
| 681 | return result; |
| 682 | } |
| 683 | |
| 684 | bool MemoryProgramCache::get(const ProgramHash &programHash, const angle::MemoryBuffer **programOut) |
| 685 | { |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 686 | const CacheEntry *entry = nullptr; |
| 687 | if (!mProgramBinaryCache.get(programHash, &entry)) |
| 688 | { |
| 689 | ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheMiss, |
| 690 | kCacheResultMax); |
| 691 | return false; |
| 692 | } |
| 693 | |
| 694 | if (entry->second == CacheSource::PutProgram) |
| 695 | { |
| 696 | ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheHitMemory, |
| 697 | kCacheResultMax); |
| 698 | } |
| 699 | else |
| 700 | { |
| 701 | ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheHitDisk, |
| 702 | kCacheResultMax); |
| 703 | } |
| 704 | |
| 705 | *programOut = &entry->first; |
| 706 | return true; |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 707 | } |
| 708 | |
Jamie Madill | c43be72 | 2017-07-13 16:22:14 -0400 | [diff] [blame] | 709 | bool MemoryProgramCache::getAt(size_t index, |
| 710 | ProgramHash *hashOut, |
| 711 | const angle::MemoryBuffer **programOut) |
| 712 | { |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 713 | const CacheEntry *entry = nullptr; |
| 714 | if (!mProgramBinaryCache.getAt(index, hashOut, &entry)) |
| 715 | { |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | *programOut = &entry->first; |
| 720 | return true; |
Jamie Madill | c43be72 | 2017-07-13 16:22:14 -0400 | [diff] [blame] | 721 | } |
| 722 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 723 | void MemoryProgramCache::remove(const ProgramHash &programHash) |
| 724 | { |
| 725 | bool result = mProgramBinaryCache.eraseByKey(programHash); |
| 726 | ASSERT(result); |
| 727 | } |
| 728 | |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 729 | void MemoryProgramCache::putProgram(const ProgramHash &programHash, |
| 730 | const Context *context, |
| 731 | const Program *program) |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 732 | { |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 733 | CacheEntry newEntry; |
| 734 | Serialize(context, program, &newEntry.first); |
| 735 | newEntry.second = CacheSource::PutProgram; |
| 736 | |
| 737 | ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramBinarySizeBytes", |
| 738 | static_cast<int>(newEntry.first.size())); |
| 739 | |
| 740 | const CacheEntry *result = |
| 741 | mProgramBinaryCache.put(programHash, std::move(newEntry), newEntry.first.size()); |
Jamie Madill | 360daee | 2017-06-29 10:36:19 -0400 | [diff] [blame] | 742 | if (!result) |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 743 | { |
| 744 | ERR() << "Failed to store binary program in memory cache, program is too large."; |
| 745 | } |
Jamie Madill | 360daee | 2017-06-29 10:36:19 -0400 | [diff] [blame] | 746 | else |
| 747 | { |
| 748 | auto *platform = ANGLEPlatformCurrent(); |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 749 | platform->cacheProgram(platform, programHash, result->first.size(), result->first.data()); |
Jamie Madill | 360daee | 2017-06-29 10:36:19 -0400 | [diff] [blame] | 750 | } |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 751 | } |
| 752 | |
Jamie Madill | 4c19a8a | 2017-07-24 11:46:06 -0400 | [diff] [blame] | 753 | void MemoryProgramCache::updateProgram(const Context *context, const Program *program) |
| 754 | { |
| 755 | gl::ProgramHash programHash; |
| 756 | ComputeHash(context, program, &programHash); |
| 757 | putProgram(programHash, context, program); |
| 758 | } |
| 759 | |
Jamie Madill | c43be72 | 2017-07-13 16:22:14 -0400 | [diff] [blame] | 760 | void MemoryProgramCache::putBinary(const ProgramHash &programHash, |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 761 | const uint8_t *binary, |
| 762 | size_t length) |
| 763 | { |
| 764 | // Copy the binary. |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 765 | CacheEntry newEntry; |
| 766 | newEntry.first.resize(length); |
| 767 | memcpy(newEntry.first.data(), binary, length); |
| 768 | newEntry.second = CacheSource::PutBinary; |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 769 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 770 | // Store the binary. |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 771 | const CacheEntry *result = mProgramBinaryCache.put(programHash, std::move(newEntry), length); |
Jamie Madill | c43be72 | 2017-07-13 16:22:14 -0400 | [diff] [blame] | 772 | if (!result) |
| 773 | { |
| 774 | ERR() << "Failed to store binary program in memory cache, program is too large."; |
| 775 | } |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | void MemoryProgramCache::clear() |
| 779 | { |
| 780 | mProgramBinaryCache.clear(); |
| 781 | mIssuedWarnings = 0; |
| 782 | } |
| 783 | |
Jamie Madill | c43be72 | 2017-07-13 16:22:14 -0400 | [diff] [blame] | 784 | void MemoryProgramCache::resize(size_t maxCacheSizeBytes) |
| 785 | { |
| 786 | mProgramBinaryCache.resize(maxCacheSizeBytes); |
| 787 | } |
| 788 | |
| 789 | size_t MemoryProgramCache::entryCount() const |
| 790 | { |
| 791 | return mProgramBinaryCache.entryCount(); |
| 792 | } |
| 793 | |
| 794 | size_t MemoryProgramCache::trim(size_t limit) |
| 795 | { |
| 796 | return mProgramBinaryCache.shrinkToSize(limit); |
| 797 | } |
| 798 | |
| 799 | size_t MemoryProgramCache::size() const |
| 800 | { |
| 801 | return mProgramBinaryCache.size(); |
| 802 | } |
| 803 | |
| 804 | size_t MemoryProgramCache::maxSize() const |
| 805 | { |
| 806 | return mProgramBinaryCache.maxSize(); |
| 807 | } |
| 808 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 809 | } // namespace gl |