memory.cpp 13 KB

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