Debian Codegraph @

tools/roughness-prefilter/src/main.cpp

raw
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <algorithm>
18 #include <cstring>
19 #include <fstream>
20 #include <iostream>
21 #include <thread>
22
23 #include <math/vec3.h>
24
25 #include <image/ColorTransform.h>
26 #include <image/ImageOps.h>
27 #include <image/ImageSampler.h>
28 #include <image/KtxBundle.h>
29
30 #include <imageio/ImageDecoder.h>
31 #include <imageio/ImageEncoder.h>
32
33 #include <utils/JobSystem.h>
34 #include <utils/Path.h>
35
36 #include <getopt/getopt.h>
37
38 using namespace filament::math;
39 using namespace image;
40 using namespace utils;
41
42 static ImageEncoder::Format g_format = ImageEncoder::Format::PNG_LINEAR;
43 static bool g_formatSpecified = false;
44 static bool g_ktxContainer = false;
45 static bool g_linearOutput = false;
46 static std::string g_compression;
47
48 static Path g_roughnessMap;
49 static float g_roughness = 0.0;
50
51 static void printUsage(const char* name) {
52 std::string execName(Path(name).getName());
53 std::string usage(
54 "ROUGHNESSPREFILTER generates pre-filtered roughness maps from normal maps\n"
55 "to help mitigate specular aliasing.\n"
56 "Usage:\n"
57 " ROUGHNESSPREFILTER [options] <normal-map> <output-roughness-map>\n"
58 "\n"
59 "Output note:\n"
60 " One file will be generated per mip-level. The size of the first level will be\n"
61 " the greater of the input normal map and input roughness map.\n"
62 "\n"
63 "Supported input formats:\n"
64 " PNG, 8 and 16 bits\n"
65 " Radiance (.hdr)\n"
66 " Photoshop (.psd), 16 and 32 bits\n"
67 " OpenEXR (.exr)\n"
68 "\n"
69 "Options:\n"
70 " --help, -h\n"
71 " print this message\n\n"
72 " --license\n"
73 " Print copyright and license information\n\n"
74 " --roughness=[0..1], -r [0..1]\n"
75 " desired constant roughness, ignored if --roughness-map is specified\n\n"
76 " --roughness-map=<input-roughness-map>, -m <input-roughness-map>\n"
77 " input roughness map\n\n"
78 " --format=[exr|hdr|psd|png|dds|ktx], -f [exr|hdr|psd|png|dds|ktx]\n"
79 " specify output file format, inferred from file name if omitted\n\n"
80 " --compression=COMPRESSION, -c COMPRESSION\n"
81 " format specific compression:\n"
82 " PNG: Ignored\n"
83 " Radiance: Ignored\n"
84 " Photoshop: 16 (default), 32\n"
85 " OpenEXR: RAW, RLE, ZIPS, ZIP, PIZ (default)\n"
86 " DDS: 8, 16 (default), 32\n\n"
87 " --linear, -l\n"
88 " force linear output when the PNG format is selected\n\n"
89 );
90
91 const std::string from("ROUGHNESSPREFILTER");
92 for (size_t pos = usage.find(from); pos != std::string::npos; pos = usage.find(from, pos)) {
93 usage.replace(pos, from.length(), execName);
94 }
95 printf("%s", usage.c_str());
96 }
97
98 static void license() {
99 static const char *license[] = {
100 #include "licenses/licenses.inc"
101 nullptr
102 };
103
104 const char **p = &license[0];
105 while (*p)
106 std::cout << *p++ << std::endl;
107 }
108
109 static int handleArguments(int argc, char* argv[]) {
110 static constexpr const char* OPTSTR = "hr:c:f:m:l";
111 static const struct option OPTIONS[] = {
112 { "help", no_argument, nullptr, 'h' },
113 { "license", no_argument, nullptr, 's' },
114 { "format", required_argument, nullptr, 'f' },
115 { "compression", required_argument, nullptr, 'c' },
116 { "roughness", required_argument, nullptr, 'r' },
117 { "roughness-map", required_argument, nullptr, 'm' },
118 { "linear", no_argument, nullptr, 'l' },
119 { nullptr, 0, nullptr, 0 } // termination of the option list
120 };
121
122 int opt;
123 int optionIndex = 0;
124
125 while ((opt = getopt_long(argc, argv, OPTSTR, OPTIONS, &optionIndex)) >= 0) {
126 std::string arg(optarg ? optarg : "");
127 switch (opt) {
128 default:
129 case 'h':
130 printUsage(argv[0]);
131 exit(0);
132 // break;
133 case 's':
134 license();
135 exit(0);
136 // break;
137 case 'f':
138 if (arg == "png") {
139 g_format = ImageEncoder::Format::PNG;
140 g_formatSpecified = true;
141 }
142 if (arg == "hdr") {
143 g_format = ImageEncoder::Format::HDR;
144 g_formatSpecified = true;
145 }
146 if (arg == "exr") {
147 g_format = ImageEncoder::Format::EXR;
148 g_formatSpecified = true;
149 }
150 if (arg == "psd") {
151 g_format = ImageEncoder::Format::PSD;
152 g_formatSpecified = true;
153 }
154 if (arg == "dds") {
155 g_format = ImageEncoder::Format::DDS_LINEAR;
156 g_formatSpecified = true;
157 }
158 if (arg == "ktx") {
159 g_ktxContainer = true;
160 g_formatSpecified = true;
161 }
162 break;
163 case 'c':
164 g_compression = arg;
165 break;
166 case 'r':
167 try {
168 g_roughness = std::stof(arg);
169 } catch (std::invalid_argument& e) {
170 std::cerr << "The specified roughness must be [0..1]: " << arg << std::endl;
171 exit(1);
172 } catch (std::out_of_range& e) {
173 std::cerr << "The specified roughness must be [0..1]: " << arg << std::endl;
174 exit(1);
175 }
176 break;
177 case 'm':
178 g_roughnessMap = arg;
179 break;
180 case 'l':
181 g_linearOutput = true;
182 break;
183 }
184 }
185
186 return optind;
187 }
188
189 inline bool isPOT(size_t x) {
190 return !(x & (x - 1));
191 }
192
193 float normalFiltering(float roughness, float variance, float threshold) {
194 return std::sqrt(roughness * roughness + std::min(2.0f * variance, threshold * threshold));
195 }
196
197 float solveVMF(const float2& pos, const size_t sampleCount, const float roughness,
198 const LinearImage& normal) {
199
200 float3 averageNormal(0.0f);
201 float2 topLeft(-float(sampleCount) / 2.0f + 0.5f);
202
203 for (size_t y = 0; y < sampleCount; y++) {
204 for (size_t x = 0; x < sampleCount; x++) {
205 float2 offset(topLeft + float2(x, y));
206 float2 samplePos(floor(pos + offset) + 0.5f);
207 float3 sampleNormal = *normal.get<float3>(size_t(samplePos.x), size_t(samplePos.y));
208 sampleNormal = sampleNormal * 2.0f - 1.0f;
209
210 averageNormal += normalize(sampleNormal);
211 }
212 }
213
214 averageNormal /= (sampleCount * sampleCount);
215
216 float r = length(averageNormal);
217 float variance = 0.0f;
218
219 if (r < 1.0f) {
220 float r2 = r * r;
221 float kappa = (3.0f * r - r * r2) / (1.0f - r2);
222 variance = 0.25f / kappa;
223 }
224
225 // See: Karis, 2018, "Normal map filtering using vMF (part 3)"
226 // The original formulation in Neubelt et al. 2013,
227 // "Crafting a Next-Gen Material Pipeline for The Order: 1886" contains an error
228 // and defines alpha' = sqrt(alpha^2 + 1 / kappa)
229 return normalFiltering(roughness, variance, 0.2f);
230 }
231
232 void prefilter(const LinearImage& normal, const size_t mipLevel, LinearImage& output) {
233 const size_t width = output.getWidth();
234 const size_t height = output.getHeight();
235 const size_t sampleCount = 1u << mipLevel;
236
237 for (size_t y = 0; y < height; y++) {
238 auto outputRow = output.get<float>(0, y);
239 for (size_t x = 0; x < width; x++, outputRow++) {
240 const float2 uv = (float2(x, y) + 0.5f) / float2(width, height);
241 const float2 pos = uv * normal.getWidth();
242 const float roughness = g_roughness;
243 *outputRow = solveVMF(pos, sampleCount, roughness, normal);
244 }
245 }
246 }
247
248 template<bool FIRST_MIP>
249 void prefilter(const LinearImage& normal, const LinearImage& roughness, const size_t mipLevel,
250 LinearImage& output) {
251 const size_t width = output.getWidth();
252 const size_t height = output.getHeight();
253 const size_t sampleCount = 1u << mipLevel;
254
255 for (size_t y = 0; y < height; y++) {
256 auto outputRow = output.get<float>(0, y);
257 for (size_t x = 0; x < width; x++, outputRow++) {
258 auto data = roughness.get<float>(x, y);
259 if (FIRST_MIP) {
260 *outputRow = *data;
261 } else {
262 const float2 uv = (float2(x, y) + 0.5f) / float2(width, height);
263 const float2 pos = uv * normal.getWidth();
264 *outputRow = solveVMF(pos, sampleCount, *data, normal);
265 }
266 }
267 }
268 }
269
270 int main(int argc, char* argv[]) {
271 int optionIndex = handleArguments(argc, argv);
272
273 int numArgs = argc - optionIndex;
274 if (numArgs < 2) {
275 printUsage(argv[0]);
276 return 1;
277 }
278
279 Path normalMap(argv[optionIndex ]);
280 Path outputMap(argv[optionIndex + 1]);
281
282 if (!normalMap.exists()) {
283 std::cerr << "The input normal map does not exist: " << normalMap << std::endl;
284 exit(1);
285 }
286
287 // make sure we load the normal maps as linear data
288 std::ifstream inputStream(normalMap, std::ios::binary);
289 LinearImage normalImage = ImageDecoder::decode(inputStream, normalMap,
290 ImageDecoder::ColorSpace::LINEAR);
291 inputStream.close();
292
293 if (!normalImage.isValid()) {
294 std::cerr << "The input normal map is invalid: " << normalMap << std::endl;
295 exit(1);
296 }
297
298 if (!isPOT(normalImage.getWidth()) || !isPOT(normalImage.getHeight()) ||
299 normalImage.getWidth() != normalImage.getHeight()) {
300 std::cerr << "The input normal map must have equal power-of-2 dimensions: " <<
301 normalImage.getWidth() << "x" << normalImage.getHeight() << std::endl;
302 exit(1);
303 }
304
305 LinearImage roughnessImage;
306 std::vector<LinearImage> mipImages;
307 bool hasRoughnessMap = false;
308
309 if (!g_roughnessMap.isEmpty()) {
310 Path roughnessMap(g_roughnessMap);
311 if (!roughnessMap.exists()) {
312 std::cerr << "The input roughness map does not exist: " << roughnessMap << std::endl;
313 exit(1);
314 }
315
316 hasRoughnessMap = true;
317
318 inputStream.open(roughnessMap, std::ios::binary);
319 roughnessImage = ImageDecoder::decode(inputStream, roughnessMap,
320 ImageDecoder::ColorSpace::LINEAR);
321 inputStream.close();
322
323 if (!roughnessImage.isValid()) {
324 std::cerr << "The input roughness map is invalid: " << roughnessMap << std::endl;
325 exit(1);
326 }
327
328 if (!isPOT(roughnessImage.getWidth()) || !isPOT(roughnessImage.getHeight()) ||
329 roughnessImage.getWidth() != roughnessImage.getHeight()) {
330 std::cerr << "The input roughness map must have equal power-of-2 dimensions: " <<
331 roughnessImage.getWidth() << "x" << roughnessImage.getHeight() << std::endl;
332 exit(1);
333 }
334
335 if (roughnessImage.getWidth() != normalImage.getWidth() ||
336 roughnessImage.getHeight() != normalImage.getHeight()) {
337 std::cerr << "The input normal and roughness maps must have the same dimensions"
338 << std::endl;
339 exit(1);
340 }
341
342 roughnessImage = extractChannel(roughnessImage, 0);
343 }
344
345 if (!g_formatSpecified) {
346 g_format = ImageEncoder::chooseFormat(outputMap, g_linearOutput);
347 } else {
348 if (g_format == ImageEncoder::Format::PNG && g_linearOutput) {
349 g_format = ImageEncoder::Format::PNG_LINEAR;
350 }
351 }
352
353 const size_t width = hasRoughnessMap ? roughnessImage.getWidth() : normalImage.getWidth();
354 const size_t height = hasRoughnessMap ? roughnessImage.getHeight() : normalImage.getHeight();
355 const size_t mipLevels = size_t(std::log2f(width)) + 1;
356
357 if (hasRoughnessMap) {
358 mipImages.resize(mipLevels);
359 mipImages[0] = roughnessImage;
360 image::generateMipmaps(roughnessImage, image::Filter::BOX, &mipImages[1], mipLevels - 1);
361 }
362
363 JobSystem js;
364 js.adopt();
365
366 // For thread safety, we allocate each KTX blob now, before invoking the job system.
367 image::KtxBundle bundle(mipLevels, 1, false);
368 if (g_ktxContainer) {
369 bundle.info() = {
370 .endianness = KtxBundle::ENDIAN_DEFAULT,
371 .glType = KtxBundle::UNSIGNED_BYTE,
372 .glTypeSize = 1,
373 .glFormat = KtxBundle::LUMINANCE,
374 .glInternalFormat = KtxBundle::LUMINANCE,
375 .glBaseInternalFormat = KtxBundle::LUMINANCE,
376 .pixelWidth = (uint32_t) width,
377 .pixelHeight = (uint32_t) height,
378 .pixelDepth = 0,
379 };
380 for (uint32_t i = 0; i < mipLevels; i++) {
381 bundle.allocateBlob({i}, (width >> i) * (height >> i));
382 }
383 }
384
385 JobSystem::Job* parent = js.createJob();
386 for (size_t i = 0; i < mipLevels; i++) {
387 JobSystem::Job* mip = jobs::createJob(js, parent, [&bundle, &normalImage, &mipImages,
388 outputMap, i, width, height, hasRoughnessMap]() {
389 const size_t w = width >> i;
390 const size_t h = height >> i;
391
392 LinearImage image(w, h, 1);
393
394 if (i == 0) {
395 if (hasRoughnessMap) {
396 if (mipImages.at(0).getWidth() == image.getWidth()) {
397 const size_t size = image.getWidth() * image.getHeight() * sizeof(float);
398 memcpy(image.getPixelRef(), mipImages.at(0).getPixelRef(), size);
399 } else {
400 prefilter<true>(normalImage, mipImages.at(0), 0, image);
401 }
402 } else {
403 std::fill_n(image.get<float>(), w * h, g_roughness);
404 }
405 } else {
406 if (hasRoughnessMap) {
407 prefilter<false>(normalImage, mipImages.at(i), i, image);
408 } else {
409 prefilter(normalImage, i, image);
410 }
411 }
412
413 if (g_ktxContainer) {
414 std::unique_ptr<uint8_t[]> data;
415 if (g_linearOutput) {
416 data = image::fromLinearToGrayscale<uint8_t>(image);
417 } else {
418 data = image::fromLinearTosRGB<uint8_t, 1>(image);
419 }
420 bundle.setBlob({(uint32_t) i}, data.get(), w * h);
421 return;
422 }
423
424 const std::string ext = outputMap.getExtension();
425 const std::string name = outputMap.getNameWithoutExtension();
426 Path out = Path(outputMap.getParent()).concat(
427 name + "_" + std::to_string(i) + "." + ext); // NOLINT
428
429 std::ofstream outputStream(out, std::ios::binary | std::ios::trunc);
430 if (!outputStream.good()) {
431 std::cerr << "The output file cannot be opened: " << out << std::endl;
432 return;
433 }
434 ImageEncoder::encode(outputStream, g_format, image, g_compression, out.getPath());
435 outputStream.close();
436 if (!outputStream.good()) {
437 std::cerr << "An error occurred while writing the output file: " << out <<
438 std::endl;
439 }
440 });
441 js.run(mip);
442 }
443 js.runAndWait(parent);
444
445 if (g_ktxContainer) {
446 using namespace std;
447 vector<uint8_t> fileContents(bundle.getSerializedLength());
448 bundle.serialize(fileContents.data(), fileContents.size());
449 ofstream outputStream(outputMap.c_str(), ios::out | ios::binary);
450 outputStream.write((const char*) fileContents.data(), fileContents.size());
451 outputStream.close();
452 }
453 }