stride3d/stride · error · ArgumentException
Element size cannot be less or equal 0 for structured buffer
Error message
Element size cannot be less or equal 0 for structured buffer
What it means
Thrown in Buffer.Recreate on D3D12 when a buffer flagged as StructuredBuffer has StructureByteStride <= 0. A structured buffer requires a nonzero element size so the GPU knows the stride between elements; the library rejects the description with ArgumentException up front.
Solutions
- Set StructureByteStride to the exact size of one element (e.g. sizeof(MyStruct)) in the BufferDescription.
- Remove the StructuredBuffer flag if the buffer is a plain byte/raw buffer.
- Validate the description before calling Buffer.New / Recreate.
Example fix
// before var desc = new BufferDescription(dataSize, BufferFlags.StructuredBuffer, ResourceUsage.Default); // after var desc = new BufferDescription(dataSize, BufferFlags.StructuredBuffer, ResourceUsage.Default); desc.StructureByteStride = Unsafe.SizeOf<MyStruct>();
Defensive patterns
Strategy: validation
Validate before calling
if (desc.BufferFlags.HasFlag(BufferFlags.StructuredBuffer) && desc.StructureByteStride <= 0)
throw new InvalidOperationException("StructureByteStride must be > 0 for StructuredBuffer"); Type guard
static bool IsValidStructuredBuffer(in BufferDescription d) => !d.BufferFlags.HasFlag(BufferFlags.StructuredBuffer) || d.StructureByteStride > 0;
Try / catch
try { buffer = Buffer.New(device, desc); }
catch (ArgumentException ex) when (ex.Message.Contains("structured buffer")) { desc.StructureByteStride = elementSize; buffer = Buffer.New(device, desc); } Prevention
- Always construct structured buffers through a helper that takes the element type and sets StructureByteStride = sizeOf<T>().
- Remember BufferDescription's default StructureByteStride is 0 — set it explicitly.
- Assert stride equals the shader-side struct size to avoid silent mismatches.
When it happens
Trigger: Creating or recreating a Buffer with BufferFlags.StructuredBuffer while BufferDescription.StructureByteStride is 0 (default) or negative — e.g. building the description manually and forgetting to set StructureByteStride, or using a helper overload that doesn't take an element size.
Common situations: Porting D3D11 code where the stride was inferred from a typed format, copy-pasting a byte-buffer description and adding the StructuredBuffer flag, or serializing a description where the stride field defaulted to 0.
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
- The length of the destination data buffer is larger than…
- The length of the source data to upload is larger than the…
- Offsets are only supported for Textures declared with
- Element size must be set to sizeof(short) = 2 or…
- size needs to be a multiple of constant buffer alignment
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/48a3e10ecdc4ffe0.
Report an issue: GitHub.
Appendix: source
Thrown at sources/engine/Stride.Graphics/Direct3D12/Buffer.Direct3D12.cs:175
var bufferFlags = bufferDescription.BufferFlags;
var desiredResourceState = ResourceStates.Common;
if (bufferFlags.HasFlag(BufferFlags.ConstantBuffer))
desiredResourceState |= ResourceStates.VertexAndConstantBuffer;
if (bufferFlags.HasFlag(BufferFlags.IndexBuffer))
desiredResourceState |= ResourceStates.IndexBuffer;
if (bufferFlags.HasFlag(BufferFlags.VertexBuffer))
desiredResourceState |= ResourceStates.VertexAndConstantBuffer;
if (bufferFlags.HasFlag(BufferFlags.ShaderResource))
desiredResourceState |= ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource;
if (bufferFlags.HasFlag(BufferFlags.StructuredBuffer))
{
if (bufferDescription.StructureByteStride <= 0)
throw new ArgumentException("Element size cannot be less or equal 0 for structured buffer");
}
if (bufferFlags.HasFlag(BufferFlags.ArgumentBuffer))
desiredResourceState |= ResourceStates.IndirectArgument;
var heapType = HeapType.Default;
IsHostVisibleHeap = false;
if (Usage == GraphicsResourceUsage.Staging)
{
// Per our own definition of staging resource (read-back only)
if (hasInitData)
throw new InvalidOperationException("D3D12: Staging buffers can't be created with initial data.");
heapType = HeapType.Readback;
desiredResourceState = ResourceStates.CopyDest;
IsHostVisibleHeap = true;
}
else if (Usage == GraphicsResourceUsage.Dynamic)View on GitHub (pinned to 96fad776d2)