Cycles: Add fundamentals to test constant folding

This commit adds some easy to use way to check whether the graph
was properly optimized. The idea is based on using mock glog sync
which keeps track on all messages and expects specific log message
to appear in the logging.

This means each optimization step should report that it optimized
something (specifying what exactly was optimized) and what was used
for optimization.

There's also some ease-ish way to define shader networks which we
might want to make a bit more global and available for all tests
in the future.

For now only RGBToBW node is covered as an example, really hope
to get help from active guys in the community to finish covering
all existing optimization cases. Ready to fix any possible issues
with builder when needed tho :)
This commit is contained in:
Sergey Sharybin 2016-07-29 18:44:29 +02:00
parent ac655be0e3
commit 37933cb048
3 changed files with 180 additions and 0 deletions

@ -18,6 +18,7 @@
#include "graph.h"
#include "util_foreach.h"
#include "util_logging.h"
CCL_NAMESPACE_BEGIN
@ -39,6 +40,7 @@ bool ConstantFolder::all_inputs_constant() const
void ConstantFolder::make_constant(float value) const
{
VLOG(1) << "Replacing " << node->name << " with constant " << value << ".";
foreach(ShaderInput *sock, output->links) {
sock->set(value);
}

@ -14,9 +14,37 @@ endmacro()
set(INC
.
..
../device
../graph
../kernel
../render
../util
)
set(ALL_CYCLES_LIBRARIES
cycles_render
cycles_device
cycles_bvh
cycles_graph
cycles_kernel_osl
cycles_util
${OPENIMAGEIO_LIBRARIES}
)
if(WITH_CYCLES_OSL)
list(APPEND ALL_CYCLES_LIBRARIES
${OSL_LIBRARIES}
${LLVM_LIBRARIES}
)
endif()
if(WITH_IMAGE_OPENJPEG AND NOT WITH_SYSTEM_OPENJPEG)
list(APPEND ALL_CYCLES_LIBRARIES
extern_openjpeg
)
endif()
list(APPEND ALL_CYCLES_LIBRARIES
${BOOST_LIBRARIES}
)
include_directories(${INC})
link_directories(${BOOST_LIBPATH})
@ -25,6 +53,7 @@ link_directories(${OPENIMAGEIO_LIBPATH})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}")
CYCLES_TEST(render_graph_finalize "${ALL_CYCLES_LIBRARIES}")
CYCLES_TEST(util_aligned_malloc "cycles_util")
CYCLES_TEST(util_path "cycles_util;${BOOST_LIBRARIES};${OPENIMAGEIO_LIBRARIES}")
CYCLES_TEST(util_string "cycles_util;${BOOST_LIBRARIES}")

@ -0,0 +1,149 @@
/*
* Copyright 2011-2016 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.
*/
#include "testing/testing.h"
#include "testing/mock_log.h"
#include "render/graph.h"
#include "render/scene.h"
#include "render/nodes.h"
#include "util/util_logging.h"
#include "util/util_string.h"
#include "util/util_vector.h"
using testing::AnyNumber;
using testing::HasSubstr;
using testing::ScopedMockLog;
using testing::_;
CCL_NAMESPACE_BEGIN
namespace {
template<typename T>
class ShaderNodeBuilder {
public:
ShaderNodeBuilder(const string& name)
: name_(name)
{
node_ = new T();
}
const string& name() const {
return name_;
}
ShaderNode *node() const {
return node_;
}
template<typename V>
ShaderNodeBuilder& set(const string& input_name, V value)
{
ShaderInput *input_socket = node_->input(input_name.c_str());
EXPECT_NE((void*)NULL, input_socket);
input_socket->set(value);
return *this;
}
protected:
string name_;
ShaderNode *node_;
};
class ShaderGraphBuilder {
public:
explicit ShaderGraphBuilder(ShaderGraph *graph)
: graph_(graph)
{
}
ShaderNode *find_node(const string& name)
{
map<string, ShaderNode *>::iterator it = node_map_.find(name);
if(it == node_map_.end()) {
return NULL;
}
return it->second;
}
template<typename T>
ShaderGraphBuilder& add_node(const T& node)
{
EXPECT_EQ(NULL, find_node(node.name()));
graph_->add(node.node());
node_map_[node.name()] = node.node();
return *this;
}
ShaderGraphBuilder& add_connection(const string& from,
const string& to)
{
vector<string> tokens_from, tokens_to;
string_split(tokens_from, from, "::");
string_split(tokens_to, to, "::");
EXPECT_EQ(2, tokens_from.size());
EXPECT_EQ(2, tokens_to.size());
ShaderNode *node_from = find_node(tokens_from[0]),
*node_to = find_node(tokens_to[0]);
EXPECT_NE((void*)NULL, node_from);
EXPECT_NE((void*)NULL, node_to);
EXPECT_NE(node_from, node_to);
ShaderOutput *socket_from = node_from->output(tokens_from[1].c_str());
ShaderInput *socket_to = node_to->input(tokens_to[1].c_str());
EXPECT_NE((void*)NULL, socket_from);
EXPECT_NE((void*)NULL, socket_to);
graph_->connect(socket_from, socket_to);
return *this;
}
protected:
ShaderGraph *graph_;
map<string, ShaderNode *> node_map_;
};
} // namespace
#define DEFINE_COMMON_VARIABLES(builder_name, mock_log_name) \
util_logging_start(); \
util_logging_verbosity_set(1); \
ScopedMockLog mock_log_name; \
DeviceInfo device_info; \
SceneParams scene_params; \
Scene scene(scene_params, device_info); \
ShaderGraph graph; \
ShaderGraphBuilder builder(&graph); \
TEST(render_graph, constant_fold_rgb_to_bw)
{
DEFINE_COMMON_VARIABLES(builder, log);
EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
EXPECT_CALL(log, Log(google::INFO, _,
HasSubstr("Replacing rgb_to_bw with constant 0.8.")));
builder
.add_node(ShaderNodeBuilder<OutputNode>("OutputNode"))
.add_node(ShaderNodeBuilder<EmissionNode>("EmissionNode"))
.add_node(ShaderNodeBuilder<RGBToBWNode>("RGBToBWNodeNode")
.set("Color", make_float3(0.8f, 0.8f, 0.8f)))
.add_connection("RGBToBWNodeNode::Val", "EmissionNode::Color")
.add_connection("EmissionNode::Emission", "OutputNode::Surface");
graph.finalize(&scene);
}
CCL_NAMESPACE_END