memory.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <vector>
  6. #include <fmt/format.h>
  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/shader_ir.h"
  12. namespace VideoCommon::Shader {
  13. using Tegra::Shader::Attribute;
  14. using Tegra::Shader::Instruction;
  15. using Tegra::Shader::OpCode;
  16. using Tegra::Shader::Register;
  17. namespace {
  18. u32 GetUniformTypeElementsCount(Tegra::Shader::UniformType uniform_type) {
  19. switch (uniform_type) {
  20. case Tegra::Shader::UniformType::Single:
  21. return 1;
  22. case Tegra::Shader::UniformType::Double:
  23. return 2;
  24. case Tegra::Shader::UniformType::Quad:
  25. case Tegra::Shader::UniformType::UnsignedQuad:
  26. return 4;
  27. default:
  28. UNIMPLEMENTED_MSG("Unimplemented size={}!", static_cast<u32>(uniform_type));
  29. return 1;
  30. }
  31. }
  32. } // namespace
  33. u32 ShaderIR::DecodeMemory(NodeBlock& bb, u32 pc) {
  34. const Instruction instr = {program_code[pc]};
  35. const auto opcode = OpCode::Decode(instr);
  36. switch (opcode->get().GetId()) {
  37. case OpCode::Id::LD_A: {
  38. // Note: Shouldn't this be interp mode flat? As in no interpolation made.
  39. UNIMPLEMENTED_IF_MSG(instr.gpr8.Value() != Register::ZeroIndex,
  40. "Indirect attribute loads are not supported");
  41. UNIMPLEMENTED_IF_MSG((instr.attribute.fmt20.immediate.Value() % sizeof(u32)) != 0,
  42. "Unaligned attribute loads are not supported");
  43. Tegra::Shader::IpaMode input_mode{Tegra::Shader::IpaInterpMode::Pass,
  44. Tegra::Shader::IpaSampleMode::Default};
  45. u64 next_element = instr.attribute.fmt20.element;
  46. auto next_index = static_cast<u64>(instr.attribute.fmt20.index.Value());
  47. const auto LoadNextElement = [&](u32 reg_offset) {
  48. const Node buffer = GetRegister(instr.gpr39);
  49. const Node attribute = GetInputAttribute(static_cast<Attribute::Index>(next_index),
  50. next_element, input_mode, buffer);
  51. SetRegister(bb, instr.gpr0.Value() + reg_offset, attribute);
  52. // Load the next attribute element into the following register. If the element
  53. // to load goes beyond the vec4 size, load the first element of the next
  54. // attribute.
  55. next_element = (next_element + 1) % 4;
  56. next_index = next_index + (next_element == 0 ? 1 : 0);
  57. };
  58. const u32 num_words = static_cast<u32>(instr.attribute.fmt20.size.Value()) + 1;
  59. for (u32 reg_offset = 0; reg_offset < num_words; ++reg_offset) {
  60. LoadNextElement(reg_offset);
  61. }
  62. break;
  63. }
  64. case OpCode::Id::LD_C: {
  65. UNIMPLEMENTED_IF(instr.ld_c.unknown != 0);
  66. Node index = GetRegister(instr.gpr8);
  67. const Node op_a =
  68. GetConstBufferIndirect(instr.cbuf36.index, instr.cbuf36.GetOffset() + 0, index);
  69. switch (instr.ld_c.type.Value()) {
  70. case Tegra::Shader::UniformType::Single:
  71. SetRegister(bb, instr.gpr0, op_a);
  72. break;
  73. case Tegra::Shader::UniformType::Double: {
  74. const Node op_b =
  75. GetConstBufferIndirect(instr.cbuf36.index, instr.cbuf36.GetOffset() + 4, index);
  76. SetTemporal(bb, 0, op_a);
  77. SetTemporal(bb, 1, op_b);
  78. SetRegister(bb, instr.gpr0, GetTemporal(0));
  79. SetRegister(bb, instr.gpr0.Value() + 1, GetTemporal(1));
  80. break;
  81. }
  82. default:
  83. UNIMPLEMENTED_MSG("Unhandled type: {}", static_cast<unsigned>(instr.ld_c.type.Value()));
  84. }
  85. break;
  86. }
  87. case OpCode::Id::LD_L: {
  88. LOG_DEBUG(HW_GPU, "LD_L cache management mode: {}",
  89. static_cast<u64>(instr.ld_l.unknown.Value()));
  90. const auto GetLmem = [&](s32 offset) {
  91. ASSERT(offset % 4 == 0);
  92. const Node immediate_offset = Immediate(static_cast<s32>(instr.smem_imm) + offset);
  93. const Node address = Operation(OperationCode::IAdd, NO_PRECISE, GetRegister(instr.gpr8),
  94. immediate_offset);
  95. return GetLocalMemory(address);
  96. };
  97. switch (instr.ldst_sl.type.Value()) {
  98. case Tegra::Shader::StoreType::Bits32:
  99. case Tegra::Shader::StoreType::Bits64:
  100. case Tegra::Shader::StoreType::Bits128: {
  101. const u32 count = [&]() {
  102. switch (instr.ldst_sl.type.Value()) {
  103. case Tegra::Shader::StoreType::Bits32:
  104. return 1;
  105. case Tegra::Shader::StoreType::Bits64:
  106. return 2;
  107. case Tegra::Shader::StoreType::Bits128:
  108. return 4;
  109. default:
  110. UNREACHABLE();
  111. return 0;
  112. }
  113. }();
  114. for (u32 i = 0; i < count; ++i)
  115. SetTemporal(bb, i, GetLmem(i * 4));
  116. for (u32 i = 0; i < count; ++i)
  117. SetRegister(bb, instr.gpr0.Value() + i, GetTemporal(i));
  118. break;
  119. }
  120. default:
  121. UNIMPLEMENTED_MSG("LD_L Unhandled type: {}",
  122. static_cast<u32>(instr.ldst_sl.type.Value()));
  123. }
  124. break;
  125. }
  126. case OpCode::Id::LDG: {
  127. const auto [real_address_base, base_address, descriptor] =
  128. TrackAndGetGlobalMemory(bb, GetRegister(instr.gpr8),
  129. static_cast<u32>(instr.ldg.immediate_offset.Value()), false);
  130. const u32 count = GetUniformTypeElementsCount(instr.ldg.type);
  131. for (u32 i = 0; i < count; ++i) {
  132. const Node it_offset = Immediate(i * 4);
  133. const Node real_address =
  134. Operation(OperationCode::UAdd, NO_PRECISE, real_address_base, it_offset);
  135. const Node gmem = StoreNode(GmemNode(real_address, base_address, descriptor));
  136. SetTemporal(bb, i, gmem);
  137. }
  138. for (u32 i = 0; i < count; ++i) {
  139. SetRegister(bb, instr.gpr0.Value() + i, GetTemporal(i));
  140. }
  141. break;
  142. }
  143. case OpCode::Id::STG: {
  144. const auto [real_address_base, base_address, descriptor] =
  145. TrackAndGetGlobalMemory(bb, GetRegister(instr.gpr8),
  146. static_cast<u32>(instr.stg.immediate_offset.Value()), true);
  147. // Encode in temporary registers like this: real_base_address, {registers_to_be_written...}
  148. SetTemporal(bb, 0, real_address_base);
  149. const u32 count = GetUniformTypeElementsCount(instr.stg.type);
  150. for (u32 i = 0; i < count; ++i) {
  151. SetTemporal(bb, i + 1, GetRegister(instr.gpr0.Value() + i));
  152. }
  153. for (u32 i = 0; i < count; ++i) {
  154. const Node it_offset = Immediate(i * 4);
  155. const Node real_address =
  156. Operation(OperationCode::UAdd, NO_PRECISE, real_address_base, it_offset);
  157. const Node gmem = StoreNode(GmemNode(real_address, base_address, descriptor));
  158. bb.push_back(Operation(OperationCode::Assign, gmem, GetTemporal(i + 1)));
  159. }
  160. break;
  161. }
  162. case OpCode::Id::ST_A: {
  163. UNIMPLEMENTED_IF_MSG(instr.gpr8.Value() != Register::ZeroIndex,
  164. "Indirect attribute loads are not supported");
  165. UNIMPLEMENTED_IF_MSG((instr.attribute.fmt20.immediate.Value() % sizeof(u32)) != 0,
  166. "Unaligned attribute loads are not supported");
  167. u64 next_element = instr.attribute.fmt20.element;
  168. auto next_index = static_cast<u64>(instr.attribute.fmt20.index.Value());
  169. const auto StoreNextElement = [&](u32 reg_offset) {
  170. const auto dest = GetOutputAttribute(static_cast<Attribute::Index>(next_index),
  171. next_element, GetRegister(instr.gpr39));
  172. const auto src = GetRegister(instr.gpr0.Value() + reg_offset);
  173. bb.push_back(Operation(OperationCode::Assign, dest, src));
  174. // Load the next attribute element into the following register. If the element
  175. // to load goes beyond the vec4 size, load the first element of the next
  176. // attribute.
  177. next_element = (next_element + 1) % 4;
  178. next_index = next_index + (next_element == 0 ? 1 : 0);
  179. };
  180. const u32 num_words = static_cast<u32>(instr.attribute.fmt20.size.Value()) + 1;
  181. for (u32 reg_offset = 0; reg_offset < num_words; ++reg_offset) {
  182. StoreNextElement(reg_offset);
  183. }
  184. break;
  185. }
  186. case OpCode::Id::ST_L: {
  187. LOG_DEBUG(HW_GPU, "ST_L cache management mode: {}",
  188. static_cast<u64>(instr.st_l.cache_management.Value()));
  189. const auto GetLmemAddr = [&](s32 offset) {
  190. ASSERT(offset % 4 == 0);
  191. const Node immediate = Immediate(static_cast<s32>(instr.smem_imm) + offset);
  192. return Operation(OperationCode::IAdd, NO_PRECISE, GetRegister(instr.gpr8), immediate);
  193. };
  194. switch (instr.ldst_sl.type.Value()) {
  195. case Tegra::Shader::StoreType::Bits128:
  196. SetLocalMemory(bb, GetLmemAddr(12), GetRegister(instr.gpr0.Value() + 3));
  197. SetLocalMemory(bb, GetLmemAddr(8), GetRegister(instr.gpr0.Value() + 2));
  198. case Tegra::Shader::StoreType::Bits64:
  199. SetLocalMemory(bb, GetLmemAddr(4), GetRegister(instr.gpr0.Value() + 1));
  200. case Tegra::Shader::StoreType::Bits32:
  201. SetLocalMemory(bb, GetLmemAddr(0), GetRegister(instr.gpr0));
  202. break;
  203. default:
  204. UNIMPLEMENTED_MSG("ST_L Unhandled type: {}",
  205. static_cast<u32>(instr.ldst_sl.type.Value()));
  206. }
  207. break;
  208. }
  209. default:
  210. UNIMPLEMENTED_MSG("Unhandled memory instruction: {}", opcode->get().GetName());
  211. }
  212. return pc;
  213. }
  214. std::tuple<Node, Node, GlobalMemoryBase> ShaderIR::TrackAndGetGlobalMemory(NodeBlock& bb,
  215. Node addr_register,
  216. u32 immediate_offset,
  217. bool is_write) {
  218. const Node base_address{
  219. TrackCbuf(addr_register, global_code, static_cast<s64>(global_code.size()))};
  220. const auto cbuf = std::get_if<CbufNode>(base_address);
  221. ASSERT(cbuf != nullptr);
  222. const auto cbuf_offset_imm = std::get_if<ImmediateNode>(cbuf->GetOffset());
  223. ASSERT(cbuf_offset_imm != nullptr);
  224. const auto cbuf_offset = cbuf_offset_imm->GetValue();
  225. bb.push_back(
  226. Comment(fmt::format("Base address is c[0x{:x}][0x{:x}]", cbuf->GetIndex(), cbuf_offset)));
  227. const GlobalMemoryBase descriptor{cbuf->GetIndex(), cbuf_offset};
  228. const auto& [entry, is_new] = used_global_memory.try_emplace(descriptor);
  229. auto& usage = entry->second;
  230. if (is_write) {
  231. usage.is_written = true;
  232. } else {
  233. usage.is_read = true;
  234. }
  235. const auto real_address =
  236. Operation(OperationCode::UAdd, NO_PRECISE, Immediate(immediate_offset), addr_register);
  237. return {real_address, base_address, descriptor};
  238. }
  239. } // namespace VideoCommon::Shader