From 917d6d679e129ad40c921db0c2b071224e82050a Mon Sep 17 00:00:00 2001 From: Daniel Kollmann Date: Sun, 14 May 2023 15:58:08 +0200 Subject: [PATCH 01/20] Added support for transforming homogenous W geometry into world geometry. --- Settings/Settings.h | 14 +++ ddraw/DebugOverlay.cpp | 3 +- ddraw/DebugOverlay.h | 2 +- ddraw/IDirect3DDeviceX.cpp | 188 +++++++++++++++++++++++++++++++++++-- ddraw/IDirect3DDeviceX.h | 4 + ddraw/RenderData.h | 37 ++++++++ dxwrapper.vcxproj | 1 + dxwrapper.vcxproj.filters | 5 +- 8 files changed, 245 insertions(+), 9 deletions(-) create mode 100644 ddraw/RenderData.h diff --git a/Settings/Settings.h b/Settings/Settings.h index 0ef68a40..30e33503 100644 --- a/Settings/Settings.h +++ b/Settings/Settings.h @@ -43,6 +43,13 @@ visit(DdrawOverrideStencilFormat) \ visit(DdrawResolutionHack) \ visit(DdrawUseDirect3D9Ex) \ + visit(DdrawConvertHomogeneousW) \ + visit(DdrawConvertHomogeneousToWorld) \ + visit(DdrawConvertHomogeneousToWorldUseGameCamera) \ + visit(DdrawConvertHomogeneousToWorldFOV) \ + visit(DdrawConvertHomogeneousToWorldNearPlane) \ + visit(DdrawConvertHomogeneousToWorldFarPlane) \ + visit(DdrawConvertHomogeneousToWorldDepthOffset) \ visit(DdrawUseNativeResolution) \ visit(DdrawEnableMouseHook) \ visit(DdrawHookSystem32) \ @@ -209,6 +216,13 @@ struct CONFIG bool DdrawIntegerScalingClamp = false; // Scales the screen by an integer value to help preserve video quality bool DdrawMaintainAspectRatio = false; // Keeps the current DirectDraw aspect ratio when overriding the game's resolution bool DdrawUseDirect3D9Ex = false; // Use Direct3D9Ex extensions for Dd7to9 + bool DdrawConvertHomogeneousW = false; // Convert primites using D3DFVF_XYZRHW to D3DFVF_XYZW. + bool DdrawConvertHomogeneousToWorld = false; // Convert primitives back into a world space. Needed for RTX. + bool DdrawConvertHomogeneousToWorldUseGameCamera = false; // Use the game's view matrix instead of replacing it with our own. + float DdrawConvertHomogeneousToWorldFOV = 90.0f; // The field of view of the camera used to reconstruct the original 3D world. + float DdrawConvertHomogeneousToWorldNearPlane = 1.0f; // The near plane of the camera used to reconstruct the original 3D world. + float DdrawConvertHomogeneousToWorldFarPlane = 1000.0f; // The far plane of the camera used to reconstruct the original 3D world. + float DdrawConvertHomogeneousToWorldDepthOffset = 0.0f; // The offset to add to the geometry so it does not clip into the near plane. bool DdrawUseNativeResolution = false; // Uses the current screen resolution for Dd7to9 DWORD DdrawClippedWidth = 0; // Used to scaled Direct3d9 to use this width when using Dd7to9 DWORD DdrawClippedHeight = 0; // Used to scaled Direct3d9 to use this height when using Dd7to9 diff --git a/ddraw/DebugOverlay.cpp b/ddraw/DebugOverlay.cpp index 2a52154f..d5763cd5 100644 --- a/ddraw/DebugOverlay.cpp +++ b/ddraw/DebugOverlay.cpp @@ -15,6 +15,7 @@ */ #include "DebugOverlay.h" +#include "RenderData.h" #include #define IMGUI_DEFINE_MATH_OPERATORS @@ -98,7 +99,7 @@ void DebugOverlay::BeginScene() ImGui::NewFrame(); } -void DebugOverlay::EndScene() +void DebugOverlay::EndScene(const RenderData &RenderData) { static bool ShowDebugUI = false; if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_LeftAlt)) && diff --git a/ddraw/DebugOverlay.h b/ddraw/DebugOverlay.h index f7e948f2..a0f2c34f 100644 --- a/ddraw/DebugOverlay.h +++ b/ddraw/DebugOverlay.h @@ -48,7 +48,7 @@ class DebugOverlay // Frame functions void BeginScene(); - void EndScene(); + void EndScene(const class RenderData &RenderData); // Functions void SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStateType, LPD3DMATRIX lpD3DMatrix); diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index f820a444..db07013e 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -16,9 +16,10 @@ #include "ddraw.h" #include +#include // Enable for testing only -//#define ENABLE_DEBUGOVERLAY +#define ENABLE_DEBUGOVERLAY #ifdef ENABLE_DEBUGOVERLAY #include "DebugOverlay.h" @@ -338,15 +339,103 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat break; } - HRESULT hr = (*d3d9Device)->SetTransform(dtstTransformStateType, lpD3DMatrix); - - if (SUCCEEDED(hr)) + if(Config.DdrawConvertHomogeneousW) { #ifdef ENABLE_DEBUGOVERLAY + // Set the original matrix DOverlay.SetTransform(dtstTransformStateType, lpD3DMatrix); #endif + + if(dtstTransformStateType == D3DTS_VIEW) + { + D3DVIEWPORT9 Viewport9; + if(SUCCEEDED((*d3d9Device)->GetViewport(&Viewport9))) + { + const float width = (float)Viewport9.Width; + const float height = (float)Viewport9.Height; + const float ratio = width / height; + + // Replace the matrix with one that handles D3DFVF_XYZRHW geometry + _D3DMATRIX view; + ZeroMemory(&view, sizeof(_D3DMATRIX)); + view._11 = 2.0f / width; + view._22 = -2.0f / height; + view._33 = 1.0f; + view._41 = -1.0f; // translate X + view._42 = 1.0f; // translate Y + view._44 = 1.0f; + + if(!Config.DdrawConvertHomogeneousToWorld) + { + // Override the original matrix + std::memcpy(lpD3DMatrix, &view, sizeof(_D3DMATRIX)); + } + else + { + // Override the original matrix + std::memcpy(lpD3DMatrix, &view, sizeof(_D3DMATRIX)); + + // Store the original matrix so it can be restored + std::memcpy(&RenderData.DdrawConvertHomogeneousToWorld_ViewMatrixOriginal, &view, sizeof(_D3DMATRIX)); + + // The Black & White matrix is an ortho camera, so create a perspective one matching the game + const float fov = Config.DdrawConvertHomogeneousToWorldFOV; + const float nearplane = Config.DdrawConvertHomogeneousToWorldNearPlane; + const float farplane = Config.DdrawConvertHomogeneousToWorldFarPlane; + DirectX::XMMATRIX proj = DirectX::XMMatrixPerspectiveFovLH(fov * (3.14159265359f / 180.0f), ratio, nearplane, farplane); + + DirectX::XMStoreFloat4x4((DirectX::XMFLOAT4X4*)&RenderData.DdrawConvertHomogeneousToWorld_ProjectionMatrix, proj); + + DirectX::XMVECTOR up = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); + + DirectX::XMMATRIX viewMatrix; + if(Config.DdrawConvertHomogeneousToWorldUseGameCamera) + { + // To reconstruct the 3D world, we need to know where the camera is and where it is looking + DirectX::XMVECTOR position = DirectX::XMVectorSet(lpD3DMatrix->_41, lpD3DMatrix->_42, lpD3DMatrix->_43, lpD3DMatrix->_44); + DirectX::XMVECTOR direction = DirectX::XMVectorSet(lpD3DMatrix->_31, lpD3DMatrix->_32, lpD3DMatrix->_33, lpD3DMatrix->_34); + + viewMatrix = DirectX::XMMatrixLookToLH(position, direction, up); + } + else + { + const float cameradir = 1.0f; + + DirectX::XMVECTOR pos = DirectX::XMVectorSet(0.0f, 0.0f, -cameradir, 0.0f); + DirectX::XMVECTOR direction = DirectX::XMVectorSet(0.0f, 0.0f, cameradir, 0.0f); + + viewMatrix = DirectX::XMMatrixLookToLH(pos, direction, up); + } + + // Store the 3D view matrix so it can be set later + DirectX::XMStoreFloat4x4((DirectX::XMFLOAT4X4*)&RenderData.DdrawConvertHomogeneousToWorld_ViewMatrix, viewMatrix); + + // Store the view inverse matrix of the game, so we can transform the geometry with it + DirectX::XMMATRIX toViewSpace = DirectX::XMLoadFloat4x4((DirectX::XMFLOAT4X4*)lpD3DMatrix); + DirectX::XMMATRIX vp = DirectX::XMMatrixMultiply(viewMatrix, proj); + DirectX::XMMATRIX vpinv = DirectX::XMMatrixInverse(nullptr, vp); + + DirectX::XMMATRIX depthoffset = DirectX::XMMatrixTranslation(0.0f, 0.0f, Config.DdrawConvertHomogeneousToWorldDepthOffset); + + RenderData.DdrawConvertHomogeneousToWorld_ViewMatrixInverse = DirectX::XMMatrixMultiply(depthoffset, DirectX::XMMatrixMultiply(toViewSpace, vpinv)); + } + } + } + else + { + return D3D_OK; + } } + HRESULT hr = (*d3d9Device)->SetTransform(dtstTransformStateType, lpD3DMatrix); + +#ifdef ENABLE_DEBUGOVERLAY + if (SUCCEEDED(hr) && !Config.DdrawConvertHomogeneousW) + { + DOverlay.SetTransform(dtstTransformStateType, lpD3DMatrix); + } +#endif + return hr; } @@ -1617,7 +1706,7 @@ HRESULT m_IDirect3DDeviceX::EndScene() } #ifdef ENABLE_DEBUGOVERLAY - DOverlay.EndScene(); + DOverlay.EndScene(RenderData); #endif // The IDirect3DDevice7::EndScene method ends a scene that was begun by calling the IDirect3DDevice7::BeginScene method. @@ -2453,11 +2542,98 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy } else { + const UINT stride = GetVertexStride(dwVertexTypeDesc); + + // Handle PositionT + if((dwVertexTypeDesc & D3DFVF_XYZRHW) != 0 && Config.DdrawConvertHomogeneousW) + { + if(!Config.DdrawConvertHomogeneousToWorld) + { + UINT8 *vertex = (UINT8*)lpVertices; + + for (UINT x = 0; x < dwVertexCount; x++) + { + float *pos = (float*) vertex; + + pos[3] = 1.0f; + + vertex += stride; + } + + // Update the FVF + dwVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZW; + } + else + { + const UINT newstride = stride - sizeof(float); + const UINT restSize = stride - sizeof(float) * 4; + + RenderData.DdrawConvertHomogeneousToWorld_IntermediateGeometry.resize(newstride * dwVertexCount); + + UINT8 *sourceVertex = (UINT8*)lpVertices; + UINT8 *targetVertex = (UINT8*)RenderData.DdrawConvertHomogeneousToWorld_IntermediateGeometry.data(); + + lpVertices = targetVertex; + + for (UINT x = 0; x < dwVertexCount; x++) + { + // Transform the vertices into world space + float *srcpos = (float*) sourceVertex; + float *trgtpos = (float*) targetVertex; + + DirectX::XMVECTOR xpos = DirectX::XMVectorSet(srcpos[0], srcpos[1], srcpos[2], srcpos[3]); + + DirectX::XMVECTOR xpos_global = DirectX::XMVector3TransformCoord(xpos, RenderData.DdrawConvertHomogeneousToWorld_ViewMatrixInverse); + + xpos_global = DirectX::XMVectorDivide(xpos_global, DirectX::XMVectorSplatW(xpos_global)); + + trgtpos[0] = DirectX::XMVectorGetX(xpos_global); + trgtpos[1] = DirectX::XMVectorGetY(xpos_global); + trgtpos[2] = DirectX::XMVectorGetZ(xpos_global); + + // Copy the rest + std::memcpy(targetVertex + sizeof(float) * 3, sourceVertex + sizeof(float) * 4, restSize); + + // Move to next vertex + sourceVertex += stride; + targetVertex += newstride; + } + + // Set transform + (*d3d9Device)->SetTransform(D3DTS_VIEW, &RenderData.DdrawConvertHomogeneousToWorld_ViewMatrix); + (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &RenderData.DdrawConvertHomogeneousToWorld_ProjectionMatrix); + + // Update the FVF + const DWORD newVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZ; + + // Set fixed function vertex type + (*d3d9Device)->SetFVF(newVertexTypeDesc); + + // Draw indexed primitive UP + hr = (*d3d9Device)->DrawIndexedPrimitiveUP(dptPrimitiveType, 0, dwVertexCount, GetNumberOfPrimitives(dptPrimitiveType, dwIndexCount), lpIndices, D3DFMT_INDEX16, lpVertices, newstride); + + // Restore transform + _D3DMATRIX identityMatrix; + ZeroMemory(&identityMatrix, sizeof(_D3DMATRIX)); + identityMatrix._11 = 1.0f; + identityMatrix._22 = 1.0f; + identityMatrix._33 = 1.0f; + + (*d3d9Device)->SetTransform(D3DTS_VIEW, &RenderData.DdrawConvertHomogeneousToWorld_ViewMatrixOriginal); + (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &identityMatrix); + + // Handle dwFlags + UnSetDrawFlags(rsClipping, rsLighting, rsExtents, newVertexTypeDesc, dwFlags, DirectXVersion); + + return hr; + } + } + // Set fixed function vertex type (*d3d9Device)->SetFVF(dwVertexTypeDesc); // Draw indexed primitive UP - hr = (*d3d9Device)->DrawIndexedPrimitiveUP(dptPrimitiveType, 0, dwVertexCount, GetNumberOfPrimitives(dptPrimitiveType, dwIndexCount), lpIndices, D3DFMT_INDEX16, lpVertices, GetVertexStride(dwVertexTypeDesc)); + hr = (*d3d9Device)->DrawIndexedPrimitiveUP(dptPrimitiveType, 0, dwVertexCount, GetNumberOfPrimitives(dptPrimitiveType, dwIndexCount), lpIndices, D3DFMT_INDEX16, lpVertices, stride); } // Handle dwFlags diff --git a/ddraw/IDirect3DDeviceX.h b/ddraw/IDirect3DDeviceX.h index 792d893a..ed8f73a8 100644 --- a/ddraw/IDirect3DDeviceX.h +++ b/ddraw/IDirect3DDeviceX.h @@ -1,6 +1,7 @@ #pragma once #include "IDirectDrawX.h" +#include "RenderData.h" class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject { @@ -27,6 +28,9 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject // SetTexture array LPDIRECTDRAWSURFACE7 AttachedTexture[8] = {}; + // The data used for rendering + RenderData RenderData; + // Wrapper interface functions inline REFIID GetWrapperType(DWORD DirectXVersion) { diff --git a/ddraw/RenderData.h b/ddraw/RenderData.h new file mode 100644 index 00000000..a758070e --- /dev/null +++ b/ddraw/RenderData.h @@ -0,0 +1,37 @@ +#pragma once + +#include +#include +#include + +class RenderData +{ +public: + + // Store the projection matrix used to transform the geometry on the gpu + _D3DMATRIX DdrawConvertHomogeneousToWorld_ProjectionMatrix; + + // Store the view matrix used to transform the geometry on the gpu + _D3DMATRIX DdrawConvertHomogeneousToWorld_ViewMatrix; + + // Store the original view matrix, so we can restore it + _D3DMATRIX DdrawConvertHomogeneousToWorld_ViewMatrixOriginal; + + // Store the inverse view matrix to transform the geometry on the cpu + DirectX::XMMATRIX DdrawConvertHomogeneousToWorld_ViewMatrixInverse; + + // Intermediate buffer for the geometry conversion + std::vector DdrawConvertHomogeneousToWorld_IntermediateGeometry; + + RenderData() + { + ZeroMemory(&DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(_D3DMATRIX)); + DdrawConvertHomogeneousToWorld_ViewMatrix._11 = 1.0f; + DdrawConvertHomogeneousToWorld_ViewMatrix._22 = 1.0f; + DdrawConvertHomogeneousToWorld_ViewMatrix._33 = 1.0f; + DdrawConvertHomogeneousToWorld_ViewMatrix._44 = 1.0f; + + std::memcpy(&DdrawConvertHomogeneousToWorld_ProjectionMatrix, &DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(_D3DMATRIX)); + std::memcpy(&DdrawConvertHomogeneousToWorld_ViewMatrixOriginal, &DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(_D3DMATRIX)); + } +}; diff --git a/dxwrapper.vcxproj b/dxwrapper.vcxproj index 3854925f..3eac7208 100644 --- a/dxwrapper.vcxproj +++ b/dxwrapper.vcxproj @@ -588,6 +588,7 @@ cmd /q /c "cd /D ""$(ProjectDir)d3d8\"" && del build.bat" + diff --git a/dxwrapper.vcxproj.filters b/dxwrapper.vcxproj.filters index e65b9e64..8c18b85b 100644 --- a/dxwrapper.vcxproj.filters +++ b/dxwrapper.vcxproj.filters @@ -1,4 +1,4 @@ - + @@ -1868,6 +1868,9 @@ ddraw + + ddraw + From e8ff88ddcfe646f2d344bfc3d2edf141051a1351 Mon Sep 17 00:00:00 2001 From: Daniel Kollmann Date: Wed, 17 May 2023 00:13:14 +0200 Subject: [PATCH 02/20] Fixed incorrect game camera position and orientation. Added option to disable lighting. Commented out unneeded XYZW code. --- Settings/Settings.h | 2 ++ ddraw/IDirect3DDeviceX.cpp | 53 ++++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Settings/Settings.h b/Settings/Settings.h index 30e33503..8c6001ea 100644 --- a/Settings/Settings.h +++ b/Settings/Settings.h @@ -52,6 +52,7 @@ visit(DdrawConvertHomogeneousToWorldDepthOffset) \ visit(DdrawUseNativeResolution) \ visit(DdrawEnableMouseHook) \ + visit(DdrawDisableLighting) \ visit(DdrawHookSystem32) \ visit(D3d8HookSystem32) \ visit(D3d9HookSystem32) \ @@ -233,6 +234,7 @@ struct CONFIG DWORD DdrawOverrideRefreshRate = 0; // Force Direct3d9 to use this refresh rate when using Dd7to9 DWORD DdrawOverrideStencilFormat = 0; // Force Direct3d9 to use this AutoStencilFormat when using Dd7to9 bool DdrawEnableMouseHook = true; // Allow to hook into mouse to limit it to the chosen resolution + bool DdrawDisableLighting = false; // Allow to disable lighting DWORD DdrawHookSystem32 = 0; // Hooks the ddraw.dll file in the Windows System32 folder DWORD D3d8HookSystem32 = 0; // Hooks the d3d8.dll file in the Windows System32 folder DWORD D3d9HookSystem32 = 0; // Hooks the d3d9.dll file in the Windows System32 folder diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index db07013e..eea98e7d 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -372,6 +372,21 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat } else { + DirectX::XMVECTOR position, direction; + if(Config.DdrawConvertHomogeneousToWorldUseGameCamera) + { + // To reconstruct the 3D world, we need to know where the camera is and where it is looking + position = DirectX::XMVectorSet(lpD3DMatrix->_41, lpD3DMatrix->_42, lpD3DMatrix->_43, lpD3DMatrix->_44); + direction = DirectX::XMVectorSet(lpD3DMatrix->_31, lpD3DMatrix->_32, lpD3DMatrix->_33, lpD3DMatrix->_34); + } + else + { + const float cameradir = 1.0f; + + position = DirectX::XMVectorSet(0.0f, 0.0f, -cameradir, 0.0f); + DirectX::XMVectorSet(0.0f, 0.0f, cameradir, 0.0f); + } + // Override the original matrix std::memcpy(lpD3DMatrix, &view, sizeof(_D3DMATRIX)); @@ -387,25 +402,7 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat DirectX::XMStoreFloat4x4((DirectX::XMFLOAT4X4*)&RenderData.DdrawConvertHomogeneousToWorld_ProjectionMatrix, proj); DirectX::XMVECTOR up = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); - - DirectX::XMMATRIX viewMatrix; - if(Config.DdrawConvertHomogeneousToWorldUseGameCamera) - { - // To reconstruct the 3D world, we need to know where the camera is and where it is looking - DirectX::XMVECTOR position = DirectX::XMVectorSet(lpD3DMatrix->_41, lpD3DMatrix->_42, lpD3DMatrix->_43, lpD3DMatrix->_44); - DirectX::XMVECTOR direction = DirectX::XMVectorSet(lpD3DMatrix->_31, lpD3DMatrix->_32, lpD3DMatrix->_33, lpD3DMatrix->_34); - - viewMatrix = DirectX::XMMatrixLookToLH(position, direction, up); - } - else - { - const float cameradir = 1.0f; - - DirectX::XMVECTOR pos = DirectX::XMVectorSet(0.0f, 0.0f, -cameradir, 0.0f); - DirectX::XMVECTOR direction = DirectX::XMVectorSet(0.0f, 0.0f, cameradir, 0.0f); - - viewMatrix = DirectX::XMMatrixLookToLH(pos, direction, up); - } + DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixLookToLH(position, direction, up); // Store the 3D view matrix so it can be set later DirectX::XMStoreFloat4x4((DirectX::XMFLOAT4X4*)&RenderData.DdrawConvertHomogeneousToWorld_ViewMatrix, viewMatrix); @@ -2139,6 +2136,12 @@ HRESULT m_IDirect3DDeviceX::SetRenderState(D3DRENDERSTATETYPE dwRenderStateType, dwRenderStateType = D3DRS_DEPTHBIAS; break; } + case D3DRS_LIGHTING: + if(Config.DdrawDisableLighting) + { + dwRenderState = FALSE; + } + break; case D3DRENDERSTATE_TEXTUREPERSPECTIVE: return D3D_OK; // As long as the device's D3DPTEXTURECAPS_PERSPECTIVE is enabled, the correction will be applied automatically. case D3DRENDERSTATE_LINEPATTERN: @@ -2549,7 +2552,7 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy { if(!Config.DdrawConvertHomogeneousToWorld) { - UINT8 *vertex = (UINT8*)lpVertices; + /*UINT8 *vertex = (UINT8*)lpVertices; for (UINT x = 0; x < dwVertexCount; x++) { @@ -2558,17 +2561,17 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy pos[3] = 1.0f; vertex += stride; - } + }*/ // Update the FVF dwVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZW; } else { - const UINT newstride = stride - sizeof(float); + const UINT targetStride = stride - sizeof(float); const UINT restSize = stride - sizeof(float) * 4; - RenderData.DdrawConvertHomogeneousToWorld_IntermediateGeometry.resize(newstride * dwVertexCount); + RenderData.DdrawConvertHomogeneousToWorld_IntermediateGeometry.resize(targetStride * dwVertexCount); UINT8 *sourceVertex = (UINT8*)lpVertices; UINT8 *targetVertex = (UINT8*)RenderData.DdrawConvertHomogeneousToWorld_IntermediateGeometry.data(); @@ -2596,7 +2599,7 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy // Move to next vertex sourceVertex += stride; - targetVertex += newstride; + targetVertex += targetStride; } // Set transform @@ -2610,7 +2613,7 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy (*d3d9Device)->SetFVF(newVertexTypeDesc); // Draw indexed primitive UP - hr = (*d3d9Device)->DrawIndexedPrimitiveUP(dptPrimitiveType, 0, dwVertexCount, GetNumberOfPrimitives(dptPrimitiveType, dwIndexCount), lpIndices, D3DFMT_INDEX16, lpVertices, newstride); + hr = (*d3d9Device)->DrawIndexedPrimitiveUP(dptPrimitiveType, 0, dwVertexCount, GetNumberOfPrimitives(dptPrimitiveType, dwIndexCount), lpIndices, D3DFMT_INDEX16, lpVertices, targetStride); // Restore transform _D3DMATRIX identityMatrix; From 17ba9b6a368199dd34f1aed6880cdd6d4e4aae3e Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Tue, 16 May 2023 17:43:35 -0700 Subject: [PATCH 03/20] Fix comments from pull request --- ddraw/DebugOverlay.cpp | 3 +-- ddraw/DebugOverlay.h | 2 +- ddraw/IDirect3DDeviceX.cpp | 34 +++++++++++++++------------------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/ddraw/DebugOverlay.cpp b/ddraw/DebugOverlay.cpp index d5763cd5..2a52154f 100644 --- a/ddraw/DebugOverlay.cpp +++ b/ddraw/DebugOverlay.cpp @@ -15,7 +15,6 @@ */ #include "DebugOverlay.h" -#include "RenderData.h" #include #define IMGUI_DEFINE_MATH_OPERATORS @@ -99,7 +98,7 @@ void DebugOverlay::BeginScene() ImGui::NewFrame(); } -void DebugOverlay::EndScene(const RenderData &RenderData) +void DebugOverlay::EndScene() { static bool ShowDebugUI = false; if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_LeftAlt)) && diff --git a/ddraw/DebugOverlay.h b/ddraw/DebugOverlay.h index a0f2c34f..f7e948f2 100644 --- a/ddraw/DebugOverlay.h +++ b/ddraw/DebugOverlay.h @@ -48,7 +48,7 @@ class DebugOverlay // Frame functions void BeginScene(); - void EndScene(const class RenderData &RenderData); + void EndScene(); // Functions void SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStateType, LPD3DMATRIX lpD3DMatrix); diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index eea98e7d..f445997f 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -19,7 +19,7 @@ #include // Enable for testing only -#define ENABLE_DEBUGOVERLAY +//#define ENABLE_DEBUGOVERLAY #ifdef ENABLE_DEBUGOVERLAY #include "DebugOverlay.h" @@ -339,17 +339,17 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat break; } - if(Config.DdrawConvertHomogeneousW) + if (Config.DdrawConvertHomogeneousW) { #ifdef ENABLE_DEBUGOVERLAY // Set the original matrix DOverlay.SetTransform(dtstTransformStateType, lpD3DMatrix); #endif - if(dtstTransformStateType == D3DTS_VIEW) + if (dtstTransformStateType == D3DTS_VIEW) { D3DVIEWPORT9 Viewport9; - if(SUCCEEDED((*d3d9Device)->GetViewport(&Viewport9))) + if (SUCCEEDED((*d3d9Device)->GetViewport(&Viewport9))) { const float width = (float)Viewport9.Width; const float height = (float)Viewport9.Height; @@ -365,15 +365,13 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat view._42 = 1.0f; // translate Y view._44 = 1.0f; - if(!Config.DdrawConvertHomogeneousToWorld) - { - // Override the original matrix - std::memcpy(lpD3DMatrix, &view, sizeof(_D3DMATRIX)); - } - else + // Override the original matrix + std::memcpy(lpD3DMatrix, &view, sizeof(_D3DMATRIX)); + + if (Config.DdrawConvertHomogeneousToWorld) { DirectX::XMVECTOR position, direction; - if(Config.DdrawConvertHomogeneousToWorldUseGameCamera) + if (Config.DdrawConvertHomogeneousToWorldUseGameCamera) { // To reconstruct the 3D world, we need to know where the camera is and where it is looking position = DirectX::XMVectorSet(lpD3DMatrix->_41, lpD3DMatrix->_42, lpD3DMatrix->_43, lpD3DMatrix->_44); @@ -384,12 +382,9 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat const float cameradir = 1.0f; position = DirectX::XMVectorSet(0.0f, 0.0f, -cameradir, 0.0f); - DirectX::XMVectorSet(0.0f, 0.0f, cameradir, 0.0f); + direction = DirectX::XMVectorSet(0.0f, 0.0f, cameradir, 0.0f); } - // Override the original matrix - std::memcpy(lpD3DMatrix, &view, sizeof(_D3DMATRIX)); - // Store the original matrix so it can be restored std::memcpy(&RenderData.DdrawConvertHomogeneousToWorld_ViewMatrixOriginal, &view, sizeof(_D3DMATRIX)); @@ -1703,7 +1698,7 @@ HRESULT m_IDirect3DDeviceX::EndScene() } #ifdef ENABLE_DEBUGOVERLAY - DOverlay.EndScene(RenderData); + DOverlay.EndScene(); #endif // The IDirect3DDevice7::EndScene method ends a scene that was begun by calling the IDirect3DDevice7::BeginScene method. @@ -2137,7 +2132,7 @@ HRESULT m_IDirect3DDeviceX::SetRenderState(D3DRENDERSTATETYPE dwRenderStateType, break; } case D3DRS_LIGHTING: - if(Config.DdrawDisableLighting) + if (Config.DdrawDisableLighting) { dwRenderState = FALSE; } @@ -2548,9 +2543,10 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy const UINT stride = GetVertexStride(dwVertexTypeDesc); // Handle PositionT - if((dwVertexTypeDesc & D3DFVF_XYZRHW) != 0 && Config.DdrawConvertHomogeneousW) + if (Config.DdrawConvertHomogeneousW && (dwVertexTypeDesc & 0x0E) == D3DFVF_XYZRHW) { - if(!Config.DdrawConvertHomogeneousToWorld) + D3DFVF_XYZB1; + if (!Config.DdrawConvertHomogeneousToWorld) { /*UINT8 *vertex = (UINT8*)lpVertices; From dbc8e10fc23bbf9875b1eac319b54e9d132f47c7 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Tue, 16 May 2023 18:12:13 -0700 Subject: [PATCH 04/20] Don't override matrix memory, just the pointer --- ddraw/IDirect3DDeviceX.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index f445997f..72432c3e 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -339,6 +339,7 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat break; } + _D3DMATRIX view; if (Config.DdrawConvertHomogeneousW) { #ifdef ENABLE_DEBUGOVERLAY @@ -356,7 +357,6 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat const float ratio = width / height; // Replace the matrix with one that handles D3DFVF_XYZRHW geometry - _D3DMATRIX view; ZeroMemory(&view, sizeof(_D3DMATRIX)); view._11 = 2.0f / width; view._22 = -2.0f / height; @@ -365,8 +365,8 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat view._42 = 1.0f; // translate Y view._44 = 1.0f; - // Override the original matrix - std::memcpy(lpD3DMatrix, &view, sizeof(_D3DMATRIX)); + // Override original matrix pointer + lpD3DMatrix = &view; if (Config.DdrawConvertHomogeneousToWorld) { From 14632efcad03be9b2fdcf8487cc626387d7d6d52 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Wed, 17 May 2023 11:04:55 -0700 Subject: [PATCH 05/20] Fix disable lighting and a couple typos --- Settings/AllSettings.ini | 7 +++++++ ddraw/IDirect3DDeviceX.cpp | 16 ++++++++++------ ddraw/RenderData.h | 12 ++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Settings/AllSettings.ini b/Settings/AllSettings.ini index 871f306a..b44ac0e5 100644 --- a/Settings/AllSettings.ini +++ b/Settings/AllSettings.ini @@ -73,6 +73,13 @@ DdrawOverrideRefreshRate = 0 DdrawOverrideStencilFormat = 0 DdrawIntegerScalingClamp = 0 DdrawMaintainAspectRatio = 0 +DdrawConvertHomogeneousW = 0 +DdrawConvertHomogeneousToWorld = 0 +DdrawConvertHomogeneousToWorldUseGameCamera = 0 +DdrawConvertHomogeneousToWorldFOV = 90.0f +DdrawConvertHomogeneousToWorldNearPlane = 1.0f +DdrawConvertHomogeneousToWorldFarPlane = 1000.0f +DdrawConvertHomogeneousToWorldDepthOffset = 0.0f [d3d9] AnisotropicFiltering = 0 diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index 72432c3e..5c8e0518 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -339,7 +339,7 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat break; } - _D3DMATRIX view; + D3DMATRIX view; if (Config.DdrawConvertHomogeneousW) { #ifdef ENABLE_DEBUGOVERLAY @@ -357,7 +357,7 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat const float ratio = width / height; // Replace the matrix with one that handles D3DFVF_XYZRHW geometry - ZeroMemory(&view, sizeof(_D3DMATRIX)); + ZeroMemory(&view, sizeof(D3DMATRIX)); view._11 = 2.0f / width; view._22 = -2.0f / height; view._33 = 1.0f; @@ -386,7 +386,7 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat } // Store the original matrix so it can be restored - std::memcpy(&RenderData.DdrawConvertHomogeneousToWorld_ViewMatrixOriginal, &view, sizeof(_D3DMATRIX)); + std::memcpy(&RenderData.DdrawConvertHomogeneousToWorld_ViewMatrixOriginal, &view, sizeof(D3DMATRIX)); // The Black & White matrix is an ortho camera, so create a perspective one matching the game const float fov = Config.DdrawConvertHomogeneousToWorldFOV; @@ -1665,6 +1665,11 @@ HRESULT m_IDirect3DDeviceX::BeginScene() DOverlay.BeginScene(); #endif + if (Config.DdrawDisableLighting) + { + (*d3d9Device)->SetRenderState(D3DRS_LIGHTING, FALSE); + } + return hr; } @@ -2545,7 +2550,6 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy // Handle PositionT if (Config.DdrawConvertHomogeneousW && (dwVertexTypeDesc & 0x0E) == D3DFVF_XYZRHW) { - D3DFVF_XYZB1; if (!Config.DdrawConvertHomogeneousToWorld) { /*UINT8 *vertex = (UINT8*)lpVertices; @@ -2612,8 +2616,8 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy hr = (*d3d9Device)->DrawIndexedPrimitiveUP(dptPrimitiveType, 0, dwVertexCount, GetNumberOfPrimitives(dptPrimitiveType, dwIndexCount), lpIndices, D3DFMT_INDEX16, lpVertices, targetStride); // Restore transform - _D3DMATRIX identityMatrix; - ZeroMemory(&identityMatrix, sizeof(_D3DMATRIX)); + D3DMATRIX identityMatrix; + ZeroMemory(&identityMatrix, sizeof(D3DMATRIX)); identityMatrix._11 = 1.0f; identityMatrix._22 = 1.0f; identityMatrix._33 = 1.0f; diff --git a/ddraw/RenderData.h b/ddraw/RenderData.h index a758070e..28f91af6 100644 --- a/ddraw/RenderData.h +++ b/ddraw/RenderData.h @@ -9,13 +9,13 @@ class RenderData public: // Store the projection matrix used to transform the geometry on the gpu - _D3DMATRIX DdrawConvertHomogeneousToWorld_ProjectionMatrix; + D3DMATRIX DdrawConvertHomogeneousToWorld_ProjectionMatrix; // Store the view matrix used to transform the geometry on the gpu - _D3DMATRIX DdrawConvertHomogeneousToWorld_ViewMatrix; + D3DMATRIX DdrawConvertHomogeneousToWorld_ViewMatrix; // Store the original view matrix, so we can restore it - _D3DMATRIX DdrawConvertHomogeneousToWorld_ViewMatrixOriginal; + D3DMATRIX DdrawConvertHomogeneousToWorld_ViewMatrixOriginal; // Store the inverse view matrix to transform the geometry on the cpu DirectX::XMMATRIX DdrawConvertHomogeneousToWorld_ViewMatrixInverse; @@ -25,13 +25,13 @@ class RenderData RenderData() { - ZeroMemory(&DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(_D3DMATRIX)); + ZeroMemory(&DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(D3DMATRIX)); DdrawConvertHomogeneousToWorld_ViewMatrix._11 = 1.0f; DdrawConvertHomogeneousToWorld_ViewMatrix._22 = 1.0f; DdrawConvertHomogeneousToWorld_ViewMatrix._33 = 1.0f; DdrawConvertHomogeneousToWorld_ViewMatrix._44 = 1.0f; - std::memcpy(&DdrawConvertHomogeneousToWorld_ProjectionMatrix, &DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(_D3DMATRIX)); - std::memcpy(&DdrawConvertHomogeneousToWorld_ViewMatrixOriginal, &DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(_D3DMATRIX)); + std::memcpy(&DdrawConvertHomogeneousToWorld_ProjectionMatrix, &DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(D3DMATRIX)); + std::memcpy(&DdrawConvertHomogeneousToWorld_ViewMatrixOriginal, &DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(D3DMATRIX)); } }; From de8fcbc6f88ba2c9d1b1b34fdc57d3021fb02812 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Wed, 17 May 2023 11:47:17 -0700 Subject: [PATCH 06/20] Set transform if not set by game --- Settings/AllSettings.ini | 1 + ddraw/IDirect3DDeviceX.cpp | 13 +++++++++++++ ddraw/RenderData.h | 3 +++ 3 files changed, 17 insertions(+) diff --git a/Settings/AllSettings.ini b/Settings/AllSettings.ini index b44ac0e5..4a73db67 100644 --- a/Settings/AllSettings.ini +++ b/Settings/AllSettings.ini @@ -73,6 +73,7 @@ DdrawOverrideRefreshRate = 0 DdrawOverrideStencilFormat = 0 DdrawIntegerScalingClamp = 0 DdrawMaintainAspectRatio = 0 +DdrawDisableLighting = 0 DdrawConvertHomogeneousW = 0 DdrawConvertHomogeneousToWorld = 0 DdrawConvertHomogeneousToWorldUseGameCamera = 0 diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index 5c8e0518..79c00d36 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -368,6 +368,9 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat // Override original matrix pointer lpD3DMatrix = &view; + // Set flag + RenderData.IsDdrawConvertHomogeneousTransformViewSet = true; + if (Config.DdrawConvertHomogeneousToWorld) { DirectX::XMVECTOR position, direction; @@ -2563,6 +2566,13 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy vertex += stride; }*/ + if (!RenderData.IsDdrawConvertHomogeneousTransformViewSet) + { + D3DMATRIX Matrix = {}; + GetTransform(D3DTS_VIEW, &Matrix); + SetTransform(D3DTS_VIEW, &Matrix); + } + // Update the FVF dwVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZW; } @@ -3083,6 +3093,9 @@ HRESULT m_IDirect3DDeviceX::CheckInterface(char *FunctionName, bool CheckD3DDevi void m_IDirect3DDeviceX::ResetDevice() { + // Reset flags + RenderData.IsDdrawConvertHomogeneousTransformViewSet = false; + // Reset textures after device reset for (UINT x = 0; x < 8; x++) { diff --git a/ddraw/RenderData.h b/ddraw/RenderData.h index 28f91af6..b86218b3 100644 --- a/ddraw/RenderData.h +++ b/ddraw/RenderData.h @@ -8,6 +8,9 @@ class RenderData { public: + // Flags + bool IsDdrawConvertHomogeneousTransformViewSet = false; + // Store the projection matrix used to transform the geometry on the gpu D3DMATRIX DdrawConvertHomogeneousToWorld_ProjectionMatrix; From a0ddc3d0352ec41a61f5f4745921b595b89886fd Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Wed, 17 May 2023 13:57:03 -0700 Subject: [PATCH 07/20] Remove RenderData --- ddraw/IDirect3DDeviceX.cpp | 44 +++++++++++++++++++++++--------------- ddraw/IDirect3DDeviceX.h | 16 +++++++++++--- ddraw/RenderData.h | 40 ---------------------------------- dxwrapper.vcxproj | 1 - dxwrapper.vcxproj.filters | 3 --- 5 files changed, 40 insertions(+), 64 deletions(-) delete mode 100644 ddraw/RenderData.h diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index 79c00d36..6f4c2caf 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -16,7 +16,6 @@ #include "ddraw.h" #include -#include // Enable for testing only //#define ENABLE_DEBUGOVERLAY @@ -354,7 +353,6 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat { const float width = (float)Viewport9.Width; const float height = (float)Viewport9.Height; - const float ratio = width / height; // Replace the matrix with one that handles D3DFVF_XYZRHW geometry ZeroMemory(&view, sizeof(D3DMATRIX)); @@ -369,7 +367,7 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat lpD3DMatrix = &view; // Set flag - RenderData.IsDdrawConvertHomogeneousTransformViewSet = true; + ConvertHomogeneous.IsTransformViewSet = true; if (Config.DdrawConvertHomogeneousToWorld) { @@ -389,21 +387,22 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat } // Store the original matrix so it can be restored - std::memcpy(&RenderData.DdrawConvertHomogeneousToWorld_ViewMatrixOriginal, &view, sizeof(D3DMATRIX)); + ConvertHomogeneous.ToWorld_ViewMatrixOriginal = view; // The Black & White matrix is an ortho camera, so create a perspective one matching the game const float fov = Config.DdrawConvertHomogeneousToWorldFOV; const float nearplane = Config.DdrawConvertHomogeneousToWorldNearPlane; const float farplane = Config.DdrawConvertHomogeneousToWorldFarPlane; + const float ratio = width / height; DirectX::XMMATRIX proj = DirectX::XMMatrixPerspectiveFovLH(fov * (3.14159265359f / 180.0f), ratio, nearplane, farplane); - DirectX::XMStoreFloat4x4((DirectX::XMFLOAT4X4*)&RenderData.DdrawConvertHomogeneousToWorld_ProjectionMatrix, proj); + DirectX::XMStoreFloat4x4((DirectX::XMFLOAT4X4*)&ConvertHomogeneous.ToWorld_ProjectionMatrix, proj); DirectX::XMVECTOR up = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixLookToLH(position, direction, up); // Store the 3D view matrix so it can be set later - DirectX::XMStoreFloat4x4((DirectX::XMFLOAT4X4*)&RenderData.DdrawConvertHomogeneousToWorld_ViewMatrix, viewMatrix); + DirectX::XMStoreFloat4x4((DirectX::XMFLOAT4X4*)&ConvertHomogeneous.ToWorld_ViewMatrix, viewMatrix); // Store the view inverse matrix of the game, so we can transform the geometry with it DirectX::XMMATRIX toViewSpace = DirectX::XMLoadFloat4x4((DirectX::XMFLOAT4X4*)lpD3DMatrix); @@ -412,7 +411,7 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat DirectX::XMMATRIX depthoffset = DirectX::XMMatrixTranslation(0.0f, 0.0f, Config.DdrawConvertHomogeneousToWorldDepthOffset); - RenderData.DdrawConvertHomogeneousToWorld_ViewMatrixInverse = DirectX::XMMatrixMultiply(depthoffset, DirectX::XMMatrixMultiply(toViewSpace, vpinv)); + ConvertHomogeneous.ToWorld_ViewMatrixInverse = DirectX::XMMatrixMultiply(depthoffset, DirectX::XMMatrixMultiply(toViewSpace, vpinv)); } } } @@ -2566,7 +2565,7 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy vertex += stride; }*/ - if (!RenderData.IsDdrawConvertHomogeneousTransformViewSet) + if (!ConvertHomogeneous.IsTransformViewSet) { D3DMATRIX Matrix = {}; GetTransform(D3DTS_VIEW, &Matrix); @@ -2581,10 +2580,10 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy const UINT targetStride = stride - sizeof(float); const UINT restSize = stride - sizeof(float) * 4; - RenderData.DdrawConvertHomogeneousToWorld_IntermediateGeometry.resize(targetStride * dwVertexCount); + ConvertHomogeneous.ToWorld_IntermediateGeometry.resize(targetStride * dwVertexCount); UINT8 *sourceVertex = (UINT8*)lpVertices; - UINT8 *targetVertex = (UINT8*)RenderData.DdrawConvertHomogeneousToWorld_IntermediateGeometry.data(); + UINT8 *targetVertex = (UINT8*)ConvertHomogeneous.ToWorld_IntermediateGeometry.data(); lpVertices = targetVertex; @@ -2596,7 +2595,7 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy DirectX::XMVECTOR xpos = DirectX::XMVectorSet(srcpos[0], srcpos[1], srcpos[2], srcpos[3]); - DirectX::XMVECTOR xpos_global = DirectX::XMVector3TransformCoord(xpos, RenderData.DdrawConvertHomogeneousToWorld_ViewMatrixInverse); + DirectX::XMVECTOR xpos_global = DirectX::XMVector3TransformCoord(xpos, ConvertHomogeneous.ToWorld_ViewMatrixInverse); xpos_global = DirectX::XMVectorDivide(xpos_global, DirectX::XMVectorSplatW(xpos_global)); @@ -2613,8 +2612,8 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy } // Set transform - (*d3d9Device)->SetTransform(D3DTS_VIEW, &RenderData.DdrawConvertHomogeneousToWorld_ViewMatrix); - (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &RenderData.DdrawConvertHomogeneousToWorld_ProjectionMatrix); + (*d3d9Device)->SetTransform(D3DTS_VIEW, &ConvertHomogeneous.ToWorld_ViewMatrix); + (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &ConvertHomogeneous.ToWorld_ProjectionMatrix); // Update the FVF const DWORD newVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZ; @@ -2626,13 +2625,12 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy hr = (*d3d9Device)->DrawIndexedPrimitiveUP(dptPrimitiveType, 0, dwVertexCount, GetNumberOfPrimitives(dptPrimitiveType, dwIndexCount), lpIndices, D3DFMT_INDEX16, lpVertices, targetStride); // Restore transform - D3DMATRIX identityMatrix; - ZeroMemory(&identityMatrix, sizeof(D3DMATRIX)); + D3DMATRIX identityMatrix = {}; identityMatrix._11 = 1.0f; identityMatrix._22 = 1.0f; identityMatrix._33 = 1.0f; - (*d3d9Device)->SetTransform(D3DTS_VIEW, &RenderData.DdrawConvertHomogeneousToWorld_ViewMatrixOriginal); + (*d3d9Device)->SetTransform(D3DTS_VIEW, &ConvertHomogeneous.ToWorld_ViewMatrixOriginal); (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &identityMatrix); // Handle dwFlags @@ -3032,6 +3030,18 @@ void m_IDirect3DDeviceX::InitDevice(DWORD DirectXVersion) return; } + if (Config.DdrawConvertHomogeneousW) + { + ZeroMemory(&ConvertHomogeneous.ToWorld_ViewMatrix, sizeof(D3DMATRIX)); + ConvertHomogeneous.ToWorld_ViewMatrix._11 = 1.0f; + ConvertHomogeneous.ToWorld_ViewMatrix._22 = 1.0f; + ConvertHomogeneous.ToWorld_ViewMatrix._33 = 1.0f; + ConvertHomogeneous.ToWorld_ViewMatrix._44 = 1.0f; + + ConvertHomogeneous.ToWorld_ProjectionMatrix = ConvertHomogeneous.ToWorld_ViewMatrix; + ConvertHomogeneous.ToWorld_ViewMatrixOriginal = ConvertHomogeneous.ToWorld_ViewMatrix; + } + AddRef(DirectXVersion); } @@ -3094,7 +3104,7 @@ HRESULT m_IDirect3DDeviceX::CheckInterface(char *FunctionName, bool CheckD3DDevi void m_IDirect3DDeviceX::ResetDevice() { // Reset flags - RenderData.IsDdrawConvertHomogeneousTransformViewSet = false; + ConvertHomogeneous.IsTransformViewSet = false; // Reset textures after device reset for (UINT x = 0; x < 8; x++) diff --git a/ddraw/IDirect3DDeviceX.h b/ddraw/IDirect3DDeviceX.h index ed8f73a8..5df4f3ef 100644 --- a/ddraw/IDirect3DDeviceX.h +++ b/ddraw/IDirect3DDeviceX.h @@ -1,7 +1,17 @@ #pragma once #include "IDirectDrawX.h" -#include "RenderData.h" +#include + +struct CONVERTHOMOGENEOUS +{ + bool IsTransformViewSet = false; // Remembers if game sets the view matrix + D3DMATRIX ToWorld_ProjectionMatrix; // Store the projection matrix used to transform the geometry on the gpu + D3DMATRIX ToWorld_ViewMatrix; // Store the view matrix used to transform the geometry on the gpu + D3DMATRIX ToWorld_ViewMatrixOriginal; // Store the original view matrix, so we can restore it + DirectX::XMMATRIX ToWorld_ViewMatrixInverse; // Store the inverse view matrix to transform the geometry on the cpu + std::vector ToWorld_IntermediateGeometry; // Intermediate buffer for the geometry conversion +}; class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject { @@ -28,8 +38,8 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject // SetTexture array LPDIRECTDRAWSURFACE7 AttachedTexture[8] = {}; - // The data used for rendering - RenderData RenderData; + // The data used for rendering Homogeneous + CONVERTHOMOGENEOUS ConvertHomogeneous; // Wrapper interface functions inline REFIID GetWrapperType(DWORD DirectXVersion) diff --git a/ddraw/RenderData.h b/ddraw/RenderData.h deleted file mode 100644 index b86218b3..00000000 --- a/ddraw/RenderData.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include -#include -#include - -class RenderData -{ -public: - - // Flags - bool IsDdrawConvertHomogeneousTransformViewSet = false; - - // Store the projection matrix used to transform the geometry on the gpu - D3DMATRIX DdrawConvertHomogeneousToWorld_ProjectionMatrix; - - // Store the view matrix used to transform the geometry on the gpu - D3DMATRIX DdrawConvertHomogeneousToWorld_ViewMatrix; - - // Store the original view matrix, so we can restore it - D3DMATRIX DdrawConvertHomogeneousToWorld_ViewMatrixOriginal; - - // Store the inverse view matrix to transform the geometry on the cpu - DirectX::XMMATRIX DdrawConvertHomogeneousToWorld_ViewMatrixInverse; - - // Intermediate buffer for the geometry conversion - std::vector DdrawConvertHomogeneousToWorld_IntermediateGeometry; - - RenderData() - { - ZeroMemory(&DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(D3DMATRIX)); - DdrawConvertHomogeneousToWorld_ViewMatrix._11 = 1.0f; - DdrawConvertHomogeneousToWorld_ViewMatrix._22 = 1.0f; - DdrawConvertHomogeneousToWorld_ViewMatrix._33 = 1.0f; - DdrawConvertHomogeneousToWorld_ViewMatrix._44 = 1.0f; - - std::memcpy(&DdrawConvertHomogeneousToWorld_ProjectionMatrix, &DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(D3DMATRIX)); - std::memcpy(&DdrawConvertHomogeneousToWorld_ViewMatrixOriginal, &DdrawConvertHomogeneousToWorld_ViewMatrix, sizeof(D3DMATRIX)); - } -}; diff --git a/dxwrapper.vcxproj b/dxwrapper.vcxproj index 3eac7208..3854925f 100644 --- a/dxwrapper.vcxproj +++ b/dxwrapper.vcxproj @@ -588,7 +588,6 @@ cmd /q /c "cd /D ""$(ProjectDir)d3d8\"" && del build.bat" - diff --git a/dxwrapper.vcxproj.filters b/dxwrapper.vcxproj.filters index 8c18b85b..2e3ec3cd 100644 --- a/dxwrapper.vcxproj.filters +++ b/dxwrapper.vcxproj.filters @@ -1868,9 +1868,6 @@ ddraw - - ddraw - From c715c6185c00f6aade5e071eed47f4c51bab6a86 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Wed, 17 May 2023 14:37:10 -0700 Subject: [PATCH 08/20] Fix default settings --- Settings/AllSettings.ini | 8 ++++---- Settings/Settings.cpp | 4 +++- Settings/Settings.h | 10 +++++----- ddraw/IDirect3DDeviceX.cpp | 14 +++++++------- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Settings/AllSettings.ini b/Settings/AllSettings.ini index 4a73db67..d71474fe 100644 --- a/Settings/AllSettings.ini +++ b/Settings/AllSettings.ini @@ -77,10 +77,10 @@ DdrawDisableLighting = 0 DdrawConvertHomogeneousW = 0 DdrawConvertHomogeneousToWorld = 0 DdrawConvertHomogeneousToWorldUseGameCamera = 0 -DdrawConvertHomogeneousToWorldFOV = 90.0f -DdrawConvertHomogeneousToWorldNearPlane = 1.0f -DdrawConvertHomogeneousToWorldFarPlane = 1000.0f -DdrawConvertHomogeneousToWorldDepthOffset = 0.0f +DdrawConvertHomogeneousToWorldFOV = 90.0 +DdrawConvertHomogeneousToWorldNearPlane = 1.0 +DdrawConvertHomogeneousToWorldFarPlane = 1000.0 +DdrawConvertHomogeneousToWorldDepthOffset = 0.0 [d3d9] AnisotropicFiltering = 0 diff --git a/Settings/Settings.cpp b/Settings/Settings.cpp index 1a722a53..f47cea4f 100644 --- a/Settings/Settings.cpp +++ b/Settings/Settings.cpp @@ -449,12 +449,14 @@ void Settings::SetDefaultConfigSettings() Config.FixSpeakerConfigType = true; // Set other default values - Config.LoopSleepTime = 120; Config.WindowSleepTime = 500; Config.PrimaryBufferBits = 16; Config.PrimaryBufferSamples = 44100; Config.PrimaryBufferChannels = 2; Config.AudioFadeOutDelayMS = 20; + Config.DdrawConvertHomogeneousToWorldFOV = 90.0f; + Config.DdrawConvertHomogeneousToWorldNearPlane = 1.0f; + Config.DdrawConvertHomogeneousToWorldFarPlane = 1000.0f; SetValue("ExcludeProcess", "dxwnd.exe", &Config.ExcludeProcess); SetValue("ExcludeProcess", "dgVoodooSetup.exe", &Config.ExcludeProcess); } diff --git a/Settings/Settings.h b/Settings/Settings.h index 8c6001ea..a239aaf2 100644 --- a/Settings/Settings.h +++ b/Settings/Settings.h @@ -218,12 +218,12 @@ struct CONFIG bool DdrawMaintainAspectRatio = false; // Keeps the current DirectDraw aspect ratio when overriding the game's resolution bool DdrawUseDirect3D9Ex = false; // Use Direct3D9Ex extensions for Dd7to9 bool DdrawConvertHomogeneousW = false; // Convert primites using D3DFVF_XYZRHW to D3DFVF_XYZW. - bool DdrawConvertHomogeneousToWorld = false; // Convert primitives back into a world space. Needed for RTX. + bool DdrawConvertHomogeneousToWorld = false; // Convert primitives back into a world space. Needed for RTX. bool DdrawConvertHomogeneousToWorldUseGameCamera = false; // Use the game's view matrix instead of replacing it with our own. - float DdrawConvertHomogeneousToWorldFOV = 90.0f; // The field of view of the camera used to reconstruct the original 3D world. - float DdrawConvertHomogeneousToWorldNearPlane = 1.0f; // The near plane of the camera used to reconstruct the original 3D world. - float DdrawConvertHomogeneousToWorldFarPlane = 1000.0f; // The far plane of the camera used to reconstruct the original 3D world. - float DdrawConvertHomogeneousToWorldDepthOffset = 0.0f; // The offset to add to the geometry so it does not clip into the near plane. + float DdrawConvertHomogeneousToWorldFOV = 0.0f; // The field of view of the camera used to reconstruct the original 3D world. + float DdrawConvertHomogeneousToWorldNearPlane = 0.0f; // The near plane of the camera used to reconstruct the original 3D world. + float DdrawConvertHomogeneousToWorldFarPlane = 0.0f; // The far plane of the camera used to reconstruct the original 3D world. + float DdrawConvertHomogeneousToWorldDepthOffset = 0.0f; // The offset to add to the geometry so it does not clip into the near plane. bool DdrawUseNativeResolution = false; // Uses the current screen resolution for Dd7to9 DWORD DdrawClippedWidth = 0; // Used to scaled Direct3d9 to use this width when using Dd7to9 DWORD DdrawClippedHeight = 0; // Used to scaled Direct3d9 to use this height when using Dd7to9 diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index 6f4c2caf..d036643f 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -2552,6 +2552,13 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy // Handle PositionT if (Config.DdrawConvertHomogeneousW && (dwVertexTypeDesc & 0x0E) == D3DFVF_XYZRHW) { + if (!ConvertHomogeneous.IsTransformViewSet) + { + D3DMATRIX Matrix = {}; + GetTransform(D3DTS_VIEW, &Matrix); + SetTransform(D3DTS_VIEW, &Matrix); + } + if (!Config.DdrawConvertHomogeneousToWorld) { /*UINT8 *vertex = (UINT8*)lpVertices; @@ -2565,13 +2572,6 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy vertex += stride; }*/ - if (!ConvertHomogeneous.IsTransformViewSet) - { - D3DMATRIX Matrix = {}; - GetTransform(D3DTS_VIEW, &Matrix); - SetTransform(D3DTS_VIEW, &Matrix); - } - // Update the FVF dwVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZW; } From d7dff145fcf252b586ceb84c643595018c7dcf2a Mon Sep 17 00:00:00 2001 From: Daniel Kollmann Date: Thu, 18 May 2023 01:01:46 +0200 Subject: [PATCH 09/20] Fixed that DdrawConvertHomogeneousToWorldUseGameCamera did not actually use the game camera transform. Simplified the opposite case. --- ddraw/IDirect3DDeviceX.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index d036643f..5ea295e6 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -369,7 +369,12 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat // Set flag ConvertHomogeneous.IsTransformViewSet = true; - if (Config.DdrawConvertHomogeneousToWorld) + if (!Config.DdrawConvertHomogeneousToWorld) + { + // Override original matrix pointer + lpD3DMatrix = &view; + } + else { DirectX::XMVECTOR position, direction; if (Config.DdrawConvertHomogeneousToWorldUseGameCamera) @@ -380,12 +385,13 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat } else { - const float cameradir = 1.0f; - - position = DirectX::XMVectorSet(0.0f, 0.0f, -cameradir, 0.0f); - direction = DirectX::XMVectorSet(0.0f, 0.0f, cameradir, 0.0f); + position = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f); + direction = DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); } + // Override original matrix pointer + lpD3DMatrix = &view; + // Store the original matrix so it can be restored ConvertHomogeneous.ToWorld_ViewMatrixOriginal = view; From 575bd6f1fd56c820e8e42ca0a426722d25bff46f Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Thu, 18 May 2023 09:10:27 -0700 Subject: [PATCH 10/20] Remove unneeded matrix assignment --- ddraw/IDirect3DDeviceX.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index 5ea295e6..f0838206 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -369,12 +369,7 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat // Set flag ConvertHomogeneous.IsTransformViewSet = true; - if (!Config.DdrawConvertHomogeneousToWorld) - { - // Override original matrix pointer - lpD3DMatrix = &view; - } - else + if (Config.DdrawConvertHomogeneousToWorld) { DirectX::XMVECTOR position, direction; if (Config.DdrawConvertHomogeneousToWorldUseGameCamera) @@ -389,9 +384,6 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat direction = DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); } - // Override original matrix pointer - lpD3DMatrix = &view; - // Store the original matrix so it can be restored ConvertHomogeneous.ToWorld_ViewMatrixOriginal = view; From 44d43381e096a1fbff974f593ef68abc4d5c7efd Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Thu, 18 May 2023 09:15:13 -0700 Subject: [PATCH 11/20] Fix DdrawConvertHomogeneousToWorldUseGameCamera --- ddraw/IDirect3DDeviceX.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index f0838206..cc0255a0 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -363,7 +363,8 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat view._42 = 1.0f; // translate Y view._44 = 1.0f; - // Override original matrix pointer + // Backup pointer before overriding original matrix pointer + LPD3DMATRIX lpOriginalD3DMatrix = lpD3DMatrix; lpD3DMatrix = &view; // Set flag @@ -375,8 +376,8 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat if (Config.DdrawConvertHomogeneousToWorldUseGameCamera) { // To reconstruct the 3D world, we need to know where the camera is and where it is looking - position = DirectX::XMVectorSet(lpD3DMatrix->_41, lpD3DMatrix->_42, lpD3DMatrix->_43, lpD3DMatrix->_44); - direction = DirectX::XMVectorSet(lpD3DMatrix->_31, lpD3DMatrix->_32, lpD3DMatrix->_33, lpD3DMatrix->_34); + position = DirectX::XMVectorSet(lpOriginalD3DMatrix->_41, lpOriginalD3DMatrix->_42, lpOriginalD3DMatrix->_43, lpOriginalD3DMatrix->_44); + direction = DirectX::XMVectorSet(lpOriginalD3DMatrix->_31, lpOriginalD3DMatrix->_32, lpOriginalD3DMatrix->_33, lpOriginalD3DMatrix->_34); } else { From f823ec3dc0235cecfc21e6c3b1d36ad58d3b319b Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Thu, 18 May 2023 10:38:45 -0700 Subject: [PATCH 12/20] Move matrix override code to the end --- ddraw/IDirect3DDeviceX.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index cc0255a0..6d76b9d7 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -363,10 +363,6 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat view._42 = 1.0f; // translate Y view._44 = 1.0f; - // Backup pointer before overriding original matrix pointer - LPD3DMATRIX lpOriginalD3DMatrix = lpD3DMatrix; - lpD3DMatrix = &view; - // Set flag ConvertHomogeneous.IsTransformViewSet = true; @@ -376,8 +372,8 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat if (Config.DdrawConvertHomogeneousToWorldUseGameCamera) { // To reconstruct the 3D world, we need to know where the camera is and where it is looking - position = DirectX::XMVectorSet(lpOriginalD3DMatrix->_41, lpOriginalD3DMatrix->_42, lpOriginalD3DMatrix->_43, lpOriginalD3DMatrix->_44); - direction = DirectX::XMVectorSet(lpOriginalD3DMatrix->_31, lpOriginalD3DMatrix->_32, lpOriginalD3DMatrix->_33, lpOriginalD3DMatrix->_34); + position = DirectX::XMVectorSet(lpD3DMatrix->_41, lpD3DMatrix->_42, lpD3DMatrix->_43, lpD3DMatrix->_44); + direction = DirectX::XMVectorSet(lpD3DMatrix->_31, lpD3DMatrix->_32, lpD3DMatrix->_33, lpD3DMatrix->_34); } else { @@ -404,7 +400,7 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat DirectX::XMStoreFloat4x4((DirectX::XMFLOAT4X4*)&ConvertHomogeneous.ToWorld_ViewMatrix, viewMatrix); // Store the view inverse matrix of the game, so we can transform the geometry with it - DirectX::XMMATRIX toViewSpace = DirectX::XMLoadFloat4x4((DirectX::XMFLOAT4X4*)lpD3DMatrix); + DirectX::XMMATRIX toViewSpace = DirectX::XMLoadFloat4x4((DirectX::XMFLOAT4X4*)&view); DirectX::XMMATRIX vp = DirectX::XMMatrixMultiply(viewMatrix, proj); DirectX::XMMATRIX vpinv = DirectX::XMMatrixInverse(nullptr, vp); @@ -412,6 +408,9 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat ConvertHomogeneous.ToWorld_ViewMatrixInverse = DirectX::XMMatrixMultiply(depthoffset, DirectX::XMMatrixMultiply(toViewSpace, vpinv)); } + + // Override original matrix pointer + lpD3DMatrix = &view; } } else From e79c2049df7415fd6f18e86b5d9d85aad1a26de7 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Tue, 6 Jun 2023 16:59:54 -0700 Subject: [PATCH 13/20] Fix DdrawConvertHomogeneousToWorldUseGameCamera --- ddraw/IDirect3DDeviceX.cpp | 42 +++++++++++++++++++++++++++++++++----- ddraw/IDirect3DDeviceX.h | 2 ++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index 6d76b9d7..1bf870fd 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -369,11 +369,43 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat if (Config.DdrawConvertHomogeneousToWorld) { DirectX::XMVECTOR position, direction; + float depthOffset = 0.0f; if (Config.DdrawConvertHomogeneousToWorldUseGameCamera) { // To reconstruct the 3D world, we need to know where the camera is and where it is looking - position = DirectX::XMVectorSet(lpD3DMatrix->_41, lpD3DMatrix->_42, lpD3DMatrix->_43, lpD3DMatrix->_44); - direction = DirectX::XMVectorSet(lpD3DMatrix->_31, lpD3DMatrix->_32, lpD3DMatrix->_33, lpD3DMatrix->_34); + position = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f); + + const float x = lpD3DMatrix->_11; + const float y = lpD3DMatrix->_12; + const float z = lpD3DMatrix->_13; + + float pitch = std::atan2(y, z); + if (pitch < 0.0f && y * z > 0.0f) // check if y and z have the same sign + { + // handle flipping of the pitch. This is not because the camera is looking up. + pitch += DirectX::XM_PI; + } + + float yaw = std::asin(x); + if (yaw < 0.0f) + { + yaw += DirectX::XM_2PI; + } + + // mirror the transform + float pitchneg = -pitch; + + float pitch_cos = std::cos(pitchneg); + float x2 = 0.0f; //std::cos(yaw) * pitch_cos; + float y2 = std::sin(pitchneg); + float z2 = /*std::sin(yaw) **/ pitch_cos; + + direction = DirectX::XMVectorSet(x2, y2, z2, 0.0f); + + depthOffset = Config.DdrawConvertHomogeneousToWorldDepthOffset; + + ConvertHomogeneous.ToWorld_GameCameraYaw = yaw; + ConvertHomogeneous.ToWorld_GameCameraPitch = pitch; } else { @@ -393,8 +425,8 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat DirectX::XMStoreFloat4x4((DirectX::XMFLOAT4X4*)&ConvertHomogeneous.ToWorld_ProjectionMatrix, proj); - DirectX::XMVECTOR up = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); - DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixLookToLH(position, direction, up); + DirectX::XMVECTOR upVector = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); + DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixLookToLH(position, direction, upVector); // Store the 3D view matrix so it can be set later DirectX::XMStoreFloat4x4((DirectX::XMFLOAT4X4*)&ConvertHomogeneous.ToWorld_ViewMatrix, viewMatrix); @@ -404,7 +436,7 @@ HRESULT m_IDirect3DDeviceX::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStat DirectX::XMMATRIX vp = DirectX::XMMatrixMultiply(viewMatrix, proj); DirectX::XMMATRIX vpinv = DirectX::XMMatrixInverse(nullptr, vp); - DirectX::XMMATRIX depthoffset = DirectX::XMMatrixTranslation(0.0f, 0.0f, Config.DdrawConvertHomogeneousToWorldDepthOffset); + DirectX::XMMATRIX depthoffset = DirectX::XMMatrixTranslation(0.0f, 0.0f, depthOffset); ConvertHomogeneous.ToWorld_ViewMatrixInverse = DirectX::XMMatrixMultiply(depthoffset, DirectX::XMMatrixMultiply(toViewSpace, vpinv)); } diff --git a/ddraw/IDirect3DDeviceX.h b/ddraw/IDirect3DDeviceX.h index 5df4f3ef..1efc94d8 100644 --- a/ddraw/IDirect3DDeviceX.h +++ b/ddraw/IDirect3DDeviceX.h @@ -11,6 +11,8 @@ struct CONVERTHOMOGENEOUS D3DMATRIX ToWorld_ViewMatrixOriginal; // Store the original view matrix, so we can restore it DirectX::XMMATRIX ToWorld_ViewMatrixInverse; // Store the inverse view matrix to transform the geometry on the cpu std::vector ToWorld_IntermediateGeometry; // Intermediate buffer for the geometry conversion + float ToWorld_GameCameraYaw = 0.0f; + float ToWorld_GameCameraPitch = 0.0f; }; class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject From 1a8607e8ba4deea769b0dae182a7e3ff0c6b8fab Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Mon, 19 Jun 2023 21:36:52 -0700 Subject: [PATCH 14/20] Fix merge conflict with main --- ddraw/IDirect3DDeviceX.cpp | 150 ++++++++++++++++++------------------- 1 file changed, 72 insertions(+), 78 deletions(-) diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index 0b42f538..c9308f38 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -2779,102 +2779,102 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy return DDERR_GENERIC; } - // Handle PositionT - if (Config.DdrawConvertHomogeneousW && (dwVertexTypeDesc & 0x0E) == D3DFVF_XYZRHW) - { - if (!ConvertHomogeneous.IsTransformViewSet) - { - D3DMATRIX Matrix = {}; - GetTransform(D3DTS_VIEW, &Matrix); - SetTransform(D3DTS_VIEW, &Matrix); - } + const UINT stride = GetVertexStride(dwVertexTypeDesc); - const UINT stride = GetVertexStride(dwVertexTypeDesc); + // Handle PositionT + if (Config.DdrawConvertHomogeneousW && (dwVertexTypeDesc & 0x0E) == D3DFVF_XYZRHW) + { + if (!ConvertHomogeneous.IsTransformViewSet) + { + D3DMATRIX Matrix = {}; + GetTransform(D3DTS_VIEW, &Matrix); + SetTransform(D3DTS_VIEW, &Matrix); + } - if (!Config.DdrawConvertHomogeneousToWorld) - { - /*UINT8 *vertex = (UINT8*)lpVertices; + if (!Config.DdrawConvertHomogeneousToWorld) + { + /*UINT8 *vertex = (UINT8*)lpVertices; - for (UINT x = 0; x < dwVertexCount; x++) - { - float *pos = (float*) vertex; + for (UINT x = 0; x < dwVertexCount; x++) + { + float *pos = (float*) vertex; - pos[3] = 1.0f; + pos[3] = 1.0f; - vertex += stride; - }*/ + vertex += stride; + }*/ - // Update the FVF - dwVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZW; - } - else - { - const UINT targetStride = stride - sizeof(float); - const UINT restSize = stride - sizeof(float) * 4; + // Update the FVF + dwVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZW; + } + else + { + const UINT targetStride = stride - sizeof(float); + const UINT restSize = stride - sizeof(float) * 4; - ConvertHomogeneous.ToWorld_IntermediateGeometry.resize(targetStride * dwVertexCount); + ConvertHomogeneous.ToWorld_IntermediateGeometry.resize(targetStride * dwVertexCount); - UINT8 *sourceVertex = (UINT8*)lpVertices; - UINT8 *targetVertex = (UINT8*)ConvertHomogeneous.ToWorld_IntermediateGeometry.data(); + UINT8* sourceVertex = (UINT8*)lpVertices; + UINT8* targetVertex = (UINT8*)ConvertHomogeneous.ToWorld_IntermediateGeometry.data(); - lpVertices = targetVertex; + lpVertices = targetVertex; - for (UINT x = 0; x < dwVertexCount; x++) - { - // Transform the vertices into world space - float *srcpos = (float*) sourceVertex; - float *trgtpos = (float*) targetVertex; + for (UINT x = 0; x < dwVertexCount; x++) + { + // Transform the vertices into world space + float* srcpos = (float*)sourceVertex; + float* trgtpos = (float*)targetVertex; - DirectX::XMVECTOR xpos = DirectX::XMVectorSet(srcpos[0], srcpos[1], srcpos[2], srcpos[3]); + DirectX::XMVECTOR xpos = DirectX::XMVectorSet(srcpos[0], srcpos[1], srcpos[2], srcpos[3]); - DirectX::XMVECTOR xpos_global = DirectX::XMVector3TransformCoord(xpos, ConvertHomogeneous.ToWorld_ViewMatrixInverse); + DirectX::XMVECTOR xpos_global = DirectX::XMVector3TransformCoord(xpos, ConvertHomogeneous.ToWorld_ViewMatrixInverse); - xpos_global = DirectX::XMVectorDivide(xpos_global, DirectX::XMVectorSplatW(xpos_global)); + xpos_global = DirectX::XMVectorDivide(xpos_global, DirectX::XMVectorSplatW(xpos_global)); - trgtpos[0] = DirectX::XMVectorGetX(xpos_global); - trgtpos[1] = DirectX::XMVectorGetY(xpos_global); - trgtpos[2] = DirectX::XMVectorGetZ(xpos_global); + trgtpos[0] = DirectX::XMVectorGetX(xpos_global); + trgtpos[1] = DirectX::XMVectorGetY(xpos_global); + trgtpos[2] = DirectX::XMVectorGetZ(xpos_global); - // Copy the rest - std::memcpy(targetVertex + sizeof(float) * 3, sourceVertex + sizeof(float) * 4, restSize); + // Copy the rest + std::memcpy(targetVertex + sizeof(float) * 3, sourceVertex + sizeof(float) * 4, restSize); - // Move to next vertex - sourceVertex += stride; - targetVertex += targetStride; - } + // Move to next vertex + sourceVertex += stride; + targetVertex += targetStride; + } - // Handle dwFlags - DWORD rsClipping = 0, rsLighting = 0, rsExtents = 0; - SetDrawFlags(rsClipping, rsLighting, rsExtents, dwVertexTypeDesc, dwFlags, DirectXVersion); + // Handle dwFlags + DWORD rsClipping = 0, rsLighting = 0, rsExtents = 0; + SetDrawFlags(rsClipping, rsLighting, rsExtents, dwVertexTypeDesc, dwFlags, DirectXVersion); - // Set transform - (*d3d9Device)->SetTransform(D3DTS_VIEW, &ConvertHomogeneous.ToWorld_ViewMatrix); - (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &ConvertHomogeneous.ToWorld_ProjectionMatrix); + // Set transform + (*d3d9Device)->SetTransform(D3DTS_VIEW, &ConvertHomogeneous.ToWorld_ViewMatrix); + (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &ConvertHomogeneous.ToWorld_ProjectionMatrix); - // Update the FVF - const DWORD newVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZ; + // Update the FVF + const DWORD newVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZ; - // Set fixed function vertex type - (*d3d9Device)->SetFVF(newVertexTypeDesc); + // Set fixed function vertex type + (*d3d9Device)->SetFVF(newVertexTypeDesc); - // Draw indexed primitive UP - hr = (*d3d9Device)->DrawIndexedPrimitiveUP(dptPrimitiveType, 0, dwVertexCount, GetNumberOfPrimitives(dptPrimitiveType, dwIndexCount), lpIndices, D3DFMT_INDEX16, lpVertices, targetStride); + // Draw indexed primitive UP + HRESULT hr = (*d3d9Device)->DrawIndexedPrimitiveUP(dptPrimitiveType, 0, dwVertexCount, GetNumberOfPrimitives(dptPrimitiveType, dwIndexCount), lpIndices, D3DFMT_INDEX16, lpVertices, targetStride); - // Restore transform - D3DMATRIX identityMatrix = {}; - identityMatrix._11 = 1.0f; - identityMatrix._22 = 1.0f; - identityMatrix._33 = 1.0f; + // Restore transform + D3DMATRIX identityMatrix = {}; + identityMatrix._11 = 1.0f; + identityMatrix._22 = 1.0f; + identityMatrix._33 = 1.0f; - (*d3d9Device)->SetTransform(D3DTS_VIEW, &ConvertHomogeneous.ToWorld_ViewMatrixOriginal); - (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &identityMatrix); + (*d3d9Device)->SetTransform(D3DTS_VIEW, &ConvertHomogeneous.ToWorld_ViewMatrixOriginal); + (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &identityMatrix); - // Handle dwFlags - UnSetDrawFlags(rsClipping, rsLighting, rsExtents, newVertexTypeDesc, dwFlags, DirectXVersion); + // Handle dwFlags + UnSetDrawFlags(rsClipping, rsLighting, rsExtents, newVertexTypeDesc, dwFlags, DirectXVersion); - return hr; - } - } + return hr; + } + } // Update vertices for Direct3D9 (needs to be first) UpdateVertices(dwVertexTypeDesc, lpVertices, dwVertexCount); @@ -2896,13 +2896,7 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy // Handle dwFlags UnSetDrawFlags(rsClipping, rsLighting, rsExtents, dwVertexTypeDesc, dwFlags, DirectXVersion); - // Set fixed function vertex type - (*d3d9Device)->SetFVF(dwVertexTypeDesc); - - // Draw indexed primitive UP - hr = (*d3d9Device)->DrawIndexedPrimitiveUP(dptPrimitiveType, 0, dwVertexCount, GetNumberOfPrimitives(dptPrimitiveType, dwIndexCount), lpIndices, D3DFMT_INDEX16, lpVertices, stride); - - if (FAILED(hr)) + if (FAILED(hr)) { LOG_LIMIT(100, __FUNCTION__ << " Error: 'DrawIndexedPrimitiveUP' call failed: " << (D3DERR)hr); } From f82ae80ef034a5dd10646500deef534c93ee095e Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Tue, 20 Jun 2023 09:07:30 -0700 Subject: [PATCH 15/20] Minor update --- ddraw/IDirect3DDeviceX.cpp | 4 ++-- ddraw/IDirect3DDeviceX.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index c9308f38..dbedc6dd 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -2779,8 +2779,6 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy return DDERR_GENERIC; } - const UINT stride = GetVertexStride(dwVertexTypeDesc); - // Handle PositionT if (Config.DdrawConvertHomogeneousW && (dwVertexTypeDesc & 0x0E) == D3DFVF_XYZRHW) { @@ -2809,6 +2807,8 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy } else { + const UINT stride = GetVertexStride(dwVertexTypeDesc); + const UINT targetStride = stride - sizeof(float); const UINT restSize = stride - sizeof(float) * 4; diff --git a/ddraw/IDirect3DDeviceX.h b/ddraw/IDirect3DDeviceX.h index 451e897b..035e526b 100644 --- a/ddraw/IDirect3DDeviceX.h +++ b/ddraw/IDirect3DDeviceX.h @@ -47,6 +47,9 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject // Vector temporary buffer cache std::vector VertexCache; + // The data used for rendering Homogeneous + CONVERTHOMOGENEOUS ConvertHomogeneous; + // Viewport array std::vector AttachedViewports; @@ -75,9 +78,6 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject return false; } - // The data used for rendering Homogeneous - CONVERTHOMOGENEOUS ConvertHomogeneous; - // Wrapper interface functions inline REFIID GetWrapperType(DWORD DirectXVersion) { From fac874bdaa3d9622e987d626a0ee70ea4d0aafc6 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Mon, 26 Jun 2023 14:29:38 -0700 Subject: [PATCH 16/20] Fix formatting --- ddraw/IDirect3DDeviceX.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index 48ef672c..53aa16c0 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -3474,7 +3474,7 @@ void m_IDirect3DDeviceX::InitDevice(DWORD DirectXVersion) return; } - if (ddrawParent) + if (ddrawParent) { d3d9Device = ddrawParent->GetDirect3D9Device(); } @@ -3500,9 +3500,9 @@ void m_IDirect3DDeviceX::InitDevice(DWORD DirectXVersion) ConvertHomogeneous.ToWorld_ProjectionMatrix = ConvertHomogeneous.ToWorld_ViewMatrix; ConvertHomogeneous.ToWorld_ViewMatrixOriginal = ConvertHomogeneous.ToWorld_ViewMatrix; - } + } - AddRef(DirectXVersion); + AddRef(DirectXVersion); } void m_IDirect3DDeviceX::ReleaseDevice() From 8b54596f1c94eb4f9c4fca9ae692db7b8934a294 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Mon, 26 Jun 2023 14:31:45 -0700 Subject: [PATCH 17/20] Fix defaults --- Settings/Settings.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Settings/Settings.cpp b/Settings/Settings.cpp index 85f4d9e1..218c7821 100644 --- a/Settings/Settings.cpp +++ b/Settings/Settings.cpp @@ -450,6 +450,7 @@ void Settings::SetDefaultConfigSettings() Config.DdrawEnableMouseHook = true; // Set other default values + Config.LoopSleepTime = 120; Config.WindowSleepTime = 500; Config.PrimaryBufferBits = 16; Config.PrimaryBufferSamples = 44100; From 3ee34d494440b11b8d11c02908e4e097dabe68fe Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Thu, 13 Jul 2023 11:49:35 -0700 Subject: [PATCH 18/20] Fix PR to work with master branch changes --- ddraw/IDirect3DDeviceX.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index 4a017675..10d0298b 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -2819,9 +2819,11 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy targetVertex += targetStride; } + // Check for color key + UpdateDrawFlags(dwFlags); + // Handle dwFlags - DWORD rsClipping = 0, rsLighting = 0, rsExtents = 0; - SetDrawFlags(rsClipping, rsLighting, rsExtents, dwVertexTypeDesc, dwFlags, DirectXVersion); + SetDrawStates(dwVertexTypeDesc, dwFlags, DirectXVersion); // Set transform (*d3d9Device)->SetTransform(D3DTS_VIEW, &ConvertHomogeneous.ToWorld_ViewMatrix); @@ -2846,7 +2848,7 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &identityMatrix); // Handle dwFlags - UnSetDrawFlags(rsClipping, rsLighting, rsExtents, newVertexTypeDesc, dwFlags, DirectXVersion); + RestoreDrawStates(newVertexTypeDesc, dwFlags, DirectXVersion); return hr; } From 0d9bb503cabadf603258052f1098885657be3208 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Thu, 13 Jul 2023 13:03:29 -0700 Subject: [PATCH 19/20] Update to make it match master branch --- ddraw/IDirect3DDeviceX.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/ddraw/IDirect3DDeviceX.cpp b/ddraw/IDirect3DDeviceX.cpp index 10d0298b..224ab5f0 100644 --- a/ddraw/IDirect3DDeviceX.cpp +++ b/ddraw/IDirect3DDeviceX.cpp @@ -2819,12 +2819,6 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy targetVertex += targetStride; } - // Check for color key - UpdateDrawFlags(dwFlags); - - // Handle dwFlags - SetDrawStates(dwVertexTypeDesc, dwFlags, DirectXVersion); - // Set transform (*d3d9Device)->SetTransform(D3DTS_VIEW, &ConvertHomogeneous.ToWorld_ViewMatrix); (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &ConvertHomogeneous.ToWorld_ProjectionMatrix); @@ -2833,11 +2827,24 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy const DWORD newVertexTypeDesc = (dwVertexTypeDesc & ~D3DFVF_XYZRHW) | D3DFVF_XYZ; // Set fixed function vertex type - (*d3d9Device)->SetFVF(newVertexTypeDesc); + if (FAILED((*d3d9Device)->SetFVF(newVertexTypeDesc))) + { + LOG_LIMIT(100, __FUNCTION__ << " Error: invalid FVF type: " << Logging::hex(dwVertexTypeDesc)); + return D3DERR_INVALIDVERTEXTYPE; + } + + // Check for color key + UpdateDrawFlags(dwFlags); + + // Handle dwFlags + SetDrawStates(newVertexTypeDesc, dwFlags, DirectXVersion); // Draw indexed primitive UP HRESULT hr = (*d3d9Device)->DrawIndexedPrimitiveUP(dptPrimitiveType, 0, dwVertexCount, GetNumberOfPrimitives(dptPrimitiveType, dwIndexCount), lpIndices, D3DFMT_INDEX16, lpVertices, targetStride); + // Handle dwFlags + RestoreDrawStates(newVertexTypeDesc, dwFlags, DirectXVersion); + // Restore transform D3DMATRIX identityMatrix = {}; identityMatrix._11 = 1.0f; @@ -2847,9 +2854,6 @@ HRESULT m_IDirect3DDeviceX::DrawIndexedPrimitive(D3DPRIMITIVETYPE dptPrimitiveTy (*d3d9Device)->SetTransform(D3DTS_VIEW, &ConvertHomogeneous.ToWorld_ViewMatrixOriginal); (*d3d9Device)->SetTransform(D3DTS_PROJECTION, &identityMatrix); - // Handle dwFlags - RestoreDrawStates(newVertexTypeDesc, dwFlags, DirectXVersion); - return hr; } } From 80dfe72ccee300d1cc270c117c351becd0c775b6 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Fri, 1 Mar 2024 17:04:37 -0800 Subject: [PATCH 20/20] Include DirectXMath --- .gitmodules | 3 +++ External/DirectXMath | 1 + Stub/BuildNo.rc | 2 +- ddraw/IDirect3DDeviceX.h | 2 +- 4 files changed, 6 insertions(+), 2 deletions(-) create mode 160000 External/DirectXMath diff --git a/.gitmodules b/.gitmodules index 7900f2e0..3417c839 100644 --- a/.gitmodules +++ b/.gitmodules @@ -20,3 +20,6 @@ [submodule "External/imgui"] path = External/imgui url = https://github.com/ocornut/imgui +[submodule "External/DirectXMath"] + path = External/DirectXMath + url = https://github.com/microsoft/DirectXMath diff --git a/External/DirectXMath b/External/DirectXMath new file mode 160000 index 00000000..d8375782 --- /dev/null +++ b/External/DirectXMath @@ -0,0 +1 @@ +Subproject commit d837578297c6c93849573858182350ede04987dc diff --git a/Stub/BuildNo.rc b/Stub/BuildNo.rc index 1a86af37..30a3d131 100644 --- a/Stub/BuildNo.rc +++ b/Stub/BuildNo.rc @@ -1 +1 @@ -#define BUILD_NUMBER 235 +#define BUILD_NUMBER 236 diff --git a/ddraw/IDirect3DDeviceX.h b/ddraw/IDirect3DDeviceX.h index 072f410d..22130886 100644 --- a/ddraw/IDirect3DDeviceX.h +++ b/ddraw/IDirect3DDeviceX.h @@ -1,7 +1,7 @@ #pragma once #include "IDirectDrawX.h" -#include +#include "External\DirectXMath\Inc\DirectXMath.h" struct CONVERTHOMOGENEOUS {