Skip to content
This repository was archived by the owner on May 11, 2024. It is now read-only.

Commit 6b31f63

Browse files
committed
- [Core] Added Shader decompilation/disassembly for DXCB program.
1 parent 61ff13f commit 6b31f63

7 files changed

+97
-12
lines changed

AssetStudio.GUI/AssetStudio.GUI.csproj

+19
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@
8989
</ContentWithTargetPath>
9090
</ItemGroup>
9191

92+
<ItemGroup>
93+
<ContentWithTargetPath Include="Libraries\x86\HLSLDecompiler.dll">
94+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
95+
<TargetPath>x86\HLSLDecompiler.dll</TargetPath>
96+
</ContentWithTargetPath>
97+
<ContentWithTargetPath Include="Libraries\x64\HLSLDecompiler.dll">
98+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
99+
<TargetPath>x64\HLSLDecompiler.dll</TargetPath>
100+
</ContentWithTargetPath>
101+
<ContentWithTargetPath Include="Libraries\x86\BinaryDecompiler.lib">
102+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
103+
<TargetPath>x86\BinaryDecompiler.lib</TargetPath>
104+
</ContentWithTargetPath>
105+
<ContentWithTargetPath Include="Libraries\x64\BinaryDecompiler.lib">
106+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
107+
<TargetPath>x64\BinaryDecompiler.lib</TargetPath>
108+
</ContentWithTargetPath>
109+
</ItemGroup>
110+
92111
<ItemGroup>
93112
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
94113
<PackageReference Include="OpenTK" Version="4.8.0" />
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

AssetStudio.Utility/AssetStudio.Utility.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<PackageReference Include="Kyaru.Texture2DDecoder.Windows" Version="0.1.0" />
1616
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
1717
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
18+
<PackageReference Include="Vortice.D3DCompiler" Version="3.4.3-beta" />
1819
</ItemGroup>
1920

2021
<ItemGroup>

AssetStudio.Utility/ShaderConverter.cs

+77-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
using SpirV;
1+
using AssetStudio.PInvoke;
2+
using SharpGen.Runtime;
3+
using SpirV;
24
using System;
35
using System.Collections.Generic;
46
using System.Globalization;
57
using System.IO;
68
using System.Linq;
9+
using System.Runtime.InteropServices;
710
using System.Text;
811
using System.Text.RegularExpressions;
12+
using Vortice.D3DCompiler;
913

1014
namespace AssetStudio
1115
{
@@ -1038,9 +1042,17 @@ public string Export()
10381042
case ShaderGpuProgramType.DX9PixelSM20:
10391043
case ShaderGpuProgramType.DX9PixelSM30:
10401044
{
1041-
/*var shaderBytecode = new ShaderBytecode(m_ProgramCode);
1042-
sb.Append(shaderBytecode.Disassemble());*/
1043-
sb.Append("// shader disassembly not supported on DXBC");
1045+
try
1046+
{
1047+
var programCodeSpan = m_ProgramCode.AsSpan();
1048+
var g = Compiler.Disassemble(programCodeSpan.GetPinnableReference(), programCodeSpan.Length, DisasmFlags.None, "");
1049+
sb.Append(g.AsString());
1050+
}
1051+
catch (Exception e)
1052+
{
1053+
sb.Append($"// disassembly error {e.Message}\n");
1054+
}
1055+
10441056
break;
10451057
}
10461058
case ShaderGpuProgramType.DX10Level9Vertex:
@@ -1054,16 +1066,42 @@ public string Export()
10541066
case ShaderGpuProgramType.DX11HullSM50:
10551067
case ShaderGpuProgramType.DX11DomainSM50:
10561068
{
1057-
/*int start = 6;
1058-
if (m_Version == 201509030) // 5.3
1069+
int type = m_ProgramCode[0];
1070+
int start = 1;
1071+
if (type > 0)
10591072
{
1060-
start = 5;
1073+
if (type == 1)
1074+
{
1075+
start = 6;
1076+
}
1077+
else if (type == 2)
1078+
{
1079+
start = 38;
1080+
}
1081+
}
1082+
1083+
var buffSpan = m_ProgramCode.AsSpan(start);
1084+
1085+
try
1086+
{
1087+
HLSLDecompiler.DecompileShader(buffSpan.ToArray(), buffSpan.Length, out var hlslText);
1088+
sb.Append(hlslText);
1089+
}
1090+
catch (Exception e)
1091+
{
1092+
Logger.Verbose($"Decompile error {e.Message}");
1093+
Logger.Verbose($"Attempting to disassemble...");
1094+
1095+
try
1096+
{
1097+
var g = Compiler.Disassemble(buffSpan.GetPinnableReference(), buffSpan.Length, DisasmFlags.None, "");
1098+
sb.Append(g.AsString());
1099+
}
1100+
catch (Exception ex)
1101+
{
1102+
sb.Append($"// decompile/disassembly error {ex.Message}\n");
1103+
}
10611104
}
1062-
var buff = new byte[m_ProgramCode.Length - start];
1063-
Buffer.BlockCopy(m_ProgramCode, start, buff, 0, buff.Length);
1064-
var shaderBytecode = new ShaderBytecode(buff);
1065-
sb.Append(shaderBytecode.Disassemble());*/
1066-
sb.Append("// shader disassembly not supported on DXBC");
10671105
break;
10681106
}
10691107
case ShaderGpuProgramType.MetalVS:
@@ -1107,4 +1145,31 @@ public string Export()
11071145
return sb.ToString();
11081146
}
11091147
}
1148+
1149+
public static class HLSLDecompiler
1150+
{
1151+
private const string DLL_NAME = "HLSLDecompiler";
1152+
static HLSLDecompiler()
1153+
{
1154+
DllLoader.PreloadDll(DLL_NAME);
1155+
}
1156+
public static void DecompileShader(byte[] shaderByteCode, int shaderByteCodeSize, out string hlslText)
1157+
{
1158+
var code = Decompile(shaderByteCode, shaderByteCodeSize, out var shaderText, out var shaderTextSize);
1159+
if (code != 0)
1160+
{
1161+
throw new Exception($"Unable to decompile shader, Error code: {code}");
1162+
}
1163+
1164+
hlslText = Marshal.PtrToStringAnsi(shaderText, shaderTextSize);
1165+
Marshal.FreeHGlobal(shaderText);
1166+
}
1167+
1168+
#region importfunctions
1169+
1170+
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
1171+
private static extern int Decompile(byte[] shaderByteCode, int shaderByteCodeSize, out IntPtr shaderText, out int shaderTextSize);
1172+
1173+
#endregion
1174+
}
11101175
}

0 commit comments

Comments
 (0)