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