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