{"record":{"id":"18cf90d505474cd7","repo":"dotnet/wpf","slug":"sr-effect-shaderbytecodesize","errorCode":null,"errorMessage":"SR.Effect_ShaderBytecodeSize","messagePattern":"SR\\.Effect_ShaderBytecodeSize","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/PixelShader.cs","lineNumber":130,"sourceCode":"        /// Reads the byte code for the pixel shader into a local byte array. If the stream is null, the byte array\n        /// will be empty (length 0). The compositor will use an identity shader.\n        /// </summary>\n        private void LoadPixelShaderFromStreamIntoMemory(Stream source) \n        {\n            _shaderBytecode = null;\n\n            if (source != null)\n            {\n                if (!source.CanSeek)\n                {\n                    throw new InvalidOperationException(SR.Effect_ShaderSeekableStream);\n                }\n\n                int len = (int)source.Length;  // only works on seekable streams.\n\n                if (len % sizeof(int) != 0)\n                {\n                    throw new InvalidOperationException(SR.Effect_ShaderBytecodeSize);\n                }\n                \n                BinaryReader br = new BinaryReader(source);\n                _shaderBytecode = new byte[len];\n                int lengthRead = br.Read(_shaderBytecode, 0, (int)len);\n\n                //\n                // The first 4 bytes contain version info in the form of\n                // [Minor][Major][xx][xx]\n                //\n                if (_shaderBytecode != null && _shaderBytecode.Length > 3)\n                {\n                    ShaderMajorVersion = _shaderBytecode[1];\n                    ShaderMinorVersion = _shaderBytecode[0];\n                }\n                else\n                {\n                    ShaderMajorVersion = ShaderMinorVersion = 0;","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/PixelShader.cs#L112-L148","documentation":"PixelShader.LoadPixelShaderFromStreamIntoMemory reads the stream length and requires the shader bytecode length to be a multiple of sizeof(int) (4 bytes). If the seekable stream's length is not 4-byte aligned, an InvalidOperationException with Effect_ShaderBytecodeSize is thrown. HLSL bytecode is always emitted in 4-byte-aligned chunks, so a non-aligned stream is corrupt or not real shader bytecode.","triggerScenarios":"Setting PixelShader.UriSource to a resource whose content is not valid ps/pshader bytecode, or assigning a StreamSource whose length is not divisible by 4 (truncated file, wrong file, text file, partially downloaded payload) before reading into _shaderBytecode.","commonSituations":"Pointing UriSource at a .fx source file instead of the compiled .ps bytecode; a build/packaging step truncating the shader resource; loading a shader from a network stream that was cut off; copying the shader file with a wrong size.","solutions":["Compile the HLSL with fxc into pixel-shader bytecode (e.g. /T ps_2_0) and embed that .ps file, not the .fx source.","Verify the stream length is a multiple of 4: if (stream.Length % 4 != 0) reject/repair it before assigning to PixelShader.StreamSource.","Ensure the resource build action (Resource) and copy settings produce the full untruncated bytecode in the assembly.","Check the shader file loads completely (seek to end, check Length) and is not empty or corrupt."],"exampleFix":"// before\npixelShader.UriSource = new Uri(\"pack://application:,,,/shaders/effect.fx\");\n// after\npixelShader.UriSource = new Uri(\"pack://application:,,,/shaders/effect.ps\"); // compiled fxc output, 4-byte aligned","handlingStrategy":"validation","validationCode":"using (var s = File.OpenRead(shaderPath))\n{\n    if (!s.CanSeek || s.Length == 0 || s.Length % 4 != 0)\n        throw new InvalidOperationException(\"Shader stream is not valid 4-byte-aligned bytecode: \" + shaderPath);\n}\npixelShader.UriSource = new Uri(\"pack://application:,,,/shaders/effect.ps\");","typeGuard":"static bool IsValidShaderStream(Stream s) => s != null && s.CanSeek && s.Length > 0 && s.Length % sizeof(int) == 0;","tryCatchPattern":null,"preventionTips":["Always compile HLSL with fxc (ps_2_0/ps_3_0 targets) and ship only the .ps bytecode.","Set shader resources' Build Action to Resource so they are embedded intact.","Add a unit test asserting every embedded shader resource length % 4 == 0."],"tags":["wpf","pixelshader","hlsl","invalid-bytecode"],"backgroundTag":"invalid-argument-format","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"}