translate_program.cpp 8.2 KB

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