[Bf-blender-cvs] [b978c1bc654] master: Cleanup: Replace recursive quadratic node link mute operation

2022-09-03 Thread Hans Goudey
Commit: b978c1bc65434c5818d84a61f7446a4957dcd211
Author: Hans Goudey
Date:   Sat Sep 3 17:57:53 2022 -0500
Branches: master
https://developer.blender.org/rBb978c1bc65434c5818d84a61f7446a4957dcd211

Cleanup: Replace recursive quadratic node link mute operation

The previous implementation iterated over all links multiple times
recursively. Instead, use the node tree topology cache, only iterate
over all links once, and use a stack to propagate the mute upsteam
and downstream.

===

M   source/blender/blenkernel/BKE_node.h
M   source/blender/blenkernel/intern/node.cc
M   source/blender/editors/space_node/node_relationships.cc

===

diff --git a/source/blender/blenkernel/BKE_node.h 
b/source/blender/blenkernel/BKE_node.h
index 1118552b643..8affbf0ca67 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -707,7 +707,10 @@ struct bNodeLink *nodeAddLink(struct bNodeTree *ntree,
   struct bNodeSocket *tosock);
 void nodeRemLink(struct bNodeTree *ntree, struct bNodeLink *link);
 void nodeRemSocketLinks(struct bNodeTree *ntree, struct bNodeSocket *sock);
-void nodeMuteLinkToggle(struct bNodeTree *ntree, struct bNodeLink *link);
+/**
+ * Set the mute status of a single link.
+ */
+void nodeLinkSetMute(struct bNodeTree *ntree, struct bNodeLink *link, const 
bool muted);
 bool nodeLinkIsHidden(const struct bNodeLink *link);
 bool nodeLinkIsSelected(const struct bNodeLink *link);
 void nodeInternalRelink(struct bNodeTree *ntree, struct bNode *node);
diff --git a/source/blender/blenkernel/intern/node.cc 
b/source/blender/blenkernel/intern/node.cc
index c2ad0a93d83..e1eaed71f37 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -118,9 +118,6 @@ static void node_free_node(bNodeTree *ntree, bNode *node);
 static void node_socket_interface_free(bNodeTree *UNUSED(ntree),
bNodeSocket *sock,
const bool do_id_user);
-static void nodeMuteRerouteOutputLinks(struct bNodeTree *ntree,
-   struct bNode *node,
-   const bool mute);
 
 static void ntree_init_data(ID *id)
 {
@@ -2350,102 +2347,11 @@ void nodeRemLink(bNodeTree *ntree, bNodeLink *link)
   }
 }
 
-/* Check if all output links are muted or not. */
-static bool nodeMuteFromSocketLinks(const bNodeTree *ntree, const bNodeSocket 
*sock)
+void nodeLinkSetMute(bNodeTree *ntree, bNodeLink *link, const bool muted)
 {
-  int tot = 0;
-  int muted = 0;
-  LISTBASE_FOREACH (const bNodeLink *, link, &ntree->links) {
-if (link->fromsock == sock) {
-  tot++;
-  if (link->flag & NODE_LINK_MUTED) {
-muted++;
-  }
-}
-  }
-  return tot == muted;
-}
-
-static void nodeMuteLink(bNodeLink *link)
-{
-  link->flag |= NODE_LINK_MUTED;
-  link->flag |= NODE_LINK_TEST;
-  if (!(link->tosock->flag & SOCK_MULTI_INPUT)) {
-link->tosock->flag &= ~SOCK_IN_USE;
-  }
-}
-
-static void nodeUnMuteLink(bNodeLink *link)
-{
-  link->flag &= ~NODE_LINK_MUTED;
-  link->flag |= NODE_LINK_TEST;
-  link->tosock->flag |= SOCK_IN_USE;
-}
-
-/* Upstream muting. Always happens when unmuting but checks when muting. 
O(n^2) algorithm. */
-static void nodeMuteRerouteInputLinks(bNodeTree *ntree, bNode *node, const 
bool mute)
-{
-  if (node->type != NODE_REROUTE) {
-return;
-  }
-  if (!mute || nodeMuteFromSocketLinks(ntree, (bNodeSocket 
*)node->outputs.first)) {
-bNodeSocket *sock = (bNodeSocket *)node->inputs.first;
-LISTBASE_FOREACH (bNodeLink *, link, &ntree->links) {
-  if (!(link->flag & NODE_LINK_VALID) || (link->tosock != sock)) {
-continue;
-  }
-  if (mute) {
-nodeMuteLink(link);
-  }
-  else {
-nodeUnMuteLink(link);
-  }
-  nodeMuteRerouteInputLinks(ntree, link->fromnode, mute);
-}
-  }
-}
-
-/* Downstream muting propagates when reaching reroute nodes. O(n^2) algorithm. 
*/
-static void nodeMuteRerouteOutputLinks(bNodeTree *ntree, bNode *node, const 
bool mute)
-{
-  if (node->type != NODE_REROUTE) {
-return;
-  }
-  bNodeSocket *sock;
-  sock = (bNodeSocket *)node->outputs.first;
-  LISTBASE_FOREACH (bNodeLink *, link, &ntree->links) {
-if (!(link->flag & NODE_LINK_VALID) || (link->fromsock != sock)) {
-  continue;
-}
-if (mute) {
-  nodeMuteLink(link);
-}
-else {
-  nodeUnMuteLink(link);
-}
-nodeMuteRerouteOutputLinks(ntree, link->tonode, mute);
-  }
-}
-
-void nodeMuteLinkToggle(bNodeTree *ntree, bNodeLink *link)
-{
-  if (link->tosock) {
-bool mute = !(link->flag & NODE_LINK_MUTED);
-if (mute) {
-  nodeMuteLink(link);
-}
-else {
-  nodeUnMuteLink(link);
-}
-if (link->tonode->type == NODE_REROUTE) {
-  nodeMuteRerouteOutput

[Bf-blender-cvs] [0ff920b7777] master: Cleanup: Clarify multi-socket input sorting

2022-09-03 Thread Hans Goudey
Commit: 0ff920b91d6dcc002257f437f86e2d14df01
Author: Hans Goudey
Date:   Sat Sep 3 23:22:12 2022 -0500
Branches: master
https://developer.blender.org/rB0ff920b91d6dcc002257f437f86e2d14df01

Cleanup: Clarify multi-socket input sorting

The multi-socket input sorting was used for two purposes: moving links
to the proper positions when dragging a new link, and resetting the
multi-input indices on the links when removing a link. They are now
separated into two functions, and the sorting when making a group
node that didn't accomplish anything is removed (in that case a
proper implementation would copy the indices from the original
exterior sockets).

===

M   source/blender/editors/space_node/node_group.cc
M   source/blender/editors/space_node/node_intern.hh
M   source/blender/editors/space_node/node_relationships.cc

===

diff --git a/source/blender/editors/space_node/node_group.cc 
b/source/blender/editors/space_node/node_group.cc
index 482b2864d58..21def1bd9d7 100644
--- a/source/blender/editors/space_node/node_group.cc
+++ b/source/blender/editors/space_node/node_group.cc
@@ -25,6 +25,7 @@
 #include "BKE_context.h"
 #include "BKE_lib_id.h"
 #include "BKE_main.h"
+#include "BKE_node_runtime.hh"
 #include "BKE_node_tree_update.h"
 #include "BKE_report.h"
 
@@ -836,8 +837,8 @@ static void node_group_make_insert_selected(const bContext 
&C, bNodeTree &ntree,
 
   /* relink external sockets */
   LISTBASE_FOREACH_MUTABLE (bNodeLink *, link, &ntree.links) {
-int fromselect = node_group_make_use_node(*link->fromnode, gnode);
-int toselect = node_group_make_use_node(*link->tonode, gnode);
+const bool fromselect = node_group_make_use_node(*link->fromnode, gnode);
+const bool toselect = node_group_make_use_node(*link->tonode, gnode);
 
 if ((fromselect && link->tonode == gnode) || (toselect && link->fromnode 
== gnode)) {
   /* remove all links to/from the gnode.
@@ -917,8 +918,8 @@ static void node_group_make_insert_selected(const bContext 
&C, bNodeTree &ntree,
 
   /* move internal links */
   LISTBASE_FOREACH_MUTABLE (bNodeLink *, link, &ntree.links) {
-int fromselect = node_group_make_use_node(*link->fromnode, gnode);
-int toselect = node_group_make_use_node(*link->tonode, gnode);
+const bool fromselect = node_group_make_use_node(*link->fromnode, gnode);
+const bool toselect = node_group_make_use_node(*link->tonode, gnode);
 
 if (fromselect && toselect) {
   BLI_remlink(&ntree.links, link);
@@ -1041,11 +1042,6 @@ static int node_group_make_exec(bContext *C, wmOperator 
*op)
 nodeSetActive(&ntree, gnode);
 if (ngroup) {
   ED_node_tree_push(&snode, ngroup, gnode);
-
-  ngroup->ensure_topology_cache();
-  LISTBASE_FOREACH (bNode *, node, &ngroup->nodes) {
-sort_multi_input_socket_links(*node, nullptr, nullptr);
-  }
 }
   }
 
diff --git a/source/blender/editors/space_node/node_intern.hh 
b/source/blender/editors/space_node/node_intern.hh
index 011b9f6b631..6754673cffc 100644
--- a/source/blender/editors/space_node/node_intern.hh
+++ b/source/blender/editors/space_node/node_intern.hh
@@ -267,8 +267,6 @@ void NODE_OT_group_edit(wmOperatorType *ot);
 
 /* node_relationships.cc */
 
-void sort_multi_input_socket_links(bNode &node, bNodeLink *drag_link, const 
float2 *cursor);
-
 void NODE_OT_link(wmOperatorType *ot);
 void NODE_OT_link_make(wmOperatorType *ot);
 void NODE_OT_links_cut(wmOperatorType *ot);
diff --git a/source/blender/editors/space_node/node_relationships.cc 
b/source/blender/editors/space_node/node_relationships.cc
index e95de7b8e85..067c01dcc58 100644
--- a/source/blender/editors/space_node/node_relationships.cc
+++ b/source/blender/editors/space_node/node_relationships.cc
@@ -73,6 +73,8 @@ static void clear_picking_highlight(ListBase *links)
 
 namespace blender::ed::space_node {
 
+void update_multi_input_indices_for_removed_links(bNode &node);
+
 /*  */
 /** \name Add Node
  * \{ */
@@ -103,11 +105,9 @@ static void pick_link(
 
   nldrag.links.append(link);
   nodeRemLink(snode.edittree, &link_to_pick);
-
-  BLI_assert(nldrag.last_node_hovered_while_dragging_a_link != nullptr);
-
   snode.edittree->ensure_topology_cache();
-  
sort_multi_input_socket_links(*nldrag.last_node_hovered_while_dragging_a_link, 
nullptr, nullptr);
+  BLI_assert(nldrag.last_node_hovered_while_dragging_a_link != nullptr);
+  
update_multi_input_indices_for_removed_links(*nldrag.last_node_hovered_while_dragging_a_link);
 
   /* Send changed event to original link->tonode. */
   if (node) {
@@ -292,7 +292,9 @@ struct LinkAndPosition {
   float2 multi_socket_position;
 };
 
-void sort_multi_input_socket_links(bNode &node, bNodeLink *drag_link, const 
float2 *cursor)
+static void sort_multi_input_socket_links_with_drag(bNode &node,

[Bf-blender-cvs] [56193eccf64] master: Cleanup: Refactor node add reroute operator

2022-09-03 Thread Hans Goudey
Commit: 56193eccf646e9e6e56c232d390ed6680ce1370c
Author: Hans Goudey
Date:   Sat Sep 3 14:12:55 2022 -0500
Branches: master
https://developer.blender.org/rB56193eccf646e9e6e56c232d390ed6680ce1370c

Cleanup: Refactor node add reroute operator

Use C++ Map that supports the duplication natively. Use vectors instead
of linked lists, and adjust naming. Also remove combination of reroutes
for input sockets, which doesn't make sense since a reroute isn't
allowed to combine multiple input links into one output.

===

M   source/blender/editors/space_node/node_add.cc
M   source/blender/editors/space_node/node_edit.cc
M   source/blender/editors/space_node/node_group.cc

===

diff --git a/source/blender/editors/space_node/node_add.cc 
b/source/blender/editors/space_node/node_add.cc
index c5a9df54a82..79d8b4fe52e 100644
--- a/source/blender/editors/space_node/node_add.cc
+++ b/source/blender/editors/space_node/node_add.cc
@@ -5,6 +5,8 @@
  * \ingroup spnode
  */
 
+#include 
+
 #include "MEM_guardedalloc.h"
 
 #include "DNA_collection_types.h"
@@ -20,6 +22,7 @@
 #include "BKE_lib_id.h"
 #include "BKE_main.h"
 #include "BKE_node.h"
+#include "BKE_node_runtime.hh"
 #include "BKE_node_tree_update.h"
 #include "BKE_report.h"
 #include "BKE_scene.h"
@@ -101,195 +104,113 @@ bNode *add_static_node(const bContext &C, int type, 
const float2 &location)
 /** \name Add Reroute Operator
  * \{ */
 
-static bool add_reroute_intersect_check(const bNodeLink &link,
-float mcoords[][2],
-int tot,
-float2 &result)
+static std::optional path_link_intersection(const bNodeLink &link, 
const Span path)
 {
   std::array coords;
   node_link_bezier_points_evaluated(link, coords);
 
-  for (int i = 0; i < tot - 1; i++) {
-for (int b = 0; b < NODE_LINK_RESOL; b++) {
-  if (isect_seg_seg_v2_point(mcoords[i], mcoords[i + 1], coords[b], 
coords[b + 1], result) >
-  0) {
-return true;
+  for (const int i : path.index_range().drop_back(1)) {
+for (const int j : IndexRange(NODE_LINK_RESOL)) {
+  float2 result;
+  if (isect_seg_seg_v2_point(path[i], path[i + 1], coords[j], coords[j + 
1], result) > 0) {
+return result;
   }
 }
   }
 
-  return false;
+  return std::nullopt;
 }
 
-struct bNodeSocketLink {
-  struct bNodeSocketLink *next, *prev;
-
-  bNodeSocket *sock;
-  bNodeLink *link;
-  float2 point;
+struct RerouteCutsForSocket {
+  /* The output socket's owner node. */
+  bNode *from_node;
+  /* Intersected links connected to the socket and their path intersection 
locations. */
+  Map links;
 };
 
-static bNodeSocketLink *add_reroute_insert_socket_link(ListBase *lb,
-   bNodeSocket *sock,
-   bNodeLink *link,
-   const float2 &point)
+static int add_reroute_exec(bContext *C, wmOperator *op)
 {
-  bNodeSocketLink *socklink, *prev;
-
-  socklink = MEM_cnew("socket link");
-  socklink->sock = sock;
-  socklink->link = link;
-  copy_v2_v2(socklink->point, point);
+  const ARegion ®ion = *CTX_wm_region(C);
+  SpaceNode &snode = *CTX_wm_space_node(C);
+  bNodeTree &ntree = *snode.edittree;
 
-  for (prev = (bNodeSocketLink *)lb->last; prev; prev = prev->prev) {
-if (prev->sock == sock) {
+  Vector path;
+  RNA_BEGIN (op->ptr, itemptr, "path") {
+float2 loc_region;
+RNA_float_get_array(&itemptr, "loc", loc_region);
+float2 loc_view;
+UI_view2d_region_to_view(®ion.v2d, loc_region.x, loc_region.y, 
&loc_view.x, &loc_view.y);
+path.append(loc_view);
+if (path.size() >= 256) {
   break;
 }
   }
-  BLI_insertlinkafter(lb, prev, socklink);
-  return socklink;
-}
-
-static bNodeSocketLink *add_reroute_do_socket_section(bContext *C,
-  bNodeSocketLink 
*socklink,
-  int in_out)
-{
-  SpaceNode *snode = CTX_wm_space_node(C);
-  bNodeTree *ntree = snode->edittree;
-  bNode *reroute_node = nullptr;
-  bNodeSocket *cursock = socklink->sock;
-  float2 insert_point{0.0f, 0.0f};
-  int num_links;
-
-  num_links = 0;
-
-  while (socklink && socklink->sock == cursock) {
-if (!(socklink->link->flag & NODE_LINK_TEST)) {
-  socklink->link->flag |= NODE_LINK_TEST;
-
-  /* create the reroute node for this cursock */
-  if (!reroute_node) {
-reroute_node = nodeAddStaticNode(C, ntree, NODE_REROUTE);
-
-/* add a single link to/from the reroute node to replace multiple 
links */
-if (in_out == SOCK_OUT) {
-  nodeAddLink(ntree,
-  socklink->link->fromnode,
-  socklink->link->fromsock,
-

[Bf-blender-cvs] [b60850d3959] master: Cleanup: Deduplicate node link intersection test

2022-09-03 Thread Hans Goudey
Commit: b60850d3959534b49a2912d1fe775878fbe8a623
Author: Hans Goudey
Date:   Sat Sep 3 14:52:27 2022 -0500
Branches: master
https://developer.blender.org/rBb60850d3959534b49a2912d1fe775878fbe8a623

Cleanup: Deduplicate node link intersection test

===

M   source/blender/editors/space_node/node_add.cc
M   source/blender/editors/space_node/node_intern.hh
M   source/blender/editors/space_node/node_relationships.cc

===

diff --git a/source/blender/editors/space_node/node_add.cc 
b/source/blender/editors/space_node/node_add.cc
index 79d8b4fe52e..7e46877d0ba 100644
--- a/source/blender/editors/space_node/node_add.cc
+++ b/source/blender/editors/space_node/node_add.cc
@@ -104,7 +104,7 @@ bNode *add_static_node(const bContext &C, int type, const 
float2 &location)
 /** \name Add Reroute Operator
  * \{ */
 
-static std::optional path_link_intersection(const bNodeLink &link, 
const Span path)
+std::optional link_path_intersection(const bNodeLink &link, const 
Span path)
 {
   std::array coords;
   node_link_bezier_points_evaluated(link, coords);
@@ -166,7 +166,7 @@ static int add_reroute_exec(bContext *C, wmOperator *op)
 if (node_link_is_hidden_or_dimmed(region.v2d, *link)) {
   continue;
 }
-const std::optional intersection = path_link_intersection(*link, 
path);
+const std::optional intersection = link_path_intersection(*link, 
path);
 if (!intersection) {
   continue;
 }
diff --git a/source/blender/editors/space_node/node_intern.hh 
b/source/blender/editors/space_node/node_intern.hh
index 92b2b82209b..011b9f6b631 100644
--- a/source/blender/editors/space_node/node_intern.hh
+++ b/source/blender/editors/space_node/node_intern.hh
@@ -236,6 +236,8 @@ void node_draw_link_bezier(const bContext &C,
 void node_link_bezier_points_evaluated(const bNodeLink &link,
std::array 
&coords);
 
+std::optional link_path_intersection(const bNodeLink &link, 
Span path);
+
 void draw_nodespace_back_pix(const bContext &C,
  ARegion ®ion,
  SpaceNode &snode,
diff --git a/source/blender/editors/space_node/node_relationships.cc 
b/source/blender/editors/space_node/node_relationships.cc
index d06864f4a8b..7d59bb6cb0c 100644
--- a/source/blender/editors/space_node/node_relationships.cc
+++ b/source/blender/editors/space_node/node_relationships.cc
@@ -1291,28 +1291,6 @@ void NODE_OT_link_make(wmOperatorType *ot)
 
 /** \} */
 
-/*  */
-/** \name Node Link Intersect
- * \{ */
-
-static bool node_links_intersect(bNodeLink &link, const float mcoords[][2], 
int tot)
-{
-  std::array coords;
-  node_link_bezier_points_evaluated(link, coords);
-
-  for (int i = 0; i < tot - 1; i++) {
-for (int b = 0; b < NODE_LINK_RESOL; b++) {
-  if (isect_seg_seg_v2(mcoords[i], mcoords[i + 1], coords[b], coords[b + 
1]) > 0) {
-return true;
-  }
-}
-  }
-
-  return false;
-}
-
-/** \} */
-
 /*  */
 /** \name Cut Link Operator
  * \{ */
@@ -1321,24 +1299,22 @@ static int cut_links_exec(bContext *C, wmOperator *op)
 {
   Main &bmain = *CTX_data_main(C);
   SpaceNode &snode = *CTX_wm_space_node(C);
-  ARegion ®ion = *CTX_wm_region(C);
+  const ARegion ®ion = *CTX_wm_region(C);
 
-  int i = 0;
-  float mcoords[256][2];
+  Vector path;
   RNA_BEGIN (op->ptr, itemptr, "path") {
-float loc[2];
-
-RNA_float_get_array(&itemptr, "loc", loc);
-UI_view2d_region_to_view(
-®ion.v2d, (int)loc[0], (int)loc[1], &mcoords[i][0], &mcoords[i][1]);
-i++;
-if (i >= 256) {
+float2 loc_region;
+RNA_float_get_array(&itemptr, "loc", loc_region);
+float2 loc_view;
+UI_view2d_region_to_view(®ion.v2d, loc_region.x, loc_region.y, 
&loc_view.x, &loc_view.y);
+path.append(loc_view);
+if (path.size() >= 256) {
   break;
 }
   }
   RNA_END;
 
-  if (i == 0) {
+  if (path.is_empty()) {
 return OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH;
   }
 
@@ -1355,7 +1331,7 @@ static int cut_links_exec(bContext *C, wmOperator *op)
   continue;
 }
 
-if (node_links_intersect(*link, mcoords, i)) {
+if (link_path_intersection(*link, path)) {
 
   if (!found) {
 /* TODO(sergey): Why did we kill jobs twice? */
@@ -1418,24 +1394,22 @@ static int mute_links_exec(bContext *C, wmOperator *op)
 {
   Main &bmain = *CTX_data_main(C);
   SpaceNode &snode = *CTX_wm_space_node(C);
-  ARegion ®ion = *CTX_wm_region(C);
+  const ARegion ®ion = *CTX_wm_region(C);
 
-  int i = 0;
-  float mcoords[256][2];
+  Vector path;
   RNA_BEGIN (op->ptr, itemptr, "path") {
-float loc[2];
-
-RNA_float_get_array(&itemptr, "loc", loc);
-UI_view2d_region_to_view(
-®ion.v2d, (int)loc[0], (int)loc[1], &mcoords[

[Bf-blender-cvs] [8d4fbfda26e] soc-2022-many-lights-sampling: Fix: resolve light tree compilation errors

2022-09-03 Thread Jeffrey Liu
Commit: 8d4fbfda26e74ea9c89869174f08819401a676fe
Author: Jeffrey Liu
Date:   Sat Sep 3 18:37:55 2022 -0500
Branches: soc-2022-many-lights-sampling
https://developer.blender.org/rB8d4fbfda26e74ea9c89869174f08819401a676fe

Fix: resolve light tree compilation errors

===

M   intern/cycles/kernel/integrator/shade_background.h
M   intern/cycles/kernel/light/light_tree.h

===

diff --git a/intern/cycles/kernel/integrator/shade_background.h 
b/intern/cycles/kernel/integrator/shade_background.h
index 041a8df8e9d..2e0bbc4899d 100644
--- a/intern/cycles/kernel/integrator/shade_background.h
+++ b/intern/cycles/kernel/integrator/shade_background.h
@@ -121,7 +121,7 @@ ccl_device_inline void integrate_background(KernelGlobals 
kg,
 
   /* multiple importance sampling, get background light pdf for ray
* direction, and compute weight with respect to BSDF pdf */
-  const float pdf = background_light_pdf(kg, ray_P, ray_D);
+  float pdf = background_light_pdf(kg, ray_P, ray_D);
   if (kernel_data.integrator.use_light_tree) {
 const float3 N = INTEGRATOR_STATE(state, path, mis_origin_n);
 pdf *= distant_lights_pdf(kg, ray_P, N, 
kernel_data.background.light_index);
diff --git a/intern/cycles/kernel/light/light_tree.h 
b/intern/cycles/kernel/light/light_tree.h
index efe3a746b6d..b265530e5e8 100644
--- a/intern/cycles/kernel/light/light_tree.h
+++ b/intern/cycles/kernel/light/light_tree.h
@@ -519,7 +519,8 @@ ccl_device float light_tree_pdf(KernelGlobals kg,
   /* We generate a random number to use for selecting a light. */
   RNGState rng_state;
   path_state_rng_load(state, &rng_state);
-  float randu = path_state_rng_1D_hash(kg, &rng_state, 0x6a21694c);
+  /* to-do: is this the correct macro to use? */
+  float randu = path_state_rng_1D(kg, &rng_state, PRNG_LIGHT); 
 
   /* We traverse to the leaf node and
* find the probability of selecting the target light. */

___
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
List details, subscription details or unsubscribe:
https://lists.blender.org/mailman/listinfo/bf-blender-cvs


[Bf-blender-cvs] [0484d5ca5b1] soc-2022-many-lights-sampling: Merge branch 'master' into soc-2022-many-lights-sampling

2022-09-03 Thread Jeffrey Liu
Commit: 0484d5ca5b17cece7f4cd55156a128c13de32bb5
Author: Jeffrey Liu
Date:   Sat Sep 3 17:10:34 2022 -0500
Branches: soc-2022-many-lights-sampling
https://developer.blender.org/rB0484d5ca5b17cece7f4cd55156a128c13de32bb5

Merge branch 'master' into soc-2022-many-lights-sampling

===



===

diff --cc intern/cycles/blender/sync.cpp
index 3e46e668d3a,fe16f19556e..02889c9c6a0
--- a/intern/cycles/blender/sync.cpp
+++ b/intern/cycles/blender/sync.cpp
@@@ -342,15 -342,8 +342,15 @@@ void BlenderSync::sync_integrator(BL::V
  
integrator->set_light_sampling_threshold(get_float(cscene, 
"light_sampling_threshold"));
  
 +  integrator->set_use_light_tree(get_boolean(cscene, "use_light_tree"));
 +  integrator->set_splitting_threshold(get_float(cscene, 
"splitting_threshold"));
 +
 +  if (integrator->use_light_tree_is_modified()) {
 +scene->light_manager->tag_update(scene, LightManager::UPDATE_ALL);
 +  }
 +
SamplingPattern sampling_pattern = (SamplingPattern)get_enum(
-   cscene, "sampling_pattern", SAMPLING_NUM_PATTERNS, 
SAMPLING_PATTERN_SOBOL);
+   cscene, "sampling_pattern", SAMPLING_NUM_PATTERNS, 
SAMPLING_PATTERN_PMJ);
integrator->set_sampling_pattern(sampling_pattern);
  
int samples = 1;
diff --cc intern/cycles/kernel/integrator/shade_background.h
index c5beda0a5ec,30ce0999258..041a8df8e9d
--- a/intern/cycles/kernel/integrator/shade_background.h
+++ b/intern/cycles/kernel/integrator/shade_background.h
@@@ -3,10 -3,11 +3,12 @@@
  
  #pragma once
  
- #include "kernel/film/accumulate.h"
- #include "kernel/integrator/shader_eval.h"
+ #include "kernel/film/light_passes.h"
+ 
+ #include "kernel/integrator/surface_shader.h"
+ 
  #include "kernel/light/light.h"
 +#include "kernel/light/light_tree.h"
  #include "kernel/light/sample.h"
  
  CCL_NAMESPACE_BEGIN
@@@ -122,13 -98,33 +99,37 @@@ ccl_device_inline void integrate_backgr
  #endif /* __MNEE__ */
  
/* Evaluate background shader. */
-   Spectrum L = (eval_background) ? integrator_eval_background_shader(kg, 
state, render_buffer) :
-zero_spectrum();
+   Spectrum L = zero_spectrum();
+ 
+   if (eval_background) {
+ L = integrator_eval_background_shader(kg, state, render_buffer);
+ 
+ /* When using the ao bounces approximation, adjust background
+  * shader intensity with ao factor. */
+ if (path_state_ao_bounce(kg, state)) {
+   L *= kernel_data.integrator.ao_bounces_factor;
+ }
  
-   /* When using the ao bounces approximation, adjust background
-* shader intensity with ao factor. */
-   if (path_state_ao_bounce(kg, state)) {
- L *= kernel_data.integrator.ao_bounces_factor;
+ /* Background MIS weights. */
+ float mis_weight = 1.0f;
+ /* Check if background light exists or if we should skip pdf. */
+ if (!(INTEGRATOR_STATE(state, path, flag) & PATH_RAY_MIS_SKIP) &&
+ kernel_data.background.use_mis) {
+   const float3 ray_P = INTEGRATOR_STATE(state, ray, P);
+   const float3 ray_D = INTEGRATOR_STATE(state, ray, D);
+   const float mis_ray_pdf = INTEGRATOR_STATE(state, path, mis_ray_pdf);
+ 
+   /* multiple importance sampling, get background light pdf for ray
+* direction, and compute weight with respect to BSDF pdf */
+   const float pdf = background_light_pdf(kg, ray_P, ray_D);
++  if (kernel_data.integrator.use_light_tree) {
++const float3 N = INTEGRATOR_STATE(state, path, mis_origin_n);
++pdf *= distant_lights_pdf(kg, ray_P, N, 
kernel_data.background.light_index);
++  }
+   mis_weight = light_sample_mis_weight_forward(kg, mis_ray_pdf, pdf);
+ }
+ 
+ L *= mis_weight;
}
  
/* Write to render buffer. */
@@@ -184,13 -181,7 +186,12 @@@ ccl_device_inline void integrate_distan
  /* multiple importance sampling, get regular light pdf,
   * and compute weight with respect to BSDF pdf */
  const float mis_ray_pdf = INTEGRATOR_STATE(state, path, mis_ray_pdf);
 -mis_weight = light_sample_mis_weight_forward(kg, mis_ray_pdf, ls.pdf);
 +if (kernel_data.integrator.use_light_tree) {
 +  const float3 ray_P = INTEGRATOR_STATE(state, ray, P);
 +  const float3 N = INTEGRATOR_STATE(state, path, mis_origin_n);
 +  ls.pdf *= distant_lights_pdf(kg, ray_P, N, lamp);
 +}
 +const float mis_weight = light_sample_mis_weight_forward(kg, 
mis_ray_pdf, ls.pdf);
- light_eval *= mis_weight;
}
  
/* Write to render buffer. */
diff --cc intern/cycles/kernel/integrator/shade_surface.h
index 0b32680c017,c19f56a9b70..62eaf6b3882
--- a/intern/cycles/kernel/integrator/shade_surface.h
+++ b/intern/cycles/kernel/integrator/shade_surface.h
@@@ -127,25 -125,13 +126,21 @@@ ccl_device_forceinline void integrate_s
  /* Multiple importance sampling, get triangle light pdf,
   * and c

[Bf-blender-cvs] [5f105939257] soc-2022-many-lights-sampling: Cycles: limit light tree traversal to 8 splits

2022-09-03 Thread Jeffrey Liu
Commit: 5f1059392575593169a57a0d193ac384a6a11782
Author: Jeffrey Liu
Date:   Sat Sep 3 16:37:17 2022 -0500
Branches: soc-2022-many-lights-sampling
https://developer.blender.org/rB5f1059392575593169a57a0d193ac384a6a11782

Cycles: limit light tree traversal to 8 splits

===

M   intern/cycles/kernel/light/light_tree.h

===

diff --git a/intern/cycles/kernel/light/light_tree.h 
b/intern/cycles/kernel/light/light_tree.h
index 55b0b43d1db..efe3a746b6d 100644
--- a/intern/cycles/kernel/light/light_tree.h
+++ b/intern/cycles/kernel/light/light_tree.h
@@ -287,6 +287,9 @@ ccl_device bool light_tree_sample(KernelGlobals kg,
   stack[0] = 0;
   pdfs[0] = 1.0f;
 
+  /* For now, we arbitrarily limit splitting to 8 so that it doesn't 
continuously split. */
+  int split_count = 0;
+
   /* First traverse the light tree until a leaf node is reached.
* Also keep track of the probability of traversing to a given node,
* so that we can scale our PDF accordingly later. */
@@ -337,12 +340,14 @@ ccl_device bool light_tree_sample(KernelGlobals kg,
 const int left_index = index + 1;
 const int right_index = knode->child_index;
 if (light_tree_should_split(kg, P, knode) &&
+split_count < 8 && 
 stack_index < stack_size - 1) {
   stack[stack_index] = left_index;
   pdfs[stack_index] = pdf;
   stack[stack_index + 1] = right_index;
   pdfs[stack_index + 1] = pdf;
   stack_index++;
+  split_count++;
   continue;
 }
 
@@ -525,6 +530,8 @@ ccl_device float light_tree_pdf(KernelGlobals kg,
   stack[0] = 0;
   pdfs[0] = 1.0f;
 
+  int split_count = 0;
+
   float light_tree_pdf = 0.0f;
   float light_leaf_pdf = 0.0f;
   float total_weight = 0.0f;
@@ -591,12 +598,15 @@ ccl_device float light_tree_pdf(KernelGlobals kg,
  * We adaptively split if the variance is high enough. */
 const int left_index = index + 1;
 const int right_index = knode->child_index;
-if (light_tree_should_split(kg, P, knode) && stack_index < stack_size - 1) 
{
+if (light_tree_should_split(kg, P, knode) &&
+split_count < 8 &&
+stack_index < stack_size - 1) {
   stack[stack_index] = left_index;
   pdfs[stack_index] = pdf;
   stack[stack_index + 1] = right_index;
   pdfs[stack_index + 1] = pdf;
   stack_index++;
+  split_count++;
   continue;
 }

___
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
List details, subscription details or unsubscribe:
https://lists.blender.org/mailman/listinfo/bf-blender-cvs


[Bf-blender-cvs] [f7b99208017] temp-angavrilov: Shrinkwrap: fix stability of the Target Normal Project mode.

2022-09-03 Thread Alexander Gavrilov
Commit: f7b99208017f90be0ba648c08eecc6ec96356790
Author: Alexander Gavrilov
Date:   Sat Sep 3 17:03:11 2022 +0300
Branches: temp-angavrilov
https://developer.blender.org/rBf7b99208017f90be0ba648c08eecc6ec96356790

Shrinkwrap: fix stability of the Target Normal Project mode.

This mode works by using an iterative process to solve a system
of equations for each triangle to find a point on its surface that
has the smooth normal pointing at the original point. If a point
within the triangle is not found, the next triangle is searched.

All instability with vertices jumping to the opposite side of
the mesh is caused by incorrectly discarding triangles for various
reasons when the solution is close to the triangle edge.

In order to optimize performance the old code was aggressively
aborting iteration when the local gradient at the edge was
pointing outside domain. However, it is wrong because it can be
caused by a sharp valley diagonal to the domain boundary with
the bottom gently sloping towards a minimum within the domain.

Now iteration is only aborted either if the solution deviates
nonsensically far from the domain, or if the gradient is proven
to slope towards the outside perpendicular to the boundary.
Until either condition is met, values are simply clamped to
the domain.

In addition, custom correction clearly has to be done after
the linear search phase of the iterative solver, because the
linear correction math assumes running after a normal Newton
method step, not some kind of custom clamping.

===

M   source/blender/blenkernel/intern/shrinkwrap.c
M   source/blender/blenlib/intern/math_solvers.c

===

diff --git a/source/blender/blenkernel/intern/shrinkwrap.c 
b/source/blender/blenkernel/intern/shrinkwrap.c
index 2593cdad533..755e4b8b32a 100644
--- a/source/blender/blenkernel/intern/shrinkwrap.c
+++ b/source/blender/blenkernel/intern/shrinkwrap.c
@@ -778,18 +778,30 @@ static void target_project_tri_jacobian(void *userdata, 
const float x[3], float
 }
 
 /* Clamp barycentric weights to the triangle. */
-static void target_project_tri_clamp(float x[3])
+static float target_project_tri_clamp(float x[3])
 {
+  float error = 0.0f;
+
   if (x[0] < 0.0f) {
+error = max_ff(error, -x[0]);
+
 x[0] = 0.0f;
   }
+
   if (x[1] < 0.0f) {
+error = max_ff(error, -x[1]);
+
 x[1] = 0.0f;
   }
+
   if (x[0] + x[1] > 1.0f) {
+error = max_ff(error, (x[0] + x[1] - 1.0f) * (float)M_SQRT1_2);
+
 x[0] = x[0] / (x[0] + x[1]);
 x[1] = 1.0f - x[0];
   }
+
+  return error;
 }
 
 /* Correct the Newton's method step to keep the coordinates within the 
triangle. */
@@ -798,71 +810,36 @@ static bool target_project_tri_correct(void 
*UNUSED(userdata),
float step[3],
float x_next[3])
 {
-  /* Insignificant correction threshold */
-  const float epsilon = 1e-5f;
-  /* Dot product threshold for checking if step is 'clearly' pointing outside. 
*/
-  const float dir_epsilon = 0.5f;
-  bool fixed = false, locked = false;
-
-  /* The barycentric coordinate domain is a triangle bounded by
-   * the X and Y axes, plus the x+y=1 diagonal. First, clamp the
-   * movement against the diagonal. Note that step is subtracted. */
-  float sum = x[0] + x[1];
-  float sstep = -(step[0] + step[1]);
-
-  if (sum + sstep > 1.0f) {
-float ldist = 1.0f - sum;
-
-/* If already at the boundary, slide along it. */
-if (ldist < epsilon * (float)M_SQRT2) {
-  float step_len = len_v2(step);
-
-  /* Abort if the solution is clearly outside the domain. */
-  if (step_len > epsilon && sstep > step_len * dir_epsilon * 
(float)M_SQRT2) {
-return false;
-  }
+  const float error = target_project_tri_clamp(x_next);
 
-  /* Project the new position onto the diagonal. */
-  add_v2_fl(step, (sum + sstep - 1.0f) * 0.5f);
-  fixed = locked = true;
-}
-else {
-  /* Scale a significant step down to arrive at the boundary. */
-  mul_v3_fl(step, ldist / sstep);
-  fixed = true;
-}
+  /* Immediately abort on a clearly wrong point.
+   *
+   * It is not appropriate to abort unless the value is extremely
+   * wrong, because if the goal function gradient forms a sharp
+   * valley with a gently sloping bottom, the iterative process may
+   * be dragged against the edge of the domain by local gradients
+   * for a number of steps even when the ultimate minimum is within
+   * the domain.
+   *
+   * This threshold basically represents an estimate of something so
+   * far outside the domain that all assumptions about the solution
+   * behavior start to break down.
+   */
+  if (error > 1.0f) {
+return false;
   }
 
-  /* Weight 0 and 1 boundary checks - along axis. */
-  for (int i = 0; i < 2; i++) {
-if (step[i] > x[i]) {
-  /* If already at the boundary, s

[Bf-blender-cvs] [4b818b15132] master: Merge branch 'blender-v3.3-release'

2022-09-03 Thread Philipp Oeser
Commit: 4b818b151329516df55b5d9a1fbcc21ec3b73e7a
Author: Philipp Oeser
Date:   Sat Sep 3 11:43:15 2022 +0200
Branches: master
https://developer.blender.org/rB4b818b151329516df55b5d9a1fbcc21ec3b73e7a

Merge branch 'blender-v3.3-release'

===



===

diff --cc source/blender/editors/interface/interface_ops.cc
index 75f8ed26829,000..a42e08c59d5
mode 100644,00..100644
--- a/source/blender/editors/interface/interface_ops.cc
+++ b/source/blender/editors/interface/interface_ops.cc
@@@ -1,2564 -1,0 +1,2569 @@@
 +/* SPDX-License-Identifier: GPL-2.0-or-later
 + * Copyright 2009 Blender Foundation. All rights reserved. */
 +
 +/** \file
 + * \ingroup edinterface
 + */
 +
 +#include 
 +
 +#include "MEM_guardedalloc.h"
 +
 +#include "DNA_armature_types.h"
 +#include "DNA_material_types.h"
 +#include "DNA_modifier_types.h" /* for handling geometry nodes properties */
 +#include "DNA_object_types.h"   /* for OB_DATA_SUPPORT_ID */
 +#include "DNA_screen_types.h"
 +#include "DNA_text_types.h"
 +
 +#include "BLI_blenlib.h"
 +#include "BLI_math_color.h"
 +
 +#include "BLF_api.h"
 +#include "BLT_lang.h"
 +#include "BLT_translation.h"
 +
 +#include "BKE_context.h"
 +#include "BKE_global.h"
 +#include "BKE_idprop.h"
 +#include "BKE_layer.h"
 +#include "BKE_lib_id.h"
 +#include "BKE_lib_override.h"
 +#include "BKE_lib_remap.h"
 +#include "BKE_material.h"
 +#include "BKE_node.h"
 +#include "BKE_report.h"
 +#include "BKE_screen.h"
 +#include "BKE_text.h"
 +
 +#include "IMB_colormanagement.h"
 +
 +#include "DEG_depsgraph.h"
 +
 +#include "RNA_access.h"
 +#include "RNA_define.h"
 +#include "RNA_path.h"
 +#include "RNA_prototypes.h"
 +#include "RNA_types.h"
 +
 +#include "UI_interface.h"
 +
 +#include "interface_intern.h"
 +
 +#include "WM_api.h"
 +#include "WM_types.h"
 +
 +#include "ED_object.h"
 +#include "ED_paint.h"
 +
 +/* for Copy As Driver */
 +#include "ED_keyframing.h"
 +
 +/* only for UI_OT_editsource */
 +#include "BKE_main.h"
 +#include "BLI_ghash.h"
 +#include "ED_screen.h"
 +#include "ED_text.h"
 +
 +/*  */
 +/** \name Immediate redraw helper
 + *
 + * Generally handlers shouldn't do any redrawing, that includes the 
layout/button definitions. That
 + * violates the Model-View-Controller pattern.
 + *
 + * But there are some operators which really need to re-run the layout 
definitions for various
 + * reasons. For example, "Edit Source" does it to find out which exact Python 
code added a button.
 + * Other operators may need to access buttons that aren't currently visible. 
In Blender's UI code
 + * design that typically means just not adding the button in the first place, 
for a particular
 + * redraw. So the operator needs to change context and re-create the layout, 
so the button becomes
 + * available to act on.
 + *
 + * \{ */
 +
 +static void ui_region_redraw_immediately(bContext *C, ARegion *region)
 +{
 +  ED_region_do_layout(C, region);
 +  WM_draw_region_viewport_bind(region);
 +  ED_region_do_draw(C, region);
 +  WM_draw_region_viewport_unbind(region);
 +  region->do_draw = false;
 +}
 +
 +/** \} */
 +
 +/*  */
 +/** \name Copy Data Path Operator
 + * \{ */
 +
 +static bool copy_data_path_button_poll(bContext *C)
 +{
 +  PointerRNA ptr;
 +  PropertyRNA *prop;
 +  char *path;
 +  int index;
 +
 +  UI_context_active_but_prop_get(C, &ptr, &prop, &index);
 +
 +  if (ptr.owner_id && ptr.data && prop) {
 +path = RNA_path_from_ID_to_property(&ptr, prop);
 +
 +if (path) {
 +  MEM_freeN(path);
 +  return true;
 +}
 +  }
 +
 +  return false;
 +}
 +
 +static int copy_data_path_button_exec(bContext *C, wmOperator *op)
 +{
 +  Main *bmain = CTX_data_main(C);
 +  PointerRNA ptr;
 +  PropertyRNA *prop;
 +  char *path;
 +  int index;
 +  ID *id;
 +
 +  const bool full_path = RNA_boolean_get(op->ptr, "full_path");
 +
 +  /* try to create driver using property retrieved from UI */
 +  UI_context_active_but_prop_get(C, &ptr, &prop, &index);
 +
 +  if (ptr.owner_id != nullptr) {
 +if (full_path) {
 +  if (prop) {
 +path = RNA_path_full_property_py_ex(bmain, &ptr, prop, index, true);
 +  }
 +  else {
 +path = RNA_path_full_struct_py(bmain, &ptr);
 +  }
 +}
 +else {
 +  path = RNA_path_from_real_ID_to_property_index(bmain, &ptr, prop, 0, 
-1, &id);
 +
 +  if (!path) {
 +path = RNA_path_from_ID_to_property(&ptr, prop);
 +  }
 +}
 +
 +if (path) {
 +  WM_clipboard_text_set(path, false);
 +  MEM_freeN(path);
 +  return OPERATOR_FINISHED;
 +}
 +  }
 +
 +  return OPERATOR_CANCELLED;
 +}
 +
 +static void UI_OT_copy_data_path_button(wmOperatorType *ot)
 +{
 +  PropertyRNA *prop;
 +
 +  /* identifiers */
 +  ot->name = "Copy Data Path";
 +  ot->idname = "UI_OT

[Bf-blender-cvs] [a50ca6a1cd7] blender-v3.3-release: Fix T100687: Geometry Attribute Convert crashes in sculpt mode

2022-09-03 Thread Philipp Oeser
Commit: a50ca6a1cd72aa0556d74dd4a54de25bf2eeadcb
Author: Philipp Oeser
Date:   Thu Sep 1 13:55:20 2022 +0200
Branches: blender-v3.3-release
https://developer.blender.org/rBa50ca6a1cd72aa0556d74dd4a54de25bf2eeadcb

Fix T100687: Geometry Attribute Convert crashes in sculpt mode

Since above commit, `BKE_id_attributes_active_get` would also return
"internal" attributes like ".hide_poly" or ".hide_vert".
As a consequence, a couple of poll functions dont return false anymore
(attribute remove, attribute convert), allowing these operators to
execute, but acting on this "internal" layers is just asking for
trouble.

In the UI, we dont see these attributes, because `MESH_UL_attributes`
checks `is_internal`, same thing we do now in
`BKE_id_attributes_active_get`.

Maniphest Tasks: T100687

Differential Revision: https://developer.blender.org/D15833

===

M   source/blender/blenkernel/intern/attribute.cc

===

diff --git a/source/blender/blenkernel/intern/attribute.cc 
b/source/blender/blenkernel/intern/attribute.cc
index 495a2c82332..e9593b49047 100644
--- a/source/blender/blenkernel/intern/attribute.cc
+++ b/source/blender/blenkernel/intern/attribute.cc
@@ -473,7 +473,7 @@ CustomDataLayer *BKE_id_attributes_active_get(ID *id)
   for (int i = 0; i < customdata->totlayer; i++) {
 CustomDataLayer *layer = &customdata->layers[i];
 if (CD_MASK_PROP_ALL & CD_TYPE_AS_MASK(layer->type)) {
-  if (index == active_index) {
+  if (index == active_index && 
BKE_attribute_allow_procedural_access(layer->name)) {
 return layer;
   }
   index++;

___
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
List details, subscription details or unsubscribe:
https://lists.blender.org/mailman/listinfo/bf-blender-cvs


[Bf-blender-cvs] [a631dc55756] blender-v3.3-release: Fix T100731: Keymap Editor context menu crash

2022-09-03 Thread Philipp Oeser
Commit: a631dc55756b9c1a9835bf5b06f5d57f388f1277
Author: Philipp Oeser
Date:   Thu Sep 1 10:52:15 2022 +0200
Branches: blender-v3.3-release
https://developer.blender.org/rBa631dc55756b9c1a9835bf5b06f5d57f388f1277

Fix T100731: Keymap Editor context menu crash

Caused by {rB3f3d82cfe9ce}

Since above commit, a `uiRNACollectionSearch` may contain a NULL
`search_prop`, crashing the menu entry for "Jump To Target"
(`ui_jump_to_target_button_poll`).

Now safeguard against this with a NULL check (getting search callbacks
to work for "Jump To Target" can be investigated in master).

Maniphest Tasks: T100731

Differential Revision: https://developer.blender.org/D15832

===

M   source/blender/editors/interface/interface_ops.c

===

diff --git a/source/blender/editors/interface/interface_ops.c 
b/source/blender/editors/interface/interface_ops.c
index 865aed01aa1..7a7496fb071 100644
--- a/source/blender/editors/interface/interface_ops.c
+++ b/source/blender/editors/interface/interface_ops.c
@@ -1566,8 +1566,13 @@ static bool jump_to_target_button(bContext *C, bool poll)
 char str_buf[MAXBONENAME];
 char *str_ptr = RNA_property_string_get_alloc(&ptr, prop, str_buf, 
sizeof(str_buf), NULL);
 
-int found = RNA_property_collection_lookup_string(
-&coll_search->search_ptr, coll_search->search_prop, str_ptr, 
&target_ptr);
+int found = 0;
+/* Jump to target only works with search properties currently, not 
search callbacks yet.
+ * See ui_but_add_search. */
+if (coll_search->search_prop != NULL) {
+  found = RNA_property_collection_lookup_string(
+  &coll_search->search_ptr, coll_search->search_prop, str_ptr, 
&target_ptr);
+}
 
 if (str_ptr != str_buf) {
   MEM_freeN(str_ptr);

___
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
List details, subscription details or unsubscribe:
https://lists.blender.org/mailman/listinfo/bf-blender-cvs