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"
|
2011-09-05 21:01:50 +00:00
|
|
|
|
|
|
|
#include "BLI_listbase.h"
|
|
|
|
#include "BLI_math.h"
|
|
|
|
#include "BLI_threads.h"
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
2011-11-09 15:00:11 +00:00
|
|
|
#include "BLF_translation.h"
|
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
#include "BKE_context.h"
|
2011-09-05 21:01:50 +00:00
|
|
|
#include "BKE_global.h"
|
|
|
|
#include "BKE_main.h"
|
|
|
|
#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"
|
|
|
|
|
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);
|
|
|
|
/* allow empty engine string too, this is from older versions that didn't have registerable engines yet */
|
|
|
|
return (scene->r.engine[0] == '\0'
|
|
|
|
|| strcmp(scene->r.engine, "BLENDER_RENDER")==0
|
|
|
|
|| strcmp(scene->r.engine, "CYCLES")==0);
|
|
|
|
}
|
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);
|
|
|
|
Object *ob = OBACT;
|
|
|
|
|
|
|
|
if (snode->shaderfrom == SNODE_SHADER_OBJECT) {
|
|
|
|
if (ob) {
|
|
|
|
if (ob->type == OB_LAMP) {
|
|
|
|
*r_from = &ob->id;
|
|
|
|
*r_id = ob->data;
|
|
|
|
*r_ntree = ((Lamp *)ob->data)->nodetree;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Material *ma = give_current_material(ob, ob->actcol);
|
|
|
|
if (ma) {
|
|
|
|
*r_from = &ob->id;
|
|
|
|
*r_id = &ma->id;
|
|
|
|
*r_ntree = ma->nodetree;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-11-07 22:14:48 +00:00
|
|
|
static void foreach_nodeclass(Scene *scene, void *calldata, bNodeClassCallback func)
|
|
|
|
{
|
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
|
|
|
|
2012-05-05 14:33:36 +00:00
|
|
|
if (BKE_scene_use_new_shading_nodes(scene)) {
|
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-18 16:34:57 +00:00
|
|
|
static void local_sync(bNodeTree *UNUSED(localtree), bNodeTree *UNUSED(ntree))
|
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");
|
|
|
|
tt->ui_icon = 0; /* defined in drawnode.c */
|
|
|
|
strcpy(tt->ui_description, "");
|
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;
|
|
|
|
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 */
|
|
|
|
|
|
|
|
void ntreeGPUMaterialNodes(bNodeTree *ntree, GPUMaterial *mat)
|
|
|
|
{
|
|
|
|
bNodeTreeExec *exec;
|
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
exec = ntreeShaderBeginExecTree(ntree);
|
2011-09-05 21:01:50 +00:00
|
|
|
|
|
|
|
ntreeExecGPUNodes(exec, mat, 1);
|
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
ntreeShaderEndExecTree(exec);
|
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 */
|
2012-09-03 02:41:12 +00: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) {
|
|
|
|
for (a = 0; a < BLENDER_MAX_THREADS; a++) {
|
|
|
|
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) {
|
|
|
|
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 */
|
|
|
|
exec->nodetree->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
|
|
|
}
|