{"record":{"id":"d3035e825213105e","repo":"stride3d/stride","slug":"a-negative-number-of-mip-maps-is-not-allowed","errorCode":null,"errorMessage":"A negative number of mip maps is not allowed.","messagePattern":"A negative number of mip maps is not allowed\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Video/VideoTexture.cs","lineNumber":39,"sourceCode":"        /// Effect used to create mipmap inside the <see cref=\"renderTargetTexture\"/>\n        /// </summary>\n        private EffectInstance effectTexture2DCopy;\n\n        private Texture originalTargetTexture;\n        private Texture renderTargetTexture;\n\n        private SamplerState minMagLinearMipPointSampler;\n\n        public VideoTexture(GraphicsDevice graphicsDevice, IServiceRegistry serviceRegistry, int width, int height, int maxMipMapCount)\n        {\n            if (width <= 0 || height <= 0)\n            {\n                throw new InvalidOperationException(\"Invalid video resolution.\");\n            }\n\n            if (maxMipMapCount < 0)\n            {\n                throw new InvalidOperationException(\"A negative number of mip maps is not allowed.\");\n            }\n\n            var effectSystem = serviceRegistry.GetSafeServiceAs<EffectSystem>();\n\n            // We want to sample mip maps using point filtering (to make sure nothing bleeds between mip maps).\n            minMagLinearMipPointSampler = SamplerState.New(graphicsDevice, new SamplerStateDescription(TextureFilter.MinMagLinearMipPoint, TextureAddressMode.Clamp));\n\n            // Allocate the effect for copying decoder output texture to our normal render texture\n            effectDecoderTextureCopy = new EffectInstance(effectSystem.LoadEffect(\"SpriteEffectExtTexture\").WaitForResult());\n            effectDecoderTextureCopy.Parameters.Set(SpriteEffectExtTextureKeys.Gamma, 2.2f);\n            effectDecoderTextureCopy.Parameters.Set(SpriteEffectExtTextureKeys.MipLevel, 0.0f);\n            effectDecoderTextureCopy.Parameters.Set(SpriteEffectExtTextureKeys.Sampler, minMagLinearMipPointSampler);\n            effectDecoderTextureCopy.UpdateEffect(graphicsDevice);\n\n            // Allocate the effect for copying regular 2d textures:\n            effectTexture2DCopy = new EffectInstance(effectSystem.LoadEffect(\"SpriteEffectExtTextureRegular\").WaitForResult());\n            effectTexture2DCopy.Parameters.Set(SpriteEffectExtTextureKeys.MipLevel, 0.0f);\n            effectTexture2DCopy.Parameters.Set(SpriteEffectExtTextureRegularKeys.Sampler, minMagLinearMipPointSampler);","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Video/VideoTexture.cs#L21-L57","documentation":"The VideoTexture constructor rejects a negative maxMipMapCount because a mip chain cannot have fewer than zero extra levels. The value is passed through to texture/sampler setup, so the constructor fails fast with InvalidOperationException when it is negative.","triggerScenarios":"Constructing VideoTexture (directly or via VideoInstance.AllocateVideoTexture) with maxMipMapCount < 0, e.g. from videoComponent.MaxMipMapCount being set to -1 in code or a component property.","commonSituations":"Typo or misunderstanding of the convention (using -1 to mean 'auto'); loading a component asset where MaxMipMapCount was serialized as -1; copying a config value that another engine treats as 'unlimited'.","solutions":["Set videoComponent.MaxMipMapCount to 0 or a positive value before allocating the texture.","If 'auto mipmaps' was intended, pass 0 (or the maximum) rather than -1, per this API's convention.","Audit serialized assets/prefabs containing the video component for a -1 MaxMipMapCount and fix them."],"exampleFix":"// before\nvideoComponent.MaxMipMapCount = -1; // 'auto' (wrong convention)\nvideoInstance.AllocateVideoTexture(w, h); // throws\n\n// after\nvideoComponent.MaxMipMapCount = 0;\nvideoInstance.AllocateVideoTexture(w, h);","handlingStrategy":"validation","validationCode":"// C#\nif (maxMipMapCount < 0)\n    maxMipMapCount = 0; // clamp before allocating\nvideoInstance.AllocateVideoTexture(width, height);","typeGuard":null,"tryCatchPattern":"// C#\ntry { videoInstance.AllocateVideoTexture(w, h); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"mip maps\"))\n{\n    // fix videoComponent.MaxMipMapCount then retry\n}","preventionTips":["Never use -1 as 'auto' for MaxMipMapCount; use 0 or a positive count","Clamp mip count to Math.Max(0, value) before allocation","Audit serialized video component assets for negative MaxMipMapCount"],"tags":["validation","video","texture","argument-out-of-range"],"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-23T08:17:48.524Z"}