stride3d/stride · error · ArgumentException

Element size must be set to sizeof(short) = 2 or…

Error message

Element size must be set to sizeof(short) = 2 or sizeof(int) = 4 for Index Buffers if bound as a Shader Resource

What it means

CheckPixelFormat validates that an IndexBuffer also flagged as ShaderResource uses an element size of 2 (ushort) or 4 (uint) bytes, since such a buffer is viewed as R16_UInt / R32_UInt. Any other element size cannot be mapped to a raw index view, so New/InitializeFrom throw ArgumentException.

Solutions

  1. Use ushort (2 bytes) or uint (4 bytes) index elements when the buffer must also be a ShaderResource.
  2. Convert the index data to 16- or 32-bit integers before creating the buffer.
  3. Remove BufferFlags.ShaderResource from the buffer description if GPU reading is not needed.

Example fix

// before
var indices = new byte[] { 0, 1, 2, 2, 1, 3 };
Buffer.New(device, indices, BufferFlags.IndexBuffer | BufferFlags.ShaderResource); // throws
// after
var indices = new ushort[] { 0, 1, 2, 2, 1, 3 };
Buffer.New(device, indices, BufferFlags.IndexBuffer | BufferFlags.ShaderResource);
Defensive patterns

Strategy: validation

Validate before calling

bool valid = !flags.HasFlag(BufferFlags.ShaderResource)
    || elementSize == sizeof(short) || elementSize == sizeof(int);

Type guard

bool IsValidIndexSrvElement(int elementSize) => elementSize is 2 or 4;

Try / catch

try { Buffer.New(device, indices, flags); }
catch (ArgumentException ex) when (ex.Message.Contains("Element size must be set"))
{
    indices = ConvertToUShortIndices(indices);
    Buffer.New(device, indices, flags);
}

Prevention

When it happens

Trigger: Creating a buffer with BufferFlags.IndexBuffer | BufferFlags.ShaderResource and elementSize other than 2 or 4 (e.g. byte-sized indices or 8-byte long indices); reading indices in a compute shader from an index buffer created with an unsupported element size.

Common situations: Attempting to read index data on the GPU for indirect drawing or GPU-driven culling; using struct/long indices while also binding the buffer as SRV.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at sources/engine/Stride.Graphics/Buffer.cs:884

        ///     <item>For other types of Buffers, this can be set to 0.</item>
        ///   </list>
        /// </param>
        /// <param name="viewFormat">
        ///   View format used if the Buffer is used as a Shader Resource View,
        ///   or <see cref="PixelFormat.None"/> if not.
        /// </param>
        /// <returns>The proposed <see cref="PixelFormat"/> to use.</returns>
        /// <exception cref="ArgumentException">
        ///   The <see cref="Buffer"/> is an <strong>Index Buffer</strong> that will be bound as a <em>Shader Resource</em>,
        ///   but the <paramref name="elementSize"/> is neither 2 bytes (<c>sizeof(short)</c>) nor 4 bytes (<c>sizeof(int)</c>).
        /// </exception>
        private static PixelFormat CheckPixelFormat(BufferFlags bufferFlags, int elementSize, PixelFormat viewFormat)
        {
            if (!bufferFlags.HasFlag(BufferFlags.IndexBuffer) || !bufferFlags.HasFlag(BufferFlags.ShaderResource))
                return viewFormat;

            if (elementSize != 2 && elementSize != 4)
                throw new ArgumentException("Element size must be set to sizeof(short) = 2 or sizeof(int) = 4 for Index Buffers if bound as a Shader Resource", nameof(elementSize));

            return elementSize == 2 ? PixelFormat.R16_UInt : PixelFormat.R32_UInt;
        }

        /// <summary>
        ///   Composes a new <see cref="BufferDescription"/> structure with the provided options.
        /// </summary>
        /// <param name="bufferSize">The size in bytes of the Buffer.</param>
        /// <param name="elementSize">The size in bytes of each element (in case of a <strong>Structured Buffer</strong>).</param>
        /// <param name="bufferFlags">The buffer flags to specify the type of Buffer.</param>
        /// <param name="usage">The usage for the Buffer, which determines who can read/write data.</param>
        /// <returns>A new <see cref="BufferDescription"/>.</returns>
        private static BufferDescription NewDescription(int bufferSize, int elementSize, BufferFlags bufferFlags, GraphicsResourceUsage usage)
        {
            return new BufferDescription
            {
                SizeInBytes = bufferSize,
                StructureByteStride = bufferFlags.HasFlag(BufferFlags.StructuredBuffer) ? elementSize : 0,

View on GitHub (pinned to 96fad776d2)