texture.cpp 26 KB

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