2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:29:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
2014-10-13 03:53:10 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/Software/DebugUtil.h"
|
|
|
|
#include "VideoBackends/Software/EfbInterface.h"
|
2014-07-14 11:51:55 +00:00
|
|
|
#include "VideoBackends/Software/SWRenderer.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/Software/TextureSampler.h"
|
|
|
|
|
2015-10-09 18:50:36 +00:00
|
|
|
#include "VideoCommon/BPMemory.h"
|
2014-07-08 14:49:33 +00:00
|
|
|
#include "VideoCommon/Fifo.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/ImageWrite.h"
|
2015-10-09 18:50:36 +00:00
|
|
|
#include "VideoCommon/Statistics.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
namespace DebugUtil
|
|
|
|
{
|
|
|
|
|
2014-08-11 00:51:10 +00:00
|
|
|
static const int NUM_OBJECT_BUFFERS = 40;
|
2010-12-02 05:38:48 +00:00
|
|
|
|
2014-08-11 00:51:10 +00:00
|
|
|
static u32 *ObjectBuffer[NUM_OBJECT_BUFFERS];
|
|
|
|
static u32 TempBuffer[NUM_OBJECT_BUFFERS];
|
2010-12-02 05:38:48 +00:00
|
|
|
|
2014-08-11 00:51:10 +00:00
|
|
|
static bool DrawnToBuffer[NUM_OBJECT_BUFFERS];
|
|
|
|
static const char* ObjectBufferName[NUM_OBJECT_BUFFERS];
|
|
|
|
static int BufferBase[NUM_OBJECT_BUFFERS];
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
2014-08-11 00:51:10 +00:00
|
|
|
for (int i = 0; i < NUM_OBJECT_BUFFERS; i++)
|
2013-04-14 03:54:02 +00:00
|
|
|
{
|
2014-03-05 14:19:10 +00:00
|
|
|
ObjectBuffer[i] = new u32[EFB_WIDTH*EFB_HEIGHT]();
|
2013-04-14 03:54:02 +00:00
|
|
|
DrawnToBuffer[i] = false;
|
2014-03-09 20:14:26 +00:00
|
|
|
ObjectBufferName[i] = nullptr;
|
2010-12-02 05:38:48 +00:00
|
|
|
BufferBase[i] = 0;
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 14:19:10 +00:00
|
|
|
void Shutdown()
|
|
|
|
{
|
2014-08-11 00:51:10 +00:00
|
|
|
for (int i = 0; i < NUM_OBJECT_BUFFERS; i++)
|
2014-03-05 14:19:10 +00:00
|
|
|
{
|
|
|
|
delete[] ObjectBuffer[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static void SaveTexture(const std::string& filename, u32 texmap, s32 mip)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
FourTexUnits& texUnit = bpmem.tex[(texmap >> 2) & 1];
|
|
|
|
u8 subTexmap = texmap & 3;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
TexImage0& ti0 = texUnit.texImage0[subTexmap];
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-01-14 21:29:52 +00:00
|
|
|
u32 width = ti0.width + 1;
|
|
|
|
u32 height = ti0.height + 1;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
u8 *data = new u8[width * height * 4];
|
|
|
|
|
2013-11-16 02:59:59 +00:00
|
|
|
GetTextureRGBA(data, texmap, mip, width, height);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-07-14 11:51:55 +00:00
|
|
|
TextureToPng(data, width*4, filename, width, height, true);
|
2013-11-16 21:34:34 +00:00
|
|
|
delete[] data;
|
2013-04-14 03:54:02 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-16 02:59:59 +00:00
|
|
|
void GetTextureRGBA(u8 *dst, u32 texmap, s32 mip, u32 width, u32 height)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
for (u32 y = 0; y < height; y++)
|
2013-01-14 21:29:52 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
for (u32 x = 0; x < width; x++)
|
2013-01-14 21:29:52 +00:00
|
|
|
{
|
2013-11-16 02:59:59 +00:00
|
|
|
TextureSampler::SampleMip(x << 7, y << 7, mip, false, texmap, dst);
|
|
|
|
dst += 4;
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2013-01-14 21:29:52 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static s32 GetMaxTextureLod(u32 texmap)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
|
|
|
FourTexUnits& texUnit = bpmem.tex[(texmap >> 2) & 1];
|
2013-04-14 03:54:02 +00:00
|
|
|
u8 subTexmap = texmap & 3;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
u8 maxLod = texUnit.texMode1[subTexmap].max_lod;
|
|
|
|
u8 mip = maxLod >> 4;
|
|
|
|
u8 fract = maxLod & 0xf;
|
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
if (fract)
|
2010-06-09 01:37:08 +00:00
|
|
|
++mip;
|
|
|
|
|
|
|
|
return (s32)mip;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DumpActiveTextures()
|
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
for (unsigned int stageNum = 0; stageNum < bpmem.genMode.numindstages; stageNum++)
|
|
|
|
{
|
|
|
|
u32 texmap = bpmem.tevindref.getTexMap(stageNum);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
s32 maxLod = GetMaxTextureLod(texmap);
|
|
|
|
for (s32 mip = 0; mip <= maxLod; ++mip)
|
|
|
|
{
|
2013-11-16 02:59:59 +00:00
|
|
|
SaveTexture(StringFromFormat("%star%i_ind%i_map%i_mip%i.png",
|
2013-11-16 22:28:11 +00:00
|
|
|
File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(),
|
2015-10-09 18:50:36 +00:00
|
|
|
stats.thisFrame.numDrawnObjects, stageNum, texmap, mip), texmap, mip);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
for (unsigned int stageNum = 0; stageNum <= bpmem.genMode.numtevstages; stageNum++)
|
|
|
|
{
|
|
|
|
int stageNum2 = stageNum >> 1;
|
|
|
|
int stageOdd = stageNum&1;
|
|
|
|
TwoTevStageOrders &order = bpmem.tevorders[stageNum2];
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
int texmap = order.getTexMap(stageOdd);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
s32 maxLod = GetMaxTextureLod(texmap);
|
2010-06-09 01:37:08 +00:00
|
|
|
for (s32 mip = 0; mip <= maxLod; ++mip)
|
|
|
|
{
|
2013-11-16 02:59:59 +00:00
|
|
|
SaveTexture(StringFromFormat("%star%i_stage%i_map%i_mip%i.png",
|
2013-11-16 22:28:11 +00:00
|
|
|
File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(),
|
2015-10-09 18:50:36 +00:00
|
|
|
stats.thisFrame.numDrawnObjects, stageNum, texmap, mip), texmap, mip);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static void DumpEfb(const std::string& filename)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
u8 *data = new u8[EFB_WIDTH * EFB_HEIGHT * 4];
|
|
|
|
u8 *writePtr = data;
|
|
|
|
u8 sample[4];
|
|
|
|
|
|
|
|
for (int y = 0; y < EFB_HEIGHT; y++)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < EFB_WIDTH; x++)
|
|
|
|
{
|
|
|
|
EfbInterface::GetColor(x, y, sample);
|
2013-11-16 02:59:59 +00:00
|
|
|
// ABGR to RGBA
|
2013-04-14 03:54:02 +00:00
|
|
|
*(writePtr++) = sample[3];
|
2013-11-16 02:59:59 +00:00
|
|
|
*(writePtr++) = sample[2];
|
|
|
|
*(writePtr++) = sample[1];
|
2013-04-14 03:54:02 +00:00
|
|
|
*(writePtr++) = sample[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 11:51:55 +00:00
|
|
|
TextureToPng(data, EFB_WIDTH * 4, filename, EFB_WIDTH, EFB_HEIGHT, true);
|
2013-11-16 21:34:34 +00:00
|
|
|
delete[] data;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 05:38:48 +00:00
|
|
|
void DrawObjectBuffer(s16 x, s16 y, u8 *color, int bufferBase, int subBuffer, const char *name)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
int buffer = bufferBase + subBuffer;
|
2010-12-02 05:38:48 +00:00
|
|
|
|
|
|
|
u32 offset = (x + y * EFB_WIDTH) * 4;
|
2013-04-14 03:54:02 +00:00
|
|
|
u8 *dst = (u8*)&ObjectBuffer[buffer][offset];
|
|
|
|
*(dst++) = color[2];
|
|
|
|
*(dst++) = color[1];
|
|
|
|
*(dst++) = color[0];
|
|
|
|
*(dst++) = color[3];
|
|
|
|
|
|
|
|
DrawnToBuffer[buffer] = true;
|
|
|
|
ObjectBufferName[buffer] = name;
|
2010-12-02 05:38:48 +00:00
|
|
|
BufferBase[buffer] = bufferBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTempBuffer(u8 *color, int buffer)
|
|
|
|
{
|
|
|
|
u8 *dst = (u8*)&TempBuffer[buffer];
|
2013-04-14 03:54:02 +00:00
|
|
|
*(dst++) = color[2];
|
|
|
|
*(dst++) = color[1];
|
|
|
|
*(dst++) = color[0];
|
|
|
|
*(dst++) = color[3];
|
2010-12-02 05:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CopyTempBuffer(s16 x, s16 y, int bufferBase, int subBuffer, const char *name)
|
|
|
|
{
|
|
|
|
int buffer = bufferBase + subBuffer;
|
|
|
|
|
|
|
|
u32 offset = (x + y * EFB_WIDTH);
|
|
|
|
ObjectBuffer[buffer][offset] = TempBuffer[buffer];
|
|
|
|
|
|
|
|
DrawnToBuffer[buffer] = true;
|
2013-04-14 03:54:02 +00:00
|
|
|
ObjectBufferName[buffer] = name;
|
2010-12-02 05:38:48 +00:00
|
|
|
BufferBase[buffer] = bufferBase;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnObjectBegin()
|
|
|
|
{
|
2016-01-24 04:20:13 +00:00
|
|
|
if (!Fifo::WillSkipCurrentFrame())
|
2013-04-14 03:54:02 +00:00
|
|
|
{
|
2015-10-09 18:50:36 +00:00
|
|
|
if (g_ActiveConfig.bDumpTextures && stats.thisFrame.numDrawnObjects >= g_ActiveConfig.drawStart && stats.thisFrame.numDrawnObjects < g_ActiveConfig.drawEnd)
|
2013-04-14 03:54:02 +00:00
|
|
|
DumpActiveTextures();
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnObjectEnd()
|
|
|
|
{
|
2016-01-24 04:20:13 +00:00
|
|
|
if (!Fifo::WillSkipCurrentFrame())
|
2013-04-14 03:54:02 +00:00
|
|
|
{
|
2015-10-09 18:50:36 +00:00
|
|
|
if (g_ActiveConfig.bDumpObjects && stats.thisFrame.numDrawnObjects >= g_ActiveConfig.drawStart && stats.thisFrame.numDrawnObjects < g_ActiveConfig.drawEnd)
|
2013-11-16 02:59:59 +00:00
|
|
|
DumpEfb(StringFromFormat("%sobject%i.png",
|
2011-02-28 20:40:15 +00:00
|
|
|
File::GetUserPath(D_DUMPFRAMES_IDX).c_str(),
|
2015-10-09 18:50:36 +00:00
|
|
|
stats.thisFrame.numDrawnObjects));
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-08-11 00:51:10 +00:00
|
|
|
for (int i = 0; i < NUM_OBJECT_BUFFERS; i++)
|
2013-04-14 03:54:02 +00:00
|
|
|
{
|
|
|
|
if (DrawnToBuffer[i])
|
|
|
|
{
|
|
|
|
DrawnToBuffer[i] = false;
|
2013-11-16 02:59:59 +00:00
|
|
|
std::string filename = StringFromFormat("%sobject%i_%s(%i).png",
|
|
|
|
File::GetUserPath(D_DUMPFRAMES_IDX).c_str(),
|
2015-10-09 18:50:36 +00:00
|
|
|
stats.thisFrame.numDrawnObjects, ObjectBufferName[i], i - BufferBase[i]);
|
2013-11-16 02:59:59 +00:00
|
|
|
|
2014-07-14 11:51:55 +00:00
|
|
|
TextureToPng((u8*)ObjectBuffer[i], EFB_WIDTH * 4, filename, EFB_WIDTH, EFB_HEIGHT, true);
|
2014-03-05 14:19:10 +00:00
|
|
|
memset(ObjectBuffer[i], 0, EFB_WIDTH * EFB_HEIGHT * sizeof(u32));
|
2013-11-16 21:34:34 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2015-10-09 18:50:36 +00:00
|
|
|
stats.thisFrame.numDrawnObjects++;
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|