HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nodeDefAPI.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef USDSHADE_GENERATED_NODEDEFAPI_H
8 #define USDSHADE_GENERATED_NODEDEFAPI_H
9 
10 /// \file usdShade/nodeDefAPI.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/usdShade/api.h"
15 #include "pxr/usd/usd/prim.h"
16 #include "pxr/usd/usd/stage.h"
18 
19 #include "pxr/usd/sdr/declare.h"
20 #include "pxr/usd/sdr/shaderNode.h"
21 
22 
23 #include "pxr/base/vt/value.h"
24 
25 #include "pxr/base/gf/vec3d.h"
26 #include "pxr/base/gf/vec3f.h"
27 #include "pxr/base/gf/matrix4d.h"
28 
29 #include "pxr/base/tf/token.h"
30 #include "pxr/base/tf/type.h"
31 
33 
34 class SdfAssetPath;
35 
36 // -------------------------------------------------------------------------- //
37 // NODEDEFAPI //
38 // -------------------------------------------------------------------------- //
39 
40 /// \class UsdShadeNodeDefAPI
41 ///
42 /// UsdShadeNodeDefAPI is an API schema that provides attributes
43 /// for a prim to select a corresponding Shader Node Definition ("Sdr Node"),
44 /// as well as to look up a runtime entry for that shader node in the
45 /// form of an SdrShaderNode.
46 ///
47 /// UsdShadeNodeDefAPI is intended to be a pre-applied API schema for any
48 /// prim type that wants to refer to the SdrRegistry for further implementation
49 /// details about the behavior of that prim. The primary use in UsdShade
50 /// itself is as UsdShadeShader, which is a basis for material shading networks
51 /// (UsdShadeMaterial), but this is intended to be used in other domains
52 /// that also use the Sdr node mechanism.
53 ///
54 /// This schema provides properties that allow a prim to identify an external
55 /// node definition, either by a direct identifier key into the SdrRegistry
56 /// (info:id), an asset to be parsed by a suitable SdrParserPlugin
57 /// (info:sourceAsset), or an inline source code that must also be parsed
58 /// (info:sourceCode); as well as a selector attribute to determine which
59 /// specifier is active (info:implementationSource).
60 ///
61 ///
62 /// For any described attribute \em Fallback \em Value or \em Allowed \em Values below
63 /// that are text/tokens, the actual token is published and defined in \ref UsdShadeTokens.
64 /// So to set an attribute to the value "rightHanded", use UsdShadeTokens->rightHanded
65 /// as the value.
66 ///
68 {
69 public:
70  /// Compile time constant representing what kind of schema this class is.
71  ///
72  /// \sa UsdSchemaKind
74 
75  /// Construct a UsdShadeNodeDefAPI on UsdPrim \p prim .
76  /// Equivalent to UsdShadeNodeDefAPI::Get(prim.GetStage(), prim.GetPath())
77  /// for a \em valid \p prim, but will not immediately throw an error for
78  /// an invalid \p prim
79  explicit UsdShadeNodeDefAPI(const UsdPrim& prim=UsdPrim())
80  : UsdAPISchemaBase(prim)
81  {
82  }
83 
84  /// Construct a UsdShadeNodeDefAPI on the prim held by \p schemaObj .
85  /// Should be preferred over UsdShadeNodeDefAPI(schemaObj.GetPrim()),
86  /// as it preserves SchemaBase state.
87  explicit UsdShadeNodeDefAPI(const UsdSchemaBase& schemaObj)
88  : UsdAPISchemaBase(schemaObj)
89  {
90  }
91 
92  /// Destructor.
94  virtual ~UsdShadeNodeDefAPI();
95 
96  /// Return a vector of names of all pre-declared attributes for this schema
97  /// class and all its ancestor classes. Does not include attributes that
98  /// may be authored by custom/extended methods of the schemas involved.
100  static const TfTokenVector &
101  GetSchemaAttributeNames(bool includeInherited=true);
102 
103  /// Return a UsdShadeNodeDefAPI holding the prim adhering to this
104  /// schema at \p path on \p stage. If no prim exists at \p path on
105  /// \p stage, or if the prim at that path does not adhere to this schema,
106  /// return an invalid schema object. This is shorthand for the following:
107  ///
108  /// \code
109  /// UsdShadeNodeDefAPI(stage->GetPrimAtPath(path));
110  /// \endcode
111  ///
113  static UsdShadeNodeDefAPI
114  Get(const UsdStagePtr &stage, const SdfPath &path);
115 
116 
117  /// Returns true if this <b>single-apply</b> API schema can be applied to
118  /// the given \p prim. If this schema can not be a applied to the prim,
119  /// this returns false and, if provided, populates \p whyNot with the
120  /// reason it can not be applied.
121  ///
122  /// Note that if CanApply returns false, that does not necessarily imply
123  /// that calling Apply will fail. Callers are expected to call CanApply
124  /// before calling Apply if they want to ensure that it is valid to
125  /// apply a schema.
126  ///
127  /// \sa UsdPrim::GetAppliedSchemas()
128  /// \sa UsdPrim::HasAPI()
129  /// \sa UsdPrim::CanApplyAPI()
130  /// \sa UsdPrim::ApplyAPI()
131  /// \sa UsdPrim::RemoveAPI()
132  ///
134  static bool
135  CanApply(const UsdPrim &prim, std::string *whyNot=nullptr);
136 
137  /// Applies this <b>single-apply</b> API schema to the given \p prim.
138  /// This information is stored by adding "NodeDefAPI" to the
139  /// token-valued, listOp metadata \em apiSchemas on the prim.
140  ///
141  /// \return A valid UsdShadeNodeDefAPI object is returned upon success.
142  /// An invalid (or empty) UsdShadeNodeDefAPI object is returned upon
143  /// failure. See \ref UsdPrim::ApplyAPI() for conditions
144  /// resulting in failure.
145  ///
146  /// \sa UsdPrim::GetAppliedSchemas()
147  /// \sa UsdPrim::HasAPI()
148  /// \sa UsdPrim::CanApplyAPI()
149  /// \sa UsdPrim::ApplyAPI()
150  /// \sa UsdPrim::RemoveAPI()
151  ///
153  static UsdShadeNodeDefAPI
154  Apply(const UsdPrim &prim);
155 
156 protected:
157  /// Returns the kind of schema this class belongs to.
158  ///
159  /// \sa UsdSchemaKind
161  UsdSchemaKind _GetSchemaKind() const override;
162 
163 private:
164  // needs to invoke _GetStaticTfType.
165  friend class UsdSchemaRegistry;
167  static const TfType &_GetStaticTfType();
168 
169  static bool _IsTypedSchema();
170 
171  // override SchemaBase virtuals.
173  const TfType &_GetTfType() const override;
174 
175 public:
176  // --------------------------------------------------------------------- //
177  // IMPLEMENTATIONSOURCE
178  // --------------------------------------------------------------------- //
179  /// Specifies the attribute that should be consulted to get the
180  /// shader's implementation or its source code.
181  ///
182  /// * If set to "id", the "info:id" attribute's value is used to
183  /// determine the shader source from the shader registry.
184  /// * If set to "sourceAsset", the resolved value of the "info:sourceAsset"
185  /// attribute corresponding to the desired implementation (or source-type)
186  /// is used to locate the shader source. A source asset file may also
187  /// specify multiple shader definitions, so there is an optional attribute
188  /// "info:sourceAsset:subIdentifier" whose value should be used to indicate
189  /// a particular shader definition from a source asset file.
190  /// * If set to "sourceCode", the value of "info:sourceCode" attribute
191  /// corresponding to the desired implementation (or source type) is used as
192  /// the shader source.
193  ///
194  ///
195  /// | ||
196  /// | -- | -- |
197  /// | Declaration | `uniform token info:implementationSource = "id"` |
198  /// | C++ Type | TfToken |
199  /// | \ref Usd_Datatypes "Usd Type" | SdfValueTypeNames->Token |
200  /// | \ref SdfVariability "Variability" | SdfVariabilityUniform |
201  /// | \ref UsdShadeTokens "Allowed Values" | id, sourceAsset, sourceCode |
204 
205  /// See GetImplementationSourceAttr(), and also
206  /// \ref Usd_Create_Or_Get_Property for when to use Get vs Create.
207  /// If specified, author \p defaultValue as the attribute's default,
208  /// sparsely (when it makes sense to do so) if \p writeSparsely is \c true -
209  /// the default for \p writeSparsely is \c false.
211  UsdAttribute CreateImplementationSourceAttr(VtValue const &defaultValue = VtValue(), bool writeSparsely=false) const;
212 
213 public:
214  // --------------------------------------------------------------------- //
215  // ID
216  // --------------------------------------------------------------------- //
217  /// The id is an identifier for the type or purpose of the
218  /// shader. E.g.: Texture or FractalFloat.
219  /// The use of this id will depend on the render context: some will turn it
220  /// into an actual shader path, some will use it to generate shader source
221  /// code dynamically.
222  ///
223  /// \sa SetShaderId()
224  ///
225  ///
226  /// | ||
227  /// | -- | -- |
228  /// | Declaration | `uniform token info:id` |
229  /// | C++ Type | TfToken |
230  /// | \ref Usd_Datatypes "Usd Type" | SdfValueTypeNames->Token |
231  /// | \ref SdfVariability "Variability" | SdfVariabilityUniform |
233  UsdAttribute GetIdAttr() const;
234 
235  /// See GetIdAttr(), and also
236  /// \ref Usd_Create_Or_Get_Property for when to use Get vs Create.
237  /// If specified, author \p defaultValue as the attribute's default,
238  /// sparsely (when it makes sense to do so) if \p writeSparsely is \c true -
239  /// the default for \p writeSparsely is \c false.
241  UsdAttribute CreateIdAttr(VtValue const &defaultValue = VtValue(), bool writeSparsely=false) const;
242 
243 public:
244  // ===================================================================== //
245  // Feel free to add custom code below this line, it will be preserved by
246  // the code generator.
247  //
248  // Just remember to:
249  // - Close the class declaration with };
250  // - Close the namespace with PXR_NAMESPACE_CLOSE_SCOPE
251  // - Close the include guard with #endif
252  // ===================================================================== //
253  // --(BEGIN CUSTOM CODE)--
254 
255 
256  // -------------------------------------------------------------------------
257  /// \anchor UsdShadeNodeDefAPI_ImplementationSource
258  /// \name Shader Source API
259  ///
260  /// This section provides API for identifying the source of a shader's
261  /// implementation.
262  ///
263  /// @{
264 
265  /// Reads the value of info:implementationSource attribute and returns a
266  /// token identifying the attribute that must be consulted to identify the
267  /// shader's source program.
268  ///
269  /// This returns
270  /// * <b>id</b>, to indicate that the "info:id" attribute must be
271  /// consulted.
272  /// * <b>sourceAsset</b> to indicate that the asset-valued
273  /// "info:{sourceType}:sourceAsset" attribute associated with the desired
274  /// <b>sourceType</b> should be consulted to locate the asset with the
275  /// shader's source.
276  /// * <b>sourceCode</b> to indicate that the string-valued
277  /// "info:{sourceType}:sourceCode" attribute associated with the desired
278  /// <b>sourceType</b> should be read to get shader's source.
279  ///
280  /// This issues a warning and returns <b>id</b> if the
281  /// <i>info:implementationSource</i> attribute has an invalid value.
282  ///
283  /// <i>{sourceType}</i> above is a place holder for a token that identifies
284  /// the type of shader source or its implementation. For example: osl,
285  /// glslfx, riCpp etc. This allows a shader to specify different sourceAsset
286  /// (or sourceCode) values for different sourceTypes. The sourceType tokens
287  /// usually correspond to the sourceType value of the SdrParserPlugin that's
288  /// used to parse the shader source (\ref SdrParserPlugin::SourceType).
289  ///
290  /// When sourceType is empty, the corresponding sourceAsset or sourceCode is
291  /// considered to be "universal" (or fallback), which is represented by the
292  /// empty-valued token UsdShadeTokens->universalSourceType. When the
293  /// sourceAsset (or sourceCode) corresponding to a specific, requested
294  /// sourceType is unavailable, the universal sourceAsset (or sourceCode) is
295  /// returned by GetSourceAsset (and GetSourceCode} API, if present.
296  ///
297  /// \sa GetShaderId()
298  /// \sa GetSourceAsset()
299  /// \sa GetSourceCode()
302 
303  /// Sets the shader's ID value. This also sets the
304  /// <i>info:implementationSource</i> attribute on the shader to
305  /// <b>UsdShadeTokens->id</b>, if the existing value is different.
307  bool SetShaderId(const TfToken &id) const;
308 
309  /// Fetches the shader's ID value from the <i>info:id</i> attribute, if the
310  /// shader's <i>info:implementationSource</i> is <b>id</b>.
311  ///
312  /// Returns <b>true</b> if the shader's implementation source is <b>id</b>
313  /// and the value was fetched properly into \p id. Returns false otherwise.
314  ///
315  /// \sa GetImplementationSource()
317  bool GetShaderId(TfToken *id) const;
318 
319  /// Sets the shader's source-asset path value to \p sourceAsset for the
320  /// given source type, \p sourceType.
321  ///
322  /// This also sets the <i>info:implementationSource</i> attribute on the
323  /// shader to <b>UsdShadeTokens->sourceAsset</b>.
325  bool SetSourceAsset(
326  const SdfAssetPath &sourceAsset,
327  const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
328 
329  /// Fetches the shader's source asset value for the specified
330  /// \p sourceType value from the <b>info:<i>sourceType:</i>sourceAsset</b>
331  /// attribute, if the shader's <i>info:implementationSource</i> is
332  /// <b>sourceAsset</b>.
333  ///
334  /// If the <i>sourceAsset</i> attribute corresponding to the requested
335  /// <i>sourceType</i> isn't present on the shader, then the <i>universal</i>
336  /// <i>fallback</i> sourceAsset attribute, i.e. <i>info:sourceAsset</i> is
337  /// consulted, if present, to get the source asset path.
338  ///
339  /// Returns <b>true</b> if the shader's implementation source is
340  /// <b>sourceAsset</b> and the source asset path value was fetched
341  /// successfully into \p sourceAsset. Returns false otherwise.
342  ///
343  /// \sa GetImplementationSource()
345  bool GetSourceAsset(
346  SdfAssetPath *sourceAsset,
347  const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
348 
349  /// Set a sub-identifier to be used with a source asset of the given source
350  /// type. This sets the <b>info:<i>sourceType:</i>sourceAsset:subIdentifier
351  /// </b>.
352  ///
353  /// This also sets the <i>info:implementationSource</i> attribute on the
354  /// shader to <b>UsdShadeTokens->sourceAsset</b>
357  const TfToken &subIdentifier,
358  const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
359 
360  /// Fetches the shader's sub-identifier for the source asset with the
361  /// specified \p sourceType value from the <b>info:<i>sourceType:</i>
362  /// sourceAsset:subIdentifier</b> attribute, if the shader's <i>info:
363  /// implementationSource</i> is <b>sourceAsset</b>.
364  ///
365  /// If the <i>subIdentifier</i> attribute corresponding to the requested
366  /// <i>sourceType</i> isn't present on the shader, then the <i>universal</i>
367  /// <i>fallback</i> sub-identifier attribute, i.e. <i>info:sourceAsset:
368  /// subIdentifier</i> is consulted, if present, to get the sub-identifier
369  /// name.
370  ///
371  /// Returns <b>true</b> if the shader's implementation source is
372  /// <b>sourceAsset</b> and the sub-identifier for the given source type was
373  /// fetched successfully into \p subIdentifier. Returns false otherwise.
376  TfToken *subIdentifier,
377  const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
378 
379  /// Sets the shader's source-code value to \p sourceCode for the given
380  /// source type, \p sourceType.
381  ///
382  /// This also sets the <i>info:implementationSource</i> attribute on the
383  /// shader to <b>UsdShadeTokens->sourceCode</b>.
384  USDSHADE_API
385  bool SetSourceCode(
386  const std::string &sourceCode,
387  const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
388 
389  /// Fetches the shader's source code for the specified \p sourceType value
390  /// by reading the <b>info:<i>sourceType:</i>sourceCode</b> attribute, if
391  /// the shader's <i>info:implementationSource</i> is <b>sourceCode</b>.
392  ///
393  /// If the <i>sourceCode</i> attribute corresponding to the
394  /// requested <i>sourceType</i> isn't present on the shader, then the
395  /// <i>universal</i> or <i>fallback</i> sourceCode attribute (i.e.
396  /// <i>info:sourceCode</i>) is consulted, if present, to get the source
397  /// code.
398  ///
399  /// Returns <b>true</b> if the shader's implementation source is
400  /// <b>sourceCode</b> and the source code string was fetched successfully
401  /// into \p sourceCode. Returns false otherwise.
402  ///
403  /// \sa GetImplementationSource()
405  bool GetSourceCode(
406  std::string *sourceCode,
407  const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
408 
409  /// Fetches the source types for the specified \p prim value
410  /// by reading the <b>info:<i>sourceType</i>:<implementationSource></b>
411  /// attribute, if the shader's <i>info:implementationSource</i> is
412  /// <b>sourceCode</b> or <b>sourceAsset</b>.
413  ///
414  /// If there are no source types listed then this returns an empty list
415  /// \sa GetImplementationSource()
417  std::vector<std::string> GetSourceTypes() const;
418 
419  /// @}
420  // -------------------------------------------------------------------------
421 
422  /// This method attempts to ensure that there is a ShaderNode in the shader
423  /// registry (i.e. \ref SdrRegistry) representing this shader for the
424  /// given \p sourceType. It may return a null pointer if none could be
425  /// found or created.
428  const;
429 
430 };
431 
433 
434 #endif
USDSHADE_API TfToken GetImplementationSource() const
#define USDSHADE_API
Definition: api.h:23
USDSHADE_API std::vector< std::string > GetSourceTypes() const
USDSHADE_API TfStaticData< UsdShadeTokensType > UsdShadeTokens
UsdShadeNodeDefAPI(const UsdPrim &prim=UsdPrim())
Definition: nodeDefAPI.h:79
Single Apply API schema.
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
USDSHADE_API bool SetSourceAsset(const SdfAssetPath &sourceAsset, const TfToken &sourceType=UsdShadeTokens->universalSourceType) const
USDSHADE_API UsdAttribute CreateIdAttr(VtValue const &defaultValue=VtValue(), bool writeSparsely=false) const
SdrShaderNode const * SdrShaderNodeConstPtr
Definition: declare.h:51
virtual USDSHADE_API ~UsdShadeNodeDefAPI()
Destructor.
USDSHADE_API UsdAttribute GetIdAttr() const
USDSHADE_API bool GetSourceAssetSubIdentifier(TfToken *subIdentifier, const TfToken &sourceType=UsdShadeTokens->universalSourceType) const
USDSHADE_API bool SetSourceCode(const std::string &sourceCode, const TfToken &sourceType=UsdShadeTokens->universalSourceType) const
Definition: token.h:70
USDSHADE_API bool GetSourceAsset(SdfAssetPath *sourceAsset, const TfToken &sourceType=UsdShadeTokens->universalSourceType) const
static USDSHADE_API UsdShadeNodeDefAPI Apply(const UsdPrim &prim)
USDSHADE_API bool SetSourceAssetSubIdentifier(const TfToken &subIdentifier, const TfToken &sourceType=UsdShadeTokens->universalSourceType) const
static USDSHADE_API const TfTokenVector & GetSchemaAttributeNames(bool includeInherited=true)
Definition: prim.h:116
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
Definition: path.h:273
USDSHADE_API UsdAttribute GetImplementationSourceAttr() const
UsdSchemaKind
Definition: common.h:112
static const UsdSchemaKind schemaKind
Definition: nodeDefAPI.h:73
USDSHADE_API bool SetShaderId(const TfToken &id) const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
USDSHADE_API UsdSchemaKind _GetSchemaKind() const override
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USDSHADE_API SdrShaderNodeConstPtr GetShaderNodeForSourceType(const TfToken &sourceType) const
Definition: type.h:47
USDSHADE_API bool GetShaderId(TfToken *id) const
USDSHADE_API bool GetSourceCode(std::string *sourceCode, const TfToken &sourceType=UsdShadeTokens->universalSourceType) const
USDSHADE_API UsdAttribute CreateImplementationSourceAttr(VtValue const &defaultValue=VtValue(), bool writeSparsely=false) const
UsdShadeNodeDefAPI(const UsdSchemaBase &schemaObj)
Definition: nodeDefAPI.h:87
static USDSHADE_API bool CanApply(const UsdPrim &prim, std::string *whyNot=nullptr)
Definition: value.h:146
static USDSHADE_API UsdShadeNodeDefAPI Get(const UsdStagePtr &stage, const SdfPath &path)