Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MOD_nodes.cc « intern « modifiers « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6d7fe559eab2412df169f7da2fe1229af5064a0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * 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 by the Blender Foundation.
 * All rights reserved.
 */

/** \file
 * \ingroup modifiers
 */

#include <cstring>
#include <iostream>
#include <string>

#include "MEM_guardedalloc.h"

#include "BLI_float3.hh"
#include "BLI_listbase.h"
#include "BLI_string.h"
#include "BLI_utildefines.h"

#include "DNA_defaults.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_modifier_types.h"
#include "DNA_node_types.h"
#include "DNA_object_types.h"
#include "DNA_pointcloud_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"

#include "BKE_customdata.h"
#include "BKE_lib_query.h"
#include "BKE_mesh.h"
#include "BKE_modifier.h"
#include "BKE_pointcloud.h"
#include "BKE_screen.h"
#include "BKE_simulation.h"

#include "BLO_read_write.h"

#include "UI_interface.h"
#include "UI_resources.h"

#include "RNA_access.h"

#include "DEG_depsgraph_build.h"
#include "DEG_depsgraph_query.h"

#include "MOD_modifiertypes.h"
#include "MOD_ui_common.h"

#include "NOD_derived_node_tree.hh"
#include "NOD_geometry_exec.hh"
#include "NOD_node_tree_multi_function.hh"
#include "NOD_type_callbacks.hh"

using blender::float3;

static void initData(ModifierData *md)
{
  NodesModifierData *nmd = (NodesModifierData *)md;

  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(nmd, modifier));

  MEMCPY_STRUCT_AFTER(nmd, DNA_struct_default_get(NodesModifierData), modifier);
}

static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
{
  NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
  if (nmd->node_group != nullptr) {
    DEG_add_node_tree_relation(ctx->node, nmd->node_group, "Nodes Modifier");
  }
}

static void foreachIDLink(ModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
{
  NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
  walk(userData, ob, (ID **)&nmd->node_group, IDWALK_CB_USER);
}

static bool isDisabled(const struct Scene *UNUSED(scene),
                       ModifierData *md,
                       bool UNUSED(useRenderParams))
{
  NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
  UNUSED_VARS(nmd);
  return false;
}

static PointCloud *modifyPointCloud(ModifierData *md,
                                    const ModifierEvalContext *UNUSED(ctx),
                                    PointCloud *pointcloud)
{
  NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
  UNUSED_VARS(nmd);
  std::cout << __func__ << "\n";
  return pointcloud;
}

/* To be replaced soon. */
using namespace blender;
using namespace blender::nodes;
using namespace blender::fn;
using namespace blender::bke;

class GeometryNodesEvaluator {
 private:
  LinearAllocator<> allocator_;
  Map<const DInputSocket *, GMutablePointer> value_by_input_;
  Vector<const DInputSocket *> group_outputs_;
  MultiFunctionByNode &mf_by_node_;
  const DataTypeConversions &conversions_;

 public:
  GeometryNodesEvaluator(const Map<const DOutputSocket *, GMutablePointer> &group_input_data,
                         Vector<const DInputSocket *> group_outputs,
                         MultiFunctionByNode &mf_by_node)
      : group_outputs_(std::move(group_outputs)),
        mf_by_node_(mf_by_node),
        conversions_(get_implicit_type_conversions())
  {
    for (auto item : group_input_data.items()) {
      this->forward_to_inputs(*item.key, item.value);
    }
  }

  Vector<GMutablePointer> execute()
  {
    Vector<GMutablePointer> results;
    for (const DInputSocket *group_output : group_outputs_) {
      GMutablePointer result = this->get_input_value(*group_output);
      results.append(result);
    }
    for (GMutablePointer value : value_by_input_.values()) {
      value.destruct();
    }
    return results;
  }

 private:
  GMutablePointer get_input_value(const DInputSocket &socket_to_compute)
  {
    std::optional<GMutablePointer> value = value_by_input_.pop_try(&socket_to_compute);
    if (value.has_value()) {
      /* This input has been computed before, return it directly. */
      return *value;
    }

    Span<const DOutputSocket *> from_sockets = socket_to_compute.linked_sockets();
    Span<const DGroupInput *> from_group_inputs = socket_to_compute.linked_group_inputs();
    const int total_inputs = from_sockets.size() + from_group_inputs.size();
    BLI_assert(total_inputs <= 1);

    const CPPType &type = *socket_cpp_type_get(*socket_to_compute.typeinfo());

    if (total_inputs == 0) {
      /* The input is not connected, use the value from the socket itself. */
      bNodeSocket &bsocket = *socket_to_compute.bsocket();
      void *buffer = allocator_.allocate(type.size(), type.alignment());
      socket_cpp_value_get(bsocket, buffer);
      return GMutablePointer{type, buffer};
    }
    if (from_group_inputs.size() == 1) {
      /* The input gets its value from the input of a group that is not further connected. */
      bNodeSocket &bsocket = *from_group_inputs[0]->bsocket();
      void *buffer = allocator_.allocate(type.size(), type.alignment());
      socket_cpp_value_get(bsocket, buffer);
      return GMutablePointer{type, buffer};
    }

    /* Compute the socket now. */
    const DOutputSocket &from_socket = *from_sockets[0];
    this->compute_output_and_forward(from_socket);
    return value_by_input_.pop(&socket_to_compute);
  }

  void compute_output_and_forward(const DOutputSocket &socket_to_compute)
  {
    const DNode &node = socket_to_compute.node();

    /* Prepare inputs required to execute the node. */
    GValueByName node_inputs{allocator_};
    for (const DInputSocket *input_socket : node.inputs()) {
      if (input_socket->is_available()) {
        GMutablePointer value = this->get_input_value(*input_socket);
        node_inputs.transfer_ownership_in(input_socket->identifier(), value);
      }
    }

    /* Execute the node. */
    GValueByName node_outputs{allocator_};
    this->execute_node(node, node_inputs, node_outputs);

    /* Forward computed outputs to linked input sockets. */
    for (const DOutputSocket *output_socket : node.outputs()) {
      if (output_socket->is_available()) {
        GMutablePointer value = node_outputs.extract(output_socket->identifier());
        this->forward_to_inputs(*output_socket, value);
      }
    }
  }

  void execute_node(const DNode &node, GValueByName &node_inputs, GValueByName &node_outputs)
  {
    bNode *bnode = node.bnode();
    if (bnode->typeinfo->geometry_node_execute != nullptr) {
      bnode->typeinfo->geometry_node_execute(bnode, node_inputs, node_outputs);
      return;
    }

    /* Use the multi-function implementation of the node. */
    const MultiFunction &fn = *mf_by_node_.lookup(&node);
    MFContextBuilder context;
    MFParamsBuilder params{fn, 1};
    Vector<GMutablePointer> input_data;
    for (const DInputSocket *dsocket : node.inputs()) {
      if (dsocket->is_available()) {
        GMutablePointer data = node_inputs.extract(dsocket->identifier());
        params.add_readonly_single_input(GSpan(*data.type(), data.get(), 1));
        input_data.append(data);
      }
    }
    Vector<GMutablePointer> output_data;
    for (const DOutputSocket *dsocket : node.outputs()) {
      if (dsocket->is_available()) {
        const CPPType &type = *socket_cpp_type_get(*dsocket->typeinfo());
        void *buffer = allocator_.allocate(type.size(), type.alignment());
        params.add_uninitialized_single_output(GMutableSpan(type, buffer, 1));
        output_data.append(GMutablePointer(type, buffer));
      }
    }
    fn.call(IndexRange(1), params, context);
    for (GMutablePointer value : input_data) {
      value.destruct();
    }
    for (const int i : node.outputs().index_range()) {
      GMutablePointer value = output_data[i];
      node_outputs.move_in(node.output(i).identifier(), value);
      value.destruct();
    }
  }

  void forward_to_inputs(const DOutputSocket &from_socket, GMutablePointer value_to_forward)
  {
    Span<const DInputSocket *> linked_sockets = from_socket.linked_sockets();

    Vector<const DInputSocket *> to_sockets_with_same_type;
    Vector<const DInputSocket *> to_sockets_with_different_type;
    for (const DInputSocket *linked_socket : linked_sockets) {
      if (from_socket.typeinfo() == linked_socket->typeinfo()) {
        to_sockets_with_same_type.append(linked_socket);
      }
      else {
        to_sockets_with_different_type.append(linked_socket);
      }
    }

    const CPPType &from_type = *value_to_forward.type();

    for (const DInputSocket *to_socket : to_sockets_with_different_type) {
      const CPPType &to_type = *socket_cpp_type_get(*to_socket->typeinfo());
      void *buffer = allocator_.allocate(to_type.size(), to_type.alignment());
      conversions_.convert(from_type, to_type, value_to_forward.get(), buffer);
      value_by_input_.add_new(to_socket, GMutablePointer{to_type, buffer});
    }

    if (to_sockets_with_same_type.size() == 0) {
      /* This value is not further used, so destruct it. */
      value_to_forward.destruct();
    }
    else if (to_sockets_with_same_type.size() == 1) {
      /* This value is only used on one input socket, no need to copy it. */
      const DInputSocket *to_socket = to_sockets_with_same_type[0];
      value_by_input_.add_new(to_socket, value_to_forward);
    }
    else {
      /* Multiple inputs use the value, make a copy for every input except for one. */
      const DInputSocket *first_to_socket = to_sockets_with_same_type[0];
      Span<const DInputSocket *> other_to_sockets = to_sockets_with_same_type.as_span().drop_front(
          1);
      const CPPType &type = *value_to_forward.type();

      value_by_input_.add_new(first_to_socket, value_to_forward);
      for (const DInputSocket *to_socket : other_to_sockets) {
        void *buffer = allocator_.allocate(type.size(), type.alignment());
        type.copy_to_uninitialized(value_to_forward.get(), buffer);
        value_by_input_.add_new(to_socket, GMutablePointer{type, buffer});
      }
    }
  }
};

/**
 * Evaluate a node group to compute the output geometry.
 * Currently, this uses a fairly basic and inefficient algorithm that might compute things more
 * often than necessary. It's going to be replaced soon.
 */
static GeometryPtr compute_geometry(const DerivedNodeTree &tree,
                                    const DOutputSocket *group_input,
                                    GeometryPtr group_input_geometry,
                                    const DInputSocket &socket_to_compute)
{
  ResourceCollector resources;
  MultiFunctionByNode mf_by_node = get_multi_function_per_node(tree, resources);

  /* Use this buffer so that it is not destructed when the scope ends. The evaluator is responsible
   * for destructing it. */
  TypedBuffer<GeometryPtr> buffer;
  new (buffer.ptr()) GeometryPtr(std::move(group_input_geometry));

  Map<const DOutputSocket *, GMutablePointer> group_inputs;
  group_inputs.add_new(group_input, GMutablePointer{buffer.ptr()});

  Vector<const DInputSocket *> group_outputs;
  group_outputs.append(&socket_to_compute);

  GeometryNodesEvaluator evaluator{group_inputs, group_outputs, mf_by_node};
  Vector<GMutablePointer> results = evaluator.execute();
  BLI_assert(results.size() == 1);
  GMutablePointer result = results[0];

  GeometryPtr output_geometry = std::move(*(GeometryPtr *)result.get());
  return output_geometry;
}

static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *UNUSED(ctx), Mesh *mesh)
{
  NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
  if (nmd->node_group == nullptr) {
    return mesh;
  }

  NodeTreeRefMap tree_refs;
  DerivedNodeTree tree{nmd->node_group, tree_refs};
  ResourceCollector resources;

  Span<const DNode *> input_nodes = tree.nodes_by_type("NodeGroupInput");
  Span<const DNode *> output_nodes = tree.nodes_by_type("NodeGroupOutput");

  if (input_nodes.size() > 1) {
    return mesh;
  }
  if (output_nodes.size() != 1) {
    return mesh;
  }

  Span<const DOutputSocket *> group_inputs = (input_nodes.size() == 1) ?
                                                 input_nodes[0]->outputs().drop_back(1) :
                                                 Span<const DOutputSocket *>{};
  Span<const DInputSocket *> group_outputs = output_nodes[0]->inputs().drop_back(1);

  if (group_outputs.size() != 1) {
    return mesh;
  }

  const DInputSocket *group_output = group_outputs[0];
  if (group_output->idname() != "NodeSocketGeometry") {
    return mesh;
  }

  GeometryPtr input_geometry{new Geometry()};
  input_geometry->mesh_set_and_keep_ownership(mesh);

  GeometryPtr new_geometry = compute_geometry(
      tree, group_inputs[0], std::move(input_geometry), *group_outputs[0]);
  make_geometry_mutable(new_geometry);
  Mesh *new_mesh = new_geometry->mesh_release();
  if (new_mesh == nullptr) {
    return BKE_mesh_new_nomain(0, 0, 0, 0, 0);
  }
  return new_mesh;
}

static void panel_draw(const bContext *UNUSED(C), Panel *panel)
{
  uiLayout *layout = panel->layout;

  PointerRNA ob_ptr;
  PointerRNA *ptr = modifier_panel_get_property_pointers(panel, &ob_ptr);

  uiLayoutSetPropSep(layout, true);
  uiLayoutSetPropDecorate(layout, false);

  uiItemR(layout, ptr, "node_group", 0, NULL, ICON_MESH_DATA);

  modifier_panel_end(layout, ptr);
}

static void panelRegister(ARegionType *region_type)
{
  modifier_panel_register(region_type, eModifierType_Nodes, panel_draw);
}

static void blendWrite(BlendWriter *writer, const ModifierData *md)
{
  const NodesModifierData *nmd = reinterpret_cast<const NodesModifierData *>(md);
  UNUSED_VARS(nmd, writer);
}

static void blendRead(BlendDataReader *reader, ModifierData *md)
{
  NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
  UNUSED_VARS(nmd, reader);
}

static void copyData(const ModifierData *md, ModifierData *target, const int flag)
{
  const NodesModifierData *nmd = reinterpret_cast<const NodesModifierData *>(md);
  NodesModifierData *tnmd = reinterpret_cast<NodesModifierData *>(target);
  UNUSED_VARS(nmd, tnmd);

  BKE_modifier_copydata_generic(md, target, flag);
}

static void freeData(ModifierData *md)
{
  NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
  UNUSED_VARS(nmd);
}

ModifierTypeInfo modifierType_Nodes = {
    /* name */ "Nodes",
    /* structName */ "NodesModifierData",
    /* structSize */ sizeof(NodesModifierData),
#ifdef WITH_GEOMETRY_NODES
    /* srna */ &RNA_NodesModifier,
#else
    /* srna */ &RNA_Modifier,
#endif
    /* type */ eModifierTypeType_Constructive,
    /* flags */ eModifierTypeFlag_AcceptsMesh,
    /* icon */ ICON_MESH_DATA, /* TODO: Use correct icon. */

    /* copyData */ copyData,

    /* deformVerts */ NULL,
    /* deformMatrices */ NULL,
    /* deformVertsEM */ NULL,
    /* deformMatricesEM */ NULL,
    /* modifyMesh */ modifyMesh,
    /* modifyHair */ NULL,
    /* modifyPointCloud */ modifyPointCloud,
    /* modifyVolume */ NULL,

    /* initData */ initData,
    /* requiredDataMask */ NULL,
    /* freeData */ freeData,
    /* isDisabled */ isDisabled,
    /* updateDepsgraph */ updateDepsgraph,
    /* dependsOnTime */ NULL,
    /* dependsOnNormals */ NULL,
    /* foreachIDLink */ foreachIDLink,
    /* foreachTexLink */ NULL,
    /* freeRuntimeData */ NULL,
    /* panelRegister */ panelRegister,
    /* blendWrite */ blendWrite,
    /* blendRead */ blendRead,
};