emit_glsl_composite.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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_glsl_instructions.h"
  6. #include "shader_recompiler/backend/glsl/glsl_emit_context.h"
  7. #include "shader_recompiler/frontend/ir/value.h"
  8. namespace Shader::Backend::GLSL {
  9. namespace {
  10. constexpr std::string_view SWIZZLE{"xyzw"};
  11. void CompositeInsert(EmitContext& ctx, std::string_view result, std::string_view composite,
  12. std::string_view object, u32 index) {
  13. if (result == composite) {
  14. // The result is aliased with the composite
  15. ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);
  16. } else {
  17. ctx.Add("{}={};{}.{}={};", result, composite, result, SWIZZLE[index], object);
  18. }
  19. }
  20. } // Anonymous namespace
  21. void EmitCompositeConstructU32x2(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
  22. std::string_view e2) {
  23. ctx.AddU32x2("{}=uvec2({},{});", inst, e1, e2);
  24. }
  25. void EmitCompositeConstructU32x3(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
  26. std::string_view e2, std::string_view e3) {
  27. ctx.AddU32x3("{}=uvec3({},{},{});", inst, e1, e2, e3);
  28. }
  29. void EmitCompositeConstructU32x4(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
  30. std::string_view e2, std::string_view e3, std::string_view e4) {
  31. ctx.AddU32x4("{}=uvec4({},{},{},{});", inst, e1, e2, e3, e4);
  32. }
  33. void EmitCompositeExtractU32x2(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  34. u32 index) {
  35. ctx.AddU32("{}={}.{};", inst, composite, SWIZZLE[index]);
  36. }
  37. void EmitCompositeExtractU32x3(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  38. u32 index) {
  39. ctx.AddU32("{}={}.{};", inst, composite, SWIZZLE[index]);
  40. }
  41. void EmitCompositeExtractU32x4(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  42. u32 index) {
  43. ctx.AddU32("{}={}.{};", inst, composite, SWIZZLE[index]);
  44. }
  45. void EmitCompositeInsertU32x2(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  46. std::string_view object, u32 index) {
  47. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32x2)};
  48. CompositeInsert(ctx, ret, composite, object, index);
  49. }
  50. void EmitCompositeInsertU32x3(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  51. std::string_view object, u32 index) {
  52. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32x3)};
  53. CompositeInsert(ctx, ret, composite, object, index);
  54. }
  55. void EmitCompositeInsertU32x4(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  56. std::string_view object, u32 index) {
  57. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32x4)};
  58. CompositeInsert(ctx, ret, composite, object, index);
  59. }
  60. void EmitCompositeConstructF16x2([[maybe_unused]] EmitContext& ctx,
  61. [[maybe_unused]] std::string_view e1,
  62. [[maybe_unused]] std::string_view e2) {
  63. NotImplemented();
  64. }
  65. void EmitCompositeConstructF16x3([[maybe_unused]] EmitContext& ctx,
  66. [[maybe_unused]] std::string_view e1,
  67. [[maybe_unused]] std::string_view e2,
  68. [[maybe_unused]] std::string_view e3) {
  69. NotImplemented();
  70. }
  71. void EmitCompositeConstructF16x4([[maybe_unused]] EmitContext& ctx,
  72. [[maybe_unused]] std::string_view e1,
  73. [[maybe_unused]] std::string_view e2,
  74. [[maybe_unused]] std::string_view e3,
  75. [[maybe_unused]] std::string_view e4) {
  76. NotImplemented();
  77. }
  78. void EmitCompositeExtractF16x2([[maybe_unused]] EmitContext& ctx,
  79. [[maybe_unused]] std::string_view composite,
  80. [[maybe_unused]] u32 index) {
  81. NotImplemented();
  82. }
  83. void EmitCompositeExtractF16x3([[maybe_unused]] EmitContext& ctx,
  84. [[maybe_unused]] std::string_view composite,
  85. [[maybe_unused]] u32 index) {
  86. NotImplemented();
  87. }
  88. void EmitCompositeExtractF16x4([[maybe_unused]] EmitContext& ctx,
  89. [[maybe_unused]] std::string_view composite,
  90. [[maybe_unused]] u32 index) {
  91. NotImplemented();
  92. }
  93. void EmitCompositeInsertF16x2([[maybe_unused]] EmitContext& ctx,
  94. [[maybe_unused]] std::string_view composite,
  95. [[maybe_unused]] std::string_view object,
  96. [[maybe_unused]] u32 index) {
  97. NotImplemented();
  98. }
  99. void EmitCompositeInsertF16x3([[maybe_unused]] EmitContext& ctx,
  100. [[maybe_unused]] std::string_view composite,
  101. [[maybe_unused]] std::string_view object,
  102. [[maybe_unused]] u32 index) {
  103. NotImplemented();
  104. }
  105. void EmitCompositeInsertF16x4([[maybe_unused]] EmitContext& ctx,
  106. [[maybe_unused]] std::string_view composite,
  107. [[maybe_unused]] std::string_view object,
  108. [[maybe_unused]] u32 index) {
  109. NotImplemented();
  110. }
  111. void EmitCompositeConstructF32x2(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
  112. std::string_view e2) {
  113. ctx.AddF32x2("{}=vec2({},{});", inst, e1, e2);
  114. }
  115. void EmitCompositeConstructF32x3(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
  116. std::string_view e2, std::string_view e3) {
  117. ctx.AddF32x3("{}=vec3({},{},{});", inst, e1, e2, e3);
  118. }
  119. void EmitCompositeConstructF32x4(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
  120. std::string_view e2, std::string_view e3, std::string_view e4) {
  121. ctx.AddF32x4("{}=vec4({},{},{},{});", inst, e1, e2, e3, e4);
  122. }
  123. void EmitCompositeExtractF32x2(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  124. u32 index) {
  125. ctx.AddF32("{}={}.{};", inst, composite, SWIZZLE[index]);
  126. }
  127. void EmitCompositeExtractF32x3(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  128. u32 index) {
  129. ctx.AddF32("{}={}.{};", inst, composite, SWIZZLE[index]);
  130. }
  131. void EmitCompositeExtractF32x4(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  132. u32 index) {
  133. ctx.AddF32("{}={}.{};", inst, composite, SWIZZLE[index]);
  134. }
  135. void EmitCompositeInsertF32x2(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  136. std::string_view object, u32 index) {
  137. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::F32x2)};
  138. CompositeInsert(ctx, ret, composite, object, index);
  139. }
  140. void EmitCompositeInsertF32x3(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  141. std::string_view object, u32 index) {
  142. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::F32x3)};
  143. CompositeInsert(ctx, ret, composite, object, index);
  144. }
  145. void EmitCompositeInsertF32x4(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
  146. std::string_view object, u32 index) {
  147. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
  148. CompositeInsert(ctx, ret, composite, object, index);
  149. }
  150. void EmitCompositeConstructF64x2([[maybe_unused]] EmitContext& ctx) {
  151. NotImplemented();
  152. }
  153. void EmitCompositeConstructF64x3([[maybe_unused]] EmitContext& ctx) {
  154. NotImplemented();
  155. }
  156. void EmitCompositeConstructF64x4([[maybe_unused]] EmitContext& ctx) {
  157. NotImplemented();
  158. }
  159. void EmitCompositeExtractF64x2([[maybe_unused]] EmitContext& ctx) {
  160. NotImplemented();
  161. }
  162. void EmitCompositeExtractF64x3([[maybe_unused]] EmitContext& ctx) {
  163. NotImplemented();
  164. }
  165. void EmitCompositeExtractF64x4([[maybe_unused]] EmitContext& ctx) {
  166. NotImplemented();
  167. }
  168. void EmitCompositeInsertF64x2(EmitContext& ctx, std::string_view composite, std::string_view object,
  169. u32 index) {
  170. ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);
  171. }
  172. void EmitCompositeInsertF64x3(EmitContext& ctx, std::string_view composite, std::string_view object,
  173. u32 index) {
  174. ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);
  175. }
  176. void EmitCompositeInsertF64x4(EmitContext& ctx, std::string_view composite, std::string_view object,
  177. u32 index) {
  178. ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);
  179. }
  180. } // namespace Shader::Backend::GLSL