stride3d/stride · error · ArgumentOutOfRangeException

Value must be > 0

Error message

Value must be > 0

What it means

CalculateMipCountFromSize computes the number of mip levels for a texture dimension. It throws ArgumentOutOfRangeException when the supplied size is zero or negative, because mip level math is undefined for non-positive dimensions.

Solutions

  1. Ensure texture description dimensions (Width/Height/Depth) are positive before creating the texture
  2. Clamp or validate the size parameter before calling CalculateMipCountFromSize
  3. Check upstream code that computes dimensions (scaling, division) so it never yields 0

Example fix

// before
var mipCount = Texture.CalculateMipCountFromSize(desc.Width);
// after
if (desc.Width <= 0) throw new ArgumentException("Width must be > 0");
var mipCount = Texture.CalculateMipCountFromSize(desc.Width);
Defensive patterns

Strategy: validation

Validate before calling

if (size <= 0) throw new ArgumentException("size must be > 0");
var mipCount = Texture.CalculateMipCountFromSize(size);

Type guard

bool IsValidSize(int s) => s > 0;

Try / catch

try { mipCount = Texture.CalculateMipCountFromSize(size); }
catch (ArgumentOutOfRangeException) { mipCount = 1; /* no mips */ }

Prevention

When it happens

Trigger: Calling CalculateMipCountFromSize (directly or via texture mip-count calculation during texture creation) with size <= 0 — e.g. a TextureDescription with Width, Height, or Depth of 0 or a negative value.

Common situations: A texture description where one dimension was left at 0 (uninitialized struct default), or computed dimension became 0 after a bad scale/divide operation before creating the texture.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/b5a2c730076c1404. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Graphics/Vulkan/Texture.Vulkan.cs:690

            if (device.Features.CurrentProfile < GraphicsProfile.Level_10_0 && (description.Flags & TextureFlags.DepthStencil) == 0 && description.Format.IsCompressed)
            {
                description.MipLevelCount = Math.Min(CalculateMipCount(description.Width, description.Height), description.MipLevelCount);
            }
            return description;
        }

        /// <summary>
        /// Calculates the mip level from a specified size.
        /// </summary>
        /// <param name="size">The size.</param>
        /// <param name="minimumSizeLastMip">The minimum size of the last mip.</param>
        /// <returns>The mip level.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Value must be > 0;size</exception>
        private static int CalculateMipCountFromSize(int size, int minimumSizeLastMip = 4)
        {
            if (size <= 0)
            {
                throw new ArgumentOutOfRangeException("Value must be > 0", "size");
            }

            if (minimumSizeLastMip <= 0)
            {
                throw new ArgumentOutOfRangeException("Value must be > 0", "minimumSizeLastMip");
            }

            int level = 1;
            while ((size / 2) >= minimumSizeLastMip)
            {
                size = Math.Max(1, size / 2);
                level++;
            }
            return level;
        }

        /// <summary>
        /// Calculates the mip level from a specified width,height,depth.

View on GitHub (pinned to 96fad776d2)