{"record":{"id":"0f22bec6cf9d563f","repo":"stride3d/stride","slug":"could-not-translate-code","errorCode":null,"errorMessage":"Could not translate code","messagePattern":"Could not translate code","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Compilers/SpirvTranslator.cs","lineNumber":178,"sourceCode":"            CombinedImageSampler* combinedImageSamplers = null;\n            if (cross.CompilerGetCombinedImageSamplers(compiler, &combinedImageSamplers, ref numSamplers) != Result.Success)\n                throw new Exception($\"{cross.CompilerGetCombinedImageSamplers(compiler, &combinedImageSamplers, ref numSamplers)}\");\n\n            for (uint i = 0; i < numSamplers; ++i)\n            {\n                var textureName = cross.CompilerGetNameS(compiler, combinedImageSamplers[i].ImageId);\n                var samplerName = cross.CompilerGetNameS(compiler, combinedImageSamplers[i].SamplerId);\n                cross.CompilerSetName(compiler, combinedImageSamplers[i].CombinedId, $\"SPIRV_Cross_Combined{textureName}{samplerName}\");\n            }\n        }\n\n        if (cross.CompilerCompile(compiler, &translated) != Result.Success)\n            throw new Exception($\"{cross.CompilerCompile(compiler, &translated)} : could not compile code\");\n\n        translatedCode = SilkMarshal.PtrToString((nint)translated);\n        cross.ContextReleaseAllocations(context);\n        cross.ContextDestroy(context);\n        return translatedCode ?? throw new Exception(\"Could not translate code\");\n    }\n}\n\n","sourceCodeStart":160,"sourceCodeEnd":182,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Compilers/SpirvTranslator.cs#L160-L182","documentation":"Translate throws this when CompilerCompile reports success but the returned translated-source pointer converts to a null C# string via SilkMarshal.PtrToString. It is a defensive post-condition check: native code said it produced output, yet no string materialized, so Translate refuses to return null. This signals a native/managed marshaling or allocation problem.","triggerScenarios":"CompilerCompile succeeding but writing a null/invalid pointer to `translated`, a marshal failure for the returned ANSI/UTF-8 buffer, or a native library ABI mismatch that misreads the out-parameter.","commonSituations":"Silk.NET binding/native-library version mismatch corrupting the out pointer; native memory corruption; extremely rare native bugs returning empty output while still returning Success.","solutions":["Check that the native spirv-cross binary version matches the Silk.NET.SPIRV.Cross bindings (ABI mismatch is the most common cause)","Inspect the translated pointer immediately after CompilerCompile in a debugger to distinguish null vs unmarshalable data","Update both the native library and Silk.NET packages together","Re-run with a minimal shader to rule out an output-size/marshaling edge case","Capture and include the error-callback log in the report before escalating upstream"],"exampleFix":"// before\ntranslatedCode = SilkMarshal.PtrToString((nint)translated);\nreturn translatedCode ?? throw new Exception(\"Could not translate code\");\n// after\ntranslatedCode = SilkMarshal.PtrToString((nint)translated);\nif (translatedCode is null)\n    throw new InvalidOperationException($\"spirv-cross returned Success but no source string was marshaled (translated=0x{(nint)translated:x}). Check native/binding version match.\");\nreturn translatedCode;","handlingStrategy":"try-catch","validationCode":"// Version-alignment check at startup between Silk.NET.SPIRV.Cross and the loaded native spirv-cross","typeGuard":null,"tryCatchPattern":"try { var src = translator.Translate(backend, entryPoint); } catch (Exception ex) when (ex.Message.Contains(\"Could not translate code\")) { Log.Error(\"spirv-cross returned success but produced no output — suspect native/binding ABI mismatch\"); throw; }","preventionTips":["Keep Silk.NET bindings and native spirv-cross version-locked","Smoke-test a trivial shader at startup to catch marshaling issues early","Treat null output despite Success as an environment bug, not a shader bug","Log the error-callback output when reproducing"],"tags":["spirv-cross","marshaling","native-interop","null"],"backgroundTag":"unexpected-response-shape","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"}