stride3d/stride · error · ArgumentOutOfRangeException

vertexStride must be >= 0

Error message

vertexStride must be >= 0

What it means

The core overload throws ArgumentOutOfRangeException('vertexStride', 'vertexStride must be >= 0') when a negative stride is passed. A negative stride would make pointer arithmetic walk outside the buffer; zero is allowed and means 'take stride from the declaration'.

Solutions

  1. Pass the correct positive stride, or 0 to let the method use vertexDeclaration.VertexStride.
  2. Compute the stride from Unsafe.SizeOf<T>() or the declaration instead of manual math.
  3. Validate the stride before the call.

Example fix

// before
int stride = ComputeStrideSomehow(); // negative
var result = VertexHelper.GenerateMultiTextureCoordinates(decl, ptr, count, 0, stride);
// after
int stride = 0; // 0 = derive from declaration
var result = VertexHelper.GenerateMultiTextureCoordinates(decl, ptr, count, 0, stride);
Defensive patterns

Strategy: validation

Validate before calling

if (vertexStride < 0) vertexStride = 0; // 0 lets the declaration supply the stride

Try / catch

try { var r = VertexHelper.GenerateMultiTextureCoordinates(decl, ptr, count, offset, stride); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "vertexStride") { /* pass 0 or the declaration stride */ }

Prevention

When it happens

Trigger: Passing a negative vertexStride — typically from a miscomputed sizeof/offset or a corrupted stride variable.

Common situations: Hand-calculated stride in interop code; stride read from a malformed asset; sign error after refactoring offsets.

Related errors


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

Appendix: source

Thrown at sources/engine/Stride.Graphics/VertexHelper.cs:123

        /// </exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// vertexCount;vertexCount must be > 0
        /// or
        /// vertexStride;vertexStride must be >= 0
        /// or
        /// maxTexcoord;maxTexcoord must be >= 0
        /// </exception>
        /// <exception cref="System.InvalidOperationException">The vertex buffer must contain at least the TEXCOORD</exception>
        /// <remarks>
        /// The original vertex buffer must contain at least a TEXCOORD[0-9] attribute in order for this method to work.
        /// This method will copy the value of the first existing TEXCOORD found in the vertex buffer to the newly created TEXCOORDS.
        /// </remarks>
        public static unsafe VertexTransformResult GenerateMultiTextureCoordinates(VertexDeclaration vertexDeclaration, IntPtr vertexBufferData, int vertexCount, int vertexOffset, int vertexStride, int maxTexcoord = 9)
        {
            if (vertexDeclaration == null) throw new ArgumentNullException("vertexDeclaration");
            if (vertexBufferData == IntPtr.Zero) throw new ArgumentNullException("vertexBufferData");
            if (vertexCount <= 0) throw new ArgumentOutOfRangeException("vertexCount", "vertexCount must be > 0");
            if (vertexStride < 0) throw new ArgumentOutOfRangeException("vertexStride", "vertexStride must be >= 0");
            if (maxTexcoord < 0) throw new ArgumentOutOfRangeException("maxTexcoord", "maxTexcoord must be >= 0");

            // Get the stride from the vertex declaration if necessary
            if (vertexStride == 0)
            {
                vertexStride = vertexDeclaration.VertexStride;
            }

            // TODO: Usage index in key
            var offsetMapping = vertexDeclaration
                .EnumerateWithOffsets()
                .ToDictionary(x => x.VertexElement.SemanticAsText, x => x.Offset);

            var newVertexElements = new List<VertexElement>();

            int vertexUVOffset = -1;

            // Use R16G16_SNorm as it is supported from HW Level 9.1

View on GitHub (pinned to 96fad776d2)