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 
26 #include <utility>
27 
28 class HUSD_AutoWriteLock;
29 class HUSD_TimeCode;
30 class OP_Node;
31 
32 
33 /// Creates a USD shader primitives from Houdini's nodes.
35 {
36 public:
37  /// Standard virtual destructor for this abstract base class.
38  virtual ~HUSD_ShaderTranslator() = default;
39 
40 
41  /// Returns true if the translator can encode a shader that reports
42  /// a given render mask (ie, is a shader for a given render target).
43  virtual bool matchesRenderMask( const UT_StringRef &render_mask ) = 0;
44 
45 
46  /// Defines a USD shader primitive that is part of the USD material.
47  /// Ie, the translator will connect the shader to the material output.
48  ///
49  /// @p usd_material_path - path to the material primitive in which
50  /// the shader primitive should be created.
51  /// @p time_code - time code at which to evaluate any properties
52  /// @p shader_node - the Houdini node that represents a shader and that
53  /// needs to be translated into a USD shader primitive
54  /// @p shader_type - some VOPs contains several shaders (eg material
55  /// builders). So this parameters specifies the type of the shader
56  /// to pick and translate.
57  /// @p output_name - the output name of the VOP node that represents
58  /// the shader to pick and translate. It can be an empty string,
59  /// if the VOP node does not have shader outputs.
60  virtual void createMaterialShader( HUSD_AutoWriteLock &lock,
61  const UT_StringRef &usd_material_path,
62  const HUSD_TimeCode &time_code,
63  OP_Node &shader_node,
64  VOP_Type shader_type,
65  const UT_StringRef &output_name) = 0;
66 
67  /// Defines a USD shader primitive that is part of a shader network chain.
68  /// Ie, the translator will create a shader primitive output, that the
69  /// caller can use to connect as an input to another shader.
70  ///
71  /// @p usd_material_path - path to the material primitive in which
72  /// the shader primitive should be created.
73  /// @p usd_parent_path - path to the primitive inside which
74  /// the shader primitive should be created directly.
75  /// @p time_code - time code at which to evaluate any properties
76  /// @p shader_node - the Houdini node that represents a shader and that
77  /// needs to be translated into a USD shader primitive
78  /// @p output_name - the output name of the VOP node that needs to be
79  /// translated into USD shader output. This is the output
80  /// the caller is interested in having representation in USD.
81  ///
82  /// @return The path to the USD shader output attribute corresponding
83  /// to the @p output_name connector on the @p shader_node.
84  virtual UT_StringHolder createShader( HUSD_AutoWriteLock &lock,
85  const UT_StringRef &usd_material_path,
86  const UT_StringRef &usd_parent_path,
87  const HUSD_TimeCode &time_code,
88  OP_Node &shader_node,
89  const UT_StringRef &output_name) = 0;
90 
91 
92  /// Re-translates the shader parameters given the shader VOP node (and its
93  /// new parameter values).
94  /// @p usd_shader_path - the path to the USD shader primitive whose
95  /// input attributes need updating due to node parm value change.
96  /// @p time_code - time code at which to evaluate any properties
97  /// @p shader_node - Houdini node that represents a shader that
98  /// needs to be re-translated into the given USD shader primitive.
99  /// @p parameter_names - the list of parameters that have changed.
100  /// If the list is empty, then any of the node's parameters may
101  /// have changed. If it's not empty, then only listed parameters
102  /// have changed.
103  virtual void updateShaderParameters( HUSD_AutoWriteLock &lock,
104  const UT_StringRef &usd_shader_path,
105  const HUSD_TimeCode &time_code,
106  OP_Node &shader_node,
107  const UT_StringArray &parameter_names) = 0;
108 
109  /// Returns the name of the renderer (render context name) that
110  /// should be used in the material output name for that USD shader.
111  virtual UT_StringHolder getRenderContextName( OP_Node &shader_node,
112  const UT_StringRef &output_name) = 0;
113 
114 
115  /// Some translators may want to know their ID in the registry.
116  virtual void setID( int id ) { myID = id; }
117  virtual int getID() const { return myID; }
118 
119 private:
120  /// Translator's ID.
121  int myID = -1;
122 };
123 
124 // ============================================================================
125 /// Creates a standard USD Preview Surface shader from Houdini's node.
127 {
128 public:
129  /// Standard virtual destructor for this abstract base class.
130  virtual ~HUSD_PreviewShaderGenerator() = default;
131 
132 
133  /// Returns true if the generator can create a USD Preview Surface shader
134  /// for a shader node that reports the given render mask.
135  virtual bool matchesRenderMask( const UT_StringRef &render_mask ) = 0;
136 
137 
138  /// Creates a USD Preview Surface shader primitive for the USD material.
139  ///
140  /// @p usd_material_path - path to the material primitive in which
141  /// the shader primitive should be created.
142  /// @p time_code - time code at which to evaluate any properties
143  /// @p shader_node - the Houdini node that represents a shader for which
144  /// the USD Preview Shader prim should be created.
145  /// @p output_name - the output name of the VOP node that represents
146  /// the shader to pick and translate. It can be an empty string,
147  /// if the VOP node does not have shader outputs.
148  virtual void createMaterialPreviewShader( HUSD_AutoWriteLock &lock,
149  const UT_StringRef &usd_material_path,
150  const HUSD_TimeCode &time_code,
151  OP_Node &shader_node,
152  const UT_StringRef &output_name) = 0;
153 
154  /// Re-generates the shader parameters given the shader VOP node (and its
155  /// new parameter values).
156  /// @p usd_shader_path - the path to the USD preview shader primitive whose
157  /// input attributes need updating due to node parm value change.
158  /// @p time_code - time code at which to evaluate any properties
159  /// @p shader_node - Houdini node whose parameters changed, thus requiring
160  /// an update to the input attributes of the corresponding
161  /// USD preview shader.
162  virtual void updateMaterialPreviewShaderParameters(HUSD_AutoWriteLock &lock,
163  const UT_StringRef &usd_shader_path,
164  const HUSD_TimeCode &time_code,
165  OP_Node &shader_node,
166  const UT_StringArray &parameter_names) = 0;
167 };
168 
169 // ============================================================================
170 /// Keeps a list of known translators that define a USD shader prim from
171 /// Houdini shader nodes.
173 {
174 public:
175  /// Returns a singelton instance.
176  static HUSD_ShaderTranslatorRegistry &get();
177 
178 
179  /// Adds the translator to the list of known translators.
180  void registerShaderTranslator( HUSD_ShaderTranslator &translator );
181 
182  /// Removes the translator from the list of known translators.
183  void unregisterShaderTranslator( HUSD_ShaderTranslator &translator );
184 
185  /// Returns a translator that accepts the given render target mask.
186  /// If no translator is found, returns nullptr.
187  HUSD_ShaderTranslator * findShaderTranslator( const OP_Node &node ) const;
188 
189  /// Returns the internal ID number of a translator that handles the
190  /// translation of the given node.
191  int findShaderTranslatorID( const OP_Node &node ) const;
192 
193 
194  /// Adds the generator to the list of known generator.
195  void registerPreviewShaderGenerator(HUSD_PreviewShaderGenerator &g);
196 
197  /// Removes the generator from the list of known generator.
198  void unregisterPreviewShaderGenerator(HUSD_PreviewShaderGenerator&g);
199 
200  /// Returns a generator that accepts the given render target mask.
201  /// If no generator is found, returns nullptr.
202  HUSD_PreviewShaderGenerator * findPreviewShaderGenerator(
203  const OP_Node &node ) const;
204 
205  /// Removes all translators and generators from the registry.
206  /// Should only be called on shutdown of the process.
207  void clear();
208 
209  /// Informs the registry about a new translation of node into a USD prim.
210  void reportShaderTranslation( const OP_Node &node,
211  const UT_StringRef &usd_shader_path );
212 
213  /// @{ Adds and removes a node from the translation observers list.
214  /// Observers are basicaly interested in creation of any new USD shader
215  /// primitive and the original VOP node based on which it was created.
216  /// Translators report such creation events with reportShaderTranslation(),
217  /// and observer LOPs can use that info to selectively re-translate
218  /// single USD prim when a only single VOP changed.
219  using TranslationRecord = std::pair<int, UT_StringHolder>;
221  void addTranslationObserver( const OP_Node &node );
222  TranslationRecords removeTranslationObserver( const OP_Node &node );
223  /// @}
224 
225 
226 private:
227  /// List of known shader translators.
228  UT_Array<HUSD_ShaderTranslator *> myTranslators;
229 
230  /// List of known preview shader generators.
232 
233  /// @{ IDs of translation observer nodes and translations reported for them.
234  UT_Array<int> myTranslationObservers;
235  UT_Array<TranslationRecords> myTranslations;
236  /// @}
237 };
238 
239 #endif
GLuint id
Definition: glew.h:1679
#define HUSD_API
Definition: HUSD_API.h:32
virtual void setID(int id)
Some translators may want to know their ID in the registry.
Creates a standard USD Preview Surface shader from Houdini's node.
std::pair< int, UT_StringHolder > TranslationRecord
Creates a USD shader primitives from Houdini's nodes.
VOP_Type
Definition: VOP_Types.h:24
virtual int getID() const
GLboolean GLboolean g
Definition: glew.h:9477