• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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/PipelineData.h"
9 
10 #include "src/core/SkChecksum.h"
11 #include "src/gpu/graphite/ShaderCodeDictionary.h"
12 
13 namespace skgpu::graphite {
14 
PipelineDataGatherer(const Caps * caps,Layout layout)15 PipelineDataGatherer::PipelineDataGatherer(const Caps* caps, Layout layout)
16         : fCaps(caps), fUniformManager(layout) {}
17 
resetWithNewLayout(Layout layout)18 void PipelineDataGatherer::resetWithNewLayout(Layout layout) {
19     fUniformManager.resetWithNewLayout(layout);
20     fTextureDataBlock.reset();
21 }
22 
23 #ifdef SK_DEBUG
checkReset()24 void PipelineDataGatherer::checkReset() {
25     SkASSERT(fTextureDataBlock.empty());
26     SkASSERT(fUniformManager.isReset());
27 }
28 
setExpectedUniforms(SkSpan<const Uniform> expectedUniforms)29 void PipelineDataGatherer::setExpectedUniforms(SkSpan<const Uniform> expectedUniforms) {
30     fUniformManager.setExpectedUniforms(expectedUniforms);
31 }
32 #endif // SK_DEBUG
33 
34 ////////////////////////////////////////////////////////////////////////////////////////////////////
Make(const UniformDataBlock & other,SkArenaAlloc * arena)35 UniformDataBlock* UniformDataBlock::Make(const UniformDataBlock& other, SkArenaAlloc* arena) {
36     static constexpr size_t kUniformAlignment = alignof(void*);
37     char* mem = static_cast<char*>(arena->makeBytesAlignedTo(other.size(), kUniformAlignment));
38     memcpy(mem, other.data(), other.size());
39 
40     return arena->make([&](void* ptr) {
41         return new (ptr) UniformDataBlock(SkSpan<const char>(mem, other.size()));
42     });
43 }
44 
hash() const45 uint32_t UniformDataBlock::hash() const {
46     return SkChecksum::Hash32(fData.data(), fData.size());
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////////////////////////
Make(const TextureDataBlock & other,SkArenaAlloc * arena)50 TextureDataBlock* TextureDataBlock::Make(const TextureDataBlock& other,
51                                              SkArenaAlloc* arena) {
52     return arena->make([&](void *ptr) {
53         return new (ptr) TextureDataBlock(other);
54     });
55 }
56 
operator ==(const TextureDataBlock & other) const57 bool TextureDataBlock::operator==(const TextureDataBlock& other) const {
58     if (fTextureData.size() != other.fTextureData.size()) {
59         return false;
60     }
61 
62     for (size_t i = 0; i < fTextureData.size(); ++i) {
63         if (fTextureData[i] != other.fTextureData[i]) {
64             return false;
65         }
66     }
67 
68     return true;
69 }
70 
hash() const71 uint32_t TextureDataBlock::hash() const {
72     uint32_t hash = 0;
73 
74     for (auto& d : fTextureData) {
75         SamplerDesc samplerKey = std::get<1>(d);
76         hash = SkChecksum::Hash32(&samplerKey, sizeof(samplerKey), hash);
77 
78         // Because the lifetime of the TextureDataCache is for just one Recording and the
79         // TextureDataBlocks hold refs on their proxies, we can just use the proxy's pointer
80         // for the hash here.
81         uintptr_t proxy = reinterpret_cast<uintptr_t>(std::get<0>(d).get());
82         hash = SkChecksum::Hash32(&proxy, sizeof(proxy), hash);
83     }
84 
85     return hash;
86 }
87 
88 #ifdef SK_DEBUG
UniformExpectationsValidator(PipelineDataGatherer * gatherer,SkSpan<const Uniform> expectedUniforms)89 UniformExpectationsValidator::UniformExpectationsValidator(PipelineDataGatherer *gatherer,
90                                                            SkSpan<const Uniform> expectedUniforms)
91         : fGatherer(gatherer) {
92     fGatherer->setExpectedUniforms(expectedUniforms);
93 }
94 #endif
95 
96 } // namespace skgpu::graphite
97