graphics_vertex_shader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <iomanip>
  5. #include <sstream>
  6. #include <QBoxLayout>
  7. #include <QLabel>
  8. #include <QPushButton>
  9. #include <QTreeView>
  10. #include "video_core/shader/shader_interpreter.h"
  11. #include "graphics_vertex_shader.h"
  12. using nihstro::OpCode;
  13. using nihstro::Instruction;
  14. using nihstro::SourceRegister;
  15. using nihstro::SwizzlePattern;
  16. GraphicsVertexShaderModel::GraphicsVertexShaderModel(QObject* parent): QAbstractItemModel(parent) {
  17. }
  18. QModelIndex GraphicsVertexShaderModel::index(int row, int column, const QModelIndex& parent) const {
  19. return createIndex(row, column);
  20. }
  21. QModelIndex GraphicsVertexShaderModel::parent(const QModelIndex& child) const {
  22. return QModelIndex();
  23. }
  24. int GraphicsVertexShaderModel::columnCount(const QModelIndex& parent) const {
  25. return 3;
  26. }
  27. int GraphicsVertexShaderModel::rowCount(const QModelIndex& parent) const {
  28. return static_cast<int>(info.code.size());
  29. }
  30. QVariant GraphicsVertexShaderModel::headerData(int section, Qt::Orientation orientation, int role) const {
  31. switch(role) {
  32. case Qt::DisplayRole:
  33. {
  34. if (section == 0) {
  35. return tr("Offset");
  36. } else if (section == 1) {
  37. return tr("Raw");
  38. } else if (section == 2) {
  39. return tr("Disassembly");
  40. }
  41. break;
  42. }
  43. }
  44. return QVariant();
  45. }
  46. QVariant GraphicsVertexShaderModel::data(const QModelIndex& index, int role) const {
  47. switch (role) {
  48. case Qt::DisplayRole:
  49. {
  50. switch (index.column()) {
  51. case 0:
  52. if (info.HasLabel(index.row()))
  53. return QString::fromStdString(info.GetLabel(index.row()));
  54. return QString("%1").arg(4*index.row(), 4, 16, QLatin1Char('0'));
  55. case 1:
  56. return QString("%1").arg(info.code[index.row()].hex, 8, 16, QLatin1Char('0'));
  57. case 2:
  58. {
  59. std::stringstream output;
  60. output.flags(std::ios::hex);
  61. Instruction instr = info.code[index.row()];
  62. const SwizzlePattern& swizzle = info.swizzle_info[instr.common.operand_desc_id].pattern;
  63. // longest known instruction name: "setemit "
  64. output << std::setw(8) << std::left << instr.opcode.Value().GetInfo().name;
  65. // e.g. "-c92.xyzw"
  66. static auto print_input = [](std::stringstream& output, const SourceRegister& input,
  67. bool negate, const std::string& swizzle_mask) {
  68. output << std::setw(4) << std::right << (negate ? "-" : "") + input.GetName();
  69. output << "." << swizzle_mask;
  70. };
  71. // e.g. "-c92[a0.x].xyzw"
  72. static auto print_input_indexed = [](std::stringstream& output, const SourceRegister& input,
  73. bool negate, const std::string& swizzle_mask,
  74. const std::string& address_register_name) {
  75. std::string relative_address;
  76. if (!address_register_name.empty())
  77. relative_address = "[" + address_register_name + "]";
  78. output << std::setw(10) << std::right << (negate ? "-" : "") + input.GetName() + relative_address;
  79. output << "." << swizzle_mask;
  80. };
  81. // Use print_input or print_input_indexed depending on whether relative addressing is used or not.
  82. static auto print_input_indexed_compact = [](std::stringstream& output, const SourceRegister& input,
  83. bool negate, const std::string& swizzle_mask,
  84. const std::string& address_register_name) {
  85. if (address_register_name.empty())
  86. print_input(output, input, negate, swizzle_mask);
  87. else
  88. print_input_indexed(output, input, negate, swizzle_mask, address_register_name);
  89. };
  90. switch (instr.opcode.Value().GetInfo().type) {
  91. case OpCode::Type::Trivial:
  92. // Nothing to do here
  93. break;
  94. case OpCode::Type::Arithmetic:
  95. {
  96. // Use custom code for special instructions
  97. switch (instr.opcode.Value().EffectiveOpCode()) {
  98. case OpCode::Id::CMP:
  99. {
  100. // NOTE: CMP always writes both cc components, so we do not consider the dest mask here.
  101. output << std::setw(4) << std::right << "cc.";
  102. output << "xy ";
  103. SourceRegister src1 = instr.common.GetSrc1(false);
  104. SourceRegister src2 = instr.common.GetSrc2(false);
  105. print_input_indexed_compact(output, src1, swizzle.negate_src1, swizzle.SelectorToString(false).substr(0,1), instr.common.AddressRegisterName());
  106. output << " " << instr.common.compare_op.ToString(instr.common.compare_op.x) << " ";
  107. print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(true).substr(0,1));
  108. output << ", ";
  109. print_input_indexed_compact(output, src1, swizzle.negate_src1, swizzle.SelectorToString(false).substr(1,1), instr.common.AddressRegisterName());
  110. output << " " << instr.common.compare_op.ToString(instr.common.compare_op.y) << " ";
  111. print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(true).substr(1,1));
  112. break;
  113. }
  114. default:
  115. {
  116. bool src_is_inverted = 0 != (instr.opcode.Value().GetInfo().subtype & OpCode::Info::SrcInversed);
  117. if (instr.opcode.Value().GetInfo().subtype & OpCode::Info::Dest) {
  118. // e.g. "r12.xy__"
  119. output << std::setw(4) << std::right << instr.common.dest.Value().GetName() + ".";
  120. output << swizzle.DestMaskToString();
  121. } else if (instr.opcode.Value().GetInfo().subtype == OpCode::Info::MOVA) {
  122. output << std::setw(4) << std::right << "a0.";
  123. output << swizzle.DestMaskToString();
  124. } else {
  125. output << " ";
  126. }
  127. output << " ";
  128. if (instr.opcode.Value().GetInfo().subtype & OpCode::Info::Src1) {
  129. SourceRegister src1 = instr.common.GetSrc1(src_is_inverted);
  130. print_input_indexed(output, src1, swizzle.negate_src1, swizzle.SelectorToString(false), instr.common.AddressRegisterName());
  131. } else {
  132. output << " ";
  133. }
  134. // TODO: In some cases, the Address Register is used as an index for SRC2 instead of SRC1
  135. if (instr.opcode.Value().GetInfo().subtype & OpCode::Info::Src2) {
  136. SourceRegister src2 = instr.common.GetSrc2(src_is_inverted);
  137. print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(true));
  138. }
  139. break;
  140. }
  141. }
  142. break;
  143. }
  144. case OpCode::Type::Conditional:
  145. {
  146. switch (instr.opcode.Value().EffectiveOpCode()) {
  147. case OpCode::Id::LOOP:
  148. output << "(unknown instruction format)";
  149. break;
  150. default:
  151. output << "if ";
  152. if (instr.opcode.Value().GetInfo().subtype & OpCode::Info::HasCondition) {
  153. const char* ops[] = {
  154. " || ", " && ", "", ""
  155. };
  156. if (instr.flow_control.op != instr.flow_control.JustY)
  157. output << ((!instr.flow_control.refx) ? "!" : " ") << "cc.x";
  158. output << ops[instr.flow_control.op];
  159. if (instr.flow_control.op != instr.flow_control.JustX)
  160. output << ((!instr.flow_control.refy) ? "!" : " ") << "cc.y";
  161. output << " ";
  162. } else if (instr.opcode.Value().GetInfo().subtype & OpCode::Info::HasUniformIndex) {
  163. output << "b" << instr.flow_control.bool_uniform_id << " ";
  164. }
  165. u32 target_addr = instr.flow_control.dest_offset;
  166. u32 target_addr_else = instr.flow_control.dest_offset;
  167. if (instr.opcode.Value().GetInfo().subtype & OpCode::Info::HasAlternative) {
  168. output << "else jump to 0x" << std::setw(4) << std::right << std::setfill('0') << 4 * instr.flow_control.dest_offset << " ";
  169. } else if (instr.opcode.Value().GetInfo().subtype & OpCode::Info::HasExplicitDest) {
  170. output << "jump to 0x" << std::setw(4) << std::right << std::setfill('0') << 4 * instr.flow_control.dest_offset << " ";
  171. } else {
  172. // TODO: Handle other cases
  173. }
  174. if (instr.opcode.Value().GetInfo().subtype & OpCode::Info::HasFinishPoint) {
  175. output << "(return on " << std::setw(4) << std::right << std::setfill('0')
  176. << 4 * instr.flow_control.dest_offset + 4 * instr.flow_control.num_instructions << ")";
  177. }
  178. break;
  179. }
  180. break;
  181. }
  182. default:
  183. output << "(unknown instruction format)";
  184. break;
  185. }
  186. return QString::fromLatin1(output.str().c_str());
  187. }
  188. default:
  189. break;
  190. }
  191. }
  192. case Qt::FontRole:
  193. return QFont("monospace");
  194. default:
  195. break;
  196. }
  197. return QVariant();
  198. }
  199. void GraphicsVertexShaderModel::OnUpdate()
  200. {
  201. beginResetModel();
  202. info.Clear();
  203. auto& shader_setup = Pica::g_state.vs;
  204. for (auto instr : shader_setup.program_code)
  205. info.code.push_back({instr});
  206. for (auto pattern : shader_setup.swizzle_data)
  207. info.swizzle_info.push_back({pattern});
  208. u32 entry_point = Pica::g_state.regs.vs.main_offset;
  209. info.labels.insert({ entry_point, "main" });
  210. endResetModel();
  211. }
  212. void GraphicsVertexShaderModel::DumpShader() {
  213. auto& setup = Pica::g_state.vs;
  214. auto& config = Pica::g_state.regs.vs;
  215. Pica::DebugUtils::DumpShader(setup.program_code.data(), setup.program_code.size(),
  216. setup.swizzle_data.data(), setup.swizzle_data.size(),
  217. config.main_offset, Pica::g_state.regs.vs_output_attributes);
  218. }
  219. GraphicsVertexShaderWidget::GraphicsVertexShaderWidget(std::shared_ptr< Pica::DebugContext > debug_context,
  220. QWidget* parent)
  221. : BreakPointObserverDock(debug_context, "Pica Vertex Shader", parent) {
  222. setObjectName("PicaVertexShader");
  223. auto binary_model = new GraphicsVertexShaderModel(this);
  224. auto binary_list = new QTreeView;
  225. binary_list->setModel(binary_model);
  226. binary_list->setRootIsDecorated(false);
  227. binary_list->setAlternatingRowColors(true);
  228. auto dump_shader = new QPushButton(tr("Dump"));
  229. connect(dump_shader, SIGNAL(clicked()), binary_model, SLOT(DumpShader()));
  230. connect(this, SIGNAL(Update()), binary_model, SLOT(OnUpdate()));
  231. auto main_widget = new QWidget;
  232. auto main_layout = new QVBoxLayout;
  233. {
  234. auto sub_layout = new QHBoxLayout;
  235. sub_layout->addWidget(binary_list);
  236. main_layout->addLayout(sub_layout);
  237. }
  238. main_layout->addWidget(dump_shader);
  239. main_widget->setLayout(main_layout);
  240. setWidget(main_widget);
  241. }
  242. void GraphicsVertexShaderWidget::OnBreakPointHit(Pica::DebugContext::Event event, void* data) {
  243. emit Update();
  244. widget()->setEnabled(true);
  245. }
  246. void GraphicsVertexShaderWidget::OnResumed() {
  247. widget()->setEnabled(false);
  248. }