Cycles: Fix filter glossy being broken after recent changes

Basically we can not use sharp closure as a substitude when filter glossy is
used. This is because we can not blur sharp reflection/refraction.

This is quite quick and not really clean implementation. Not really happy
with manual handling of original settings, but this is as good as we can do
in the quick patch. It's a good acknowledgment and we now can re-consider
some aspects of graph simplification to make such cases more natively
supported.

P.S. This failure would have been shown by our regression tests, so please,
bother a bit to run Cycles's test sweep before doing such optimizations.
This commit is contained in:
Sergey Sharybin
2015-11-20 18:18:27 +05:00
parent c4188c2bba
commit 7e71be261b
11 changed files with 160 additions and 53 deletions

View File

@@ -266,7 +266,10 @@ void ShaderGraph::relink(vector<ShaderInput*> inputs, vector<ShaderInput*> outpu
}
}
void ShaderGraph::finalize(bool do_bump, bool do_osl)
void ShaderGraph::finalize(Scene *scene,
bool do_bump,
bool do_osl,
bool do_simplify)
{
/* before compiling, the shader graph may undergo a number of modifications.
* currently we set default geometry shader inputs, and create automatic bump
@@ -274,7 +277,7 @@ void ShaderGraph::finalize(bool do_bump, bool do_osl)
* modified afterwards. */
if(!finalized) {
clean();
clean(scene);
default_inputs(do_osl);
refine_bump_nodes();
@@ -293,6 +296,9 @@ void ShaderGraph::finalize(bool do_bump, bool do_osl)
finalized = true;
}
else if(do_simplify) {
simplify_nodes(scene);
}
}
void ShaderGraph::find_dependencies(set<ShaderNode*>& dependencies, ShaderInput *input)
@@ -557,10 +563,10 @@ void ShaderGraph::remove_unneeded_nodes()
}
/* Step 3: Simplification.*/
void ShaderGraph::simplify_nodes()
void ShaderGraph::simplify_nodes(Scene *scene)
{
foreach(ShaderNode *node, nodes) {
node->optimize();
node->optimize(scene);
}
}
@@ -588,7 +594,7 @@ void ShaderGraph::break_cycles(ShaderNode *node, vector<bool>& visited, vector<b
on_stack[node->id] = false;
}
void ShaderGraph::clean()
void ShaderGraph::clean(Scene *scene)
{
/* Graph simplification:
* 1: Remove unnecesarry nodes
@@ -604,7 +610,7 @@ void ShaderGraph::clean()
/* TODO(dingto): Implement */
/* 3: Simplification. */
simplify_nodes();
simplify_nodes(scene);
/* 4: De-duplication. */
/* TODO(dingto): Implement */

View File

@@ -29,6 +29,7 @@
CCL_NAMESPACE_BEGIN
class AttributeRequestSet;
class Scene;
class Shader;
class ShaderInput;
class ShaderOutput;
@@ -196,7 +197,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 void optimize(Scene * /*scene*/) {};
virtual bool has_surface_emission() { return false; }
virtual bool has_surface_transparent() { return false; }
@@ -204,6 +205,7 @@ public:
virtual bool has_bssrdf_bump() { return false; }
virtual bool has_spatial_varying() { return false; }
virtual bool has_object_dependency() { return false; }
virtual bool has_integrator_dependency() { return false; }
vector<ShaderInput*> inputs;
vector<ShaderOutput*> outputs;
@@ -276,8 +278,10 @@ 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);
void finalize(Scene *scene,
bool do_bump = false,
bool do_osl = false,
bool do_simplify = false);
int get_num_closures();
@@ -290,7 +294,8 @@ protected:
void copy_nodes(set<ShaderNode*>& nodes, map<ShaderNode*, ShaderNode*>& nnodemap);
void break_cycles(ShaderNode *node, vector<bool>& visited, vector<bool>& on_stack);
void clean();
void clean(Scene *scene);
void simplify_nodes(Scene *scene);
void bump_from_displacement();
void refine_bump_nodes();
void default_inputs(bool do_osl);

View File

@@ -18,6 +18,7 @@
#include "integrator.h"
#include "light.h"
#include "scene.h"
#include "shader.h"
#include "sobol.h"
#include "util_foreach.h"
@@ -217,8 +218,14 @@ bool Integrator::modified(const Integrator& integrator)
sample_all_lights_indirect == integrator.sample_all_lights_indirect);
}
void Integrator::tag_update(Scene * /*scene*/)
void Integrator::tag_update(Scene *scene)
{
foreach(Shader *shader, scene->shaders) {
if(shader->has_integrator_dependency) {
scene->shader_manager->need_update = true;
break;
}
}
need_update = true;
}

View File

@@ -15,7 +15,9 @@
*/
#include "image.h"
#include "integrator.h"
#include "nodes.h"
#include "scene.h"
#include "svm.h"
#include "svm_math_util.h"
#include "osl.h"
@@ -1874,20 +1876,37 @@ GlossyBsdfNode::GlossyBsdfNode()
{
closure = CLOSURE_BSDF_MICROFACET_GGX_ID;
distribution = ustring("GGX");
distribution_orig = ustring("");
add_input("Roughness", SHADER_SOCKET_FLOAT, 0.2f);
}
void GlossyBsdfNode::optimize()
void GlossyBsdfNode::optimize(Scene *scene)
{
/* 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");
if(distribution_orig == "") {
distribution_orig = distribution;
}
Integrator *integrator = scene->integrator;
if(integrator->filter_glossy == 0.0f) {
/* 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) {
distribution = ustring("Sharp");
}
}
else {
/* Rollback to original distribution when filter glossy is used. */
distribution = distribution_orig;
}
closure = (ClosureType)distribution_enum[distribution];
}
bool GlossyBsdfNode::has_integrator_dependency()
{
ShaderInput *roughness_input = get_input("Roughness");
return !roughness_input->link && roughness_input->value.x <= 1e-4f;
}
void GlossyBsdfNode::compile(SVMCompiler& compiler)
@@ -1925,21 +1944,38 @@ GlassBsdfNode::GlassBsdfNode()
{
closure = CLOSURE_BSDF_SHARP_GLASS_ID;
distribution = ustring("Sharp");
distribution_orig = ustring("");
add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
}
void GlassBsdfNode::optimize()
void GlassBsdfNode::optimize(Scene *scene)
{
/* 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");
if(distribution_orig == "") {
distribution_orig = distribution;
}
Integrator *integrator = scene->integrator;
if(integrator->filter_glossy == 0.0f) {
/* 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) {
distribution = ustring("Sharp");
}
}
else {
/* Rollback to original distribution when filter glossy is used. */
distribution = distribution_orig;
}
closure = (ClosureType)distribution_enum[distribution];
}
bool GlassBsdfNode::has_integrator_dependency()
{
ShaderInput *roughness_input = get_input("Roughness");
return !roughness_input->link && roughness_input->value.x <= 1e-4f;
}
void GlassBsdfNode::compile(SVMCompiler& compiler)
@@ -1977,21 +2013,38 @@ RefractionBsdfNode::RefractionBsdfNode()
{
closure = CLOSURE_BSDF_REFRACTION_ID;
distribution = ustring("Sharp");
distribution_orig = ustring("");
add_input("Roughness", SHADER_SOCKET_FLOAT, 0.0f);
add_input("IOR", SHADER_SOCKET_FLOAT, 0.3f);
}
void RefractionBsdfNode::optimize()
void RefractionBsdfNode::optimize(Scene *scene)
{
/* 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");
if(distribution_orig == "") {
distribution_orig = distribution;
}
Integrator *integrator = scene->integrator;
if(integrator->filter_glossy == 0.0f) {
/* 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) {
distribution = ustring("Sharp");
}
}
else {
/* Rollback to original distribution when filter glossy is used. */
distribution = distribution_orig;
}
closure = (ClosureType)distribution_enum[distribution];
}
bool RefractionBsdfNode::has_integrator_dependency()
{
ShaderInput *roughness_input = get_input("Roughness");
return !roughness_input->link && roughness_input->value.x <= 1e-4f;
}
void RefractionBsdfNode::compile(SVMCompiler& compiler)

View File

@@ -24,6 +24,7 @@
CCL_NAMESPACE_BEGIN
class ImageManager;
class Scene;
class Shader;
/* Texture Mapping */
@@ -310,9 +311,10 @@ class GlossyBsdfNode : public BsdfNode {
public:
SHADER_NODE_CLASS(GlossyBsdfNode)
void optimize();
void optimize(Scene *scene);
bool has_integrator_dependency();
ustring distribution;
ustring distribution, distribution_orig;
static ShaderEnum distribution_enum;
};
@@ -320,9 +322,10 @@ class GlassBsdfNode : public BsdfNode {
public:
SHADER_NODE_CLASS(GlassBsdfNode)
void optimize();
void optimize(Scene *scene);
bool has_integrator_dependency();
ustring distribution;
ustring distribution, distribution_orig;
static ShaderEnum distribution_enum;
};
@@ -330,9 +333,10 @@ class RefractionBsdfNode : public BsdfNode {
public:
SHADER_NODE_CLASS(RefractionBsdfNode)
void optimize();
void optimize(Scene *scene);
bool has_integrator_dependency();
ustring distribution;
ustring distribution, distribution_orig;
static ShaderEnum distribution_enum;
};

View File

@@ -100,7 +100,7 @@ void OSLShaderManager::device_update(Device *device, DeviceScene *dscene, Scene
OSLCompiler compiler((void*)this, (void*)ss, scene->image_manager);
compiler.background = (shader == scene->shaders[scene->default_background]);
compiler.compile(og, shader);
compiler.compile(scene, og, shader);
if(shader->use_mis && shader->has_surface_emission)
scene->light_manager->need_update = true;
@@ -573,6 +573,10 @@ void OSLCompiler::add(ShaderNode *node, const char *name, bool isfilepath)
if(node->has_object_dependency()) {
current_shader->has_object_dependency = true;
}
if(node->has_integrator_dependency()) {
current_shader->has_integrator_dependency = true;
}
}
void OSLCompiler::parameter(const char *name, float f)
@@ -788,7 +792,7 @@ OSL::ShadingAttribStateRef OSLCompiler::compile_type(Shader *shader, ShaderGraph
return group;
}
void OSLCompiler::compile(OSLGlobals *og, Shader *shader)
void OSLCompiler::compile(Scene *scene, OSLGlobals *og, Shader *shader)
{
if(shader->need_update) {
ShaderGraph *graph = shader->graph;
@@ -800,9 +804,16 @@ void OSLCompiler::compile(OSLGlobals *og, Shader *shader)
shader->graph_bump = shader->graph->copy();
/* finalize */
shader->graph->finalize(false, true);
if(shader->graph_bump)
shader->graph_bump->finalize(true, true);
shader->graph->finalize(scene,
false,
true,
shader->has_integrator_dependency);
if(shader->graph_bump) {
shader->graph_bump->finalize(scene,
true,
true,
shader->has_integrator_dependency);
}
current_shader = shader;
@@ -815,6 +826,7 @@ void OSLCompiler::compile(OSLGlobals *og, Shader *shader)
shader->has_displacement = false;
shader->has_heterogeneous_volume = false;
shader->has_object_dependency = false;
shader->has_integrator_dependency = false;
/* generate surface shader */
if(shader->used && graph && output->input("Surface")->link) {

View File

@@ -113,7 +113,7 @@ protected:
class OSLCompiler {
public:
OSLCompiler(void *manager, void *shadingsys, ImageManager *image_manager);
void compile(OSLGlobals *og, Shader *shader);
void compile(Scene *scene, OSLGlobals *og, Shader *shader);
void add(ShaderNode *node, const char *name, bool isfilepath = false);

View File

@@ -154,6 +154,7 @@ Shader::Shader()
has_bssrdf_bump = false;
has_heterogeneous_volume = false;
has_object_dependency = false;
has_integrator_dependency = false;
used = false;

View File

@@ -107,6 +107,7 @@ public:
bool has_bssrdf_bump;
bool has_heterogeneous_volume;
bool has_object_dependency;
bool has_integrator_dependency;
/* requested mesh attributes */
AttributeRequestSet attributes;

View File

@@ -78,7 +78,7 @@ void SVMShaderManager::device_update(Device *device, DeviceScene *dscene, Scene
SVMCompiler compiler(scene->shader_manager, scene->image_manager);
compiler.background = ((int)i == scene->default_background);
compiler.compile(shader, svm_nodes, i);
compiler.compile(scene, shader, svm_nodes, i);
}
dscene->svm_nodes.copy((uint4*)&svm_nodes[0], svm_nodes.size());
@@ -396,6 +396,10 @@ void SVMCompiler::generate_node(ShaderNode *node, set<ShaderNode*>& done)
if(node->has_object_dependency()) {
current_shader->has_object_dependency = true;
}
if(node->has_integrator_dependency()) {
current_shader->has_integrator_dependency = true;
}
}
void SVMCompiler::generate_svm_nodes(const set<ShaderNode*>& nodes, set<ShaderNode*>& done)
@@ -691,7 +695,10 @@ void SVMCompiler::compile_type(Shader *shader, ShaderGraph *graph, ShaderType ty
add_node(NODE_END, 0, 0, 0);
}
void SVMCompiler::compile(Shader *shader, vector<int4>& global_svm_nodes, int index)
void SVMCompiler::compile(Scene *scene,
Shader *shader,
vector<int4>& global_svm_nodes,
int index)
{
/* copy graph for shader with bump mapping */
ShaderNode *node = shader->graph->output();
@@ -701,9 +708,16 @@ void SVMCompiler::compile(Shader *shader, vector<int4>& global_svm_nodes, int in
shader->graph_bump = shader->graph->copy();
/* finalize */
shader->graph->finalize(false, false);
if(shader->graph_bump)
shader->graph_bump->finalize(true, false);
shader->graph->finalize(scene,
false,
false,
shader->has_integrator_dependency);
if(shader->graph_bump) {
shader->graph_bump->finalize(scene,
true,
false,
shader->has_integrator_dependency);
}
current_shader = shader;
@@ -716,6 +730,7 @@ void SVMCompiler::compile(Shader *shader, vector<int4>& global_svm_nodes, int in
shader->has_displacement = false;
shader->has_heterogeneous_volume = false;
shader->has_object_dependency = false;
shader->has_integrator_dependency = false;
/* generate surface shader */
compile_type(shader, shader->graph, SHADER_TYPE_SURFACE);

View File

@@ -53,7 +53,10 @@ public:
class SVMCompiler {
public:
SVMCompiler(ShaderManager *shader_manager, ImageManager *image_manager);
void compile(Shader *shader, vector<int4>& svm_nodes, int index);
void compile(Scene *scene,
Shader *shader,
vector<int4>& svm_nodes,
int index);
void stack_assign(ShaderOutput *output);
void stack_assign(ShaderInput *input);