debug_utils.cpp 24 KB

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