blob: 5fa6f64a6ae51fd7625b9dd3e84b0ec5f0a542a8 [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);
45 stream->writeInt(var.arraySize);
46 stream->writeInt(var.staticUse);
47 stream->writeString(var.structName);
48 ASSERT(var.fields.empty());
49}
50
51void 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
jchen10eaef1e52017-06-13 10:44:11 +080062void 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
78void 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 Qin3a9090f2017-09-27 14:37:04 +080093void WriteBufferVariable(BinaryOutputStream *stream, const BufferVariable &var)
94{
95 WriteShaderVar(stream, var);
96
97 stream->writeInt(var.bufferIndex);
98 stream->writeInt(var.blockInfo.offset);
99 stream->writeInt(var.blockInfo.arrayStride);
100 stream->writeInt(var.blockInfo.matrixStride);
101 stream->writeInt(var.blockInfo.isRowMajorMatrix);
102 stream->writeInt(var.blockInfo.topLevelArrayStride);
103 stream->writeInt(var.topLevelArraySize);
104 stream->writeInt(var.vertexStaticUse);
105 stream->writeInt(var.fragmentStaticUse);
106 stream->writeInt(var.computeStaticUse);
107}
108
109void LoadBufferVariable(BinaryInputStream *stream, BufferVariable *var)
110{
111 LoadShaderVar(stream, var);
112
113 var->bufferIndex = stream->readInt<int>();
114 var->blockInfo.offset = stream->readInt<int>();
115 var->blockInfo.arrayStride = stream->readInt<int>();
116 var->blockInfo.matrixStride = stream->readInt<int>();
117 var->blockInfo.isRowMajorMatrix = stream->readBool();
118 var->blockInfo.topLevelArrayStride = stream->readInt<int>();
119 var->topLevelArraySize = stream->readInt<int>();
120 var->vertexStaticUse = stream->readBool();
121 var->fragmentStaticUse = stream->readBool();
122 var->computeStaticUse = stream->readBool();
123}
124
Jiajia Qin729b2c62017-08-14 09:36:11 +0800125void WriteInterfaceBlock(BinaryOutputStream *stream, const InterfaceBlock &block)
126{
127 stream->writeString(block.name);
128 stream->writeString(block.mappedName);
129 stream->writeInt(block.isArray);
130 stream->writeInt(block.arrayElement);
131
132 WriteShaderVariableBuffer(stream, block);
133}
134
135void LoadInterfaceBlock(BinaryInputStream *stream, InterfaceBlock *block)
136{
137 block->name = stream->readString();
138 block->mappedName = stream->readString();
139 block->isArray = stream->readBool();
140 block->arrayElement = stream->readInt<unsigned int>();
141
142 LoadShaderVariableBuffer(stream, block);
143}
144
Jamie Madill32447362017-06-28 14:53:52 -0400145class HashStream final : angle::NonCopyable
146{
147 public:
148 std::string str() { return mStringStream.str(); }
149
150 template <typename T>
151 HashStream &operator<<(T value)
152 {
153 mStringStream << value << kSeparator;
154 return *this;
155 }
156
157 private:
158 static constexpr char kSeparator = ':';
159 std::ostringstream mStringStream;
160};
161
162HashStream &operator<<(HashStream &stream, const Shader *shader)
163{
164 if (shader)
165 {
166 stream << shader->getSourceString().c_str() << shader->getSourceString().length()
167 << shader->getCompilerResourcesString().c_str();
168 }
169 return stream;
170}
171
172HashStream &operator<<(HashStream &stream, const Program::Bindings &bindings)
173{
174 for (const auto &binding : bindings)
175 {
176 stream << binding.first << binding.second;
177 }
178 return stream;
179}
180
181HashStream &operator<<(HashStream &stream, const std::vector<std::string> &strings)
182{
183 for (const auto &str : strings)
184 {
185 stream << str;
186 }
187 return stream;
188}
189
Jamie Madill4f86d052017-06-05 12:59:26 -0400190} // anonymous namespace
191
Jamie Madill32447362017-06-28 14:53:52 -0400192MemoryProgramCache::MemoryProgramCache(size_t maxCacheSizeBytes)
193 : mProgramBinaryCache(maxCacheSizeBytes), mIssuedWarnings(0)
194{
195}
196
197MemoryProgramCache::~MemoryProgramCache()
198{
199}
200
Jamie Madill4f86d052017-06-05 12:59:26 -0400201// static
202LinkResult MemoryProgramCache::Deserialize(const Context *context,
203 const Program *program,
204 ProgramState *state,
205 const uint8_t *binary,
206 size_t length,
207 InfoLog &infoLog)
208{
209 BinaryInputStream stream(binary, length);
210
211 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
212 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
213 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) !=
214 0)
215 {
216 infoLog << "Invalid program binary version.";
217 return false;
218 }
219
220 int majorVersion = stream.readInt<int>();
221 int minorVersion = stream.readInt<int>();
222 if (majorVersion != context->getClientMajorVersion() ||
223 minorVersion != context->getClientMinorVersion())
224 {
225 infoLog << "Cannot load program binaries across different ES context versions.";
226 return false;
227 }
228
229 state->mComputeShaderLocalSize[0] = stream.readInt<int>();
230 state->mComputeShaderLocalSize[1] = stream.readInt<int>();
231 state->mComputeShaderLocalSize[2] = stream.readInt<int>();
232
Martin Radev7cf61662017-07-26 17:10:53 +0300233 state->mNumViews = stream.readInt<int>();
234
Jamie Madill4f86d052017-06-05 12:59:26 -0400235 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
236 "Too many vertex attribs for mask");
237 state->mActiveAttribLocationsMask = stream.readInt<unsigned long>();
238
239 unsigned int attribCount = stream.readInt<unsigned int>();
240 ASSERT(state->mAttributes.empty());
241 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
242 {
243 sh::Attribute attrib;
244 LoadShaderVar(&stream, &attrib);
245 attrib.location = stream.readInt<int>();
246 state->mAttributes.push_back(attrib);
247 }
248
249 unsigned int uniformCount = stream.readInt<unsigned int>();
250 ASSERT(state->mUniforms.empty());
251 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
252 {
253 LinkedUniform uniform;
254 LoadShaderVar(&stream, &uniform);
255
jchen10eaef1e52017-06-13 10:44:11 +0800256 uniform.bufferIndex = stream.readInt<int>();
Jamie Madill4f86d052017-06-05 12:59:26 -0400257 uniform.blockInfo.offset = stream.readInt<int>();
258 uniform.blockInfo.arrayStride = stream.readInt<int>();
259 uniform.blockInfo.matrixStride = stream.readInt<int>();
260 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
261
Jamie Madillf00f7ff2017-08-31 14:39:15 -0400262 uniform.typeInfo = &GetUniformTypeInfo(uniform.type);
263
Jamie Madill4f86d052017-06-05 12:59:26 -0400264 state->mUniforms.push_back(uniform);
265 }
266
267 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
268 ASSERT(state->mUniformLocations.empty());
269 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
270 uniformIndexIndex++)
271 {
272 VariableLocation variable;
Olli Etuaho1734e172017-10-27 15:30:27 +0300273 stream.readInt(&variable.arrayIndex);
Jamie Madill4f86d052017-06-05 12:59:26 -0400274 stream.readInt(&variable.index);
Jamie Madill4f86d052017-06-05 12:59:26 -0400275 stream.readBool(&variable.ignored);
276
277 state->mUniformLocations.push_back(variable);
278 }
279
280 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
281 ASSERT(state->mUniformBlocks.empty());
282 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
283 ++uniformBlockIndex)
284 {
Jiajia Qin729b2c62017-08-14 09:36:11 +0800285 InterfaceBlock uniformBlock;
286 LoadInterfaceBlock(&stream, &uniformBlock);
Jamie Madill4f86d052017-06-05 12:59:26 -0400287 state->mUniformBlocks.push_back(uniformBlock);
Jamie Madill4f86d052017-06-05 12:59:26 -0400288
jchen107a20b972017-06-13 14:25:26 +0800289 state->mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlock.binding != 0);
Jamie Madill4f86d052017-06-05 12:59:26 -0400290 }
Jiajia Qin729b2c62017-08-14 09:36:11 +0800291
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800292 unsigned int bufferVariableCount = stream.readInt<unsigned int>();
293 ASSERT(state->mBufferVariables.empty());
294 for (unsigned int index = 0; index < bufferVariableCount; ++index)
295 {
296 BufferVariable bufferVariable;
297 LoadBufferVariable(&stream, &bufferVariable);
298 state->mBufferVariables.push_back(bufferVariable);
299 }
300
Jiajia Qin729b2c62017-08-14 09:36:11 +0800301 unsigned int shaderStorageBlockCount = stream.readInt<unsigned int>();
302 ASSERT(state->mShaderStorageBlocks.empty());
303 for (unsigned int shaderStorageBlockIndex = 0;
304 shaderStorageBlockIndex < shaderStorageBlockCount; ++shaderStorageBlockIndex)
305 {
306 InterfaceBlock shaderStorageBlock;
307 LoadInterfaceBlock(&stream, &shaderStorageBlock);
308 state->mShaderStorageBlocks.push_back(shaderStorageBlock);
309 }
310
jchen10eaef1e52017-06-13 10:44:11 +0800311 unsigned int atomicCounterBufferCount = stream.readInt<unsigned int>();
312 ASSERT(state->mAtomicCounterBuffers.empty());
313 for (unsigned int bufferIndex = 0; bufferIndex < atomicCounterBufferCount; ++bufferIndex)
314 {
315 AtomicCounterBuffer atomicCounterBuffer;
316 LoadShaderVariableBuffer(&stream, &atomicCounterBuffer);
317
318 state->mAtomicCounterBuffers.push_back(atomicCounterBuffer);
319 }
Jamie Madill4f86d052017-06-05 12:59:26 -0400320
321 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madillffe00c02017-06-27 16:26:55 -0400322
323 // Reject programs that use transform feedback varyings if the hardware cannot support them.
324 if (transformFeedbackVaryingCount > 0 &&
325 context->getWorkarounds().disableProgramCachingForTransformFeedback)
326 {
327 infoLog << "Current driver does not support transform feedback in binary programs.";
328 return false;
329 }
330
Jamie Madill4f86d052017-06-05 12:59:26 -0400331 ASSERT(state->mLinkedTransformFeedbackVaryings.empty());
332 for (unsigned int transformFeedbackVaryingIndex = 0;
333 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
334 ++transformFeedbackVaryingIndex)
335 {
336 sh::Varying varying;
337 stream.readInt(&varying.arraySize);
338 stream.readInt(&varying.type);
339 stream.readString(&varying.name);
340
341 GLuint arrayIndex = stream.readInt<GLuint>();
342
343 state->mLinkedTransformFeedbackVaryings.emplace_back(varying, arrayIndex);
344 }
345
346 stream.readInt(&state->mTransformFeedbackBufferMode);
347
348 unsigned int outputCount = stream.readInt<unsigned int>();
349 ASSERT(state->mOutputVariables.empty());
350 for (unsigned int outputIndex = 0; outputIndex < outputCount; ++outputIndex)
351 {
352 sh::OutputVariable output;
353 LoadShaderVar(&stream, &output);
354 output.location = stream.readInt<int>();
355 state->mOutputVariables.push_back(output);
356 }
357
358 unsigned int outputVarCount = stream.readInt<unsigned int>();
Olli Etuahod2551232017-10-26 20:03:33 +0300359 ASSERT(state->mOutputLocations.empty());
Jamie Madill4f86d052017-06-05 12:59:26 -0400360 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
361 {
Jamie Madill4f86d052017-06-05 12:59:26 -0400362 VariableLocation locationData;
Olli Etuaho1734e172017-10-27 15:30:27 +0300363 stream.readInt(&locationData.arrayIndex);
Jamie Madill4f86d052017-06-05 12:59:26 -0400364 stream.readInt(&locationData.index);
Jamie Madillfb997ec2017-09-20 15:44:27 -0400365 stream.readBool(&locationData.ignored);
Olli Etuahod2551232017-10-26 20:03:33 +0300366 state->mOutputLocations.push_back(locationData);
Jamie Madill4f86d052017-06-05 12:59:26 -0400367 }
368
369 unsigned int outputTypeCount = stream.readInt<unsigned int>();
370 for (unsigned int outputIndex = 0; outputIndex < outputTypeCount; ++outputIndex)
371 {
372 state->mOutputVariableTypes.push_back(stream.readInt<GLenum>());
373 }
374 static_assert(IMPLEMENTATION_MAX_DRAW_BUFFERS < 8 * sizeof(uint32_t),
375 "All bits of DrawBufferMask can be contained in an uint32_t");
376 state->mActiveOutputVariables = stream.readInt<uint32_t>();
377
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800378 unsigned int samplerRangeLow = stream.readInt<unsigned int>();
379 unsigned int samplerRangeHigh = stream.readInt<unsigned int>();
380 state->mSamplerUniformRange = RangeUI(samplerRangeLow, samplerRangeHigh);
Jamie Madill4f86d052017-06-05 12:59:26 -0400381 unsigned int samplerCount = stream.readInt<unsigned int>();
382 for (unsigned int samplerIndex = 0; samplerIndex < samplerCount; ++samplerIndex)
383 {
384 GLenum textureType = stream.readInt<GLenum>();
385 size_t bindingCount = stream.readInt<size_t>();
Jamie Madill54164b02017-08-28 15:17:37 -0400386 bool unreferenced = stream.readBool();
387 state->mSamplerBindings.emplace_back(
388 SamplerBinding(textureType, bindingCount, unreferenced));
Jamie Madill4f86d052017-06-05 12:59:26 -0400389 }
390
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800391 unsigned int imageRangeLow = stream.readInt<unsigned int>();
392 unsigned int imageRangeHigh = stream.readInt<unsigned int>();
393 state->mImageUniformRange = RangeUI(imageRangeLow, imageRangeHigh);
Xinghua Cao0328b572017-06-26 15:51:36 +0800394 unsigned int imageBindingCount = stream.readInt<unsigned int>();
395 for (unsigned int imageIndex = 0; imageIndex < imageBindingCount; ++imageIndex)
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800396 {
Xinghua Cao0328b572017-06-26 15:51:36 +0800397 unsigned int elementCount = stream.readInt<unsigned int>();
398 ImageBinding imageBinding(elementCount);
399 for (unsigned int i = 0; i < elementCount; ++i)
400 {
401 imageBinding.boundImageUnits[i] = stream.readInt<unsigned int>();
402 }
403 state->mImageBindings.emplace_back(imageBinding);
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800404 }
405
jchen10eaef1e52017-06-13 10:44:11 +0800406 unsigned int atomicCounterRangeLow = stream.readInt<unsigned int>();
407 unsigned int atomicCounterRangeHigh = stream.readInt<unsigned int>();
408 state->mAtomicCounterUniformRange = RangeUI(atomicCounterRangeLow, atomicCounterRangeHigh);
409
Jamie Madill4f86d052017-06-05 12:59:26 -0400410 return program->getImplementation()->load(context, infoLog, &stream);
411}
412
413// static
414void MemoryProgramCache::Serialize(const Context *context,
415 const gl::Program *program,
416 angle::MemoryBuffer *binaryOut)
417{
418 BinaryOutputStream stream;
419
420 stream.writeBytes(reinterpret_cast<const unsigned char *>(ANGLE_COMMIT_HASH),
421 ANGLE_COMMIT_HASH_SIZE);
422
423 // nullptr context is supported when computing binary length.
424 if (context)
425 {
426 stream.writeInt(context->getClientVersion().major);
427 stream.writeInt(context->getClientVersion().minor);
428 }
429 else
430 {
431 stream.writeInt(2);
432 stream.writeInt(0);
433 }
434
435 const auto &state = program->getState();
436
437 const auto &computeLocalSize = state.getComputeShaderLocalSize();
438
439 stream.writeInt(computeLocalSize[0]);
440 stream.writeInt(computeLocalSize[1]);
441 stream.writeInt(computeLocalSize[2]);
442
Martin Radev7cf61662017-07-26 17:10:53 +0300443 stream.writeInt(state.mNumViews);
444
Jamie Madill4f86d052017-06-05 12:59:26 -0400445 stream.writeInt(state.getActiveAttribLocationsMask().to_ulong());
446
447 stream.writeInt(state.getAttributes().size());
448 for (const sh::Attribute &attrib : state.getAttributes())
449 {
450 WriteShaderVar(&stream, attrib);
451 stream.writeInt(attrib.location);
452 }
453
454 stream.writeInt(state.getUniforms().size());
455 for (const LinkedUniform &uniform : state.getUniforms())
456 {
457 WriteShaderVar(&stream, uniform);
458
459 // FIXME: referenced
460
jchen10eaef1e52017-06-13 10:44:11 +0800461 stream.writeInt(uniform.bufferIndex);
Jamie Madill4f86d052017-06-05 12:59:26 -0400462 stream.writeInt(uniform.blockInfo.offset);
463 stream.writeInt(uniform.blockInfo.arrayStride);
464 stream.writeInt(uniform.blockInfo.matrixStride);
465 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
466 }
467
468 stream.writeInt(state.getUniformLocations().size());
469 for (const auto &variable : state.getUniformLocations())
470 {
Olli Etuaho1734e172017-10-27 15:30:27 +0300471 stream.writeInt(variable.arrayIndex);
Jamie Madillfb997ec2017-09-20 15:44:27 -0400472 stream.writeIntOrNegOne(variable.index);
Jamie Madill4f86d052017-06-05 12:59:26 -0400473 stream.writeInt(variable.ignored);
474 }
475
476 stream.writeInt(state.getUniformBlocks().size());
Jiajia Qin729b2c62017-08-14 09:36:11 +0800477 for (const InterfaceBlock &uniformBlock : state.getUniformBlocks())
Jamie Madill4f86d052017-06-05 12:59:26 -0400478 {
Jiajia Qin729b2c62017-08-14 09:36:11 +0800479 WriteInterfaceBlock(&stream, uniformBlock);
480 }
Jamie Madill4f86d052017-06-05 12:59:26 -0400481
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800482 stream.writeInt(state.getBufferVariables().size());
483 for (const BufferVariable &bufferVariable : state.getBufferVariables())
484 {
485 WriteBufferVariable(&stream, bufferVariable);
486 }
487
Jiajia Qin729b2c62017-08-14 09:36:11 +0800488 stream.writeInt(state.getShaderStorageBlocks().size());
489 for (const InterfaceBlock &shaderStorageBlock : state.getShaderStorageBlocks())
490 {
491 WriteInterfaceBlock(&stream, shaderStorageBlock);
jchen10eaef1e52017-06-13 10:44:11 +0800492 }
Jamie Madill4f86d052017-06-05 12:59:26 -0400493
jchen10eaef1e52017-06-13 10:44:11 +0800494 stream.writeInt(state.mAtomicCounterBuffers.size());
495 for (const auto &atomicCounterBuffer : state.mAtomicCounterBuffers)
496 {
497 WriteShaderVariableBuffer(&stream, atomicCounterBuffer);
Jamie Madill4f86d052017-06-05 12:59:26 -0400498 }
499
Jamie Madillffe00c02017-06-27 16:26:55 -0400500 // Warn the app layer if saving a binary with unsupported transform feedback.
501 if (!state.getLinkedTransformFeedbackVaryings().empty() &&
502 context->getWorkarounds().disableProgramCachingForTransformFeedback)
503 {
504 WARN() << "Saving program binary with transform feedback, which is not supported on this "
505 "driver.";
506 }
507
Jamie Madill4f86d052017-06-05 12:59:26 -0400508 stream.writeInt(state.getLinkedTransformFeedbackVaryings().size());
509 for (const auto &var : state.getLinkedTransformFeedbackVaryings())
510 {
511 stream.writeInt(var.arraySize);
512 stream.writeInt(var.type);
513 stream.writeString(var.name);
514
515 stream.writeIntOrNegOne(var.arrayIndex);
516 }
517
518 stream.writeInt(state.getTransformFeedbackBufferMode());
519
520 stream.writeInt(state.getOutputVariables().size());
521 for (const sh::OutputVariable &output : state.getOutputVariables())
522 {
523 WriteShaderVar(&stream, output);
524 stream.writeInt(output.location);
525 }
526
527 stream.writeInt(state.getOutputLocations().size());
Olli Etuahod2551232017-10-26 20:03:33 +0300528 for (const auto &outputVar : state.getOutputLocations())
Jamie Madill4f86d052017-06-05 12:59:26 -0400529 {
Olli Etuaho1734e172017-10-27 15:30:27 +0300530 stream.writeInt(outputVar.arrayIndex);
Olli Etuahod2551232017-10-26 20:03:33 +0300531 stream.writeIntOrNegOne(outputVar.index);
Olli Etuahod2551232017-10-26 20:03:33 +0300532 stream.writeInt(outputVar.ignored);
Jamie Madill4f86d052017-06-05 12:59:26 -0400533 }
534
535 stream.writeInt(state.mOutputVariableTypes.size());
536 for (const auto &outputVariableType : state.mOutputVariableTypes)
537 {
538 stream.writeInt(outputVariableType);
539 }
540
541 static_assert(IMPLEMENTATION_MAX_DRAW_BUFFERS < 8 * sizeof(uint32_t),
542 "All bits of DrawBufferMask can be contained in an uint32_t");
543 stream.writeInt(static_cast<uint32_t>(state.mActiveOutputVariables.to_ulong()));
544
545 stream.writeInt(state.getSamplerUniformRange().low());
546 stream.writeInt(state.getSamplerUniformRange().high());
547
548 stream.writeInt(state.getSamplerBindings().size());
549 for (const auto &samplerBinding : state.getSamplerBindings())
550 {
551 stream.writeInt(samplerBinding.textureType);
552 stream.writeInt(samplerBinding.boundTextureUnits.size());
Jamie Madill54164b02017-08-28 15:17:37 -0400553 stream.writeInt(samplerBinding.unreferenced);
Jamie Madill4f86d052017-06-05 12:59:26 -0400554 }
555
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800556 stream.writeInt(state.getImageUniformRange().low());
557 stream.writeInt(state.getImageUniformRange().high());
558
559 stream.writeInt(state.getImageBindings().size());
560 for (const auto &imageBinding : state.getImageBindings())
561 {
Xinghua Cao0328b572017-06-26 15:51:36 +0800562 stream.writeInt(imageBinding.boundImageUnits.size());
563 for (size_t i = 0; i < imageBinding.boundImageUnits.size(); ++i)
564 {
565 stream.writeInt(imageBinding.boundImageUnits[i]);
566 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800567 }
568
jchen10eaef1e52017-06-13 10:44:11 +0800569 stream.writeInt(state.getAtomicCounterUniformRange().low());
570 stream.writeInt(state.getAtomicCounterUniformRange().high());
571
Jamie Madill27a60632017-06-30 15:12:01 -0400572 program->getImplementation()->save(context, &stream);
Jamie Madill4f86d052017-06-05 12:59:26 -0400573
574 ASSERT(binaryOut);
575 binaryOut->resize(stream.length());
576 memcpy(binaryOut->data(), stream.data(), stream.length());
577}
578
Jamie Madill32447362017-06-28 14:53:52 -0400579// static
580void MemoryProgramCache::ComputeHash(const Context *context,
581 const Program *program,
582 ProgramHash *hashOut)
583{
584 auto vertexShader = program->getAttachedVertexShader();
585 auto fragmentShader = program->getAttachedFragmentShader();
586 auto computeShader = program->getAttachedComputeShader();
587
588 // Compute the program hash. Start with the shader hashes and resource strings.
589 HashStream hashStream;
590 hashStream << vertexShader << fragmentShader << computeShader;
591
592 // Add some ANGLE metadata and Context properties, such as version and back-end.
593 hashStream << ANGLE_COMMIT_HASH << context->getClientMajorVersion()
594 << context->getClientMinorVersion() << context->getString(GL_RENDERER);
595
596 // Hash pre-link program properties.
597 hashStream << program->getAttributeBindings() << program->getUniformLocationBindings()
598 << program->getFragmentInputBindings()
599 << program->getState().getTransformFeedbackVaryingNames()
600 << program->getState().getTransformFeedbackBufferMode();
601
602 // Call the secure SHA hashing function.
603 const std::string &programKey = hashStream.str();
604 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(programKey.c_str()),
605 programKey.length(), hashOut->data());
606}
607
608LinkResult MemoryProgramCache::getProgram(const Context *context,
609 const Program *program,
610 ProgramState *state,
611 ProgramHash *hashOut)
612{
613 ComputeHash(context, program, hashOut);
614 const angle::MemoryBuffer *binaryProgram = nullptr;
615 LinkResult result(false);
616 if (get(*hashOut, &binaryProgram))
617 {
618 InfoLog infoLog;
619 ANGLE_TRY_RESULT(Deserialize(context, program, state, binaryProgram->data(),
620 binaryProgram->size(), infoLog),
621 result);
Jamie Madill6c58b062017-08-01 13:44:25 -0400622 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", result.getResult());
Jamie Madill32447362017-06-28 14:53:52 -0400623 if (!result.getResult())
624 {
625 // Cache load failed, evict.
626 if (mIssuedWarnings++ < kWarningLimit)
627 {
628 WARN() << "Failed to load binary from cache: " << infoLog.str();
629
630 if (mIssuedWarnings == kWarningLimit)
631 {
632 WARN() << "Reaching warning limit for cache load failures, silencing "
633 "subsequent warnings.";
634 }
635 }
636 remove(*hashOut);
637 }
638 }
639 return result;
640}
641
642bool MemoryProgramCache::get(const ProgramHash &programHash, const angle::MemoryBuffer **programOut)
643{
Jamie Madill6c58b062017-08-01 13:44:25 -0400644 const CacheEntry *entry = nullptr;
645 if (!mProgramBinaryCache.get(programHash, &entry))
646 {
647 ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheMiss,
648 kCacheResultMax);
649 return false;
650 }
651
652 if (entry->second == CacheSource::PutProgram)
653 {
654 ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheHitMemory,
655 kCacheResultMax);
656 }
657 else
658 {
659 ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheHitDisk,
660 kCacheResultMax);
661 }
662
663 *programOut = &entry->first;
664 return true;
Jamie Madill32447362017-06-28 14:53:52 -0400665}
666
Jamie Madillc43be722017-07-13 16:22:14 -0400667bool MemoryProgramCache::getAt(size_t index,
668 ProgramHash *hashOut,
669 const angle::MemoryBuffer **programOut)
670{
Jamie Madill6c58b062017-08-01 13:44:25 -0400671 const CacheEntry *entry = nullptr;
672 if (!mProgramBinaryCache.getAt(index, hashOut, &entry))
673 {
674 return false;
675 }
676
677 *programOut = &entry->first;
678 return true;
Jamie Madillc43be722017-07-13 16:22:14 -0400679}
680
Jamie Madill32447362017-06-28 14:53:52 -0400681void MemoryProgramCache::remove(const ProgramHash &programHash)
682{
683 bool result = mProgramBinaryCache.eraseByKey(programHash);
684 ASSERT(result);
685}
686
Jamie Madill6c58b062017-08-01 13:44:25 -0400687void MemoryProgramCache::putProgram(const ProgramHash &programHash,
688 const Context *context,
689 const Program *program)
Jamie Madill32447362017-06-28 14:53:52 -0400690{
Jamie Madill6c58b062017-08-01 13:44:25 -0400691 CacheEntry newEntry;
692 Serialize(context, program, &newEntry.first);
693 newEntry.second = CacheSource::PutProgram;
694
695 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramBinarySizeBytes",
696 static_cast<int>(newEntry.first.size()));
697
698 const CacheEntry *result =
699 mProgramBinaryCache.put(programHash, std::move(newEntry), newEntry.first.size());
Jamie Madill360daee2017-06-29 10:36:19 -0400700 if (!result)
Jamie Madill32447362017-06-28 14:53:52 -0400701 {
702 ERR() << "Failed to store binary program in memory cache, program is too large.";
703 }
Jamie Madill360daee2017-06-29 10:36:19 -0400704 else
705 {
706 auto *platform = ANGLEPlatformCurrent();
Jamie Madill6c58b062017-08-01 13:44:25 -0400707 platform->cacheProgram(platform, programHash, result->first.size(), result->first.data());
Jamie Madill360daee2017-06-29 10:36:19 -0400708 }
Jamie Madill32447362017-06-28 14:53:52 -0400709}
710
Jamie Madill4c19a8a2017-07-24 11:46:06 -0400711void MemoryProgramCache::updateProgram(const Context *context, const Program *program)
712{
713 gl::ProgramHash programHash;
714 ComputeHash(context, program, &programHash);
715 putProgram(programHash, context, program);
716}
717
Jamie Madillc43be722017-07-13 16:22:14 -0400718void MemoryProgramCache::putBinary(const ProgramHash &programHash,
Jamie Madill32447362017-06-28 14:53:52 -0400719 const uint8_t *binary,
720 size_t length)
721{
722 // Copy the binary.
Jamie Madill6c58b062017-08-01 13:44:25 -0400723 CacheEntry newEntry;
724 newEntry.first.resize(length);
725 memcpy(newEntry.first.data(), binary, length);
726 newEntry.second = CacheSource::PutBinary;
Jamie Madill32447362017-06-28 14:53:52 -0400727
Jamie Madill32447362017-06-28 14:53:52 -0400728 // Store the binary.
Jamie Madill6c58b062017-08-01 13:44:25 -0400729 const CacheEntry *result = mProgramBinaryCache.put(programHash, std::move(newEntry), length);
Jamie Madillc43be722017-07-13 16:22:14 -0400730 if (!result)
731 {
732 ERR() << "Failed to store binary program in memory cache, program is too large.";
733 }
Jamie Madill32447362017-06-28 14:53:52 -0400734}
735
736void MemoryProgramCache::clear()
737{
738 mProgramBinaryCache.clear();
739 mIssuedWarnings = 0;
740}
741
Jamie Madillc43be722017-07-13 16:22:14 -0400742void MemoryProgramCache::resize(size_t maxCacheSizeBytes)
743{
744 mProgramBinaryCache.resize(maxCacheSizeBytes);
745}
746
747size_t MemoryProgramCache::entryCount() const
748{
749 return mProgramBinaryCache.entryCount();
750}
751
752size_t MemoryProgramCache::trim(size_t limit)
753{
754 return mProgramBinaryCache.shrinkToSize(limit);
755}
756
757size_t MemoryProgramCache::size() const
758{
759 return mProgramBinaryCache.size();
760}
761
762size_t MemoryProgramCache::maxSize() const
763{
764 return mProgramBinaryCache.maxSize();
765}
766
Jamie Madill4f86d052017-06-05 12:59:26 -0400767} // namespace gl