HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
COP_ApexProgram.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  */
7 
8 #pragma once
9 
10 #include "COP_API.h"
11 
12 #include "COP_MicroNode.h"
13 #include "COP_Signature.h"
14 
15 #include <IMX/IMX_Layer.h>
16 #include <IMX/IMX_VDB.h>
17 #include <IMX/IMX_Attribute.h>
18 #include <UT/UT_StringMap.h>
19 
20 class COP_ApexNodeDependencies;
21 class COP_Block;
22 class COP_CableStructure;
23 class COP_GraphProxyDirect;
24 class COP_Node;
25 class COP_Request;
27 class GU_Detail;
28 class GU_DetailHandle;
29 class OP_Context;
30 class UT_ErrorManager;
31 class UT_StringRef;
32 
33 namespace apex
34 {
35  class APEX_Graph;
36 };
37 
38 /// This class creates an executable program out of a COP block. All parameters
39 /// are evaluated and hardened in the graph.
41 {
42 public:
44  {
45  FAIL = 0,
46  REBUILD_NOT_NEEDED,
47  SUCCESS
48  };
49 
50  /// This tag can be used with setOutputBlock() and buildFromBlock() methods
51  /// to indicate what the block begin's input states are (i.e. which ports
52  /// are actually connected).
53  enum class BeginBindingsMode
54  {
55  /// Will assume all inputs are connected non-null data.
56  ALL_INPUTS_BOUND,
57  /// Will use the network's connections.
58  NETWORK_STATE
59  };
60 
61 public:
62  /// If harden_parms is true, the parameters will be evaluated and baked into
63  /// the built graphs.
64  COP_ApexProgram(bool harden_parms = true);
65  ~COP_ApexProgram();
66 
67  /// Set the block end node for this program, without trying to rebuild.
68  /// Returns false if output is a block begin node, in which case the output
69  /// node (and the program as a whole) is unchanged. If sigs and input_cables
70  /// are provided, they will be used as the authoritative state of inputs to
71  /// the block (on the begin side); if one is given, the other must be as
72  /// well. If these arguments are nullptr, then the built program will
73  /// reflect state of the network.
74  bool setOutputBlock(const COP_Block* output,
75  const UT_Array<COP_Signature>* sigs,
76  const UT_Array<COP_CableStructure>* input_cables);
77  /// This version of the method assumes that all inputs to the block are
78  /// properly bound non-null data when the program gets built.
79  bool setOutputBlock(const COP_Block* output, BeginBindingsMode mode);
80  /// Builds an executable out of the block end node, containing everything up
81  /// to its begin node.
82  bool buildFromBlock(const COP_Block* output,
83  const UT_Array<COP_Signature>* sigs,
84  const UT_Array<COP_CableStructure>* cables,
85  const OP_Context& context, UT_ErrorManager& error);
86  /// This version of the method assumes that all inputs to the block are
87  /// properly bound non-null data when the program gets built.
88  bool buildFromBlock(const COP_Block* output, BeginBindingsMode mode,
89  const OP_Context& context, UT_ErrorManager& error);
90 
91  /// Builds a program that cooks the specified outputs of a single node. The
92  /// node can be a subnetwork or an atomic node.
93  bool buildFromSingleNode(COP_Node* output,
94  const UT_Array<COP_Request>& requests,
95  const COP_Signature& signature,
96  const OP_Context& context, UT_ErrorManager& error);
97 
98  /// Returns the cable structure of an output of the block this is built
99  /// from.
100  COP_CableStructure getBlockOutputCableStructure(OP_OutputIdx port) const;
101 
102  /// Saves the graph as geometry.
103  void saveToGeometry(GU_Detail* gdp) const;
104  /// Loads a graph from geometry.
105  bool loadFromGeometry(const GU_Detail* gdp);
106 
107  /// Saves the graph to a (geometry) file.
108  bool saveToFile(const char* filename) const;
109  /// Loads the graph from a geometry file.
110  bool loadFromFile(const char* filename);
111 
112  /// Binds an input to the program.
113  bool setInputLayer(const UT_StringRef& name,
114  const IMX_LayerConstPtr& layer);
115  bool setInputGeometry(const UT_StringRef& name,
116  const GU_ConstDetailHandle& geo);
117  bool setInputVDB(const UT_StringRef& name,
118  const IMX_VDBConstPtr& vdb);
119  bool setInputAttribute(const UT_StringRef& name,
120  const IMX_AttributeConstPtr& attrib);
121  /// If as_cable is true, all fields from the port are bound as inputs (based
122  /// on the field names).
123  bool setInput(const UT_StringRef& name, const COP_PortData& input,
124  bool as_cable = false);
125  /// If as_cable is true, all inputs that match name.* are cleared.
126  bool clearInput(const UT_StringRef& name, bool as_cable = false);
127 
128  /// Runs the program after the inputs have been bound.
129  void runProgram(const OP_Context& context, UT_ErrorManager& error);
130 
131  /// Extracts results after the program has executed.
132  IMX_LayerPtr getOutputLayer(const UT_StringRef& name) const;
133  GU_ConstDetailHandle getOutputGeometry(const UT_StringRef& name) const;
134  IMX_VDBPtr getOutputVDB(const UT_StringRef& name) const;
135  IMX_AttributePtr getOutputAttribute(const UT_StringRef& name) const;
136  COP_PortData getOutput(const UT_StringRef& name) const;
137  /// Extracts the port data for a single cable output of the given name. The
138  /// incoming cable structure is assumed to be correct.
139  COP_PortData getOutputCable(const UT_StringRef& name,
140  const COP_CableStructure& cable) const;
141 
142  /// Clears all the graph inputs, letting go of the held references.
143  void clearInputs();
144 
145  /// clears all the graph outputs, letting go of the held references.
146  void clearOutputs();
147 
148  /// Clears all the buffers internal to this, including inputs and outputs
149  void clearBuffers();
150 
151  /// Returns a map of all COP inputs to the program. Key of an input is its
152  /// name, while the value is its expected type in the program.
154  {
155  return myProgramInputs;
156  }
157  /// Returns a map of all COP outputs of the program. Key of an output is its
158  /// name, while the value is its type in the program.
161  {
162  return myProgramOutputs;
163  }
164  /// Returns type of the given output.
165  COP_Type getOutputType(const UT_StringRef& output) const;
166 
167  /// Returns true if the input of the given name exists but is not used.
168  bool isUnusedInput(const UT_StringRef& name) const;
169  /// Returns true if the input of the given name exists and is used by the
170  /// program. If as_cable is true, this program is scanned for inputs that
171  /// match name.*.
172  bool isUsedInput(const UT_StringRef& name, bool as_cable = false) const;
173 
174  /// Returns the micronode that captures all dependencies for the program.
176  {
177  return myGraphDep;
178  }
179  /// Returns the micronode that captures all dynamic runtime dependencies
180  /// from the previous program execution.
182  {
183  return myResultsDep;
184  }
185 
186  /// When enabled, the registered callback is invoked whenever the program's
187  /// results become out of date.
188  /// Note that the callback may be invoked more than once when the results
189  /// are dirtied!!!
190  void enableCallback(bool enable)
191  {
192  myGraphDep.enableCallback(enable);
193  myResultsDep.enableCallback(enable);
194  }
195  /// Registers the callback that is to run when the program's results become
196  /// out of date. Use enableCallback() to control whether or not the callback
197  /// is forced to run. The data pointer is passed to the callback function
198  /// when it executes.
199  /// Note that the callback may be invoked more than once when the results
200  /// are dirtied!!!
201  void setCallback(void (*callback)(void*), void* data)
202  {
203  myGraphDep.setCallback(callback, data);
204  myResultsDep.setCallback(callback, data);
205  myErroredMonitor.setCallback(callback, data);
206  }
207 
208  /// Rebuilds the APEX graph if necessary and returns one of the above status
209  /// codes. If reset_deps is false, previous dependencies are retained; this
210  /// should be used if running multiple iterations of a program, since the
211  /// intent there is to accummulate all the dependencies across all runs.
212  SlapCompBuildStatus rebuildIfNeeded(const OP_Context& context,
214  bool reset_deps);
215 
216  /// Returns true if the user should bind input layers that have correct
217  /// camera metadata. If this is false, then the input layers are expected to
218  /// use the default transformation.
219  bool expectsLayerCameras() const
220  {
221  return myExpectInputCameras;
222  }
223 
224  /// Signature evaluation context override ID seen by this program.
225  int getGraphProxyOverrideId() const;
226 
227  /// Returns the Block End node this program is built for; if the program was
228  /// built for a single node, this returns nullptr.
229  COP_Block *getOutputBlock() const;
230  /// Returns the id of the node this program was built from, if it was built
231  /// from a block or a single node.
232  int getOutputNodeId() const;
233  /// Returns the node this program was built from, if it was built from a
234  /// block or a single node, and if that node still exists.
235  COP_Node *getOutputNode() const;
236  /// Returns if this program is for a block that is registered for slap comp.
237  bool isOutputBlockSlapComp() const;
238 
239 protected:
240  /// What the program is built for.
241  enum class BuildTarget
242  {
243  BLOCK,
244  SINGLE_NODE,
245  INVALID
246  };
247 
248 protected:
249  /// Helper function to register dependencies on the signature and port names
250  /// of the block nodes.
251  void addBlockDeps(DEP_MicroNode& dep, COP_Block* begin, COP_Block* end);
252  /// Sets the target output node for the program, possibly marking the graph
253  /// dirty if it's different from a built program's.
254  void setOutputNodeInternal(const COP_Node* output, BuildTarget target,
255  const UT_Array<COP_Signature>* sigs,
256  const UT_Array<COP_CableStructure>* cables,
257  const UT_Array<COP_Request>* requests);
258  /// Returns this program's graph proxy, allocating it if it's not yet
259  /// created.
260  COP_GraphProxyDirect* getGraphProxy();
261 
262 protected:
264  /// Map of all inputs used by the program.
267  /// Map of all inputs that are not used by the program. These are saved as
268  /// they are part of the program's signature.
273  /// Mapping of bound passthrough outputs (by their name) to the bound input
274  /// that they correspond to.
276  int myOutputNodeId = -1;
277  BuildTarget myBuildTarget = BuildTarget::INVALID;
279 
280  // Marks us as invalid forcing a rebuild
281  bool myForceRebuild = false;
282 
285  /// This micronode is used only when the build fails. It gets notified when
286  /// a relevant node is rewired or changed.
288 
289  /// Contains all the dependency-tracking data for node parameters (if built
290  /// from a node).
293 
294  /// This program's graph proxy that might have registered overrides (if
295  /// building from a node).
298 
299  /// This flag controls how the graph is built from a node. If true, all
300  /// parameters are evaluated and baked into the graph; otherwise, they're
301  /// made into graph inputs.
303  /// This flag indicates to the user whether they should place the input
304  /// layers at the correct 3D locations or use default transforms.
305  bool myExpectInputCameras = true;
306 };
307 
308 // Not trivially movable because tbb::concurrent_vector
309 // DEP_MicroNode::myExplicitOutputs is not trivially movable
311 
const UT_StringMap< std::pair< COP_Type, bool > > & getProgramInputs() const
GT_API const UT_StringHolder filename
UT_UniquePtr< COP_ApexNodeDependencies > myParameterDep
void enableCallback(bool enable)
UT_StringMap< std::pair< COP_Type, bool > > myUnusedInputs
UT_Array< COP_Request > myOutputRequests
BuildTarget
What the program is built for.
const UT_StringMap< std::pair< COP_Type, UT_StringHolder > > & getProgramOutputs() const
bool expectsLayerCameras() const
COP_MicroNodeWithCallback myErroredMonitor
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
< returns > If no error
Definition: snippets.dox:2
UT_SharedPtr< const IMX_Layer > IMX_LayerConstPtr
Definition: IMX_Layer.h:28
GLuint GLuint end
Definition: glcorearb.h:475
UT_SharedPtr< const IMX_VDB > IMX_VDBConstPtr
Definition: IMX_VDB.h:35
UT_StringMap< std::pair< COP_Type, bool > > myProgramInputs
Map of all inputs used by the program.
UT_SharedPtr< const IMX_Attribute > IMX_AttributeConstPtr
Definition: IMX_Attribute.h:36
GLenum target
Definition: glcorearb.h:1667
#define SYS_DECLARE_IS_NOT_TR(T)
Declare that that trivial relocation with type T is not guaranteed to be safe.
UT_SharedPtr< IMX_Attribute > IMX_AttributePtr
Definition: IMX_Attribute.h:35
COP_MicroNodeWithCallback myGraphDep
GLuint const GLchar * name
Definition: glcorearb.h:786
COP_MicroNodeWithCallback myResultsDep
GLenum mode
Definition: glcorearb.h:99
COP_Type
Types of basic data that are passed around a COP network.
Definition: COP_Signature.h:17
UT_StringMap< COP_PortData > myPassthroughOutputs
UT_UniquePtr< COP_GraphProxyDirect > myGraphProxy
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:587
UT_UniquePtr< apex::APEX_Graph > myGraph
A global error manager scope.
#define COP_API
Definition: COP_API.h:8
int OP_OutputIdx
Definition: OP_DataTypes.h:185
COP_MicroNodeWithCallback & getGraphDep()
Returns the micronode that captures all dependencies for the program.
void setCallback(void(*callback)(void *), void *data)
UT_SharedPtr< IMX_VDB > IMX_VDBPtr
Definition: IMX_VDB.h:34
UT_StringMap< std::pair< COP_Type, UT_StringHolder > > myProgramOutputs
COP_MicroNodeWithCallback & getResultsDep()
UT_SharedPtr< IMX_Layer > IMX_LayerPtr
Definition: IMX_Layer.h:27
Definition: format.h:1821