From 35ba01e01f053ff3ac1e49d5a96dd98fc2b61c36 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 17 Sep 2022 18:42:59 -0700 Subject: [PATCH] softgpu: Refactor imm draws to bypass vert read. --- GPU/Common/DrawEngineCommon.cpp | 37 ++++++++ GPU/Common/DrawEngineCommon.h | 5 +- GPU/GPUCommon.cpp | 36 +------ GPU/Software/TransformUnit.cpp | 161 ++++++++++++++++++++++---------- GPU/Software/TransformUnit.h | 3 +- 5 files changed, 152 insertions(+), 90 deletions(-) diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 06bf6513ff..31707fa76d 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -19,6 +19,7 @@ #include "Common/Data/Convert/ColorConv.h" #include "Common/Profiler/Profiler.h" +#include "Common/LogReporting.h" #include "Core/Config.h" #include "GPU/Common/DrawEngineCommon.h" #include "GPU/Common/SplineCommon.h" @@ -188,6 +189,42 @@ u32 DrawEngineCommon::NormalizeVertices(u8 *outPtr, u8 *bufPtr, const u8 *inPtr, return DrawEngineCommon::NormalizeVertices(outPtr, bufPtr, inPtr, dec, lowerBound, upperBound, vertType); } +void DrawEngineCommon::DispatchSubmitImm(GEPrimitiveType prim, TransformedVertex *buffer, int vertexCount, int cullMode) { + // Instead of plumbing through properly (we'd need to inject these pretransformed vertices in the middle + // of SoftwareTransform(), which would take a lot of refactoring), we'll cheat and just turn these into + // through vertices. + // Since the only known use is Thrillville and it only uses it to clear, we just use color and pos. + struct ImmVertex { + float uv[2]; + uint32_t color; + float xyz[3]; + }; + std::vector temp; + temp.resize(vertexCount); + uint32_t color1Used = 0; + for (int i = 0; i < vertexCount; i++) { + // Since we're sending through, scale back up to w/h. + temp[i].uv[0] = buffer[i].u * gstate.getTextureWidth(0); + temp[i].uv[1] = buffer[i].v * gstate.getTextureHeight(0); + temp[i].color = buffer[i].color0_32; + temp[i].xyz[0] = buffer[i].pos[0]; + temp[i].xyz[1] = buffer[i].pos[1]; + temp[i].xyz[2] = buffer[i].pos[2]; + color1Used |= buffer[i].color1_32; + } + int vtype = GE_VTYPE_TC_FLOAT | GE_VTYPE_POS_FLOAT | GE_VTYPE_COL_8888 | GE_VTYPE_THROUGH; + // TODO: Handle fog and secondary color somehow? + + if (color1Used != 0 && gstate.isUsingSecondaryColor()) { + WARN_LOG_REPORT_ONCE(geimmcolor1, G3D, "Imm vertex used secondary color"); + } + + int bytesRead; + uint32_t vertTypeID = GetVertTypeID(vtype, 0); + SubmitPrim(&temp[0], nullptr, prim, vertexCount, vertTypeID, cullMode, &bytesRead); + DispatchFlush(); +} + // This code has plenty of potential for optimization. // // It does the simplest and safest test possible: If all points of a bbox is outside a single of diff --git a/GPU/Common/DrawEngineCommon.h b/GPU/Common/DrawEngineCommon.h index e6ba0b37d9..49c5e73200 100644 --- a/GPU/Common/DrawEngineCommon.h +++ b/GPU/Common/DrawEngineCommon.h @@ -90,10 +90,7 @@ public: SubmitPrim(verts, inds, prim, vertexCount, vertTypeID, cullMode, bytesRead); } - virtual void DispatchSubmitImm(const void *verts, const void *inds, GEPrimitiveType prim, int vertexCount, u32 vertTypeID, int cullMode, int *bytesRead) { - SubmitPrim(verts, inds, prim, vertexCount, vertTypeID, cullMode, bytesRead); - DispatchFlush(); - } + virtual void DispatchSubmitImm(GEPrimitiveType prim, TransformedVertex *buffer, int vertexCount, int cullMode); bool TestBoundingBox(const void* control_points, int vertexCount, u32 vertType, int *bytesRead); diff --git a/GPU/GPUCommon.cpp b/GPU/GPUCommon.cpp index 107398b7c8..bda1f7e51a 100644 --- a/GPU/GPUCommon.cpp +++ b/GPU/GPUCommon.cpp @@ -2416,8 +2416,7 @@ void GPUCommon::Execute_ImmVertexAlphaPrim(u32 op, u32 diff) { immFlags_ = op & 0x00FFF800; } else if (prim == GE_PRIM_KEEP_PREVIOUS && immPrim_ != GE_PRIM_INVALID) { static constexpr int flushPrimCount[] = { 1, 2, 0, 3, 0, 0, 2, 0 }; - // Instead of finding a proper point to flush, we just emit a full rectangle every time one - // is finished. + // Instead of finding a proper point to flush, we just emit prims when we can. if (immCount_ == flushPrimCount[immPrim_ & 7]) FlushImm(); } else { @@ -2439,31 +2438,6 @@ void GPUCommon::FlushImm() { } UpdateUVScaleOffset(); - // Instead of plumbing through properly (we'd need to inject these pretransformed vertices in the middle - // of SoftwareTransform(), which would take a lot of refactoring), we'll cheat and just turn these into - // through vertices. - // Since the only known use is Thrillville and it only uses it to clear, we just use color and pos. - struct ImmVertex { - float uv[2]; - uint32_t color; - float xyz[3]; - }; - ImmVertex temp[MAX_IMMBUFFER_SIZE]; - uint32_t color1Used = 0; - for (int i = 0; i < immCount_; i++) { - // Since we're sending through, scale back up to w/h. - temp[i].uv[0] = immBuffer_[i].u * gstate.getTextureWidth(0); - temp[i].uv[1] = immBuffer_[i].v * gstate.getTextureHeight(0); - temp[i].color = immBuffer_[i].color0_32; - temp[i].xyz[0] = immBuffer_[i].pos[0]; - temp[i].xyz[1] = immBuffer_[i].pos[1]; - temp[i].xyz[2] = immBuffer_[i].pos[2]; - color1Used |= immBuffer_[i].color1_32; - } - int vtype = GE_VTYPE_TC_FLOAT | GE_VTYPE_POS_FLOAT | GE_VTYPE_COL_8888 | GE_VTYPE_THROUGH; - - // TODO: Handle fog and secondary color somehow? - bool antialias = (immFlags_ & GE_IMM_ANTIALIAS) != 0; bool prevAntialias = gstate.isAntiAliasEnabled(); bool shading = (immFlags_ & GE_IMM_SHADING) != 0; @@ -2480,8 +2454,6 @@ void GPUCommon::FlushImm() { WARN_LOG_REPORT_ONCE(geimmclipvalue, G3D, "Imm vertex used clip value, flags=%06x", immFlags_); } else if ((immFlags_ & GE_IMM_FOG) != 0) { WARN_LOG_REPORT_ONCE(geimmfog, G3D, "Imm vertex used fog, flags=%06x", immFlags_); - } else if (color1Used != 0 && gstate.isUsingSecondaryColor()) { - WARN_LOG_REPORT_ONCE(geimmcolor1, G3D, "Imm vertex used secondary color, flags=%06x", immFlags_); } if (texturing != prevTexturing || cullEnable != prevCullEnable || dither != prevDither || prevShading != shading) { @@ -2494,11 +2466,7 @@ void GPUCommon::FlushImm() { gstate_c.Dirty(DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE | DIRTY_RASTER_STATE); } - int bytesRead; - uint32_t vertTypeID = GetVertTypeID(vtype, 0); - drawEngineCommon_->DispatchSubmitImm(temp, nullptr, immPrim_, immCount_, vertTypeID, cullMode, &bytesRead); - // TODO: In the future, make a special path for these. - // drawEngineCommon_->DispatchSubmitImm(immBuffer_, immCount_); + drawEngineCommon_->DispatchSubmitImm(immPrim_, immBuffer_, immCount_, cullMode); immCount_ = 0; gstate.antiAliasEnable = (GE_CMD_ANTIALIASENABLE << 24) | (int)prevAntialias; diff --git a/GPU/Software/TransformUnit.cpp b/GPU/Software/TransformUnit.cpp index 81ff27c4c1..c052a472cd 100644 --- a/GPU/Software/TransformUnit.cpp +++ b/GPU/Software/TransformUnit.cpp @@ -70,12 +70,45 @@ void SoftwareDrawEngine::DispatchSubmitPrim(const void *verts, const void *inds, transformUnit.SubmitPrimitive(verts, inds, prim, vertexCount, vertTypeID, bytesRead, this); } -void SoftwareDrawEngine::DispatchSubmitImm(const void *verts, const void *inds, GEPrimitiveType prim, int vertexCount, u32 vertTypeID, int cullMode, int *bytesRead) { +void SoftwareDrawEngine::DispatchSubmitImm(GEPrimitiveType prim, TransformedVertex *buffer, int vertexCount, int cullMode) { + // TODO: Handle fog and secondary color somehow? + int vtype = GE_VTYPE_TC_FLOAT | GE_VTYPE_POS_FLOAT | GE_VTYPE_COL_8888 | GE_VTYPE_THROUGH; + uint32_t vertTypeID = GetVertTypeID(vtype, 0); + int flipCull = cullMode != gstate.getCullMode() ? 1 : 0; // TODO: For now, just setting all dirty. transformUnit.SetDirty(SoftDirty(-1)); gstate.cullmode ^= flipCull; - transformUnit.SubmitPrimitive(verts, inds, prim, vertexCount, vertTypeID, bytesRead, this); + + // Instead of plumbing through properly (we'd need to inject these pretransformed vertices in the middle + // of SoftwareTransform(), which would take a lot of refactoring), we'll cheat and just turn these into + // through vertices. + // Since the only known use is Thrillville and it only uses it to clear, we just use color and pos. + struct ImmVertex { + float uv[2]; + uint32_t color; + float xyz[3]; + }; + std::vector temp; + temp.resize(vertexCount); + uint32_t color1Used = 0; + for (int i = 0; i < vertexCount; i++) { + // Since we're sending through, scale back up to w/h. + temp[i].uv[0] = buffer[i].u * gstate.getTextureWidth(0); + temp[i].uv[1] = buffer[i].v * gstate.getTextureHeight(0); + temp[i].color = buffer[i].color0_32; + temp[i].xyz[0] = buffer[i].pos[0]; + temp[i].xyz[1] = buffer[i].pos[1]; + temp[i].xyz[2] = buffer[i].pos[2]; + color1Used |= buffer[i].color1_32; + } + + if (color1Used != 0 && gstate.isUsingSecondaryColor()) { + WARN_LOG_REPORT_ONCE(geimmcolor1, G3D, "Imm vertex used secondary color"); + } + + transformUnit.SubmitPrimitive(&temp[0], nullptr, prim, vertexCount, vertTypeID, nullptr, this); + gstate.cullmode ^= flipCull; // TODO: Should really clear, but the vertex type is faked so things might need resetting... transformUnit.SetDirty(SoftDirty(-1)); @@ -475,15 +508,6 @@ void TransformUnit::SubmitPrimitive(const void* vertices, const void* indices, G prim_type = prev_prim_; } - int vtcs_per_prim; - switch (prim_type) { - case GE_PRIM_POINTS: vtcs_per_prim = 1; break; - case GE_PRIM_LINES: vtcs_per_prim = 2; break; - case GE_PRIM_TRIANGLES: vtcs_per_prim = 3; break; - case GE_PRIM_RECTANGLES: vtcs_per_prim = 2; break; - default: vtcs_per_prim = 0; break; - } - // TODO: Do this in two passes - first process the vertices (before indexing/stripping), // then resolve the indices. This lets us avoid transforming shared vertices twice. @@ -552,39 +576,50 @@ void TransformUnit::SubmitPrimitive(const void* vertices, const void* indices, G return; } + // Note: intentionally, these allow for the case of vertex_count == 0, but data_index_ > 0. + // This is used for immediate-mode primitives. switch (prim_type) { case GE_PRIM_POINTS: - case GE_PRIM_LINES: - case GE_PRIM_TRIANGLES: - { - for (int vtx = 0; vtx < vertex_count; ++vtx) { - data_[data_index_++] = readVertexAt(vreader, transformState, vtx); - if (data_index_ < vtcs_per_prim) { - // Keep reading. Note: an incomplete prim will stay read for GE_PRIM_KEEP_PREVIOUS. - continue; - } - // Okay, we've got enough verts. Reset the index for next time. - data_index_ = 0; - - switch (prim_type) { - case GE_PRIM_TRIANGLES: - SendTriangle(cullType, &data_[0]); - break; - - case GE_PRIM_LINES: - Clipper::ProcessLine(data_[0], data_[1], *binner_); - break; - - case GE_PRIM_POINTS: - Clipper::ProcessPoint(data_[0], *binner_); - break; - - default: - _dbg_assert_msg_(false, "Unexpected prim type: %d", prim_type); - } - } - break; + for (int i = 0; i < data_index_; ++i) + Clipper::ProcessPoint(data_[i], *binner_); + data_index_ = 0; + for (int vtx = 0; vtx < vertex_count; ++vtx) { + data_[0] = readVertexAt(vreader, transformState, vtx); + Clipper::ProcessPoint(data_[0], *binner_); } + break; + + case GE_PRIM_LINES: + for (int i = 0; i < data_index_ - 1; i += 2) + Clipper::ProcessLine(data_[i + 0], data_[i + 1], *binner_); + data_index_ &= 1; + for (int vtx = 0; vtx < vertex_count; ++vtx) { + data_[data_index_++] = readVertexAt(vreader, transformState, vtx); + if (data_index_ == 2) { + Clipper::ProcessLine(data_[0], data_[1], *binner_); + data_index_ = 0; + } + } + break; + + case GE_PRIM_TRIANGLES: + for (int vtx = 0; vtx < vertex_count; ++vtx) { + data_[data_index_++] = readVertexAt(vreader, transformState, vtx); + if (data_index_ < 3) { + // Keep reading. Note: an incomplete prim will stay read for GE_PRIM_KEEP_PREVIOUS. + continue; + } + // Okay, we've got enough verts. Reset the index for next time. + data_index_ = 0; + + SendTriangle(cullType, &data_[0]); + } + // In case vertex_count was 0. + if (data_index_ >= 3) { + SendTriangle(cullType, &data_[0]); + data_index_ = 0; + } + break; case GE_PRIM_RECTANGLES: for (int vtx = 0; vtx < vertex_count; ++vtx) { @@ -625,6 +660,9 @@ void TransformUnit::SubmitPrimitive(const void* vertices, const void* indices, G Clipper::ProcessLine(data_[data_index_ & 1], data_[(data_index_ & 1) ^ 1], *binner_); } } + // If this is from immediate-mode drawing, we always had one new vert (already in data_.) + if (isImmDraw_ && data_index_ >= 2) + Clipper::ProcessLine(data_[data_index_ & 1], data_[(data_index_ & 1) ^ 1], *binner_); break; } @@ -647,6 +685,7 @@ void TransformUnit::SubmitPrimitive(const void* vertices, const void* indices, G if (Rasterizer::DetectRectangleFromStrip(binner_->State(), data_, &tl, &br)) { Clipper::ProcessRect(data_[tl], data_[br], *binner_); start_vtx += 2; + skip_count = 0; if (base + 4 >= vertex_count) { start_vtx = vertex_count; break; @@ -663,15 +702,25 @@ void TransformUnit::SubmitPrimitive(const void* vertices, const void* indices, G } } + for (int vtx = start_vtx; vtx < vertex_count && skip_count > 0; ++vtx) { + int provoking_index = (data_index_++) % 3; + data_[provoking_index] = readVertexAt(vreader, transformState, vtx); + --skip_count; + ++start_vtx; + } + for (int vtx = start_vtx; vtx < vertex_count; ++vtx) { int provoking_index = (data_index_++) % 3; data_[provoking_index] = readVertexAt(vreader, transformState, vtx); - if (skip_count) { - --skip_count; - continue; - } + int wind = (data_index_ - 1) % 2; + CullType altCullType = cullType == CullType::OFF ? cullType : CullType((int)cullType ^ wind); + SendTriangle(altCullType, &data_[0], provoking_index); + } + // If this is from immediate-mode drawing, we always had one new vert (already in data_.) + if (isImmDraw_ && data_index_ >= 3) { + int provoking_index = (data_index_ - 1) % 3; int wind = (data_index_ - 1) % 2; CullType altCullType = cullType == CullType::OFF ? cullType : CullType((int)cullType ^ wind); SendTriangle(altCullType, &data_[0], provoking_index); @@ -687,7 +736,7 @@ void TransformUnit::SubmitPrimitive(const void* vertices, const void* indices, G int start_vtx = 0; // Only read the central vertex if we're not continuing. - if (data_index_ == 0) { + if (data_index_ == 0 && vertex_count > 0) { data_[0] = readVertexAt(vreader, transformState, 0); data_index_++; start_vtx = 1; @@ -705,19 +754,29 @@ void TransformUnit::SubmitPrimitive(const void* vertices, const void* indices, G } } + for (int vtx = start_vtx; vtx < vertex_count && skip_count > 0; ++vtx) { + int provoking_index = 2 - ((data_index_++) % 2); + data_[provoking_index] = readVertexAt(vreader, transformState, vtx); + --skip_count; + ++start_vtx; + } + for (int vtx = start_vtx; vtx < vertex_count; ++vtx) { int provoking_index = 2 - ((data_index_++) % 2); data_[provoking_index] = readVertexAt(vreader, transformState, vtx); - if (skip_count) { - --skip_count; - continue; - } - int wind = (data_index_ - 1) % 2; CullType altCullType = cullType == CullType::OFF ? cullType : CullType((int)cullType ^ wind); SendTriangle(altCullType, &data_[0], provoking_index); } + + // If this is from immediate-mode drawing, we always had one new vert (already in data_.) + if (isImmDraw_ && data_index_ >= 3) { + int wind = (data_index_ - 1) % 2; + int provoking_index = 2 - wind; + CullType altCullType = cullType == CullType::OFF ? cullType : CullType((int)cullType ^ wind); + SendTriangle(altCullType, &data_[0], provoking_index); + } break; } diff --git a/GPU/Software/TransformUnit.h b/GPU/Software/TransformUnit.h index dcfe2ecd0b..e19d69fff7 100644 --- a/GPU/Software/TransformUnit.h +++ b/GPU/Software/TransformUnit.h @@ -154,6 +154,7 @@ private: int data_index_ = 0; GEPrimitiveType prev_prim_ = GE_PRIM_POINTS; bool hasDraws_ = false; + bool isImmDraw_ = false; }; class SoftwareDrawEngine : public DrawEngineCommon { @@ -163,7 +164,7 @@ public: void DispatchFlush() override; void DispatchSubmitPrim(const void *verts, const void *inds, GEPrimitiveType prim, int vertexCount, u32 vertType, int cullMode, int *bytesRead) override; - void DispatchSubmitImm(const void *verts, const void *inds, GEPrimitiveType prim, int vertexCount, u32 vertTypeID, int cullMode, int *bytesRead) override; + void DispatchSubmitImm(GEPrimitiveType prim, TransformedVertex *buffer, int vertexCount, int cullMode) override; VertexDecoder *FindVertexDecoder(u32 vtype);