memory.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. Tegra::Shader::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. UNIMPLEMENTED();
  185. // WriteTexsInstructionHalfFloat(bb, instr, texture);
  186. }
  187. break;
  188. }
  189. case OpCode::Id::TLD4: {
  190. ASSERT(instr.tld4.texture_type == Tegra::Shader::TextureType::Texture2D);
  191. ASSERT(instr.tld4.array == 0);
  192. UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(TextureMiscMode::AOFFI),
  193. "AOFFI is not implemented");
  194. UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(TextureMiscMode::NDV),
  195. "NDV is not implemented");
  196. UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(TextureMiscMode::PTP),
  197. "PTP is not implemented");
  198. if (instr.tld4.UsesMiscMode(TextureMiscMode::NODEP)) {
  199. LOG_WARNING(HW_GPU, "TLD4.NODEP implementation is incomplete");
  200. }
  201. const bool depth_compare = instr.tld4.UsesMiscMode(TextureMiscMode::DC);
  202. auto texture_type = instr.tld4.texture_type.Value();
  203. u32 num_coordinates = static_cast<u32>(GetCoordCount(texture_type));
  204. if (depth_compare)
  205. num_coordinates += 1;
  206. std::vector<Node> params;
  207. switch (num_coordinates) {
  208. case 2: {
  209. params.push_back(GetRegister(instr.gpr8));
  210. params.push_back(GetRegister(instr.gpr8.Value() + 1));
  211. break;
  212. }
  213. case 3: {
  214. params.push_back(GetRegister(instr.gpr8));
  215. params.push_back(GetRegister(instr.gpr8.Value() + 1));
  216. params.push_back(GetRegister(instr.gpr8.Value() + 2));
  217. break;
  218. }
  219. default:
  220. UNIMPLEMENTED_MSG("Unhandled coordinates number {}", static_cast<u32>(num_coordinates));
  221. params.push_back(GetRegister(instr.gpr8));
  222. params.push_back(GetRegister(instr.gpr8.Value() + 1));
  223. num_coordinates = 2;
  224. texture_type = Tegra::Shader::TextureType::Texture2D;
  225. }
  226. params.push_back(Immediate(static_cast<u32>(instr.tld4.component)));
  227. const auto& sampler = GetSampler(instr.sampler, texture_type, false, depth_compare);
  228. MetaTexture meta{sampler, num_coordinates};
  229. const Node texture =
  230. Operation(OperationCode::F4TextureGather, std::move(meta), std::move(params));
  231. if (depth_compare) {
  232. SetRegister(bb, instr.gpr0, texture);
  233. } else {
  234. MetaComponents meta;
  235. std::array<Node, 4> dest;
  236. std::size_t dest_elem = 0;
  237. for (std::size_t elem = 0; elem < 4; ++elem) {
  238. if (!instr.tex.IsComponentEnabled(elem)) {
  239. // Skip disabled components
  240. continue;
  241. }
  242. meta.components_map[dest_elem] = static_cast<u32>(elem);
  243. dest[dest_elem] = GetRegister(instr.gpr0.Value() + dest_elem);
  244. ++dest_elem;
  245. }
  246. std::generate(dest.begin() + dest_elem, dest.end(), [&]() { return GetRegister(RZ); });
  247. bb.push_back(Operation(OperationCode::AssignComposite, std::move(meta), texture,
  248. dest[0], dest[1], dest[2], dest[3]));
  249. }
  250. break;
  251. }
  252. case OpCode::Id::TLD4S: {
  253. UNIMPLEMENTED_IF_MSG(instr.tld4s.UsesMiscMode(TextureMiscMode::AOFFI),
  254. "AOFFI is not implemented");
  255. if (instr.tld4s.UsesMiscMode(TextureMiscMode::NODEP)) {
  256. LOG_WARNING(HW_GPU, "TLD4S.NODEP implementation is incomplete");
  257. }
  258. const bool depth_compare = instr.tld4s.UsesMiscMode(TextureMiscMode::DC);
  259. const Node op_a = GetRegister(instr.gpr8);
  260. const Node op_b = GetRegister(instr.gpr20);
  261. std::vector<Node> params;
  262. // TODO(Subv): Figure out how the sampler type is encoded in the TLD4S instruction.
  263. if (depth_compare) {
  264. // Note: TLD4S coordinate encoding works just like TEXS's
  265. const Node op_y = GetRegister(instr.gpr8.Value() + 1);
  266. params.push_back(op_a);
  267. params.push_back(op_y);
  268. params.push_back(op_b);
  269. } else {
  270. params.push_back(op_a);
  271. params.push_back(op_b);
  272. }
  273. const auto num_coords = static_cast<u32>(params.size());
  274. params.push_back(Immediate(static_cast<u32>(instr.tld4s.component)));
  275. const auto& sampler =
  276. GetSampler(instr.sampler, TextureType::Texture2D, false, depth_compare);
  277. MetaTexture meta{sampler, num_coords};
  278. WriteTexsInstructionFloat(
  279. bb, instr, Operation(OperationCode::F4TextureGather, meta, std::move(params)));
  280. break;
  281. }
  282. case OpCode::Id::TXQ: {
  283. if (instr.txq.UsesMiscMode(TextureMiscMode::NODEP)) {
  284. LOG_WARNING(HW_GPU, "TXQ.NODEP implementation is incomplete");
  285. }
  286. // TODO: The new commits on the texture refactor, change the way samplers work.
  287. // Sadly, not all texture instructions specify the type of texture their sampler
  288. // uses. This must be fixed at a later instance.
  289. const auto& sampler =
  290. GetSampler(instr.sampler, Tegra::Shader::TextureType::Texture2D, false, false);
  291. switch (instr.txq.query_type) {
  292. case Tegra::Shader::TextureQueryType::Dimension: {
  293. MetaTexture meta_texture{sampler};
  294. const MetaComponents meta_components{{0, 1, 2, 3}};
  295. const Node texture = Operation(OperationCode::F4TextureQueryDimensions, meta_texture,
  296. GetRegister(instr.gpr8));
  297. std::array<Node, 4> dest;
  298. for (std::size_t i = 0; i < dest.size(); ++i) {
  299. dest[i] = GetRegister(instr.gpr0.Value() + i);
  300. }
  301. bb.push_back(Operation(OperationCode::AssignComposite, meta_components, texture,
  302. dest[0], dest[1], dest[2], dest[3]));
  303. break;
  304. }
  305. default:
  306. UNIMPLEMENTED_MSG("Unhandled texture query type: {}",
  307. static_cast<u32>(instr.txq.query_type.Value()));
  308. }
  309. break;
  310. }
  311. case OpCode::Id::TMML: {
  312. UNIMPLEMENTED_IF_MSG(instr.tmml.UsesMiscMode(Tegra::Shader::TextureMiscMode::NDV),
  313. "NDV is not implemented");
  314. if (instr.tmml.UsesMiscMode(TextureMiscMode::NODEP)) {
  315. LOG_WARNING(HW_GPU, "TMML.NODEP implementation is incomplete");
  316. }
  317. auto texture_type = instr.tmml.texture_type.Value();
  318. const bool is_array = instr.tmml.array != 0;
  319. const auto& sampler = GetSampler(instr.sampler, texture_type, is_array, false);
  320. std::vector<Node> coords;
  321. // TODO: Add coordinates for different samplers once other texture types are implemented.
  322. switch (texture_type) {
  323. case TextureType::Texture1D:
  324. coords.push_back(GetRegister(instr.gpr8));
  325. break;
  326. case TextureType::Texture2D:
  327. coords.push_back(GetRegister(instr.gpr8.Value() + 0));
  328. coords.push_back(GetRegister(instr.gpr8.Value() + 1));
  329. break;
  330. default:
  331. UNIMPLEMENTED_MSG("Unhandled texture type {}", static_cast<u32>(texture_type));
  332. // Fallback to interpreting as a 2D texture for now
  333. coords.push_back(GetRegister(instr.gpr8.Value() + 0));
  334. coords.push_back(GetRegister(instr.gpr8.Value() + 1));
  335. texture_type = TextureType::Texture2D;
  336. }
  337. MetaTexture meta_texture{sampler, static_cast<u32>(coords.size())};
  338. const Node texture =
  339. Operation(OperationCode::F4TextureQueryLod, meta_texture, std::move(coords));
  340. const MetaComponents meta_composite{{0, 1, 2, 3}};
  341. bb.push_back(Operation(OperationCode::AssignComposite, meta_composite, texture,
  342. GetRegister(instr.gpr0), GetRegister(instr.gpr0.Value() + 1),
  343. GetRegister(RZ), GetRegister(RZ)));
  344. break;
  345. }
  346. default:
  347. UNIMPLEMENTED_MSG("Unhandled memory instruction: {}", opcode->get().GetName());
  348. }
  349. return pc;
  350. }
  351. const Sampler& ShaderIR::GetSampler(const Tegra::Shader::Sampler& sampler, TextureType type,
  352. bool is_array, bool is_shadow) {
  353. const auto offset = static_cast<std::size_t>(sampler.index.Value());
  354. // If this sampler has already been used, return the existing mapping.
  355. const auto itr =
  356. std::find_if(used_samplers.begin(), used_samplers.end(),
  357. [&](const Sampler& entry) { return entry.GetOffset() == offset; });
  358. if (itr != used_samplers.end()) {
  359. ASSERT(itr->GetType() == type && itr->IsArray() == is_array &&
  360. itr->IsShadow() == is_shadow);
  361. return *itr;
  362. }
  363. // Otherwise create a new mapping for this sampler
  364. const std::size_t next_index = used_samplers.size();
  365. const Sampler entry{offset, next_index, type, is_array, is_shadow};
  366. return *used_samplers.emplace(entry).first;
  367. }
  368. void ShaderIR::WriteTexsInstructionFloat(BasicBlock& bb, Tegra::Shader::Instruction instr,
  369. Node texture) {
  370. // TEXS has two destination registers and a swizzle. The first two elements in the swizzle
  371. // go into gpr0+0 and gpr0+1, and the rest goes into gpr28+0 and gpr28+1
  372. MetaComponents meta;
  373. std::array<Node, 4> dest;
  374. std::size_t written_components = 0;
  375. for (u32 component = 0; component < 4; ++component) {
  376. if (!instr.texs.IsComponentEnabled(component)) {
  377. continue;
  378. }
  379. meta.components_map[written_components] = static_cast<u32>(component);
  380. if (written_components < 2) {
  381. // Write the first two swizzle components to gpr0 and gpr0+1
  382. dest[written_components] = GetRegister(instr.gpr0.Value() + written_components % 2);
  383. } else {
  384. ASSERT(instr.texs.HasTwoDestinations());
  385. // Write the rest of the swizzle components to gpr28 and gpr28+1
  386. dest[written_components] = GetRegister(instr.gpr28.Value() + written_components % 2);
  387. }
  388. ++written_components;
  389. }
  390. std::generate(dest.begin() + written_components, dest.end(), [&]() { return GetRegister(RZ); });
  391. bb.push_back(Operation(OperationCode::AssignComposite, meta, texture, dest[0], dest[1], dest[2],
  392. dest[3]));
  393. }
  394. Node ShaderIR::GetTextureCode(Instruction instr, TextureType texture_type,
  395. TextureProcessMode process_mode, bool depth_compare, bool is_array,
  396. std::size_t array_offset, std::size_t bias_offset,
  397. std::vector<Node>&& coords) {
  398. UNIMPLEMENTED_IF_MSG(
  399. (texture_type == TextureType::Texture3D && (is_array || depth_compare)) ||
  400. (texture_type == TextureType::TextureCube && is_array && depth_compare),
  401. "This method is not supported.");
  402. const auto& sampler = GetSampler(instr.sampler, texture_type, is_array, depth_compare);
  403. const bool lod_needed = process_mode == TextureProcessMode::LZ ||
  404. process_mode == TextureProcessMode::LL ||
  405. process_mode == TextureProcessMode::LLA;
  406. // LOD selection (either via bias or explicit textureLod) not supported in GL for
  407. // sampler2DArrayShadow and samplerCubeArrayShadow.
  408. const bool gl_lod_supported =
  409. !((texture_type == Tegra::Shader::TextureType::Texture2D && is_array && depth_compare) ||
  410. (texture_type == Tegra::Shader::TextureType::TextureCube && is_array && depth_compare));
  411. const OperationCode read_method =
  412. lod_needed && gl_lod_supported ? OperationCode::F4TextureLod : OperationCode::F4Texture;
  413. UNIMPLEMENTED_IF(process_mode != TextureProcessMode::None && !gl_lod_supported);
  414. std::optional<u32> array_offset_value;
  415. if (is_array)
  416. array_offset_value = static_cast<u32>(array_offset);
  417. MetaTexture meta{sampler, static_cast<u32>(coords.size()), array_offset_value};
  418. std::vector<Node> params = std::move(coords);
  419. if (process_mode != TextureProcessMode::None && gl_lod_supported) {
  420. if (process_mode == TextureProcessMode::LZ) {
  421. params.push_back(Immediate(0.0f));
  422. } else {
  423. // If present, lod or bias are always stored in the register indexed by the gpr20 field
  424. // with an offset depending on the usage of the other registers
  425. params.push_back(GetRegister(instr.gpr20.Value() + bias_offset));
  426. }
  427. }
  428. return Operation(read_method, meta, std::move(params));
  429. }
  430. Node ShaderIR::GetTexCode(Instruction instr, TextureType texture_type,
  431. TextureProcessMode process_mode, bool depth_compare, bool is_array) {
  432. const bool lod_bias_enabled =
  433. (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ);
  434. const auto [coord_count, total_coord_count] = ValidateAndGetCoordinateElement(
  435. texture_type, depth_compare, is_array, lod_bias_enabled, 4, 5);
  436. // If enabled arrays index is always stored in the gpr8 field
  437. const u64 array_register = instr.gpr8.Value();
  438. // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
  439. const u64 coord_register = array_register + (is_array ? 1 : 0);
  440. std::vector<Node> coords;
  441. for (std::size_t i = 0; i < coord_count; ++i) {
  442. coords.push_back(GetRegister(coord_register + i));
  443. }
  444. // 1D.DC in opengl the 2nd component is ignored.
  445. if (depth_compare && !is_array && texture_type == TextureType::Texture1D) {
  446. coords.push_back(Immediate(0.0f));
  447. }
  448. std::size_t array_offset{};
  449. if (is_array) {
  450. array_offset = coords.size();
  451. coords.push_back(GetRegister(array_register));
  452. }
  453. if (depth_compare) {
  454. // Depth is always stored in the register signaled by gpr20
  455. // or in the next register if lod or bias are used
  456. const u64 depth_register = instr.gpr20.Value() + (lod_bias_enabled ? 1 : 0);
  457. coords.push_back(GetRegister(depth_register));
  458. }
  459. // Fill ignored coordinates
  460. while (coords.size() < total_coord_count) {
  461. coords.push_back(Immediate(0));
  462. }
  463. return GetTextureCode(instr, texture_type, process_mode, depth_compare, is_array, array_offset,
  464. 0, std::move(coords));
  465. }
  466. Node ShaderIR::GetTexsCode(Instruction instr, TextureType texture_type,
  467. TextureProcessMode process_mode, bool depth_compare, bool is_array) {
  468. const bool lod_bias_enabled =
  469. (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ);
  470. const auto [coord_count, total_coord_count] = ValidateAndGetCoordinateElement(
  471. texture_type, depth_compare, is_array, lod_bias_enabled, 4, 4);
  472. // If enabled arrays index is always stored in the gpr8 field
  473. const u64 array_register = instr.gpr8.Value();
  474. // First coordinate index is stored in gpr8 field or (gpr8 + 1) when arrays are used
  475. const u64 coord_register = array_register + (is_array ? 1 : 0);
  476. const u64 last_coord_register =
  477. (is_array || !(lod_bias_enabled || depth_compare) || (coord_count > 2))
  478. ? static_cast<u64>(instr.gpr20.Value())
  479. : coord_register + 1;
  480. std::vector<Node> coords;
  481. for (std::size_t i = 0; i < coord_count; ++i) {
  482. const bool last = (i == (coord_count - 1)) && (coord_count > 1);
  483. coords.push_back(GetRegister(last ? last_coord_register : coord_register + i));
  484. }
  485. std::size_t array_offset{};
  486. if (is_array) {
  487. array_offset = coords.size();
  488. coords.push_back(GetRegister(array_register));
  489. }
  490. if (depth_compare) {
  491. // Depth is always stored in the register signaled by gpr20
  492. // or in the next register if lod or bias are used
  493. const u64 depth_register = instr.gpr20.Value() + (lod_bias_enabled ? 1 : 0);
  494. coords.push_back(GetRegister(depth_register));
  495. }
  496. // Fill ignored coordinates
  497. while (coords.size() < total_coord_count) {
  498. coords.push_back(Immediate(0));
  499. }
  500. return GetTextureCode(instr, texture_type, process_mode, depth_compare, is_array, array_offset,
  501. (coord_count > 2 ? 1 : 0), std::move(coords));
  502. }
  503. std::tuple<std::size_t, std::size_t> ShaderIR::ValidateAndGetCoordinateElement(
  504. TextureType texture_type, bool depth_compare, bool is_array, bool lod_bias_enabled,
  505. std::size_t max_coords, std::size_t max_inputs) {
  506. const std::size_t coord_count = GetCoordCount(texture_type);
  507. std::size_t total_coord_count = coord_count + (is_array ? 1 : 0) + (depth_compare ? 1 : 0);
  508. const std::size_t total_reg_count = total_coord_count + (lod_bias_enabled ? 1 : 0);
  509. if (total_coord_count > max_coords || total_reg_count > max_inputs) {
  510. UNIMPLEMENTED_MSG("Unsupported Texture operation");
  511. total_coord_count = std::min(total_coord_count, max_coords);
  512. }
  513. // 1D.DC OpenGL is using a vec3 but 2nd component is ignored later.
  514. total_coord_count +=
  515. (depth_compare && !is_array && texture_type == TextureType::Texture1D) ? 1 : 0;
  516. return {coord_count, total_coord_count};
  517. }
  518. } // namespace VideoCommon::Shader