| 1 |
/* |
| 2 |
* Copyright 2025 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/graphite/SerializationUtils.h" |
| 9 |
|
| 10 |
#include "include/core/SkFourByteTag.h" |
| 11 |
#include "include/core/SkStream.h" |
| 12 |
#include "src/base/SkAutoMalloc.h" |
| 13 |
#include "src/gpu/SwizzlePriv.h" |
| 14 |
#include "src/gpu/graphite/Caps.h" |
| 15 |
#include "src/gpu/graphite/GraphicsPipelineDesc.h" |
| 16 |
#include "src/gpu/graphite/PaintParamsKey.h" |
| 17 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 18 |
#include "src/gpu/graphite/Renderer.h" |
| 19 |
#include "src/gpu/graphite/ShaderCodeDictionary.h" |
| 20 |
#include "src/gpu/graphite/TextureInfoPriv.h" |
| 21 |
|
| 22 |
namespace skgpu::graphite { |
| 23 |
|
| 24 |
// This is the main control to version the serialized Pipelines (c.f. stream_is_blob) |
| 25 |
static const int kCurrent_Version = 1; |
| 26 |
|
| 27 |
namespace { |
| 28 |
|
| 29 |
static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'p', 'e' }; |
| 30 |
|
| 31 |
constexpr bool is_valid_samplecount(uint32_t sampleCount) { |
| 32 |
return SkIsPow2(sampleCount) && sampleCount >= 1 && sampleCount <= 16; |
| 33 |
} |
| 34 |
|
| 35 |
[[nodiscard]] bool stream_is_pipeline(SkStream* stream) { |
| 36 |
char magic[8]; |
| 37 |
static_assert(sizeof(kMagic) == sizeof(magic), ""); |
| 38 |
|
| 39 |
if (stream->read(magic, sizeof(kMagic)) != sizeof(kMagic)) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
if (0 != memcmp(magic, kMagic, sizeof(kMagic))) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
uint32_t version; |
| 48 |
if (!stream->readU32(&version)) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
if (version != kCurrent_Version) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
[[nodiscard]] bool serialize_graphics_pipeline_desc(ShaderCodeDictionary* shaderCodeDictionary, |
| 60 |
SkWStream* stream, |
| 61 |
const GraphicsPipelineDesc& pipelineDesc) { |
| 62 |
PaintParamsKey key = shaderCodeDictionary->lookup(pipelineDesc.paintParamsID()); |
| 63 |
|
| 64 |
if (!stream->write32(static_cast<uint32_t>(pipelineDesc.renderStepID()))) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
if (!key.isValid()) { |
| 69 |
if (!stream->write32(0)) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
// Not all GraphicsPipeline have a valid PaintParamsKey |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
const SkSpan<const uint32_t> keySpan = key.data(); |
| 77 |
|
| 78 |
if (!key.isSerializable(shaderCodeDictionary)) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
if (!stream->write32(SkToU32(keySpan.size()))) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
if (!stream->write(keySpan.data(), 4 * keySpan.size())) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
[[nodiscard]] bool deserialize_graphics_pipeline_desc(ShaderCodeDictionary* shaderCodeDictionary, |
| 92 |
SkStream* stream, |
| 93 |
GraphicsPipelineDesc* pipelineDesc) { |
| 94 |
uint32_t tmp; |
| 95 |
if (!stream->readU32(&tmp)) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
if (tmp >= RenderStep::kNumRenderSteps) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
RenderStep::RenderStepID renderStepID = static_cast<RenderStep::RenderStepID>(tmp); |
| 103 |
|
| 104 |
if (!stream->readU32(&tmp)) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
UniquePaintParamsID paintParamsID = UniquePaintParamsID::Invalid(); |
| 109 |
if (tmp) { |
| 110 |
SkAutoMalloc storage(4 * tmp); |
| 111 |
if (stream->read(storage.get(), 4 * tmp) != 4 * tmp) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
PaintParamsKey ppk = PaintParamsKey(SkSpan<uint32_t>((uint32_t*) storage.get(), tmp)); |
| 116 |
|
| 117 |
if (!ppk.isSerializable(shaderCodeDictionary)) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
paintParamsID = shaderCodeDictionary->findOrCreate(ppk); |
| 122 |
} |
| 123 |
|
| 124 |
*pipelineDesc = GraphicsPipelineDesc(renderStepID, paintParamsID); |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
[[nodiscard]] bool serialize_attachment_desc(SkWStream* stream, |
| 129 |
const AttachmentDesc& attachmentDesc) { |
| 130 |
uint32_t tag = attachmentDesc.fFormat == TextureFormat::kUnsupported |
| 131 |
? SkSetFourByteTag(static_cast<uint8_t>(TextureFormat::kUnsupported), 0, 0, 1) |
| 132 |
: SkSetFourByteTag(static_cast<uint8_t>(attachmentDesc.fFormat), |
| 133 |
static_cast<uint8_t>(attachmentDesc.fLoadOp), |
| 134 |
static_cast<uint8_t>(attachmentDesc.fStoreOp), |
| 135 |
static_cast<uint8_t>(attachmentDesc.fSampleCount)); |
| 136 |
return stream->write32(tag); |
| 137 |
} |
| 138 |
|
| 139 |
[[nodiscard]] bool deserialize_attachment_desc(SkStream* stream, |
| 140 |
AttachmentDesc* attachmentDesc) { |
| 141 |
uint32_t tag; |
| 142 |
if (!stream->readU32(&tag)) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
uint8_t format = static_cast<uint8_t>((tag >> 24) & 0xFF); |
| 147 |
uint8_t loadOp = static_cast<uint8_t>((tag >> 16) & 0xFF); |
| 148 |
uint8_t storeOp = static_cast<uint8_t>((tag >> 8) & 0xFF); |
| 149 |
uint8_t sampleCount = static_cast<uint8_t>((tag >> 0) & 0xFF); |
| 150 |
|
| 151 |
if (format >= kTextureFormatCount) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
if (loadOp >= kLoadOpCount) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
if (storeOp >= kStoreOpCount) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
if (!is_valid_samplecount(sampleCount)) { |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
*attachmentDesc = {static_cast<TextureFormat>(format), |
| 165 |
static_cast<LoadOp>(loadOp), |
| 166 |
static_cast<StoreOp>(storeOp), |
| 167 |
static_cast<SampleCount>(sampleCount)}; |
| 168 |
|
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
[[nodiscard]] bool serialize_render_pass_desc(SkWStream* stream, |
| 173 |
const RenderPassDesc& renderPassDesc) { |
| 174 |
if (!serialize_attachment_desc(stream, renderPassDesc.fColorAttachment)) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
if (!serialize_attachment_desc(stream, renderPassDesc.fColorResolveAttachment)) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
if (!serialize_attachment_desc(stream, renderPassDesc.fDepthStencilAttachment)) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
if (!stream->write16(renderPassDesc.fWriteSwizzle.asKey())) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
if (!stream->write8(static_cast<uint8_t>(renderPassDesc.fSampleCount))) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
// Omit clear values for the various attachments as they do not effect structure. |
| 192 |
// Omit fDstReadStrategy from the serialization because it is not a part of RenderPassDesc |
| 193 |
// keys and does not impact pipeline creation. When deserializing, the strategy can be |
| 194 |
// obtained via caps->getDstReadStrategy(). |
| 195 |
|
| 196 |
return true; |
| 197 |
} |
| 198 |
|
| 199 |
[[nodiscard]] bool deserialize_render_pass_desc(const Caps* caps, |
| 200 |
SkStream* stream, |
| 201 |
RenderPassDesc* renderPassDesc) { |
| 202 |
if (!deserialize_attachment_desc(stream, &renderPassDesc->fColorAttachment)) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
if (!deserialize_attachment_desc(stream, &renderPassDesc->fColorResolveAttachment)) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
if (!deserialize_attachment_desc(stream, &renderPassDesc->fDepthStencilAttachment)) { |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
uint16_t swizzle; |
| 213 |
if (!stream->readU16(&swizzle)) { |
| 214 |
return false; |
| 215 |
} |
| 216 |
renderPassDesc->fWriteSwizzle = SwizzleCtorAccessor::Make(swizzle); |
| 217 |
|
| 218 |
uint8_t sampleCount; |
| 219 |
if (!stream->readU8(&sampleCount) || !is_valid_samplecount(sampleCount)) { |
| 220 |
return false; |
| 221 |
} |
| 222 |
renderPassDesc->fSampleCount = static_cast<SampleCount>(sampleCount); |
| 223 |
|
| 224 |
// RenderPassDesc dst read strategy is not serialized as it is not something we key on and does |
| 225 |
// not impact pipeline creation. When deserializing, simply query Caps again for a |
| 226 |
// DstReadStrategy. Leave clear color/depth/stencil as their default values. |
| 227 |
renderPassDesc->fDstReadStrategy = caps->getDstReadStrategy(); |
| 228 |
|
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
#define SK_BLOB_END_TAG SkSetFourByteTag('e', 'n', 'd', ' ') |
| 233 |
|
| 234 |
bool SerializePipelineDesc(ShaderCodeDictionary* shaderCodeDictionary, |
| 235 |
SkWStream* stream, |
| 236 |
const GraphicsPipelineDesc& pipelineDesc, |
| 237 |
const RenderPassDesc& renderPassDesc) { |
| 238 |
|
| 239 |
stream->write(kMagic, sizeof(kMagic)); |
| 240 |
stream->write32(kCurrent_Version); |
| 241 |
|
| 242 |
if (!serialize_graphics_pipeline_desc(shaderCodeDictionary, stream, pipelineDesc)) { |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
if (!serialize_render_pass_desc(stream, renderPassDesc)) { |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
stream->write32(SK_BLOB_END_TAG); |
| 251 |
return true; |
| 252 |
} |
| 253 |
|
| 254 |
bool DeserializePipelineDesc(const Caps* caps, |
| 255 |
ShaderCodeDictionary* shaderCodeDictionary, |
| 256 |
SkStream* stream, |
| 257 |
GraphicsPipelineDesc* pipelineDesc, |
| 258 |
RenderPassDesc* renderPassDesc) { |
| 259 |
SkASSERT(stream); |
| 260 |
|
| 261 |
if (!stream_is_pipeline(stream)) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
if (!deserialize_graphics_pipeline_desc(shaderCodeDictionary, stream, pipelineDesc)) { |
| 266 |
return false; |
| 267 |
} |
| 268 |
|
| 269 |
if (!deserialize_render_pass_desc(caps, stream, renderPassDesc)) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
uint32_t tag; |
| 274 |
if (!stream->readU32(&tag)) { |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
if (tag != SK_BLOB_END_TAG) { |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
return true; |
| 283 |
} |
| 284 |
|
| 285 |
} // anonymous namespace |
| 286 |
|
| 287 |
sk_sp<SkData> PipelineDescToData(const Caps* caps, |
| 288 |
ShaderCodeDictionary* shaderCodeDictionary, |
| 289 |
const GraphicsPipelineDesc& pipelineDesc, |
| 290 |
const RenderPassDesc& renderPassDesc) { |
| 291 |
SkDynamicMemoryWStream stream; |
| 292 |
|
| 293 |
if (!SerializePipelineDesc(shaderCodeDictionary, |
| 294 |
&stream, |
| 295 |
pipelineDesc, renderPassDesc)) { |
| 296 |
return nullptr; |
| 297 |
} |
| 298 |
|
| 299 |
sk_sp<SkData> data = stream.detachAsData(); |
| 300 |
|
| 301 |
#if 0 // Enable this to thoroughly test Pipeline serialization |
| 302 |
{ |
| 303 |
// Check that the PipelineDesc round trips through serialization |
| 304 |
GraphicsPipelineDesc readBackPipelineDesc; |
| 305 |
RenderPassDesc readBackRenderPassDesc; |
| 306 |
|
| 307 |
SkAssertResult(DataToPipelineDesc(caps, |
| 308 |
shaderCodeDictionary, |
| 309 |
data.get(), |
| 310 |
&readBackPipelineDesc, |
| 311 |
&readBackRenderPassDesc)); |
| 312 |
|
| 313 |
DumpPipelineDesc("invokeCallback - original", shaderCodeDictionary, |
| 314 |
pipelineDesc, renderPassDesc); |
| 315 |
|
| 316 |
DumpPipelineDesc("invokeCallback - readback", shaderCodeDictionary, |
| 317 |
readBackPipelineDesc, readBackRenderPassDesc); |
| 318 |
|
| 319 |
SkASSERT(ComparePipelineDescs(pipelineDesc, renderPassDesc, |
| 320 |
readBackPipelineDesc, readBackRenderPassDesc)); |
| 321 |
} |
| 322 |
#endif |
| 323 |
|
| 324 |
return data; |
| 325 |
} |
| 326 |
|
| 327 |
bool DataToPipelineDesc(const Caps* caps, |
| 328 |
ShaderCodeDictionary* shaderCodeDictionary, |
| 329 |
const SkData* data, |
| 330 |
GraphicsPipelineDesc* pipelineDesc, |
| 331 |
RenderPassDesc* renderPassDesc) { |
| 332 |
if (!data) { |
| 333 |
return false; |
| 334 |
} |
| 335 |
SkMemoryStream stream(data->data(), data->size()); |
| 336 |
|
| 337 |
if (!DeserializePipelineDesc(caps, |
| 338 |
shaderCodeDictionary, |
| 339 |
&stream, |
| 340 |
pipelineDesc, |
| 341 |
renderPassDesc)) { |
| 342 |
return false; |
| 343 |
} |
| 344 |
|
| 345 |
return true; |
| 346 |
} |
| 347 |
|
| 348 |
#if defined(GPU_TEST_UTILS) |
| 349 |
void DumpPipelineDesc(const Caps* caps, |
| 350 |
const char* label, |
| 351 |
ShaderCodeDictionary* shaderCodeDictionary, |
| 352 |
const GraphicsPipelineDesc& pipelineDesc, |
| 353 |
const RenderPassDesc& renderPassDesc) { |
| 354 |
SkString pipelineStr = pipelineDesc.toString(caps, shaderCodeDictionary); |
| 355 |
SkString renderPassStr = renderPassDesc.toPipelineLabel(); |
| 356 |
SkDebugf("%s: %s - %s\n", label, pipelineStr.c_str(), renderPassStr.c_str()); |
| 357 |
} |
| 358 |
|
| 359 |
bool ComparePipelineDescs(const GraphicsPipelineDesc& a1, const RenderPassDesc& b1, |
| 360 |
const GraphicsPipelineDesc& a2, const RenderPassDesc& b2) { |
| 361 |
return (a1 == a2) && (b1 == b2); |
| 362 |
} |
| 363 |
#endif |
| 364 |
|
| 365 |
} // namespace skgpu::graphite |