translate_program.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <ranges>
  7. #include <vector>
  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. auto syntax_blocks{syntax_list | std::views::filter([](const auto& node) {
  21. return node.type == IR::AbstractSyntaxNode::Type::Block;
  22. })};
  23. IR::BlockList blocks(std::ranges::distance(syntax_blocks));
  24. std::ranges::transform(syntax_blocks, blocks.begin(),
  25. [](const IR::AbstractSyntaxNode& node) { return node.data.block; });
  26. return blocks;
  27. }
  28. void RemoveUnreachableBlocks(IR::Program& program) {
  29. // Some blocks might be unreachable if a function call exists unconditionally
  30. // If this happens the number of blocks and post order blocks will mismatch
  31. if (program.blocks.size() == program.post_order_blocks.size()) {
  32. return;
  33. }
  34. const auto begin{program.blocks.begin() + 1};
  35. const auto end{program.blocks.end()};
  36. const auto pred{[](IR::Block* block) { return block->ImmPredecessors().empty(); }};
  37. program.blocks.erase(std::remove_if(begin, end, pred), end);
  38. }
  39. void CollectInterpolationInfo(Environment& env, IR::Program& program) {
  40. if (program.stage != Stage::Fragment) {
  41. return;
  42. }
  43. const ProgramHeader& sph{env.SPH()};
  44. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  45. std::optional<PixelImap> imap;
  46. for (const PixelImap value : sph.ps.GenericInputMap(static_cast<u32>(index))) {
  47. if (value == PixelImap::Unused) {
  48. continue;
  49. }
  50. if (imap && imap != value) {
  51. throw NotImplementedException("Per component interpolation");
  52. }
  53. imap = value;
  54. }
  55. if (!imap) {
  56. continue;
  57. }
  58. program.info.interpolation[index] = [&] {
  59. switch (*imap) {
  60. case PixelImap::Unused:
  61. case PixelImap::Perspective:
  62. return Interpolation::Smooth;
  63. case PixelImap::Constant:
  64. return Interpolation::Flat;
  65. case PixelImap::ScreenLinear:
  66. return Interpolation::NoPerspective;
  67. }
  68. throw NotImplementedException("Unknown interpolation {}", *imap);
  69. }();
  70. }
  71. }
  72. void AddNVNStorageBuffers(IR::Program& program) {
  73. if (!program.info.uses_global_memory) {
  74. return;
  75. }
  76. const u32 driver_cbuf{0};
  77. const u32 descriptor_size{0x10};
  78. const u32 num_buffers{16};
  79. const u32 base{[&] {
  80. switch (program.stage) {
  81. case Stage::VertexA:
  82. case Stage::VertexB:
  83. return 0x110u;
  84. case Stage::TessellationControl:
  85. return 0x210u;
  86. case Stage::TessellationEval:
  87. return 0x310u;
  88. case Stage::Geometry:
  89. return 0x410u;
  90. case Stage::Fragment:
  91. return 0x510u;
  92. case Stage::Compute:
  93. return 0x310u;
  94. }
  95. throw InvalidArgument("Invalid stage {}", program.stage);
  96. }()};
  97. auto& descs{program.info.storage_buffers_descriptors};
  98. for (u32 index = 0; index < num_buffers; ++index) {
  99. if (!program.info.nvn_buffer_used[index]) {
  100. continue;
  101. }
  102. const u32 offset{base + index * descriptor_size};
  103. const auto it{std::ranges::find(descs, offset, &StorageBufferDescriptor::cbuf_offset)};
  104. if (it != descs.end()) {
  105. it->is_written |= program.info.stores_global_memory;
  106. continue;
  107. }
  108. descs.push_back({
  109. .cbuf_index = driver_cbuf,
  110. .cbuf_offset = offset,
  111. .count = 1,
  112. .is_written = program.info.stores_global_memory,
  113. });
  114. }
  115. }
  116. } // Anonymous namespace
  117. IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
  118. Environment& env, Flow::CFG& cfg, const HostTranslateInfo& host_info) {
  119. IR::Program program;
  120. program.syntax_list = BuildASL(inst_pool, block_pool, env, cfg);
  121. program.blocks = GenerateBlocks(program.syntax_list);
  122. program.post_order_blocks = PostOrder(program.syntax_list.front());
  123. program.stage = env.ShaderStage();
  124. program.local_memory_size = env.LocalMemorySize();
  125. switch (program.stage) {
  126. case Stage::TessellationControl: {
  127. const ProgramHeader& sph{env.SPH()};
  128. program.invocations = sph.common2.threads_per_input_primitive;
  129. break;
  130. }
  131. case Stage::Geometry: {
  132. const ProgramHeader& sph{env.SPH()};
  133. program.output_topology = sph.common3.output_topology;
  134. program.output_vertices = sph.common4.max_output_vertices;
  135. program.invocations = sph.common2.threads_per_input_primitive;
  136. program.is_geometry_passthrough = sph.common0.geometry_passthrough != 0;
  137. if (program.is_geometry_passthrough) {
  138. const auto& mask{env.GpPassthroughMask()};
  139. for (size_t i = 0; i < program.info.passthrough.mask.size(); ++i) {
  140. program.info.passthrough.mask[i] = ((mask[i / 32] >> (i % 32)) & 1) == 0;
  141. }
  142. }
  143. break;
  144. }
  145. case Stage::Compute:
  146. program.workgroup_size = env.WorkgroupSize();
  147. program.shared_memory_size = env.SharedMemorySize();
  148. break;
  149. default:
  150. break;
  151. }
  152. RemoveUnreachableBlocks(program);
  153. // Replace instructions before the SSA rewrite
  154. if (!host_info.support_float16) {
  155. Optimization::LowerFp16ToFp32(program);
  156. }
  157. if (!host_info.support_int64) {
  158. Optimization::LowerInt64ToInt32(program);
  159. }
  160. Optimization::SsaRewritePass(program);
  161. Optimization::GlobalMemoryToStorageBufferPass(program);
  162. Optimization::TexturePass(env, program);
  163. Optimization::ConstantPropagationPass(program);
  164. Optimization::DeadCodeEliminationPass(program);
  165. if (Settings::values.renderer_debug) {
  166. Optimization::VerificationPass(program);
  167. }
  168. Optimization::CollectShaderInfoPass(env, program);
  169. CollectInterpolationInfo(env, program);
  170. AddNVNStorageBuffers(program);
  171. return program;
  172. }
  173. IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b,
  174. Environment& env_vertex_b) {
  175. IR::Program result{};
  176. Optimization::VertexATransformPass(vertex_a);
  177. Optimization::VertexBTransformPass(vertex_b);
  178. for (const auto& term : vertex_a.syntax_list) {
  179. if (term.type != IR::AbstractSyntaxNode::Type::Return) {
  180. result.syntax_list.push_back(term);
  181. }
  182. }
  183. result.syntax_list.insert(result.syntax_list.end(), vertex_b.syntax_list.begin(),
  184. vertex_b.syntax_list.end());
  185. result.blocks = GenerateBlocks(result.syntax_list);
  186. result.post_order_blocks = vertex_b.post_order_blocks;
  187. for (const auto& block : vertex_a.post_order_blocks) {
  188. result.post_order_blocks.push_back(block);
  189. }
  190. result.stage = Stage::VertexB;
  191. result.info = vertex_a.info;
  192. result.local_memory_size = std::max(vertex_a.local_memory_size, vertex_b.local_memory_size);
  193. result.info.loads.mask |= vertex_b.info.loads.mask;
  194. result.info.stores.mask |= vertex_b.info.stores.mask;
  195. Optimization::JoinTextureInfo(result.info, vertex_b.info);
  196. Optimization::JoinStorageInfo(result.info, vertex_b.info);
  197. Optimization::DeadCodeEliminationPass(result);
  198. if (Settings::values.renderer_debug) {
  199. Optimization::VerificationPass(result);
  200. }
  201. Optimization::CollectShaderInfoPass(env_vertex_b, result);
  202. return result;
  203. }
  204. } // namespace Shader::Maxwell