• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 "tests/Test.h"
9 
10 #include "experimental/graphite/include/Context.h"
11 #include "experimental/graphite/include/Recorder.h"
12 #include "experimental/graphite/src/RecorderPriv.h"
13 #include "experimental/graphite/src/UniformCache.h"
14 #include "src/core/SkUniform.h"
15 #include "src/core/SkUniformData.h"
16 
17 using namespace skgpu;
18 
19 namespace {
20 
make_ub(int numUniforms,int dataSize)21 std::unique_ptr<SkUniformBlock> make_ub(int numUniforms, int dataSize) {
22     static constexpr int kMaxUniforms = 3;
23     static constexpr SkUniform kUniforms[kMaxUniforms] {
24         {"point0",   SkSLType::kFloat2 },
25         {"point1",   SkSLType::kFloat2 },
26         {"point2",   SkSLType::kFloat2 },
27     };
28 
29     SkASSERT(numUniforms <= kMaxUniforms);
30 
31     sk_sp<SkUniformData> ud = SkUniformData::Make(SkSpan<const SkUniform>(kUniforms, numUniforms),
32                                                   dataSize);
33     for (int i = 0; i < numUniforms; ++i) {
34         ud->offsets()[i] = i;
35     }
36     for (int i = 0; i < dataSize; ++i) {
37         ud->data()[i] = i % 255;
38     }
39 
40     return std::make_unique<SkUniformBlock>(std::move(ud));
41 }
42 
43 } // anonymous namespace
44 
DEF_GRAPHITE_TEST_FOR_CONTEXTS(UniformCacheTest,reporter,context)45 DEF_GRAPHITE_TEST_FOR_CONTEXTS(UniformCacheTest, reporter, context) {
46     std::unique_ptr<Recorder> recorder = context->makeRecorder();
47 
48     auto cache = recorder->priv().uniformCache();
49 
50     REPORTER_ASSERT(reporter, cache->count() == 0);
51 
52     // Nullptr should already be in the cache and return kInvalidUniformID
53     {
54         uint32_t result0 = cache->insert(nullptr);
55         REPORTER_ASSERT(reporter, result0 == UniformCache::kInvalidUniformID);
56         REPORTER_ASSERT(reporter, cache->count() == 0);
57     }
58 
59     // Add a new unique UB
60     SkUniformBlock* danglingUB1 = nullptr;
61     uint32_t result1;
62     {
63         std::unique_ptr<SkUniformBlock> ub1 = make_ub(2, 16);
64         danglingUB1 = ub1.get();
65         result1 = cache->insert(std::move(ub1));
66         REPORTER_ASSERT(reporter, result1 != UniformCache::kInvalidUniformID);
67         SkUniformBlock* lookup = cache->lookup(result1);
68         REPORTER_ASSERT(reporter, lookup == danglingUB1);
69 
70         REPORTER_ASSERT(reporter, cache->count() == 1);
71     }
72 
73     // Try to add a duplicate UB
74     {
75         std::unique_ptr<SkUniformBlock> ub2 = make_ub(2, 16);
76         SkUniformBlock* danglingUB2 = ub2.get();
77         uint32_t result2 = cache->insert(std::move(ub2));
78         REPORTER_ASSERT(reporter, result2 != UniformCache::kInvalidUniformID);
79         REPORTER_ASSERT(reporter, result2 == result1);
80         SkUniformBlock* lookup = cache->lookup(result2);
81         REPORTER_ASSERT(reporter, lookup != danglingUB2);
82         REPORTER_ASSERT(reporter, lookup == danglingUB1);
83 
84         REPORTER_ASSERT(reporter, cache->count() == 1);
85     }
86 
87     // Add a second new unique UB
88     {
89         std::unique_ptr<SkUniformBlock> ub3 = make_ub(3, 16);
90         SkUniformBlock* danglingUB3 = ub3.get();
91         uint32_t result3 = cache->insert(std::move(ub3));
92         REPORTER_ASSERT(reporter, result3 != UniformCache::kInvalidUniformID);
93         REPORTER_ASSERT(reporter, result3 != result1);
94         SkUniformBlock* lookup = cache->lookup(result3);
95         REPORTER_ASSERT(reporter, lookup == danglingUB3);
96         REPORTER_ASSERT(reporter, lookup != danglingUB1);
97 
98         REPORTER_ASSERT(reporter, cache->count() == 2);
99     }
100 
101     // TODO(robertphillips): expand this test to exercise all the UD comparison failure modes
102 }
103