diff options
author | Alexander Gavrilov <angavrilov@gmail.com> | 2020-08-05 19:14:40 +0300 |
---|---|---|
committer | Alexander Gavrilov <angavrilov@gmail.com> | 2020-11-03 16:35:44 +0300 |
commit | 6fdcca8de64cd70f237640b67ce2d0068b918d05 (patch) | |
tree | a894a557505bcd127d300edfb412ab57060ef2d2 /intern/cycles/blender/blender_shader.cpp | |
parent | 91d320edc3cfb30443af4adbcb09bc3d7a609e1d (diff) |
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
Diffstat (limited to 'intern/cycles/blender/blender_shader.cpp')
-rw-r--r-- | intern/cycles/blender/blender_shader.cpp | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp index c171982b29d..0c1b240c593 100644 --- a/intern/cycles/blender/blender_shader.cpp +++ b/intern/cycles/blender/blender_shader.cpp @@ -97,6 +97,53 @@ static ImageAlphaType get_image_alpha_type(BL::Image &b_image) return (ImageAlphaType)validate_enum_value(value, IMAGE_ALPHA_NUM_TYPES, IMAGE_ALPHA_AUTO); } +/* Attribute name translation utilities */ + +/* Since Eevee needs to know whether the attribute is uniform or varying + * at the time it compiles the shader for the material, Blender had to + * introduce different namespaces (types) in its attribute node. However, + * Cycles already has object attributes that form a uniform namespace with + * the more common varying attributes. Without completely reworking the + * attribute handling in Cycles to introduce separate namespaces (this could + * be especially hard for OSL which directly uses the name string), the + * space identifier has to be added to the attribute name as a prefix. + * + * The prefixes include a control character to ensure the user specified + * name can't accidentally include a special prefix. + */ + +static const string_view object_attr_prefix("\x01object:"); +static const string_view instancer_attr_prefix("\x01instancer:"); + +static ustring blender_attribute_name_add_type(const string &name, BlenderAttributeType type) +{ + switch (type) { + case BL::ShaderNodeAttribute::attribute_type_OBJECT: + return ustring::concat(object_attr_prefix, name); + case BL::ShaderNodeAttribute::attribute_type_INSTANCER: + return ustring::concat(instancer_attr_prefix, name); + default: + return ustring(name); + } +} + +BlenderAttributeType blender_attribute_name_split_type(ustring name, string *r_real_name) +{ + string_view sname(name); + + if (sname.substr(0, object_attr_prefix.size()) == object_attr_prefix) { + *r_real_name = sname.substr(object_attr_prefix.size()); + return BL::ShaderNodeAttribute::attribute_type_OBJECT; + } + + if (sname.substr(0, instancer_attr_prefix.size()) == instancer_attr_prefix) { + *r_real_name = sname.substr(instancer_attr_prefix.size()); + return BL::ShaderNodeAttribute::attribute_type_INSTANCER; + } + + return BL::ShaderNodeAttribute::attribute_type_GEOMETRY; +} + /* Graph */ static BL::NodeSocket get_node_output(BL::Node &b_node, const string &name) @@ -369,7 +416,8 @@ static ShaderNode *add_node(Scene *scene, else if (b_node.is_a(&RNA_ShaderNodeAttribute)) { BL::ShaderNodeAttribute b_attr_node(b_node); AttributeNode *attr = graph->create_node<AttributeNode>(); - attr->attribute = b_attr_node.attribute_name(); + attr->attribute = blender_attribute_name_add_type(b_attr_node.attribute_name(), + b_attr_node.attribute_type()); node = attr; } else if (b_node.is_a(&RNA_ShaderNodeBackground)) { |