From 0ef7d158fedcf4984a69c7ddddcad6e64a0b9d28 Mon Sep 17 00:00:00 2001 From: Vsevolod Kremianskii Date: Sun, 14 Feb 2021 14:18:37 +0700 Subject: [PATCH] perf: Compute bone matrices on CPU, not in vertex shader --- src/render/shaders.cpp | 19 ++++++++++--------- src/scene/node/modelnodescenenode.cpp | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/render/shaders.cpp b/src/render/shaders.cpp index c3c87d3b..205fb6d0 100644 --- a/src/render/shaders.cpp +++ b/src/render/shaders.cpp @@ -384,7 +384,7 @@ out vec2 fragLightmapCoords; out mat3 fragTanSpace; void main() { - vec3 newPosition = vec3(0.0); + vec4 P = vec4(0.0); if (isFeatureEnabled(FEATURE_SKELETAL)) { float weight0 = aBoneWeights.x; @@ -397,19 +397,20 @@ void main() { int index2 = int(aBoneIndices.z); int index3 = int(aBoneIndices.w); - vec4 position4 = vec4(aPosition, 1.0); + vec4 position = vec4(aPosition, 1.0); - newPosition += weight0 * (uAbsTransformInv * uBones[index0] * uAbsTransform * position4).xyz; - newPosition += weight1 * (uAbsTransformInv * uBones[index1] * uAbsTransform * position4).xyz; - newPosition += weight2 * (uAbsTransformInv * uBones[index2] * uAbsTransform * position4).xyz; - newPosition += weight3 * (uAbsTransformInv * uBones[index3] * uAbsTransform * position4).xyz; + P += weight0 * uBones[index0] * position; + P += weight1 * uBones[index1] * position; + P += weight2 * uBones[index2] * position; + P += weight3 * uBones[index3] * position; + + P = vec4(P.xyz, 1.0); } else { - newPosition = aPosition; + P = vec4(aPosition, 1.0); } - vec4 newPosition4 = vec4(newPosition, 1.0); - fragPosition = vec3(uModel * newPosition4); + fragPosition = vec3(uModel * P); fragNormal = mat3(transpose(inverse(uModel))) * aNormal; fragTexCoords = aTexCoords; fragLightmapCoords = aLightmapCoords; diff --git a/src/scene/node/modelnodescenenode.cpp b/src/scene/node/modelnodescenenode.cpp index 58701167..b9d51fd4 100644 --- a/src/scene/node/modelnodescenenode.cpp +++ b/src/scene/node/modelnodescenenode.cpp @@ -178,7 +178,7 @@ void ModelNodeSceneNode::renderSingle(bool shadowPass) const { ModelNodeSceneNode *bone = _modelSceneNode->getModelNodeByIndex(nodeIdx); if (bone) { - locals.skeletal->bones[boneIdx] = bone->boneTransform(); + locals.skeletal->bones[boneIdx] = _modelNode->absoluteTransformInverse() * bone->boneTransform() * _modelNode->absoluteTransform(); } } }