1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef USDSHADE_GENERATED_SHADER_H
25 #define USDSHADE_GENERATED_SHADER_H
26 
27 /// \file usdShade/shader.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/usdShade/api.h"
31 #include "pxr/usd/usd/typed.h"
32 #include "pxr/usd/usd/prim.h"
33 #include "pxr/usd/usd/stage.h"
34 
35 #include "pxr/usd/usdShade/input.h"
36 #include "pxr/usd/usdShade/output.h"
37 #include "pxr/usd/usdShade/tokens.h"
38 #include "pxr/usd/ndr/declare.h"
39 #include "pxr/usd/sdr/shaderNode.h"
40 
41 #include "pxr/base/vt/value.h"
42 
43 #include "pxr/base/gf/vec3d.h"
44 #include "pxr/base/gf/vec3f.h"
45 #include "pxr/base/gf/matrix4d.h"
46 
47 #include "pxr/base/tf/token.h"
48 #include "pxr/base/tf/type.h"
49 
50 PXR_NAMESPACE_OPEN_SCOPE
51 
52 class SdfAssetPath;
53 
54 // -------------------------------------------------------------------------- //
55 // SHADER                                                                     //
56 // -------------------------------------------------------------------------- //
57 
58 /// \class UsdShadeShader
59 ///
60 /// Base class for all USD shaders. Shaders are the building blocks
61 /// of shading networks. While UsdShadeShader objects are not target specific,
62 /// each renderer or application target may derive its own renderer-specific
63 /// shader object types from this base, if needed.
64 ///
65 /// Objects of this class generally represent a single shading object, whether
66 /// it exists in the target renderer or not. For example, a texture, a fractal,
67 /// or a mix node.
68 ///
69 /// The UsdShadeNodeDefAPI provides attributes to uniquely identify the
70 /// type of this node.  The id resolution into a renderable shader target
71 /// type of this node.  The id resolution into a renderable shader target
72 /// is deferred to the consuming application.
73 ///
74 /// The purpose of representing them in Usd is two-fold:
75 /// \li To represent, via "connections" the topology of the shading network
76 /// that must be reconstructed in the renderer. Facilities for authoring and
77 /// manipulating connections are encapsulated in the API schema
78 /// UsdShadeConnectableAPI.
79 /// \li To present a (partial or full) interface of typed input parameters
80 /// whose values can be set and overridden in Usd, to be provided later at
81 /// render-time as parameter values to the actual render shader objects. Shader
82 /// input parameters are encapsulated in the property schema UsdShadeInput.
83 ///
84 ///
85 class UsdShadeShader : public UsdTyped
86 {
87 public:
88     /// Compile time constant representing what kind of schema this class is.
89     ///
90     /// \sa UsdSchemaKind
91     static const UsdSchemaKind schemaKind = UsdSchemaKind::ConcreteTyped;
92 
93     /// Construct a UsdShadeShader on UsdPrim \p prim .
94     /// Equivalent to UsdShadeShader::Get(prim.GetStage(), prim.GetPath())
95     /// for a \em valid \p prim, but will not immediately throw an error for
96     /// an invalid \p prim
97     explicit UsdShadeShader(const UsdPrim& prim=UsdPrim())
UsdTyped(prim)98         : UsdTyped(prim)
99     {
100     }
101 
102     /// Construct a UsdShadeShader on the prim held by \p schemaObj .
103     /// Should be preferred over UsdShadeShader(schemaObj.GetPrim()),
104     /// as it preserves SchemaBase state.
UsdShadeShader(const UsdSchemaBase & schemaObj)105     explicit UsdShadeShader(const UsdSchemaBase& schemaObj)
106         : UsdTyped(schemaObj)
107     {
108     }
109 
110     /// Destructor.
111     USDSHADE_API
112     virtual ~UsdShadeShader();
113 
114     /// Return a vector of names of all pre-declared attributes for this schema
115     /// class and all its ancestor classes.  Does not include attributes that
116     /// may be authored by custom/extended methods of the schemas involved.
117     USDSHADE_API
118     static const TfTokenVector &
119     GetSchemaAttributeNames(bool includeInherited=true);
120 
121     /// Return a UsdShadeShader holding the prim adhering to this
122     /// schema at \p path on \p stage.  If no prim exists at \p path on
123     /// \p stage, or if the prim at that path does not adhere to this schema,
124     /// return an invalid schema object.  This is shorthand for the following:
125     ///
126     /// \code
127     /// UsdShadeShader(stage->GetPrimAtPath(path));
128     /// \endcode
129     ///
130     USDSHADE_API
131     static UsdShadeShader
132     Get(const UsdStagePtr &stage, const SdfPath &path);
133 
134     /// Attempt to ensure a \a UsdPrim adhering to this schema at \p path
135     /// is defined (according to UsdPrim::IsDefined()) on this stage.
136     ///
137     /// If a prim adhering to this schema at \p path is already defined on this
138     /// stage, return that prim.  Otherwise author an \a SdfPrimSpec with
139     /// \a specifier == \a SdfSpecifierDef and this schema's prim type name for
140     /// the prim at \p path at the current EditTarget.  Author \a SdfPrimSpec s
141     /// with \p specifier == \a SdfSpecifierDef and empty typeName at the
142     /// current EditTarget for any nonexistent, or existing but not \a Defined
143     /// ancestors.
144     ///
145     /// The given \a path must be an absolute prim path that does not contain
146     /// any variant selections.
147     ///
148     /// If it is impossible to author any of the necessary PrimSpecs, (for
149     /// example, in case \a path cannot map to the current UsdEditTarget's
150     /// namespace) issue an error and return an invalid \a UsdPrim.
151     ///
152     /// Note that this method may return a defined prim whose typeName does not
153     /// specify this schema class, in case a stronger typeName opinion overrides
154     /// the opinion at the current EditTarget.
155     ///
156     USDSHADE_API
157     static UsdShadeShader
158     Define(const UsdStagePtr &stage, const SdfPath &path);
159 
160 protected:
161     /// Returns the kind of schema this class belongs to.
162     ///
163     /// \sa UsdSchemaKind
164     USDSHADE_API
165     UsdSchemaKind _GetSchemaKind() const override;
166 
167 private:
168     // needs to invoke _GetStaticTfType.
169     friend class UsdSchemaRegistry;
170     USDSHADE_API
171     static const TfType &_GetStaticTfType();
172 
173     static bool _IsTypedSchema();
174 
175     // override SchemaBase virtuals.
176     USDSHADE_API
177     const TfType &_GetTfType() const override;
178 
179 public:
180     // ===================================================================== //
181     // Feel free to add custom code below this line, it will be preserved by
182     // the code generator.
183     //
184     // Just remember to:
185     //  - Close the class declaration with };
186     //  - Close the namespace with PXR_NAMESPACE_CLOSE_SCOPE
187     //  - Close the include guard with #endif
188     // ===================================================================== //
189     // --(BEGIN CUSTOM CODE)--
190 
191     // -------------------------------------------------------------------------
192     /// \name Conversion to and from UsdShadeConnectableAPI
193     ///
194     /// @{
195 
196     /// Constructor that takes a ConnectableAPI object.
197     /// Allow implicit (auto) conversion of UsdShadeShader to
198     /// UsdShadeConnectableAPI, so that a shader can be passed into any function
199     /// that accepts a ConnectableAPI.
200     USDSHADE_API
201     UsdShadeShader(const UsdShadeConnectableAPI &connectable);
202 
203     /// Contructs and returns a UsdShadeConnectableAPI object with this shader.
204     ///
205     /// Note that most tasks can be accomplished without explicitly constructing
206     /// a UsdShadeConnectable API, since connection-related API such as
207     /// UsdShadeConnectableAPI::ConnectToSource() are static methods, and
208     /// UsdShadeShader will auto-convert to a UsdShadeConnectableAPI when
209     /// passed to functions that want to act generically on a connectable
210     /// UsdShadeConnectableAPI object.
211     USDSHADE_API
212     UsdShadeConnectableAPI ConnectableAPI() const;
213 
214     /// @}
215 
216     // -------------------------------------------------------------------------
217     /// \name Outputs API
218     ///
219     /// Outputs represent a typed attribute on a shader or node-graph whose value
220     /// is computed externally.
221     ///
222     /// When they exist on a node-graph, they are connectable and are typically
223     /// connected to the output of a shader within the node-graph.
224     ///
225     /// @{
226 
227     /// Create an output which can either have a value or can be connected.
228     /// The attribute representing the output is created in the "outputs:"
229     /// namespace. Outputs on a shader cannot be connected, as their
230     /// value is assumed to be computed externally.
231     ///
232     USDSHADE_API
233     UsdShadeOutput CreateOutput(const TfToken& name,
234                                 const SdfValueTypeName& typeName);
235 
236     /// Return the requested output if it exists.
237     ///
238     USDSHADE_API
239     UsdShadeOutput GetOutput(const TfToken &name) const;
240 
241     /// Outputs are represented by attributes in the "outputs:" namespace.
242     /// If \p onlyAuthored is true (the default), then only return authored
243     /// attributes; otherwise, this also returns un-authored builtins.
244     ///
245     USDSHADE_API
246     std::vector<UsdShadeOutput> GetOutputs(bool onlyAuthored=true) const;
247 
248     /// @}
249 
250     // -------------------------------------------------------------------------
251 
252     /// \name Inputs API
253     ///
254     /// Inputs are connectable attribute with a typed value.
255     ///
256     /// On shaders, the shader parameters are encoded as inputs. On node-graphs,
257     /// interface attributes are represented as inputs.
258     ///
259     /// @{
260 
261     /// Create an input which can either have a value or can be connected.
262     /// The attribute representing the input is created in the "inputs:"
263     /// namespace. Inputs on both shaders and node-graphs are connectable.
264     ///
265     USDSHADE_API
266     UsdShadeInput CreateInput(const TfToken& name,
267                               const SdfValueTypeName& typeName);
268 
269     /// Return the requested input if it exists.
270     ///
271     USDSHADE_API
272     UsdShadeInput GetInput(const TfToken &name) const;
273 
274     /// Inputs are represented by attributes in the "inputs:" namespace.
275     /// If \p onlyAuthored is true (the default), then only return authored
276     /// attributes; otherwise, this also returns un-authored builtins.
277     ///
278     USDSHADE_API
279     std::vector<UsdShadeInput> GetInputs(bool onlyAuthored=true) const;
280 
281     /// @}
282 
283     // -------------------------------------------------------------------------
284     /// \name UsdShadeNodeDefAPI forwarding
285     ///
286     /// @{
287 
288     /// Forwards to UsdShadeNodeDefAPI(prim).
289     USDSHADE_API
290     UsdAttribute GetImplementationSourceAttr() const;
291 
292     /// Forwards to UsdShadeNodeDefAPI(prim).
293     USDSHADE_API
294     UsdAttribute CreateImplementationSourceAttr(VtValue const &defaultValue = VtValue(), bool writeSparsely=false) const;
295 
296     /// Forwards to UsdShadeNodeDefAPI(prim).
297     USDSHADE_API
298     UsdAttribute GetIdAttr() const;
299 
300     /// Forwards to UsdShadeNodeDefAPI(prim).
301     USDSHADE_API
302     UsdAttribute CreateIdAttr(VtValue const &defaultValue = VtValue(), bool writeSparsely=false) const;
303 
304     /// Forwards to UsdShadeNodeDefAPI(prim).
305     USDSHADE_API
306     TfToken GetImplementationSource() const;
307 
308     /// Forwards to UsdShadeNodeDefAPI(prim).
309     USDSHADE_API
310     bool SetShaderId(const TfToken &id) const;
311 
312     /// Forwards to UsdShadeNodeDefAPI(prim).
313     USDSHADE_API
314     bool GetShaderId(TfToken *id) const;
315 
316     /// Forwards to UsdShadeNodeDefAPI(prim).
317     USDSHADE_API
318     bool SetSourceAsset(
319         const SdfAssetPath &sourceAsset,
320         const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
321 
322     /// Forwards to UsdShadeNodeDefAPI(prim).
323     USDSHADE_API
324     bool GetSourceAsset(
325         SdfAssetPath *sourceAsset,
326         const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
327 
328     /// Forwards to UsdShadeNodeDefAPI(prim).
329     USDSHADE_API
330     bool SetSourceAssetSubIdentifier(
331         const TfToken &subIdentifier,
332         const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
333 
334     /// Forwards to UsdShadeNodeDefAPI(prim).
335     USDSHADE_API
336     bool GetSourceAssetSubIdentifier(
337         TfToken *subIdentifier,
338         const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
339 
340     /// Forwards to UsdShadeNodeDefAPI(prim).
341     USDSHADE_API
342     bool SetSourceCode(
343         const std::string &sourceCode,
344         const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
345 
346     /// Forwards to UsdShadeNodeDefAPI(prim).
347     USDSHADE_API
348     bool GetSourceCode(
349         std::string *sourceCode,
350         const TfToken &sourceType=UsdShadeTokens->universalSourceType) const;
351 
352     /// Forwards to UsdShadeNodeDefAPI(prim).
353     USDSHADE_API
354     SdrShaderNodeConstPtr GetShaderNodeForSourceType(const TfToken &sourceType)
355         const;
356 
357     /// @}
358 
359     // -------------------------------------------------------------------------
360 
361     /// \anchor UsdShadeShader_SdrMetadata_API
362     /// \name Shader Sdr Metadata API
363     ///
364     /// This section provides API for authoring and querying shader registry
365     /// metadata. When the shader's implementationSource is <b>sourceAsset</b>
366     /// or <b>sourceCode</b>, the authored "sdrMetadata" dictionary value
367     /// provides additional metadata needed to process the shader source
368     /// correctly. It is used in combination with the sourceAsset or sourceCode
369     /// value to fetch the appropriate node from the shader registry.
370     ///
371     /// We expect the keys in sdrMetadata to correspond to the keys
372     /// in \ref SdrNodeMetadata. However, this is not strictly enforced in the
373     /// API. The only allowed value type in the "sdrMetadata" dictionary is a
374     /// std::string since it needs to be converted into a NdrTokenMap, which Sdr
375     /// will parse using the utilities available in \ref SdrMetadataHelpers.
376     ///
377     /// @{
378 
379     /// Returns this shader's composed "sdrMetadata" dictionary as a
380     /// NdrTokenMap.
381     USDSHADE_API
382     NdrTokenMap GetSdrMetadata() const;
383 
384     /// Returns the value corresponding to \p key in the composed
385     /// <b>sdrMetadata</b> dictionary.
386     USDSHADE_API
387     std::string GetSdrMetadataByKey(const TfToken &key) const;
388 
389     /// Authors the given \p sdrMetadata on this shader at the current
390     /// EditTarget.
391     USDSHADE_API
392     void SetSdrMetadata(const NdrTokenMap &sdrMetadata) const;
393 
394     /// Sets the value corresponding to \p key to the given string \p value, in
395     /// the shader's "sdrMetadata" dictionary at the current EditTarget.
396     USDSHADE_API
397     void SetSdrMetadataByKey(
398         const TfToken &key,
399         const std::string &value) const;
400 
401     /// Returns true if the shader has a non-empty composed "sdrMetadata"
402     /// dictionary value.
403     USDSHADE_API
404     bool HasSdrMetadata() const;
405 
406     /// Returns true if there is a value corresponding to the given \p key in
407     /// the composed "sdrMetadata" dictionary.
408     USDSHADE_API
409     bool HasSdrMetadataByKey(const TfToken &key) const;
410 
411     /// Clears any "sdrMetadata" value authored on the shader in the current
412     /// EditTarget.
413     USDSHADE_API
414     void ClearSdrMetadata() const;
415 
416     /// Clears the entry corresponding to the given \p key in the
417     /// "sdrMetadata" dictionary authored in the current EditTarget.
418     USDSHADE_API
419     void ClearSdrMetadataByKey(const TfToken &key) const;
420 
421     /// @}
422 };
423 
424 PXR_NAMESPACE_CLOSE_SCOPE
425 
426 #endif
427