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_Signature.h"
13 
14 #include <DEP/DEP_TimedMicroNode.h>
15 #include <IMX/IMX_Layer.h>
16 #include <UT/UT_StringMap.h>
17 
18 class COP_Block;
20 class GU_Detail;
21 class GU_DetailHandle;
22 class OP_Context;
23 class UT_ErrorManager;
24 class UT_StringRef;
25 
26 namespace apex
27 {
28  class APEX_Graph;
29 };
30 
31 /// This micronode can invoke a callback when it becomes dirty. It's used by
32 /// COP_ApexProgram's callback registration, to make sure a function runs when
33 /// the program's results are changed.
35 {
36 public:
38  DEP_TimedMicroNode(), myCallback(nullptr), myEnabled(false)
39  {
40  }
41 
42  /// Controls whether or not the callback runs when the micronode is dirtied.
43  void enableCallback(bool enable)
44  {
45  myEnabled = enable;
46  }
47 
48  /// Returns true if the callback is enabled (even if not set).
49  bool callbackEnabled() const
50  {
51  return myEnabled;
52  }
53 
54  /// Set the callback to run when the micronode is dirtied. The callback will
55  /// only be invoked if enableCallback(true) was previously called. The data
56  /// pointer is passed to the callback function when it's executed.
57  void setCallback(void (*callback)(void*), void* data)
58  {
59  myCallback = callback;
60  myData = data;
61  }
62 
63  void becameDirty(DEP_MicroNode& src, const DEP_PropagateData& propdata)
64  override;
65 
66 private:
67  void (*myCallback)(void*);
68  void* myData;
69  bool myEnabled;
70 };
71 
72 /// This class creates an executable program out of a COP block. All parameters
73 /// are evaluated and hardened in the graph.
75 {
76 public:
78  {
79  FAIL = 0,
82  };
83 
84 public:
86  ~COP_ApexProgram();
87 
88  /// Set the block end node for this program, without trying to rebuild.
89  /// Returns false if output is a block begin node, in which case the output
90  /// node (and the program as a whole) is unchanged.
91  bool setOutputNode(COP_Block* output);
92 
93  /// Builds an executable out of the block end node, containing everything up
94  /// to its begin node.
95  bool buildFromNode(COP_Block* output, const OP_Context& context,
97 
98  /// Saves the graph as geometry.
99  void saveToGeometry(GU_Detail* gdp) const;
100  /// Loads a graph from geometry.
101  bool loadFromGeometry(const GU_Detail* gdp);
102 
103  /// Saves the graph to a (geometry) file.
104  bool saveToFile(const char* filename) const;
105  /// Loads the graph from a geometry file.
106  bool loadFromFile(const char* filename);
107 
108  /// Binds an input to the program.
109  bool setInputLayer(const UT_StringRef& name,
110  const IMX_LayerConstPtr& layer);
111  bool setInputGeometry(const UT_StringRef& name,
112  const GU_ConstDetailHandle& geo);
113  bool setInput(const UT_StringRef& name, const COP_PortData& input);
114 
115  /// Runs the program after the inputs have been bound.
116  void runProgram(const OP_Context& context, UT_ErrorManager& error);
117 
118  /// Extracts results after the program has executed.
119  IMX_LayerPtr getOutputLayer(const UT_StringRef& name) const;
120  GU_DetailHandle getOutputGeometry(const UT_StringRef& name) const;
121  COP_PortData getOutput(const UT_StringRef& name) const;
122 
123  /// Clears all the graph inputs, letting go of the held references.
124  void clearInputs();
125 
126  /// Returns a map of all COP inputs to the program. Key of an input is its
127  /// name, while the value is its expected type in the program.
129  {
130  return myProgramInputs;
131  }
132  /// Returns a map of all COP outputs of the program. Key of an output is its
133  /// name, while the value is its type in the program.
136  {
137  return myProgramOutputs;
138  }
139 
140  /// Returns true if the input of the given name exists but is not used.
141  bool isUnusedInput(const UT_StringRef& name) const;
142  /// Returns true if the input of the given name exists and is used by the
143  /// program.
144  bool isUsedInput(const UT_StringRef& name) const;
145 
146  /// Returns the micronode that captures all dependencies for the program.
148  {
149  return myGraphDep;
150  }
151  /// Returns the micronode that captures all dynamic runtime dependencies
152  /// from the previous program execution.
154  {
155  return myResultsDep;
156  }
157 
158  /// When enabled, the registered callback is invoked whenever the program's
159  /// results become out of date.
160  /// Note that the callback may be invoked more than once when the results
161  /// are dirtied!!!
162  void enableCallback(bool enable)
163  {
164  myGraphDep.enableCallback(enable);
165  myResultsDep.enableCallback(enable);
166  }
167  /// Registers the callback that is to run when the program's results become
168  /// out of date. Use enableCallback() to control whether or not the callback
169  /// is forced to run. The data pointer is passed to the callback function
170  /// when it executes.
171  /// Note that the callback may be invoked more than once when the results
172  /// are dirtied!!!
173  void setCallback(void (*callback)(void*), void* data)
174  {
175  myGraphDep.setCallback(callback, data);
176  myResultsDep.setCallback(callback, data);
177  myErroredMonitor.setCallback(callback, data);
178  }
179 
180  /// Rebuilds the APEX graph if necessary and returns one of the above status
181  /// codes.
182  SlapcompBuildStatus rebuildIfNeeded(const OP_Context& context,
184 
185  /// Returns true if the user should bind input layers that have correct
186  /// camera metadata. If this is false, then the input layers are expected to
187  /// use the default transformation.
188  bool expectsLayerCameras() const
189  {
190  return myExpectInputCameras;
191  }
192 
193  /// Returns the Block End node this program is built for.
194  COP_Block *getOutputNode() const;
195  /// Returns if the block end is registered for a slapcomp
196  bool isOutputNodeSlapComp() const;
197 
198 protected:
199  void addBlockDeps(DEP_MicroNode& dep, COP_Block* begin, COP_Block* end);
200 
201 protected:
203  /// Map of all inputs used by the program.
206  /// Map of all inputs that are not used by the program. These are saved as
207  /// they are part of the program's signature.
212  /// Mapping of bound passthrough outputs (by their name) to the bound input
213  /// that they correspond to.
215  int myOutputNodeId = -1;
218  /// This micronode is used only when the build fails. It gets notified when
219  /// a relevant node is rewired or changed.
221 
222  /// This flag indicates to the user whether they should place the input
223  /// layers at the correct 3D locations or use default transforms.
224  bool myExpectInputCameras = true;
225 };
226 
const UT_StringMap< std::pair< COP_Type, bool > > & getProgramInputs() const
GT_API const UT_StringHolder filename
virtual void becameDirty(DEP_MicroNode &src, const DEP_PropagateData &propdata)
void enableCallback(bool enable)
void
Definition: png.h:1083
void setCallback(void(*callback)(void *), void *data)
GLboolean * data
Definition: glcorearb.h:131
UT_StringMap< std::pair< COP_Type, bool > > myUnusedInputs
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:366
GLuint GLuint end
Definition: glcorearb.h:475
UT_StringMap< std::pair< COP_Type, bool > > myProgramInputs
Map of all inputs used by the program.
bool callbackEnabled() const
Returns true if the callback is enabled (even if not set).
COP_MicroNodeWithCallback myGraphDep
GLuint const GLchar * name
Definition: glcorearb.h:786
COP_MicroNodeWithCallback myResultsDep
DEP_TimedMicroNode & getGraphDep()
Returns the micronode that captures all dependencies for the program.
UT_StringMap< COP_PortData > myPassthroughOutputs
UT_UniquePtr< apex::APEX_Graph > myGraph
void enableCallback(bool enable)
Controls whether or not the callback runs when the micronode is dirtied.
A global error manager scope.
#define COP_API
Definition: COP_API.h:8
Propagation info for a dep micro node.
Definition: DEP_MicroNode.h:36
void setCallback(void(*callback)(void *), void *data)
UT_StringMap< std::pair< COP_Type, UT_StringHolder > > myProgramOutputs
DEP_TimedMicroNode & getResultsDep()
UT_SharedPtr< IMX_Layer > IMX_LayerPtr
Definition: IMX_Layer.h:365
Definition: format.h:895
GLenum src
Definition: glcorearb.h:1793
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:558