2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2016-09-21 22:53:13 +00:00
|
|
|
#include "VideoBackends/Software/DebugUtil.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
2020-12-16 14:34:50 +00:00
|
|
|
#include <memory>
|
2016-09-21 22:53:13 +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"
|
2020-12-16 14:34:50 +00:00
|
|
|
#include "Common/Image.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "Common/Swap.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "VideoBackends/Software/EfbInterface.h"
|
|
|
|
#include "VideoBackends/Software/TextureSampler.h"
|
|
|
|
|
2015-10-09 18:50:36 +00:00
|
|
|
#include "VideoCommon/BPMemory.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
2019-07-17 00:18:48 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2015-10-09 18:50:36 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +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
|
|
|
|
2009-10-28 03:01:07 +00:00
|
|
|
void Init()
|
|
|
|
{
|
2014-08-11 00:51:10 +00:00
|
|
|
for (int i = 0; i < NUM_OBJECT_BUFFERS; i++)
|
2009-10-28 03:01:07 +00:00
|
|
|
{
|
2014-03-05 14:19:10 +00:00
|
|
|
ObjectBuffer[i] = new u32[EFB_WIDTH * EFB_HEIGHT]();
|
2009-10-28 03:01:07 +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;
|
2009-10-28 03:01:07 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-03-05 14:19:10 +00:00
|
|
|
void Shutdown()
|
|
|
|
{
|
2019-02-01 18:47:07 +00:00
|
|
|
for (auto& slot : ObjectBuffer)
|
2014-03-05 14:19:10 +00:00
|
|
|
{
|
2019-02-01 18:47:07 +00:00
|
|
|
delete[] slot;
|
2014-03-05 14:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static void SaveTexture(const std::string& filename, u32 texmap, s32 mip)
|
2009-10-12 00:48:24 +00:00
|
|
|
{
|
2021-10-09 19:16:15 +00:00
|
|
|
u32 width = bpmem.tex.GetUnit(texmap).texImage0.width + 1;
|
|
|
|
u32 height = bpmem.tex.GetUnit(texmap).texImage0.height + 1;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2020-12-16 14:34:50 +00:00
|
|
|
auto data = std::make_unique<u8[]>(width * height * 4);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2020-12-16 14:34:50 +00:00
|
|
|
GetTextureRGBA(data.get(), texmap, mip, width, height);
|
|
|
|
Common::SavePNG(filename, data.get(), Common::ImageByteFormat::RGBA, width, height, width * 4);
|
2009-10-12 00:48:24 +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)
|
2009-10-12 00:48:24 +00:00
|
|
|
{
|
2013-01-14 21:29:52 +00:00
|
|
|
for (u32 y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
for (u32 x = 0; x < width; x++)
|
|
|
|
{
|
2013-11-16 02:59:59 +00:00
|
|
|
TextureSampler::SampleMip(x << 7, y << 7, mip, false, texmap, dst);
|
|
|
|
dst += 4;
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2013-01-14 21:29:52 +00:00
|
|
|
}
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static s32 GetMaxTextureLod(u32 texmap)
|
2010-03-09 04:38:07 +00:00
|
|
|
{
|
2021-10-09 19:16:15 +00:00
|
|
|
u8 maxLod = bpmem.tex.GetUnit(texmap).texMode1.max_lod;
|
2010-03-09 04:38:07 +00:00
|
|
|
u8 mip = maxLod >> 4;
|
|
|
|
u8 fract = maxLod & 0xf;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
if (fract)
|
2010-03-09 04:38:07 +00:00
|
|
|
++mip;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
return (s32)mip;
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
void DumpActiveTextures()
|
|
|
|
{
|
|
|
|
for (unsigned int stageNum = 0; stageNum < bpmem.genMode.numindstages; stageNum++)
|
|
|
|
{
|
|
|
|
u32 texmap = bpmem.tevindref.getTexMap(stageNum);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
s32 maxLod = GetMaxTextureLod(texmap);
|
2010-04-14 03:27:45 +00:00
|
|
|
for (s32 mip = 0; mip <= maxLod; ++mip)
|
2010-03-09 04:38:07 +00:00
|
|
|
{
|
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(),
|
2019-07-11 03:34:50 +00:00
|
|
|
g_stats.this_frame.num_drawn_objects, stageNum, texmap, mip),
|
2015-10-09 18:50:36 +00:00
|
|
|
texmap, mip);
|
2010-03-09 04:38:07 +00:00
|
|
|
}
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +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];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
int texmap = order.getTexMap(stageOdd);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
s32 maxLod = GetMaxTextureLod(texmap);
|
2010-04-14 03:27:45 +00:00
|
|
|
for (s32 mip = 0; mip <= maxLod; ++mip)
|
2010-03-09 04:38:07 +00:00
|
|
|
{
|
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(),
|
2019-07-11 03:34:50 +00:00
|
|
|
g_stats.this_frame.num_drawn_objects, stageNum, texmap, mip),
|
2015-10-09 18:50:36 +00:00
|
|
|
texmap, mip);
|
2010-03-09 04:38:07 +00:00
|
|
|
}
|
2009-10-12 00:48:24 +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)
|
2009-10-12 00:48:24 +00:00
|
|
|
{
|
2020-12-16 14:34:50 +00:00
|
|
|
auto data = std::make_unique<u8[]>(EFB_WIDTH * EFB_HEIGHT * 4);
|
|
|
|
u8* writePtr = data.get();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-03-23 06:09:41 +00:00
|
|
|
for (u32 y = 0; y < EFB_HEIGHT; y++)
|
2009-10-12 00:48:24 +00:00
|
|
|
{
|
2020-03-23 06:09:41 +00:00
|
|
|
for (u32 x = 0; x < EFB_WIDTH; x++)
|
2013-04-14 03:54:02 +00:00
|
|
|
{
|
2013-11-16 02:59:59 +00:00
|
|
|
// ABGR to RGBA
|
2016-09-21 22:53:13 +00:00
|
|
|
const u32 sample = Common::swap32(EfbInterface::GetColor(x, y));
|
|
|
|
|
|
|
|
std::memcpy(writePtr, &sample, sizeof(u32));
|
|
|
|
writePtr += sizeof(u32);
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-12-16 14:34:50 +00:00
|
|
|
Common::SavePNG(filename, data.get(), Common::ImageByteFormat::RGBA, EFB_WIDTH, EFB_HEIGHT,
|
|
|
|
EFB_WIDTH * 4);
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2016-09-23 01:05:17 +00:00
|
|
|
void DrawObjectBuffer(s16 x, s16 y, const u8* color, int bufferBase, int subBuffer,
|
|
|
|
const char* name)
|
2009-10-28 03:01:07 +00:00
|
|
|
{
|
2010-12-02 05:38:48 +00:00
|
|
|
int buffer = bufferBase + subBuffer;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-12-02 05:38:48 +00:00
|
|
|
u32 offset = (x + y * EFB_WIDTH) * 4;
|
|
|
|
u8* dst = (u8*)&ObjectBuffer[buffer][offset];
|
2009-10-28 03:01:07 +00:00
|
|
|
*(dst++) = color[2];
|
|
|
|
*(dst++) = color[1];
|
|
|
|
*(dst++) = color[0];
|
|
|
|
*(dst++) = color[3];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-28 03:01:07 +00:00
|
|
|
DrawnToBuffer[buffer] = true;
|
|
|
|
ObjectBufferName[buffer] = name;
|
2010-12-02 05:38:48 +00:00
|
|
|
BufferBase[buffer] = bufferBase;
|
|
|
|
}
|
|
|
|
|
2016-09-23 01:05:17 +00:00
|
|
|
void DrawTempBuffer(const u8* color, int buffer)
|
2010-12-02 05:38:48 +00:00
|
|
|
{
|
|
|
|
u8* dst = (u8*)&TempBuffer[buffer];
|
|
|
|
*(dst++) = color[2];
|
|
|
|
*(dst++) = color[1];
|
|
|
|
*(dst++) = color[0];
|
|
|
|
*(dst++) = color[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
ObjectBufferName[buffer] = name;
|
|
|
|
BufferBase[buffer] = bufferBase;
|
2009-10-28 03:01:07 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
void OnObjectBegin()
|
|
|
|
{
|
2019-07-11 03:11:14 +00:00
|
|
|
if (g_ActiveConfig.bDumpTextures &&
|
2019-07-11 03:34:50 +00:00
|
|
|
g_stats.this_frame.num_drawn_objects >= g_ActiveConfig.drawStart &&
|
|
|
|
g_stats.this_frame.num_drawn_objects < g_ActiveConfig.drawEnd)
|
2019-07-11 03:11:14 +00:00
|
|
|
{
|
2016-10-08 02:55:47 +00:00
|
|
|
DumpActiveTextures();
|
2019-07-11 03:11:14 +00:00
|
|
|
}
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
void OnObjectEnd()
|
|
|
|
{
|
2019-07-11 03:11:14 +00:00
|
|
|
if (g_ActiveConfig.bDumpObjects &&
|
2019-07-11 03:34:50 +00:00
|
|
|
g_stats.this_frame.num_drawn_objects >= g_ActiveConfig.drawStart &&
|
|
|
|
g_stats.this_frame.num_drawn_objects < g_ActiveConfig.drawEnd)
|
2019-07-11 03:11:14 +00:00
|
|
|
{
|
2018-06-06 12:42:41 +00:00
|
|
|
DumpEfb(StringFromFormat("%sobject%i.png", File::GetUserPath(D_DUMPOBJECTS_IDX).c_str(),
|
2019-07-11 03:34:50 +00:00
|
|
|
g_stats.this_frame.num_drawn_objects));
|
2019-07-11 03:11:14 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-10-08 02:55:47 +00:00
|
|
|
for (int i = 0; i < NUM_OBJECT_BUFFERS; i++)
|
|
|
|
{
|
|
|
|
if (DrawnToBuffer[i])
|
2009-10-28 03:01:07 +00:00
|
|
|
{
|
2016-10-08 02:55:47 +00:00
|
|
|
DrawnToBuffer[i] = false;
|
2019-07-11 03:11:14 +00:00
|
|
|
std::string filename = StringFromFormat(
|
|
|
|
"%sobject%i_%s(%i).png", File::GetUserPath(D_DUMPOBJECTS_IDX).c_str(),
|
2019-07-11 03:34:50 +00:00
|
|
|
g_stats.this_frame.num_drawn_objects, ObjectBufferName[i], i - BufferBase[i]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-12-16 14:34:50 +00:00
|
|
|
Common::SavePNG(filename, reinterpret_cast<u8*>(ObjectBuffer[i]),
|
|
|
|
Common::ImageByteFormat::RGBA, EFB_WIDTH, EFB_HEIGHT, EFB_WIDTH * 4);
|
2016-10-08 02:55:47 +00:00
|
|
|
memset(ObjectBuffer[i], 0, EFB_WIDTH * EFB_HEIGHT * sizeof(u32));
|
|
|
|
}
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2016-10-08 02:55:47 +00:00
|
|
|
|
2019-07-11 03:34:50 +00:00
|
|
|
g_stats.this_frame.num_drawn_objects++;
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace DebugUtil
|