Skip to content

Commit

Permalink
use precise shader blob length calcuation
Browse files Browse the repository at this point in the history
properly fix for problems with false positive end/comment block detections
  • Loading branch information
megai2 committed Apr 10, 2021
1 parent cae10c4 commit 0163bd3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
52 changes: 16 additions & 36 deletions d912pxy/d912pxy_shader_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,47 +50,27 @@ void d912pxy_shader_db::UnInit()
d912pxy_noncom::UnInit();
}

d912pxy_shader_uid d912pxy_shader_db::GetUID(DWORD * code, UINT32* len)
UINT32 d912pxy_shader_db::CalcTokenCount(DWORD* code)
{
/*
//this is not optimal! but fancy? FUU!
UINT ctr = 0;
while (code[ctr] != 0x0000FFFF)
++ctr;
UINT32 ret = 0;
//use inst len for sh ver 2 and up
bool sm1 = D3DSHADER_VERSION_MAJOR(code[ret++]) <= 1;

*len = (ctr >> 2) + 1;
return d912pxy::Hash64(d912pxy::MemoryBlock((void*)code, len));
*/
d912pxy_dxbc9::token tok;
tok.load(code[ret], d912pxy_dxbc9::token_type::unk);

UINT64 hash = 0xcbf29ce484222325;
UINT ctr = 0;

DWORD iter = code[ctr >> 2];
while (iter != 0x0000FFFF)
{
UINT8 dataByte = ((UINT8*)code)[ctr];

hash = hash ^ dataByte;
hash = hash * 1099511628211;
++ctr;

if ((ctr % 4) == 0)
{
iter = code[ctr >> 2];
if (d912pxy_dxbc9::token_comment::test(iter))
{
d912pxy_dxbc9::token_comment comment;
comment.load(iter);
ctr += (1 + comment.length) * sizeof(DWORD);
}
}


while (tok.iType != d912pxy_dxbc9::token_type::end)
{
ret += tok.length(sm1);
tok.load(code[ret], d912pxy_dxbc9::token_type::unk);
}
return ret + 1;
}

*len = (ctr >> 2) + 1;

return hash;
d912pxy_shader_uid d912pxy_shader_db::GetUID(DWORD * code, UINT32* len)
{
*len = CalcTokenCount(code);
return d912pxy::Hash64(d912pxy::MemoryArea((void*)code, (*len * sizeof(DWORD)))).value;
}

d912pxy_shader_pair_hash_type d912pxy_shader_db::GetPairUID(d912pxy_shader * vs, d912pxy_shader * ps)
Expand Down
1 change: 1 addition & 0 deletions d912pxy/d912pxy_shader_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class d912pxy_shader_db : public d912pxy_noncom
void Init();
void UnInit();

UINT32 CalcTokenCount(DWORD* code);
d912pxy_shader_uid GetUID(DWORD* code, UINT32* len);

d912pxy_shader_pair_hash_type GetPairUID(d912pxy_shader* vs, d912pxy_shader* ps);
Expand Down

0 comments on commit 0163bd3

Please sign in to comment.