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

  1. Check that the native spirv-cross binary version matches the Silk.NET.SPIRV.Cross bindings (ABI mismatch is the most common cause)
  2. Inspect the translated pointer immediately after CompilerCompile in a debugger to distinguish null vs unmarshalable data
  3. Update both the native library and Silk.NET packages together
  4. Re-run with a minimal shader to rule out an output-size/marshaling edge case
  5. 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

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


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)