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;
19 class GU_Detail;
20 class OP_Context;
21 class UT_ErrorManager;
22 class UT_StringRef;
23 
24 namespace apex
25 {
26  class APEX_Graph;
27 };
28 
29 /// This micronode can invoke a callback when it becomes dirty. It's used by
30 /// COP_ApexProgram's callback registration, to make sure a function runs when
31 /// the program's results are changed.
33 {
34 public:
36  DEP_TimedMicroNode(), myCallback(nullptr), myEnabled(false)
37  {
38  }
39 
40  /// Controls whether or not the callback runs when the micronode is dirtied.
41  void enableCallback(bool enable)
42  {
43  myEnabled = enable;
44  }
45 
46  /// Returns true if the callback is enabled (even if not set).
47  bool callbackEnabled() const
48  {
49  return myEnabled;
50  }
51 
52  /// Set the callback to run when the micronode is dirtied. The callback will
53  /// only be invoked if enableCallback(true) was previously called. The data
54  /// pointer is passed to the callback function when it's executed.
55  void setCallback(void (*callback)(void*), void* data)
56  {
57  myCallback = callback;
58  myData = data;
59  }
60 
61  void becameDirty(DEP_MicroNode& src, const DEP_PropagateData& propdata)
62  override;
63 
64 private:
65  void (*myCallback)(void*);
66  void* myData;
67  bool myEnabled;
68 };
69 
70 /// This class creates an executable program out of a COP block. All parameters
71 /// are evaluated and hardened in the graph.
73 {
74 public:
76  {
77  FAIL = 0,
80  };
81 
82 public:
84  ~COP_ApexProgram();
85 
86  /// Set the block end node for this program, without trying to rebuild.
87  /// Returns false if output is a block begin node, in which case the output
88  /// node (and the program as a whole) is unchanged.
89  bool setOutputNode(COP_Block* output);
90 
91  /// Builds an executable out of the block end node, containing everything up
92  /// to its begin node.
93  bool buildFromNode(COP_Block* output, const OP_Context& context,
95 
96  /// Saves the graph as geometry.
97  void saveToGeometry(GU_Detail* gdp) const;
98  /// Loads a graph from geometry.
99  bool loadFromGeometry(const GU_Detail* gdp);
100 
101  /// Saves the graph to a (geometry) file.
102  bool saveToFile(const char* filename) const;
103  /// Loads the graph from a geometry file.
104  bool loadFromFile(const char* filename);
105 
106  /// Binds an input to the program.
107  bool setInputLayer(const UT_StringRef& name,
108  const IMX_LayerConstPtr& layer);
109 
110  /// Runs the program after the inputs have been bound.
111  void runProgram(const OP_Context& context, UT_ErrorManager& error);
112 
113  /// Extracts results after the program has executed.
114  IMX_LayerPtr getOutputLayer(const UT_StringRef& name) const;
115 
116  /// Clears all the graph inputs, letting go of the held references.
117  void clearInputs();
118 
119  /// Returns a map of all COP inputs to the program. Key of an input is its
120  /// name, while the value is its expected type in the program.
122  {
123  return myProgramInputs;
124  }
125  /// Returns a map of all COP outputs of the program. Key of an output is its
126  /// name, while the value is its type in the program.
129  {
130  return myProgramOutputs;
131  }
132 
133  /// Returns true if the input of the given name exists but is not used.
134  bool isUnusedInput(const UT_StringRef& name) const;
135  /// Returns true if the input of the given name exists and is used by the
136  /// program.
137  bool isUsedInput(const UT_StringRef& name) const;
138 
139  /// Returns the micronode that captures all dependencies for the program.
141  {
142  return myGraphDep;
143  }
144  /// Returns the micronode that captures all dynamic runtime dependencies
145  /// from the previous program execution.
147  {
148  return myResultsDep;
149  }
150 
151  /// When enabled, the registered callback is invoked whenever the program's
152  /// results become out of date.
153  /// Note that the callback may be invoked more than once when the results
154  /// are dirtied!!!
155  void enableCallback(bool enable)
156  {
157  myGraphDep.enableCallback(enable);
158  myResultsDep.enableCallback(enable);
159  }
160  /// Registers the callback that is to run when the program's results become
161  /// out of date. Use enableCallback() to control whether or not the callback
162  /// is forced to run. The data pointer is passed to the callback function
163  /// when it executes.
164  /// Note that the callback may be invoked more than once when the results
165  /// are dirtied!!!
166  void setCallback(void (*callback)(void*), void* data)
167  {
168  myGraphDep.setCallback(callback, data);
169  myResultsDep.setCallback(callback, data);
170  myErroredMonitor.setCallback(callback, data);
171  }
172 
173  /// Rebuilds the APEX graph if necessary and returns one of the above status
174  /// codes.
175  SlapcompBuildStatus rebuildIfNeeded(const OP_Context& context,
177 
178  /// Returns true if the user should bind input layers that have correct
179  /// camera metadata. If this is false, then the input layers are expected to
180  /// use the default transformation.
181  bool expectsLayerCameras() const
182  {
183  return myExpectInputCameras;
184  }
185 
186  /// Returns the Block End node this program is built for.
187  COP_Block *getOutputNode() const;
188  /// Returns if the block end is registered for a slapcomp
189  bool isOutputNodeSlapComp() const;
190 
191 protected:
192  void addBlockDeps(DEP_MicroNode& dep, COP_Block* begin, COP_Block* end);
193 
194 protected:
196  /// Map of all inputs used by the program.
199  /// Map of all inputs that are not used by the program. These are saved as
200  /// they are part of the program's signature.
205  /// Mapping of bound passthrough outputs (by their name) to the bound input
206  /// that they correspond to.
208  int myOutputNodeId = -1;
211  /// This micronode is used only when the build fails. It gets notified when
212  /// a relevant node is rewired or changed.
214 
215  /// This flag indicates to the user whether they should place the input
216  /// layers at the correct 3D locations or use default transforms.
217  bool myExpectInputCameras = true;
218 };
219 
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)
UT_StringMap< IMX_LayerPtr > myPassthroughOutputs
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_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