HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
COP_SlapCompProgramManager.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 #include "COP_ApexProgram.h"
12 #include "COP_CableStructure.h"
14 
15 #include <UT/UT_Span.h>
16 #include <OP/OP_Node.h>
17 
18 /// Abstract interface for managing slapcomp.
20 {
21  // Virtual interface that needs to be implemented by concrete subclasses...
22 public:
23  COP_SlapCompManager() = default;
28  virtual ~COP_SlapCompManager() = default;
29 
30  /// Set the image filters defined on the USD Stage
31  virtual void setStage(const UT_Span<const int> &target_nodes,
32  const OP_Context&) = 0;
33 
34  /// Set the image filter from the viewport control (which gets applied
35  /// after the stage filters)
36  virtual void setViewport(const OP_Node *node,
37  const OP_Context &ctx) = 0;
38 
39  /// Clear all filters
41  {
42  OP_Context ctx(0);
43  setStage(UT_Span<const int>(), ctx);
44  setViewport(nullptr, ctx);
45  }
46 
47  /// Enable all/any slapcomp. This sets the state (whether there are
48  /// actually filters to be run or not).
49  void enable()
50  {
51  myEnableState = true;
52  updateEnableState();
53  }
54  /// Disable all slapcomp.
55  void disable()
56  {
57  myEnableState = false;
58  updateEnableState();
59  }
60  void setEnabled(bool state)
61  {
62  if (state)
63  enable();
64  else
65  disable();
66  }
67 
68  /// Called when slapcomp is enabled disabled
69  virtual void updateEnableState() = 0;
70 
71  /// Returns the on-off state of slapcomp.
72  bool isEnabled() const
73  {
74  return getEnableState() && (hasStageFilters() || hasViewportFilters());
75  }
76  /// Returns the enable state
77  bool getEnableState() const { return myEnableState; }
78  /// Returns whether the viewport slapcomp is enabled
79  virtual bool hasStageFilters() const = 0;
80  /// Returns whether the viewport slapcomp is enabled
81  virtual bool hasViewportFilters() const = 0;
82  /// Returns true if the last run of slapcomp had errors.
83  virtual bool hasErrors() const = 0;
84  /// Return target nodes the slapcomp programs this was built for
85  /// (typically size==1, but may be mode if there are chained programs).
86  virtual UT_Array<int> getStageTargetNodes() const = 0;
87  /// Return the viewport target node
88  virtual int getViewportTargetNode() const = 0;
89 
90  /// Sets a callback that gets called when reporting slapcomp errors.
91  virtual void setErrorCallback(void (*cb)(void*, const char*),
92  void *data) = 0;
93  /// Sets a callback that gets called when reporting slapcomp warnings.
94  virtual void setWarningCallback(void (*cb)(void*, const char*),
95  void *data) = 0;
96  /// Sets a callback that gets called when slapcomp is run.
97  virtual void setRunCallback(void (*cb)(void*), void *data) = 0;
98  /// Sets a callback that gets called when slapcomp changes.
99  virtual void setUpdateCallback(void (*cb)(void*), void *data) = 0;
100 
101  /// Returns true if the slapcomp program ouputs an AOV with the given name.
102  /// Should accept "color" as an alias for the "C" AOV.
103  virtual bool isSlapCompAOV(const UT_StringHolder& name) const = 0;
104  /// Should return true if this slapcomp requires the input layers to bring
105  /// in their cameras.
106  virtual bool expectsLayerCameras() const { return false; }
107 
108  /// Optionally builds and then runs slapcomp with the specified inputs and
109  /// context. Returns successfulness.
111  const OP_Context& context,
112  bool should_build=true)
113  {
114  if (should_build && !build(input_fetcher, context))
115  return false;
116  runSlapCompImpl(input_fetcher, context);
117  return true;
118  }
119 
120  /// Prepares for applying slapcomp for the provided set of inputs.
121  bool build(const COP_SlapCompInputFetcher& input_fetcher,
122  const OP_Context& context)
123  {
124  return buildForInputs(input_fetcher.getLayerStructure(), context);
125  }
126  /// Prepares for applying slapcomp for inputs of the given types.
127  virtual bool buildForInputs(const COP_CableStructure& inputs,
128  const OP_Context& context) = 0;
129 
130  /// Should run slapcomp with the specified inputs and context.
131  virtual void runSlapCompImpl(COP_SlapCompInputFetcher& input_fetcher,
132  const OP_Context& context) = 0;
133 
134  /// Should return the outputs that this slapcomp produces.
135  virtual COP_CableStructure
136  getOutputStructure() const = 0;
137 
138  /// Returns a particular output by name.
139  virtual IMX_LayerPtr getOutputLayer(const UT_StringHolder& name) const = 0;
140 
141  /// Returns a version number, which is incremented every time the program is
142  /// rebuilt.
143  virtual int getVersion() const = 0;
144  /// Returns true if the last attempted application of slapcomp may have
145  /// changed the possible outputs. This flag is is reset before each
146  /// application of slapcomp.
147  virtual bool hasStateChanged() const = 0;
148 
149 protected:
150  /// Reports an error (through the callback if possible).
151  virtual void reportError(const char *message) = 0;
152  /// Reports a warning (through the callback if possible).
153  virtual void reportWarning(const char *message) = 0;
154 
155 public:
156  struct Callbacks
157  {
158  /// Callback function to report errors to the outside world.
159  void (*myErrorCB)(void*, const char*) = nullptr;
160  /// Data to send to the error reporting callback.
161  void *myErrorCBData = nullptr;
162  /// Callback function to report warnings to the outside world.
163  void (*myWarningCB)(void*, const char*) = nullptr;
164  /// Data to send to the warning reporting callback.
165  void *myWarningCBData = nullptr;
166  /// Callback function for when the slapcomp program is run.
167  void (*myRunCB)(void*) = nullptr;
168  /// Data to send to the run callback.
169  void *myRunCBData = nullptr;
170  /// Callback function for when the slapcomp program is updated.
171  void (*myUpdateCB)(void*) = nullptr;
172  /// Data to send to the update callback.
173  void *myUpdateCBData = nullptr;
174  };
175  struct Output
176  {
179  };
180 
181  /// Returns an output map constructed from the outputs of the last execution
182  /// of the slapcomp program. This map is safe to use even after the program
183  /// outputs change as a result of building or running it. If the program is
184  /// not enabled or in an error state, the returned map will be empty.
186  getOutputs() const;
187 
188  /// Set all callbacks using a struct.
189  void setCallbacks(const Callbacks &cbs)
190  {
191  setErrorCallback(cbs.myErrorCB, cbs.myErrorCBData);
192  setWarningCallback(cbs.myWarningCB, cbs.myWarningCBData);
193  setRunCallback(cbs.myRunCB, cbs.myRunCBData);
194  setUpdateCallback(cbs.myUpdateCB, cbs.myUpdateCBData);
195  }
196 
197 protected:
198  /// Relays all warnings and messages from the error manager.
199  void reportAllErrorManagerErrors(const UT_ErrorManager&);
200 
201 public:
202  /// This should really be private, but due to difficult visibility rules, we
203  /// just settle for this...
204  /// This should only be called by higher-level managers that interface with
205  /// other managers.
206  virtual void setVersion(int version) = 0;
207 
208 private:
209  bool myEnableState = true;
210 };
211 
212 /// Manages building and running a slapcomp program from a block, including
213 /// handling of errors.
215 {
216 public:
218 
223  delete;
225 
226  /// Enable and set up the slapcomp program.
227  void setStage(const UT_Span<const int> &target_node_id,
228  const OP_Context&) override;
229  /// Enable and set up the slapcomp program for the viewportj
230  void setViewport(const OP_Node *node,
231  const OP_Context &ctx) override;
232  void updateEnableState() override;
233  bool hasStageFilters() const override;
234  bool hasViewportFilters() const override;
235  /// Returns true if the last run of slapcomp had errors.
236  bool hasErrors() const override { return myHasErrors; }
237 
238  UT_Array<int> getStageTargetNodes() const override;
239  int getViewportTargetNode() const override;
240 
241  /// Rebuilds the slapcomp program if necessary. Any errors or warnings are
242  /// reported through the callbacks.
243  /// @see setErrorCallback
244  /// @see setWarningCallback
246  const OP_Context&) override;
247 
248  /// Run the slapcomp program using the latest build. It is likely you want
249  /// to rebuild the program prior to running it. Any errors or warnings are
250  /// reported through the callbacks.
251  /// @see buildProgram
252  /// @see setErrorCallback
253  /// @see setWarningCallback
255  const OP_Context&) override;
256 
257  /// Returns the specified slapcomp output.
258  IMX_LayerPtr getOutputLayer(const UT_StringHolder& name) const override;
259 
260  /// Returns true if the slapcomp program ouputs an AOV with the given name.
261  /// Accepts "color" as an alias for the "C" AOV.
262  bool isSlapCompAOV(
263  const UT_StringHolder &name)
264  const override;
265  bool expectsLayerCameras() const override;
266  COP_CableStructure getOutputStructure() const override;
267 
268  /// Get the managed slapcomp program.
269  const COP_ApexProgram &getProgram(exint i=0) const { return *myProgram; }
270 
271  /// If this returns true, then the last attempted application of slapcomp
272  /// may have changed the possible outputs.
273  /// This flag is reset before each application of slapcomp.
274  bool hasStateChanged() const override
275  { return myStateChanged; }
276 
277  /// SlapComp program version number that is incremented each time the
278  /// program is sucessfully rebuilt.
279  int getVersion() const override
280  { return myVersion; }
281 
282  /// Sets a callback that gets called when reporting errors.
283  void setErrorCallback(void (*cb)(void*, const char*),
284  void *data) override
285  {
286  myCallbacks.myErrorCB = cb;
287  myCallbacks.myErrorCBData = data;
288  }
289  /// Sets a callback that gets called when reporting warnings.
290  void setWarningCallback(void (*cb)(void*, const char*),
291  void *data) override
292  {
293  myCallbacks.myWarningCB = cb;
294  myCallbacks.myWarningCBData = data;
295  }
296  /// Sets a callback that gets called when the slapcomp program is run.
297  void setRunCallback(void (*cb)(void*), void *data) override
298  {
299  myCallbacks.myRunCB = cb;
300  myCallbacks.myRunCBData = data;
301  }
302  /// Sets a callback that gets called when the slapcomp program is updated.
303  void setUpdateCallback(void (*cb)(void*), void *data) override
304  {
305  myCallbacks.myUpdateCB = cb;
306  myCallbacks.myUpdateCBData = data;
307  }
308 
309 // semi-public:
310  /// Called by the slapcomp program callback when it is updated.
311  void update(const OP_Context&);
312 
313  void setVersion(int version) override
314  { myVersion = version; }
315 
316 protected:
317  void reportError(const char *message) override;
318  void reportWarning(const char *message) override;
319 
320 private:
321  /// Resets the state and error flags.
322  void clearFlags();
323 
324  /// Run a single program
325  bool runSingleProgram(COP_ApexProgram *pgm,
326  COP_SlapCompInputFetcher &input_fetcher,
327  const OP_Context &context,
328  UT_Optional<UT_Vector2I> &input_wh);
329 
330  /// Reports an error if an output layer does not match the input size.
331  /// This is to ensure that all AOVs have the same size.
332  void reportOutputSizeErrors(
333  UT_Vector2I input_size);
334 
335  /// The slapcomp program.
337  UT_UniquePtr<COP_ApexProgram> myViewportProgram;
338 
339  /// Incremented each time the program is sucessfully rebuilt.
340  int myVersion;
341  /// Enable state of slapcomp.
342  bool myStageSet;
343  bool myViewportSet;
344  /// If this flag is true, last application of slapcomp incurred a rebuild of
345  /// the program that may have changed the available output layers.
346  bool myStateChanged;
347  /// If this flag is true, last application of slapcomp had errors and
348  /// failed.
349  bool myHasErrors;
350 
351  Callbacks myCallbacks;
352 };
353 
void(* myUpdateCB)(void *)
Callback function for when the slapcomp program is updated.
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2543
void * myErrorCBData
Data to send to the error reporting callback.
int getVersion(int version)
Definition: ImfVersion.h:99
void
Definition: png.h:1083
Abstract interface for managing slapcomp.
GLboolean * data
Definition: glcorearb.h:131
const COP_ApexProgram & getProgram(exint i=0) const
Get the managed slapcomp program.
virtual void reportWarning(const char *message)=0
Reports a warning (through the callback if possible).
void(* myWarningCB)(void *, const char *)
Callback function to report warnings to the outside world.
virtual COP_CableStructure getOutputStructure() const =0
Should return the outputs that this slapcomp produces.
int64 exint
Definition: SYS_Types.h:125
virtual bool expectsLayerCameras() const
virtual IMX_LayerPtr getOutputLayer(const UT_StringHolder &name) const =0
Returns a particular output by name.
void(* myRunCB)(void *)
Callback function for when the slapcomp program is run.
virtual bool isSlapCompAOV(const UT_StringHolder &name) const =0
virtual COP_CableStructure getLayerStructure() const =0
Returns types and names of all inputs available to this fetcher.
void * myUpdateCBData
Data to send to the update callback.
std::optional< T > UT_Optional
Definition: UT_Optional.h:26
void setCallbacks(const Callbacks &cbs)
Set all callbacks using a struct.
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
virtual bool hasStageFilters() const =0
Returns whether the viewport slapcomp is enabled.
virtual void updateEnableState()=0
Called when slapcomp is enabled disabled.
void setUpdateCallback(void(*cb)(void *), void *data) override
Sets a callback that gets called when the slapcomp program is updated.
virtual void setStage(const UT_Span< const int > &target_nodes, const OP_Context &)=0
Set the image filters defined on the USD Stage.
UT_SharedPtr< const IMX_Layer > IMX_LayerConstPtr
Definition: IMX_Layer.h:28
OPENVDB_API void setVersion(std::ios_base &, const VersionId &libraryVersion, uint32_t fileVersion)
Associate specific file format and library version numbers with the given stream. ...
virtual void runSlapCompImpl(COP_SlapCompInputFetcher &input_fetcher, const OP_Context &context)=0
Should run slapcomp with the specified inputs and context.
virtual UT_Array< int > getStageTargetNodes() const =0
void setRunCallback(void(*cb)(void *), void *data) override
Sets a callback that gets called when the slapcomp program is run.
void * myWarningCBData
Data to send to the warning reporting callback.
void setErrorCallback(void(*cb)(void *, const char *), void *data) override
Sets a callback that gets called when reporting errors.
GLuint const GLchar * name
Definition: glcorearb.h:786
void(* myErrorCB)(void *, const char *)
Callback function to report errors to the outside world.
virtual void setViewport(const OP_Node *node, const OP_Context &ctx)=0
COP_SlapCompManager & operator=(const COP_SlapCompManager &)=delete
bool build(const COP_SlapCompInputFetcher &input_fetcher, const OP_Context &context)
Prepares for applying slapcomp for the provided set of inputs.
GT_API const UT_StringHolder version
bool getEnableState() const
Returns the enable state.
COP_Type
Types of basic data that are passed around a COP network.
Definition: COP_Signature.h:17
virtual void reportError(const char *message)=0
Reports an error (through the callback if possible).
void setVersion(int version) override
void * myRunCBData
Data to send to the run callback.
virtual bool buildForInputs(const COP_CableStructure &inputs, const OP_Context &context)=0
Prepares for applying slapcomp for inputs of the given types.
virtual int getViewportTargetNode() const =0
Return the viewport target node.
virtual bool hasViewportFilters() const =0
Returns whether the viewport slapcomp is enabled.
Interface for getting IMX_Layer inputs for a slapcomp program on demand.
LeafData & operator=(const LeafData &)=delete
A global error manager scope.
bool isEnabled() const
Returns the on-off state of slapcomp.
#define COP_API
Definition: COP_API.h:8
bool hasErrors() const override
Returns true if the last run of slapcomp had errors.
bool runSlapComp(COP_SlapCompInputFetcher &input_fetcher, const OP_Context &context, bool should_build=true)
void disable()
Disable all slapcomp.
state
Definition: core.h:2289
UT_SharedPtr< IMX_Layer > IMX_LayerPtr
Definition: IMX_Layer.h:27
Definition: format.h:1821
void clearAllFilters()
Clear all filters.
void setWarningCallback(void(*cb)(void *, const char *), void *data) override
Sets a callback that gets called when reporting warnings.