-
Notifications
You must be signed in to change notification settings - Fork 146
refactor(dx8): Replace unsafe Matrix4x4/D3DMATRIX casts with proper transpose conversion #1852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(dx8): Replace unsafe Matrix4x4/D3DMATRIX casts with proper transpose conversion #1852
Conversation
518ebd4 to
fa2fff2
Compare
Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
Outdated
Show resolved
Hide resolved
9a7854f to
de78d22
Compare
93986cd to
2a0b73e
Compare
xezon
left a comment
There was a problem hiding this 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); | |||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
Outdated
Show resolved
Hide resolved
…ranspose conversion
84f0b89 to
fb08dee
Compare
Summary
Replaces all C-style casts between
Matrix4x4/Matrix3DandD3DXMATRIX/D3DMATRIXwith proper conversion functions that handle the row-major/column-major transpose.Before:
After: