MonoGame/MonoGame · error · ArgumentException
Rectangle must be inside the texture bounds
Error message
Rectangle must be inside the texture bounds
What it means
Thrown in ValidateParams when the optional `rect` (sub-rectangle to read/write) is not fully contained within the level's texture bounds, or has non-positive width/height. The texture bounds are computed as `Math.Max(Size >> level, 1)` per side, so each mip level has its own valid region. This prevents partial/out-of-range GPU region access.
Solutions
- Compute the per-level bounds (`Math.Max(Size >> level, 1)`) and clamp the rect to fit before calling.
- Pass `null` for rect when you want the whole level instead of a hand-built full-size rectangle.
- Validate `rect.X >= 0 && rect.Y >= 0 && rect.Right <= levelSize && rect.Bottom <= levelSize && rect.Width > 0 && rect.Height > 0`.
- Scale any source rectangle down by `1 << level` when targeting a specific mip level.
Example fix
// before var srcRect = new Rectangle(0, 0, cube.Size, cube.Size); cube.SetData(face, level, srcRect, data, 0, data.Length); // after int levelSize = Math.Max(cube.Size >> level, 1); var srcRect = new Rectangle(0, 0, levelSize, levelSize); cube.SetData(face, level, srcRect, data, 0, data.Length);
Defensive patterns
Strategy: validation
Validate before calling
int levelSize = Math.Max(cube.Size >> level, 1);
Rectangle? safeRect = rect == null ? null
: (rect.Value.X >= 0 && rect.Value.Y >= 0
&& rect.Value.Right <= levelSize && rect.Value.Bottom <= levelSize
&& rect.Value.Width > 0 && rect.Value.Height > 0 ? rect : null);
if (rect != null && safeRect == null) throw new ArgumentOutOfRangeException(nameof(rect));
cube.SetData(face, level, safeRect, data, 0, data.Length); Prevention
- Pass null for rect when you want the whole level.
- Scale source rectangles by 1 << level for mip levels.
- Validate rect extent against the per-level size, not the base size.
When it happens
Trigger: Passing a Rectangle whose X/Y/Width/Height exceed the mip level dimensions; passing a rect sized for level 0 to a higher mip level; rect with zero or negative extent; rect located at coordinates outside [0, levelSize).
Common situations: Reusing a source-image rectangle that is larger than the mip face; forgetting that each mip level halves dimensions; passing a full-size rect to a 1x1 or 2x2 mip level; sign errors producing negative width/height.
Related errors
- level must be smaller than the number of levels in this…
- Cube size must be greater than zero
- data
- elementCount is not the right size, elementCount *…
- startIndex must be at least zero and smaller than…
AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13).
Data as JSON: /api/errors/bb14961060f485c6.
Report an issue: GitHub.
Appendix: source
Thrown at MonoGame.Framework/Graphics/TextureCube.cs:312
/// </list>
/// </exception>
/// <exception cref="ArgumentNullException">The <paramref name="data"/> parameter is null.</exception>
public void SetData<T>(CubeMapFace face, int level, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct
{
Rectangle checkedRect;
ValidateParams(level, rect, data, startIndex, elementCount, out checkedRect);
PlatformSetData(face, level, checkedRect, data, startIndex, elementCount);
}
private void ValidateParams<T>(int level, Rectangle? rect, T[] data, int startIndex,
int elementCount, out Rectangle checkedRect) where T : struct
{
var textureBounds = new Rectangle(0, 0, Math.Max(Size >> level, 1), Math.Max(Size >> level, 1));
checkedRect = rect ?? textureBounds;
if (level < 0 || level >= LevelCount)
throw new ArgumentException("level must be smaller than the number of levels in this texture.");
if (!textureBounds.Contains(checkedRect) || checkedRect.Width <= 0 || checkedRect.Height <= 0)
throw new ArgumentException("Rectangle must be inside the texture bounds", "rect");
if (data == null)
throw new ArgumentNullException("data");
var tSize = ReflectionHelpers.FastSizeOf<T>();
var fSize = Format.GetSize();
if (tSize > fSize || fSize % tSize != 0)
throw new ArgumentException("Type T is of an invalid size for the format of this texture.", "T");
if (startIndex < 0 || startIndex >= data.Length)
throw new ArgumentException("startIndex must be at least zero and smaller than data.Length.", "startIndex");
if (data.Length < startIndex + elementCount)
throw new ArgumentException("The data array is too small.");
int dataByteSize;
if (Format.IsCompressedFormat())
{
// round x and y down to next multiple of four; width and height up to next multiple of four
var roundedWidth = (checkedRect.Width + 3) & ~0x3;
var roundedHeight = (checkedRect.Height + 3) & ~0x3;
checkedRect = new Rectangle(checkedRect.X & ~0x3, checkedRect.Y & ~0x3,View on GitHub (pinned to 1d71bbd0ff)