2
// Copyright 2016 Pixar
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:
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.
14
// You may obtain a copy of the Apache License at
16
// http://www.apache.org/licenses/LICENSE-2.0
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.
24
#include "pxr/usd/usdShade/pShaderUtils.h"
32
#define USD_USAGE_VALS \
33
((Attribute, "attribute")) \
34
((CoshaderInstance, "coshaderInstance")) \
35
((Parameter, "parameter")) \
36
((Binding, "binding"))
39
// We have introduced one change from the current Presto shading OM:
40
// 1) We are holding off on adding "rib" until we really need it; it does not
41
// yet appear in any src or inst menva file on cake.
44
/// \anchor UsdUsageVals
45
/// \brief <b>UsdUsageVals</b> provides the core values for the "Usage"
46
/// metadata that specifies how an Attribute or Relationship is meant to be
47
/// used by clients. It is implicit that no attribute can have more than one
50
/// Values include UsdUsageVals->XX for XX in:
51
/// \li <b>Attribute</b> : use as an "inherited attribute", inspired by
52
/// RiAttribute. Note relationships can also serve as
53
/// "Attribute" with the meaning that the targetted
54
/// prim defines a shader to be emitted.
55
/// \li <b>CoshaderInstance</b> : the attribute's value names a coshader asset.
56
/// Optional "coshaderHandle" metadatum on
57
/// the attribute specifies the name by
58
/// which other shaders can refer to this
59
/// specific coshader instance as a parameter.
60
/// \li <b>Parameter</b> : the attribute or relationship should serve as a
61
/// parameter to the shader or procedural represented by
62
/// the prim on which the attribute is defined.
63
/// \li <b>Binding</b> : a relationship should be consumed as a
64
/// shader binding. The targetted shader(s) can be
66
/// UsdRelationship::ComposeForwardedTargets
68
TF_DEFINE_PRIVATE_TOKENS(
84
////////////////////////////////////////////////////////////////////////
86
template <typename TYPE>
88
_GetMetadataAs(const UsdObject &obj, const std::string &key, TYPE *result)
91
if (obj.GetMetadata(TfToken(key), &val)) {
92
if (val.CanCast<TYPE>()) {
93
val = val.Cast<TYPE>();
94
*result = val.Get<TYPE>();
102
_GetRiType(const UsdObject &obj)
105
_GetMetadataAs(obj, _tokens->riType, &riType);
110
_GetRiName(const UsdObject &obj)
113
if (_GetMetadataAs(obj, _tokens->riName, &riName)) {
116
return obj.GetPath().GetName();
120
_GetPath(const UsdObject &obj)
122
return obj.GetPath().GetString();
125
////////////////////////////////////////////////////////////////////////
127
bool UsdShdIsShaderBinding(const UsdRelationship &rel)
129
if (rel.GetPath().GetName() == _tokens->surface) {
133
std::string riType = _GetRiType(rel);
134
return (riType == _tokens->binding or riType == _tokens->attribute);
137
std::string UsdShdGetSloArgName(const UsdAttribute &prop)
139
std::string riType = _GetRiType(prop);
140
if (riType == _tokens->parameter or
141
riType == _tokens->coshaderInstance) {
142
return _GetRiName(prop);
144
return std::string();
147
std::string UsdShdGetSloArgName(const UsdRelationship &prop)
149
std::string riType = _GetRiType(prop);
150
if (riType == _tokens->parameter or
151
riType == _tokens->coshaderInstance) {
152
return _GetRiName(prop);
154
return std::string();
157
std::string UsdShdGetRibAttributeName(const UsdAttribute &prop)
159
if (_GetRiType(prop) == _tokens->attribute) {
160
return _GetRiName(prop);
162
return std::string();
165
std::string UsdShdGetRibAttributeName(const UsdRelationship &prop)
167
if (_GetRiType(prop) == _tokens->attribute) {
168
return _GetRiName(prop);
170
return std::string();
173
std::string UsdShdGetCoshaderHandle(const UsdAttribute &prop)
175
std::string coshaderHandle;
176
_GetMetadataAs(prop, _tokens->coshaderHandle, &coshaderHandle);
177
return coshaderHandle;
180
std::string UsdShdGetCoshaderHandle(const UsdRelationship &prop)
182
std::string coshaderHandle;
183
_GetMetadataAs(prop, _tokens->coshaderHandle, &coshaderHandle);
184
return coshaderHandle;
187
bool UsdShdIsCoshaderInstance(const UsdAttribute &prop)
189
return (_GetRiType(prop) == _tokens->coshaderInstance);
192
bool UsdShdIsCoshaderInstance(const UsdRelationship &prop)
194
return (_GetRiType(prop) == _tokens->coshaderInstance);
197
bool UsdShdIsExplicitRib(const UsdAttribute &prop)
199
return (_GetRiType(prop) == _tokens->rib);
202
bool UsdShdIsExplicitRib(const UsdRelationship &prop)
204
return (_GetRiType(prop) == _tokens->rib);
207
bool UsdShdIsCoshaderArray(const UsdAttribute &prop)
209
bool isCoshaderArray = false;
210
_GetMetadataAs(prop, _tokens->isCoshaderArray, &isCoshaderArray);
211
return isCoshaderArray;
214
bool UsdShdIsCoshaderArray(const UsdRelationship &prop)
216
bool isCoshaderArray = false;
217
_GetMetadataAs(prop, _tokens->isCoshaderArray, &isCoshaderArray);
218
return isCoshaderArray;
221
string ShdGetPath(const UsdPrim &obj)
223
return obj.GetPath().GetString();
226
string ShdGetShaderHandle(const UsdPrim &obj)
228
std::string riName = _GetRiName(obj);
229
return not riName.empty() ? riName : _GetPath(obj);
232
//////////////////////////////////////////////////////////////////////////////
234
bool UsdShdSplitRibAttributeName(const std::string &name,
235
std::string *attrName, std::string *argName)
237
size_t i = name.find(':');
238
if (i == name.npos) {
240
// If we don't find ':', fall back to '_'
242
if (i == name.npos) {
247
*attrName = name.substr(0,i);
248
*argName = name.substr(i+1);