From 8e03b64ca46f84748ccfbcd6c78267cfb45557ea Mon Sep 17 00:00:00 2001 From: WinterSnowfall Date: Sun, 8 Sep 2024 00:56:18 +0300 Subject: [PATCH] [d3d8] Validate viewport dimensions on SetViewport --- src/d3d8/d3d8_device.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/d3d8/d3d8_device.cpp b/src/d3d8/d3d8_device.cpp index ec224298..9eee94f0 100644 --- a/src/d3d8/d3d8_device.cpp +++ b/src/d3d8/d3d8_device.cpp @@ -926,6 +926,22 @@ namespace dxvk { } HRESULT STDMETHODCALLTYPE D3D8Device::SetViewport(const D3DVIEWPORT8* pViewport) { + if (likely(pViewport != nullptr)) { + // we need a valid render target to validate the viewport + if (unlikely(m_renderTarget == nullptr)) + return D3DERR_INVALIDCALL; + + D3DSURFACE_DESC rtDesc; + HRESULT res = m_renderTarget->GetDesc(&rtDesc); + + // D3D8 will fail when setting a viewport that's outside of the + // current render target, although this apparently works in D3D9 + if (likely(SUCCEEDED(res)) && + unlikely(pViewport->X + pViewport->Width > rtDesc.Width || + pViewport->Y + pViewport->Height > rtDesc.Height)) + return D3DERR_INVALIDCALL; + } + StateChange(); return GetD3D9()->SetViewport(reinterpret_cast(pViewport)); }