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.
175  OP_Node *srcnode,
176  FS_IndexFile *hda,
177  const char *context_mask) override;
178 
179  /// Utility function for cleaning up HDA of any compiled sections.
180  static void removeVopCodeSections( FS_IndexFile * hda );
181 
182 
183 protected:
184  /// Sets up class members for compilation an encapsulated vop.
185  bool initForVopCompilation( OP_Node * srcnode );
186 
187  /// @{
188  /// Accessors for the member data, available to derived classes.
190  { return myHDAFile; }
192  { return myVopNode; }
194  { return myCodeGenerator; }
195  /// @}
196 
197 private:
198  // Obtains vfl, compiles it into vex, and adds both to HDA.
199  bool compileVopCodeAndAddToHDA(VOP_ContextType vop_context);
200 
201  bool getVexCode(VOP_ContextType vop_context,
202  std::ostream &vex_stream);
203 
204  bool getContextsFromMask(const char *context_mask,
205  VOP_ContextTypeList &context_types);
206 
207 private:
208  // Member variables used during compilation
209  FS_IndexFile * myHDAFile; // HDA definition file
210  VOP_Node * myVopNode; // src node
211  VOP_CodeGenerator * myCodeGenerator; // code gen to use
212 };
213 
214 // ===========================================================================
215 /// A class that compiles a shader (or vop-based operator like Vop SOP),
216 /// into a script-based HDA (with or without the contents network).
218 {
219 public:
220  /// Constructor
222 
223  /// @{
224  /// Configures the file path where PRMan shaders are located.
225  void setRManPath( const char * path )
226  { myRManPath.harden( path ); }
227  const UT_String & getRManPath() const
228  { return myRManPath; }
229  /// @}
230 
231  /// Utility function for cleaning up HDA of any compiled sections.
232  static void removeShaderCodeSections( FS_IndexFile * file );
233 
234 protected:
235  /// @{
236  /// Accessors for the member data, available to derived classes.
237  OP_Node * getNode() const
238  { return myNode; }
240  { return myCodeGenerator; }
242  { return myTable; }
243  /// @}
244 
245  /// Sets up member variables to be ready for compilation.
246  bool initForShaderCompilation( OP_Node * srcnode );
247 
248  /// Obtains the context types for which to compile the code.
249  /// The subclass needs to implement this virtual to provide that
250  /// information.
251  virtual void getContextTypesForCompilation(
252  VOP_ContextTypeList & contexts ) const = 0;
253 
254  /// Compiles the given context type of the source node to the HDA in OTL.
255  /// @param code_generator The code generator to use for compilation.
256  /// @param context_type The context for which to compile the code.
257  /// @param section_name The name of the HDA section in which to save
258  /// the compiled code.
259  /// @param output_node The node to use for generating the code,
260  /// in case there are several output nodes
261  /// for the same context.
262  virtual void doCompile( FS_IndexFile *hda,
263  VOP_CodeGenerator * code_generator,
264  VOP_ContextType context_type,
265  const char * section_name,
266  OP_Node * output_node ) = 0;
267 
268 protected:
269  /// Compiles the vex code for the Output VOP nodes. If there are a few
270  /// oputput nodes for the same context (connecting to collect vop via
271  /// a switch), then the currently selected one is used.
272  /// @return Returns true if the HDA definition was actually created
273  /// (it may not be if there are no primary output nodes).
274  bool compilePrimaryOutputNodes(
275  FS_IndexFile *hda,
276  opui_ScriptMaterialCodeMapper & mapper );
277 
278  /// Inspects the mapper for vop nodes it has mapped and compiles vex code
279  /// associated with each mapped node. The code is saved to HDA section.
280  void compileMappedNodes(
281  FS_IndexFile *hda,
282  opui_ScriptMaterialCodeMapper & mapper );
283 
284 private:
285  /// Directory where PRMan shaders are saved
286  UT_String myRManPath;
287 
288  // Member variables used during compilation
289  OP_Node * myNode; // src node
290  VOP_CodeGenerator * myCodeGenerator; // code gen to use
291  OP_OperatorTable * myTable; // HDA table
292 };
293 
294 //=============================================================================
295 /// A class that compiles a shader into a new script-based shader HDA (ie, u
296 /// no contents network) and saves it in OTL.
298 {
299 public:
300  /// Constructor
302 
303  /// Compiles a given node to a new script-based HDA (ie, the content nodes
304  /// are not saved; only the vex code that they generate is saved)
305  /// This is used for HDAs that don't have contents network, and only use
306  /// the compiled code for cooking.
307  /// @param srcnode The node from which to create the new HDA.
308  /// @param name The name of the new operator.
309  /// @param label The label of the new operator.
310  /// @param path The file path of the OTL to which the new HDA is saved.
311  /// @param metasrc The meta source for the OTL containing new HDA.
312  /// @param types The shader context types to save. If NULL, all
313  /// implemented context types are saved.
314  /// @return True if creation of new HDA succeeded. On failure,
315  /// false is returned and the member error message is set.
317  OP_Node *srcnode,
318  const char *name,
319  const char *label,
320  const char *path,
321  const char *metasrc,
322  const VOP_ContextTypeList *types = nullptr
323  ) override;
324 
325 protected:
326  /// Obtains the context types for which to compile the code.
328  VOP_ContextTypeList &contexts) const override;
329 
330  /// Compiles the given context type of the source node to the HDA in OTL.
331  void doCompile( FS_IndexFile *hda,
332  VOP_CodeGenerator * code_generator,
333  VOP_ContextType context_type,
334  const char * section_name,
335  OP_Node * output_node ) override;
336 
337 private:
338  FS_IndexFile *createLibrarySection(OP_OTLLibrary &otl,
339  UT_String &otl_section);
340 
341  /// Compiles new HDA based on the gathered info.
342  void compileAddToLibraryAndReload();
343 
344  /// Compiles the HDA and saves it to the library
345  void compileAndAddToLibrary();
346  void addShaderInfoSection(FS_IndexFile &hda);
347 
348  /// Returns the render mask string for the HDA.
349  const char *getRenderMask() const;
350 
351 private:
352  UT_String myOpName; // HDA name
353  UT_String myLabel; // HDA label
354  UT_String myPath; // OTL file path
355  bool myIsEmbedded; // OTL is embedded library
356  UT_String myMetaSrc; // OTL meta source
357  const VOP_ContextTypeList *myContextTypes; // requested context types
358 };
359 
360 //=============================================================================
361 /// A class that compiles a shader and saves it into an existing shader HDA.
363 {
364 public:
365  /// Constructor
367 
368  /// Compiles a given node to the sections of a given HDA (already existing).
369  /// This is used for HDAs that have contents network, but also want to store
370  /// cached vfl/vex code.
371  /// @param srcnode The node from which to obtain vex code.
372  /// @param hda The HDA definition to which save the vfl and vex code.
374  OP_Node *srcnode,
375  FS_IndexFile *hda,
376  const char *context_mask) override;
377 
378 protected:
379  /// Obtains the context types for which to compile the code.
381  VOP_ContextTypeList &contexts) const override;
382 
383  /// Compiles the given context type of the source node to the HDA in OTL.
384  void doCompile( FS_IndexFile *hda,
385  VOP_CodeGenerator * code_generator,
386  VOP_ContextType context_type,
387  const char * section_name,
388  OP_Node * output_node ) override;
389 
390 private:
391  /// Compiles the vex to HDA sections.
392  void compileAndAddToHDA();
393 
394  /// Saves the vop network meta-data to the HDA sectin.
395  void addInfoToHDA( const char * mat_info );
396 
397 private:
398  FS_IndexFile * myHDADefFile; // HDA definition
399 };
400 
401 // ===========================================================================
402 /// A convenience class that automatically chooses a suitable compiler object
403 /// given the node type and the kind of HDA (network-based or code-only).
405 {
406 public:
407  /// Constructor that initializes the object to use an instance of
408  /// an HDA code compiler object that is suitable for compiling vfl/vex code.
409  /// @parm srcnode The HDA node whose network is going to be compiled
410  /// into the vex code.
411  /// @parm for_network_hda If true, the compiler will be able to
412  /// compileVexCodeToSections(), ie it is suitable for
413  /// network-based HDAs with additional cached code sections.
414  /// Otherwise, it will be able to compileNewOperatorType(),
415  /// ie it is suitable for code-based HDAs (with no network).
416  VOP_AutoHDACodeCompiler( const OP_Node *srcnode,
417  bool for_network_hda,
418  const char *shader_name = nullptr);
420 
421  /// Obtain the underlying code compiler.
423  { return myCodeCompiler; }
424 
425 private:
426  VOP_HDACodeCompiler * myCodeCompiler; // instance of HDA code compiler
427 };
428 
429 #endif
430 
const UT_String & getRManPath() const
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2543
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
OP_Node * getNode() const
VOP_Node * getVopNode() const
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
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:177
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:786
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:2542
virtual bool compileVexCodeToSections(OP_Node *srcnode, FS_IndexFile *hda, const char *context_mask=0)
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