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
|
|
|
|
|
|
|
#include "Common.h"
|
|
|
|
|
|
|
|
#include "DebugUtil.h"
|
|
|
|
#include "BPMemLoader.h"
|
|
|
|
#include "TextureSampler.h"
|
2011-01-29 06:26:03 +00:00
|
|
|
#include "SWVideoConfig.h"
|
2010-06-09 01:37:08 +00:00
|
|
|
#include "EfbInterface.h"
|
2011-02-03 19:55:30 +00:00
|
|
|
#include "SWStatistics.h"
|
2010-06-09 01:37:08 +00:00
|
|
|
#include "HwRasterizer.h"
|
|
|
|
#include "StringUtil.h"
|
2011-02-03 19:55:30 +00:00
|
|
|
#include "SWCommandProcessor.h"
|
2011-03-16 22:48:17 +00:00
|
|
|
#include "ImageWrite.h"
|
2010-06-09 01:37:08 +00:00
|
|
|
#include "FileUtil.h"
|
|
|
|
|
|
|
|
namespace DebugUtil
|
|
|
|
{
|
|
|
|
|
|
|
|
u32 skipFrames = 0;
|
2010-12-02 05:38:48 +00:00
|
|
|
bool drawingHwTriangles = false;
|
|
|
|
|
|
|
|
enum { NumObjectBuffers = 40};
|
|
|
|
|
|
|
|
u32 ObjectBuffer[NumObjectBuffers][EFB_WIDTH*EFB_HEIGHT];
|
|
|
|
u32 TempBuffer[NumObjectBuffers];
|
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
bool DrawnToBuffer[NumObjectBuffers];
|
|
|
|
const char* ObjectBufferName[NumObjectBuffers];
|
2010-12-02 05:38:48 +00:00
|
|
|
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++)
|
|
|
|
{
|
|
|
|
memset(ObjectBuffer[i], 0, sizeof(ObjectBuffer[i]));
|
|
|
|
DrawnToBuffer[i] = false;
|
|
|
|
ObjectBufferName[i] = 0;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void SaveTexture(const char* filename, u32 texmap, s32 mip)
|
|
|
|
{
|
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-04-14 03:54:02 +00:00
|
|
|
GetTextureBGRA(data, texmap, mip, width, height);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
(void)SaveTGA(filename, width, height, data);
|
|
|
|
|
|
|
|
delete []data;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 21:29:52 +00:00
|
|
|
void GetTextureBGRA(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
|
|
|
u8 sample[4];
|
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-04-14 03:54:02 +00:00
|
|
|
TextureSampler::SampleMip(x << 7, y << 7, mip, false, texmap, sample);
|
|
|
|
|
2013-01-14 21:29:52 +00:00
|
|
|
// RGBA to BGRA
|
2013-04-14 03:54:02 +00:00
|
|
|
*(dst++) = sample[2];
|
|
|
|
*(dst++) = sample[1];
|
|
|
|
*(dst++) = sample[0];
|
|
|
|
*(dst++) = sample[3];
|
|
|
|
}
|
2013-01-14 21:29:52 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s32 GetMaxTextureLod(u32 texmap)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
if(fract)
|
|
|
|
++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)
|
|
|
|
{
|
2011-02-28 20:40:15 +00:00
|
|
|
SaveTexture(StringFromFormat("%star%i_ind%i_map%i_mip%i.tga",
|
|
|
|
File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(),
|
|
|
|
swstats.thisFrame.numDrawnObjects, stageNum, texmap, mip).c_str(), 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)
|
|
|
|
{
|
2011-02-28 20:40:15 +00:00
|
|
|
SaveTexture(StringFromFormat("%star%i_stage%i_map%i_mip%i.tga",
|
|
|
|
File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(),
|
|
|
|
swstats.thisFrame.numDrawnObjects, stageNum, texmap, mip).c_str(), 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
|
|
|
}
|
|
|
|
|
|
|
|
void DumpEfb(const char* filename)
|
|
|
|
{
|
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);
|
|
|
|
// ABGR to BGRA
|
|
|
|
*(writePtr++) = sample[1];
|
|
|
|
*(writePtr++) = sample[2];
|
|
|
|
*(writePtr++) = sample[3];
|
|
|
|
*(writePtr++) = sample[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)SaveTGA(filename, EFB_WIDTH, EFB_HEIGHT, data);
|
|
|
|
|
|
|
|
delete []data;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DumpDepth(const char* filename)
|
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
u8 *data = new u8[EFB_WIDTH * EFB_HEIGHT * 4];
|
|
|
|
u8 *writePtr = data;
|
|
|
|
|
|
|
|
for (int y = 0; y < EFB_HEIGHT; y++)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < EFB_WIDTH; x++)
|
|
|
|
{
|
|
|
|
u32 depth = EfbInterface::GetDepth(x, y);
|
|
|
|
// depth to bgra
|
|
|
|
*(writePtr++) = (depth >> 16) & 0xff;
|
|
|
|
*(writePtr++) = (depth >> 8) & 0xff;
|
|
|
|
*(writePtr++) = depth & 0xff;
|
|
|
|
*(writePtr++) = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)SaveTGA(filename, EFB_WIDTH, EFB_HEIGHT, data);
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
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)
|
2011-02-28 20:40:15 +00:00
|
|
|
DumpEfb(StringFromFormat("%sobject%i.tga",
|
|
|
|
File::GetUserPath(D_DUMPFRAMES_IDX).c_str(),
|
|
|
|
swstats.thisFrame.numDrawnObjects).c_str());
|
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;
|
2011-02-28 20:40:15 +00:00
|
|
|
(void)SaveTGA(StringFromFormat("%sobject%i_%s(%i).tga",
|
|
|
|
File::GetUserPath(D_DUMPFRAMES_IDX).c_str(),
|
|
|
|
swstats.thisFrame.numDrawnObjects, ObjectBufferName[i], i - BufferBase[i]).c_str(),
|
|
|
|
EFB_WIDTH, EFB_HEIGHT, ObjectBuffer[i]);
|
|
|
|
memset(ObjectBuffer[i], 0, sizeof(ObjectBuffer[i]));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void OnFrameEnd()
|
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
if (!g_bSkipCurrentFrame)
|
|
|
|
{
|
|
|
|
if (g_SWVideoConfig.bDumpFrames)
|
|
|
|
{
|
2011-02-28 20:40:15 +00:00
|
|
|
DumpEfb(StringFromFormat("%sframe%i_color.tga",
|
|
|
|
File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), swstats.frameCount).c_str());
|
|
|
|
DumpDepth(StringFromFormat("%sframe%i_depth.tga",
|
|
|
|
File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), swstats.frameCount).c_str());
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|