---
title: "Expand HLSL macros to text in C#"
url: https://recursiveai.org/oneshader/cookbook/expand-hlsl-macros-without-compiling/
description: "Expand HLSL macros to text in C# with OneShader. Print the preprocessed source and check diagnostics without compiling a shader or creating a binary."
version: "1.7.1"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneShader) · [GitHub](https://github.com/recursive-ai-oss/oneshader-csharp) · [Docs](https://recursiveai.org/oneshader/docs/) · [Download example (.zip)](https://recursiveai.org/oneshader/cookbook/expand-hlsl-macros-without-compiling/expand-hlsl-macros-without-compiling.zip)

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

```shell
dotnet add package RecursiveAI.OneShader
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/oneshader/cookbook/expand-hlsl-macros-without-compiling/expand-hlsl-macros-without-compiling.zip), then `dotnet run`.

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

```text
float gain = ((3) + (3));
```

## The full program

```csharp
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:

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

```text
float gain = 2;
```

## Related recipes

- [Compile HLSL with in-memory includes](https://recursiveai.org/oneshader/cookbook/compile-hlsl-with-in-memory-includes/)
- [Define an HLSL build macro in a .csproj](https://recursiveai.org/oneshader/cookbook/define-hlsl-build-macro-in-csproj/)

---

## 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](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-appendix-pre-define) 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`](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-appendix-pre-ifdef) 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.
