2023-06-14 16:52:36 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
2016-07-16 13:16:54 +02:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "scene/constant_fold.h"
|
|
|
|
|
#include "scene/shader_graph.h"
|
2016-07-16 13:16:54 +02:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "util/log.h"
|
2016-07-16 13:16:54 +02:00
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
2018-06-14 17:48:19 +02:00
|
|
|
ConstantFolder::ConstantFolder(ShaderGraph *graph,
|
|
|
|
|
ShaderNode *node,
|
|
|
|
|
ShaderOutput *output,
|
|
|
|
|
Scene *scene)
|
|
|
|
|
: graph(graph), node(node), output(output), scene(scene)
|
2016-07-16 13:16:54 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConstantFolder::all_inputs_constant() const
|
|
|
|
|
{
|
2024-12-26 19:41:25 +01:00
|
|
|
for (ShaderInput *input : node->inputs) {
|
2016-07-16 13:16:54 +02:00
|
|
|
if (input->link) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
void ConstantFolder::make_constant(const float value) const
|
2016-07-16 13:16:54 +02:00
|
|
|
{
|
2022-06-16 19:39:13 +02:00
|
|
|
VLOG_DEBUG << "Folding " << node->name << "::" << output->name() << " to constant (" << value
|
|
|
|
|
<< ").";
|
2016-08-01 18:53:20 +03:00
|
|
|
|
2024-12-26 19:41:25 +01:00
|
|
|
for (ShaderInput *sock : output->links) {
|
2016-07-16 13:16:54 +02:00
|
|
|
sock->set(value);
|
2021-06-28 23:15:32 +02:00
|
|
|
sock->constant_folded_in = true;
|
2016-07-16 13:16:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
graph->disconnect(output);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
void ConstantFolder::make_constant(const float3 value) const
|
2016-07-16 13:16:54 +02:00
|
|
|
{
|
2022-06-16 19:39:13 +02:00
|
|
|
VLOG_DEBUG << "Folding " << node->name << "::" << output->name() << " to constant " << value
|
|
|
|
|
<< ".";
|
2016-08-01 18:53:20 +03:00
|
|
|
|
2024-12-26 19:41:25 +01:00
|
|
|
for (ShaderInput *sock : output->links) {
|
2016-07-16 13:16:54 +02:00
|
|
|
sock->set(value);
|
2021-06-28 23:15:32 +02:00
|
|
|
sock->constant_folded_in = true;
|
2016-07-16 13:16:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
graph->disconnect(output);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
void ConstantFolder::make_constant(const int value) const
|
2024-06-12 10:37:39 +02:00
|
|
|
{
|
|
|
|
|
VLOG_DEBUG << "Folding " << node->name << "::" << output->name() << " to constant (" << value
|
|
|
|
|
<< ").";
|
|
|
|
|
|
2024-12-26 19:41:25 +01:00
|
|
|
for (ShaderInput *sock : output->links) {
|
2024-06-12 10:37:39 +02:00
|
|
|
sock->set(value);
|
|
|
|
|
sock->constant_folded_in = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
graph->disconnect(output);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
void ConstantFolder::make_constant_clamp(const float value, bool clamp) const
|
2016-07-16 13:16:54 +02:00
|
|
|
{
|
2021-10-27 13:28:13 +02:00
|
|
|
make_constant(clamp ? saturatef(value) : value);
|
2016-07-16 13:16:54 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
void ConstantFolder::make_constant_clamp(float3 value, const bool clamp) const
|
2016-07-16 13:16:54 +02:00
|
|
|
{
|
2016-07-30 20:03:49 +02:00
|
|
|
if (clamp) {
|
2021-10-27 13:28:13 +02:00
|
|
|
value.x = saturatef(value.x);
|
|
|
|
|
value.y = saturatef(value.y);
|
|
|
|
|
value.z = saturatef(value.z);
|
2016-07-16 13:16:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
make_constant(value);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-30 20:03:49 +02:00
|
|
|
void ConstantFolder::make_zero() const
|
|
|
|
|
{
|
|
|
|
|
if (output->type() == SocketType::FLOAT) {
|
|
|
|
|
make_constant(0.0f);
|
|
|
|
|
}
|
|
|
|
|
else if (SocketType::is_float3(output->type())) {
|
2021-02-17 01:47:18 +01:00
|
|
|
make_constant(zero_float3());
|
2016-07-30 20:03:49 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-29 00:35:53 +03:00
|
|
|
void ConstantFolder::make_one() const
|
|
|
|
|
{
|
|
|
|
|
if (output->type() == SocketType::FLOAT) {
|
|
|
|
|
make_constant(1.0f);
|
|
|
|
|
}
|
|
|
|
|
else if (SocketType::is_float3(output->type())) {
|
2021-02-17 01:47:18 +01:00
|
|
|
make_constant(one_float3());
|
2016-09-29 00:35:53 +03:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-16 13:16:54 +02:00
|
|
|
void ConstantFolder::bypass(ShaderOutput *new_output) const
|
|
|
|
|
{
|
|
|
|
|
assert(new_output);
|
|
|
|
|
|
2022-06-16 19:39:13 +02:00
|
|
|
VLOG_DEBUG << "Folding " << node->name << "::" << output->name() << " to socket "
|
|
|
|
|
<< new_output->parent->name << "::" << new_output->name() << ".";
|
2016-08-01 18:53:20 +03:00
|
|
|
|
2016-07-16 13:16:54 +02:00
|
|
|
/* Remove all outgoing links from socket and connect them to new_output instead.
|
|
|
|
|
* The graph->relink method affects node inputs, so it's not safe to use in constant
|
|
|
|
|
* folding if the node has multiple outputs and will thus be folded multiple times. */
|
2024-12-29 17:32:00 +01:00
|
|
|
const vector<ShaderInput *> outputs = output->links;
|
2016-07-16 13:16:54 +02:00
|
|
|
|
|
|
|
|
graph->disconnect(output);
|
|
|
|
|
|
2024-12-26 19:41:25 +01:00
|
|
|
for (ShaderInput *sock : outputs) {
|
2016-07-16 13:16:54 +02:00
|
|
|
graph->connect(new_output, sock);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConstantFolder::discard() const
|
|
|
|
|
{
|
|
|
|
|
assert(output->type() == SocketType::CLOSURE);
|
2016-08-01 18:53:20 +03:00
|
|
|
|
2022-06-16 19:39:13 +02:00
|
|
|
VLOG_DEBUG << "Discarding closure " << node->name << ".";
|
2016-08-01 18:53:20 +03:00
|
|
|
|
2016-07-16 13:16:54 +02:00
|
|
|
graph->disconnect(output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConstantFolder::bypass_or_discard(ShaderInput *input) const
|
|
|
|
|
{
|
|
|
|
|
assert(input->type() == SocketType::CLOSURE);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 20:03:49 +02:00
|
|
|
if (input->link) {
|
2016-07-16 13:16:54 +02:00
|
|
|
bypass(input->link);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
discard();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-30 20:03:49 +02:00
|
|
|
bool ConstantFolder::try_bypass_or_make_constant(ShaderInput *input, bool clamp) const
|
2016-07-16 13:16:54 +02:00
|
|
|
{
|
2016-07-30 20:03:49 +02:00
|
|
|
if (input->type() != output->type()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-12-26 17:53:59 +01:00
|
|
|
if (!input->link) {
|
2016-07-30 20:03:49 +02:00
|
|
|
if (input->type() == SocketType::FLOAT) {
|
|
|
|
|
make_constant_clamp(node->get_float(input->socket_type), clamp);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-12-26 17:53:59 +01:00
|
|
|
if (SocketType::is_float3(input->type())) {
|
2016-07-30 20:03:49 +02:00
|
|
|
make_constant_clamp(node->get_float3(input->socket_type), clamp);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-07-16 13:16:54 +02:00
|
|
|
}
|
|
|
|
|
else if (!clamp) {
|
|
|
|
|
bypass(input->link);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-07-03 05:25:27 +02:00
|
|
|
else {
|
|
|
|
|
/* disconnect other inputs if we can't fully bypass due to clamp */
|
2024-12-26 19:41:25 +01:00
|
|
|
for (ShaderInput *other : node->inputs) {
|
2017-07-03 05:25:27 +02:00
|
|
|
if (other != input && other->link) {
|
|
|
|
|
graph->disconnect(other);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-07-03 12:12:27 +02:00
|
|
|
return false;
|
2016-07-16 13:16:54 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-30 20:03:49 +02:00
|
|
|
bool ConstantFolder::is_zero(ShaderInput *input) const
|
|
|
|
|
{
|
|
|
|
|
if (!input->link) {
|
|
|
|
|
if (input->type() == SocketType::FLOAT) {
|
|
|
|
|
return node->get_float(input->socket_type) == 0.0f;
|
|
|
|
|
}
|
2024-12-26 17:53:59 +01:00
|
|
|
if (SocketType::is_float3(input->type())) {
|
2021-02-17 01:47:18 +01:00
|
|
|
return node->get_float3(input->socket_type) == zero_float3();
|
2016-07-30 20:03:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 20:03:49 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConstantFolder::is_one(ShaderInput *input) const
|
|
|
|
|
{
|
|
|
|
|
if (!input->link) {
|
|
|
|
|
if (input->type() == SocketType::FLOAT) {
|
|
|
|
|
return node->get_float(input->socket_type) == 1.0f;
|
|
|
|
|
}
|
2024-12-26 17:53:59 +01:00
|
|
|
if (SocketType::is_float3(input->type())) {
|
2021-02-17 01:47:18 +01:00
|
|
|
return node->get_float3(input->socket_type) == one_float3();
|
2016-07-30 20:03:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 20:03:49 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Specific nodes */
|
|
|
|
|
|
|
|
|
|
void ConstantFolder::fold_mix(NodeMix type, bool clamp) const
|
|
|
|
|
{
|
|
|
|
|
ShaderInput *fac_in = node->input("Fac");
|
|
|
|
|
ShaderInput *color1_in = node->input("Color1");
|
|
|
|
|
ShaderInput *color2_in = node->input("Color2");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-12-29 17:32:00 +01:00
|
|
|
const float fac = saturatef(node->get_float(fac_in->socket_type));
|
|
|
|
|
const bool fac_is_zero = !fac_in->link && fac == 0.0f;
|
|
|
|
|
const bool fac_is_one = !fac_in->link && fac == 1.0f;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 20:03:49 +02:00
|
|
|
/* remove no-op node when factor is 0.0 */
|
|
|
|
|
if (fac_is_zero) {
|
|
|
|
|
/* note that some of the modes will clamp out of bounds values even without use_clamp */
|
|
|
|
|
if (!(type == NODE_MIX_LIGHT || type == NODE_MIX_DODGE || type == NODE_MIX_BURN)) {
|
|
|
|
|
if (try_bypass_or_make_constant(color1_in, clamp)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-08-30 11:05:46 +01:00
|
|
|
switch (type) {
|
|
|
|
|
case NODE_MIX_BLEND:
|
|
|
|
|
/* remove useless mix colors nodes */
|
|
|
|
|
if (color1_in->link && color2_in->link) {
|
|
|
|
|
if (color1_in->link == color2_in->link) {
|
|
|
|
|
try_bypass_or_make_constant(color1_in, clamp);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!color1_in->link && !color2_in->link) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const float3 color1 = node->get_float3(color1_in->socket_type);
|
|
|
|
|
const float3 color2 = node->get_float3(color2_in->socket_type);
|
2022-08-30 11:05:46 +01:00
|
|
|
if (color1 == color2) {
|
|
|
|
|
try_bypass_or_make_constant(color1_in, clamp);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* remove no-op mix color node when factor is 1.0 */
|
|
|
|
|
if (fac_is_one) {
|
|
|
|
|
try_bypass_or_make_constant(color2_in, clamp);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_MIX_ADD:
|
|
|
|
|
/* 0 + X (fac 1) == X */
|
|
|
|
|
if (is_zero(color1_in) && fac_is_one) {
|
|
|
|
|
try_bypass_or_make_constant(color2_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
/* X + 0 (fac ?) == X */
|
|
|
|
|
else if (is_zero(color2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(color1_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_MIX_SUB:
|
|
|
|
|
/* X - 0 (fac ?) == X */
|
|
|
|
|
if (is_zero(color2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(color1_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
/* X - X (fac 1) == 0 */
|
|
|
|
|
else if (color1_in->link && color1_in->link == color2_in->link && fac_is_one) {
|
|
|
|
|
make_zero();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_MIX_MUL:
|
|
|
|
|
/* X * 1 (fac ?) == X, 1 * X (fac 1) == X */
|
|
|
|
|
if (is_one(color1_in) && fac_is_one) {
|
|
|
|
|
try_bypass_or_make_constant(color2_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
else if (is_one(color2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(color1_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
/* 0 * ? (fac ?) == 0, ? * 0 (fac 1) == 0 */
|
|
|
|
|
else if (is_zero(color1_in)) {
|
|
|
|
|
make_zero();
|
|
|
|
|
}
|
|
|
|
|
else if (is_zero(color2_in) && fac_is_one) {
|
|
|
|
|
make_zero();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_MIX_DIV:
|
|
|
|
|
/* X / 1 (fac ?) == X */
|
|
|
|
|
if (is_one(color2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(color1_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
/* 0 / ? (fac ?) == 0 */
|
|
|
|
|
else if (is_zero(color1_in)) {
|
|
|
|
|
make_zero();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConstantFolder::fold_mix_color(NodeMix type, bool clamp_factor, bool clamp) const
|
|
|
|
|
{
|
|
|
|
|
ShaderInput *fac_in = node->input("Factor");
|
|
|
|
|
ShaderInput *color1_in = node->input("A");
|
|
|
|
|
ShaderInput *color2_in = node->input("B");
|
|
|
|
|
|
2024-12-29 17:32:00 +01:00
|
|
|
const float fac = clamp_factor ? saturatef(node->get_float(fac_in->socket_type)) :
|
|
|
|
|
node->get_float(fac_in->socket_type);
|
|
|
|
|
const bool fac_is_zero = !fac_in->link && fac == 0.0f;
|
|
|
|
|
const bool fac_is_one = !fac_in->link && fac == 1.0f;
|
2022-08-30 11:05:46 +01:00
|
|
|
|
|
|
|
|
/* remove no-op node when factor is 0.0 */
|
|
|
|
|
if (fac_is_zero) {
|
|
|
|
|
/* note that some of the modes will clamp out of bounds values even without use_clamp */
|
|
|
|
|
if (!(type == NODE_MIX_LIGHT || type == NODE_MIX_DODGE || type == NODE_MIX_BURN)) {
|
|
|
|
|
if (try_bypass_or_make_constant(color1_in, clamp)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-30 20:03:49 +02:00
|
|
|
switch (type) {
|
|
|
|
|
case NODE_MIX_BLEND:
|
|
|
|
|
/* remove useless mix colors nodes */
|
|
|
|
|
if (color1_in->link && color2_in->link) {
|
|
|
|
|
if (color1_in->link == color2_in->link) {
|
2025-05-20 12:30:20 +02:00
|
|
|
if (!try_bypass_or_make_constant(color1_in, clamp)) {
|
|
|
|
|
/* If can't bypass, set `fac` to 0 to only use `color1_in`. */
|
|
|
|
|
fac_in->set(0.0f);
|
|
|
|
|
}
|
2016-07-30 20:03:49 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!color1_in->link && !color2_in->link) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const float3 color1 = node->get_float3(color1_in->socket_type);
|
|
|
|
|
const float3 color2 = node->get_float3(color2_in->socket_type);
|
2016-07-30 20:03:49 +02:00
|
|
|
if (color1 == color2) {
|
|
|
|
|
try_bypass_or_make_constant(color1_in, clamp);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* remove no-op mix color node when factor is 1.0 */
|
|
|
|
|
if (fac_is_one) {
|
|
|
|
|
try_bypass_or_make_constant(color2_in, clamp);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_MIX_ADD:
|
|
|
|
|
/* 0 + X (fac 1) == X */
|
|
|
|
|
if (is_zero(color1_in) && fac_is_one) {
|
|
|
|
|
try_bypass_or_make_constant(color2_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
/* X + 0 (fac ?) == X */
|
|
|
|
|
else if (is_zero(color2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(color1_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_MIX_SUB:
|
|
|
|
|
/* X - 0 (fac ?) == X */
|
|
|
|
|
if (is_zero(color2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(color1_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
/* X - X (fac 1) == 0 */
|
|
|
|
|
else if (color1_in->link && color1_in->link == color2_in->link && fac_is_one) {
|
|
|
|
|
make_zero();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_MIX_MUL:
|
|
|
|
|
/* X * 1 (fac ?) == X, 1 * X (fac 1) == X */
|
|
|
|
|
if (is_one(color1_in) && fac_is_one) {
|
|
|
|
|
try_bypass_or_make_constant(color2_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
else if (is_one(color2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(color1_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
/* 0 * ? (fac ?) == 0, ? * 0 (fac 1) == 0 */
|
2016-07-31 14:56:49 +02:00
|
|
|
else if (is_zero(color1_in)) {
|
2016-07-30 20:03:49 +02:00
|
|
|
make_zero();
|
|
|
|
|
}
|
2016-07-31 14:56:49 +02:00
|
|
|
else if (is_zero(color2_in) && fac_is_one) {
|
2016-07-30 20:03:49 +02:00
|
|
|
make_zero();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_MIX_DIV:
|
|
|
|
|
/* X / 1 (fac ?) == X */
|
|
|
|
|
if (is_one(color2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(color1_in, clamp);
|
|
|
|
|
}
|
|
|
|
|
/* 0 / ? (fac ?) == 0 */
|
|
|
|
|
else if (is_zero(color1_in)) {
|
|
|
|
|
make_zero();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 17:28:59 +01:00
|
|
|
void ConstantFolder::fold_mix_float(bool clamp_factor, bool clamp) const
|
|
|
|
|
{
|
|
|
|
|
ShaderInput *fac_in = node->input("Factor");
|
|
|
|
|
ShaderInput *float1_in = node->input("A");
|
|
|
|
|
ShaderInput *float2_in = node->input("B");
|
|
|
|
|
|
2024-12-29 17:32:00 +01:00
|
|
|
const float fac = clamp_factor ? saturatef(node->get_float(fac_in->socket_type)) :
|
|
|
|
|
node->get_float(fac_in->socket_type);
|
|
|
|
|
const bool fac_is_zero = !fac_in->link && fac == 0.0f;
|
|
|
|
|
const bool fac_is_one = !fac_in->link && fac == 1.0f;
|
2022-12-12 17:28:59 +01:00
|
|
|
|
|
|
|
|
/* remove no-op node when factor is 0.0 */
|
|
|
|
|
if (fac_is_zero) {
|
|
|
|
|
if (try_bypass_or_make_constant(float1_in, clamp)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* remove useless mix floats nodes */
|
|
|
|
|
if (float1_in->link && float2_in->link) {
|
|
|
|
|
if (float1_in->link == float2_in->link) {
|
|
|
|
|
try_bypass_or_make_constant(float1_in, clamp);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!float1_in->link && !float2_in->link) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const float value1 = node->get_float(float1_in->socket_type);
|
|
|
|
|
const float value2 = node->get_float(float2_in->socket_type);
|
2022-12-12 17:28:59 +01:00
|
|
|
if (value1 == value2) {
|
|
|
|
|
try_bypass_or_make_constant(float1_in, clamp);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* remove no-op mix float node when factor is 1.0 */
|
|
|
|
|
if (fac_is_one) {
|
|
|
|
|
try_bypass_or_make_constant(float2_in, clamp);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-18 11:16:04 +02:00
|
|
|
void ConstantFolder::fold_math(NodeMathType type) const
|
2016-07-30 20:03:49 +02:00
|
|
|
{
|
|
|
|
|
ShaderInput *value1_in = node->input("Value1");
|
|
|
|
|
ShaderInput *value2_in = node->input("Value2");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 20:03:49 +02:00
|
|
|
switch (type) {
|
|
|
|
|
case NODE_MATH_ADD:
|
|
|
|
|
/* X + 0 == 0 + X == X */
|
|
|
|
|
if (is_zero(value1_in)) {
|
2019-08-18 11:16:04 +02:00
|
|
|
try_bypass_or_make_constant(value2_in);
|
2016-07-30 20:03:49 +02:00
|
|
|
}
|
|
|
|
|
else if (is_zero(value2_in)) {
|
2019-08-18 11:16:04 +02:00
|
|
|
try_bypass_or_make_constant(value1_in);
|
2016-07-30 20:03:49 +02:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_MATH_SUBTRACT:
|
|
|
|
|
/* X - 0 == X */
|
|
|
|
|
if (is_zero(value2_in)) {
|
2019-08-18 11:16:04 +02:00
|
|
|
try_bypass_or_make_constant(value1_in);
|
2016-07-30 20:03:49 +02:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_MATH_MULTIPLY:
|
|
|
|
|
/* X * 1 == 1 * X == X */
|
|
|
|
|
if (is_one(value1_in)) {
|
2019-08-18 11:16:04 +02:00
|
|
|
try_bypass_or_make_constant(value2_in);
|
2016-07-30 20:03:49 +02:00
|
|
|
}
|
|
|
|
|
else if (is_one(value2_in)) {
|
2019-08-18 11:16:04 +02:00
|
|
|
try_bypass_or_make_constant(value1_in);
|
2016-07-30 20:03:49 +02:00
|
|
|
}
|
|
|
|
|
/* X * 0 == 0 * X == 0 */
|
|
|
|
|
else if (is_zero(value1_in) || is_zero(value2_in)) {
|
|
|
|
|
make_zero();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_MATH_DIVIDE:
|
|
|
|
|
/* X / 1 == X */
|
|
|
|
|
if (is_one(value2_in)) {
|
2019-08-18 11:16:04 +02:00
|
|
|
try_bypass_or_make_constant(value1_in);
|
2016-07-30 20:03:49 +02:00
|
|
|
}
|
|
|
|
|
/* 0 / X == 0 */
|
|
|
|
|
else if (is_zero(value1_in)) {
|
|
|
|
|
make_zero();
|
|
|
|
|
}
|
|
|
|
|
break;
|
2016-09-29 00:35:53 +03:00
|
|
|
case NODE_MATH_POWER:
|
|
|
|
|
/* 1 ^ X == X ^ 0 == 1 */
|
|
|
|
|
if (is_one(value1_in) || is_zero(value2_in)) {
|
|
|
|
|
make_one();
|
|
|
|
|
}
|
|
|
|
|
/* X ^ 1 == X */
|
|
|
|
|
else if (is_one(value2_in)) {
|
2019-08-18 11:16:04 +02:00
|
|
|
try_bypass_or_make_constant(value1_in);
|
2016-09-29 00:35:53 +03:00
|
|
|
}
|
2016-07-30 20:03:49 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
void ConstantFolder::fold_vector_math(NodeVectorMathType type) const
|
2016-07-30 20:03:49 +02:00
|
|
|
{
|
|
|
|
|
ShaderInput *vector1_in = node->input("Vector1");
|
|
|
|
|
ShaderInput *vector2_in = node->input("Vector2");
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
ShaderInput *scale_in = node->input("Scale");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-30 20:03:49 +02:00
|
|
|
switch (type) {
|
|
|
|
|
case NODE_VECTOR_MATH_ADD:
|
|
|
|
|
/* X + 0 == 0 + X == X */
|
|
|
|
|
if (is_zero(vector1_in)) {
|
|
|
|
|
try_bypass_or_make_constant(vector2_in);
|
|
|
|
|
}
|
|
|
|
|
else if (is_zero(vector2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(vector1_in);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_VECTOR_MATH_SUBTRACT:
|
|
|
|
|
/* X - 0 == X */
|
|
|
|
|
if (is_zero(vector2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(vector1_in);
|
|
|
|
|
}
|
|
|
|
|
break;
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
case NODE_VECTOR_MATH_MULTIPLY:
|
|
|
|
|
/* X * 0 == 0 * X == 0 */
|
|
|
|
|
if (is_zero(vector1_in) || is_zero(vector2_in)) {
|
|
|
|
|
make_zero();
|
|
|
|
|
} /* X * 1 == 1 * X == X */
|
|
|
|
|
else if (is_one(vector1_in)) {
|
|
|
|
|
try_bypass_or_make_constant(vector2_in);
|
|
|
|
|
}
|
|
|
|
|
else if (is_one(vector2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(vector1_in);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_VECTOR_MATH_DIVIDE:
|
|
|
|
|
/* X / 0 == 0 / X == 0 */
|
|
|
|
|
if (is_zero(vector1_in) || is_zero(vector2_in)) {
|
|
|
|
|
make_zero();
|
|
|
|
|
} /* X / 1 == X */
|
|
|
|
|
else if (is_one(vector2_in)) {
|
|
|
|
|
try_bypass_or_make_constant(vector1_in);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2016-07-30 20:03:49 +02:00
|
|
|
case NODE_VECTOR_MATH_DOT_PRODUCT:
|
|
|
|
|
case NODE_VECTOR_MATH_CROSS_PRODUCT:
|
|
|
|
|
/* X * 0 == 0 * X == 0 */
|
|
|
|
|
if (is_zero(vector1_in) || is_zero(vector2_in)) {
|
|
|
|
|
make_zero();
|
|
|
|
|
}
|
|
|
|
|
break;
|
Shading: Add more operators to Vector Math node.
Add Multiply, Divide, Project, Reflect, Distance, Length, Scale, Snap,
Floor, Ceil, Modulo, Fraction, Absolute, Minimum, and Maximum operators
to the Vector Math node. The Value output has been removed from operators
whose output is a vector, and the other way around. All of those removals
has been handled properly in versioning code.
The patch doesn't include tests for the new operators. Tests will be added
in a later patch.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5523
2019-08-21 19:36:33 +02:00
|
|
|
case NODE_VECTOR_MATH_LENGTH:
|
|
|
|
|
case NODE_VECTOR_MATH_ABSOLUTE:
|
|
|
|
|
if (is_zero(vector1_in)) {
|
|
|
|
|
make_zero();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NODE_VECTOR_MATH_SCALE:
|
|
|
|
|
/* X * 0 == 0 * X == 0 */
|
|
|
|
|
if (is_zero(vector1_in) || is_zero(scale_in)) {
|
|
|
|
|
make_zero();
|
|
|
|
|
} /* X * 1 == X */
|
|
|
|
|
else if (is_one(scale_in)) {
|
|
|
|
|
try_bypass_or_make_constant(vector1_in);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2016-07-30 20:03:49 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 23:17:13 +02:00
|
|
|
void ConstantFolder::fold_mapping(NodeMappingType type) const
|
|
|
|
|
{
|
|
|
|
|
ShaderInput *vector_in = node->input("Vector");
|
|
|
|
|
ShaderInput *location_in = node->input("Location");
|
|
|
|
|
ShaderInput *rotation_in = node->input("Rotation");
|
|
|
|
|
ShaderInput *scale_in = node->input("Scale");
|
|
|
|
|
|
|
|
|
|
if (is_zero(scale_in)) {
|
|
|
|
|
make_zero();
|
|
|
|
|
}
|
2022-01-20 19:08:19 +01:00
|
|
|
else if (
|
|
|
|
|
/* Can't constant fold since we always need to normalize the output. */
|
|
|
|
|
(type != NODE_MAPPING_TYPE_NORMAL) &&
|
|
|
|
|
/* Check all use values are zero, note location is not used by vector and normal types. */
|
|
|
|
|
(is_zero(location_in) || type == NODE_MAPPING_TYPE_VECTOR ||
|
|
|
|
|
type == NODE_MAPPING_TYPE_NORMAL) &&
|
|
|
|
|
is_zero(rotation_in) && is_one(scale_in))
|
|
|
|
|
{
|
2019-09-04 23:17:13 +02:00
|
|
|
try_bypass_or_make_constant(vector_in);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-16 13:16:54 +02:00
|
|
|
CCL_NAMESPACE_END
|