From 0905b6a5adb1ea0700530a70206d032fb12bf088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 26 Nov 2023 11:14:13 +0100 Subject: [PATCH 01/10] Frustum-cull small draws Some games do a poor job of culling stuff, and some transparent sprites can be very expensive if they cause a copy. Skipping them if outside the viewport makes sense in that case. One example are the flame sprites in #17797 . Additionally, we should be able to cull through-mode draws easily, this one doesn't even try. --- Common/UI/View.cpp | 1 - GPU/Common/DrawEngineCommon.cpp | 25 +++++++++++++++ GPU/Common/DrawEngineCommon.h | 2 ++ GPU/GPU.h | 2 ++ GPU/GPUCommonHW.cpp | 54 ++++++++++++++++++++++++++++++--- 5 files changed, 79 insertions(+), 5 deletions(-) diff --git a/Common/UI/View.cpp b/Common/UI/View.cpp index b60f6ac88d..37f8e9ab02 100644 --- a/Common/UI/View.cpp +++ b/Common/UI/View.cpp @@ -620,7 +620,6 @@ CollapsibleHeader::CollapsibleHeader(bool *toggle, const std::string &text, Layo void CollapsibleHeader::Draw(UIContext &dc) { Style style = dc.theme->itemStyle; - style.background.color = 0; if (HasFocus()) style = dc.theme->itemFocusedStyle; if (down_) style = dc.theme->itemDownStyle; if (!IsEnabled()) style = dc.theme->itemDisabledStyle; diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 8927a1c2a0..2ce5daded7 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -670,6 +670,31 @@ int DrawEngineCommon::ExtendNonIndexedPrim(const uint32_t *cmd, const uint32_t * return cmd - start; } +void DrawEngineCommon::SkipPrim(GEPrimitiveType prim, int vertexCount, u32 vertTypeID, int *bytesRead) { + if (!indexGen.PrimCompatible(prevPrim_, prim)) { + DispatchFlush(); + } + + // This isn't exactly right, if we flushed, since prims can straddle previous calls. + // But it generally works for common usage. + if (prim == GE_PRIM_KEEP_PREVIOUS) { + // Has to be set to something, let's assume POINTS (0) if no previous. + if (prevPrim_ == GE_PRIM_INVALID) + prevPrim_ = GE_PRIM_POINTS; + prim = prevPrim_; + } else { + prevPrim_ = prim; + } + + // If vtype has changed, setup the vertex decoder. + if (vertTypeID != lastVType_ || !dec_) { + dec_ = GetVertexDecoder(vertTypeID); + lastVType_ = vertTypeID; + } + + *bytesRead = vertexCount * dec_->VertexSize(); +} + // vertTypeID is the vertex type but with the UVGen mode smashed into the top bits. bool DrawEngineCommon::SubmitPrim(const void *verts, const void *inds, GEPrimitiveType prim, int vertexCount, u32 vertTypeID, bool clockwise, int *bytesRead) { if (!indexGen.PrimCompatible(prevPrim_, prim) || numDrawVerts_ >= MAX_DEFERRED_DRAW_VERTS || numDrawInds_ >= MAX_DEFERRED_DRAW_INDS || vertexCountInDrawCalls_ + vertexCount > VERTEX_BUFFER_MAX) { diff --git a/GPU/Common/DrawEngineCommon.h b/GPU/Common/DrawEngineCommon.h index e9e8870ef3..ad274bb692 100644 --- a/GPU/Common/DrawEngineCommon.h +++ b/GPU/Common/DrawEngineCommon.h @@ -113,6 +113,8 @@ public: int ExtendNonIndexedPrim(const uint32_t *cmd, const uint32_t *stall, u32 vertTypeID, bool clockwise, int *bytesRead, bool isTriangle); bool SubmitPrim(const void *verts, const void *inds, GEPrimitiveType prim, int vertexCount, u32 vertTypeID, bool clockwise, int *bytesRead); + void SkipPrim(GEPrimitiveType prim, int vertexCount, u32 vertTypeID, int *bytesRead); + template void SubmitCurve(const void *control_points, const void *indices, Surface &surface, u32 vertType, int *bytesRead, const char *scope); void ClearSplineBezierWeights(); diff --git a/GPU/GPU.h b/GPU/GPU.h index 7d4d4d1c0a..f2edbdc1f6 100644 --- a/GPU/GPU.h +++ b/GPU/GPU.h @@ -76,6 +76,7 @@ struct GPUStatistics { void ResetFrame() { numDrawCalls = 0; numVertexDecodes = 0; + numCulledDraws = 0; numDrawSyncs = 0; numListSyncs = 0; numVertsSubmitted = 0; @@ -111,6 +112,7 @@ struct GPUStatistics { // Per frame statistics int numDrawCalls; int numVertexDecodes; + int numCulledDraws; int numDrawSyncs; int numListSyncs; int numFlushes; diff --git a/GPU/GPUCommonHW.cpp b/GPU/GPUCommonHW.cpp index f961880d8a..e064fe7454 100644 --- a/GPU/GPUCommonHW.cpp +++ b/GPU/GPUCommonHW.cpp @@ -989,9 +989,36 @@ void GPUCommonHW::Execute_Prim(u32 op, u32 diff) { int cullMode = gstate.getCullMode(); uint32_t vertTypeID = GetVertTypeID(vertexType, gstate.getUVGenMode(), g_Config.bSoftwareSkinning); - if (!drawEngineCommon_->SubmitPrim(verts, inds, prim, count, vertTypeID, true, &bytesRead)) { + +#define MAX_CULL_CHECK_COUNT 6 + +#define PASSES_CULLING ((vertexType & (GE_VTYPE_THROUGH_MASK | GE_VTYPE_MORPHCOUNT_MASK | GE_VTYPE_WEIGHT_MASK | GE_VTYPE_IDX_MASK)) || count > MAX_CULL_CHECK_COUNT) + + // If certain conditions are true, do frustum culling. + bool passCulling = PASSES_CULLING; + if (!passCulling) { + // Do software culling. + if (drawEngineCommon_->TestBoundingBox(verts, inds, count, vertexType)) { + passCulling = true; + } else { + gpuStats.numCulledDraws++; + } + } + + // If the first one in a batch passes, let's assume the whole batch passes. + // Cuts down on checking, while not losing that much efficiency. + bool onePassed = false; + if (passCulling) { + if (!drawEngineCommon_->SubmitPrim(verts, inds, prim, count, vertTypeID, true, &bytesRead)) { + canExtend = false; + } + onePassed = true; + } else { + // Still need to advance bytesRead. + drawEngineCommon_->SkipPrim(prim, count, vertTypeID, &bytesRead); canExtend = false; } + // After drawing, we advance the vertexAddr (when non indexed) or indexAddr (when indexed). // Some games rely on this, they don't bother reloading VADDR and IADDR. // The VADDR/IADDR registers are NOT updated. @@ -1027,7 +1054,7 @@ void GPUCommonHW::Execute_Prim(u32 op, u32 diff) { bool clockwise = !gstate.isCullEnabled() || gstate.getCullMode() == cullMode; if (canExtend) { // Non-indexed draws can be cheaply merged if vertexAddr hasn't changed, that means the vertices - // are consecutive in memory. + // are consecutive in memory. We also ignore culling here. _dbg_assert_((vertexType & GE_VTYPE_IDX_MASK) == GE_VTYPE_IDX_NONE); int commandsExecuted = drawEngineCommon_->ExtendNonIndexedPrim(src, stall, vertTypeID, clockwise, &bytesRead, isTriangle); if (!commandsExecuted) { @@ -1047,7 +1074,25 @@ void GPUCommonHW::Execute_Prim(u32 op, u32 diff) { // We can extend again after submitting a normal draw. canExtend = isTriangle; } - if (!drawEngineCommon_->SubmitPrim(verts, inds, newPrim, count, vertTypeID, clockwise, &bytesRead)) { + + bool passCulling = onePassed || PASSES_CULLING; + if (!passCulling) { + // Do software culling. + if (drawEngineCommon_->TestBoundingBox(verts, inds, count, vertexType)) { + passCulling = true; + } else { + gpuStats.numCulledDraws++; + } + } + if (passCulling) { + if (!drawEngineCommon_->SubmitPrim(verts, inds, newPrim, count, vertTypeID, clockwise, &bytesRead)) { + canExtend = false; + } + // As soon as one passes, assume we don't need to check the rest of this batch. + onePassed = true; + } else { + // Still need to advance bytesRead. + drawEngineCommon_->SkipPrim(newPrim, count, vertTypeID, &bytesRead); canExtend = false; } AdvanceVerts(vertexType, count, bytesRead); @@ -1691,7 +1736,7 @@ size_t GPUCommonHW::FormatGPUStatsCommon(char *buffer, size_t size) { float vertexAverageCycles = gpuStats.numVertsSubmitted > 0 ? (float)gpuStats.vertexGPUCycles / (float)gpuStats.numVertsSubmitted : 0.0f; return snprintf(buffer, size, "DL processing time: %0.2f ms, %d drawsync, %d listsync\n" - "Draw: %d (%d dec), flushes %d, clears %d, bbox jumps %d (%d updates)\n" + "Draw: %d (%d dec, %d culled), flushes %d, clears %d, bbox jumps %d (%d updates)\n" "Vertices: %d drawn: %d\n" "FBOs active: %d (evaluations: %d)\n" "Textures: %d, dec: %d, invalidated: %d, hashed: %d kB\n" @@ -1705,6 +1750,7 @@ size_t GPUCommonHW::FormatGPUStatsCommon(char *buffer, size_t size) { gpuStats.numListSyncs, gpuStats.numDrawCalls, gpuStats.numVertexDecodes, + gpuStats.numCulledDraws, gpuStats.numFlushes, gpuStats.numClears, gpuStats.numBBOXJumps, From 89d8ef87ecf3e4f3c3970d4aca376ed1b41d0931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 4 Dec 2023 18:54:46 +0100 Subject: [PATCH 02/10] Use a less accurate but faster frustum cull for the general draws. --- GPU/Common/DrawEngineCommon.cpp | 81 +++++++++++++++++++++++++++++++++ GPU/Common/DrawEngineCommon.h | 5 ++ GPU/GPUCommonHW.cpp | 2 +- 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 2ce5daded7..9065ab9fc4 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -214,6 +214,9 @@ void DrawEngineCommon::UpdatePlanes() { minOffset_ = baseOffset + Vec2f(std::max(gstate.getRegionRateX() - 0x100, gstate.getScissorX1()), std::max(gstate.getRegionRateY() - 0x100, gstate.getScissorY1())) - Vec2f(1.0f, 1.0f); maxOffset_ = baseOffset + Vec2f(std::min(gstate.getRegionX2(), gstate.getScissorX2()), std::min(gstate.getRegionY2(), gstate.getScissorY2())) + Vec2f(1.0f, 1.0f); + // Let's not handle these special cases in the fast culler. + offsetOutsideEdge_ = maxOffset_.x >= 4096.0f || minOffset_.x < 1.0f || minOffset_.y < 1.0f || maxOffset_.y >= 4096.0f; + // Now let's apply the viewport to our scissor/region + offset range. Vec2f inverseViewportScale = Vec2f(1.0f / gstate.getViewportXScale(), 1.0f / gstate.getViewportYScale()); Vec2f minViewport = (minOffset_ - Vec2f(gstate.getViewportXCenter(), gstate.getViewportYCenter())) * inverseViewportScale; @@ -388,6 +391,84 @@ bool DrawEngineCommon::TestBoundingBox(const void *vdata, const void *inds, int return true; } +// NOTE: This doesn't handle through-mode, indexing, morph, or skinning. +bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u32 vertType) { + 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; + + // 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)) { + UpdatePlanes(); + gpuStats.numPlaneUpdates++; + gstate_c.Clean(DIRTY_CULL_PLANES); + } + + // Also let's just bail if offsetOutsideEdge_ is set, instead of handling the cases. + if (offsetOutsideEdge_) + return true; + + // 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; + break; + } + + // We only check the 4 sides. Near/far won't likely make a huge difference. + // This will be nice for SIMD. + int totalPlanes = 4; + for (int plane = 0; plane < totalPlanes; plane++) { + int inside = 0; + for (int i = 0; i < vertexCount; 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. + const float *pos = verts + i * vertStride; + float value = planes_[plane].Test(pos); + if (value >= 0.0f) + inside++; + } + + // No vertices inside this one plane? Don't need to draw. + if (inside == 0) { + // All out - but check for X and Y if the offset was near the cullbox edge. + return false; + } + + // Any out. For testing that the planes are in the right locations. + // if (out != 0) return false; + } + return true; +} + // TODO: This probably is not the best interface. bool DrawEngineCommon::GetCurrentSimpleVertices(int count, std::vector &vertices, std::vector &indices) { // This is always for the current vertices. diff --git a/GPU/Common/DrawEngineCommon.h b/GPU/Common/DrawEngineCommon.h index ad274bb692..b4b6313071 100644 --- a/GPU/Common/DrawEngineCommon.h +++ b/GPU/Common/DrawEngineCommon.h @@ -104,6 +104,10 @@ public: bool TestBoundingBox(const void *control_points, const void *inds, int vertexCount, u32 vertType); + // This is a less accurate version of TestBoundingBox, but faster. Can have more false positives. + // Doesn't support indexing. + bool TestBoundingBoxFast(const void *control_points, int vertexCount, u32 vertType); + void FlushSkin() { bool applySkin = (lastVType_ & GE_VTYPE_WEIGHT_MASK) && decOptions_.applySkinInDecode; if (applySkin) { @@ -292,4 +296,5 @@ protected: Plane planes_[6]; Vec2f minOffset_; Vec2f maxOffset_; + bool offsetOutsideEdge_; }; diff --git a/GPU/GPUCommonHW.cpp b/GPU/GPUCommonHW.cpp index e064fe7454..d6ae039d33 100644 --- a/GPU/GPUCommonHW.cpp +++ b/GPU/GPUCommonHW.cpp @@ -998,7 +998,7 @@ void GPUCommonHW::Execute_Prim(u32 op, u32 diff) { bool passCulling = PASSES_CULLING; if (!passCulling) { // Do software culling. - if (drawEngineCommon_->TestBoundingBox(verts, inds, count, vertexType)) { + if (drawEngineCommon_->TestBoundingBoxFast(verts, count, vertexType)) { passCulling = true; } else { gpuStats.numCulledDraws++; From dbf796bb66b37a4a5d4a75ea73947902af0b66d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 5 Dec 2023 00:53:35 +0100 Subject: [PATCH 03/10] Fastcull: SSE/NEON-optimize 16-bit position conversion --- GPU/Common/DrawEngineCommon.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 9065ab9fc4..6a62a24f40 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -395,7 +395,6 @@ bool DrawEngineCommon::TestBoundingBox(const void *vdata, const void *inds, int bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u32 vertType) { 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. @@ -418,6 +417,9 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u VertexDecoder *dec = GetVertexDecoder(vertType); int stride = dec->VertexSize(); int offset = dec->posoff; + int vertStride = 3; + + // TODO: Possibly do the plane tests directly against the source formats instead of converting. switch (vertType & GE_VTYPE_POS_MASK) { case GE_VTYPE_POS_8BIT: for (int i = 0; i < vertexCount; i++) { @@ -428,13 +430,34 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u } break; case GE_VTYPE_POS_16BIT: + { +#if defined(_M_SSE) + __m128 scaleFactor = _mm_set1_ps(1.0f / 32768.0f); + for (int i = 0; i < vertexCount; i++) { + const s16 *data = ((const s16 *)((const s8 *)vdata + i * stride + offset)); + __m128i bits = _mm_castpd_si128(_mm_load_sd((const double *)data)); + // Sign extension. Ugly without SSE4. + bits = _mm_srai_epi32(_mm_unpacklo_epi16(bits, bits), 16); + __m128 pos = _mm_mul_ps(_mm_cvtepi32_ps(bits), scaleFactor); + _mm_storeu_ps(verts + i * 3, pos); // TODO: use stride 4 to avoid clashing writes? + } +#elif PPSSPP_ARCH(ARM_NEON) + for (int i = 0; i < vertexCount; i++) { + const int16_t *dataPtr = ((const int16_t *)((const s8 *)vdata + i * stride + offset)); + int32x4_t data = vmovl_s16(vld1_s16(dataPtr)); + float32x4_t pos = vcvtq_n_s32_f32(data, 15); // This does the division by 32768.0f, effectively. + vst1q_f32(verts + i * 3, pos); + } +#else 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); } } +#endif 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); From a043962447eb4410b8a65602e05f9c20e510ee89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 9 Dec 2023 00:13:01 +0100 Subject: [PATCH 04/10] World space planes --- GPU/Common/DrawEngineCommon.cpp | 22 ++++++++++++---------- GPU/GPUCommonHW.cpp | 4 ++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 6a62a24f40..4e2043b7f8 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -197,15 +197,10 @@ void DrawEngineCommon::DispatchSubmitImm(GEPrimitiveType prim, TransformedVertex // Gated by DIRTY_CULL_PLANES void DrawEngineCommon::UpdatePlanes() { - float world[16]; float view[16]; - float worldview[16]; - float worldviewproj[16]; - ConvertMatrix4x3To4x4(world, gstate.worldMatrix); + float viewproj[16]; ConvertMatrix4x3To4x4(view, gstate.viewMatrix); - // TODO: Create a Matrix4x3ByMatrix4x3, and Matrix4x4ByMatrix4x3? - Matrix4ByMatrix4(worldview, world, view); - Matrix4ByMatrix4(worldviewproj, worldview, gstate.projMatrix); + Matrix4ByMatrix4(viewproj, view, gstate.projMatrix); // Next, we need to apply viewport, scissor, region, and even offset - but only for X/Y. // Note that the PSP does not clip against the viewport. @@ -235,7 +230,7 @@ void DrawEngineCommon::UpdatePlanes() { applyViewport.wy = -(maxViewport.y + minViewport.y) * viewportInvSize.y; float mtx[16]; - Matrix4ByMatrix4(mtx, worldviewproj, applyViewport.m); + Matrix4ByMatrix4(mtx, viewproj, applyViewport.m); planes_[0].Set(mtx[3] - mtx[0], mtx[7] - mtx[4], mtx[11] - mtx[8], mtx[15] - mtx[12]); // Right planes_[1].Set(mtx[3] + mtx[0], mtx[7] + mtx[4], mtx[11] + mtx[8], mtx[15] + mtx[12]); // Left @@ -362,7 +357,9 @@ bool DrawEngineCommon::TestBoundingBox(const void *vdata, const void *inds, int // 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. const float *pos = verts + i * vertStride; - float value = planes_[plane].Test(pos); + float worldpos[3]; + Vec3ByMatrix43(worldpos, pos, gstate.worldMatrix); + float value = planes_[plane].Test(worldpos); if (value <= -FLT_EPSILON) // Not sure why we use exactly this value. Probably '< 0' would do. out++; else @@ -470,14 +467,19 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u int totalPlanes = 4; for (int plane = 0; plane < totalPlanes; plane++) { int inside = 0; + int out = 0; for (int i = 0; i < vertexCount; 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. const float *pos = verts + i * vertStride; - float value = planes_[plane].Test(pos); + float worldpos[3]; + Vec3ByMatrix43(worldpos, pos, gstate.worldMatrix); + float value = planes_[plane].Test(worldpos); if (value >= 0.0f) inside++; + else + out++; } // No vertices inside this one plane? Don't need to draw. diff --git a/GPU/GPUCommonHW.cpp b/GPU/GPUCommonHW.cpp index d6ae039d33..8af997b67b 100644 --- a/GPU/GPUCommonHW.cpp +++ b/GPU/GPUCommonHW.cpp @@ -1457,7 +1457,7 @@ void GPUCommonHW::Execute_WorldMtxNum(u32 op, u32 diff) { if (dst[i] != newVal) { Flush(); dst[i] = newVal; - gstate_c.Dirty(DIRTY_WORLDMATRIX | DIRTY_CULL_PLANES); + gstate_c.Dirty(DIRTY_WORLDMATRIX); } if (++i >= end) { break; @@ -1480,7 +1480,7 @@ void GPUCommonHW::Execute_WorldMtxData(u32 op, u32 diff) { if (num < 12 && newVal != ((const u32 *)gstate.worldMatrix)[num]) { Flush(); ((u32 *)gstate.worldMatrix)[num] = newVal; - gstate_c.Dirty(DIRTY_WORLDMATRIX | DIRTY_CULL_PLANES); + gstate_c.Dirty(DIRTY_WORLDMATRIX); } num++; gstate.worldmtxnum = (GE_CMD_WORLDMATRIXNUMBER << 24) | (num & 0x00FFFFFF); From 62c936babf5cdfcfa7dbe048aacb2c9f04aac81a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 9 Dec 2023 00:55:02 +0100 Subject: [PATCH 05/10] Flip the cull plane data around to avoid transforming each vertex multiple times. --- GPU/Common/DrawEngineCommon.cpp | 71 +++++++++++++++------------------ GPU/Common/DrawEngineCommon.h | 12 +++--- 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 4e2043b7f8..40590da54f 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -231,13 +231,13 @@ void DrawEngineCommon::UpdatePlanes() { float mtx[16]; Matrix4ByMatrix4(mtx, viewproj, applyViewport.m); - - planes_[0].Set(mtx[3] - mtx[0], mtx[7] - mtx[4], mtx[11] - mtx[8], mtx[15] - mtx[12]); // Right - planes_[1].Set(mtx[3] + mtx[0], mtx[7] + mtx[4], mtx[11] + mtx[8], mtx[15] + mtx[12]); // Left - planes_[2].Set(mtx[3] + mtx[1], mtx[7] + mtx[5], mtx[11] + mtx[9], mtx[15] + mtx[13]); // Bottom - planes_[3].Set(mtx[3] - mtx[1], mtx[7] - mtx[5], mtx[11] - mtx[9], mtx[15] - mtx[13]); // Top - planes_[4].Set(mtx[3] + mtx[2], mtx[7] + mtx[6], mtx[11] + mtx[10], mtx[15] + mtx[14]); // Near - planes_[5].Set(mtx[3] - mtx[2], mtx[7] - mtx[6], mtx[11] - mtx[10], mtx[15] - mtx[14]); // Far + // I'm sure there's some fairly optimized way to set these. + planes_.Set(0, mtx[3] - mtx[0], mtx[7] - mtx[4], mtx[11] - mtx[8], mtx[15] - mtx[12]); // Right + planes_.Set(1, mtx[3] + mtx[0], mtx[7] + mtx[4], mtx[11] + mtx[8], mtx[15] + mtx[12]); // Left + planes_.Set(2, mtx[3] + mtx[1], mtx[7] + mtx[5], mtx[11] + mtx[9], mtx[15] + mtx[13]); // Bottom + planes_.Set(3, mtx[3] - mtx[1], mtx[7] - mtx[5], mtx[11] - mtx[9], mtx[15] - mtx[13]); // Top + planes_.Set(4, mtx[3] + mtx[2], mtx[7] + mtx[6], mtx[11] + mtx[10], mtx[15] + mtx[14]); // Near + planes_.Set(5, mtx[3] - mtx[2], mtx[7] - mtx[6], mtx[11] - mtx[10], mtx[15] - mtx[14]); // Far } // This code has plenty of potential for optimization. @@ -260,7 +260,6 @@ bool DrawEngineCommon::TestBoundingBox(const void *vdata, const void *inds, int 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. @@ -336,17 +335,23 @@ bool DrawEngineCommon::TestBoundingBox(const void *vdata, const void *inds, int } 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); + for (int i = 0; i < vertexCount; i++) + memcpy(&verts[i * 3], (const u8 *)vdata + stride * i + offset, sizeof(float) * 3); break; } } } + // Pretransform the verts in-place so we don't have to do it inside the loop. + // We do this differently in the fast version below since we skip the max/minOffset checks there + // making it easier to get the whole thing ready for SIMD. + for (int i = 0; i < vertexCount; i++) { + float worldpos[3]; + Vec3ByMatrix43(worldpos, &verts[i * 3], gstate.worldMatrix); + memcpy(&verts[i * 3], worldpos, 12); + } + // 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++) { @@ -356,10 +361,8 @@ bool DrawEngineCommon::TestBoundingBox(const void *vdata, const void *inds, int // 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. - const float *pos = verts + i * vertStride; - float worldpos[3]; - Vec3ByMatrix43(worldpos, pos, gstate.worldMatrix); - float value = planes_[plane].Test(worldpos); + const float *worldpos = verts + i * 3; + float value = planes_.Test(plane, worldpos); if (value <= -FLT_EPSILON) // Not sure why we use exactly this value. Probably '< 0' would do. out++; else @@ -463,33 +466,25 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u } // We only check the 4 sides. Near/far won't likely make a huge difference. - // This will be nice for SIMD. + // We test one vertex against 4 planes to get some SIMD. Vertices need to be transformed to world space + // for testing, don't want to re-do that, so we have to use that "pivot" of the data. + int inside[4]{}; int totalPlanes = 4; - for (int plane = 0; plane < totalPlanes; plane++) { - int inside = 0; - int out = 0; - for (int i = 0; i < vertexCount; 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. - const float *pos = verts + i * vertStride; - float worldpos[3]; - Vec3ByMatrix43(worldpos, pos, gstate.worldMatrix); - float value = planes_[plane].Test(worldpos); + for (int i = 0; i < vertexCount; i++) { + const float *pos = verts + i * vertStride; + float worldpos[3]; + Vec3ByMatrix43(worldpos, pos, gstate.worldMatrix); + for (int plane = 0; plane < 4; plane++) { + float value = planes_.Test(plane, worldpos); if (value >= 0.0f) - inside++; - else - out++; + inside[plane]++; } + } - // No vertices inside this one plane? Don't need to draw. - if (inside == 0) { - // All out - but check for X and Y if the offset was near the cullbox edge. + for (int plane = 0; plane < 4; plane++) { + if (inside[plane] == 0) { return false; } - - // Any out. For testing that the planes are in the right locations. - // if (out != 0) return false; } return true; } diff --git a/GPU/Common/DrawEngineCommon.h b/GPU/Common/DrawEngineCommon.h index b4b6313071..147a1178bd 100644 --- a/GPU/Common/DrawEngineCommon.h +++ b/GPU/Common/DrawEngineCommon.h @@ -69,11 +69,11 @@ public: virtual void SendDataToShader(const SimpleVertex *const *points, int size_u, int size_v, u32 vertType, const Spline::Weight2D &weights) = 0; }; -// Culling plane. -struct Plane { - float x, y, z, w; - void Set(float _x, float _y, float _z, float _w) { x = _x; y = _y; z = _z; w = _w; } - float Test(const float f[3]) const { return x * f[0] + y * f[1] + z * f[2] + w; } +// Culling plane, group of 8. +struct alignas(16) Plane8 { + float x[8], y[8], z[8], w[8]; + void Set(int i, float _x, float _y, float _z, float _w) { x[i] = _x; y[i] = _y; z[i] = _z; w[i] = _w; } + float Test(int i, const float f[3]) const { return x[i] * f[0] + y[i] * f[1] + z[i] * f[2] + w[i]; } }; class DrawEngineCommon { @@ -293,7 +293,7 @@ protected: TessellationDataTransfer *tessDataTransfer; // Culling - Plane planes_[6]; + Plane8 planes_; Vec2f minOffset_; Vec2f maxOffset_; bool offsetOutsideEdge_; From 5b44e2515046b44ee2956f23aef7538323f609e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 9 Dec 2023 01:26:28 +0100 Subject: [PATCH 06/10] SSE-optimize the frustum culling --- GPU/Common/DrawEngineCommon.cpp | 46 ++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 40590da54f..3c289d9e56 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -410,6 +410,7 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u } // Also let's just bail if offsetOutsideEdge_ is set, instead of handling the cases. + // NOTE: This is written to in UpdatePlanes so can't check it before. if (offsetOutsideEdge_) return true; @@ -468,8 +469,49 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u // We only check the 4 sides. Near/far won't likely make a huge difference. // We test one vertex against 4 planes to get some SIMD. Vertices need to be transformed to world space // for testing, don't want to re-do that, so we have to use that "pivot" of the data. +#ifdef _M_SSE + __m128 inside = _mm_set1_ps(0.0f); + __m128 worldX = _mm_loadu_ps(gstate.worldMatrix); + __m128 worldY = _mm_loadu_ps(gstate.worldMatrix + 3); + __m128 worldZ = _mm_loadu_ps(gstate.worldMatrix + 6); + __m128 worldW = _mm_loadu_ps(gstate.worldMatrix + 9); + __m128 planeX = _mm_loadu_ps(planes_.x); + __m128 planeY = _mm_loadu_ps(planes_.y); + __m128 planeZ = _mm_loadu_ps(planes_.z); + __m128 planeW = _mm_loadu_ps(planes_.w); + for (int i = 0; i < vertexCount; i++) { + const float *pos = verts + i * vertStride; + __m128 worldpos = _mm_add_ps( + _mm_add_ps( + _mm_mul_ps(worldX, _mm_set1_ps(pos[0])), + _mm_mul_ps(worldY, _mm_set1_ps(pos[1])) + ), + _mm_add_ps( + _mm_mul_ps(worldZ, _mm_set1_ps(pos[2])), + worldW + ) + ); + // OK, now we check it against the four planes. + // This is really curiously similar to a matrix multiplication (well, it is one). + __m128 posX = _mm_shuffle_ps(worldpos, worldpos, 0); + __m128 posY = _mm_shuffle_ps(worldpos, worldpos, 1 | (1 << 2) | (1 << 4) | (1 << 6)); + __m128 posZ = _mm_shuffle_ps(worldpos, worldpos, 2 | (2 << 2) | (2 << 4) | (2 << 6)); + __m128 planeDist = _mm_add_ps( + _mm_add_ps( + _mm_mul_ps(planeX, posX), + _mm_mul_ps(planeY, posY) + ), + _mm_add_ps( + _mm_mul_ps(planeZ, posZ), + planeW + ) + ); + inside = _mm_or_ps(inside, _mm_cmpge_ps(planeDist, _mm_setzero_ps())); + } + u8 mask = _mm_movemask_ps(inside); + return mask == 0xF; // 0xF means that we found at least one vertex inside every one of the planes. We don't bother with counts, though it wouldn't be hard. +#else int inside[4]{}; - int totalPlanes = 4; for (int i = 0; i < vertexCount; i++) { const float *pos = verts + i * vertStride; float worldpos[3]; @@ -486,6 +528,8 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u return false; } } +#endif + return true; } From 6a7ef83f4b987e3059c056d46b0d2e0c2bc85c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 9 Dec 2023 12:36:47 +0100 Subject: [PATCH 07/10] NEON-optimize the culling --- Common/Common.vcxproj | 1 + Common/Common.vcxproj.filters | 3 ++ GPU/Common/DrawEngineCommon.cpp | 60 +++++++++++++++++++++++++++------ 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/Common/Common.vcxproj b/Common/Common.vcxproj index 93e91681b0..2b31119bd9 100644 --- a/Common/Common.vcxproj +++ b/Common/Common.vcxproj @@ -484,6 +484,7 @@ + diff --git a/Common/Common.vcxproj.filters b/Common/Common.vcxproj.filters index 73aefb9323..6d82c03c04 100644 --- a/Common/Common.vcxproj.filters +++ b/Common/Common.vcxproj.filters @@ -518,6 +518,9 @@ GPU\Vulkan + + Math + diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 3c289d9e56..c551eb6fbf 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -442,11 +442,12 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u __m128 pos = _mm_mul_ps(_mm_cvtepi32_ps(bits), scaleFactor); _mm_storeu_ps(verts + i * 3, pos); // TODO: use stride 4 to avoid clashing writes? } -#elif PPSSPP_ARCH(ARM_NEON) +#elif 0 && PPSSPP_ARCH(ARM_NEON) + __m128 scaleFactor = vdupq_n_f32(1.0f / 32768.0f); for (int i = 0; i < vertexCount; i++) { const int16_t *dataPtr = ((const int16_t *)((const s8 *)vdata + i * stride + offset)); int32x4_t data = vmovl_s16(vld1_s16(dataPtr)); - float32x4_t pos = vcvtq_n_s32_f32(data, 15); // This does the division by 32768.0f, effectively. + float32x4_t pos = vmulq_f32(scaleFactor, vcvtq_s32_f32(data)); // This does the division by 32768.0f, effectively. vst1q_f32(verts + i * 3, pos); } #else @@ -470,15 +471,15 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u // We test one vertex against 4 planes to get some SIMD. Vertices need to be transformed to world space // for testing, don't want to re-do that, so we have to use that "pivot" of the data. #ifdef _M_SSE + const __m128 worldX = _mm_loadu_ps(gstate.worldMatrix); + const __m128 worldY = _mm_loadu_ps(gstate.worldMatrix + 3); + const __m128 worldZ = _mm_loadu_ps(gstate.worldMatrix + 6); + const __m128 worldW = _mm_loadu_ps(gstate.worldMatrix + 9); + const __m128 planeX = _mm_loadu_ps(planes_.x); + const __m128 planeY = _mm_loadu_ps(planes_.y); + const __m128 planeZ = _mm_loadu_ps(planes_.z); + const __m128 planeW = _mm_loadu_ps(planes_.w); __m128 inside = _mm_set1_ps(0.0f); - __m128 worldX = _mm_loadu_ps(gstate.worldMatrix); - __m128 worldY = _mm_loadu_ps(gstate.worldMatrix + 3); - __m128 worldZ = _mm_loadu_ps(gstate.worldMatrix + 6); - __m128 worldW = _mm_loadu_ps(gstate.worldMatrix + 9); - __m128 planeX = _mm_loadu_ps(planes_.x); - __m128 planeY = _mm_loadu_ps(planes_.y); - __m128 planeZ = _mm_loadu_ps(planes_.z); - __m128 planeW = _mm_loadu_ps(planes_.w); for (int i = 0; i < vertexCount; i++) { const float *pos = verts + i * vertStride; __m128 worldpos = _mm_add_ps( @@ -510,6 +511,45 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u } u8 mask = _mm_movemask_ps(inside); return mask == 0xF; // 0xF means that we found at least one vertex inside every one of the planes. We don't bother with counts, though it wouldn't be hard. +#elif PPSSPP_ARCH(ARM_NEON) + const float32x4_t worldX = vld1q_f32(gstate.worldMatrix); + const float32x4_t worldY = vld1q_f32(gstate.worldMatrix + 3); + const float32x4_t worldZ = vld1q_f32(gstate.worldMatrix + 6); + const float32x4_t worldW = vld1q_f32(gstate.worldMatrix + 9); + const float32x4_t planeX = vld1q_f32(planes_.x); + const float32x4_t planeY = vld1q_f32(planes_.y); + const float32x4_t planeZ = vld1q_f32(planes_.z); + const float32x4_t planeW = vld1q_f32(planes_.w); + uint32x4_t inside = vdupq_n_u32(0); + for (int i = 0; i < vertexCount; i++) { + const float *pos = verts + i * vertStride; + float32x4_t objpos = vld1q_f32(pos); + float32x4_t worldpos = vaddq_f32( + vaddq_f32( + vmulq_laneq_f32(worldX, objpos, 0), + vmulq_laneq_f32(worldY, objpos, 1) + ), + vaddq_f32( + vmulq_laneq_f32(worldZ, objpos, 2), + worldW + ) + ); + // OK, now we check it against the four planes. + // This is really curiously similar to a matrix multiplication (well, it is one). + float32x4_t planeDist = vaddq_f32( + vaddq_f32( + vmulq_laneq_f32(planeX, worldpos, 0), + vmulq_laneq_f32(planeY, worldpos, 1) + ), + vaddq_f32( + vmulq_laneq_f32(planeZ, worldpos, 2), + planeW + ) + ); + inside = vorrq_u32(inside, vcgeq_f32(planeDist, vdupq_n_f32(0.0f))); + } + uint64_t insideBits = vget_lane_u64(vreinterpret_u64_u16(vmovn_u32(inside)), 0); + return ~insideBits == 0; // InsideBits all ones means that we found at least one vertex inside every one of the planes. We don't bother with counts, though it wouldn't be hard. #else int inside[4]{}; for (int i = 0; i < vertexCount; i++) { From 99548be8a32a41f134785493a511fd1c822b5c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 9 Dec 2023 13:26:02 +0100 Subject: [PATCH 08/10] NEON culling: Use mla operations to shave off some more cycles. ARM32 compat. --- CMakeLists.txt | 1 + Common/Math/CrossSIMD.h | 54 +++++++++++++++++++++++++ Core/Config.cpp | 2 +- GPU/Common/DrawEngineCommon.cpp | 40 +++++++++--------- UWP/CommonUWP/CommonUWP.vcxproj | 1 + UWP/CommonUWP/CommonUWP.vcxproj.filters | 3 ++ 6 files changed, 79 insertions(+), 22 deletions(-) create mode 100644 Common/Math/CrossSIMD.h diff --git a/CMakeLists.txt b/CMakeLists.txt index caaef33781..520ae2b0a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -737,6 +737,7 @@ add_library(Common STATIC Common/Input/InputState.cpp Common/Input/InputState.h Common/Math/fast/fast_matrix.c + Common/Math/CrossSIMD.h Common/Math/curves.cpp Common/Math/curves.h Common/Math/expression_parser.cpp diff --git a/Common/Math/CrossSIMD.h b/Common/Math/CrossSIMD.h new file mode 100644 index 0000000000..ce42d9dcd4 --- /dev/null +++ b/Common/Math/CrossSIMD.h @@ -0,0 +1,54 @@ +// CrossSIMD +// +// Compatibility wrappers for SIMD dialects. +// +// In the long run, might do a more general single-source-SIMD wrapper here consisting +// of defines that translate to either NEON or SSE. It would be possible to write quite a lot of +// our various color conversion functions and so on in a pretty generic manner. + +#include "ppsspp_config.h" + +#include + +#ifdef _M_SSE +#include +#endif + +#if PPSSPP_ARCH(ARM_NEON) +#if defined(_MSC_VER) && PPSSPP_ARCH(ARM64) +#include +#else +#include +#endif +#endif + +// Basic types + +#if PPSSPP_ARCH(ARM64_NEON) + +// No special ones here. + +#elif PPSSPP_ARCH(ARM_NEON) + +// Compatibility wrappers making ARM64 NEON code run on ARM32 +// With optimization on, these should compile down to the optimal code. + +inline float32x4_t vmulq_laneq_f32(float32x4_t a, float32x4_t b, int lane) { + switch (lane & 3) { + case 0: return vmulq_lane_f32(a, vget_low_f32(b), 0); + case 1: return vmulq_lane_f32(a, vget_low_f32(b), 1); + case 2: return vmulq_lane_f32(a, vget_high_f32(b), 0); + case 3: return vmulq_lane_f32(a, vget_high_f32(b), 1); + } +} + +inline float32x4_t vmlaq_laneq_f32(float32x4_t a, float32x4_t b, float32x4_t c, int lane) { + switch (lane & 3) { + case 0: return vmlaq_lane_f32(a, b, vget_low_f32(c), 0); + case 1: return vmlaq_lane_f32(a, b, vget_low_f32(c), 1); + case 2: return vmlaq_lane_f32(a, b, vget_high_f32(c), 0); + case 3: return vmlaq_lane_f32(a, b, vget_high_f32(c), 1); + } +} + +#endif diff --git a/Core/Config.cpp b/Core/Config.cpp index fc4a2b913a..eec83a23e8 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -1893,7 +1893,7 @@ void PlayTimeTracker::Load(const Section *section) { // Parse the string. PlayTime gameTime{}; - if (2 == sscanf(value.c_str(), "%d,%llu", &gameTime.totalTimePlayed, &gameTime.lastTimePlayed)) { + if (2 == sscanf(value.c_str(), "%d,%llu", &gameTime.totalTimePlayed, (long long *)&gameTime.lastTimePlayed)) { tracker_[key] = gameTime; } } diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index c551eb6fbf..52b48defb3 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -21,6 +21,7 @@ #include "Common/Data/Convert/ColorConv.h" #include "Common/Profiler/Profiler.h" #include "Common/LogReporting.h" +#include "Common/Math/CrossSIMD.h" #include "Common/Math/lin/matrix4x4.h" #include "Core/Config.h" #include "GPU/Common/DrawEngineCommon.h" @@ -442,12 +443,12 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u __m128 pos = _mm_mul_ps(_mm_cvtepi32_ps(bits), scaleFactor); _mm_storeu_ps(verts + i * 3, pos); // TODO: use stride 4 to avoid clashing writes? } -#elif 0 && PPSSPP_ARCH(ARM_NEON) - __m128 scaleFactor = vdupq_n_f32(1.0f / 32768.0f); +#elif PPSSPP_ARCH(ARM_NEON) + float32x4_t scaleFactor = vdupq_n_f32(1.0f / 32768.0f); for (int i = 0; i < vertexCount; i++) { - const int16_t *dataPtr = ((const int16_t *)((const s8 *)vdata + i * stride + offset)); + const s16 *dataPtr = ((const s16 *)((const s8 *)vdata + i * stride + offset)); int32x4_t data = vmovl_s16(vld1_s16(dataPtr)); - float32x4_t pos = vmulq_f32(scaleFactor, vcvtq_s32_f32(data)); // This does the division by 32768.0f, effectively. + float32x4_t pos = vmulq_f32(vcvtq_f32_s32(data), scaleFactor); vst1q_f32(verts + i * 3, pos); } #else @@ -470,7 +471,11 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u // We only check the 4 sides. Near/far won't likely make a huge difference. // We test one vertex against 4 planes to get some SIMD. Vertices need to be transformed to world space // for testing, don't want to re-do that, so we have to use that "pivot" of the data. +<<<<<<< HEAD #ifdef _M_SSE +======= +#if PPSSPP_ARCH(SSE2) +>>>>>>> c5a94c3799 (Buildfix again) const __m128 worldX = _mm_loadu_ps(gstate.worldMatrix); const __m128 worldY = _mm_loadu_ps(gstate.worldMatrix + 3); const __m128 worldZ = _mm_loadu_ps(gstate.worldMatrix + 6); @@ -509,8 +514,9 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u ); inside = _mm_or_ps(inside, _mm_cmpge_ps(planeDist, _mm_setzero_ps())); } - u8 mask = _mm_movemask_ps(inside); - return mask == 0xF; // 0xF means that we found at least one vertex inside every one of the planes. We don't bother with counts, though it wouldn't be hard. + // 0xF means that we found at least one vertex inside every one of the planes. + // We don't bother with counts, though it wouldn't be hard if we had a use for them. + return _mm_movemask_ps(inside) == 0xF; #elif PPSSPP_ARCH(ARM_NEON) const float32x4_t worldX = vld1q_f32(gstate.worldMatrix); const float32x4_t worldY = vld1q_f32(gstate.worldMatrix + 3); @@ -525,28 +531,20 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u const float *pos = verts + i * vertStride; float32x4_t objpos = vld1q_f32(pos); float32x4_t worldpos = vaddq_f32( - vaddq_f32( + vmlaq_laneq_f32( vmulq_laneq_f32(worldX, objpos, 0), - vmulq_laneq_f32(worldY, objpos, 1) - ), - vaddq_f32( - vmulq_laneq_f32(worldZ, objpos, 2), - worldW - ) + worldY, objpos, 1), + vmlaq_laneq_f32(worldW, worldZ, objpos, 2) ); // OK, now we check it against the four planes. // This is really curiously similar to a matrix multiplication (well, it is one). float32x4_t planeDist = vaddq_f32( - vaddq_f32( + vmlaq_laneq_f32( vmulq_laneq_f32(planeX, worldpos, 0), - vmulq_laneq_f32(planeY, worldpos, 1) - ), - vaddq_f32( - vmulq_laneq_f32(planeZ, worldpos, 2), - planeW - ) + planeY, worldpos, 1), + vmlaq_laneq_f32(planeW, planeZ, worldpos, 2) ); - inside = vorrq_u32(inside, vcgeq_f32(planeDist, vdupq_n_f32(0.0f))); + inside = vorrq_u32(inside, vcgezq_f32(planeDist)); } uint64_t insideBits = vget_lane_u64(vreinterpret_u64_u16(vmovn_u32(inside)), 0); return ~insideBits == 0; // InsideBits all ones means that we found at least one vertex inside every one of the planes. We don't bother with counts, though it wouldn't be hard. diff --git a/UWP/CommonUWP/CommonUWP.vcxproj b/UWP/CommonUWP/CommonUWP.vcxproj index b4d9a89375..b27cbdbb06 100644 --- a/UWP/CommonUWP/CommonUWP.vcxproj +++ b/UWP/CommonUWP/CommonUWP.vcxproj @@ -105,6 +105,7 @@ + diff --git a/UWP/CommonUWP/CommonUWP.vcxproj.filters b/UWP/CommonUWP/CommonUWP.vcxproj.filters index 2eedf9e8c6..262e1e7af3 100644 --- a/UWP/CommonUWP/CommonUWP.vcxproj.filters +++ b/UWP/CommonUWP/CommonUWP.vcxproj.filters @@ -862,6 +862,9 @@ ext\naett + + Math + From 4e2a1bf81c51e5f365bc22e36765bea100b7f245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 9 Dec 2023 16:40:37 +0100 Subject: [PATCH 09/10] NEON: vcvtq can scale directly, no need for a mul by const. --- Common/Math/CrossSIMD.h | 8 ++++++-- GPU/Common/DrawEngineCommon.cpp | 3 +-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Common/Math/CrossSIMD.h b/Common/Math/CrossSIMD.h index ce42d9dcd4..c2851b5775 100644 --- a/Common/Math/CrossSIMD.h +++ b/Common/Math/CrossSIMD.h @@ -38,7 +38,7 @@ inline float32x4_t vmulq_laneq_f32(float32x4_t a, float32x4_t b, int lane) { case 0: return vmulq_lane_f32(a, vget_low_f32(b), 0); case 1: return vmulq_lane_f32(a, vget_low_f32(b), 1); case 2: return vmulq_lane_f32(a, vget_high_f32(b), 0); - case 3: return vmulq_lane_f32(a, vget_high_f32(b), 1); + default: return vmulq_lane_f32(a, vget_high_f32(b), 1); } } @@ -47,8 +47,12 @@ inline float32x4_t vmlaq_laneq_f32(float32x4_t a, float32x4_t b, float32x4_t c, case 0: return vmlaq_lane_f32(a, b, vget_low_f32(c), 0); case 1: return vmlaq_lane_f32(a, b, vget_low_f32(c), 1); case 2: return vmlaq_lane_f32(a, b, vget_high_f32(c), 0); - case 3: return vmlaq_lane_f32(a, b, vget_high_f32(c), 1); + default: return vmlaq_lane_f32(a, b, vget_high_f32(c), 1); } } +inline uint32x4_t vcgezq_f32(float32x4_t v) { + return vcgeq_f32(v, vdupq_n_f32(0.0f)); +} + #endif diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 52b48defb3..71ead60a02 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -444,11 +444,10 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u _mm_storeu_ps(verts + i * 3, pos); // TODO: use stride 4 to avoid clashing writes? } #elif PPSSPP_ARCH(ARM_NEON) - float32x4_t scaleFactor = vdupq_n_f32(1.0f / 32768.0f); for (int i = 0; i < vertexCount; i++) { const s16 *dataPtr = ((const s16 *)((const s8 *)vdata + i * stride + offset)); int32x4_t data = vmovl_s16(vld1_s16(dataPtr)); - float32x4_t pos = vmulq_f32(vcvtq_f32_s32(data), scaleFactor); + float32x4_t pos = vcvtq_n_f32_s32(data, 15); // >> 15 = division by 32768.0f vst1q_f32(verts + i * 3, pos); } #else From 7e85d3d10a83b3bd767f30f1bd974c90e086dce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 9 Dec 2023 14:50:14 +0100 Subject: [PATCH 10/10] Disable the new culling on RISC-V for now. --- Common/Math/CrossSIMD.h | 2 +- GPU/Common/DrawEngineCommon.cpp | 15 +++++---------- GPU/GPUCommonHW.cpp | 9 +++++++++ ppsspp_config.h | 2 ++ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Common/Math/CrossSIMD.h b/Common/Math/CrossSIMD.h index c2851b5775..3eb8e0e75e 100644 --- a/Common/Math/CrossSIMD.h +++ b/Common/Math/CrossSIMD.h @@ -10,7 +10,7 @@ #include -#ifdef _M_SSE +#if PPSSPP_ARCH(SSE2) #include #endif diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 71ead60a02..96a81b36b9 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -433,12 +433,12 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u break; case GE_VTYPE_POS_16BIT: { -#if defined(_M_SSE) +#if PPSSPP_ARCH(SSE2) __m128 scaleFactor = _mm_set1_ps(1.0f / 32768.0f); for (int i = 0; i < vertexCount; i++) { const s16 *data = ((const s16 *)((const s8 *)vdata + i * stride + offset)); __m128i bits = _mm_castpd_si128(_mm_load_sd((const double *)data)); - // Sign extension. Ugly without SSE4. + // Sign extension. Hacky without SSE4. bits = _mm_srai_epi32(_mm_unpacklo_epi16(bits, bits), 16); __m128 pos = _mm_mul_ps(_mm_cvtepi32_ps(bits), scaleFactor); _mm_storeu_ps(verts + i * 3, pos); // TODO: use stride 4 to avoid clashing writes? @@ -470,11 +470,7 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u // We only check the 4 sides. Near/far won't likely make a huge difference. // We test one vertex against 4 planes to get some SIMD. Vertices need to be transformed to world space // for testing, don't want to re-do that, so we have to use that "pivot" of the data. -<<<<<<< HEAD -#ifdef _M_SSE -======= #if PPSSPP_ARCH(SSE2) ->>>>>>> c5a94c3799 (Buildfix again) const __m128 worldX = _mm_loadu_ps(gstate.worldMatrix); const __m128 worldY = _mm_loadu_ps(gstate.worldMatrix + 3); const __m128 worldZ = _mm_loadu_ps(gstate.worldMatrix + 6); @@ -498,9 +494,9 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u ); // OK, now we check it against the four planes. // This is really curiously similar to a matrix multiplication (well, it is one). - __m128 posX = _mm_shuffle_ps(worldpos, worldpos, 0); - __m128 posY = _mm_shuffle_ps(worldpos, worldpos, 1 | (1 << 2) | (1 << 4) | (1 << 6)); - __m128 posZ = _mm_shuffle_ps(worldpos, worldpos, 2 | (2 << 2) | (2 << 4) | (2 << 6)); + __m128 posX = _mm_shuffle_ps(worldpos, worldpos, _MM_SHUFFLE(0, 0, 0, 0)); + __m128 posY = _mm_shuffle_ps(worldpos, worldpos, _MM_SHUFFLE(1, 1, 1, 1)); + __m128 posZ = _mm_shuffle_ps(worldpos, worldpos, _MM_SHUFFLE(2, 2, 2, 2)); __m128 planeDist = _mm_add_ps( _mm_add_ps( _mm_mul_ps(planeX, posX), @@ -566,7 +562,6 @@ bool DrawEngineCommon::TestBoundingBoxFast(const void *vdata, int vertexCount, u } } #endif - return true; } diff --git a/GPU/GPUCommonHW.cpp b/GPU/GPUCommonHW.cpp index 8af997b67b..66906f4b1b 100644 --- a/GPU/GPUCommonHW.cpp +++ b/GPU/GPUCommonHW.cpp @@ -992,8 +992,17 @@ void GPUCommonHW::Execute_Prim(u32 op, u32 diff) { #define MAX_CULL_CHECK_COUNT 6 +// For now, turn off culling on platforms where we don't have SIMD bounding box tests, like RISC-V. +#if PPSSPP_ARCH(ARM_NEON) || PPSSPP_ARCH(SSE2) + #define PASSES_CULLING ((vertexType & (GE_VTYPE_THROUGH_MASK | GE_VTYPE_MORPHCOUNT_MASK | GE_VTYPE_WEIGHT_MASK | GE_VTYPE_IDX_MASK)) || count > MAX_CULL_CHECK_COUNT) +#else + +#define PASSES_CULLING true + +#endif + // If certain conditions are true, do frustum culling. bool passCulling = PASSES_CULLING; if (!passCulling) { diff --git a/ppsspp_config.h b/ppsspp_config.h index 2861b621b3..71e7b9c946 100644 --- a/ppsspp_config.h +++ b/ppsspp_config.h @@ -11,6 +11,7 @@ #if defined(_M_IX86) || defined(__i386__) || defined (__EMSCRIPTEN__) #define PPSSPP_ARCH_X86 1 #define PPSSPP_ARCH_32BIT 1 + #define PPSSPP_ARCH_SSE2 1 //TODO: Remove this compat define #ifndef _M_IX86 #define _M_IX86 600 @@ -19,6 +20,7 @@ #if (defined(_M_X64) || defined(__amd64__) || defined(__x86_64__)) && !defined(__EMSCRIPTEN__) #define PPSSPP_ARCH_AMD64 1 + #define PPSSPP_ARCH_SSE2 1 #if defined(__ILP32__) #define PPSSPP_ARCH_32BIT 1 #else