Advertisement
Guest User

Untitled

a guest
Aug 13th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 8.65 KB | None | 0 0
  1. From ec416813e75ca4270fc76d781d80151cdd7a480f Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= <foucault.clem@gmail.com>
  3. Date: Tue, 13 Aug 2019 00:11:36 +0200
  4. Subject: Eevee: Remove Additive & Multiply Blend mode
  5.  
  6. This commit also provide a compatibility code that will convert old
  7. materials using Additive or Multiply Blend mode to their node equivalent.
  8.  
  9. This conversion is only done on outputs that are enabled for eevee.
  10.  
  11. diff --git a/source/blender/blenkernel/BKE_blender_version.h b/source/blender/blenkernel/BKE_blender_version.h
  12. index ced9f4a3153..0c55ae8ee83 100644
  13. --- a/source/blender/blenkernel/BKE_blender_version.h
  14. +++ b/source/blender/blenkernel/BKE_blender_version.h
  15. @@ -27,7 +27,7 @@
  16.   * \note Use #STRINGIFY() rather than defining with quotes.
  17.   */
  18.  #define BLENDER_VERSION 281
  19. -#define BLENDER_SUBVERSION 1
  20. +#define BLENDER_SUBVERSION 2
  21.  /** Several breakages with 280, e.g. collections vs layers. */
  22.  #define BLENDER_MINVERSION 280
  23.  #define BLENDER_MINSUBVERSION 0
  24. diff --git a/source/blender/blenloader/intern/versioning_280.c b/source/blender/blenloader/intern/versioning_280.c
  25. index 15b4f513050..e04be9aaefd 100644
  26. --- a/source/blender/blenloader/intern/versioning_280.c
  27. +++ b/source/blender/blenloader/intern/versioning_280.c
  28. @@ -734,6 +734,108 @@ static void do_versions_seq_alloc_transform_and_crop(ListBase *seqbase)
  29.    }
  30.  }
  31.  
  32. +static void do_versions_material_convert_legacy_blend_mode(Material *ma)
  33. +{
  34. +  bNodeTree *ntree = ma->nodetree;
  35. +  bool need_update = false;
  36. +  char blend_method = ma->blend_method;
  37. +
  38. +  /* MA_BM_ADD and MA_BM_MULTIPLY */
  39. +  if (ELEM(blend_method, 1, 2)) {
  40. +    ma->blend_method = MA_BM_BLEND;
  41. +  }
  42. +  else {
  43. +    return;
  44. +  }
  45. +
  46. +  /* We can't do much with material without nodetrees. */
  47. +  if (!ma->use_nodes) {
  48. +    return;
  49. +  }
  50. +
  51. +  /* Iterate backwards from end so we don't encounter newly added links. */
  52. +  bNodeLink *prevlink;
  53. +  for (bNodeLink *link = ntree->links.last; link; link = prevlink) {
  54. +    prevlink = link->prev;
  55. +
  56. +    /* Detect link to replace. */
  57. +    bNode *fromnode = link->fromnode;
  58. +    bNodeSocket *fromsock = link->fromsock;
  59. +    bNode *tonode = link->tonode;
  60. +    bNodeSocket *tosock = link->tosock;
  61. +
  62. +    if (!(tonode->type && STREQ(tosock->identifier, "Surface"))) {
  63. +      continue;
  64. +    }
  65. +
  66. +    /* Only do outputs that are enabled for EEVEE */
  67. +    if (!ELEM(tonode->custom1, SHD_OUTPUT_ALL, SHD_OUTPUT_EEVEE)) {
  68. +      continue;
  69. +    }
  70. +
  71. +    /* Replace link with additive setup. */
  72. +    nodeRemLink(ntree, link);
  73. +
  74. +    if (blend_method == 1 /* MA_BM_ADD */) {
  75. +      bNode *add_node = nodeAddStaticNode(NULL, ntree, SH_NODE_ADD_SHADER);
  76. +      add_node->locx = 0.5f * (fromnode->locx + tonode->locx);
  77. +      add_node->locy = 0.5f * (fromnode->locy + tonode->locy);
  78. +
  79. +      bNodeSocket *shader1_socket = add_node->inputs.first;
  80. +      bNodeSocket *shader2_socket = add_node->inputs.last;
  81. +      bNodeSocket *add_socket = nodeFindSocket(add_node, SOCK_OUT, "Shader");
  82. +
  83. +      bNode *transp_node = nodeAddStaticNode(NULL, ntree, SH_NODE_BSDF_TRANSPARENT);
  84. +      transp_node->locx = add_node->locx;
  85. +      transp_node->locy = add_node->locy - 110.0f;
  86. +
  87. +      bNodeSocket *transp_socket = nodeFindSocket(transp_node, SOCK_OUT, "BSDF");
  88. +
  89. +      /* Link to input and material output node. */
  90. +      nodeAddLink(ntree, fromnode, fromsock, add_node, shader1_socket);
  91. +      nodeAddLink(ntree, transp_node, transp_socket, add_node, shader2_socket);
  92. +      nodeAddLink(ntree, add_node, add_socket, tonode, tosock);
  93. +    }
  94. +    else if (blend_method == 2 /* MA_BM_MULTIPLY */) {
  95. +      bNode *transp_node = nodeAddStaticNode(NULL, ntree, SH_NODE_BSDF_TRANSPARENT);
  96. +
  97. +      bNodeSocket *color_socket = nodeFindSocket(transp_node, SOCK_IN, "Color");
  98. +      bNodeSocket *transp_socket = nodeFindSocket(transp_node, SOCK_OUT, "BSDF");
  99. +
  100. +      /* If incomming link is from a closure socket, we need to convert it. */
  101. +      if (fromsock->type == SOCK_SHADER) {
  102. +        transp_node->locx = 0.33f * fromnode->locx + 0.66f * tonode->locx;
  103. +        transp_node->locy = 0.33f * fromnode->locy + 0.66f * tonode->locy;
  104. +
  105. +        bNode *shtorgb_node = nodeAddStaticNode(NULL, ntree, SH_NODE_SHADERTORGB);
  106. +        shtorgb_node->locx = 0.66f * fromnode->locx + 0.33f * tonode->locx;
  107. +        shtorgb_node->locy = 0.66f * fromnode->locy + 0.33f * tonode->locy;
  108. +
  109. +        bNodeSocket *shader_socket = nodeFindSocket(shtorgb_node, SOCK_IN, "Shader");
  110. +        bNodeSocket *rgba_socket = nodeFindSocket(shtorgb_node, SOCK_OUT, "Color");
  111. +
  112. +        nodeAddLink(ntree, fromnode, fromsock, shtorgb_node, shader_socket);
  113. +        nodeAddLink(ntree, shtorgb_node, rgba_socket, transp_node, color_socket);
  114. +      }
  115. +      else {
  116. +        transp_node->locx = 0.5f * (fromnode->locx + tonode->locx);
  117. +        transp_node->locy = 0.5f * (fromnode->locy + tonode->locy);
  118. +
  119. +        nodeAddLink(ntree, fromnode, fromsock, transp_node, color_socket);
  120. +      }
  121. +
  122. +      /* Link to input and material output node. */
  123. +      nodeAddLink(ntree, transp_node, transp_socket, tonode, tosock);
  124. +    }
  125. +
  126. +    need_update = true;
  127. +  }
  128. +
  129. +  if (need_update) {
  130. +    ntreeUpdateTree(NULL, ntree);
  131. +  }
  132. +}
  133. +
  134.  void do_versions_after_linking_280(Main *bmain)
  135.  {
  136.    bool use_collection_compat_28 = true;
  137. @@ -1129,6 +1231,14 @@ void do_versions_after_linking_280(Main *bmain)
  138.        camera->dof_ob = NULL;
  139.      }
  140.    }
  141. +
  142. +  if (!MAIN_VERSION_ATLEAST(bmain, 281, 2)) {
  143. +    /* Replace Multiply and Additive blend mode by Alpha Blend
  144. +     * now that we use dualsource blending. */
  145. +    for (Material *mat = bmain->materials.first; mat; mat = mat->id.next) {
  146. +      do_versions_material_convert_legacy_blend_mode(mat);
  147. +    }
  148. +  }
  149.  }
  150.  
  151.  /* NOTE: This version patch is intended for versions < 2.52.2,
  152. diff --git a/source/blender/draw/engines/eevee/eevee_materials.c b/source/blender/draw/engines/eevee/eevee_materials.c
  153. index ccc2d6ba020..738745f3072 100644
  154. --- a/source/blender/draw/engines/eevee/eevee_materials.c
  155. +++ b/source/blender/draw/engines/eevee/eevee_materials.c
  156. @@ -1474,21 +1474,10 @@ static void material_transparent(Material *ma,
  157.                          DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_DEPTH_EQUAL |
  158.                          DRW_STATE_BLEND_CUSTOM);
  159.  
  160. -  DRWState cur_state = DRW_STATE_WRITE_COLOR;
  161. +  DRWState cur_state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_CUSTOM;
  162.    cur_state |= (use_prepass) ? DRW_STATE_DEPTH_EQUAL : DRW_STATE_DEPTH_LESS_EQUAL;
  163.    cur_state |= (do_cull) ? DRW_STATE_CULL_BACK : 0;
  164.  
  165. -  switch (ma->blend_method) {
  166. -    case MA_BM_ADD:
  167. -    case MA_BM_MULTIPLY:
  168. -    case MA_BM_BLEND:
  169. -      cur_state |= DRW_STATE_BLEND_CUSTOM;
  170. -      break;
  171. -    default:
  172. -      BLI_assert(0);
  173. -      break;
  174. -  }
  175. -
  176.    /* Disable other blend modes and use the one we want. */
  177.    DRW_shgroup_state_disable(*shgrp, all_state);
  178.    DRW_shgroup_state_enable(*shgrp, cur_state);
  179. @@ -1564,8 +1553,6 @@ void EEVEE_materials_cache_populate(EEVEE_Data *vedata,
  180.                            &shgrp_depth_array[i],
  181.                            &shgrp_depth_clip_array[i]);
  182.            break;
  183. -        case MA_BM_ADD:
  184. -        case MA_BM_MULTIPLY:
  185.          case MA_BM_BLEND:
  186.            material_transparent(ma_array[i],
  187.                                 sldata,
  188. diff --git a/source/blender/makesdna/DNA_material_types.h b/source/blender/makesdna/DNA_material_types.h
  189. index 152ecb85991..1d1ccef8846 100644
  190. --- a/source/blender/makesdna/DNA_material_types.h
  191. +++ b/source/blender/makesdna/DNA_material_types.h
  192. @@ -306,12 +306,12 @@ typedef struct Material {
  193.  
  194.  /* blend_method */
  195.  enum {
  196. -  MA_BM_SOLID,
  197. -  MA_BM_ADD,
  198. -  MA_BM_MULTIPLY,
  199. -  MA_BM_CLIP,
  200. -  MA_BM_HASHED,
  201. -  MA_BM_BLEND,
  202. +  MA_BM_SOLID = 0,
  203. +  // MA_BM_ADD = 1, /* deprecated */
  204. +  // MA_BM_MULTIPLY = 2,  /* deprecated */
  205. +  MA_BM_CLIP = 3,
  206. +  MA_BM_HASHED = 4,
  207. +  MA_BM_BLEND = 5,
  208.  };
  209.  
  210.  /* blend_flag */
  211. diff --git a/source/blender/makesrna/intern/rna_material.c b/source/blender/makesrna/intern/rna_material.c
  212. index 6378ee15279..8bfc4bf6313 100644
  213. --- a/source/blender/makesrna/intern/rna_material.c
  214. +++ b/source/blender/makesrna/intern/rna_material.c
  215. @@ -719,16 +719,6 @@ void RNA_def_material(BlenderRNA *brna)
  216.  
  217.    static EnumPropertyItem prop_eevee_blend_items[] = {
  218.        {MA_BM_SOLID, "OPAQUE", 0, "Opaque", "Render surface without transparency"},
  219. -      {MA_BM_ADD,
  220. -       "ADD",
  221. -       0,
  222. -       "Additive",
  223. -       "Render surface and blend the result with additive blending"},
  224. -      {MA_BM_MULTIPLY,
  225. -       "MULTIPLY",
  226. -       0,
  227. -       "Multiply",
  228. -       "Render surface and blend the result with multiplicative blending"},
  229.        {MA_BM_CLIP,
  230.         "CLIP",
  231.         0,
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement