shader_ir.cpp 17 KB

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