emit_glsl.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <ranges>
  5. #include <string>
  6. #include "shader_recompiler/backend/glsl/emit_context.h"
  7. #include "shader_recompiler/backend/glsl/emit_glsl.h"
  8. #include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
  9. #include "shader_recompiler/frontend/ir/ir_emitter.h"
  10. namespace Shader::Backend::GLSL {
  11. namespace {
  12. template <class Func>
  13. struct FuncTraits {};
  14. template <class ReturnType_, class... Args>
  15. struct FuncTraits<ReturnType_ (*)(Args...)> {
  16. using ReturnType = ReturnType_;
  17. static constexpr size_t NUM_ARGS = sizeof...(Args);
  18. template <size_t I>
  19. using ArgType = std::tuple_element_t<I, std::tuple<Args...>>;
  20. };
  21. template <auto func, typename... Args>
  22. void SetDefinition(EmitContext& ctx, IR::Inst* inst, Args... args) {
  23. inst->SetDefinition<Id>(func(ctx, std::forward<Args>(args)...));
  24. }
  25. template <typename ArgType>
  26. auto Arg(EmitContext& ctx, const IR::Value& arg) {
  27. if constexpr (std::is_same_v<ArgType, std::string_view>) {
  28. return ctx.reg_alloc.Consume(arg);
  29. } else if constexpr (std::is_same_v<ArgType, const IR::Value&>) {
  30. return arg;
  31. } else if constexpr (std::is_same_v<ArgType, u32>) {
  32. return arg.U32();
  33. } else if constexpr (std::is_same_v<ArgType, IR::Attribute>) {
  34. return arg.Attribute();
  35. } else if constexpr (std::is_same_v<ArgType, IR::Patch>) {
  36. return arg.Patch();
  37. } else if constexpr (std::is_same_v<ArgType, IR::Reg>) {
  38. return arg.Reg();
  39. }
  40. }
  41. template <auto func, bool is_first_arg_inst, size_t... I>
  42. void Invoke(EmitContext& ctx, IR::Inst* inst, std::index_sequence<I...>) {
  43. using Traits = FuncTraits<decltype(func)>;
  44. if constexpr (std::is_same_v<typename Traits::ReturnType, Id>) {
  45. if constexpr (is_first_arg_inst) {
  46. SetDefinition<func>(
  47. ctx, inst, *inst,
  48. Arg<typename Traits::template ArgType<I + 2>>(ctx, inst->Arg(I))...);
  49. } else {
  50. SetDefinition<func>(
  51. ctx, inst, Arg<typename Traits::template ArgType<I + 1>>(ctx, inst->Arg(I))...);
  52. }
  53. } else {
  54. if constexpr (is_first_arg_inst) {
  55. func(ctx, *inst, Arg<typename Traits::template ArgType<I + 2>>(ctx, inst->Arg(I))...);
  56. } else {
  57. func(ctx, Arg<typename Traits::template ArgType<I + 1>>(ctx, inst->Arg(I))...);
  58. }
  59. }
  60. }
  61. template <auto func>
  62. void Invoke(EmitContext& ctx, IR::Inst* inst) {
  63. using Traits = FuncTraits<decltype(func)>;
  64. static_assert(Traits::NUM_ARGS >= 1, "Insufficient arguments");
  65. if constexpr (Traits::NUM_ARGS == 1) {
  66. Invoke<func, false>(ctx, inst, std::make_index_sequence<0>{});
  67. } else {
  68. using FirstArgType = typename Traits::template ArgType<1>;
  69. static constexpr bool is_first_arg_inst = std::is_same_v<FirstArgType, IR::Inst&>;
  70. using Indices = std::make_index_sequence<Traits::NUM_ARGS - (is_first_arg_inst ? 2 : 1)>;
  71. Invoke<func, is_first_arg_inst>(ctx, inst, Indices{});
  72. }
  73. }
  74. void EmitInst(EmitContext& ctx, IR::Inst* inst) {
  75. // ctx.Add("/* $ {} $ */", inst->GetOpcode());
  76. switch (inst->GetOpcode()) {
  77. #define OPCODE(name, result_type, ...) \
  78. case IR::Opcode::name: \
  79. return Invoke<&Emit##name>(ctx, inst);
  80. #include "shader_recompiler/frontend/ir/opcodes.inc"
  81. #undef OPCODE
  82. }
  83. throw LogicError("Invalid opcode {}", inst->GetOpcode());
  84. }
  85. bool IsReference(IR::Inst& inst) {
  86. return inst.GetOpcode() == IR::Opcode::Reference;
  87. }
  88. void PrecolorInst(IR::Inst& phi) {
  89. // Insert phi moves before references to avoid overwritting other phis
  90. const size_t num_args{phi.NumArgs()};
  91. for (size_t i = 0; i < num_args; ++i) {
  92. IR::Block& phi_block{*phi.PhiBlock(i)};
  93. auto it{std::find_if_not(phi_block.rbegin(), phi_block.rend(), IsReference).base()};
  94. IR::IREmitter ir{phi_block, it};
  95. const IR::Value arg{phi.Arg(i)};
  96. if (arg.IsImmediate()) {
  97. ir.PhiMove(phi, arg);
  98. } else {
  99. ir.PhiMove(phi, IR::Value{arg.InstRecursive()});
  100. }
  101. }
  102. }
  103. void Precolor(const IR::Program& program) {
  104. for (IR::Block* const block : program.blocks) {
  105. for (IR::Inst& phi : block->Instructions() | std::views::take_while(IR::IsPhi)) {
  106. PrecolorInst(phi);
  107. }
  108. }
  109. }
  110. void EmitCode(EmitContext& ctx, const IR::Program& program) {
  111. for (const IR::AbstractSyntaxNode& node : program.syntax_list) {
  112. switch (node.type) {
  113. case IR::AbstractSyntaxNode::Type::Block:
  114. for (IR::Inst& inst : node.data.block->Instructions()) {
  115. EmitInst(ctx, &inst);
  116. }
  117. break;
  118. case IR::AbstractSyntaxNode::Type::If:
  119. ctx.Add("if ({}){{", ctx.reg_alloc.Consume(node.data.if_node.cond));
  120. break;
  121. case IR::AbstractSyntaxNode::Type::EndIf:
  122. ctx.Add("}}");
  123. break;
  124. case IR::AbstractSyntaxNode::Type::Break:
  125. if (node.data.break_node.cond.IsImmediate()) {
  126. if (node.data.break_node.cond.U1()) {
  127. ctx.Add("break;");
  128. }
  129. } else {
  130. ctx.Add("if({}){{break;}}", ctx.reg_alloc.Consume(node.data.break_node.cond));
  131. }
  132. break;
  133. case IR::AbstractSyntaxNode::Type::Return:
  134. case IR::AbstractSyntaxNode::Type::Unreachable:
  135. ctx.Add("return;");
  136. break;
  137. case IR::AbstractSyntaxNode::Type::Loop:
  138. ctx.Add("for(;;){{");
  139. break;
  140. case IR::AbstractSyntaxNode::Type::Repeat:
  141. ctx.Add("if({}){{", ctx.reg_alloc.Consume(node.data.repeat.cond));
  142. ctx.Add("continue;\n}}else{{");
  143. ctx.Add("break;\n}}\n}}");
  144. break;
  145. default:
  146. fmt::print("{}", node.type);
  147. throw NotImplementedException("{}", node.type);
  148. break;
  149. }
  150. }
  151. }
  152. std::string GlslVersionSpecifier(const EmitContext& ctx) {
  153. if (ctx.uses_y_direction) {
  154. return " compatibility";
  155. }
  156. return "";
  157. }
  158. } // Anonymous namespace
  159. std::string EmitGLSL(const Profile& profile, const RuntimeInfo& runtime_info, IR::Program& program,
  160. Bindings& bindings) {
  161. EmitContext ctx{program, bindings, profile, runtime_info};
  162. Precolor(program);
  163. EmitCode(ctx, program);
  164. const std::string version{fmt::format("#version 450{}\n", GlslVersionSpecifier(ctx))};
  165. ctx.header.insert(0, version);
  166. for (size_t index = 0; index < ctx.reg_alloc.num_used_registers; ++index) {
  167. ctx.header += fmt::format("{} R{};", ctx.reg_alloc.reg_types[index], index);
  168. }
  169. if (ctx.uses_cc_carry) {
  170. ctx.header += "uint carry;";
  171. }
  172. if (program.info.uses_subgroup_shuffles) {
  173. ctx.header += "bool shfl_in_bounds;";
  174. }
  175. ctx.header += "\n";
  176. ctx.code.insert(0, ctx.header);
  177. ctx.code += "}";
  178. // fmt::print("\n{}\n", ctx.code);
  179. return ctx.code;
  180. }
  181. } // namespace Shader::Backend::GLSL