texture.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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/node_helper.h"
  13. #include "video_core/shader/registry.h"
  14. #include "video_core/shader/shader_ir.h"
  15. namespace VideoCommon::Shader {
  16. using Tegra::Shader::Instruction;
  17. using Tegra::Shader::OpCode;
  18. using Tegra::Shader::Register;
  19. using Tegra::Shader::TextureMiscMode;
  20. using Tegra::Shader::TextureProcessMode;
  21. using Tegra::Shader::TextureType;
  22. static std::size_t GetCoordCount(TextureType texture_type) {
  23. switch (texture_type) {
  24. case TextureType::Texture1D:
  25. return 1;
  26. case TextureType::Texture2D:
  27. return 2;
  28. case TextureType::Texture3D:
  29. case TextureType::TextureCube:
  30. return 3;
  31. default:
  32. UNIMPLEMENTED_MSG("Unhandled texture type: {}", static_cast<u32>(texture_type));
  33. return 0;
  34. }
  35. }
  36. u32 ShaderIR::DecodeTexture(NodeBlock& bb, u32 pc) {
  37. const Instruction instr = {program_code[pc]};
  38. const auto opcode = OpCode::Decode(instr);
  39. bool is_bindless = false;
  40. switch (opcode->get().GetId()) {
  41. case OpCode::Id::TEX: {
  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. const TextureType texture_type{instr.tex_b.texture_type};
  56. const bool is_array = instr.tex_b.array != 0;
  57. const bool is_aoffi = instr.tex.UsesMiscMode(TextureMiscMode::AOFFI);
  58. const bool depth_compare = instr.tex_b.UsesMiscMode(TextureMiscMode::DC);
  59. const auto process_mode = instr.tex_b.GetTextureProcessMode();
  60. WriteTexInstructionFloat(bb, instr,
  61. GetTexCode(instr, texture_type, process_mode, depth_compare,
  62. is_array, is_aoffi, {instr.gpr20}));
  63. break;
  64. }
  65. case OpCode::Id::TEXS: {
  66. const TextureType texture_type{instr.texs.GetTextureType()};
  67. const bool is_array{instr.texs.IsArrayTexture()};
  68. const bool depth_compare = instr.texs.UsesMiscMode(TextureMiscMode::DC);
  69. const auto process_mode = instr.texs.GetTextureProcessMode();
  70. const Node4 components =
  71. GetTexsCode(instr, texture_type, process_mode, depth_compare, is_array);
  72. if (instr.texs.fp32_flag) {
  73. WriteTexsInstructionFloat(bb, instr, components);
  74. } else {
  75. WriteTexsInstructionHalfFloat(bb, instr, components);
  76. }
  77. break;
  78. }
  79. case OpCode::Id::TLD4_B: {
  80. is_bindless = true;
  81. [[fallthrough]];
  82. }
  83. case OpCode::Id::TLD4: {
  84. UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(TextureMiscMode::NDV),
  85. "NDV is not implemented");
  86. const auto texture_type = instr.tld4.texture_type.Value();
  87. const bool depth_compare = is_bindless ? instr.tld4_b.UsesMiscMode(TextureMiscMode::DC)
  88. : instr.tld4.UsesMiscMode(TextureMiscMode::DC);
  89. const bool is_array = instr.tld4.array != 0;
  90. const bool is_aoffi = is_bindless ? instr.tld4_b.UsesMiscMode(TextureMiscMode::AOFFI)
  91. : instr.tld4.UsesMiscMode(TextureMiscMode::AOFFI);
  92. const bool is_ptp = is_bindless ? instr.tld4_b.UsesMiscMode(TextureMiscMode::PTP)
  93. : instr.tld4.UsesMiscMode(TextureMiscMode::PTP);
  94. WriteTexInstructionFloat(bb, instr,
  95. GetTld4Code(instr, texture_type, depth_compare, is_array, is_aoffi,
  96. is_ptp, is_bindless));
  97. break;
  98. }
  99. case OpCode::Id::TLD4S: {
  100. constexpr std::size_t num_coords = 2;
  101. const bool is_aoffi = instr.tld4s.UsesMiscMode(TextureMiscMode::AOFFI);
  102. const bool is_depth_compare = instr.tld4s.UsesMiscMode(TextureMiscMode::DC);
  103. const Node op_a = GetRegister(instr.gpr8);
  104. const Node op_b = GetRegister(instr.gpr20);
  105. // TODO(Subv): Figure out how the sampler type is encoded in the TLD4S instruction.
  106. std::vector<Node> coords;
  107. std::vector<Node> aoffi;
  108. Node depth_compare;
  109. if (is_depth_compare) {
  110. // Note: TLD4S coordinate encoding works just like TEXS's
  111. const Node op_y = GetRegister(instr.gpr8.Value() + 1);
  112. coords.push_back(op_a);
  113. coords.push_back(op_y);
  114. if (is_aoffi) {
  115. aoffi = GetAoffiCoordinates(op_b, num_coords, true);
  116. depth_compare = GetRegister(instr.gpr20.Value() + 1);
  117. } else {
  118. depth_compare = op_b;
  119. }
  120. } else {
  121. // There's no depth compare
  122. coords.push_back(op_a);
  123. if (is_aoffi) {
  124. coords.push_back(GetRegister(instr.gpr8.Value() + 1));
  125. aoffi = GetAoffiCoordinates(op_b, num_coords, true);
  126. } else {
  127. coords.push_back(op_b);
  128. }
  129. }
  130. const Node component = Immediate(static_cast<u32>(instr.tld4s.component));
  131. SamplerInfo info;
  132. info.is_shadow = is_depth_compare;
  133. const std::optional<Sampler> sampler = GetSampler(instr.sampler, info);
  134. Node4 values;
  135. for (u32 element = 0; element < values.size(); ++element) {
  136. MetaTexture meta{*sampler, {}, depth_compare, aoffi, {}, {},
  137. {}, {}, component, element, {}};
  138. values[element] = Operation(OperationCode::TextureGather, meta, coords);
  139. }
  140. if (instr.tld4s.fp16_flag) {
  141. WriteTexsInstructionHalfFloat(bb, instr, values, true);
  142. } else {
  143. WriteTexsInstructionFloat(bb, instr, values, true);
  144. }
  145. break;
  146. }
  147. case OpCode::Id::TXD_B:
  148. is_bindless = true;
  149. [[fallthrough]];
  150. case OpCode::Id::TXD: {
  151. UNIMPLEMENTED_IF_MSG(instr.txd.UsesMiscMode(TextureMiscMode::AOFFI),
  152. "AOFFI is not implemented");
  153. const bool is_array = instr.txd.is_array != 0;
  154. const auto derivate_reg = instr.gpr20.Value();
  155. const auto texture_type = instr.txd.texture_type.Value();
  156. const auto coord_count = GetCoordCount(texture_type);
  157. u64 base_reg = instr.gpr8.Value();
  158. Node index_var;
  159. SamplerInfo info;
  160. info.type = texture_type;
  161. info.is_array = is_array;
  162. const std::optional<Sampler> sampler = is_bindless
  163. ? GetBindlessSampler(base_reg, info, index_var)
  164. : GetSampler(instr.sampler, info);
  165. Node4 values;
  166. if (!sampler) {
  167. std::generate(values.begin(), values.end(), [this] { return Immediate(0); });
  168. WriteTexInstructionFloat(bb, instr, values);
  169. break;
  170. }
  171. if (is_bindless) {
  172. base_reg++;
  173. }
  174. std::vector<Node> coords;
  175. std::vector<Node> derivates;
  176. for (std::size_t i = 0; i < coord_count; ++i) {
  177. coords.push_back(GetRegister(base_reg + i));
  178. const std::size_t derivate = i * 2;
  179. derivates.push_back(GetRegister(derivate_reg + derivate));
  180. derivates.push_back(GetRegister(derivate_reg + derivate + 1));
  181. }
  182. Node array_node = {};
  183. if (is_array) {
  184. const Node info_reg = GetRegister(base_reg + coord_count);
  185. array_node = BitfieldExtract(info_reg, 0, 16);
  186. }
  187. for (u32 element = 0; element < values.size(); ++element) {
  188. MetaTexture meta{*sampler, array_node, {}, {}, {}, derivates,
  189. {}, {}, {}, element, index_var};
  190. values[element] = Operation(OperationCode::TextureGradient, std::move(meta), coords);
  191. }
  192. WriteTexInstructionFloat(bb, instr, values);
  193. break;
  194. }
  195. case OpCode::Id::TXQ_B:
  196. is_bindless = true;
  197. [[fallthrough]];
  198. case OpCode::Id::TXQ: {
  199. Node index_var;
  200. const std::optional<Sampler> sampler = is_bindless
  201. ? GetBindlessSampler(instr.gpr8, {}, index_var)
  202. : GetSampler(instr.sampler, {});
  203. if (!sampler) {
  204. u32 indexer = 0;
  205. for (u32 element = 0; element < 4; ++element) {
  206. if (!instr.txq.IsComponentEnabled(element)) {
  207. continue;
  208. }
  209. const Node value = Immediate(0);
  210. SetTemporary(bb, indexer++, value);
  211. }
  212. for (u32 i = 0; i < indexer; ++i) {
  213. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  214. }
  215. break;
  216. }
  217. u32 indexer = 0;
  218. switch (instr.txq.query_type) {
  219. case Tegra::Shader::TextureQueryType::Dimension: {
  220. for (u32 element = 0; element < 4; ++element) {
  221. if (!instr.txq.IsComponentEnabled(element)) {
  222. continue;
  223. }
  224. MetaTexture meta{*sampler, {}, {}, {}, {}, {}, {}, {}, {}, element, index_var};
  225. const Node value =
  226. Operation(OperationCode::TextureQueryDimensions, meta,
  227. GetRegister(instr.gpr8.Value() + (is_bindless ? 1 : 0)));
  228. SetTemporary(bb, indexer++, value);
  229. }
  230. for (u32 i = 0; i < indexer; ++i) {
  231. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  232. }
  233. break;
  234. }
  235. default:
  236. UNIMPLEMENTED_MSG("Unhandled texture query type: {}",
  237. static_cast<u32>(instr.txq.query_type.Value()));
  238. }
  239. break;
  240. }
  241. case OpCode::Id::TMML_B:
  242. is_bindless = true;
  243. [[fallthrough]];
  244. case OpCode::Id::TMML: {
  245. UNIMPLEMENTED_IF_MSG(instr.tmml.UsesMiscMode(Tegra::Shader::TextureMiscMode::NDV),
  246. "NDV is not implemented");
  247. const auto texture_type = instr.tmml.texture_type.Value();
  248. const bool is_array = instr.tmml.array != 0;
  249. SamplerInfo info;
  250. info.type = texture_type;
  251. info.is_array = is_array;
  252. Node index_var;
  253. const std::optional<Sampler> sampler =
  254. is_bindless ? GetBindlessSampler(instr.gpr20, info, index_var)
  255. : GetSampler(instr.sampler, info);
  256. if (!sampler) {
  257. u32 indexer = 0;
  258. for (u32 element = 0; element < 2; ++element) {
  259. if (!instr.tmml.IsComponentEnabled(element)) {
  260. continue;
  261. }
  262. const Node value = Immediate(0);
  263. SetTemporary(bb, indexer++, value);
  264. }
  265. for (u32 i = 0; i < indexer; ++i) {
  266. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  267. }
  268. break;
  269. }
  270. std::vector<Node> coords;
  271. // TODO: Add coordinates for different samplers once other texture types are implemented.
  272. switch (texture_type) {
  273. case TextureType::Texture1D:
  274. coords.push_back(GetRegister(instr.gpr8));
  275. break;
  276. case TextureType::Texture2D:
  277. coords.push_back(GetRegister(instr.gpr8.Value() + 0));
  278. coords.push_back(GetRegister(instr.gpr8.Value() + 1));
  279. break;
  280. default:
  281. UNIMPLEMENTED_MSG("Unhandled texture type {}", static_cast<int>(texture_type));
  282. // Fallback to interpreting as a 2D texture for now
  283. coords.push_back(GetRegister(instr.gpr8.Value() + 0));
  284. coords.push_back(GetRegister(instr.gpr8.Value() + 1));
  285. }
  286. u32 indexer = 0;
  287. for (u32 element = 0; element < 2; ++element) {
  288. if (!instr.tmml.IsComponentEnabled(element)) {
  289. continue;
  290. }
  291. auto params = coords;
  292. MetaTexture meta{*sampler, {}, {}, {}, {}, {}, {}, {}, {}, element, index_var};
  293. const Node value = Operation(OperationCode::TextureQueryLod, meta, std::move(params));
  294. SetTemporary(bb, indexer++, value);
  295. }
  296. for (u32 i = 0; i < indexer; ++i) {
  297. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  298. }
  299. break;
  300. }
  301. case OpCode::Id::TLD: {
  302. UNIMPLEMENTED_IF_MSG(instr.tld.aoffi, "AOFFI is not implemented");
  303. UNIMPLEMENTED_IF_MSG(instr.tld.ms, "MS is not implemented");
  304. UNIMPLEMENTED_IF_MSG(instr.tld.cl, "CL is not implemented");
  305. WriteTexInstructionFloat(bb, instr, GetTldCode(instr));
  306. break;
  307. }
  308. case OpCode::Id::TLDS: {
  309. const TextureType texture_type{instr.tlds.GetTextureType()};
  310. const bool is_array{instr.tlds.IsArrayTexture()};
  311. UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(TextureMiscMode::AOFFI),
  312. "AOFFI is not implemented");
  313. UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(TextureMiscMode::MZ), "MZ is not implemented");
  314. const Node4 components = GetTldsCode(instr, texture_type, is_array);
  315. if (instr.tlds.fp32_flag) {
  316. WriteTexsInstructionFloat(bb, instr, components);
  317. } else {
  318. WriteTexsInstructionHalfFloat(bb, instr, components);
  319. }
  320. break;
  321. }
  322. default:
  323. UNIMPLEMENTED_MSG("Unhandled memory instruction: {}", opcode->get().GetName());
  324. }
  325. return pc;
  326. }
  327. ShaderIR::SamplerInfo ShaderIR::GetSamplerInfo(SamplerInfo info, u32 offset,
  328. std::optional<u32> buffer) {
  329. if (info.IsComplete()) {
  330. return info;
  331. }
  332. const auto sampler = buffer ? registry.ObtainBindlessSampler(*buffer, offset)
  333. : registry.ObtainBoundSampler(offset);
  334. if (!sampler) {
  335. LOG_WARNING(HW_GPU, "Unknown sampler info");
  336. info.type = info.type.value_or(Tegra::Shader::TextureType::Texture2D);
  337. info.is_array = info.is_array.value_or(false);
  338. info.is_shadow = info.is_shadow.value_or(false);
  339. info.is_buffer = info.is_buffer.value_or(false);
  340. return info;
  341. }
  342. info.type = info.type.value_or(sampler->texture_type);
  343. info.is_array = info.is_array.value_or(sampler->is_array != 0);
  344. info.is_shadow = info.is_shadow.value_or(sampler->is_shadow != 0);
  345. info.is_buffer = info.is_buffer.value_or(sampler->is_buffer != 0);
  346. return info;
  347. }
  348. std::optional<Sampler> ShaderIR::GetSampler(Tegra::Shader::Sampler sampler,
  349. SamplerInfo sampler_info) {
  350. const auto offset = static_cast<u32>(sampler.index.Value());
  351. const auto info = GetSamplerInfo(sampler_info, offset);
  352. // If this sampler has already been used, return the existing mapping.
  353. const auto it = std::find_if(used_samplers.begin(), used_samplers.end(),
  354. [offset](const Sampler& entry) { return entry.offset == offset; });
  355. if (it != used_samplers.end()) {
  356. ASSERT(!it->is_bindless && it->type == info.type && it->is_array == info.is_array &&
  357. it->is_shadow == info.is_shadow && it->is_buffer == info.is_buffer);
  358. return *it;
  359. }
  360. // Otherwise create a new mapping for this sampler
  361. const auto next_index = static_cast<u32>(used_samplers.size());
  362. return used_samplers.emplace_back(next_index, offset, *info.type, *info.is_array,
  363. *info.is_shadow, *info.is_buffer, false);
  364. }
  365. std::optional<Sampler> ShaderIR::GetBindlessSampler(Tegra::Shader::Register reg, SamplerInfo info,
  366. Node& index_var) {
  367. const Node sampler_register = GetRegister(reg);
  368. const auto [base_node, tracked_sampler_info] =
  369. TrackBindlessSampler(sampler_register, global_code, static_cast<s64>(global_code.size()));
  370. ASSERT(base_node != nullptr);
  371. if (base_node == nullptr) {
  372. return std::nullopt;
  373. }
  374. if (const auto bindless_sampler_info =
  375. std::get_if<BindlessSamplerNode>(&*tracked_sampler_info)) {
  376. const u32 buffer = bindless_sampler_info->GetIndex();
  377. const u32 offset = bindless_sampler_info->GetOffset();
  378. info = GetSamplerInfo(info, offset, buffer);
  379. // If this sampler has already been used, return the existing mapping.
  380. const auto it = std::find_if(used_samplers.begin(), used_samplers.end(),
  381. [buffer = buffer, offset = offset](const Sampler& entry) {
  382. return entry.buffer == buffer && entry.offset == offset;
  383. });
  384. if (it != used_samplers.end()) {
  385. ASSERT(it->is_bindless && it->type == info.type && it->is_array == info.is_array &&
  386. it->is_shadow == info.is_shadow);
  387. return *it;
  388. }
  389. // Otherwise create a new mapping for this sampler
  390. const auto next_index = static_cast<u32>(used_samplers.size());
  391. return used_samplers.emplace_back(next_index, offset, buffer, *info.type, *info.is_array,
  392. *info.is_shadow, *info.is_buffer, false);
  393. }
  394. if (const auto array_sampler_info = std::get_if<ArraySamplerNode>(&*tracked_sampler_info)) {
  395. const u32 base_offset = array_sampler_info->GetBaseOffset() / 4;
  396. index_var = GetCustomVariable(array_sampler_info->GetIndexVar());
  397. info = GetSamplerInfo(info, base_offset);
  398. // If this sampler has already been used, return the existing mapping.
  399. const auto it = std::find_if(
  400. used_samplers.begin(), used_samplers.end(),
  401. [base_offset](const Sampler& entry) { return entry.offset == base_offset; });
  402. if (it != used_samplers.end()) {
  403. ASSERT(!it->is_bindless && it->type == info.type && it->is_array == info.is_array &&
  404. it->is_shadow == info.is_shadow && it->is_buffer == info.is_buffer &&
  405. it->is_indexed);
  406. return *it;
  407. }
  408. uses_indexed_samplers = true;
  409. // Otherwise create a new mapping for this sampler
  410. const auto next_index = static_cast<u32>(used_samplers.size());
  411. return used_samplers.emplace_back(next_index, base_offset, *info.type, *info.is_array,
  412. *info.is_shadow, *info.is_buffer, true);
  413. }
  414. return std::nullopt;
  415. }
  416. void ShaderIR::WriteTexInstructionFloat(NodeBlock& bb, Instruction instr, const Node4& components) {
  417. u32 dest_elem = 0;
  418. for (u32 elem = 0; elem < 4; ++elem) {
  419. if (!instr.tex.IsComponentEnabled(elem)) {
  420. // Skip disabled components
  421. continue;
  422. }
  423. SetTemporary(bb, dest_elem++, components[elem]);
  424. }
  425. // After writing values in temporals, move them to the real registers
  426. for (u32 i = 0; i < dest_elem; ++i) {
  427. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  428. }
  429. }
  430. void ShaderIR::WriteTexsInstructionFloat(NodeBlock& bb, Instruction instr, const Node4& components,
  431. bool ignore_mask) {
  432. // TEXS has two destination registers and a swizzle. The first two elements in the swizzle
  433. // go into gpr0+0 and gpr0+1, and the rest goes into gpr28+0 and gpr28+1
  434. u32 dest_elem = 0;
  435. for (u32 component = 0; component < 4; ++component) {
  436. if (!instr.texs.IsComponentEnabled(component) && !ignore_mask)
  437. continue;
  438. SetTemporary(bb, dest_elem++, components[component]);
  439. }
  440. for (u32 i = 0; i < dest_elem; ++i) {
  441. if (i < 2) {
  442. // Write the first two swizzle components to gpr0 and gpr0+1
  443. SetRegister(bb, instr.gpr0.Value() + i % 2, GetTemporary(i));
  444. } else {
  445. ASSERT(instr.texs.HasTwoDestinations());
  446. // Write the rest of the swizzle components to gpr28 and gpr28+1
  447. SetRegister(bb, instr.gpr28.Value() + i % 2, GetTemporary(i));
  448. }
  449. }
  450. }
  451. void ShaderIR::WriteTexsInstructionHalfFloat(NodeBlock& bb, Instruction instr,
  452. const Node4& components, bool ignore_mask) {
  453. // TEXS.F16 destionation registers are packed in two registers in pairs (just like any half
  454. // float instruction).
  455. Node4 values;
  456. u32 dest_elem = 0;
  457. for (u32 component = 0; component < 4; ++component) {
  458. if (!instr.texs.IsComponentEnabled(component) && !ignore_mask)
  459. continue;
  460. values[dest_elem++] = components[component];
  461. }
  462. if (dest_elem == 0)
  463. return;
  464. std::generate(values.begin() + dest_elem, values.end(), [&]() { return Immediate(0); });
  465. const Node first_value = Operation(OperationCode::HPack2, values[0], values[1]);
  466. if (dest_elem <= 2) {
  467. SetRegister(bb, instr.gpr0, first_value);
  468. return;
  469. }
  470. SetTemporary(bb, 0, first_value);
  471. SetTemporary(bb, 1, Operation(OperationCode::HPack2, values[2], values[3]));
  472. SetRegister(bb, instr.gpr0, GetTemporary(0));
  473. SetRegister(bb, instr.gpr28, GetTemporary(1));
  474. }
  475. Node4 ShaderIR::GetTextureCode(Instruction instr, TextureType texture_type,
  476. TextureProcessMode process_mode, std::vector<Node> coords,
  477. Node array, Node depth_compare, u32 bias_offset,
  478. std::vector<Node> aoffi,
  479. std::optional<Tegra::Shader::Register> bindless_reg) {
  480. const bool is_array = array != nullptr;
  481. const bool is_shadow = depth_compare != nullptr;
  482. const bool is_bindless = bindless_reg.has_value();
  483. UNIMPLEMENTED_IF(texture_type == TextureType::TextureCube && is_array && is_shadow);
  484. ASSERT_MSG(texture_type != TextureType::Texture3D || !is_array || !is_shadow,
  485. "Illegal texture type");
  486. SamplerInfo info;
  487. info.type = texture_type;
  488. info.is_array = is_array;
  489. info.is_shadow = is_shadow;
  490. info.is_buffer = false;
  491. Node index_var;
  492. const std::optional<Sampler> sampler = is_bindless
  493. ? GetBindlessSampler(*bindless_reg, info, index_var)
  494. : GetSampler(instr.sampler, info);
  495. if (!sampler) {
  496. return {Immediate(0), Immediate(0), Immediate(0), Immediate(0)};
  497. }
  498. const bool lod_needed = process_mode == TextureProcessMode::LZ ||
  499. process_mode == TextureProcessMode::LL ||
  500. process_mode == TextureProcessMode::LLA;
  501. const OperationCode opcode = lod_needed ? OperationCode::TextureLod : OperationCode::Texture;
  502. Node bias;
  503. Node lod;
  504. switch (process_mode) {
  505. case TextureProcessMode::None:
  506. break;
  507. case TextureProcessMode::LZ:
  508. lod = Immediate(0.0f);
  509. break;
  510. case TextureProcessMode::LB:
  511. // If present, lod or bias are always stored in the register indexed by the gpr20 field with
  512. // an offset depending on the usage of the other registers.
  513. bias = GetRegister(instr.gpr20.Value() + bias_offset);
  514. break;
  515. case TextureProcessMode::LL:
  516. lod = GetRegister(instr.gpr20.Value() + bias_offset);
  517. break;
  518. default:
  519. UNIMPLEMENTED_MSG("Unimplemented process mode={}", static_cast<u32>(process_mode));
  520. break;
  521. }
  522. Node4 values;
  523. for (u32 element = 0; element < values.size(); ++element) {
  524. MetaTexture meta{*sampler, array, depth_compare, aoffi, {}, {}, bias,
  525. lod, {}, element, index_var};
  526. values[element] = Operation(opcode, meta, coords);
  527. }
  528. return values;
  529. }
  530. Node4 ShaderIR::GetTexCode(Instruction instr, TextureType texture_type,
  531. TextureProcessMode process_mode, bool depth_compare, bool is_array,
  532. bool is_aoffi, std::optional<Tegra::Shader::Register> bindless_reg) {
  533. const bool lod_bias_enabled{
  534. (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ)};
  535. const bool is_bindless = bindless_reg.has_value();
  536. u64 parameter_register = instr.gpr20.Value();
  537. if (is_bindless) {
  538. ++parameter_register;
  539. }
  540. const u32 bias_lod_offset = (is_bindless ? 1 : 0);
  541. if (lod_bias_enabled) {
  542. ++parameter_register;
  543. }
  544. const auto coord_counts = ValidateAndGetCoordinateElement(texture_type, depth_compare, is_array,
  545. lod_bias_enabled, 4, 5);
  546. const auto coord_count = std::get<0>(coord_counts);
  547. // If enabled arrays index is always stored in the gpr8 field
  548. const u64 array_register = instr.gpr8.Value();
  549. // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
  550. const u64 coord_register = array_register + (is_array ? 1 : 0);
  551. std::vector<Node> coords;
  552. for (std::size_t i = 0; i < coord_count; ++i) {
  553. coords.push_back(GetRegister(coord_register + i));
  554. }
  555. // 1D.DC in OpenGL the 2nd component is ignored.
  556. if (depth_compare && !is_array && texture_type == TextureType::Texture1D) {
  557. coords.push_back(Immediate(0.0f));
  558. }
  559. const Node array = is_array ? GetRegister(array_register) : nullptr;
  560. std::vector<Node> aoffi;
  561. if (is_aoffi) {
  562. aoffi = GetAoffiCoordinates(GetRegister(parameter_register++), coord_count, false);
  563. }
  564. Node dc;
  565. if (depth_compare) {
  566. // Depth is always stored in the register signaled by gpr20 or in the next register if lod
  567. // or bias are used
  568. dc = GetRegister(parameter_register++);
  569. }
  570. return GetTextureCode(instr, texture_type, process_mode, coords, array, dc, bias_lod_offset,
  571. aoffi, bindless_reg);
  572. }
  573. Node4 ShaderIR::GetTexsCode(Instruction instr, TextureType texture_type,
  574. TextureProcessMode process_mode, bool depth_compare, bool is_array) {
  575. const bool lod_bias_enabled =
  576. (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ);
  577. const auto coord_counts = ValidateAndGetCoordinateElement(texture_type, depth_compare, is_array,
  578. lod_bias_enabled, 4, 4);
  579. const auto coord_count = std::get<0>(coord_counts);
  580. // If enabled arrays index is always stored in the gpr8 field
  581. const u64 array_register = instr.gpr8.Value();
  582. // First coordinate index is stored in gpr8 field or (gpr8 + 1) when arrays are used
  583. const u64 coord_register = array_register + (is_array ? 1 : 0);
  584. const u64 last_coord_register =
  585. (is_array || !(lod_bias_enabled || depth_compare) || (coord_count > 2))
  586. ? static_cast<u64>(instr.gpr20.Value())
  587. : coord_register + 1;
  588. const u32 bias_offset = coord_count > 2 ? 1 : 0;
  589. std::vector<Node> coords;
  590. for (std::size_t i = 0; i < coord_count; ++i) {
  591. const bool last = (i == (coord_count - 1)) && (coord_count > 1);
  592. coords.push_back(GetRegister(last ? last_coord_register : coord_register + i));
  593. }
  594. const Node array = is_array ? GetRegister(array_register) : nullptr;
  595. Node dc;
  596. if (depth_compare) {
  597. // Depth is always stored in the register signaled by gpr20 or in the next register if lod
  598. // or bias are used
  599. const u64 depth_register = instr.gpr20.Value() + (lod_bias_enabled ? 1 : 0);
  600. dc = GetRegister(depth_register);
  601. }
  602. return GetTextureCode(instr, texture_type, process_mode, coords, array, dc, bias_offset, {},
  603. {});
  604. }
  605. Node4 ShaderIR::GetTld4Code(Instruction instr, TextureType texture_type, bool depth_compare,
  606. bool is_array, bool is_aoffi, bool is_ptp, bool is_bindless) {
  607. ASSERT_MSG(!(is_aoffi && is_ptp), "AOFFI and PTP can't be enabled at the same time");
  608. const std::size_t coord_count = GetCoordCount(texture_type);
  609. // If enabled arrays index is always stored in the gpr8 field
  610. const u64 array_register = instr.gpr8.Value();
  611. // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
  612. const u64 coord_register = array_register + (is_array ? 1 : 0);
  613. std::vector<Node> coords;
  614. for (std::size_t i = 0; i < coord_count; ++i) {
  615. coords.push_back(GetRegister(coord_register + i));
  616. }
  617. u64 parameter_register = instr.gpr20.Value();
  618. SamplerInfo info;
  619. info.type = texture_type;
  620. info.is_array = is_array;
  621. info.is_shadow = depth_compare;
  622. Node index_var;
  623. const std::optional<Sampler> sampler =
  624. is_bindless ? GetBindlessSampler(parameter_register++, info, index_var)
  625. : GetSampler(instr.sampler, info);
  626. Node4 values;
  627. if (!sampler) {
  628. for (u32 element = 0; element < values.size(); ++element) {
  629. values[element] = Immediate(0);
  630. }
  631. return values;
  632. }
  633. std::vector<Node> aoffi, ptp;
  634. if (is_aoffi) {
  635. aoffi = GetAoffiCoordinates(GetRegister(parameter_register++), coord_count, true);
  636. } else if (is_ptp) {
  637. ptp = GetPtpCoordinates(
  638. {GetRegister(parameter_register++), GetRegister(parameter_register++)});
  639. }
  640. Node dc;
  641. if (depth_compare) {
  642. dc = GetRegister(parameter_register++);
  643. }
  644. const Node component = is_bindless ? Immediate(static_cast<u32>(instr.tld4_b.component))
  645. : Immediate(static_cast<u32>(instr.tld4.component));
  646. for (u32 element = 0; element < values.size(); ++element) {
  647. auto coords_copy = coords;
  648. MetaTexture meta{
  649. *sampler, GetRegister(array_register), dc, aoffi, ptp, {}, {}, {}, component, element,
  650. index_var};
  651. values[element] = Operation(OperationCode::TextureGather, meta, std::move(coords_copy));
  652. }
  653. return values;
  654. }
  655. Node4 ShaderIR::GetTldCode(Tegra::Shader::Instruction instr) {
  656. const auto texture_type{instr.tld.texture_type};
  657. const bool is_array{instr.tld.is_array};
  658. const bool lod_enabled{instr.tld.GetTextureProcessMode() == TextureProcessMode::LL};
  659. const std::size_t coord_count{GetCoordCount(texture_type)};
  660. u64 gpr8_cursor{instr.gpr8.Value()};
  661. const Node array_register{is_array ? GetRegister(gpr8_cursor++) : nullptr};
  662. std::vector<Node> coords;
  663. coords.reserve(coord_count);
  664. for (std::size_t i = 0; i < coord_count; ++i) {
  665. coords.push_back(GetRegister(gpr8_cursor++));
  666. }
  667. u64 gpr20_cursor{instr.gpr20.Value()};
  668. // const Node bindless_register{is_bindless ? GetRegister(gpr20_cursor++) : nullptr};
  669. const Node lod{lod_enabled ? GetRegister(gpr20_cursor++) : Immediate(0u)};
  670. // const Node aoffi_register{is_aoffi ? GetRegister(gpr20_cursor++) : nullptr};
  671. // const Node multisample{is_multisample ? GetRegister(gpr20_cursor++) : nullptr};
  672. const std::optional<Sampler> sampler = GetSampler(instr.sampler, {});
  673. Node4 values;
  674. for (u32 element = 0; element < values.size(); ++element) {
  675. auto coords_copy = coords;
  676. MetaTexture meta{*sampler, array_register, {}, {}, {}, {}, {}, lod, {}, element, {}};
  677. values[element] = Operation(OperationCode::TexelFetch, meta, std::move(coords_copy));
  678. }
  679. return values;
  680. }
  681. Node4 ShaderIR::GetTldsCode(Instruction instr, TextureType texture_type, bool is_array) {
  682. SamplerInfo info;
  683. info.type = texture_type;
  684. info.is_array = is_array;
  685. info.is_shadow = false;
  686. const std::optional<Sampler> sampler = GetSampler(instr.sampler, info);
  687. const std::size_t type_coord_count = GetCoordCount(texture_type);
  688. const bool lod_enabled = instr.tlds.GetTextureProcessMode() == TextureProcessMode::LL;
  689. // If enabled arrays index is always stored in the gpr8 field
  690. const u64 array_register = instr.gpr8.Value();
  691. // if is array gpr20 is used
  692. const u64 coord_register = is_array ? instr.gpr20.Value() : instr.gpr8.Value();
  693. const u64 last_coord_register =
  694. ((type_coord_count > 2) || (type_coord_count == 2 && !lod_enabled)) && !is_array
  695. ? static_cast<u64>(instr.gpr20.Value())
  696. : coord_register + 1;
  697. std::vector<Node> coords;
  698. for (std::size_t i = 0; i < type_coord_count; ++i) {
  699. const bool last = (i == (type_coord_count - 1)) && (type_coord_count > 1);
  700. coords.push_back(GetRegister(last ? last_coord_register : coord_register + i));
  701. }
  702. const Node array = is_array ? GetRegister(array_register) : nullptr;
  703. // When lod is used always is in gpr20
  704. const Node lod = lod_enabled ? GetRegister(instr.gpr20) : Immediate(0);
  705. Node4 values;
  706. for (u32 element = 0; element < values.size(); ++element) {
  707. auto coords_copy = coords;
  708. MetaTexture meta{*sampler, array, {}, {}, {}, {}, {}, lod, {}, element, {}};
  709. values[element] = Operation(OperationCode::TexelFetch, meta, std::move(coords_copy));
  710. }
  711. return values;
  712. }
  713. std::tuple<std::size_t, std::size_t> ShaderIR::ValidateAndGetCoordinateElement(
  714. TextureType texture_type, bool depth_compare, bool is_array, bool lod_bias_enabled,
  715. std::size_t max_coords, std::size_t max_inputs) {
  716. const std::size_t coord_count = GetCoordCount(texture_type);
  717. std::size_t total_coord_count = coord_count + (is_array ? 1 : 0) + (depth_compare ? 1 : 0);
  718. const std::size_t total_reg_count = total_coord_count + (lod_bias_enabled ? 1 : 0);
  719. if (total_coord_count > max_coords || total_reg_count > max_inputs) {
  720. UNIMPLEMENTED_MSG("Unsupported Texture operation");
  721. total_coord_count = std::min(total_coord_count, max_coords);
  722. }
  723. // 1D.DC OpenGL is using a vec3 but 2nd component is ignored later.
  724. total_coord_count +=
  725. (depth_compare && !is_array && texture_type == TextureType::Texture1D) ? 1 : 0;
  726. return {coord_count, total_coord_count};
  727. }
  728. std::vector<Node> ShaderIR::GetAoffiCoordinates(Node aoffi_reg, std::size_t coord_count,
  729. bool is_tld4) {
  730. const std::array coord_offsets = is_tld4 ? std::array{0U, 8U, 16U} : std::array{0U, 4U, 8U};
  731. const u32 size = is_tld4 ? 6 : 4;
  732. const s32 wrap_value = is_tld4 ? 32 : 8;
  733. const s32 diff_value = is_tld4 ? 64 : 16;
  734. const u32 mask = (1U << size) - 1;
  735. std::vector<Node> aoffi;
  736. aoffi.reserve(coord_count);
  737. const auto aoffi_immediate{
  738. TrackImmediate(aoffi_reg, global_code, static_cast<s64>(global_code.size()))};
  739. if (!aoffi_immediate) {
  740. // Variable access, not supported on AMD.
  741. LOG_WARNING(HW_GPU,
  742. "AOFFI constant folding failed, some hardware might have graphical issues");
  743. for (std::size_t coord = 0; coord < coord_count; ++coord) {
  744. const Node value = BitfieldExtract(aoffi_reg, coord_offsets[coord], size);
  745. const Node condition =
  746. Operation(OperationCode::LogicalIGreaterEqual, value, Immediate(wrap_value));
  747. const Node negative = Operation(OperationCode::IAdd, value, Immediate(-diff_value));
  748. aoffi.push_back(Operation(OperationCode::Select, condition, negative, value));
  749. }
  750. return aoffi;
  751. }
  752. for (std::size_t coord = 0; coord < coord_count; ++coord) {
  753. s32 value = (*aoffi_immediate >> coord_offsets[coord]) & mask;
  754. if (value >= wrap_value) {
  755. value -= diff_value;
  756. }
  757. aoffi.push_back(Immediate(value));
  758. }
  759. return aoffi;
  760. }
  761. std::vector<Node> ShaderIR::GetPtpCoordinates(std::array<Node, 2> ptp_regs) {
  762. static constexpr u32 num_entries = 8;
  763. std::vector<Node> ptp;
  764. ptp.reserve(num_entries);
  765. const auto global_size = static_cast<s64>(global_code.size());
  766. const std::optional low = TrackImmediate(ptp_regs[0], global_code, global_size);
  767. const std::optional high = TrackImmediate(ptp_regs[1], global_code, global_size);
  768. if (!low || !high) {
  769. for (u32 entry = 0; entry < num_entries; ++entry) {
  770. const u32 reg = entry / 4;
  771. const u32 offset = entry % 4;
  772. const Node value = BitfieldExtract(ptp_regs[reg], offset * 8, 6);
  773. const Node condition =
  774. Operation(OperationCode::LogicalIGreaterEqual, value, Immediate(32));
  775. const Node negative = Operation(OperationCode::IAdd, value, Immediate(-64));
  776. ptp.push_back(Operation(OperationCode::Select, condition, negative, value));
  777. }
  778. return ptp;
  779. }
  780. const u64 immediate = (static_cast<u64>(*high) << 32) | static_cast<u64>(*low);
  781. for (u32 entry = 0; entry < num_entries; ++entry) {
  782. s32 value = (immediate >> (entry * 8)) & 0b111111;
  783. if (value >= 32) {
  784. value -= 64;
  785. }
  786. ptp.push_back(Immediate(value));
  787. }
  788. return ptp;
  789. }
  790. } // namespace VideoCommon::Shader