Advertisement
Guest User

Blender GLSL Node

a guest
Mar 20th, 2010
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 14.97 KB | None | 0 0
  1. Index: source/blender/blenkernel/BKE_node.h
  2. ===================================================================
  3. --- source/blender/blenkernel/BKE_node.h    (revision 27607)
  4. +++ source/blender/blenkernel/BKE_node.h    (working copy)
  5. @@ -228,6 +228,7 @@
  6.  #define SH_NODE_VALTORGB   104
  7.  #define SH_NODE_RGBTOBW        105
  8.  #define SH_NODE_TEXTURE        106
  9. +#define SH_NODE_GLSL       124
  10.  #define SH_NODE_NORMAL     107
  11.  #define SH_NODE_GEOMETRY   108
  12.  #define SH_NODE_MAPPING        109
  13. Index: source/blender/blenkernel/intern/node.c
  14. ===================================================================
  15. --- source/blender/blenkernel/intern/node.c (revision 27607)
  16. +++ source/blender/blenkernel/intern/node.c (working copy)
  17. @@ -3120,6 +3120,7 @@
  18.     nodeRegisterType(ntypelist, &sh_node_value);
  19.     nodeRegisterType(ntypelist, &sh_node_rgb);
  20.     nodeRegisterType(ntypelist, &sh_node_texture);
  21. +   nodeRegisterType(ntypelist, &sh_node_glsl);
  22.     nodeRegisterType(ntypelist, &node_dynamic_typeinfo);
  23.     nodeRegisterType(ntypelist, &sh_node_invert);
  24.     nodeRegisterType(ntypelist, &sh_node_seprgb);
  25. Index: source/blender/gpu/intern/gpu_codegen.c
  26. ===================================================================
  27. --- source/blender/gpu/intern/gpu_codegen.c (revision 27607)
  28. +++ source/blender/gpu/intern/gpu_codegen.c (working copy)
  29. @@ -92,6 +92,11 @@
  30.     ListBase outputs;
  31.  };
  32.  
  33. +struct GPUCode {
  34. +   struct GPUCode *next, *prev;
  35. +   char* body;
  36. +};
  37. +
  38.  struct GPUNodeLink {
  39.     GPUNodeStack *socket;
  40.  
  41. @@ -299,6 +304,79 @@
  42.     }
  43.  }
  44.  
  45. +char* GPU_parse_function( char *code )
  46. +{
  47. +   GHash *hash;
  48. +   GPUFunction *function;
  49. +   int i, type, qual;
  50. +
  51. +   if(!FUNCTION_HASH) {
  52. +       FUNCTION_HASH = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp);
  53. +       gpu_parse_functions_string(FUNCTION_HASH, datatoc_gpu_shader_material_glsl);
  54. +       /*FUNCTION_PROTOTYPES = gpu_generate_function_prototyps(FUNCTION_HASH);
  55. +       FUNCTION_LIB = GPU_shader_create_lib(datatoc_gpu_shader_material_glsl);*/
  56. +   }
  57. +   hash = FUNCTION_HASH;
  58. +
  59. +   while((code = strstr(code, "void "))) {
  60. +       function = MEM_callocN(sizeof(GPUFunction), "GPUFunction");
  61. +
  62. +       code = gpu_str_skip_token(code, NULL, 0);
  63. +       code = gpu_str_skip_token(code, function->name, MAX_FUNCTION_NAME);
  64. +
  65. +       /* get parameters */
  66. +       while(*code && *code != ')') {
  67. +           /* test if it's an input or output */
  68. +           qual = FUNCTION_QUAL_IN;
  69. +           if(gpu_str_prefix(code, "out "))
  70. +               qual = FUNCTION_QUAL_OUT;
  71. +           if(gpu_str_prefix(code, "inout "))
  72. +               qual = FUNCTION_QUAL_INOUT;
  73. +           if((qual != FUNCTION_QUAL_IN) || gpu_str_prefix(code, "in "))
  74. +               code = gpu_str_skip_token(code, NULL, 0);
  75. +
  76. +           /* test for type */
  77. +           type= 0;
  78. +           for(i=1; i<=16; i++) {
  79. +               if(GPU_DATATYPE_STR[i] && gpu_str_prefix(code, GPU_DATATYPE_STR[i])) {
  80. +                   type= i;
  81. +                   break;
  82. +               }
  83. +           }
  84. +
  85. +           if(!type && gpu_str_prefix(code, "sampler2DShadow"))
  86. +               type= GPU_SHADOW2D;
  87. +           if(!type && gpu_str_prefix(code, "sampler1D"))
  88. +               type= GPU_TEX1D;
  89. +           if(!type && gpu_str_prefix(code, "sampler2D"))
  90. +               type= GPU_TEX2D;
  91. +
  92. +           if(type) {
  93. +               /* add paramater */
  94. +               code = gpu_str_skip_token(code, NULL, 0);
  95. +               code = gpu_str_skip_token(code, NULL, 0);
  96. +               function->paramqual[function->totparam]= qual;
  97. +               function->paramtype[function->totparam]= type;
  98. +               function->totparam++;
  99. +           }
  100. +           else {
  101. +               fprintf(stderr, "GPU invalid function parameter in %s.\n", function->name);
  102. +               break;
  103. +           }
  104. +       }
  105. +
  106. +       if(strlen(function->name) == 0 || function->totparam == 0) {
  107. +           fprintf(stderr, "GPU functions parse error.\n");
  108. +           MEM_freeN(function);
  109. +           break;
  110. +       }
  111. +
  112. +       BLI_ghash_insert(hash, function->name, function);
  113. +       return function->name;
  114. +   }
  115. +   return 0;
  116. +}
  117. +
  118.  #if 0
  119.  static char *gpu_generate_function_prototyps(GHash *hash)
  120.  {
  121. @@ -638,6 +716,14 @@
  122.     BLI_dynstr_append(ds, "\n");
  123.  }
  124.  
  125. +static void codegen_define_functions(DynStr *ds, ListBase* functions )
  126. +{
  127. +   GPUCode* code;
  128. +   for (code=functions->first; code; code=code->next) {
  129. +       BLI_dynstr_printf(ds, code->body );
  130. +   }
  131. +}
  132. +
  133.  static void codegen_call_functions(DynStr *ds, ListBase *nodes, GPUOutput *finaloutput)
  134.  {
  135.     GPUNode *node;
  136. @@ -689,13 +775,15 @@
  137.     BLI_dynstr_append(ds, ";\n");
  138.  }
  139.  
  140. -static char *code_generate_fragment(ListBase *nodes, GPUOutput *output, const char *name)
  141. +static char *code_generate_fragment(ListBase *codes, ListBase *nodes, GPUOutput *output, const char *name)
  142.  {
  143.     DynStr *ds = BLI_dynstr_new();
  144.     char *code;
  145.  
  146.     /*BLI_dynstr_append(ds, FUNCTION_PROTOTYPES);*/
  147.  
  148. +   codegen_define_functions(ds, codes );
  149. +
  150.     codegen_set_unique_ids(nodes);
  151.     codegen_print_uniforms_functions(ds, nodes);
  152.  
  153. @@ -908,6 +996,15 @@
  154.     }
  155.  }
  156.  
  157. +/* Code Functions */
  158. +
  159. +GPUCode *GPU_code_begin(char * body)
  160. +{
  161. +   GPUCode *code = MEM_callocN(sizeof(GPUCode), "GPUCode");
  162. +   code->body = body;
  163. +   return code;
  164. +}
  165. +
  166.  /* Node Functions */
  167.  
  168.  GPUNode *GPU_node_begin(char *name)
  169. @@ -1392,7 +1489,7 @@
  170.     }
  171.  }
  172.  
  173. -GPUPass *GPU_generate_pass(ListBase *nodes, GPUNodeLink *outlink, GPUVertexAttribs *attribs, int *builtins, const char *name)
  174. +GPUPass *GPU_generate_pass(ListBase *codes, ListBase *nodes, GPUNodeLink *outlink, GPUVertexAttribs *attribs, int *builtins, const char *name)
  175.  {
  176.     GPUShader *shader;
  177.     GPUPass *pass;
  178. @@ -1410,7 +1507,7 @@
  179.     gpu_nodes_get_builtin_flag(nodes, builtins);
  180.  
  181.     /* generate code and compile with opengl */
  182. -   fragmentcode = code_generate_fragment(nodes, outlink->output, name);
  183. +   fragmentcode = code_generate_fragment(codes, nodes, outlink->output, name);
  184.     vertexcode = code_generate_vertex(nodes);
  185.     shader = GPU_shader_create(vertexcode, fragmentcode, datatoc_gpu_shader_material_glsl); /*FUNCTION_LIB);*/
  186.     MEM_freeN(fragmentcode);
  187. Index: source/blender/gpu/intern/gpu_codegen.h
  188. ===================================================================
  189. --- source/blender/gpu/intern/gpu_codegen.h (revision 27607)
  190. +++ source/blender/gpu/intern/gpu_codegen.h (working copy)
  191. @@ -66,7 +66,7 @@
  192.  struct GPUPass;
  193.  typedef struct GPUPass GPUPass;
  194.  
  195. -GPUPass *GPU_generate_pass(ListBase *nodes, struct GPUNodeLink *outlink,
  196. +GPUPass *GPU_generate_pass(ListBase* functions, ListBase *nodes, struct GPUNodeLink *outlink,
  197.     struct GPUVertexAttribs *attribs, int *builtin, const char *name);
  198.  
  199.  struct GPUShader *GPU_pass_shader(GPUPass *pass);
  200. Index: source/blender/gpu/intern/gpu_material.c
  201. ===================================================================
  202. --- source/blender/gpu/intern/gpu_material.c    (revision 27607)
  203. +++ source/blender/gpu/intern/gpu_material.c    (working copy)
  204. @@ -78,6 +78,7 @@
  205.     Material *ma;
  206.  
  207.     /* for creating the material */
  208. +   ListBase codes;
  209.     ListBase nodes;
  210.     GPUNodeLink *outlink;
  211.  
  212. @@ -190,7 +191,7 @@
  213.         GPUShader *shader;
  214.  
  215.         outlink = material->outlink;
  216. -       material->pass = GPU_generate_pass(&material->nodes, outlink,
  217. +       material->pass = GPU_generate_pass(&material->codes, &material->nodes, outlink,
  218.             &material->attribs, &material->builtins, material->ma->id.name);
  219.  
  220.         if(!material->pass)
  221. @@ -367,6 +368,12 @@
  222.     BLI_addtail(&material->nodes, node);
  223.  }
  224.  
  225. +
  226. +void GPU_material_add_code(GPUMaterial* material, GPUCode* code)
  227. +{
  228. +   BLI_addtail(&material->codes, code);
  229. +}
  230. +
  231.  /* Code generation */
  232.  
  233.  static GPUNodeLink *lamp_get_visibility(GPUMaterial *mat, GPULamp *lamp, GPUNodeLink **lv, GPUNodeLink **dist)
  234. Index: source/blender/gpu/GPU_material.h
  235. ===================================================================
  236. --- source/blender/gpu/GPU_material.h   (revision 27607)
  237. +++ source/blender/gpu/GPU_material.h   (working copy)
  238. @@ -38,6 +38,7 @@
  239.  #endif
  240.  
  241.  struct Image;
  242. +struct Text;
  243.  struct ImageUser;
  244.  struct Material;
  245.  struct Object;
  246. @@ -54,6 +55,7 @@
  247.  struct GPULamp;
  248.  
  249.  typedef struct GPUNode GPUNode;
  250. +typedef struct GPUCode GPUCode;
  251.  typedef struct GPUNodeLink GPUNodeLink;
  252.  typedef struct GPUMaterial GPUMaterial;
  253.  typedef struct GPULamp GPULamp;
  254. @@ -110,6 +112,10 @@
  255.  GPUNodeLink *GPU_socket(GPUNodeStack *sock);
  256.  GPUNodeLink *GPU_builtin(GPUBuiltin builtin);
  257.  
  258. +char* GPU_parse_function(char *code);
  259. +GPUCode* GPU_code_begin(char * body);
  260. +void GPU_add_function(GPUMaterial* mat, GPUCode *code);
  261. +
  262.  int GPU_link(GPUMaterial *mat, char *name, ...);
  263.  int GPU_stack_link(GPUMaterial *mat, char *name, GPUNodeStack *in, GPUNodeStack *out, ...);
  264.  
  265. Index: source/blender/makesrna/intern/rna_nodetree_types.h
  266. ===================================================================
  267. --- source/blender/makesrna/intern/rna_nodetree_types.h (revision 27607)
  268. +++ source/blender/makesrna/intern/rna_nodetree_types.h (working copy)
  269. @@ -31,6 +31,7 @@
  270.  DefNode( ShaderNode,     SH_NODE_VALTORGB,        def_colorramp,          "VALTORGB",       ValToRGB,         "Value to RGB",      ""              )
  271.  DefNode( ShaderNode,     SH_NODE_RGBTOBW,         0,                      "RGBTOBW",        RGBToBW,          "RGB to BW",         ""              )
  272.  DefNode( ShaderNode,     SH_NODE_TEXTURE,         def_texture,            "TEXTURE",        Texture,          "Texture",           ""              )
  273. +DefNode( ShaderNode,     SH_NODE_GLSL,            def_glsl  ,             "GLSL",           GLSL,             "GLSL",              ""              )
  274.  DefNode( ShaderNode,     SH_NODE_NORMAL,          0,                      "NORMAL",         Normal,           "Normal",            ""              )
  275.  DefNode( ShaderNode,     SH_NODE_GEOMETRY,        def_sh_geometry,        "GEOMETRY",       Geometry,         "Geometry",          ""              )
  276.  DefNode( ShaderNode,     SH_NODE_MAPPING,         def_sh_mapping,         "MAPPING",        Mapping,          "Mapping",           ""              )
  277. Index: source/blender/makesrna/intern/rna_nodetree.c
  278. ===================================================================
  279. --- source/blender/makesrna/intern/rna_nodetree.c   (revision 27607)
  280. +++ source/blender/makesrna/intern/rna_nodetree.c   (working copy)
  281. @@ -695,7 +695,18 @@
  282.     RNA_def_property_update(prop, NC_NODE|NA_EDITED, "rna_Node_update");
  283.  }
  284.  
  285. +static void def_glsl(StructRNA *srna)
  286. +{
  287. +   PropertyRNA *prop;
  288.  
  289. +   prop = RNA_def_property(srna, "shader", PROP_POINTER, PROP_NONE);
  290. +   RNA_def_property_pointer_sdna(prop, NULL, "id");
  291. +   RNA_def_property_struct_type(prop, "Text");
  292. +   RNA_def_property_flag(prop, PROP_EDITABLE);
  293. +   RNA_def_property_ui_text(prop, "GLSL Shader Text", "");
  294. +   RNA_def_property_update(prop, NC_NODE|NA_EDITED, "rna_Node_update");
  295. +}
  296. +
  297.  /* -- Shader Nodes ---------------------------------------------------------- */
  298.  
  299.  static void def_sh_material(StructRNA *srna)
  300. Index: source/blender/nodes/intern/SHD_nodes/SHD_glsl.c
  301. ===================================================================
  302. --- source/blender/nodes/intern/SHD_nodes/SHD_glsl.c    (revision 0)
  303. +++ source/blender/nodes/intern/SHD_nodes/SHD_glsl.c    (revision 0)
  304. @@ -0,0 +1,90 @@
  305. +/**
  306. + * $Id: SHD_glsl.c 26958 2010-03-19 15:45:19Z blendix $
  307. + *
  308. + * ***** BEGIN GPL LICENSE BLOCK *****
  309. + *
  310. + * This program is free software; you can redistribute it and/or
  311. + * modify it under the terms of the GNU General Public License
  312. + * as published by the Free Software Foundation; either version 2
  313. + * of the License, or (at your option) any later version.
  314. + *
  315. + * This program is distributed in the hope that it will be useful,
  316. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  317. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  318. + * GNU General Public License for more details.
  319. + *
  320. + * You should have received a copy of the GNU General Public License
  321. + * along with this program; if not, write to the Free Software Foundation,
  322. + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  323. + *
  324. + * The Original Code is Copyright (C) 2005 Blender Foundation.
  325. + * All rights reserved.
  326. + *
  327. + * The Original Code is: all of this file.
  328. + *
  329. + * Contributor(s): Matthias Fauconneau
  330. + *
  331. + * ***** END GPL LICENSE BLOCK *****
  332. + */
  333. +
  334. +#include "BKE_text.h"
  335. +#include "../SHD_util.h"
  336. +#include "GPU_material.h"
  337. +
  338. +typedef struct Text Text;
  339. +
  340. +/* **************** GLSL ******************** */
  341. +static bNodeSocketType sh_node_glsl_in[]= {
  342. +   {   SOCK_VECTOR, 1, "Vector",   0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},   /* no limit */
  343. +   {   SOCK_VECTOR, 1, "Vector",   0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},   /* no limit */
  344. +   {   SOCK_VECTOR, 1, "Vector",   0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},   /* no limit */
  345. +   {   SOCK_VECTOR, 1, "Vector",   0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},   /* no limit */
  346. +   {   -1, 0, ""   }
  347. +};
  348. +static bNodeSocketType sh_node_glsl_out[]= {
  349. +   {   SOCK_VECTOR, 1, "Vector",   0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
  350. +   {   SOCK_VECTOR, 1, "Vector",   0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
  351. +   {   SOCK_VECTOR, 1, "Vector",   0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
  352. +   {   SOCK_VECTOR, 1, "Vector",   0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
  353. +   {   -1, 0, ""   }
  354. +};
  355. +
  356. +static void node_shader_exec_glsl(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
  357. +{
  358. +}
  359. +
  360. +static int gpu_shader_glsl(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
  361. +{
  362. +   Text* shader = (Text*)node->id;
  363. +   if(shader) {
  364. +       char* name = GPU_parse_function( txt_to_buf(shader) );
  365. +       if( name && GPU_stack_link(mat, name, in, out ) ) {
  366. +           GPU_material_add_code(mat, GPU_code_begin( txt_to_buf(shader) ) );
  367. +           return 1;
  368. +       }
  369. +       else
  370. +           return 0;
  371. +   }
  372. +   else
  373. +       return 0;
  374. +}
  375. +
  376. +bNodeType sh_node_glsl= {
  377. +   /* *next,*prev */   NULL, NULL,
  378. +   /* type code   */   SH_NODE_GLSL,
  379. +   /* name        */   "GLSL",
  380. +   /* width+range */   120, 80, 240,
  381. +   /* class+opts  */   NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW,
  382. +   /* input sock  */   sh_node_glsl_in,
  383. +   /* output sock */   sh_node_glsl_out,
  384. +   /* storage     */   "",
  385. +   /* execfunc    */   node_shader_exec_glsl,
  386. +   /* butfunc     */   NULL,
  387. +   /* initfunc    */   NULL,
  388. +   /* freestoragefunc    */    NULL,
  389. +   /* copystoragefunc    */    NULL,
  390. +   /* id          */   NULL, NULL, NULL,
  391. +   /* gpufunc     */   gpu_shader_glsl
  392. +
  393. +};
  394. +
  395. Index: source/blender/nodes/SHD_node.h
  396. ===================================================================
  397. --- source/blender/nodes/SHD_node.h (revision 27607)
  398. +++ source/blender/nodes/SHD_node.h (working copy)
  399. @@ -48,6 +48,7 @@
  400.  extern bNodeType sh_node_valtorgb;
  401.  extern bNodeType sh_node_rgbtobw;
  402.  extern bNodeType sh_node_texture;
  403. +extern bNodeType sh_node_glsl;
  404.  extern bNodeType sh_node_normal;
  405.  extern bNodeType sh_node_geom;
  406.  extern bNodeType sh_node_mapping;
  407. Index: source/blender/editors/space_node/drawnode.c
  408. ===================================================================
  409. --- source/blender/editors/space_node/drawnode.c    (revision 27607)
  410. +++ source/blender/editors/space_node/drawnode.c    (working copy)
  411. @@ -286,6 +286,11 @@
  412.     }
  413.  }
  414.  
  415. +static void node_buts_glsl(uiLayout *layout, bContext *C, PointerRNA *ptr)
  416. +{
  417. +   uiItemR(layout, "", 0, ptr, "shader", 0);
  418. +}
  419. +
  420.  static void node_buts_math(uiLayout *layout, bContext *C, PointerRNA *ptr)
  421.  {
  422.     uiItemR(layout, "", 0, ptr, "operation", 0);
  423. @@ -425,6 +430,9 @@
  424.         case SH_NODE_TEXTURE:
  425.             ntype->uifunc= node_buts_texture;
  426.             break;
  427. +       case SH_NODE_GLSL:
  428. +           ntype->uifunc= node_buts_glsl;
  429. +           break;
  430.         case SH_NODE_NORMAL:
  431.             ntype->uifunc= node_buts_normal;
  432.             break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement