Read UsedResources from shader reflection to list a function’s textures and buffers without a GPU.
dotnet add package RecursiveAI.OneShaderRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneShader; var result = ShaderCompiler.CompileFile( "passes.hlsl"); if (result.Reflection is not { } reflection) throw new InvalidOperationException( string.Join("\n", result.Diagnostics)); var entry = reflection.EntryPoints.Single( entry => entry.Name == "Fill"); foreach (string name in entry.UsedResources) Console.WriteLine(name);
The snippet is the complete C# program. If you’re not using the zip, run dotnet new console and the install command above, paste the snippet into Program.cs, put passes.hlsl next to it, then run dotnet run.
Use this passes.hlsl (also in the zip):
RWStructuredBuffer<uint> Colors : register(u0); RWStructuredBuffer<uint> Depths : register(u1); [shader("compute")] [numthreads(8, 1, 1)] void Fill(uint3 id : SV_DispatchThreadID) { Colors[id.x] = id.x; } [shader("compute")] [numthreads(8, 1, 1)] void ClearDepth(uint3 id : SV_DispatchThreadID) { Depths[id.x] = 0; }
Fill writes Colors; ClearDepth writes Depths. If compilation fails, Reflection is null and the program reports the diagnostics instead of looking up the entry point.
Unzip the example, then dotnet run.
ColorsVariations
To list the resources used by ClearDepth, replace the entry-point selection line:
var entry = reflection.EntryPoints.Single( entry => entry.Name == "ClearDepth");
DepthsRelated recipes
The short answer
Compile the shader, select the entry point by name from Reflection.EntryPoints, and read its UsedResources. In OneShader’s text reflection format, those names are the uses name=... records under that entry point. You don’t have to parse the text format if you’re calling the C# compiler API.
What is shader reflection for resource bindings?
Reflection describes a shader’s interface: what it reads or writes and how your application can bind those resources. OneShader’s reflection format separates a file’s resource records from the usage lists of individual entry points. UsedResources gives names, not binding numbers. Look up a name in Reflection.Resources to get its kind and the binding slots for the compiled targets. A slot for SPIR-V is a set and binding; Metal’s slot uses the argument index as the binding. Keep the names and slots separate when your build targets more than one graphics API.
How do I tell which resources one HLSL entry point uses?
Read the usage list under that entry, not the resource table for the file. Both buffers in the example appear in the resource table, but only Colors belongs to Fill’s list. Filtering by stage alone would not separate the buffers: both entry points are compute shaders. This distinction matters when building separate pipelines from one source file. For a pipeline with more than one stage, account for the resources used by all of the selected entry points, not just one.
CompileFile with the default options finds functions marked with [shader(...)]. If your source doesn’t carry those attributes, supply the names and stages through CompileOptions.EntryPoints instead. Check the compile result before looking up an entry; a missing function name and invalid shader code are different failures. Specialization constants are tracked separately in the text format as specuse records, so don’t expect them in UsedResources.