2011-10-23 17:52:20 +00:00
|
|
|
/*
|
2011-09-05 21:01:50 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2007 Blender Foundation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file blender/nodes/shader/node_shader_tree.c
|
|
|
|
* \ingroup nodes
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-11-02 18:55:32 +00:00
|
|
|
#include "DNA_lamp_types.h"
|
2011-09-05 21:01:50 +00:00
|
|
|
#include "DNA_material_types.h"
|
|
|
|
#include "DNA_node_types.h"
|
2011-11-07 22:14:48 +00:00
|
|
|
#include "DNA_scene_types.h"
|
2013-03-18 16:34:57 +00:00
|
|
|
#include "DNA_space_types.h"
|
2011-11-02 18:55:32 +00:00
|
|
|
#include "DNA_world_types.h"
|
2014-07-11 16:51:04 +09:00
|
|
|
#include "DNA_linestyle_types.h"
|
2017-10-16 17:15:03 -02:00
|
|
|
#include "DNA_workspace_types.h"
|
2011-09-05 21:01:50 +00:00
|
|
|
|
|
|
|
#include "BLI_listbase.h"
|
|
|
|
#include "BLI_threads.h"
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
#include "BLT_translation.h"
|
2011-11-09 15:00:11 +00:00
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
#include "BKE_context.h"
|
2014-07-16 13:53:00 +09:00
|
|
|
#include "BKE_linestyle.h"
|
2011-09-05 21:01:50 +00:00
|
|
|
#include "BKE_node.h"
|
2011-11-07 22:14:48 +00:00
|
|
|
#include "BKE_scene.h"
|
2011-09-05 21:01:50 +00:00
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
#include "RNA_access.h"
|
|
|
|
|
2011-09-05 21:01:50 +00:00
|
|
|
#include "GPU_material.h"
|
|
|
|
|
|
|
|
#include "RE_shader_ext.h"
|
|
|
|
|
2016-06-17 12:14:36 +02:00
|
|
|
#include "NOD_common.h"
|
|
|
|
|
2012-08-06 18:49:28 +00:00
|
|
|
#include "node_common.h"
|
2011-09-05 21:01:50 +00:00
|
|
|
#include "node_exec.h"
|
|
|
|
#include "node_util.h"
|
|
|
|
#include "node_shader_util.h"
|
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
static int shader_tree_poll(const bContext *C, bNodeTreeType *UNUSED(treetype))
|
2011-09-05 21:01:50 +00:00
|
|
|
{
|
2013-03-18 16:34:57 +00:00
|
|
|
Scene *scene = CTX_data_scene(C);
|
2017-10-16 17:15:03 -02:00
|
|
|
WorkSpace *workspace = CTX_wm_workspace(C);
|
|
|
|
ViewRender *view_render = BKE_viewrender_get(scene, workspace);
|
|
|
|
const char *engine_id = view_render->engine_id;
|
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
/* allow empty engine string too, this is from older versions that didn't have registerable engines yet */
|
2017-10-16 17:15:03 -02:00
|
|
|
return (engine_id[0] == '\0' ||
|
|
|
|
STREQ(engine_id, RE_engine_id_BLENDER_RENDER) ||
|
|
|
|
STREQ(engine_id, RE_engine_id_BLENDER_GAME) ||
|
|
|
|
STREQ(engine_id, RE_engine_id_CYCLES) ||
|
|
|
|
!BKE_viewrender_use_shading_nodes_custom(view_render));
|
2013-03-18 16:34:57 +00:00
|
|
|
}
|
2011-11-02 18:55:32 +00:00
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
static void shader_get_from_context(const bContext *C, bNodeTreeType *UNUSED(treetype), bNodeTree **r_ntree, ID **r_id, ID **r_from)
|
|
|
|
{
|
|
|
|
SpaceNode *snode = CTX_wm_space_node(C);
|
|
|
|
Scene *scene = CTX_data_scene(C);
|
2017-10-16 17:15:03 -02:00
|
|
|
WorkSpace *workspace = CTX_wm_workspace(C);
|
2017-03-02 15:03:02 +01:00
|
|
|
SceneLayer *sl = CTX_data_scene_layer(C);
|
2017-08-15 15:59:38 +02:00
|
|
|
Object *ob = OBACT_NEW(sl);
|
2017-10-16 17:15:03 -02:00
|
|
|
ViewRender *view_render = BKE_viewrender_get(scene, workspace);
|
2013-03-18 16:34:57 +00:00
|
|
|
|
2014-05-29 02:32:25 +10:00
|
|
|
if ((snode->shaderfrom == SNODE_SHADER_OBJECT) ||
|
2017-10-16 17:15:03 -02:00
|
|
|
(BKE_viewrender_use_new_shading_nodes(view_render) == false))
|
2014-05-29 02:32:25 +10:00
|
|
|
{
|
2013-03-18 16:34:57 +00:00
|
|
|
if (ob) {
|
2013-03-19 10:42:33 +00:00
|
|
|
*r_from = &ob->id;
|
2013-03-18 16:34:57 +00:00
|
|
|
if (ob->type == OB_LAMP) {
|
|
|
|
*r_id = ob->data;
|
|
|
|
*r_ntree = ((Lamp *)ob->data)->nodetree;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Material *ma = give_current_material(ob, ob->actcol);
|
|
|
|
if (ma) {
|
|
|
|
*r_id = &ma->id;
|
|
|
|
*r_ntree = ma->nodetree;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-11 16:51:04 +09:00
|
|
|
#ifdef WITH_FREESTYLE
|
|
|
|
else if (snode->shaderfrom == SNODE_SHADER_LINESTYLE) {
|
2014-07-17 13:04:01 +09:00
|
|
|
FreestyleLineStyle *linestyle = BKE_linestyle_active_from_scene(scene);
|
2014-07-11 16:51:04 +09:00
|
|
|
if (linestyle) {
|
|
|
|
*r_from = NULL;
|
|
|
|
*r_id = &linestyle->id;
|
|
|
|
*r_ntree = linestyle->nodetree;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-03-18 16:34:57 +00:00
|
|
|
else { /* SNODE_SHADER_WORLD */
|
|
|
|
if (scene->world) {
|
|
|
|
*r_from = NULL;
|
|
|
|
*r_id = &scene->world->id;
|
|
|
|
*r_ntree = scene->world->nodetree;
|
|
|
|
}
|
|
|
|
}
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 17:15:03 -02:00
|
|
|
static void foreach_nodeclass(ViewRender *view_render, void *calldata, bNodeClassCallback func)
|
2011-11-07 22:14:48 +00:00
|
|
|
{
|
2012-05-16 15:01:46 +00:00
|
|
|
func(calldata, NODE_CLASS_INPUT, N_("Input"));
|
|
|
|
func(calldata, NODE_CLASS_OUTPUT, N_("Output"));
|
2011-11-07 22:14:48 +00:00
|
|
|
|
2017-10-16 17:15:03 -02:00
|
|
|
if (BKE_viewrender_use_new_shading_nodes(view_render)) {
|
2012-05-16 15:01:46 +00:00
|
|
|
func(calldata, NODE_CLASS_SHADER, N_("Shader"));
|
|
|
|
func(calldata, NODE_CLASS_TEXTURE, N_("Texture"));
|
2011-11-07 22:14:48 +00:00
|
|
|
}
|
2012-11-03 14:32:26 +00:00
|
|
|
|
2012-05-16 15:01:46 +00:00
|
|
|
func(calldata, NODE_CLASS_OP_COLOR, N_("Color"));
|
|
|
|
func(calldata, NODE_CLASS_OP_VECTOR, N_("Vector"));
|
|
|
|
func(calldata, NODE_CLASS_CONVERTOR, N_("Convertor"));
|
2012-11-03 14:32:26 +00:00
|
|
|
func(calldata, NODE_CLASS_SCRIPT, N_("Script"));
|
2012-05-16 15:01:46 +00:00
|
|
|
func(calldata, NODE_CLASS_GROUP, N_("Group"));
|
2013-03-18 16:34:57 +00:00
|
|
|
func(calldata, NODE_CLASS_INTERFACE, N_("Interface"));
|
2012-05-16 15:01:46 +00:00
|
|
|
func(calldata, NODE_CLASS_LAYOUT, N_("Layout"));
|
2011-11-07 22:14:48 +00:00
|
|
|
}
|
|
|
|
|
2012-02-27 17:38:16 +00:00
|
|
|
static void localize(bNodeTree *localtree, bNodeTree *UNUSED(ntree))
|
|
|
|
{
|
|
|
|
bNode *node, *node_next;
|
|
|
|
|
2012-06-01 12:38:03 +00:00
|
|
|
/* replace muted nodes and reroute nodes by internal links */
|
2012-09-03 02:41:12 +00:00
|
|
|
for (node = localtree->nodes.first; node; node = node_next) {
|
2012-02-27 17:38:16 +00:00
|
|
|
node_next = node->next;
|
|
|
|
|
2012-06-01 12:38:03 +00:00
|
|
|
if (node->flag & NODE_MUTED || node->type == NODE_REROUTE) {
|
2012-02-27 17:38:16 +00:00
|
|
|
nodeInternalRelink(localtree, node);
|
|
|
|
nodeFreeNode(localtree, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 10:34:52 +00:00
|
|
|
static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
|
2011-09-05 21:01:50 +00:00
|
|
|
{
|
2013-03-22 10:34:52 +00:00
|
|
|
BKE_node_preview_sync_tree(ntree, localtree);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void local_merge(bNodeTree *localtree, bNodeTree *ntree)
|
|
|
|
{
|
|
|
|
BKE_node_preview_merge_tree(ntree, localtree, true);
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
|
2011-10-19 17:08:35 +00:00
|
|
|
static void update(bNodeTree *ntree)
|
|
|
|
{
|
|
|
|
ntreeSetOutput(ntree);
|
2012-08-06 18:49:28 +00:00
|
|
|
|
|
|
|
ntree_update_reroute_nodes(ntree);
|
2013-03-18 16:34:57 +00:00
|
|
|
|
|
|
|
if (ntree->update & NTREE_UPDATE_NODES) {
|
|
|
|
/* clean up preview cache, in case nodes have been removed */
|
|
|
|
BKE_node_preview_remove_unused(ntree);
|
|
|
|
}
|
2011-10-19 17:08:35 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
bNodeTreeType *ntreeType_Shader;
|
|
|
|
|
2013-03-18 18:25:05 +00:00
|
|
|
void register_node_tree_type_sh(void)
|
2013-03-18 16:34:57 +00:00
|
|
|
{
|
|
|
|
bNodeTreeType *tt = ntreeType_Shader = MEM_callocN(sizeof(bNodeTreeType), "shader node tree type");
|
|
|
|
|
|
|
|
tt->type = NTREE_SHADER;
|
|
|
|
strcpy(tt->idname, "ShaderNodeTree");
|
|
|
|
strcpy(tt->ui_name, "Shader");
|
2013-05-27 08:04:07 +00:00
|
|
|
tt->ui_icon = 0; /* defined in drawnode.c */
|
2013-05-28 08:58:27 +00:00
|
|
|
strcpy(tt->ui_description, "Shader nodes");
|
2011-09-05 21:01:50 +00:00
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
tt->foreach_nodeclass = foreach_nodeclass;
|
|
|
|
tt->localize = localize;
|
|
|
|
tt->local_sync = local_sync;
|
2013-03-22 10:34:52 +00:00
|
|
|
tt->local_merge = local_merge;
|
2013-03-18 16:34:57 +00:00
|
|
|
tt->update = update;
|
|
|
|
tt->poll = shader_tree_poll;
|
|
|
|
tt->get_from_context = shader_get_from_context;
|
2011-09-05 21:01:50 +00:00
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
tt->ext.srna = &RNA_ShaderNodeTree;
|
|
|
|
|
|
|
|
ntreeTypeAdd(tt);
|
|
|
|
}
|
2011-09-05 21:01:50 +00:00
|
|
|
|
|
|
|
/* GPU material from shader nodes */
|
|
|
|
|
2016-06-14 11:31:00 +02:00
|
|
|
static void ntree_shader_link_builtin_normal(bNodeTree *ntree,
|
|
|
|
bNode *node_from,
|
|
|
|
bNodeSocket *socket_from,
|
|
|
|
bNode *displacement_node,
|
|
|
|
bNodeSocket *displacement_socket);
|
|
|
|
|
2016-05-20 14:16:54 +02:00
|
|
|
/* Find an output node of the shader tree.
|
|
|
|
*
|
|
|
|
* NOTE: it will only return output which is NOT in the group, which isn't how
|
|
|
|
* render engines works but it's how the GPU shader compilation works. This we
|
|
|
|
* can change in the future and make it a generic function, but for now it stays
|
|
|
|
* private here.
|
2017-08-01 18:03:16 +02:00
|
|
|
*
|
|
|
|
* It also does not yet take into account render engine specific output nodes,
|
|
|
|
* it should give priority to e.g. the Eevee material output node for Eevee.
|
2016-05-20 14:16:54 +02:00
|
|
|
*/
|
|
|
|
static bNode *ntree_shader_output_node(bNodeTree *ntree)
|
|
|
|
{
|
|
|
|
/* Make sure we only have single node tagged as output. */
|
|
|
|
ntreeSetOutput(ntree);
|
|
|
|
for (bNode *node = ntree->nodes.first; node != NULL; node = node->next) {
|
|
|
|
if (node->flag & NODE_DO_OUTPUT) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find socket with a specified identifier. */
|
|
|
|
static bNodeSocket *ntree_shader_node_find_socket(ListBase *sockets,
|
|
|
|
const char *identifier)
|
|
|
|
{
|
|
|
|
for (bNodeSocket *sock = sockets->first; sock != NULL; sock = sock->next) {
|
|
|
|
if (STREQ(sock->identifier, identifier)) {
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find input socket with a specified identifier. */
|
|
|
|
static bNodeSocket *ntree_shader_node_find_input(bNode *node,
|
|
|
|
const char *identifier)
|
|
|
|
{
|
|
|
|
return ntree_shader_node_find_socket(&node->inputs, identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find output socket with a specified identifier. */
|
|
|
|
static bNodeSocket *ntree_shader_node_find_output(bNode *node,
|
|
|
|
const char *identifier)
|
|
|
|
{
|
|
|
|
return ntree_shader_node_find_socket(&node->outputs, identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether shader has a displacement.
|
|
|
|
*
|
|
|
|
* Will also return a node and it's socket which is connected to a displacement
|
|
|
|
* output. Additionally, link which is attached to the displacement output is
|
|
|
|
* also returned.
|
|
|
|
*/
|
|
|
|
static bool ntree_shader_has_displacement(bNodeTree *ntree,
|
|
|
|
bNode **r_node,
|
|
|
|
bNodeSocket **r_socket,
|
|
|
|
bNodeLink **r_link)
|
|
|
|
{
|
|
|
|
bNode *output_node = ntree_shader_output_node(ntree);
|
|
|
|
if (output_node == NULL) {
|
|
|
|
/* We can't have displacement without output node, apparently. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* Make sure sockets links pointers are correct. */
|
|
|
|
ntreeUpdateTree(G.main, ntree);
|
|
|
|
bNodeSocket *displacement = ntree_shader_node_find_input(output_node,
|
|
|
|
"Displacement");
|
|
|
|
|
|
|
|
if (displacement == NULL) {
|
|
|
|
/* Non-cycles node is used as an output. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (displacement->link != NULL) {
|
|
|
|
*r_node = displacement->link->fromnode;
|
|
|
|
*r_socket = displacement->link->fromsock;
|
|
|
|
*r_link = displacement->link;
|
|
|
|
}
|
|
|
|
return displacement->link != NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-14 11:31:00 +02:00
|
|
|
static bool ntree_shader_relink_node_normal(bNodeTree *ntree,
|
|
|
|
bNode *node,
|
|
|
|
bNode *node_from,
|
|
|
|
bNodeSocket *socket_from)
|
|
|
|
{
|
|
|
|
bNodeSocket *sock = ntree_shader_node_find_input(node, "Normal");
|
|
|
|
/* TODO(sergey): Can we do something smarter here than just a name-based
|
|
|
|
* matching?
|
|
|
|
*/
|
|
|
|
if (sock == NULL) {
|
|
|
|
/* There's no Normal input, nothing to link. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (sock->link != NULL) {
|
|
|
|
/* Something is linked to the normal input already. can't
|
|
|
|
* use other input for that.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* Create connection between specified node and the normal input. */
|
|
|
|
nodeAddLink(ntree, node_from, socket_from, node, sock);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ntree_shader_link_builtin_group_normal(
|
|
|
|
bNodeTree *ntree,
|
|
|
|
bNode *group_node,
|
|
|
|
bNode *node_from,
|
|
|
|
bNodeSocket *socket_from,
|
|
|
|
bNode *displacement_node,
|
|
|
|
bNodeSocket *displacement_socket)
|
|
|
|
{
|
|
|
|
bNodeTree *group_ntree = (bNodeTree *)group_node->id;
|
|
|
|
/* Create input socket to plug displacement connection to. */
|
|
|
|
bNodeSocket *group_normal_socket =
|
|
|
|
ntreeAddSocketInterface(group_ntree,
|
|
|
|
SOCK_IN,
|
|
|
|
"NodeSocketVector",
|
|
|
|
"Normal");
|
|
|
|
/* Need to update tree so all node instances nodes gets proper sockets. */
|
2016-06-17 12:14:36 +02:00
|
|
|
bNode *group_input_node = ntreeFindType(group_ntree, NODE_GROUP_INPUT);
|
|
|
|
node_group_verify(ntree, group_node, &group_ntree->id);
|
2016-10-12 13:10:09 +02:00
|
|
|
if (group_input_node)
|
|
|
|
node_group_input_verify(group_ntree, group_input_node, &group_ntree->id);
|
2016-06-14 11:31:00 +02:00
|
|
|
ntreeUpdateTree(G.main, group_ntree);
|
|
|
|
/* Assumes sockets are always added at the end. */
|
2016-06-19 06:25:54 +10:00
|
|
|
bNodeSocket *group_node_normal_socket = group_node->inputs.last;
|
2016-06-14 11:31:00 +02:00
|
|
|
if (displacement_node == group_node) {
|
|
|
|
/* If displacement is coming from this node group we need to perform
|
|
|
|
* some internal re-linking in order to avoid cycles.
|
|
|
|
*/
|
|
|
|
bNode *group_output_node = ntreeFindType(group_ntree, NODE_GROUP_OUTPUT);
|
|
|
|
BLI_assert(group_output_node != NULL);
|
|
|
|
bNodeSocket *group_output_node_displacement_socket =
|
|
|
|
nodeFindSocket(group_output_node,
|
|
|
|
SOCK_IN,
|
|
|
|
displacement_socket->identifier);
|
|
|
|
bNodeLink *group_displacement_link = group_output_node_displacement_socket->link;
|
|
|
|
if (group_displacement_link == NULL) {
|
|
|
|
/* Displacement output is not connected to anything, can just stop
|
|
|
|
* right away.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* This code is similar to ntree_shader_relink_displacement() */
|
|
|
|
bNode *group_displacement_node = group_displacement_link->fromnode;
|
|
|
|
bNodeSocket *group_displacement_socket = group_displacement_link->fromsock;
|
|
|
|
/* Create and link bump node.
|
|
|
|
* Can't re-use bump node from parent tree because it'll cause cycle.
|
|
|
|
*/
|
|
|
|
bNode *bump_node = nodeAddStaticNode(NULL, group_ntree, SH_NODE_BUMP);
|
|
|
|
bNodeSocket *bump_input_socket = ntree_shader_node_find_input(bump_node, "Height");
|
|
|
|
bNodeSocket *bump_output_socket = ntree_shader_node_find_output(bump_node, "Normal");
|
|
|
|
BLI_assert(bump_input_socket != NULL);
|
|
|
|
BLI_assert(bump_output_socket != NULL);
|
|
|
|
nodeAddLink(group_ntree,
|
|
|
|
group_displacement_node, group_displacement_socket,
|
|
|
|
bump_node, bump_input_socket);
|
|
|
|
/* Relink normals inside of the instanced tree. */
|
|
|
|
ntree_shader_link_builtin_normal(group_ntree,
|
|
|
|
bump_node,
|
|
|
|
bump_output_socket,
|
|
|
|
group_displacement_node,
|
|
|
|
group_displacement_socket);
|
|
|
|
ntreeUpdateTree(G.main, group_ntree);
|
|
|
|
}
|
2016-10-12 13:10:09 +02:00
|
|
|
else if (group_input_node) {
|
2016-06-14 11:31:00 +02:00
|
|
|
/* Connect group node normal input. */
|
|
|
|
nodeAddLink(ntree,
|
|
|
|
node_from, socket_from,
|
|
|
|
group_node, group_node_normal_socket);
|
|
|
|
BLI_assert(group_input_node != NULL);
|
|
|
|
bNodeSocket *group_input_node_normal_socket =
|
|
|
|
nodeFindSocket(group_input_node,
|
|
|
|
SOCK_OUT,
|
|
|
|
group_normal_socket->identifier);
|
|
|
|
BLI_assert(group_input_node_normal_socket != NULL);
|
|
|
|
/* Relink normals inside of the instanced tree. */
|
|
|
|
ntree_shader_link_builtin_normal(group_ntree,
|
|
|
|
group_input_node,
|
|
|
|
group_input_node_normal_socket,
|
|
|
|
displacement_node,
|
|
|
|
displacement_socket);
|
|
|
|
ntreeUpdateTree(G.main, group_ntree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 14:16:54 +02:00
|
|
|
/* Use specified node and socket as an input for unconnected normal sockets. */
|
|
|
|
static void ntree_shader_link_builtin_normal(bNodeTree *ntree,
|
|
|
|
bNode *node_from,
|
2016-06-14 11:31:00 +02:00
|
|
|
bNodeSocket *socket_from,
|
|
|
|
bNode *displacement_node,
|
|
|
|
bNodeSocket *displacement_socket)
|
2016-05-20 14:16:54 +02:00
|
|
|
{
|
|
|
|
for (bNode *node = ntree->nodes.first; node != NULL; node = node->next) {
|
|
|
|
if (node == node_from) {
|
|
|
|
/* Don't connect node itself! */
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-14 11:31:00 +02:00
|
|
|
if (node->type == NODE_GROUP && node->id) {
|
|
|
|
/* Special re-linking for group nodes. */
|
|
|
|
ntree_shader_link_builtin_group_normal(ntree,
|
|
|
|
node,
|
|
|
|
node_from,
|
|
|
|
socket_from,
|
|
|
|
displacement_node,
|
|
|
|
displacement_socket);
|
2016-05-20 14:16:54 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-14 11:31:00 +02:00
|
|
|
if (ELEM(node->type, NODE_GROUP_INPUT, NODE_GROUP_OUTPUT)) {
|
|
|
|
/* Group inputs and outputs needs nothing special. */
|
2016-05-20 14:16:54 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-14 11:31:00 +02:00
|
|
|
ntree_shader_relink_node_normal(ntree, node, node_from, socket_from);
|
2016-05-20 14:16:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-link displacement output to unconnected normal sockets via bump node.
|
|
|
|
* This way material with have proper displacement in the viewport.
|
|
|
|
*/
|
|
|
|
static void ntree_shader_relink_displacement(bNodeTree *ntree,
|
|
|
|
short compatibility)
|
|
|
|
{
|
|
|
|
if (compatibility != NODE_NEW_SHADING) {
|
|
|
|
/* We can only deal with new shading system here. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bNode *displacement_node;
|
|
|
|
bNodeSocket *displacement_socket;
|
|
|
|
bNodeLink *displacement_link;
|
|
|
|
if (!ntree_shader_has_displacement(ntree,
|
|
|
|
&displacement_node,
|
|
|
|
&displacement_socket,
|
|
|
|
&displacement_link))
|
|
|
|
{
|
|
|
|
/* There is no displacement output connected, nothing to re-link. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* We have to disconnect displacement output socket, otherwise we'll have
|
|
|
|
* cycles in the Cycles material :)
|
|
|
|
*/
|
|
|
|
nodeRemLink(ntree, displacement_link);
|
|
|
|
/* We can't connect displacement to normal directly, use bump node for that
|
|
|
|
* and hope that it gives good enough approximation.
|
|
|
|
*/
|
|
|
|
bNode *bump_node = nodeAddStaticNode(NULL, ntree, SH_NODE_BUMP);
|
|
|
|
bNodeSocket *bump_input_socket = ntree_shader_node_find_input(bump_node, "Height");
|
|
|
|
bNodeSocket *bump_output_socket = ntree_shader_node_find_output(bump_node, "Normal");
|
|
|
|
BLI_assert(bump_input_socket != NULL);
|
|
|
|
BLI_assert(bump_output_socket != NULL);
|
|
|
|
/* Connect bump node to where displacement output was originally
|
|
|
|
* connected to.
|
|
|
|
*/
|
|
|
|
nodeAddLink(ntree,
|
|
|
|
displacement_node, displacement_socket,
|
|
|
|
bump_node, bump_input_socket);
|
|
|
|
/* Connect all free-standing Normal inputs. */
|
2016-06-14 11:31:00 +02:00
|
|
|
ntree_shader_link_builtin_normal(ntree,
|
|
|
|
bump_node,
|
|
|
|
bump_output_socket,
|
|
|
|
displacement_node,
|
|
|
|
displacement_socket);
|
2016-05-20 14:16:54 +02:00
|
|
|
/* TODO(sergey): Reconnect Geometry Info->Normal sockets to the new
|
|
|
|
* bump node.
|
|
|
|
*/
|
|
|
|
/* We modified the tree, it needs to be updated now. */
|
|
|
|
ntreeUpdateTree(G.main, ntree);
|
|
|
|
}
|
|
|
|
|
2017-07-15 16:09:44 +02:00
|
|
|
static bool ntree_tag_ssr_bsdf_cb(bNode *fromnode, bNode *UNUSED(tonode), void *userdata, const bool UNUSED(reversed))
|
|
|
|
{
|
|
|
|
switch (fromnode->type) {
|
|
|
|
case SH_NODE_BSDF_ANISOTROPIC:
|
|
|
|
case SH_NODE_EEVEE_SPECULAR:
|
|
|
|
case SH_NODE_BSDF_PRINCIPLED:
|
|
|
|
case SH_NODE_BSDF_GLOSSY:
|
2017-08-15 09:36:23 +02:00
|
|
|
case SH_NODE_BSDF_GLASS:
|
2017-07-15 16:09:44 +02:00
|
|
|
fromnode->ssr_id = (*(float *)userdata);
|
|
|
|
(*(float *)userdata) += 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* We could return false here but since we (will)
|
|
|
|
* allow the use of Closure as RGBA, we can have
|
|
|
|
* Bsdf nodes linked to other Bsdf nodes. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EEVEE: Scan the ntree to set the Screen Space Reflection
|
|
|
|
* layer id of every specular node.
|
|
|
|
*/
|
|
|
|
static void ntree_shader_tag_ssr_node(bNodeTree *ntree, short compatibility)
|
|
|
|
{
|
2017-10-16 16:36:44 -02:00
|
|
|
if (compatibility & NODE_NEWER_SHADING) {
|
2017-07-15 16:09:44 +02:00
|
|
|
/* We can only deal with new shading system here. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bNode *output_node = ntree_shader_output_node(ntree);
|
|
|
|
if (output_node == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Make sure sockets links pointers are correct. */
|
|
|
|
ntreeUpdateTree(G.main, ntree);
|
|
|
|
|
|
|
|
int lobe_count = 0;
|
|
|
|
nodeChainIter(ntree, output_node, ntree_tag_ssr_bsdf_cb, &lobe_count, true);
|
|
|
|
}
|
|
|
|
|
2014-02-14 15:11:19 +01:00
|
|
|
void ntreeGPUMaterialNodes(bNodeTree *ntree, GPUMaterial *mat, short compatibility)
|
2011-09-05 21:01:50 +00:00
|
|
|
{
|
2013-08-30 17:18:15 +00:00
|
|
|
/* localize tree to create links for reroute and mute */
|
|
|
|
bNodeTree *localtree = ntreeLocalize(ntree);
|
2011-09-05 21:01:50 +00:00
|
|
|
bNodeTreeExec *exec;
|
|
|
|
|
2016-05-20 14:16:54 +02:00
|
|
|
/* Perform all needed modifications on the tree in order to support
|
|
|
|
* displacement/bump mapping.
|
|
|
|
*/
|
|
|
|
ntree_shader_relink_displacement(localtree, compatibility);
|
|
|
|
|
2017-07-15 16:09:44 +02:00
|
|
|
ntree_shader_tag_ssr_node(localtree, compatibility);
|
|
|
|
|
2013-08-30 17:18:15 +00:00
|
|
|
exec = ntreeShaderBeginExecTree(localtree);
|
2014-02-14 15:11:19 +01:00
|
|
|
ntreeExecGPUNodes(exec, mat, 1, compatibility);
|
2013-03-18 16:34:57 +00:00
|
|
|
ntreeShaderEndExecTree(exec);
|
2013-08-30 17:18:15 +00:00
|
|
|
|
ID-Remap - Step one: core work (cleanup and rework of generic ID datablock handling).
This commit changes a lot of how IDs are handled internally, especially the unlinking/freeing
processes. So far, this was very fuzy, to summarize cleanly deleting or replacing a datablock
was pretty much impossible, except for a few special cases.
Also, unlinking was handled by each datatype, in a rather messy and prone-to-errors way (quite
a few ID usages were missed or wrongly handled that way).
One of the main goal of id-remap branch was to cleanup this, and fatorize ID links handling
by using library_query utils to allow generic handling of those, which is now the case
(now, generic ID links handling is only "knwon" from readfile.c and library_query.c).
This commit also adds backends to allow live replacement and deletion of datablocks in Blender
(so-called 'remapping' process, where we replace all usages of a given ID pointer by a new one,
or NULL one in case of unlinking).
This will allow nice new features, like ability to easily reload or relocate libraries, real immediate
deletion of datablocks in blender, replacement of one datablock by another, etc.
Some of those are for next commits.
A word of warning: this commit is highly risky, because it affects potentially a lot in Blender core.
Though it was tested rather deeply, being totally impossible to check all possible ID usage cases,
it's likely there are some remaining issues and bugs in new code... Please report them! ;)
Review task: D2027 (https://developer.blender.org/D2027).
Reviewed by campbellbarton, thanks a bunch.
2016-06-22 17:29:38 +02:00
|
|
|
ntreeFreeTree(localtree);
|
2013-08-30 17:18:15 +00:00
|
|
|
MEM_freeN(localtree);
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* **************** call to switch lamploop for material node ************ */
|
|
|
|
|
|
|
|
void (*node_shader_lamp_loop)(struct ShadeInput *, struct ShadeResult *);
|
|
|
|
|
|
|
|
void set_node_shader_lamp_loop(void (*lamp_loop_func)(ShadeInput *, ShadeResult *))
|
|
|
|
{
|
2012-09-03 02:41:12 +00:00
|
|
|
node_shader_lamp_loop = lamp_loop_func;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
bNodeTreeExec *ntreeShaderBeginExecTree_internal(bNodeExecContext *context, bNodeTree *ntree, bNodeInstanceKey parent_key)
|
2011-09-05 21:01:50 +00:00
|
|
|
{
|
|
|
|
bNodeTreeExec *exec;
|
|
|
|
bNode *node;
|
|
|
|
|
|
|
|
/* ensures only a single output node is enabled */
|
|
|
|
ntreeSetOutput(ntree);
|
|
|
|
|
|
|
|
/* common base initialization */
|
2013-03-18 16:34:57 +00:00
|
|
|
exec = ntree_exec_begin(context, ntree, parent_key);
|
2011-09-05 21:01:50 +00:00
|
|
|
|
|
|
|
/* allocate the thread stack listbase array */
|
2015-06-08 13:46:33 +02:00
|
|
|
exec->threadstack = MEM_callocN(BLENDER_MAX_THREADS * sizeof(ListBase), "thread stack array");
|
2011-09-05 21:01:50 +00:00
|
|
|
|
2012-09-03 02:41:12 +00:00
|
|
|
for (node = exec->nodetree->nodes.first; node; node = node->next)
|
|
|
|
node->need_exec = 1;
|
2011-09-05 21:01:50 +00:00
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
return exec;
|
|
|
|
}
|
|
|
|
|
|
|
|
bNodeTreeExec *ntreeShaderBeginExecTree(bNodeTree *ntree)
|
|
|
|
{
|
|
|
|
bNodeExecContext context;
|
|
|
|
bNodeTreeExec *exec;
|
|
|
|
|
|
|
|
/* XXX hack: prevent exec data from being generated twice.
|
|
|
|
* this should be handled by the renderer!
|
|
|
|
*/
|
|
|
|
if (ntree->execdata)
|
|
|
|
return ntree->execdata;
|
|
|
|
|
|
|
|
context.previews = ntree->previews;
|
|
|
|
|
|
|
|
exec = ntreeShaderBeginExecTree_internal(&context, ntree, NODE_INSTANCE_KEY_BASE);
|
|
|
|
|
|
|
|
/* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
|
|
|
|
* which only store the ntree pointer. Should be fixed at some point!
|
|
|
|
*/
|
|
|
|
ntree->execdata = exec;
|
2011-09-05 21:01:50 +00:00
|
|
|
|
|
|
|
return exec;
|
|
|
|
}
|
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
void ntreeShaderEndExecTree_internal(bNodeTreeExec *exec)
|
2011-09-05 21:01:50 +00:00
|
|
|
{
|
2013-03-18 16:34:57 +00:00
|
|
|
bNodeThreadStack *nts;
|
|
|
|
int a;
|
|
|
|
|
|
|
|
if (exec->threadstack) {
|
2015-06-08 13:46:33 +02:00
|
|
|
for (a = 0; a < BLENDER_MAX_THREADS; a++) {
|
2013-03-18 16:34:57 +00:00
|
|
|
for (nts = exec->threadstack[a].first; nts; nts = nts->next)
|
|
|
|
if (nts->stack) MEM_freeN(nts->stack);
|
|
|
|
BLI_freelistN(&exec->threadstack[a]);
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
MEM_freeN(exec->threadstack);
|
|
|
|
exec->threadstack = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ntree_exec_end(exec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ntreeShaderEndExecTree(bNodeTreeExec *exec)
|
|
|
|
{
|
|
|
|
if (exec) {
|
2013-04-04 14:07:10 +00:00
|
|
|
/* exec may get freed, so assign ntree */
|
|
|
|
bNodeTree *ntree = exec->nodetree;
|
2013-03-18 16:34:57 +00:00
|
|
|
ntreeShaderEndExecTree_internal(exec);
|
2011-09-05 21:01:50 +00:00
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
/* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
|
2013-04-04 14:07:10 +00:00
|
|
|
ntree->execdata = NULL;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-24 16:11:07 +00:00
|
|
|
/* only for Blender internal */
|
2013-01-24 21:57:13 +00:00
|
|
|
bool ntreeShaderExecTree(bNodeTree *ntree, ShadeInput *shi, ShadeResult *shr)
|
2011-09-05 21:01:50 +00:00
|
|
|
{
|
|
|
|
ShaderCallData scd;
|
2012-03-09 18:28:30 +00:00
|
|
|
/**
|
|
|
|
* \note: preserve material from ShadeInput for material id, nodetree execs change it
|
|
|
|
* fix for bug "[#28012] Mat ID messy with shader nodes"
|
|
|
|
*/
|
2011-11-08 11:38:16 +00:00
|
|
|
Material *mat = shi->mat;
|
|
|
|
bNodeThreadStack *nts = NULL;
|
2011-09-05 21:01:50 +00:00
|
|
|
bNodeTreeExec *exec = ntree->execdata;
|
2013-01-24 16:11:07 +00:00
|
|
|
int compat;
|
2011-09-05 21:01:50 +00:00
|
|
|
|
|
|
|
/* convert caller data to struct */
|
2012-09-03 02:41:12 +00:00
|
|
|
scd.shi = shi;
|
|
|
|
scd.shr = shr;
|
2011-09-05 21:01:50 +00:00
|
|
|
|
|
|
|
/* each material node has own local shaderesult, with optional copying */
|
|
|
|
memset(shr, 0, sizeof(ShadeResult));
|
|
|
|
|
2011-10-31 17:00:59 +00:00
|
|
|
/* ensure execdata is only initialized once */
|
|
|
|
if (!exec) {
|
|
|
|
BLI_lock_thread(LOCK_NODES);
|
2012-03-24 06:38:07 +00:00
|
|
|
if (!ntree->execdata)
|
2013-03-18 16:34:57 +00:00
|
|
|
ntree->execdata = ntreeShaderBeginExecTree(ntree);
|
2011-10-31 17:00:59 +00:00
|
|
|
BLI_unlock_thread(LOCK_NODES);
|
|
|
|
|
|
|
|
exec = ntree->execdata;
|
|
|
|
}
|
2011-09-05 21:01:50 +00:00
|
|
|
|
2012-09-03 02:41:12 +00:00
|
|
|
nts = ntreeGetThreadStack(exec, shi->thread);
|
2013-01-24 16:11:07 +00:00
|
|
|
compat = ntreeExecThreadNodes(exec, nts, &scd, shi->thread);
|
2011-09-05 21:01:50 +00:00
|
|
|
ntreeReleaseThreadStack(nts);
|
|
|
|
|
2012-03-02 16:05:54 +00:00
|
|
|
// \note: set material back to preserved material
|
2011-09-05 21:01:50 +00:00
|
|
|
shi->mat = mat;
|
2013-01-24 16:11:07 +00:00
|
|
|
|
2011-09-05 21:01:50 +00:00
|
|
|
/* better not allow negative for now */
|
2012-09-03 02:41:12 +00:00
|
|
|
if (shr->combined[0] < 0.0f) shr->combined[0] = 0.0f;
|
|
|
|
if (shr->combined[1] < 0.0f) shr->combined[1] = 0.0f;
|
|
|
|
if (shr->combined[2] < 0.0f) shr->combined[2] = 0.0f;
|
2013-01-24 16:11:07 +00:00
|
|
|
|
|
|
|
/* if compat is zero, it has been using non-compatible nodes */
|
|
|
|
return compat;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|