shader_ir.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <cmath>
  7. #include "common/assert.h"
  8. #include "common/common_types.h"
  9. #include "common/logging/log.h"
  10. #include "video_core/engines/shader_bytecode.h"
  11. #include "video_core/shader/node_helper.h"
  12. #include "video_core/shader/registry.h"
  13. #include "video_core/shader/shader_ir.h"
  14. namespace VideoCommon::Shader {
  15. using Tegra::Shader::Attribute;
  16. using Tegra::Shader::Instruction;
  17. using Tegra::Shader::IpaMode;
  18. using Tegra::Shader::Pred;
  19. using Tegra::Shader::PredCondition;
  20. using Tegra::Shader::PredOperation;
  21. using Tegra::Shader::Register;
  22. ShaderIR::ShaderIR(const ProgramCode& program_code, u32 main_offset, CompilerSettings settings,
  23. Registry& registry)
  24. : program_code{program_code}, main_offset{main_offset}, settings{settings}, registry{registry} {
  25. Decode();
  26. PostDecode();
  27. }
  28. ShaderIR::~ShaderIR() = default;
  29. Node ShaderIR::GetRegister(Register reg) {
  30. if (reg != Register::ZeroIndex) {
  31. used_registers.insert(static_cast<u32>(reg));
  32. }
  33. return MakeNode<GprNode>(reg);
  34. }
  35. Node ShaderIR::GetCustomVariable(u32 id) {
  36. return MakeNode<CustomVarNode>(id);
  37. }
  38. Node ShaderIR::GetImmediate19(Instruction instr) {
  39. return Immediate(instr.alu.GetImm20_19());
  40. }
  41. Node ShaderIR::GetImmediate32(Instruction instr) {
  42. return Immediate(instr.alu.GetImm20_32());
  43. }
  44. Node ShaderIR::GetConstBuffer(u64 index_, u64 offset_) {
  45. const auto index = static_cast<u32>(index_);
  46. const auto offset = static_cast<u32>(offset_);
  47. used_cbufs.try_emplace(index).first->second.MarkAsUsed(offset);
  48. return MakeNode<CbufNode>(index, Immediate(offset));
  49. }
  50. Node ShaderIR::GetConstBufferIndirect(u64 index_, u64 offset_, Node node) {
  51. const auto index = static_cast<u32>(index_);
  52. const auto offset = static_cast<u32>(offset_);
  53. used_cbufs.try_emplace(index).first->second.MarkAsUsedIndirect();
  54. Node final_offset = [&] {
  55. // Attempt to inline constant buffer without a variable offset. This is done to allow
  56. // tracking LDC calls.
  57. if (const auto gpr = std::get_if<GprNode>(&*node)) {
  58. if (gpr->GetIndex() == Register::ZeroIndex) {
  59. return Immediate(offset);
  60. }
  61. }
  62. return Operation(OperationCode::UAdd, NO_PRECISE, std::move(node), Immediate(offset));
  63. }();
  64. return MakeNode<CbufNode>(index, std::move(final_offset));
  65. }
  66. Node ShaderIR::GetPredicate(u64 pred_, bool negated) {
  67. const auto pred = static_cast<Pred>(pred_);
  68. if (pred != Pred::UnusedIndex && pred != Pred::NeverExecute) {
  69. used_predicates.insert(pred);
  70. }
  71. return MakeNode<PredicateNode>(pred, negated);
  72. }
  73. Node ShaderIR::GetPredicate(bool immediate) {
  74. return GetPredicate(static_cast<u64>(immediate ? Pred::UnusedIndex : Pred::NeverExecute));
  75. }
  76. Node ShaderIR::GetInputAttribute(Attribute::Index index, u64 element, Node buffer) {
  77. MarkAttributeUsage(index, element);
  78. used_input_attributes.emplace(index);
  79. return MakeNode<AbufNode>(index, static_cast<u32>(element), std::move(buffer));
  80. }
  81. Node ShaderIR::GetPhysicalInputAttribute(Tegra::Shader::Register physical_address, Node buffer) {
  82. uses_physical_attributes = true;
  83. return MakeNode<AbufNode>(GetRegister(physical_address), buffer);
  84. }
  85. Node ShaderIR::GetOutputAttribute(Attribute::Index index, u64 element, Node buffer) {
  86. MarkAttributeUsage(index, element);
  87. used_output_attributes.insert(index);
  88. return MakeNode<AbufNode>(index, static_cast<u32>(element), std::move(buffer));
  89. }
  90. Node ShaderIR::GetInternalFlag(InternalFlag flag, bool negated) const {
  91. const Node node = MakeNode<InternalFlagNode>(flag);
  92. if (negated) {
  93. return Operation(OperationCode::LogicalNegate, node);
  94. }
  95. return node;
  96. }
  97. Node ShaderIR::GetLocalMemory(Node address) {
  98. return MakeNode<LmemNode>(std::move(address));
  99. }
  100. Node ShaderIR::GetSharedMemory(Node address) {
  101. return MakeNode<SmemNode>(std::move(address));
  102. }
  103. Node ShaderIR::GetTemporary(u32 id) {
  104. return GetRegister(Register::ZeroIndex + 1 + id);
  105. }
  106. Node ShaderIR::GetOperandAbsNegFloat(Node value, bool absolute, bool negate) {
  107. if (absolute) {
  108. value = Operation(OperationCode::FAbsolute, NO_PRECISE, std::move(value));
  109. }
  110. if (negate) {
  111. value = Operation(OperationCode::FNegate, NO_PRECISE, std::move(value));
  112. }
  113. return value;
  114. }
  115. Node ShaderIR::GetSaturatedFloat(Node value, bool saturate) {
  116. if (!saturate) {
  117. return value;
  118. }
  119. Node positive_zero = Immediate(std::copysignf(0, 1));
  120. Node positive_one = Immediate(1.0f);
  121. return Operation(OperationCode::FClamp, NO_PRECISE, std::move(value), std::move(positive_zero),
  122. std::move(positive_one));
  123. }
  124. Node ShaderIR::ConvertIntegerSize(Node value, Register::Size size, bool is_signed) {
  125. switch (size) {
  126. case Register::Size::Byte:
  127. value = SignedOperation(OperationCode::ILogicalShiftLeft, is_signed, NO_PRECISE,
  128. std::move(value), Immediate(24));
  129. value = SignedOperation(OperationCode::IArithmeticShiftRight, is_signed, NO_PRECISE,
  130. std::move(value), Immediate(24));
  131. return value;
  132. case Register::Size::Short:
  133. value = SignedOperation(OperationCode::ILogicalShiftLeft, is_signed, NO_PRECISE,
  134. std::move(value), Immediate(16));
  135. value = SignedOperation(OperationCode::IArithmeticShiftRight, is_signed, NO_PRECISE,
  136. std::move(value), Immediate(16));
  137. return value;
  138. case Register::Size::Word:
  139. // Default - do nothing
  140. return value;
  141. default:
  142. UNREACHABLE_MSG("Unimplemented conversion size: {}", static_cast<u32>(size));
  143. return value;
  144. }
  145. }
  146. Node ShaderIR::GetOperandAbsNegInteger(Node value, bool absolute, bool negate, bool is_signed) {
  147. if (!is_signed) {
  148. // Absolute or negate on an unsigned is pointless
  149. return value;
  150. }
  151. if (absolute) {
  152. value = Operation(OperationCode::IAbsolute, NO_PRECISE, std::move(value));
  153. }
  154. if (negate) {
  155. value = Operation(OperationCode::INegate, NO_PRECISE, std::move(value));
  156. }
  157. return value;
  158. }
  159. Node ShaderIR::UnpackHalfImmediate(Instruction instr, bool has_negation) {
  160. Node value = Immediate(instr.half_imm.PackImmediates());
  161. if (!has_negation) {
  162. return value;
  163. }
  164. Node first_negate = GetPredicate(instr.half_imm.first_negate != 0);
  165. Node second_negate = GetPredicate(instr.half_imm.second_negate != 0);
  166. return Operation(OperationCode::HNegate, NO_PRECISE, std::move(value), std::move(first_negate),
  167. std::move(second_negate));
  168. }
  169. Node ShaderIR::UnpackHalfFloat(Node value, Tegra::Shader::HalfType type) {
  170. return Operation(OperationCode::HUnpack, type, std::move(value));
  171. }
  172. Node ShaderIR::HalfMerge(Node dest, Node src, Tegra::Shader::HalfMerge merge) {
  173. switch (merge) {
  174. case Tegra::Shader::HalfMerge::H0_H1:
  175. return src;
  176. case Tegra::Shader::HalfMerge::F32:
  177. return Operation(OperationCode::HMergeF32, std::move(src));
  178. case Tegra::Shader::HalfMerge::Mrg_H0:
  179. return Operation(OperationCode::HMergeH0, std::move(dest), std::move(src));
  180. case Tegra::Shader::HalfMerge::Mrg_H1:
  181. return Operation(OperationCode::HMergeH1, std::move(dest), std::move(src));
  182. }
  183. UNREACHABLE();
  184. return src;
  185. }
  186. Node ShaderIR::GetOperandAbsNegHalf(Node value, bool absolute, bool negate) {
  187. if (absolute) {
  188. value = Operation(OperationCode::HAbsolute, NO_PRECISE, std::move(value));
  189. }
  190. if (negate) {
  191. value = Operation(OperationCode::HNegate, NO_PRECISE, std::move(value), GetPredicate(true),
  192. GetPredicate(true));
  193. }
  194. return value;
  195. }
  196. Node ShaderIR::GetSaturatedHalfFloat(Node value, bool saturate) {
  197. if (!saturate) {
  198. return value;
  199. }
  200. Node positive_zero = Immediate(std::copysignf(0, 1));
  201. Node positive_one = Immediate(1.0f);
  202. return Operation(OperationCode::HClamp, NO_PRECISE, std::move(value), std::move(positive_zero),
  203. std::move(positive_one));
  204. }
  205. Node ShaderIR::GetPredicateComparisonFloat(PredCondition condition, Node op_a, Node op_b) {
  206. static constexpr std::array comparison_table{
  207. std::pair{PredCondition::LessThan, OperationCode::LogicalFLessThan},
  208. std::pair{PredCondition::Equal, OperationCode::LogicalFEqual},
  209. std::pair{PredCondition::LessEqual, OperationCode::LogicalFLessEqual},
  210. std::pair{PredCondition::GreaterThan, OperationCode::LogicalFGreaterThan},
  211. std::pair{PredCondition::NotEqual, OperationCode::LogicalFNotEqual},
  212. std::pair{PredCondition::GreaterEqual, OperationCode::LogicalFGreaterEqual},
  213. std::pair{PredCondition::LessThanWithNan, OperationCode::LogicalFLessThan},
  214. std::pair{PredCondition::NotEqualWithNan, OperationCode::LogicalFNotEqual},
  215. std::pair{PredCondition::LessEqualWithNan, OperationCode::LogicalFLessEqual},
  216. std::pair{PredCondition::GreaterThanWithNan, OperationCode::LogicalFGreaterThan},
  217. std::pair{PredCondition::GreaterEqualWithNan, OperationCode::LogicalFGreaterEqual},
  218. };
  219. const auto comparison =
  220. std::find_if(comparison_table.cbegin(), comparison_table.cend(),
  221. [condition](const auto entry) { return condition == entry.first; });
  222. UNIMPLEMENTED_IF_MSG(comparison == comparison_table.cend(),
  223. "Unknown predicate comparison operation");
  224. Node predicate = Operation(comparison->second, NO_PRECISE, op_a, op_b);
  225. if (condition == PredCondition::LessThanWithNan ||
  226. condition == PredCondition::NotEqualWithNan ||
  227. condition == PredCondition::LessEqualWithNan ||
  228. condition == PredCondition::GreaterThanWithNan ||
  229. condition == PredCondition::GreaterEqualWithNan) {
  230. predicate = Operation(OperationCode::LogicalOr, predicate,
  231. Operation(OperationCode::LogicalFIsNan, op_a));
  232. predicate = Operation(OperationCode::LogicalOr, predicate,
  233. Operation(OperationCode::LogicalFIsNan, op_b));
  234. }
  235. return predicate;
  236. }
  237. Node ShaderIR::GetPredicateComparisonInteger(PredCondition condition, bool is_signed, Node op_a,
  238. Node op_b) {
  239. static constexpr std::array comparison_table{
  240. std::pair{PredCondition::LessThan, OperationCode::LogicalILessThan},
  241. std::pair{PredCondition::Equal, OperationCode::LogicalIEqual},
  242. std::pair{PredCondition::LessEqual, OperationCode::LogicalILessEqual},
  243. std::pair{PredCondition::GreaterThan, OperationCode::LogicalIGreaterThan},
  244. std::pair{PredCondition::NotEqual, OperationCode::LogicalINotEqual},
  245. std::pair{PredCondition::GreaterEqual, OperationCode::LogicalIGreaterEqual},
  246. std::pair{PredCondition::LessThanWithNan, OperationCode::LogicalILessThan},
  247. std::pair{PredCondition::NotEqualWithNan, OperationCode::LogicalINotEqual},
  248. std::pair{PredCondition::LessEqualWithNan, OperationCode::LogicalILessEqual},
  249. std::pair{PredCondition::GreaterThanWithNan, OperationCode::LogicalIGreaterThan},
  250. std::pair{PredCondition::GreaterEqualWithNan, OperationCode::LogicalIGreaterEqual},
  251. };
  252. const auto comparison =
  253. std::find_if(comparison_table.cbegin(), comparison_table.cend(),
  254. [condition](const auto entry) { return condition == entry.first; });
  255. UNIMPLEMENTED_IF_MSG(comparison == comparison_table.cend(),
  256. "Unknown predicate comparison operation");
  257. Node predicate = SignedOperation(comparison->second, is_signed, NO_PRECISE, std::move(op_a),
  258. std::move(op_b));
  259. UNIMPLEMENTED_IF_MSG(condition == PredCondition::LessThanWithNan ||
  260. condition == PredCondition::NotEqualWithNan ||
  261. condition == PredCondition::LessEqualWithNan ||
  262. condition == PredCondition::GreaterThanWithNan ||
  263. condition == PredCondition::GreaterEqualWithNan,
  264. "NaN comparisons for integers are not implemented");
  265. return predicate;
  266. }
  267. Node ShaderIR::GetPredicateComparisonHalf(Tegra::Shader::PredCondition condition, Node op_a,
  268. Node op_b) {
  269. static constexpr std::array comparison_table{
  270. std::pair{PredCondition::LessThan, OperationCode::Logical2HLessThan},
  271. std::pair{PredCondition::Equal, OperationCode::Logical2HEqual},
  272. std::pair{PredCondition::LessEqual, OperationCode::Logical2HLessEqual},
  273. std::pair{PredCondition::GreaterThan, OperationCode::Logical2HGreaterThan},
  274. std::pair{PredCondition::NotEqual, OperationCode::Logical2HNotEqual},
  275. std::pair{PredCondition::GreaterEqual, OperationCode::Logical2HGreaterEqual},
  276. std::pair{PredCondition::LessThanWithNan, OperationCode::Logical2HLessThanWithNan},
  277. std::pair{PredCondition::NotEqualWithNan, OperationCode::Logical2HNotEqualWithNan},
  278. std::pair{PredCondition::LessEqualWithNan, OperationCode::Logical2HLessEqualWithNan},
  279. std::pair{PredCondition::GreaterThanWithNan, OperationCode::Logical2HGreaterThanWithNan},
  280. std::pair{PredCondition::GreaterEqualWithNan, OperationCode::Logical2HGreaterEqualWithNan},
  281. };
  282. const auto comparison =
  283. std::find_if(comparison_table.cbegin(), comparison_table.cend(),
  284. [condition](const auto entry) { return condition == entry.first; });
  285. UNIMPLEMENTED_IF_MSG(comparison == comparison_table.cend(),
  286. "Unknown predicate comparison operation");
  287. return Operation(comparison->second, NO_PRECISE, std::move(op_a), std::move(op_b));
  288. }
  289. OperationCode ShaderIR::GetPredicateCombiner(PredOperation operation) {
  290. static constexpr std::array operation_table{
  291. OperationCode::LogicalAnd,
  292. OperationCode::LogicalOr,
  293. OperationCode::LogicalXor,
  294. };
  295. const auto index = static_cast<std::size_t>(operation);
  296. if (index >= operation_table.size()) {
  297. UNIMPLEMENTED_MSG("Unknown predicate operation.");
  298. return {};
  299. }
  300. return operation_table[index];
  301. }
  302. Node ShaderIR::GetConditionCode(Tegra::Shader::ConditionCode cc) const {
  303. switch (cc) {
  304. case Tegra::Shader::ConditionCode::NEU:
  305. return GetInternalFlag(InternalFlag::Zero, true);
  306. case Tegra::Shader::ConditionCode::FCSM_TR:
  307. UNIMPLEMENTED_MSG("EXIT.FCSM_TR is not implemented");
  308. return MakeNode<PredicateNode>(Pred::NeverExecute, false);
  309. default:
  310. UNIMPLEMENTED_MSG("Unimplemented condition code: {}", static_cast<u32>(cc));
  311. return MakeNode<PredicateNode>(Pred::NeverExecute, false);
  312. }
  313. }
  314. void ShaderIR::SetRegister(NodeBlock& bb, Register dest, Node src) {
  315. bb.push_back(Operation(OperationCode::Assign, GetRegister(dest), std::move(src)));
  316. }
  317. void ShaderIR::SetPredicate(NodeBlock& bb, u64 dest, Node src) {
  318. bb.push_back(Operation(OperationCode::LogicalAssign, GetPredicate(dest), std::move(src)));
  319. }
  320. void ShaderIR::SetInternalFlag(NodeBlock& bb, InternalFlag flag, Node value) {
  321. bb.push_back(Operation(OperationCode::LogicalAssign, GetInternalFlag(flag), std::move(value)));
  322. }
  323. void ShaderIR::SetLocalMemory(NodeBlock& bb, Node address, Node value) {
  324. bb.push_back(
  325. Operation(OperationCode::Assign, GetLocalMemory(std::move(address)), std::move(value)));
  326. }
  327. void ShaderIR::SetSharedMemory(NodeBlock& bb, Node address, Node value) {
  328. bb.push_back(
  329. Operation(OperationCode::Assign, GetSharedMemory(std::move(address)), std::move(value)));
  330. }
  331. void ShaderIR::SetTemporary(NodeBlock& bb, u32 id, Node value) {
  332. SetRegister(bb, Register::ZeroIndex + 1 + id, std::move(value));
  333. }
  334. void ShaderIR::SetInternalFlagsFromFloat(NodeBlock& bb, Node value, bool sets_cc) {
  335. if (!sets_cc) {
  336. return;
  337. }
  338. Node zerop = Operation(OperationCode::LogicalFEqual, std::move(value), Immediate(0.0f));
  339. SetInternalFlag(bb, InternalFlag::Zero, std::move(zerop));
  340. LOG_WARNING(HW_GPU, "Condition codes implementation is incomplete");
  341. }
  342. void ShaderIR::SetInternalFlagsFromInteger(NodeBlock& bb, Node value, bool sets_cc) {
  343. if (!sets_cc) {
  344. return;
  345. }
  346. Node zerop = Operation(OperationCode::LogicalIEqual, std::move(value), Immediate(0));
  347. SetInternalFlag(bb, InternalFlag::Zero, std::move(zerop));
  348. LOG_WARNING(HW_GPU, "Condition codes implementation is incomplete");
  349. }
  350. Node ShaderIR::BitfieldExtract(Node value, u32 offset, u32 bits) {
  351. return Operation(OperationCode::UBitfieldExtract, NO_PRECISE, std::move(value),
  352. Immediate(offset), Immediate(bits));
  353. }
  354. Node ShaderIR::BitfieldInsert(Node base, Node insert, u32 offset, u32 bits) {
  355. return Operation(OperationCode::UBitfieldInsert, NO_PRECISE, base, insert, Immediate(offset),
  356. Immediate(bits));
  357. }
  358. void ShaderIR::MarkAttributeUsage(Attribute::Index index, u64 element) {
  359. switch (index) {
  360. case Attribute::Index::LayerViewportPointSize:
  361. switch (element) {
  362. case 0:
  363. UNIMPLEMENTED();
  364. break;
  365. case 1:
  366. uses_layer = true;
  367. break;
  368. case 2:
  369. uses_viewport_index = true;
  370. break;
  371. case 3:
  372. uses_point_size = true;
  373. break;
  374. }
  375. break;
  376. case Attribute::Index::TessCoordInstanceIDVertexID:
  377. switch (element) {
  378. case 2:
  379. uses_instance_id = true;
  380. break;
  381. case 3:
  382. uses_vertex_id = true;
  383. break;
  384. }
  385. break;
  386. case Attribute::Index::ClipDistances0123:
  387. case Attribute::Index::ClipDistances4567: {
  388. const u64 clip_index = (index == Attribute::Index::ClipDistances4567 ? 4 : 0) + element;
  389. used_clip_distances.at(clip_index) = true;
  390. break;
  391. }
  392. case Attribute::Index::FrontColor:
  393. case Attribute::Index::FrontSecondaryColor:
  394. case Attribute::Index::BackColor:
  395. case Attribute::Index::BackSecondaryColor:
  396. uses_legacy_varyings = true;
  397. break;
  398. default:
  399. if (index >= Attribute::Index::TexCoord_0 && index <= Attribute::Index::TexCoord_7) {
  400. uses_legacy_varyings = true;
  401. }
  402. break;
  403. }
  404. }
  405. std::size_t ShaderIR::DeclareAmend(Node new_amend) {
  406. const std::size_t id = amend_code.size();
  407. amend_code.push_back(new_amend);
  408. return id;
  409. }
  410. u32 ShaderIR::NewCustomVariable() {
  411. return num_custom_variables++;
  412. }
  413. } // namespace VideoCommon::Shader