debug_utils.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <condition_variable>
  6. #include <list>
  7. #include <map>
  8. #include <fstream>
  9. #include <mutex>
  10. #include <string>
  11. #ifdef HAVE_PNG
  12. #include <png.h>
  13. #endif
  14. #include "common/log.h"
  15. #include "common/file_util.h"
  16. #include "video_core/math.h"
  17. #include "video_core/pica.h"
  18. #include "debug_utils.h"
  19. namespace Pica {
  20. void DebugContext::OnEvent(Event event, void* data) {
  21. if (!breakpoints[event].enabled)
  22. return;
  23. {
  24. std::unique_lock<std::mutex> lock(breakpoint_mutex);
  25. // TODO: Should stop the CPU thread here once we multithread emulation.
  26. active_breakpoint = event;
  27. at_breakpoint = true;
  28. // Tell all observers that we hit a breakpoint
  29. for (auto& breakpoint_observer : breakpoint_observers) {
  30. breakpoint_observer->OnPicaBreakPointHit(event, data);
  31. }
  32. // Wait until another thread tells us to Resume()
  33. resume_from_breakpoint.wait(lock, [&]{ return !at_breakpoint; });
  34. }
  35. }
  36. void DebugContext::Resume() {
  37. {
  38. std::unique_lock<std::mutex> lock(breakpoint_mutex);
  39. // Tell all observers that we are about to resume
  40. for (auto& breakpoint_observer : breakpoint_observers) {
  41. breakpoint_observer->OnPicaResume();
  42. }
  43. // Resume the waiting thread (i.e. OnEvent())
  44. at_breakpoint = false;
  45. }
  46. resume_from_breakpoint.notify_one();
  47. }
  48. std::shared_ptr<DebugContext> g_debug_context; // TODO: Get rid of this global
  49. namespace DebugUtils {
  50. void GeometryDumper::AddTriangle(Vertex& v0, Vertex& v1, Vertex& v2) {
  51. vertices.push_back(v0);
  52. vertices.push_back(v1);
  53. vertices.push_back(v2);
  54. int num_vertices = vertices.size();
  55. faces.push_back({ num_vertices-3, num_vertices-2, num_vertices-1 });
  56. }
  57. void GeometryDumper::Dump() {
  58. // NOTE: Permanently enabling this just trashes the hard disk for no reason.
  59. // Hence, this is currently disabled.
  60. return;
  61. static int index = 0;
  62. std::string filename = std::string("geometry_dump") + std::to_string(++index) + ".obj";
  63. std::ofstream file(filename);
  64. for (const auto& vertex : vertices) {
  65. file << "v " << vertex.pos[0]
  66. << " " << vertex.pos[1]
  67. << " " << vertex.pos[2] << std::endl;
  68. }
  69. for (const Face& face : faces) {
  70. file << "f " << 1+face.index[0]
  71. << " " << 1+face.index[1]
  72. << " " << 1+face.index[2] << std::endl;
  73. }
  74. }
  75. #pragma pack(1)
  76. struct DVLBHeader {
  77. enum : u32 {
  78. MAGIC_WORD = 0x424C5644, // "DVLB"
  79. };
  80. u32 magic_word;
  81. u32 num_programs;
  82. // u32 dvle_offset_table[];
  83. };
  84. static_assert(sizeof(DVLBHeader) == 0x8, "Incorrect structure size");
  85. struct DVLPHeader {
  86. enum : u32 {
  87. MAGIC_WORD = 0x504C5644, // "DVLP"
  88. };
  89. u32 magic_word;
  90. u32 version;
  91. u32 binary_offset; // relative to DVLP start
  92. u32 binary_size_words;
  93. u32 swizzle_patterns_offset;
  94. u32 swizzle_patterns_num_entries;
  95. u32 unk2;
  96. };
  97. static_assert(sizeof(DVLPHeader) == 0x1C, "Incorrect structure size");
  98. struct DVLEHeader {
  99. enum : u32 {
  100. MAGIC_WORD = 0x454c5644, // "DVLE"
  101. };
  102. enum class ShaderType : u8 {
  103. VERTEX = 0,
  104. GEOMETRY = 1,
  105. };
  106. u32 magic_word;
  107. u16 pad1;
  108. ShaderType type;
  109. u8 pad2;
  110. u32 main_offset_words; // offset within binary blob
  111. u32 endmain_offset_words;
  112. u32 pad3;
  113. u32 pad4;
  114. u32 constant_table_offset;
  115. u32 constant_table_size; // number of entries
  116. u32 label_table_offset;
  117. u32 label_table_size;
  118. u32 output_register_table_offset;
  119. u32 output_register_table_size;
  120. u32 uniform_table_offset;
  121. u32 uniform_table_size;
  122. u32 symbol_table_offset;
  123. u32 symbol_table_size;
  124. };
  125. static_assert(sizeof(DVLEHeader) == 0x40, "Incorrect structure size");
  126. #pragma pack()
  127. void DumpShader(const u32* binary_data, u32 binary_size, const u32* swizzle_data, u32 swizzle_size,
  128. u32 main_offset, const Regs::VSOutputAttributes* output_attributes)
  129. {
  130. // NOTE: Permanently enabling this just trashes hard disks for no reason.
  131. // Hence, this is currently disabled.
  132. return;
  133. struct StuffToWrite {
  134. u8* pointer;
  135. u32 size;
  136. };
  137. std::vector<StuffToWrite> writing_queue;
  138. u32 write_offset = 0;
  139. auto QueueForWriting = [&writing_queue,&write_offset](u8* pointer, u32 size) {
  140. writing_queue.push_back({pointer, size});
  141. u32 old_write_offset = write_offset;
  142. write_offset += size;
  143. return old_write_offset;
  144. };
  145. // First off, try to translate Pica state (one enum for output attribute type and component)
  146. // into shbin format (separate type and component mask).
  147. union OutputRegisterInfo {
  148. enum Type : u64 {
  149. POSITION = 0,
  150. COLOR = 2,
  151. TEXCOORD0 = 3,
  152. TEXCOORD1 = 5,
  153. TEXCOORD2 = 6,
  154. };
  155. BitField< 0, 64, u64> hex;
  156. BitField< 0, 16, Type> type;
  157. BitField<16, 16, u64> id;
  158. BitField<32, 4, u64> component_mask;
  159. };
  160. // This is put into a try-catch block to make sure we notice unknown configurations.
  161. std::vector<OutputRegisterInfo> output_info_table;
  162. for (unsigned i = 0; i < 7; ++i) {
  163. using OutputAttributes = Pica::Regs::VSOutputAttributes;
  164. // TODO: It's still unclear how the attribute components map to the register!
  165. // Once we know that, this code probably will not make much sense anymore.
  166. std::map<OutputAttributes::Semantic, std::pair<OutputRegisterInfo::Type, u32> > map = {
  167. { OutputAttributes::POSITION_X, { OutputRegisterInfo::POSITION, 1} },
  168. { OutputAttributes::POSITION_Y, { OutputRegisterInfo::POSITION, 2} },
  169. { OutputAttributes::POSITION_Z, { OutputRegisterInfo::POSITION, 4} },
  170. { OutputAttributes::POSITION_W, { OutputRegisterInfo::POSITION, 8} },
  171. { OutputAttributes::COLOR_R, { OutputRegisterInfo::COLOR, 1} },
  172. { OutputAttributes::COLOR_G, { OutputRegisterInfo::COLOR, 2} },
  173. { OutputAttributes::COLOR_B, { OutputRegisterInfo::COLOR, 4} },
  174. { OutputAttributes::COLOR_A, { OutputRegisterInfo::COLOR, 8} },
  175. { OutputAttributes::TEXCOORD0_U, { OutputRegisterInfo::TEXCOORD0, 1} },
  176. { OutputAttributes::TEXCOORD0_V, { OutputRegisterInfo::TEXCOORD0, 2} },
  177. { OutputAttributes::TEXCOORD1_U, { OutputRegisterInfo::TEXCOORD1, 1} },
  178. { OutputAttributes::TEXCOORD1_V, { OutputRegisterInfo::TEXCOORD1, 2} },
  179. { OutputAttributes::TEXCOORD2_U, { OutputRegisterInfo::TEXCOORD2, 1} },
  180. { OutputAttributes::TEXCOORD2_V, { OutputRegisterInfo::TEXCOORD2, 2} }
  181. };
  182. for (const auto& semantic : std::vector<OutputAttributes::Semantic>{
  183. output_attributes[i].map_x,
  184. output_attributes[i].map_y,
  185. output_attributes[i].map_z,
  186. output_attributes[i].map_w }) {
  187. if (semantic == OutputAttributes::INVALID)
  188. continue;
  189. try {
  190. OutputRegisterInfo::Type type = map.at(semantic).first;
  191. u32 component_mask = map.at(semantic).second;
  192. auto it = std::find_if(output_info_table.begin(), output_info_table.end(),
  193. [&i, &type](const OutputRegisterInfo& info) {
  194. return info.id == i && info.type == type;
  195. }
  196. );
  197. if (it == output_info_table.end()) {
  198. output_info_table.push_back({});
  199. output_info_table.back().type = type;
  200. output_info_table.back().component_mask = component_mask;
  201. output_info_table.back().id = i;
  202. } else {
  203. it->component_mask = it->component_mask | component_mask;
  204. }
  205. } catch (const std::out_of_range& ) {
  206. _dbg_assert_msg_(HW_GPU, 0, "Unknown output attribute mapping");
  207. LOG_ERROR(HW_GPU, "Unknown output attribute mapping: %03x, %03x, %03x, %03x",
  208. (int)output_attributes[i].map_x.Value(),
  209. (int)output_attributes[i].map_y.Value(),
  210. (int)output_attributes[i].map_z.Value(),
  211. (int)output_attributes[i].map_w.Value());
  212. }
  213. }
  214. }
  215. struct {
  216. DVLBHeader header;
  217. u32 dvle_offset;
  218. } dvlb{ {DVLBHeader::MAGIC_WORD, 1 } }; // 1 DVLE
  219. DVLPHeader dvlp{ DVLPHeader::MAGIC_WORD };
  220. DVLEHeader dvle{ DVLEHeader::MAGIC_WORD };
  221. QueueForWriting((u8*)&dvlb, sizeof(dvlb));
  222. u32 dvlp_offset = QueueForWriting((u8*)&dvlp, sizeof(dvlp));
  223. dvlb.dvle_offset = QueueForWriting((u8*)&dvle, sizeof(dvle));
  224. // TODO: Reduce the amount of binary code written to relevant portions
  225. dvlp.binary_offset = write_offset - dvlp_offset;
  226. dvlp.binary_size_words = binary_size;
  227. QueueForWriting((u8*)binary_data, binary_size * sizeof(u32));
  228. dvlp.swizzle_patterns_offset = write_offset - dvlp_offset;
  229. dvlp.swizzle_patterns_num_entries = swizzle_size;
  230. u32 dummy = 0;
  231. for (unsigned int i = 0; i < swizzle_size; ++i) {
  232. QueueForWriting((u8*)&swizzle_data[i], sizeof(swizzle_data[i]));
  233. QueueForWriting((u8*)&dummy, sizeof(dummy));
  234. }
  235. dvle.main_offset_words = main_offset;
  236. dvle.output_register_table_offset = write_offset - dvlb.dvle_offset;
  237. dvle.output_register_table_size = output_info_table.size();
  238. QueueForWriting((u8*)output_info_table.data(), output_info_table.size() * sizeof(OutputRegisterInfo));
  239. // TODO: Create a label table for "main"
  240. // Write data to file
  241. static int dump_index = 0;
  242. std::string filename = std::string("shader_dump") + std::to_string(++dump_index) + std::string(".shbin");
  243. std::ofstream file(filename, std::ios_base::out | std::ios_base::binary);
  244. for (auto& chunk : writing_queue) {
  245. file.write((char*)chunk.pointer, chunk.size);
  246. }
  247. }
  248. static std::unique_ptr<PicaTrace> pica_trace;
  249. static std::mutex pica_trace_mutex;
  250. static int is_pica_tracing = false;
  251. void StartPicaTracing()
  252. {
  253. if (is_pica_tracing) {
  254. LOG_WARNING(HW_GPU, "StartPicaTracing called even though tracing already running!");
  255. return;
  256. }
  257. pica_trace_mutex.lock();
  258. pica_trace = std::unique_ptr<PicaTrace>(new PicaTrace);
  259. is_pica_tracing = true;
  260. pica_trace_mutex.unlock();
  261. }
  262. bool IsPicaTracing()
  263. {
  264. return is_pica_tracing != 0;
  265. }
  266. void OnPicaRegWrite(u32 id, u32 value)
  267. {
  268. // Double check for is_pica_tracing to avoid pointless locking overhead
  269. if (!is_pica_tracing)
  270. return;
  271. std::unique_lock<std::mutex> lock(pica_trace_mutex);
  272. if (!is_pica_tracing)
  273. return;
  274. pica_trace->writes.push_back({id, value});
  275. }
  276. std::unique_ptr<PicaTrace> FinishPicaTracing()
  277. {
  278. if (!is_pica_tracing) {
  279. LOG_WARNING(HW_GPU, "FinishPicaTracing called even though tracing isn't running!");
  280. return {};
  281. }
  282. // signalize that no further tracing should be performed
  283. is_pica_tracing = false;
  284. // Wait until running tracing is finished
  285. pica_trace_mutex.lock();
  286. std::unique_ptr<PicaTrace> ret(std::move(pica_trace));
  287. pica_trace_mutex.unlock();
  288. return std::move(ret);
  289. }
  290. const Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const TextureInfo& info, bool disable_alpha) {
  291. // Images are split into 8x8 tiles. Each tile is composed of four 4x4 subtiles each
  292. // of which is composed of four 2x2 subtiles each of which is composed of four texels.
  293. // Each structure is embedded into the next-bigger one in a diagonal pattern, e.g.
  294. // texels are laid out in a 2x2 subtile like this:
  295. // 2 3
  296. // 0 1
  297. //
  298. // The full 8x8 tile has the texels arranged like this:
  299. //
  300. // 42 43 46 47 58 59 62 63
  301. // 40 41 44 45 56 57 60 61
  302. // 34 35 38 39 50 51 54 55
  303. // 32 33 36 37 48 49 52 53
  304. // 10 11 14 15 26 27 30 31
  305. // 08 09 12 13 24 25 28 29
  306. // 02 03 06 07 18 19 22 23
  307. // 00 01 04 05 16 17 20 21
  308. // TODO(neobrain): Not sure if this swizzling pattern is used for all textures.
  309. // To be flexible in case different but similar patterns are used, we keep this
  310. // somewhat inefficient code around for now.
  311. int texel_index_within_tile = 0;
  312. for (int block_size_index = 0; block_size_index < 3; ++block_size_index) {
  313. int sub_tile_width = 1 << block_size_index;
  314. int sub_tile_height = 1 << block_size_index;
  315. int sub_tile_index = (x & sub_tile_width) << block_size_index;
  316. sub_tile_index += 2 * ((y & sub_tile_height) << block_size_index);
  317. texel_index_within_tile += sub_tile_index;
  318. }
  319. const int block_width = 8;
  320. const int block_height = 8;
  321. int coarse_x = (x / block_width) * block_width;
  322. int coarse_y = (y / block_height) * block_height;
  323. switch (info.format) {
  324. case Regs::TextureFormat::RGBA8:
  325. {
  326. const u8* source_ptr = source + coarse_x * block_height * 4 + coarse_y * info.stride + texel_index_within_tile * 4;
  327. return { source_ptr[3], source_ptr[2], source_ptr[1], disable_alpha ? 255 : source_ptr[0] };
  328. }
  329. case Regs::TextureFormat::RGB8:
  330. {
  331. const u8* source_ptr = source + coarse_x * block_height * 3 + coarse_y * info.stride + texel_index_within_tile * 3;
  332. return { source_ptr[2], source_ptr[1], source_ptr[0], 255 };
  333. }
  334. case Regs::TextureFormat::RGBA5551:
  335. {
  336. const u16 source_ptr = *(const u16*)(source + coarse_x * block_height * 2 + coarse_y * info.stride + texel_index_within_tile * 2);
  337. u8 r = (source_ptr >> 11) & 0x1F;
  338. u8 g = ((source_ptr) >> 6) & 0x1F;
  339. u8 b = (source_ptr >> 1) & 0x1F;
  340. u8 a = source_ptr & 1;
  341. return Math::MakeVec<u8>((r << 3) | (r >> 2), (g << 3) | (g >> 2), (b << 3) | (b >> 2), disable_alpha ? 255 : (a * 255));
  342. }
  343. case Regs::TextureFormat::RGBA4:
  344. {
  345. const u8* source_ptr = source + coarse_x * block_height * 2 + coarse_y * info.stride + texel_index_within_tile * 2;
  346. u8 r = source_ptr[1] >> 4;
  347. u8 g = source_ptr[1] & 0xFF;
  348. u8 b = source_ptr[0] >> 4;
  349. u8 a = source_ptr[0] & 0xFF;
  350. r = (r << 4) | r;
  351. g = (g << 4) | g;
  352. b = (b << 4) | b;
  353. a = (a << 4) | a;
  354. return { r, g, b, disable_alpha ? 255 : a };
  355. }
  356. case Regs::TextureFormat::A8:
  357. {
  358. const u8* source_ptr = source + coarse_x * block_height + coarse_y * info.stride + texel_index_within_tile;
  359. // TODO: Better control this...
  360. if (disable_alpha) {
  361. return { *source_ptr, *source_ptr, *source_ptr, 255 };
  362. } else {
  363. return { 0, 0, 0, *source_ptr };
  364. }
  365. }
  366. default:
  367. LOG_ERROR(HW_GPU, "Unknown texture format: %x", (u32)info.format);
  368. _dbg_assert_(HW_GPU, 0);
  369. return {};
  370. }
  371. }
  372. TextureInfo TextureInfo::FromPicaRegister(const Regs::TextureConfig& config,
  373. const Regs::TextureFormat& format)
  374. {
  375. TextureInfo info;
  376. info.address = config.GetPhysicalAddress();
  377. info.width = config.width;
  378. info.height = config.height;
  379. info.format = format;
  380. info.stride = Pica::Regs::BytesPerPixel(info.format) * info.width;
  381. return info;
  382. }
  383. void DumpTexture(const Pica::Regs::TextureConfig& texture_config, u8* data) {
  384. // NOTE: Permanently enabling this just trashes hard disks for no reason.
  385. // Hence, this is currently disabled.
  386. return;
  387. #ifndef HAVE_PNG
  388. return;
  389. #else
  390. if (!data)
  391. return;
  392. // Write data to file
  393. static int dump_index = 0;
  394. std::string filename = std::string("texture_dump") + std::to_string(++dump_index) + std::string(".png");
  395. u32 row_stride = texture_config.width * 3;
  396. u8* buf;
  397. char title[] = "Citra texture dump";
  398. char title_key[] = "Title";
  399. png_structp png_ptr = nullptr;
  400. png_infop info_ptr = nullptr;
  401. // Open file for writing (binary mode)
  402. FileUtil::IOFile fp(filename, "wb");
  403. // Initialize write structure
  404. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
  405. if (png_ptr == nullptr) {
  406. LOG_ERROR(Debug_GPU, "Could not allocate write struct\n");
  407. goto finalise;
  408. }
  409. // Initialize info structure
  410. info_ptr = png_create_info_struct(png_ptr);
  411. if (info_ptr == nullptr) {
  412. LOG_ERROR(Debug_GPU, "Could not allocate info struct\n");
  413. goto finalise;
  414. }
  415. // Setup Exception handling
  416. if (setjmp(png_jmpbuf(png_ptr))) {
  417. LOG_ERROR(Debug_GPU, "Error during png creation\n");
  418. goto finalise;
  419. }
  420. png_init_io(png_ptr, fp.GetHandle());
  421. // Write header (8 bit colour depth)
  422. png_set_IHDR(png_ptr, info_ptr, texture_config.width, texture_config.height,
  423. 8, PNG_COLOR_TYPE_RGB /*_ALPHA*/, PNG_INTERLACE_NONE,
  424. PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  425. png_text title_text;
  426. title_text.compression = PNG_TEXT_COMPRESSION_NONE;
  427. title_text.key = title_key;
  428. title_text.text = title;
  429. png_set_text(png_ptr, info_ptr, &title_text, 1);
  430. png_write_info(png_ptr, info_ptr);
  431. buf = new u8[row_stride * texture_config.height];
  432. for (unsigned y = 0; y < texture_config.height; ++y) {
  433. for (unsigned x = 0; x < texture_config.width; ++x) {
  434. TextureInfo info;
  435. info.width = texture_config.width;
  436. info.height = texture_config.height;
  437. info.stride = row_stride;
  438. info.format = registers.texture0_format;
  439. Math::Vec4<u8> texture_color = LookupTexture(data, x, y, info);
  440. buf[3 * x + y * row_stride ] = texture_color.r();
  441. buf[3 * x + y * row_stride + 1] = texture_color.g();
  442. buf[3 * x + y * row_stride + 2] = texture_color.b();
  443. }
  444. }
  445. // Write image data
  446. for (unsigned y = 0; y < texture_config.height; ++y)
  447. {
  448. u8* row_ptr = (u8*)buf + y * row_stride;
  449. u8* ptr = row_ptr;
  450. png_write_row(png_ptr, row_ptr);
  451. }
  452. delete[] buf;
  453. // End write
  454. png_write_end(png_ptr, nullptr);
  455. finalise:
  456. if (info_ptr != nullptr) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  457. if (png_ptr != nullptr) png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
  458. #endif
  459. }
  460. void DumpTevStageConfig(const std::array<Pica::Regs::TevStageConfig,6>& stages)
  461. {
  462. using Source = Pica::Regs::TevStageConfig::Source;
  463. using ColorModifier = Pica::Regs::TevStageConfig::ColorModifier;
  464. using AlphaModifier = Pica::Regs::TevStageConfig::AlphaModifier;
  465. using Operation = Pica::Regs::TevStageConfig::Operation;
  466. std::string stage_info = "Tev setup:\n";
  467. for (size_t index = 0; index < stages.size(); ++index) {
  468. const auto& tev_stage = stages[index];
  469. const std::map<Source, std::string> source_map = {
  470. { Source::PrimaryColor, "PrimaryColor" },
  471. { Source::Texture0, "Texture0" },
  472. { Source::Constant, "Constant" },
  473. { Source::Previous, "Previous" },
  474. };
  475. const std::map<ColorModifier, std::string> color_modifier_map = {
  476. { ColorModifier::SourceColor, { "%source.rgb" } }
  477. };
  478. const std::map<AlphaModifier, std::string> alpha_modifier_map = {
  479. { AlphaModifier::SourceAlpha, "%source.a" }
  480. };
  481. std::map<Operation, std::string> combiner_map = {
  482. { Operation::Replace, "%source1" },
  483. { Operation::Modulate, "(%source1 * %source2) / 255" },
  484. };
  485. auto ReplacePattern =
  486. [](const std::string& input, const std::string& pattern, const std::string& replacement) -> std::string {
  487. size_t start = input.find(pattern);
  488. if (start == std::string::npos)
  489. return input;
  490. std::string ret = input;
  491. ret.replace(start, pattern.length(), replacement);
  492. return ret;
  493. };
  494. auto GetColorSourceStr =
  495. [&source_map,&color_modifier_map,&ReplacePattern](const Source& src, const ColorModifier& modifier) {
  496. auto src_it = source_map.find(src);
  497. std::string src_str = "Unknown";
  498. if (src_it != source_map.end())
  499. src_str = src_it->second;
  500. auto modifier_it = color_modifier_map.find(modifier);
  501. std::string modifier_str = "%source.????";
  502. if (modifier_it != color_modifier_map.end())
  503. modifier_str = modifier_it->second;
  504. return ReplacePattern(modifier_str, "%source", src_str);
  505. };
  506. auto GetColorCombinerStr =
  507. [&](const Regs::TevStageConfig& tev_stage) {
  508. auto op_it = combiner_map.find(tev_stage.color_op);
  509. std::string op_str = "Unknown op (%source1, %source2, %source3)";
  510. if (op_it != combiner_map.end())
  511. op_str = op_it->second;
  512. op_str = ReplacePattern(op_str, "%source1", GetColorSourceStr(tev_stage.color_source1, tev_stage.color_modifier1));
  513. op_str = ReplacePattern(op_str, "%source2", GetColorSourceStr(tev_stage.color_source2, tev_stage.color_modifier2));
  514. return ReplacePattern(op_str, "%source3", GetColorSourceStr(tev_stage.color_source3, tev_stage.color_modifier3));
  515. };
  516. auto GetAlphaSourceStr =
  517. [&source_map,&alpha_modifier_map,&ReplacePattern](const Source& src, const AlphaModifier& modifier) {
  518. auto src_it = source_map.find(src);
  519. std::string src_str = "Unknown";
  520. if (src_it != source_map.end())
  521. src_str = src_it->second;
  522. auto modifier_it = alpha_modifier_map.find(modifier);
  523. std::string modifier_str = "%source.????";
  524. if (modifier_it != alpha_modifier_map.end())
  525. modifier_str = modifier_it->second;
  526. return ReplacePattern(modifier_str, "%source", src_str);
  527. };
  528. auto GetAlphaCombinerStr =
  529. [&](const Regs::TevStageConfig& tev_stage) {
  530. auto op_it = combiner_map.find(tev_stage.alpha_op);
  531. std::string op_str = "Unknown op (%source1, %source2, %source3)";
  532. if (op_it != combiner_map.end())
  533. op_str = op_it->second;
  534. op_str = ReplacePattern(op_str, "%source1", GetAlphaSourceStr(tev_stage.alpha_source1, tev_stage.alpha_modifier1));
  535. op_str = ReplacePattern(op_str, "%source2", GetAlphaSourceStr(tev_stage.alpha_source2, tev_stage.alpha_modifier2));
  536. return ReplacePattern(op_str, "%source3", GetAlphaSourceStr(tev_stage.alpha_source3, tev_stage.alpha_modifier3));
  537. };
  538. stage_info += "Stage " + std::to_string(index) + ": " + GetColorCombinerStr(tev_stage) + " " + GetAlphaCombinerStr(tev_stage) + "\n";
  539. }
  540. LOG_TRACE(HW_GPU, "%s", stage_info.c_str());
  541. }
  542. } // namespace
  543. } // namespace