{"record":{"id":"7e4062203e26aff1","repo":"stride3d/stride","slug":"cross-compilercompile-compiler-translated-could-not-compile","errorCode":null,"errorMessage":"{cross.CompilerCompile(compiler, &translated)} : could not compile code","messagePattern":"(.+?) : could not compile code","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Compilers/SpirvTranslator.cs","lineNumber":173,"sourceCode":"        {\n            if (cross.CompilerBuildCombinedImageSamplers(compiler) != Result.Success)\n                throw new Exception($\"{cross.CompilerBuildCombinedImageSamplers(compiler)} : Could not enable combined image samplers\");\n\n            nuint numSamplers = 0;\n            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":155,"sourceCodeEnd":182,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Compilers/SpirvTranslator.cs#L155-L182","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the error-callback log output (Log.Error lines) emitted just before the throw — spirv-cross names the failing instruction/variable","Raise or lower the HLSL shader model option if targeting SM5 with modern constructs","Validate the SPIR-V and check for instructions unsupported by the target backend","Update the native spirv-cross binary to pick up codegen fixes","Translate to a different backend to confirm whether the failure is backend-specific"],"exampleFix":"// before\ncross.CompilerOptionsSetUint(compilerOptions, CompilerOption.HlslShaderModel, 50);\nvar hlsl = translator.Translate(Backend.Hlsl, entryPoint);\n// after\ntry { var hlsl = translator.Translate(Backend.Hlsl, entryPoint); }\ncatch (Exception ex) when (ex.Message.Contains(\"could not compile code\"))\n{ Log.Error($\"Codegen failed, retrying with SM6: {ex.Message}\"); /* re-run with HlslShaderModel=60 */ throw; }","handlingStrategy":"try-catch","validationCode":"// Validate SPIR-V offline: spirv-val shader.spv; ensure no features beyond target (e.g. SM5) support","typeGuard":null,"tryCatchPattern":"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; }","preventionTips":["Always inspect the error-callback log lines — they name the failing construct","Match HLSL shader-model option to the constructs your shaders use","Keep spirv-cross updated for new SPIR-V features","Test all shaders against every target backend in CI"],"tags":["spirv-cross","codegen","shader-compilation","hlsl","glsl"],"backgroundTag":"unsupported-operation","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"}