blob: e27d426cb2599039ddc133dbf4decca9939848a1 [file] [log] [blame]
Jamie Madill4f86d052017-06-05 12:59:26 -04001//
2// Copyright 2017 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// MemoryProgramCache: Stores compiled and linked programs in memory so they don't
7// always have to be re-compiled. Can be used in conjunction with the platform
8// layer to warm up the cache from disk.
9
10#include "libANGLE/MemoryProgramCache.h"
11
12#include <GLSLANG/ShaderVars.h>
Jamie Madill32447362017-06-28 14:53:52 -040013#include <anglebase/sha1.h>
Jamie Madill4f86d052017-06-05 12:59:26 -040014
Jamie Madillf00f7ff2017-08-31 14:39:15 -040015#include "common/utilities.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040016#include "common/version.h"
17#include "libANGLE/BinaryStream.h"
18#include "libANGLE/Context.h"
19#include "libANGLE/Uniform.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040020#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040021#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill360daee2017-06-29 10:36:19 -040022#include "platform/Platform.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040023
24namespace gl
25{
26
27namespace
28{
Jamie Madill6c58b062017-08-01 13:44:25 -040029enum CacheResult
30{
31 kCacheMiss,
32 kCacheHitMemory,
33 kCacheHitDisk,
34 kCacheResultMax,
35};
36
Jamie Madill32447362017-06-28 14:53:52 -040037constexpr unsigned int kWarningLimit = 3;
Jamie Madill4f86d052017-06-05 12:59:26 -040038
39void 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 Etuaho465835d2017-09-26 13:34:10 +030045 stream->writeIntVector(var.arraySizes);
Jamie Madill4f86d052017-06-05 12:59:26 -040046 stream->writeInt(var.staticUse);
Olli Etuaho107c7242018-03-20 15:45:35 +020047 stream->writeInt(var.active);
Jamie Madill4f86d052017-06-05 12:59:26 -040048 stream->writeString(var.structName);
49 ASSERT(var.fields.empty());
50}
51
52void 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 Etuaho465835d2017-09-26 13:34:10 +030058 stream->readIntVector<unsigned int>(&var->arraySizes);
Jamie Madill4f86d052017-06-05 12:59:26 -040059 var->staticUse = stream->readBool();
Olli Etuaho107c7242018-03-20 15:45:35 +020060 var->active = stream->readBool();
Jamie Madill4f86d052017-06-05 12:59:26 -040061 var->structName = stream->readString();
62}
63
jchen10eaef1e52017-06-13 10:44:11 +080064void WriteShaderVariableBuffer(BinaryOutputStream *stream, const ShaderVariableBuffer &var)
65{
66 stream->writeInt(var.binding);
67 stream->writeInt(var.dataSize);
68
Olli Etuaho107c7242018-03-20 15:45:35 +020069 stream->writeInt(var.vertexActive);
70 stream->writeInt(var.fragmentActive);
71 stream->writeInt(var.computeActive);
72 stream->writeInt(var.geometryActive);
jchen10eaef1e52017-06-13 10:44:11 +080073
74 stream->writeInt(var.memberIndexes.size());
75 for (unsigned int memberCounterIndex : var.memberIndexes)
76 {
77 stream->writeInt(memberCounterIndex);
78 }
79}
80
81void LoadShaderVariableBuffer(BinaryInputStream *stream, ShaderVariableBuffer *var)
82{
83 var->binding = stream->readInt<int>();
84 var->dataSize = stream->readInt<unsigned int>();
Olli Etuaho107c7242018-03-20 15:45:35 +020085 var->vertexActive = stream->readBool();
86 var->fragmentActive = stream->readBool();
87 var->computeActive = stream->readBool();
88 var->geometryActive = stream->readBool();
jchen10eaef1e52017-06-13 10:44:11 +080089
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 Qin3a9090f2017-09-27 14:37:04 +080097void 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 Etuaho107c7242018-03-20 15:45:35 +0200108 stream->writeInt(var.vertexActive);
109 stream->writeInt(var.fragmentActive);
110 stream->writeInt(var.computeActive);
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800111}
112
113void 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 Etuaho107c7242018-03-20 15:45:35 +0200124 var->vertexActive = stream->readBool();
125 var->fragmentActive = stream->readBool();
126 var->computeActive = stream->readBool();
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800127}
128
Jiajia Qin729b2c62017-08-14 09:36:11 +0800129void 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
139void 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 Madill32447362017-06-28 14:53:52 -0400149class 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
166HashStream &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 Madill3c1da042017-11-27 18:33:40 -0500176HashStream &operator<<(HashStream &stream, const ProgramBindings &bindings)
Jamie Madill32447362017-06-28 14:53:52 -0400177{
178 for (const auto &binding : bindings)
179 {
180 stream << binding.first << binding.second;
181 }
182 return stream;
183}
184
185HashStream &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 Madill4f86d052017-06-05 12:59:26 -0400194} // anonymous namespace
195
Jamie Madill32447362017-06-28 14:53:52 -0400196MemoryProgramCache::MemoryProgramCache(size_t maxCacheSizeBytes)
197 : mProgramBinaryCache(maxCacheSizeBytes), mIssuedWarnings(0)
198{
199}
200
201MemoryProgramCache::~MemoryProgramCache()
202{
203}
204
Jamie Madill4f86d052017-06-05 12:59:26 -0400205// static
206LinkResult 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 Shao4ed05da2018-02-02 14:26:15 +0800237 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 Radev7cf61662017-07-26 17:10:53 +0300242 state->mNumViews = stream.readInt<int>();
243
Brandon Jonesc405ae72017-12-06 14:15:03 -0800244 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 Madill4f86d052017-06-05 12:59:26 -0400249 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
jchen10eaef1e52017-06-13 10:44:11 +0800270 uniform.bufferIndex = stream.readInt<int>();
Jamie Madill4f86d052017-06-05 12:59:26 -0400271 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 Madillf00f7ff2017-08-31 14:39:15 -0400276 uniform.typeInfo = &GetUniformTypeInfo(uniform.type);
277
Jamie Madill4f86d052017-06-05 12:59:26 -0400278 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 Etuaho1734e172017-10-27 15:30:27 +0300287 stream.readInt(&variable.arrayIndex);
Jamie Madill4f86d052017-06-05 12:59:26 -0400288 stream.readInt(&variable.index);
Jamie Madill4f86d052017-06-05 12:59:26 -0400289 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 Qin729b2c62017-08-14 09:36:11 +0800299 InterfaceBlock uniformBlock;
300 LoadInterfaceBlock(&stream, &uniformBlock);
Jamie Madill4f86d052017-06-05 12:59:26 -0400301 state->mUniformBlocks.push_back(uniformBlock);
Jamie Madill4f86d052017-06-05 12:59:26 -0400302
jchen107a20b972017-06-13 14:25:26 +0800303 state->mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlock.binding != 0);
Jamie Madill4f86d052017-06-05 12:59:26 -0400304 }
Jiajia Qin729b2c62017-08-14 09:36:11 +0800305
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800306 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 Qin729b2c62017-08-14 09:36:11 +0800315 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
jchen10eaef1e52017-06-13 10:44:11 +0800325 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 Madill4f86d052017-06-05 12:59:26 -0400334
335 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madillffe00c02017-06-27 16:26:55 -0400336
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 Madill4f86d052017-06-05 12:59:26 -0400345 ASSERT(state->mLinkedTransformFeedbackVaryings.empty());
346 for (unsigned int transformFeedbackVaryingIndex = 0;
347 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
348 ++transformFeedbackVaryingIndex)
349 {
350 sh::Varying varying;
Olli Etuaho465835d2017-09-26 13:34:10 +0300351 stream.readIntVector<unsigned int>(&varying.arraySizes);
Jamie Madill4f86d052017-06-05 12:59:26 -0400352 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 Etuahod2551232017-10-26 20:03:33 +0300373 ASSERT(state->mOutputLocations.empty());
Jamie Madill4f86d052017-06-05 12:59:26 -0400374 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
375 {
Jamie Madill4f86d052017-06-05 12:59:26 -0400376 VariableLocation locationData;
Olli Etuaho1734e172017-10-27 15:30:27 +0300377 stream.readInt(&locationData.arrayIndex);
Jamie Madill4f86d052017-06-05 12:59:26 -0400378 stream.readInt(&locationData.index);
Jamie Madillfb997ec2017-09-20 15:44:27 -0400379 stream.readBool(&locationData.ignored);
Olli Etuahod2551232017-10-26 20:03:33 +0300380 state->mOutputLocations.push_back(locationData);
Jamie Madill4f86d052017-06-05 12:59:26 -0400381 }
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 Jones76746f92017-11-22 11:44:41 -0800388
Brandon Jonesc405ae72017-12-06 14:15:03 -0800389 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 Madill4f86d052017-06-05 12:59:26 -0400393 state->mActiveOutputVariables = stream.readInt<uint32_t>();
394
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800395 unsigned int samplerRangeLow = stream.readInt<unsigned int>();
396 unsigned int samplerRangeHigh = stream.readInt<unsigned int>();
397 state->mSamplerUniformRange = RangeUI(samplerRangeLow, samplerRangeHigh);
Jamie Madill4f86d052017-06-05 12:59:26 -0400398 unsigned int samplerCount = stream.readInt<unsigned int>();
399 for (unsigned int samplerIndex = 0; samplerIndex < samplerCount; ++samplerIndex)
400 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800401 TextureType textureType = stream.readEnum<TextureType>();
Jamie Madill4f86d052017-06-05 12:59:26 -0400402 size_t bindingCount = stream.readInt<size_t>();
Jamie Madill54164b02017-08-28 15:17:37 -0400403 bool unreferenced = stream.readBool();
404 state->mSamplerBindings.emplace_back(
405 SamplerBinding(textureType, bindingCount, unreferenced));
Jamie Madill4f86d052017-06-05 12:59:26 -0400406 }
407
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800408 unsigned int imageRangeLow = stream.readInt<unsigned int>();
409 unsigned int imageRangeHigh = stream.readInt<unsigned int>();
410 state->mImageUniformRange = RangeUI(imageRangeLow, imageRangeHigh);
Xinghua Cao0328b572017-06-26 15:51:36 +0800411 unsigned int imageBindingCount = stream.readInt<unsigned int>();
412 for (unsigned int imageIndex = 0; imageIndex < imageBindingCount; ++imageIndex)
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800413 {
Xinghua Cao0328b572017-06-26 15:51:36 +0800414 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 Cao65ec0b22017-03-28 16:10:52 +0800421 }
422
jchen10eaef1e52017-06-13 10:44:11 +0800423 unsigned int atomicCounterRangeLow = stream.readInt<unsigned int>();
424 unsigned int atomicCounterRangeHigh = stream.readInt<unsigned int>();
425 state->mAtomicCounterUniformRange = RangeUI(atomicCounterRangeLow, atomicCounterRangeHigh);
426
Jiawei Shao385b3e02018-03-21 09:43:28 +0800427 static_assert(static_cast<unsigned long>(ShaderType::EnumCount) <= sizeof(unsigned long) * 8,
428 "Too many shader types");
Yunchao He85072e82017-11-14 15:43:28 +0800429 state->mLinkedShaderStages = stream.readInt<unsigned long>();
430
Jamie Madill4f86d052017-06-05 12:59:26 -0400431 return program->getImplementation()->load(context, infoLog, &stream);
432}
433
434// static
435void MemoryProgramCache::Serialize(const Context *context,
436 const gl::Program *program,
437 angle::MemoryBuffer *binaryOut)
438{
439 BinaryOutputStream stream;
440
441 stream.writeBytes(reinterpret_cast<const unsigned char *>(ANGLE_COMMIT_HASH),
442 ANGLE_COMMIT_HASH_SIZE);
443
444 // nullptr context is supported when computing binary length.
445 if (context)
446 {
447 stream.writeInt(context->getClientVersion().major);
448 stream.writeInt(context->getClientVersion().minor);
449 }
450 else
451 {
452 stream.writeInt(2);
453 stream.writeInt(0);
454 }
455
456 const auto &state = program->getState();
457
458 const auto &computeLocalSize = state.getComputeShaderLocalSize();
459
460 stream.writeInt(computeLocalSize[0]);
461 stream.writeInt(computeLocalSize[1]);
462 stream.writeInt(computeLocalSize[2]);
463
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800464 ASSERT(state.mGeometryShaderInvocations >= 1 && state.mGeometryShaderMaxVertices >= 0);
465 stream.writeInt(state.mGeometryShaderInputPrimitiveType);
466 stream.writeInt(state.mGeometryShaderOutputPrimitiveType);
467 stream.writeInt(state.mGeometryShaderInvocations);
468 stream.writeInt(state.mGeometryShaderMaxVertices);
469
Martin Radev7cf61662017-07-26 17:10:53 +0300470 stream.writeInt(state.mNumViews);
471
Brandon Jonesc405ae72017-12-06 14:15:03 -0800472 static_assert(MAX_VERTEX_ATTRIBS * 2 <= sizeof(uint32_t) * 8,
473 "All bits of mAttributesTypeMask types and mask fit into 32 bits each");
474 stream.writeInt(static_cast<int>(state.mAttributesTypeMask.to_ulong()));
475 stream.writeInt(static_cast<int>(state.mAttributesMask.to_ulong()));
476
Jamie Madill4f86d052017-06-05 12:59:26 -0400477 stream.writeInt(state.getActiveAttribLocationsMask().to_ulong());
478
479 stream.writeInt(state.getAttributes().size());
480 for (const sh::Attribute &attrib : state.getAttributes())
481 {
482 WriteShaderVar(&stream, attrib);
483 stream.writeInt(attrib.location);
484 }
485
486 stream.writeInt(state.getUniforms().size());
487 for (const LinkedUniform &uniform : state.getUniforms())
488 {
489 WriteShaderVar(&stream, uniform);
490
491 // FIXME: referenced
492
jchen10eaef1e52017-06-13 10:44:11 +0800493 stream.writeInt(uniform.bufferIndex);
Jamie Madill4f86d052017-06-05 12:59:26 -0400494 stream.writeInt(uniform.blockInfo.offset);
495 stream.writeInt(uniform.blockInfo.arrayStride);
496 stream.writeInt(uniform.blockInfo.matrixStride);
497 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
498 }
499
500 stream.writeInt(state.getUniformLocations().size());
501 for (const auto &variable : state.getUniformLocations())
502 {
Olli Etuaho1734e172017-10-27 15:30:27 +0300503 stream.writeInt(variable.arrayIndex);
Jamie Madillfb997ec2017-09-20 15:44:27 -0400504 stream.writeIntOrNegOne(variable.index);
Jamie Madill4f86d052017-06-05 12:59:26 -0400505 stream.writeInt(variable.ignored);
506 }
507
508 stream.writeInt(state.getUniformBlocks().size());
Jiajia Qin729b2c62017-08-14 09:36:11 +0800509 for (const InterfaceBlock &uniformBlock : state.getUniformBlocks())
Jamie Madill4f86d052017-06-05 12:59:26 -0400510 {
Jiajia Qin729b2c62017-08-14 09:36:11 +0800511 WriteInterfaceBlock(&stream, uniformBlock);
512 }
Jamie Madill4f86d052017-06-05 12:59:26 -0400513
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800514 stream.writeInt(state.getBufferVariables().size());
515 for (const BufferVariable &bufferVariable : state.getBufferVariables())
516 {
517 WriteBufferVariable(&stream, bufferVariable);
518 }
519
Jiajia Qin729b2c62017-08-14 09:36:11 +0800520 stream.writeInt(state.getShaderStorageBlocks().size());
521 for (const InterfaceBlock &shaderStorageBlock : state.getShaderStorageBlocks())
522 {
523 WriteInterfaceBlock(&stream, shaderStorageBlock);
jchen10eaef1e52017-06-13 10:44:11 +0800524 }
Jamie Madill4f86d052017-06-05 12:59:26 -0400525
jchen10eaef1e52017-06-13 10:44:11 +0800526 stream.writeInt(state.mAtomicCounterBuffers.size());
527 for (const auto &atomicCounterBuffer : state.mAtomicCounterBuffers)
528 {
529 WriteShaderVariableBuffer(&stream, atomicCounterBuffer);
Jamie Madill4f86d052017-06-05 12:59:26 -0400530 }
531
Jamie Madillffe00c02017-06-27 16:26:55 -0400532 // Warn the app layer if saving a binary with unsupported transform feedback.
533 if (!state.getLinkedTransformFeedbackVaryings().empty() &&
534 context->getWorkarounds().disableProgramCachingForTransformFeedback)
535 {
536 WARN() << "Saving program binary with transform feedback, which is not supported on this "
537 "driver.";
538 }
539
Jamie Madill4f86d052017-06-05 12:59:26 -0400540 stream.writeInt(state.getLinkedTransformFeedbackVaryings().size());
541 for (const auto &var : state.getLinkedTransformFeedbackVaryings())
542 {
Olli Etuaho465835d2017-09-26 13:34:10 +0300543 stream.writeIntVector(var.arraySizes);
Jamie Madill4f86d052017-06-05 12:59:26 -0400544 stream.writeInt(var.type);
545 stream.writeString(var.name);
546
547 stream.writeIntOrNegOne(var.arrayIndex);
548 }
549
550 stream.writeInt(state.getTransformFeedbackBufferMode());
551
552 stream.writeInt(state.getOutputVariables().size());
553 for (const sh::OutputVariable &output : state.getOutputVariables())
554 {
555 WriteShaderVar(&stream, output);
556 stream.writeInt(output.location);
557 }
558
559 stream.writeInt(state.getOutputLocations().size());
Olli Etuahod2551232017-10-26 20:03:33 +0300560 for (const auto &outputVar : state.getOutputLocations())
Jamie Madill4f86d052017-06-05 12:59:26 -0400561 {
Olli Etuaho1734e172017-10-27 15:30:27 +0300562 stream.writeInt(outputVar.arrayIndex);
Olli Etuahod2551232017-10-26 20:03:33 +0300563 stream.writeIntOrNegOne(outputVar.index);
Olli Etuahod2551232017-10-26 20:03:33 +0300564 stream.writeInt(outputVar.ignored);
Jamie Madill4f86d052017-06-05 12:59:26 -0400565 }
566
567 stream.writeInt(state.mOutputVariableTypes.size());
568 for (const auto &outputVariableType : state.mOutputVariableTypes)
569 {
570 stream.writeInt(outputVariableType);
571 }
572
Brandon Jonesc405ae72017-12-06 14:15:03 -0800573 static_assert(
574 IMPLEMENTATION_MAX_DRAW_BUFFERS * 2 <= 8 * sizeof(uint32_t),
575 "All bits of mDrawBufferTypeMask and mActiveOutputVariables can be contained in 32 bits");
576 stream.writeInt(static_cast<int>(state.mDrawBufferTypeMask.to_ulong()));
577 stream.writeInt(static_cast<int>(state.mActiveOutputVariables.to_ulong()));
Jamie Madill4f86d052017-06-05 12:59:26 -0400578
579 stream.writeInt(state.getSamplerUniformRange().low());
580 stream.writeInt(state.getSamplerUniformRange().high());
581
582 stream.writeInt(state.getSamplerBindings().size());
583 for (const auto &samplerBinding : state.getSamplerBindings())
584 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800585 stream.writeEnum(samplerBinding.textureType);
Jamie Madill4f86d052017-06-05 12:59:26 -0400586 stream.writeInt(samplerBinding.boundTextureUnits.size());
Jamie Madill54164b02017-08-28 15:17:37 -0400587 stream.writeInt(samplerBinding.unreferenced);
Jamie Madill4f86d052017-06-05 12:59:26 -0400588 }
589
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800590 stream.writeInt(state.getImageUniformRange().low());
591 stream.writeInt(state.getImageUniformRange().high());
592
593 stream.writeInt(state.getImageBindings().size());
594 for (const auto &imageBinding : state.getImageBindings())
595 {
Xinghua Cao0328b572017-06-26 15:51:36 +0800596 stream.writeInt(imageBinding.boundImageUnits.size());
597 for (size_t i = 0; i < imageBinding.boundImageUnits.size(); ++i)
598 {
599 stream.writeInt(imageBinding.boundImageUnits[i]);
600 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800601 }
602
jchen10eaef1e52017-06-13 10:44:11 +0800603 stream.writeInt(state.getAtomicCounterUniformRange().low());
604 stream.writeInt(state.getAtomicCounterUniformRange().high());
605
Yunchao He85072e82017-11-14 15:43:28 +0800606 stream.writeInt(state.getLinkedShaderStages().to_ulong());
607
Jamie Madill27a60632017-06-30 15:12:01 -0400608 program->getImplementation()->save(context, &stream);
Jamie Madill4f86d052017-06-05 12:59:26 -0400609
610 ASSERT(binaryOut);
611 binaryOut->resize(stream.length());
612 memcpy(binaryOut->data(), stream.data(), stream.length());
613}
614
Jamie Madill32447362017-06-28 14:53:52 -0400615// static
616void MemoryProgramCache::ComputeHash(const Context *context,
617 const Program *program,
618 ProgramHash *hashOut)
619{
Jamie Madill32447362017-06-28 14:53:52 -0400620 // Compute the program hash. Start with the shader hashes and resource strings.
621 HashStream hashStream;
Jiawei Shao385b3e02018-03-21 09:43:28 +0800622 for (ShaderType shaderType : AllShaderTypes())
623 {
624 hashStream << program->getAttachedShader(shaderType);
625 }
Jamie Madill32447362017-06-28 14:53:52 -0400626
627 // Add some ANGLE metadata and Context properties, such as version and back-end.
628 hashStream << ANGLE_COMMIT_HASH << context->getClientMajorVersion()
629 << context->getClientMinorVersion() << context->getString(GL_RENDERER);
630
631 // Hash pre-link program properties.
632 hashStream << program->getAttributeBindings() << program->getUniformLocationBindings()
633 << program->getFragmentInputBindings()
634 << program->getState().getTransformFeedbackVaryingNames()
635 << program->getState().getTransformFeedbackBufferMode();
636
637 // Call the secure SHA hashing function.
638 const std::string &programKey = hashStream.str();
639 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(programKey.c_str()),
640 programKey.length(), hashOut->data());
641}
642
643LinkResult MemoryProgramCache::getProgram(const Context *context,
644 const Program *program,
645 ProgramState *state,
646 ProgramHash *hashOut)
647{
648 ComputeHash(context, program, hashOut);
649 const angle::MemoryBuffer *binaryProgram = nullptr;
650 LinkResult result(false);
651 if (get(*hashOut, &binaryProgram))
652 {
653 InfoLog infoLog;
654 ANGLE_TRY_RESULT(Deserialize(context, program, state, binaryProgram->data(),
655 binaryProgram->size(), infoLog),
656 result);
Jamie Madill6c58b062017-08-01 13:44:25 -0400657 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", result.getResult());
Jamie Madill32447362017-06-28 14:53:52 -0400658 if (!result.getResult())
659 {
660 // Cache load failed, evict.
661 if (mIssuedWarnings++ < kWarningLimit)
662 {
663 WARN() << "Failed to load binary from cache: " << infoLog.str();
664
665 if (mIssuedWarnings == kWarningLimit)
666 {
667 WARN() << "Reaching warning limit for cache load failures, silencing "
668 "subsequent warnings.";
669 }
670 }
671 remove(*hashOut);
672 }
673 }
674 return result;
675}
676
677bool MemoryProgramCache::get(const ProgramHash &programHash, const angle::MemoryBuffer **programOut)
678{
Jamie Madill6c58b062017-08-01 13:44:25 -0400679 const CacheEntry *entry = nullptr;
680 if (!mProgramBinaryCache.get(programHash, &entry))
681 {
682 ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheMiss,
683 kCacheResultMax);
684 return false;
685 }
686
687 if (entry->second == CacheSource::PutProgram)
688 {
689 ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheHitMemory,
690 kCacheResultMax);
691 }
692 else
693 {
694 ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheHitDisk,
695 kCacheResultMax);
696 }
697
698 *programOut = &entry->first;
699 return true;
Jamie Madill32447362017-06-28 14:53:52 -0400700}
701
Jamie Madillc43be722017-07-13 16:22:14 -0400702bool MemoryProgramCache::getAt(size_t index,
703 ProgramHash *hashOut,
704 const angle::MemoryBuffer **programOut)
705{
Jamie Madill6c58b062017-08-01 13:44:25 -0400706 const CacheEntry *entry = nullptr;
707 if (!mProgramBinaryCache.getAt(index, hashOut, &entry))
708 {
709 return false;
710 }
711
712 *programOut = &entry->first;
713 return true;
Jamie Madillc43be722017-07-13 16:22:14 -0400714}
715
Jamie Madill32447362017-06-28 14:53:52 -0400716void MemoryProgramCache::remove(const ProgramHash &programHash)
717{
718 bool result = mProgramBinaryCache.eraseByKey(programHash);
719 ASSERT(result);
720}
721
Jamie Madill6c58b062017-08-01 13:44:25 -0400722void MemoryProgramCache::putProgram(const ProgramHash &programHash,
723 const Context *context,
724 const Program *program)
Jamie Madill32447362017-06-28 14:53:52 -0400725{
Jamie Madill6c58b062017-08-01 13:44:25 -0400726 CacheEntry newEntry;
727 Serialize(context, program, &newEntry.first);
728 newEntry.second = CacheSource::PutProgram;
729
730 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramBinarySizeBytes",
731 static_cast<int>(newEntry.first.size()));
732
733 const CacheEntry *result =
734 mProgramBinaryCache.put(programHash, std::move(newEntry), newEntry.first.size());
Jamie Madill360daee2017-06-29 10:36:19 -0400735 if (!result)
Jamie Madill32447362017-06-28 14:53:52 -0400736 {
737 ERR() << "Failed to store binary program in memory cache, program is too large.";
738 }
Jamie Madill360daee2017-06-29 10:36:19 -0400739 else
740 {
741 auto *platform = ANGLEPlatformCurrent();
Jamie Madill6c58b062017-08-01 13:44:25 -0400742 platform->cacheProgram(platform, programHash, result->first.size(), result->first.data());
Jamie Madill360daee2017-06-29 10:36:19 -0400743 }
Jamie Madill32447362017-06-28 14:53:52 -0400744}
745
Jamie Madill4c19a8a2017-07-24 11:46:06 -0400746void MemoryProgramCache::updateProgram(const Context *context, const Program *program)
747{
748 gl::ProgramHash programHash;
749 ComputeHash(context, program, &programHash);
750 putProgram(programHash, context, program);
751}
752
Jamie Madillc43be722017-07-13 16:22:14 -0400753void MemoryProgramCache::putBinary(const ProgramHash &programHash,
Jamie Madill32447362017-06-28 14:53:52 -0400754 const uint8_t *binary,
755 size_t length)
756{
757 // Copy the binary.
Jamie Madill6c58b062017-08-01 13:44:25 -0400758 CacheEntry newEntry;
759 newEntry.first.resize(length);
760 memcpy(newEntry.first.data(), binary, length);
761 newEntry.second = CacheSource::PutBinary;
Jamie Madill32447362017-06-28 14:53:52 -0400762
Jamie Madill32447362017-06-28 14:53:52 -0400763 // Store the binary.
Jamie Madill6c58b062017-08-01 13:44:25 -0400764 const CacheEntry *result = mProgramBinaryCache.put(programHash, std::move(newEntry), length);
Jamie Madillc43be722017-07-13 16:22:14 -0400765 if (!result)
766 {
767 ERR() << "Failed to store binary program in memory cache, program is too large.";
768 }
Jamie Madill32447362017-06-28 14:53:52 -0400769}
770
771void MemoryProgramCache::clear()
772{
773 mProgramBinaryCache.clear();
774 mIssuedWarnings = 0;
775}
776
Jamie Madillc43be722017-07-13 16:22:14 -0400777void MemoryProgramCache::resize(size_t maxCacheSizeBytes)
778{
779 mProgramBinaryCache.resize(maxCacheSizeBytes);
780}
781
782size_t MemoryProgramCache::entryCount() const
783{
784 return mProgramBinaryCache.entryCount();
785}
786
787size_t MemoryProgramCache::trim(size_t limit)
788{
789 return mProgramBinaryCache.shrinkToSize(limit);
790}
791
792size_t MemoryProgramCache::size() const
793{
794 return mProgramBinaryCache.size();
795}
796
797size_t MemoryProgramCache::maxSize() const
798{
799 return mProgramBinaryCache.maxSize();
800}
801
Jamie Madill4f86d052017-06-05 12:59:26 -0400802} // namespace gl