This repository was archived by the owner on Feb 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpy_micromesh.cpp
384 lines (319 loc) · 16.3 KB
/
py_micromesh.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>
#include <Python.h>
#include <nvmath/nvmath.h>
#include <meshops/meshops_operations.h>
#include <meshops/meshops_mesh_view.h>
#include <meshops/meshops_vk.h>
#include <microutils/microutils.hpp>
#include <tool_meshops_objects.hpp>
#include <thread>
#include "py_enums.hpp"
#include "py_types.hpp"
#include "py_operations.hpp"
//
// Required due to some linklib requiring symbols
#define TINYGLTF_IMPLEMENTATION
#define TINYGLTF_NO_EXTERNAL_IMAGE
#define TINYGLTF_NO_INCLUDE_STB_IMAGE // Include nvpro_core's stb_image instead of tinygltf's
#define TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE
#include "third_party/stb/stb_image.h"
#include "third_party/stb/stb_image_write.h"
// Should be removed if that is included in another static lib
#define VMA_IMPLEMENTATION
#include "vk_mem_alloc.h"
#include "nvvk/memallocator_vma_vk.hpp"
namespace py = pybind11;
using namespace meshops;
const char *micromesh_Result_str[] =
{
"Success",
"Failure",
"Continue",
"InvalidFrequency",
"InvalidFormat",
"InvalidBlockFormat",
"InvalidRange",
"InvalidValue",
"InvalidLayout",
"InvalidOperationOrder",
"MismatchingInputEdgeValues",
"MismatchingOutputEdgeValues",
"UnsupportedVersion",
"UnsupportedShaderCodeType"
};
const char * getResultString(const micromesh::Result& result)
{
return micromesh_Result_str[(int)result];
}
class PyMeshopsContext
{
public:
PyMeshopsContext(bool verbose=false, int maxThreadCount=-1) :
m_context(nullptr), m_verbose(verbose), m_maxThreadCount(maxThreadCount)
{
createContext();
}
PyMeshopsContext(const PyMeshopsContext&) = delete;
PyMeshopsContext& operator=(const PyMeshopsContext&) = delete;
~PyMeshopsContext()
{
destroyContext();
}
bool createContext()
{
destroyContext();
ContextConfig meshopsContextConfig{};
meshopsContextConfig.messageCallback = microutils::makeDefaultMessageCallback();
if (m_maxThreadCount <= 0)
{
meshopsContextConfig.threadCount = std::thread::hardware_concurrency();
}
else
{
meshopsContextConfig.threadCount = m_maxThreadCount;
}
if (m_verbose)
{
meshopsContextConfig.verbosityLevel = 999;
}
else
{
meshopsContextConfig.verbosityLevel = 1;
}
meshopsContextConfig.requiresDeviceContext = true;
nvvk::ContextCreateInfo contextInfo;
std::vector<uint8_t> createInfoData;
meshopsGetContextRequirements(meshopsContextConfig, contextInfo, createInfoData);
contextInfo.verboseUsed = m_verbose;
contextInfo.verboseCompatibleDevices = m_verbose;
try
{
py::gil_scoped_release release;
micromesh::Result result = meshopsContextCreate(meshopsContextConfig, &m_context);
if (result != micromesh::Result::eSuccess)
{
throw std::runtime_error("Error creating meshops context (" + std::string(getResultString(result)) + ")");
}
}
catch (const std::exception e)
{
throw std::runtime_error("Error creating meshops context (" + std::string(e.what()) + ")");
}
catch (...)
{
throw std::runtime_error("Error creating meshops context (unknown)");
}
LOGI("Context created\n");
return true;
}
bool destroyContext()
{
if (m_context != nullptr)
{
meshopsContextDestroy(m_context);
LOGI("Context destroyed\n");
m_context = nullptr;
}
return true;
}
meshops::Context context() const { return m_context; }
private:
meshops::Context m_context;
bool m_verbose;
int m_maxThreadCount;
};
std::shared_ptr<PyMeshopsContext> getContext()
{
std::cout << "Creating temporary context; call createContext/destroyContext to create a reusable context object and pass to operator" << std::endl;
return std::make_shared<PyMeshopsContext>();
}
void setVerbosity(PyVerbosity verbosity)
{
nvprintSetConsoleLogging(false, LOGBITS_ALL);
nvprintSetConsoleLogging(true, verbosity);
}
PyMeshopsContext* createContext(PyVerbosity verbosity=PyVerbosity::eWarnings, int maxThreadCount=-1)
{
setVerbosity(verbosity);
return new PyMeshopsContext(verbosity == PyVerbosity::eInfo, maxThreadCount);
}
PyMicromeshData pyBaker(PyMeshopsContext *context, PyBakerInput& input)
{
PyMicromeshData output;
bake((context != nullptr ? context : getContext().get())->context(), input, output);
return output;
}
PyMesh pyDisplaceMesh(PyMeshopsContext *context, PyMesh& input, PyMicromeshData& micromesh)
{
PyMesh output;
displace((context != nullptr ? context : getContext().get())->context(), input, micromesh, output);
return output;
}
PyMesh pyRemesh(PyMeshopsContext *context, PyMesh& input, PyRemesherSettings& settings)
{
PyMesh output;
remesh((context != nullptr ? context : getContext().get())->context(), input, settings, output);
return output;
}
PyMesh pyPreTessellate(PyMeshopsContext *context, PyMesh& input, PyPreTessellatorSettings& settings)
{
PyMesh output;
preTessellate((context != nullptr ? context : getContext().get())->context(), input, settings, output);
return output;
}
bool pyWriteBary(PyMeshopsContext *context, const std::string& filepath, PyMesh& mesh, PyMicromeshData& micromesh, bool forceOverwrite=false)
{
return writeBary((context != nullptr ? context : getContext().get())->context(), filepath, mesh, micromesh, forceOverwrite);
}
PyMicromeshData pyReadBary(PyMeshopsContext *context, const std::string& filepath, PyMesh& mesh)
{
PyMicromeshData output;
readBary((context != nullptr ? context : getContext().get())->context(), filepath, mesh, output);
return output;
}
PYBIND11_MODULE(micromesh_python, m) {
m.doc() = "meshops python bindings";
py::class_<PyMeshopsContext>(m, "MeshopsContext")
.def(py::init<>());
m.def("createContext", &createContext, py::return_value_policy::take_ownership, py::arg("verbosity") = false, py::arg("maxThreadCount") = -1, "Create context");
m.def("setVerbosity", &setVerbosity, py::arg("verbosity") = false, "Set logging verbosity");
registerEnums(m);
py::class_<PyMesh>(m, "Mesh")
.def(py::init<>())
.def_readwrite("triangleVertices", &PyMesh::triangleVertices)
.def_readwrite("vertexPositions", &PyMesh::vertexPositions)
.def_readwrite("vertexNormals", &PyMesh::vertexNormals)
.def_readwrite("vertexTexcoords0", &PyMesh::vertexTexcoords0)
.def_readwrite("vertexTangents", &PyMesh::vertexTangents)
.def_readwrite("vertexDirections", &PyMesh::vertexDirections)
.def_readwrite("vertexDirectionBounds", &PyMesh::vertexDirectionBounds)
.def_readwrite("vertexImportance", &PyMesh::vertexImportance)
.def_readwrite("triangleSubdivisionLevels", &PyMesh::triangleSubdivisionLevels)
.def_readwrite("trianglePrimitiveFlags", &PyMesh::trianglePrimitiveFlags);
//.def_readwrite("triangleMappings", &PyMesh::triangleMappings);
py::class_<PyTexture> (m, "Texture")
.def(py::init<>())
.def_readwrite("type", &PyTexture::type)
.def_readwrite("filepath", &PyTexture::filepath)
.def_readwrite("format", &PyTexture::format)
.def_readwrite("width", &PyTexture::width)
.def_readwrite("height", &PyTexture::height)
.def_readwrite("data", &PyTexture::data);
py::class_<PyHeightMap, PyTexture>(m, "Heightmap")
.def(py::init<>())
.def_readwrite("bias", &PyHeightMap::bias)
.def_readwrite("scale", &PyHeightMap::scale);
py::class_<PyResamplerInput>(m, "ResamplerInput")
.def(py::init<>())
.def_readwrite("input", &PyResamplerInput::input)
.def_readwrite("output", &PyResamplerInput::output);
py::class_<PyBakerInput>(m, "BakerInput")
.def(py::init<>())
.def_readwrite("baseMesh", &PyBakerInput::baseMesh)
.def_readwrite("baseMeshTransform", &PyBakerInput::baseMeshTransform)
.def_readwrite("referenceMesh", &PyBakerInput::referenceMesh)
.def_readwrite("referenceMeshTransform", &PyBakerInput::referenceMeshTransform)
.def_readwrite("resamplerInput", &PyBakerInput::resamplerInput) // Disabled until fully supported
.def_readwrite("heightmap", &PyBakerInput::heightmap)
.def_readwrite("quaternionMapFilepath", &PyBakerInput::quaternionMapFilepath)
.def_readwrite("quaternionMapResolution", &PyBakerInput::quaternionMapResolution)
.def_readwrite("offsetMapFilepath", &PyBakerInput::offsetMapFilepath)
.def_readwrite("offsetMapResolution", &PyBakerInput::offsetMapResolution)
.def_readwrite("settings", &PyBakerInput::settings);
py::class_<PyMicromeshData>(m, "MicromeshData")
.def(py::init<>())
.def_readwrite("vertexDirections", &PyMicromeshData::vertexDirections)
.def_readwrite("vertexDirectionBounds", &PyMicromeshData::vertexDirectionBounds)
.def_readwrite("minSubdivLevel", &PyMicromeshData::minSubdivLevel)
.def_readwrite("maxSubdivLevel", &PyMicromeshData::maxSubdivLevel)
.def_readwrite("bias", &PyMicromeshData::bias)
.def_readwrite("scale", &PyMicromeshData::scale)
.def_readwrite("values", &PyMicromeshData::values)
.def_readwrite("valueFormat", &PyMicromeshData::valueFormat)
.def_readwrite("valueLayout", &PyMicromeshData::valueLayout)
.def_readwrite("valueFrequency", &PyMicromeshData::valueFrequency)
.def_readwrite("valueCount", &PyMicromeshData::valueCount)
.def_readwrite("valueByteSize", &PyMicromeshData::valueByteSize)
.def_readwrite("valueByteAlignment", &PyMicromeshData::valueByteAlignment)
.def_readwrite("triangleValueOffsets", &PyMicromeshData::triangleValueOffsets)
.def_readwrite("triangleSubdivLevels", &PyMicromeshData::triangleSubdivLevels)
.def_readwrite("triangleBlockFormats", &PyMicromeshData::triangleBlockFormats)
.def_readwrite("histogramEntryCounts", &PyMicromeshData::histogramEntryCounts)
.def_readwrite("histogramEntrySubdivLevels", &PyMicromeshData::histogramEntrySubdivLevels)
.def_readwrite("histogramEntryBlockFormats", &PyMicromeshData::histogramEntryBlockFormats)
.def_readwrite("triangleMinMaxs", &PyMicromeshData::triangleMinMaxs)
.def_readwrite("triangleMinMaxFormat", &PyMicromeshData::triangleMinMaxFormat)
.def_readwrite("triangleMinMaxCount", &PyMicromeshData::triangleMinMaxCount)
.def_readwrite("triangleMinMaxByteSize", &PyMicromeshData::triangleMinMaxByteSize)
.def_readwrite("triangleMinMaxByteAlignment", &PyMicromeshData::triangleMinMaxByteAlignment)
.def_readwrite("triangleFlags", &PyMicromeshData::triangleFlags)
.def_readwrite("triangleFlagFormat", &PyMicromeshData::triangleFlagFormat)
.def_readwrite("triangleFlagCount", &PyMicromeshData::triangleFlagCount)
.def_readwrite("triangleFlagByteSize", &PyMicromeshData::triangleFlagByteSize)
.def_readwrite("triangleFlagByteAlignment", &PyMicromeshData::triangleFlagByteAlignment);
py::class_<PyBakerSettings>(m, "BakerSettings")
.def(py::init<>())
.def_readwrite("level", &PyBakerSettings::level)
.def_readwrite("maxTraceLength", &PyBakerSettings::maxTraceLength)
.def_readwrite("uniDirectional", &PyBakerSettings::uniDirectional)
.def_readwrite("fitDirectionBounds", &PyBakerSettings::fitDirectionBounds)
.def_readwrite("memLimitBytes", &PyBakerSettings::memLimitBytes)
.def_readwrite("uncompressedLayout", &PyBakerSettings::uncompressedLayout)
.def_readwrite("uncompressedDisplacementFormat", &PyBakerSettings::uncompressedDisplacementFormat)
.def_readwrite("uncompressedNormalFormat", &PyBakerSettings::uncompressedNormalFormat)
.def_readwrite("subdivMethod", &PyBakerSettings::subdivMethod)
.def_readwrite("adaptiveFactor", &PyBakerSettings::adaptiveFactor)
.def_readwrite("normalReduceOp", &PyBakerSettings::normalReduceOp)
.def_readwrite("tangentAlgorithm", &PyBakerSettings::tangentAlgorithm)
.def_readwrite("enableCompression", &PyBakerSettings::enableCompression)
.def_readwrite("lowTesselationBias", &PyBakerSettings::lowTessBias)
.def_readwrite("highTesselationBias", &PyBakerSettings::highTessBias)
.def_readwrite("minPSNR", &PyBakerSettings::minPSNR)
.def_readwrite("compressedRasterData", &PyBakerSettings::compressedRasterData);
py::class_<PyRemesherSettings>(m, "RemesherSettings")
.def(py::init<>())
.def_readwrite("errorThreshold", &PyRemesherSettings::errorThreshold)
.def_readwrite("maxOutputTriangleCount", &PyRemesherSettings::maxOutputTriangleCount, "Maximum output triangle count (if non-zero then decimationRatio is ignored)")
.def_readwrite("curvaturePower", &PyRemesherSettings::curvaturePower)
.def_readwrite("importanceWeight", &PyRemesherSettings::importanceWeight)
.def_readwrite("curvatureMaxDist", &PyRemesherSettings::curvatureMaxDist)
.def_readwrite("directionBoundsFactor", &PyRemesherSettings::directionBoundsFactor)
.def_readwrite("curvatureMaxDistMode", &PyRemesherSettings::curvatureMaxDistMode)
.def_readwrite("fitToOriginalSurface", &PyRemesherSettings::fitToOriginalSurface)
.def_readwrite("maxSubdivLevel", &PyRemesherSettings::maxSubdivLevel)
.def_readwrite("heightmapWidth", &PyRemesherSettings::heightmapWidth)
.def_readwrite("heightmapHeight", &PyRemesherSettings::heightmapHeight)
.def_readwrite("heightmapTexcoord", &PyRemesherSettings::heightmapTexcoord)
.def_readwrite("importanceMap", &PyRemesherSettings::importanceMap)
.def_readwrite("importanceTexcoord", &PyRemesherSettings::importanceTexcoord)
.def_readwrite("decimationRatio", &PyRemesherSettings::decimationRatio)
.def_readwrite("maxVertexValence", &PyRemesherSettings::maxVertexValence)
.def_readwrite("importanceThreshold", &PyRemesherSettings::importanceThreshold)
.def_readwrite("ignoreTexCoords", &PyRemesherSettings::ignoreTexCoords)
.def_readwrite("ignoreNormals", &PyRemesherSettings::ignoreNormals)
.def_readwrite("ignoreTangents", &PyRemesherSettings::ignoreTangents)
.def_readwrite("ignoreDisplacementDirections", &PyRemesherSettings::ignoreDisplacementDirections)
.def_readwrite("disableMicromeshData", &PyRemesherSettings::disableMicromeshData);
py::class_<PyPreTessellatorSettings>(m, "PreTessellatorSettings")
.def(py::init<>())
.def_readwrite("maxSubdivLevel", &PyPreTessellatorSettings::maxSubdivLevel)
.def_readwrite("heightmapWidth", &PyPreTessellatorSettings::heightmapWidth, "Maximum output triangle count (if non-zero then decimationRatio is ignored)")
.def_readwrite("heightmapHeight", &PyPreTessellatorSettings::heightmapHeight)
.def_readwrite("subdivLevelBias", &PyPreTessellatorSettings::subdivLevelBias)
.def_readwrite("edgeLengthBased", &PyPreTessellatorSettings::edgeLengthBased);
m.def("bakeMicromesh", &pyBaker, py::return_value_policy::move, py::arg("context"), py::arg("bakerInput"),
"Create micromesh data using base mesh with reference mesh and/or heightmap");
m.def("displaceMicromesh", &pyDisplaceMesh, py::return_value_policy::move, py::arg("context"), py::arg("inputMesh"), py::arg("inputMicromesh"),
"Create micromesh data using base mesh with reference mesh and/or heightmap and produce displaced base mesh");
m.def("remesh", &pyRemesh, py::return_value_policy::move, py::arg("context"), py::arg("inputMesh"), py::arg("settings"),
"Remesh the input");
m.def("preTessellate", &pyPreTessellate, py::return_value_policy::move, py::arg("context"), py::arg("inputMesh"), py::arg("settings"),
"Pretessellate the input");
m.def("writeBary", &pyWriteBary, py::arg("context"), py::arg("filepath"), py::arg("mesh"), py::arg("micromesh"), py::arg("forceOverwrite") = false,
"Write the micromesh out to .bary format at the given filepath");
m.def("readBary", &pyReadBary, py::arg("context"), py::arg("filepath"), py::arg("mesh"), "Read the .bary file at the given filepath");
}