From 24f64ae1a8d4aeae9d79f608b2d33e8b22c9810d Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 25 Jul 2021 18:11:25 +0100 Subject: [PATCH] Fix an edge case where the wrong sheet may be mapped to a depth sprite. --- OpenRA.Game/Graphics/SpriteRenderer.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/OpenRA.Game/Graphics/SpriteRenderer.cs b/OpenRA.Game/Graphics/SpriteRenderer.cs index 90146b0990..b07f70da5e 100644 --- a/OpenRA.Game/Graphics/SpriteRenderer.cs +++ b/OpenRA.Game/Graphics/SpriteRenderer.cs @@ -82,28 +82,32 @@ namespace OpenRA.Graphics for (; secondarySheetIndex < ns; secondarySheetIndex++) if (sheets[secondarySheetIndex] == secondarySheet) break; + + // If neither sheet has been mapped both index values will be set to ns. + // This is fine if they both reference the same texture, but if they don't + // we must increment the secondary sheet index to the next free sampler. + if (secondarySheetIndex == sheetIndex && secondarySheet != sheet) + secondarySheetIndex++; } // Make sure that we have enough free samplers to map both if needed, otherwise flush - var needSamplers = (sheetIndex == ns ? 1 : 0) + (secondarySheetIndex == ns ? 1 : 0); - if (ns + needSamplers >= sheets.Length) + if (Math.Max(sheetIndex, secondarySheetIndex) >= sheets.Length) { Flush(); sheetIndex = 0; - if (ss != null) - secondarySheetIndex = 1; + secondarySheetIndex = ss != null && ss.SecondarySheet != sheet ? 1 : 0; } if (sheetIndex >= ns) { sheets[sheetIndex] = sheet; - ns += 1; + ns++; } if (secondarySheetIndex >= ns && ss != null) { sheets[secondarySheetIndex] = ss.SecondarySheet; - ns += 1; + ns++; } return new int2(sheetIndex, secondarySheetIndex);