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