blob: f7d4e6c8302fe37cd4c92a0c6d7ef931a379b80a [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
Yunchao He85072e82017-11-14 15:43:28 +0800427 static_assert(SHADER_TYPE_MAX <= sizeof(unsigned long) * 8, "Too many shader types");
428 state->mLinkedShaderStages = stream.readInt<unsigned long>();
429
Jamie Madill4f86d052017-06-05 12:59:26 -0400430 return program->getImplementation()->load(context, infoLog, &stream);
431}
432
433// static
434void MemoryProgramCache::Serialize(const Context *context,
435 const gl::Program *program,
436 angle::MemoryBuffer *binaryOut)
437{
438 BinaryOutputStream stream;
439
440 stream.writeBytes(reinterpret_cast<const unsigned char *>(ANGLE_COMMIT_HASH),
441 ANGLE_COMMIT_HASH_SIZE);
442
443 // nullptr context is supported when computing binary length.
444 if (context)
445 {
446 stream.writeInt(context->getClientVersion().major);
447 stream.writeInt(context->getClientVersion().minor);
448 }
449 else
450 {
451 stream.writeInt(2);
452 stream.writeInt(0);
453 }
454
455 const auto &state = program->getState();
456
457 const auto &computeLocalSize = state.getComputeShaderLocalSize();
458
459 stream.writeInt(computeLocalSize[0]);
460 stream.writeInt(computeLocalSize[1]);
461 stream.writeInt(computeLocalSize[2]);
462
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800463 ASSERT(state.mGeometryShaderInvocations >= 1 && state.mGeometryShaderMaxVertices >= 0);
464 stream.writeInt(state.mGeometryShaderInputPrimitiveType);
465 stream.writeInt(state.mGeometryShaderOutputPrimitiveType);
466 stream.writeInt(state.mGeometryShaderInvocations);
467 stream.writeInt(state.mGeometryShaderMaxVertices);
468
Martin Radev7cf61662017-07-26 17:10:53 +0300469 stream.writeInt(state.mNumViews);
470
Brandon Jonesc405ae72017-12-06 14:15:03 -0800471 static_assert(MAX_VERTEX_ATTRIBS * 2 <= sizeof(uint32_t) * 8,
472 "All bits of mAttributesTypeMask types and mask fit into 32 bits each");
473 stream.writeInt(static_cast<int>(state.mAttributesTypeMask.to_ulong()));
474 stream.writeInt(static_cast<int>(state.mAttributesMask.to_ulong()));
475
Jamie Madill4f86d052017-06-05 12:59:26 -0400476 stream.writeInt(state.getActiveAttribLocationsMask().to_ulong());
477
478 stream.writeInt(state.getAttributes().size());
479 for (const sh::Attribute &attrib : state.getAttributes())
480 {
481 WriteShaderVar(&stream, attrib);
482 stream.writeInt(attrib.location);
483 }
484
485 stream.writeInt(state.getUniforms().size());
486 for (const LinkedUniform &uniform : state.getUniforms())
487 {
488 WriteShaderVar(&stream, uniform);
489
490 // FIXME: referenced
491
jchen10eaef1e52017-06-13 10:44:11 +0800492 stream.writeInt(uniform.bufferIndex);
Jamie Madill4f86d052017-06-05 12:59:26 -0400493 stream.writeInt(uniform.blockInfo.offset);
494 stream.writeInt(uniform.blockInfo.arrayStride);
495 stream.writeInt(uniform.blockInfo.matrixStride);
496 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
497 }
498
499 stream.writeInt(state.getUniformLocations().size());
500 for (const auto &variable : state.getUniformLocations())
501 {
Olli Etuaho1734e172017-10-27 15:30:27 +0300502 stream.writeInt(variable.arrayIndex);
Jamie Madillfb997ec2017-09-20 15:44:27 -0400503 stream.writeIntOrNegOne(variable.index);
Jamie Madill4f86d052017-06-05 12:59:26 -0400504 stream.writeInt(variable.ignored);
505 }
506
507 stream.writeInt(state.getUniformBlocks().size());
Jiajia Qin729b2c62017-08-14 09:36:11 +0800508 for (const InterfaceBlock &uniformBlock : state.getUniformBlocks())
Jamie Madill4f86d052017-06-05 12:59:26 -0400509 {
Jiajia Qin729b2c62017-08-14 09:36:11 +0800510 WriteInterfaceBlock(&stream, uniformBlock);
511 }
Jamie Madill4f86d052017-06-05 12:59:26 -0400512
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800513 stream.writeInt(state.getBufferVariables().size());
514 for (const BufferVariable &bufferVariable : state.getBufferVariables())
515 {
516 WriteBufferVariable(&stream, bufferVariable);
517 }
518
Jiajia Qin729b2c62017-08-14 09:36:11 +0800519 stream.writeInt(state.getShaderStorageBlocks().size());
520 for (const InterfaceBlock &shaderStorageBlock : state.getShaderStorageBlocks())
521 {
522 WriteInterfaceBlock(&stream, shaderStorageBlock);
jchen10eaef1e52017-06-13 10:44:11 +0800523 }
Jamie Madill4f86d052017-06-05 12:59:26 -0400524
jchen10eaef1e52017-06-13 10:44:11 +0800525 stream.writeInt(state.mAtomicCounterBuffers.size());
526 for (const auto &atomicCounterBuffer : state.mAtomicCounterBuffers)
527 {
528 WriteShaderVariableBuffer(&stream, atomicCounterBuffer);
Jamie Madill4f86d052017-06-05 12:59:26 -0400529 }
530
Jamie Madillffe00c02017-06-27 16:26:55 -0400531 // Warn the app layer if saving a binary with unsupported transform feedback.
532 if (!state.getLinkedTransformFeedbackVaryings().empty() &&
533 context->getWorkarounds().disableProgramCachingForTransformFeedback)
534 {
535 WARN() << "Saving program binary with transform feedback, which is not supported on this "
536 "driver.";
537 }
538
Jamie Madill4f86d052017-06-05 12:59:26 -0400539 stream.writeInt(state.getLinkedTransformFeedbackVaryings().size());
540 for (const auto &var : state.getLinkedTransformFeedbackVaryings())
541 {
Olli Etuaho465835d2017-09-26 13:34:10 +0300542 stream.writeIntVector(var.arraySizes);
Jamie Madill4f86d052017-06-05 12:59:26 -0400543 stream.writeInt(var.type);
544 stream.writeString(var.name);
545
546 stream.writeIntOrNegOne(var.arrayIndex);
547 }
548
549 stream.writeInt(state.getTransformFeedbackBufferMode());
550
551 stream.writeInt(state.getOutputVariables().size());
552 for (const sh::OutputVariable &output : state.getOutputVariables())
553 {
554 WriteShaderVar(&stream, output);
555 stream.writeInt(output.location);
556 }
557
558 stream.writeInt(state.getOutputLocations().size());
Olli Etuahod2551232017-10-26 20:03:33 +0300559 for (const auto &outputVar : state.getOutputLocations())
Jamie Madill4f86d052017-06-05 12:59:26 -0400560 {
Olli Etuaho1734e172017-10-27 15:30:27 +0300561 stream.writeInt(outputVar.arrayIndex);
Olli Etuahod2551232017-10-26 20:03:33 +0300562 stream.writeIntOrNegOne(outputVar.index);
Olli Etuahod2551232017-10-26 20:03:33 +0300563 stream.writeInt(outputVar.ignored);
Jamie Madill4f86d052017-06-05 12:59:26 -0400564 }
565
566 stream.writeInt(state.mOutputVariableTypes.size());
567 for (const auto &outputVariableType : state.mOutputVariableTypes)
568 {
569 stream.writeInt(outputVariableType);
570 }
571
Brandon Jonesc405ae72017-12-06 14:15:03 -0800572 static_assert(
573 IMPLEMENTATION_MAX_DRAW_BUFFERS * 2 <= 8 * sizeof(uint32_t),
574 "All bits of mDrawBufferTypeMask and mActiveOutputVariables can be contained in 32 bits");
575 stream.writeInt(static_cast<int>(state.mDrawBufferTypeMask.to_ulong()));
576 stream.writeInt(static_cast<int>(state.mActiveOutputVariables.to_ulong()));
Jamie Madill4f86d052017-06-05 12:59:26 -0400577
578 stream.writeInt(state.getSamplerUniformRange().low());
579 stream.writeInt(state.getSamplerUniformRange().high());
580
581 stream.writeInt(state.getSamplerBindings().size());
582 for (const auto &samplerBinding : state.getSamplerBindings())
583 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800584 stream.writeEnum(samplerBinding.textureType);
Jamie Madill4f86d052017-06-05 12:59:26 -0400585 stream.writeInt(samplerBinding.boundTextureUnits.size());
Jamie Madill54164b02017-08-28 15:17:37 -0400586 stream.writeInt(samplerBinding.unreferenced);
Jamie Madill4f86d052017-06-05 12:59:26 -0400587 }
588
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800589 stream.writeInt(state.getImageUniformRange().low());
590 stream.writeInt(state.getImageUniformRange().high());
591
592 stream.writeInt(state.getImageBindings().size());
593 for (const auto &imageBinding : state.getImageBindings())
594 {
Xinghua Cao0328b572017-06-26 15:51:36 +0800595 stream.writeInt(imageBinding.boundImageUnits.size());
596 for (size_t i = 0; i < imageBinding.boundImageUnits.size(); ++i)
597 {
598 stream.writeInt(imageBinding.boundImageUnits[i]);
599 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800600 }
601
jchen10eaef1e52017-06-13 10:44:11 +0800602 stream.writeInt(state.getAtomicCounterUniformRange().low());
603 stream.writeInt(state.getAtomicCounterUniformRange().high());
604
Yunchao He85072e82017-11-14 15:43:28 +0800605 stream.writeInt(state.getLinkedShaderStages().to_ulong());
606
Jamie Madill27a60632017-06-30 15:12:01 -0400607 program->getImplementation()->save(context, &stream);
Jamie Madill4f86d052017-06-05 12:59:26 -0400608
609 ASSERT(binaryOut);
610 binaryOut->resize(stream.length());
611 memcpy(binaryOut->data(), stream.data(), stream.length());
612}
613
Jamie Madill32447362017-06-28 14:53:52 -0400614// static
615void MemoryProgramCache::ComputeHash(const Context *context,
616 const Program *program,
617 ProgramHash *hashOut)
618{
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500619 const Shader *vertexShader = program->getAttachedVertexShader();
620 const Shader *fragmentShader = program->getAttachedFragmentShader();
621 const Shader *computeShader = program->getAttachedComputeShader();
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800622 const Shader *geometryShader = program->getAttachedGeometryShader();
Jamie Madill32447362017-06-28 14:53:52 -0400623
624 // Compute the program hash. Start with the shader hashes and resource strings.
625 HashStream hashStream;
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800626 hashStream << vertexShader << fragmentShader << computeShader << geometryShader;
Jamie Madill32447362017-06-28 14:53:52 -0400627
628 // Add some ANGLE metadata and Context properties, such as version and back-end.
629 hashStream << ANGLE_COMMIT_HASH << context->getClientMajorVersion()
630 << context->getClientMinorVersion() << context->getString(GL_RENDERER);
631
632 // Hash pre-link program properties.
633 hashStream << program->getAttributeBindings() << program->getUniformLocationBindings()
634 << program->getFragmentInputBindings()
635 << program->getState().getTransformFeedbackVaryingNames()
636 << program->getState().getTransformFeedbackBufferMode();
637
638 // Call the secure SHA hashing function.
639 const std::string &programKey = hashStream.str();
640 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(programKey.c_str()),
641 programKey.length(), hashOut->data());
642}
643
644LinkResult MemoryProgramCache::getProgram(const Context *context,
645 const Program *program,
646 ProgramState *state,
647 ProgramHash *hashOut)
648{
649 ComputeHash(context, program, hashOut);
650 const angle::MemoryBuffer *binaryProgram = nullptr;
651 LinkResult result(false);
652 if (get(*hashOut, &binaryProgram))
653 {
654 InfoLog infoLog;
655 ANGLE_TRY_RESULT(Deserialize(context, program, state, binaryProgram->data(),
656 binaryProgram->size(), infoLog),
657 result);
Jamie Madill6c58b062017-08-01 13:44:25 -0400658 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", result.getResult());
Jamie Madill32447362017-06-28 14:53:52 -0400659 if (!result.getResult())
660 {
661 // Cache load failed, evict.
662 if (mIssuedWarnings++ < kWarningLimit)
663 {
664 WARN() << "Failed to load binary from cache: " << infoLog.str();
665
666 if (mIssuedWarnings == kWarningLimit)
667 {
668 WARN() << "Reaching warning limit for cache load failures, silencing "
669 "subsequent warnings.";
670 }
671 }
672 remove(*hashOut);
673 }
674 }
675 return result;
676}
677
678bool MemoryProgramCache::get(const ProgramHash &programHash, const angle::MemoryBuffer **programOut)
679{
Jamie Madill6c58b062017-08-01 13:44:25 -0400680 const CacheEntry *entry = nullptr;
681 if (!mProgramBinaryCache.get(programHash, &entry))
682 {
683 ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheMiss,
684 kCacheResultMax);
685 return false;
686 }
687
688 if (entry->second == CacheSource::PutProgram)
689 {
690 ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheHitMemory,
691 kCacheResultMax);
692 }
693 else
694 {
695 ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheHitDisk,
696 kCacheResultMax);
697 }
698
699 *programOut = &entry->first;
700 return true;
Jamie Madill32447362017-06-28 14:53:52 -0400701}
702
Jamie Madillc43be722017-07-13 16:22:14 -0400703bool MemoryProgramCache::getAt(size_t index,
704 ProgramHash *hashOut,
705 const angle::MemoryBuffer **programOut)
706{
Jamie Madill6c58b062017-08-01 13:44:25 -0400707 const CacheEntry *entry = nullptr;
708 if (!mProgramBinaryCache.getAt(index, hashOut, &entry))
709 {
710 return false;
711 }
712
713 *programOut = &entry->first;
714 return true;
Jamie Madillc43be722017-07-13 16:22:14 -0400715}
716
Jamie Madill32447362017-06-28 14:53:52 -0400717void MemoryProgramCache::remove(const ProgramHash &programHash)
718{
719 bool result = mProgramBinaryCache.eraseByKey(programHash);
720 ASSERT(result);
721}
722
Jamie Madill6c58b062017-08-01 13:44:25 -0400723void MemoryProgramCache::putProgram(const ProgramHash &programHash,
724 const Context *context,
725 const Program *program)
Jamie Madill32447362017-06-28 14:53:52 -0400726{
Jamie Madill6c58b062017-08-01 13:44:25 -0400727 CacheEntry newEntry;
728 Serialize(context, program, &newEntry.first);
729 newEntry.second = CacheSource::PutProgram;
730
731 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramBinarySizeBytes",
732 static_cast<int>(newEntry.first.size()));
733
734 const CacheEntry *result =
735 mProgramBinaryCache.put(programHash, std::move(newEntry), newEntry.first.size());
Jamie Madill360daee2017-06-29 10:36:19 -0400736 if (!result)
Jamie Madill32447362017-06-28 14:53:52 -0400737 {
738 ERR() << "Failed to store binary program in memory cache, program is too large.";
739 }
Jamie Madill360daee2017-06-29 10:36:19 -0400740 else
741 {
742 auto *platform = ANGLEPlatformCurrent();
Jamie Madill6c58b062017-08-01 13:44:25 -0400743 platform->cacheProgram(platform, programHash, result->first.size(), result->first.data());
Jamie Madill360daee2017-06-29 10:36:19 -0400744 }
Jamie Madill32447362017-06-28 14:53:52 -0400745}
746
Jamie Madill4c19a8a2017-07-24 11:46:06 -0400747void MemoryProgramCache::updateProgram(const Context *context, const Program *program)
748{
749 gl::ProgramHash programHash;
750 ComputeHash(context, program, &programHash);
751 putProgram(programHash, context, program);
752}
753
Jamie Madillc43be722017-07-13 16:22:14 -0400754void MemoryProgramCache::putBinary(const ProgramHash &programHash,
Jamie Madill32447362017-06-28 14:53:52 -0400755 const uint8_t *binary,
756 size_t length)
757{
758 // Copy the binary.
Jamie Madill6c58b062017-08-01 13:44:25 -0400759 CacheEntry newEntry;
760 newEntry.first.resize(length);
761 memcpy(newEntry.first.data(), binary, length);
762 newEntry.second = CacheSource::PutBinary;
Jamie Madill32447362017-06-28 14:53:52 -0400763
Jamie Madill32447362017-06-28 14:53:52 -0400764 // Store the binary.
Jamie Madill6c58b062017-08-01 13:44:25 -0400765 const CacheEntry *result = mProgramBinaryCache.put(programHash, std::move(newEntry), length);
Jamie Madillc43be722017-07-13 16:22:14 -0400766 if (!result)
767 {
768 ERR() << "Failed to store binary program in memory cache, program is too large.";
769 }
Jamie Madill32447362017-06-28 14:53:52 -0400770}
771
772void MemoryProgramCache::clear()
773{
774 mProgramBinaryCache.clear();
775 mIssuedWarnings = 0;
776}
777
Jamie Madillc43be722017-07-13 16:22:14 -0400778void MemoryProgramCache::resize(size_t maxCacheSizeBytes)
779{
780 mProgramBinaryCache.resize(maxCacheSizeBytes);
781}
782
783size_t MemoryProgramCache::entryCount() const
784{
785 return mProgramBinaryCache.entryCount();
786}
787
788size_t MemoryProgramCache::trim(size_t limit)
789{
790 return mProgramBinaryCache.shrinkToSize(limit);
791}
792
793size_t MemoryProgramCache::size() const
794{
795 return mProgramBinaryCache.size();
796}
797
798size_t MemoryProgramCache::maxSize() const
799{
800 return mProgramBinaryCache.maxSize();
801}
802
Jamie Madill4f86d052017-06-05 12:59:26 -0400803} // namespace gl