summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/IndexGenerator.cpp
diff options
context:
space:
mode:
authorMai <mai.iam2048@gmail.com>2023-01-05 15:15:09 +0000
committerGitHub <noreply@github.com>2023-01-05 15:15:09 +0000
commitcc14d60bbb9fd069e831bd3d84b8dc4fa1b3ddee (patch)
tree2c9012ecb89f5c196a43c5cb6df04c2549b12421 /Source/Core/VideoCommon/IndexGenerator.cpp
parent7cd3839fd66a9169f122709765baa50d4269b800 (diff)
parentcefcd9c93c2d76074c3be7a6cb96812dad082e9c (diff)
Merge pull request #11402 from Pokechu22/too-many-indices
IndexGenerator: Fix off-by-one in GetRemainingIndices
Diffstat (limited to 'Source/Core/VideoCommon/IndexGenerator.cpp')
-rw-r--r--Source/Core/VideoCommon/IndexGenerator.cpp23
1 files changed, 20 insertions, 3 deletions
diff --git a/Source/Core/VideoCommon/IndexGenerator.cpp b/Source/Core/VideoCommon/IndexGenerator.cpp
index 5d86561ffd..03d0815a13 100644
--- a/Source/Core/VideoCommon/IndexGenerator.cpp
+++ b/Source/Core/VideoCommon/IndexGenerator.cpp
@@ -328,12 +328,29 @@ void IndexGenerator::AddExternalIndices(const u16* indices, u32 num_indices, u32
u32 IndexGenerator::GetRemainingIndices(OpcodeDecoder::Primitive primitive) const
{
- u32 max_index = USHRT_MAX;
+ u32 max_index = UINT16_MAX;
if (g_Config.UseVSForLinePointExpand() && primitive >= OpcodeDecoder::Primitive::GX_DRAW_LINES)
max_index >>= 2;
- // -1 is reserved for primitive restart
+ // Although we reserve UINT16_MAX for primitive restart, we aren't allowed to use that as an
+ // actual index. But, the maximum number of vertices a game can send is UINT16_MAX, so up to
+ // 0xffff indices will be used by the game. These indices would be 0x0000 through 0xfffe,
+ // and since m_base_index gets incremented for each index used, after that m_base_index
+ // would be 0xffff and no indices remain. If a game uses 0xfffe vertices, assuming m_base_index
+ // started at 0 it would end at 0xfffe and one more index could be used. So, we do not need to
+ // subtract 1 from max_index to correctly reserve one index for primitive restart.
+ //
+ // Pocoyo Racing uses a draw command with 0xffff vertices, which previously caused issues; see
+ // https://bugs.dolphin-emu.org/issues/13136 for details.
- return max_index - m_base_index - 1;
+ if (m_base_index > max_index) [[unlikely]]
+ {
+ PanicAlertFmt("GetRemainingIndices would overflow; we've already written too many indices? "
+ "base index {} > max index {}",
+ m_base_index, max_index);
+ return 0;
+ }
+
+ return max_index - m_base_index;
}