Skip to content

Commit

Permalink
d3d8: fixed D3D_g_Stream reference offset to correct location and ren…
Browse files Browse the repository at this point in the history
…ame old reference to D3D_g_Stream_i_pVertexBuffer reference
  • Loading branch information
RadWolfie committed Dec 9, 2024
1 parent 1ab6832 commit 1e42478
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 89 deletions.
1 change: 1 addition & 0 deletions include/xref/d3d8.def
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ XREF_SYMBOL(D3D_g_ComplexRenderState)
XREF_SYMBOL(D3D_g_DeferredTextureState)
XREF_SYMBOL(D3D_g_pDevice)
XREF_SYMBOL(D3D_g_Stream)
XREF_SYMBOL(D3D_g_Stream_i_pVertexBuffer)
XREF_SYMBOL(D3DRS_BackFillMode)
XREF_SYMBOL(D3DRS_CullMode)
XREF_SYMBOL(D3DRS_Dxt1NoiseEnable)
Expand Down
5 changes: 3 additions & 2 deletions src/OOVPADatabase/D3D8/3911.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1742,10 +1742,11 @@ OOVPA_SIG_MATCH(
// ******************************************************************
OOVPA_SIG_HEADER_XREF(D3DDevice_SetStreamSource,
3911,
XRefOne)
XRefTwo)
OOVPA_SIG_MATCH(

XREF_ENTRY(0x23, XREF_D3D_g_Stream), // Derived
XREF_ENTRY(0x23, XREF_D3D_g_Stream_i_pVertexBuffer), // Derived
XREF_ENTRY(0x53, XREF_D3D_g_Stream), // Derived

// D3DDevice_SetStreamSource+0x00 : push ebx
{ 0x00, 0x53 },
Expand Down
5 changes: 3 additions & 2 deletions src/OOVPADatabase/D3D8/4034.inl
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,11 @@ OOVPA_SIG_MATCH(
// * D3DDevice_SetStreamSource
// ******************************************************************
OOVPA_XREF(D3DDevice_SetStreamSource, 4034, 1 + 14, // 1+28
XRefOne)
XRefTwo)
{

XREF_ENTRY(0x22, XREF_D3D_g_Stream), // Derived
XREF_ENTRY(0x22, XREF_D3D_g_Stream_i_pVertexBuffer), // Derived
XREF_ENTRY(0x52, XREF_D3D_g_Stream), // Derived

#if 0
{ 0x00, 0x8B },
Expand Down
5 changes: 3 additions & 2 deletions src/OOVPADatabase/D3D8LTCG/3911.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1165,10 +1165,11 @@ OOVPA_SIG_MATCH(
//7406810300000800
OOVPA_SIG_HEADER_XREF(D3DDevice_SetStreamSource_8__LTCG_edx1,
2039,
XRefOne)
XRefTwo)
OOVPA_SIG_MATCH(

XREF_ENTRY(0x47, XREF_D3D_g_Stream), // Derived
XREF_ENTRY(0x47, XREF_D3D_g_Stream_i_pVertexBuffer), // Derived
XREF_ENTRY(0x77, XREF_D3D_g_Stream), // Derived

{ 0x00, 0x53 },
{ 0x01, 0x8B },
Expand Down
5 changes: 3 additions & 2 deletions src/OOVPADatabase/D3D8LTCG/4039.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1119,10 +1119,11 @@ OOVPA_SIG_MATCH(
//7406810300000800 ...C20800
OOVPA_SIG_HEADER_XREF(D3DDevice_SetStreamSource_8__LTCG_eax1,
2040,
XRefOne)
XRefTwo)
OOVPA_SIG_MATCH(

XREF_ENTRY(0x23, XREF_D3D_g_Stream), // Derived
XREF_ENTRY(0x23, XREF_D3D_g_Stream_i_pVertexBuffer), // Derived
XREF_ENTRY(0x53, XREF_D3D_g_Stream), // Derived

{ 0x00, 0x8B },
{ 0x01, 0x0D },
Expand Down
5 changes: 3 additions & 2 deletions src/OOVPADatabase/D3D8LTCG/4432.inl
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ OOVPA_SIG_MATCH(
//7406810300000800
OOVPA_SIG_HEADER_XREF(D3DDevice_SetStreamSource,
1044,
XRefOne)
XRefTwo)
OOVPA_SIG_MATCH(

XREF_ENTRY(0x22, XREF_D3D_g_Stream), // Derived
XREF_ENTRY(0x22, XREF_D3D_g_Stream_i_pVertexBuffer), // Derived
XREF_ENTRY(0x52, XREF_D3D_g_Stream), // Derived

{ 0x00, 0x8B },
{ 0x01, 0x0D },
Expand Down
10 changes: 6 additions & 4 deletions src/OOVPADatabase/D3D8LTCG/5849.inl
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,11 @@ OOVPA_SIG_MATCH(
// ******************************************************************
OOVPA_SIG_HEADER_XREF(D3DDevice_SetStreamSource_0__LTCG_eax1_edi2_ebx3,
2058,
XRefOne)
XRefTwo)
OOVPA_SIG_MATCH(

XREF_ENTRY(0x19, XREF_D3D_g_Stream), // Derived
XREF_ENTRY(0x19, XREF_D3D_g_Stream_i_pVertexBuffer), // Derived
XREF_ENTRY(0x49, XREF_D3D_g_Stream), // Derived

// test edi, edi; mov ecx, [...]
OV_MATCH(0x00, 0x85, 0xFF, 0x8B, 0x0D),
Expand All @@ -725,10 +726,11 @@ OOVPA_SIG_MATCH(
//7406810300000800 ...C20400
OOVPA_SIG_HEADER_XREF(D3DDevice_SetStreamSource_4__LTCG_eax1_ebx2,
2058,
XRefOne)
XRefTwo)
OOVPA_SIG_MATCH(

XREF_ENTRY(0x1E, XREF_D3D_g_Stream), // Derived
XREF_ENTRY(0x1E, XREF_D3D_g_Stream_i_pVertexBuffer), // Derived
XREF_ENTRY(0x4E, XREF_D3D_g_Stream), // Derived

{ 0x00, 0x85 },
{ 0x01, 0xDB },
Expand Down
1 change: 1 addition & 0 deletions src/lib/libXbSymbolDatabase.c
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ bool XbSymbolDatabase_CreateXbSymbolContext(XbSymbolContextHandle* ppHandle,
// D3D
pContext->xref_database[XREF_D3DTSS_TEXCOORDINDEX] = XREF_ADDR_DERIVE; //In use
pContext->xref_database[XREF_D3D_g_Stream] = XREF_ADDR_DERIVE; //In use
pContext->xref_database[XREF_D3D_g_Stream_i_pVertexBuffer] = XREF_ADDR_DERIVE; //In use
pContext->xref_database[XREF_D3DDevice__m_PixelShader_OFFSET] = XREF_ADDR_DERIVE; //
pContext->xref_database[XREF_D3DDevice__m_Textures_OFFSET] = XREF_ADDR_DERIVE; //
pContext->xref_database[XREF_D3DDevice__m_Palettes_OFFSET] = XREF_ADDR_DERIVE; //
Expand Down
84 changes: 11 additions & 73 deletions src/lib/manual_d3d8__ltcg.h
Original file line number Diff line number Diff line change
Expand Up @@ -621,31 +621,6 @@ static void manual_scan_section_dx8_register_D3DTSS(iXbSymbolContext* pContext,
internal_RegisterXRef(pContext, pLibrarySession, XREF_D3D_g_DeferredTextureState, 0, "D3D_g_DeferredTextureState", EmuD3DDeferredTextureState, symbol_variable, call_none, 0, NULL, true);
}

static void manual_scan_section_dx8_register_stream(iXbSymbolContext* pContext,
const iXbSymbolLibrarySession* pLibrarySession,
memptr_t pFunc,
uint32_t iCodeOffsetFor_g_Stream)
{
if (pFunc == NULL) {
return;
}
const XbSDBLibrary* pLibrary = pLibrarySession->pLibrary;

// Read address of Xbox_g_Stream from D3DDevice_SetStreamSource
uint32_t Derived_g_Stream = *((uint32_t*)(pFunc + iCodeOffsetFor_g_Stream));

// Temporary verification - is XREF_D3D_g_Stream derived correctly?
// TODO : Remove this when XREF_D3D_g_Stream derivation is deemed stable
#if 0 // TODO: How can we enforce it for callback?
VerifySymbolAddressAgainstXRef("g_Stream", Derived_g_Stream, XREF_D3D_g_Stream);
#endif

// Now that both Derived XREF and OOVPA-based function-contents match,
// correct base-address (because "g_Stream" is actually "g_Stream"+8") :
Derived_g_Stream -= 8;
pContext->register_func(pLibrary->name, pLibrary->flag, XREF_D3D_g_Stream, "D3D_g_Stream", Derived_g_Stream, 0, symbol_variable, call_none, 0, NULL);
}

// Has dependency on D3D_g_pDevice xref.
static bool manual_scan_section_dx8_register_callbacks(iXbSymbolContext* pContext,
const iXbSymbolLibrarySession* pLibrarySession,
Expand Down Expand Up @@ -712,7 +687,6 @@ static bool manual_scan_section_dx8(iXbSymbolContext* pContext,
{
// Generic usage
memptr_t pSymbolAddr = 0;
int iCodeOffsetFor_g_Stream;
int pXRefOffset = 0; // TODO : Rename into something understandable
uintptr_t virt_start_relative = (uintptr_t)pSection->buffer_lower - pSection->xb_virt_addr;
const XbSDBLibrary* pLibrary = pLibrarySession->pLibrary;
Expand Down Expand Up @@ -829,12 +803,6 @@ static bool manual_scan_section_dx8(iXbSymbolContext* pContext,
manual_scan_section_dx8_register_D3DTSS(pContext, pLibrarySession, pSymbolAddr, pXRefOffset);
}

// Locate Xbox symbol "g_Stream" and store it's address
// verified for D3D8 with 4361, 4627, 5344, 5558, 5659, 5788, 5849, 5933
// and verified for LTCG with 4432, 4627, 5344, 5558, 5849
iCodeOffsetFor_g_Stream = 0x22;

// Will this affect not being in D3D8 vs D3D8LTCG?
pSymbolEntry = NULL;
pSymbolEntryRevision = NULL;
pSymbolAddr = internal_SymbolDatabaseList_ScanByReference(pContext,
Expand All @@ -848,51 +816,21 @@ static bool manual_scan_section_dx8(iXbSymbolContext* pContext,
&pSymbolEntry,
&pSymbolEntryRevision);

if (pLibrary->flag == XbSymbolLib_D3D8) {
if (pLibrary->build_version < 4034) {
iCodeOffsetFor_g_Stream = 0x23; // verified for 3911
}
}
else { // D3D8LTCG
typedef struct {
XbSDBParamType params[3];
unsigned short revisionNumber;
int iCodeOffsetFor_g_StreamReturn;
} func_search;
const func_search findRevisions[] = {
// D3DDevice_SetStreamSource_4__LTCG_eax1_ebx2
{ .params = { param_eax, param_ebx, param_psh }, .revisionNumber = 2058, .iCodeOffsetFor_g_StreamReturn = 0x1E }, // TODO: Verify 4034
// D3DDevice_SetStreamSource_8__LTCG_eax1
{ .params = { param_eax, param_psh, param_psh }, .revisionNumber = 2040, .iCodeOffsetFor_g_StreamReturn = 0x23 }, // verified for 4039
// D3DDevice_SetStreamSource_8__LTCG_edx1
{ .params = { param_edx, param_psh, param_psh }, .revisionNumber = 2039, .iCodeOffsetFor_g_StreamReturn = 0x47 }, // verified for 3925
};
size_t findRevisionsSize = XBSDB_ARRAY_SIZE(findRevisions);
if (pSymbolAddr != 0) {
const XbSDBSymbolParam* param_list = pSymbolEntry->param_list;
// Verify symbol has three parameters.
if (pSymbolEntry->param_count == 3) {
// Optimized code to look up each parameter and revision number match without direct reference to OOVPA signature.
for (unsigned i = 0; i < findRevisionsSize; i++) {
if (findRevisions[i].revisionNumber == pSymbolEntryRevision->Version &&
findRevisions[i].params[0] == param_list[0].type &&
findRevisions[i].params[1] == param_list[1].type &&
findRevisions[i].params[2] == param_list[2].type) {
iCodeOffsetFor_g_Stream = findRevisions[i].iCodeOffsetFor_g_StreamReturn;
break;
}
}
}
}
}

if (pSymbolAddr != 0) {
// Self register D3DDevice_SetStreamSource

// NOTE: Is a requirement to align properly.
pSymbolAddr += virt_start_relative;
xbaddr D3D_g_Stream = pContext->xref_database[XREF_D3D_g_Stream];
xbaddr D3D_g_Stream_i_pVertexBuffer = pContext->xref_database[XREF_D3D_g_Stream_i_pVertexBuffer];

// Verify both variables are offset correctly.
if (D3D_g_Stream_i_pVertexBuffer - D3D_g_Stream != 8) {
// If not correct, then do the correction in case of third-party rely on this.
output_message_format(&pContext->output, XB_OUTPUT_MESSAGE_ERROR, "D3D_g_Stream_i_pVertexBuffer (0x%08X) - D3D_g_Stream (0x%08X) != 0x8", D3D_g_Stream_i_pVertexBuffer, D3D_g_Stream);
pContext->xref_database[XREF_D3D_g_Stream] = D3D_g_Stream_i_pVertexBuffer - 8;
}

manual_scan_section_dx8_register_stream(pContext, pLibrarySession, pSymbolAddr, iCodeOffsetFor_g_Stream);
// TODO: Use internal_FindByReferenceHelper to get OOVPA revision. Or better yet, do self register.
internal_RegisterValidXRefAddr(pContext, pLibrary->name, pLibrary->flag, XREF_D3D_g_Stream, 0, "D3D_g_Stream", symbol_variable, call_none, 0, NULL);
}

bComplete = manual_scan_section_dx8_register_callbacks(pContext, pLibrarySession, pLibraryDB, pSection);
Expand Down
4 changes: 2 additions & 2 deletions src/test/libverify/D3D8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ void getLibraryD3D8(library_db& lib_db)
// TODO: database is incomplete + 3 internal reference need to be exclude.
// Excluding:
// - 3 internal manual scan reference
// - 1 variables
// - 2 variables
// - 5 internal offsets
lib_db.xref_exclude = 9;
lib_db.xref_exclude = 10;
}

0 comments on commit 1e42478

Please sign in to comment.