forked from bartvdbraak/blender
Cycles / Shader graph: Fallback to Sharp closures for very small roughness.
We fallback to Sharp closures for Glossy, Glass and Refraction nodes now, in case the Roughness input is disconnected and 0 (< 1e-4f to be exact). This way we gain a few percentages of performance, in case the user did not manually set the closure type to "Sharp" in the UI. Sharp will probably be removed from the UI as a followup, not needed anymore with this internal optimization. Original idea by Lukas Stockner(Differential Revision: https://developer.blender.org/D1439), code implementation by myself.
This commit is contained in:
parent
836c69c92f
commit
0639ba8ea5
@ -354,7 +354,13 @@ void ShaderGraph::copy_nodes(set<ShaderNode*>& nodes, map<ShaderNode*, ShaderNod
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Graph simplification */
|
||||
/* ******************** */
|
||||
|
||||
/* Step 1: Remove unused nodes.
|
||||
* Remove nodes which are not needed in the graph, such as proxies,
|
||||
* mix nodes with a factor of 0 or 1, emission shaders without contribution...
|
||||
*/
|
||||
void ShaderGraph::remove_unneeded_nodes()
|
||||
{
|
||||
vector<bool> removed(num_node_ids, false);
|
||||
@ -550,6 +556,14 @@ void ShaderGraph::remove_unneeded_nodes()
|
||||
}
|
||||
}
|
||||
|
||||
/* Step 3: Simplification.*/
|
||||
void ShaderGraph::simplify_nodes()
|
||||
{
|
||||
foreach(ShaderNode *node, nodes) {
|
||||
node->optimize();
|
||||
}
|
||||
}
|
||||
|
||||
void ShaderGraph::break_cycles(ShaderNode *node, vector<bool>& visited, vector<bool>& on_stack)
|
||||
{
|
||||
visited[node->id] = true;
|
||||
@ -590,7 +604,7 @@ void ShaderGraph::clean()
|
||||
/* TODO(dingto): Implement */
|
||||
|
||||
/* 3: Simplification. */
|
||||
/* TODO(dingto): Implement */
|
||||
simplify_nodes();
|
||||
|
||||
/* 4: De-duplication. */
|
||||
/* TODO(dingto): Implement */
|
||||
|
@ -196,6 +196,7 @@ public:
|
||||
virtual void attributes(Shader *shader, AttributeRequestSet *attributes);
|
||||
virtual void compile(SVMCompiler& compiler) = 0;
|
||||
virtual void compile(OSLCompiler& compiler) = 0;
|
||||
virtual void optimize() {};
|
||||
|
||||
virtual bool has_surface_emission() { return false; }
|
||||
virtual bool has_surface_transparent() { return false; }
|
||||
@ -275,6 +276,7 @@ public:
|
||||
void relink(vector<ShaderInput*> inputs, vector<ShaderInput*> outputs, ShaderOutput *output);
|
||||
|
||||
void remove_unneeded_nodes();
|
||||
void simplify_nodes();
|
||||
void finalize(bool do_bump = false, bool do_osl = false);
|
||||
|
||||
int get_num_closures();
|
||||
|
@ -1878,6 +1878,18 @@ GlossyBsdfNode::GlossyBsdfNode()
|
||||
add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
|
||||
}
|
||||
|
||||
void GlossyBsdfNode::optimize()
|
||||
{
|
||||
/* Fallback to Sharp closure for Roughness close to 0.
|
||||
* Note: Keep the epsilon in sync with kernel!
|
||||
*/
|
||||
ShaderInput *roughness_input = get_input("Roughness");
|
||||
if(!roughness_input->link && roughness_input->value.x <= 1e-4f) {
|
||||
closure = CLOSURE_BSDF_REFLECTION_ID;
|
||||
distribution = ustring("Sharp");
|
||||
}
|
||||
}
|
||||
|
||||
void GlossyBsdfNode::compile(SVMCompiler& compiler)
|
||||
{
|
||||
closure = (ClosureType)distribution_enum[distribution];
|
||||
@ -1918,6 +1930,18 @@ GlassBsdfNode::GlassBsdfNode()
|
||||
add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
|
||||
}
|
||||
|
||||
void GlassBsdfNode::optimize()
|
||||
{
|
||||
/* Fallback to Sharp closure for Roughness close to 0.
|
||||
* Note: Keep the epsilon in sync with kernel!
|
||||
*/
|
||||
ShaderInput *roughness_input = get_input("Roughness");
|
||||
if(!roughness_input->link && roughness_input->value.x <= 1e-4f) {
|
||||
closure = CLOSURE_BSDF_SHARP_GLASS_ID;
|
||||
distribution = ustring("Sharp");
|
||||
}
|
||||
}
|
||||
|
||||
void GlassBsdfNode::compile(SVMCompiler& compiler)
|
||||
{
|
||||
closure = (ClosureType)distribution_enum[distribution];
|
||||
@ -1958,6 +1982,18 @@ RefractionBsdfNode::RefractionBsdfNode()
|
||||
add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
|
||||
}
|
||||
|
||||
void RefractionBsdfNode::optimize()
|
||||
{
|
||||
/* Fallback to Sharp closure for Roughness close to 0.
|
||||
* Note: Keep the epsilon in sync with kernel!
|
||||
*/
|
||||
ShaderInput *roughness_input = get_input("Roughness");
|
||||
if(!roughness_input->link && roughness_input->value.x <= 1e-4f) {
|
||||
closure = CLOSURE_BSDF_REFRACTION_ID;
|
||||
distribution = ustring("Sharp");
|
||||
}
|
||||
}
|
||||
|
||||
void RefractionBsdfNode::compile(SVMCompiler& compiler)
|
||||
{
|
||||
closure = (ClosureType)distribution_enum[distribution];
|
||||
|
@ -310,6 +310,8 @@ class GlossyBsdfNode : public BsdfNode {
|
||||
public:
|
||||
SHADER_NODE_CLASS(GlossyBsdfNode)
|
||||
|
||||
void optimize();
|
||||
|
||||
ustring distribution;
|
||||
static ShaderEnum distribution_enum;
|
||||
};
|
||||
@ -318,6 +320,8 @@ class GlassBsdfNode : public BsdfNode {
|
||||
public:
|
||||
SHADER_NODE_CLASS(GlassBsdfNode)
|
||||
|
||||
void optimize();
|
||||
|
||||
ustring distribution;
|
||||
static ShaderEnum distribution_enum;
|
||||
};
|
||||
@ -326,6 +330,8 @@ class RefractionBsdfNode : public BsdfNode {
|
||||
public:
|
||||
SHADER_NODE_CLASS(RefractionBsdfNode)
|
||||
|
||||
void optimize();
|
||||
|
||||
ustring distribution;
|
||||
static ShaderEnum distribution_enum;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user