{"record":{"id":"2b115c7091c586af","repo":"stride3d/stride","slug":"cross-contextcreate-context-could-not-create-spirv-context","errorCode":null,"errorMessage":"{cross.ContextCreate(&context)} : Could not create spirv context","messagePattern":"(.+?) : Could not create spirv context","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"sources/shaders/Stride.Shaders.Compilers/SpirvTranslator.cs","lineNumber":32,"sourceCode":"\npublic unsafe record struct SpirvTranslator(ReadOnlyMemory<uint> Words)\n{\n    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);","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Compilers/SpirvTranslator.cs#L14-L50","documentation":"SpirvTranslator.GetEntryPoints first creates a SPIRV-Cross context via cross.ContextCreate. If the native call does not return Result.Success, the translator throws with the raw result code and \"Could not create spirv context\". This is a native library initialization failure — no SPIR-V has been parsed yet, so the problem is with creating the cross-compilation context itself (native state, memory, or bad library initialization).","triggerScenarios":"Calling GetEntryPoints (or any translation entry) when the native SPIRV-Cross library fails to allocate/initialize its context — e.g. native DLL not properly loaded/initialized, incompatible native build, or OOM. The thrown message embeds the numeric Result returned by ContextCreate.","commonSituations":"Missing or mismatched native spirv-cross binary for the platform; running on an architecture without the native dependency; corrupted deployment of native assets.","solutions":["Verify the native SPIRV-Cross library for your platform/architecture ships with the app (correct runtime identifier folder).","Note the numeric Result code in the message and check it against SPIRV-Cross error codes (OOM vs initialization failure).","Reinstall/repair the package that carries the native binaries; ensure no old incompatible version is loaded from PATH.","If transient OOM, reduce concurrent translators or free memory and retry."],"exampleFix":"// before: native deps missing on linux-x64 deployment\ntranslator.GetEntryPoints(); // throws 'Could not create spirv context'\n// after: ensure runtimes/linux-x64/native/libspirv-cross.so is deployed next to the app\ntranslator.GetEntryPoints();","handlingStrategy":"try-catch","validationCode":"// verify native library loads before translation\ntry { _ = NativeLibrary.Load(\"spirv-cross\"); }\ncatch (DllNotFoundException) { EnsureNativeAssetsDeployed(); }","typeGuard":null,"tryCatchPattern":"try { var eps = translator.GetEntryPoints(); }\ncatch (Exception ex) when (ex.Message.Contains(\"Could not create spirv context\")) {\n    throw new InvalidOperationException(\n        \"Native SPIRV-Cross failed to init — check native binaries/Result code in message\", ex);\n}","preventionTips":["Ship native SPIRV-Cross binaries for every RID you deploy.","Run a smoke translation at startup to fail fast on missing native deps.","Log and map the numeric Result code in the message to a diagnosis."],"tags":["native-interop","spirv","initialization-failed"],"backgroundTag":"module-init-failed","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"}