-
-
Notifications
You must be signed in to change notification settings - Fork 21.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix shader uniforms has null as default value #95626
Conversation
Chaosus
commented
Aug 16, 2024
- Closes Shader uniforms has null as default value #44454
7e2b344
to
5368dff
Compare
5368dff
to
469c536
Compare
469c536
to
55fccdf
Compare
55fccdf
to
285a9e5
Compare
case ShaderLanguage::TYPE_BOOL: | ||
if (array_size > 0) { | ||
PackedInt32Array array; | ||
for (int i = 0; i < array_size; i++) { | ||
array.push_back(false); | ||
} | ||
value = Variant(array); | ||
} else { | ||
VariantInitializer<bool>::init(&value); | ||
VariantDefaultInitializer<bool>::init(&value); | ||
} | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TL;DR: I looked into how to improve this code myself but in the end I gave up.
I was still wondering if there isn't a simpler way to do this. There must exist a pre-existing method in the codebase to construct a Variant with a given type and default initialize it.
From a quick look, I found:
void Variant::construct(Variant::Type p_type, Variant &base, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
Which I think you would use like this:
Callable::CallError ce;
Variant value;
Variant::construct(variant_type, value, nullptr, 0, ce);
This would allow to simplify the cases quite a bit, like (untested):
switch (p_type) {
case ShaderLanguage::TYPE_BOOL:
if (array_size > 0) {
type = Variant::PACKED_INT32_ARRAY;
} else {
type = Variant::BOOL;
}
break;
case ShaderLanguage::TYPE_BVEC2:
if (array_size > 0) {
type = Variant::PACKED_INT32_ARRAY;
array_size *= 2;
} else {
type = Variant::INT;
}
break;
...
}
}
Callable::CallError ce;
Variant value;
Variant::construct(variant_type, value, nullptr, 0, ce);
if (array_size > 0) {
// Figure out a way to resize the array in Variant and `zero()` it.
}
But I finally noticed that this is pretty much adapted from ShaderLanguage::constant_value_to_variant
and so keeping a consistent format might be worth it. The type conversions are also less trivial than I thought with types not natively compatible with Variant like bvec2
.
And so at this point I gave up on trying to make this better :P
Would be worth a rebase before merging as this is fairly old. |
285a9e5
to
570e59d
Compare
@akien-mga Done |
Thanks! |