Parallel skinning and texture decoding, --bench option #2
No files matched your search
+2
-1
@@ -78,7 +78,8 @@ add_library(mr_core STATIC
|
||||
src/model/ModelLoader.cpp
|
||||
src/model/Poser.cpp)
|
||||
target_include_directories(mr_core PUBLIC src ${OPENSCENEGRAPH_INCLUDE_DIRS})
|
||||
target_link_libraries(mr_core PUBLIC ${OPENSCENEGRAPH_LIBRARIES} assimp::assimp)
|
||||
find_package(Threads REQUIRED)
|
||||
target_link_libraries(mr_core PUBLIC ${OPENSCENEGRAPH_LIBRARIES} assimp::assimp Threads::Threads)
|
||||
if(NOT WIN32)
|
||||
target_link_libraries(mr_core PRIVATE Iconv::Iconv)
|
||||
endif()
|
||||
|
||||
+62
-3
@@ -1,8 +1,12 @@
|
||||
#include "app/AppUi.h"
|
||||
|
||||
#include "render/ModelNode.h"
|
||||
|
||||
#include <osg/Group>
|
||||
#include <osgViewer/Viewer>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
@@ -33,7 +37,8 @@ void printUsage() {
|
||||
" --ui-scale S scale the UI (e.g. 2 on high-DPI screens)\n"
|
||||
" --screenshot FILE render, save a PNG and exit\n"
|
||||
" --frames N frames to render before the screenshot (default 5)\n"
|
||||
" --list print bone and morph names after loading\n";
|
||||
" --list print bone and morph names after loading\n"
|
||||
" --bench N time N idle and N animated frames, then exit\n";
|
||||
}
|
||||
|
||||
std::vector<float> parseFloats(const std::string& s) {
|
||||
@@ -44,12 +49,56 @@ std::vector<float> parseFloats(const std::string& s) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// Times idle frames, frames that re-pose the model, and the CPU deformation alone.
|
||||
void runBenchmark(osgViewer::Viewer& viewer, mr::ModelNode* node, int frames) {
|
||||
using Clock = std::chrono::steady_clock;
|
||||
auto ms = [](Clock::duration d) { return std::chrono::duration<double, std::milli>(d).count(); };
|
||||
auto start = Clock::now();
|
||||
viewer.frame();
|
||||
std::printf("first frame: %.0f ms\n", ms(Clock::now() - start));
|
||||
for (int f = 0; f < 10; ++f) viewer.frame();
|
||||
start = Clock::now();
|
||||
for (int f = 0; f < frames; ++f) viewer.frame();
|
||||
const double idle = ms(Clock::now() - start) / frames;
|
||||
std::printf("idle frame: %.2f ms\n", idle);
|
||||
if (!node) return;
|
||||
|
||||
mr::Poser& poser = node->poser();
|
||||
const mr::Model& model = node->model();
|
||||
int morph = -1;
|
||||
for (size_t i = 0; i < model.morphs.size() && morph < 0; ++i)
|
||||
if (model.morphs[i].type == mr::MorphType::Vertex) morph = static_cast<int>(i);
|
||||
std::vector<int> influence(model.bones.size(), 0);
|
||||
for (const auto& v : model.vertices)
|
||||
for (int k = 0; k < 4; ++k)
|
||||
if (v.bones[k] >= 0) ++influence[v.bones[k]];
|
||||
const int bone = static_cast<int>(std::max_element(influence.begin(), influence.end()) - influence.begin());
|
||||
auto animate = [&](int f) {
|
||||
if (morph >= 0) poser.morphWeights()[morph] = (f % 20) / 20.f;
|
||||
if (!model.bones.empty()) poser.bonePoses()[bone].eulerDegrees.set(0.f, 0.f, float(f % 30));
|
||||
};
|
||||
|
||||
start = Clock::now();
|
||||
for (int f = 0; f < frames; ++f) {
|
||||
animate(f + 1);
|
||||
viewer.frame();
|
||||
}
|
||||
const double animated = ms(Clock::now() - start) / frames;
|
||||
start = Clock::now();
|
||||
for (int f = 0; f < frames; ++f) {
|
||||
animate(f + 2);
|
||||
poser.update();
|
||||
}
|
||||
const double deform = ms(Clock::now() - start) / frames;
|
||||
std::printf("animated frame: %.2f ms\ndeform only: %.2f ms\n", animated, deform);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::string modelPath, screenshot;
|
||||
std::vector<std::pair<std::string, std::string>> morphArgs, boneArgs;
|
||||
int width = 1280, height = 800, frames = 5;
|
||||
int width = 1280, height = 800, frames = 5, benchFrames = 0;
|
||||
float azimuth = 0.f, elevation = 0.f, uiScale = 1.f;
|
||||
bool skeleton = false, hideUi = false, listNames = false;
|
||||
|
||||
@@ -90,6 +139,8 @@ int main(int argc, char** argv) {
|
||||
screenshot = next();
|
||||
} else if (a == "--list") {
|
||||
listNames = true;
|
||||
} else if (a == "--bench") {
|
||||
benchFrames = std::max(1, std::atoi(next().c_str()));
|
||||
} else if (a == "--frames") {
|
||||
frames = std::max(1, std::atoi(next().c_str()));
|
||||
} else if (!a.empty() && a[0] != '-') {
|
||||
@@ -113,13 +164,17 @@ int main(int argc, char** argv) {
|
||||
viewer.setCameraManipulator(manipulator);
|
||||
|
||||
mr::App app(viewer, root, manipulator);
|
||||
const auto loadStart = std::chrono::steady_clock::now();
|
||||
if (!modelPath.empty() && !app.loadModel(modelPath)) std::cerr << app.loadError() << "\n";
|
||||
const double loadMs =
|
||||
std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - loadStart).count();
|
||||
|
||||
if (auto* node = app.modelNode()) {
|
||||
const mr::Model& model = node->model();
|
||||
std::cout << "Loaded '" << model.name << "': " << model.vertices.size() << " vertices, "
|
||||
<< model.indices.size() / 3 << " triangles, " << model.materials.size() << " materials, "
|
||||
<< model.bones.size() << " bones, " << model.morphs.size() << " morphs\n";
|
||||
<< model.bones.size() << " bones, " << model.morphs.size() << " morphs in " << int(loadMs)
|
||||
<< " ms\n";
|
||||
for (const auto& w : model.warnings) std::cout << " warning: " << w << "\n";
|
||||
if (listNames) {
|
||||
for (const auto& b : model.bones) std::cout << " bone: " << b.name << "\n";
|
||||
@@ -148,6 +203,10 @@ int main(int argc, char** argv) {
|
||||
if (uiScale != 1.f) app.imgui()->setUiScale(uiScale);
|
||||
|
||||
viewer.realize();
|
||||
if (benchFrames > 0) {
|
||||
runBenchmark(viewer, app.modelNode(), benchFrames);
|
||||
return 0;
|
||||
}
|
||||
if (!screenshot.empty()) {
|
||||
for (int f = 0; f < frames && !viewer.done(); ++f) viewer.frame();
|
||||
app.imgui()->requestScreenshot(screenshot);
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace mr {
|
||||
|
||||
// Runs fn(begin, end) over [0, count) split across hardware threads; small ranges stay on the caller.
|
||||
template <typename Fn>
|
||||
void parallelFor(size_t count, size_t minPerThread, Fn&& fn) {
|
||||
const size_t hw = std::max(1u, std::thread::hardware_concurrency());
|
||||
const size_t threads = std::min(hw, std::max<size_t>(1, count / std::max<size_t>(1, minPerThread)));
|
||||
if (threads <= 1) {
|
||||
fn(size_t(0), count);
|
||||
return;
|
||||
}
|
||||
const size_t chunk = (count + threads - 1) / threads;
|
||||
std::vector<std::thread> workers;
|
||||
workers.reserve(threads - 1);
|
||||
for (size_t t = 1; t < threads; ++t) {
|
||||
const size_t begin = t * chunk, end = std::min(count, begin + chunk);
|
||||
if (begin < end) workers.emplace_back([&fn, begin, end] { fn(begin, end); });
|
||||
}
|
||||
fn(size_t(0), std::min(count, chunk));
|
||||
for (auto& w : workers) w.join();
|
||||
}
|
||||
|
||||
} // namespace mr
|
||||
+26
-18
@@ -1,5 +1,7 @@
|
||||
#include "model/Poser.h"
|
||||
|
||||
#include "model/Parallel.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <numeric>
|
||||
@@ -8,6 +10,8 @@ namespace mr {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr size_t kMinVerticesPerThread = 4096;
|
||||
|
||||
osg::Quat eulerToQuat(const osg::Vec3f& deg) {
|
||||
const double k = osg::PI / 180.0;
|
||||
return osg::Quat(deg.x() * k, osg::X_AXIS, deg.y() * k, osg::Y_AXIS, deg.z() * k, osg::Z_AXIS);
|
||||
@@ -145,7 +149,9 @@ bool Poser::update() {
|
||||
|
||||
const size_t vertexCount = model.vertices.size();
|
||||
morphed_.resize(vertexCount);
|
||||
for (size_t i = 0; i < vertexCount; ++i) morphed_[i] = model.vertices[i].position;
|
||||
parallelFor(vertexCount, kMinVerticesPerThread, [&](size_t begin, size_t end) {
|
||||
for (size_t i = begin; i < end; ++i) morphed_[i] = model.vertices[i].position;
|
||||
});
|
||||
for (size_t i = 0; i < effective_.size(); ++i) {
|
||||
const float w = effective_[i];
|
||||
if (w == 0.f || model.morphs[i].type != MorphType::Vertex) continue;
|
||||
@@ -154,24 +160,26 @@ bool Poser::update() {
|
||||
|
||||
positions_.resize(vertexCount);
|
||||
normals_.resize(vertexCount);
|
||||
for (size_t i = 0; i < vertexCount; ++i) {
|
||||
const Vertex& v = model.vertices[i];
|
||||
if (v.bones[0] < 0) {
|
||||
positions_[i] = morphed_[i];
|
||||
normals_[i] = v.normal;
|
||||
continue;
|
||||
parallelFor(vertexCount, kMinVerticesPerThread, [&](size_t begin, size_t end) {
|
||||
for (size_t i = begin; i < end; ++i) {
|
||||
const Vertex& v = model.vertices[i];
|
||||
if (v.bones[0] < 0) {
|
||||
positions_[i] = morphed_[i];
|
||||
normals_[i] = v.normal;
|
||||
continue;
|
||||
}
|
||||
osg::Vec3f p, n;
|
||||
for (int k = 0; k < 4; ++k) {
|
||||
if (v.bones[k] < 0) continue;
|
||||
const osg::Matrixf& m = skin_[v.bones[k]];
|
||||
p += (morphed_[i] * m) * v.weights[k];
|
||||
n += osg::Matrixf::transform3x3(v.normal, m) * v.weights[k];
|
||||
}
|
||||
n.normalize();
|
||||
positions_[i] = p;
|
||||
normals_[i] = n;
|
||||
}
|
||||
osg::Vec3f p, n;
|
||||
for (int k = 0; k < 4; ++k) {
|
||||
if (v.bones[k] < 0) continue;
|
||||
const osg::Matrixf& m = skin_[v.bones[k]];
|
||||
p += (morphed_[i] * m) * v.weights[k];
|
||||
n += osg::Matrixf::transform3x3(v.normal, m) * v.weights[k];
|
||||
}
|
||||
n.normalize();
|
||||
positions_[i] = p;
|
||||
normals_[i] = n;
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "render/ModelNode.h"
|
||||
|
||||
#include "model/Parallel.h"
|
||||
#include "render/Shaders.h"
|
||||
#include "render/TextureLoader.h"
|
||||
|
||||
@@ -50,6 +51,7 @@ ModelNode::ModelNode(std::shared_ptr<Model> model) : model_(std::move(model)), p
|
||||
|
||||
textures_.resize(model_->textures.size());
|
||||
textureTried_.assign(model_->textures.size(), false);
|
||||
preloadTextureImages();
|
||||
|
||||
buildMesh();
|
||||
buildSkeleton();
|
||||
@@ -58,11 +60,20 @@ ModelNode::ModelNode(std::shared_ptr<Model> model) : model_(std::move(model)), p
|
||||
setUpdateCallback(new DeformCallback);
|
||||
}
|
||||
|
||||
// Decoding large PNGs dominates load time, so decode all textures in parallel up front.
|
||||
void ModelNode::preloadTextureImages() {
|
||||
const auto& textures = model_->textures;
|
||||
preloadedImages_.assign(textures.size(), nullptr);
|
||||
parallelFor(textures.size(), 1, [&](size_t begin, size_t end) {
|
||||
for (size_t i = begin; i < end; ++i) preloadedImages_[i] = loadTextureImage(textures[i]);
|
||||
});
|
||||
}
|
||||
|
||||
osg::Texture2D* ModelNode::texture(int index, bool clamp) {
|
||||
if (index < 0 || index >= static_cast<int>(textures_.size())) return nullptr;
|
||||
if (!textureTried_[index]) {
|
||||
textureTried_[index] = true;
|
||||
if (osg::ref_ptr<osg::Image> image = loadTextureImage(model_->textures[index]))
|
||||
if (osg::ref_ptr<osg::Image> image = std::move(preloadedImages_[index]))
|
||||
textures_[index] = makeTexture(image.get(), clamp);
|
||||
}
|
||||
return textures_[index].get();
|
||||
|
||||
@@ -38,6 +38,7 @@ public:
|
||||
private:
|
||||
void buildMesh();
|
||||
osg::StateSet* buildMaterialState(const Material& material, int order);
|
||||
void preloadTextureImages();
|
||||
osg::Texture2D* texture(int index, bool clamp);
|
||||
void buildSkeleton();
|
||||
void updateSkeleton();
|
||||
@@ -50,6 +51,7 @@ private:
|
||||
std::vector<osg::ref_ptr<osg::Geometry>> geometries_;
|
||||
std::vector<osg::ref_ptr<osg::Texture2D>> textures_;
|
||||
std::vector<bool> textureTried_;
|
||||
std::vector<osg::ref_ptr<osg::Image>> preloadedImages_;
|
||||
osg::ref_ptr<osg::Texture2D> sharedToons_[10];
|
||||
|
||||
osg::ref_ptr<osg::Geode> skeletonGeode_;
|
||||
|
||||
@@ -70,6 +70,7 @@ osg::ref_ptr<osg::Image> loadTextureImage(const Texture& texture) {
|
||||
osg::ref_ptr<osg::Texture2D> makeTexture(osg::Image* image, bool clampToEdge) {
|
||||
osg::ref_ptr<osg::Texture2D> tex = new osg::Texture2D(image);
|
||||
tex->setResizeNonPowerOfTwoHint(false);
|
||||
tex->setUnRefImageDataAfterApply(true); // the GPU keeps its own copy; frees ~170 MB for large models
|
||||
tex->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
|
||||
tex->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
|
||||
const auto wrap = clampToEdge ? osg::Texture::CLAMP_TO_EDGE : osg::Texture::REPEAT;
|
||||
|
||||
Reference in new issue
Block a user