1 /*
2 * Copyright 2024 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 "tools/graphite/UniqueKeyUtils.h"
9
10 #include "src/gpu/ResourceKey.h"
11 #include "src/gpu/graphite/Caps.h"
12 #include "src/gpu/graphite/ContextPriv.h"
13 #include "src/gpu/graphite/GraphicsPipelineDesc.h"
14 #include "src/gpu/graphite/RenderPassDesc.h"
15 #include "src/gpu/graphite/RendererProvider.h"
16
17 using namespace skgpu::graphite;
18 using namespace skgpu;
19
20
21 namespace UniqueKeyUtils {
22
FetchUniqueKeys(GlobalCache * globalCache,std::vector<UniqueKey> * keys)23 void FetchUniqueKeys(GlobalCache* globalCache,
24 std::vector<UniqueKey>* keys) {
25 keys->reserve(globalCache->numGraphicsPipelines());
26 globalCache->forEachGraphicsPipeline([keys](const UniqueKey& key,
27 const GraphicsPipeline* pipeline) {
28 keys->push_back(key);
29 });
30 }
31
32 #ifdef SK_DEBUG
DumpDescs(const RendererProvider * rendererProvider,const ShaderCodeDictionary * dict,const GraphicsPipelineDesc & pipelineDesc,const RenderPassDesc & rpd)33 void DumpDescs(const RendererProvider* rendererProvider,
34 const ShaderCodeDictionary* dict,
35 const GraphicsPipelineDesc& pipelineDesc,
36 const RenderPassDesc& rpd) {
37 const RenderStep* rs = rendererProvider->lookup(pipelineDesc.renderStepID());
38 SkDebugf("GraphicsPipelineDesc: %u %s\n", pipelineDesc.paintParamsID().asUInt(), rs->name());
39
40 dict->dump(pipelineDesc.paintParamsID());
41
42 SkDebugf("RenderPassDesc:\n");
43 SkDebugf(" colorAttach: %s\n", rpd.fColorAttachment.toString().c_str());
44 SkDebugf(" colorResolveAttach: %s\n", rpd.fColorResolveAttachment.toString().c_str());
45 SkDebugf(" depthStencilAttach: %s\n", rpd.fDepthStencilAttachment.toString().c_str());
46 SkDebugf(" clearColor: %.2f %.2f %.2f %.2f\n"
47 " clearDepth: %.2f\n"
48 " stencilClear: %u\n"
49 " writeSwizzle: %s\n"
50 " sampleCount: %u\n",
51 rpd.fClearColor[0], rpd.fClearColor[1], rpd.fClearColor[2], rpd.fClearColor[3],
52 rpd.fClearDepth,
53 rpd.fClearStencil,
54 rpd.fWriteSwizzle.asString().c_str(),
55 rpd.fSampleCount);
56
57 }
58 #endif // SK_DEBUG
59
ExtractKeyDescs(Context * context,const UniqueKey & origKey,GraphicsPipelineDesc * pipelineDesc,RenderPassDesc * renderPassDesc)60 bool ExtractKeyDescs(Context* context,
61 const UniqueKey& origKey,
62 GraphicsPipelineDesc* pipelineDesc,
63 RenderPassDesc* renderPassDesc) {
64 const Caps* caps = context->priv().caps();
65 const RendererProvider* rendererProvider = context->priv().rendererProvider();
66
67 bool extracted = caps->extractGraphicsDescs(origKey, pipelineDesc, renderPassDesc,
68 rendererProvider);
69 if (!extracted) {
70 SkASSERT(0);
71 return false;
72 }
73
74 #ifdef SK_DEBUG
75 const ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
76
77 UniqueKey newKey = caps->makeGraphicsPipelineKey(*pipelineDesc, *renderPassDesc);
78 if (origKey != newKey) {
79 SkDebugf("------- The UniqueKey didn't round trip!\n");
80 origKey.dump("original key:");
81 newKey.dump("reassembled key:");
82 DumpDescs(rendererProvider, dict, *pipelineDesc, *renderPassDesc);
83 SkDebugf("------------------------\n");
84 }
85 SkASSERT(origKey == newKey);
86 #endif
87
88 return true;
89 }
90
91 } // namespace UniqueKeyUtils
92