memory.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 "common/assert.h"
  7. #include "common/common_types.h"
  8. #include "video_core/engines/shader_bytecode.h"
  9. #include "video_core/shader/shader_ir.h"
  10. namespace VideoCommon::Shader {
  11. using Tegra::Shader::Attribute;
  12. using Tegra::Shader::Instruction;
  13. using Tegra::Shader::OpCode;
  14. using Tegra::Shader::Register;
  15. using Tegra::Shader::TextureMiscMode;
  16. using Tegra::Shader::TextureProcessMode;
  17. using Tegra::Shader::TextureType;
  18. static std::size_t GetCoordCount(TextureType texture_type) {
  19. switch (texture_type) {
  20. case TextureType::Texture1D:
  21. return 1;
  22. case TextureType::Texture2D:
  23. return 2;
  24. case TextureType::Texture3D:
  25. case TextureType::TextureCube:
  26. return 3;
  27. default:
  28. UNIMPLEMENTED_MSG("Unhandled texture type: {}", static_cast<u32>(texture_type));
  29. return 0;
  30. }
  31. }
  32. u32 ShaderIR::DecodeMemory(BasicBlock& bb, u32 pc) {
  33. const Instruction instr = {program_code[pc]};
  34. const auto opcode = OpCode::Decode(instr);
  35. switch (opcode->get().GetId()) {
  36. case OpCode::Id::LD_A: {
  37. // Note: Shouldn't this be interp mode flat? As in no interpolation made.
  38. UNIMPLEMENTED_IF_MSG(instr.gpr8.Value() != Register::ZeroIndex,
  39. "Indirect attribute loads are not supported");
  40. UNIMPLEMENTED_IF_MSG((instr.attribute.fmt20.immediate.Value() % sizeof(u32)) != 0,
  41. "Unaligned attribute loads are not supported");
  42. Tegra::Shader::IpaMode input_mode{Tegra::Shader::IpaInterpMode::Perspective,
  43. Tegra::Shader::IpaSampleMode::Default};
  44. u64 next_element = instr.attribute.fmt20.element;
  45. auto next_index = static_cast<u64>(instr.attribute.fmt20.index.Value());
  46. const auto LoadNextElement = [&](u32 reg_offset) {
  47. const Node buffer = GetRegister(instr.gpr39);
  48. const Node attribute = GetInputAttribute(static_cast<Attribute::Index>(next_index),
  49. next_element, input_mode, buffer);
  50. SetRegister(bb, instr.gpr0.Value() + reg_offset, attribute);
  51. // Load the next attribute element into the following register. If the element
  52. // to load goes beyond the vec4 size, load the first element of the next
  53. // attribute.
  54. next_element = (next_element + 1) % 4;
  55. next_index = next_index + (next_element == 0 ? 1 : 0);
  56. };
  57. const u32 num_words = static_cast<u32>(instr.attribute.fmt20.size.Value()) + 1;
  58. for (u32 reg_offset = 0; reg_offset < num_words; ++reg_offset) {
  59. LoadNextElement(reg_offset);
  60. }
  61. break;
  62. }
  63. case OpCode::Id::LD_C: {
  64. UNIMPLEMENTED_IF(instr.ld_c.unknown != 0);
  65. Node index = GetRegister(instr.gpr8);
  66. const Node op_a =
  67. GetConstBufferIndirect(instr.cbuf36.index, instr.cbuf36.offset + 0, index);
  68. switch (instr.ld_c.type.Value()) {
  69. case Tegra::Shader::UniformType::Single:
  70. SetRegister(bb, instr.gpr0, op_a);
  71. break;
  72. case Tegra::Shader::UniformType::Double: {
  73. const Node op_b =
  74. GetConstBufferIndirect(instr.cbuf36.index, instr.cbuf36.offset + 4, index);
  75. const Node composite =
  76. Operation(OperationCode::Composite, op_a, op_b, GetRegister(RZ), GetRegister(RZ));
  77. MetaComponents meta{{0, 1, 2, 3}};
  78. bb.push_back(Operation(OperationCode::AssignComposite, meta, composite,
  79. GetRegister(instr.gpr0), GetRegister(instr.gpr0.Value() + 1),
  80. GetRegister(RZ), GetRegister(RZ)));
  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. UNIMPLEMENTED_IF_MSG(instr.ld_l.unknown == 1, "LD_L Unhandled mode: {}",
  90. static_cast<unsigned>(instr.ld_l.unknown.Value()));
  91. const Node index = Operation(OperationCode::IAdd, GetRegister(instr.gpr8),
  92. Immediate(static_cast<s32>(instr.smem_imm)));
  93. const Node lmem = GetLocalMemory(index);
  94. switch (instr.ldst_sl.type.Value()) {
  95. case Tegra::Shader::StoreType::Bytes32:
  96. SetRegister(bb, instr.gpr0, lmem);
  97. break;
  98. default:
  99. UNIMPLEMENTED_MSG("LD_L Unhandled type: {}",
  100. static_cast<unsigned>(instr.ldst_sl.type.Value()));
  101. }
  102. break;
  103. }
  104. case OpCode::Id::ST_A: {
  105. UNIMPLEMENTED_IF_MSG(instr.gpr8.Value() != Register::ZeroIndex,
  106. "Indirect attribute loads are not supported");
  107. UNIMPLEMENTED_IF_MSG((instr.attribute.fmt20.immediate.Value() % sizeof(u32)) != 0,
  108. "Unaligned attribute loads are not supported");
  109. u64 next_element = instr.attribute.fmt20.element;
  110. auto next_index = static_cast<u64>(instr.attribute.fmt20.index.Value());
  111. const auto StoreNextElement = [&](u32 reg_offset) {
  112. const auto dest = GetOutputAttribute(static_cast<Attribute::Index>(next_index),
  113. next_element, GetRegister(instr.gpr39));
  114. const auto src = GetRegister(instr.gpr0.Value() + reg_offset);
  115. bb.push_back(Operation(OperationCode::Assign, dest, src));
  116. // Load the next attribute element into the following register. If the element
  117. // to load goes beyond the vec4 size, load the first element of the next
  118. // attribute.
  119. next_element = (next_element + 1) % 4;
  120. next_index = next_index + (next_element == 0 ? 1 : 0);
  121. };
  122. const u32 num_words = static_cast<u32>(instr.attribute.fmt20.size.Value()) + 1;
  123. for (u32 reg_offset = 0; reg_offset < num_words; ++reg_offset) {
  124. StoreNextElement(reg_offset);
  125. }
  126. break;
  127. }
  128. case OpCode::Id::ST_L: {
  129. // UNIMPLEMENTED_IF_MSG(instr.st_l.unknown == 0, "ST_L Unhandled mode: {}",
  130. // static_cast<u32>(instr.st_l.unknown.Value()));
  131. const Node index = Operation(OperationCode::IAdd, NO_PRECISE, GetRegister(instr.gpr8),
  132. Immediate(static_cast<s32>(instr.smem_imm)));
  133. switch (instr.ldst_sl.type.Value()) {
  134. case Tegra::Shader::StoreType::Bytes32:
  135. SetLocalMemory(bb, index, GetRegister(instr.gpr0));
  136. break;
  137. default:
  138. UNIMPLEMENTED_MSG("ST_L Unhandled type: {}",
  139. static_cast<u32>(instr.ldst_sl.type.Value()));
  140. }
  141. break;
  142. }
  143. case OpCode::Id::TEX: {
  144. Tegra::Shader::TextureType texture_type{instr.tex.texture_type};
  145. const bool is_array = instr.tex.array != 0;
  146. const bool depth_compare = instr.tex.UsesMiscMode(TextureMiscMode::DC);
  147. const auto process_mode = instr.tex.GetTextureProcessMode();
  148. UNIMPLEMENTED_IF_MSG(instr.tex.UsesMiscMode(TextureMiscMode::AOFFI),
  149. "AOFFI is not implemented");
  150. if (instr.tex.UsesMiscMode(TextureMiscMode::NODEP)) {
  151. LOG_WARNING(HW_GPU, "TEX.NODEP implementation is incomplete");
  152. }
  153. const Node texture = GetTexCode(instr, texture_type, process_mode, depth_compare, is_array);
  154. MetaComponents meta;
  155. std::array<Node, 4> dest;
  156. std::size_t dest_elem = 0;
  157. for (std::size_t elem = 0; elem < 4; ++elem) {
  158. if (!instr.tex.IsComponentEnabled(elem)) {
  159. // Skip disabled components
  160. continue;
  161. }
  162. meta.components_map[dest_elem] = static_cast<u32>(elem);
  163. dest[dest_elem] = GetRegister(instr.gpr0.Value() + dest_elem);
  164. ++dest_elem;
  165. }
  166. std::generate(dest.begin() + dest_elem, dest.end(), [&]() { return GetRegister(RZ); });
  167. bb.push_back(Operation(OperationCode::AssignComposite, std::move(meta), texture, dest[0],
  168. dest[1], dest[2], dest[3]));
  169. break;
  170. }
  171. case OpCode::Id::TEXS: {
  172. const TextureType texture_type{instr.texs.GetTextureType()};
  173. const bool is_array{instr.texs.IsArrayTexture()};
  174. const bool depth_compare = instr.texs.UsesMiscMode(TextureMiscMode::DC);
  175. const auto process_mode = instr.texs.GetTextureProcessMode();
  176. if (instr.texs.UsesMiscMode(TextureMiscMode::NODEP)) {
  177. LOG_WARNING(HW_GPU, "TEXS.NODEP implementation is incomplete");
  178. }
  179. const Node texture =
  180. GetTexsCode(instr, texture_type, process_mode, depth_compare, is_array);
  181. if (instr.texs.fp32_flag) {
  182. WriteTexsInstructionFloat(bb, instr, texture);
  183. } else {
  184. WriteTexsInstructionHalfFloat(bb, instr, texture);
  185. }
  186. break;
  187. }
  188. case OpCode::Id::TLD4: {
  189. ASSERT(instr.tld4.array == 0);
  190. UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(TextureMiscMode::AOFFI),
  191. "AOFFI is not implemented");
  192. UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(TextureMiscMode::NDV),
  193. "NDV is not implemented");
  194. UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(TextureMiscMode::PTP),
  195. "PTP is not implemented");
  196. if (instr.tld4.UsesMiscMode(TextureMiscMode::NODEP)) {
  197. LOG_WARNING(HW_GPU, "TLD4.NODEP implementation is incomplete");
  198. }
  199. const auto texture_type = instr.tld4.texture_type.Value();
  200. const bool depth_compare = instr.tld4.UsesMiscMode(TextureMiscMode::DC);
  201. const bool is_array = instr.tld4.array != 0;
  202. const Node texture = GetTld4Code(instr, texture_type, depth_compare, is_array);
  203. MetaComponents meta_components;
  204. std::array<Node, 4> dest;
  205. std::size_t dest_elem = 0;
  206. for (std::size_t elem = 0; elem < 4; ++elem) {
  207. if (!instr.tex.IsComponentEnabled(elem)) {
  208. // Skip disabled components
  209. continue;
  210. }
  211. meta_components.components_map[dest_elem] = static_cast<u32>(elem);
  212. dest[dest_elem] = GetRegister(instr.gpr0.Value() + dest_elem);
  213. ++dest_elem;
  214. }
  215. std::generate(dest.begin() + dest_elem, dest.end(), [&]() { return GetRegister(RZ); });
  216. bb.push_back(Operation(OperationCode::AssignComposite, std::move(meta_components), texture,
  217. dest[0], dest[1], dest[2], dest[3]));
  218. break;
  219. }
  220. case OpCode::Id::TLD4S: {
  221. UNIMPLEMENTED_IF_MSG(instr.tld4s.UsesMiscMode(TextureMiscMode::AOFFI),
  222. "AOFFI is not implemented");
  223. if (instr.tld4s.UsesMiscMode(TextureMiscMode::NODEP)) {
  224. LOG_WARNING(HW_GPU, "TLD4S.NODEP implementation is incomplete");
  225. }
  226. const bool depth_compare = instr.tld4s.UsesMiscMode(TextureMiscMode::DC);
  227. const Node op_a = GetRegister(instr.gpr8);
  228. const Node op_b = GetRegister(instr.gpr20);
  229. std::vector<Node> params;
  230. // TODO(Subv): Figure out how the sampler type is encoded in the TLD4S instruction.
  231. if (depth_compare) {
  232. // Note: TLD4S coordinate encoding works just like TEXS's
  233. const Node op_y = GetRegister(instr.gpr8.Value() + 1);
  234. params.push_back(op_a);
  235. params.push_back(op_y);
  236. params.push_back(op_b);
  237. } else {
  238. params.push_back(op_a);
  239. params.push_back(op_b);
  240. }
  241. const auto num_coords = static_cast<u32>(params.size());
  242. params.push_back(Immediate(static_cast<u32>(instr.tld4s.component)));
  243. const auto& sampler =
  244. GetSampler(instr.sampler, TextureType::Texture2D, false, depth_compare);
  245. MetaTexture meta{sampler, num_coords};
  246. WriteTexsInstructionFloat(
  247. bb, instr, Operation(OperationCode::F4TextureGather, meta, std::move(params)));
  248. break;
  249. }
  250. case OpCode::Id::TXQ: {
  251. if (instr.txq.UsesMiscMode(TextureMiscMode::NODEP)) {
  252. LOG_WARNING(HW_GPU, "TXQ.NODEP implementation is incomplete");
  253. }
  254. // TODO: The new commits on the texture refactor, change the way samplers work.
  255. // Sadly, not all texture instructions specify the type of texture their sampler
  256. // uses. This must be fixed at a later instance.
  257. const auto& sampler =
  258. GetSampler(instr.sampler, Tegra::Shader::TextureType::Texture2D, false, false);
  259. switch (instr.txq.query_type) {
  260. case Tegra::Shader::TextureQueryType::Dimension: {
  261. MetaTexture meta_texture{sampler};
  262. const MetaComponents meta_components{{0, 1, 2, 3}};
  263. const Node texture = Operation(OperationCode::F4TextureQueryDimensions, meta_texture,
  264. GetRegister(instr.gpr8));
  265. std::array<Node, 4> dest;
  266. for (std::size_t i = 0; i < dest.size(); ++i) {
  267. dest[i] = GetRegister(instr.gpr0.Value() + i);
  268. }
  269. bb.push_back(Operation(OperationCode::AssignComposite, meta_components, texture,
  270. dest[0], dest[1], dest[2], dest[3]));
  271. break;
  272. }
  273. default:
  274. UNIMPLEMENTED_MSG("Unhandled texture query type: {}",
  275. static_cast<u32>(instr.txq.query_type.Value()));
  276. }
  277. break;
  278. }
  279. case OpCode::Id::TMML: {
  280. UNIMPLEMENTED_IF_MSG(instr.tmml.UsesMiscMode(Tegra::Shader::TextureMiscMode::NDV),
  281. "NDV is not implemented");
  282. if (instr.tmml.UsesMiscMode(TextureMiscMode::NODEP)) {
  283. LOG_WARNING(HW_GPU, "TMML.NODEP implementation is incomplete");
  284. }
  285. auto texture_type = instr.tmml.texture_type.Value();
  286. const bool is_array = instr.tmml.array != 0;
  287. const auto& sampler = GetSampler(instr.sampler, texture_type, is_array, false);
  288. std::vector<Node> coords;
  289. // TODO: Add coordinates for different samplers once other texture types are implemented.
  290. switch (texture_type) {
  291. case TextureType::Texture1D:
  292. coords.push_back(GetRegister(instr.gpr8));
  293. break;
  294. case TextureType::Texture2D:
  295. coords.push_back(GetRegister(instr.gpr8.Value() + 0));
  296. coords.push_back(GetRegister(instr.gpr8.Value() + 1));
  297. break;
  298. default:
  299. UNIMPLEMENTED_MSG("Unhandled texture type {}", static_cast<u32>(texture_type));
  300. // Fallback to interpreting as a 2D texture for now
  301. coords.push_back(GetRegister(instr.gpr8.Value() + 0));
  302. coords.push_back(GetRegister(instr.gpr8.Value() + 1));
  303. texture_type = TextureType::Texture2D;
  304. }
  305. MetaTexture meta_texture{sampler, static_cast<u32>(coords.size())};
  306. const Node texture =
  307. Operation(OperationCode::F4TextureQueryLod, meta_texture, std::move(coords));
  308. const MetaComponents meta_composite{{0, 1, 2, 3}};
  309. bb.push_back(Operation(OperationCode::AssignComposite, meta_composite, texture,
  310. GetRegister(instr.gpr0), GetRegister(instr.gpr0.Value() + 1),
  311. GetRegister(RZ), GetRegister(RZ)));
  312. break;
  313. }
  314. case OpCode::Id::TLDS: {
  315. const Tegra::Shader::TextureType texture_type{instr.tlds.GetTextureType()};
  316. const bool is_array{instr.tlds.IsArrayTexture()};
  317. UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(TextureMiscMode::AOFFI),
  318. "AOFFI is not implemented");
  319. UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(TextureMiscMode::MZ), "MZ is not implemented");
  320. if (instr.tlds.UsesMiscMode(TextureMiscMode::NODEP)) {
  321. LOG_WARNING(HW_GPU, "TMML.NODEP implementation is incomplete");
  322. }
  323. const Node texture = GetTldsCode(instr, texture_type, is_array);
  324. WriteTexsInstructionFloat(bb, instr, texture);
  325. break;
  326. }
  327. default:
  328. UNIMPLEMENTED_MSG("Unhandled memory instruction: {}", opcode->get().GetName());
  329. }
  330. return pc;
  331. }
  332. const Sampler& ShaderIR::GetSampler(const Tegra::Shader::Sampler& sampler, TextureType type,
  333. bool is_array, bool is_shadow) {
  334. const auto offset = static_cast<std::size_t>(sampler.index.Value());
  335. // If this sampler has already been used, return the existing mapping.
  336. const auto itr =
  337. std::find_if(used_samplers.begin(), used_samplers.end(),
  338. [&](const Sampler& entry) { return entry.GetOffset() == offset; });
  339. if (itr != used_samplers.end()) {
  340. ASSERT(itr->GetType() == type && itr->IsArray() == is_array &&
  341. itr->IsShadow() == is_shadow);
  342. return *itr;
  343. }
  344. // Otherwise create a new mapping for this sampler
  345. const std::size_t next_index = used_samplers.size();
  346. const Sampler entry{offset, next_index, type, is_array, is_shadow};
  347. return *used_samplers.emplace(entry).first;
  348. }
  349. void ShaderIR::WriteTexsInstructionFloat(BasicBlock& bb, Instruction instr, Node texture) {
  350. // TEXS has two destination registers and a swizzle. The first two elements in the swizzle
  351. // go into gpr0+0 and gpr0+1, and the rest goes into gpr28+0 and gpr28+1
  352. MetaComponents meta;
  353. std::array<Node, 4> dest;
  354. for (u32 component = 0; component < 4; ++component) {
  355. if (!instr.texs.IsComponentEnabled(component)) {
  356. continue;
  357. }
  358. meta.components_map[meta.count] = component;
  359. if (meta.count < 2) {
  360. // Write the first two swizzle components to gpr0 and gpr0+1
  361. dest[meta.count] = GetRegister(instr.gpr0.Value() + meta.count % 2);
  362. } else {
  363. ASSERT(instr.texs.HasTwoDestinations());
  364. // Write the rest of the swizzle components to gpr28 and gpr28+1
  365. dest[meta.count] = GetRegister(instr.gpr28.Value() + meta.count % 2);
  366. }
  367. ++meta.count;
  368. }
  369. std::generate(dest.begin() + meta.count, dest.end(), [&]() { return GetRegister(RZ); });
  370. bb.push_back(Operation(OperationCode::AssignComposite, meta, texture, dest[0], dest[1], dest[2],
  371. dest[3]));
  372. }
  373. void ShaderIR::WriteTexsInstructionHalfFloat(BasicBlock& bb, Instruction instr, Node texture) {
  374. // TEXS.F16 destionation registers are packed in two registers in pairs (just like any half
  375. // float instruction).
  376. MetaComponents meta;
  377. for (u32 component = 0; component < 4; ++component) {
  378. if (!instr.texs.IsComponentEnabled(component))
  379. continue;
  380. meta.components_map[meta.count++] = component;
  381. }
  382. if (meta.count == 0)
  383. return;
  384. bb.push_back(Operation(OperationCode::AssignCompositeHalf, meta, texture,
  385. GetRegister(instr.gpr0), GetRegister(instr.gpr28)));
  386. }
  387. Node ShaderIR::GetTextureCode(Instruction instr, TextureType texture_type,
  388. TextureProcessMode process_mode, bool depth_compare, bool is_array,
  389. std::size_t array_offset, std::size_t bias_offset,
  390. std::vector<Node>&& coords) {
  391. UNIMPLEMENTED_IF_MSG(
  392. (texture_type == TextureType::Texture3D && (is_array || depth_compare)) ||
  393. (texture_type == TextureType::TextureCube && is_array && depth_compare),
  394. "This method is not supported.");
  395. const auto& sampler = GetSampler(instr.sampler, texture_type, is_array, depth_compare);
  396. const bool lod_needed = process_mode == TextureProcessMode::LZ ||
  397. process_mode == TextureProcessMode::LL ||
  398. process_mode == TextureProcessMode::LLA;
  399. // LOD selection (either via bias or explicit textureLod) not supported in GL for
  400. // sampler2DArrayShadow and samplerCubeArrayShadow.
  401. const bool gl_lod_supported =
  402. !((texture_type == Tegra::Shader::TextureType::Texture2D && is_array && depth_compare) ||
  403. (texture_type == Tegra::Shader::TextureType::TextureCube && is_array && depth_compare));
  404. const OperationCode read_method =
  405. lod_needed && gl_lod_supported ? OperationCode::F4TextureLod : OperationCode::F4Texture;
  406. UNIMPLEMENTED_IF(process_mode != TextureProcessMode::None && !gl_lod_supported);
  407. std::optional<u32> array_offset_value;
  408. if (is_array)
  409. array_offset_value = static_cast<u32>(array_offset);
  410. MetaTexture meta{sampler, static_cast<u32>(coords.size()), array_offset_value};
  411. std::vector<Node> params = std::move(coords);
  412. if (process_mode != TextureProcessMode::None && gl_lod_supported) {
  413. if (process_mode == TextureProcessMode::LZ) {
  414. params.push_back(Immediate(0.0f));
  415. } else {
  416. // If present, lod or bias are always stored in the register indexed by the gpr20 field
  417. // with an offset depending on the usage of the other registers
  418. params.push_back(GetRegister(instr.gpr20.Value() + bias_offset));
  419. }
  420. }
  421. return Operation(read_method, meta, std::move(params));
  422. }
  423. Node ShaderIR::GetTexCode(Instruction instr, TextureType texture_type,
  424. TextureProcessMode process_mode, bool depth_compare, bool is_array) {
  425. const bool lod_bias_enabled =
  426. (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ);
  427. const auto [coord_count, total_coord_count] = ValidateAndGetCoordinateElement(
  428. texture_type, depth_compare, is_array, lod_bias_enabled, 4, 5);
  429. // If enabled arrays index is always stored in the gpr8 field
  430. const u64 array_register = instr.gpr8.Value();
  431. // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
  432. const u64 coord_register = array_register + (is_array ? 1 : 0);
  433. std::vector<Node> coords;
  434. for (std::size_t i = 0; i < coord_count; ++i) {
  435. coords.push_back(GetRegister(coord_register + i));
  436. }
  437. // 1D.DC in opengl the 2nd component is ignored.
  438. if (depth_compare && !is_array && texture_type == TextureType::Texture1D) {
  439. coords.push_back(Immediate(0.0f));
  440. }
  441. std::size_t array_offset{};
  442. if (is_array) {
  443. array_offset = coords.size();
  444. coords.push_back(GetRegister(array_register));
  445. }
  446. if (depth_compare) {
  447. // Depth is always stored in the register signaled by gpr20
  448. // or in the next register if lod or bias are used
  449. const u64 depth_register = instr.gpr20.Value() + (lod_bias_enabled ? 1 : 0);
  450. coords.push_back(GetRegister(depth_register));
  451. }
  452. // Fill ignored coordinates
  453. while (coords.size() < total_coord_count) {
  454. coords.push_back(Immediate(0));
  455. }
  456. return GetTextureCode(instr, texture_type, process_mode, depth_compare, is_array, array_offset,
  457. 0, std::move(coords));
  458. }
  459. Node ShaderIR::GetTexsCode(Instruction instr, TextureType texture_type,
  460. TextureProcessMode process_mode, bool depth_compare, bool is_array) {
  461. const bool lod_bias_enabled =
  462. (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ);
  463. const auto [coord_count, total_coord_count] = ValidateAndGetCoordinateElement(
  464. texture_type, depth_compare, is_array, lod_bias_enabled, 4, 4);
  465. // If enabled arrays index is always stored in the gpr8 field
  466. const u64 array_register = instr.gpr8.Value();
  467. // First coordinate index is stored in gpr8 field or (gpr8 + 1) when arrays are used
  468. const u64 coord_register = array_register + (is_array ? 1 : 0);
  469. const u64 last_coord_register =
  470. (is_array || !(lod_bias_enabled || depth_compare) || (coord_count > 2))
  471. ? static_cast<u64>(instr.gpr20.Value())
  472. : coord_register + 1;
  473. std::vector<Node> coords;
  474. for (std::size_t i = 0; i < coord_count; ++i) {
  475. const bool last = (i == (coord_count - 1)) && (coord_count > 1);
  476. coords.push_back(GetRegister(last ? last_coord_register : coord_register + i));
  477. }
  478. std::size_t array_offset{};
  479. if (is_array) {
  480. array_offset = coords.size();
  481. coords.push_back(GetRegister(array_register));
  482. }
  483. if (depth_compare) {
  484. // Depth is always stored in the register signaled by gpr20
  485. // or in the next register if lod or bias are used
  486. const u64 depth_register = instr.gpr20.Value() + (lod_bias_enabled ? 1 : 0);
  487. coords.push_back(GetRegister(depth_register));
  488. }
  489. // Fill ignored coordinates
  490. while (coords.size() < total_coord_count) {
  491. coords.push_back(Immediate(0));
  492. }
  493. return GetTextureCode(instr, texture_type, process_mode, depth_compare, is_array, array_offset,
  494. (coord_count > 2 ? 1 : 0), std::move(coords));
  495. }
  496. Node ShaderIR::GetTld4Code(Instruction instr, TextureType texture_type, bool depth_compare,
  497. bool is_array) {
  498. const std::size_t coord_count = GetCoordCount(texture_type);
  499. const std::size_t total_coord_count = coord_count + (is_array ? 1 : 0);
  500. const std::size_t total_reg_count = total_coord_count + (depth_compare ? 1 : 0);
  501. // If enabled arrays index is always stored in the gpr8 field
  502. const u64 array_register = instr.gpr8.Value();
  503. // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
  504. const u64 coord_register = array_register + (is_array ? 1 : 0);
  505. std::vector<Node> params;
  506. for (size_t i = 0; i < coord_count; ++i) {
  507. params.push_back(GetRegister(coord_register + i));
  508. }
  509. std::optional<u32> array_offset;
  510. if (is_array) {
  511. array_offset = static_cast<u32>(params.size());
  512. params.push_back(GetRegister(array_register));
  513. }
  514. const auto& sampler = GetSampler(instr.sampler, texture_type, is_array, depth_compare);
  515. MetaTexture meta{sampler, static_cast<u32>(params.size()), array_offset};
  516. return Operation(OperationCode::F4TextureGather, std::move(meta), std::move(params));
  517. }
  518. Node ShaderIR::GetTldsCode(Instruction instr, TextureType texture_type, bool is_array) {
  519. const std::size_t type_coord_count = GetCoordCount(texture_type);
  520. const std::size_t total_coord_count = type_coord_count + (is_array ? 1 : 0);
  521. const bool lod_enabled = instr.tlds.GetTextureProcessMode() == TextureProcessMode::LL;
  522. // If enabled arrays index is always stored in the gpr8 field
  523. const u64 array_register = instr.gpr8.Value();
  524. // if is array gpr20 is used
  525. const u64 coord_register = is_array ? instr.gpr20.Value() : instr.gpr8.Value();
  526. const u64 last_coord_register =
  527. ((type_coord_count > 2) || (type_coord_count == 2 && !lod_enabled)) && !is_array
  528. ? static_cast<u64>(instr.gpr20.Value())
  529. : coord_register + 1;
  530. std::vector<Node> params;
  531. for (std::size_t i = 0; i < type_coord_count; ++i) {
  532. const bool last = (i == (type_coord_count - 1)) && (type_coord_count > 1);
  533. params.push_back(GetRegister(last ? last_coord_register : coord_register + i));
  534. }
  535. std::optional<u32> array_offset;
  536. if (is_array) {
  537. array_offset = static_cast<u32>(params.size());
  538. params.push_back(GetRegister(array_register));
  539. }
  540. const auto coords_count = static_cast<u32>(params.size());
  541. if (lod_enabled) {
  542. // When lod is used always is in grp20
  543. params.push_back(GetRegister(instr.gpr20));
  544. } else {
  545. params.push_back(Immediate(0));
  546. }
  547. const auto& sampler = GetSampler(instr.sampler, texture_type, is_array, false);
  548. MetaTexture meta{sampler, coords_count, array_offset};
  549. return Operation(OperationCode::F4TexelFetch, std::move(meta), std::move(params));
  550. }
  551. std::tuple<std::size_t, std::size_t> ShaderIR::ValidateAndGetCoordinateElement(
  552. TextureType texture_type, bool depth_compare, bool is_array, bool lod_bias_enabled,
  553. std::size_t max_coords, std::size_t max_inputs) {
  554. const std::size_t coord_count = GetCoordCount(texture_type);
  555. std::size_t total_coord_count = coord_count + (is_array ? 1 : 0) + (depth_compare ? 1 : 0);
  556. const std::size_t total_reg_count = total_coord_count + (lod_bias_enabled ? 1 : 0);
  557. if (total_coord_count > max_coords || total_reg_count > max_inputs) {
  558. UNIMPLEMENTED_MSG("Unsupported Texture operation");
  559. total_coord_count = std::min(total_coord_count, max_coords);
  560. }
  561. // 1D.DC OpenGL is using a vec3 but 2nd component is ignored later.
  562. total_coord_count +=
  563. (depth_compare && !is_array && texture_type == TextureType::Texture1D) ? 1 : 0;
  564. return {coord_count, total_coord_count};
  565. }
  566. } // namespace VideoCommon::Shader