Repository navigation
Expand file tree
/
Copy pathscene_simplify_overrides.cpp
More file actions
240 lines (198 loc) · 7.29 KB
/
Copy pathscene_simplify_overrides.cpp
File metadata and controls
240 lines (198 loc) · 7.29 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
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include <fstream>
#include <json.hpp>
#include <nvutils/logger.hpp>
#include <nvutils/file_operations.hpp>
#include "scene.hpp"
namespace lodclusters {
// json key -> SceneConfig member. Keys match the equivalent command line options, so a
// setting is named the same everywhere. Only settings that affect simplification are listed,
// everything else about a geometry must stay global (cluster sizes and the like feed the
// storage layout and the renderer's worst-case buffers).
using SimplifyField = SimplifyOverrides::Field;
// append only, that keeps the `Entry::setMask` bits and therefore existing cache hashes stable
static const SimplifyOverrides::Field s_fields[] = {
{"simplifynormalweight", SimplifyField::eFloat, offsetof(SceneConfig, simplifyNormalWeight)},
{"simplifyuvweight", SimplifyField::eFloat, offsetof(SceneConfig, simplifyTexCoordWeight)},
{"simplifytangentweight", SimplifyField::eFloat, offsetof(SceneConfig, simplifyTangentWeight)},
{"simplifytangentsignweight", SimplifyField::eFloat, offsetof(SceneConfig, simplifyTangentSignWeight)},
{"simplifymaterialweight", SimplifyField::eFloat, offsetof(SceneConfig, simplifyMaterialWeight)},
{"loderrormergeprevious", SimplifyField::eFloat, offsetof(SceneConfig, lodErrorMergePrevious)},
{"loderrormergeadditive", SimplifyField::eFloat, offsetof(SceneConfig, lodErrorMergeAdditive)},
{"loderroredgelimit", SimplifyField::eFloat, offsetof(SceneConfig, lodErrorEdgeLimit)},
{"simplifyerrorclamped", SimplifyField::eBool, offsetof(SceneConfig, simplifyErrorClamped)},
{"simplifypreservefolds", SimplifyField::eBool, offsetof(SceneConfig, simplifyPreserveFolds)},
{"simplifydilateall", SimplifyField::eBool, offsetof(SceneConfig, simplifyDilateBordersAll)},
{"optimizeclusterslevel", SimplifyField::eUint, offsetof(SceneConfig, optimizeClustersLevel)},
{"simplifydilatetwosided", SimplifyField::eBool, offsetof(SceneConfig, simplifyDilateBordersTwoSided)},
};
static size_t fieldSize(SimplifyField::Kind kind)
{
switch(kind)
{
case SimplifyField::eBool:
return sizeof(bool);
case SimplifyField::eUint:
return sizeof(uint32_t);
default:
return sizeof(float);
}
}
static_assert(std::size(s_fields) <= 32, "SimplifyOverrides::Entry::setMask is 32 bits");
std::span<const SimplifyOverrides::Field> SimplifyOverrides::getFields()
{
return std::span<const Field>(s_fields, std::size(s_fields));
}
static const uint64_t FNV_OFFSET_BASIS = 0xcbf29ce484222325ULL;
static const uint64_t FNV_PRIME = 0x100000001b3ULL;
static uint64_t hashBytesFNV(uint64_t hash, const void* data, size_t size)
{
for(size_t i = 0; i < size; i++)
{
hash = (hash ^ uint64_t(((const uint8_t*)data)[i])) * FNV_PRIME;
}
return hash;
}
bool SimplifyOverrides::load(const std::filesystem::path& filePath)
{
entries.clear();
std::string fileName = nvutils::utf8FromPath(filePath);
std::ifstream stream(filePath);
if(!stream.is_open())
{
LOGE("SimplifyOverrides: could not open \"%s\"\n", fileName.c_str());
return false;
}
nlohmann::json root;
try
{
stream >> root;
}
catch(const std::exception& e)
{
LOGE("SimplifyOverrides: \"%s\" is not valid json: %s\n", fileName.c_str(), e.what());
return false;
}
if(!root.is_array())
{
LOGE("SimplifyOverrides: \"%s\" must contain an array of override entries\n", fileName.c_str());
return false;
}
for(size_t i = 0; i < root.size(); i++)
{
const nlohmann::json& item = root[i];
if(!item.is_object())
{
LOGE("SimplifyOverrides: \"%s\" entry %zu is not an object\n", fileName.c_str(), i);
return false;
}
auto itMesh = item.find("mesh");
if(itMesh == item.end() || !itMesh->is_string())
{
LOGE("SimplifyOverrides: \"%s\" entry %zu needs a \"mesh\" string with the name regular expression\n", fileName.c_str(), i);
return false;
}
Entry entry;
entry.pattern = itMesh->get<std::string>();
try
{
entry.regex = std::regex(entry.pattern);
}
catch(const std::regex_error& e)
{
LOGE("SimplifyOverrides: \"%s\" entry %zu has an invalid \"mesh\" regular expression \"%s\": %s\n",
fileName.c_str(), i, entry.pattern.c_str(), e.what());
return false;
}
for(auto it = item.begin(); it != item.end(); ++it)
{
if(it.key() == "mesh")
continue;
uint32_t fieldIndex = ~0u;
for(uint32_t f = 0; f < uint32_t(std::size(s_fields)); f++)
{
if(it.key() == s_fields[f].key)
{
fieldIndex = f;
break;
}
}
if(fieldIndex == ~0u)
{
LOGE("SimplifyOverrides: \"%s\" entry %zu has unknown setting \"%s\"\n", fileName.c_str(), i, it.key().c_str());
return false;
}
const Field& field = s_fields[fieldIndex];
uint8_t* dst = (uint8_t*)&entry.values + field.offset;
if(field.kind == SimplifyField::eFloat)
{
if(!it->is_number())
{
LOGE("SimplifyOverrides: \"%s\" entry %zu setting \"%s\" must be a number\n", fileName.c_str(), i, field.key);
return false;
}
*(float*)dst = it->get<float>();
}
else if(field.kind == SimplifyField::eUint)
{
if(!it->is_number_unsigned())
{
LOGE("SimplifyOverrides: \"%s\" entry %zu setting \"%s\" must be a non-negative integer\n", fileName.c_str(),
i, field.key);
return false;
}
*(uint32_t*)dst = it->get<uint32_t>();
}
else
{
if(!it->is_boolean())
{
LOGE("SimplifyOverrides: \"%s\" entry %zu setting \"%s\" must be true or false\n", fileName.c_str(), i, field.key);
return false;
}
*(bool*)dst = it->get<bool>();
}
entry.setMask |= 1u << fieldIndex;
}
if(!entry.setMask)
{
LOGW("SimplifyOverrides: \"%s\" entry %zu (\"%s\") sets nothing\n", fileName.c_str(), i, entry.pattern.c_str());
}
entries.push_back(std::move(entry));
}
LOGI("SimplifyOverrides: %zu entries from\n %s\n", entries.size(), fileName.c_str());
return true;
}
uint64_t SimplifyOverrides::match(const std::string& meshName, SceneConfig* config) const
{
uint64_t hash = FNV_OFFSET_BASIS;
bool matched = false;
for(const Entry& entry : entries)
{
if(!std::regex_match(meshName, entry.regex))
continue;
matched = true;
// the hash must describe what was applied, not the resulting config, so that it is
// independent of the global settings the overrides are layered on top of
hash = hashBytesFNV(hash, entry.pattern.data(), entry.pattern.size());
hash = hashBytesFNV(hash, &entry.setMask, sizeof(entry.setMask));
for(uint32_t f = 0; f < uint32_t(std::size(s_fields)); f++)
{
if(!(entry.setMask & (1u << f)))
continue;
const Field& field = s_fields[f];
const size_t size = fieldSize(field.kind);
const void* src = (const uint8_t*)&entry.values + field.offset;
hash = hashBytesFNV(hash, src, size);
if(config)
{
memcpy((uint8_t*)config + field.offset, src, size);
}
}
}
return matched ? hash : 0;
}
} // namespace lodclusters