SlimShader.Compiler

SlimShader.Compiler transforms HLSL source code into SlimShader objects ready for use in SlimShader.VirtualMachine


Keywords
HLSL, shader, assembly, fxc, bytecode, decompiler, compiler, Direct3D
Install
Install-Package SlimShader.Compiler -Version 0.2.0

Documentation

SlimShader

SlimShader is a Direct3D shader bytecode parser for .NET and C++. It is a Portable Class Library (PCL), compatible with .NET Framework 4.0+ and .NET for Windows Store apps. This is the repository for the .NET version; the C++ version can be found at tgjones/slimshader-cpp.

Usage

var fileBytes = File.ReadAllBytes("CompiledShader.o");
var bytecodeContainer = BytecodeContainer.Parse(fileBytes);

Console.WriteLine(bytecodeContainer.InputSignature.Parameters.Count);
Console.WriteLine(bytecodeContainer.Statistics.InstructionCount);
Console.WriteLine(bytecodeContainer.Statistics.StaticFlowControlCount);
Console.WriteLine(bytecodeContainer.Shader.Tokens.Count);

Virtual Machine

SlimShader also includes a virtual machine, which can execute HLSL shaders on the CPU. See the source code here. SlimShader.VirtualMachine includes both an interpreter and a just-in-time (JIT) compiler.

HlslUnit

HlslUnit is a .NET library that allows you to unit test your HLSL shaders. It is built on top of SlimShader and SlimShader.VirtualMachine. I blogged about it here.

GUI

I've written a simple GUI to showcase SlimShader. You can open a compiled (binary) shader file, view the disassembled version, and view various properties (from the STAT chunk).

Screenshot

Acknowledgements

  • SlimShader uses several test shaders from the HLSLCrossCompiler project, by kind permission of James Jones.
  • The Nuclex Framework, in particular the HlslShaderReflector class, was very helpful when figuring out the RDEF, ISGN and OSGN chunks.
  • The Wine project, in particular Wine's shader reflection code, had some good tips for decoding the STAT chunk.
  • FXDIS was useful to look at when getting started, but the techniques used in that project (casting raw bytes to struct types) don't translate well from C++ to C#.
  • For the SHDR chunk, I mostly just used D3D11TokenizedProgramFormat.hpp, a header file that comes with the Windows DDK.

License

SlimShader is released under the MIT License.