Skip to content

Conversation

@bobtista
Copy link

@bobtista bobtista commented Nov 13, 2025

Summary

Replaces all C-style casts between Matrix4x4/Matrix3D and D3DXMATRIX/D3DMATRIX with proper conversion functions that handle the row-major/column-major transpose.

Before:

DX8CALL(SetTransform(transform, (D3DMATRIX*)&m));

After:

D3DXMATRIX d3dMat = Build_D3DXMATRIX(m);
DX8CALL(SetTransform(transform, &d3dMat));

@bobtista bobtista force-pushed the bobtista/fix-matrix4-d3d-transpose branch from 518ebd4 to fa2fff2 Compare November 13, 2025 17:13
@bobtista bobtista force-pushed the bobtista/fix-matrix4-d3d-transpose branch 2 times, most recently from 9a7854f to de78d22 Compare December 3, 2025 20:51
@bobtista bobtista changed the title refactor(dx8): Replace unsafe Matrix4/D3DMATRIX casts with explicit conversions refactor(dx8): Replace unsafe Matrix4x4/D3DMATRIX casts with proper transpose conversion Dec 3, 2025
@bobtista bobtista force-pushed the bobtista/fix-matrix4-d3d-transpose branch 5 times, most recently from 93986cd to 2a0b73e Compare December 3, 2025 21:38
@bobtista bobtista marked this pull request as ready for review December 3, 2025 21:53
Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goes into the right direction but it still looks like there are quirks.

Did you test the code paths? Is the math still correct?

@@ -242,14 +242,15 @@ void WaterRenderObjClass::setupJbaWaterShader(void)

Matrix4x4 curView;
DX8Wrapper::_Get_DX8_Transform(D3DTS_VIEW, curView);
D3DXMatrixInverse(&inv, &det, (D3DXMATRIX*)&curView);
D3DXMATRIX d3dCurView = Build_D3DXMATRIX(curView);
D3DXMatrixInverse(&inv, &det, &d3dCurView);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this inverse meant to be a transpose or does it really want to do the inverse here on a D3DXMATRIX that is in DX coordinates?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure - the old code cast a column-major Matrix4x4 to D3DXMATRIX* (row-major) without conversion, then inverted.
For orthogonal view matrices, inverse equals transpose.
In the Thyme commit, they still use D3DXMatrixInverse after removing the casts, so maybe inverse is intended? I'm going to test both and see what's right.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just played a skirmish with this build, and it seems normal to me. I imagine it would be very obvious if matrix math was inverted. I suppose I could run two clients and set a breakpoint and see if any values differ? What would you recommend?

Copy link
Author

@bobtista bobtista Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok update: I spent today testing and debugging this. I think it's ready now.

On main:

  • Matrix4x4 and D3DMATRIX have the same memory layout (both row-major in memory).
  • The unsafe cast is a direct memory copy with no transpose, which worked because both types share the same memory layout.

Main branch: Matrix4x4 M → direct copy → D3DMATRIX (same bytes) → direct copy → Matrix4x4 M
Fix branch: Matrix4x4 M → transpose → D3DMATRIX M^T → transpose → Matrix4x4 M

In today's testing I noticed that the ship cannon fire animations were glitching in the shell map.
The issue was that, while projection is directly sent to directx8 (transposed) and then we get it from directx8 (transpoesd), render_state.world and render_state.view are cached as Matrix4x4, only transposed when we send them to directx8 - and we can get them from the cache as Matrix4x4 too. The bug was that I was transposing world and view, but they're already in the right format. Once I removed the extra transpose, the cannon fire animations were back to normal.

I added debug lines and verified that the math now comes out the same on main and the branch (in the previous commit, the math was sometimes off because of the extra transpose).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this branch uses D3DXMatrixInverse() in W3DWater.cpp (lines 245, 1605, and 3001). This seems to be consistent with both main and the Thyme change. I tested changing them to D3DXMatrixTranspose() during debugging and water rendering still looked correct, but I haven't committed those changes. Google says For orthogonal matrices (rotation only), inverse = transpose, so if the view matrix is orthogonal at those points, transpose would be correct and more efficient. It's correct as it is, and matches the original, so I left it this way.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I found that I needed to update a bunch of other callers with my approach from before, and it was making the diff bigger. So, instead of storing Matrix4x4 in RenderStateStruct and converting at every use site, we now store D3DXMATRIX directly - this is more like how it was in the Thyme version too. Tested again, it looks good.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes storing D3DMATRIX in RenderStateStruct makes sense.

I reimplemented the change with #2052 from scratch because I was not entirely happy with the implementation details of this change.

@bobtista bobtista force-pushed the bobtista/fix-matrix4-d3d-transpose branch from 84f0b89 to fb08dee Compare December 19, 2025 16:00
@xezon xezon added Minor Severity: Minor < Major < Critical < Blocker Refactor Edits the code with insignificant behavior changes, is never user facing Stability Concerns stability of the runtime labels Jan 2, 2026
@bobtista bobtista closed this Jan 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Minor Severity: Minor < Major < Critical < Blocker Refactor Edits the code with insignificant behavior changes, is never user facing Stability Concerns stability of the runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Properly implement transpose between Matrix4 and D3DMatrix

2 participants