From 4cfae88f09d4ae5606fcd9cdc5ee494ae2d06801 Mon Sep 17 00:00:00 2001 From: David Murmann Date: Wed, 12 Jun 2024 10:37:39 +0200 Subject: [PATCH] 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 --- intern/cycles/scene/constant_fold.cpp | 13 ++++++++++ intern/cycles/scene/constant_fold.h | 1 + intern/cycles/scene/shader_graph.h | 4 ++++ intern/cycles/scene/shader_nodes.cpp | 34 +++++++++++++++++++-------- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/intern/cycles/scene/constant_fold.cpp b/intern/cycles/scene/constant_fold.cpp index 9739de59014..0be3a23ab1c 100644 --- a/intern/cycles/scene/constant_fold.cpp +++ b/intern/cycles/scene/constant_fold.cpp @@ -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); diff --git a/intern/cycles/scene/constant_fold.h b/intern/cycles/scene/constant_fold.h index c97d12e086c..6b50498adea 100644 --- a/intern/cycles/scene/constant_fold.h +++ b/intern/cycles/scene/constant_fold.h @@ -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; diff --git a/intern/cycles/scene/shader_graph.h b/intern/cycles/scene/shader_graph.h index 82ea2a781c7..b0a2a12bdea 100644 --- a/intern/cycles/scene/shader_graph.h +++ b/intern/cycles/scene/shader_graph.h @@ -96,6 +96,10 @@ class ShaderInput { { ((Node *)parent)->set(socket_type, f); } + void set(int f) + { + ((Node *)parent)->set(socket_type, f); + } void disconnect(); diff --git a/intern/cycles/scene/shader_nodes.cpp b/intern/cycles/scene/shader_nodes.cpp index 70e5ab55f2a..6e14b27e1b7 100644 --- a/intern/cycles/scene/shader_nodes.cpp +++ b/intern/cycles/scene/shader_nodes.cpp @@ -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)) {