stride3d/stride · error · Exception
Could not translate code
Error message
Could not translate code
What it means
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.
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
Example fix
// before
translatedCode = SilkMarshal.PtrToString((nint)translated);
return translatedCode ?? throw new Exception("Could not translate code");
// after
translatedCode = SilkMarshal.PtrToString((nint)translated);
if (translatedCode is null)
throw new InvalidOperationException($"spirv-cross returned Success but no source string was marshaled (translated=0x{(nint)translated:x}). Check native/binding version match.");
return translatedCode; Defensive patterns
Strategy: try-catch
Validate before calling
// Version-alignment check at startup between Silk.NET.SPIRV.Cross and the loaded native spirv-cross
Try / catch
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; } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- : could not create compiler
- : could not create shader resources
- {cross.CompilerGetCombinedImageSamplers(compiler…
- Value cannot be null. (Parameter 'part')
- Cannot add an empty asset item reference
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/0f22bec6cf9d563f.
Report an issue: GitHub.
Appendix: source
Thrown at sources/shaders/Stride.Shaders.Compilers/SpirvTranslator.cs:178
CombinedImageSampler* combinedImageSamplers = null;
if (cross.CompilerGetCombinedImageSamplers(compiler, &combinedImageSamplers, ref numSamplers) != Result.Success)
throw new Exception($"{cross.CompilerGetCombinedImageSamplers(compiler, &combinedImageSamplers, ref numSamplers)}");
for (uint i = 0; i < numSamplers; ++i)
{
var textureName = cross.CompilerGetNameS(compiler, combinedImageSamplers[i].ImageId);
var samplerName = cross.CompilerGetNameS(compiler, combinedImageSamplers[i].SamplerId);
cross.CompilerSetName(compiler, combinedImageSamplers[i].CombinedId, $"SPIRV_Cross_Combined{textureName}{samplerName}");
}
}
if (cross.CompilerCompile(compiler, &translated) != Result.Success)
throw new Exception($"{cross.CompilerCompile(compiler, &translated)} : could not compile code");
translatedCode = SilkMarshal.PtrToString((nint)translated);
cross.ContextReleaseAllocations(context);
cross.ContextDestroy(context);
return translatedCode ?? throw new Exception("Could not translate code");
}
}
View on GitHub (pinned to 96fad776d2)