Is it possible to decompile a .xnb file? I've lost my shader code, but only have my .xnb file left.

  • \$\begingroup\$ I'd be very surprised if you get back anything like the orignal source as shaders are heavily optimised during compilation (not just compressed into XNB format. I'd re-write it. That said, here's the XNB file format \$\endgroup\$ – George Duckett Feb 20 '12 at 11:12

It's unlikely you'll be able to retrieve the source from the xnb file as it has undergone compilation into a binary format during the XNA pipeline stages. While it's theoretically not impossible, in my opinion you'd probably be much better off rewriting the shader..


The best you can get is a disassembly, which may help you recreate the source code. (Unless someone can point you to an actual HLSL decompiler. I've never seen one.)

If you happen to be using XNA 3.1 you could simply load it into an Effect object via the content system and then call Effect.Disassemble on it.

XNA 4.0 removes this - so it will be much harder:

First of all you need the actual effect data. Probably the easiest way to get it would be to use reflection to get access to the private Effect.pCachedEffectData member which (I think) contains it.

Once you have the binary data, you'll want to disassemble it. I can't figure out a way to use fxc.exe to disassemble the data directly. But perhaps you can install XNA 3.1 and use that. Or use Direct3D and D3DDisassemble - which is, I suspect, what XNA 3.1 uses internally.

  • \$\begingroup\$ Can you show me how to write the code to use Disassemble? I've tried but it complains that it can't find the file. \$\endgroup\$ – Neil Knight Feb 20 '12 at 19:24
  • \$\begingroup\$ @NeilKnight Not really. But perhaps ask in a fresh question and provide more details. \$\endgroup\$ – Andrew Russell Feb 22 '12 at 1:07

http://mirror.dataorb.net/XNB_Exporter.zip - Maybe this can help you.

  • 3
    \$\begingroup\$ You should write, what it is. At least some short description. \$\endgroup\$ – zacharmarz Feb 20 '12 at 9:39
  • \$\begingroup\$ Ah.. Its xnb exporter. I found it in popular topic of Terraria forum.. :p \$\endgroup\$ – ChaosDev Feb 20 '12 at 9:42
  • \$\begingroup\$ I'm after Shader files, not converting image files. \$\endgroup\$ – Neil Knight Feb 20 '12 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.