Recursive AI

Expand HLSL macros to text in C#

OneShader is a free, open-source HLSL compiler library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

You can preprocess a fragment of HLSL without an entry point or a compiled shader binary.

Terminal
dotnet add package RecursiveAI.OneShader

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneShader.Diagnostics;
using OneShader.Lexing;
using OneShader.Preprocessing;
 
var bag = new DiagnosticBag();
var expanded = Preprocessor.Preprocess(
    new SourceFile("gain.hlsl", """
    #define DOUBLE(x) ((x) + (x))
    float gain = DOUBLE(3);
    """), new PreprocessorOptions(), bag);
foreach (var diagnostic in bag.Items)
    Console.Error.WriteLine(diagnostic);
if (bag.HasErrors) return 1;
Console.WriteLine(expanded.ToText().Trim());
return 0;

Unzip the example, then dotnet run.

Prints the expanded declaration. It doesn’t write a shader binary.

Text
float gain = ((3) + (3));

The full program

C#
using OneShader.Diagnostics;
using OneShader.Lexing;
using OneShader.Preprocessing;
 
const string hlsl = """
#define DOUBLE(x) ((x) + (x))
float gain = DOUBLE(3);
""";
var diagnostics = new DiagnosticBag();
var source = new SourceFile("gain.hlsl", hlsl);
var expanded = Preprocessor.Preprocess(
    source, new PreprocessorOptions(), diagnostics);
foreach (var diagnostic in diagnostics.Items)
    Console.Error.WriteLine(diagnostic);
if (diagnostics.HasErrors) return 1;
Console.WriteLine(expanded.ToText().Trim());
return 0;
Text
float gain = ((3) + (3));

gain.hlsl is a diagnostic filename; the HLSL comes from the string, not from disk. If a directive such as #error reports an error, the example writes the diagnostic to standard error and exits without printing the expansion.

Variations

Inspect an HLSL #ifdef branch. Replace the HLSL string in the zipped program with this one:

C#
const string hlsl = """
#define USE_TINT
#ifdef USE_TINT
float gain = 2;
#else
float gain = 1;
#endif
""";
Text
float gain = 2;

The short answer

Pass the HLSL source and a DiagnosticBag to Preprocessor.Preprocess. Check HasErrors before using PreprocessResult.ToText(). This stops before shader compilation, so you can inspect a macro in a fragment of HLSL that doesn’t have an entry point. It won’t tell you whether that fragment type-checks as part of a shader.

What does #define do in HLSL?

HLSL’s #define directive substitutes tokens. A macro with parameters, such as DOUBLE(x), looks like a function call but expands before the shader is compiled. In the output above, 3 appears twice, and ((3) + (3)) stays an expression; the preprocessor doesn’t evaluate the sum. This distinction matters when debugging a larger macro: each appearance of a parameter in the replacement text can insert a copy of the argument.

ToText() is a view of the processed tokens, not an edit of the original file. It adds a space if two adjacent tokens would otherwise merge. The original #define is gone; OneShader keeps #pragma directives other than #pragma once in the token stream.

What are #ifdef and #ifndef in HLSL?

#ifdef and #ifndef check whether a macro is defined or not defined, respectively. They don’t check its value: even #define USE_TINT 0 makes #ifdef USE_TINT take the first branch. Change the definition or remove it, then run the preprocessor again to see which branch reaches the compiler.

If your shader branches on target-profile macros, set PreprocessorOptions.TargetProfile to the profile you intend to compile for. For example, ps_6_0 supplies the pixel-shader stage and shader model values. Without the same preprocessing options as your build, the text you inspect can come from a different branch than the shader you ship.

All oneshader recipes