blob: f38d78b61ee424667420d425ad27b40aa2c18c13 [file] [log] [blame]
Brian Osman6b797fe2019-04-08 13:56:36 -04001/*
2 * Copyright 2019 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#ifndef GrPersistentCacheEntry_DEFINED
9#define GrPersistentCacheEntry_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkData.h"
12#include "include/private/GrTypesPriv.h"
13#include "src/core/SkReader32.h"
14#include "src/core/SkWriter32.h"
15#include "src/sksl/SkSLString.h"
16#include "src/sksl/ir/SkSLProgram.h"
Brian Osman6b797fe2019-04-08 13:56:36 -040017
Brian Osmana5a010b2019-04-08 15:01:32 -040018// The GrPersistentCache stores opaque blobs, as far as clients are concerned. It's helpful to
19// inspect certain kinds of cached data within our tools, so for those cases (GLSL, SPIR-V), we
20// put the serialization logic here, to be shared by the backend code and the tool code.
Brian Osman6b797fe2019-04-08 13:56:36 -040021namespace GrPersistentCacheUtils {
22
Brian Osman4524e842019-09-24 16:03:41 -040023struct ShaderMetadata {
24 SkSL::Program::Settings* fSettings = nullptr;
25 SkTArray<SkSL::String> fAttributeNames;
26 bool fHasCustomColorOutput = false;
27 bool fHasSecondaryColorOutput = false;
28};
29
Brian Osmana085a412019-04-25 09:44:43 -040030static inline sk_sp<SkData> PackCachedShaders(SkFourByteTag shaderType,
31 const SkSL::String shaders[],
32 const SkSL::Program::Inputs inputs[],
Brian Osmaned58e002019-09-06 14:42:43 -040033 int numInputs,
Brian Osman4524e842019-09-24 16:03:41 -040034 const ShaderMetadata* meta = nullptr) {
Brian Osmana085a412019-04-25 09:44:43 -040035 // For consistency (so tools can blindly pack and unpack cached shaders), we always write
36 // kGrShaderTypeCount inputs. If the backend gives us fewer, we just replicate the last one.
37 SkASSERT(numInputs >= 1 && numInputs <= kGrShaderTypeCount);
Brian Osman6b797fe2019-04-08 13:56:36 -040038
Brian Osmane6ef03d2019-04-11 14:38:27 -040039 SkWriter32 writer;
Brian Osmana085a412019-04-25 09:44:43 -040040 writer.write32(shaderType);
Brian Osmana5a010b2019-04-08 15:01:32 -040041 for (int i = 0; i < kGrShaderTypeCount; ++i) {
42 writer.writeString(shaders[i].c_str(), shaders[i].size());
Brian Osman788b9162020-02-07 10:36:46 -050043 writer.writePad(&inputs[std::min(i, numInputs - 1)], sizeof(SkSL::Program::Inputs));
Brian Osmana5a010b2019-04-08 15:01:32 -040044 }
Brian Osman4524e842019-09-24 16:03:41 -040045 writer.writeBool(SkToBool(meta));
46 if (meta) {
47 writer.writeBool(SkToBool(meta->fSettings));
48 if (meta->fSettings) {
49 writer.writeBool(meta->fSettings->fFlipY);
50 writer.writeBool(meta->fSettings->fFragColorIsInOut);
51 writer.writeBool(meta->fSettings->fForceHighPrecision);
52 }
53
54 writer.writeInt(meta->fAttributeNames.count());
55 for (const auto& attr : meta->fAttributeNames) {
56 writer.writeString(attr.c_str(), attr.size());
57 }
58
59 writer.writeBool(meta->fHasCustomColorOutput);
60 writer.writeBool(meta->fHasSecondaryColorOutput);
Brian Osmaned58e002019-09-06 14:42:43 -040061 }
Brian Osmane6ef03d2019-04-11 14:38:27 -040062 return writer.snapshotAsData();
Brian Osmana5a010b2019-04-08 15:01:32 -040063}
64
Brian Osmana66081d2019-09-03 14:59:26 -040065static inline void UnpackCachedShaders(SkReader32* reader,
66 SkSL::String shaders[],
67 SkSL::Program::Inputs inputs[],
Brian Osmaned58e002019-09-06 14:42:43 -040068 int numInputs,
Brian Osman4524e842019-09-24 16:03:41 -040069 ShaderMetadata* meta = nullptr) {
Brian Osmana5a010b2019-04-08 15:01:32 -040070 for (int i = 0; i < kGrShaderTypeCount; ++i) {
71 size_t stringLen = 0;
Brian Osmana66081d2019-09-03 14:59:26 -040072 const char* string = reader->readString(&stringLen);
Brian Osmana5a010b2019-04-08 15:01:32 -040073 shaders[i] = SkSL::String(string, stringLen);
Brian Osmana085a412019-04-25 09:44:43 -040074
75 // GL, for example, only wants one set of Inputs
76 if (i < numInputs) {
Brian Osmana66081d2019-09-03 14:59:26 -040077 reader->read(&inputs[i], sizeof(inputs[i]));
Brian Osmana085a412019-04-25 09:44:43 -040078 } else {
Brian Osmana66081d2019-09-03 14:59:26 -040079 reader->skip(sizeof(SkSL::Program::Inputs));
Brian Osmana085a412019-04-25 09:44:43 -040080 }
Brian Osmana5a010b2019-04-08 15:01:32 -040081 }
Brian Osman4524e842019-09-24 16:03:41 -040082 if (reader->readBool() && meta) {
83 SkASSERT(meta->fSettings != nullptr);
84
85 if (reader->readBool()) {
86 meta->fSettings->fFlipY = reader->readBool();
87 meta->fSettings->fFragColorIsInOut = reader->readBool();
88 meta->fSettings->fForceHighPrecision = reader->readBool();
89 }
90
91 meta->fAttributeNames.resize(reader->readInt());
92 for (int i = 0; i < meta->fAttributeNames.count(); ++i) {
93 size_t stringLen = 0;
94 const char* string = reader->readString(&stringLen);
95 meta->fAttributeNames[i] = SkSL::String(string, stringLen);
96 }
97
98 meta->fHasCustomColorOutput = reader->readBool();
99 meta->fHasSecondaryColorOutput = reader->readBool();
Brian Osmaned58e002019-09-06 14:42:43 -0400100 }
Brian Osmana5a010b2019-04-08 15:01:32 -0400101}
102
Brian Osman6b797fe2019-04-08 13:56:36 -0400103}
104
105#endif