emit_glsl.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <string>
  5. #include <tuple>
  6. #include "shader_recompiler/backend/bindings.h"
  7. #include "shader_recompiler/backend/glsl/emit_context.h"
  8. #include "shader_recompiler/backend/glsl/emit_glsl.h"
  9. #include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
  10. #include "shader_recompiler/frontend/ir/program.h"
  11. #include "shader_recompiler/profile.h"
  12. #pragma optimize("", off)
  13. namespace Shader::Backend::GLSL {
  14. namespace {
  15. template <class Func>
  16. struct FuncTraits {};
  17. template <class ReturnType_, class... Args>
  18. struct FuncTraits<ReturnType_ (*)(Args...)> {
  19. using ReturnType = ReturnType_;
  20. static constexpr size_t NUM_ARGS = sizeof...(Args);
  21. template <size_t I>
  22. using ArgType = std::tuple_element_t<I, std::tuple<Args...>>;
  23. };
  24. template <auto func, typename... Args>
  25. void SetDefinition(EmitContext& ctx, IR::Inst* inst, Args... args) {
  26. inst->SetDefinition<Id>(func(ctx, std::forward<Args>(args)...));
  27. }
  28. template <typename ArgType>
  29. ArgType Arg(EmitContext& ctx, const IR::Value& arg) {
  30. if constexpr (std::is_same_v<ArgType, std::string_view>) {
  31. return ctx.reg_alloc.Consume(arg);
  32. } else if constexpr (std::is_same_v<ArgType, IR::Inst&>) {
  33. return *arg.Inst();
  34. } else if constexpr (std::is_same_v<ArgType, const IR::Value&>) {
  35. return arg;
  36. } else if constexpr (std::is_same_v<ArgType, u32>) {
  37. return arg.U32();
  38. } else if constexpr (std::is_same_v<ArgType, IR::Attribute>) {
  39. return arg.Attribute();
  40. } else if constexpr (std::is_same_v<ArgType, IR::Patch>) {
  41. return arg.Patch();
  42. } else if constexpr (std::is_same_v<ArgType, IR::Reg>) {
  43. return arg.Reg();
  44. }
  45. }
  46. template <auto func, bool is_first_arg_inst, size_t... I>
  47. void Invoke(EmitContext& ctx, IR::Inst* inst, std::index_sequence<I...>) {
  48. using Traits = FuncTraits<decltype(func)>;
  49. if constexpr (std::is_same_v<typename Traits::ReturnType, Id>) {
  50. if constexpr (is_first_arg_inst) {
  51. SetDefinition<func>(
  52. ctx, inst, inst,
  53. Arg<typename Traits::template ArgType<I + 2>>(ctx, inst->Arg(I))...);
  54. } else {
  55. SetDefinition<func>(
  56. ctx, inst, Arg<typename Traits::template ArgType<I + 1>>(ctx, inst->Arg(I))...);
  57. }
  58. } else {
  59. if constexpr (is_first_arg_inst) {
  60. func(ctx, inst, Arg<typename Traits::template ArgType<I + 2>>(ctx, inst->Arg(I))...);
  61. } else {
  62. func(ctx, Arg<typename Traits::template ArgType<I + 1>>(ctx, inst->Arg(I))...);
  63. }
  64. }
  65. }
  66. template <auto func>
  67. void Invoke(EmitContext& ctx, IR::Inst* inst) {
  68. using Traits = FuncTraits<decltype(func)>;
  69. static_assert(Traits::NUM_ARGS >= 1, "Insufficient arguments");
  70. if constexpr (Traits::NUM_ARGS == 1) {
  71. Invoke<func, false>(ctx, inst, std::make_index_sequence<0>{});
  72. } else {
  73. using FirstArgType = typename Traits::template ArgType<1>;
  74. static constexpr bool is_first_arg_inst = std::is_same_v<FirstArgType, IR::Inst*>;
  75. using Indices = std::make_index_sequence<Traits::NUM_ARGS - (is_first_arg_inst ? 2 : 1)>;
  76. Invoke<func, is_first_arg_inst>(ctx, inst, Indices{});
  77. }
  78. }
  79. void EmitInst(EmitContext& ctx, IR::Inst* inst) {
  80. switch (inst->GetOpcode()) {
  81. #define OPCODE(name, result_type, ...) \
  82. case IR::Opcode::name: \
  83. return Invoke<&Emit##name>(ctx, inst);
  84. #include "shader_recompiler/frontend/ir/opcodes.inc"
  85. #undef OPCODE
  86. }
  87. throw LogicError("Invalid opcode {}", inst->GetOpcode());
  88. }
  89. void EmitCode(EmitContext& ctx, const IR::Program& program) {
  90. for (const IR::AbstractSyntaxNode& node : program.syntax_list) {
  91. switch (node.type) {
  92. case IR::AbstractSyntaxNode::Type::Block:
  93. for (IR::Inst& inst : node.data.block->Instructions()) {
  94. EmitInst(ctx, &inst);
  95. }
  96. break;
  97. case IR::AbstractSyntaxNode::Type::If:
  98. ctx.Add("if (");
  99. break;
  100. case IR::AbstractSyntaxNode::Type::EndIf:
  101. ctx.Add("){{");
  102. break;
  103. case IR::AbstractSyntaxNode::Type::Loop:
  104. ctx.Add("while (");
  105. break;
  106. case IR::AbstractSyntaxNode::Type::Repeat:
  107. if (node.data.repeat.cond.IsImmediate()) {
  108. if (node.data.repeat.cond.U1()) {
  109. ctx.Add("ENDREP;");
  110. } else {
  111. ctx.Add("BRK;"
  112. "ENDREP;");
  113. }
  114. }
  115. break;
  116. case IR::AbstractSyntaxNode::Type::Break:
  117. if (node.data.break_node.cond.IsImmediate()) {
  118. if (node.data.break_node.cond.U1()) {
  119. ctx.Add("break;");
  120. }
  121. }
  122. break;
  123. case IR::AbstractSyntaxNode::Type::Return:
  124. case IR::AbstractSyntaxNode::Type::Unreachable:
  125. ctx.Add("return;");
  126. break;
  127. default:
  128. ctx.Add("UNAHNDLED {}", node.type);
  129. break;
  130. }
  131. }
  132. }
  133. } // Anonymous namespace
  134. std::string EmitGLSL(const Profile& profile, IR::Program& program, Bindings& bindings) {
  135. EmitContext ctx{program, bindings, profile};
  136. // ctx.SetupBuffers();
  137. EmitCode(ctx, program);
  138. ctx.code += "}";
  139. return ctx.code;
  140. }
  141. } // namespace Shader::Backend::GLSL