{"record":{"id":"5613d222635a3982","repo":"dotnet/wpf","slug":"sr-mediacontext-nobadshaderhandler","errorCode":null,"errorMessage":"SR.MediaContext_NoBadShaderHandler","messagePattern":"SR\\.MediaContext_NoBadShaderHandler","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaContext.cs","lineNumber":403,"sourceCode":"\n        /// <summary>\n        /// NotifyBadPixelShader - this method is called when the render\n        /// thread has detected a bad pixel shader.  The render thread continues\n        /// to raise this until the problem's been corrected.  This method\n        /// invokes the listeners on the static event in PixelShader.\n        /// </summary>\n        private void NotifyBadPixelShader()\n        {\n            if (InvalidPixelShaderEncountered != null)\n            {\n                InvalidPixelShaderEncountered(null, null);\n            }\n            else\n            {\n                // It's never correct to not have an event handler hooked up in\n                // the case when an invalid shader is encountered.  Raise an\n                // exception directing the app to hook up an event handler.\n                throw new InvalidOperationException(SR.MediaContext_NoBadShaderHandler);\n            }\n        }\n\n        /// <summary>\n        /// The partition this media context is connected to went into\n        /// zombie state. This means either an unhandled batch processing,\n        /// rendering or presentation error and will require us to reconnect.\n        /// </summary>\n        private void NotifyPartitionIsZombie(int failureCode)\n        {\n            //\n            // We only get back these kinds of notification:-\n            // For all OOM cases, we get E_OUTOFMEMORY.\n            // For all OOVM cases, we get D3DERR_OUTOFVIDEOMEMORY and\n            // for all other errors we get WGXERR_UCE_RENDERTHREADFAILURE.\n            //\n\n            switch (failureCode)","sourceCodeStart":385,"sourceCodeEnd":421,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaContext.cs#L385-L421","documentation":"MediaContext.NotifyBadPixelShader throws InvalidOperationException(SR.MediaContext_NoBadPixelShaderHandler) when the render thread reports an invalid/crashed pixel shader but no Application-specific bad-shade handler (MediaContext.RenderCapability... / EffectsLayer invalidation handler) is hooked. WPF requires an event handler to decide what to do when a shader effect is invalid.","triggerScenarios":"Using a BitmapEffect / custom pixel shader (ShaderEffect) that fails validation or crashes on the render thread, while no bad-shader handler is registered, and MediaContext notifies the failure.","commonSituations":"Custom HLSL PS 2.0/3.0 shaders with invalid instruction counts, shaders compiled for unsupported instruction sets, or hardware that fails to run the effect; apps that shipped effects without handling shader failure events.","solutions":["Hook the bad-shader notification handler (System.Windows.Media.RenderCapability tier check / HardwareAcceleration) or ensure Effects are valid PS 2.0/3.0.","Remove or replace the offending ShaderEffect with a software-renderable effect.","Validate the compiled .ps bytecode (instruction count, texture reads) against the target shader profile.","Disable hardware acceleration fallback path by testing with Tier 0 rendering to catch shader problems early."],"exampleFix":"// before\n// No handler; shader crash -> InvalidOperationException\nmyEffect = new MyCustomShaderEffect();\n// after\n// Validate tier / fall back to software effect if shader unsupported\nif (RenderCapability.Tier >> 16 < 2)\n    myEffect = new BlurEffect(); // safe software fallback\nelse\n    myEffect = new MyCustomShaderEffect();","handlingStrategy":"try-catch","validationCode":"// Prefer software-safe effects when hardware tier is low:\nbool hwOk = RenderCapability.Tier >> 16 >= 2;\nif (!hwOk) effect = softwareFallbackEffect;","typeGuard":null,"tryCatchPattern":"try { ApplyShaderEffect(element); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"shader\")) { element.Effect = new BlurEffect(); /* software fallback */ }","preventionTips":["Validate custom .ps shaders (profile, instruction count) in CI before shipping","Hook bad-shader handling rather than relying on defaults","Test effects on Tier 1/0 hardware and in remote sessions"],"tags":["wpf","pixel-shader","event-handler","render-thread"],"backgroundTag":"missing-dependency","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}