2013-04-18 03:29:41 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/Common.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
|
|
|
#include "VideoBackends/Software/BPMemLoader.h"
|
|
|
|
#include "VideoBackends/Software/DebugUtil.h"
|
|
|
|
#include "VideoBackends/Software/EfbInterface.h"
|
|
|
|
#include "VideoBackends/Software/HwRasterizer.h"
|
|
|
|
#include "VideoBackends/Software/SWCommandProcessor.h"
|
2014-07-14 11:51:55 +00:00
|
|
|
#include "VideoBackends/Software/SWRenderer.h"
|
2014-02-19 11:14:09 +00:00
|
|
|
#include "VideoBackends/Software/SWStatistics.h"
|
|
|
|
#include "VideoBackends/Software/SWVideoConfig.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/Software/TextureSampler.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"
|
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
namespace DebugUtil
|
|
|
|
{
|
|
|
|
|
2014-03-05 14:19:10 +00:00
|
|
|
static bool drawingHwTriangles = false;
|
2010-12-02 05:38:48 +00:00
|
|
|
|
|
|
|
enum { NumObjectBuffers = 40};
|
|
|
|
|
2014-03-05 14:19:10 +00:00
|
|
|
static u32 *ObjectBuffer[NumObjectBuffers];
|
|
|
|
static u32 TempBuffer[NumObjectBuffers];
|
2010-12-02 05:38:48 +00:00
|
|
|
|
2014-03-05 14:19:10 +00:00
|
|
|
static bool DrawnToBuffer[NumObjectBuffers];
|
|
|
|
static const char* ObjectBufferName[NumObjectBuffers];
|
|
|
|
static int BufferBase[NumObjectBuffers];
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
for (int i = 0; i < NumObjectBuffers; i++)
|
|
|
|
{
|
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()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < NumObjectBuffers; i++)
|
|
|
|
{
|
|
|
|
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(),
|
2013-11-16 22:25:12 +00:00
|
|
|
swstats.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(),
|
2013-11-16 22:25:12 +00:00
|
|
|
swstats.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
|
|
|
}
|
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
|
|
|
|
|
2014-07-14 11:51:55 +00:00
|
|
|
static void DumpColorTexture(const std::string& filename, u32 width, u32 height)
|
|
|
|
{
|
|
|
|
TextureToPng(SWRenderer::getCurrentColorTexture(), width * 4, filename, width, height, true);
|
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()
|
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
if (!g_bSkipCurrentFrame)
|
|
|
|
{
|
|
|
|
if (g_SWVideoConfig.bDumpTextures && swstats.thisFrame.numDrawnObjects >= g_SWVideoConfig.drawStart && swstats.thisFrame.numDrawnObjects < g_SWVideoConfig.drawEnd)
|
|
|
|
DumpActiveTextures();
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
if (g_SWVideoConfig.bHwRasterizer)
|
2010-12-02 05:38:48 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
HwRasterizer::BeginTriangles();
|
2010-12-02 05:38:48 +00:00
|
|
|
drawingHwTriangles = true;
|
|
|
|
}
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnObjectEnd()
|
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
if (!g_bSkipCurrentFrame)
|
|
|
|
{
|
|
|
|
if (g_SWVideoConfig.bDumpObjects && swstats.thisFrame.numDrawnObjects >= g_SWVideoConfig.drawStart && swstats.thisFrame.numDrawnObjects < g_SWVideoConfig.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(),
|
2013-11-16 22:25:12 +00:00
|
|
|
swstats.thisFrame.numDrawnObjects));
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
if (g_SWVideoConfig.bHwRasterizer || drawingHwTriangles)
|
2010-12-02 05:38:48 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
HwRasterizer::EndTriangles();
|
2010-12-02 05:38:48 +00:00
|
|
|
drawingHwTriangles = false;
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
for (int i = 0; i < NumObjectBuffers; i++)
|
|
|
|
{
|
|
|
|
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(),
|
|
|
|
swstats.thisFrame.numDrawnObjects, ObjectBufferName[i], i - BufferBase[i]);
|
|
|
|
|
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
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
swstats.thisFrame.numDrawnObjects++;
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2014-07-14 11:51:55 +00:00
|
|
|
// If frame dumping is enabled, dump whatever is drawn to the screen.
|
|
|
|
void OnFrameEnd(u32 width, u32 height)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
if (!g_bSkipCurrentFrame)
|
|
|
|
{
|
|
|
|
if (g_SWVideoConfig.bDumpFrames)
|
|
|
|
{
|
2014-07-14 11:51:55 +00:00
|
|
|
DumpColorTexture(StringFromFormat("%sframe%i_color.png",
|
|
|
|
File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), swstats.frameCount), width, height);
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|