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>
26 #include <UT/UT_IntArray.h>
27 
28 #include <utility>
29 
30 class HUSD_AutoWriteLock;
31 class HUSD_TimeCode;
32 class OP_Node;
33 
34 
35 /// Creates a USD shader primitives from Houdini's nodes.
37 {
38 public:
39  /// Standard virtual destructor for this abstract base class.
40  virtual ~HUSD_ShaderTranslator() = default;
41 
42 
43  /// Returns true if the translator can encode a shader that reports
44  /// a given render mask (ie, is a shader for a given render target).
45  virtual bool matchesRenderMask( const UT_StringRef &render_mask ) = 0;
46 
47  /// @{ Methods that delineate the authoring of a USD material primitive.
48  /// The first method is called before this translator is asked to translate
49  /// any shader into the given material, while the second one is called after
50  /// all relevant shader have been translated.
51  /// This allows the translator to do some preparatory or cleanup work.
52  /// For example to configure the USD material, or to reset any caches.
53  virtual void beginMaterialTranslation( HUSD_AutoWriteLock &lock,
54  const UT_StringRef &usd_material_path );
55  virtual void endMaterialTranslation(HUSD_AutoWriteLock &lock,
56  const UT_StringRef &usd_material_path );
57  /// @}
58 
59  /// Class used for marking the scope as containing an active shader
60  /// translator. This allows checking if any shader translator is actively
61  /// translating shader nodes to USD at the moment.
63  {
64  public:
65  ActiveToken();
66  ~ActiveToken();
67 
68  /// Returns true if there are any existing token objects
69  /// live in the given thread.
70  static bool hasActiveToken(int thread);
71 
72  private:
73  static UT_ThreadSpecificValue<int> theActiveTranslatorCount;
74  };
75 
76  /// Returns true if there are any shader translators executing
77  /// on the given thread (ie, if passed current thread's ID, ie
78  /// it checks the call originates from a translator on the call stack).
79  static bool isTranslatorActive(int thread)
80  { return ActiveToken::hasActiveToken(thread); }
81 
82  /// Defines a USD shader primitive that is part of the USD material.
83  /// Ie, the translator will connect the shader to the material output.
84  ///
85  /// @p usd_material_path - path to the material primitive in which
86  /// the shader primitive should be created.
87  /// @p time_code - time code at which to evaluate any properties
88  /// @p shader_node - the Houdini node that represents a shader and that
89  /// needs to be translated into a USD shader primitive
90  /// @p shader_type - some VOPs contains several shaders (eg material
91  /// builders). So this parameters specifies the type of the shader
92  /// to pick and translate.
93  /// @p output_name - the output name of the VOP node that represents
94  /// the shader to pick and translate. It can be an empty string,
95  /// if the VOP node does not have shader outputs.
96  virtual void createMaterialShader( HUSD_AutoWriteLock &lock,
97  const UT_StringRef &usd_material_path,
98  const HUSD_TimeCode &time_code,
99  OP_Node &shader_node,
100  VOP_Type shader_type,
101  const UT_StringRef &output_name) = 0;
102 
103  /// Defines a USD shader primitive that is part of a shader network chain.
104  /// Ie, the translator will create a shader primitive output, that the
105  /// caller can use to connect as an input to another shader.
106  ///
107  /// @p usd_material_path - path to the material primitive in which
108  /// the shader primitive should be created.
109  /// @p usd_parent_path - path to the primitive inside which
110  /// the shader primitive should be created directly.
111  /// @p usd_shader_name - name of the shader primitive to create,
112  /// (may be an empty string, in which case use node name, etc).
113  /// @p time_code - time code at which to evaluate any properties
114  /// @p shader_node - the Houdini node that represents a shader and that
115  /// needs to be translated into a USD shader primitive
116  /// @p output_name - the output name of the VOP node that needs to be
117  /// translated into USD shader output. This is the output
118  /// the caller is interested in having representation in USD.
119  ///
120  /// @return The path to the USD shader output attribute corresponding
121  /// to the @p output_name connector on the @p shader_node.
123  const UT_StringRef &usd_material_path,
124  const UT_StringRef &usd_parent_path,
125  const UT_StringRef &usd_shader_name,
126  const HUSD_TimeCode &time_code,
127  OP_Node &shader_node,
128  const UT_StringRef &output_name) = 0;
129 
130 
131  /// Re-translates the shader parameters given the shader VOP node (and its
132  /// new parameter values).
133  /// @p usd_shader_path - the path to the USD shader primitive whose
134  /// input attributes need updating due to node parm value change.
135  /// @p time_code - time code at which to evaluate any properties
136  /// @p shader_node - Houdini node that represents a shader that
137  /// needs to be re-translated into the given USD shader primitive.
138  /// @p parameter_names - the list of parameters that have changed.
139  /// If the list is empty, then any of the node's parameters may
140  /// have changed. If it's not empty, then only listed parameters
141  /// have changed.
142  virtual void updateShaderParameters( HUSD_AutoWriteLock &lock,
143  const UT_StringRef &usd_shader_path,
144  const HUSD_TimeCode &time_code,
145  OP_Node &shader_node,
146  const UT_StringArray &parameter_names) = 0;
147 
148  /// Returns the name of the renderer (render context name) that
149  /// should be used in the material output name for that USD shader.
150  virtual UT_StringHolder getRenderContextName( OP_Node &shader_node,
151  const UT_StringRef &output_name) = 0;
152 
153 
154  /// Some translators may want to know their ID in the registry.
155  virtual void setID( int id ) { myID = id; }
156  virtual int getID() const { return myID; }
157 
158  /// Designates the nodes as shader node dependents, given their IDs.
159  void setDependentNodeIDs( const UT_IntArray &node_ids )
160  { myDependentNodeIDs = node_ids; }
162  { return myDependentNodeIDs; }
163 
164 private:
165  /// Translator's ID.
166  int myID = -1;
167 
168  /// Houdini nodes (LOPs) that depend on translated shader nodes (VOPs).
169  UT_IntArray myDependentNodeIDs;
170 };
171 
172 // ============================================================================
173 /// Creates a standard USD Preview Surface shader from Houdini's node.
175 {
176 public:
177  /// Standard virtual destructor for this abstract base class.
178  virtual ~HUSD_PreviewShaderTranslator() = default;
179 
180 
181  /// Returns true if the translator can create a USD Preview Surface shader
182  /// for a shader node that reports the given render mask.
183  virtual bool matchesRenderContext( const UT_StringRef &render_context ) = 0;
184 
185 
186  /// Creates a USD Preview Surface shader primitive for the USD material.
187  ///
188  /// @p usd_material_path - path to the material primitive in which
189  /// the preview shader primitive should be created.
190  /// @p usd_shader_path - path to a specific render context shader primitive
191  /// based on which the universal render context preview shader
192  /// should be created.
193  /// @p time_code - time code at which to evaluate and author any properties.
194  virtual void createMaterialPreviewShader( HUSD_AutoWriteLock &lock,
195  const UT_StringRef &usd_material_path,
196  const UT_StringRef &usd_shader_path,
197  const HUSD_TimeCode &time_code ) = 0;
198 
199 
200  /// Destroys the USD Preview Surface shader and its input chains.
201  virtual void deleteMaterialPreviewShader( HUSD_AutoWriteLock &lock,
202  const UT_StringRef &usd_material_path ) = 0;
203 
204  /// Re-translates the shader parameters given the shader VOP node (and its
205  /// new parameter values).
206  /// @p usd_shader_path - the path to the USD main surface shader primitive,
207  /// based on which the preview shader nput attributes need to be
208  /// updated, due to node parm value change.
209  /// @p time_code - time code at which to evaluate and author any properties.
210  virtual void updateMaterialPreviewShaderParameters(HUSD_AutoWriteLock &lock,
211  const UT_StringRef &usd_main_shader_path,
212  const HUSD_TimeCode &time_code ) = 0;
213 };
214 
215 // ============================================================================
216 /// Keeps a list of known translators that define a USD shader prim from
217 /// Houdini shader nodes.
219 {
220 public:
221  /// Returns a singelton instance.
222  static HUSD_ShaderTranslatorRegistry &get();
223 
224 
225  /// Adds the translator to the list of known translators.
226  void registerShaderTranslator( HUSD_ShaderTranslator &translator );
227 
228  /// Removes the translator from the list of known translators.
229  void unregisterShaderTranslator( HUSD_ShaderTranslator &translator );
230 
231  /// Returns a translator that accepts the given render target mask.
232  /// If no translator is found, returns nullptr.
233  HUSD_ShaderTranslator * findShaderTranslator( const OP_Node &node ) const;
234 
235  /// Returns the internal ID number of a translator that handles the
236  /// translation of the given node.
237  int findShaderTranslatorID( const OP_Node &node ) const;
238 
239 
240  /// Adds the translator to the list of known translators.
241  void registerPreviewShaderTranslator( HUSD_PreviewShaderTranslator &t );
242 
243  /// Removes the translator from the list of known translators.
244  void unregisterPreviewShaderTranslator( HUSD_PreviewShaderTranslator &t);
245 
246  /// Returns a translator that accepts the given USD render context name.
247  HUSD_PreviewShaderTranslator * findPreviewShaderTranslator(
248  const UT_StringRef &usd_render_context_name ) const;
249 
250  /// Removes all translators from the registry.
251  /// Should only be called on shutdown of the process.
252  void clear();
253 
254  /// Informs the registry about a new translation of node into a USD prim.
255  void reportShaderTranslation( const OP_Node &node,
256  const UT_StringRef &usd_shader_path );
257 
258  /// @{ Adds and removes a node from the translation observers list.
259  /// Observers are basicaly interested in creation of any new USD shader
260  /// primitive and the original VOP node based on which it was created.
261  /// Translators report such creation events with reportShaderTranslation(),
262  /// and observer LOPs can use that info to selectively re-translate
263  /// single USD prim when a only single VOP changed.
264  using TranslationRecord = std::pair<int, UT_StringHolder>;
266  void addTranslationObserver( const OP_Node &node );
267  TranslationRecords removeTranslationObserver( const OP_Node &node );
268  /// @}
269 
270 
271 private:
272  /// List of known shader translators.
273  UT_Array<HUSD_ShaderTranslator *> myTranslators;
274 
275  /// List of known preview shader translators.
276  UT_Array<HUSD_PreviewShaderTranslator *> myPreviewTranslators;
277 
278  /// @{ IDs of translation observer nodes and translations reported for them.
279  UT_Array<int> myTranslationObservers;
280  UT_Array<TranslationRecords> myTranslations;
281  /// @}
282 };
283 
284 #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
**Note that the tasks the is the thread number *for the or if it s being executed by a non pool thread(this *can happen in cases where the whole pool is occupied and the calling *thread contributes to running the work load).**Thread pool.Have fun
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.
static bool isTranslatorActive(int thread)