{"record":{"id":"2d10172ac877a82c","repo":"stride3d/stride","slug":"can-t-serialize-a-nameof-texture-back-as-a-nameof","errorCode":null,"errorMessage":"Can't serialize a {nameof(Texture)} back as a {nameof(RenderTextureDescriptor)}","messagePattern":"Can't serialize a (.+?) back as a (.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Rendering/Rendering/RenderTextures/RenderTextureDescriptor.cs","lineNumber":95,"sourceCode":"                        break;\n                    case RenderTextureFormat.HDR:\n                        pixelFormat = PixelFormat.R16G16B16A16_Float;\n                        break;\n                    default:\n                        throw new ArgumentOutOfRangeException();\n                }\n\n                // Find color space\n                // If linear, use sRGB\n                if (renderTextureDescriptor.ColorSpace == ColorSpace.Linear)\n                    pixelFormat = pixelFormat.ToSRgb();\n\n                var textureDescription = TextureDescription.New2D(renderTextureDescriptor.Width, renderTextureDescriptor.Height, pixelFormat, TextureFlags.ShaderResource | TextureFlags.RenderTarget);\n                texture.InitializeFrom(textureDescription, new TextureViewDescription());\n            }\n            else\n            {\n                throw new NotSupportedException($\"Can't serialize a {nameof(Texture)} back as a {nameof(RenderTextureDescriptor)}\");\n            }\n        }\n\n        public override object Construct(ContentSerializerContext context)\n        {\n            return new Texture();\n        }\n    }\n}\n","sourceCodeStart":77,"sourceCodeEnd":105,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Rendering/Rendering/RenderTextures/RenderTextureDescriptor.cs#L77-L105","documentation":"RenderTextureDescriptor is a serializable description of a render texture, not a live GPU resource. During serialization Stride requires the object in this slot to be the descriptor itself; if the field actually holds a live Texture instance (deserialized via Construct, which returns new Texture()), the serializer cannot map it back and throws NotSupportedException to signal the round-trip is impossible.","triggerScenarios":"Assigning a loaded/instantiated Texture object to a property or field declared as RenderTextureDescriptor (or to a RenderTexture's descriptor slot) and then serializing it; e.g. code that fetches a Texture from a content manager or GraphicsDevice and stores it where the descriptor was expected.","commonSituations":"Confusing Texture with RenderTextureDescriptor when wiring up render textures in scenes; editor code replacing a descriptor with a runtime-created texture; custom entity components that keep a Texture reference but are serialized as descriptors.","solutions":["Store the RenderTextureDescriptor (width/height/format/flags) in serialized fields, not a Texture instance","If you need the actual texture, reference it via a Texture asset reference (Content) instead of a descriptor property","Check code that overwrites the descriptor with a Texture at runtime; keep them in separate fields and exclude the Texture from serialization","If creating render textures dynamically, construct a RenderTextureDescriptor from the texture's Description instead of serializing the texture"],"exampleFix":"// before\npublic RenderTextureDescriptor Descriptor { get; set; }\nmyComponent.Descriptor = someTexture; // NotSupportedException on serialize\n// after\nmyComponent.Descriptor = new RenderTextureDescriptor(someTexture.Width, someTexture.Height, someTexture.Format, TextureFlags.ShaderResource | TextureFlags.RenderTarget);","handlingStrategy":"validation","validationCode":"if (value is Texture)\n    throw new ArgumentException(\"Store a RenderTextureDescriptor here, not a Texture instance.\");","typeGuard":"bool IsDescriptor(object o) => o is RenderTextureDescriptor && o is not Texture;","tryCatchPattern":"try\n{\n    serializer.Serialize(stream, descriptor);\n}\ncatch (NotSupportedException)\n{\n    // rebuild descriptor from texture\n    descriptor = new RenderTextureDescriptor(tex.Width, tex.Height, tex.Format, TextureFlags.ShaderResource);\n}","preventionTips":["Keep serialized fields typed as RenderTextureDescriptor; hold live Textures in [NonSerialized]/separate runtime fields","Never assign Content-loaded textures into descriptor-typed properties","Write a unit test that round-trips serialized components"],"tags":["serialization","rendering","textures"],"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"}