debug_utils.cpp 24 KB

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