{"record":{"id":"806a27d7432fbb94","repo":"dotnet/wpf","slug":"sr-effect-shadersamplertype","errorCode":null,"errorMessage":"SR.Effect_ShaderSamplerType","messagePattern":"SR\\.Effect_ShaderSamplerType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/ShaderEffect.cs","lineNumber":423,"sourceCode":"        // form that the HLSL shaders want, and stores that value, since it will\n        // be sent on every update.\n        // We WritePreamble/Postscript here since this method is called by the user with the callback \n        // created in PixelShaderSamplerCallback.\n        private void UpdateShaderSampler(DependencyProperty dp, object newValue, int registerIndex, SamplingMode samplingMode)\n        {\n            WritePreamble();\n\n            if (newValue != null)\n            {\n                if (newValue is not VisualBrush\n                    and not BitmapCacheBrush\n                    and not ImplicitInputBrush\n                    and not ImageBrush)\n                {\n                    // Note that if the type of the brush is ImplicitInputBrush and the value is non null, the value is actually\n                    // Effect.ImplicitInput. This is because ImplicitInputBrush is internal and the user can only get to the singleton\n                    // Effect.ImplicitInput.\n                    throw new ArgumentException(SR.Effect_ShaderSamplerType, nameof(dp));\n                }\n            }\n\n            //\n            // Treat as ps_2_0 by default\n            //\n            int registerMax = PS_2_0_SAMPLER_LIMIT;\n            string srid = nameof(SR.Effect_Shader20SamplerRegisterLimit);\n\n            if (PixelShader != null && PixelShader.ShaderMajorVersion >= 3)\n            {\n                registerMax = PS_3_0_SAMPLER_LIMIT;\n                srid = nameof(SR.Effect_Shader30SamplerRegisterLimit);\n            }\n\n            if (registerIndex >= registerMax || registerIndex < 0)\n            {\n                throw new ArgumentException(SR.GetResourceString(srid));","sourceCodeStart":405,"sourceCodeEnd":441,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/ShaderEffect.cs#L405-L441","documentation":"UpdateShaderSampler validates that the value bound via PixelShaderSamplerCallback is a Brush the sampler pipeline supports: null, ImplicitInput, or specific supported brush types (e.g. not an arbitrary unsupported Brush such as a non-image/implicit brush type). If the property value is a Brush of an unsupported type (and not ImplicitInputBrush/ImageBrush-supported), ArgumentException with Effect_ShaderSamplerType is thrown.","triggerScenarios":"Assigning an unsupported Brush-derived type (e.g. VisualBrush, DrawingBrush, custom Brush) to a sampler-mapped dependency property that uses PixelShaderSamplerCallback.","commonSituations":"Using a VisualBrush to feed a shader sampler; passing a custom brush class; using a brush type not supported by the effect sampler pipeline in the target WPF version.","solutions":["Use ImageBrush with a valid BitmapImage/RenderTargetBitmap source for the sampler input.","Use Effect.ImplicitInput for the element's own rendering, or null.","Render the unsupported brush (e.g. VisualBrush) into a RenderTargetBitmap and assign that as an ImageBrush.","Convert DrawingBrush/other brushes to bitmaps before applying the effect."],"exampleFix":"// before\nsamplerInput = new VisualBrush(targetVisual);\n// after\nvar rtb = new RenderTargetBitmap((int)w, (int)h, 96, 96, PixelFormats.Pbgra32);\nrtb.Render(targetVisual);\nsamplerInput = new ImageBrush(rtb);","handlingStrategy":"validation","validationCode":"if (value is Brush b && b is not ImageBrush && value != Effect.ImplicitInput)\n    // unsupported sampler brush: convert to ImageBrush via RenderTargetBitmap before assigning","typeGuard":"static bool IsSupportedSamplerValue(object v) =>\n    v == null || v == Effect.ImplicitInput || v is ImageBrush;","tryCatchPattern":"try { effect.InputBrush = brush; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"sampler\"))\n{\n    effect.InputBrush = RenderBrushToImageBrush(brush); // RenderTargetBitmap fallback\n}","preventionTips":["Only assign ImageBrush (or Effect.ImplicitInput / null) to sampler properties.","Convert VisualBrush/DrawingBrush to RenderTargetBitmap-backed ImageBrush first.","Avoid custom Brush subclasses as shader sampler inputs."],"tags":["wpf","shadereffect","sampler","brush"],"backgroundTag":"type-mismatch","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"}