From b76d519682c77bfd92d32462fd9bd97527012bef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 26 Nov 2023 11:43:40 +0100 Subject: [PATCH 1/3] Minor langtool update --- Tools/langtool/Cargo.lock | 8 ++++---- Tools/langtool/src/inifile.rs | 9 +++------ Tools/langtool/src/section.rs | 10 ++++------ 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/Tools/langtool/Cargo.lock b/Tools/langtool/Cargo.lock index 2fadb455c5..e8dd042a50 100644 --- a/Tools/langtool/Cargo.lock +++ b/Tools/langtool/Cargo.lock @@ -76,9 +76,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.148" +version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" +checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" [[package]] name = "proc-macro-error" @@ -106,9 +106,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.67" +version = "1.0.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" dependencies = [ "unicode-ident", ] diff --git a/Tools/langtool/src/inifile.rs b/Tools/langtool/src/inifile.rs index c70f4636ec..f0c23bd298 100644 --- a/Tools/langtool/src/inifile.rs +++ b/Tools/langtool/src/inifile.rs @@ -120,12 +120,9 @@ impl IniFile { } pub fn get_section_mut(&mut self, section_name: &str) -> Option<&mut Section> { - for section in &mut self.sections { - if section.name == section_name { - return Some(section); - } - } - None + self.sections + .iter_mut() + .find(|section| section.name == section_name) } } diff --git a/Tools/langtool/src/section.rs b/Tools/langtool/src/section.rs index ec07dfcc72..f845d60716 100644 --- a/Tools/langtool/src/section.rs +++ b/Tools/langtool/src/section.rs @@ -117,12 +117,10 @@ impl Section { if prefix.starts_with("Font") || prefix.starts_with('#') { continue; } - if !other.lines.iter().any(|line| line.starts_with(prefix)) { - if !prefix.contains("URL") { - println!("Commenting out from {}: {line}", other.name); - // Comment out the line. - *line = "#".to_owned() + line; - } + if !other.lines.iter().any(|line| line.starts_with(prefix)) && !prefix.contains("URL") { + println!("Commenting out from {}: {line}", other.name); + // Comment out the line. + *line = "#".to_owned() + line; } } } From aec0606ba4b539aad1d59cc1ab474af5f0077aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 26 Nov 2023 11:13:48 +0100 Subject: [PATCH 2/3] Optimize the bounding box code for more vertex formats --- GPU/Common/DrawEngineCommon.cpp | 159 +++++++++++++++++++++----------- 1 file changed, 103 insertions(+), 56 deletions(-) diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 8d202f6a6c..8927a1c2a0 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -219,16 +219,17 @@ void DrawEngineCommon::UpdatePlanes() { Vec2f minViewport = (minOffset_ - Vec2f(gstate.getViewportXCenter(), gstate.getViewportYCenter())) * inverseViewportScale; Vec2f maxViewport = (maxOffset_ - Vec2f(gstate.getViewportXCenter(), gstate.getViewportYCenter())) * inverseViewportScale; - Lin::Matrix4x4 applyViewport; - applyViewport.empty(); + Vec2f viewportInvSize = Vec2f(1.0f / (maxViewport.x - minViewport.x), 1.0f / (maxViewport.y - minViewport.y)); + + Lin::Matrix4x4 applyViewport{}; // Scale to the viewport's size. - applyViewport.xx = 2.0f / (maxViewport.x - minViewport.x); - applyViewport.yy = 2.0f / (maxViewport.y - minViewport.y); + applyViewport.xx = 2.0f * viewportInvSize.x; + applyViewport.yy = 2.0f * viewportInvSize.y; applyViewport.zz = 1.0f; applyViewport.ww = 1.0f; // And offset to the viewport's centers. - applyViewport.wx = -(maxViewport.x + minViewport.x) / (maxViewport.x - minViewport.x); - applyViewport.wy = -(maxViewport.y + minViewport.y) / (maxViewport.y - minViewport.y); + applyViewport.wx = -(maxViewport.x + minViewport.x) * viewportInvSize.x; + applyViewport.wy = -(maxViewport.y + minViewport.y) * viewportInvSize.y; float mtx[16]; Matrix4ByMatrix4(mtx, worldviewproj, applyViewport.m); @@ -245,54 +246,29 @@ void DrawEngineCommon::UpdatePlanes() { // // It does the simplest and safest test possible: If all points of a bbox is outside a single of // our clipping planes, we reject the box. Tighter bounds would be desirable but would take more calculations. -bool DrawEngineCommon::TestBoundingBox(const void *control_points, const void *inds, int vertexCount, u32 vertType) { +// The name is a slight misnomer, because any bounding shape will work, not just boxes. +// +// Potential optimizations: +// * SIMD-ify the plane culling, and also the vertex data conversion (could even group together xxxxyyyyzzzz for example) +// * Compute min/max of the verts, and then compute a bounding sphere and check that against the planes. +// - Less accurate, but.. +// - Only requires six plane evaluations then. + +bool DrawEngineCommon::TestBoundingBox(const void *vdata, const void *inds, int vertexCount, u32 vertType) { + // Grab temp buffer space from large offsets in decoded_. Not exactly safe for large draws. + if (vertexCount > 1024) { + return true; + } + SimpleVertex *corners = (SimpleVertex *)(decoded_ + 65536 * 12); float *verts = (float *)(decoded_ + 65536 * 18); + int vertStride = 3; // Although this may lead to drawing that shouldn't happen, the viewport is more complex on VR. // Let's always say objects are within bounds. if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) return true; - // Try to skip NormalizeVertices if it's pure positions. No need to bother with a vertex decoder - // and a large vertex format. - if ((vertType & 0xFFFFFF) == GE_VTYPE_POS_FLOAT && !inds) { - verts = (float *)control_points; - } else if ((vertType & 0xFFFFFF) == GE_VTYPE_POS_8BIT && !inds) { - const s8 *vtx = (const s8 *)control_points; - for (int i = 0; i < vertexCount * 3; i++) { - verts[i] = vtx[i] * (1.0f / 128.0f); - } - } else if ((vertType & 0xFFFFFF) == GE_VTYPE_POS_16BIT && !inds) { - const s16 *vtx = (const s16 *)control_points; - for (int i = 0; i < vertexCount * 3; i++) { - verts[i] = vtx[i] * (1.0f / 32768.0f); - } - } else { - // Simplify away indices, bones, and morph before proceeding. - u8 *temp_buffer = decoded_ + 65536 * 24; - int vertexSize = 0; - - u16 indexLowerBound = 0; - u16 indexUpperBound = (u16)vertexCount - 1; - if (vertexCount > 0 && inds) { - GetIndexBounds(inds, vertexCount, vertType, &indexLowerBound, &indexUpperBound); - } - - // Force software skinning. - bool wasApplyingSkinInDecode = decOptions_.applySkinInDecode; - decOptions_.applySkinInDecode = true; - NormalizeVertices((u8 *)corners, temp_buffer, (const u8 *)control_points, indexLowerBound, indexUpperBound, vertType); - decOptions_.applySkinInDecode = wasApplyingSkinInDecode; - - IndexConverter conv(vertType, inds); - for (int i = 0; i < vertexCount; i++) { - verts[i * 3] = corners[conv(i)].pos.x; - verts[i * 3 + 1] = corners[conv(i)].pos.y; - verts[i * 3 + 2] = corners[conv(i)].pos.z; - } - } - // Due to world matrix updates per "thing", this isn't quite as effective as it could be if we did world transform // in here as well. Though, it still does cut down on a lot of updates in Tekken 6. if (gstate_c.IsDirty(DIRTY_CULL_PLANES)) { @@ -301,6 +277,78 @@ bool DrawEngineCommon::TestBoundingBox(const void *control_points, const void *i gstate_c.Clean(DIRTY_CULL_PLANES); } + // Try to skip NormalizeVertices if it's pure positions. No need to bother with a vertex decoder + // and a large vertex format. + if ((vertType & 0xFFFFFF) == GE_VTYPE_POS_FLOAT && !inds) { + verts = (float *)vdata; + } else if ((vertType & 0xFFFFFF) == GE_VTYPE_POS_8BIT && !inds) { + const s8 *vtx = (const s8 *)vdata; + for (int i = 0; i < vertexCount * 3; i++) { + verts[i] = vtx[i] * (1.0f / 128.0f); + } + } else if ((vertType & 0xFFFFFF) == GE_VTYPE_POS_16BIT && !inds) { + const s16 *vtx = (const s16 *)vdata; + for (int i = 0; i < vertexCount * 3; i++) { + verts[i] = vtx[i] * (1.0f / 32768.0f); + } + } else { + // Simplify away indices, bones, and morph before proceeding. + u8 *temp_buffer = decoded_ + 65536 * 24; + + if ((inds || (vertType & (GE_VTYPE_WEIGHT_MASK | GE_VTYPE_MORPHCOUNT_MASK)))) { + u16 indexLowerBound = 0; + u16 indexUpperBound = (u16)vertexCount - 1; + + if (vertexCount > 0 && inds) { + GetIndexBounds(inds, vertexCount, vertType, &indexLowerBound, &indexUpperBound); + } + // TODO: Avoid normalization if just plain skinning. + // Force software skinning. + bool wasApplyingSkinInDecode = decOptions_.applySkinInDecode; + decOptions_.applySkinInDecode = true; + NormalizeVertices((u8 *)corners, temp_buffer, (const u8 *)vdata, indexLowerBound, indexUpperBound, vertType); + decOptions_.applySkinInDecode = wasApplyingSkinInDecode; + + IndexConverter conv(vertType, inds); + for (int i = 0; i < vertexCount; i++) { + verts[i * 3] = corners[conv(i)].pos.x; + verts[i * 3 + 1] = corners[conv(i)].pos.y; + verts[i * 3 + 2] = corners[conv(i)].pos.z; + } + } else { + // Simple, most common case. + VertexDecoder *dec = GetVertexDecoder(vertType); + int stride = dec->VertexSize(); + int offset = dec->posoff; + switch (vertType & GE_VTYPE_POS_MASK) { + case GE_VTYPE_POS_8BIT: + for (int i = 0; i < vertexCount; i++) { + const s8 *data = (const s8 *)vdata + i * stride + offset; + for (int j = 0; j < 3; j++) { + verts[i * 3 + j] = data[j] * (1.0f / 128.0f); + } + } + break; + case GE_VTYPE_POS_16BIT: + for (int i = 0; i < vertexCount; i++) { + const s16 *data = ((const s16 *)((const s8 *)vdata + i * stride + offset)); + for (int j = 0; j < 3; j++) { + verts[i * 3 + j] = data[j] * (1.0f / 32768.0f); + } + } + break; + case GE_VTYPE_POS_FLOAT: + // No need to copy in this case, we can just read directly from the source format with a stride. + verts = (float *)((uint8_t *)vdata + offset); + vertStride = stride / 4; + // Previous code: + // for (int i = 0; i < vertexCount; i++) + // memcpy(&verts[i * 3], (const u8 *)vdata + stride * i + offset, sizeof(float) * 3); + break; + } + } + } + // Note: near/far are not checked without clamp/clip enabled, so we skip those planes. int totalPlanes = gstate.isDepthClampEnabled() ? 6 : 4; for (int plane = 0; plane < totalPlanes; plane++) { @@ -310,8 +358,9 @@ bool DrawEngineCommon::TestBoundingBox(const void *control_points, const void *i // Test against the frustum planes, and count. // TODO: We should test 4 vertices at a time using SIMD. // I guess could also test one vertex against 4 planes at a time, though a lot of waste at the common case of 6. - float value = planes_[plane].Test(verts + i * 3); - if (value <= -FLT_EPSILON) + const float *pos = verts + i * vertStride; + float value = planes_[plane].Test(pos); + if (value <= -FLT_EPSILON) // Not sure why we use exactly this value. Probably '< 0' would do. out++; else inside++; @@ -321,14 +370,12 @@ bool DrawEngineCommon::TestBoundingBox(const void *control_points, const void *i if (inside == 0) { // All out - but check for X and Y if the offset was near the cullbox edge. bool outsideEdge = false; - if (plane == 1) - outsideEdge = minOffset_.x < 1.0f; - if (plane == 2) - outsideEdge = minOffset_.y < 1.0f; - else if (plane == 0) - outsideEdge = maxOffset_.x >= 4096.0f; - else if (plane == 3) - outsideEdge = maxOffset_.y >= 4096.0f; + switch (plane) { + case 0: outsideEdge = maxOffset_.x >= 4096.0f; break; + case 1: outsideEdge = minOffset_.x < 1.0f; break; + case 2: outsideEdge = minOffset_.y < 1.0f; break; + case 3: outsideEdge = maxOffset_.y >= 4096.0f; break; + } // Only consider this outside if offset + scissor/region is fully inside the cullbox. if (!outsideEdge) From dae758e5f45ddd576c5e7abef025e54834ab3777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 26 Nov 2023 13:43:11 +0100 Subject: [PATCH 3/3] Fix some bugs and mistakes found by Nemoumbra through static analysis --- Common/VR/VRFramebuffer.cpp | 4 ++-- GPU/Common/FramebufferManagerCommon.cpp | 6 ++---- Windows/MainWindow.cpp | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Common/VR/VRFramebuffer.cpp b/Common/VR/VRFramebuffer.cpp index f4b83cc1c8..6fbceac036 100644 --- a/Common/VR/VRFramebuffer.cpp +++ b/Common/VR/VRFramebuffer.cpp @@ -109,8 +109,8 @@ static bool ovrFramebuffer_CreateGLES(XrSession session, ovrFramebuffer* frameBu swapChainCreateInfo.arraySize = multiview ? 2 : 1; #ifdef ANDROID + XrSwapchainCreateInfoFoveationFB swapChainFoveationCreateInfo; if (VR_GetPlatformFlag(VR_PLATFORM_EXTENSION_FOVEATION)) { - XrSwapchainCreateInfoFoveationFB swapChainFoveationCreateInfo; memset(&swapChainFoveationCreateInfo, 0, sizeof(swapChainFoveationCreateInfo)); swapChainFoveationCreateInfo.type = XR_TYPE_SWAPCHAIN_CREATE_INFO_FOVEATION_FB; swapChainCreateInfo.next = &swapChainFoveationCreateInfo; @@ -203,8 +203,8 @@ static bool ovrFramebuffer_CreateVK(XrSession session, ovrFramebuffer* frameBuff swapChainCreateInfo.arraySize = multiview ? 2 : 1; #ifdef ANDROID + XrSwapchainCreateInfoFoveationFB swapChainFoveationCreateInfo; if (VR_GetPlatformFlag(VR_PLATFORM_EXTENSION_FOVEATION)) { - XrSwapchainCreateInfoFoveationFB swapChainFoveationCreateInfo; memset(&swapChainFoveationCreateInfo, 0, sizeof(swapChainFoveationCreateInfo)); swapChainFoveationCreateInfo.type = XR_TYPE_SWAPCHAIN_CREATE_INFO_FOVEATION_FB; swapChainCreateInfo.next = &swapChainFoveationCreateInfo; diff --git a/GPU/Common/FramebufferManagerCommon.cpp b/GPU/Common/FramebufferManagerCommon.cpp index ac7a401d79..dc01f798c9 100644 --- a/GPU/Common/FramebufferManagerCommon.cpp +++ b/GPU/Common/FramebufferManagerCommon.cpp @@ -1593,10 +1593,8 @@ void FramebufferManagerCommon::CopyDisplayToOutput(bool reallyDirty) { if (!vfb) { if (Memory::IsValidAddress(fbaddr)) { // The game is displaying something directly from RAM. In GTA, it's decoded video. - if (!vfb) { - DrawFramebufferToOutput(Memory::GetPointerUnchecked(fbaddr), displayStride_, displayFormat_); - return; - } + DrawFramebufferToOutput(Memory::GetPointerUnchecked(fbaddr), displayStride_, displayFormat_); + return; } else { DEBUG_LOG(FRAMEBUF, "Found no FBO to display! displayFBPtr = %08x", fbaddr); // No framebuffer to display! Clear to black. diff --git a/Windows/MainWindow.cpp b/Windows/MainWindow.cpp index 2e52ae8d50..0b7f1afe5e 100644 --- a/Windows/MainWindow.cpp +++ b/Windows/MainWindow.cpp @@ -660,7 +660,7 @@ namespace MainWindow double now = time_now_d(); if ((now - lastMouseDown) < 0.001 * GetDoubleClickTime()) { float dx = lastMouseDownX - x; - float dy = lastMouseDownX - x; + float dy = lastMouseDownY - y; float distSq = dx * dx + dy * dy; if (distSq < 3.0f*3.0f && !g_Config.bShowTouchControls && !g_Config.bMouseControl && GetUIState() == UISTATE_INGAME && g_Config.bFullscreenOnDoubleclick) { SendToggleFullscreen(!g_Config.UseFullScreen());