Add XMMatrixInverseTranspose for optimal normal vector transformation - #336
Open
RohithPariki wants to merge 2 commits into
Open
Add XMMatrixInverseTranspose for optimal normal vector transformation#336RohithPariki wants to merge 2 commits into
RohithPariki wants to merge 2 commits into
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Collaborator
|
Do you have some test code for this? I can integrate it into https://github.com/walbourn/directxmathtest |
|
|
||
| //------------------------------------------------------------------------------ | ||
|
|
||
| inline XMMATRIX XM_CALLCONV XMMatrixInverseTranspose |
RohithPariki
force-pushed
the
feature/matrix-inverse-transpose
branch
from
August 4, 2026 21:07
3f9455d to
cff3cc1
Compare
Author
|
Hi @walbourn, Yes, absolutely! Here is the C++ test code I used to verify the new implementation against the chained Feel free to integrate this into #include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdint>
#include <DirectXMath.h>
using namespace DirectX;
// Helper to compare matrices with epsilon tolerance
bool MatrixApproxEqual(FXMMATRIX A, CXMMATRIX B, float eps = 1e-4f) {
for (int r = 0; r < 4; r++) {
XMFLOAT4 a, b;
XMStoreFloat4(&a, A.r[r]);
XMStoreFloat4(&b, B.r[r]);
if (fabsf(a.x - b.x) > eps || fabsf(a.y - b.y) > eps ||
fabsf(a.z - b.z) > eps || fabsf(a.w - b.w) > eps)
return false;
}
return true;
}
// Helper for beautiful printing
void PrintMatrix(const char* name, FXMMATRIX M) {
std::cout << " " << name << ":\n";
for (int r = 0; r < 4; r++) {
XMFLOAT4 row;
XMStoreFloat4(&row, M.r[r]);
std::cout << " [ "
<< std::setw(8) << std::fixed << std::setprecision(4) << row.x << ", "
<< std::setw(8) << std::fixed << std::setprecision(4) << row.y << ", "
<< std::setw(8) << std::fixed << std::setprecision(4) << row.z << ", "
<< std::setw(8) << std::fixed << std::setprecision(4) << row.w << " ]\n";
}
}
int main() {
std::cout << "========================================================\n";
std::cout << " DirectXMath Unit Test: XMMatrixInverseTranspose\n";
std::cout << "========================================================\n\n";
bool allPassed = true;
// Test 1: Identity Matrix
{
std::cout << "[Test 1] Identity Matrix (InvT(I) == I)\n";
XMMATRIX I = XMMatrixIdentity();
XMMATRIX result = XMMatrixInverseTranspose(nullptr, I);
bool ok = MatrixApproxEqual(result, I);
std::cout << " -> Status: " << (ok ? "PASS" : "FAIL") << "\n\n";
allPassed &= ok;
}
// Test 2: Rotation Matrix
{
std::cout << "[Test 2] Rotation Matrix\n";
std::cout << " (For pure rotation R, R^-1 = R^T, so (R^-1)^T = R)\n";
// Rotate 45 degrees on Y axis
XMMATRIX R = XMMatrixRotationY(XM_PIDIV4);
XMMATRIX result = XMMatrixInverseTranspose(nullptr, R);
bool ok = MatrixApproxEqual(result, R);
if (!ok) {
PrintMatrix("Expected (R)", R);
PrintMatrix("Actual", result);
}
std::cout << " -> Status: " << (ok ? "PASS" : "FAIL") << "\n\n";
allPassed &= ok;
}
// Test 3: Scale Matrix
{
std::cout << "[Test 3] Scale Matrix\n";
std::cout << " (Scale(2, 3, 4)^-1^T == Scale(0.5, 0.33, 0.25))\n";
XMMATRIX S = XMMatrixScaling(2.0f, 3.0f, 4.0f);
XMMATRIX expected = XMMatrixScaling(0.5f, 1.0f/3.0f, 0.25f);
XMMATRIX result = XMMatrixInverseTranspose(nullptr, S);
bool ok = MatrixApproxEqual(result, expected);
if (!ok) {
PrintMatrix("Expected", expected);
PrintMatrix("Actual", result);
}
std::cout << " -> Status: " << (ok ? "PASS" : "FAIL") << "\n\n";
allPassed &= ok;
}
// Test 4: Arbitrary Matrix Consistency
{
std::cout << "[Test 4] Arbitrary Matrix Consistency\n";
std::cout << " (Ensures single-pass result exactly matches chained calls)\n";
// Setup an arbitrary non-degenerate matrix
XMMATRIX M = XMMatrixSet(
2.0f, 3.0f, 1.0f, 0.0f,
0.0f, 4.0f, 2.0f, 0.0f,
1.0f, -1.0f, 3.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
XMVECTOR detDirect, detChained;
// New optimized method
XMMATRIX directResult = XMMatrixInverseTranspose(&detDirect, M);
// Old chained method (what users used to do)
XMMATRIX chainedResult = XMMatrixTranspose(XMMatrixInverse(&detChained, M));
bool matOk = MatrixApproxEqual(directResult, chainedResult);
XMFLOAT4 d1f, d2f;
XMStoreFloat4(&d1f, detDirect);
XMStoreFloat4(&d2f, detChained);
bool detOk = fabsf(d1f.x - d2f.x) < 1e-4f;
if (!matOk) {
PrintMatrix("Direct Result", directResult);
PrintMatrix("Chained Result", chainedResult);
}
std::cout << " -> Matrix Match Status: " << (matOk ? "PASS" : "FAIL") << "\n";
std::cout << " -> Determinant Match Status: " << (detOk ? "PASS" : "FAIL")
<< " (Direct=" << d1f.x << ", Chained=" << d2f.x << ")\n\n";
allPassed &= (matOk && detOk);
}
std::cout << "========================================================\n";
if (allPassed) {
std::cout << " ALL VERIFICATION TESTS PASSED SUCCESSFULLY! \n";
std::cout << "========================================================\n";
return 0;
} else {
std::cout << " SOME VERIFICATION TESTS FAILED! \n";
std::cout << "========================================================\n";
return 1;
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9
Description
This PR introduces
XMMatrixInverseTranspose, computing(M^-1)^Tin a single pass. This is a common operation used for transforming surface normals, where developers previously had to chainXMMatrixTranspose(XMMatrixInverse(nullptr, M)).Optimization Insight:
The standard
XMMatrixInverseinternally transposes the matrix first (MT = M^T) and then computes the cofactors ofMTto produceadj(M^T) = adj(M)^T. Since the caller ultimately requests the transpose of the inverse, that final transpose cancels out the internal one.XMMatrixInverseTransposesimply runs the cofactor algorithm directly onM(skipping the initial transpose block entirely) to produceadj(M) / det(M).Savings:
_mm_shuffle_psinstructions on the_XM_SSE_INTRINSICS_path.Verification
All paths (Scalar, NEON, and SSE) were successfully implemented and verified against chained calls (
Transpose(Inverse(M))) for:(invT(R) == R)i can share the test code , if needed.