2021-10-28 13:58:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tests/Test.h"
|
|
|
|
|
|
|
|
#include "experimental/graphite/include/Context.h"
|
|
|
|
#include "experimental/graphite/src/ContextUtils.h"
|
|
|
|
#include "experimental/graphite/src/Recorder.h"
|
|
|
|
#include "experimental/graphite/src/Uniform.h"
|
|
|
|
#include "experimental/graphite/src/UniformCache.h"
|
|
|
|
|
|
|
|
using namespace skgpu;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
sk_sp<UniformData> make_ud(int numUniforms, int dataSize) {
|
|
|
|
static constexpr int kMaxUniforms = 3;
|
|
|
|
static constexpr Uniform kUniforms[kMaxUniforms] {
|
|
|
|
{"point0", SLType::kFloat2 },
|
|
|
|
{"point1", SLType::kFloat2 },
|
|
|
|
{"point2", SLType::kFloat2 },
|
|
|
|
};
|
|
|
|
|
|
|
|
SkASSERT(numUniforms <= kMaxUniforms);
|
|
|
|
|
|
|
|
sk_sp<UniformData> ud = UniformData::Make(numUniforms, kUniforms, dataSize);
|
|
|
|
for (int i = 0; i < numUniforms; ++i) {
|
|
|
|
ud->offsets()[i] = i;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < dataSize; ++i) {
|
|
|
|
ud->data()[i] = i % 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ud;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
DEF_GRAPHITE_TEST_FOR_CONTEXTS(UniformCacheTest, reporter, context) {
|
|
|
|
Recorder recorder(sk_ref_sp(context));
|
|
|
|
|
|
|
|
auto cache = recorder.uniformCache();
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, cache->count() == 0);
|
|
|
|
|
2021-11-23 21:53:16 +00:00
|
|
|
// Nullptr should already be in the cache and return kInvalidUniformID
|
|
|
|
{
|
|
|
|
uint32_t result0 = cache->insert(nullptr);
|
|
|
|
REPORTER_ASSERT(reporter, result0 == UniformCache::kInvalidUniformID);
|
|
|
|
REPORTER_ASSERT(reporter, cache->count() == 0);
|
|
|
|
}
|
|
|
|
|
2021-10-28 13:58:10 +00:00
|
|
|
// Add a new unique UD
|
2021-11-23 21:53:16 +00:00
|
|
|
sk_sp<UniformData> ud1;
|
|
|
|
uint32_t result1;
|
2021-10-28 13:58:10 +00:00
|
|
|
{
|
2021-11-23 21:53:16 +00:00
|
|
|
ud1 = make_ud(2, 16);
|
|
|
|
result1 = cache->insert(ud1);
|
|
|
|
REPORTER_ASSERT(reporter, result1 != UniformCache::kInvalidUniformID);
|
|
|
|
sk_sp<UniformData> lookup = cache->lookup(result1);
|
|
|
|
REPORTER_ASSERT(reporter, lookup.get() == ud1.get());
|
2021-10-28 13:58:10 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, cache->count() == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to add a duplicate UD
|
|
|
|
{
|
|
|
|
sk_sp<UniformData> ud2 = make_ud(2, 16);
|
2021-11-23 21:53:16 +00:00
|
|
|
uint32_t result2 = cache->insert(ud2);
|
|
|
|
REPORTER_ASSERT(reporter, result2 != UniformCache::kInvalidUniformID);
|
|
|
|
REPORTER_ASSERT(reporter, result2 == result1);
|
|
|
|
sk_sp<UniformData> lookup = cache->lookup(result2);
|
|
|
|
REPORTER_ASSERT(reporter, lookup.get() != ud2.get());
|
|
|
|
REPORTER_ASSERT(reporter, lookup.get() == ud1.get());
|
2021-10-28 13:58:10 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, cache->count() == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a second new unique UD
|
|
|
|
{
|
|
|
|
sk_sp<UniformData> ud3 = make_ud(3, 16);
|
2021-11-23 21:53:16 +00:00
|
|
|
uint32_t result3 = cache->insert(ud3);
|
|
|
|
REPORTER_ASSERT(reporter, result3 != UniformCache::kInvalidUniformID);
|
|
|
|
REPORTER_ASSERT(reporter, result3 != result1);
|
|
|
|
sk_sp<UniformData> lookup = cache->lookup(result3);
|
|
|
|
REPORTER_ASSERT(reporter, lookup.get() == ud3.get());
|
|
|
|
REPORTER_ASSERT(reporter, lookup.get() != ud1.get());
|
2021-10-28 13:58:10 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, cache->count() == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(robertphillips): expand this test to exercise all the UD comparison failure modes
|
|
|
|
}
|