translate_program.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <memory>
  6. #include <vector>
  7. #include <queue>
  8. #include "common/settings.h"
  9. #include "shader_recompiler/exception.h"
  10. #include "shader_recompiler/frontend/ir/basic_block.h"
  11. #include "shader_recompiler/frontend/ir/post_order.h"
  12. #include "shader_recompiler/frontend/maxwell/structured_control_flow.h"
  13. #include "shader_recompiler/frontend/maxwell/translate/translate.h"
  14. #include "shader_recompiler/frontend/maxwell/translate_program.h"
  15. #include "shader_recompiler/host_translate_info.h"
  16. #include "shader_recompiler/ir_opt/passes.h"
  17. namespace Shader::Maxwell {
  18. namespace {
  19. IR::BlockList GenerateBlocks(const IR::AbstractSyntaxList& syntax_list) {
  20. size_t num_syntax_blocks{};
  21. for (const auto& node : syntax_list) {
  22. if (node.type == IR::AbstractSyntaxNode::Type::Block) {
  23. ++num_syntax_blocks;
  24. }
  25. }
  26. IR::BlockList blocks;
  27. blocks.reserve(num_syntax_blocks);
  28. u32 order_index{};
  29. for (const auto& node : syntax_list) {
  30. if (node.type == IR::AbstractSyntaxNode::Type::Block) {
  31. blocks.push_back(node.data.block);
  32. blocks.back()->SetOrder(order_index++);
  33. }
  34. }
  35. return blocks;
  36. }
  37. void RemoveUnreachableBlocks(IR::Program& program) {
  38. // Some blocks might be unreachable if a function call exists unconditionally
  39. // If this happens the number of blocks and post order blocks will mismatch
  40. if (program.blocks.size() == program.post_order_blocks.size()) {
  41. return;
  42. }
  43. const auto begin{program.blocks.begin() + 1};
  44. const auto end{program.blocks.end()};
  45. const auto pred{[](IR::Block* block) { return block->ImmPredecessors().empty(); }};
  46. program.blocks.erase(std::remove_if(begin, end, pred), end);
  47. }
  48. void CollectInterpolationInfo(Environment& env, IR::Program& program) {
  49. if (program.stage != Stage::Fragment) {
  50. return;
  51. }
  52. const ProgramHeader& sph{env.SPH()};
  53. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  54. std::optional<PixelImap> imap;
  55. for (const PixelImap value : sph.ps.GenericInputMap(static_cast<u32>(index))) {
  56. if (value == PixelImap::Unused) {
  57. continue;
  58. }
  59. if (imap && imap != value) {
  60. throw NotImplementedException("Per component interpolation");
  61. }
  62. imap = value;
  63. }
  64. if (!imap) {
  65. continue;
  66. }
  67. program.info.interpolation[index] = [&] {
  68. switch (*imap) {
  69. case PixelImap::Unused:
  70. case PixelImap::Perspective:
  71. return Interpolation::Smooth;
  72. case PixelImap::Constant:
  73. return Interpolation::Flat;
  74. case PixelImap::ScreenLinear:
  75. return Interpolation::NoPerspective;
  76. }
  77. throw NotImplementedException("Unknown interpolation {}", *imap);
  78. }();
  79. }
  80. }
  81. void AddNVNStorageBuffers(IR::Program& program) {
  82. if (!program.info.uses_global_memory) {
  83. return;
  84. }
  85. const u32 driver_cbuf{0};
  86. const u32 descriptor_size{0x10};
  87. const u32 num_buffers{16};
  88. const u32 base{[&] {
  89. switch (program.stage) {
  90. case Stage::VertexA:
  91. case Stage::VertexB:
  92. return 0x110u;
  93. case Stage::TessellationControl:
  94. return 0x210u;
  95. case Stage::TessellationEval:
  96. return 0x310u;
  97. case Stage::Geometry:
  98. return 0x410u;
  99. case Stage::Fragment:
  100. return 0x510u;
  101. case Stage::Compute:
  102. return 0x310u;
  103. }
  104. throw InvalidArgument("Invalid stage {}", program.stage);
  105. }()};
  106. auto& descs{program.info.storage_buffers_descriptors};
  107. for (u32 index = 0; index < num_buffers; ++index) {
  108. if (!program.info.nvn_buffer_used[index]) {
  109. continue;
  110. }
  111. const u32 offset{base + index * descriptor_size};
  112. const auto it{std::ranges::find(descs, offset, &StorageBufferDescriptor::cbuf_offset)};
  113. if (it != descs.end()) {
  114. it->is_written |= program.info.stores_global_memory;
  115. continue;
  116. }
  117. descs.push_back({
  118. .cbuf_index = driver_cbuf,
  119. .cbuf_offset = offset,
  120. .count = 1,
  121. .is_written = program.info.stores_global_memory,
  122. });
  123. }
  124. }
  125. } // Anonymous namespace
  126. IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
  127. Environment& env, Flow::CFG& cfg, const HostTranslateInfo& host_info) {
  128. IR::Program program;
  129. program.syntax_list = BuildASL(inst_pool, block_pool, env, cfg, host_info);
  130. program.blocks = GenerateBlocks(program.syntax_list);
  131. program.post_order_blocks = PostOrder(program.syntax_list.front());
  132. program.stage = env.ShaderStage();
  133. program.local_memory_size = env.LocalMemorySize();
  134. switch (program.stage) {
  135. case Stage::TessellationControl: {
  136. const ProgramHeader& sph{env.SPH()};
  137. program.invocations = sph.common2.threads_per_input_primitive;
  138. break;
  139. }
  140. case Stage::Geometry: {
  141. const ProgramHeader& sph{env.SPH()};
  142. program.output_topology = sph.common3.output_topology;
  143. program.output_vertices = sph.common4.max_output_vertices;
  144. program.invocations = sph.common2.threads_per_input_primitive;
  145. program.is_geometry_passthrough = sph.common0.geometry_passthrough != 0;
  146. if (program.is_geometry_passthrough) {
  147. const auto& mask{env.GpPassthroughMask()};
  148. for (size_t i = 0; i < program.info.passthrough.mask.size(); ++i) {
  149. program.info.passthrough.mask[i] = ((mask[i / 32] >> (i % 32)) & 1) == 0;
  150. }
  151. }
  152. break;
  153. }
  154. case Stage::Compute:
  155. program.workgroup_size = env.WorkgroupSize();
  156. program.shared_memory_size = env.SharedMemorySize();
  157. break;
  158. default:
  159. break;
  160. }
  161. RemoveUnreachableBlocks(program);
  162. // Replace instructions before the SSA rewrite
  163. if (!host_info.support_float16) {
  164. Optimization::LowerFp16ToFp32(program);
  165. }
  166. if (!host_info.support_int64) {
  167. Optimization::LowerInt64ToInt32(program);
  168. }
  169. Optimization::SsaRewritePass(program);
  170. Optimization::GlobalMemoryToStorageBufferPass(program);
  171. Optimization::TexturePass(env, program);
  172. Optimization::ConstantPropagationPass(program);
  173. if (Settings::values.resolution_info.active) {
  174. Optimization::RescalingPass(program);
  175. }
  176. Optimization::DeadCodeEliminationPass(program);
  177. if (Settings::values.renderer_debug) {
  178. Optimization::VerificationPass(program);
  179. }
  180. Optimization::CollectShaderInfoPass(env, program);
  181. CollectInterpolationInfo(env, program);
  182. AddNVNStorageBuffers(program);
  183. return program;
  184. }
  185. IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b,
  186. Environment& env_vertex_b) {
  187. IR::Program result{};
  188. Optimization::VertexATransformPass(vertex_a);
  189. Optimization::VertexBTransformPass(vertex_b);
  190. for (const auto& term : vertex_a.syntax_list) {
  191. if (term.type != IR::AbstractSyntaxNode::Type::Return) {
  192. result.syntax_list.push_back(term);
  193. }
  194. }
  195. result.syntax_list.insert(result.syntax_list.end(), vertex_b.syntax_list.begin(),
  196. vertex_b.syntax_list.end());
  197. result.blocks = GenerateBlocks(result.syntax_list);
  198. result.post_order_blocks = vertex_b.post_order_blocks;
  199. for (const auto& block : vertex_a.post_order_blocks) {
  200. result.post_order_blocks.push_back(block);
  201. }
  202. result.stage = Stage::VertexB;
  203. result.info = vertex_a.info;
  204. result.local_memory_size = std::max(vertex_a.local_memory_size, vertex_b.local_memory_size);
  205. result.info.loads.mask |= vertex_b.info.loads.mask;
  206. result.info.stores.mask |= vertex_b.info.stores.mask;
  207. Optimization::JoinTextureInfo(result.info, vertex_b.info);
  208. Optimization::JoinStorageInfo(result.info, vertex_b.info);
  209. Optimization::DeadCodeEliminationPass(result);
  210. if (Settings::values.renderer_debug) {
  211. Optimization::VerificationPass(result);
  212. }
  213. Optimization::CollectShaderInfoPass(env_vertex_b, result);
  214. return result;
  215. }
  216. bool IsLegacyAttribute(IR::Attribute attribute) {
  217. return (attribute >= IR::Attribute::ColorFrontDiffuseR &&
  218. attribute <= IR::Attribute::ColorBackSpecularA) ||
  219. attribute == IR::Attribute::FogCoordinate ||
  220. (attribute >= IR::Attribute::FixedFncTexture0S &&
  221. attribute <= IR::Attribute::FixedFncTexture9Q);
  222. }
  223. std::map<IR::Attribute, IR::Attribute> GenerateLegacyToGenericMappings(
  224. const VaryingState& state, std::queue<IR::Attribute> ununsed_generics) {
  225. std::map<IR::Attribute, IR::Attribute> mapping;
  226. for (size_t index = 0; index < 4; ++index) {
  227. auto attr = IR::Attribute::ColorFrontDiffuseR + index * 4;
  228. if (state.AnyComponent(attr)) {
  229. for (size_t i = 0; i < 4; ++i) {
  230. mapping.insert({attr + i, ununsed_generics.front() + i});
  231. }
  232. ununsed_generics.pop();
  233. }
  234. }
  235. if (state[IR::Attribute::FogCoordinate]) {
  236. mapping.insert({IR::Attribute::FogCoordinate, ununsed_generics.front()});
  237. ununsed_generics.pop();
  238. }
  239. for (size_t index = 0; index < IR::NUM_FIXED_FNC_TEXTURES; ++index) {
  240. auto attr = IR::Attribute::FixedFncTexture0S + index * 4;
  241. if (state.AnyComponent(attr)) {
  242. for (size_t i = 0; i < 4; ++i) {
  243. mapping.insert({attr + i, ununsed_generics.front() + i});
  244. }
  245. ununsed_generics.pop();
  246. }
  247. }
  248. return mapping;
  249. }
  250. void ConvertLegacyToGeneric(IR::Program& program, const Shader::RuntimeInfo& runtime_info) {
  251. auto& stores = program.info.stores;
  252. if (stores.Legacy()) {
  253. std::queue<IR::Attribute> ununsed_output_generics{};
  254. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  255. if (!stores.Generic(index)) {
  256. ununsed_output_generics.push(IR::Attribute::Generic0X + index * 4);
  257. }
  258. }
  259. auto mappings = GenerateLegacyToGenericMappings(stores, ununsed_output_generics);
  260. for (IR::Block* const block : program.post_order_blocks) {
  261. for (IR::Inst& inst : block->Instructions()) {
  262. switch (inst.GetOpcode()) {
  263. case IR::Opcode::SetAttribute: {
  264. const auto attr = inst.Arg(0).Attribute();
  265. if (IsLegacyAttribute(attr)) {
  266. stores.Set(mappings[attr], true);
  267. inst.SetArg(0, Shader::IR::Value(mappings[attr]));
  268. }
  269. break;
  270. }
  271. default:
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. auto& loads = program.info.loads;
  278. if (loads.Legacy()) {
  279. std::queue<IR::Attribute> ununsed_input_generics{};
  280. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  281. const AttributeType input_type{runtime_info.generic_input_types[index]};
  282. if (!runtime_info.previous_stage_stores.Generic(index) || !loads.Generic(index) ||
  283. input_type == AttributeType::Disabled) {
  284. ununsed_input_generics.push(IR::Attribute::Generic0X + index * 4);
  285. }
  286. }
  287. auto mappings = GenerateLegacyToGenericMappings(loads, ununsed_input_generics);
  288. for (IR::Block* const block : program.post_order_blocks) {
  289. for (IR::Inst& inst : block->Instructions()) {
  290. switch (inst.GetOpcode()) {
  291. case IR::Opcode::GetAttribute: {
  292. const auto attr = inst.Arg(0).Attribute();
  293. if (IsLegacyAttribute(attr)) {
  294. loads.Set(mappings[attr], true);
  295. inst.SetArg(0, Shader::IR::Value(mappings[attr]));
  296. }
  297. break;
  298. }
  299. default:
  300. break;
  301. }
  302. }
  303. }
  304. }
  305. }
  306. } // namespace Shader::Maxwell