-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
fix(core/rdr3): AMV hardcoded limit causing blinking #3145
Open
alexguirre
wants to merge
3
commits into
citizenfx:master
Choose a base branch
from
alexguirre:fix-amv-blinking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <StdInc.h> | ||
#include <Hooking.h> | ||
#include <Hooking.Patterns.h> | ||
#include <Pool.h> | ||
|
||
// | ||
// There is a global bitset used to store the enabled/disabled state of every AMV. This bitset has a hardcoded size | ||
// of 6016 entries, once there are more AMVs than the bitset size, it starts reading/writing out-of-bounds. This causes | ||
// the AMVs to start blinking and can potentially crash. | ||
// | ||
// Here we increase the size of that bitset to fit the capacity of all the AMV pools. | ||
// | ||
|
||
static HookFunction hookFunction([]() | ||
{ | ||
// keep in sync with gameconfig | ||
constexpr size_t CAmbientMaskVolume_PoolSize = 6686; | ||
constexpr size_t CAmbientMaskVolumeDoor_PoolSize = 1850; | ||
constexpr size_t CAmbientMaskVolumeEntity_PoolSize = 150; | ||
|
||
constexpr size_t TotalAMVs = CAmbientMaskVolume_PoolSize + CAmbientMaskVolumeDoor_PoolSize + CAmbientMaskVolumeEntity_PoolSize; | ||
|
||
// Due to how AMVPresentBuffer is compiled, the number of blocks needs to match the form (N*32+28). AMVPresentBuffer copies the bitset | ||
// from update thread to the render thread bitset. The copy loop is vectorized to copy 32 blocks per iteration, then has an epilogue to | ||
// copy the remaining 28 blocks, which works out for the original 188 blocks. When resizing the bitset we need to pad the number of blocks | ||
// so it doesn't read/write out-of-bounds. | ||
constexpr size_t BitsetNumBlocksNoPadding = (TotalAMVs + 31) / 32; // minimum number of 32-bit blocks needed by the AMVs | ||
constexpr size_t BitsetPresentCopyNumIterations = (BitsetNumBlocksNoPadding - 28 + 31) / 32; // smallest N such that (N*32+28) >= BitsetNumBlocksNoPadding | ||
constexpr size_t BitsetNumBlocks = BitsetPresentCopyNumIterations * 32 + 28; | ||
constexpr size_t BitsetNumBytes = BitsetNumBlocks * sizeof(uint32_t); | ||
|
||
// x2 because the bitset is double-buffered, one for render thread and another one for update thread | ||
uint32_t* amvEnabledBitsetReplacement = reinterpret_cast<uint32_t*>(hook::AllocateStubMemory(BitsetNumBytes * 2)); | ||
memset(amvEnabledBitsetReplacement, 0, BitsetNumBytes * 2); | ||
|
||
// AMVInit | ||
{ | ||
auto location = hook::get_pattern<uint8_t>("41 B8 ? ? ? ? 44 89 25"); | ||
hook::put<int32_t>(location + 2, BitsetNumBytes * 2); | ||
hook::put<int32_t>(location + 0x10, (intptr_t)amvEnabledBitsetReplacement - (intptr_t)location - 0x10 - 4); | ||
} | ||
|
||
// AMVIsEnabled | ||
{ | ||
|
||
auto location = hook::get_pattern<uint8_t>("48 69 C8 ? ? ? ? 44 8B C2"); | ||
hook::put<int32_t>(location + 3, BitsetNumBlocks); | ||
hook::put<int32_t>(location + 0xD, (intptr_t)amvEnabledBitsetReplacement - (intptr_t)location - 0xD - 4); | ||
} | ||
|
||
// AMVSetEnabled | ||
{ | ||
auto location = hook::get_pattern<uint8_t>("4C 69 C0 ? ? ? ? 0F B7 C9"); | ||
hook::put<int32_t>(location + 3, BitsetNumBytes); | ||
hook::put<int32_t>(location + 0xD, (intptr_t)amvEnabledBitsetReplacement - (intptr_t)location - 0xD - 4); | ||
} | ||
|
||
// AMVPresentBuffer | ||
{ | ||
auto location = hook::get_pattern<uint8_t>("48 69 D0 ? ? ? ? 41 8B C0"); | ||
hook::put<int32_t>(location + 3, BitsetNumBytes); | ||
hook::put<int32_t>(location + 0x14, BitsetNumBytes); | ||
hook::put<int32_t>(location + 0x19, BitsetPresentCopyNumIterations); | ||
hook::put<int32_t>(location + 0xD, (intptr_t)amvEnabledBitsetReplacement - (intptr_t)location - 0xD - 4); | ||
} | ||
}); |
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.
Currently we don't allow to increase these 3 pools and such way is safe, but what should we do in future when anybody need to extend one of these pools?
And what will happen if we define bigger value here than real pool size? Probably we can set here maximum possible value, for example double pool size, then it's fine.
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.
In case of enabling
increase_pool_size
with these pools, looking up the increase values infx::PoolSizeManager::GetIncreaseRequest()
and adding them to these pool size constants would be enough. The increase request map is already initialized when this hook function runs, so should be straightforward to add.Nothing, just a bit of unused memory.