Debian Codegraph @

src/gpu/ganesh/GrPersistentCacheUtils.cpp

raw
1 /*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/gpu/ganesh/GrPersistentCacheUtils.h"
9
10 #include "include/private/base/SkTo.h"
11 #include "include/private/gpu/ganesh/GrTypesPriv.h"
12 #include "src/core/SkReadBuffer.h"
13 #include "src/core/SkWriteBuffer.h"
14 #include "src/sksl/SkSLProgramSettings.h"
15 #include "src/sksl/codegen/SkSLNativeShader.h"
16
17 #include <algorithm>
18 #include <cstddef>
19 #include <cstdint>
20 #include <vector>
21
22 namespace GrPersistentCacheUtils {
23
24 static constexpr int kCurrentVersion = 12;
25
26 int GetCurrentVersion() {
27 // The persistent cache stores a copy of the SkSL::Program::Interface struct. If you alter the
28 // Program::Interface struct in any way, you must increment kCurrentVersion to invalidate the
29 // outdated persistent cache files. The KnownSkSLProgramInterface struct must also be updated
30 // to match the new contents of Program::Interface.
31 struct KnownSkSLProgramInterface {
32 bool useLastFragColor;
33 bool useRTFlipUniform;
34 bool outputSecondaryColor;
35 };
36 static_assert(sizeof(SkSL::Program::Interface) == sizeof(KnownSkSLProgramInterface));
37
38 return kCurrentVersion;
39 }
40
41 sk_sp<SkData> PackCachedShaders(SkFourByteTag shaderType,
42 const SkSL::NativeShader shaders[],
43 const SkSL::Program::Interface interfaces[],
44 int numInterfaces,
45 const ShaderMetadata* meta) {
46 // For consistency (so tools can blindly pack and unpack cached shaders), we always write
47 // kGrShaderTypeCount interfaces. If the backend gives us fewer, we just replicate the last one.
48 SkASSERT(numInterfaces >= 1 && numInterfaces <= kGrShaderTypeCount);
49
50 SkBinaryWriteBuffer writer({});
51 writer.writeInt(kCurrentVersion);
52 writer.writeUInt(shaderType);
53 for (int i = 0; i < kGrShaderTypeCount; ++i) {
54 if (shaders[i].isBinary()) {
55 writer.writeByteArray(shaders[i].fBinary.data(),
56 shaders[i].fBinary.size() * sizeof(uint32_t));
57 } else {
58 writer.writeByteArray(shaders[i].fText.c_str(), shaders[i].fText.size());
59 }
60
61 writer.writePad32(&interfaces[std::min(i, numInterfaces - 1)],
62 sizeof(SkSL::Program::Interface));
63 }
64 writer.writeBool(SkToBool(meta));
65 if (meta) {
66 writer.writeBool(SkToBool(meta->fSettings));
67 if (meta->fSettings) {
68 writer.writeBool(meta->fSettings->fForceNoRTFlip);
69 writer.writeBool(meta->fSettings->fFragColorIsInOut);
70 writer.writeBool(meta->fSettings->fForceHighPrecision);
71 writer.writeBool(meta->fSettings->fUseVulkanPushConstantsForGaneshRTAdjust);
72 }
73
74 writer.writeInt(meta->fAttributeNames.size());
75 for (const auto& attr : meta->fAttributeNames) {
76 writer.writeByteArray(attr.c_str(), attr.size());
77 }
78
79 writer.writeBool(meta->fHasSecondaryColorOutput);
80
81 if (meta->fPlatformData) {
82 writer.writeByteArray(meta->fPlatformData->data(), meta->fPlatformData->size());
83 }
84 }
85 return writer.snapshotAsData();
86 }
87
88 SkFourByteTag GetType(SkReadBuffer* reader) {
89 constexpr SkFourByteTag kInvalidTag = ~0;
90 int version = reader->readInt();
91 SkFourByteTag typeTag = reader->readUInt();
92 return reader->validate(version == kCurrentVersion) ? typeTag : kInvalidTag;
93 }
94
95 bool UnpackCachedShaders(SkReadBuffer* reader,
96 SkSL::NativeShader shaders[],
97 bool areShadersBinary,
98 SkSL::Program::Interface interfaces[],
99 int numInterfaces,
100 ShaderMetadata* meta) {
101 for (int i = 0; i < kGrShaderTypeCount; ++i) {
102 size_t shaderLen = 0;
103 const void* shaderBuf = reader->skipByteArray(&shaderLen);
104 if (shaderBuf) {
105 if (areShadersBinary) {
106 const uint32_t* words = static_cast<const uint32_t*>(shaderBuf);
107 SkASSERT(shaderLen % 4 == 0);
108 shaders[i].fBinary.insert(
109 shaders[i].fBinary.end(), words, words + shaderLen / sizeof(uint32_t));
110 } else {
111 shaders[i].fText.assign(static_cast<const char*>(shaderBuf), shaderLen);
112 }
113 }
114
115 // GL, for example, only wants one Interface
116 if (i < numInterfaces) {
117 reader->readPad32(&interfaces[i], sizeof(interfaces[i]));
118 } else {
119 reader->skip(sizeof(SkSL::Program::Interface));
120 }
121 }
122 if (reader->readBool() && meta) {
123 SkASSERT(meta->fSettings != nullptr);
124
125 if (reader->readBool()) {
126 meta->fSettings->fForceNoRTFlip = reader->readBool();
127 meta->fSettings->fFragColorIsInOut = reader->readBool();
128 meta->fSettings->fForceHighPrecision = reader->readBool();
129 meta->fSettings->fUseVulkanPushConstantsForGaneshRTAdjust = reader->readBool();
130 }
131
132 meta->fAttributeNames.resize(reader->readInt());
133 for (auto& attr : meta->fAttributeNames) {
134 size_t attrLen = 0;
135 const char* attrName = static_cast<const char*>(reader->skipByteArray(&attrLen));
136 if (attrName) {
137 attr.assign(attrName, attrLen);
138 }
139 }
140
141 meta->fHasSecondaryColorOutput = reader->readBool();
142
143 // a given platform will be responsible for reading its data
144 }
145
146 if (!reader->isValid()) {
147 for (int i = 0; i < kGrShaderTypeCount; ++i) {
148 shaders[i].fText.clear();
149 shaders[i].fBinary.clear();
150 }
151 }
152 return reader->isValid();
153 }
154
155 } // namespace GrPersistentCacheUtils