{"record":{"id":"a3a79f87a52b978e","repo":"stride3d/stride","slug":"element-size-must-be-set-to-sizeof-short-2-or-sizeof-int-4","errorCode":null,"errorMessage":"Element size must be set to sizeof(short) = 2 or sizeof(int) = 4 for Index Buffers if bound as a Shader Resource","messagePattern":"Element size must be set to sizeof\\(short\\) = 2 or sizeof\\(int\\) = 4 for Index Buffers if bound as a Shader Resource","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/Buffer.cs","lineNumber":884,"sourceCode":"        ///     <item>For other types of Buffers, this can be set to 0.</item>\n        ///   </list>\n        /// </param>\n        /// <param name=\"viewFormat\">\n        ///   View format used if the Buffer is used as a Shader Resource View,\n        ///   or <see cref=\"PixelFormat.None\"/> if not.\n        /// </param>\n        /// <returns>The proposed <see cref=\"PixelFormat\"/> to use.</returns>\n        /// <exception cref=\"ArgumentException\">\n        ///   The <see cref=\"Buffer\"/> is an <strong>Index Buffer</strong> that will be bound as a <em>Shader Resource</em>,\n        ///   but the <paramref name=\"elementSize\"/> is neither 2 bytes (<c>sizeof(short)</c>) nor 4 bytes (<c>sizeof(int)</c>).\n        /// </exception>\n        private static PixelFormat CheckPixelFormat(BufferFlags bufferFlags, int elementSize, PixelFormat viewFormat)\n        {\n            if (!bufferFlags.HasFlag(BufferFlags.IndexBuffer) || !bufferFlags.HasFlag(BufferFlags.ShaderResource))\n                return viewFormat;\n\n            if (elementSize != 2 && elementSize != 4)\n                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));\n\n            return elementSize == 2 ? PixelFormat.R16_UInt : PixelFormat.R32_UInt;\n        }\n\n        /// <summary>\n        ///   Composes a new <see cref=\"BufferDescription\"/> structure with the provided options.\n        /// </summary>\n        /// <param name=\"bufferSize\">The size in bytes of the Buffer.</param>\n        /// <param name=\"elementSize\">The size in bytes of each element (in case of a <strong>Structured Buffer</strong>).</param>\n        /// <param name=\"bufferFlags\">The buffer flags to specify the type of Buffer.</param>\n        /// <param name=\"usage\">The usage for the Buffer, which determines who can read/write data.</param>\n        /// <returns>A new <see cref=\"BufferDescription\"/>.</returns>\n        private static BufferDescription NewDescription(int bufferSize, int elementSize, BufferFlags bufferFlags, GraphicsResourceUsage usage)\n        {\n            return new BufferDescription\n            {\n                SizeInBytes = bufferSize,\n                StructureByteStride = bufferFlags.HasFlag(BufferFlags.StructuredBuffer) ? elementSize : 0,","sourceCodeStart":866,"sourceCodeEnd":902,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/Buffer.cs#L866-L902","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use ushort (2 bytes) or uint (4 bytes) index elements when the buffer must also be a ShaderResource.","Convert the index data to 16- or 32-bit integers before creating the buffer.","Remove BufferFlags.ShaderResource from the buffer description if GPU reading is not needed."],"exampleFix":"// before\nvar indices = new byte[] { 0, 1, 2, 2, 1, 3 };\nBuffer.New(device, indices, BufferFlags.IndexBuffer | BufferFlags.ShaderResource); // throws\n// after\nvar indices = new ushort[] { 0, 1, 2, 2, 1, 3 };\nBuffer.New(device, indices, BufferFlags.IndexBuffer | BufferFlags.ShaderResource);","handlingStrategy":"validation","validationCode":"bool valid = !flags.HasFlag(BufferFlags.ShaderResource)\n    || elementSize == sizeof(short) || elementSize == sizeof(int);","typeGuard":"bool IsValidIndexSrvElement(int elementSize) => elementSize is 2 or 4;","tryCatchPattern":"try { Buffer.New(device, indices, flags); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Element size must be set\"))\n{\n    indices = ConvertToUShortIndices(indices);\n    Buffer.New(device, indices, flags);\n}","preventionTips":["Use ushort/uint indices when binding as SRV.","Don't add ShaderResource flag unless reading on GPU.","Document the index element type with the buffer."],"tags":["graphics","buffer","index-buffer","argument-validation"],"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"}