Skip to content

Commit

Permalink
Added code to support (some) DX9 (SM1-3) features.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nintynuts committed May 15, 2022
1 parent 184c418 commit a2f0d2b
Show file tree
Hide file tree
Showing 4 changed files with 393 additions and 310 deletions.
116 changes: 4 additions & 112 deletions D3D_Shaders/Assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#include "float.h"
#include <stdexcept>

#if MIGOTO_DX == 9
#include <d3dx9shader.h>
#endif
#define MIGOTO_DX 11

using namespace std;

Expand Down Expand Up @@ -1477,13 +1475,12 @@ static vector<DWORD> assemble_undecipherable_custom_data(string& s, vector<DWORD
return v;
}

static vector<DWORD> assembleIns(string s)
static vector<DWORD>& assembleIns(string s)
{
unsigned msaa_samples = 0;

if (hackMap.find(s) != hackMap.end()) {
auto v = hackMap[s];
return v;
return hackMap[s];
}
DWORD op = 0;
shader_ins* ins = (shader_ins*)&op;
Expand Down Expand Up @@ -2564,51 +2561,6 @@ vector<string> stringToLines(const char* start, size_t size)
}
return lines;
}
static vector<string> stringToLinesDX9(const char* start, size_t size) {
vector<string> lines;
const char* pStart = start;
const char* pEnd = pStart;
const char* pRealEnd = pStart + size;
while (true) {
while (*pEnd != '\n' && pEnd < pRealEnd) {
pEnd++;
}
if (*pStart == 0) {
break;
}
string s(pStart, pEnd++);
pStart = pEnd;
lines.push_back(s);
if (pStart >= pRealEnd) {
break;
}
}
for (unsigned int i = 0; i < lines.size(); i++) {
string s = lines[i];
// Bug fixed: This would not strip carriage returns from DOS
// style newlines if they were the only character on the line,
// corrupting the resulting shader binary. -DarkStarSword
if (s.size() >= 1 && s[s.size() - 1] == '\r')
s.erase(--s.end());

// Strip whitespace from the end of each line. This isn't
// strictly necessary, but the MS disassembler inserts an extra
// space after "ret ", "else " and "endif ", which has been a
// gotcha for trying to match it with ShaderRegex since it's
// easy to miss the fact that there is a space there and not
// understand why the pattern isn't matching. By removing
// excess spaces from the end of each line now we can make this
// gotcha go away.
while (s.size() >= 1 && s[s.size() - 1] == ' ')
s.erase(--s.end());

while (s.size() >= 1 && s[0] == ' ')
s.erase(s.begin());

lines[i] = s;
}
return lines;
}

static void hexdump_instruction(string& s, vector<DWORD>& v,
vector<string>& lines, DWORD* i,
Expand Down Expand Up @@ -2850,46 +2802,6 @@ static inline void replace_cb_offsets_with_indices(string* line)
*line += suffix;
}

#if MIGOTO_DX == 9
HRESULT disassemblerDX9(vector<byte>* buffer, vector<byte>* ret, const char* comment)
{
char* asmBuffer;
size_t asmSize;
vector<byte> asmBuf;
ID3DBlob* pDissassembly = NULL;
LPD3DXBUFFER pD3DXDissassembly = NULL;
HRESULT ok = D3DDisassemble(buffer->data(), buffer->size(), D3D_DISASM_ENABLE_DEFAULT_VALUE_PRINTS, comment, &pDissassembly);
if (FAILED(ok))
ok = D3DDisassemble(buffer->data(), buffer->size(), NULL, comment, &pDissassembly);
if (FAILED(ok))
ok = D3DDisassemble(buffer->data(), buffer->size(), D3D_DISASM_DISABLE_DEBUG_INFO, comment, &pDissassembly);
if (FAILED(ok)) {
//below sometimes give an access violation for some reason
//ok = D3DXDisassembleShader((DWORD*)buffer->data(), false, NULL, &pD3DXDissassembly);
//if (FAILED(ok))
return ok;
//asmBuffer = (char*)pD3DXDissassembly->GetBufferPointer();
//asmSize = pD3DXDissassembly->GetBufferSize();
} else {
asmBuffer = (char*)pDissassembly->GetBufferPointer();
asmSize = pDissassembly->GetBufferSize();
}
vector<string> lines = stringToLinesDX9(asmBuffer, asmSize);
ret->clear();
for (size_t i = 0; i < lines.size(); i++) {
for (size_t j = 0; j < lines[i].size(); j++) {
ret->insert(ret->end(), lines[i][j]);
}
ret->insert(ret->end(), '\n');
}
if (pDissassembly)
pDissassembly->Release();
if (pD3DXDissassembly)
pD3DXDissassembly->Release();
return S_OK;
}
#endif

HRESULT disassembler(vector<byte>* buffer, vector<byte>* ret, const char* comment,
int hexdump, bool d3dcompiler_46_compat,
bool disassemble_undecipherable_data,
Expand Down Expand Up @@ -3101,7 +3013,7 @@ static vector<DWORD> ComputeHash(byte const* input, DWORD size)
bool sizeHash56 = sizeHash >= 56;
DWORD restSize = sizeHash56 ? 120 - 56 : 56 - sizeHash;
DWORD loopSize = (size + 8 + restSize) >> 6;
DWORD Dst[16];
DWORD Dst[16]{};
DWORD Data[] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
DWORD loopSize2 = loopSize - (sizeHash56 ? 2 : 1);
DWORD start_0 = 0;
Expand Down Expand Up @@ -3341,23 +3253,3 @@ vector<byte> assembler(vector<char>* asmFile, vector<byte> origBytecode,
dwordBuffer[4] = hash[3];
return origBytecode;
}
#if MIGOTO_DX == 9
vector<byte> assemblerDX9(vector<char>* asmFile)
{
vector<byte> ret;
LPD3DXBUFFER pAssembly;
HRESULT hr = D3DXAssembleShader(asmFile->data(), (UINT)asmFile->size(), NULL, NULL, 0, &pAssembly, NULL);
if (!FAILED(hr)) {
size_t size = pAssembly->GetBufferSize();
LPVOID buffer = pAssembly->GetBufferPointer();
ret.resize(size);
std::memcpy(ret.data(), buffer, size);
pAssembly->Release();
// FIXME: Pass warnings back to the caller
} else {
// FIXME: Pass error messages back to the caller
throw std::invalid_argument("assembler: Bad shader assembly (FIXME: RETURN ERROR MESSAGES)");
}
return ret;
}
#endif
Loading

0 comments on commit a2f0d2b

Please sign in to comment.