{"record":{"id":"b0649aa6938782cf","repo":"stride3d/stride","slug":"width-height-depth-must-be-power-of-2-texture","errorCode":null,"errorMessage":"Width/Height/Depth must be power of 2","messagePattern":"Width/Height/Depth must be power of 2","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/Texture.cs","lineNumber":774,"sourceCode":"        /// <exception cref=\"ArgumentOutOfRangeException\">\n        ///   <paramref name=\"mipLevels\"/> is greater than the maximum number of possible mip-levels for the provided\n        ///   <paramref name=\"width\"/>, <paramref name=\"height\"/>, and <paramref name=\"depth\"/>.\n        /// </exception>\n        /// <exception cref=\"ArgumentException\">\n        ///   <paramref name=\"width\"/>, <paramref name=\"height\"/>, and <paramref name=\"depth\"/> must all be\n        ///   a power of two.\n        /// </exception>\n        /// <exception cref=\"InvalidOperationException\">\n        ///   The dimensions must be a <strong>power of two (2^n)</strong>.\n        /// </exception>\n        public static int CountMipLevels(int width, int height, int depth, MipMapCount mipLevels)\n        {\n            switch (mipLevels.Count)\n            {\n                case > 1:  // Specific number\n                {\n                    if (!int.IsPow2(width) || !int.IsPow2(height) || !int.IsPow2(depth))\n                        throw new InvalidOperationException(\"Width/Height/Depth must be power of 2\");\n\n                    var maxMipLevels = CountMipLevels(width, height, depth);\n                    ArgumentOutOfRangeException.ThrowIfGreaterThan(mipLevels.Count, maxMipLevels, nameof(mipLevels));\n                    return mipLevels.Count;\n                }\n                case 0:\n                    if (!int.IsPow2(width) || !int.IsPow2(height) || !int.IsPow2(depth))\n                        throw new InvalidOperationException(\"Width/Height/Depth must be power of 2\");\n\n                    return CountMipLevels(width, height, depth);  // All mips\n\n                default:\n                    return 1;  // Single mip\n            }\n        }\n\n        /// <summary>\n        ///   Counts the number of mip-levels for a three-dimensional Texture.","sourceCodeStart":756,"sourceCodeEnd":792,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/Texture.cs#L756-L792","documentation":"When CountMipLevels is asked for a specific mip count greater than 1, Stride requires the texture dimensions to be powers of two, because a fixed explicit mip chain for non-PoW2 sizes is ambiguous/unsupported. It throws InvalidOperationException when width, height, or depth is not a power of 2.","triggerScenarios":"Calling Texture.CountMipLevels(width, height, depth) with mipLevels.Count > 1 on non-power-of-two dimensions — e.g. 1920x1080 or a user-resized texture with an explicit mip count.","commonSituations":"Screen-sized render targets (1080p, 1440p) with manual mip specification; textures resized by user content; porting code that assumed PoW2 textures.","solutions":["Use power-of-two texture dimensions when requesting an explicit mip count.","Pass mipLevels.Count == 0 to let Stride compute the full mip chain automatically.","Round dimensions up/down to the nearest power of two before the call."],"exampleFix":"// before\nint mips = Texture.CountMipLevels(1920, 1080, 1, 4); // throws\n// after\nint mips = Texture.CountMipLevels(2048, 2048, 1, 12); // PoW2 dims\n// or let the library compute all mips:\nint mips = Texture.CountMipLevels(1920, 1080, 1);","handlingStrategy":"validation","validationCode":"if (mipCount > 1 && !(int.IsPow2(width) && int.IsPow2(height) && int.IsPow2(depth)))\n    throw new InvalidOperationException($\"Explicit mip count requires PoW2 dims, got {width}x{height}x{depth}\");","typeGuard":"static bool IsPow2Dims(int w, int h, int d) => int.IsPow2(w) && int.IsPow2(h) && int.IsPow2(d);","tryCatchPattern":"try { mips = Texture.CountMipLevels(w, h, d, count); }\ncatch (InvalidOperationException ex)\n{\n    logger.LogError(ex, \"NPOT dims {W}x{H} with explicit mip count\", w, h);\n    mips = Texture.CountMipLevels(w, h, d);\n}","preventionTips":["Prefer PoW2 texture sizes for anything needing mipmaps.","Pass explicit mip counts only for DDS-style PoW2 assets.","Add a startup assert for NPOT sizes in content pipeline tools."],"tags":["graphics","stride","mipmaps","pow2"],"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"}