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 <GA/GA_Detail.h>
16 #include <IMX/IMX_Layer.h>
17 #include <IMX/IMX_VDB.h>
18 #include <IMX/IMX_Attribute.h>
19 #include <UT/UT_StringMap.h>
20 
21 class COP_ApexNodeDependencies;
22 class COP_Block;
23 class COP_CableStructure;
24 class COP_GraphProxyDirect;
25 class COP_Node;
26 class COP_Request;
28 class GU_Detail;
29 class GU_DetailHandle;
30 class OP_Context;
31 class UT_ErrorManager;
32 class UT_StringRef;
33 
34 namespace apex
35 {
36  class APEX_Graph;
37 };
38 
39 /// This class creates an executable program out of a COP block. All parameters
40 /// are evaluated and hardened in the graph.
42 {
43 public:
45  {
46  FAIL = 0,
47  REBUILD_NOT_NEEDED,
48  SUCCESS
49  };
50 
51  /// This tag can be used with setOutputBlock() and buildFromBlock() methods
52  /// to indicate what the block begin's input states are (i.e. which ports
53  /// are actually connected).
54  enum class BeginBindingsMode
55  {
56  /// Will assume all inputs are connected non-null data.
57  ALL_INPUTS_BOUND,
58  /// Will use the network's connections.
59  NETWORK_STATE
60  };
61 
62 public:
63  /// If harden_parms is true, the parameters will be evaluated and baked into
64  /// the built graphs.
65  COP_ApexProgram(bool harden_parms = true);
66  ~COP_ApexProgram();
67 
68  /// Set the block end node for this program, without trying to rebuild.
69  /// Returns false if output is a block begin node, in which case the output
70  /// node (and the program as a whole) is unchanged. If sigs and input_cables
71  /// are provided, they will be used as the authoritative state of inputs to
72  /// the block (on the begin side); if one is given, the other must be as
73  /// well. If these arguments are nullptr, then the built program will
74  /// reflect state of the network.
75  bool setOutputBlock(const COP_Block* output,
76  const UT_Array<COP_Signature>* sigs,
77  const UT_Array<COP_CableStructure>* input_cables);
78  /// This version of the method assumes that all inputs to the block are
79  /// properly bound non-null data when the program gets built.
80  bool setOutputBlock(const COP_Block* output, BeginBindingsMode mode);
81  /// Builds an executable out of the block end node, containing everything up
82  /// to its begin node.
83  bool buildFromBlock(const COP_Block* output,
84  const UT_Array<COP_Signature>* sigs,
85  const UT_Array<COP_CableStructure>* cables,
86  const OP_Context& context, UT_ErrorManager& error);
87  /// This version of the method assumes that all inputs to the block are
88  /// properly bound non-null data when the program gets built.
89  bool buildFromBlock(const COP_Block* output, BeginBindingsMode mode,
90  const OP_Context& context, UT_ErrorManager& error);
91 
92  /// Builds a program that cooks the specified outputs of a single node. The
93  /// node can be a subnetwork or an atomic node.
94  bool buildFromSingleNode(COP_Node* output,
95  const UT_Array<COP_Request>& requests,
96  const COP_Signature& signature,
97  const OP_Context& context, UT_ErrorManager& error);
98 
99  /// Returns the cable structure of an output of the block this is built
100  /// from.
101  COP_CableStructure getBlockOutputCableStructure(OP_OutputIdx port) const;
102 
103  /// Saves the graph as geometry.
104  void saveToGeometry(GU_Detail* gdp) const;
105  /// Loads a graph from geometry.
106  bool loadFromGeometry(const GU_Detail* gdp);
107 
108  /// Saves the graph to a (geometry) file.
109  bool saveToFile(const char* filename) const;
110  /// Loads the graph from a geometry file.
111  bool loadFromFile(const char* filename);
112 
113  /// Binds an input to the program.
114  bool setInputLayer(const UT_StringRef& name,
115  const IMX_LayerConstPtr& layer);
116  bool setInputGeometry(const UT_StringRef& name,
117  const GU_ConstDetailHandle& geo);
118  bool setInputVDB(const UT_StringRef& name,
119  const IMX_VDBConstPtr& vdb);
120  bool setInputAttribute(const UT_StringRef& name,
121  const IMX_AttributeConstPtr& attrib);
122  /// If as_cable is true, all fields from the port are bound as inputs (based
123  /// on the field names).
124  bool setInput(const UT_StringRef& name, const COP_PortData& input,
125  bool as_cable = false);
126  /// If as_cable is true, all inputs that match name.* are cleared.
127  bool clearInput(const UT_StringRef& name, bool as_cable = false);
128 
129  /// Runs the program after the inputs have been bound.
130  void runProgram(const OP_Context& context, UT_ErrorManager& error);
131 
132  /// Extracts results after the program has executed.
133  IMX_LayerPtr getOutputLayer(const UT_StringRef& name) const;
134  GU_ConstDetailHandle getOutputGeometry(const UT_StringRef& name) const;
135  IMX_VDBPtr getOutputVDB(const UT_StringRef& name) const;
136  IMX_AttributePtr getOutputAttribute(const UT_StringRef& name) const;
137  COP_PortData getOutput(const UT_StringRef& name) const;
138  /// Extracts the port data for a single cable output of the given name. The
139  /// incoming cable structure is assumed to be correct.
140  COP_PortData getOutputCable(const UT_StringRef& name,
141  const COP_CableStructure& cable) const;
142 
143  /// Clears all the graph inputs, letting go of the held references.
144  void clearInputs();
145 
146  /// clears all the graph outputs, letting go of the held references.
147  void clearOutputs();
148 
149  /// Clears all the buffers internal to this, including inputs and outputs
150  void clearBuffers();
151 
152  /// Returns a map of all COP inputs to the program. Key of an input is its
153  /// name, while the value is its expected type in the program.
155  {
156  return myProgramInputs;
157  }
158  /// Returns a map of all COP outputs of the program. Key of an output is its
159  /// name, while the value is its type in the program.
162  {
163  return myProgramOutputs;
164  }
165  /// Returns type of the given output.
166  COP_Type getOutputType(const UT_StringRef& output) const;
167 
168  /// Returns true if the input of the given name exists but is not used.
169  bool isUnusedInput(const UT_StringRef& name) const;
170  /// Returns true if the input of the given name exists and is used by the
171  /// program. If as_cable is true, this program is scanned for inputs that
172  /// match name.*.
173  bool isUsedInput(const UT_StringRef& name, bool as_cable = false) const;
174 
175  /// Returns the micronode that captures all dependencies for the program.
177  {
178  return myGraphDep;
179  }
180  /// Returns the micronode that captures all dynamic runtime dependencies
181  /// from the previous program execution.
183  {
184  return myResultsDep;
185  }
186 
187  /// When enabled, the registered callback is invoked whenever the program's
188  /// results become out of date.
189  /// Note that the callback may be invoked more than once when the results
190  /// are dirtied!!!
191  void enableCallback(bool enable)
192  {
193  myGraphDep.enableCallback(enable);
194  myResultsDep.enableCallback(enable);
195  }
196  /// Registers the callback that is to run when the program's results become
197  /// out of date. Use enableCallback() to control whether or not the callback
198  /// is forced to run. The data pointer is passed to the callback function
199  /// when it executes.
200  /// Note that the callback may be invoked more than once when the results
201  /// are dirtied!!!
202  void setCallback(void (*callback)(void*), void* data)
203  {
204  myGraphDep.setCallback(callback, data);
205  myResultsDep.setCallback(callback, data);
206  myErroredMonitor.setCallback(callback, data);
207  }
208 
209  /// Rebuilds the APEX graph if necessary and returns one of the above status
210  /// codes. If reset_deps is false, previous dependencies are retained; this
211  /// should be used if running multiple iterations of a program, since the
212  /// intent there is to accummulate all the dependencies across all runs.
213  SlapCompBuildStatus rebuildIfNeeded(const OP_Context& context,
215  bool reset_deps);
216 
217  /// Returns true if the user should bind input layers that have correct
218  /// camera metadata. If this is false, then the input layers are expected to
219  /// use the default transformation.
220  bool expectsLayerCameras() const
221  {
222  return myExpectInputCameras;
223  }
224 
225  /// Signature evaluation context override ID seen by this program.
226  int getGraphProxyOverrideId() const;
227 
228  /// Returns the Block End node this program is built for; if the program was
229  /// built for a single node, this returns nullptr.
230  COP_Block *getOutputBlock() const;
231  /// Returns the id of the node this program was built from, if it was built
232  /// from a block or a single node.
233  int getOutputNodeId() const;
234  /// Returns the node this program was built from, if it was built from a
235  /// block or a single node, and if that node still exists.
236  COP_Node *getOutputNode() const;
237  /// Returns if this program is for a block that is registered for slap comp.
238  bool isOutputBlockSlapComp() const;
239 
240 protected:
241  /// What the program is built for.
242  enum class BuildTarget
243  {
244  BLOCK,
245  SINGLE_NODE,
246  INVALID
247  };
248 
249 protected:
250  /// Helper function to register dependencies on the signature and port names
251  /// of the block nodes.
252  void addBlockDeps(DEP_MicroNode& dep, COP_Block* begin, COP_Block* end);
253  /// Sets the target output node for the program, possibly marking the graph
254  /// dirty if it's different from a built program's.
255  void setOutputNodeInternal(const COP_Node* output, BuildTarget target,
256  const UT_Array<COP_Signature>* sigs,
257  const UT_Array<COP_CableStructure>* cables,
258  const UT_Array<COP_Request>* requests);
260  {
261  myDetailUniqueId = -1;
262  myDetailMetaCacheCount = GA_Detail::INVALID_CACHE_COUNT;
263  }
264  /// Returns this program's graph proxy, allocating it if it's not yet
265  /// created.
266  COP_GraphProxyDirect* getGraphProxy();
267 
268 protected:
270  /// Map of all inputs used by the program.
273  /// Map of all inputs that are not used by the program. These are saved as
274  /// they are part of the program's signature.
279  /// Mapping of bound passthrough outputs (by their name) to the bound input
280  /// that they correspond to.
282  int myOutputNodeId = -1;
283  BuildTarget myBuildTarget = BuildTarget::INVALID;
285 
286  // The "unique ID" of the detail we loaded from
287  exint myDetailUniqueId = -1;
288  // The "meta cache count" (combination of all cache counts) of the detail we
289  // loaded from
290  exint myDetailMetaCacheCount = -1;
291  // Whether we successfully loaded from the geometry whose fingerprint
292  // matches the above two parameters.
293  bool myIsLoaded = false;
294  // Marks us as invalid forcing a rebuild
295  bool myForceRebuild = false;
296 
299  /// This micronode is used only when the build fails. It gets notified when
300  /// a relevant node is rewired or changed.
302 
303  /// Contains all the dependency-tracking data for node parameters (if built
304  /// from a node).
307 
308  /// This program's graph proxy that might have registered overrides (if
309  /// building from a node).
312 
313  /// This flag controls how the graph is built from a node. If true, all
314  /// parameters are evaluated and baked into the graph; otherwise, they're
315  /// made into graph inputs.
317  /// This flag indicates to the user whether they should place the input
318  /// layers at the correct 3D locations or use default transforms.
319  bool myExpectInputCameras = true;
320 };
321 
322 // Not trivially movable because tbb::concurrent_vector
323 // DEP_MicroNode::myExplicitOutputs is not trivially movable
325 
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
int64 exint
Definition: SYS_Types.h:125
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
static const int64 INVALID_CACHE_COUNT
Definition: GA_Detail.h:2420
UT_UniquePtr< apex::APEX_Graph > myGraph
A global error manager scope.
void invalidateGeoLoadCache()
#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