MaterialX logging #7

Merged
Bogdan Nagirniak merged 3 commits from BogdanNagirniak/blender:matx-logging into matx-export-material 2023-08-31 20:11:18 +02:00
8 changed files with 62 additions and 34 deletions

View File

@ -1010,7 +1010,7 @@ static void rna_def_render_engine(BlenderRNA *brna)
prop = RNA_def_property(srna, "bl_use_materialx", PROP_BOOLEAN, PROP_NONE); prop = RNA_def_property(srna, "bl_use_materialx", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, nullptr, "type->flag", RE_USE_MATERIALX); RNA_def_property_boolean_sdna(prop, nullptr, "type->flag", RE_USE_MATERIALX);
RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL); RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
RNA_def_property_ui_text(prop, "Use Material", "Use MaterialX for exporting materials to Hydra"); RNA_def_property_ui_text(prop, "Use MaterialX", "Use MaterialX for exporting materials to Hydra");
RNA_define_verify_sdna(true); RNA_define_verify_sdna(true);
} }

View File

@ -24,7 +24,8 @@ set(INC
if(WITH_HYDRA) if(WITH_HYDRA)
list(APPEND INC list(APPEND INC
../../io/usd ../../io/usd
) ../../../../intern/clog
)
add_definitions(-DWITH_HYDRA) add_definitions(-DWITH_HYDRA)
endif() endif()
@ -144,8 +145,6 @@ set(LIB
) )
if(WITH_MATERIALX) if(WITH_MATERIALX)
list(APPEND LIB MaterialXCore)
list(APPEND LIB MaterialXFormat)
list(APPEND SRC list(APPEND SRC
materialx/material.cc materialx/material.cc
materialx/nodes/bsdf_principled.cc materialx/nodes/bsdf_principled.cc
@ -164,6 +163,10 @@ if(WITH_MATERIALX)
materialx/nodes/node_item.h materialx/nodes/node_item.h
materialx/nodes/node_parser.h materialx/nodes/node_parser.h
) )
list(APPEND LIB
MaterialXCore
MaterialXFormat
)
endif() endif()
if(WITH_FREESTYLE) if(WITH_FREESTYLE)

View File

@ -5,13 +5,18 @@
#include "material.h" #include "material.h"
#include "nodes/node_parser.h" #include "nodes/node_parser.h"
#include <MaterialXCore/Node.h>
#include <MaterialXFormat/XmlIo.h> #include <MaterialXFormat/XmlIo.h>
#include "DEG_depsgraph.h"
#include "DNA_material_types.h"
#include "NOD_shader.h" #include "NOD_shader.h"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
CLG_LOGREF_DECLARE_GLOBAL(LOG_MATERIALX_SHADER, "materialx.shader");
static void export_nodegraph(MaterialX::GraphElement *graph, static void export_nodegraph(MaterialX::GraphElement *graph,
Depsgraph *depsgraph, Depsgraph *depsgraph,
Material *material) Material *material)
@ -48,7 +53,7 @@ MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *mater
create_standard_surface(doc.get(), material); create_standard_surface(doc.get(), material);
} }
std::string str = MaterialX::writeToXmlString(doc); std::string str = MaterialX::writeToXmlString(doc);
printf("\nMaterial: %s\n%s\n", material->id.name, str.c_str()); CLOG_INFO(LOG_MATERIALX_SHADER, 1, "Material: %s\n%s", material->id.name, str.c_str());
return doc; return doc;
} }

View File

@ -6,11 +6,15 @@
#include <MaterialXCore/Document.h> #include <MaterialXCore/Document.h>
#include "DEG_depsgraph.h" #include "CLG_log.h"
#include "DNA_material_types.h"
struct Depsgraph;
struct Material;
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
extern struct CLG_LogRef *LOG_MATERIALX_SHADER;
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material); MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material);
} // namespace blender::nodes::materialx } // namespace blender::nodes::materialx

View File

@ -72,8 +72,12 @@ NodeItem BSDFPrincipledNodeParser::compute()
res.set_input("base", 1.0, "float"); res.set_input("base", 1.0, "float");
res.set_input("base_color", base_color.to_color3()); res.set_input("base_color", base_color.to_color3());
res.set_input("diffuse_roughness", roughness); res.set_input("diffuse_roughness", roughness);
if (normal) {
res.set_input("normal", normal); res.set_input("normal", normal);
}
if (tangent) {
res.set_input("tangent", tangent); res.set_input("tangent", tangent);
}
if (metallic != zero) { if (metallic != zero) {
res.set_input("metalness", metallic); res.set_input("metalness", metallic);
@ -84,9 +88,11 @@ NodeItem BSDFPrincipledNodeParser::compute()
res.set_input("specular_color", base_color.to_color3()); res.set_input("specular_color", base_color.to_color3());
res.set_input("specular_roughness", roughness); res.set_input("specular_roughness", roughness);
res.set_input("specular_IOR", ior); res.set_input("specular_IOR", ior);
if (anisotropic) {
res.set_input("specular_anisotropy", anisotropic); res.set_input("specular_anisotropy", anisotropic);
res.set_input("specular_rotation", anisotropic_rotation); res.set_input("specular_rotation", anisotropic_rotation);
} }
}
if (transmission != zero) { if (transmission != zero) {
res.set_input("transmission", transmission); res.set_input("transmission", transmission);
@ -98,8 +104,10 @@ NodeItem BSDFPrincipledNodeParser::compute()
res.set_input("subsurface", subsurface); res.set_input("subsurface", subsurface);
res.set_input("subsurface_color", subsurface_color); res.set_input("subsurface_color", subsurface_color);
res.set_input("subsurface_radius", subsurface_radius); res.set_input("subsurface_radius", subsurface_radius);
if (anisotropic) {
res.set_input("subsurface_anisotropy", anisotropic); res.set_input("subsurface_anisotropy", anisotropic);
} }
}
if (sheen != zero) { if (sheen != zero) {
res.set_input("sheen", sheen); res.set_input("sheen", sheen);
@ -112,8 +120,10 @@ NodeItem BSDFPrincipledNodeParser::compute()
res.set_input("coat_color", base_color.to_color3()); res.set_input("coat_color", base_color.to_color3());
res.set_input("coat_roughness", clearcoat_roughness); res.set_input("coat_roughness", clearcoat_roughness);
res.set_input("coat_IOR", ior); res.set_input("coat_IOR", ior);
if (anisotropic) {
res.set_input("coat_anisotropy", anisotropic); res.set_input("coat_anisotropy", anisotropic);
res.set_input("coat_rotation", anisotropic_rotation); res.set_input("coat_rotation", anisotropic_rotation);
}
res.set_input("coat_normal", clearcoat_normal); res.set_input("coat_normal", clearcoat_normal);
} }

View File

@ -3,16 +3,14 @@
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "node_parser.h" #include "node_parser.h"
#include "../material.h"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
NodeItem MathNodeParser::compute() NodeItem MathNodeParser::compute()
{ {
/* TODO: finish some math operations */ /* TODO: finish some math operations */
auto op = node->custom1; auto op = node->custom1;
printf("%d\n", int(op));
NodeItem res = empty(); NodeItem res = empty();
/* Single operand operations */ /* Single operand operations */
@ -123,13 +121,13 @@ NodeItem MathNodeParser::compute()
res = x.atan2(y); res = x.atan2(y);
break; break;
case NODE_MATH_SNAP: case NODE_MATH_SNAP:
// res = ; CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op);
break; break;
case NODE_MATH_PINGPONG: case NODE_MATH_PINGPONG:
// res = ; CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op);
break; break;
case NODE_MATH_FLOORED_MODULO: case NODE_MATH_FLOORED_MODULO:
// res = ; CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op);
break; break;
default: { default: {
@ -137,7 +135,7 @@ NodeItem MathNodeParser::compute()
NodeItem z = get_input_value(2); NodeItem z = get_input_value(2);
switch (op) { switch (op) {
case NODE_MATH_WRAP: case NODE_MATH_WRAP:
// res = ; CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op);
break; break;
case NODE_MATH_COMPARE: case NODE_MATH_COMPARE:
res = z.if_else("<", (x - y).abs(), value(1.0f), value(0.0f)); res = z.if_else("<", (x - y).abs(), value(1.0f), value(0.0f));
@ -146,10 +144,10 @@ NodeItem MathNodeParser::compute()
res = x * y + z; res = x * y + z;
break; break;
case NODE_MATH_SMOOTH_MIN: case NODE_MATH_SMOOTH_MIN:
// res = ; CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op);
break; break;
case NODE_MATH_SMOOTH_MAX: case NODE_MATH_SMOOTH_MAX:
// res = ; CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op);
break; break;
default: default:

View File

@ -3,6 +3,7 @@
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "node_item.h" #include "node_item.h"
#include "../material.h"
#include "BLI_assert.h" #include "BLI_assert.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
@ -26,6 +27,9 @@ void NodeItem::set_input(const std::string &name,
else if (item.node) { else if (item.node) {
set_input(name, item.node, output_name); set_input(name, item.node, output_name);
} }
else {
CLOG_WARN(LOG_MATERIALX_SHADER, "Empty item to input: %s", name.c_str());
}
} }
void NodeItem::set_input(const std::string &name, const MaterialX::ValuePtr value) void NodeItem::set_input(const std::string &name, const MaterialX::ValuePtr value)
@ -218,6 +222,9 @@ NodeItem NodeItem::dotproduct(const NodeItem &other) const
auto v = value->asA<MaterialX::Vector4>(); auto v = value->asA<MaterialX::Vector4>();
f = v[0] + v[1] + v[2] + v[3]; f = v[0] + v[1] + v[2] + v[3];
} }
else {
BLI_assert_unreachable();
}
d.value = MaterialX::Value::createValue(f); d.value = MaterialX::Value::createValue(f);
} }
return d; return d;
@ -369,27 +376,27 @@ NodeItem NodeItem::exp() const
NodeItem NodeItem::to_color3() const NodeItem NodeItem::to_color3() const
{ {
std::string t = type(); std::string mx_type = type();
NodeItem res = empty(); NodeItem res = empty();
if (value) { if (value) {
MaterialX::Color3 c; MaterialX::Color3 c;
if (t == "float") { if (mx_type == "float") {
float v = value->asA<float>(); float v = value->asA<float>();
c = {v, v, v}; c = {v, v, v};
} }
else if (t == "color3") { else if (mx_type == "color3") {
auto v = value->asA<MaterialX::Color3>(); auto v = value->asA<MaterialX::Color3>();
c = {v[0], v[1], v[2]}; c = {v[0], v[1], v[2]};
} }
else if (t == "color4") { else if (mx_type == "color4") {
auto v = value->asA<MaterialX::Color4>(); auto v = value->asA<MaterialX::Color4>();
c = {v[0], v[1], v[2]}; c = {v[0], v[1], v[2]};
} }
else if (t == "vector3") { else if (mx_type == "vector3") {
auto v = value->asA<MaterialX::Vector3>(); auto v = value->asA<MaterialX::Vector3>();
c = {v[0], v[1], v[2]}; c = {v[0], v[1], v[2]};
} }
else if (t == "vector4") { else if (mx_type == "vector4") {
auto v = value->asA<MaterialX::Vector4>(); auto v = value->asA<MaterialX::Vector4>();
c = {v[0], v[1], v[2]}; c = {v[0], v[1], v[2]};
} }
@ -399,7 +406,7 @@ NodeItem NodeItem::to_color3() const
res.value = MaterialX::Value::createValue<MaterialX::Color3>(c); res.value = MaterialX::Value::createValue<MaterialX::Color3>(c);
} }
else if (node) { else if (node) {
if (t != "color3") { if (mx_type != "color3") {
return res; return res;
} }
res.node = node; res.node = node;

View File

@ -3,8 +3,9 @@
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "node_parser.h" #include "node_parser.h"
#include "../material.h"
#include <BKE_node_runtime.hh> #include "BKE_node_runtime.hh"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
@ -82,7 +83,7 @@ NodeItem NodeParser::get_input_default(const bNodeSocket &socket)
MaterialX::Color4(v[0], v[1], v[2], v[3])); MaterialX::Color4(v[0], v[1], v[2], v[3]));
} break; } break;
default: { default: {
// TODO log warn CLOG_WARN(LOG_MATERIALX_SHADER, "Unsupported socket type: %d", socket.type);
} }
} }
return res; return res;
@ -136,7 +137,7 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket)
parser = std::make_unique<TexCheckerNodeParser>(graph, depsgraph, material, in_node); parser = std::make_unique<TexCheckerNodeParser>(graph, depsgraph, material, in_node);
break; break;
default: default:
// TODO: warning log CLOG_WARN(LOG_MATERIALX_SHADER, "Unsupported node: %s (%d)", in_node->name, in_node->type);
return res; return res;
} }