emit_glasm_context_get_set.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <string_view>
  4. #include "shader_recompiler/backend/glasm/emit_glasm_instructions.h"
  5. #include "shader_recompiler/backend/glasm/glasm_emit_context.h"
  6. #include "shader_recompiler/frontend/ir/value.h"
  7. #include "shader_recompiler/profile.h"
  8. #include "shader_recompiler/shader_info.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. const Register ret{ctx.reg_alloc.Define(inst)};
  14. if (offset.type == Type::U32) {
  15. // Avoid reading arrays out of bounds, matching hardware's behavior
  16. if (offset.imm_u32 >= 0x10'000) {
  17. ctx.Add("MOV.S {},0;", ret);
  18. return;
  19. }
  20. }
  21. if (binding.IsImmediate()) {
  22. ctx.Add("LDC.{} {},c{}[{}];", size, ret, binding.U32(), offset);
  23. return;
  24. }
  25. const ScalarU32 idx{ctx.reg_alloc.Consume(binding)};
  26. for (u32 i = 0; i < Info::MAX_INDIRECT_CBUFS; i++) {
  27. ctx.Add("SEQ.S.CC RC.x,{},{};"
  28. "IF NE.x;"
  29. "LDC.{} {},c{}[{}];",
  30. idx, i, size, ret, i, offset);
  31. if (i != Info::MAX_INDIRECT_CBUFS - 1) {
  32. ctx.Add("ELSE;");
  33. }
  34. }
  35. for (u32 i = 0; i < Info::MAX_INDIRECT_CBUFS; i++) {
  36. ctx.Add("ENDIF;");
  37. }
  38. }
  39. bool IsInputArray(Stage stage) {
  40. return stage == Stage::Geometry || stage == Stage::TessellationControl ||
  41. stage == Stage::TessellationEval;
  42. }
  43. std::string VertexIndex(EmitContext& ctx, ScalarU32 vertex) {
  44. return IsInputArray(ctx.stage) ? fmt::format("[{}]", vertex) : "";
  45. }
  46. u32 TexCoordIndex(IR::Attribute attr) {
  47. return (static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
  48. }
  49. } // Anonymous namespace
  50. void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  51. GetCbuf(ctx, inst, binding, offset, "U8");
  52. }
  53. void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  54. GetCbuf(ctx, inst, binding, offset, "S8");
  55. }
  56. void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  57. GetCbuf(ctx, inst, binding, offset, "U16");
  58. }
  59. void EmitGetCbufS16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  60. GetCbuf(ctx, inst, binding, offset, "S16");
  61. }
  62. void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  63. GetCbuf(ctx, inst, binding, offset, "U32");
  64. }
  65. void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  66. GetCbuf(ctx, inst, binding, offset, "F32");
  67. }
  68. void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  69. ScalarU32 offset) {
  70. GetCbuf(ctx, inst, binding, offset, "U32X2");
  71. }
  72. void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, ScalarU32 vertex) {
  73. const u32 element{static_cast<u32>(attr) % 4};
  74. const char swizzle{"xyzw"[element]};
  75. if (IR::IsGeneric(attr)) {
  76. const u32 index{IR::GenericAttributeIndex(attr)};
  77. ctx.Add("MOV.F {}.x,in_attr{}{}[0].{};", inst, index, VertexIndex(ctx, vertex), swizzle);
  78. return;
  79. }
  80. if (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture9Q) {
  81. const u32 index{TexCoordIndex(attr)};
  82. ctx.Add("MOV.F {}.x,{}.texcoord[{}].{};", inst, ctx.attrib_name, index, swizzle);
  83. return;
  84. }
  85. switch (attr) {
  86. case IR::Attribute::PrimitiveId:
  87. ctx.Add("MOV.F {}.x,primitive.id;", inst);
  88. break;
  89. case IR::Attribute::PositionX:
  90. case IR::Attribute::PositionY:
  91. case IR::Attribute::PositionZ:
  92. case IR::Attribute::PositionW:
  93. if (IsInputArray(ctx.stage)) {
  94. ctx.Add("MOV.F {}.x,vertex_position{}.{};", inst, VertexIndex(ctx, vertex), swizzle);
  95. } else {
  96. ctx.Add("MOV.F {}.x,{}.position.{};", inst, ctx.attrib_name, swizzle);
  97. }
  98. break;
  99. case IR::Attribute::ColorFrontDiffuseR:
  100. case IR::Attribute::ColorFrontDiffuseG:
  101. case IR::Attribute::ColorFrontDiffuseB:
  102. case IR::Attribute::ColorFrontDiffuseA:
  103. ctx.Add("MOV.F {}.x,{}.color.{};", inst, ctx.attrib_name, swizzle);
  104. break;
  105. case IR::Attribute::PointSpriteS:
  106. case IR::Attribute::PointSpriteT:
  107. ctx.Add("MOV.F {}.x,{}.pointcoord.{};", inst, ctx.attrib_name, swizzle);
  108. break;
  109. case IR::Attribute::TessellationEvaluationPointU:
  110. case IR::Attribute::TessellationEvaluationPointV:
  111. ctx.Add("MOV.F {}.x,vertex.tesscoord.{};", inst, swizzle);
  112. break;
  113. case IR::Attribute::InstanceId:
  114. ctx.Add("MOV.F {}.x,{}.instance;", inst, ctx.attrib_name);
  115. break;
  116. case IR::Attribute::VertexId:
  117. ctx.Add("MOV.F {}.x,{}.id;", inst, ctx.attrib_name);
  118. break;
  119. case IR::Attribute::FrontFace:
  120. ctx.Add("CMP.F {}.x,{}.facing.x,0,-1;", inst, ctx.attrib_name);
  121. break;
  122. default:
  123. throw NotImplementedException("Get attribute {}", attr);
  124. }
  125. }
  126. void EmitGetAttributeU32(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, ScalarU32) {
  127. switch (attr) {
  128. case IR::Attribute::PrimitiveId:
  129. ctx.Add("MOV.S {}.x,primitive.id;", inst);
  130. break;
  131. case IR::Attribute::InstanceId:
  132. ctx.Add("MOV.S {}.x,{}.instance;", inst, ctx.attrib_name);
  133. break;
  134. case IR::Attribute::VertexId:
  135. ctx.Add("MOV.S {}.x,{}.id;", inst, ctx.attrib_name);
  136. break;
  137. default:
  138. throw NotImplementedException("Get U32 attribute {}", attr);
  139. }
  140. }
  141. void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, ScalarF32 value,
  142. [[maybe_unused]] ScalarU32 vertex) {
  143. const u32 element{static_cast<u32>(attr) % 4};
  144. const char swizzle{"xyzw"[element]};
  145. if (IR::IsGeneric(attr)) {
  146. const u32 index{IR::GenericAttributeIndex(attr)};
  147. ctx.Add("MOV.F out_attr{}[0].{},{};", index, swizzle, value);
  148. return;
  149. }
  150. if (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture9R) {
  151. const u32 index{TexCoordIndex(attr)};
  152. ctx.Add("MOV.F result.texcoord[{}].{},{};", index, swizzle, value);
  153. return;
  154. }
  155. switch (attr) {
  156. case IR::Attribute::Layer:
  157. if (ctx.stage == Stage::Geometry || ctx.profile.support_viewport_index_layer_non_geometry) {
  158. ctx.Add("MOV.F result.layer.x,{};", value);
  159. } else {
  160. LOG_WARNING(Shader_GLASM,
  161. "Layer stored outside of geometry shader not supported by device");
  162. }
  163. break;
  164. case IR::Attribute::ViewportIndex:
  165. if (ctx.stage == Stage::Geometry || ctx.profile.support_viewport_index_layer_non_geometry) {
  166. ctx.Add("MOV.F result.viewport.x,{};", value);
  167. } else {
  168. LOG_WARNING(Shader_GLASM,
  169. "Viewport stored outside of geometry shader not supported by device");
  170. }
  171. break;
  172. case IR::Attribute::ViewportMask:
  173. // NV_viewport_array2 is required to access result.viewportmask, regardless of shader stage.
  174. if (ctx.profile.support_viewport_index_layer_non_geometry) {
  175. ctx.Add("MOV.F result.viewportmask[0].x,{};", value);
  176. } else {
  177. LOG_WARNING(Shader_GLASM, "Device does not support storing to ViewportMask");
  178. }
  179. break;
  180. case IR::Attribute::PointSize:
  181. ctx.Add("MOV.F result.pointsize.x,{};", value);
  182. break;
  183. case IR::Attribute::PositionX:
  184. case IR::Attribute::PositionY:
  185. case IR::Attribute::PositionZ:
  186. case IR::Attribute::PositionW:
  187. ctx.Add("MOV.F result.position.{},{};", swizzle, value);
  188. break;
  189. case IR::Attribute::ColorFrontDiffuseR:
  190. case IR::Attribute::ColorFrontDiffuseG:
  191. case IR::Attribute::ColorFrontDiffuseB:
  192. case IR::Attribute::ColorFrontDiffuseA:
  193. ctx.Add("MOV.F result.color.{},{};", swizzle, value);
  194. break;
  195. case IR::Attribute::ColorFrontSpecularR:
  196. case IR::Attribute::ColorFrontSpecularG:
  197. case IR::Attribute::ColorFrontSpecularB:
  198. case IR::Attribute::ColorFrontSpecularA:
  199. ctx.Add("MOV.F result.color.secondary.{},{};", swizzle, value);
  200. break;
  201. case IR::Attribute::ColorBackDiffuseR:
  202. case IR::Attribute::ColorBackDiffuseG:
  203. case IR::Attribute::ColorBackDiffuseB:
  204. case IR::Attribute::ColorBackDiffuseA:
  205. ctx.Add("MOV.F result.color.back.{},{};", swizzle, value);
  206. break;
  207. case IR::Attribute::ColorBackSpecularR:
  208. case IR::Attribute::ColorBackSpecularG:
  209. case IR::Attribute::ColorBackSpecularB:
  210. case IR::Attribute::ColorBackSpecularA:
  211. ctx.Add("MOV.F result.color.back.secondary.{},{};", swizzle, value);
  212. break;
  213. case IR::Attribute::FogCoordinate:
  214. ctx.Add("MOV.F result.fogcoord.x,{};", value);
  215. break;
  216. case IR::Attribute::ClipDistance0:
  217. case IR::Attribute::ClipDistance1:
  218. case IR::Attribute::ClipDistance2:
  219. case IR::Attribute::ClipDistance3:
  220. case IR::Attribute::ClipDistance4:
  221. case IR::Attribute::ClipDistance5:
  222. case IR::Attribute::ClipDistance6:
  223. case IR::Attribute::ClipDistance7: {
  224. const u32 index{static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::ClipDistance0)};
  225. ctx.Add("MOV.F result.clip[{}].x,{};", index, value);
  226. break;
  227. }
  228. default:
  229. throw NotImplementedException("Set attribute {}", attr);
  230. }
  231. }
  232. void EmitGetAttributeIndexed(EmitContext& ctx, IR::Inst& inst, ScalarS32 offset, ScalarU32 vertex) {
  233. // RC.x = base_index
  234. // RC.y = masked_index
  235. // RC.z = compare_index
  236. ctx.Add("SHR.S RC.x,{},2;"
  237. "AND.S RC.y,RC.x,3;"
  238. "SHR.S RC.z,{},4;",
  239. offset, offset);
  240. const Register ret{ctx.reg_alloc.Define(inst)};
  241. u32 num_endifs{};
  242. const auto read{[&](u32 compare_index, const std::array<std::string, 4>& values) {
  243. ++num_endifs;
  244. ctx.Add("SEQ.S.CC RC.w,RC.z,{};" // compare_index
  245. "IF NE.w;"
  246. // X
  247. "SEQ.S.CC RC.w,RC.y,0;"
  248. "IF NE.w;"
  249. "MOV {}.x,{};"
  250. "ELSE;"
  251. // Y
  252. "SEQ.S.CC RC.w,RC.y,1;"
  253. "IF NE.w;"
  254. "MOV {}.x,{};"
  255. "ELSE;"
  256. // Z
  257. "SEQ.S.CC RC.w,RC.y,2;"
  258. "IF NE.w;"
  259. "MOV {}.x,{};"
  260. "ELSE;"
  261. // W
  262. "MOV {}.x,{};"
  263. "ENDIF;"
  264. "ENDIF;"
  265. "ENDIF;"
  266. "ELSE;",
  267. compare_index, ret, values[0], ret, values[1], ret, values[2], ret, values[3]);
  268. }};
  269. const auto read_swizzled{[&](u32 compare_index, std::string_view value) {
  270. const std::array values{fmt::format("{}.x", value), fmt::format("{}.y", value),
  271. fmt::format("{}.z", value), fmt::format("{}.w", value)};
  272. read(compare_index, values);
  273. }};
  274. if (ctx.info.loads.AnyComponent(IR::Attribute::PositionX)) {
  275. const u32 index{static_cast<u32>(IR::Attribute::PositionX)};
  276. if (IsInputArray(ctx.stage)) {
  277. read_swizzled(index, fmt::format("vertex_position{}", VertexIndex(ctx, vertex)));
  278. } else {
  279. read_swizzled(index, fmt::format("{}.position", ctx.attrib_name));
  280. }
  281. }
  282. for (u32 index = 0; index < static_cast<u32>(IR::NUM_GENERICS); ++index) {
  283. if (!ctx.info.loads.Generic(index)) {
  284. continue;
  285. }
  286. read_swizzled(index, fmt::format("in_attr{}{}[0]", index, VertexIndex(ctx, vertex)));
  287. }
  288. for (u32 i = 0; i < num_endifs; ++i) {
  289. ctx.Add("ENDIF;");
  290. }
  291. }
  292. void EmitSetAttributeIndexed([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] ScalarU32 offset,
  293. [[maybe_unused]] ScalarF32 value, [[maybe_unused]] ScalarU32 vertex) {
  294. throw NotImplementedException("GLASM instruction");
  295. }
  296. void EmitGetPatch(EmitContext& ctx, IR::Inst& inst, IR::Patch patch) {
  297. if (!IR::IsGeneric(patch)) {
  298. throw NotImplementedException("Non-generic patch load");
  299. }
  300. const u32 index{IR::GenericPatchIndex(patch)};
  301. const u32 element{IR::GenericPatchElement(patch)};
  302. const char swizzle{"xyzw"[element]};
  303. const std::string_view out{ctx.stage == Stage::TessellationControl ? ".out" : ""};
  304. ctx.Add("MOV.F {},primitive{}.patch.attrib[{}].{};", inst, out, index, swizzle);
  305. }
  306. void EmitSetPatch(EmitContext& ctx, IR::Patch patch, ScalarF32 value) {
  307. if (IR::IsGeneric(patch)) {
  308. const u32 index{IR::GenericPatchIndex(patch)};
  309. const u32 element{IR::GenericPatchElement(patch)};
  310. ctx.Add("MOV.F result.patch.attrib[{}].{},{};", index, "xyzw"[element], value);
  311. return;
  312. }
  313. switch (patch) {
  314. case IR::Patch::TessellationLodLeft:
  315. case IR::Patch::TessellationLodRight:
  316. case IR::Patch::TessellationLodTop:
  317. case IR::Patch::TessellationLodBottom: {
  318. const u32 index{static_cast<u32>(patch) - u32(IR::Patch::TessellationLodLeft)};
  319. ctx.Add("MOV.F result.patch.tessouter[{}].x,{};", index, value);
  320. break;
  321. }
  322. case IR::Patch::TessellationLodInteriorU:
  323. ctx.Add("MOV.F result.patch.tessinner[0].x,{};", value);
  324. break;
  325. case IR::Patch::TessellationLodInteriorV:
  326. ctx.Add("MOV.F result.patch.tessinner[1].x,{};", value);
  327. break;
  328. default:
  329. throw NotImplementedException("Patch {}", patch);
  330. }
  331. }
  332. void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, ScalarF32 value) {
  333. ctx.Add("MOV.F frag_color{}.{},{};", index, "xyzw"[component], value);
  334. }
  335. void EmitSetSampleMask(EmitContext& ctx, ScalarS32 value) {
  336. ctx.Add("MOV.S result.samplemask.x,{};", value);
  337. }
  338. void EmitSetFragDepth(EmitContext& ctx, ScalarF32 value) {
  339. ctx.Add("MOV.F result.depth.z,{};", value);
  340. }
  341. void EmitWorkgroupId(EmitContext& ctx, IR::Inst& inst) {
  342. ctx.Add("MOV.S {},invocation.groupid;", inst);
  343. }
  344. void EmitLocalInvocationId(EmitContext& ctx, IR::Inst& inst) {
  345. ctx.Add("MOV.S {},invocation.localid;", inst);
  346. }
  347. void EmitInvocationId(EmitContext& ctx, IR::Inst& inst) {
  348. ctx.Add("MOV.S {}.x,primitive_invocation.x;", inst);
  349. }
  350. void EmitInvocationInfo(EmitContext& ctx, IR::Inst& inst) {
  351. switch (ctx.stage) {
  352. case Stage::TessellationControl:
  353. case Stage::TessellationEval:
  354. ctx.Add("SHL.U {}.x,primitive.vertexcount,16;", inst);
  355. break;
  356. default:
  357. LOG_WARNING(Shader, "(STUBBED) called");
  358. ctx.Add("MOV.S {}.x,0x00ff0000;", inst);
  359. }
  360. }
  361. void EmitSampleId(EmitContext& ctx, IR::Inst& inst) {
  362. ctx.Add("MOV.S {}.x,fragment.sampleid.x;", inst);
  363. }
  364. void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst) {
  365. ctx.Add("MOV.S {}.x,fragment.helperthread.x;", inst);
  366. }
  367. void EmitYDirection(EmitContext& ctx, IR::Inst& inst) {
  368. ctx.uses_y_direction = true;
  369. ctx.Add("MOV.F {}.x,y_direction[0].w;", inst);
  370. }
  371. void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst) {
  372. ctx.Add("MOV.F {}.x,scaling[0].z;", inst);
  373. }
  374. void EmitRenderArea(EmitContext& ctx, IR::Inst& inst) {
  375. ctx.Add("MOV.F {},render_area[0];", inst);
  376. }
  377. void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset) {
  378. ctx.Add("MOV.U {},lmem[{}].x;", inst, word_offset);
  379. }
  380. void EmitWriteLocal(EmitContext& ctx, ScalarU32 word_offset, ScalarU32 value) {
  381. ctx.Add("MOV.U lmem[{}].x,{};", word_offset, value);
  382. }
  383. } // namespace Shader::Backend::GLASM