stride3d/stride · error · InvalidOperationException

Unknown resource type

Error message

Unknown resource type

What it means

UpdateSubresource builds a full-size ResourceRegion by pattern-matching the resource against Texture and Buffer. Any other GraphicsResource-derived type (or null outside those cases) falls to the discard arm and throws InvalidOperationException('Unknown resource type').

Solutions

  1. Ensure the resource passed is a Texture or Buffer instance.
  2. If a new resource type was added, extend the switch expression to map it to a ResourceRegion.
  3. Verify with a type check before the call and route other resource kinds to the appropriate update API.
  4. Null-check the resource; a null fails the type patterns and lands in the discard arm.

Example fix

// before
commandList.UpdateSubresource(customResource, 0, data); // throws
// after
if (resource is Texture t) commandList.UpdateSubresource(t, 0, data);
else if (resource is Buffer b) commandList.UpdateSubresource(b, 0, data);
Defensive patterns

Strategy: type-guard

Validate before calling

if (resource is not Texture && resource is not Buffer)
    throw new ArgumentException("UpdateSubresource requires Texture or Buffer", nameof(resource));

Type guard

static bool IsUpdatable(GraphicsResource r) => r is Texture or Buffer;

Try / catch

try { commandList.UpdateSubresource(resource, 0, data); }
catch (InvalidOperationException ex) { log.Error("Unsupported resource type for update", ex); }

Prevention

When it happens

Trigger: Calling CommandList.UpdateSubresource (or the overload that computes the region) with a resource that is neither Texture nor Buffer, e.g. a custom GraphicsResource subclass or a null/other resource on the Direct3D12 backend.

Common situations: Passing a QueryPool, custom resource wrapper, or wrong-typed variable to UpdateSubresource; refactor introduced a new resource kind not handled by the switch.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at sources/engine/Stride.Graphics/Direct3D12/CommandList.Direct3D12.cs:1926

        /// <summary>
        ///   Copies data from memory to a sub-resource created in non-mappable memory.
        /// </summary>
        /// <param name="resource">The destination Graphics Resource to copy data to.</param>
        /// <param name="subResourceIndex">The sub-resource index of <paramref name="resource"/> to copy data to.</param>
        /// <param name="sourceData">The source data in CPU memory to copy.</param>
        /// <exception cref="ArgumentNullException"><paramref name="resource"/> is <see langword="null"/>.</exception>
        /// <exception cref="InvalidOperationException">
        ///   Only <see cref="Texture"/>s and <see cref="Buffer"/>s are supported.
        /// </exception>
        /// <inheritdoc cref="UpdateSubResource(GraphicsResource, int, ReadOnlySpan{byte})" path="/remarks" />
        internal void UpdateSubResource(GraphicsResource resource, int subResourceIndex, DataBox sourceData)
        {
            ResourceRegion region = resource switch
            {
                Texture texture => new ResourceRegion(left: 0, top: 0, front: 0, texture.Width, texture.Height, texture.Depth),
                Buffer buffer => new ResourceRegion(left: 0, top: 0, front: 0, buffer.SizeInBytes, bottom: 1, back: 1),

                _ => throw new InvalidOperationException("Unknown resource type")
            };

            UpdateSubResource(resource, subResourceIndex, sourceData, region);
        }

        /// <summary>
        ///   Copies data from memory to a sub-resource created in non-mappable memory.
        /// </summary>
        /// <param name="resource">The destination Graphics Resource to copy data to.</param>
        /// <param name="subResourceIndex">The sub-resource index of <paramref name="resource"/> to copy data to.</param>
        /// <param name="sourceData">The source data in CPU memory to copy.</param>
        /// <param name="region">
        ///   <para>
        ///     A <see cref="ResourceRegion"/> that defines the portion of the destination sub-resource to copy the resource data into.
        ///     Coordinates are in bytes for Buffers and in texels for Textures.
        ///     The dimensions of the source must fit the destination.
        ///   </para>
        ///   <para>

View on GitHub (pinned to 96fad776d2)