forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRender.cpp
226 lines (203 loc) · 7 KB
/
Render.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXTest/Catch/catch.hpp>
#include <MaterialXTest/RenderUtil.h>
#include <MaterialXRender/ShaderRenderer.h>
#include <MaterialXRender/StbImageLoader.h>
#include <MaterialXRender/TinyObjLoader.h>
#include <MaterialXRender/Types.h>
#ifdef MATERIALX_BUILD_OIIO
#include <MaterialXRender/OiioImageLoader.h>
#endif
#ifdef MATERIALX_BUILD_CONTRIB
#include <MaterialXContrib/Handlers/TinyEXRImageLoader.h>
#endif
#include <fstream>
#include <iostream>
#include <limits>
#include <unordered_set>
namespace mx = MaterialX;
TEST_CASE("Render: Half Float", "[rendercore]")
{
const std::vector<float> values =
{
0.0f, 0.25f, 0.5f, 0.75f,
1.0f, 8.0f, 64.0f, 512.0f,
std::numeric_limits<float>::infinity()
};
const std::vector<float> signs = { 1.0f, -1.0f };
for (float value : values)
{
for (float sign : signs)
{
float f(value * sign);
mx::Half h(f);
REQUIRE(h == f);
REQUIRE(h + mx::Half(1.0f) == f + 1.0f);
REQUIRE(h - mx::Half(1.0f) == f - 1.0f);
REQUIRE(h * mx::Half(2.0f) == f * 2.0f);
REQUIRE(h / mx::Half(2.0f) == f / 2.0f);
REQUIRE((h += mx::Half(3.0f)) == (f += 3.0f));
REQUIRE((h -= mx::Half(3.0f)) == (f -= 3.0f));
REQUIRE((h *= mx::Half(4.0f)) == (f *= 4.0f));
REQUIRE((h /= mx::Half(4.0f)) == (f /= 4.0f));
REQUIRE(-h == -f);
}
}
}
struct GeomHandlerTestOptions
{
mx::GeometryHandlerPtr geomHandler;
std::ofstream* logFile;
mx::StringSet testExtensions;
mx::StringVec skipExtensions;
};
void testGeomHandler(GeomHandlerTestOptions& options)
{
mx::FilePath imagePath = mx::FilePath::getCurrentPath() / mx::FilePath("resources/Geometry/");
mx::FilePathVec files;
unsigned int loadFailed = 0;
for (const std::string& extension : options.testExtensions)
{
if (options.skipExtensions.end() != std::find(options.skipExtensions.begin(), options.skipExtensions.end(), extension))
{
continue;
}
files = imagePath.getFilesInDirectory(extension);
for (const mx::FilePath& file : files)
{
const mx::FilePath filePath = imagePath / file;
bool loaded = options.geomHandler->loadGeometry(filePath);
if (options.logFile)
{
*(options.logFile) << "Loaded image: " << filePath.asString() << ". Loaded: " << loaded << std::endl;
}
if (!loaded)
{
loadFailed++;
}
}
}
CHECK(loadFailed == 0);
}
TEST_CASE("Render: Geometry Handler Load", "[rendercore]")
{
std::ofstream geomHandlerLog;
geomHandlerLog.open("render_geom_handler_test.txt");
bool geomLoaded = false;
try
{
geomHandlerLog << "** Test TinyOBJ geom loader **" << std::endl;
mx::TinyObjLoaderPtr loader = mx::TinyObjLoader::create();
mx::GeometryHandlerPtr handler = mx::GeometryHandler::create();
handler->addLoader(loader);
GeomHandlerTestOptions options;
options.logFile = &geomHandlerLog;
options.geomHandler = handler;
handler->supportedExtensions(options.testExtensions);
testGeomHandler(options);
geomLoaded = true;
}
catch (mx::ExceptionShaderRenderError& e)
{
for (const auto& error : e.errorLog())
{
geomHandlerLog << e.what() << " " << error << std::endl;
}
}
catch (mx::Exception& e)
{
std::cout << e.what();
}
CHECK(geomLoaded);
geomHandlerLog.close();
}
struct ImageHandlerTestOptions
{
mx::ImageHandlerPtr imageHandler;
std::ofstream* logFile;
mx::StringSet testExtensions;
mx::StringVec skipExtensions;
};
void testImageHandler(ImageHandlerTestOptions& options)
{
mx::FilePath imagePath = mx::FilePath::getCurrentPath() / mx::FilePath("resources/Images/");
mx::FilePathVec files;
unsigned int loadFailed = 0;
for (const std::string& extension : options.testExtensions)
{
if (options.skipExtensions.end() != std::find(options.skipExtensions.begin(), options.skipExtensions.end(), extension))
{
continue;
}
files = imagePath.getFilesInDirectory(extension);
for (const mx::FilePath& file : files)
{
const mx::FilePath filePath = imagePath / file;
mx::ImagePtr image = options.imageHandler->acquireImage(filePath, false);
CHECK(image);
image->releaseResourceBuffer();
CHECK(!image->getResourceBuffer());
if (options.logFile)
{
*(options.logFile) << "Loaded image: " << filePath.asString() << ". Loaded: " << (bool) image << std::endl;
}
if (!image)
{
loadFailed++;
}
}
}
CHECK(loadFailed == 0);
}
TEST_CASE("Render: Image Handler Load", "[rendercore]")
{
std::ofstream imageHandlerLog;
imageHandlerLog.open("render_image_handler_test.txt");
bool imagesLoaded = false;
try
{
mx::Color4 color(1.0f, 0.0f, 0.0f, 1.0f);
mx::ImagePtr uniformImage = mx::Image::createConstantColor(1, 1, color);
CHECK(uniformImage->getWidth() == 1);
CHECK(uniformImage->getHeight() == 1);
CHECK(uniformImage->getMaxMipCount() == 1);
CHECK(uniformImage->getTexelColor(0, 0) == color);
mx::ImageHandlerPtr imageHandler = mx::ImageHandler::create(nullptr);
ImageHandlerTestOptions options;
options.logFile = &imageHandlerLog;
imageHandlerLog << "** Test STB image loader **" << std::endl;
mx::StbImageLoaderPtr stbLoader = mx::StbImageLoader::create();
imageHandler->addLoader(stbLoader);
options.testExtensions = stbLoader->supportedExtensions();
options.imageHandler = imageHandler;
testImageHandler(options);
#if defined(MATERIALX_BUILD_OIIO) && defined(OPENIMAGEIO_ROOT_DIR)
imageHandlerLog << "** Test OpenImageIO image loader **" << std::endl;
mx::OiioImageLoaderPtr oiioLoader = mx::OiioImageLoader::create();
mx::ImageHandlerPtr imageHandler3 = mx::ImageHandler::create(nullptr);
imageHandler3->addLoader(oiioLoader);
options.testExtensions = oiioLoader->supportedExtensions();
options.imageHandler = imageHandler3;
// Getting libpng warning: iCCP: known incorrect sRGB profile for some reason. TBD.
options.skipExtensions.push_back("gif");
testImageHandler(options);
#endif
imagesLoaded = true;
}
catch (mx::ExceptionShaderRenderError& e)
{
for (const auto& error : e.errorLog())
{
imageHandlerLog << e.what() << " " << error << std::endl;
}
}
catch (mx::Exception& e)
{
std::cout << e.what();
}
CHECK(imagesLoaded);
imageHandlerLog.close();
}