texture.cpp 30 KB

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