From 670f86af96162f6c4b50e0a37b6c0edc8850952b Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Thu, 10 Oct 2024 23:05:40 -0700 Subject: [PATCH] Check index buffer size before creation --- Dllmain/BuildNo.rc | 2 +- ddraw/IDirect3DDeviceX.cpp | 20 +++++++++++++------- ddraw/IDirect3DVertexBufferX.cpp | 3 ++- ddraw/IDirectDrawSurfaceX.cpp | 6 ++++-- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Dllmain/BuildNo.rc b/Dllmain/BuildNo.rc index a0d5b7b2..5d70f5b6 100644 --- a/Dllmain/BuildNo.rc +++ b/Dllmain/BuildNo.rc @@ -1 +1 @@ -#define BUILD_NUMBER 7236 +#define BUILD_NUMBER 7237 diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index 9697ae27..b2887e4a 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -3665,13 +3665,6 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitiveVB(D3DPRIMITIVETYPE dptPrimitive return DDERR_GENERIC; } - LPDIRECT3DINDEXBUFFER9 d3d9IndexBuffer = pVertexBufferX->SetupIndexBuffer(lpwIndices, dwIndexCount); - if (!d3d9IndexBuffer) - { - LOG_LIMIT(100, __FUNCTION__ << " Error: could not get d3d9 index buffer!"); - return DDERR_GENERIC; - } - DWORD FVF = pVertexBufferX->GetFVF9(); // Set fixed function vertex type @@ -3681,6 +3674,19 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitiveVB(D3DPRIMITIVETYPE dptPrimitive return DDERR_INVALIDPARAMS; } + // No operation to performed + if (dwIndexCount == 0) + { + return D3D_OK; + } + + LPDIRECT3DINDEXBUFFER9 d3d9IndexBuffer = pVertexBufferX->SetupIndexBuffer(lpwIndices, dwIndexCount); + if (!d3d9IndexBuffer) + { + LOG_LIMIT(100, __FUNCTION__ << " Error: could not get d3d9 index buffer!"); + return DDERR_GENERIC; + } + // Set stream source (*d3d9Device)->SetStreamSource(0, d3d9VertexBuffer, 0, GetVertexStride(FVF)); diff --git a/ddraw/IDirect3DVertexBufferX.cpp b/ddraw/IDirect3DVertexBufferX.cpp index a57d3085..569a0132 100644 --- a/ddraw/IDirect3DVertexBufferX.cpp +++ b/ddraw/IDirect3DVertexBufferX.cpp @@ -690,7 +690,7 @@ LPDIRECT3DINDEXBUFFER9 m_IDirect3DVertexBufferX::SetupIndexBuffer(LPWORD lpwIndi if (FAILED(hr)) { - LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create index buffer: " << (D3DERR)hr); + LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create index buffer: " << (D3DERR)hr << " Size: " << NewIndexSize); return nullptr; } @@ -726,6 +726,7 @@ void m_IDirect3DVertexBufferX::ReleaseD3D9IndexBuffer() Logging::Log() << __FUNCTION__ << " (" << this << ")" << " Error: there is still a reference to 'd3d9IndexBuffer' " << ref; } d3d9IndexBuffer = nullptr; + IndexBufferSize = 0; } } diff --git a/ddraw/IDirectDrawSurfaceX.cpp b/ddraw/IDirectDrawSurfaceX.cpp index 08312e55..cedecc95 100644 --- a/ddraw/IDirectDrawSurfaceX.cpp +++ b/ddraw/IDirectDrawSurfaceX.cpp @@ -23,6 +23,8 @@ #include "d3dx9.h" #include "Utils\Utils.h" +constexpr DWORD ExtraDataBufferSize = 200; + // Used to allow presenting non-primary surfaces in case the primary surface present fails bool dirtyFlag = false; bool SceneReady = false; @@ -4840,7 +4842,7 @@ HRESULT m_IDirectDrawSurfaceX::CreateDCSurface() ZeroMemory(surface.emu->bmiMemory, sizeof(surface.emu->bmiMemory)); surface.emu->bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); surface.emu->bmi->bmiHeader.biWidth = Width; - surface.emu->bmi->bmiHeader.biHeight = -((LONG)Height + 200); + surface.emu->bmi->bmiHeader.biHeight = -((LONG)Height + ExtraDataBufferSize); surface.emu->bmi->bmiHeader.biPlanes = 1; surface.emu->bmi->bmiHeader.biBitCount = (WORD)surface.BitCount; surface.emu->bmi->bmiHeader.biCompression = @@ -5492,7 +5494,7 @@ void m_IDirectDrawSurfaceX::LockEmuLock(LPRECT lpDestRect, LPDDSURFACEDESC2 lpDD EmuLock.Height = lpDDSurfaceDesc->dwHeight; // Update surface memory and pitch - size_t Size = NewPitch * lpDDSurfaceDesc->dwHeight; + size_t Size = NewPitch * (lpDDSurfaceDesc->dwHeight + ExtraDataBufferSize); if (EmuLock.Mem.size() < Size) { EmuLock.Mem.resize(Size);