Loading...
Searching...
No Matches
node.h
Go to the documentation of this file.
1//
2// Copyright 2025 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_EXEC_VDF_NODE_H
8#define PXR_EXEC_VDF_NODE_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/exec/vdf/api.h"
17#include "pxr/exec/vdf/input.h"
19#include "pxr/exec/vdf/mask.h"
20#include "pxr/exec/vdf/output.h"
22#include "pxr/exec/vdf/types.h"
23
28#include "pxr/base/tf/token.h"
29
30#include <functional>
31#include <string>
32#include <typeinfo>
33#include <type_traits>
34#include <utility>
35#include <vector>
36
37PXR_NAMESPACE_OPEN_SCOPE
38
39class VdfContext;
41class VdfMaskedOutput;
42class VdfNetwork;
44class VdfVector;
45
52class VDF_API_TYPE VdfNode
53{
54private:
55
56 // Map of tokens to output connectors.
57 //
59
60 // Map of tokens to input connectors.
61 //
63
64public:
65
66 TF_MALLOC_TAG_NEW("Vdf", "new VdfNode");
67
68 // This little class allows clients to use TF_FOR_ALL to iterate
69 // through the input connectors.
70 //
71 class InputMapIterator {
72 public:
73 typedef _TokenInputMap::const_iterator const_iterator;
74 typedef _TokenInputMap::const_reverse_iterator const_reverse_iterator;
75
76 InputMapIterator(const _TokenInputMap &map) : _map(map) {}
77 const_iterator begin() const { return _map.begin(); }
78 const_iterator end() const { return _map.end(); }
79 const_reverse_iterator rbegin() const { return _map.rbegin(); }
80 const_reverse_iterator rend() const { return _map.rend(); }
81
82 private:
83 const _TokenInputMap &_map;
84 };
85
86 // This little class allows clients to use TF_FOR_ALL to iterate
87 // through the output connectors.
88 //
89 class OutputMapIterator {
90 public:
91 typedef _TokenOutputMap::const_iterator const_iterator;
92 typedef _TokenOutputMap::const_reverse_iterator const_reverse_iterator;
93
94 OutputMapIterator(const _TokenOutputMap &map) : _map(map) {}
95 const_iterator begin() const { return _map.begin(); }
96 const_iterator end() const { return _map.end(); }
97 const_reverse_iterator rbegin() const { return _map.rbegin(); }
98 const_reverse_iterator rend() const { return _map.rend(); }
99
100 private:
101 const _TokenOutputMap &_map;
102 };
103
104
108 VDF_API
110 const VdfInputSpecs &inputSpecs,
111 const VdfOutputSpecs &outputSpecs);
112
116 VdfId GetId() const { return _id; }
117
123 static VdfIndex GetIndexFromId(const VdfId id) {
124 return static_cast<VdfIndex>(id);
125 }
126
133 return static_cast<VdfVersion>(id >> 32);
134 }
135
138 const VdfNetwork &GetNetwork() const { return _network; }
139
142 VdfNetwork &GetNetwork() { return _network; }
143
146 template<typename TYPE>
147 bool IsA() const
148 {
149 return dynamic_cast<const TYPE *>(this);
150 }
151
154
158 return _specs->GetInputSpecs();
159 }
160
164 const VdfInput *GetInput(const TfToken &inputName) const {
165 return const_cast<VdfNode *>(this)->GetInput(inputName);
166 }
167
171 VDF_API
172 VdfInput *GetInput(const TfToken &inputName);
173
177 const InputMapIterator GetInputsIterator() const {
178 return InputMapIterator(_inputs);
179 }
180
183 bool HasInputConnections() const {
184 TF_FOR_ALL(i, _inputs) {
185 if (i->second->GetNumConnections())
186 return true;
187 }
188 return false;
189 }
190
193 bool HasOutputConnections() const {
194 TF_FOR_ALL(output, _outputs) {
195 if (output->second->GetConnections().size())
196 return true;
197 }
198 return false;
199 }
200
203 VDF_API
205
208 VDF_API
210
212
213
216
220 return _specs->GetOutputSpecs();
221 }
222
227 const VdfOutput *GetOutput(const TfToken &name) const {
228 return const_cast<VdfNode *>(this)->GetOutput(name);
229 }
230
235 VDF_API
237
242 VDF_API
244
249 const VdfOutput *GetOptionalOutput(const TfToken &name) const {
250 return const_cast<VdfNode *>(this)->GetOptionalOutput(name);
251 }
252
260 const VdfOutput *GetOutput() const {
261 return const_cast<VdfNode *>(this)->GetOutput();
262 }
263
271 VDF_API
273
277 const OutputMapIterator GetOutputsIterator() const {
278 return OutputMapIterator(_outputs);
279 }
280
283 size_t GetNumOutputs() const {
284 return _outputs.size();
285 }
286
289 size_t GetNumInputs() const {
290 return _inputs.size();
291 }
292
294
295
298
304 VDF_API
305 void SetDebugName(const std::string &name);
306
312 template <class F>
314 TfAutoMallocTag tag("Vdf", __ARCH_PRETTY_FUNCTION__);
315 SetDebugNameCallback(VdfNodeDebugNameCallback(std::forward<F>(f)));
316 }
317
325 VDF_API
327
331 VDF_API
332 const std::string GetDebugName() const;
333
339 VDF_API
340 virtual size_t GetMemoryUsage() const;
341
343
344
347
353 virtual void Compute(const VdfContext &context) const = 0;
354
359 VDF_API
360 virtual bool IsSpeculationNode() const;
361
369 VDF_API
371 const VdfContext &context) const;
372
386 VDF_API
388 const VdfConnection &inputConnection,
389 const VdfMask &inputDependencyMask,
390 const VdfOutput &output) const;
391
396 VDF_API
398 const VdfConnection &inputConnection,
399 const VdfMask &inputDependencyMask,
400 VdfMaskedOutputVector *outputDependencies) const;
401
417 VDF_API
419 const VdfMaskedOutput &maskedOutput,
420 const VdfConnection &inputConnection) const;
421
427 VDF_API
429 const VdfMaskedOutput &maskedOutput,
430 bool skipAssociatedInputs) const;
431
436 VDF_API
438 const VdfMaskedOutputVector &request) const;
439
441
442
445 VDF_API
446 bool IsEqual(const VdfNode &rhs) const;
447
448protected:
449
458 VDF_API
460
466 VDF_API
468 const VdfInputSpecs &inputSpecs,
469 std::vector<VdfInput*> *resultingInputs = NULL);
470
476 VDF_API
478 const VdfOutputSpecs &outputSpecs,
479 std::vector<VdfOutput*> *resultingOutputs = NULL);
480
487 VDF_API
488 virtual bool _IsDerivedEqual(const VdfNode &rhs) const;
489
501 VDF_API
502 virtual void _DidAddInputConnection(const VdfConnection *c, int atIndex);
503
510 VDF_API
512
513 // The network is the only entity allowed to delete a node and set a node's
514 // id.
515 friend class VdfNetwork;
516
521 VDF_API
522 virtual ~VdfNode();
523
526 void _SetId(const VdfVersion version, const VdfIndex index) {
527 _id = (static_cast<VdfId>(version) << 32) | static_cast<VdfId>(index);
528 }
529
532
538 VDF_API
540 const VdfInputSpecs &inputSpecs,
541 const VdfOutputSpecs &outputSpecs);
542
549 VDF_API
551 const VdfInputAndOutputSpecs *specs);
552
558 VDF_API
560 const VdfInputAndOutputSpecs *specs);
561
568 VDF_API
570
574 VDF_API
575 void _ReplaceInputSpecs(const VdfInputSpecs &inputSpecs);
576
578
586 template <typename BaseClassType, typename ClassType>
587 static size_t _GetMemoryUsage(const ClassType &c, size_t dynamicSize)
588 {
589 return c.BaseClassType::GetMemoryUsage() +
590 (sizeof(ClassType) - sizeof(BaseClassType)) +
591 dynamicSize;
592 }
593
614 VDF_API
616 const VdfConnection &inputConnection,
617 const VdfMask &inputDependencyMask,
618 const VdfOutput &output) const;
619
627 VDF_API
629 const VdfConnection &inputConnection,
630 const VdfMask &inputDependencyMask,
631 VdfMaskedOutputVector *outputDependencies) const;
632
639 VDF_API
641 const VdfMaskedOutputVector &request) const;
642
663 VDF_API
665 const VdfMaskedOutput &maskedOutput,
666 const VdfConnection &inputConnection) const;
667
676 VDF_API
678 const VdfMaskedOutput &maskedOutput,
679 bool skipAssociatedInputs) const;
680
681private:
682
683 // This is the network to which the node belongs.
684 // XXX:memory
685 // We may want to come up with a better scheme than adding a back pointer
686 // per-node!
687 VdfNetwork &_network;
688
689 // This is the unique id of this node in its network.
690 VdfId _id;
691
692 // This object holds on to the specs of the input and output connectors.
693 // This object is never owned by VdfNode. In the base implementation,
694 // this object is owned by Vdf_InputAndOutputSpecsRegistry and must be
695 // acquired and released through this object. Derived classes may provide
696 // their own management of this pointer.
697 const VdfInputAndOutputSpecs *_specs;
698
699 // The list of inputs. _inputs[i] gives you all the links into
700 // the i-th input of this node.
701 _TokenInputMap _inputs;
702
703 // The list of outputs. _outputs[name] gives you all the links out of
704 // the output connector of this node with the label name.
705 //
706 // We require _outputs.size() be constant time.
707 _TokenOutputMap _outputs;
708};
709
710template <>
711struct Tf_ShouldIterateOverCopy<VdfNode::InputMapIterator> : std::true_type {};
712template <>
713struct Tf_ShouldIterateOverCopy<VdfNode::OutputMapIterator> : std::true_type {};
714
716
717PXR_NAMESPACE_CLOSE_SCOPE
718
719#endif
A simple iterator adapter for STL containers.
Fast, compressed bit array which is capable of performing logical operations without first decompress...
Scoped (i.e.
Definition: mallocTag.h:249
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
A class that fully represents a connection between two VdfNodes.
Definition: connection.h:30
A context is the parameter bundle passed to callbacks of computations.
Definition: context.h:40
Abstract base class for classes that execute a VdfNetwork to compute a requested set of values.
Hashable holder of a VdfInputSpec and VdfOutputSpec.
A VdfInput is used to connect a VdfNode to one or more VdfNodes' outputs.
Definition: input.h:36
VdfInputSpecs is a container for VdfInputSpec objects.
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:37
Class to hold on to an externally owned output and a mask.
Definition: maskedOutput.h:32
A VdfNetwork is a collection of VdfNodes and their connections.
Definition: network.h:60
This is the base class for all nodes in a VdfNetwork.
Definition: node.h:53
virtual VDF_API bool _ComputeOutputDependencyMasks(const VdfConnection &inputConnection, const VdfMask &inputDependencyMask, VdfMaskedOutputVector *outputDependencies) const
Vectorized version of _ComputeOutputDependencyMask.
VDF_API VdfConnectionVector GetInputConnections() const
Returns a flat vector of all input connections.
void _SetId(const VdfVersion version, const VdfIndex index)
Sets the node id.
Definition: node.h:526
const InputMapIterator GetInputsIterator() const
Returns an iterator class that can be used with TF_FOR_ALL to iterate through the inputs.
Definition: node.h:177
void SetDebugNameCallback(F &&f)
Sets the debug name for this node with a lazily invoked callback.
Definition: node.h:313
virtual VDF_API VdfConnectionAndMaskVector _ComputeInputDependencyMasks(const VdfMaskedOutput &maskedOutput, bool skipAssociatedInputs) const
Vectorized version of _ComputeInputDependencyMask.
bool HasInputConnections() const
Returns true if the node has input connections, false otherwise.
Definition: node.h:183
VDF_API VdfMask::Bits ComputeInputDependencyMask(const VdfMaskedOutput &maskedOutput, const VdfConnection &inputConnection) const
Returns a mask that indicates which elements of the data that flows along inputConnection are needed ...
virtual VDF_API void _WillRemoveInputConnection(const VdfConnection *c)
Notifies a node that one connection will be removed.
VDF_API VdfConnectionAndMaskVector ComputeInputDependencyRequest(const VdfMaskedOutputVector &request) const
Vectorized version of ComputeInputDependencyMasks().
VDF_API void _ReplaceInputSpecs(const VdfInputSpecs &inputSpecs)
Replaces the node's input specs with inputSpecs and rebuilds all inputs.
VDF_API VdfOutput * GetOptionalOutput(const TfToken &name)
Returns the output object named name.
VDF_API void _ClearInputAndOutputSpecsPointer()
Clears the input/output specs pointer.
static VdfIndex GetIndexFromId(const VdfId id)
Get the node index from the node id.
Definition: node.h:123
const VdfInputSpecs & GetInputSpecs() const
Returns the list of input specs.
Definition: node.h:157
virtual VDF_API bool _IsDerivedEqual(const VdfNode &rhs) const
Can be overridden by derived classes to facilitate equality comparision.
VDF_API void SetDebugNameCallback(VdfNodeDebugNameCallback &&callback)
Sets the debug name for this node with a lazily invoked callback.
virtual VDF_API const VdfInputAndOutputSpecs * _AcquireInputAndOutputSpecsPointer(const VdfInputSpecs &inputSpecs, const VdfOutputSpecs &outputSpecs)
Gets an input/output specs pointer that the node can use.
virtual VDF_API VdfMask::Bits _ComputeInputDependencyMask(const VdfMaskedOutput &maskedOutput, const VdfConnection &inputConnection) const
Returns a mask that indicates which elements of the data that flows along inputConnection are needed ...
static size_t _GetMemoryUsage(const ClassType &c, size_t dynamicSize)
Helper method for determining the amount of memory that a node uses.
Definition: node.h:587
VDF_API const std::string GetDebugName() const
Returns the debug name for this node, if one is registered.
size_t GetNumOutputs() const
Returns the number of outputs that this node currently has.
Definition: node.h:283
const VdfOutput * GetOutput(const TfToken &name) const
Returns the output object named name.
Definition: node.h:227
virtual VDF_API void _ReleaseInputAndOutputSpecsPointer(const VdfInputAndOutputSpecs *specs)
Releases an input/output specs pointer that was acquired with a previous call to _AcquireInputAndOutp...
bool IsA() const
Returns true, if this node is of type TYPE.
Definition: node.h:147
bool HasOutputConnections() const
Returns true if the node has output connections, false otherwise.
Definition: node.h:193
virtual VDF_API void _DidAddInputConnection(const VdfConnection *c, int atIndex)
Notifies a node that one connection has been added.
VDF_API void _AppendInputs(const VdfInputSpecs &inputSpecs, std::vector< VdfInput * > *resultingInputs=NULL)
Builds inputs from the supplied input specs and appends them to the already-existing set of inputs,...
VdfId GetId() const
Returns the unique id of this node in its network.
Definition: node.h:116
const VdfOutput * GetOptionalOutput(const TfToken &name) const
Returns the output object named name.
Definition: node.h:249
size_t GetNumInputs() const
Returns the number of inputs that this node currently has.
Definition: node.h:289
VDF_API void SetDebugName(const std::string &name)
Sets the debug name for this node.
VDF_API VdfConnectionVector GetOutputConnections() const
Returns a flat vector of all output connections.
VDF_API void _AppendOutputs(const VdfOutputSpecs &outputSpecs, std::vector< VdfOutput * > *resultingOutputs=NULL)
Builds outputs from the supplied output specs and appends them to the already-existing set of outputs...
VDF_API VdfOutput * GetOutput(const TfToken &name)
Returns the output object named name.
VdfNetwork & GetNetwork()
Returns the network to which this node belongs.
Definition: node.h:142
VDF_API VdfMask ComputeOutputDependencyMask(const VdfConnection &inputConnection, const VdfMask &inputDependencyMask, const VdfOutput &output) const
Returns a mask that indicates which elements of the data that flows along output depend on the elemen...
const VdfInput * GetInput(const TfToken &inputName) const
Returns the connector named inputName, returns NULL if no input of that name exists.
Definition: node.h:164
virtual VDF_API VdfConnectionAndMaskVector _ComputeInputDependencyRequest(const VdfMaskedOutputVector &request) const
Vectorized version of _ComputeOutputDependencyMasks.
VDF_API void ComputeOutputDependencyMasks(const VdfConnection &inputConnection, const VdfMask &inputDependencyMask, VdfMaskedOutputVector *outputDependencies) const
Vectorized version of ComputeOutputDependencyMask.
const VdfNetwork & GetNetwork() const
Returns the network to which this node belongs.
Definition: node.h:138
VDF_API bool IsEqual(const VdfNode &rhs) const
Returns true, if rhs and this node compute the same value(s).
VDF_API VdfNode(VdfNetwork *network, const VdfInputSpecs &inputSpecs, const VdfOutputSpecs &outputSpecs)
Constructs a node in network with the inputs described by inputSpecs and outputs described by outputS...
const OutputMapIterator GetOutputsIterator() const
Returns an iterator class that can be used with TF_FOR_ALL to iterator through the output connectors.
Definition: node.h:277
const VdfOutput * GetOutput() const
Returns the only output object that this node contains.
Definition: node.h:260
const VdfOutputSpecs & GetOutputSpecs() const
Returns the list of output specs.
Definition: node.h:219
VDF_API VdfNode(VdfNetwork *network)
Protected constructor.
VDF_API VdfOutput * GetOutput()
Returns the only output object that this node contains.
VDF_API void _InitializeInputAndOutputSpecs(const VdfInputAndOutputSpecs *specs)
Initializes the input/output specs pointer for this node.
virtual VDF_API VdfRequiredInputsPredicate GetRequiredInputsPredicate(const VdfContext &context) const
Returns a predicate, determining whether a given input and its connections are required in order to f...
virtual VDF_API bool IsSpeculationNode() const
Returns true if this node performs speculation.
static VdfVersion GetVersionFromId(const VdfId id)
Get the node version from the node id.
Definition: node.h:132
virtual VDF_API size_t GetMemoryUsage() const
Returns the amount of memory used by the node in bytes.
VDF_API VdfConnectionAndMaskVector ComputeInputDependencyMasks(const VdfMaskedOutput &maskedOutput, bool skipAssociatedInputs) const
Vectorized version of ComputeInputDependencyMask.
VDF_API VdfInput * GetInput(const TfToken &inputName)
Returns the input named inputName, returns NULL if no input of that name exists.
virtual VDF_API ~VdfNode()
Protected Destructor.
virtual void Compute(const VdfContext &context) const =0
This is the method called to perform computation.
virtual VDF_API VdfMask _ComputeOutputDependencyMask(const VdfConnection &inputConnection, const VdfMask &inputDependencyMask, const VdfOutput &output) const
Returns a mask that indicates which elements of the data that flows along output depend on the elemen...
A VdfOutput represents an output on a node.
Definition: output.h:32
VdfOutputSpecs is a container for VdfOutputSpec objects.
This predicate determines whether a given input value is needed to fulfill the input dependencies req...
This class is used to abstract away knowledge of the cache data used for each node.
Definition: vector.h:56
Demangle C++ typenames generated by the typeid() facility.
std::function< std::string()> VdfNodeDebugNameCallback
Type of callback for building a node debug name.
Definition: types.h:71
std::vector< VdfConnectionAndMask > VdfConnectionAndMaskVector
A vector of VdfConnectionAndMasks.
Definition: types.h:80
uint64_t VdfId
The unique identifier type for Vdf objects.
Definition: types.h:107
uint32_t VdfIndex
The index type for Vdf objects.
Definition: types.h:110
uint32_t VdfVersion
The version type for Vdf objects.
Definition: types.h:113
Define preprocessor function name macros.
#define TF_FOR_ALL(iter, c)
Macro for iterating over a container.
Definition: iterator.h:373
#define TF_MALLOC_TAG_NEW(name1, name2)
Enable lib/tf memory management.
Definition: mallocTag.h:475
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...