emit_glsl_warp.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_view>
  5. #include "shader_recompiler/backend/glsl/emit_context.h"
  6. #include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
  7. #include "shader_recompiler/frontend/ir/value.h"
  8. #include "shader_recompiler/profile.h"
  9. namespace Shader::Backend::GLSL {
  10. namespace {
  11. void SetInBoundsFlag(EmitContext& ctx, IR::Inst& inst) {
  12. IR::Inst* const in_bounds{inst.GetAssociatedPseudoOperation(IR::Opcode::GetInBoundsFromOp)};
  13. if (!in_bounds) {
  14. return;
  15. }
  16. ctx.AddU1("{}=shfl_in_bounds;", *in_bounds);
  17. in_bounds->Invalidate();
  18. }
  19. std::string ComputeMinThreadId(std::string_view thread_id, std::string_view segmentation_mask) {
  20. return fmt::format("({}&{})", thread_id, segmentation_mask);
  21. }
  22. std::string ComputeMaxThreadId(std::string_view min_thread_id, std::string_view clamp,
  23. std::string_view not_seg_mask) {
  24. return fmt::format("({})|({}&{})", min_thread_id, clamp, not_seg_mask);
  25. }
  26. std::string GetMaxThreadId(std::string_view thread_id, std::string_view clamp,
  27. std::string_view segmentation_mask) {
  28. const auto not_seg_mask{fmt::format("(~{})", segmentation_mask)};
  29. const auto min_thread_id{ComputeMinThreadId(thread_id, segmentation_mask)};
  30. return ComputeMaxThreadId(min_thread_id, clamp, not_seg_mask);
  31. }
  32. } // namespace
  33. void EmitLaneId([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst) {
  34. throw NotImplementedException("GLSL Instruction");
  35. }
  36. void EmitVoteAll(EmitContext& ctx, IR::Inst& inst, std::string_view pred) {
  37. if (!ctx.profile.warp_size_potentially_larger_than_guest) {
  38. ctx.AddU1("{}=allInvocationsEqualARB({});", inst, pred);
  39. } else {
  40. const auto active_mask{fmt::format("uvec2(ballotARB(true))[gl_SubGroupInvocationARB]")};
  41. const auto ballot{fmt::format("uvec2(ballotARB({}))[gl_SubGroupInvocationARB]", pred)};
  42. ctx.AddU1("{}=({}&{})=={};", inst, ballot, active_mask, active_mask);
  43. }
  44. }
  45. void EmitVoteAny(EmitContext& ctx, IR::Inst& inst, std::string_view pred) {
  46. if (!ctx.profile.warp_size_potentially_larger_than_guest) {
  47. ctx.AddU1("{}=anyInvocationARB({});", inst, pred);
  48. } else {
  49. const auto active_mask{fmt::format("uvec2(ballotARB(true))[gl_SubGroupInvocationARB]")};
  50. const auto ballot{fmt::format("uvec2(ballotARB({}))[gl_SubGroupInvocationARB]", pred)};
  51. ctx.AddU1("{}=({}&{})!=0u;", inst, ballot, active_mask, active_mask);
  52. }
  53. }
  54. void EmitVoteEqual(EmitContext& ctx, IR::Inst& inst, std::string_view pred) {
  55. if (!ctx.profile.warp_size_potentially_larger_than_guest) {
  56. ctx.AddU1("{}=allInvocationsEqualARB({});", inst, pred);
  57. } else {
  58. const auto active_mask{fmt::format("uvec2(ballotARB(true))[gl_SubGroupInvocationARB]")};
  59. const auto ballot{fmt::format("uvec2(ballotARB({}))[gl_SubGroupInvocationARB]", pred)};
  60. const auto value{fmt::format("({}^{})", ballot, active_mask)};
  61. ctx.AddU1("{}=({}==0)||({}=={});", inst, value, value, active_mask);
  62. }
  63. }
  64. void EmitSubgroupBallot(EmitContext& ctx, IR::Inst& inst, std::string_view pred) {
  65. if (!ctx.profile.warp_size_potentially_larger_than_guest) {
  66. ctx.AddU32("{}=uvec2(ballotARB({})).x;", inst, pred);
  67. } else {
  68. ctx.AddU32("{}=uvec2(ballotARB({}))[gl_SubGroupInvocationARB];", inst, pred);
  69. }
  70. }
  71. void EmitSubgroupEqMask(EmitContext& ctx, IR::Inst& inst) {
  72. ctx.AddU32("{}=uvec2(gl_SubGroupEqMaskARB).x;", inst);
  73. }
  74. void EmitSubgroupLtMask(EmitContext& ctx, IR::Inst& inst) {
  75. ctx.AddU32("{}=uvec2(gl_SubGroupLtMaskARB).x;", inst);
  76. }
  77. void EmitSubgroupLeMask(EmitContext& ctx, IR::Inst& inst) {
  78. ctx.AddU32("{}=uvec2(gl_SubGroupLeMaskARB).x;", inst);
  79. }
  80. void EmitSubgroupGtMask(EmitContext& ctx, IR::Inst& inst) {
  81. ctx.AddU32("{}=uvec2(gl_SubGroupGtMaskARB).x;", inst);
  82. }
  83. void EmitSubgroupGeMask(EmitContext& ctx, IR::Inst& inst) {
  84. ctx.AddU32("{}=uvec2(gl_SubGroupGeMaskARB).x;", inst);
  85. }
  86. void EmitShuffleIndex(EmitContext& ctx, IR::Inst& inst, std::string_view value,
  87. std::string_view index, std::string_view clamp,
  88. std::string_view segmentation_mask) {
  89. const auto not_seg_mask{fmt::format("(~{})", segmentation_mask)};
  90. const auto thread_id{"gl_SubGroupInvocationARB"};
  91. const auto min_thread_id{ComputeMinThreadId(thread_id, segmentation_mask)};
  92. const auto max_thread_id{ComputeMaxThreadId(min_thread_id, clamp, not_seg_mask)};
  93. const auto lhs{fmt::format("({}&{})", index, not_seg_mask)};
  94. const auto src_thread_id{fmt::format("({})|({})", lhs, min_thread_id)};
  95. ctx.Add("shfl_in_bounds=int({})<=int({});", src_thread_id, max_thread_id);
  96. SetInBoundsFlag(ctx, inst);
  97. ctx.AddU32("{}=shfl_in_bounds?{}:{};", inst, value, src_thread_id);
  98. }
  99. void EmitShuffleUp(EmitContext& ctx, IR::Inst& inst, std::string_view value, std::string_view index,
  100. std::string_view clamp, std::string_view segmentation_mask) {
  101. const auto thread_id{"gl_SubGroupInvocationARB"};
  102. const auto max_thread_id{GetMaxThreadId(thread_id, clamp, segmentation_mask)};
  103. const auto src_thread_id{fmt::format("({}-{})", thread_id, index)};
  104. ctx.Add("shfl_in_bounds=int({})>=int({});", src_thread_id, max_thread_id);
  105. SetInBoundsFlag(ctx, inst);
  106. ctx.AddU32("{}=shfl_in_bounds?{}:{};", inst, value, src_thread_id);
  107. }
  108. void EmitShuffleDown(EmitContext& ctx, IR::Inst& inst, std::string_view value,
  109. std::string_view index, std::string_view clamp,
  110. std::string_view segmentation_mask) {
  111. const auto thread_id{"gl_SubGroupInvocationARB"};
  112. const auto max_thread_id{GetMaxThreadId(thread_id, clamp, segmentation_mask)};
  113. const auto src_thread_id{fmt::format("({}+{})", thread_id, index)};
  114. ctx.Add("shfl_in_bounds=int({})<=int({});", src_thread_id, max_thread_id);
  115. SetInBoundsFlag(ctx, inst);
  116. ctx.AddU32("{}=shfl_in_bounds?{}:{};", inst, value, src_thread_id);
  117. }
  118. void EmitShuffleButterfly(EmitContext& ctx, IR::Inst& inst, std::string_view value,
  119. std::string_view index, std::string_view clamp,
  120. std::string_view segmentation_mask) {
  121. const auto thread_id{"gl_SubGroupInvocationARB"};
  122. const auto max_thread_id{GetMaxThreadId(thread_id, clamp, segmentation_mask)};
  123. const auto src_thread_id{fmt::format("({}^{})", thread_id, index)};
  124. ctx.Add("shfl_in_bounds=int({})<=int({});", src_thread_id, max_thread_id);
  125. SetInBoundsFlag(ctx, inst);
  126. ctx.AddU32("{}=shfl_in_bounds?{}:{};", inst, value, src_thread_id);
  127. }
  128. void EmitFSwizzleAdd([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  129. [[maybe_unused]] std::string_view op_a, [[maybe_unused]] std::string_view op_b,
  130. [[maybe_unused]] std::string_view swizzle) {
  131. throw NotImplementedException("GLSL Instruction");
  132. }
  133. void EmitDPdxFine(EmitContext& ctx, IR::Inst& inst, std::string_view op_a) {
  134. ctx.AddF32("{}=dFdxFine({});", inst, op_a);
  135. }
  136. void EmitDPdyFine(EmitContext& ctx, IR::Inst& inst, std::string_view op_a) {
  137. ctx.AddF32("{}=dFdyFine({});", inst, op_a);
  138. }
  139. void EmitDPdxCoarse(EmitContext& ctx, IR::Inst& inst, std::string_view op_a) {
  140. ctx.AddF32("{}=dFdxCoarse({});", inst, op_a);
  141. }
  142. void EmitDPdyCoarse(EmitContext& ctx, IR::Inst& inst, std::string_view op_a) {
  143. ctx.AddF32("{}=dFdyCoarse({});", inst, op_a);
  144. }
  145. } // namespace Shader::Backend::GLSL