emit_glasm_context_get_set.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. namespace Shader::Backend::GLASM {
  10. namespace {
  11. void GetCbuf(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset,
  12. std::string_view size) {
  13. if (!binding.IsImmediate()) {
  14. throw NotImplementedException("Indirect constant buffer loading");
  15. }
  16. const Register ret{ctx.reg_alloc.Define(inst)};
  17. ctx.Add("LDC.{} {},c{}[{}];", size, ret, binding.U32(), offset);
  18. }
  19. std::string VertexIndex(EmitContext& ctx, ScalarU32 vertex) {
  20. switch (ctx.stage) {
  21. case Stage::TessellationControl:
  22. case Stage::TessellationEval:
  23. case Stage::Geometry:
  24. return fmt::format("[{}]", vertex);
  25. default:
  26. return "";
  27. }
  28. }
  29. } // Anonymous namespace
  30. void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  31. GetCbuf(ctx, inst, binding, offset, "U8");
  32. }
  33. void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  34. GetCbuf(ctx, inst, binding, offset, "S8");
  35. }
  36. void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  37. GetCbuf(ctx, inst, binding, offset, "U16");
  38. }
  39. void EmitGetCbufS16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  40. GetCbuf(ctx, inst, binding, offset, "S16");
  41. }
  42. void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  43. GetCbuf(ctx, inst, binding, offset, "U32");
  44. }
  45. void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  46. GetCbuf(ctx, inst, binding, offset, "F32");
  47. }
  48. void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  49. ScalarU32 offset) {
  50. GetCbuf(ctx, inst, binding, offset, "U32X2");
  51. }
  52. void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, ScalarU32 vertex) {
  53. const u32 element{static_cast<u32>(attr) % 4};
  54. const char swizzle{"xyzw"[element]};
  55. if (IR::IsGeneric(attr)) {
  56. const u32 index{IR::GenericAttributeIndex(attr)};
  57. ctx.Add("MOV.F {}.x,in_attr{}{}[0].{};", inst, index, VertexIndex(ctx, vertex), swizzle);
  58. return;
  59. }
  60. switch (attr) {
  61. case IR::Attribute::PrimitiveId:
  62. ctx.Add("MOV.S {}.x,primitive.id;", inst);
  63. break;
  64. case IR::Attribute::PositionX:
  65. case IR::Attribute::PositionY:
  66. case IR::Attribute::PositionZ:
  67. case IR::Attribute::PositionW:
  68. if (ctx.stage == Stage::Geometry) {
  69. ctx.Add("MOV.F {}.x,vertex_position{}.{};", inst, VertexIndex(ctx, vertex), swizzle);
  70. } else {
  71. ctx.Add("MOV.F {}.x,{}.position.{};", inst, ctx.attrib_name, swizzle);
  72. }
  73. break;
  74. case IR::Attribute::TessellationEvaluationPointU:
  75. case IR::Attribute::TessellationEvaluationPointV:
  76. ctx.Add("MOV.F {}.x,vertex.tesscoord.{};", inst, swizzle);
  77. break;
  78. case IR::Attribute::PointSpriteS:
  79. case IR::Attribute::PointSpriteT:
  80. ctx.Add("MOV.F {}.x,{}.pointcoord.{};", inst, ctx.attrib_name, swizzle);
  81. break;
  82. case IR::Attribute::InstanceId:
  83. ctx.Add("MOV.S {}.x,{}.instance;", inst, ctx.attrib_name);
  84. break;
  85. case IR::Attribute::VertexId:
  86. ctx.Add("MOV.S {}.x,{}.id;", inst, ctx.attrib_name);
  87. break;
  88. case IR::Attribute::FrontFace:
  89. ctx.Add("CMP.S {}.x,{}.facing.x,0,-1;", inst, ctx.attrib_name);
  90. break;
  91. default:
  92. throw NotImplementedException("Get attribute {}", attr);
  93. }
  94. }
  95. void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, ScalarF32 value,
  96. [[maybe_unused]] ScalarU32 vertex) {
  97. const u32 element{static_cast<u32>(attr) % 4};
  98. const char swizzle{"xyzw"[element]};
  99. if (IR::IsGeneric(attr)) {
  100. const u32 index{IR::GenericAttributeIndex(attr)};
  101. ctx.Add("MOV.F out_attr{}[0].{},{};", index, swizzle, value);
  102. return;
  103. }
  104. switch (attr) {
  105. case IR::Attribute::PointSize:
  106. ctx.Add("MOV.F result.pointsize.x,{};", value);
  107. break;
  108. case IR::Attribute::PositionX:
  109. case IR::Attribute::PositionY:
  110. case IR::Attribute::PositionZ:
  111. case IR::Attribute::PositionW:
  112. ctx.Add("MOV.F result.position.{},{};", swizzle, value);
  113. break;
  114. case IR::Attribute::ClipDistance0:
  115. case IR::Attribute::ClipDistance1:
  116. case IR::Attribute::ClipDistance2:
  117. case IR::Attribute::ClipDistance3:
  118. case IR::Attribute::ClipDistance4:
  119. case IR::Attribute::ClipDistance5:
  120. case IR::Attribute::ClipDistance6:
  121. case IR::Attribute::ClipDistance7: {
  122. const u32 index{static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::ClipDistance0)};
  123. ctx.Add("MOV.F result.clip[{}].x,{};", index, value);
  124. break;
  125. }
  126. case IR::Attribute::ViewportIndex:
  127. if (ctx.stage == Stage::Geometry || ctx.profile.support_viewport_index_layer_non_geometry) {
  128. ctx.Add("MOV.F result.viewport.x,{};", value);
  129. } else {
  130. // LOG_WARNING
  131. }
  132. break;
  133. default:
  134. throw NotImplementedException("Set attribute {}", attr);
  135. }
  136. }
  137. void EmitGetAttributeIndexed([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] ScalarU32 offset,
  138. [[maybe_unused]] ScalarU32 vertex) {
  139. throw NotImplementedException("GLASM instruction");
  140. }
  141. void EmitSetAttributeIndexed([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] ScalarU32 offset,
  142. [[maybe_unused]] ScalarF32 value, [[maybe_unused]] ScalarU32 vertex) {
  143. throw NotImplementedException("GLASM instruction");
  144. }
  145. void EmitGetPatch(EmitContext& ctx, IR::Inst& inst, IR::Patch patch) {
  146. if (!IR::IsGeneric(patch)) {
  147. throw NotImplementedException("Non-generic patch load");
  148. }
  149. const u32 index{IR::GenericPatchIndex(patch)};
  150. const u32 element{IR::GenericPatchElement(patch)};
  151. ctx.Add("MOV.F {},result.patch.attrib[{}].{};", inst, index, "xyzw"[element]);
  152. }
  153. void EmitSetPatch(EmitContext& ctx, IR::Patch patch, ScalarF32 value) {
  154. if (IR::IsGeneric(patch)) {
  155. const u32 index{IR::GenericPatchIndex(patch)};
  156. const u32 element{IR::GenericPatchElement(patch)};
  157. ctx.Add("MOV.F result.patch.attrib[{}].{},{};", index, "xyzw"[element], value);
  158. return;
  159. }
  160. switch (patch) {
  161. case IR::Patch::TessellationLodLeft:
  162. case IR::Patch::TessellationLodRight:
  163. case IR::Patch::TessellationLodTop:
  164. case IR::Patch::TessellationLodBottom: {
  165. const u32 index{static_cast<u32>(patch) - u32(IR::Patch::TessellationLodLeft)};
  166. ctx.Add("MOV.F result.patch.tessouter[{}].x,{};", index, value);
  167. break;
  168. }
  169. case IR::Patch::TessellationLodInteriorU:
  170. ctx.Add("MOV.F result.patch.tessinner[0].x,{};", value);
  171. break;
  172. case IR::Patch::TessellationLodInteriorV:
  173. ctx.Add("MOV.F result.patch.tessinner[1].x,{};", value);
  174. break;
  175. default:
  176. throw NotImplementedException("Patch {}", patch);
  177. }
  178. }
  179. void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, ScalarF32 value) {
  180. ctx.Add("MOV.F frag_color{}.{},{};", index, "xyzw"[component], value);
  181. }
  182. void EmitSetSampleMask(EmitContext& ctx, ScalarS32 value) {
  183. ctx.Add("MOV.S result.samplemask.x,{};", value);
  184. }
  185. void EmitSetFragDepth(EmitContext& ctx, ScalarF32 value) {
  186. ctx.Add("MOV.F result.depth.z,{};", value);
  187. }
  188. void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset) {
  189. ctx.Add("MOV.U {},lmem[{}].x;", inst, word_offset);
  190. }
  191. void EmitWriteLocal(EmitContext& ctx, ScalarU32 word_offset, ScalarU32 value) {
  192. ctx.Add("MOV.U lmem[{}].x,{};", word_offset, value);
  193. }
  194. } // namespace Shader::Backend::GLASM