image.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. #include "video_core/textures/texture.h"
  15. namespace VideoCommon::Shader {
  16. using Tegra::Shader::Instruction;
  17. using Tegra::Shader::OpCode;
  18. using Tegra::Shader::PredCondition;
  19. using Tegra::Shader::StoreType;
  20. using Tegra::Texture::ComponentType;
  21. using Tegra::Texture::TextureFormat;
  22. using Tegra::Texture::TICEntry;
  23. namespace {
  24. ComponentType GetComponentType(Tegra::Engines::SamplerDescriptor descriptor,
  25. std::size_t component) {
  26. const TextureFormat format{descriptor.format};
  27. switch (format) {
  28. case TextureFormat::R16G16B16A16:
  29. case TextureFormat::R32G32B32A32:
  30. case TextureFormat::R32G32B32:
  31. case TextureFormat::R32G32:
  32. case TextureFormat::R16G16:
  33. case TextureFormat::R32:
  34. case TextureFormat::R16:
  35. case TextureFormat::R8:
  36. case TextureFormat::R1:
  37. if (component == 0) {
  38. return descriptor.r_type;
  39. }
  40. if (component == 1) {
  41. return descriptor.g_type;
  42. }
  43. if (component == 2) {
  44. return descriptor.b_type;
  45. }
  46. if (component == 3) {
  47. return descriptor.a_type;
  48. }
  49. break;
  50. case TextureFormat::A8R8G8B8:
  51. if (component == 0) {
  52. return descriptor.a_type;
  53. }
  54. if (component == 1) {
  55. return descriptor.r_type;
  56. }
  57. if (component == 2) {
  58. return descriptor.g_type;
  59. }
  60. if (component == 3) {
  61. return descriptor.b_type;
  62. }
  63. break;
  64. case TextureFormat::A2B10G10R10:
  65. case TextureFormat::A4B4G4R4:
  66. case TextureFormat::A5B5G5R1:
  67. case TextureFormat::A1B5G5R5:
  68. if (component == 0) {
  69. return descriptor.a_type;
  70. }
  71. if (component == 1) {
  72. return descriptor.b_type;
  73. }
  74. if (component == 2) {
  75. return descriptor.g_type;
  76. }
  77. if (component == 3) {
  78. return descriptor.r_type;
  79. }
  80. break;
  81. case TextureFormat::R32_B24G8:
  82. if (component == 0) {
  83. return descriptor.r_type;
  84. }
  85. if (component == 1) {
  86. return descriptor.b_type;
  87. }
  88. if (component == 2) {
  89. return descriptor.g_type;
  90. }
  91. break;
  92. case TextureFormat::B5G6R5:
  93. case TextureFormat::B6G5R5:
  94. case TextureFormat::B10G11R11:
  95. if (component == 0) {
  96. return descriptor.b_type;
  97. }
  98. if (component == 1) {
  99. return descriptor.g_type;
  100. }
  101. if (component == 2) {
  102. return descriptor.r_type;
  103. }
  104. break;
  105. case TextureFormat::R24G8:
  106. case TextureFormat::R8G24:
  107. case TextureFormat::R8G8:
  108. case TextureFormat::G4R4:
  109. if (component == 0) {
  110. return descriptor.g_type;
  111. }
  112. if (component == 1) {
  113. return descriptor.r_type;
  114. }
  115. break;
  116. default:
  117. break;
  118. }
  119. UNIMPLEMENTED_MSG("Texture format not implemented={}", format);
  120. return ComponentType::FLOAT;
  121. }
  122. bool IsComponentEnabled(std::size_t component_mask, std::size_t component) {
  123. constexpr u8 R = 0b0001;
  124. constexpr u8 G = 0b0010;
  125. constexpr u8 B = 0b0100;
  126. constexpr u8 A = 0b1000;
  127. constexpr std::array<u8, 16> mask = {
  128. 0, (R), (G), (R | G), (B), (R | B), (G | B), (R | G | B),
  129. (A), (R | A), (G | A), (R | G | A), (B | A), (R | B | A), (G | B | A), (R | G | B | A)};
  130. return std::bitset<4>{mask.at(component_mask)}.test(component);
  131. }
  132. u32 GetComponentSize(TextureFormat format, std::size_t component) {
  133. switch (format) {
  134. case TextureFormat::R32G32B32A32:
  135. return 32;
  136. case TextureFormat::R16G16B16A16:
  137. return 16;
  138. case TextureFormat::R32G32B32:
  139. return component <= 2 ? 32 : 0;
  140. case TextureFormat::R32G32:
  141. return component <= 1 ? 32 : 0;
  142. case TextureFormat::R16G16:
  143. return component <= 1 ? 16 : 0;
  144. case TextureFormat::R32:
  145. return component == 0 ? 32 : 0;
  146. case TextureFormat::R16:
  147. return component == 0 ? 16 : 0;
  148. case TextureFormat::R8:
  149. return component == 0 ? 8 : 0;
  150. case TextureFormat::R1:
  151. return component == 0 ? 1 : 0;
  152. case TextureFormat::A8R8G8B8:
  153. return 8;
  154. case TextureFormat::A2B10G10R10:
  155. return (component == 3 || component == 2 || component == 1) ? 10 : 2;
  156. case TextureFormat::A4B4G4R4:
  157. return 4;
  158. case TextureFormat::A5B5G5R1:
  159. return (component == 0 || component == 1 || component == 2) ? 5 : 1;
  160. case TextureFormat::A1B5G5R5:
  161. return (component == 1 || component == 2 || component == 3) ? 5 : 1;
  162. case TextureFormat::R32_B24G8:
  163. if (component == 0) {
  164. return 32;
  165. }
  166. if (component == 1) {
  167. return 24;
  168. }
  169. if (component == 2) {
  170. return 8;
  171. }
  172. return 0;
  173. case TextureFormat::B5G6R5:
  174. if (component == 0 || component == 2) {
  175. return 5;
  176. }
  177. if (component == 1) {
  178. return 6;
  179. }
  180. return 0;
  181. case TextureFormat::B6G5R5:
  182. if (component == 1 || component == 2) {
  183. return 5;
  184. }
  185. if (component == 0) {
  186. return 6;
  187. }
  188. return 0;
  189. case TextureFormat::B10G11R11:
  190. if (component == 1 || component == 2) {
  191. return 11;
  192. }
  193. if (component == 0) {
  194. return 10;
  195. }
  196. return 0;
  197. case TextureFormat::R24G8:
  198. if (component == 0) {
  199. return 8;
  200. }
  201. if (component == 1) {
  202. return 24;
  203. }
  204. return 0;
  205. case TextureFormat::R8G24:
  206. if (component == 0) {
  207. return 8;
  208. }
  209. if (component == 1) {
  210. return 24;
  211. }
  212. return 0;
  213. case TextureFormat::R8G8:
  214. return (component == 0 || component == 1) ? 8 : 0;
  215. case TextureFormat::G4R4:
  216. return (component == 0 || component == 1) ? 4 : 0;
  217. default:
  218. UNIMPLEMENTED_MSG("Texture format not implemented={}", format);
  219. return 0;
  220. }
  221. }
  222. std::size_t GetImageComponentMask(TextureFormat format) {
  223. constexpr u8 R = 0b0001;
  224. constexpr u8 G = 0b0010;
  225. constexpr u8 B = 0b0100;
  226. constexpr u8 A = 0b1000;
  227. switch (format) {
  228. case TextureFormat::R32G32B32A32:
  229. case TextureFormat::R16G16B16A16:
  230. case TextureFormat::A8R8G8B8:
  231. case TextureFormat::A2B10G10R10:
  232. case TextureFormat::A4B4G4R4:
  233. case TextureFormat::A5B5G5R1:
  234. case TextureFormat::A1B5G5R5:
  235. return std::size_t{R | G | B | A};
  236. case TextureFormat::R32G32B32:
  237. case TextureFormat::R32_B24G8:
  238. case TextureFormat::B5G6R5:
  239. case TextureFormat::B6G5R5:
  240. case TextureFormat::B10G11R11:
  241. return std::size_t{R | G | B};
  242. case TextureFormat::R32G32:
  243. case TextureFormat::R16G16:
  244. case TextureFormat::R24G8:
  245. case TextureFormat::R8G24:
  246. case TextureFormat::R8G8:
  247. case TextureFormat::G4R4:
  248. return std::size_t{R | G};
  249. case TextureFormat::R32:
  250. case TextureFormat::R16:
  251. case TextureFormat::R8:
  252. case TextureFormat::R1:
  253. return std::size_t{R};
  254. default:
  255. UNIMPLEMENTED_MSG("Texture format not implemented={}", format);
  256. return std::size_t{R | G | B | A};
  257. }
  258. }
  259. std::size_t GetImageTypeNumCoordinates(Tegra::Shader::ImageType image_type) {
  260. switch (image_type) {
  261. case Tegra::Shader::ImageType::Texture1D:
  262. case Tegra::Shader::ImageType::TextureBuffer:
  263. return 1;
  264. case Tegra::Shader::ImageType::Texture1DArray:
  265. case Tegra::Shader::ImageType::Texture2D:
  266. return 2;
  267. case Tegra::Shader::ImageType::Texture2DArray:
  268. case Tegra::Shader::ImageType::Texture3D:
  269. return 3;
  270. }
  271. UNREACHABLE();
  272. return 1;
  273. }
  274. } // Anonymous namespace
  275. std::pair<Node, bool> ShaderIR::GetComponentValue(ComponentType component_type, u32 component_size,
  276. Node original_value) {
  277. switch (component_type) {
  278. case ComponentType::SNORM: {
  279. // range [-1.0, 1.0]
  280. auto cnv_value = Operation(OperationCode::FMul, original_value,
  281. Immediate(static_cast<float>(1 << component_size) / 2.f - 1.f));
  282. cnv_value = Operation(OperationCode::ICastFloat, std::move(cnv_value));
  283. return {BitfieldExtract(std::move(cnv_value), 0, component_size), true};
  284. }
  285. case ComponentType::SINT:
  286. case ComponentType::UNORM: {
  287. bool is_signed = component_type == ComponentType::SINT;
  288. // range [0.0, 1.0]
  289. auto cnv_value = Operation(OperationCode::FMul, original_value,
  290. Immediate(static_cast<float>(1 << component_size) - 1.f));
  291. return {SignedOperation(OperationCode::ICastFloat, is_signed, std::move(cnv_value)),
  292. is_signed};
  293. }
  294. case ComponentType::UINT: // range [0, (1 << component_size) - 1]
  295. return {std::move(original_value), false};
  296. case ComponentType::FLOAT:
  297. if (component_size == 16) {
  298. return {Operation(OperationCode::HCastFloat, original_value), true};
  299. } else {
  300. return {std::move(original_value), true};
  301. }
  302. default:
  303. UNIMPLEMENTED_MSG("Unimplemented component type={}", component_type);
  304. return {std::move(original_value), true};
  305. }
  306. }
  307. u32 ShaderIR::DecodeImage(NodeBlock& bb, u32 pc) {
  308. const Instruction instr = {program_code[pc]};
  309. const auto opcode = OpCode::Decode(instr);
  310. const auto GetCoordinates = [this, instr](Tegra::Shader::ImageType image_type) {
  311. std::vector<Node> coords;
  312. const std::size_t num_coords{GetImageTypeNumCoordinates(image_type)};
  313. coords.reserve(num_coords);
  314. for (std::size_t i = 0; i < num_coords; ++i) {
  315. coords.push_back(GetRegister(instr.gpr8.Value() + i));
  316. }
  317. return coords;
  318. };
  319. switch (opcode->get().GetId()) {
  320. case OpCode::Id::SULD: {
  321. UNIMPLEMENTED_IF(instr.suldst.out_of_bounds_store !=
  322. Tegra::Shader::OutOfBoundsStore::Ignore);
  323. const auto type{instr.suldst.image_type};
  324. auto& image{instr.suldst.is_immediate ? GetImage(instr.image, type)
  325. : GetBindlessImage(instr.gpr39, type)};
  326. image.MarkRead();
  327. if (instr.suldst.mode == Tegra::Shader::SurfaceDataMode::P) {
  328. u32 indexer = 0;
  329. for (u32 element = 0; element < 4; ++element) {
  330. if (!instr.suldst.IsComponentEnabled(element)) {
  331. continue;
  332. }
  333. MetaImage meta{image, {}, element};
  334. Node value = Operation(OperationCode::ImageLoad, meta, GetCoordinates(type));
  335. SetTemporary(bb, indexer++, std::move(value));
  336. }
  337. for (u32 i = 0; i < indexer; ++i) {
  338. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  339. }
  340. } else if (instr.suldst.mode == Tegra::Shader::SurfaceDataMode::D_BA) {
  341. UNIMPLEMENTED_IF(instr.suldst.GetStoreDataLayout() != StoreType::Bits32 &&
  342. instr.suldst.GetStoreDataLayout() != StoreType::Bits64);
  343. auto descriptor = [this, instr] {
  344. std::optional<Tegra::Engines::SamplerDescriptor> descriptor;
  345. if (instr.suldst.is_immediate) {
  346. descriptor =
  347. registry.ObtainBoundSampler(static_cast<u32>(instr.image.index.Value()));
  348. } else {
  349. const Node image_register = GetRegister(instr.gpr39);
  350. const auto result = TrackCbuf(image_register, global_code,
  351. static_cast<s64>(global_code.size()));
  352. const auto buffer = std::get<1>(result);
  353. const auto offset = std::get<2>(result);
  354. descriptor = registry.ObtainBindlessSampler(buffer, offset);
  355. }
  356. if (!descriptor) {
  357. UNREACHABLE_MSG("Failed to obtain image descriptor");
  358. }
  359. return *descriptor;
  360. }();
  361. const auto comp_mask = GetImageComponentMask(descriptor.format);
  362. switch (instr.suldst.GetStoreDataLayout()) {
  363. case StoreType::Bits32:
  364. case StoreType::Bits64: {
  365. u32 indexer = 0;
  366. u32 shifted_counter = 0;
  367. Node value = Immediate(0);
  368. for (u32 element = 0; element < 4; ++element) {
  369. if (!IsComponentEnabled(comp_mask, element)) {
  370. continue;
  371. }
  372. const auto component_type = GetComponentType(descriptor, element);
  373. const auto component_size = GetComponentSize(descriptor.format, element);
  374. MetaImage meta{image, {}, element};
  375. auto [converted_value, is_signed] = GetComponentValue(
  376. component_type, component_size,
  377. Operation(OperationCode::ImageLoad, meta, GetCoordinates(type)));
  378. // shift element to correct position
  379. const auto shifted = shifted_counter;
  380. if (shifted > 0) {
  381. converted_value =
  382. SignedOperation(OperationCode::ILogicalShiftLeft, is_signed,
  383. std::move(converted_value), Immediate(shifted));
  384. }
  385. shifted_counter += component_size;
  386. // add value into result
  387. value = Operation(OperationCode::UBitwiseOr, value, std::move(converted_value));
  388. // if we shifted enough for 1 byte -> we save it into temp
  389. if (shifted_counter >= 32) {
  390. SetTemporary(bb, indexer++, std::move(value));
  391. // reset counter and value to prepare pack next byte
  392. value = Immediate(0);
  393. shifted_counter = 0;
  394. }
  395. }
  396. for (u32 i = 0; i < indexer; ++i) {
  397. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  398. }
  399. break;
  400. }
  401. default:
  402. UNREACHABLE();
  403. break;
  404. }
  405. }
  406. break;
  407. }
  408. case OpCode::Id::SUST: {
  409. UNIMPLEMENTED_IF(instr.suldst.mode != Tegra::Shader::SurfaceDataMode::P);
  410. UNIMPLEMENTED_IF(instr.suldst.out_of_bounds_store !=
  411. Tegra::Shader::OutOfBoundsStore::Ignore);
  412. UNIMPLEMENTED_IF(instr.suldst.component_mask_selector != 0xf); // Ensure we have RGBA
  413. std::vector<Node> values;
  414. constexpr std::size_t hardcoded_size{4};
  415. for (std::size_t i = 0; i < hardcoded_size; ++i) {
  416. values.push_back(GetRegister(instr.gpr0.Value() + i));
  417. }
  418. const auto type{instr.suldst.image_type};
  419. auto& image{instr.suldst.is_immediate ? GetImage(instr.image, type)
  420. : GetBindlessImage(instr.gpr39, type)};
  421. image.MarkWrite();
  422. MetaImage meta{image, std::move(values)};
  423. bb.push_back(Operation(OperationCode::ImageStore, meta, GetCoordinates(type)));
  424. break;
  425. }
  426. case OpCode::Id::SUATOM: {
  427. UNIMPLEMENTED_IF(instr.suatom_d.is_ba != 0);
  428. const OperationCode operation_code = [instr] {
  429. switch (instr.suatom_d.operation_type) {
  430. case Tegra::Shader::ImageAtomicOperationType::S32:
  431. case Tegra::Shader::ImageAtomicOperationType::U32:
  432. switch (instr.suatom_d.operation) {
  433. case Tegra::Shader::ImageAtomicOperation::Add:
  434. return OperationCode::AtomicImageAdd;
  435. case Tegra::Shader::ImageAtomicOperation::And:
  436. return OperationCode::AtomicImageAnd;
  437. case Tegra::Shader::ImageAtomicOperation::Or:
  438. return OperationCode::AtomicImageOr;
  439. case Tegra::Shader::ImageAtomicOperation::Xor:
  440. return OperationCode::AtomicImageXor;
  441. case Tegra::Shader::ImageAtomicOperation::Exch:
  442. return OperationCode::AtomicImageExchange;
  443. default:
  444. break;
  445. }
  446. default:
  447. break;
  448. }
  449. UNIMPLEMENTED_MSG("Unimplemented operation={}, type={}",
  450. static_cast<u64>(instr.suatom_d.operation.Value()),
  451. static_cast<u64>(instr.suatom_d.operation_type.Value()));
  452. return OperationCode::AtomicImageAdd;
  453. }();
  454. Node value = GetRegister(instr.gpr0);
  455. const auto type = instr.suatom_d.image_type;
  456. auto& image = GetImage(instr.image, type);
  457. image.MarkAtomic();
  458. MetaImage meta{image, {std::move(value)}};
  459. SetRegister(bb, instr.gpr0, Operation(operation_code, meta, GetCoordinates(type)));
  460. break;
  461. }
  462. default:
  463. UNIMPLEMENTED_MSG("Unhandled image instruction: {}", opcode->get().GetName());
  464. }
  465. return pc;
  466. }
  467. Image& ShaderIR::GetImage(Tegra::Shader::Image image, Tegra::Shader::ImageType type) {
  468. const auto offset = static_cast<u32>(image.index.Value());
  469. const auto it = std::find_if(std::begin(used_images), std::end(used_images),
  470. [offset](const Image& entry) { return entry.offset == offset; });
  471. if (it != std::end(used_images)) {
  472. ASSERT(!it->is_bindless && it->type == type);
  473. return *it;
  474. }
  475. const auto next_index = static_cast<u32>(used_images.size());
  476. return used_images.emplace_back(next_index, offset, type);
  477. }
  478. Image& ShaderIR::GetBindlessImage(Tegra::Shader::Register reg, Tegra::Shader::ImageType type) {
  479. const Node image_register = GetRegister(reg);
  480. const auto result =
  481. TrackCbuf(image_register, global_code, static_cast<s64>(global_code.size()));
  482. const auto buffer = std::get<1>(result);
  483. const auto offset = std::get<2>(result);
  484. const auto it = std::find_if(std::begin(used_images), std::end(used_images),
  485. [buffer, offset](const Image& entry) {
  486. return entry.buffer == buffer && entry.offset == offset;
  487. });
  488. if (it != std::end(used_images)) {
  489. ASSERT(it->is_bindless && it->type == type);
  490. return *it;
  491. }
  492. const auto next_index = static_cast<u32>(used_images.size());
  493. return used_images.emplace_back(next_index, offset, buffer, type);
  494. }
  495. } // namespace VideoCommon::Shader