image.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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::R16_G16_B16_A16:
  29. case TextureFormat::R32_G32_B32_A32:
  30. case TextureFormat::R32_G32_B32:
  31. case TextureFormat::R32_G32:
  32. case TextureFormat::R16_G16:
  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. if (component == 0) {
  95. return descriptor.b_type;
  96. }
  97. if (component == 1) {
  98. return descriptor.g_type;
  99. }
  100. if (component == 2) {
  101. return descriptor.r_type;
  102. }
  103. break;
  104. case TextureFormat::G8R24:
  105. case TextureFormat::G24R8:
  106. case TextureFormat::G8R8:
  107. case TextureFormat::G4R4:
  108. if (component == 0) {
  109. return descriptor.g_type;
  110. }
  111. if (component == 1) {
  112. return descriptor.r_type;
  113. }
  114. break;
  115. }
  116. UNIMPLEMENTED_MSG("texture format not implement={}", format);
  117. return ComponentType::FLOAT;
  118. }
  119. bool IsComponentEnabled(std::size_t component_mask, std::size_t component) {
  120. constexpr u8 R = 0b0001;
  121. constexpr u8 G = 0b0010;
  122. constexpr u8 B = 0b0100;
  123. constexpr u8 A = 0b1000;
  124. constexpr std::array<u8, 16> mask = {
  125. 0, (R), (G), (R | G), (B), (R | B), (G | B), (R | G | B),
  126. (A), (R | A), (G | A), (R | G | A), (B | A), (R | B | A), (G | B | A), (R | G | B | A)};
  127. return std::bitset<4>{mask.at(component_mask)}.test(component);
  128. }
  129. u32 GetComponentSize(TextureFormat format, std::size_t component) {
  130. switch (format) {
  131. case TextureFormat::R32_G32_B32_A32:
  132. return 32;
  133. case TextureFormat::R16_G16_B16_A16:
  134. return 16;
  135. case TextureFormat::R32_G32_B32:
  136. return (component == 0 || component == 1 || component == 2) ? 32 : 0;
  137. case TextureFormat::R32_G32:
  138. return (component == 0 || component == 1) ? 32 : 0;
  139. case TextureFormat::R16_G16:
  140. return (component == 0 || component == 1) ? 16 : 0;
  141. case TextureFormat::R32:
  142. return (component == 0) ? 32 : 0;
  143. case TextureFormat::R16:
  144. return (component == 0) ? 16 : 0;
  145. case TextureFormat::R8:
  146. return (component == 0) ? 8 : 0;
  147. case TextureFormat::R1:
  148. return (component == 0) ? 1 : 0;
  149. case TextureFormat::A8R8G8B8:
  150. return 8;
  151. case TextureFormat::A2B10G10R10:
  152. return (component == 3 || component == 2 || component == 1) ? 10 : 2;
  153. case TextureFormat::A4B4G4R4:
  154. return 4;
  155. case TextureFormat::A5B5G5R1:
  156. return (component == 0 || component == 1 || component == 2) ? 5 : 1;
  157. case TextureFormat::A1B5G5R5:
  158. return (component == 1 || component == 2 || component == 3) ? 5 : 1;
  159. case TextureFormat::R32_B24G8:
  160. if (component == 0) {
  161. return 32;
  162. }
  163. if (component == 1) {
  164. return 24;
  165. }
  166. if (component == 2) {
  167. return 8;
  168. }
  169. return 0;
  170. case TextureFormat::B5G6R5:
  171. if (component == 0 || component == 2) {
  172. return 5;
  173. }
  174. if (component == 1) {
  175. return 6;
  176. }
  177. return 0;
  178. case TextureFormat::B6G5R5:
  179. if (component == 1 || component == 2) {
  180. return 5;
  181. }
  182. if (component == 0) {
  183. return 6;
  184. }
  185. return 0;
  186. case TextureFormat::G8R24:
  187. if (component == 0) {
  188. return 8;
  189. }
  190. if (component == 1) {
  191. return 24;
  192. }
  193. return 0;
  194. case TextureFormat::G24R8:
  195. if (component == 0) {
  196. return 8;
  197. }
  198. if (component == 1) {
  199. return 24;
  200. }
  201. return 0;
  202. case TextureFormat::G8R8:
  203. return (component == 0 || component == 1) ? 8 : 0;
  204. case TextureFormat::G4R4:
  205. return (component == 0 || component == 1) ? 4 : 0;
  206. default:
  207. UNIMPLEMENTED_MSG("texture format not implement={}", format);
  208. return 0;
  209. }
  210. }
  211. std::size_t GetImageComponentMask(TextureFormat format) {
  212. constexpr u8 R = 0b0001;
  213. constexpr u8 G = 0b0010;
  214. constexpr u8 B = 0b0100;
  215. constexpr u8 A = 0b1000;
  216. switch (format) {
  217. case TextureFormat::R32_G32_B32_A32:
  218. case TextureFormat::R16_G16_B16_A16:
  219. case TextureFormat::A8R8G8B8:
  220. case TextureFormat::A2B10G10R10:
  221. case TextureFormat::A4B4G4R4:
  222. case TextureFormat::A5B5G5R1:
  223. case TextureFormat::A1B5G5R5:
  224. return std::size_t{R | G | B | A};
  225. case TextureFormat::R32_G32_B32:
  226. case TextureFormat::R32_B24G8:
  227. case TextureFormat::B5G6R5:
  228. case TextureFormat::B6G5R5:
  229. return std::size_t{R | G | B};
  230. case TextureFormat::R32_G32:
  231. case TextureFormat::R16_G16:
  232. case TextureFormat::G8R24:
  233. case TextureFormat::G24R8:
  234. case TextureFormat::G8R8:
  235. case TextureFormat::G4R4:
  236. return std::size_t{R | G};
  237. case TextureFormat::R32:
  238. case TextureFormat::R16:
  239. case TextureFormat::R8:
  240. case TextureFormat::R1:
  241. return std::size_t{R};
  242. default:
  243. UNIMPLEMENTED_MSG("texture format not implement={}", format);
  244. return std::size_t{R | G | B | A};
  245. }
  246. }
  247. std::size_t GetImageTypeNumCoordinates(Tegra::Shader::ImageType image_type) {
  248. switch (image_type) {
  249. case Tegra::Shader::ImageType::Texture1D:
  250. case Tegra::Shader::ImageType::TextureBuffer:
  251. return 1;
  252. case Tegra::Shader::ImageType::Texture1DArray:
  253. case Tegra::Shader::ImageType::Texture2D:
  254. return 2;
  255. case Tegra::Shader::ImageType::Texture2DArray:
  256. case Tegra::Shader::ImageType::Texture3D:
  257. return 3;
  258. }
  259. UNREACHABLE();
  260. return 1;
  261. }
  262. } // Anonymous namespace
  263. u32 ShaderIR::DecodeImage(NodeBlock& bb, u32 pc) {
  264. const Instruction instr = {program_code[pc]};
  265. const auto opcode = OpCode::Decode(instr);
  266. const auto GetCoordinates = [this, instr](Tegra::Shader::ImageType image_type) {
  267. std::vector<Node> coords;
  268. const std::size_t num_coords{GetImageTypeNumCoordinates(image_type)};
  269. coords.reserve(num_coords);
  270. for (std::size_t i = 0; i < num_coords; ++i) {
  271. coords.push_back(GetRegister(instr.gpr8.Value() + i));
  272. }
  273. return coords;
  274. };
  275. switch (opcode->get().GetId()) {
  276. case OpCode::Id::SULD: {
  277. UNIMPLEMENTED_IF(instr.suldst.out_of_bounds_store !=
  278. Tegra::Shader::OutOfBoundsStore::Ignore);
  279. const auto type{instr.suldst.image_type};
  280. auto& image{instr.suldst.is_immediate ? GetImage(instr.image, type)
  281. : GetBindlessImage(instr.gpr39, type)};
  282. image.MarkRead();
  283. if (instr.suldst.mode == Tegra::Shader::SurfaceDataMode::P) {
  284. u32 indexer = 0;
  285. for (u32 element = 0; element < 4; ++element) {
  286. if (!instr.suldst.IsComponentEnabled(element)) {
  287. continue;
  288. }
  289. MetaImage meta{image, {}, element};
  290. Node value = Operation(OperationCode::ImageLoad, meta, GetCoordinates(type));
  291. SetTemporary(bb, indexer++, std::move(value));
  292. }
  293. for (u32 i = 0; i < indexer; ++i) {
  294. SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i));
  295. }
  296. } else if (instr.suldst.mode == Tegra::Shader::SurfaceDataMode::D_BA) {
  297. UNIMPLEMENTED_IF(instr.suldst.GetStoreDataLayout() != StoreType::Bits32);
  298. auto descriptor = [this, instr] {
  299. std::optional<Tegra::Engines::SamplerDescriptor> descriptor;
  300. if (instr.suldst.is_immediate) {
  301. descriptor = registry.ObtainBoundSampler(instr.image.index.Value());
  302. } else {
  303. const Node image_register = GetRegister(instr.gpr39);
  304. const auto [base_image, buffer, offset] = TrackCbuf(
  305. image_register, global_code, static_cast<s64>(global_code.size()));
  306. descriptor = registry.ObtainBindlessSampler(buffer, offset);
  307. }
  308. if (!descriptor) {
  309. UNREACHABLE_MSG("Failed to obtain image descriptor");
  310. }
  311. return *descriptor;
  312. }();
  313. const auto comp_mask = GetImageComponentMask(descriptor.format);
  314. // TODO(namkazt): let's suppose image format is same as store type. we check on it
  315. // later.
  316. switch (instr.suldst.GetStoreDataLayout()) {
  317. case StoreType::Bits32: {
  318. u32 shifted_counter = 0;
  319. // value should be RGBA format
  320. Node value = Immediate(0);
  321. for (u32 element = 0; element < 4; ++element) {
  322. if (!IsComponentEnabled(comp_mask, element)) {
  323. continue;
  324. }
  325. const auto component_type = GetComponentType(descriptor, element);
  326. const auto component_size = GetComponentSize(descriptor.format, element);
  327. bool is_signed = true;
  328. MetaImage meta{image, {}, element};
  329. const Node original_value =
  330. Operation(OperationCode::ImageLoad, meta, GetCoordinates(type));
  331. Node converted_value = [&] {
  332. switch (component_type) {
  333. case ComponentType::SNORM: {
  334. // range [-1.0, 1.0]
  335. auto cnv_value =
  336. Operation(OperationCode::FAdd, original_value, Immediate(1.f));
  337. cnv_value = Operation(OperationCode::FMul, std::move(cnv_value),
  338. Immediate(127.f));
  339. is_signed = false;
  340. return SignedOperation(OperationCode::ICastFloat, is_signed,
  341. std::move(cnv_value));
  342. }
  343. case ComponentType::UNORM: {
  344. // range [0.0, 1.0]
  345. auto cnv_value =
  346. Operation(OperationCode::FMul, original_value, Immediate(255.f));
  347. is_signed = false;
  348. return SignedOperation(OperationCode::ICastFloat, is_signed,
  349. std::move(cnv_value));
  350. }
  351. case ComponentType::SINT: // range [-128,128]
  352. return Operation(OperationCode::IAdd, original_value, Immediate(128));
  353. case ComponentType::UINT: // range [0, 255]
  354. is_signed = false;
  355. return original_value;
  356. case ComponentType::FLOAT:
  357. return original_value;
  358. default:
  359. UNIMPLEMENTED_MSG("Unimplement component type={}", component_type);
  360. return original_value;
  361. }
  362. }();
  363. // shift element to correct position
  364. const auto shifted = shifted_counter;
  365. if (shifted > 0) {
  366. converted_value =
  367. SignedOperation(OperationCode::ILogicalShiftLeft, is_signed,
  368. std::move(converted_value), Immediate(shifted));
  369. }
  370. shifted_counter += component_size;
  371. // add value into result
  372. value = Operation(OperationCode::UBitwiseOr, value, std::move(converted_value));
  373. }
  374. SetRegister(bb, instr.gpr0.Value(), std::move(value));
  375. break;
  376. }
  377. default:
  378. UNREACHABLE();
  379. break;
  380. }
  381. }
  382. break;
  383. }
  384. case OpCode::Id::SUST: {
  385. UNIMPLEMENTED_IF(instr.suldst.mode != Tegra::Shader::SurfaceDataMode::P);
  386. UNIMPLEMENTED_IF(instr.suldst.out_of_bounds_store !=
  387. Tegra::Shader::OutOfBoundsStore::Ignore);
  388. UNIMPLEMENTED_IF(instr.suldst.component_mask_selector != 0xf); // Ensure we have RGBA
  389. std::vector<Node> values;
  390. constexpr std::size_t hardcoded_size{4};
  391. for (std::size_t i = 0; i < hardcoded_size; ++i) {
  392. values.push_back(GetRegister(instr.gpr0.Value() + i));
  393. }
  394. const auto type{instr.suldst.image_type};
  395. auto& image{instr.suldst.is_immediate ? GetImage(instr.image, type)
  396. : GetBindlessImage(instr.gpr39, type)};
  397. image.MarkWrite();
  398. MetaImage meta{image, std::move(values)};
  399. bb.push_back(Operation(OperationCode::ImageStore, meta, GetCoordinates(type)));
  400. break;
  401. }
  402. case OpCode::Id::SUATOM: {
  403. UNIMPLEMENTED_IF(instr.suatom_d.is_ba != 0);
  404. const OperationCode operation_code = [instr] {
  405. switch (instr.suatom_d.operation_type) {
  406. case Tegra::Shader::ImageAtomicOperationType::S32:
  407. case Tegra::Shader::ImageAtomicOperationType::U32:
  408. switch (instr.suatom_d.operation) {
  409. case Tegra::Shader::ImageAtomicOperation::Add:
  410. return OperationCode::AtomicImageAdd;
  411. case Tegra::Shader::ImageAtomicOperation::And:
  412. return OperationCode::AtomicImageAnd;
  413. case Tegra::Shader::ImageAtomicOperation::Or:
  414. return OperationCode::AtomicImageOr;
  415. case Tegra::Shader::ImageAtomicOperation::Xor:
  416. return OperationCode::AtomicImageXor;
  417. case Tegra::Shader::ImageAtomicOperation::Exch:
  418. return OperationCode::AtomicImageExchange;
  419. }
  420. default:
  421. break;
  422. }
  423. UNIMPLEMENTED_MSG("Unimplemented operation={} type={}",
  424. static_cast<u64>(instr.suatom_d.operation.Value()),
  425. static_cast<u64>(instr.suatom_d.operation_type.Value()));
  426. return OperationCode::AtomicImageAdd;
  427. }();
  428. Node value = GetRegister(instr.gpr0);
  429. const auto type = instr.suatom_d.image_type;
  430. auto& image = GetImage(instr.image, type);
  431. image.MarkAtomic();
  432. MetaImage meta{image, {std::move(value)}};
  433. SetRegister(bb, instr.gpr0, Operation(operation_code, meta, GetCoordinates(type)));
  434. break;
  435. }
  436. default:
  437. UNIMPLEMENTED_MSG("Unhandled image instruction: {}", opcode->get().GetName());
  438. }
  439. return pc;
  440. }
  441. Image& ShaderIR::GetImage(Tegra::Shader::Image image, Tegra::Shader::ImageType type) {
  442. const auto offset = static_cast<u32>(image.index.Value());
  443. const auto it =
  444. std::find_if(std::begin(used_images), std::end(used_images),
  445. [offset](const Image& entry) { return entry.GetOffset() == offset; });
  446. if (it != std::end(used_images)) {
  447. ASSERT(!it->IsBindless() && it->GetType() == it->GetType());
  448. return *it;
  449. }
  450. const auto next_index = static_cast<u32>(used_images.size());
  451. return used_images.emplace_back(next_index, offset, type);
  452. }
  453. Image& ShaderIR::GetBindlessImage(Tegra::Shader::Register reg, Tegra::Shader::ImageType type) {
  454. const Node image_register = GetRegister(reg);
  455. const auto [base_image, buffer, offset] =
  456. TrackCbuf(image_register, global_code, static_cast<s64>(global_code.size()));
  457. const auto it =
  458. std::find_if(std::begin(used_images), std::end(used_images),
  459. [buffer = buffer, offset = offset](const Image& entry) {
  460. return entry.GetBuffer() == buffer && entry.GetOffset() == offset;
  461. });
  462. if (it != std::end(used_images)) {
  463. ASSERT(it->IsBindless() && it->GetType() == it->GetType());
  464. return *it;
  465. }
  466. const auto next_index = static_cast<u32>(used_images.size());
  467. return used_images.emplace_back(next_index, offset, buffer, type);
  468. }
  469. } // namespace VideoCommon::Shader