Cycles: Optimize math node without links to a single value node

Pretty straightforward implementation. Just needed to move some functions
around to make them available at shader compile time.
This commit is contained in:
Sergey Sharybin 2014-10-26 19:40:04 +05:00
parent db4d7004d8
commit 6a4a911fc3
5 changed files with 89 additions and 51 deletions

View File

@ -91,6 +91,7 @@ set(SRC_SVM_HEADERS
svm/svm_magic.h
svm/svm_mapping.h
svm/svm_math.h
svm/svm_math_util.h
svm/svm_mix.h
svm/svm_musgrave.h
svm/svm_noise.h

View File

@ -164,6 +164,7 @@ CCL_NAMESPACE_END
#include "svm_mapping.h"
#include "svm_normal.h"
#include "svm_wave.h"
#include "svm_math_util.h"
#include "svm_math.h"
#include "svm_mix.h"
#include "svm_ramp.h"

View File

@ -16,56 +16,6 @@
CCL_NAMESPACE_BEGIN
ccl_device float svm_math(NodeMath type, float Fac1, float Fac2)
{
float Fac;
if(type == NODE_MATH_ADD)
Fac = Fac1 + Fac2;
else if(type == NODE_MATH_SUBTRACT)
Fac = Fac1 - Fac2;
else if(type == NODE_MATH_MULTIPLY)
Fac = Fac1*Fac2;
else if(type == NODE_MATH_DIVIDE)
Fac = safe_divide(Fac1, Fac2);
else if(type == NODE_MATH_SINE)
Fac = sinf(Fac1);
else if(type == NODE_MATH_COSINE)
Fac = cosf(Fac1);
else if(type == NODE_MATH_TANGENT)
Fac = tanf(Fac1);
else if(type == NODE_MATH_ARCSINE)
Fac = safe_asinf(Fac1);
else if(type == NODE_MATH_ARCCOSINE)
Fac = safe_acosf(Fac1);
else if(type == NODE_MATH_ARCTANGENT)
Fac = atanf(Fac1);
else if(type == NODE_MATH_POWER)
Fac = safe_powf(Fac1, Fac2);
else if(type == NODE_MATH_LOGARITHM)
Fac = safe_logf(Fac1, Fac2);
else if(type == NODE_MATH_MINIMUM)
Fac = fminf(Fac1, Fac2);
else if(type == NODE_MATH_MAXIMUM)
Fac = fmaxf(Fac1, Fac2);
else if(type == NODE_MATH_ROUND)
Fac = floorf(Fac1 + 0.5f);
else if(type == NODE_MATH_LESS_THAN)
Fac = Fac1 < Fac2;
else if(type == NODE_MATH_GREATER_THAN)
Fac = Fac1 > Fac2;
else if(type == NODE_MATH_MODULO)
Fac = safe_modulo(Fac1, Fac2);
else if(type == NODE_MATH_ABSOLUTE)
Fac = fabsf(Fac1);
else if(type == NODE_MATH_CLAMP)
Fac = clamp(Fac1, 0.0f, 1.0f);
else
Fac = 0.0f;
return Fac;
}
ccl_device float average_fac(float3 v)
{
return (fabsf(v.x) + fabsf(v.y) + fabsf(v.z))/3.0f;

View File

@ -0,0 +1,70 @@
/*
* Copyright 2011-2014 Blender Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
CCL_NAMESPACE_BEGIN
ccl_device float svm_math(NodeMath type, float Fac1, float Fac2)
{
float Fac;
if(type == NODE_MATH_ADD)
Fac = Fac1 + Fac2;
else if(type == NODE_MATH_SUBTRACT)
Fac = Fac1 - Fac2;
else if(type == NODE_MATH_MULTIPLY)
Fac = Fac1*Fac2;
else if(type == NODE_MATH_DIVIDE)
Fac = safe_divide(Fac1, Fac2);
else if(type == NODE_MATH_SINE)
Fac = sinf(Fac1);
else if(type == NODE_MATH_COSINE)
Fac = cosf(Fac1);
else if(type == NODE_MATH_TANGENT)
Fac = tanf(Fac1);
else if(type == NODE_MATH_ARCSINE)
Fac = safe_asinf(Fac1);
else if(type == NODE_MATH_ARCCOSINE)
Fac = safe_acosf(Fac1);
else if(type == NODE_MATH_ARCTANGENT)
Fac = atanf(Fac1);
else if(type == NODE_MATH_POWER)
Fac = safe_powf(Fac1, Fac2);
else if(type == NODE_MATH_LOGARITHM)
Fac = safe_logf(Fac1, Fac2);
else if(type == NODE_MATH_MINIMUM)
Fac = fminf(Fac1, Fac2);
else if(type == NODE_MATH_MAXIMUM)
Fac = fmaxf(Fac1, Fac2);
else if(type == NODE_MATH_ROUND)
Fac = floorf(Fac1 + 0.5f);
else if(type == NODE_MATH_LESS_THAN)
Fac = Fac1 < Fac2;
else if(type == NODE_MATH_GREATER_THAN)
Fac = Fac1 > Fac2;
else if(type == NODE_MATH_MODULO)
Fac = safe_modulo(Fac1, Fac2);
else if(type == NODE_MATH_ABSOLUTE)
Fac = fabsf(Fac1);
else if(type == NODE_MATH_CLAMP)
Fac = clamp(Fac1, 0.0f, 1.0f);
else
Fac = 0.0f;
return Fac;
}
CCL_NAMESPACE_END

View File

@ -17,6 +17,7 @@
#include "image.h"
#include "nodes.h"
#include "svm.h"
#include "svm_math_util.h"
#include "osl.h"
#include "sky_model.h"
@ -3682,9 +3683,24 @@ void MathNode::compile(SVMCompiler& compiler)
ShaderInput *value2_in = input("Value2");
ShaderOutput *value_out = output("Value");
compiler.stack_assign(value_out);
/* Optimize math node without links to a single value node. */
if(value1_in->link == NULL && value2_in->link == NULL) {
float optimized_value = svm_math((NodeMath)type_enum[type],
value1_in->value.x,
value2_in->value.x);
if(use_clamp) {
optimized_value = clamp(optimized_value, 0.0f, 1.0f);
}
compiler.add_node(NODE_VALUE_F,
__float_as_int(optimized_value),
value_out->stack_offset);
return;
}
compiler.stack_assign(value1_in);
compiler.stack_assign(value2_in);
compiler.stack_assign(value_out);
compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
compiler.add_node(NODE_MATH, value_out->stack_offset);