emit_glsl_composite.cpp 8.5 KB

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