emit_glasm_context_get_set.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/glasm/emit_context.h"
  6. #include "shader_recompiler/backend/glasm/emit_glasm_instructions.h"
  7. #include "shader_recompiler/frontend/ir/value.h"
  8. #include "shader_recompiler/profile.h"
  9. #include "shader_recompiler/shader_info.h"
  10. namespace Shader::Backend::GLASM {
  11. namespace {
  12. void GetCbuf(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset,
  13. std::string_view size) {
  14. if (!binding.IsImmediate()) {
  15. throw NotImplementedException("Indirect constant buffer loading");
  16. }
  17. const Register ret{ctx.reg_alloc.Define(inst)};
  18. ctx.Add("LDC.{} {},c{}[{}];", size, ret, binding.U32(), offset);
  19. }
  20. bool IsInputArray(Stage stage) {
  21. return stage == Stage::Geometry || stage == Stage::TessellationControl ||
  22. stage == Stage::TessellationEval;
  23. }
  24. std::string VertexIndex(EmitContext& ctx, ScalarU32 vertex) {
  25. return IsInputArray(ctx.stage) ? fmt::format("[{}]", vertex) : "";
  26. }
  27. } // Anonymous namespace
  28. void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  29. GetCbuf(ctx, inst, binding, offset, "U8");
  30. }
  31. void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  32. GetCbuf(ctx, inst, binding, offset, "S8");
  33. }
  34. void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  35. GetCbuf(ctx, inst, binding, offset, "U16");
  36. }
  37. void EmitGetCbufS16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  38. GetCbuf(ctx, inst, binding, offset, "S16");
  39. }
  40. void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  41. GetCbuf(ctx, inst, binding, offset, "U32");
  42. }
  43. void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  44. GetCbuf(ctx, inst, binding, offset, "F32");
  45. }
  46. void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  47. ScalarU32 offset) {
  48. GetCbuf(ctx, inst, binding, offset, "U32X2");
  49. }
  50. void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, ScalarU32 vertex) {
  51. const u32 element{static_cast<u32>(attr) % 4};
  52. const char swizzle{"xyzw"[element]};
  53. if (IR::IsGeneric(attr)) {
  54. const u32 index{IR::GenericAttributeIndex(attr)};
  55. ctx.Add("MOV.F {}.x,in_attr{}{}[0].{};", inst, index, VertexIndex(ctx, vertex), swizzle);
  56. return;
  57. }
  58. switch (attr) {
  59. case IR::Attribute::PrimitiveId:
  60. ctx.Add("MOV.S {}.x,primitive.id;", inst);
  61. break;
  62. case IR::Attribute::PositionX:
  63. case IR::Attribute::PositionY:
  64. case IR::Attribute::PositionZ:
  65. case IR::Attribute::PositionW:
  66. if (IsInputArray(ctx.stage)) {
  67. ctx.Add("MOV.F {}.x,vertex_position{}.{};", inst, VertexIndex(ctx, vertex), swizzle);
  68. } else {
  69. ctx.Add("MOV.F {}.x,{}.position.{};", inst, ctx.attrib_name, swizzle);
  70. }
  71. break;
  72. case IR::Attribute::TessellationEvaluationPointU:
  73. case IR::Attribute::TessellationEvaluationPointV:
  74. ctx.Add("MOV.F {}.x,vertex.tesscoord.{};", inst, swizzle);
  75. break;
  76. case IR::Attribute::PointSpriteS:
  77. case IR::Attribute::PointSpriteT:
  78. ctx.Add("MOV.F {}.x,{}.pointcoord.{};", inst, ctx.attrib_name, swizzle);
  79. break;
  80. case IR::Attribute::InstanceId:
  81. ctx.Add("MOV.S {}.x,{}.instance;", inst, ctx.attrib_name);
  82. break;
  83. case IR::Attribute::VertexId:
  84. ctx.Add("MOV.S {}.x,{}.id;", inst, ctx.attrib_name);
  85. break;
  86. case IR::Attribute::FrontFace:
  87. ctx.Add("CMP.S {}.x,{}.facing.x,0,-1;", inst, ctx.attrib_name);
  88. break;
  89. default:
  90. throw NotImplementedException("Get attribute {}", attr);
  91. }
  92. }
  93. void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, ScalarF32 value,
  94. [[maybe_unused]] ScalarU32 vertex) {
  95. const u32 element{static_cast<u32>(attr) % 4};
  96. const char swizzle{"xyzw"[element]};
  97. if (IR::IsGeneric(attr)) {
  98. const u32 index{IR::GenericAttributeIndex(attr)};
  99. ctx.Add("MOV.F out_attr{}[0].{},{};", index, swizzle, value);
  100. return;
  101. }
  102. switch (attr) {
  103. case IR::Attribute::PointSize:
  104. ctx.Add("MOV.F result.pointsize.x,{};", value);
  105. break;
  106. case IR::Attribute::PositionX:
  107. case IR::Attribute::PositionY:
  108. case IR::Attribute::PositionZ:
  109. case IR::Attribute::PositionW:
  110. ctx.Add("MOV.F result.position.{},{};", swizzle, value);
  111. break;
  112. case IR::Attribute::ClipDistance0:
  113. case IR::Attribute::ClipDistance1:
  114. case IR::Attribute::ClipDistance2:
  115. case IR::Attribute::ClipDistance3:
  116. case IR::Attribute::ClipDistance4:
  117. case IR::Attribute::ClipDistance5:
  118. case IR::Attribute::ClipDistance6:
  119. case IR::Attribute::ClipDistance7: {
  120. const u32 index{static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::ClipDistance0)};
  121. ctx.Add("MOV.F result.clip[{}].x,{};", index, value);
  122. break;
  123. }
  124. case IR::Attribute::Layer:
  125. if (ctx.stage == Stage::Geometry || ctx.profile.support_viewport_index_layer_non_geometry) {
  126. ctx.Add("MOV.F result.layer.x,{};", value);
  127. } else {
  128. // LOG_WARNING
  129. }
  130. break;
  131. case IR::Attribute::ViewportIndex:
  132. if (ctx.stage == Stage::Geometry || ctx.profile.support_viewport_index_layer_non_geometry) {
  133. ctx.Add("MOV.F result.viewport.x,{};", value);
  134. } else {
  135. // LOG_WARNING
  136. }
  137. break;
  138. default:
  139. throw NotImplementedException("Set attribute {}", attr);
  140. }
  141. }
  142. void EmitGetAttributeIndexed(EmitContext& ctx, IR::Inst& inst, ScalarS32 offset, ScalarU32 vertex) {
  143. // RC.x = base_index
  144. // RC.y = masked_index
  145. // RC.z = compare_index
  146. ctx.Add("SHR.S RC.x,{},2;"
  147. "AND.S RC.y,RC.x,3;"
  148. "SHR.S RC.z,{},4;",
  149. offset, offset);
  150. const Register ret{ctx.reg_alloc.Define(inst)};
  151. u32 num_endifs{};
  152. const auto read{[&](u32 compare_index, const std::array<std::string, 4>& values) {
  153. ++num_endifs;
  154. ctx.Add("SEQ.S.CC RC.w,RC.z,{};" // compare_index
  155. "IF NE.w;"
  156. // X
  157. "SEQ.S.CC RC.w,RC.y,0;"
  158. "IF NE.w;"
  159. "MOV {}.x,{};"
  160. "ELSE;"
  161. // Y
  162. "SEQ.S.CC RC.w,RC.y,1;"
  163. "IF NE.w;"
  164. "MOV {}.x,{};"
  165. "ELSE;"
  166. // Z
  167. "SEQ.S.CC RC.w,RC.y,2;"
  168. "IF NE.w;"
  169. "MOV {}.x,{};"
  170. "ELSE;"
  171. // W
  172. "MOV {}.x,{};"
  173. "ENDIF;"
  174. "ENDIF;"
  175. "ENDIF;"
  176. "ELSE;",
  177. compare_index, ret, values[0], ret, values[1], ret, values[2], ret, values[3]);
  178. }};
  179. const auto read_swizzled{[&](u32 compare_index, std::string_view value) {
  180. const std::array values{fmt::format("{}.x", value), fmt::format("{}.y", value),
  181. fmt::format("{}.z", value), fmt::format("{}.w", value)};
  182. read(compare_index, values);
  183. }};
  184. if (ctx.info.loads_position) {
  185. const u32 index{static_cast<u32>(IR::Attribute::PositionX)};
  186. if (IsInputArray(ctx.stage)) {
  187. read_swizzled(index, fmt::format("vertex_position{}", VertexIndex(ctx, vertex)));
  188. } else {
  189. read_swizzled(index, fmt::format("{}.position", ctx.attrib_name));
  190. }
  191. }
  192. const u32 base_attribute_value{static_cast<u32>(IR::Attribute::Generic0X) >> 2};
  193. for (u32 index = 0; index < ctx.info.input_generics.size(); ++index) {
  194. if (!ctx.info.input_generics[index].used) {
  195. continue;
  196. }
  197. read_swizzled(index, fmt::format("in_attr{}{}[0]", index, VertexIndex(ctx, vertex)));
  198. }
  199. for (u32 i = 0; i < num_endifs; ++i) {
  200. ctx.Add("ENDIF;");
  201. }
  202. }
  203. void EmitSetAttributeIndexed([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] ScalarU32 offset,
  204. [[maybe_unused]] ScalarF32 value, [[maybe_unused]] ScalarU32 vertex) {
  205. throw NotImplementedException("GLASM instruction");
  206. }
  207. void EmitGetPatch(EmitContext& ctx, IR::Inst& inst, IR::Patch patch) {
  208. if (!IR::IsGeneric(patch)) {
  209. throw NotImplementedException("Non-generic patch load");
  210. }
  211. const u32 index{IR::GenericPatchIndex(patch)};
  212. const u32 element{IR::GenericPatchElement(patch)};
  213. const char swizzle{"xyzw"[element]};
  214. const std::string_view out{ctx.stage == Stage::TessellationControl ? ".out" : ""};
  215. ctx.Add("MOV.F {},primitive{}.patch.attrib[{}].{};", inst, out, index, swizzle);
  216. }
  217. void EmitSetPatch(EmitContext& ctx, IR::Patch patch, ScalarF32 value) {
  218. if (IR::IsGeneric(patch)) {
  219. const u32 index{IR::GenericPatchIndex(patch)};
  220. const u32 element{IR::GenericPatchElement(patch)};
  221. ctx.Add("MOV.F result.patch.attrib[{}].{},{};", index, "xyzw"[element], value);
  222. return;
  223. }
  224. switch (patch) {
  225. case IR::Patch::TessellationLodLeft:
  226. case IR::Patch::TessellationLodRight:
  227. case IR::Patch::TessellationLodTop:
  228. case IR::Patch::TessellationLodBottom: {
  229. const u32 index{static_cast<u32>(patch) - u32(IR::Patch::TessellationLodLeft)};
  230. ctx.Add("MOV.F result.patch.tessouter[{}].x,{};", index, value);
  231. break;
  232. }
  233. case IR::Patch::TessellationLodInteriorU:
  234. ctx.Add("MOV.F result.patch.tessinner[0].x,{};", value);
  235. break;
  236. case IR::Patch::TessellationLodInteriorV:
  237. ctx.Add("MOV.F result.patch.tessinner[1].x,{};", value);
  238. break;
  239. default:
  240. throw NotImplementedException("Patch {}", patch);
  241. }
  242. }
  243. void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, ScalarF32 value) {
  244. ctx.Add("MOV.F frag_color{}.{},{};", index, "xyzw"[component], value);
  245. }
  246. void EmitSetSampleMask(EmitContext& ctx, ScalarS32 value) {
  247. ctx.Add("MOV.S result.samplemask.x,{};", value);
  248. }
  249. void EmitSetFragDepth(EmitContext& ctx, ScalarF32 value) {
  250. ctx.Add("MOV.F result.depth.z,{};", value);
  251. }
  252. void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset) {
  253. ctx.Add("MOV.U {},lmem[{}].x;", inst, word_offset);
  254. }
  255. void EmitWriteLocal(EmitContext& ctx, ScalarU32 word_offset, ScalarU32 value) {
  256. ctx.Add("MOV.U lmem[{}].x,{};", word_offset, value);
  257. }
  258. } // namespace Shader::Backend::GLASM