emit_glasm_composite.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "shader_recompiler/backend/glasm/emit_context.h"
  5. #include "shader_recompiler/backend/glasm/emit_glasm_instructions.h"
  6. #include "shader_recompiler/frontend/ir/value.h"
  7. namespace Shader::Backend::GLASM {
  8. namespace {
  9. template <auto read_imm, char type, typename... Values>
  10. void CompositeConstruct(EmitContext& ctx, IR::Inst& inst, Values&&... elements) {
  11. const Register ret{ctx.reg_alloc.Define(inst)};
  12. if (std::ranges::any_of(std::array{elements...},
  13. [](const IR::Value& value) { return value.IsImmediate(); })) {
  14. using Type = std::invoke_result_t<decltype(read_imm), IR::Value>;
  15. const std::array<Type, 4> values{(elements.IsImmediate() ? (elements.*read_imm)() : 0)...};
  16. ctx.Add("MOV.{} {},{{{},{},{},{}}};", type, ret, fmt::to_string(values[0]),
  17. fmt::to_string(values[1]), fmt::to_string(values[2]), fmt::to_string(values[3]));
  18. }
  19. size_t index{};
  20. for (const IR::Value& element : {elements...}) {
  21. if (!element.IsImmediate()) {
  22. const ScalarU32 value{ctx.reg_alloc.Consume(element)};
  23. ctx.Add("MOV.{} {}.{},{};", type, ret, "xyzw"[index], value);
  24. }
  25. ++index;
  26. }
  27. }
  28. void CompositeExtract(EmitContext& ctx, IR::Inst& inst, Register composite, u32 index, char type) {
  29. const Register ret{ctx.reg_alloc.Define(inst)};
  30. if (ret == composite && index == 0) {
  31. // No need to do anything here, the source and destination are the same register
  32. return;
  33. }
  34. ctx.Add("MOV.{} {}.x,{}.{};", type, ret, composite, "xyzw"[index]);
  35. }
  36. template <typename ObjectType>
  37. void CompositeInsert(EmitContext& ctx, IR::Inst& inst, Register composite, ObjectType object,
  38. u32 index, char type) {
  39. const Register ret{ctx.reg_alloc.Define(inst)};
  40. const char swizzle{"xyzw"[index]};
  41. if (ret != composite && ret == object) {
  42. // The object is aliased with the return value, so we have to use a temporary to insert
  43. ctx.Add("MOV.{} RC,{};"
  44. "MOV.{} RC.{},{};"
  45. "MOV.{} {},RC;",
  46. type, composite, type, swizzle, object, type, ret);
  47. } else if (ret != composite) {
  48. // The input composite is not aliased with the return value so we have to copy it before
  49. // hand. But the insert object is not aliased with the return value, so we don't have to
  50. // worry about that
  51. ctx.Add("MOV.{} {},{};MOV.{} {}.{},{};", type, ret, composite, type, ret, swizzle, object);
  52. } else {
  53. // The return value is alised so we can just insert the object, it doesn't matter if it's
  54. // aliased
  55. ctx.Add("MOV.{} {}.{},{};", type, ret, swizzle, object);
  56. }
  57. }
  58. } // Anonymous namespace
  59. void EmitCompositeConstructU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& e1,
  60. const IR::Value& e2) {
  61. CompositeConstruct<&IR::Value::U32, 'U'>(ctx, inst, e1, e2);
  62. }
  63. void EmitCompositeConstructU32x3(EmitContext& ctx, IR::Inst& inst, const IR::Value& e1,
  64. const IR::Value& e2, const IR::Value& e3) {
  65. CompositeConstruct<&IR::Value::U32, 'U'>(ctx, inst, e1, e2, e3);
  66. }
  67. void EmitCompositeConstructU32x4(EmitContext& ctx, IR::Inst& inst, const IR::Value& e1,
  68. const IR::Value& e2, const IR::Value& e3, const IR::Value& e4) {
  69. CompositeConstruct<&IR::Value::U32, 'U'>(ctx, inst, e1, e2, e3, e4);
  70. }
  71. void EmitCompositeExtractU32x2(EmitContext& ctx, IR::Inst& inst, Register composite, u32 index) {
  72. CompositeExtract(ctx, inst, composite, index, 'U');
  73. }
  74. void EmitCompositeExtractU32x3(EmitContext& ctx, IR::Inst& inst, Register composite, u32 index) {
  75. CompositeExtract(ctx, inst, composite, index, 'U');
  76. }
  77. void EmitCompositeExtractU32x4(EmitContext& ctx, IR::Inst& inst, Register composite, u32 index) {
  78. CompositeExtract(ctx, inst, composite, index, 'U');
  79. }
  80. void EmitCompositeInsertU32x2([[maybe_unused]] EmitContext& ctx,
  81. [[maybe_unused]] Register composite,
  82. [[maybe_unused]] ScalarU32 object, [[maybe_unused]] u32 index) {
  83. throw NotImplementedException("GLASM instruction");
  84. }
  85. void EmitCompositeInsertU32x3([[maybe_unused]] EmitContext& ctx,
  86. [[maybe_unused]] Register composite,
  87. [[maybe_unused]] ScalarU32 object, [[maybe_unused]] u32 index) {
  88. throw NotImplementedException("GLASM instruction");
  89. }
  90. void EmitCompositeInsertU32x4([[maybe_unused]] EmitContext& ctx,
  91. [[maybe_unused]] Register composite,
  92. [[maybe_unused]] ScalarU32 object, [[maybe_unused]] u32 index) {
  93. throw NotImplementedException("GLASM instruction");
  94. }
  95. void EmitCompositeConstructF16x2([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] Register e1,
  96. [[maybe_unused]] Register e2) {
  97. throw NotImplementedException("GLASM instruction");
  98. }
  99. void EmitCompositeConstructF16x3([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] Register e1,
  100. [[maybe_unused]] Register e2, [[maybe_unused]] Register e3) {
  101. throw NotImplementedException("GLASM instruction");
  102. }
  103. void EmitCompositeConstructF16x4([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] Register e1,
  104. [[maybe_unused]] Register e2, [[maybe_unused]] Register e3,
  105. [[maybe_unused]] Register e4) {
  106. throw NotImplementedException("GLASM instruction");
  107. }
  108. void EmitCompositeExtractF16x2([[maybe_unused]] EmitContext& ctx,
  109. [[maybe_unused]] Register composite, [[maybe_unused]] u32 index) {
  110. throw NotImplementedException("GLASM instruction");
  111. }
  112. void EmitCompositeExtractF16x3([[maybe_unused]] EmitContext& ctx,
  113. [[maybe_unused]] Register composite, [[maybe_unused]] u32 index) {
  114. throw NotImplementedException("GLASM instruction");
  115. }
  116. void EmitCompositeExtractF16x4([[maybe_unused]] EmitContext& ctx,
  117. [[maybe_unused]] Register composite, [[maybe_unused]] u32 index) {
  118. throw NotImplementedException("GLASM instruction");
  119. }
  120. void EmitCompositeInsertF16x2([[maybe_unused]] EmitContext& ctx,
  121. [[maybe_unused]] Register composite, [[maybe_unused]] Register object,
  122. [[maybe_unused]] u32 index) {
  123. throw NotImplementedException("GLASM instruction");
  124. }
  125. void EmitCompositeInsertF16x3([[maybe_unused]] EmitContext& ctx,
  126. [[maybe_unused]] Register composite, [[maybe_unused]] Register object,
  127. [[maybe_unused]] u32 index) {
  128. throw NotImplementedException("GLASM instruction");
  129. }
  130. void EmitCompositeInsertF16x4([[maybe_unused]] EmitContext& ctx,
  131. [[maybe_unused]] Register composite, [[maybe_unused]] Register object,
  132. [[maybe_unused]] u32 index) {
  133. throw NotImplementedException("GLASM instruction");
  134. }
  135. void EmitCompositeConstructF32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& e1,
  136. const IR::Value& e2) {
  137. CompositeConstruct<&IR::Value::F32, 'F'>(ctx, inst, e1, e2);
  138. }
  139. void EmitCompositeConstructF32x3(EmitContext& ctx, IR::Inst& inst, const IR::Value& e1,
  140. const IR::Value& e2, const IR::Value& e3) {
  141. CompositeConstruct<&IR::Value::F32, 'F'>(ctx, inst, e1, e2, e3);
  142. }
  143. void EmitCompositeConstructF32x4(EmitContext& ctx, IR::Inst& inst, const IR::Value& e1,
  144. const IR::Value& e2, const IR::Value& e3, const IR::Value& e4) {
  145. CompositeConstruct<&IR::Value::F32, 'F'>(ctx, inst, e1, e2, e3, e4);
  146. }
  147. void EmitCompositeExtractF32x2(EmitContext& ctx, IR::Inst& inst, Register composite, u32 index) {
  148. CompositeExtract(ctx, inst, composite, index, 'F');
  149. }
  150. void EmitCompositeExtractF32x3(EmitContext& ctx, IR::Inst& inst, Register composite, u32 index) {
  151. CompositeExtract(ctx, inst, composite, index, 'F');
  152. }
  153. void EmitCompositeExtractF32x4(EmitContext& ctx, IR::Inst& inst, Register composite, u32 index) {
  154. CompositeExtract(ctx, inst, composite, index, 'F');
  155. }
  156. void EmitCompositeInsertF32x2(EmitContext& ctx, IR::Inst& inst, Register composite,
  157. ScalarF32 object, u32 index) {
  158. CompositeInsert(ctx, inst, composite, object, index, 'F');
  159. }
  160. void EmitCompositeInsertF32x3(EmitContext& ctx, IR::Inst& inst, Register composite,
  161. ScalarF32 object, u32 index) {
  162. CompositeInsert(ctx, inst, composite, object, index, 'F');
  163. }
  164. void EmitCompositeInsertF32x4(EmitContext& ctx, IR::Inst& inst, Register composite,
  165. ScalarF32 object, u32 index) {
  166. CompositeInsert(ctx, inst, composite, object, index, 'F');
  167. }
  168. void EmitCompositeConstructF64x2([[maybe_unused]] EmitContext& ctx) {
  169. throw NotImplementedException("GLASM instruction");
  170. }
  171. void EmitCompositeConstructF64x3([[maybe_unused]] EmitContext& ctx) {
  172. throw NotImplementedException("GLASM instruction");
  173. }
  174. void EmitCompositeConstructF64x4([[maybe_unused]] EmitContext& ctx) {
  175. throw NotImplementedException("GLASM instruction");
  176. }
  177. void EmitCompositeExtractF64x2([[maybe_unused]] EmitContext& ctx) {
  178. throw NotImplementedException("GLASM instruction");
  179. }
  180. void EmitCompositeExtractF64x3([[maybe_unused]] EmitContext& ctx) {
  181. throw NotImplementedException("GLASM instruction");
  182. }
  183. void EmitCompositeExtractF64x4([[maybe_unused]] EmitContext& ctx) {
  184. throw NotImplementedException("GLASM instruction");
  185. }
  186. void EmitCompositeInsertF64x2([[maybe_unused]] EmitContext& ctx,
  187. [[maybe_unused]] Register composite, [[maybe_unused]] Register object,
  188. [[maybe_unused]] u32 index) {
  189. throw NotImplementedException("GLASM instruction");
  190. }
  191. void EmitCompositeInsertF64x3([[maybe_unused]] EmitContext& ctx,
  192. [[maybe_unused]] Register composite, [[maybe_unused]] Register object,
  193. [[maybe_unused]] u32 index) {
  194. throw NotImplementedException("GLASM instruction");
  195. }
  196. void EmitCompositeInsertF64x4([[maybe_unused]] EmitContext& ctx,
  197. [[maybe_unused]] Register composite, [[maybe_unused]] Register object,
  198. [[maybe_unused]] u32 index) {
  199. throw NotImplementedException("GLASM instruction");
  200. }
  201. } // namespace Shader::Backend::GLASM