texture.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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/shader_ir.h"
  14. namespace VideoCommon::Shader {
  15. using Tegra::Shader::Instruction;
  16. using Tegra::Shader::OpCode;
  17. using Tegra::Shader::Register;
  18. using Tegra::Shader::TextureMiscMode;
  19. using Tegra::Shader::TextureProcessMode;
  20. using Tegra::Shader::TextureType;
  21. static std::size_t GetCoordCount(TextureType texture_type) {
  22. switch (texture_type) {
  23. case TextureType::Texture1D:
  24. return 1;
  25. case TextureType::Texture2D:
  26. return 2;
  27. case TextureType::Texture3D:
  28. case TextureType::TextureCube:
  29. return 3;
  30. default:
  31. UNIMPLEMENTED_MSG("Unhandled texture type: {}", static_cast<u32>(texture_type));
  32. return 0;
  33. }
  34. }
  35. u32 ShaderIR::DecodeTexture(NodeBlock& bb, u32 pc) {
  36. const Instruction instr = {program_code[pc]};
  37. const auto opcode = OpCode::Decode(instr);
  38. bool is_bindless = false;
  39. switch (opcode->get().GetId()) {
  40. case OpCode::Id::TEX: {
  41. const TextureType texture_type{instr.tex.texture_type};
  42. const bool is_array = instr.tex.array != 0;
  43. const bool is_aoffi = instr.tex.UsesMiscMode(TextureMiscMode::AOFFI);
  44. const bool depth_compare = instr.tex.UsesMiscMode(TextureMiscMode::DC);
  45. const auto process_mode = instr.tex.GetTextureProcessMode();
  46. WriteTexInstructionFloat(
  47. bb, instr,
  48. GetTexCode(instr, texture_type, process_mode, depth_compare, is_array, is_aoffi, {}));
  49. break;
  50. }
  51. case OpCode::Id::TEX_B: {
  52. UNIMPLEMENTED_IF_MSG(instr.tex.UsesMiscMode(TextureMiscMode::AOFFI),
  53. "AOFFI is not implemented");
  54. const TextureType texture_type{instr.tex_b.texture_type};
  55. const bool is_array = instr.tex_b.array != 0;
  56. const bool is_aoffi = instr.tex.UsesMiscMode(TextureMiscMode::AOFFI);
  57. const bool depth_compare = instr.tex_b.UsesMiscMode(TextureMiscMode::DC);
  58. const auto process_mode = instr.tex_b.GetTextureProcessMode();
  59. WriteTexInstructionFloat(bb, instr,
  60. GetTexCode(instr, texture_type, process_mode, depth_compare,
  61. is_array, is_aoffi, {instr.gpr20}));
  62. break;
  63. }
  64. case OpCode::Id::TEXS: {
  65. const TextureType texture_type{instr.texs.GetTextureType()};
  66. const bool is_array{instr.texs.IsArrayTexture()};
  67. const bool depth_compare = instr.texs.UsesMiscMode(TextureMiscMode::DC);
  68. const auto process_mode = instr.texs.GetTextureProcessMode();
  69. const Node4 components =
  70. GetTexsCode(instr, texture_type, process_mode, depth_compare, is_array);
  71. if (instr.texs.fp32_flag) {
  72. WriteTexsInstructionFloat(bb, instr, components);
  73. } else {
  74. WriteTexsInstructionHalfFloat(bb, instr, components);
  75. }
  76. break;
  77. }
  78. case OpCode::Id::TLD4_B: {
  79. is_bindless = true;
  80. [[fallthrough]];
  81. }
  82. case OpCode::Id::TLD4: {
  83. UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(TextureMiscMode::NDV),
  84. "NDV is not implemented");
  85. const auto texture_type = instr.tld4.texture_type.Value();
  86. const bool depth_compare = is_bindless ? instr.tld4_b.UsesMiscMode(TextureMiscMode::DC)
  87. : instr.tld4.UsesMiscMode(TextureMiscMode::DC);
  88. const bool is_array = instr.tld4.array != 0;
  89. const bool is_aoffi = is_bindless ? instr.tld4_b.UsesMiscMode(TextureMiscMode::AOFFI)
  90. : instr.tld4.UsesMiscMode(TextureMiscMode::AOFFI);
  91. const bool is_ptp = is_bindless ? instr.tld4_b.UsesMiscMode(TextureMiscMode::PTP)
  92. : instr.tld4.UsesMiscMode(TextureMiscMode::PTP);
  93. WriteTexInstructionFloat(bb, instr,
  94. GetTld4Code(instr, texture_type, depth_compare, is_array, is_aoffi,
  95. is_ptp, is_bindless));
  96. break;
  97. }
  98. case OpCode::Id::TLD4S: {
  99. constexpr std::size_t num_coords = 2;
  100. const bool is_aoffi = instr.tld4s.UsesMiscMode(TextureMiscMode::AOFFI);
  101. const bool is_depth_compare = instr.tld4s.UsesMiscMode(TextureMiscMode::DC);
  102. const Node op_a = GetRegister(instr.gpr8);
  103. const Node op_b = GetRegister(instr.gpr20);
  104. // TODO(Subv): Figure out how the sampler type is encoded in the TLD4S instruction.
  105. std::vector<Node> coords;
  106. std::vector<Node> aoffi;
  107. Node depth_compare;
  108. if (is_depth_compare) {
  109. // Note: TLD4S coordinate encoding works just like TEXS's
  110. const Node op_y = GetRegister(instr.gpr8.Value() + 1);
  111. coords.push_back(op_a);
  112. coords.push_back(op_y);
  113. if (is_aoffi) {
  114. aoffi = GetAoffiCoordinates(op_b, num_coords, true);
  115. depth_compare = GetRegister(instr.gpr20.Value() + 1);
  116. } else {
  117. depth_compare = op_b;
  118. }
  119. } else {
  120. // There's no depth compare
  121. coords.push_back(op_a);
  122. if (is_aoffi) {
  123. coords.push_back(GetRegister(instr.gpr8.Value() + 1));
  124. aoffi = GetAoffiCoordinates(op_b, num_coords, true);
  125. } else {
  126. coords.push_back(op_b);
  127. }
  128. }
  129. const Node component = Immediate(static_cast<u32>(instr.tld4s.component));
  130. const SamplerInfo info{TextureType::Texture2D, false, is_depth_compare};
  131. const Sampler& sampler = *GetSampler(instr.sampler, info);
  132. Node4 values;
  133. for (u32 element = 0; element < values.size(); ++element) {
  134. auto coords_copy = coords;
  135. MetaTexture meta{sampler, {}, depth_compare, aoffi, {}, {},
  136. {}, {}, component, element, {}};
  137. values[element] = Operation(OperationCode::TextureGather, meta, std::move(coords_copy));
  138. }
  139. if (instr.tld4s.fp16_flag) {
  140. WriteTexsInstructionHalfFloat(bb, instr, values, true);
  141. } else {
  142. WriteTexsInstructionFloat(bb, instr, values, true);
  143. }
  144. break;
  145. }
  146. case OpCode::Id::TXD_B:
  147. is_bindless = true;
  148. [[fallthrough]];
  149. case OpCode::Id::TXD: {
  150. UNIMPLEMENTED_IF_MSG(instr.txd.UsesMiscMode(TextureMiscMode::AOFFI),
  151. "AOFFI is not implemented");
  152. const bool is_array = instr.txd.is_array != 0;
  153. u64 base_reg = instr.gpr8.Value();
  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. Node index_var{};
  158. const Sampler* sampler =
  159. is_bindless ? GetBindlessSampler(base_reg, index_var, {{texture_type, is_array, false}})
  160. : GetSampler(instr.sampler, {{texture_type, is_array, false}});
  161. Node4 values;
  162. if (sampler == nullptr) {
  163. for (u32 element = 0; element < values.size(); ++element) {
  164. values[element] = Immediate(0);
  165. }
  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 =
  330. buffer ? locker.ObtainBindlessSampler(*buffer, offset) : locker.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 auto is_array = static_cast<bool>(array);
  473. const auto is_shadow = static_cast<bool>(depth_compare);
  474. const bool is_bindless = bindless_reg.has_value();
  475. UNIMPLEMENTED_IF_MSG((texture_type == TextureType::Texture3D && (is_array || is_shadow)) ||
  476. (texture_type == TextureType::TextureCube && is_array && is_shadow),
  477. "This method is not supported.");
  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. Node4 values;
  483. if (sampler == nullptr) {
  484. for (u32 element = 0; element < values.size(); ++element) {
  485. values[element] = Immediate(0);
  486. }
  487. return values;
  488. }
  489. const bool lod_needed = process_mode == TextureProcessMode::LZ ||
  490. process_mode == TextureProcessMode::LL ||
  491. process_mode == TextureProcessMode::LLA;
  492. // LOD selection (either via bias or explicit textureLod) not supported in GL for
  493. // sampler2DArrayShadow and samplerCubeArrayShadow.
  494. const bool gl_lod_supported =
  495. !((texture_type == Tegra::Shader::TextureType::Texture2D && is_array && is_shadow) ||
  496. (texture_type == Tegra::Shader::TextureType::TextureCube && is_array && is_shadow));
  497. const OperationCode read_method =
  498. (lod_needed && gl_lod_supported) ? OperationCode::TextureLod : OperationCode::Texture;
  499. UNIMPLEMENTED_IF(process_mode != TextureProcessMode::None && !gl_lod_supported);
  500. Node bias;
  501. Node lod;
  502. if (process_mode != TextureProcessMode::None && gl_lod_supported) {
  503. switch (process_mode) {
  504. case TextureProcessMode::LZ:
  505. lod = Immediate(0.0f);
  506. break;
  507. case TextureProcessMode::LB:
  508. // If present, lod or bias are always stored in the register
  509. // indexed by the gpr20 field with an offset depending on the
  510. // usage of the other registers
  511. bias = GetRegister(instr.gpr20.Value() + bias_offset);
  512. break;
  513. case TextureProcessMode::LL:
  514. lod = GetRegister(instr.gpr20.Value() + bias_offset);
  515. break;
  516. default:
  517. UNIMPLEMENTED_MSG("Unimplemented process mode={}", static_cast<u32>(process_mode));
  518. break;
  519. }
  520. }
  521. for (u32 element = 0; element < values.size(); ++element) {
  522. auto copy_coords = coords;
  523. MetaTexture meta{*sampler, array, depth_compare, aoffi, {}, {}, bias,
  524. lod, {}, element, index_var};
  525. values[element] = Operation(read_method, meta, std::move(copy_coords));
  526. }
  527. return values;
  528. }
  529. Node4 ShaderIR::GetTexCode(Instruction instr, TextureType texture_type,
  530. TextureProcessMode process_mode, bool depth_compare, bool is_array,
  531. bool is_aoffi, std::optional<Tegra::Shader::Register> bindless_reg) {
  532. const bool lod_bias_enabled{
  533. (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ)};
  534. const bool is_bindless = bindless_reg.has_value();
  535. u64 parameter_register = instr.gpr20.Value();
  536. if (is_bindless) {
  537. ++parameter_register;
  538. }
  539. const u32 bias_lod_offset = (is_bindless ? 1 : 0);
  540. if (lod_bias_enabled) {
  541. ++parameter_register;
  542. }
  543. const auto [coord_count, total_coord_count] = ValidateAndGetCoordinateElement(
  544. texture_type, depth_compare, is_array, lod_bias_enabled, 4, 5);
  545. // If enabled arrays index is always stored in the gpr8 field
  546. const u64 array_register = instr.gpr8.Value();
  547. // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
  548. const u64 coord_register = array_register + (is_array ? 1 : 0);
  549. std::vector<Node> coords;
  550. for (std::size_t i = 0; i < coord_count; ++i) {
  551. coords.push_back(GetRegister(coord_register + i));
  552. }
  553. // 1D.DC in OpenGL the 2nd component is ignored.
  554. if (depth_compare && !is_array && texture_type == TextureType::Texture1D) {
  555. coords.push_back(Immediate(0.0f));
  556. }
  557. const Node array = is_array ? GetRegister(array_register) : nullptr;
  558. std::vector<Node> aoffi;
  559. if (is_aoffi) {
  560. aoffi = GetAoffiCoordinates(GetRegister(parameter_register++), coord_count, false);
  561. }
  562. Node dc;
  563. if (depth_compare) {
  564. // Depth is always stored in the register signaled by gpr20 or in the next register if lod
  565. // or bias are used
  566. dc = GetRegister(parameter_register++);
  567. }
  568. return GetTextureCode(instr, texture_type, process_mode, coords, array, dc, bias_lod_offset,
  569. aoffi, bindless_reg);
  570. }
  571. Node4 ShaderIR::GetTexsCode(Instruction instr, TextureType texture_type,
  572. TextureProcessMode process_mode, bool depth_compare, bool is_array) {
  573. const bool lod_bias_enabled =
  574. (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ);
  575. const auto [coord_count, total_coord_count] = ValidateAndGetCoordinateElement(
  576. texture_type, depth_compare, is_array, lod_bias_enabled, 4, 4);
  577. // If enabled arrays index is always stored in the gpr8 field
  578. const u64 array_register = instr.gpr8.Value();
  579. // First coordinate index is stored in gpr8 field or (gpr8 + 1) when arrays are used
  580. const u64 coord_register = array_register + (is_array ? 1 : 0);
  581. const u64 last_coord_register =
  582. (is_array || !(lod_bias_enabled || depth_compare) || (coord_count > 2))
  583. ? static_cast<u64>(instr.gpr20.Value())
  584. : coord_register + 1;
  585. const u32 bias_offset = coord_count > 2 ? 1 : 0;
  586. std::vector<Node> coords;
  587. for (std::size_t i = 0; i < coord_count; ++i) {
  588. const bool last = (i == (coord_count - 1)) && (coord_count > 1);
  589. coords.push_back(GetRegister(last ? last_coord_register : coord_register + i));
  590. }
  591. const Node array = is_array ? GetRegister(array_register) : nullptr;
  592. Node dc;
  593. if (depth_compare) {
  594. // Depth is always stored in the register signaled by gpr20 or in the next register if lod
  595. // or bias are used
  596. const u64 depth_register = instr.gpr20.Value() + (lod_bias_enabled ? 1 : 0);
  597. dc = GetRegister(depth_register);
  598. }
  599. return GetTextureCode(instr, texture_type, process_mode, coords, array, dc, bias_offset, {},
  600. {});
  601. }
  602. Node4 ShaderIR::GetTld4Code(Instruction instr, TextureType texture_type, bool depth_compare,
  603. bool is_array, bool is_aoffi, bool is_ptp, bool is_bindless) {
  604. ASSERT_MSG(!(is_aoffi && is_ptp), "AOFFI and PTP can't be enabled at the same time");
  605. const std::size_t coord_count = GetCoordCount(texture_type);
  606. // If enabled arrays index is always stored in the gpr8 field
  607. const u64 array_register = instr.gpr8.Value();
  608. // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
  609. const u64 coord_register = array_register + (is_array ? 1 : 0);
  610. std::vector<Node> coords;
  611. for (std::size_t i = 0; i < coord_count; ++i) {
  612. coords.push_back(GetRegister(coord_register + i));
  613. }
  614. u64 parameter_register = instr.gpr20.Value();
  615. const SamplerInfo info{texture_type, is_array, depth_compare, false};
  616. Node index_var{};
  617. const Sampler* sampler = is_bindless ? GetBindlessSampler(parameter_register++, index_var, info)
  618. : GetSampler(instr.sampler, info);
  619. Node4 values;
  620. if (sampler == nullptr) {
  621. for (u32 element = 0; element < values.size(); ++element) {
  622. values[element] = Immediate(0);
  623. }
  624. return values;
  625. }
  626. std::vector<Node> aoffi, ptp;
  627. if (is_aoffi) {
  628. aoffi = GetAoffiCoordinates(GetRegister(parameter_register++), coord_count, true);
  629. } else if (is_ptp) {
  630. ptp = GetPtpCoordinates(
  631. {GetRegister(parameter_register++), GetRegister(parameter_register++)});
  632. }
  633. Node dc;
  634. if (depth_compare) {
  635. dc = GetRegister(parameter_register++);
  636. }
  637. const Node component = is_bindless ? Immediate(static_cast<u32>(instr.tld4_b.component))
  638. : Immediate(static_cast<u32>(instr.tld4.component));
  639. for (u32 element = 0; element < values.size(); ++element) {
  640. auto coords_copy = coords;
  641. MetaTexture meta{
  642. *sampler, GetRegister(array_register), dc, aoffi, ptp, {}, {}, {}, component, element,
  643. index_var};
  644. values[element] = Operation(OperationCode::TextureGather, meta, std::move(coords_copy));
  645. }
  646. return values;
  647. }
  648. Node4 ShaderIR::GetTldCode(Tegra::Shader::Instruction instr) {
  649. const auto texture_type{instr.tld.texture_type};
  650. const bool is_array{instr.tld.is_array};
  651. const bool lod_enabled{instr.tld.GetTextureProcessMode() == TextureProcessMode::LL};
  652. const std::size_t coord_count{GetCoordCount(texture_type)};
  653. u64 gpr8_cursor{instr.gpr8.Value()};
  654. const Node array_register{is_array ? GetRegister(gpr8_cursor++) : nullptr};
  655. std::vector<Node> coords;
  656. coords.reserve(coord_count);
  657. for (std::size_t i = 0; i < coord_count; ++i) {
  658. coords.push_back(GetRegister(gpr8_cursor++));
  659. }
  660. u64 gpr20_cursor{instr.gpr20.Value()};
  661. // const Node bindless_register{is_bindless ? GetRegister(gpr20_cursor++) : nullptr};
  662. const Node lod{lod_enabled ? GetRegister(gpr20_cursor++) : Immediate(0u)};
  663. // const Node aoffi_register{is_aoffi ? GetRegister(gpr20_cursor++) : nullptr};
  664. // const Node multisample{is_multisample ? GetRegister(gpr20_cursor++) : nullptr};
  665. const auto& sampler = *GetSampler(instr.sampler);
  666. Node4 values;
  667. for (u32 element = 0; element < values.size(); ++element) {
  668. auto coords_copy = coords;
  669. MetaTexture meta{sampler, array_register, {}, {}, {}, {}, {}, lod, {}, element, {}};
  670. values[element] = Operation(OperationCode::TexelFetch, meta, std::move(coords_copy));
  671. }
  672. return values;
  673. }
  674. Node4 ShaderIR::GetTldsCode(Instruction instr, TextureType texture_type, bool is_array) {
  675. const Sampler& sampler = *GetSampler(instr.sampler);
  676. const std::size_t type_coord_count = GetCoordCount(texture_type);
  677. const bool lod_enabled = instr.tlds.GetTextureProcessMode() == TextureProcessMode::LL;
  678. // If enabled arrays index is always stored in the gpr8 field
  679. const u64 array_register = instr.gpr8.Value();
  680. // if is array gpr20 is used
  681. const u64 coord_register = is_array ? instr.gpr20.Value() : instr.gpr8.Value();
  682. const u64 last_coord_register =
  683. ((type_coord_count > 2) || (type_coord_count == 2 && !lod_enabled)) && !is_array
  684. ? static_cast<u64>(instr.gpr20.Value())
  685. : coord_register + 1;
  686. std::vector<Node> coords;
  687. for (std::size_t i = 0; i < type_coord_count; ++i) {
  688. const bool last = (i == (type_coord_count - 1)) && (type_coord_count > 1);
  689. coords.push_back(GetRegister(last ? last_coord_register : coord_register + i));
  690. }
  691. const Node array = is_array ? GetRegister(array_register) : nullptr;
  692. // When lod is used always is in gpr20
  693. const Node lod = lod_enabled ? GetRegister(instr.gpr20) : Immediate(0);
  694. // Fill empty entries from the guest sampler
  695. const std::size_t entry_coord_count = GetCoordCount(sampler.GetType());
  696. if (type_coord_count != entry_coord_count) {
  697. LOG_WARNING(HW_GPU, "Bound and built texture types mismatch");
  698. // When the size is higher we insert zeroes
  699. for (std::size_t i = type_coord_count; i < entry_coord_count; ++i) {
  700. coords.push_back(GetRegister(Register::ZeroIndex));
  701. }
  702. // Then we ensure the size matches the number of entries (dropping unused values)
  703. coords.resize(entry_coord_count);
  704. }
  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