{"record":{"id":"2a2cf1a47f38d86d","repo":"stride3d/stride","slug":"miplevels-must-be-maxmips","errorCode":null,"errorMessage":"MipLevels must be <= {maxMips}","messagePattern":"MipLevels must be <= (.+?)","errorType":"validation","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Foundation/Graphics/Image.cs","lineNumber":622,"sourceCode":"        {\n            ArgumentNullException.ThrowIfNull(imageStream);\n\n            Save(PixelBuffers, PixelBuffers.Length, Description, imageStream, fileType);\n        }\n\n        /// <summary>\n        /// Calculates the number of miplevels for a Texture 1D.\n        /// </summary>\n        /// <param name=\"width\">The width of the texture.</param>\n        /// <param name=\"mipLevels\">A <see cref=\"MipMapCount\"/>, set to true to calculates all mipmaps, to false to calculate only 1 miplevel, or > 1 to calculate a specific amount of levels.</param>\n        /// <returns>The number of miplevels.</returns>\n        public static int CalculateMipLevels(int width, MipMapCount mipLevels)\n        {\n            if (mipLevels > 1)\n            {\n                int maxMips = CountMips(width);\n                if (mipLevels > maxMips)\n                    throw new InvalidOperationException($\"MipLevels must be <= {maxMips}\");\n            }\n            else if (mipLevels == 0)\n            {\n                mipLevels = CountMips(width);\n            }\n            else\n            {\n                mipLevels = 1;\n            }\n            return mipLevels;\n        }\n\n        /// <summary>\n        /// Calculates the number of miplevels for a Texture 2D.\n        /// </summary>\n        /// <param name=\"width\">The width of the texture.</param>\n        /// <param name=\"height\">The height of the texture.</param>\n        /// <param name=\"mipLevels\">A <see cref=\"MipMapCount\"/>, set to true to calculates all mipmaps, to false to calculate only 1 miplevel, or > 1 to calculate a specific amount of levels.</param>","sourceCodeStart":604,"sourceCodeEnd":640,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Foundation/Graphics/Image.cs#L604-L640","documentation":"Image.CalculateMipLevels(width, mipLevels) computes the mip chain length for a 1D image. When an explicit MipMapCount greater than 1 is requested, it must not exceed CountMips(width); otherwise an InvalidOperationException is thrown because the requested chain cannot be built from the given width.","triggerScenarios":"Calling CalculateMipLevels(width, mipLevels) with mipLevels > 1 and mipLevels > CountMips(width) — e.g. requesting 8 mip levels on a 16-pixel-wide image (max 5).","commonSituations":"Hardcoding mip counts (e.g. 10) from another texture size; loading mip data from a file whose chain does not match the declared width; generating mips for very small dimensions.","solutions":["Use MipMapCount.Auto (0) so the maximum valid chain is computed automatically","Compute CountMips(width) first and clamp the requested count to it","Increase the image width if more mip levels are genuinely required"],"exampleFix":"// before\nint mips = Image.CalculateMipLevels(16, 10);\n// after\nint mips = Image.CalculateMipLevels(16, MipMapCount.Auto);","handlingStrategy":"validation","validationCode":"int max = Image.CountMips(width);\nif (requestedMips > max) requestedMips = MipMapCount.Auto; // or clamp to max\nint mips = Image.CalculateMipLevels(width, requestedMips);","typeGuard":"bool IsValidMipCount(int width, MipMapCount mips) => mips <= 1 || mips <= Image.CountMips(width);","tryCatchPattern":"try { int mips = Image.CalculateMipLevels(width, count); }\ncatch (InvalidOperationException) { mips = Image.CalculateMipLevels(width, MipMapCount.Auto); }","preventionTips":["Prefer MipMapCount.Auto over fixed counts","Recompute mip counts whenever texture dimensions change"],"tags":["csharp","stride","graphics","mipmap"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}