HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HUSD_ShaderTranslator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Side Effects Software Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #ifndef __HUSD_ShaderTranslator_h__
19 #define __HUSD_ShaderTranslator_h__
20 
21 #include "HUSD_API.h"
22 
23 #include <VOP/VOP_Types.h>
24 #include <UT/UT_StringArray.h>
25 #include <UT/UT_IntArray.h>
26 
27 #include <utility>
28 
29 class HUSD_AutoWriteLock;
30 class HUSD_TimeCode;
31 class OP_Node;
32 
33 
34 /// Creates a USD shader primitives from Houdini's nodes.
36 {
37 public:
38  /// Standard virtual destructor for this abstract base class.
39  virtual ~HUSD_ShaderTranslator() = default;
40 
41 
42  /// Returns true if the translator can encode a shader that reports
43  /// a given render mask (ie, is a shader for a given render target).
44  virtual bool matchesRenderMask( const UT_StringRef &render_mask ) = 0;
45 
46  /// @{ Methods that delineate the authoring of a USD material primitive.
47  /// The first method is called before this translator is asked to translate
48  /// any shader into the given material, while the second one is called after
49  /// all relevant shader have been translated.
50  /// This allows the translator to do some preparatory or cleanup work.
51  /// For example to configure the USD material, or to reset any caches.
52  virtual void beginMaterialTranslation( HUSD_AutoWriteLock &lock,
53  const UT_StringRef &usd_material_path );
54  virtual void endMaterialTranslation(HUSD_AutoWriteLock &lock,
55  const UT_StringRef &usd_material_path );
56  /// @}
57 
58  /// Defines a USD shader primitive that is part of the USD material.
59  /// Ie, the translator will connect the shader to the material output.
60  ///
61  /// @p usd_material_path - path to the material primitive in which
62  /// the shader primitive should be created.
63  /// @p time_code - time code at which to evaluate any properties
64  /// @p shader_node - the Houdini node that represents a shader and that
65  /// needs to be translated into a USD shader primitive
66  /// @p shader_type - some VOPs contains several shaders (eg material
67  /// builders). So this parameters specifies the type of the shader
68  /// to pick and translate.
69  /// @p output_name - the output name of the VOP node that represents
70  /// the shader to pick and translate. It can be an empty string,
71  /// if the VOP node does not have shader outputs.
72  virtual void createMaterialShader( HUSD_AutoWriteLock &lock,
73  const UT_StringRef &usd_material_path,
74  const HUSD_TimeCode &time_code,
75  OP_Node &shader_node,
76  VOP_Type shader_type,
77  const UT_StringRef &output_name) = 0;
78 
79  /// Defines a USD shader primitive that is part of a shader network chain.
80  /// Ie, the translator will create a shader primitive output, that the
81  /// caller can use to connect as an input to another shader.
82  ///
83  /// @p usd_material_path - path to the material primitive in which
84  /// the shader primitive should be created.
85  /// @p usd_parent_path - path to the primitive inside which
86  /// the shader primitive should be created directly.
87  /// @p usd_shader_name - name of the shader primitive to create,
88  /// (may be an empty string, in which case use node name, etc).
89  /// @p time_code - time code at which to evaluate any properties
90  /// @p shader_node - the Houdini node that represents a shader and that
91  /// needs to be translated into a USD shader primitive
92  /// @p output_name - the output name of the VOP node that needs to be
93  /// translated into USD shader output. This is the output
94  /// the caller is interested in having representation in USD.
95  ///
96  /// @return The path to the USD shader output attribute corresponding
97  /// to the @p output_name connector on the @p shader_node.
99  const UT_StringRef &usd_material_path,
100  const UT_StringRef &usd_parent_path,
101  const UT_StringRef &usd_shader_name,
102  const HUSD_TimeCode &time_code,
103  OP_Node &shader_node,
104  const UT_StringRef &output_name) = 0;
105 
106 
107  /// Re-translates the shader parameters given the shader VOP node (and its
108  /// new parameter values).
109  /// @p usd_shader_path - the path to the USD shader primitive whose
110  /// input attributes need updating due to node parm value change.
111  /// @p time_code - time code at which to evaluate any properties
112  /// @p shader_node - Houdini node that represents a shader that
113  /// needs to be re-translated into the given USD shader primitive.
114  /// @p parameter_names - the list of parameters that have changed.
115  /// If the list is empty, then any of the node's parameters may
116  /// have changed. If it's not empty, then only listed parameters
117  /// have changed.
118  virtual void updateShaderParameters( HUSD_AutoWriteLock &lock,
119  const UT_StringRef &usd_shader_path,
120  const HUSD_TimeCode &time_code,
121  OP_Node &shader_node,
122  const UT_StringArray &parameter_names) = 0;
123 
124  /// Returns the name of the renderer (render context name) that
125  /// should be used in the material output name for that USD shader.
126  virtual UT_StringHolder getRenderContextName( OP_Node &shader_node,
127  const UT_StringRef &output_name) = 0;
128 
129 
130  /// Some translators may want to know their ID in the registry.
131  virtual void setID( int id ) { myID = id; }
132  virtual int getID() const { return myID; }
133 
134  /// Designates the nodes as shader node dependents, given their IDs.
135  void setDependentNodeIDs( const UT_IntArray &node_ids )
136  { myDependentNodeIDs = node_ids; }
138  { return myDependentNodeIDs; }
139 
140 private:
141  /// Translator's ID.
142  int myID = -1;
143 
144  /// Houdini nodes (LOPs) that depend on translated shader nodes (VOPs).
145  UT_IntArray myDependentNodeIDs;
146 };
147 
148 // ============================================================================
149 /// Creates a standard USD Preview Surface shader from Houdini's node.
151 {
152 public:
153  /// Standard virtual destructor for this abstract base class.
154  virtual ~HUSD_PreviewShaderTranslator() = default;
155 
156 
157  /// Returns true if the translator can create a USD Preview Surface shader
158  /// for a shader node that reports the given render mask.
159  virtual bool matchesRenderContext( const UT_StringRef &render_context ) = 0;
160 
161 
162  /// Creates a USD Preview Surface shader primitive for the USD material.
163  ///
164  /// @p usd_material_path - path to the material primitive in which
165  /// the preview shader primitive should be created.
166  /// @p usd_shader_path - path to a specific render context shader primitive
167  /// based on which the universal render context preview shader
168  /// should be created.
169  /// @p time_code - time code at which to evaluate and author any properties.
170  virtual void createMaterialPreviewShader( HUSD_AutoWriteLock &lock,
171  const UT_StringRef &usd_material_path,
172  const UT_StringRef &usd_shader_path,
173  const HUSD_TimeCode &time_code ) = 0;
174 
175 
176  /// Destroys the USD Preview Surface shader and its input chains.
177  virtual void deleteMaterialPreviewShader( HUSD_AutoWriteLock &lock,
178  const UT_StringRef &usd_material_path ) = 0;
179 
180  /// Re-translates the shader parameters given the shader VOP node (and its
181  /// new parameter values).
182  /// @p usd_shader_path - the path to the USD main surface shader primitive,
183  /// based on which the preview shader nput attributes need to be
184  /// updated, due to node parm value change.
185  /// @p time_code - time code at which to evaluate and author any properties.
186  virtual void updateMaterialPreviewShaderParameters(HUSD_AutoWriteLock &lock,
187  const UT_StringRef &usd_main_shader_path,
188  const HUSD_TimeCode &time_code ) = 0;
189 };
190 
191 // ============================================================================
192 /// Keeps a list of known translators that define a USD shader prim from
193 /// Houdini shader nodes.
195 {
196 public:
197  /// Returns a singelton instance.
198  static HUSD_ShaderTranslatorRegistry &get();
199 
200 
201  /// Adds the translator to the list of known translators.
202  void registerShaderTranslator( HUSD_ShaderTranslator &translator );
203 
204  /// Removes the translator from the list of known translators.
205  void unregisterShaderTranslator( HUSD_ShaderTranslator &translator );
206 
207  /// Returns a translator that accepts the given render target mask.
208  /// If no translator is found, returns nullptr.
209  HUSD_ShaderTranslator * findShaderTranslator( const OP_Node &node ) const;
210 
211  /// Returns the internal ID number of a translator that handles the
212  /// translation of the given node.
213  int findShaderTranslatorID( const OP_Node &node ) const;
214 
215 
216  /// Adds the translator to the list of known translators.
217  void registerPreviewShaderTranslator( HUSD_PreviewShaderTranslator &t );
218 
219  /// Removes the translator from the list of known translators.
220  void unregisterPreviewShaderTranslator( HUSD_PreviewShaderTranslator &t);
221 
222  /// Returns a translator that accepts the given USD render context name.
223  HUSD_PreviewShaderTranslator * findPreviewShaderTranslator(
224  const UT_StringRef &usd_render_context_name ) const;
225 
226  /// Removes all translators from the registry.
227  /// Should only be called on shutdown of the process.
228  void clear();
229 
230  /// Informs the registry about a new translation of node into a USD prim.
231  void reportShaderTranslation( const OP_Node &node,
232  const UT_StringRef &usd_shader_path );
233 
234  /// @{ Adds and removes a node from the translation observers list.
235  /// Observers are basicaly interested in creation of any new USD shader
236  /// primitive and the original VOP node based on which it was created.
237  /// Translators report such creation events with reportShaderTranslation(),
238  /// and observer LOPs can use that info to selectively re-translate
239  /// single USD prim when a only single VOP changed.
240  using TranslationRecord = std::pair<int, UT_StringHolder>;
242  void addTranslationObserver( const OP_Node &node );
243  TranslationRecords removeTranslationObserver( const OP_Node &node );
244  /// @}
245 
246 
247 private:
248  /// List of known shader translators.
249  UT_Array<HUSD_ShaderTranslator *> myTranslators;
250 
251  /// List of known preview shader translators.
252  UT_Array<HUSD_PreviewShaderTranslator *> myPreviewTranslators;
253 
254  /// @{ IDs of translation observer nodes and translations reported for them.
255  UT_Array<int> myTranslationObservers;
256  UT_Array<TranslationRecords> myTranslations;
257  /// @}
258 };
259 
260 #endif
void setDependentNodeIDs(const UT_IntArray &node_ids)
Designates the nodes as shader node dependents, given their IDs.
#define HUSD_API
Definition: HUSD_API.h:32
virtual void setID(int id)
Some translators may want to know their ID in the registry.
const UT_IntArray & getDependentNodeIDs() const
GLuint id
Definition: glcorearb.h:655
Creates a standard USD Preview Surface shader from Houdini's node.
GLdouble t
Definition: glad.h:2397
std::pair< int, UT_StringHolder > TranslationRecord
Creates a USD shader primitives from Houdini's nodes.
VOP_Type
Enumeration of the built-in (basic) VOP data types.
Definition: VOP_Types.h:25
virtual int getID() const
MX_RENDER_API ShaderPtr createShader(const string &shaderName, GenContext &context, ElementPtr elem)
Create a shader for a given element.