Add generic uniform setter function to SkRuntimeShaderBuilder

Change-Id: Idab539a4b39fe5ceab54948c99c0dcd6d19fd345
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/352743
Commit-Queue: Derek Sollenberger <djsollen@google.com>
Auto-Submit: Derek Sollenberger <djsollen@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
This commit is contained in:
Derek Sollenberger 2021-01-14 08:30:52 -05:00 committed by Skia Commit-Bot
parent 3d81fdcbd2
commit 9e1cedda63
2 changed files with 45 additions and 0 deletions

View File

@ -222,6 +222,22 @@ public:
return *this; return *this;
} }
template <typename T>
bool set(const T val[], const int count) {
static_assert(std::is_trivially_copyable<T>::value, "Value must be trivial copyable");
if (!fVar) {
SkDEBUGFAIL("Assigning to missing variable");
return false;
} else if (sizeof(T) * count != fVar->sizeInBytes()) {
SkDEBUGFAIL("Incorrect value size");
return false;
} else {
memcpy(SkTAddOffset<void>(fOwner->writableUniformData(), fVar->fOffset),
val, sizeof(T) * count);
}
return true;
}
SkRuntimeShaderBuilder* fOwner; SkRuntimeShaderBuilder* fOwner;
const SkRuntimeEffect::Uniform* fVar; // nullptr if the variable was not found const SkRuntimeEffect::Uniform* fVar; // nullptr if the variable was not found
}; };

View File

@ -334,6 +334,35 @@ DEF_TEST(SkRuntimeShaderBuilderReuse, r) {
auto shader_1 = b.makeShader(nullptr, true); auto shader_1 = b.makeShader(nullptr, true);
} }
DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) {
const char* kSource = R"(
uniform half x;
uniform vec2 offset;
half4 main() { return half4(x); }
)";
sk_sp<SkRuntimeEffect> effect = std::get<0>(SkRuntimeEffect::Make(SkString(kSource)));
REPORTER_ASSERT(r, effect);
SkRuntimeShaderBuilder b(std::move(effect));
// Test passes if this sequence doesn't assert.
float x = 1.0f;
REPORTER_ASSERT(r, b.uniform("x").set(&x, 1));
// add extra value to ensure that set doesn't try to use sizeof(array)
float origin[] = { 2.0f, 3.0f, 4.0f };
REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2));
#ifndef SK_DEBUG
REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1));
REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3));
#endif
auto shader = b.makeShader(nullptr, false);
}
DEF_TEST(SkRuntimeEffectThreaded, r) { DEF_TEST(SkRuntimeEffectThreaded, r) {
// SkRuntimeEffect uses a single compiler instance, but it's mutex locked. // SkRuntimeEffect uses a single compiler instance, but it's mutex locked.
// This tests that we can safely use it from more than one thread, and also // This tests that we can safely use it from more than one thread, and also