-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtestMaterial.cpp
More file actions
185 lines (148 loc) · 6.26 KB
/
Copy pathtestMaterial.cpp
File metadata and controls
185 lines (148 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2026 Autodesk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <hvt/material/material.h>
#include <hvt/tasks/resources.h>
#include <pxr/base/vt/value.h>
#include <pxr/imaging/hd/material.h>
#include <pxr/imaging/hd/tokens.h>
#include <pxr/pxr.h>
#include <pxr/usd/sdf/path.h>
#include <gtest/gtest.h>
#include <RenderingFramework/TestFlags.h>
#include <RenderingFramework/TestHelpers.h>
#include <algorithm>
#include <filesystem>
#include <string>
PXR_NAMESPACE_USING_DIRECTIVE
namespace
{
// Builds a fully-valid set of matcap creation parameters that tests can mutate
// to exercise a single failure mode at a time.
hvt::MatcapCreationParams _MakeValidMatcapParams()
{
const auto resourceDir = TestHelpers::getPublicResourceFolder();
const auto shaderPath = resourceDir / "shaders" / "matcap.glslfx";
const auto texturePath = resourceDir / "shaders" / "matcap.png";
hvt::MatcapCreationParams params;
params.materialPath = SdfPath("/matcap");
params.shaderFilePath = shaderPath.string();
params.textureFilePath = texturePath.string();
params.textureInputName = TfToken("matcap");
return params;
}
} // namespace
// =====================================================================
// CreateMaterial — error paths
// =====================================================================
HVT_TEST(TestMaterial, EmptyShaderPath_ReturnsEmpty)
{
auto params = _MakeValidMatcapParams();
params.shaderFilePath.clear();
const VtValue v = hvt::CreateMaterial(params);
EXPECT_TRUE(v.IsEmpty());
}
HVT_TEST(TestMaterial, EmptyTexturePath_ReturnsEmpty)
{
auto params = _MakeValidMatcapParams();
params.textureFilePath.clear();
const VtValue v = hvt::CreateMaterial(params);
EXPECT_TRUE(v.IsEmpty());
}
HVT_TEST(TestMaterial, EmptyMaterialPath_ReturnsEmpty)
{
auto params = _MakeValidMatcapParams();
params.materialPath = {};
const VtValue v = hvt::CreateMaterial(params);
EXPECT_TRUE(v.IsEmpty());
}
HVT_TEST(TestMaterial, NonExistentShaderFile_ReturnsEmpty)
{
auto params = _MakeValidMatcapParams();
params.shaderFilePath = "/no/such/file.glslfx";
const VtValue v = hvt::CreateMaterial(params);
EXPECT_TRUE(v.IsEmpty());
}
HVT_TEST(TestMaterial, NonExistentTextureFile_ReturnsEmpty)
{
auto params = _MakeValidMatcapParams();
params.textureFilePath = "/no/such/file.png";
const VtValue v = hvt::CreateMaterial(params);
EXPECT_TRUE(v.IsEmpty());
}
HVT_TEST(TestMaterial, NonGlslfxShaderFile_ReturnsEmpty)
{
auto params = _MakeValidMatcapParams();
// matcap.png exists but is not a valid .glslfx; SdrRegistry must reject it.
params.shaderFilePath =
(TestHelpers::getPublicResourceFolder() / "shaders" / "matcap.png").string();
const VtValue v = hvt::CreateMaterial(params);
EXPECT_TRUE(v.IsEmpty());
}
HVT_TEST(TestMaterial, UnknownTextureInputName_ReturnsEmpty)
{
auto params = _MakeValidMatcapParams();
params.textureInputName = TfToken("nonexistent");
const VtValue v = hvt::CreateMaterial(params);
EXPECT_TRUE(v.IsEmpty());
}
// =====================================================================
// CreateMaterial — happy path
// =====================================================================
HVT_TEST(TestMaterial, BuildsExpectedNetworkMap_FromDefaultParams)
{
// Need to save and override the resource directory to ensure the
// shader and texture are found in GetDefaultMatcapCreationParams
const auto savedResourceDir = hvt::GetResourceDirectory();
hvt::SetResourceDirectory(TestHelpers::getPublicResourceFolder());
auto params = hvt::GetDefaultMatcapCreationParams();
params.materialPath = SdfPath("/matcap");
const VtValue v = hvt::CreateMaterial(params);
hvt::SetResourceDirectory(savedResourceDir);
ASSERT_TRUE(v.IsHolding<HdMaterialNetworkMap>());
auto const& nm = v.UncheckedGet<HdMaterialNetworkMap>();
ASSERT_EQ(nm.terminals.size(), 1u);
EXPECT_EQ(nm.terminals.front(), params.materialPath);
}
HVT_TEST(TestMaterial, BuildsExpectedNetworkMap)
{
const SdfPath materialPath("/matcap");
auto params = _MakeValidMatcapParams();
params.materialPath = materialPath;
const VtValue v = hvt::CreateMaterial(params);
ASSERT_TRUE(v.IsHolding<HdMaterialNetworkMap>());
auto const& nm = v.UncheckedGet<HdMaterialNetworkMap>();
// Surface terminal exists and points at the material path.
ASSERT_EQ(nm.map.count(HdMaterialTerminalTokens->surface), 1u);
ASSERT_EQ(nm.terminals.size(), 1u);
EXPECT_EQ(nm.terminals.front(), materialPath);
auto const& network = nm.map.at(HdMaterialTerminalTokens->surface);
// Shader node is present at materialPath.
auto shaderIt = std::find_if(network.nodes.begin(), network.nodes.end(),
[&](auto const& n) { return n.path == materialPath; });
ASSERT_NE(shaderIt, network.nodes.end());
// At least one UsdUVTexture child node carrying the texture file path.
auto texIt = std::find_if(network.nodes.begin(), network.nodes.end(),
[&](auto const& n) { return n.identifier == TfToken("UsdUVTexture"); });
ASSERT_NE(texIt, network.nodes.end());
EXPECT_EQ(texIt->path.GetParentPath(), materialPath);
auto const& fileParam = texIt->parameters.at(TfToken("file"));
EXPECT_TRUE(fileParam.IsHolding<std::string>());
EXPECT_NE(fileParam.Get<std::string>().find("matcap.png"), std::string::npos);
// Texture node is wired into the shader's matcap input.
ASSERT_FALSE(network.relationships.empty());
EXPECT_EQ(network.relationships.front().outputId, materialPath);
EXPECT_EQ(network.relationships.front().outputName, TfToken("matcap"));
EXPECT_EQ(network.relationships.front().inputId, texIt->path);
EXPECT_EQ(texIt->path, materialPath.AppendChild(TfToken("matcap")));
}