forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 145
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
Closed
bobtista
wants to merge
12
commits into
TheSuperHackers:main
from
bobtista:bobtista/fix-matrix4-d3d-transpose
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
01e794e
refactor(dx8): Replace unsafe Matrix4x4/D3DMATRIX casts with proper t…
bobtista ca3f233
refactor(dx8): Add Build_Matrix4 overloads for D3DXMATRIX
bobtista 8da4e83
refactor(dx8): Remove unsafe cast and add D3DXMATRIX getter in Genera…
bobtista 72908ac
refactor(dx8): Remove unsafe cast and add D3DXMATRIX getter in Generals
bobtista 5e5d151
refactor(dx8): Use D3DXMATRIX getter directly in water shaders
bobtista fb08dee
refactor(dx8): dedupe Matrix4x4 D3D conversion declarations
bobtista 7fad65c
replicate sortingrenderer changes to Generals
bobtista bfa58a6
remove extra .transpose call
bobtista 3378e28
refactor(dx8): Use Build_Matrix4 in Get_Transform default case
bobtista d187b29
revert protected change
bobtista 0f8a51f
fix(dx8): Complete Matrix4x4/D3DXMATRIX conversion coverage
bobtista 0793191
refactor(dx8): Store D3DXMATRIX directly in RenderStateStruct
bobtista File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
168 changes: 98 additions & 70 deletions
168
Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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?
Uh oh!
There was an error while loading. Please reload this page.
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:
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.