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) 2005 Blender Foundation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file blender/nodes/shader/node_shader_util.c
|
|
|
|
* \ingroup nodes
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "DNA_node_types.h"
|
|
|
|
|
|
|
|
#include "node_shader_util.h"
|
|
|
|
|
|
|
|
#include "node_exec.h"
|
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
|
|
|
|
int sh_node_poll_default(bNodeType *UNUSED(ntype), bNodeTree *ntree)
|
|
|
|
{
|
2013-05-27 07:57:17 +00:00
|
|
|
return STREQ(ntree->idname, "ShaderNodeTree");
|
2013-03-18 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sh_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
|
|
|
|
{
|
|
|
|
node_type_base(ntype, type, name, nclass, flag);
|
|
|
|
|
|
|
|
ntype->poll = sh_node_poll_default;
|
|
|
|
ntype->update_internal_links = node_update_internal_links_default;
|
|
|
|
}
|
|
|
|
|
2011-09-05 21:01:50 +00:00
|
|
|
/* ****** */
|
|
|
|
|
|
|
|
void nodestack_get_vec(float *in, short type_in, bNodeStack *ns)
|
|
|
|
{
|
2013-05-27 07:57:17 +00:00
|
|
|
float *from = ns->vec;
|
2011-09-05 21:01:50 +00:00
|
|
|
|
2013-05-27 07:57:17 +00:00
|
|
|
if (type_in == SOCK_FLOAT) {
|
|
|
|
if (ns->sockettype == SOCK_FLOAT)
|
|
|
|
*in = *from;
|
2011-09-05 21:01:50 +00:00
|
|
|
else
|
2013-05-27 07:57:17 +00:00
|
|
|
*in = (from[0] + from[1] + from[2]) / 3.0f;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
2013-05-27 07:57:17 +00:00
|
|
|
else if (type_in == SOCK_VECTOR) {
|
|
|
|
if (ns->sockettype == SOCK_FLOAT) {
|
2012-10-22 08:15:51 +00:00
|
|
|
in[0] = from[0];
|
|
|
|
in[1] = from[0];
|
|
|
|
in[2] = from[0];
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-11-06 16:23:28 +00:00
|
|
|
copy_v3_v3(in, from);
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* type_in==SOCK_RGBA */
|
2013-05-27 07:57:17 +00:00
|
|
|
if (ns->sockettype == SOCK_RGBA) {
|
2011-11-06 16:23:28 +00:00
|
|
|
copy_v4_v4(in, from);
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
2013-05-27 07:57:17 +00:00
|
|
|
else if (ns->sockettype == SOCK_FLOAT) {
|
2012-10-22 08:15:51 +00:00
|
|
|
in[0] = from[0];
|
|
|
|
in[1] = from[0];
|
|
|
|
in[2] = from[0];
|
|
|
|
in[3] = 1.0f;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-11-06 16:23:28 +00:00
|
|
|
copy_v3_v3(in, from);
|
2012-10-22 08:15:51 +00:00
|
|
|
in[3] = 1.0f;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* go over all used Geometry and Texture nodes, and return a texco flag */
|
|
|
|
/* no group inside needed, this function is called for groups too */
|
|
|
|
void ntreeShaderGetTexcoMode(bNodeTree *ntree, int r_mode, short *texco, int *mode)
|
|
|
|
{
|
|
|
|
bNode *node;
|
|
|
|
bNodeSocket *sock;
|
|
|
|
int a;
|
|
|
|
|
2013-03-18 18:25:05 +00:00
|
|
|
for (node = ntree->nodes.first; node; node = node->next) {
|
2013-05-27 07:57:17 +00:00
|
|
|
if (node->type == SH_NODE_TEXTURE) {
|
2012-03-24 06:38:07 +00:00
|
|
|
if ((r_mode & R_OSA) && node->id) {
|
2013-05-27 07:57:17 +00:00
|
|
|
Tex *tex = (Tex *)node->id;
|
2012-05-29 10:21:07 +00:00
|
|
|
if (ELEM(tex->type, TEX_IMAGE, TEX_ENVMAP)) {
|
2013-05-27 07:57:17 +00:00
|
|
|
*texco |= TEXCO_OSA | NEED_UV;
|
2012-03-25 22:35:18 +00:00
|
|
|
}
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
/* usability exception... without input we still give the node orcos */
|
2013-05-27 07:57:17 +00:00
|
|
|
sock = node->inputs.first;
|
|
|
|
if (sock == NULL || sock->link == NULL)
|
|
|
|
*texco |= TEXCO_ORCO | NEED_UV;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
2013-05-27 07:57:17 +00:00
|
|
|
else if (node->type == SH_NODE_GEOMETRY) {
|
2011-09-05 21:01:50 +00:00
|
|
|
/* note; sockets always exist for the given type! */
|
2013-05-27 07:57:17 +00:00
|
|
|
for (a = 0, sock = node->outputs.first; sock; sock = sock->next, a++) {
|
2012-03-24 06:38:07 +00:00
|
|
|
if (sock->flag & SOCK_IN_USE) {
|
2012-04-28 06:31:57 +00:00
|
|
|
switch (a) {
|
2011-09-05 21:01:50 +00:00
|
|
|
case GEOM_OUT_GLOB:
|
2013-05-27 07:57:17 +00:00
|
|
|
*texco |= TEXCO_GLOB | NEED_UV; break;
|
2011-09-05 21:01:50 +00:00
|
|
|
case GEOM_OUT_VIEW:
|
2013-05-27 07:57:17 +00:00
|
|
|
*texco |= TEXCO_VIEW | NEED_UV; break;
|
2011-09-05 21:01:50 +00:00
|
|
|
case GEOM_OUT_ORCO:
|
2013-05-27 07:57:17 +00:00
|
|
|
*texco |= TEXCO_ORCO | NEED_UV; break;
|
2011-09-05 21:01:50 +00:00
|
|
|
case GEOM_OUT_UV:
|
2013-05-27 07:57:17 +00:00
|
|
|
*texco |= TEXCO_UV | NEED_UV; break;
|
2011-09-05 21:01:50 +00:00
|
|
|
case GEOM_OUT_NORMAL:
|
2013-05-27 07:57:17 +00:00
|
|
|
*texco |= TEXCO_NORM | NEED_UV; break;
|
2011-09-05 21:01:50 +00:00
|
|
|
case GEOM_OUT_VCOL:
|
|
|
|
*texco |= NEED_UV; *mode |= MA_VERTEXCOL; break;
|
2011-09-20 17:51:04 +00:00
|
|
|
case GEOM_OUT_VCOL_ALPHA:
|
|
|
|
*texco |= NEED_UV; *mode |= MA_VERTEXCOL; break;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void node_gpu_stack_from_data(struct GPUNodeStack *gs, int type, bNodeStack *ns)
|
|
|
|
{
|
|
|
|
memset(gs, 0, sizeof(*gs));
|
|
|
|
|
2011-11-06 16:23:28 +00:00
|
|
|
copy_v4_v4(gs->vec, ns->vec);
|
2013-05-27 07:57:17 +00:00
|
|
|
gs->link = ns->data;
|
2011-09-05 21:01:50 +00:00
|
|
|
|
|
|
|
if (type == SOCK_FLOAT)
|
2013-05-27 07:57:17 +00:00
|
|
|
gs->type = GPU_FLOAT;
|
2011-09-05 21:01:50 +00:00
|
|
|
else if (type == SOCK_VECTOR)
|
2013-05-27 07:57:17 +00:00
|
|
|
gs->type = GPU_VEC3;
|
2011-09-05 21:01:50 +00:00
|
|
|
else if (type == SOCK_RGBA)
|
2013-05-27 07:57:17 +00:00
|
|
|
gs->type = GPU_VEC4;
|
2011-11-02 18:55:32 +00:00
|
|
|
else if (type == SOCK_SHADER)
|
2013-05-27 07:57:17 +00:00
|
|
|
gs->type = GPU_VEC4;
|
2011-09-05 21:01:50 +00:00
|
|
|
else
|
2013-05-27 07:57:17 +00:00
|
|
|
gs->type = GPU_NONE;
|
2011-09-05 21:01:50 +00:00
|
|
|
|
|
|
|
gs->name = "";
|
2013-05-27 07:57:17 +00:00
|
|
|
gs->hasinput = ns->hasinput && ns->data;
|
2012-07-16 23:23:33 +00:00
|
|
|
/* XXX Commented out the ns->data check here, as it seems it's not always set,
|
2011-11-20 16:38:23 +00:00
|
|
|
* even though there *is* a valid connection/output... But that might need
|
|
|
|
* further investigation.
|
|
|
|
*/
|
2013-05-27 07:57:17 +00:00
|
|
|
gs->hasoutput = ns->hasoutput /*&& ns->data*/;
|
|
|
|
gs->sockettype = ns->sockettype;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void node_data_from_gpu_stack(bNodeStack *ns, GPUNodeStack *gs)
|
|
|
|
{
|
2013-01-15 12:16:27 +00:00
|
|
|
copy_v4_v4(ns->vec, gs->vec);
|
2013-05-27 07:57:17 +00:00
|
|
|
ns->data = gs->link;
|
|
|
|
ns->sockettype = gs->sockettype;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gpu_stack_from_data_list(GPUNodeStack *gs, ListBase *sockets, bNodeStack **ns)
|
|
|
|
{
|
|
|
|
bNodeSocket *sock;
|
|
|
|
int i;
|
|
|
|
|
2013-05-27 07:57:17 +00:00
|
|
|
for (sock = sockets->first, i = 0; sock; sock = sock->next, i++)
|
2011-09-05 21:01:50 +00:00
|
|
|
node_gpu_stack_from_data(&gs[i], sock->type, ns[i]);
|
|
|
|
|
2013-05-27 07:57:17 +00:00
|
|
|
gs[i].type = GPU_NONE;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void data_from_gpu_stack_list(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs)
|
|
|
|
{
|
|
|
|
bNodeSocket *sock;
|
|
|
|
int i;
|
|
|
|
|
2013-05-27 07:57:17 +00:00
|
|
|
for (sock = sockets->first, i = 0; sock; sock = sock->next, i++)
|
2011-09-05 21:01:50 +00:00
|
|
|
node_data_from_gpu_stack(ns[i], &gs[i]);
|
|
|
|
}
|
|
|
|
|
2011-11-08 13:07:16 +00:00
|
|
|
bNode *nodeGetActiveTexture(bNodeTree *ntree)
|
|
|
|
{
|
|
|
|
/* this is the node we texture paint and draw in textured draw */
|
2013-09-02 16:05:51 +00:00
|
|
|
bNode *node, *tnode, *inactivenode = NULL;
|
2011-11-08 13:07:16 +00:00
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (!ntree)
|
2011-11-08 13:07:16 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-09-02 16:05:51 +00:00
|
|
|
for (node = ntree->nodes.first; node; node = node->next) {
|
2012-03-24 06:38:07 +00:00
|
|
|
if (node->flag & NODE_ACTIVE_TEXTURE)
|
2011-11-08 13:07:16 +00:00
|
|
|
return node;
|
2013-09-02 16:05:51 +00:00
|
|
|
else if (!inactivenode && node->typeinfo->nclass == NODE_CLASS_TEXTURE)
|
|
|
|
inactivenode = node;
|
|
|
|
}
|
2011-11-08 13:07:16 +00:00
|
|
|
|
2013-03-18 16:34:57 +00:00
|
|
|
/* node active texture node in this tree, look inside groups */
|
2013-03-18 18:25:05 +00:00
|
|
|
for (node = ntree->nodes.first; node; node = node->next) {
|
2013-05-27 07:57:17 +00:00
|
|
|
if (node->type == NODE_GROUP) {
|
|
|
|
tnode = nodeGetActiveTexture((bNodeTree *)node->id);
|
2013-09-02 16:05:51 +00:00
|
|
|
if (tnode && ((tnode->flag & NODE_ACTIVE_TEXTURE) || !inactivenode))
|
2013-03-18 16:34:57 +00:00
|
|
|
return tnode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 16:05:51 +00:00
|
|
|
return inactivenode;
|
2011-11-08 13:07:16 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 15:11:19 +01:00
|
|
|
void ntreeExecGPUNodes(bNodeTreeExec *exec, GPUMaterial *mat, int do_outputs, short compatibility)
|
2011-09-05 21:01:50 +00:00
|
|
|
{
|
|
|
|
bNodeExec *nodeexec;
|
|
|
|
bNode *node;
|
|
|
|
int n;
|
|
|
|
bNodeStack *stack;
|
2013-05-27 07:57:17 +00:00
|
|
|
bNodeStack *nsin[MAX_SOCKET]; /* arbitrary... watch this */
|
|
|
|
bNodeStack *nsout[MAX_SOCKET]; /* arbitrary... watch this */
|
2013-02-02 04:58:03 +00:00
|
|
|
GPUNodeStack gpuin[MAX_SOCKET + 1], gpuout[MAX_SOCKET + 1];
|
2012-05-19 13:28:19 +00:00
|
|
|
int do_it;
|
2011-09-05 21:01:50 +00:00
|
|
|
|
2013-05-27 07:57:17 +00:00
|
|
|
stack = exec->stack;
|
2011-09-05 21:01:50 +00:00
|
|
|
|
2013-05-27 07:57:17 +00:00
|
|
|
for (n = 0, nodeexec = exec->nodeexec; n < exec->totnodes; ++n, ++nodeexec) {
|
2011-09-05 21:01:50 +00:00
|
|
|
node = nodeexec->node;
|
|
|
|
|
2012-05-19 13:28:19 +00:00
|
|
|
do_it = FALSE;
|
2011-09-05 21:01:50 +00:00
|
|
|
/* for groups, only execute outputs for edited group */
|
2013-05-27 07:57:17 +00:00
|
|
|
if (node->typeinfo->nclass == NODE_CLASS_OUTPUT) {
|
2014-02-14 15:11:19 +01:00
|
|
|
if (node->typeinfo->compatibility & compatibility)
|
|
|
|
if (do_outputs && (node->flag & NODE_DO_OUTPUT))
|
|
|
|
do_it = TRUE;
|
2011-09-05 21:01:50 +00:00
|
|
|
}
|
|
|
|
else
|
2012-05-19 13:28:19 +00:00
|
|
|
do_it = TRUE;
|
2011-09-05 21:01:50 +00:00
|
|
|
|
2012-05-19 13:28:19 +00:00
|
|
|
if (do_it) {
|
2012-03-24 06:38:07 +00:00
|
|
|
if (node->typeinfo->gpufunc) {
|
2011-09-05 21:01:50 +00:00
|
|
|
node_get_stack(node, stack, nsin, nsout);
|
|
|
|
gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
|
|
|
|
gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
|
2013-03-18 16:34:57 +00:00
|
|
|
if (node->typeinfo->gpufunc(mat, node, &nodeexec->data, gpuin, gpuout))
|
2011-09-05 21:01:50 +00:00
|
|
|
data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-08 11:38:16 +00:00
|
|
|
|
|
|
|
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *UNUSED(out))
|
|
|
|
{
|
2013-05-27 07:57:17 +00:00
|
|
|
NodeTexBase *base = node->storage;
|
|
|
|
TexMapping *texmap = &base->tex_mapping;
|
|
|
|
float domin = (texmap->flag & TEXMAP_CLIP_MIN) != 0;
|
|
|
|
float domax = (texmap->flag & TEXMAP_CLIP_MAX) != 0;
|
2011-11-08 11:38:16 +00:00
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (domin || domax || !(texmap->flag & TEXMAP_UNIT_MATRIX)) {
|
2012-12-28 14:19:05 +00:00
|
|
|
GPUNodeLink *tmat = GPU_uniform((float *)texmap->mat);
|
2011-11-08 11:38:16 +00:00
|
|
|
GPUNodeLink *tmin = GPU_uniform(texmap->min);
|
|
|
|
GPUNodeLink *tmax = GPU_uniform(texmap->max);
|
|
|
|
GPUNodeLink *tdomin = GPU_uniform(&domin);
|
|
|
|
GPUNodeLink *tdomax = GPU_uniform(&domax);
|
|
|
|
|
|
|
|
GPU_link(mat, "mapping", in[0].link, tmat, tmin, tmax, tdomin, tdomax, &in[0].link);
|
2013-09-25 20:28:49 +00:00
|
|
|
|
|
|
|
if (texmap->type == TEXMAP_TYPE_NORMAL)
|
|
|
|
GPU_link(mat, "texco_norm", in[0].link, &in[0].link);
|
2011-11-08 11:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|