texture.cpp 34 KB

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