{"record":{"id":"bb14961060f485c6","repo":"MonoGame/MonoGame","slug":"rectangle-must-be-inside-the-texture-bounds-bb1496","errorCode":null,"errorMessage":"Rectangle must be inside the texture bounds","messagePattern":"Rectangle must be inside the texture bounds","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/Graphics/TextureCube.cs","lineNumber":312,"sourceCode":"        /// </list>\n        /// </exception>\n        /// <exception cref=\"ArgumentNullException\">The <paramref name=\"data\"/> parameter is null.</exception>\n        public void SetData<T>(CubeMapFace face, int level, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct\n        {\n            Rectangle checkedRect;\n            ValidateParams(level, rect, data, startIndex, elementCount, out checkedRect);\n            PlatformSetData(face, level, checkedRect, data, startIndex, elementCount);\n\t\t}\n\n        private void ValidateParams<T>(int level, Rectangle? rect, T[] data, int startIndex,\n            int elementCount, out Rectangle checkedRect) where T : struct\n        {\n            var textureBounds = new Rectangle(0, 0, Math.Max(Size >> level, 1), Math.Max(Size >> level, 1));\n            checkedRect = rect ?? textureBounds;\n            if (level < 0 || level >= LevelCount)\n                throw new ArgumentException(\"level must be smaller than the number of levels in this texture.\");\n            if (!textureBounds.Contains(checkedRect) || checkedRect.Width <= 0 || checkedRect.Height <= 0)\n                throw new ArgumentException(\"Rectangle must be inside the texture bounds\", \"rect\");\n            if (data == null)\n                throw new ArgumentNullException(\"data\");\n            var tSize = ReflectionHelpers.FastSizeOf<T>();\n            var fSize = Format.GetSize();\n            if (tSize > fSize || fSize % tSize != 0)\n                throw new ArgumentException(\"Type T is of an invalid size for the format of this texture.\", \"T\");\n            if (startIndex < 0 || startIndex >= data.Length)\n                throw new ArgumentException(\"startIndex must be at least zero and smaller than data.Length.\", \"startIndex\");\n            if (data.Length < startIndex + elementCount)\n                throw new ArgumentException(\"The data array is too small.\");\n\n            int dataByteSize;\n            if (Format.IsCompressedFormat())\n            {\n                // round x and y down to next multiple of four; width and height up to next multiple of four\n                var roundedWidth = (checkedRect.Width + 3) & ~0x3;\n                var roundedHeight = (checkedRect.Height + 3) & ~0x3;\n                checkedRect = new Rectangle(checkedRect.X & ~0x3, checkedRect.Y & ~0x3,","sourceCodeStart":294,"sourceCodeEnd":330,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/Graphics/TextureCube.cs#L294-L330","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nvar srcRect = new Rectangle(0, 0, cube.Size, cube.Size);\ncube.SetData(face, level, srcRect, data, 0, data.Length);\n\n// after\nint levelSize = Math.Max(cube.Size >> level, 1);\nvar srcRect = new Rectangle(0, 0, levelSize, levelSize);\ncube.SetData(face, level, srcRect, data, 0, data.Length);","handlingStrategy":"validation","validationCode":"int levelSize = Math.Max(cube.Size >> level, 1);\nRectangle? safeRect = rect == null ? null\n    : (rect.Value.X >= 0 && rect.Value.Y >= 0\n       && rect.Value.Right <= levelSize && rect.Value.Bottom <= levelSize\n       && rect.Value.Width > 0 && rect.Value.Height > 0 ? rect : null);\nif (rect != null && safeRect == null) throw new ArgumentOutOfRangeException(nameof(rect));\ncube.SetData(face, level, safeRect, data, 0, data.Length);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["monogame","xna","graphics","texture","mipmap","argument-validation","texturecube"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}