Add GPU (hardware) skinning path with CPU fallback
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
091c036d7b
commit
c3cb854875
12 files changed
+317
-18
No files matched your search
+134
-6
@@ -1,6 +1,7 @@
|
||||
#include "render/ModelNode.h"
|
||||
|
||||
#include "model/Parallel.h"
|
||||
#include "render/GpuCaps.h"
|
||||
#include "render/Shaders.h"
|
||||
#include "render/TextureLoader.h"
|
||||
|
||||
@@ -27,6 +28,20 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
// In hardware mode the vertex arrays hold rest positions, so bounds come from the posed skeleton.
|
||||
class PosedBoundsCallback : public osg::Drawable::ComputeBoundingBoxCallback {
|
||||
public:
|
||||
explicit PosedBoundsCallback(const ModelNode* node) : node_(node) {}
|
||||
osg::BoundingBox computeBound(const osg::Drawable& drawable) const override {
|
||||
if (node_->activeSkinningMode() == SkinningMode::Hardware && node_->posedBounds().valid())
|
||||
return node_->posedBounds();
|
||||
return drawable.computeBoundingBox();
|
||||
}
|
||||
|
||||
private:
|
||||
const ModelNode* node_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
ModelNode::ModelNode(std::shared_ptr<Model> model) : model_(std::move(model)), poser_(model_) {
|
||||
@@ -54,6 +69,7 @@ ModelNode::ModelNode(std::shared_ptr<Model> model) : model_(std::move(model)), p
|
||||
preloadTextureImages();
|
||||
|
||||
buildMesh();
|
||||
buildSkinning();
|
||||
buildSkeleton();
|
||||
setShowSkeleton(false);
|
||||
updateDeformation();
|
||||
@@ -142,6 +158,7 @@ void ModelNode::buildMesh() {
|
||||
GL_TRIANGLES, model.indices.begin() + m.indexOffset, model.indices.begin() + m.indexOffset + m.indexCount);
|
||||
geom->addPrimitiveSet(elements);
|
||||
geom->setStateSet(buildMaterialState(m, static_cast<int>(i)));
|
||||
geom->setComputeBoundingBoxCallback(new PosedBoundsCallback(this));
|
||||
geode->addDrawable(geom);
|
||||
geometries_.push_back(geom);
|
||||
}
|
||||
@@ -149,6 +166,96 @@ void ModelNode::buildMesh() {
|
||||
addChild(meshGroup_);
|
||||
}
|
||||
|
||||
void ModelNode::buildSkinning() {
|
||||
const Model& model = *model_;
|
||||
const size_t boneCount = model.bones.size();
|
||||
osg::StateSet* ss = getOrCreateStateSet();
|
||||
skinningUniform_ = new osg::Uniform("u_skinning", 0);
|
||||
ss->addUniform(skinningUniform_);
|
||||
|
||||
osg::ref_ptr<osg::Vec4Array> indices = new osg::Vec4Array(model.vertices.size());
|
||||
osg::ref_ptr<osg::Vec4Array> weights = new osg::Vec4Array(model.vertices.size());
|
||||
for (size_t i = 0; i < model.vertices.size(); ++i) {
|
||||
const Vertex& v = model.vertices[i];
|
||||
for (int k = 0; k < 4; ++k) {
|
||||
const bool used = v.bones[k] >= 0 && v.weights[k] > 0.f;
|
||||
(*indices)[i][k] = used ? float(v.bones[k]) : 0.f;
|
||||
(*weights)[i][k] = used ? v.weights[k] : 0.f;
|
||||
}
|
||||
}
|
||||
for (auto& g : geometries_) {
|
||||
g->setVertexAttribArray(kBoneIndexAttrib, indices, osg::Array::BIND_PER_VERTEX);
|
||||
g->setVertexAttribArray(kBoneWeightAttrib, weights, osg::Array::BIND_PER_VERTEX);
|
||||
}
|
||||
|
||||
boneRestPositions_.resize(boneCount);
|
||||
for (size_t b = 0; b < boneCount; ++b)
|
||||
boneRestPositions_[b] = osg::Matrixf::inverse(model.bones[b].inverseBind).getTrans();
|
||||
float morphReach = 0.f;
|
||||
for (const auto& m : model.morphs)
|
||||
for (const auto& o : m.vertexOffsets) morphReach = std::max(morphReach, o.offset.length());
|
||||
for (const auto& v : model.vertices) {
|
||||
if (v.bones[0] < 0) {
|
||||
unskinnedBounds_.expandBy(v.position);
|
||||
continue;
|
||||
}
|
||||
int main = 0;
|
||||
for (int k = 1; k < 4; ++k)
|
||||
if (v.bones[k] >= 0 && v.weights[k] > v.weights[main]) main = k;
|
||||
boneReach_ = std::max(boneReach_, (v.position - boneRestPositions_[v.bones[main]]).length());
|
||||
}
|
||||
boneReach_ += morphReach;
|
||||
if (unskinnedBounds_.valid()) {
|
||||
const osg::Vec3f pad(morphReach, morphReach, morphReach);
|
||||
unskinnedBounds_.expandBy(unskinnedBounds_._max + pad);
|
||||
unskinnedBounds_.expandBy(unskinnedBounds_._min - pad);
|
||||
}
|
||||
if (boneCount == 0) return;
|
||||
|
||||
const int columns = static_cast<int>(std::min<size_t>(boneCount, kBonesPerTextureRow));
|
||||
const int rows = static_cast<int>((boneCount + kBonesPerTextureRow - 1) / kBonesPerTextureRow);
|
||||
boneImage_ = new osg::Image;
|
||||
boneImage_->allocateImage(columns * 3, rows, 1, GL_RGBA, GL_FLOAT);
|
||||
boneImage_->setInternalTextureFormat(GL_RGBA32F_ARB);
|
||||
std::fill(reinterpret_cast<float*>(boneImage_->data()),
|
||||
reinterpret_cast<float*>(boneImage_->data()) + columns * 3 * rows * 4, 0.f);
|
||||
boneImage_->setDataVariance(osg::Object::DYNAMIC);
|
||||
boneTexture_ = new osg::Texture2D(boneImage_);
|
||||
boneTexture_->setDataVariance(osg::Object::DYNAMIC);
|
||||
boneTexture_->setResizeNonPowerOfTwoHint(false);
|
||||
boneTexture_->setUseHardwareMipMapGeneration(false);
|
||||
boneTexture_->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
|
||||
boneTexture_->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
|
||||
boneTexture_->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
|
||||
boneTexture_->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
|
||||
ss->setTextureAttribute(kBoneTextureUnit, boneTexture_);
|
||||
ss->addUniform(new osg::Uniform("u_boneTexture", static_cast<int>(kBoneTextureUnit)));
|
||||
ss->addUniform(new osg::Uniform("u_boneTexSize", osg::Vec2f(float(columns * 3), float(rows))));
|
||||
}
|
||||
|
||||
void ModelNode::uploadBoneMatrices() {
|
||||
const auto& skin = poser_.skinMatrices();
|
||||
const auto& bones = model_->bones;
|
||||
float* data = reinterpret_cast<float*>(boneImage_->data());
|
||||
const int width = boneImage_->s();
|
||||
posedBounds_ = unskinnedBounds_;
|
||||
const osg::Vec3f reach(boneReach_, boneReach_, boneReach_);
|
||||
for (size_t b = 0; b < skin.size(); ++b) {
|
||||
const osg::Matrixf& m = skin[b];
|
||||
float* texel = data + ((b / kBonesPerTextureRow) * width + (b % kBonesPerTextureRow) * 3) * 4;
|
||||
for (int k = 0; k < 3; ++k)
|
||||
for (int r = 0; r < 4; ++r) texel[k * 4 + r] = m(r, k);
|
||||
if (bones[b].isSkinBone) {
|
||||
const osg::Vec3f p = boneRestPositions_[b] * m;
|
||||
posedBounds_.expandBy(p - reach);
|
||||
posedBounds_.expandBy(p + reach);
|
||||
}
|
||||
}
|
||||
boneImage_->dirty();
|
||||
}
|
||||
|
||||
void ModelNode::setSkinningMode(SkinningMode mode) { requestedSkinning_ = mode; }
|
||||
|
||||
void ModelNode::buildSkeleton() {
|
||||
const Model& model = *model_;
|
||||
for (size_t i = 0; i < model.bones.size(); ++i)
|
||||
@@ -219,13 +326,34 @@ void ModelNode::updateSkeleton() {
|
||||
}
|
||||
|
||||
void ModelNode::updateDeformation() {
|
||||
const bool hardware =
|
||||
requestedSkinning_ == SkinningMode::Hardware && gpuCaps().skinning && boneImage_.valid();
|
||||
const SkinningMode wanted = hardware ? SkinningMode::Hardware : SkinningMode::Software;
|
||||
if (wanted != activeSkinning_) {
|
||||
activeSkinning_ = wanted;
|
||||
poser_.setSkinningMode(wanted);
|
||||
skinningUniform_->set(hardware ? 1 : 0);
|
||||
if (hardware) {
|
||||
for (size_t i = 0; i < model_->vertices.size(); ++i) (*normals_)[i] = model_->vertices[i].normal;
|
||||
normals_->dirty();
|
||||
}
|
||||
}
|
||||
if (poser_.update()) {
|
||||
const auto& p = poser_.positions();
|
||||
const auto& n = poser_.normals();
|
||||
std::copy(p.begin(), p.end(), positions_->begin());
|
||||
std::copy(n.begin(), n.end(), normals_->begin());
|
||||
positions_->dirty();
|
||||
normals_->dirty();
|
||||
if (activeSkinning_ == SkinningMode::Software) {
|
||||
const auto& p = poser_.positions();
|
||||
const auto& n = poser_.normals();
|
||||
std::copy(p.begin(), p.end(), positions_->begin());
|
||||
std::copy(n.begin(), n.end(), normals_->begin());
|
||||
positions_->dirty();
|
||||
normals_->dirty();
|
||||
} else {
|
||||
uploadBoneMatrices();
|
||||
if (poser_.morphsChangedInLastUpdate()) {
|
||||
const auto& p = poser_.morphedPositions();
|
||||
std::copy(p.begin(), p.end(), positions_->begin());
|
||||
positions_->dirty();
|
||||
}
|
||||
}
|
||||
for (auto& g : geometries_) g->dirtyBound();
|
||||
skeletonDirty_ = true;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user