namespace Brutal.ShaderCApi; /// /// Extension methods providing a high-level throwing API over the generated surface. /// public static class CompilerExtensions { /// /// Returns unchanged when compilation succeeded; otherwise throws the /// typed that corresponds to the . /// /// /// /// This method is fluent and does not dispose ; ownership remains /// with the caller regardless of whether it succeeds or throws. /// /// /// The compilation result to inspect. /// when . /// /// Thrown when the status is . /// /// /// Thrown when the status is . /// /// /// Thrown when the status is . /// /// /// Thrown when the status is . /// /// /// Thrown when the status is . /// /// /// Thrown when the status is . /// /// /// Thrown when the status is . /// /// /// Thrown when the status is . /// /// Thrown when the status is unrecognised. public static CompilationResult ThrowIfFailed(this in CompilationResult result) => result.GetCompilationStatus() switch { CompilationStatus.Success => result, CompilationStatus.InvalidStage => throw new ShaderInvalidStageException(result), CompilationStatus.CompilationError => throw new ShaderCompilationException(result), CompilationStatus.InternalError => throw new ShaderInternalException(result), CompilationStatus.NullResultObject => throw new ShaderNullResultObjectException(result), CompilationStatus.InvalidAssembly => throw new ShaderInvalidAssemblyException(result), CompilationStatus.ValidationError => throw new ShaderValidationException(result), CompilationStatus.TransformationError => throw new ShaderTransformationException(result), CompilationStatus.ConfigurationError => throw new ShaderConfigurationException(result), _ => throw new ShaderException(result), }; /// /// Returns when compilation succeeded; otherwise disposes the intermediate /// result and rethrows the typed , so a failed compile never leaks the /// native handle while a successful one stays owned by the caller. /// private static CompilationResult ThrowIfFailedOrDispose(this in CompilationResult result) { try { return result.ThrowIfFailed(); } catch { result.Dispose(); throw; } } /// /// Compiles GLSL/HLSL to binary SPIR-V, throwing a typed /// on failure. /// /// /// /// On success the returned is owned by the caller and must be /// disposed. While the result is undisposed the raw SPIR-V bytes are accessible via /// . /// /// /// On failure the intermediate result is disposed before the exception propagates, so no /// native handle is leaked. /// /// /// The compiler to use. /// The GLSL/HLSL source code encoded as UTF-8. /// The shader stage. /// Source file name used in diagnostic messages. /// Name of the entry-point function. /// Compilation options. /// /// A with containing /// the compiled SPIR-V bytes. /// /// Thrown on any compilation failure. public static CompilationResult Compile( this in Compiler c, ReadOnlySpan source, ShaderKind kind, RefString8 inputFileName, RefString8 entryPoint, CompileOptions options ) { return c.CompileIntoSpv(source, kind, inputFileName, entryPoint, options).ThrowIfFailedOrDispose(); } /// /// Pre-processes GLSL/HLSL , throwing a typed /// on failure. /// /// /// /// On success the returned is owned by the caller and must be /// disposed. While the result is undisposed the pre-processed text bytes are accessible via /// . /// /// /// On failure the intermediate result is disposed before the exception propagates, so no /// native handle is leaked. /// /// /// The compiler to use. /// The GLSL/HLSL source code encoded as UTF-8. /// The shader stage. /// Source file name used in diagnostic messages. /// Name of the entry-point function. /// Compilation options. /// /// A with containing /// the pre-processed text. /// /// Thrown on any pre-processing failure. public static CompilationResult Preprocess( this in Compiler c, ReadOnlySpan source, ShaderKind kind, RefString8 inputFileName, RefString8 entryPoint, CompileOptions options ) { return c.CompileIntoPreprocessedText(source, kind, inputFileName, entryPoint, options).ThrowIfFailedOrDispose(); } /// /// Compiles GLSL/HLSL (supplied as a managed ) to binary /// SPIR-V, throwing a typed on failure. /// /// /// /// This overload UTF-8 encodes , , and /// then delegates to the span-based overload. /// /// /// On success the returned is owned by the caller and must be /// disposed. While the result is undisposed the raw SPIR-V bytes are accessible via /// . /// /// /// On failure the intermediate result is disposed before the exception propagates, so no /// native handle is leaked. /// /// /// When is omitted (default), no custom compilation options or include /// callbacks are applied; shaderc compiles with its built-in defaults. /// /// /// The compiler to use. /// The GLSL/HLSL source code as a managed string. /// The shader stage. /// Source file name used in diagnostic messages. /// Name of the entry-point function. Defaults to "main". /// /// Compilation options, or to use shaderc built-in defaults with no /// include callbacks. /// /// /// A with containing /// the compiled SPIR-V bytes. /// /// Thrown on any compilation failure. public static CompilationResult Compile( this in Compiler c, string source, ShaderKind kind, string inputFileName, string entryPoint = "main", CompileOptions options = default ) { using var writer = new BespokeUtf8Encoder( source, inputFileName, entryPoint, out var source8, out var inputFileName8, out var entryPoint8 ); return c.Compile(source8, kind, inputFileName8, entryPoint8, options); } /// /// Reads the file at and compiles its GLSL/HLSL content to binary SPIR-V, /// throwing a typed on failure. /// /// /// /// The file is read with and passed as raw bytes to the /// span-based overload. is passed as the /// inputFileName, so when include support is enabled by passing /// configured with , relative #include /// paths anchor against it. With the default shaderc has no include /// support, so a source containing #include fails to compile. /// /// /// On success the returned is owned by the caller and must be /// disposed. While the result is undisposed the raw SPIR-V bytes are accessible via /// . /// /// /// On failure the intermediate result is disposed before the exception propagates, so no /// native handle is leaked. /// /// /// When is omitted (default), no custom compilation options or include /// callbacks are applied; shaderc compiles with its built-in defaults. /// /// /// The compiler to use. /// Path to the shader source file. /// The shader stage. /// Name of the entry-point function. Defaults to "main". /// /// Compilation options, or to use shaderc built-in defaults with no /// include callbacks. /// /// /// A with containing /// the compiled SPIR-V bytes. /// /// Thrown on any compilation failure. /// Thrown if no file exists at . /// Thrown if the file at cannot be read. public static CompilationResult CompileFile( this in Compiler c, string path, ShaderKind kind, string entryPoint = "main", CompileOptions options = default ) { using var writer = new BespokeUtf8Encoder(path, entryPoint, out var path8, out var entryPoint8); var sourceUtf8 = File.ReadAllBytes(path).AsSpan(); // wip: improve allocation strategy return c.Compile(sourceUtf8, kind, path8, entryPoint8, options); } /// /// Assembles SPIR-V text assembly in into binary SPIR-V, /// throwing a typed on failure. /// /// /// /// On success the returned is owned by the caller and must be /// disposed. While the result is undisposed the raw SPIR-V bytes are accessible via /// . /// /// /// On failure the intermediate result is disposed before the exception propagates, so no /// native handle is leaked. /// /// /// The compiler to use. /// The SPIR-V text assembly encoded as UTF-8. /// Compilation options. /// /// A with containing /// the assembled SPIR-V bytes. /// /// Thrown on any assembly failure. public static CompilationResult Assemble( this in Compiler c, ReadOnlySpan sourceAssembly, CompileOptions options ) { return c.AssembleIntoSpv(sourceAssembly, options).ThrowIfFailedOrDispose(); } }