texture.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. const SamplerInfo info{TextureType::Texture2D, false, is_depth_compare, false};
  132. const Sampler& sampler = *GetSampler(instr.sampler, info);
  133. Node4 values;
  134. for (u32 element = 0; element < values.size(); ++element) {
  135. auto coords_copy = coords;
  136. MetaTexture meta{sampler, {}, depth_compare, aoffi, {}, {},
  137. {}, {}, component, element, {}};
  138. values[element] = Operation(OperationCode::TextureGather, meta, std::move(coords_copy));
  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. u64 base_reg = instr.gpr8.Value();
  155. const auto derivate_reg = instr.gpr20.Value();
  156. const auto texture_type = instr.txd.texture_type.Value();
  157. const auto coord_count = GetCoordCount(texture_type);
  158. Node index_var{};
  159. const Sampler* sampler =
  160. is_bindless
  161. ? GetBindlessSampler(base_reg, index_var, {{texture_type, is_array, false, false}})
  162. : GetSampler(instr.sampler, {{texture_type, is_array, false, false}});
  163. Node4 values;
  164. if (sampler == nullptr) {
  165. std::generate(values.begin(), values.end(), [] { return Immediate(0); });
  166. WriteTexInstructionFloat(bb, instr, values);
  167. break;
  168. }
  169. if (is_bindless) {
  170. base_reg++;
  171. }
  172. std::vector<Node> coords;
  173. std::vector<Node> derivates;
  174. for (std::size_t i = 0; i < coord_count; ++i) {
  175. coords.push_back(GetRegister(base_reg + i));
  176. const std::size_t derivate = i * 2;
  177. derivates.push_back(GetRegister(derivate_reg + derivate));
  178. derivates.push_back(GetRegister(derivate_reg + derivate + 1));
  179. }
  180. Node array_node = {};
  181. if (is_array) {
  182. const Node info_reg = GetRegister(base_reg + coord_count);
  183. array_node = BitfieldExtract(info_reg, 0, 16);
  184. }
  185. for (u32 element = 0; element < values.size(); ++element) {
  186. MetaTexture meta{*sampler, array_node, {}, {}, {}, derivates,
  187. {}, {}, {}, element, index_var};
  188. values[element] = Operation(OperationCode::TextureGradient, std::move(meta), coords);
  189. }
  190. WriteTexInstructionFloat(bb, instr, values);
  191. break;
  192. }
  193. case OpCode::Id::TXQ_B:
  194. is_bindless = true;
  195. [[fallthrough]];
  196. case OpCode::Id::TXQ: {
  197. // TODO: The new commits on the texture refactor, change the way samplers work.
  198. // Sadly, not all texture instructions specify the type of texture their sampler
  199. // uses. This must be fixed at a later instance.
  200. Node index_var{};
  201. const Sampler* sampler =
  202. is_bindless ? GetBindlessSampler(instr.gpr8, index_var) : GetSampler(instr.sampler);
  203. if (sampler == nullptr) {
  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. auto texture_type = instr.tmml.texture_type.Value();
  248. const bool is_array = instr.tmml.array != 0;
  249. Node index_var{};
  250. const Sampler* sampler =
  251. is_bindless ? GetBindlessSampler(instr.gpr20, index_var) : GetSampler(instr.sampler);
  252. if (sampler == nullptr) {
  253. u32 indexer = 0;
  254. for (u32 element = 0; element < 2; ++element) {
  255. if (!instr.tmml.IsComponentEnabled(element)) {
  256. continue;
  257. }
  258. const Node value = Immediate(0);
  259. SetTemporary(bb, indexer++, value);
  260. }
  261. for (u32 i = 0; i < indexer; ++i) {
  262. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  263. }
  264. break;
  265. }
  266. std::vector<Node> coords;
  267. // TODO: Add coordinates for different samplers once other texture types are implemented.
  268. switch (texture_type) {
  269. case TextureType::Texture1D:
  270. coords.push_back(GetRegister(instr.gpr8));
  271. break;
  272. case TextureType::Texture2D:
  273. coords.push_back(GetRegister(instr.gpr8.Value() + 0));
  274. coords.push_back(GetRegister(instr.gpr8.Value() + 1));
  275. break;
  276. default:
  277. UNIMPLEMENTED_MSG("Unhandled texture type {}", static_cast<u32>(texture_type));
  278. // Fallback to interpreting as a 2D texture for now
  279. coords.push_back(GetRegister(instr.gpr8.Value() + 0));
  280. coords.push_back(GetRegister(instr.gpr8.Value() + 1));
  281. texture_type = TextureType::Texture2D;
  282. }
  283. u32 indexer = 0;
  284. for (u32 element = 0; element < 2; ++element) {
  285. if (!instr.tmml.IsComponentEnabled(element)) {
  286. continue;
  287. }
  288. auto params = coords;
  289. MetaTexture meta{*sampler, {}, {}, {}, {}, {}, {}, {}, {}, element, index_var};
  290. const Node value = Operation(OperationCode::TextureQueryLod, meta, std::move(params));
  291. SetTemporary(bb, indexer++, value);
  292. }
  293. for (u32 i = 0; i < indexer; ++i) {
  294. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  295. }
  296. break;
  297. }
  298. case OpCode::Id::TLD: {
  299. UNIMPLEMENTED_IF_MSG(instr.tld.aoffi, "AOFFI is not implemented");
  300. UNIMPLEMENTED_IF_MSG(instr.tld.ms, "MS is not implemented");
  301. UNIMPLEMENTED_IF_MSG(instr.tld.cl, "CL is not implemented");
  302. WriteTexInstructionFloat(bb, instr, GetTldCode(instr));
  303. break;
  304. }
  305. case OpCode::Id::TLDS: {
  306. const TextureType texture_type{instr.tlds.GetTextureType()};
  307. const bool is_array{instr.tlds.IsArrayTexture()};
  308. UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(TextureMiscMode::AOFFI),
  309. "AOFFI is not implemented");
  310. UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(TextureMiscMode::MZ), "MZ is not implemented");
  311. const Node4 components = GetTldsCode(instr, texture_type, is_array);
  312. if (instr.tlds.fp32_flag) {
  313. WriteTexsInstructionFloat(bb, instr, components);
  314. } else {
  315. WriteTexsInstructionHalfFloat(bb, instr, components);
  316. }
  317. break;
  318. }
  319. default:
  320. UNIMPLEMENTED_MSG("Unhandled memory instruction: {}", opcode->get().GetName());
  321. }
  322. return pc;
  323. }
  324. ShaderIR::SamplerInfo ShaderIR::GetSamplerInfo(std::optional<SamplerInfo> sampler_info, u32 offset,
  325. std::optional<u32> buffer) {
  326. if (sampler_info) {
  327. return *sampler_info;
  328. }
  329. const auto sampler = buffer ? registry.ObtainBindlessSampler(*buffer, offset)
  330. : registry.ObtainBoundSampler(offset);
  331. if (!sampler) {
  332. LOG_WARNING(HW_GPU, "Unknown sampler info");
  333. return SamplerInfo{TextureType::Texture2D, false, false, false};
  334. }
  335. return SamplerInfo{sampler->texture_type, sampler->is_array != 0, sampler->is_shadow != 0,
  336. sampler->is_buffer != 0};
  337. }
  338. const Sampler* ShaderIR::GetSampler(const Tegra::Shader::Sampler& sampler,
  339. std::optional<SamplerInfo> sampler_info) {
  340. const auto offset = static_cast<u32>(sampler.index.Value());
  341. const auto info = GetSamplerInfo(sampler_info, offset);
  342. // If this sampler has already been used, return the existing mapping.
  343. const auto it =
  344. std::find_if(used_samplers.begin(), used_samplers.end(),
  345. [offset](const Sampler& entry) { return entry.GetOffset() == offset; });
  346. if (it != used_samplers.end()) {
  347. ASSERT(!it->IsBindless() && it->GetType() == info.type && it->IsArray() == info.is_array &&
  348. it->IsShadow() == info.is_shadow && it->IsBuffer() == info.is_buffer);
  349. return &*it;
  350. }
  351. // Otherwise create a new mapping for this sampler
  352. const auto next_index = static_cast<u32>(used_samplers.size());
  353. return &used_samplers.emplace_back(next_index, offset, info.type, info.is_array, info.is_shadow,
  354. info.is_buffer, false);
  355. }
  356. const Sampler* ShaderIR::GetBindlessSampler(Tegra::Shader::Register reg, Node& index_var,
  357. std::optional<SamplerInfo> sampler_info) {
  358. const Node sampler_register = GetRegister(reg);
  359. const auto [base_node, tracked_sampler_info] =
  360. TrackBindlessSampler(sampler_register, global_code, static_cast<s64>(global_code.size()));
  361. ASSERT(base_node != nullptr);
  362. if (base_node == nullptr) {
  363. return nullptr;
  364. }
  365. if (const auto bindless_sampler_info =
  366. std::get_if<BindlessSamplerNode>(&*tracked_sampler_info)) {
  367. const u32 buffer = bindless_sampler_info->GetIndex();
  368. const u32 offset = bindless_sampler_info->GetOffset();
  369. const auto info = GetSamplerInfo(sampler_info, offset, buffer);
  370. // If this sampler has already been used, return the existing mapping.
  371. const auto it =
  372. std::find_if(used_samplers.begin(), used_samplers.end(),
  373. [buffer = buffer, offset = offset](const Sampler& entry) {
  374. return entry.GetBuffer() == buffer && entry.GetOffset() == offset;
  375. });
  376. if (it != used_samplers.end()) {
  377. ASSERT(it->IsBindless() && it->GetType() == info.type &&
  378. it->IsArray() == info.is_array && it->IsShadow() == info.is_shadow);
  379. return &*it;
  380. }
  381. // Otherwise create a new mapping for this sampler
  382. const auto next_index = static_cast<u32>(used_samplers.size());
  383. return &used_samplers.emplace_back(next_index, offset, buffer, info.type, info.is_array,
  384. info.is_shadow, info.is_buffer, false);
  385. } else if (const auto array_sampler_info =
  386. std::get_if<ArraySamplerNode>(&*tracked_sampler_info)) {
  387. const u32 base_offset = array_sampler_info->GetBaseOffset() / 4;
  388. index_var = GetCustomVariable(array_sampler_info->GetIndexVar());
  389. const auto info = GetSamplerInfo(sampler_info, base_offset);
  390. // If this sampler has already been used, return the existing mapping.
  391. const auto it = std::find_if(
  392. used_samplers.begin(), used_samplers.end(),
  393. [base_offset](const Sampler& entry) { return entry.GetOffset() == base_offset; });
  394. if (it != used_samplers.end()) {
  395. ASSERT(!it->IsBindless() && it->GetType() == info.type &&
  396. it->IsArray() == info.is_array && it->IsShadow() == info.is_shadow &&
  397. it->IsBuffer() == info.is_buffer && it->IsIndexed());
  398. return &*it;
  399. }
  400. uses_indexed_samplers = true;
  401. // Otherwise create a new mapping for this sampler
  402. const auto next_index = static_cast<u32>(used_samplers.size());
  403. return &used_samplers.emplace_back(next_index, base_offset, info.type, info.is_array,
  404. info.is_shadow, info.is_buffer, true);
  405. }
  406. return nullptr;
  407. }
  408. void ShaderIR::WriteTexInstructionFloat(NodeBlock& bb, Instruction instr, const Node4& components) {
  409. u32 dest_elem = 0;
  410. for (u32 elem = 0; elem < 4; ++elem) {
  411. if (!instr.tex.IsComponentEnabled(elem)) {
  412. // Skip disabled components
  413. continue;
  414. }
  415. SetTemporary(bb, dest_elem++, components[elem]);
  416. }
  417. // After writing values in temporals, move them to the real registers
  418. for (u32 i = 0; i < dest_elem; ++i) {
  419. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  420. }
  421. }
  422. void ShaderIR::WriteTexsInstructionFloat(NodeBlock& bb, Instruction instr, const Node4& components,
  423. bool ignore_mask) {
  424. // TEXS has two destination registers and a swizzle. The first two elements in the swizzle
  425. // go into gpr0+0 and gpr0+1, and the rest goes into gpr28+0 and gpr28+1
  426. u32 dest_elem = 0;
  427. for (u32 component = 0; component < 4; ++component) {
  428. if (!instr.texs.IsComponentEnabled(component) && !ignore_mask)
  429. continue;
  430. SetTemporary(bb, dest_elem++, components[component]);
  431. }
  432. for (u32 i = 0; i < dest_elem; ++i) {
  433. if (i < 2) {
  434. // Write the first two swizzle components to gpr0 and gpr0+1
  435. SetRegister(bb, instr.gpr0.Value() + i % 2, GetTemporary(i));
  436. } else {
  437. ASSERT(instr.texs.HasTwoDestinations());
  438. // Write the rest of the swizzle components to gpr28 and gpr28+1
  439. SetRegister(bb, instr.gpr28.Value() + i % 2, GetTemporary(i));
  440. }
  441. }
  442. }
  443. void ShaderIR::WriteTexsInstructionHalfFloat(NodeBlock& bb, Instruction instr,
  444. const Node4& components, bool ignore_mask) {
  445. // TEXS.F16 destionation registers are packed in two registers in pairs (just like any half
  446. // float instruction).
  447. Node4 values;
  448. u32 dest_elem = 0;
  449. for (u32 component = 0; component < 4; ++component) {
  450. if (!instr.texs.IsComponentEnabled(component) && !ignore_mask)
  451. continue;
  452. values[dest_elem++] = components[component];
  453. }
  454. if (dest_elem == 0)
  455. return;
  456. std::generate(values.begin() + dest_elem, values.end(), [&]() { return Immediate(0); });
  457. const Node first_value = Operation(OperationCode::HPack2, values[0], values[1]);
  458. if (dest_elem <= 2) {
  459. SetRegister(bb, instr.gpr0, first_value);
  460. return;
  461. }
  462. SetTemporary(bb, 0, first_value);
  463. SetTemporary(bb, 1, Operation(OperationCode::HPack2, values[2], values[3]));
  464. SetRegister(bb, instr.gpr0, GetTemporary(0));
  465. SetRegister(bb, instr.gpr28, GetTemporary(1));
  466. }
  467. Node4 ShaderIR::GetTextureCode(Instruction instr, TextureType texture_type,
  468. TextureProcessMode process_mode, std::vector<Node> coords,
  469. Node array, Node depth_compare, u32 bias_offset,
  470. std::vector<Node> aoffi,
  471. std::optional<Tegra::Shader::Register> bindless_reg) {
  472. const bool is_array = array != nullptr;
  473. const bool is_shadow = depth_compare != nullptr;
  474. const bool is_bindless = bindless_reg.has_value();
  475. UNIMPLEMENTED_IF(texture_type == TextureType::TextureCube && is_array && is_shadow);
  476. ASSERT_MSG(texture_type != TextureType::Texture3D || !is_array || !is_shadow,
  477. "Illegal texture type");
  478. const SamplerInfo info{texture_type, is_array, is_shadow, false};
  479. Node index_var;
  480. const Sampler* sampler = is_bindless ? GetBindlessSampler(*bindless_reg, index_var, info)
  481. : GetSampler(instr.sampler, info);
  482. if (!sampler) {
  483. return {Immediate(0), Immediate(0), Immediate(0), Immediate(0)};
  484. }
  485. const bool lod_needed = process_mode == TextureProcessMode::LZ ||
  486. process_mode == TextureProcessMode::LL ||
  487. process_mode == TextureProcessMode::LLA;
  488. const OperationCode opcode = lod_needed ? OperationCode::TextureLod : OperationCode::Texture;
  489. Node bias;
  490. Node lod;
  491. switch (process_mode) {
  492. case TextureProcessMode::None:
  493. break;
  494. case TextureProcessMode::LZ:
  495. lod = Immediate(0.0f);
  496. break;
  497. case TextureProcessMode::LB:
  498. // If present, lod or bias are always stored in the register indexed by the gpr20 field with
  499. // an offset depending on the usage of the other registers.
  500. bias = GetRegister(instr.gpr20.Value() + bias_offset);
  501. break;
  502. case TextureProcessMode::LL:
  503. lod = GetRegister(instr.gpr20.Value() + bias_offset);
  504. break;
  505. default:
  506. UNIMPLEMENTED_MSG("Unimplemented process mode={}", static_cast<u32>(process_mode));
  507. break;
  508. }
  509. Node4 values;
  510. for (u32 element = 0; element < values.size(); ++element) {
  511. MetaTexture meta{*sampler, array, depth_compare, aoffi, {}, {}, bias,
  512. lod, {}, element, index_var};
  513. values[element] = Operation(opcode, meta, coords);
  514. }
  515. return values;
  516. }
  517. Node4 ShaderIR::GetTexCode(Instruction instr, TextureType texture_type,
  518. TextureProcessMode process_mode, bool depth_compare, bool is_array,
  519. bool is_aoffi, std::optional<Tegra::Shader::Register> bindless_reg) {
  520. const bool lod_bias_enabled{
  521. (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ)};
  522. const bool is_bindless = bindless_reg.has_value();
  523. u64 parameter_register = instr.gpr20.Value();
  524. if (is_bindless) {
  525. ++parameter_register;
  526. }
  527. const u32 bias_lod_offset = (is_bindless ? 1 : 0);
  528. if (lod_bias_enabled) {
  529. ++parameter_register;
  530. }
  531. const auto [coord_count, total_coord_count] = ValidateAndGetCoordinateElement(
  532. texture_type, depth_compare, is_array, lod_bias_enabled, 4, 5);
  533. // If enabled arrays index is always stored in the gpr8 field
  534. const u64 array_register = instr.gpr8.Value();
  535. // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
  536. const u64 coord_register = array_register + (is_array ? 1 : 0);
  537. std::vector<Node> coords;
  538. for (std::size_t i = 0; i < coord_count; ++i) {
  539. coords.push_back(GetRegister(coord_register + i));
  540. }
  541. // 1D.DC in OpenGL the 2nd component is ignored.
  542. if (depth_compare && !is_array && texture_type == TextureType::Texture1D) {
  543. coords.push_back(Immediate(0.0f));
  544. }
  545. const Node array = is_array ? GetRegister(array_register) : nullptr;
  546. std::vector<Node> aoffi;
  547. if (is_aoffi) {
  548. aoffi = GetAoffiCoordinates(GetRegister(parameter_register++), coord_count, false);
  549. }
  550. Node dc;
  551. if (depth_compare) {
  552. // Depth is always stored in the register signaled by gpr20 or in the next register if lod
  553. // or bias are used
  554. dc = GetRegister(parameter_register++);
  555. }
  556. return GetTextureCode(instr, texture_type, process_mode, coords, array, dc, bias_lod_offset,
  557. aoffi, bindless_reg);
  558. }
  559. Node4 ShaderIR::GetTexsCode(Instruction instr, TextureType texture_type,
  560. TextureProcessMode process_mode, bool depth_compare, bool is_array) {
  561. const bool lod_bias_enabled =
  562. (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ);
  563. const auto [coord_count, total_coord_count] = ValidateAndGetCoordinateElement(
  564. texture_type, depth_compare, is_array, lod_bias_enabled, 4, 4);
  565. // If enabled arrays index is always stored in the gpr8 field
  566. const u64 array_register = instr.gpr8.Value();
  567. // First coordinate index is stored in gpr8 field or (gpr8 + 1) when arrays are used
  568. const u64 coord_register = array_register + (is_array ? 1 : 0);
  569. const u64 last_coord_register =
  570. (is_array || !(lod_bias_enabled || depth_compare) || (coord_count > 2))
  571. ? static_cast<u64>(instr.gpr20.Value())
  572. : coord_register + 1;
  573. const u32 bias_offset = coord_count > 2 ? 1 : 0;
  574. std::vector<Node> coords;
  575. for (std::size_t i = 0; i < coord_count; ++i) {
  576. const bool last = (i == (coord_count - 1)) && (coord_count > 1);
  577. coords.push_back(GetRegister(last ? last_coord_register : coord_register + i));
  578. }
  579. const Node array = is_array ? GetRegister(array_register) : nullptr;
  580. Node dc;
  581. if (depth_compare) {
  582. // Depth is always stored in the register signaled by gpr20 or in the next register if lod
  583. // or bias are used
  584. const u64 depth_register = instr.gpr20.Value() + (lod_bias_enabled ? 1 : 0);
  585. dc = GetRegister(depth_register);
  586. }
  587. return GetTextureCode(instr, texture_type, process_mode, coords, array, dc, bias_offset, {},
  588. {});
  589. }
  590. Node4 ShaderIR::GetTld4Code(Instruction instr, TextureType texture_type, bool depth_compare,
  591. bool is_array, bool is_aoffi, bool is_ptp, bool is_bindless) {
  592. ASSERT_MSG(!(is_aoffi && is_ptp), "AOFFI and PTP can't be enabled at the same time");
  593. const std::size_t coord_count = GetCoordCount(texture_type);
  594. // If enabled arrays index is always stored in the gpr8 field
  595. const u64 array_register = instr.gpr8.Value();
  596. // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
  597. const u64 coord_register = array_register + (is_array ? 1 : 0);
  598. std::vector<Node> coords;
  599. for (std::size_t i = 0; i < coord_count; ++i) {
  600. coords.push_back(GetRegister(coord_register + i));
  601. }
  602. u64 parameter_register = instr.gpr20.Value();
  603. const SamplerInfo info{texture_type, is_array, depth_compare, false};
  604. Node index_var{};
  605. const Sampler* sampler = is_bindless ? GetBindlessSampler(parameter_register++, index_var, info)
  606. : GetSampler(instr.sampler, info);
  607. Node4 values;
  608. if (sampler == nullptr) {
  609. for (u32 element = 0; element < values.size(); ++element) {
  610. values[element] = Immediate(0);
  611. }
  612. return values;
  613. }
  614. std::vector<Node> aoffi, ptp;
  615. if (is_aoffi) {
  616. aoffi = GetAoffiCoordinates(GetRegister(parameter_register++), coord_count, true);
  617. } else if (is_ptp) {
  618. ptp = GetPtpCoordinates(
  619. {GetRegister(parameter_register++), GetRegister(parameter_register++)});
  620. }
  621. Node dc;
  622. if (depth_compare) {
  623. dc = GetRegister(parameter_register++);
  624. }
  625. const Node component = is_bindless ? Immediate(static_cast<u32>(instr.tld4_b.component))
  626. : Immediate(static_cast<u32>(instr.tld4.component));
  627. for (u32 element = 0; element < values.size(); ++element) {
  628. auto coords_copy = coords;
  629. MetaTexture meta{
  630. *sampler, GetRegister(array_register), dc, aoffi, ptp, {}, {}, {}, component, element,
  631. index_var};
  632. values[element] = Operation(OperationCode::TextureGather, meta, std::move(coords_copy));
  633. }
  634. return values;
  635. }
  636. Node4 ShaderIR::GetTldCode(Tegra::Shader::Instruction instr) {
  637. const auto texture_type{instr.tld.texture_type};
  638. const bool is_array{instr.tld.is_array};
  639. const bool lod_enabled{instr.tld.GetTextureProcessMode() == TextureProcessMode::LL};
  640. const std::size_t coord_count{GetCoordCount(texture_type)};
  641. u64 gpr8_cursor{instr.gpr8.Value()};
  642. const Node array_register{is_array ? GetRegister(gpr8_cursor++) : nullptr};
  643. std::vector<Node> coords;
  644. coords.reserve(coord_count);
  645. for (std::size_t i = 0; i < coord_count; ++i) {
  646. coords.push_back(GetRegister(gpr8_cursor++));
  647. }
  648. u64 gpr20_cursor{instr.gpr20.Value()};
  649. // const Node bindless_register{is_bindless ? GetRegister(gpr20_cursor++) : nullptr};
  650. const Node lod{lod_enabled ? GetRegister(gpr20_cursor++) : Immediate(0u)};
  651. // const Node aoffi_register{is_aoffi ? GetRegister(gpr20_cursor++) : nullptr};
  652. // const Node multisample{is_multisample ? GetRegister(gpr20_cursor++) : nullptr};
  653. const auto& sampler = *GetSampler(instr.sampler);
  654. Node4 values;
  655. for (u32 element = 0; element < values.size(); ++element) {
  656. auto coords_copy = coords;
  657. MetaTexture meta{sampler, array_register, {}, {}, {}, {}, {}, lod, {}, element, {}};
  658. values[element] = Operation(OperationCode::TexelFetch, meta, std::move(coords_copy));
  659. }
  660. return values;
  661. }
  662. Node4 ShaderIR::GetTldsCode(Instruction instr, TextureType texture_type, bool is_array) {
  663. const Sampler& sampler = *GetSampler(instr.sampler);
  664. const std::size_t type_coord_count = GetCoordCount(texture_type);
  665. const bool lod_enabled = instr.tlds.GetTextureProcessMode() == TextureProcessMode::LL;
  666. // If enabled arrays index is always stored in the gpr8 field
  667. const u64 array_register = instr.gpr8.Value();
  668. // if is array gpr20 is used
  669. const u64 coord_register = is_array ? instr.gpr20.Value() : instr.gpr8.Value();
  670. const u64 last_coord_register =
  671. ((type_coord_count > 2) || (type_coord_count == 2 && !lod_enabled)) && !is_array
  672. ? static_cast<u64>(instr.gpr20.Value())
  673. : coord_register + 1;
  674. std::vector<Node> coords;
  675. for (std::size_t i = 0; i < type_coord_count; ++i) {
  676. const bool last = (i == (type_coord_count - 1)) && (type_coord_count > 1);
  677. coords.push_back(GetRegister(last ? last_coord_register : coord_register + i));
  678. }
  679. const Node array = is_array ? GetRegister(array_register) : nullptr;
  680. // When lod is used always is in gpr20
  681. const Node lod = lod_enabled ? GetRegister(instr.gpr20) : Immediate(0);
  682. Node4 values;
  683. for (u32 element = 0; element < values.size(); ++element) {
  684. auto coords_copy = coords;
  685. MetaTexture meta{sampler, array, {}, {}, {}, {}, {}, lod, {}, element, {}};
  686. values[element] = Operation(OperationCode::TexelFetch, meta, std::move(coords_copy));
  687. }
  688. return values;
  689. }
  690. std::tuple<std::size_t, std::size_t> ShaderIR::ValidateAndGetCoordinateElement(
  691. TextureType texture_type, bool depth_compare, bool is_array, bool lod_bias_enabled,
  692. std::size_t max_coords, std::size_t max_inputs) {
  693. const std::size_t coord_count = GetCoordCount(texture_type);
  694. std::size_t total_coord_count = coord_count + (is_array ? 1 : 0) + (depth_compare ? 1 : 0);
  695. const std::size_t total_reg_count = total_coord_count + (lod_bias_enabled ? 1 : 0);
  696. if (total_coord_count > max_coords || total_reg_count > max_inputs) {
  697. UNIMPLEMENTED_MSG("Unsupported Texture operation");
  698. total_coord_count = std::min(total_coord_count, max_coords);
  699. }
  700. // 1D.DC OpenGL is using a vec3 but 2nd component is ignored later.
  701. total_coord_count +=
  702. (depth_compare && !is_array && texture_type == TextureType::Texture1D) ? 1 : 0;
  703. return {coord_count, total_coord_count};
  704. }
  705. std::vector<Node> ShaderIR::GetAoffiCoordinates(Node aoffi_reg, std::size_t coord_count,
  706. bool is_tld4) {
  707. const std::array coord_offsets = is_tld4 ? std::array{0U, 8U, 16U} : std::array{0U, 4U, 8U};
  708. const u32 size = is_tld4 ? 6 : 4;
  709. const s32 wrap_value = is_tld4 ? 32 : 8;
  710. const s32 diff_value = is_tld4 ? 64 : 16;
  711. const u32 mask = (1U << size) - 1;
  712. std::vector<Node> aoffi;
  713. aoffi.reserve(coord_count);
  714. const auto aoffi_immediate{
  715. TrackImmediate(aoffi_reg, global_code, static_cast<s64>(global_code.size()))};
  716. if (!aoffi_immediate) {
  717. // Variable access, not supported on AMD.
  718. LOG_WARNING(HW_GPU,
  719. "AOFFI constant folding failed, some hardware might have graphical issues");
  720. for (std::size_t coord = 0; coord < coord_count; ++coord) {
  721. const Node value = BitfieldExtract(aoffi_reg, coord_offsets[coord], size);
  722. const Node condition =
  723. Operation(OperationCode::LogicalIGreaterEqual, value, Immediate(wrap_value));
  724. const Node negative = Operation(OperationCode::IAdd, value, Immediate(-diff_value));
  725. aoffi.push_back(Operation(OperationCode::Select, condition, negative, value));
  726. }
  727. return aoffi;
  728. }
  729. for (std::size_t coord = 0; coord < coord_count; ++coord) {
  730. s32 value = (*aoffi_immediate >> coord_offsets[coord]) & mask;
  731. if (value >= wrap_value) {
  732. value -= diff_value;
  733. }
  734. aoffi.push_back(Immediate(value));
  735. }
  736. return aoffi;
  737. }
  738. std::vector<Node> ShaderIR::GetPtpCoordinates(std::array<Node, 2> ptp_regs) {
  739. static constexpr u32 num_entries = 8;
  740. std::vector<Node> ptp;
  741. ptp.reserve(num_entries);
  742. const auto global_size = static_cast<s64>(global_code.size());
  743. const std::optional low = TrackImmediate(ptp_regs[0], global_code, global_size);
  744. const std::optional high = TrackImmediate(ptp_regs[1], global_code, global_size);
  745. if (!low || !high) {
  746. for (u32 entry = 0; entry < num_entries; ++entry) {
  747. const u32 reg = entry / 4;
  748. const u32 offset = entry % 4;
  749. const Node value = BitfieldExtract(ptp_regs[reg], offset * 8, 6);
  750. const Node condition =
  751. Operation(OperationCode::LogicalIGreaterEqual, value, Immediate(32));
  752. const Node negative = Operation(OperationCode::IAdd, value, Immediate(-64));
  753. ptp.push_back(Operation(OperationCode::Select, condition, negative, value));
  754. }
  755. return ptp;
  756. }
  757. const u64 immediate = (static_cast<u64>(*high) << 32) | static_cast<u64>(*low);
  758. for (u32 entry = 0; entry < num_entries; ++entry) {
  759. s32 value = (immediate >> (entry * 8)) & 0b111111;
  760. if (value >= 32) {
  761. value -= 64;
  762. }
  763. ptp.push_back(Immediate(value));
  764. }
  765. return ptp;
  766. }
  767. } // namespace VideoCommon::Shader