Fix #122739: mix causes unnecessary evaluation when ints are used

Fixes #122739.

I added all five missing conversions that the convert node can do in the svm code to the constant folding.

Pull Request: https://projects.blender.org/blender/blender/pulls/123035
This commit is contained in:
David Murmann
2024-06-12 10:37:39 +02:00
committed by Sergey Sharybin
parent 4d8eea1402
commit 4cfae88f09
4 changed files with 42 additions and 10 deletions

View File

@@ -55,6 +55,19 @@ void ConstantFolder::make_constant(float3 value) const
graph->disconnect(output);
}
void ConstantFolder::make_constant(int value) const
{
VLOG_DEBUG << "Folding " << node->name << "::" << output->name() << " to constant (" << value
<< ").";
foreach (ShaderInput *sock, output->links) {
sock->set(value);
sock->constant_folded_in = true;
}
graph->disconnect(output);
}
void ConstantFolder::make_constant_clamp(float value, bool clamp) const
{
make_constant(clamp ? saturatef(value) : value);

View File

@@ -31,6 +31,7 @@ class ConstantFolder {
/* Constant folding helpers */
void make_constant(float value) const;
void make_constant(float3 value) const;
void make_constant(int value) const;
void make_constant_clamp(float value, bool clamp) const;
void make_constant_clamp(float3 value, bool clamp) const;
void make_zero() const;

View File

@@ -96,6 +96,10 @@ class ShaderInput {
{
((Node *)parent)->set(socket_type, f);
}
void set(int f)
{
((Node *)parent)->set(socket_type, f);
}
void disconnect();

View File

@@ -2123,24 +2123,38 @@ void ConvertNode::constant_fold(const ConstantFolder &folder)
/* proxy nodes should have been removed at this point */
assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
/* TODO(DingTo): conversion from/to int is not supported yet, don't fold in that case */
if (folder.all_inputs_constant()) {
if (from == SocketType::FLOAT) {
if (from == SocketType::FLOAT || from == SocketType::INT) {
float val = value_float;
if (from == SocketType::INT) {
val = value_int;
}
if (SocketType::is_float3(to)) {
folder.make_constant(make_float3(value_float, value_float, value_float));
folder.make_constant(make_float3(val, val, val));
}
else if (to == SocketType::INT) {
folder.make_constant((int)val);
}
else if (to == SocketType::FLOAT) {
folder.make_constant(val);
}
}
else if (SocketType::is_float3(from)) {
if (to == SocketType::FLOAT) {
if (to == SocketType::FLOAT || to == SocketType::INT) {
float val;
if (from == SocketType::COLOR) {
/* color to float */
float val = folder.scene->shader_manager->linear_rgb_to_gray(value_color);
folder.make_constant(val);
/* color to scalar */
val = folder.scene->shader_manager->linear_rgb_to_gray(value_color);
}
else {
/* vector/point/normal to float */
folder.make_constant(average(value_vector));
/* vector/point/normal to scalar */
val = average(value_vector);
}
if (to == SocketType::INT) {
folder.make_constant((int)val);
}
else if (to == SocketType::FLOAT) {
folder.make_constant(val);
}
}
else if (SocketType::is_float3(to)) {