{"record":{"id":"b2bd1a249ac9c652","repo":"stride3d/stride","slug":"cross-contextparsespirv-context-w-nuint-words-length-ir","errorCode":null,"errorMessage":"{cross.ContextParseSpirv(context, w, (nuint)Words.Length, &ir)} : Could not parse spirv","messagePattern":"(.+?) : Could not parse spirv","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"sources/shaders/Stride.Shaders.Compilers/SpirvTranslator.cs","lineNumber":35,"sourceCode":"    static readonly Cross cross;\n    static readonly Logger Log = GlobalLogger.GetLogger(\"SpirvTranslator\");\n\n    static SpirvTranslator()\n    {\n        NativeLibraryHelper.PreloadLibrary(\"spirv-cross\", typeof(SpirvTranslator));\n        cross = Cross.GetApi();\n    }\n\n    public List<(string RealName, string TranslatedName, ExecutionModel ExecutionModel)> GetEntryPoints(Backend backend = Backend.Hlsl)\n    {\n        Context* context = null;\n        ParsedIr* ir = null;\n        Compiler* compiler = null;\n        if (cross.ContextCreate(&context) != Result.Success)\n            throw new Exception($\"{cross.ContextCreate(&context)} : Could not create spirv context\");\n        fixed (uint* w = Words.Span)\n            if (cross.ContextParseSpirv(context, w, (nuint)Words.Length, &ir) != Result.Success)\n                throw new Exception($\"{cross.ContextParseSpirv(context, w, (nuint)Words.Length, &ir)} : Could not parse spirv\");\n\n        cross.ContextSetErrorCallback(context, new((void* userData, byte* errorData) =>\n        {\n            var error = Marshal.PtrToStringAnsi((IntPtr)errorData);\n            if (error != null)\n                Log.Error(error);\n        }), null);\n\n        if (cross.ContextCreateCompiler(context, backend, ir, CaptureMode.Copy, &compiler) != Result.Success)\n            throw new Exception($\"{cross.ContextCreateCompiler(context, backend, ir, CaptureMode.Copy, &compiler)} : could not create compiler\");\n\n        var result = new List<(string RealName, string TranslatedName, ExecutionModel ExecutionModel)>();\n        EntryPoint* entry_points = null;\n        nuint num_entry_points = 0;\n        cross.CompilerGetEntryPoints(compiler, &entry_points, &num_entry_points);\n        for (int i = 0; i < (int)num_entry_points; ++i)\n        {\n            var entryPointModel = entry_points[i].ExecutionModel;","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Compilers/SpirvTranslator.cs#L17-L53","documentation":"After the context is created, GetEntryPoints parses the stored SPIR-V words via cross.ContextParseSpirv. If the parse fails (invalid magic number, truncated module, unsupported SPIR-V version), the translator throws with the raw Result code and \"Could not parse spirv\". This means the bytecode buffer handed to SpirvTranslator is not a valid SPIR-V module.","triggerScenarios":"Calling GetEntryPoints on a SpirvTranslator whose Words.Span does not contain valid SPIR-V: buffer written in wrong endianness/offset, HLSL/DXBC or DXIL bytes passed instead of SPIR-V, truncated buffer, or SPIR-V version newer than the bundled parser.","commonSituations":"Feeding translated-then-saved shader blobs from an incompatible backend; cache corruption; passing a shader bytecode buffer produced for a different API (e.g. DXBC) into the SPIR-V translator.","solutions":["Validate the buffer starts with the SPIR-V magic number 0x07230203 and is properly word-aligned before constructing the translator.","Confirm the buffer actually contains SPIR-V (not DXBC/DXIL/other) and was produced by a compatible SPIR-V frontend version.","Re-export/recompile the shader to regenerate a complete, untruncated SPIR-V module; clear possibly corrupt caches.","Check the numeric Result code in the message against SPIRV-Cross parse error codes for the specific cause."],"exampleFix":"// before\nvar translator = new SpirvTranslator(dxbcBytes); // wrong format\nvar entries = translator.GetEntryPoints(); // 'Could not parse spirv'\n// after\nif (BitConverter.ToUInt32(spirvBytes, 0) != 0x07230203u)\n    throw new InvalidDataException(\"Not SPIR-V\");\nvar translator = new SpirvTranslator(spirvBytes);\nvar entries = translator.GetEntryPoints();","handlingStrategy":"validation","validationCode":"static bool LooksLikeSpirv(ReadOnlySpan<uint> words) =>\n    words.Length >= 5 && words[0] == 0x07230203u; // magic number","typeGuard":"if (!LooksLikeSpirv(words))\n    throw new InvalidDataException(\"Buffer is not a SPIR-V module (bad magic number)\");","tryCatchPattern":"try { var eps = translator.GetEntryPoints(); }\ncatch (Exception ex) when (ex.Message.Contains(\"Could not parse spirv\")) {\n    InvalidateShaderCache(blobId); // discard corrupt/unsupported module\n}","preventionTips":["Check the SPIR-V magic number 0x07230203 before constructing the translator.","Never feed DXBC/DXIL/HLSL bytecode into the SPIR-V translator.","Version-stamp shader caches so blobs from incompatible compiler versions are regenerated."],"tags":["spirv","invalid-binary","native-interop"],"backgroundTag":"invalid-argument-format","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}