DebugUtil.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Copyright 2009 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include "Common/CommonTypes.h"
  5. #include "Common/FileUtil.h"
  6. #include "Common/StringUtil.h"
  7. #include "Core/ConfigManager.h"
  8. #include "VideoBackends/Software/BPMemLoader.h"
  9. #include "VideoBackends/Software/DebugUtil.h"
  10. #include "VideoBackends/Software/EfbInterface.h"
  11. #include "VideoBackends/Software/HwRasterizer.h"
  12. #include "VideoBackends/Software/SWCommandProcessor.h"
  13. #include "VideoBackends/Software/SWRenderer.h"
  14. #include "VideoBackends/Software/SWStatistics.h"
  15. #include "VideoBackends/Software/SWVideoConfig.h"
  16. #include "VideoBackends/Software/TextureSampler.h"
  17. #include "VideoCommon/Fifo.h"
  18. #include "VideoCommon/ImageWrite.h"
  19. namespace DebugUtil
  20. {
  21. static bool drawingHwTriangles = false;
  22. static const int NUM_OBJECT_BUFFERS = 40;
  23. static u32 *ObjectBuffer[NUM_OBJECT_BUFFERS];
  24. static u32 TempBuffer[NUM_OBJECT_BUFFERS];
  25. static bool DrawnToBuffer[NUM_OBJECT_BUFFERS];
  26. static const char* ObjectBufferName[NUM_OBJECT_BUFFERS];
  27. static int BufferBase[NUM_OBJECT_BUFFERS];
  28. void Init()
  29. {
  30. for (int i = 0; i < NUM_OBJECT_BUFFERS; i++)
  31. {
  32. ObjectBuffer[i] = new u32[EFB_WIDTH*EFB_HEIGHT]();
  33. DrawnToBuffer[i] = false;
  34. ObjectBufferName[i] = nullptr;
  35. BufferBase[i] = 0;
  36. }
  37. }
  38. void Shutdown()
  39. {
  40. for (int i = 0; i < NUM_OBJECT_BUFFERS; i++)
  41. {
  42. delete[] ObjectBuffer[i];
  43. }
  44. }
  45. static void SaveTexture(const std::string& filename, u32 texmap, s32 mip)
  46. {
  47. FourTexUnits& texUnit = bpmem.tex[(texmap >> 2) & 1];
  48. u8 subTexmap = texmap & 3;
  49. TexImage0& ti0 = texUnit.texImage0[subTexmap];
  50. u32 width = ti0.width + 1;
  51. u32 height = ti0.height + 1;
  52. u8 *data = new u8[width * height * 4];
  53. GetTextureRGBA(data, texmap, mip, width, height);
  54. TextureToPng(data, width*4, filename, width, height, true);
  55. delete[] data;
  56. }
  57. void GetTextureRGBA(u8 *dst, u32 texmap, s32 mip, u32 width, u32 height)
  58. {
  59. for (u32 y = 0; y < height; y++)
  60. {
  61. for (u32 x = 0; x < width; x++)
  62. {
  63. TextureSampler::SampleMip(x << 7, y << 7, mip, false, texmap, dst);
  64. dst += 4;
  65. }
  66. }
  67. }
  68. static s32 GetMaxTextureLod(u32 texmap)
  69. {
  70. FourTexUnits& texUnit = bpmem.tex[(texmap >> 2) & 1];
  71. u8 subTexmap = texmap & 3;
  72. u8 maxLod = texUnit.texMode1[subTexmap].max_lod;
  73. u8 mip = maxLod >> 4;
  74. u8 fract = maxLod & 0xf;
  75. if (fract)
  76. ++mip;
  77. return (s32)mip;
  78. }
  79. void DumpActiveTextures()
  80. {
  81. for (unsigned int stageNum = 0; stageNum < bpmem.genMode.numindstages; stageNum++)
  82. {
  83. u32 texmap = bpmem.tevindref.getTexMap(stageNum);
  84. s32 maxLod = GetMaxTextureLod(texmap);
  85. for (s32 mip = 0; mip <= maxLod; ++mip)
  86. {
  87. SaveTexture(StringFromFormat("%star%i_ind%i_map%i_mip%i.png",
  88. File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(),
  89. swstats.thisFrame.numDrawnObjects, stageNum, texmap, mip), texmap, mip);
  90. }
  91. }
  92. for (unsigned int stageNum = 0; stageNum <= bpmem.genMode.numtevstages; stageNum++)
  93. {
  94. int stageNum2 = stageNum >> 1;
  95. int stageOdd = stageNum&1;
  96. TwoTevStageOrders &order = bpmem.tevorders[stageNum2];
  97. int texmap = order.getTexMap(stageOdd);
  98. s32 maxLod = GetMaxTextureLod(texmap);
  99. for (s32 mip = 0; mip <= maxLod; ++mip)
  100. {
  101. SaveTexture(StringFromFormat("%star%i_stage%i_map%i_mip%i.png",
  102. File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(),
  103. swstats.thisFrame.numDrawnObjects, stageNum, texmap, mip), texmap, mip);
  104. }
  105. }
  106. }
  107. static void DumpEfb(const std::string& filename)
  108. {
  109. u8 *data = new u8[EFB_WIDTH * EFB_HEIGHT * 4];
  110. u8 *writePtr = data;
  111. u8 sample[4];
  112. for (int y = 0; y < EFB_HEIGHT; y++)
  113. {
  114. for (int x = 0; x < EFB_WIDTH; x++)
  115. {
  116. EfbInterface::GetColor(x, y, sample);
  117. // ABGR to RGBA
  118. *(writePtr++) = sample[3];
  119. *(writePtr++) = sample[2];
  120. *(writePtr++) = sample[1];
  121. *(writePtr++) = sample[0];
  122. }
  123. }
  124. TextureToPng(data, EFB_WIDTH * 4, filename, EFB_WIDTH, EFB_HEIGHT, true);
  125. delete[] data;
  126. }
  127. static void DumpColorTexture(const std::string& filename, u32 width, u32 height)
  128. {
  129. TextureToPng(SWRenderer::GetCurrentColorTexture(), width * 4, filename, width, height, true);
  130. }
  131. void DrawObjectBuffer(s16 x, s16 y, u8 *color, int bufferBase, int subBuffer, const char *name)
  132. {
  133. int buffer = bufferBase + subBuffer;
  134. u32 offset = (x + y * EFB_WIDTH) * 4;
  135. u8 *dst = (u8*)&ObjectBuffer[buffer][offset];
  136. *(dst++) = color[2];
  137. *(dst++) = color[1];
  138. *(dst++) = color[0];
  139. *(dst++) = color[3];
  140. DrawnToBuffer[buffer] = true;
  141. ObjectBufferName[buffer] = name;
  142. BufferBase[buffer] = bufferBase;
  143. }
  144. void DrawTempBuffer(u8 *color, int buffer)
  145. {
  146. u8 *dst = (u8*)&TempBuffer[buffer];
  147. *(dst++) = color[2];
  148. *(dst++) = color[1];
  149. *(dst++) = color[0];
  150. *(dst++) = color[3];
  151. }
  152. void CopyTempBuffer(s16 x, s16 y, int bufferBase, int subBuffer, const char *name)
  153. {
  154. int buffer = bufferBase + subBuffer;
  155. u32 offset = (x + y * EFB_WIDTH);
  156. ObjectBuffer[buffer][offset] = TempBuffer[buffer];
  157. DrawnToBuffer[buffer] = true;
  158. ObjectBufferName[buffer] = name;
  159. BufferBase[buffer] = bufferBase;
  160. }
  161. void OnObjectBegin()
  162. {
  163. if (!g_bSkipCurrentFrame)
  164. {
  165. if (g_SWVideoConfig.bDumpTextures && swstats.thisFrame.numDrawnObjects >= g_SWVideoConfig.drawStart && swstats.thisFrame.numDrawnObjects < g_SWVideoConfig.drawEnd)
  166. DumpActiveTextures();
  167. if (g_SWVideoConfig.bHwRasterizer)
  168. {
  169. HwRasterizer::BeginTriangles();
  170. drawingHwTriangles = true;
  171. }
  172. }
  173. }
  174. void OnObjectEnd()
  175. {
  176. if (!g_bSkipCurrentFrame)
  177. {
  178. if (g_SWVideoConfig.bDumpObjects && swstats.thisFrame.numDrawnObjects >= g_SWVideoConfig.drawStart && swstats.thisFrame.numDrawnObjects < g_SWVideoConfig.drawEnd)
  179. DumpEfb(StringFromFormat("%sobject%i.png",
  180. File::GetUserPath(D_DUMPFRAMES_IDX).c_str(),
  181. swstats.thisFrame.numDrawnObjects));
  182. if (g_SWVideoConfig.bHwRasterizer || drawingHwTriangles)
  183. {
  184. HwRasterizer::EndTriangles();
  185. drawingHwTriangles = false;
  186. }
  187. for (int i = 0; i < NUM_OBJECT_BUFFERS; i++)
  188. {
  189. if (DrawnToBuffer[i])
  190. {
  191. DrawnToBuffer[i] = false;
  192. std::string filename = StringFromFormat("%sobject%i_%s(%i).png",
  193. File::GetUserPath(D_DUMPFRAMES_IDX).c_str(),
  194. swstats.thisFrame.numDrawnObjects, ObjectBufferName[i], i - BufferBase[i]);
  195. TextureToPng((u8*)ObjectBuffer[i], EFB_WIDTH * 4, filename, EFB_WIDTH, EFB_HEIGHT, true);
  196. memset(ObjectBuffer[i], 0, EFB_WIDTH * EFB_HEIGHT * sizeof(u32));
  197. }
  198. }
  199. swstats.thisFrame.numDrawnObjects++;
  200. }
  201. }
  202. // If frame dumping is enabled, dump whatever is drawn to the screen.
  203. void OnFrameEnd(u32 width, u32 height)
  204. {
  205. if (!g_bSkipCurrentFrame)
  206. {
  207. if (SConfig::GetInstance().m_DumpFrames)
  208. {
  209. DumpColorTexture(StringFromFormat("%sframe%i_color.png",
  210. File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), swstats.frameCount), width, height);
  211. }
  212. }
  213. }
  214. }