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/src/ContextUtils.h" 12#include "experimental/graphite/src/Recorder.h" 13#include "experimental/graphite/src/Uniform.h" 14#include "experimental/graphite/src/UniformCache.h" 15 16using namespace skgpu; 17 18namespace { 19 20sk_sp<UniformData> make_ud(int numUniforms, int dataSize) { 21 static constexpr int kMaxUniforms = 3; 22 static constexpr Uniform kUniforms[kMaxUniforms] { 23 {"point0", SLType::kFloat2 }, 24 {"point1", SLType::kFloat2 }, 25 {"point2", SLType::kFloat2 }, 26 }; 27 28 SkASSERT(numUniforms <= kMaxUniforms); 29 30 sk_sp<UniformData> ud = UniformData::Make(numUniforms, kUniforms, dataSize); 31 for (int i = 0; i < numUniforms; ++i) { 32 ud->offsets()[i] = i; 33 } 34 for (int i = 0; i < dataSize; ++i) { 35 ud->data()[i] = i % 255; 36 } 37 38 return ud; 39} 40 41} // anonymous namespace 42 43DEF_GRAPHITE_TEST_FOR_CONTEXTS(UniformCacheTest, reporter, context) { 44 Recorder recorder(sk_ref_sp(context)); 45 46 auto cache = recorder.uniformCache(); 47 48 REPORTER_ASSERT(reporter, cache->count() == 0); 49 50 // Add a new unique UD 51 sk_sp<UniformData> result1; 52 { 53 sk_sp<UniformData> ud1 = make_ud(2, 16); 54 result1 = cache->findOrCreate(ud1); 55 REPORTER_ASSERT(reporter, result1->id() != UniformData::kInvalidUniformID); 56 REPORTER_ASSERT(reporter, ud1->id() == result1->id()); 57 sk_sp<UniformData> lookup = cache->lookup(result1->id()); 58 REPORTER_ASSERT(reporter, lookup->id() == result1->id()); 59 60 REPORTER_ASSERT(reporter, cache->count() == 1); 61 } 62 63 // Try to add a duplicate UD 64 { 65 sk_sp<UniformData> ud2 = make_ud(2, 16); 66 sk_sp<UniformData> result2 = cache->findOrCreate(ud2); 67 REPORTER_ASSERT(reporter, result2->id() != UniformData::kInvalidUniformID); 68 REPORTER_ASSERT(reporter, ud2->id() == UniformData::kInvalidUniformID); 69 REPORTER_ASSERT(reporter, result2->id() == result1->id()); 70 sk_sp<UniformData> lookup = cache->lookup(result2->id()); 71 REPORTER_ASSERT(reporter, lookup->id() == result2->id()); 72 73 REPORTER_ASSERT(reporter, cache->count() == 1); 74 } 75 76 // Add a second new unique UD 77 { 78 sk_sp<UniformData> ud3 = make_ud(3, 16); 79 sk_sp<UniformData> result3 = cache->findOrCreate(ud3); 80 REPORTER_ASSERT(reporter, result3->id() != UniformData::kInvalidUniformID); 81 REPORTER_ASSERT(reporter, ud3->id() == result3->id()); 82 REPORTER_ASSERT(reporter, result3->id() != result1->id()); 83 sk_sp<UniformData> lookup = cache->lookup(result3->id()); 84 REPORTER_ASSERT(reporter, lookup->id() == result3->id()); 85 86 REPORTER_ASSERT(reporter, cache->count() == 2); 87 } 88 89 // TODO(robertphillips): expand this test to exercise all the UD comparison failure modes 90} 91