Skip to content

Commit

Permalink
Moved HLSL Decompiler and CRC32C-HW code into library projects.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nintynuts committed May 21, 2022
1 parent b86a07c commit e1fd299
Show file tree
Hide file tree
Showing 45 changed files with 1,413 additions and 832 deletions.
2 changes: 1 addition & 1 deletion BinaryDecompiler/internal_includes/reflect.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef REFLECT_H
#define REFLECT_H

#include "hlslcc.h"
#include "../include/hlslcc.h"

ResourceGroup ResourceTypeToResourceGroup(ResourceType);

Expand Down
6 changes: 3 additions & 3 deletions BinaryDecompiler/internal_includes/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <map>
#include <vector>
#include <string>
#include "hlslcc.h"
#include "../include/hlslcc.h"

#include "internal_includes/tokens.h"
#include "internal_includes/reflect.h"
#include "tokens.h"
#include "reflect.h"

enum{ MAX_SUB_OPERANDS = 3};

Expand Down
2 changes: 1 addition & 1 deletion BinaryDecompiler/internal_includes/tokens.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef TOKENS_H
#define TOKENS_H

#include "hlslcc.h"
#include "../include/hlslcc.h"

typedef enum
{
Expand Down
2 changes: 1 addition & 1 deletion D3DCompiler/d3dcWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <algorithm>
#include <ctime>

#include "util.h"
#include "../util.h"

#define COMPILER_DLL_VERSION "46"
#define COMPILER_DLL_VERSIONL L"46"
Expand Down
55 changes: 17 additions & 38 deletions D3D_Shaders/D3D_Shaders.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,34 @@
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(SolutionDir);$(SolutionDir)BinaryDecompiler;$(SolutionDir)BinaryDecompiler\include;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
<LibraryPath>$(WindowsSDK_LibraryPath_x86);$(VC_LibraryPath_x86);$(DXSDK_DIR)Lib\x86</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(SolutionDir);$(SolutionDir)BinaryDecompiler;$(SolutionDir)BinaryDecompiler\include;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
<LibraryPath>$(WindowsSDK_LibraryPath_x64);$(VC_LibraryPath_x64);$(DXSDK_DIR)Lib\x64</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(SolutionDir);$(SolutionDir)BinaryDecompiler;$(SolutionDir)BinaryDecompiler\include;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
<LibraryPath>$(WindowsSDK_LibraryPath_x86);$(VC_LibraryPath_x86);$(DXSDK_DIR)Lib\x86</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(SolutionDir);$(SolutionDir)BinaryDecompiler;$(SolutionDir)BinaryDecompiler\include;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
<LibraryPath>$(WindowsSDK_LibraryPath_x64);$(VC_LibraryPath_x64);$(DXSDK_DIR)Lib\x64</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
Expand All @@ -99,31 +93,25 @@
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>D3dcompiler.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<AdditionalDependencies>d3dcompiler.lib;d3dx9.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>D3dcompiler.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(OutDir)D3D_Shaders.exe</OutputFile>
<AdditionalDependencies>d3dcompiler.lib;d3dx9.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
Expand All @@ -135,46 +123,37 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>D3dcompiler.lib;%(AdditionalDependencies)</AdditionalDependencies>
<IgnoreAllDefaultLibraries>
</IgnoreAllDefaultLibraries>
<IgnoreSpecificDefaultLibraries>
</IgnoreSpecificDefaultLibraries>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<AdditionalDependencies>d3dcompiler.lib;d3dx9.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>D3dcompiler.lib;%(AdditionalDependencies)</AdditionalDependencies>
<IgnoreAllDefaultLibraries>
</IgnoreAllDefaultLibraries>
<IgnoreSpecificDefaultLibraries>
</IgnoreSpecificDefaultLibraries>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<AdditionalDependencies>d3dcompiler.lib;d3dx9.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Assembler.cpp" />
<ClCompile Include="Shaders.cpp" />
<ClCompile Include="SignatureParser.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HLSLDecompiler\HLSLDecompiler.vcxproj">
<Project>{00b6ba42-4050-488d-91be-a8cd7adaeb87}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
10 changes: 0 additions & 10 deletions D3D_Shaders/D3D_Shaders.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">
Expand All @@ -26,11 +22,5 @@
<ClCompile Include="Shaders.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Assembler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SignatureParser.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
49 changes: 46 additions & 3 deletions D3D_Shaders/Shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <vector>
#include <iostream>
#include <direct.h>
#include "D3DCompiler.h"
#include "..\HLSLDecompiler\Assembler.h"

using namespace std;

Expand Down Expand Up @@ -49,13 +51,54 @@ static vector<byte> readFile(string fileName) {
return buffer;
}

void writeLUT(const unordered_map<string, vector<DWORD>>& codeBin)
{
FILE* f;

fopen_s(&f, "lut.asm", "wb");
if (!f)
return;

for (auto it = codeBin.begin(); it != codeBin.end(); ++it) {
fputs(it->first.c_str(), f);
fputs(":->", f);
vector<DWORD> b = it->second;
int nextOperand = 1;
for (DWORD i = 0; i < b.size(); i++) {
if (i == 0) {
char hex[40];
shader_ins* ins = (shader_ins*)&b[0];
if (ins->_11_23 > 0) {
if (ins->extended)
sprintf_s(hex, "0x%08X: %d,%d,%d<>%d->", b[0], ins->opcode, ins->_11_23, ins->length, ins->extended);
else
sprintf_s(hex, "0x%08X: %d,%d,%d->", b[0], ins->opcode, ins->_11_23, ins->length);
} else {
if (ins->extended)
sprintf_s(hex, "0x%08X: %d,%d<>%d->", b[0], ins->opcode, ins->length, ins->extended);
else
sprintf_s(hex, "0x%08X: %d,%d->", b[0], ins->opcode, ins->length);
}
fputs(hex, f);
} else {
char hex[20];
sprintf_s(hex, " 0x%08X", b[i]);
fputs(hex, f);
}
}
fputs("\n", f);
}
fclose(f);
}

int _tmain(int argc, _TCHAR* argv[])
{
int shaderNo = 1;
vector<string> gameNames;
string pathName;
vector<string> files;
FILE* f;
unordered_map<string, vector<DWORD>> codeBin;
char cwd[MAX_PATH];
char gamebuffer[10000];

Expand Down Expand Up @@ -90,7 +133,7 @@ int _tmain(int argc, _TCHAR* argv[])
string fileName = files[i];

vector<byte> ASM;
disassembler(&readFile(fileName), &ASM, NULL);
disassembler(&readFile(fileName), &ASM, NULL, codeBin);

fileName.erase(fileName.size() - 3, 3);
fileName.append("txt");
Expand Down Expand Up @@ -152,7 +195,7 @@ int _tmain(int argc, _TCHAR* argv[])
string fileName = files[i];

vector<byte> ASM;
disassembler(&readFile(fileName), &ASM, NULL);
disassembler(&readFile(fileName), &ASM, NULL, codeBin);

fileName.erase(fileName.size() - 3, 3);
fileName.append("txt");
Expand All @@ -172,7 +215,7 @@ int _tmain(int argc, _TCHAR* argv[])
}
cout << endl;

writeLUT();
writeLUT(codeBin);
}
return 0;
}
86 changes: 0 additions & 86 deletions D3D_Shaders/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,90 +7,4 @@

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include "stdint.h"
#include "D3DCompiler.h"
#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

// VS2013 BUG WORKAROUND: Make sure this class has a unique type name!
class AssemblerParseError: public exception {
public:
string context, desc, msg;
int line_no;

AssemblerParseError(string context, string desc) :
context(context),
desc(desc),
line_no(0)
{
update_msg();
}

void update_msg()
{
msg = "Assembly parse error";
if (line_no > 0)
msg += string(" on line ") + to_string(line_no);
msg += ", " + desc + ":\n\"" + context + "\"";
}

const char* what() const
{
return msg.c_str();
}
};

struct shader_ins
{
union {
struct {
// XXX Beware that bitfield packing is not defined in
// the C/C++ standards and this is relying on compiler
// specific packing. This approach is not recommended.

unsigned opcode : 11;
unsigned _11_23 : 13;
unsigned length : 7;
unsigned extended : 1;
};
DWORD op;
};
};
struct token_operand
{
union {
struct {
// XXX Beware that bitfield packing is not defined in
// the C/C++ standards and this is relying on compiler
// specific packing. This approach is not recommended.

unsigned comps_enum : 2; /* sm4_operands_comps */
unsigned mode : 2; /* sm4_operand_mode */
unsigned sel : 8;
unsigned file : 8; /* SM_FILE */
unsigned num_indices : 2;
unsigned index0_repr : 3; /* sm4_operand_index_repr */
unsigned index1_repr : 3; /* sm4_operand_index_repr */
unsigned index2_repr : 3; /* sm4_operand_index_repr */
unsigned extended : 1;
};
DWORD op;
};
};

vector<string> stringToLines(const char* start, size_t size);
HRESULT disassembler(vector<byte> *buffer, vector<byte> *ret, const char *comment,
int hexdump = 0, bool d3dcompiler_46_compat = false,
bool disassemble_undecipherable_data = false,
bool patch_cb_offsets = false);
HRESULT disassemblerDX9(vector<byte> *buffer, vector<byte> *ret, const char *comment);
vector<byte> assembler(vector<char> *asmFile, vector<byte> origBytecode, vector<AssemblerParseError> *parse_errors = NULL);
vector<byte> assemblerDX9(vector<char> *asmFile);
void writeLUT();
HRESULT AssembleFluganWithSignatureParsing(vector<char> *assembly, vector<byte> *result_bytecode, vector<AssemblerParseError> *parse_errors = NULL);
vector<byte> AssembleFluganWithOptionalSignatureParsing(vector<char> *assembly, bool assemble_signatures, vector<byte> *orig_bytecode, vector<AssemblerParseError> *parse_errors = NULL);
Loading

0 comments on commit e1fd299

Please sign in to comment.