Fix audio buffer allocation

Use prediction if do have enough buffer purpose before allow allocate.
This fixed Mafia title for loading "running man" mission.

Plus add log info for SetEvent failure.
This commit is contained in:
RadWolfie 2018-04-18 16:24:56 -05:00
parent d8246a0074
commit cc781826c3
2 changed files with 16 additions and 7 deletions

View File

@ -204,7 +204,7 @@ static XTL::X_CDirectSoundStream* g_pDSoundStreamCache[SOUNDSTREAM_CACHE_SIZE]
static int g_bDSoundCreateCalled = FALSE;
unsigned int g_iDSoundSynchPlaybackCounter = 0;
// Managed memory xbox audio variables
#define X_DS_SGE_COUNT_MAX 2048
#define X_DS_SGE_COUNT_MAX 2047
#define X_DS_SGE_PAGE_MAX (4 * ONE_KB)
#define X_DS_SGE_SIZE_MAX (X_DS_SGE_COUNT_MAX * X_DS_SGE_PAGE_MAX)
DWORD g_dwXbMemAllocated = 0;
@ -964,7 +964,7 @@ HRESULT WINAPI XTL::EMUPATCH(DirectSoundCreateBuffer)
}
}
//If out of space, return out of memory.
if (ppDSoundBufferCache == nullptr || !DSoundSGEMenAllocCheck()) {
if (ppDSoundBufferCache == nullptr || !DSoundSGEMenAllocCheck(pdsbd->dwBufferBytes)) {
hRet = DSERR_OUTOFMEMORY;
} else {
@ -998,7 +998,7 @@ HRESULT WINAPI XTL::EMUPATCH(DirectSoundCreateBuffer)
GeneratePCMFormat(DSBufferDesc, pdsbd->lpwfxFormat, (*ppBuffer)->EmuFlags, pdsbd->dwBufferBytes, &(*ppBuffer)->X_BufferCache, (*ppBuffer)->X_BufferCacheSize);
(*ppBuffer)->EmuBufferDesc = DSBufferDesc;
DbgPrintf("EmuDSound: DirectSoundCreateBuffer, *ppBuffer := 0x%.08X, bytes := 0x%.08X\n", *ppBuffer, (*ppBuffer)->EmuBufferDesc.dwBufferBytes);
DbgPrintf("EmuDSound: DirectSoundCreateBuffer, *ppBuffer := 0x%08X, bytes := 0x%08X\n", *ppBuffer, (*ppBuffer)->EmuBufferDesc.dwBufferBytes);
DSoundBufferCreate(&DSBufferDesc, (*ppBuffer)->EmuDirectSoundBuffer8);
if (pdsbd->dwFlags & DSBCAPS_CTRL3D) {
@ -1007,6 +1007,11 @@ HRESULT WINAPI XTL::EMUPATCH(DirectSoundCreateBuffer)
*ppDSoundBufferCache = *ppBuffer;
}
printf("DEBUG: ==DirectSoundCreateBuffer==\n");
printf("DEBUG: *ppBuffer = %8X | requestSize = %8X\n", *ppBuffer, pdsbd->dwBufferBytes);
printf("DEBUG: g_dwXbMemAllocated = %8X\n", g_dwXbMemAllocated);
printf("DEBUG: ===========================\n");
leaveCriticalSection;
return hRet;
@ -1075,7 +1080,7 @@ HRESULT WINAPI XTL::EMUPATCH(IDirectSoundBuffer_SetBufferData)
}
HRESULT hRet = DSERR_OUTOFMEMORY;
if (DSoundSGEMenAllocCheck()) {
if (DSoundSGEMenAllocCheck(dwBufferBytes)) {
// Confirmed it perform a reset to default.
DSoundBufferRegionSetDefault(pThis);

View File

@ -71,8 +71,8 @@ inline void DSoundSGEMemAlloc(DWORD size) {
inline void DSoundSGEMemDealloc(DWORD size) {
g_dwXbMemAllocated -= size;
}
inline bool DSoundSGEMenAllocCheck() {
int leftOverSize = X_DS_SGE_SIZE_MAX - g_dwXbMemAllocated;
inline bool DSoundSGEMenAllocCheck(DWORD sizeRequest) {
int leftOverSize = X_DS_SGE_SIZE_MAX - (g_dwXbMemAllocated + sizeRequest);
// Don't let xbox title to alloc any more.
if (leftOverSize < 0) {
return false;
@ -729,7 +729,11 @@ inline void DSoundStreamClearPacket(
if (Xb_lpfnCallback != xbnullptr) {
Xb_lpfnCallback(Xb_lpvContext, buffer->xmp_data.pContext, status);
} else if (buffer->xmp_data.hCompletionEvent != 0) {
SetEvent(buffer->xmp_data.hCompletionEvent);
BOOL checkHandle = SetEvent(buffer->xmp_data.hCompletionEvent);
if (checkHandle == 0) {
DWORD error = GetLastError();
EmuWarning("DSOUND: Unable to set event on packet's hCompletionEvent. %8X | error = %8X", buffer->xmp_data.hCompletionEvent, error);
}
}
}