memory.cpp 12 KB

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