forked from NVIDIA-RTX/RTXNTC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
259 lines (229 loc) · 8.59 KB
/
Utils.cpp
File metadata and controls
259 lines (229 loc) · 8.59 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: LicenseRef-NvidiaProprietary
*
* NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
* property and proprietary rights in and to this material, related
* documentation and any modifications thereto. Any use, reproduction,
* disclosure or distribution of this material and related documentation
* without an express license agreement from NVIDIA CORPORATION or
* its affiliates is strictly prohibited.
*/
#include "Utils.h"
#include <lodepng.h>
#include <taskflow/taskflow.hpp>
#include <algorithm>
#include <stb_image_write.h>
#include <tinyexr.h>
#include <ntc-utils/Manifest.h>
static tf::Executor g_Executor;
static const BcFormatDefinition c_BlockCompressedFormats[] = {
{ ntc::BlockCompressedFormat::BC1, DXGI_FORMAT_BC1_UNORM, DXGI_FORMAT_BC1_UNORM_SRGB, nvrhi::Format::BC1_UNORM, 8, 4 },
{ ntc::BlockCompressedFormat::BC2, DXGI_FORMAT_BC2_UNORM, DXGI_FORMAT_BC2_UNORM_SRGB, nvrhi::Format::BC2_UNORM, 16, 4 },
{ ntc::BlockCompressedFormat::BC3, DXGI_FORMAT_BC3_UNORM, DXGI_FORMAT_BC3_UNORM_SRGB, nvrhi::Format::BC3_UNORM, 16, 4 },
{ ntc::BlockCompressedFormat::BC4, DXGI_FORMAT_BC4_UNORM, DXGI_FORMAT_BC4_UNORM, nvrhi::Format::BC4_UNORM, 8, 1 },
{ ntc::BlockCompressedFormat::BC5, DXGI_FORMAT_BC5_UNORM, DXGI_FORMAT_BC5_UNORM, nvrhi::Format::BC5_UNORM, 16, 2 },
{ ntc::BlockCompressedFormat::BC6, DXGI_FORMAT_BC6H_UF16, DXGI_FORMAT_BC6H_UF16, nvrhi::Format::BC6H_UFLOAT, 16, 3 },
{ ntc::BlockCompressedFormat::BC7, DXGI_FORMAT_BC7_UNORM, DXGI_FORMAT_BC7_UNORM_SRGB, nvrhi::Format::BC7_UNORM, 16, 4 },
};
BcFormatDefinition const* GetBcFormatDefinition(ntc::BlockCompressedFormat format)
{
for (const auto& formatCandidate : c_BlockCompressedFormats)
{
if (formatCandidate.ntcFormat == format)
{
return &formatCandidate;
}
}
assert(false);
return nullptr;
}
float Median(std::vector<float>& items)
{
size_t middleIndex = items.size() / 2;
std::nth_element(items.begin(), items.begin() + middleIndex, items.end());
return items[middleIndex];
}
bool WriteDdsHeader(ntc::IStream* ddsFile, int width, int height, int mipLevels, BcFormatDefinition const* outputFormatDefinition, ntc::ColorSpace colorSpace)
{
using namespace donut::engine::dds;
DDS_HEADER ddsHeader{};
DDS_HEADER_DXT10 dx10header = {};
ddsHeader.size = sizeof(DDS_HEADER);
ddsHeader.flags = DDS_HEADER_FLAGS_TEXTURE;
ddsHeader.width = width;
ddsHeader.height = height;
ddsHeader.depth = 1;
ddsHeader.mipMapCount = mipLevels;
ddsHeader.ddspf.size = sizeof(DDS_PIXELFORMAT);
ddsHeader.ddspf.flags = DDS_FOURCC;
ddsHeader.ddspf.fourCC = MAKEFOURCC('D', 'X', '1', '0');
dx10header.resourceDimension = DDS_DIMENSION_TEXTURE2D;
dx10header.arraySize = 1;
dx10header.dxgiFormat = colorSpace == ntc::ColorSpace::sRGB ? outputFormatDefinition->dxgiFormatSrgb : outputFormatDefinition->dxgiFormat;
uint32_t ddsMagic = DDS_MAGIC;
bool success;
success = ddsFile->Write(&ddsMagic, sizeof(ddsMagic));
success &= ddsFile->Write(&ddsHeader, sizeof(ddsHeader));
success &= ddsFile->Write(&dx10header, sizeof(dx10header));
return success;
}
bool SavePNG(uint8_t* data, int mipWidth, int mipHeight, int numChannels, bool is16Bit, char const* fileName)
{
// Use LodePNG to save PNG's instead of STB.
// It can write 16-bit-per-channel images and extended metadata.
// LodePNG expects 16-bit data in big endian format, so byte-swap it.
if (is16Bit)
{
for (int offset = 0; offset < mipWidth * mipHeight * numChannels * 2; offset += 2)
{
std::swap(data[offset], data[offset + 1]);
}
}
// Prepare input parameters
LodePNGColorType const colorType =
numChannels == 4 ? LCT_RGBA :
numChannels == 3 ? LCT_RGB :
numChannels == 2 ? LCT_GREY_ALPHA :
LCT_GREY;
unsigned const bitDepth = is16Bit ? 16 : 8;
// Fill out LodePNGState
// Note: extra info like color profile can also go here.
LodePNGState state;
lodepng_state_init(&state);
state.info_raw.colortype = colorType;
state.info_raw.bitdepth = bitDepth;
state.info_png.color.colortype = colorType;
state.info_png.color.bitdepth = bitDepth;
state.encoder.zlibsettings.windowsize = 512; // slightly worse compression but much faster, default = 2048
// Encode the PNG
unsigned char* pngData = nullptr;
size_t pngSize = 0;
lodepng_encode(&pngData, &pngSize, data, mipWidth, mipHeight, &state);
bool success = state.error == 0;
lodepng_state_cleanup(&state);
if (success)
{
// Save the PNG data into the output file
FILE* outputFile = fopen(fileName, "wb");
if (outputFile)
{
if (fwrite(pngData, pngSize, 1, outputFile) != 1)
success = false;
fclose(outputFile);
}
else
success = false;
}
if (pngData)
free(pngData);
return success;
}
void StartAsyncTask(std::function<void()> function)
{
g_Executor.async(function);
}
void WaitForAllTasks()
{
g_Executor.wait_for_all();
}
std::optional<ImageContainer> ParseImageContainer(char const* container)
{
if (!container || !container[0])
return ImageContainer::Auto;
std::string uppercaseContainer = container;
UppercaseString(uppercaseContainer);
if (uppercaseContainer == "AUTO")
return ImageContainer::Auto;
if (uppercaseContainer == "BMP")
return ImageContainer::BMP;
if (uppercaseContainer == "EXR")
return ImageContainer::EXR;
if (uppercaseContainer == "JPG" || uppercaseContainer == "JPEG" )
return ImageContainer::JPG;
if (uppercaseContainer == "PNG")
return ImageContainer::PNG;
if (uppercaseContainer == "PNG16")
return ImageContainer::PNG16;
if (uppercaseContainer == "TGA")
return ImageContainer::TGA;
return std::optional<ImageContainer>();
}
ntc::ChannelFormat GetContainerChannelFormat(ImageContainer container)
{
switch(container)
{
case ImageContainer::Auto:
default:
return ntc::ChannelFormat::UNKNOWN;
case ImageContainer::BMP:
case ImageContainer::JPG:
case ImageContainer::PNG:
case ImageContainer::TGA:
return ntc::ChannelFormat::UNORM8;
case ImageContainer::EXR:
return ntc::ChannelFormat::FLOAT32;
case ImageContainer::PNG16:
return ntc::ChannelFormat::UNORM16;
}
}
char const* GetContainerExtension(ImageContainer container)
{
switch(container)
{
case ImageContainer::Auto:
default:
return nullptr; // Invalid call
case ImageContainer::BMP:
return ".bmp";
case ImageContainer::JPG:
return ".jpg";
case ImageContainer::PNG:
case ImageContainer::PNG16:
return ".png";
case ImageContainer::TGA:
return ".tga";
case ImageContainer::EXR:
return ".exr";
}
}
bool SaveImageToContainer(ImageContainer container, void const* data, int width, int height, int channels, char const* fileName)
{
switch(container)
{
case ImageContainer::Auto:
default:
return false; // Invalid call
case ImageContainer::BMP:
return !!stbi_write_bmp(fileName, width, height, channels, data);
case ImageContainer::JPG:
return !!stbi_write_jpg(fileName, width, height, channels, data, /* quality = */ 95);
case ImageContainer::PNG:
return SavePNG((uint8_t*)data, width, height, channels, false, fileName);
case ImageContainer::PNG16:
return SavePNG((uint8_t*)data, width, height, channels, true, fileName);
case ImageContainer::TGA:
return !!stbi_write_tga(fileName, width, height, channels, data);
case ImageContainer::EXR:
return SaveEXR((float const*)data, width, height, channels, /* save_as_fp16 = */ true,
fileName, /* err = */ nullptr) == TINYEXR_SUCCESS;
}
}
std::optional<int> ParseNetworkVersion(char const* version)
{
if (!version || !version[0])
return NTC_NETWORK_UNKNOWN;
std::string uppercaseVersion = version;
UppercaseString(uppercaseVersion);
if (uppercaseVersion == "AUTO")
return NTC_NETWORK_UNKNOWN;
if (uppercaseVersion == "SMALL")
return NTC_NETWORK_SMALL;
if (uppercaseVersion == "MEDIUM")
return NTC_NETWORK_MEDIUM;
if (uppercaseVersion == "LARGE")
return NTC_NETWORK_LARGE;
if (uppercaseVersion == "XLARGE")
return NTC_NETWORK_XLARGE;
return std::optional<int>();
}