~popey/+junk/usd

« back to all changes in this revision

Viewing changes to USD/pxr/usd/lib/usdShade/pShaderUtils.cpp

  • Committer: Alan Pope
  • Date: 2016-09-29 12:05:28 UTC
  • Revision ID: alan@popey.com-20160929120528-32j3uk1x0dgaorip
Initial attempt to snap

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "pxr/usd/usdShade/pShaderUtils.h"
 
25
#include <vector>
 
26
#include <string>
 
27
 
 
28
using std::vector;
 
29
using std::string;
 
30
 
 
31
/// \hideinitializer
 
32
#define USD_USAGE_VALS                               \
 
33
        ((Attribute,        "attribute"))            \
 
34
        ((CoshaderInstance, "coshaderInstance"))     \
 
35
        ((Parameter,        "parameter"))            \
 
36
        ((Binding,          "binding"))
 
37
 
 
38
// XXX
 
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.
 
42
//
 
43
 
 
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
 
48
/// "use"
 
49
///
 
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
 
65
///                            resolved via 
 
66
///                            UsdRelationship::ComposeForwardedTargets
 
67
 
 
68
TF_DEFINE_PRIVATE_TOKENS(
 
69
    _tokens,
 
70
    (surface)
 
71
    (attribute)
 
72
    (parameter)
 
73
    (rib)
 
74
    (coshaderInstance)
 
75
    (coshaderHandle)
 
76
    (binding)
 
77
    (isCoshaderArray)
 
78
    (displayColor)
 
79
    (displayOpacity)
 
80
    (riName)
 
81
    (riType)
 
82
);
 
83
 
 
84
////////////////////////////////////////////////////////////////////////
 
85
 
 
86
template <typename TYPE>
 
87
static bool
 
88
_GetMetadataAs(const UsdObject &obj, const std::string &key, TYPE *result)
 
89
{
 
90
    VtValue val;
 
91
    if (obj.GetMetadata(TfToken(key), &val)) {
 
92
        if (val.CanCast<TYPE>()) {
 
93
            val = val.Cast<TYPE>();
 
94
            *result = val.Get<TYPE>();
 
95
            return true;
 
96
        }
 
97
    }
 
98
    return false;
 
99
}
 
100
 
 
101
static std::string
 
102
_GetRiType(const UsdObject &obj)
 
103
{
 
104
    std::string riType;
 
105
    _GetMetadataAs(obj, _tokens->riType, &riType);
 
106
    return riType;
 
107
}
 
108
 
 
109
static std::string
 
110
_GetRiName(const UsdObject &obj)
 
111
{
 
112
    std::string riName;
 
113
    if (_GetMetadataAs(obj, _tokens->riName, &riName)) {
 
114
        return riName;
 
115
    }
 
116
    return obj.GetPath().GetName();
 
117
}
 
118
 
 
119
static string
 
120
_GetPath(const UsdObject &obj)
 
121
{
 
122
    return obj.GetPath().GetString();
 
123
}
 
124
 
 
125
////////////////////////////////////////////////////////////////////////
 
126
 
 
127
bool UsdShdIsShaderBinding(const UsdRelationship &rel)
 
128
{
 
129
    if (rel.GetPath().GetName() == _tokens->surface) {
 
130
        return true;
 
131
    }
 
132
 
 
133
    std::string riType = _GetRiType(rel);
 
134
    return (riType == _tokens->binding or riType == _tokens->attribute);
 
135
}
 
136
 
 
137
std::string UsdShdGetSloArgName(const UsdAttribute &prop)
 
138
{
 
139
    std::string riType = _GetRiType(prop);
 
140
    if (riType == _tokens->parameter or
 
141
        riType == _tokens->coshaderInstance) {
 
142
        return _GetRiName(prop);
 
143
    }
 
144
    return std::string();
 
145
}
 
146
 
 
147
std::string UsdShdGetSloArgName(const UsdRelationship &prop)
 
148
{
 
149
    std::string riType = _GetRiType(prop);
 
150
    if (riType == _tokens->parameter or
 
151
        riType == _tokens->coshaderInstance) {
 
152
        return _GetRiName(prop);
 
153
    }
 
154
    return std::string();
 
155
}
 
156
 
 
157
std::string UsdShdGetRibAttributeName(const UsdAttribute &prop)
 
158
{
 
159
    if (_GetRiType(prop) == _tokens->attribute) {
 
160
        return _GetRiName(prop);
 
161
    }
 
162
    return std::string();
 
163
}
 
164
 
 
165
std::string UsdShdGetRibAttributeName(const UsdRelationship &prop)
 
166
{
 
167
    if (_GetRiType(prop) == _tokens->attribute) {
 
168
        return _GetRiName(prop);
 
169
    }
 
170
    return std::string();
 
171
}
 
172
 
 
173
std::string UsdShdGetCoshaderHandle(const UsdAttribute &prop)
 
174
{
 
175
    std::string coshaderHandle;
 
176
    _GetMetadataAs(prop, _tokens->coshaderHandle, &coshaderHandle);
 
177
    return coshaderHandle;
 
178
}
 
179
 
 
180
std::string UsdShdGetCoshaderHandle(const UsdRelationship &prop)
 
181
{
 
182
    std::string coshaderHandle;
 
183
    _GetMetadataAs(prop, _tokens->coshaderHandle, &coshaderHandle);
 
184
    return coshaderHandle;
 
185
}
 
186
 
 
187
bool UsdShdIsCoshaderInstance(const UsdAttribute &prop)
 
188
{
 
189
    return (_GetRiType(prop) == _tokens->coshaderInstance);
 
190
}
 
191
 
 
192
bool UsdShdIsCoshaderInstance(const UsdRelationship &prop)
 
193
{
 
194
    return (_GetRiType(prop) == _tokens->coshaderInstance);
 
195
}
 
196
 
 
197
bool UsdShdIsExplicitRib(const UsdAttribute &prop)
 
198
{
 
199
    return (_GetRiType(prop) == _tokens->rib);
 
200
}
 
201
 
 
202
bool UsdShdIsExplicitRib(const UsdRelationship &prop)
 
203
{
 
204
    return (_GetRiType(prop) == _tokens->rib);
 
205
}
 
206
 
 
207
bool UsdShdIsCoshaderArray(const UsdAttribute &prop)
 
208
{
 
209
    bool isCoshaderArray = false;
 
210
    _GetMetadataAs(prop, _tokens->isCoshaderArray, &isCoshaderArray);
 
211
    return isCoshaderArray;
 
212
}
 
213
 
 
214
bool UsdShdIsCoshaderArray(const UsdRelationship &prop)
 
215
{
 
216
    bool isCoshaderArray = false;
 
217
    _GetMetadataAs(prop, _tokens->isCoshaderArray, &isCoshaderArray);
 
218
    return isCoshaderArray;
 
219
}
 
220
 
 
221
string ShdGetPath(const UsdPrim &obj)
 
222
{
 
223
    return obj.GetPath().GetString();
 
224
}
 
225
 
 
226
string ShdGetShaderHandle(const UsdPrim &obj)
 
227
{
 
228
    std::string riName = _GetRiName(obj);
 
229
    return not riName.empty() ? riName : _GetPath(obj);
 
230
}
 
231
 
 
232
//////////////////////////////////////////////////////////////////////////////
 
233
 
 
234
bool UsdShdSplitRibAttributeName(const std::string &name,
 
235
                                 std::string *attrName, std::string *argName)
 
236
{
 
237
    size_t i = name.find(':');
 
238
    if (i == name.npos) {
 
239
 
 
240
        // If we don't find ':', fall back to '_'
 
241
        i = name.find('_');
 
242
        if (i == name.npos) {
 
243
            return false;
 
244
        }
 
245
    }
 
246
    
 
247
    *attrName = name.substr(0,i);
 
248
    *argName = name.substr(i+1);
 
249
    return true;
 
250
}