stride3d/stride · error · Exception

: could not compile code

Error message

{cross.CompilerCompile(compiler, &translated)} : could not compile code

What it means

Translate throws this when the final CompilerCompile call fails to emit the backend source (HLSL/GLSL) from the configured compiler. This is the terminal code-generation step, so the failure reflects a backend codegen error — spirv-cross logs the specific cause via the installed error callback. The message includes the returned Result code.

Solutions

  1. Read the error-callback log output (Log.Error lines) emitted just before the throw — spirv-cross names the failing instruction/variable
  2. Raise or lower the HLSL shader model option if targeting SM5 with modern constructs
  3. Validate the SPIR-V and check for instructions unsupported by the target backend
  4. Update the native spirv-cross binary to pick up codegen fixes
  5. Translate to a different backend to confirm whether the failure is backend-specific

Example fix

// before
cross.CompilerOptionsSetUint(compilerOptions, CompilerOption.HlslShaderModel, 50);
var hlsl = translator.Translate(Backend.Hlsl, entryPoint);
// after
try { var hlsl = translator.Translate(Backend.Hlsl, entryPoint); }
catch (Exception ex) when (ex.Message.Contains("could not compile code"))
{ Log.Error($"Codegen failed, retrying with SM6: {ex.Message}"); /* re-run with HlslShaderModel=60 */ throw; }
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate SPIR-V offline: spirv-val shader.spv; ensure no features beyond target (e.g. SM5) support

Try / catch

try { var src = translator.Translate(backend, entryPoint); } catch (Exception ex) when (ex.Message.Contains("could not compile code")) { Log.Error($"Codegen failed for {backend}: {ex.Message}"); throw; }

Prevention

When it happens

Trigger: Calling Translate when code generation fails: unsupported SPIR-V instructions for the target backend, HLSL Shader Model 50 incompatibilities (e.g. structured buffers, compute features), invalid decorations, or options that produced an inconsistent compiler state.

Common situations: Translating Vulkan-era SPIR-V features (sparse residency, fragment shading rate, ray tracing) to HLSL SM5 or GLSL which cannot express them; FXC-targeted SM50 output with unsupported constructs; a spirv-cross version lacking a codegen fix for a construct emitted by a newer shader compiler.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/7e4062203e26aff1. Report an issue: GitHub.

Appendix: source

Thrown at sources/shaders/Stride.Shaders.Compilers/SpirvTranslator.cs:173

        {
            if (cross.CompilerBuildCombinedImageSamplers(compiler) != Result.Success)
                throw new Exception($"{cross.CompilerBuildCombinedImageSamplers(compiler)} : Could not enable combined image samplers");

            nuint numSamplers = 0;
            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)