memory.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/node_helper.h"
  12. #include "video_core/shader/shader_ir.h"
  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 GetLdgMemorySize(Tegra::Shader::UniformType uniform_type) {
  20. switch (uniform_type) {
  21. case Tegra::Shader::UniformType::UnsignedByte:
  22. case Tegra::Shader::UniformType::Single:
  23. return 1;
  24. case Tegra::Shader::UniformType::Double:
  25. return 2;
  26. case Tegra::Shader::UniformType::Quad:
  27. case Tegra::Shader::UniformType::UnsignedQuad:
  28. return 4;
  29. default:
  30. UNIMPLEMENTED_MSG("Unimplemented size={}!", static_cast<u32>(uniform_type));
  31. return 1;
  32. }
  33. }
  34. u32 GetStgMemorySize(Tegra::Shader::UniformType uniform_type) {
  35. switch (uniform_type) {
  36. case Tegra::Shader::UniformType::Single:
  37. return 1;
  38. case Tegra::Shader::UniformType::Double:
  39. return 2;
  40. case Tegra::Shader::UniformType::Quad:
  41. case Tegra::Shader::UniformType::UnsignedQuad:
  42. return 4;
  43. default:
  44. UNIMPLEMENTED_MSG("Unimplemented size={}!", static_cast<u32>(uniform_type));
  45. return 1;
  46. }
  47. }
  48. } // Anonymous namespace
  49. u32 ShaderIR::DecodeMemory(NodeBlock& bb, u32 pc) {
  50. const Instruction instr = {program_code[pc]};
  51. const auto opcode = OpCode::Decode(instr);
  52. switch (opcode->get().GetId()) {
  53. case OpCode::Id::LD_A: {
  54. // Note: Shouldn't this be interp mode flat? As in no interpolation made.
  55. UNIMPLEMENTED_IF_MSG(instr.gpr8.Value() != Register::ZeroIndex,
  56. "Indirect attribute loads are not supported");
  57. UNIMPLEMENTED_IF_MSG((instr.attribute.fmt20.immediate.Value() % sizeof(u32)) != 0,
  58. "Unaligned attribute loads are not supported");
  59. UNIMPLEMENTED_IF_MSG(instr.attribute.fmt20.IsPhysical() &&
  60. instr.attribute.fmt20.size != Tegra::Shader::AttributeSize::Word,
  61. "Non-32 bits PHYS reads are not implemented");
  62. const Node buffer{GetRegister(instr.gpr39)};
  63. u64 next_element = instr.attribute.fmt20.element;
  64. auto next_index = static_cast<u64>(instr.attribute.fmt20.index.Value());
  65. const auto LoadNextElement = [&](u32 reg_offset) {
  66. const Node attribute{instr.attribute.fmt20.IsPhysical()
  67. ? GetPhysicalInputAttribute(instr.gpr8, buffer)
  68. : GetInputAttribute(static_cast<Attribute::Index>(next_index),
  69. next_element, buffer)};
  70. SetRegister(bb, instr.gpr0.Value() + reg_offset, attribute);
  71. // Load the next attribute element into the following register. If the element
  72. // to load goes beyond the vec4 size, load the first element of the next
  73. // attribute.
  74. next_element = (next_element + 1) % 4;
  75. next_index = next_index + (next_element == 0 ? 1 : 0);
  76. };
  77. const u32 num_words = static_cast<u32>(instr.attribute.fmt20.size.Value()) + 1;
  78. for (u32 reg_offset = 0; reg_offset < num_words; ++reg_offset) {
  79. LoadNextElement(reg_offset);
  80. }
  81. break;
  82. }
  83. case OpCode::Id::LD_C: {
  84. UNIMPLEMENTED_IF(instr.ld_c.unknown != 0);
  85. Node index = GetRegister(instr.gpr8);
  86. const Node op_a =
  87. GetConstBufferIndirect(instr.cbuf36.index, instr.cbuf36.GetOffset() + 0, index);
  88. switch (instr.ld_c.type.Value()) {
  89. case Tegra::Shader::UniformType::Single:
  90. SetRegister(bb, instr.gpr0, op_a);
  91. break;
  92. case Tegra::Shader::UniformType::Double: {
  93. const Node op_b =
  94. GetConstBufferIndirect(instr.cbuf36.index, instr.cbuf36.GetOffset() + 4, index);
  95. SetTemporary(bb, 0, op_a);
  96. SetTemporary(bb, 1, op_b);
  97. SetRegister(bb, instr.gpr0, GetTemporary(0));
  98. SetRegister(bb, instr.gpr0.Value() + 1, GetTemporary(1));
  99. break;
  100. }
  101. default:
  102. UNIMPLEMENTED_MSG("Unhandled type: {}", static_cast<unsigned>(instr.ld_c.type.Value()));
  103. }
  104. break;
  105. }
  106. case OpCode::Id::LD_L:
  107. LOG_DEBUG(HW_GPU, "LD_L cache management mode: {}", static_cast<u64>(instr.ld_l.unknown));
  108. [[fallthrough]];
  109. case OpCode::Id::LD_S: {
  110. const auto GetMemory = [&](s32 offset) {
  111. ASSERT(offset % 4 == 0);
  112. const Node immediate_offset = Immediate(static_cast<s32>(instr.smem_imm) + offset);
  113. const Node address = Operation(OperationCode::IAdd, NO_PRECISE, GetRegister(instr.gpr8),
  114. immediate_offset);
  115. return opcode->get().GetId() == OpCode::Id::LD_S ? GetSharedMemory(address)
  116. : GetLocalMemory(address);
  117. };
  118. switch (instr.ldst_sl.type.Value()) {
  119. case Tegra::Shader::StoreType::Bits32:
  120. case Tegra::Shader::StoreType::Bits64:
  121. case Tegra::Shader::StoreType::Bits128: {
  122. const u32 count = [&]() {
  123. switch (instr.ldst_sl.type.Value()) {
  124. case Tegra::Shader::StoreType::Bits32:
  125. return 1;
  126. case Tegra::Shader::StoreType::Bits64:
  127. return 2;
  128. case Tegra::Shader::StoreType::Bits128:
  129. return 4;
  130. default:
  131. UNREACHABLE();
  132. return 0;
  133. }
  134. }();
  135. for (u32 i = 0; i < count; ++i) {
  136. SetTemporary(bb, i, GetMemory(i * 4));
  137. }
  138. for (u32 i = 0; i < count; ++i) {
  139. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  140. }
  141. break;
  142. }
  143. default:
  144. UNIMPLEMENTED_MSG("{} Unhandled type: {}", opcode->get().GetName(),
  145. static_cast<u32>(instr.ldst_sl.type.Value()));
  146. }
  147. break;
  148. }
  149. case OpCode::Id::LD:
  150. case OpCode::Id::LDG: {
  151. const auto type = [instr, &opcode]() -> Tegra::Shader::UniformType {
  152. switch (opcode->get().GetId()) {
  153. case OpCode::Id::LD:
  154. UNIMPLEMENTED_IF_MSG(!instr.generic.extended, "Unextended LD is not implemented");
  155. return instr.generic.type;
  156. case OpCode::Id::LDG:
  157. return instr.ldg.type;
  158. default:
  159. UNREACHABLE();
  160. return {};
  161. }
  162. }();
  163. const auto [real_address_base, base_address, descriptor] =
  164. TrackGlobalMemory(bb, instr, false);
  165. const u32 count = GetLdgMemorySize(type);
  166. if (!real_address_base || !base_address) {
  167. // Tracking failed, load zeroes.
  168. for (u32 i = 0; i < count; ++i) {
  169. SetRegister(bb, instr.gpr0.Value() + i, Immediate(0.0f));
  170. }
  171. break;
  172. }
  173. for (u32 i = 0; i < count; ++i) {
  174. const Node it_offset = Immediate(i * 4);
  175. const Node real_address = Operation(OperationCode::UAdd, real_address_base, it_offset);
  176. Node gmem = MakeNode<GmemNode>(real_address, base_address, descriptor);
  177. if (type == Tegra::Shader::UniformType::UnsignedByte) {
  178. // To handle unaligned loads get the byte used to dereferenced global memory
  179. // and extract that byte from the loaded uint32.
  180. Node byte = Operation(OperationCode::UBitwiseAnd, real_address, Immediate(3));
  181. byte = Operation(OperationCode::ULogicalShiftLeft, std::move(byte), Immediate(3));
  182. gmem = Operation(OperationCode::UBitfieldExtract, std::move(gmem), std::move(byte),
  183. Immediate(8));
  184. }
  185. SetTemporary(bb, i, gmem);
  186. }
  187. for (u32 i = 0; i < count; ++i) {
  188. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  189. }
  190. break;
  191. }
  192. case OpCode::Id::ST_A: {
  193. UNIMPLEMENTED_IF_MSG(instr.gpr8.Value() != Register::ZeroIndex,
  194. "Indirect attribute loads are not supported");
  195. UNIMPLEMENTED_IF_MSG((instr.attribute.fmt20.immediate.Value() % sizeof(u32)) != 0,
  196. "Unaligned attribute loads are not supported");
  197. u64 element = instr.attribute.fmt20.element;
  198. auto index = static_cast<u64>(instr.attribute.fmt20.index.Value());
  199. const u32 num_words = static_cast<u32>(instr.attribute.fmt20.size.Value()) + 1;
  200. for (u32 reg_offset = 0; reg_offset < num_words; ++reg_offset) {
  201. Node dest;
  202. if (instr.attribute.fmt20.patch) {
  203. const u32 offset = static_cast<u32>(index) * 4 + static_cast<u32>(element);
  204. dest = MakeNode<PatchNode>(offset);
  205. } else {
  206. dest = GetOutputAttribute(static_cast<Attribute::Index>(index), element,
  207. GetRegister(instr.gpr39));
  208. }
  209. const auto src = GetRegister(instr.gpr0.Value() + reg_offset);
  210. bb.push_back(Operation(OperationCode::Assign, dest, src));
  211. // Load the next attribute element into the following register. If the element to load
  212. // goes beyond the vec4 size, load the first element of the next attribute.
  213. element = (element + 1) % 4;
  214. index = index + (element == 0 ? 1 : 0);
  215. }
  216. break;
  217. }
  218. case OpCode::Id::ST_L:
  219. LOG_DEBUG(HW_GPU, "ST_L cache management mode: {}",
  220. static_cast<u64>(instr.st_l.cache_management.Value()));
  221. [[fallthrough]];
  222. case OpCode::Id::ST_S: {
  223. const auto GetAddress = [&](s32 offset) {
  224. ASSERT(offset % 4 == 0);
  225. const Node immediate = Immediate(static_cast<s32>(instr.smem_imm) + offset);
  226. return Operation(OperationCode::IAdd, NO_PRECISE, GetRegister(instr.gpr8), immediate);
  227. };
  228. const auto set_memory = opcode->get().GetId() == OpCode::Id::ST_L
  229. ? &ShaderIR::SetLocalMemory
  230. : &ShaderIR::SetSharedMemory;
  231. switch (instr.ldst_sl.type.Value()) {
  232. case Tegra::Shader::StoreType::Bits128:
  233. (this->*set_memory)(bb, GetAddress(12), GetRegister(instr.gpr0.Value() + 3));
  234. (this->*set_memory)(bb, GetAddress(8), GetRegister(instr.gpr0.Value() + 2));
  235. [[fallthrough]];
  236. case Tegra::Shader::StoreType::Bits64:
  237. (this->*set_memory)(bb, GetAddress(4), GetRegister(instr.gpr0.Value() + 1));
  238. [[fallthrough]];
  239. case Tegra::Shader::StoreType::Bits32:
  240. (this->*set_memory)(bb, GetAddress(0), GetRegister(instr.gpr0));
  241. break;
  242. default:
  243. UNIMPLEMENTED_MSG("{} unhandled type: {}", opcode->get().GetName(),
  244. static_cast<u32>(instr.ldst_sl.type.Value()));
  245. }
  246. break;
  247. }
  248. case OpCode::Id::ST:
  249. case OpCode::Id::STG: {
  250. const auto type = [instr, &opcode]() -> Tegra::Shader::UniformType {
  251. switch (opcode->get().GetId()) {
  252. case OpCode::Id::ST:
  253. UNIMPLEMENTED_IF_MSG(!instr.generic.extended, "Unextended ST is not implemented");
  254. return instr.generic.type;
  255. case OpCode::Id::STG:
  256. return instr.stg.type;
  257. default:
  258. UNREACHABLE();
  259. return {};
  260. }
  261. }();
  262. const auto [real_address_base, base_address, descriptor] =
  263. TrackGlobalMemory(bb, instr, true);
  264. if (!real_address_base || !base_address) {
  265. // Tracking failed, skip the store.
  266. break;
  267. }
  268. const u32 count = GetStgMemorySize(type);
  269. for (u32 i = 0; i < count; ++i) {
  270. const Node it_offset = Immediate(i * 4);
  271. const Node real_address = Operation(OperationCode::UAdd, real_address_base, it_offset);
  272. const Node gmem = MakeNode<GmemNode>(real_address, base_address, descriptor);
  273. const Node value = GetRegister(instr.gpr0.Value() + i);
  274. bb.push_back(Operation(OperationCode::Assign, gmem, value));
  275. }
  276. break;
  277. }
  278. case OpCode::Id::AL2P: {
  279. // Ignore al2p.direction since we don't care about it.
  280. // Calculate emulation fake physical address.
  281. const Node fixed_address{Immediate(static_cast<u32>(instr.al2p.address))};
  282. const Node reg{GetRegister(instr.gpr8)};
  283. const Node fake_address{Operation(OperationCode::IAdd, NO_PRECISE, reg, fixed_address)};
  284. // Set the fake address to target register.
  285. SetRegister(bb, instr.gpr0, fake_address);
  286. // Signal the shader IR to declare all possible attributes and varyings
  287. uses_physical_attributes = true;
  288. break;
  289. }
  290. default:
  291. UNIMPLEMENTED_MSG("Unhandled memory instruction: {}", opcode->get().GetName());
  292. }
  293. return pc;
  294. }
  295. std::tuple<Node, Node, GlobalMemoryBase> ShaderIR::TrackGlobalMemory(NodeBlock& bb,
  296. Instruction instr,
  297. bool is_write) {
  298. const auto addr_register{GetRegister(instr.gmem.gpr)};
  299. const auto immediate_offset{static_cast<u32>(instr.gmem.offset)};
  300. const auto [base_address, index, offset] =
  301. TrackCbuf(addr_register, global_code, static_cast<s64>(global_code.size()));
  302. ASSERT_OR_EXECUTE_MSG(base_address != nullptr,
  303. { return std::make_tuple(nullptr, nullptr, GlobalMemoryBase{}); },
  304. "Global memory tracking failed");
  305. bb.push_back(Comment(fmt::format("Base address is c[0x{:x}][0x{:x}]", index, offset)));
  306. const GlobalMemoryBase descriptor{index, offset};
  307. const auto& [entry, is_new] = used_global_memory.try_emplace(descriptor);
  308. auto& usage = entry->second;
  309. if (is_write) {
  310. usage.is_written = true;
  311. } else {
  312. usage.is_read = true;
  313. }
  314. const auto real_address =
  315. Operation(OperationCode::UAdd, NO_PRECISE, Immediate(immediate_offset), addr_register);
  316. return {real_address, base_address, descriptor};
  317. }
  318. } // namespace VideoCommon::Shader