Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #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 Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 17 | |
Brian Osman | a5a010b | 2019-04-08 15:01:32 -0400 | [diff] [blame] | 18 | // 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 Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 21 | namespace GrPersistentCacheUtils { |
| 22 | |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 23 | struct ShaderMetadata { |
| 24 | SkSL::Program::Settings* fSettings = nullptr; |
| 25 | SkTArray<SkSL::String> fAttributeNames; |
| 26 | bool fHasCustomColorOutput = false; |
| 27 | bool fHasSecondaryColorOutput = false; |
| 28 | }; |
| 29 | |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 30 | static inline sk_sp<SkData> PackCachedShaders(SkFourByteTag shaderType, |
| 31 | const SkSL::String shaders[], |
| 32 | const SkSL::Program::Inputs inputs[], |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 33 | int numInputs, |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 34 | const ShaderMetadata* meta = nullptr) { |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 35 | // 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 Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 38 | |
Brian Osman | e6ef03d | 2019-04-11 14:38:27 -0400 | [diff] [blame] | 39 | SkWriter32 writer; |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 40 | writer.write32(shaderType); |
Brian Osman | a5a010b | 2019-04-08 15:01:32 -0400 | [diff] [blame] | 41 | for (int i = 0; i < kGrShaderTypeCount; ++i) { |
| 42 | writer.writeString(shaders[i].c_str(), shaders[i].size()); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 43 | writer.writePad(&inputs[std::min(i, numInputs - 1)], sizeof(SkSL::Program::Inputs)); |
Brian Osman | a5a010b | 2019-04-08 15:01:32 -0400 | [diff] [blame] | 44 | } |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 45 | 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 Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 61 | } |
Brian Osman | e6ef03d | 2019-04-11 14:38:27 -0400 | [diff] [blame] | 62 | return writer.snapshotAsData(); |
Brian Osman | a5a010b | 2019-04-08 15:01:32 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 65 | static inline void UnpackCachedShaders(SkReader32* reader, |
| 66 | SkSL::String shaders[], |
| 67 | SkSL::Program::Inputs inputs[], |
Brian Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 68 | int numInputs, |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 69 | ShaderMetadata* meta = nullptr) { |
Brian Osman | a5a010b | 2019-04-08 15:01:32 -0400 | [diff] [blame] | 70 | for (int i = 0; i < kGrShaderTypeCount; ++i) { |
| 71 | size_t stringLen = 0; |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 72 | const char* string = reader->readString(&stringLen); |
Brian Osman | a5a010b | 2019-04-08 15:01:32 -0400 | [diff] [blame] | 73 | shaders[i] = SkSL::String(string, stringLen); |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 74 | |
| 75 | // GL, for example, only wants one set of Inputs |
| 76 | if (i < numInputs) { |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 77 | reader->read(&inputs[i], sizeof(inputs[i])); |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 78 | } else { |
Brian Osman | a66081d | 2019-09-03 14:59:26 -0400 | [diff] [blame] | 79 | reader->skip(sizeof(SkSL::Program::Inputs)); |
Brian Osman | a085a41 | 2019-04-25 09:44:43 -0400 | [diff] [blame] | 80 | } |
Brian Osman | a5a010b | 2019-04-08 15:01:32 -0400 | [diff] [blame] | 81 | } |
Brian Osman | 4524e84 | 2019-09-24 16:03:41 -0400 | [diff] [blame] | 82 | 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 Osman | ed58e00 | 2019-09-06 14:42:43 -0400 | [diff] [blame] | 100 | } |
Brian Osman | a5a010b | 2019-04-08 15:01:32 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Brian Osman | 6b797fe | 2019-04-08 13:56:36 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | #endif |