HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VOP_HDACodeCompiler.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  * NAME: VOP_HDACodeCompiler.h ( VOP Library, C++)
7  *
8  * COMMENTS: A class that compiles a node into a script-based HDA.
9  * There are two separate and distinct compilation types:
10  * - compiling whole network into a shader (or vex-based op,
11  * such as Vop SOP or Vop COP, etc)
12  * - compiling a vop subnet into an encapsulated Vop operator
13  * that represents a function call inside a shader
14  */
15 
16 
17 #ifndef __VOP_HDACodeCompiler__
18 #define __VOP_HDACodeCompiler__
19 
20 #include "VOP_API.h"
21 #include <UT/UT_Error.h>
22 #include <UT/UT_SharedPtr.h>
23 #include <UT/UT_String.h>
24 #include <UT/UT_StringMap.h>
25 #include <iosfwd>
26 #include "VOP_Types.h"
27 
28 class FS_IndexFile;
29 class OP_Node;
30 class OP_OperatorTable;
31 class VOP_Node;
32 class VOP_CodeGenerator;
33 class VCC_Compiler;
34 class opui_ScriptMaterialCodeMapper;
35 class OP_OTLLibrary;
36 
37 // ===========================================================================
38 /// A class that compiles a node into a script-based HDA.
39 /// This is a base class that presents two interface functions,
40 /// compileVexCodeToSections() and compileNewOperatorType() and the derived
41 /// class should implement at least one of them.
43 {
44 public:
46  virtual ~VOP_HDACodeCompiler();
47 
48  /// @{ Main shader name used in compilation.
49  void setShaderName(const char *shader_name)
50  { myShaderName = shader_name; }
52  { return myShaderName; }
53  /// @}
54 
55  /// @{
56  /// Returns an error or a warning message from the last compilation.
57  UT_ErrorSeverity getStatus(UT_String &message, UT_String *details = 0);
58  /// @}
59 
60  /// @{
61  /// Configures the flag to force creation of the new operator even
62  /// if there is already one by that name.
64  { myForceOnOpNameConflict = flag; }
66  { return myForceOnOpNameConflict; }
67  /// @}
68 
69  /// Tests if the node can be compiled into vex code sections.
70  /// @return True if node can be compiled; otherwise, false.
71  static bool canCompileNode( OP_Node * srcnode );
72 
73  /// Utility function for cleaning up HDA of any compiled sections.
74  /// @parm node The node defined by the HDA saved in file. It is used
75  /// to determine the section name convention for the given
76  /// type of HDA (shader vs. vop).
77  static void removeCodeSections( OP_Node *node, FS_IndexFile *file );
78 
79  /// Utility function for compiling node to code sections, but also
80  /// to update function name section if needed.
81  /// Returns true on success; false otherwise (along with error message)
82  static bool updateCodeSections( OP_Node *node, FS_IndexFile *file,
84 
85  /// Compiles a given node to the sections of a given HDA (already existing).
86  /// This is used for HDAs that have contents network, but also want to store
87  /// cached vfl/vex code.
88  /// @param srcnode The node from which to obtain vex code.
89  /// @param hda The HDA definition to which save the vfl and vex code.
90  virtual bool compileVexCodeToSections( OP_Node * srcnode,
91  FS_IndexFile * hda,
92  const char *context_mask = 0);
93 
94  /// Compiles a given node to a new script-based HDA (ie, the content nodes
95  /// are not saved; only the vex code that they generate is saved).
96  /// This is used for HDAs that don't have contents network, and only use
97  /// the compiled code for cooking.
98  /// @param srcnode The node from which to create the new HDA.
99  /// @param name The name of the new operator.
100  /// @param label The label of the new operator.
101  /// @param path The file path of the OTL to which the new HDA is saved.
102  /// @param metasrc The meta source for the OTL containing new HDA.
103  /// @param types The shader context types to save. If NULL, all
104  /// implemented context types are saved.
105  /// @return True if creation of new HDA succeeded. On failure,
106  /// false is returned and the member error message is set.
107  virtual bool compileNewOperatorType( OP_Node * srcnode,
108  const char *name,
109  const char *label,
110  const char *path,
111  const char *metasrc,
112  const VOP_ContextTypeList * types = NULL );
113 
114  /// Obtains info about imported shaders that needed to be also compiled
115  /// and saved in HDA sections. Eg, imported shaders that are implemented
116  /// as nested Material Builders (or HDAs based on them).
117  /// The keys are the shader function names and mapped values are
118  /// the section names where they are stored.
120  { return myFunctionsSectionsMap; }
121 
122 protected:
123  /// Saves the code to section.
124  void saveVflCodeToSection(FS_IndexFile *hda,
125  OP_Node *shader_node,
126  VOP_CodeGenerator * code_generator,
127  VOP_ContextType context_type,
128  const char * section_name,
129  OP_Node * output_node );
130 
131  /// @{
132  /// Allow subclasses to set the error and warning messages.
133  void setStatus(UT_ErrorSeverity sev, const char *msg,
134  const char *details = 0);
135 
136  void clearStatus();
137  /// @}
138 
139  /// Checks if there was an error during compilation up till now.
140  bool isOK() const
141  { return mySeverity < UT_ERROR_ABORT; }
142 
143 private:
144  /// Shader name used for compiled shader.
145  UT_StringHolder myShaderName;
146 
147  /// The string containing an error message from the last compile attempt.
148  UT_ErrorSeverity mySeverity;
149  UT_String myStatusMessage;
150  UT_String myStatusDetails;
151 
152  /// Continue with creation of the new operator even if there is already
153  /// one by this name.
154  bool myForceOnOpNameConflict;
155 
156  /// A map from shader function name to code section name that contains
157  /// the shader code.
158  UT_StringMap<UT_StringHolder> myFunctionsSectionsMap;
159 };
160 
161 // ===========================================================================
162 /// A class that compiles the encapsulated function vop code into HDA section.
164 {
165 public:
166  /// Constructor.
168 
169  /// Compiles a given node to the sections of a given HDA (already existing).
170  /// This is used for HDAs that have contents network, but also want to store
171  /// cached vfl/vex code.
172  /// @param srcnode The node from which to obtain vex code.
173  /// @param hda The HDA definition to which save the vfl and vex code.
174  virtual bool compileVexCodeToSections( OP_Node * srcnode,
175  FS_IndexFile * hda,
176  const char *context_mask);
177 
178  /// Utility function for cleaning up HDA of any compiled sections.
179  static void removeVopCodeSections( FS_IndexFile * hda );
180 
181 
182 protected:
183  /// Sets up class members for compilation an encapsulated vop.
184  bool initForVopCompilation( OP_Node * srcnode );
185 
186  /// @{
187  /// Accessors for the member data, available to derived classes.
189  { return myHDAFile; }
191  { return myVopNode; }
193  { return myCodeGenerator; }
194  /// @}
195 
196 private:
197  // Obtains vfl, compiles it into vex, and adds both to HDA.
198  bool compileVopCodeAndAddToHDA(VOP_ContextType vop_context);
199 
200  bool getVexCode(VOP_ContextType vop_context,
201  std::ostream &vex_stream);
202 
203  bool getContextsFromMask(const char *context_mask,
204  VOP_ContextTypeList &context_types);
205 
206 private:
207  // Member variables used during compilation
208  FS_IndexFile * myHDAFile; // HDA definition file
209  VOP_Node * myVopNode; // src node
210  VOP_CodeGenerator * myCodeGenerator; // code gen to use
211 };
212 
213 // ===========================================================================
214 /// A class that compiles a shader (or vop-based operator like Vop SOP),
215 /// into a script-based HDA (with or without the contents network).
217 {
218 public:
219  /// Constructor
221 
222  /// @{
223  /// Configures the file path where PRMan shaders are located.
224  void setRManPath( const char * path )
225  { myRManPath.harden( path ); }
226  const UT_String & getRManPath() const
227  { return myRManPath; }
228  /// @}
229 
230  /// Utility function for cleaning up HDA of any compiled sections.
231  static void removeShaderCodeSections( FS_IndexFile * file );
232 
233 protected:
234  /// @{
235  /// Accessors for the member data, available to derived classes.
236  OP_Node * getNode() const
237  { return myNode; }
239  { return myCodeGenerator; }
241  { return myTable; }
242  /// @}
243 
244  /// Sets up member variables to be ready for compilation.
245  bool initForShaderCompilation( OP_Node * srcnode );
246 
247  /// Obtains the context types for which to compile the code.
248  /// The subclass needs to implement this virtual to provide that
249  /// information.
250  virtual void getContextTypesForCompilation(
251  VOP_ContextTypeList & contexts ) const = 0;
252 
253  /// Compiles the given context type of the source node to the HDA in OTL.
254  /// @param code_generator The code generator to use for compilation.
255  /// @param context_type The context for which to compile the code.
256  /// @param section_name The name of the HDA section in which to save
257  /// the compiled code.
258  /// @param output_node The node to use for generating the code,
259  /// in case there are several output nodes
260  /// for the same context.
261  virtual void doCompile( FS_IndexFile *hda,
262  VOP_CodeGenerator * code_generator,
263  VOP_ContextType context_type,
264  const char * section_name,
265  OP_Node * output_node ) = 0;
266 
267 protected:
268  /// Compiles the vex code for the Output VOP nodes. If there are a few
269  /// oputput nodes for the same context (connecting to collect vop via
270  /// a switch), then the currently selected one is used.
271  /// @return Returns true if the HDA definition was actually created
272  /// (it may not be if there are no primary output nodes).
273  bool compilePrimaryOutputNodes(
274  FS_IndexFile *hda,
275  opui_ScriptMaterialCodeMapper & mapper );
276 
277  /// Inspects the mapper for vop nodes it has mapped and compiles vex code
278  /// associated with each mapped node. The code is saved to HDA section.
279  void compileMappedNodes(
280  FS_IndexFile *hda,
281  opui_ScriptMaterialCodeMapper & mapper );
282 
283 private:
284  /// Directory where PRMan shaders are saved
285  UT_String myRManPath;
286 
287  // Member variables used during compilation
288  OP_Node * myNode; // src node
289  VOP_CodeGenerator * myCodeGenerator; // code gen to use
290  OP_OperatorTable * myTable; // HDA table
291 };
292 
293 //=============================================================================
294 /// A class that compiles a shader into a new script-based shader HDA (ie, u
295 /// no contents network) and saves it in OTL.
297 {
298 public:
299  /// Constructor
301 
302  /// Compiles a given node to a new script-based HDA (ie, the content nodes
303  /// are not saved; only the vex code that they generate is saved)
304  /// This is used for HDAs that don't have contents network, and only use
305  /// the compiled code for cooking.
306  /// @param srcnode The node from which to create the new HDA.
307  /// @param name The name of the new operator.
308  /// @param label The label of the new operator.
309  /// @param path The file path of the OTL to which the new HDA is saved.
310  /// @param metasrc The meta source for the OTL containing new HDA.
311  /// @param types The shader context types to save. If NULL, all
312  /// implemented context types are saved.
313  /// @return True if creation of new HDA succeeded. On failure,
314  /// false is returned and the member error message is set.
315  virtual bool compileNewOperatorType( OP_Node * srcnode,
316  const char *name,
317  const char *label,
318  const char *path,
319  const char *metasrc,
320  const VOP_ContextTypeList * types = NULL );
321 
322 protected:
323  /// Obtains the context types for which to compile the code.
324  virtual void getContextTypesForCompilation(
325  VOP_ContextTypeList & contexts ) const;
326 
327  /// Compiles the given context type of the source node to the HDA in OTL.
328  virtual void doCompile( FS_IndexFile *hda,
329  VOP_CodeGenerator * code_generator,
330  VOP_ContextType context_type,
331  const char * section_name,
332  OP_Node * output_node );
333 
334 private:
335  FS_IndexFile *createLibrarySection(OP_OTLLibrary &otl,
336  UT_String &otl_section);
337 
338  /// Compiles new HDA based on the gathered info.
339  void compileAddToLibraryAndReload();
340 
341  /// Compiles the HDA and saves it to the library
342  void compileAndAddToLibrary();
343  void addShaderInfoSection(FS_IndexFile &hda);
344 
345  /// Returns the render mask string for the HDA.
346  const char *getRenderMask() const;
347 
348 private:
349  UT_String myOpName; // HDA name
350  UT_String myLabel; // HDA label
351  UT_String myPath; // OTL file path
352  bool myIsEmbedded; // OTL is embedded library
353  UT_String myMetaSrc; // OTL meta source
354  const VOP_ContextTypeList *myContextTypes; // requested context types
355 };
356 
357 //=============================================================================
358 /// A class that compiles a shader and saves it into an existing shader HDA.
360 {
361 public:
362  /// Constructor
364 
365  /// Compiles a given node to the sections of a given HDA (already existing).
366  /// This is used for HDAs that have contents network, but also want to store
367  /// cached vfl/vex code.
368  /// @param srcnode The node from which to obtain vex code.
369  /// @param hda The HDA definition to which save the vfl and vex code.
370  virtual bool compileVexCodeToSections( OP_Node * srcnode,
371  FS_IndexFile * hda,
372  const char *context_mask);
373 
374 protected:
375  /// Obtains the context types for which to compile the code.
376  virtual void getContextTypesForCompilation(
377  VOP_ContextTypeList & contexts ) const;
378 
379  /// Compiles the given context type of the source node to the HDA in OTL.
380  virtual void doCompile( FS_IndexFile *hda,
381  VOP_CodeGenerator * code_generator,
382  VOP_ContextType context_type,
383  const char * section_name,
384  OP_Node * output_node );
385 
386 private:
387  /// Compiles the vex to HDA sections.
388  void compileAndAddToHDA();
389 
390  /// Saves the vop network meta-data to the HDA sectin.
391  void addInfoToHDA( const char * mat_info );
392 
393 private:
394  FS_IndexFile * myHDADefFile; // HDA definition
395 };
396 
397 // ===========================================================================
398 /// A convenience class that automatically chooses a suitable compiler object
399 /// given the node type and the kind of HDA (network-based or code-only).
401 {
402 public:
403  /// Constructor that initializes the object to use an instance of
404  /// an HDA code compiler object that is suitable for compiling vfl/vex code.
405  /// @parm srcnode The HDA node whose network is going to be compiled
406  /// into the vex code.
407  /// @parm for_network_hda If true, the compiler will be able to
408  /// compileVexCodeToSections(), ie it is suitable for
409  /// network-based HDAs with additional cached code sections.
410  /// Otherwise, it will be able to compileNewOperatorType(),
411  /// ie it is suitable for code-based HDAs (with no network).
412  VOP_AutoHDACodeCompiler( const OP_Node *srcnode,
413  bool for_network_hda,
414  const char *shader_name = nullptr);
416 
417  /// Obtain the underlying code compiler.
419  { return myCodeCompiler; }
420 
421 private:
422  VOP_HDACodeCompiler * myCodeCompiler; // instance of HDA code compiler
423 };
424 
425 #endif
426 
const UT_String & getRManPath() const
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2544
OP_Node * getNode() const
VOP_Node * getVopNode() const
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
OP_OperatorTable * getTable() const
VOP_HDACodeCompiler * getCC() const
Obtain the underlying code compiler.
UT_ErrorSeverity
Definition: UT_Error.h:25
virtual void getContextTypesForCompilation(VOP_ContextTypeList &contexts) const =0
int VOP_ContextType
Definition: VOP_Types.h:149
void setShaderName(const char *shader_name)
Main shader name used in compilation.
const UT_StringMap< UT_StringHolder > & getFunctionsSectionsMap() const
#define VOP_API
Definition: VOP_API.h:10
A class that compiles the encapsulated function vop code into HDA section.
bool isOK() const
Checks if there was an error during compilation up till now.
void setRManPath(const char *path)
virtual bool compileNewOperatorType(OP_Node *srcnode, const char *name, const char *label, const char *path, const char *metasrc, const VOP_ContextTypeList *types=NULL)
const UT_StringHolder & getShaderName() const
Main shader name used in compilation.
GLuint const GLchar * name
Definition: glcorearb.h:785
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2542
FS_IndexFile * getHDAFile() const
VOP_CodeGenerator * getCodeGenerator() const
A class that compiles a shader and saves it into an existing shader HDA.
void setForceCompilationOnOpNameConflict(bool flag)
bool getForceCompilationOnOpNameConflict()
GLsizei GLenum GLenum * types
Definition: glcorearb.h:2541
virtual bool compileVexCodeToSections(OP_Node *srcnode, FS_IndexFile *hda, const char *context_mask=0)
png_infop png_uint_32 flag
Definition: png.h:2242
VOP_CodeGenerator * getCodeGenerator() const
virtual void doCompile(FS_IndexFile *hda, VOP_CodeGenerator *code_generator, VOP_ContextType context_type, const char *section_name, OP_Node *output_node)=0