Merge pull request #8246 from lioncash/fifo

Core/FifoAnalyzer: Tidy up interface
This commit is contained in:
Léo Lam 2019-07-21 15:17:43 +08:00 committed by GitHub
commit cd7a374ab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 151 additions and 135 deletions

View File

@ -4,10 +4,10 @@
#include "Core/FifoPlayer/FifoAnalyzer.h" #include "Core/FifoPlayer/FifoAnalyzer.h"
#include <algorithm>
#include <numeric> #include <numeric>
#include "Common/Assert.h" #include "Common/Assert.h"
#include "Common/MsgHandler.h"
#include "Common/Swap.h" #include "Common/Swap.h"
#include "Core/FifoPlayer/FifoRecordAnalyzer.h" #include "Core/FifoPlayer/FifoRecordAnalyzer.h"
@ -20,30 +20,141 @@
namespace FifoAnalyzer namespace FifoAnalyzer
{ {
bool s_DrawingObject; namespace
FifoAnalyzer::CPMemory s_CpMem; {
u8 ReadFifo8(const u8*& data) u8 ReadFifo8(const u8*& data)
{ {
u8 value = data[0]; const u8 value = data[0];
data += 1; data += 1;
return value; return value;
} }
u16 ReadFifo16(const u8*& data) u16 ReadFifo16(const u8*& data)
{ {
u16 value = Common::swap16(data); const u16 value = Common::swap16(data);
data += 2; data += 2;
return value; return value;
} }
u32 ReadFifo32(const u8*& data) u32 ReadFifo32(const u8*& data)
{ {
u32 value = Common::swap32(data); const u32 value = Common::swap32(data);
data += 4; data += 4;
return value; return value;
} }
std::array<int, 21> CalculateVertexElementSizes(int vatIndex, const CPMemory& cpMem)
{
const TVtxDesc& vtxDesc = cpMem.vtxDesc;
const VAT& vtxAttr = cpMem.vtxAttr[vatIndex];
// Colors
const std::array<u64, 2> colDesc{
vtxDesc.Color0,
vtxDesc.Color1,
};
const std::array<u32, 2> colComp{
vtxAttr.g0.Color0Comp,
vtxAttr.g0.Color1Comp,
};
const std::array<u32, 8> 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<u32, 8> 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,
};
std::array<int, 21> sizes{};
// Add position and texture matrix indices
u64 vtxDescHex = cpMem.vtxDesc.Hex;
for (int i = 0; i < 9; ++i)
{
sizes[i] = vtxDescHex & 1;
vtxDescHex >>= 1;
}
// Position
sizes[9] = VertexLoader_Position::GetSize(vtxDesc.Position, vtxAttr.g0.PosFormat,
vtxAttr.g0.PosElements);
// Normals
if (vtxDesc.Normal != NOT_PRESENT)
{
sizes[10] = VertexLoader_Normal::GetSize(vtxDesc.Normal, vtxAttr.g0.NormalFormat,
vtxAttr.g0.NormalElements, vtxAttr.g0.NormalIndex3);
}
else
{
sizes[10] = 0;
}
// Colors
for (size_t i = 0; i < colDesc.size(); i++)
{
int size = 0;
switch (colDesc[i])
{
case NOT_PRESENT:
break;
case DIRECT:
switch (colComp[i])
{
case FORMAT_16B_565:
size = 2;
break;
case FORMAT_24B_888:
size = 3;
break;
case FORMAT_32B_888x:
size = 4;
break;
case FORMAT_16B_4444:
size = 2;
break;
case FORMAT_24B_6666:
size = 3;
break;
case FORMAT_32B_8888:
size = 4;
break;
default:
ASSERT(0);
break;
}
break;
case INDEX8:
size = 1;
break;
case INDEX16:
size = 2;
break;
}
sizes[11 + i] = size;
}
// Texture coordinates
vtxDescHex = vtxDesc.Hex >> 17;
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
bool s_DrawingObject;
FifoAnalyzer::CPMemory s_CpMem;
u32 AnalyzeCommand(const u8* data, DecodeMode mode) u32 AnalyzeCommand(const u8* data, DecodeMode mode)
{ {
const u8* dataStart = data; const u8* dataStart = data;
@ -88,7 +199,7 @@ u32 AnalyzeCommand(const u8* data, DecodeMode mode)
int array = 0xc + (cmd - OpcodeDecoder::GX_LOAD_INDX_A) / 8; int array = 0xc + (cmd - OpcodeDecoder::GX_LOAD_INDX_A) / 8;
u32 value = ReadFifo32(data); u32 value = ReadFifo32(data);
if (mode == DECODE_RECORD) if (mode == DecodeMode::Record)
FifoRecordAnalyzer::ProcessLoadIndexedXf(value, array); FifoRecordAnalyzer::ProcessLoadIndexedXf(value, array);
break; break;
} }
@ -113,27 +224,28 @@ u32 AnalyzeCommand(const u8* data, DecodeMode mode)
{ {
s_DrawingObject = true; s_DrawingObject = true;
int sizes[21]; const std::array<int, 21> sizes =
CalculateVertexElementSizes(sizes, cmd & OpcodeDecoder::GX_VAT_MASK, s_CpMem); CalculateVertexElementSizes(cmd & OpcodeDecoder::GX_VAT_MASK, s_CpMem);
// Determine offset of each element that might be a vertex array // 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. // The first 9 elements are never vertex arrays so we just accumulate their sizes.
int offsets[12]; int offset = std::accumulate(sizes.begin(), sizes.begin() + 9, 0u);
int offset = std::accumulate(&sizes[0], &sizes[9], 0u); std::array<int, 12> offsets;
for (int i = 0; i < 12; ++i) for (size_t i = 0; i < offsets.size(); ++i)
{ {
offsets[i] = offset; offsets[i] = offset;
offset += sizes[i + 9]; offset += sizes[i + 9];
} }
int vertexSize = offset; const int vertexSize = offset;
int numVertices = ReadFifo16(data); const int numVertices = ReadFifo16(data);
if (mode == DECODE_RECORD && numVertices > 0) 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<int>(i), data + offsets[i], vertexSize,
numVertices);
} }
} }
@ -188,101 +300,4 @@ void LoadCPReg(u32 subCmd, u32 value, CPMemory& cpMem)
break; break;
} }
} }
void CalculateVertexElementSizes(int sizes[], 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 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 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};
// Add position and texture matrix indices
u64 vtxDescHex = cpMem.vtxDesc.Hex;
for (int i = 0; i < 9; ++i)
{
sizes[i] = vtxDescHex & 1;
vtxDescHex >>= 1;
}
// Position
sizes[9] = VertexLoader_Position::GetSize(vtxDesc.Position, vtxAttr.g0.PosFormat,
vtxAttr.g0.PosElements);
// Normals
if (vtxDesc.Normal != NOT_PRESENT)
{
sizes[10] = VertexLoader_Normal::GetSize(vtxDesc.Normal, vtxAttr.g0.NormalFormat,
vtxAttr.g0.NormalElements, vtxAttr.g0.NormalIndex3);
}
else
{
sizes[10] = 0;
}
// Colors
for (int i = 0; i < 2; i++)
{
int size = 0;
switch (colDesc[i])
{
case NOT_PRESENT:
break;
case DIRECT:
switch (colComp[i])
{
case FORMAT_16B_565:
size = 2;
break;
case FORMAT_24B_888:
size = 3;
break;
case FORMAT_32B_888x:
size = 4;
break;
case FORMAT_16B_4444:
size = 2;
break;
case FORMAT_24B_6666:
size = 3;
break;
case FORMAT_32B_8888:
size = 4;
break;
default:
ASSERT(0);
break;
}
break;
case INDEX8:
size = 1;
break;
case INDEX16:
size = 2;
break;
}
sizes[11 + i] = size;
}
// Texture coordinates
vtxDescHex = vtxDesc.Hex >> 17;
for (int i = 0; i < 8; i++)
{
sizes[13 + i] = VertexLoader_TextCoord::GetSize(vtxDescHex & 3, tcFormat[i], tcElements[i]);
vtxDescHex >>= 2;
}
}
} // namespace FifoAnalyzer } // namespace FifoAnalyzer

View File

@ -4,20 +4,17 @@
#pragma once #pragma once
#include "Common/CommonTypes.h" #include <array>
#include "Common/CommonTypes.h"
#include "VideoCommon/CPMemory.h" #include "VideoCommon/CPMemory.h"
namespace FifoAnalyzer namespace FifoAnalyzer
{ {
u8 ReadFifo8(const u8*& data); enum class DecodeMode
u16 ReadFifo16(const u8*& data);
u32 ReadFifo32(const u8*& data);
enum DecodeMode
{ {
DECODE_RECORD, Record,
DECODE_PLAYBACK, Playback,
}; };
u32 AnalyzeCommand(const u8* data, DecodeMode mode); u32 AnalyzeCommand(const u8* data, DecodeMode mode);
@ -25,15 +22,13 @@ u32 AnalyzeCommand(const u8* data, DecodeMode mode);
struct CPMemory struct CPMemory
{ {
TVtxDesc vtxDesc; TVtxDesc vtxDesc;
VAT vtxAttr[8]; std::array<VAT, 8> vtxAttr;
u32 arrayBases[16]; std::array<u32, 16> arrayBases;
u32 arrayStrides[16]; std::array<u32, 16> arrayStrides;
}; };
void LoadCPReg(u32 subCmd, u32 value, CPMemory& cpMem); void LoadCPReg(u32 subCmd, u32 value, CPMemory& cpMem);
void CalculateVertexElementSizes(int sizes[], int vatIndex, const CPMemory& cpMem);
extern bool s_DrawingObject; extern bool s_DrawingObject;
extern FifoAnalyzer::CPMemory s_CpMem; extern FifoAnalyzer::CPMemory s_CpMem;
} // namespace FifoAnalyzer } // namespace FifoAnalyzer

View File

@ -63,9 +63,9 @@ void FifoPlaybackAnalyzer::AnalyzeFrames(FifoDataFile* file,
++nextMemUpdate; ++nextMemUpdate;
} }
bool wasDrawing = s_DrawingObject; const bool wasDrawing = s_DrawingObject;
const u32 cmdSize =
u32 cmdSize = FifoAnalyzer::AnalyzeCommand(&frame.fifoData[cmdStart], DECODE_PLAYBACK); FifoAnalyzer::AnalyzeCommand(&frame.fifoData[cmdStart], DecodeMode::Playback);
#if LOG_FIFO_CMDS #if LOG_FIFO_CMDS
CmdData cmdData; CmdData cmdData;

View File

@ -5,7 +5,6 @@
#include "Core/FifoPlayer/FifoRecordAnalyzer.h" #include "Core/FifoPlayer/FifoRecordAnalyzer.h"
#include <algorithm> #include <algorithm>
#include <cstring>
#include "Core/FifoPlayer/FifoAnalyzer.h" #include "Core/FifoPlayer/FifoAnalyzer.h"
#include "Core/FifoPlayer/FifoRecorder.h" #include "Core/FifoPlayer/FifoRecorder.h"
@ -22,8 +21,13 @@ void FifoRecordAnalyzer::Initialize(const u32* cpMem)
for (int i = 0; i < 8; ++i) for (int i = 0; i < 8; ++i)
FifoAnalyzer::LoadCPReg(0x70 + i, *(cpMem + 0x70 + i), s_CpMem); FifoAnalyzer::LoadCPReg(0x70 + i, *(cpMem + 0x70 + i), s_CpMem);
memcpy(s_CpMem.arrayBases, cpMem + 0xA0, 16 * 4); const u32* const bases_start = cpMem + 0xA0;
memcpy(s_CpMem.arrayStrides, cpMem + 0xB0, 16 * 4); 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) void FifoRecordAnalyzer::ProcessLoadIndexedXf(u32 val, int array)

View File

@ -77,12 +77,14 @@ void FifoRecorder::WriteGPCommand(const u8* data, u32 size)
{ {
// Assumes data contains all information for the command // Assumes data contains all information for the command
// Calls FifoRecorder::UseMemory // Calls FifoRecorder::UseMemory
u32 analyzed_size = FifoAnalyzer::AnalyzeCommand(data, FifoAnalyzer::DECODE_RECORD); const u32 analyzed_size = FifoAnalyzer::AnalyzeCommand(data, FifoAnalyzer::DecodeMode::Record);
// Make sure FifoPlayer's command analyzer agrees about the size of the command. // Make sure FifoPlayer's command analyzer agrees about the size of the command.
if (analyzed_size != size) if (analyzed_size != size)
{
PanicAlert("FifoRecorder: Expected command to be %i bytes long, we were given %i bytes", PanicAlert("FifoRecorder: Expected command to be %i bytes long, we were given %i bytes",
analyzed_size, size); analyzed_size, size);
}
// Copy data to buffer // Copy data to buffer
size_t currentSize = m_FifoData.size(); size_t currentSize = m_FifoData.size();