Table of Contents

Class ShaderConverter

Namespace
ShadowDusk.Compiler
Assembly
ShadowDusk.Compiler.dll

One auto-detecting entry point per output kind, for callers that receive shaders without a trustworthy file extension. Each method detects the source language from the text (ShaderFormatDetector), routes an .fx effect or HLSL-compatible Slang to the same pipeline the format-specific entry points use, and returns an UnsupportedInput error (code SD0650) without compiling anything when the input is not one of those two.

public static class ShaderConverter
Inheritance
ShaderConverter
Inherited Members

Remarks

Telling "not mine" from "broken": a failed result whose errors contain a UnsupportedInput error means nothing was attempted, so a caller that chains loaders can try the next one. Any other failure (a compile error, an AmbiguousInput error, an include error) is a real error in a recognized shader and should be reported.

GLSL input (Glsl) is reported as unsupported here: its converter is the separate ShadowDusk.ShaderToy package, which this package does not reference. Call ShaderToyConverter.Convert first, then pass its .fx output to any method on this class.

The format-specific entry points (EffectCompiler, SlangFrontend, SkslConverter, RaylibConverter) are unchanged and remain the way to force a format.

Fields

UnsupportedInputCode

The diagnostic code of an UnsupportedInput error.

public const string UnsupportedInputCode = "SD0650"

Field Value

string

Methods

CanConvert(string, string?)

Whether the methods on this class would try to convert source. Cheap: a text scan, no compile. False means the input is not a format this package reads, and a caller can hand it to something else. True means the conversion will be attempted and any failure is a real error; that includes ambiguous input, which fails loudly.

public static bool CanConvert(string source, string? nameHint = null)

Parameters

source string

The shader source text.

nameHint string

Optional file name; see Detect(string, string?).

Returns

bool

Compile(IShaderCompiler, string, CompilerOptions, string?, CancellationToken)

Detects the format of source and compiles it with compiler to the target in options (MonoGame, KNI, FNA). Slang is converted to .fx first, exactly as ConvertToFx(string, SlangConvertOptions) does.

public static Result<CompiledShader, ShaderError[]> Compile(IShaderCompiler compiler, string source, CompilerOptions options, string? nameHint = null, CancellationToken cancellationToken = default)

Parameters

compiler IShaderCompiler

The compiler to run, usually an EffectCompiler.

source string

The shader source text.

options CompilerOptions

Compilation settings, as for Compile(string, CompilerOptions, CancellationToken).

nameHint string

Optional file name; used for detection ties and in diagnostics.

cancellationToken CancellationToken

Observed between pipeline stages.

Returns

Result<CompiledShader, ShaderError[]>

CompileAsync(IShaderCompiler, string, CompilerOptions, string?, CancellationToken)

Asynchronous counterpart of Compile(IShaderCompiler, string, CompilerOptions, string?, CancellationToken); same detection and routing.

public static Task<Result<CompiledShader, ShaderError[]>> CompileAsync(IShaderCompiler compiler, string source, CompilerOptions options, string? nameHint = null, CancellationToken cancellationToken = default)

Parameters

compiler IShaderCompiler

The compiler to run, usually an EffectCompiler.

source string

The shader source text.

options CompilerOptions

Compilation settings, as for CompileAsync(string, CompilerOptions, CancellationToken).

nameHint string

Optional file name; used for detection ties and in diagnostics.

cancellationToken CancellationToken

Token used to cancel a long-running compile.

Returns

Task<Result<CompiledShader, ShaderError[]>>

ConvertToRaylib(string, RaylibConvertOptions, string?, CancellationToken)

Detects the format of source and converts it to a raylib fragment shader: Convert(string, RaylibConvertOptions, CancellationToken) for an .fx effect, ConvertSlang(string, RaylibConvertOptions, CancellationToken) for Slang. A raylib-style GLSL .fs file is GLSL input, so it is reported as unsupported and can be loaded by raylib directly.

public static Result<RaylibShader, ShaderError[]> ConvertToRaylib(string source, RaylibConvertOptions options, string? nameHint = null, CancellationToken cancellationToken = default)

Parameters

source string

The shader source text.

options RaylibConvertOptions

Conversion options; see RaylibConvertOptions.

nameHint string

Optional file name; used only for detection ties.

cancellationToken CancellationToken

Observed between pipeline stages.

Returns

Result<RaylibShader, ShaderError[]>

ConvertToSksl(string, SkslConvertOptions, string?, CancellationToken)

Detects the format of source and converts it to an SkSL fragment shader: Convert(string, SkslConvertOptions, CancellationToken) for an .fx effect, ConvertSlang(string, SkslConvertOptions, CancellationToken) for Slang.

public static Result<SkslConversion, ShaderError[]> ConvertToSksl(string source, SkslConvertOptions options, string? nameHint = null, CancellationToken cancellationToken = default)

Parameters

source string

The shader source text.

options SkslConvertOptions

Conversion options; see SkslConvertOptions.

nameHint string

Optional file name; used only for detection ties.

cancellationToken CancellationToken

Observed between pipeline stages.

Returns

Result<SkslConversion, ShaderError[]>