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 class HUSD_AutoWriteLock;
27 class HUSD_TimeCode;
28 class OP_Node;
29 
30 
31 /// Creates a USD shader primitives from Houdini's nodes.
33 {
34 public:
35  /// Standard virtual destructor for this abstract base class.
36  virtual ~HUSD_ShaderTranslator() = default;
37 
38 
39  /// Returns true if the translator can encode a shader that reports
40  /// a given render mask (ie, is a shader for a given render target).
41  virtual bool matchesRenderMask( const UT_StringRef &render_mask ) = 0;
42 
43 
44  /// Defines a USD shader primitive that is part of the USD material.
45  /// Ie, the translator will connect the shader to the material output.
46  ///
47  /// @p usd_material_path - path to the material primitive in which
48  /// the shader primitive should be created.
49  /// @p time_code - time code at which to evaluate any properties
50  /// @p shader_node - the Houdini node that represents a shader and that
51  /// needs to be translated into a USD shader primitive
52  /// @p shader_type - some VOPs contains several shaders (eg material
53  /// builders). So this parameters specifies the type of the shader
54  /// to pick and translate.
55  /// @p output_name - the output name of the VOP node that represents
56  /// the shader to pick and translate. It can be an empty string,
57  /// if the VOP node does not have shader outputs.
58  virtual void createMaterialShader( HUSD_AutoWriteLock &lock,
59  const UT_StringRef &usd_material_path,
60  const HUSD_TimeCode &time_code,
61  OP_Node &shader_node,
62  VOP_Type shader_type,
63  const UT_StringRef &output_name) = 0;
64 
65  /// Defines a USD shader primitive that is part of a shader network chain.
66  /// Ie, the translator will create a shader primitive output, that the
67  /// caller can use to connect as an input to another shader.
68  ///
69  /// @p usd_material_path - path to the material primitive in which
70  /// the shader primitive should be created.
71  /// @p usd_parent_path - path to the primitive inside which
72  /// the shader primitive should be created directly.
73  /// @p time_code - time code at which to evaluate any properties
74  /// @p shader_node - the Houdini node that represents a shader and that
75  /// needs to be translated into a USD shader primitive
76  /// @p output_name - the output name of the VOP node that needs to be
77  /// translated into USD shader output. This is the output
78  /// the caller is interested in having representation in USD.
79  ///
80  /// @return The path to the USD shader output attribute corresponding
81  /// to the @p output_name connector on the @p shader_node.
82  virtual UT_StringHolder createShader( HUSD_AutoWriteLock &lock,
83  const UT_StringRef &usd_material_path,
84  const UT_StringRef &usd_parent_path,
85  const HUSD_TimeCode &time_code,
86  OP_Node &shader_node,
87  const UT_StringRef &output_name) = 0;
88 
89 
90  /// Returns the name of the renderer (render context name) that
91  /// should be used in the material output name for that USD shader.
92  virtual UT_StringHolder getRenderContextName( OP_Node &shader_node,
93  const UT_StringRef &output_name) = 0;
94 
95 
96  /// Some translators may want to know their ID in the registry.
97  virtual void setID( int id ) { myID = id; }
98  virtual int getID() const { return myID; }
99 
100 private:
101  /// Translator's ID.
102  int myID = -1;
103 };
104 
105 // ============================================================================
106 /// Creates a standard USD Preview Surface shader from Houdini's node.
108 {
109 public:
110  /// Standard virtual destructor for this abstract base class.
111  virtual ~HUSD_PreviewShaderGenerator() = default;
112 
113 
114  /// Returns true if the generator can create a USD Preview Surface shader
115  /// for a shader node that reports the given render mask.
116  virtual bool matchesRenderMask( const UT_StringRef &render_mask ) = 0;
117 
118 
119  /// Creates a USD Preview Surface shader primitive for the USD material.
120  ///
121  /// @p usd_material_path - path to the material primitive in which
122  /// the shader primitive should be created.
123  /// @p time_code - time code at which to evaluate any properties
124  /// @p shader_node - the Houdini node that represents a shader for which
125  /// the USD Preview Shader prim should be created.
126  /// @p output_name - the output name of the VOP node that represents
127  /// the shader to pick and translate. It can be an empty string,
128  /// if the VOP node does not have shader outputs.
129  virtual void createMaterialPreviewShader( HUSD_AutoWriteLock &lock,
130  const UT_StringRef &usd_material_path,
131  const HUSD_TimeCode &time_code,
132  OP_Node &shader_node,
133  const UT_StringRef &output_name) = 0;
134 };
135 
136 // ============================================================================
137 /// Keeps a list of known translators that define a USD shader prim from
138 /// Houdini shader nodes.
140 {
141 public:
142  /// Returns a singelton instance.
143  static HUSD_ShaderTranslatorRegistry &get();
144 
145 
146  /// Adds the translator to the list of known translators.
147  void registerShaderTranslator( HUSD_ShaderTranslator &translator );
148 
149  /// Removes the translator from the list of known translators.
150  void unregisterShaderTranslator( HUSD_ShaderTranslator &translator );
151 
152  /// Returns a translator that accepts the given render target mask.
153  /// If no translator is found, returns nullptr.
154  HUSD_ShaderTranslator * findShaderTranslator( const OP_Node &node ) const;
155 
156  /// Returns the internal ID number of a translator that handles the
157  /// translation of the given node.
158  int findShaderTranslatorID( const OP_Node &node ) const;
159 
160 
161  /// Adds the generator to the list of known generator.
162  void registerPreviewShaderGenerator(HUSD_PreviewShaderGenerator &g);
163 
164  /// Removes the generator from the list of known generator.
165  void unregisterPreviewShaderGenerator(HUSD_PreviewShaderGenerator&g);
166 
167  /// Returns a generator that accepts the given render target mask.
168  /// If no generator is found, returns nullptr.
169  HUSD_PreviewShaderGenerator * findPreviewShaderGenerator(
170  const OP_Node &node ) const;
171 
172  /// Removes all translators and generators from the registry.
173  /// Should only be called on shutdown of the process.
174  void clear();
175 
176 private:
177  /// List of known shader translators.
178  UT_Array<HUSD_ShaderTranslator *> myTranslators;
179 
180  /// List of known preview shader generators.
182 };
183 
184 #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.
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