1
- using SpirV ;
1
+ using AssetStudio . PInvoke ;
2
+ using SharpGen . Runtime ;
3
+ using SpirV ;
2
4
using System ;
3
5
using System . Collections . Generic ;
4
6
using System . Globalization ;
5
7
using System . IO ;
6
8
using System . Linq ;
9
+ using System . Runtime . InteropServices ;
7
10
using System . Text ;
8
11
using System . Text . RegularExpressions ;
12
+ using Vortice . D3DCompiler ;
9
13
10
14
namespace AssetStudio
11
15
{
@@ -1038,9 +1042,17 @@ public string Export()
1038
1042
case ShaderGpuProgramType . DX9PixelSM20 :
1039
1043
case ShaderGpuProgramType . DX9PixelSM30 :
1040
1044
{
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
+
1044
1056
break ;
1045
1057
}
1046
1058
case ShaderGpuProgramType . DX10Level9Vertex :
@@ -1054,16 +1066,42 @@ public string Export()
1054
1066
case ShaderGpuProgramType . DX11HullSM50 :
1055
1067
case ShaderGpuProgramType . DX11DomainSM50 :
1056
1068
{
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 )
1059
1072
{
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
+ }
1061
1104
}
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" ) ;
1067
1105
break ;
1068
1106
}
1069
1107
case ShaderGpuProgramType . MetalVS :
@@ -1107,4 +1145,31 @@ public string Export()
1107
1145
return sb . ToString ( ) ;
1108
1146
}
1109
1147
}
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
+ }
1110
1175
}
0 commit comments