From ac6d70b82b4a34a5d74f503ae67407239109f112 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 14 Jul 2019 20:54:47 -0400 Subject: [PATCH] Core/FifoAnalyzer: Use std::array where applicable This lets us convert CalculateVertexElementSizes() from a function using an out pointer into one that simply returns the array data as a return value. It also lets us dehardcode some values, as we can just query std::array's size() member function instead. --- Source/Core/Core/FifoPlayer/FifoAnalyzer.cpp | 58 +++++++++++-------- Source/Core/Core/FifoPlayer/FifoAnalyzer.h | 9 +-- .../Core/FifoPlayer/FifoRecordAnalyzer.cpp | 10 +++- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/Source/Core/Core/FifoPlayer/FifoAnalyzer.cpp b/Source/Core/Core/FifoPlayer/FifoAnalyzer.cpp index f2a369a84f..2f68b17153 100644 --- a/Source/Core/Core/FifoPlayer/FifoAnalyzer.cpp +++ b/Source/Core/Core/FifoPlayer/FifoAnalyzer.cpp @@ -4,10 +4,10 @@ #include "Core/FifoPlayer/FifoAnalyzer.h" -#include #include #include "Common/Assert.h" +#include "Common/MsgHandler.h" #include "Common/Swap.h" #include "Core/FifoPlayer/FifoRecordAnalyzer.h" @@ -43,24 +43,33 @@ u32 ReadFifo32(const u8*& data) return value; } -void CalculateVertexElementSizes(int sizes[], int vatIndex, const CPMemory& cpMem) +std::array CalculateVertexElementSizes(int vatIndex, const CPMemory& cpMem) { const TVtxDesc& vtxDesc = cpMem.vtxDesc; const VAT& vtxAttr = cpMem.vtxAttr[vatIndex]; // Colors - const u64 colDesc[2] = {vtxDesc.Color0, vtxDesc.Color1}; - const u32 colComp[2] = {vtxAttr.g0.Color0Comp, vtxAttr.g0.Color1Comp}; + const std::array colDesc{ + vtxDesc.Color0, + vtxDesc.Color1, + }; + const std::array colComp{ + vtxAttr.g0.Color0Comp, + vtxAttr.g0.Color1Comp, + }; - const u32 tcElements[8] = {vtxAttr.g0.Tex0CoordElements, vtxAttr.g1.Tex1CoordElements, - vtxAttr.g1.Tex2CoordElements, vtxAttr.g1.Tex3CoordElements, - vtxAttr.g1.Tex4CoordElements, vtxAttr.g2.Tex5CoordElements, - vtxAttr.g2.Tex6CoordElements, vtxAttr.g2.Tex7CoordElements}; + const std::array tcElements{ + vtxAttr.g0.Tex0CoordElements, vtxAttr.g1.Tex1CoordElements, vtxAttr.g1.Tex2CoordElements, + vtxAttr.g1.Tex3CoordElements, vtxAttr.g1.Tex4CoordElements, vtxAttr.g2.Tex5CoordElements, + vtxAttr.g2.Tex6CoordElements, vtxAttr.g2.Tex7CoordElements, + }; + const std::array tcFormat{ + vtxAttr.g0.Tex0CoordFormat, vtxAttr.g1.Tex1CoordFormat, vtxAttr.g1.Tex2CoordFormat, + vtxAttr.g1.Tex3CoordFormat, vtxAttr.g1.Tex4CoordFormat, vtxAttr.g2.Tex5CoordFormat, + vtxAttr.g2.Tex6CoordFormat, vtxAttr.g2.Tex7CoordFormat, + }; - const u32 tcFormat[8] = {vtxAttr.g0.Tex0CoordFormat, vtxAttr.g1.Tex1CoordFormat, - vtxAttr.g1.Tex2CoordFormat, vtxAttr.g1.Tex3CoordFormat, - vtxAttr.g1.Tex4CoordFormat, vtxAttr.g2.Tex5CoordFormat, - vtxAttr.g2.Tex6CoordFormat, vtxAttr.g2.Tex7CoordFormat}; + std::array sizes{}; // Add position and texture matrix indices u64 vtxDescHex = cpMem.vtxDesc.Hex; @@ -86,7 +95,7 @@ void CalculateVertexElementSizes(int sizes[], int vatIndex, const CPMemory& cpMe } // Colors - for (int i = 0; i < 2; i++) + for (size_t i = 0; i < colDesc.size(); i++) { int size = 0; @@ -133,11 +142,13 @@ void CalculateVertexElementSizes(int sizes[], int vatIndex, const CPMemory& cpMe // Texture coordinates vtxDescHex = vtxDesc.Hex >> 17; - for (int i = 0; i < 8; i++) + for (size_t i = 0; i < tcFormat.size(); i++) { sizes[13 + i] = VertexLoader_TextCoord::GetSize(vtxDescHex & 3, tcFormat[i], tcElements[i]); vtxDescHex >>= 2; } + + return sizes; } } // Anonymous namespace @@ -213,27 +224,28 @@ u32 AnalyzeCommand(const u8* data, DecodeMode mode) { s_DrawingObject = true; - int sizes[21]; - CalculateVertexElementSizes(sizes, cmd & OpcodeDecoder::GX_VAT_MASK, s_CpMem); + const std::array sizes = + CalculateVertexElementSizes(cmd & OpcodeDecoder::GX_VAT_MASK, s_CpMem); // Determine offset of each element that might be a vertex array // The first 9 elements are never vertex arrays so we just accumulate their sizes. - int offsets[12]; - int offset = std::accumulate(&sizes[0], &sizes[9], 0u); - for (int i = 0; i < 12; ++i) + int offset = std::accumulate(sizes.begin(), sizes.begin() + 9, 0u); + std::array offsets; + for (size_t i = 0; i < offsets.size(); ++i) { offsets[i] = offset; offset += sizes[i + 9]; } - int vertexSize = offset; - int numVertices = ReadFifo16(data); + const int vertexSize = offset; + const int numVertices = ReadFifo16(data); if (mode == DecodeMode::Record && numVertices > 0) { - for (int i = 0; i < 12; ++i) + for (size_t i = 0; i < offsets.size(); ++i) { - FifoRecordAnalyzer::WriteVertexArray(i, data + offsets[i], vertexSize, numVertices); + FifoRecordAnalyzer::WriteVertexArray(static_cast(i), data + offsets[i], vertexSize, + numVertices); } } diff --git a/Source/Core/Core/FifoPlayer/FifoAnalyzer.h b/Source/Core/Core/FifoPlayer/FifoAnalyzer.h index 52fbeb7179..5f1d883ba2 100644 --- a/Source/Core/Core/FifoPlayer/FifoAnalyzer.h +++ b/Source/Core/Core/FifoPlayer/FifoAnalyzer.h @@ -4,8 +4,9 @@ #pragma once -#include "Common/CommonTypes.h" +#include +#include "Common/CommonTypes.h" #include "VideoCommon/CPMemory.h" namespace FifoAnalyzer @@ -21,9 +22,9 @@ u32 AnalyzeCommand(const u8* data, DecodeMode mode); struct CPMemory { TVtxDesc vtxDesc; - VAT vtxAttr[8]; - u32 arrayBases[16]; - u32 arrayStrides[16]; + std::array vtxAttr; + std::array arrayBases; + std::array arrayStrides; }; void LoadCPReg(u32 subCmd, u32 value, CPMemory& cpMem); diff --git a/Source/Core/Core/FifoPlayer/FifoRecordAnalyzer.cpp b/Source/Core/Core/FifoPlayer/FifoRecordAnalyzer.cpp index d57637076e..284ecba620 100644 --- a/Source/Core/Core/FifoPlayer/FifoRecordAnalyzer.cpp +++ b/Source/Core/Core/FifoPlayer/FifoRecordAnalyzer.cpp @@ -5,7 +5,6 @@ #include "Core/FifoPlayer/FifoRecordAnalyzer.h" #include -#include #include "Core/FifoPlayer/FifoAnalyzer.h" #include "Core/FifoPlayer/FifoRecorder.h" @@ -22,8 +21,13 @@ void FifoRecordAnalyzer::Initialize(const u32* cpMem) for (int i = 0; i < 8; ++i) FifoAnalyzer::LoadCPReg(0x70 + i, *(cpMem + 0x70 + i), s_CpMem); - memcpy(s_CpMem.arrayBases, cpMem + 0xA0, 16 * 4); - memcpy(s_CpMem.arrayStrides, cpMem + 0xB0, 16 * 4); + const u32* const bases_start = cpMem + 0xA0; + const u32* const bases_end = bases_start + s_CpMem.arrayBases.size(); + std::copy(bases_start, bases_end, s_CpMem.arrayBases.begin()); + + const u32* const strides_start = cpMem + 0xB0; + const u32* const strides_end = strides_start + s_CpMem.arrayStrides.size(); + std::copy(strides_start, strides_end, s_CpMem.arrayStrides.begin()); } void FifoRecordAnalyzer::ProcessLoadIndexedXf(u32 val, int array)