{"record":{"id":"29bf15af944a5d50","repo":"stride3d/stride","slug":"a-region-can-only-be-specified-for-textures-with","errorCode":null,"errorMessage":"A region can only be specified for Textures with GraphicsResourceUsage.Default","messagePattern":"A region can only be specified for Textures with GraphicsResourceUsage\\.Default","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/Texture.cs","lineNumber":1452,"sourceCode":"        ///   This can also occur when the stride is different from the optimal stride, and <typeparamref name=\"TData\"/> is not the same largestSize as\n        ///   the largestSize of <see cref=\"Format\"/>.\n        /// </exception>\n        /// <exception cref=\"ArgumentException\">\n        ///   <paramref name=\"region\"/> can only be specified (non-<see langword=\"null\"/>) for Textures with <see cref=\"GraphicsResourceUsage.Default\"/>.\n        /// </exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\">\n        ///   The <paramref name=\"region\"/> largestSize (in any of its dimensions) cannot be greater than the mip-level largestSize.\n        /// </exception>\n        /// <remarks>\n        ///   See <see cref=\"CommandList.MapSubResource\"/> and <see cref=\"CommandList.UpdateSubResource\"/> for more information about\n        ///   usage and restrictions.\n        /// </remarks>\n        public unsafe void SetData<TData>(CommandList commandList, ReadOnlySpan<TData> fromData, int arrayIndex = 0, int mipLevel = 0, ResourceRegion? region = null) where TData : unmanaged\n        {\n            ArgumentNullException.ThrowIfNull(commandList);\n\n            if (region.HasValue && Usage != GraphicsResourceUsage.Default)\n                throw new ArgumentException($\"A region can only be specified for Textures with {nameof(GraphicsResourceUsage)}.{nameof(GraphicsResourceUsage.Default)}\", nameof(region));\n\n            // Get a description for the specified mip-level\n            ref readonly var mipmap = ref GetMipMapDescription(mipLevel);\n\n            int width = mipmap.Width;\n            int height = mipmap.Height;\n            int depth = mipmap.Depth;\n\n            // If we are using a region, then check that parameters are fine\n            if (region is ResourceRegion regionToCheck)\n            {\n                if (regionToCheck.Width > width)\n                    throw new ArgumentOutOfRangeException(nameof(region), $\"The region's width [{regionToCheck.Width}] cannot be greater than the mip-level's width [{width}]\");\n                if (regionToCheck.Height > height)\n                    throw new ArgumentOutOfRangeException(nameof(region), $\"The region's height [{regionToCheck.Height}] cannot be greater than the mip-level's height [{height}]\");\n                if (regionToCheck.Depth > depth)\n                    throw new ArgumentOutOfRangeException(nameof(region), $\"The region's depth [{regionToCheck.Depth}] cannot be greater than the mip-level's depth [{depth}]\");\n","sourceCodeStart":1434,"sourceCodeEnd":1470,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/Texture.cs#L1434-L1470","documentation":"Texture.SetData allows partial updates via a ResourceRegion only when the texture usage is GraphicsResourceUsage.Default (GPU-side updatable). For other usages (e.g. Immutable, which is uploaded via staging copies), specifying a region is invalid and ArgumentException is thrown with 'region' as the parameter name.","triggerScenarios":"Calling texture.SetData(commandList, data, region: someRegion) on a texture created with GraphicsResourceUsage.Immutable or Dynamic; partial sub-rectangle updates on upload-only textures.","commonSituations":"Updating a sub-rectangle of a texture that was created immutable; porting dynamic-atlas code without changing texture usage; copy-pasted SetData calls that pass a non-null region.","solutions":["Recreate the texture with GraphicsResourceUsage.Default if you need region updates.","Drop the region argument and upload the full mip level for non-Default usages.","Use a Dynamic/Staging texture with appropriate copy commands for streaming updates."],"exampleFix":"// before\nvar tex = Texture.New2D(device, w, h, fmt, TextureFlags.ShaderResource, usage: GraphicsResourceUsage.Immutable);\ntex.SetData(commandList, data, region: new ResourceRegion(0, 0, 0, 16, 16, 1)); // throws\n// after\nvar tex = Texture.New2D(device, w, h, fmt, TextureFlags.ShaderResource, usage: GraphicsResourceUsage.Default);\ntex.SetData(commandList, data, region: new ResourceRegion(0, 0, 0, 16, 16, 1));","handlingStrategy":"validation","validationCode":"if (region.HasValue && texture.Usage != GraphicsResourceUsage.Default)\n    throw new InvalidOperationException(\"Region updates require GraphicsResourceUsage.Default\");","typeGuard":"static bool SupportsRegionUpdate(Texture t) => t.Usage == GraphicsResourceUsage.Default;","tryCatchPattern":"try { tex.SetData(commandList, data, region: r); }\ncatch (ArgumentException ex)\n{\n    logger.LogError(ex, \"Region update on usage {Usage}\", tex.Usage);\n    tex.SetData(commandList, data); // full-mip fallback\n}","preventionTips":["Create textures with GraphicsResourceUsage.Default when you plan partial updates.","Omit the region parameter for Immutable/Dynamic textures.","Wrap update helpers that assert usage before calling SetData."],"tags":["graphics","stride","setdata","resource-usage"],"backgroundTag":"unsupported-operation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}